未验证 提交 3ae700ec 编写于 作者: M Michael Weiß 提交者: Christian Brauner

fs/proc: apply the time namespace offset to /proc/stat btime

'/proc/stat' provides the field 'btime' which states the time stamp of
system boot in seconds. In case of time namespaces, the offset to the
boot time stamp was not applied earlier.
This confuses tasks which are in another time universe, e.g., in a
container of a container runtime which utilize time namespaces to
virtualize boottime.

Therefore, we make procfs to virtualize also the btime field by
subtracting the offset of the timens boottime from 'btime' before
printing the stats.

Since start_boottime of processes are seconds since boottime and the
boottime stamp is now shifted according to the timens offset, the
offset of the time namespace also needs to be applied before the
process stats are given to userspace.

This avoids that processes shown, e.g., by 'ps' appear as time
travelers in the corresponding time namespace.
Signed-off-by: NMichael Weiß <michael.weiss@aisec.fraunhofer.de>
Reviewed-by: NAndrei Vagin <avagin@gmail.com>
Acked-by: NThomas Gleixner <tglx@linutronix.de>
Acked-by: NChristian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/r/20201027204258.7869-3-michael.weiss@aisec.fraunhofer.de
上级 31909e33
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
#include <linux/types.h> #include <linux/types.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/time.h> #include <linux/time.h>
#include <linux/time_namespace.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/kernel_stat.h> #include <linux/kernel_stat.h>
#include <linux/tty.h> #include <linux/tty.h>
...@@ -533,8 +534,9 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns, ...@@ -533,8 +534,9 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
priority = task_prio(task); priority = task_prio(task);
nice = task_nice(task); nice = task_nice(task);
/* convert nsec -> ticks */ /* apply timens offset for boottime and convert nsec -> ticks */
start_time = nsec_to_clock_t(task->start_boottime); start_time =
nsec_to_clock_t(timens_add_boottime_ns(task->start_boottime));
seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns)); seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
seq_puts(m, " ("); seq_puts(m, " (");
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <linux/seq_file.h> #include <linux/seq_file.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/time.h> #include <linux/time.h>
#include <linux/time_namespace.h>
#include <linux/irqnr.h> #include <linux/irqnr.h>
#include <linux/sched/cputime.h> #include <linux/sched/cputime.h>
#include <linux/tick.h> #include <linux/tick.h>
...@@ -118,6 +119,8 @@ static int show_stat(struct seq_file *p, void *v) ...@@ -118,6 +119,8 @@ static int show_stat(struct seq_file *p, void *v)
irq = softirq = steal = 0; irq = softirq = steal = 0;
guest = guest_nice = 0; guest = guest_nice = 0;
getboottime64(&boottime); getboottime64(&boottime);
/* shift boot timestamp according to the timens offset */
timens_sub_boottime(&boottime);
for_each_possible_cpu(i) { for_each_possible_cpu(i) {
struct kernel_cpustat kcpustat; struct kernel_cpustat kcpustat;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册