cputime: Generic virtual based cputime accounting
cover cputime: Generic virtual based cputime accounting
So,
This is a proposition to handle the cputime accounting without the tick to prepare for stopping the tick further idle.
I have managed to reuse and generalize the kernel/user boundary hooks used by RCU. This way we can minimize the changes in archs that don't support virtual cputime and we can also switch between vtime and tick based accounting to minimize the overhead depending on the tick behaviour.
This is based on top of rcu/idle branch in Paul's tree and my vtime consolidation series: https://lkml.org/lkml/2012/6/20/762
(still waiting to be picked btw).
You, adventurer, can pull from:
git://github.com/fweisbec/linux-dynticks.git
vtime/generic
Frederic Weisbecker (5):
user_hooks: New user hooks subsystem
cputime: Don't allow virtual and irq finegrained cputime accounting
simultaneously
cputime: Allow dynamic switch between tick/virtual based cputime
accounting
cputime: Rename account_system_vtime to account_vtime
cputime: Generic on-demand virtual cputime accounting
arch/Kconfig | 10 +-
arch/ia64/include/asm/cputime.h | 5 +
arch/ia64/kernel/time.c | 6 +-
arch/powerpc/include/asm/cputime.h | 5 +
arch/powerpc/kernel/time.c | 10 +-
arch/s390/include/asm/cputime.h | 5 +
arch/s390/kernel/vtime.c | 6 +-
arch/x86/Kconfig | 2 +-
arch/x86/include/asm/{rcu.h => user_hooks.h} | 12 +-
arch/x86/kernel/ptrace.c | 6 +-
arch/x86/kernel/signal.c | 5 +-
arch/x86/kernel/traps.c | 2 +-
arch/x86/mm/fault.c | 2 +-
include/asm-generic/cputime.h | 7 +
include/linux/hardirq.h | 8 +-
include/linux/kernel_stat.h | 13 ++
include/linux/kvm_host.h | 4 +-
include/linux/rcupdate.h | 2 -
include/linux/sched.h | 13 +--
include/linux/user_hooks.h | 36 ++++++
init/Kconfig | 35 ++++--
kernel/Makefile | 1 +
kernel/fork.c | 3 +-
kernel/rcutree.c | 42 +-------
kernel/sched/core.c | 9 +-
kernel/sched/cputime.c | 159 +++++++++++++++++++++----
kernel/softirq.c | 6 +-
kernel/time/tick-sched.c | 28 ++---
kernel/user_hooks.c | 56 +++++++++
29 files changed, 348 insertions(+), 150 deletions(-)
rename arch/x86/include/asm/{rcu.h => user_hooks.h} (56%)
create mode 100644 include/linux/user_hooks.h
create mode 100644 kernel/user_hooks.cSignature · 1 line
1.7.5.4
PATCH 1/5 user_hooks: New user hooks subsystem
Create a new subsystem that handles the hooks on kernel/user boundaries currently used by RCU for its userspace extended quiescent state.
We need to pull this up from RCU into this new level of indirection because these hooks are also going to be used to implement an "on demand" generic virtual cputime accounting. A necessary step to shutdown the tick while still accounting the cputime.
---
arch/Kconfig | 10 ++--
arch/x86/Kconfig | 2 +-
arch/x86/include/asm/{rcu.h => user_hooks.h} | 12 +++---
arch/x86/kernel/ptrace.c | 6 +-
arch/x86/kernel/signal.c | 5 +-
arch/x86/kernel/traps.c | 2 +-
arch/x86/mm/fault.c | 2 +-
include/linux/rcupdate.h | 2 -
include/linux/sched.h | 8 ----
include/linux/user_hooks.h | 18 ++++++++
init/Kconfig | 24 +++++++----
kernel/Makefile | 1 +
kernel/rcutree.c | 42 +------------------
kernel/sched/core.c | 9 ++--
kernel/user_hooks.c | 56 ++++++++++++++++++++++++++
15 files changed, 117 insertions(+), 82 deletions(-)
rename arch/x86/include/asm/{rcu.h => user_hooks.h} (56%)
create mode 100644 include/linux/user_hooks.h
create mode 100644 kernel/user_hooks.carch/Kconfig +5 -5
@@ -277,14 +277,14 @@ config SECCOMP_FILTER config HAVE_VIRT_CPU_ACCOUNTING bool -config HAVE_RCU_USER_QS +config HAVE_USER_HOOKS bool help Provide kernel entry/exit hooks necessary for userspace RCU extended quiescent state. Syscalls need to be wrapped inside - rcu_user_exit()-rcu_user_enter() through the slow path using - TIF_NOHZ flag. Exceptions handlers must be wrapped as well. Irqs - are already protected inside rcu_irq_enter/rcu_irq_exit() but - preemption or signal handling on irq exit still need to be protected. + user_exit()-user_enter() through the slow path using TIF_NOHZ flag. + Exceptions handlers must be wrapped as well. Irqs are already + protected inside rcu_irq_enter/rcu_irq_exit() but preemption or + signal handling on irq exit still need to be protected. source "kernel/gcov/Kconfig"
arch/x86/Kconfig +1 -1
@@ -95,7 +95,7 @@ config X86 select KTIME_SCALAR if X86_32 select GENERIC_STRNCPY_FROM_USER select GENERIC_STRNLEN_USER - select HAVE_RCU_USER_QS if X86_64 + select HAVE_USER_HOOKS if X86_64 config INSTRUCTION_DECODER def_bool (KPROBES || PERF_EVENTS || UPROBES)
arch/x86/include/asm/user_hooks.h renamed from arch/x86/include/asm/rcu.h +6 -6
@@ -1,19 +1,19 @@ -#ifndef _ASM_X86_RCU_H -#define _ASM_X86_RCU_H +#ifndef _ASM_X86_USER_HOOKS_H +#define _ASM_X86_USER_HOOKS_H -#include <linux/rcupdate.h> +#include <linux/user_hooks.h> #include <asm/ptrace.h> static inline void exception_enter (struct pt_regs *regs) { -rcu_user_exit (); +user_exit (); }static inline void exception_exit (struct pt_regs *regs) { -#ifdef CONFIG_RCU_USER_QS +#ifdef CONFIG_USER_HOOKS if (user_mode (regs)) -rcu_user_enter (); +user_enter ();#endif }
arch/x86/kernel/ptrace.c +3 -3
@@ -21,7 +21,7 @@#include <linux/signal.h> #include <linux/perf_event.h> #include <linux/hw_breakpoint.h> -#include <linux/rcupdate.h> +#include <linux/user_hooks.h> #include <asm/uaccess.h> #include <asm/pgtable.h>
@@ -1464,7 +1464,7 @@ long syscall_trace_enter(struct pt_regs *regs) {long ret =0 ; -rcu_user_exit (); +user_exit ();/* * If we stepped into a sysenter/syscall insn, it trapped in
@@ -1530,5 +1530,5 @@ void syscall_trace_leave(struct pt_regs *regs)if (step ||test_thread_flag (TIF_SYSCALL_TRACE ))tracehook_report_syscall_exit (regs, step); -rcu_user_enter (); +user_enter (); }
arch/x86/kernel/signal.c +3 -2
@@ -19,6 +19,7 @@#include <linux/uaccess.h> #include <linux/user-return-notifier.h> #include <linux/uprobes.h> +#include <linux/user_hooks.h> #include <asm/processor.h> #include <asm/ucontext.h>
@@ -776,7 +777,7 @@ static void do_signal(struct pt_regs *regs)void do_notify_resume (struct pt_regs *regs,void *unused,__u32 thread_info_flags) { -rcu_user_exit (); +user_exit ();#ifdef CONFIG_X86_MCE /* notify userspace of pending MCEs */
@@ -804,7 +805,7 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)clear_thread_flag (TIF_IRET );#endif /* CONFIG_X86_32 */ -rcu_user_enter (); +user_enter (); }void signal_fault (struct pt_regs *regs,void __user *frame,char *where)
arch/x86/kernel/traps.c +1 -1
@@ -52,7 +52,7 @@#include <asm/i387.h> #include <asm/fpu-internal.h> #include <asm/mce.h> -#include <asm/rcu.h> +#include <asm/user_hooks.h> #include <asm/mach_traps.h>
arch/x86/mm/fault.c +1 -1
@@ -18,7 +18,7 @@#include <asm/pgalloc.h> /* pgd_*(), ... */ #include <asm/kmemcheck.h> /* kmemcheck_*(), ... */ #include <asm/fixmap.h> /* VSYSCALL_START */ -#include <asm/rcu.h> /* exception_enter(), ... */ +#include <asm/user_hooks.h> /* exception_enter(), ... */ /* * Page fault error code bits:
include/linux/rcupdate.h +0 -2
@@ -197,8 +197,6 @@ extern void rcu_user_enter(void);extern void rcu_user_exit (void );extern void rcu_user_enter_irq (void );extern void rcu_user_exit_irq (void ); -extern void rcu_user_hooks_switch (struct task_struct *prev, -struct task_struct *next);#else static inline void rcu_user_enter (void ) { }static inline void rcu_user_exit (void ) { }
include/linux/sched.h +0 -8
@@ -1899,14 +1899,6 @@ static inline void rcu_copy_process(struct task_struct *p)#endif -static inline void rcu_switch (struct task_struct *prev, -struct task_struct *next) -{ -#ifdef CONFIG_RCU_USER_QS -rcu_user_hooks_switch (prev, next); -#endif -} -#ifdef CONFIG_SMP extern void do_set_cpus_allowed (struct task_struct *p,const struct cpumask *new_mask);
include/linux/user_hooks.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef _LINUX_USER_HOOKS_H +#define _LINUX_USER_HOOKS_H + +#ifdef CONFIG_USER_HOOKS +#include <linux/sched.h> + +extern void user_enter (void ); +extern void user_exit (void ); +extern void user_hooks_switch (struct task_struct *prev, +struct task_struct *next); +#else +static inline void user_enter (void ) { } +static inline void user_exit (void ) { } +static inline void user_hooks_switch (struct task_struct *prev, +struct task_struct *next) { } +#endif /* !CONFIG_USER_HOOKS */ + +#endif
init/Kconfig +15 -9
@@ -404,6 +404,19 @@ config AUDIT_LOGINUID_IMMUTABLE source "kernel/irq/Kconfig" source "kernel/time/Kconfig" +config USER_HOOKS + bool + +config USER_HOOKS_FORCE + bool "Force userspace hooks" + depends on USER_HOOKS + help + Set the hooks in user/kernel boundaries by default in order to + test the features that rely on it such as userspace RCU extended + quiescent states. + This test is there for debugging until we have a real user like a + full adaptive nohz option. + menu "RCU Subsystem" choice
@@ -456,7 +469,8 @@ config PREEMPT_RCU config RCU_USER_QS bool "Consider userspace as in RCU extended quiescent state" - depends on HAVE_RCU_USER_QS && SMP + depends on HAVE_USER_HOOKS && SMP + select USER_HOOKS help This option sets hooks on kernel / userspace boundaries and puts RCU in extended quiescent state when the CPU runs in
@@ -464,14 +478,6 @@ config RCU_USER_QS excluded from the global RCU state machine and thus doesn't to keep the timer tick on for RCU. -config RCU_USER_QS_FORCE - bool "Force userspace extended QS by default" - depends on RCU_USER_QS - help - Set the hooks in user/kernel boundaries by default in order to - test this feature that treats userspace as an extended quiescent - state until we have a real user like a full adaptive nohz option. - config RCU_FANOUT int "Tree-based hierarchical RCU fanout value" range 2 64 if 64BIT
kernel/Makefile +1 -0
@@ -110,6 +110,7 @@ obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o obj-$(CONFIG_PADATA) += padata.o obj-$(CONFIG_CRASH_DUMP) += crash_dump.o obj-$(CONFIG_JUMP_LABEL) += jump_label.o +obj-$(CONFIG_USER_HOOKS) += user_hooks.o $(obj)/configs.o: $(obj)/config_data.h
kernel/rcutree.c +2 -40
66 lines not on this page · raw message
kernel/sched/core.c +5 -4
@@ -72,6 +72,7 @@#include <linux/slab.h> #include <linux/init_task.h> #include <linux/binfmts.h> +#include <linux/user_hooks.h> #include <asm/switch_to.h> #include <asm/tlb.h>
@@ -1926,7 +1927,7 @@ context_switch(struct rq *rq, struct task_struct *prev,#endif /* Here we just switch the register state and the stack. */ -rcu_switch (prev, next); +user_hooks_switch (prev, next);switch_to (prev, next, prev);barrier ();
@@ -2920,9 +2921,9 @@ EXPORT_SYMBOL(schedule); asmlinkage void __sched schedule_user(void) { -rcu_user_exit (); +user_exit ();schedule (); -rcu_user_enter (); +user_enter (); }/**
@@ -3026,7 +3027,7 @@ asmlinkage void __sched preempt_schedule_irq(void)/* Catch callers which need to be fixed */ BUG_ON (ti->preempt_count || !irqs_disabled ()); -rcu_user_exit (); +user_exit (); do { add_preempt_count(PREEMPT_ACTIVE); local_irq_enable();
kernel/user_hooks.c new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
1.7.5.4
Re: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 8 lines · Frederic Weisbecker
Create a new subsystem that handles the hooks on kernel/user boundaries currently used by RCU for its userspace extended quiescent state.
We need to pull this up from RCU into this new level of indirection because these hooks are also going to be used to implement an "on demand" generic virtual cputime accounting. A necessary step to shutdown the tick while still accounting the cputime.
So this eliminates the case where the architecture might enter an RCU extended quiescent state multiple times, but exit it only once? (I am hoping that it does...)
Thanx, Paul
[ . . . ]
Quoted 64 lines
kernel/rcutree.c +2
-33@@ -212,9 +212,6 @@ EXPORT_SYMBOL_GPL(rcu_note_context_switch); DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = { .dynticks_nesting =DYNTICK_TASK_EXIT_IDLE , .dynticks =ATOMIC_INIT (1 ), -#if defined(CONFIG_RCU_USER_QS) && !defined(CONFIG_RCU_USER_QS_FORCE) - .ignore_user_qs = true, -#endif };static int blimit =10 ;/* Maximum callbacks per rcu_do_batch. */ @@ -448,18 +445,7 @@ EXPORT_SYMBOL_GPL(rcu_idle_enter);*/ void rcu_user_enter (void ) { -unsigned long flags; -struct rcu_dynticks *rdtp; - -WARN_ON_ONCE (!current->mm); - -local_irq_save (flags); - rdtp = &__get_cpu_var (rcu_dynticks); -if (!rdtp->ignore_user_qs && !rdtp->in_user) { - rdtp->in_user = true; -rcu_eqs_enter (1 ); - } -local_irq_restore (flags); +rcu_eqs_enter (1 ); }EXPORT_SYMBOL_GPL (rcu_user_enter);@@ -597,16 +583,7 @@ EXPORT_SYMBOL_GPL(rcu_idle_exit);*/ void rcu_user_exit (void ) { -unsigned long flags; -struct rcu_dynticks *rdtp; - -local_irq_save (flags); - rdtp = &__get_cpu_var (rcu_dynticks); -if (rdtp->in_user) { - rdtp->in_user = false; -rcu_eqs_exit (1 ); - } -local_irq_restore (flags); +rcu_eqs_exit (1 ); }EXPORT_SYMBOL_GPL (rcu_user_exit);@@ -730,21 +707,6 @@ int rcu_is_cpu_idle(void) }EXPORT_SYMBOL (rcu_is_cpu_idle); -#ifdef CONFIG_RCU_USER_QS -void rcu_user_hooks_switch (struct task_struct *prev, -struct task_struct *next) -{ -struct rcu_dynticks *rdtp; - -/* Interrupts are disabled in context switch */ - rdtp = &__get_cpu_var (rcu_dynticks);
kernel/rcutree.c int rcu_is_cpu_idle(void) · line 712
Quoted 10 lines
kernel/rcutree.c +0
-7-if (!rdtp->ignore_user_qs) { -clear_tsk_thread_flag (prev,TIF_NOHZ ); -set_tsk_thread_flag (next,TIF_NOHZ ); - } -} -#endif /* #ifdef CONFIG_RCU_USER_QS */ -#if defined(CONFIG_PROVE_RCU ) && defined(CONFIG_HOTPLUG_CPU )/*
[ . . . ]
Re: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 5 lines · Paul E. McKenney
Quoted 4 lines · Frederic Weisbecker
Create a new subsystem that handles the hooks on kernel/user boundaries currently used by RCU for its userspace extended quiescent state.
Quoted 8 lines
Quoted 4 lines
We need to pull this up from RCU into this new level of indirection because these hooks are also going to be used to implement an "on demand" generic virtual cputime accounting. A necessary step to shutdown the tick while still accounting the cputime.
So this eliminates the case where the architecture might enter an RCU extended quiescent state multiple times, but exit it only once? (I am hoping that it does...)
Yeah. It should handle that.
Peter Zijlstra
maintainerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
In reply to Frederic Weisbeckerkernel/user_hooks.c line 9
Quoted 11 lines · Frederic Weisbecker
+++
b/kernel/user_hooks.ckernel/user_hooks.c new file +9
-0@@ -0,0 +1,56 @@ +#include <linux/user_hooks.h> +#include <linux/rcupdate.h> +#include <linux/sched.h> +#include <linux/percpu.h> + +struct user_hooks { +bool hooking; +bool in_user; +};
I really detest using bool in structures.. but that's just me. Also this really wants a comment as to wtf 'hooking' means. in_user I can just about guess.
Quoted 8 lines
kernel/user_hooks.c new file +8
-0+DEFINE_PER_CPU(struct user_hooks, user_hooks) = { +#ifdef CONFIG_USER_HOOKS_FORCE + .hooking = true, +#endif +}; + +void user_enter (void ) +{
kernel/user_hooks.c line 28
Quoted 10 lines
kernel/user_hooks.c new file +10
-0+unsigned long flags; +struct user_hooks *uh; + +WARN_ON_ONCE (!current->mm); +local_irq_save (flags); + uh = &__get_cpu_var (user_hooks); +if (uh->hooking && !uh->in_user) { + uh->in_user = true; +rcu_user_enter (); + }
By not using __get_cpu_var() but __this_cpu_*() you generate much better code (esp. on x86).
IOW. something like:
if (__this_cpu_read(uh.hooking) && !__this_cpu_read(uh.in_user)) {
__this_cpu_write(uh.in_user, true);
rcu_user_enter();}
Quoted 17 lines
kernel/user_hooks.c new file +17
-0+local_irq_restore (flags); +} + +void user_exit (void ) +{ +unsigned long flags; +struct user_hooks *uh; + +local_irq_save (flags); + uh = &__get_cpu_var (user_hooks); +if (uh->in_user) { + uh->in_user = false; +rcu_user_exit (); + } +local_irq_restore (flags); +} +
kernel/user_hooks.c line 55
Quoted 10 lines
kernel/user_hooks.c new file +10
-0+void user_hooks_switch (struct task_struct *prev, +struct task_struct *next) +{ +struct user_hooks *uh; + + uh = &__get_cpu_var (user_hooks); +if (uh->hooking) { +clear_tsk_thread_flag (prev,TIF_NOHZ ); +set_tsk_thread_flag (next,TIF_NOHZ ); + }
This seems pointless to me.. why are we flipping that flag on context switch instead of keeping it enabled at all times? This are two atomic ops in the context switch path, why?
Quoted 1 line
(quoted diff) +1
-0+}
Steven Rostedt
reviewerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 16 lines · Peter Zijlstra
kernel/user_hooks.c line 9
Quoted 11 lines · Frederic Weisbecker
+++
b/kernel/user_hooks.ckernel/user_hooks.c new file +9
-0@@ -0,0 +1,56 @@ +#include <linux/user_hooks.h> +#include <linux/rcupdate.h> +#include <linux/sched.h> +#include <linux/percpu.h> + +struct user_hooks { +bool hooking; +bool in_user; +};I really detest using bool in structures.. but that's just me. Also this really wants a comment as to wtf 'hooking' means.
in_userI can just about guess.
I'm curious to what you have against bool in structures? Would you prefer a:
struct user_hooks {
unsigned int hooking:1;
unsigned int in_user:1;
};instead? I haven't checked, but I would hope that gcc would optimize the struct into a single word.
But I could see that it can cause races as that would make modifying hooking and in_user dependent on each other. That is, if one CPU updates hooking as another CPU updates in_user, that could cause a read-modify-write race. At least in this case the modification is only done on local cpu variables.
-- Steve
Peter Zijlstra
maintainerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 1 line · Steven Rostedt
I'm curious to what you have against bool in structures?
_Bool as per the C std doesn't have a specified storage. Now IIRC hpa recently said that all GCC versions so far were consistent and used char (a byte) for it, but I might mis-remember.
Re: [PATCH 1/5] user_hooks: New user hooks subsystem
In reply to Peter ZijlstraQuoted 16 lines · Peter Zijlstra
kernel/user_hooks.c line 9
Quoted 11 lines · Frederic Weisbecker
+++
b/kernel/user_hooks.ckernel/user_hooks.c new file +9
-0@@ -0,0 +1,56 @@ +#include <linux/user_hooks.h> +#include <linux/rcupdate.h> +#include <linux/sched.h> +#include <linux/percpu.h> + +struct user_hooks { +bool hooking; +bool in_user; +};I really detest using bool in structures.. but that's just me. Also this really wants a comment as to wtf 'hooking' means.
in_userI can just about guess.
I really don't mind changing that to int. I just like them as bool because they better describe the purpose of the field.
hooking means that the hooks are set (the TIF flag is set on the current task and we also handle the exception hooks).
I can call that is_hooking instead? And/or add a comment to explain the purpose of this.
Quoted 23 lines
Quoted 9 lines
kernel/user_hooks.c new file +8
-0+DEFINE_PER_CPU(struct user_hooks, user_hooks) = { +#ifdef CONFIG_USER_HOOKS_FORCE + .hooking = true, +#endif +}; + +void user_enter (void ) +{kernel/user_hooks.c line 28
Quoted 10 lines
kernel/user_hooks.c new file +10
-0+unsigned long flags; +struct user_hooks *uh; + +WARN_ON_ONCE (!current->mm); +local_irq_save (flags); + uh = &__get_cpu_var (user_hooks); +if (uh->hooking && !uh->in_user) { + uh->in_user = true; +rcu_user_enter (); + }By not using
__get_cpu_var()but __this_cpu_*() you generate much better code (esp. on x86).
Quoted 6 lines
IOW. something like:
if (__this_cpu_read(uh.hooking) && !__this_cpu_read(uh.in_user)) { __this_cpu_write(uh.in_user, true); rcu_user_enter();}
Ok, I'll replace.
Quoted 24 lines
Quoted 24 lines
kernel/user_hooks.c new file +23
-0+local_irq_restore (flags); +} + +void user_exit (void ) +{ +unsigned long flags; +struct user_hooks *uh; + +local_irq_save (flags); + uh = &__get_cpu_var (user_hooks); +if (uh->in_user) { + uh->in_user = false; +rcu_user_exit (); + } +local_irq_restore (flags); +} + +void user_hooks_switch (struct task_struct *prev, +struct task_struct *next) +{ +struct user_hooks *uh; + + uh = &__get_cpu_var (user_hooks);
Quoted 8 lines
kernel/user_hooks.c line 55
Quoted 4 lines
kernel/user_hooks.c new file +4
-0+if (uh->hooking) { +clear_tsk_thread_flag (prev,TIF_NOHZ ); +set_tsk_thread_flag (next,TIF_NOHZ ); + }This seems pointless to me.. why are we flipping that flag on context switch instead of keeping it enabled at all times? This are two atomic ops in the context switch path, why?
Because the hooks are per cpu. If we activate the hooks on CPU 1 but not on CPU 2 and prev was running on CPU 1 and migrates on CPU 2, it's going to keep the hook on that CPU 2 if we don't clear the flag.
Steven Rostedt
reviewerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 18 lines · Frederic Weisbecker
Quoted 16 lines · Peter Zijlstra
kernel/user_hooks.c line 9
Quoted 11 lines · Frederic Weisbecker
+++
b/kernel/user_hooks.ckernel/user_hooks.c new file +9
-0@@ -0,0 +1,56 @@ +#include <linux/user_hooks.h> +#include <linux/rcupdate.h> +#include <linux/sched.h> +#include <linux/percpu.h> + +struct user_hooks { +bool hooking; +bool in_user; +};I really detest using bool in structures.. but that's just me. Also this really wants a comment as to wtf 'hooking' means.
in_userI can just about guess.
Quoted 2 lines
I really don't mind changing that to int. I just like them as bool because they better describe the purpose of the field.
Not only does bool describe it better, it should also allow gcc to optimize it better as well. Unless Peter has a legitimate rational why using bool in struct is bad, I would keep it as is.
Quoted 6 lines
hooking means that the hooks are set (the TIF flag is set on the current task and we also handle the exception hooks).
I can call that
is_hookinginstead? And/or add a comment to explain the purpose of this.
Would 'is_hooked' be better? 'is_hooking' sounds more like what women in high heels, really short skirts and lots of makeup are doing late night on a corner of a Paris street ;-)
A comment to explain the purpose should be added regardless.
-- Steve
Peter Zijlstra
maintainerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 4 lines · Steven Rostedt
Would '
is_hooked' be better? 'is_hooking' sounds more like what women in high heels, really short skirts and lots of makeup are doing late night on a corner of a Paris street ;-)
This is exactly the first thing I though of when I read the name ;-)
Quoted 1 line
A comment to explain the purpose should be added regardless.
Ack.
Peter Zijlstra
maintainerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
In reply to Steven RostedtQuoted 4 lines · Steven Rostedt
Not only does bool describe it better, it should also allow gcc to optimize it better as well. Unless Peter has a legitimate rational why using bool in struct is bad, I would keep it as is.
I don't mind too much, but like said, I hate using types without specified storage, you never really know what the compiler will end up doing.
Ingo Molnar
maintainerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
In reply to Frederic WeisbeckerQuoted 18 lines · Frederic Weisbecker
Quoted 16 lines · Peter Zijlstra
kernel/user_hooks.c line 9
Quoted 11 lines · Frederic Weisbecker
+++
b/kernel/user_hooks.ckernel/user_hooks.c new file +9
-0@@ -0,0 +1,56 @@ +#include <linux/user_hooks.h> +#include <linux/rcupdate.h> +#include <linux/sched.h> +#include <linux/percpu.h> + +struct user_hooks { +bool hooking; +bool in_user; +};I really detest using bool in structures.. but that's just me. Also this really wants a comment as to wtf 'hooking' means.
in_userI can just about guess.
Quoted 8 lines
I really don't mind changing that to int. I just like them as bool because they better describe the purpose of the field.
hooking means that the hooks are set (the TIF flag is set on the current task and we also handle the exception hooks).
I can call that
is_hookinginstead? And/or add a comment to explain the purpose of this.
Please don't use this horrible naming - use something more technical like struct user_callback and callback::active, ok?
Thanks,
Ingo
Re: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 24 lines · Ingo Molnar
Quoted 21 lines · Frederic Weisbecker
Quoted 16 lines · Peter Zijlstra
kernel/user_hooks.c line 9
Quoted 11 lines · Frederic Weisbecker
+++
b/kernel/user_hooks.ckernel/user_hooks.c new file +9
-0@@ -0,0 +1,56 @@ +#include <linux/user_hooks.h> +#include <linux/rcupdate.h> +#include <linux/sched.h> +#include <linux/percpu.h> + +struct user_hooks { +bool hooking; +bool in_user; +};I really detest using bool in structures.. but that's just me. Also this really wants a comment as to wtf 'hooking' means.
in_userI can just about guess.I really don't mind changing that to int. I just like them as bool because they better describe the purpose of the field.
Quoted 8 lines
Quoted 5 lines
hooking means that the hooks are set (the TIF flag is set on the current task and we also handle the exception hooks).
I can call that
is_hookinginstead? And/or add a comment to explain the purpose of this.Please don't use this horrible naming - use something more technical like struct
user_callbackandcallback::active, ok?
Ok, user callback should be fine. I'll respin with that.
Ingo Molnar
maintainerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 28 lines · Frederic Weisbecker
Quoted 27 lines · Ingo Molnar
Quoted 24 lines · Frederic Weisbecker
Quoted 16 lines · Peter Zijlstra
kernel/user_hooks.c line 9
Quoted 11 lines · Frederic Weisbecker
+++
b/kernel/user_hooks.ckernel/user_hooks.c new file +9
-0@@ -0,0 +1,56 @@ +#include <linux/user_hooks.h> +#include <linux/rcupdate.h> +#include <linux/sched.h> +#include <linux/percpu.h> + +struct user_hooks { +bool hooking; +bool in_user; +};I really detest using bool in structures.. but that's just me. Also this really wants a comment as to wtf 'hooking' means.
in_userI can just about guess.I really don't mind changing that to int. I just like them as bool because they better describe the purpose of the field.
hooking means that the hooks are set (the TIF flag is set on the current task and we also handle the exception hooks).
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
I can call that
is_hookinginstead? And/or add a comment to explain the purpose of this.Please don't use this horrible naming - use something more technical like struct
user_callbackandcallback::active, ok?Ok, user callback should be fine. I'll respin with that.
One problem I have with the word 'hook' is that it's rarely clear whether it's used as a noun or a verb - and the naming in your patch shows that kind of confusion in action.
'callback', while a longer word, is almost always used as a noun within the kernel - and it also has a pretty narrow meaning.
Thanks,
Ingo
Peter Zijlstra
maintainerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 3 lines · Ingo Molnar
'callback', while a longer word, is almost always used as a noun within the kernel - and it also has a pretty narrow meaning.
An altogether different naming would be something like:
struct user_kernel_tracking {
int want_uk_tracking;
enum {
in_kernel = 0,
in_user,
} uk_state;
};Re: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Ingo Molnar
'callback', while a longer word, is almost always used as a noun within the kernel - and it also has a pretty narrow meaning.
An altogether different naming would be something like:
Quoted 9 lines
struct user_kernel_tracking {
int want_uk_tracking;
enum {
in_kernel = 0,
in_user,
} uk_state;
};You bet we might also extend this to track guest as well in the future because it appears that we could also enter into RCU extended quiescent state when we run in guest.
So we probably need to generalize a bit more. Some naming based on "code domain"?
struct code_domain {
int is_tracking;
enum {
in_kernel,
in_user,
in_guest
} state;
}Steven Rostedt
reviewerRe: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 5 lines · Frederic Weisbecker
On Tue, Jul 31, 2012 at 06:14:22PM +0200, Peter Zijlstra wrote:
So we probably need to generalize a bit more. Some naming based on "code domain"?
Quoted 8 lines
struct code_domain {
int is_tracking;
enum {
in_kernel,
in_user,
in_guest
} state;
}Is there a fundamental difference between 'in_user' and 'in_guest' though?
-- Steve
Re: [PATCH 1/5] user_hooks: New user hooks subsystem
Quoted 15 lines · Steven Rostedt
Quoted 5 lines · Frederic Weisbecker
On Tue, Jul 31, 2012 at 06:14:22PM +0200, Peter Zijlstra wrote:
So we probably need to generalize a bit more. Some naming based on "code domain"?
Quoted 8 lines
struct code_domain { int is_tracking; enum { in_kernel, in_user, in_guest } state; }
Quoted 2 lines
Is there a fundamental difference between '
in_user' and 'in_guest' though?
Probably not from RCU POV. But the cputime is not accounted the same.
PATCH 2/5 cputime: Don't allow virtual and irq finegrained cputime accounting simultaneously
In reply to Frederic WeisbeckerWe may soon be able to run both at the same time. But we need to rework a bit the irq finegrained accounting before that. Just do a mutual exclusion for now.
--- init/Kconfig | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
init/Kconfig +1 -1
@@ -269,7 +269,7 @@ config POSIX_MQUEUE_SYSCTL config VIRT_CPU_ACCOUNTING bool "Deterministic task and CPU time accounting" - depends on HAVE_VIRT_CPU_ACCOUNTING + depends on HAVE_VIRT_CPU_ACCOUNTING && !IRQ_TIME_ACCOUNTING default y if PPC64 help Select this option to enable more accurate task and CPU time
Signature · 1 line
1.7.5.4
PATCH 3/5 cputime: Allow dynamic switch between tick/virtual based cputime accounting
In reply to Frederic WeisbeckerAllow to dynamically switch between tick and virtual based cputime accounting. This way we can provide a kind of "on-demand" virtual based cputime accounting. In this mode, the kernel will rely on the user hooks subsystem to dynamically hook on kernel boundaries.
This is in preparation for beeing able to stop the timer tick further idle. Doing so will depend on CONFIG_VIRT_CPU_ACCOUNTING which makes it possible to account the cputime without the tick by hooking on kernel/user boundaries.
Depending whether the tick is stopped or not, we can switch between tick and vtime based accounting anytime in order to minimize the overhead associated to user hooks.
--- arch/ia64/include/asm/cputime.h | 5 ++++ arch/ia64/kernel/time.c | 2 +- arch/powerpc/include/asm/cputime.h | 5 ++++ arch/powerpc/kernel/time.c | 2 +- arch/s390/include/asm/cputime.h | 5 ++++ arch/s390/kernel/vtime.c | 2 +- include/asm-generic/cputime.h | 5 ++++ include/linux/kernel_stat.h | 3 ++ include/linux/sched.h | 5 +--- kernel/fork.c | 3 +- kernel/sched/cputime.c | 39 +++++++++++++++-------------------- kernel/time/tick-sched.c | 28 ++++++++++++------------- 12 files changed, 58 insertions(+), 46 deletions(-)
arch/ia64/include/asm/cputime.h +5 -0
@@ -103,5 +103,10 @@ static inline void cputime_to_timeval(const cputime_t ct, struct timeval *val)#define cputime64_to_clock_t (__ct) \ cputime_to_clock_t((__force cputime_t)__ct) +static inline bool accounting_vtime (void ) +{ +return true; +} +#endif /* CONFIG_VIRT_CPU_ACCOUNTING */ #endif /* __IA64_CPUTIME_H */
arch/ia64/kernel/time.c +1 -1
@@ -144,7 +144,7 @@ EXPORT_SYMBOL_GPL(account_system_vtime);* Called from the timer interrupt handler to charge accumulated user time * to the current process. Must be called with interrupts disabled. */ -void account_process_tick (struct task_struct *p,int user_tick) +void account_process_tick_vtime (struct task_struct *p,int user_tick) {struct thread_info *ti =task_thread_info (p);cputime_t delta_utime;
arch/powerpc/include/asm/cputime.h +5 -0
@@ -228,6 +228,11 @@ static inline cputime_t clock_t_to_cputime(const unsigned long clk)#define cputime64_to_clock_t (ct) cputime_to_clock_t((cputime_t)(ct)) +static inline bool accounting_vtime (void ) +{ +return true; +} +#endif /* __KERNEL__ */ #endif /* CONFIG_VIRT_CPU_ACCOUNTING */ #endif /* __POWERPC_CPUTIME_H */
arch/powerpc/kernel/time.c +1 -1
@@ -354,7 +354,7 @@ EXPORT_SYMBOL_GPL(account_system_vtime);* (i.e. since the last entry from usermode) so that * get_paca()->user_time_scaled is up to date. */ -void account_process_tick (struct task_struct *tsk,int user_tick) +void account_process_tick_vtime (struct task_struct *tsk,int user_tick) {cputime_t utime, utimescaled;
arch/s390/include/asm/cputime.h +5 -0
@@ -188,4 +188,9 @@ static inline int s390_nohz_delay(int cpu)#define arch_needs_cpu (cpu) s390_nohz_delay(cpu) +static inline bool accounting_vtime (void ) +{ +return true; +} +#endif /* _S390_CPUTIME_H */
arch/s390/kernel/vtime.c +1 -1
@@ -99,7 +99,7 @@ void account_switch_vtime(struct task_struct *prev) S390_lowcore.system_timer = ti->system_timer; } -void account_process_tick (struct task_struct *tsk,int user_tick) +void account_process_tick_vtime (struct task_struct *tsk,int user_tick) {do_account_vtime (tsk,HARDIRQ_OFFSET ); }
include/asm-generic/cputime.h +5 -0
@@ -66,4 +66,9 @@ typedef u64 __nocast cputime64_t;#define cputime64_to_clock_t (__ct) \ jiffies_64_to_clock_t(cputime64_to_jiffies64(__ct)) +static inline bool accounting_vtime (void ) +{ +return false; +} +#endif
include/linux/kernel_stat.h +3 -0
@@ -132,8 +132,11 @@ extern void account_idle_ticks(unsigned long ticks);#ifdef CONFIG_VIRT_CPU_ACCOUNTING extern void account_switch_vtime (struct task_struct *prev); +extern bool account_process_tick_vtime (struct task_struct *p,int user_tick);#else static inline void account_switch_vtime (struct task_struct *prev) { } +static inline void account_process_tick_vtime (struct task_struct *p, +int user_tick) { }#endif #endif /* _LINUX_KERNEL_STAT_H */
include/linux/sched.h +1 -4
@@ -612,9 +612,7 @@ struct signal_struct {cputime_t utime, stime, cutime, cstime;cputime_t gtime;cputime_t cgtime; -#ifndef CONFIG_VIRT_CPU_ACCOUNTING cputime_t prev_utime, prev_stime; -#endif unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;unsigned long inblock, oublock, cinblock, coublock;
@@ -1363,9 +1361,8 @@ struct task_struct {cputime_t utime, stime, utimescaled, stimescaled;cputime_t gtime; -#ifndef CONFIG_VIRT_CPU_ACCOUNTING cputime_t prev_utime, prev_stime; -#endif +unsigned long nvcsw, nivcsw;/* context switch counts */ struct timespec start_time;/* monotonic time */ struct timespec real_start_time;/* boot based time */
kernel/fork.c +1 -2
@@ -1245,9 +1245,8 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->utime = p->stime = p->gtime =0 ; p->utimescaled = p->stimescaled =0 ; -#ifndef CONFIG_VIRT_CPU_ACCOUNTING p->prev_utime = p->prev_stime =0 ; -#endif +#if defined(SPLIT_RSS_COUNTING )memset (&p->rss_stat,0 ,sizeof (p->rss_stat));#endif
kernel/sched/cputime.c +17 -22
@@ -287,8 +287,6 @@ static __always_inline bool steal_account_process_tick(void)return false; } -#ifndef CONFIG_VIRT_CPU_ACCOUNTING -#ifdef CONFIG_IRQ_TIME_ACCOUNTING /* * Account a tick to a process and cpustat
@@ -368,6 +366,11 @@ void account_process_tick(struct task_struct *p, int user_tick)cputime_t one_jiffy_scaled =cputime_to_scaled (cputime_one_jiffy);struct rq *rq =this_rq (); +if (accounting_vtime ()) { +account_process_tick_vtime (p, user_tick); +return ; + } +if (sched_clock_irqtime) {irqtime_account_process_tick (p, user_tick, rq);return ;
@@ -410,29 +413,10 @@ void account_idle_ticks(unsigned long ticks)account_idle_time (jiffies_to_cputime (ticks)); } -#endif /* * Use precise platform statistics if available: */ -#ifdef CONFIG_VIRT_CPU_ACCOUNTING -void task_times (struct task_struct *p,cputime_t *ut,cputime_t *st) -{ - *ut = p->utime; - *st = p->stime; -} - -void thread_group_times (struct task_struct *p,cputime_t *ut,cputime_t *st) -{ -struct task_cputime cputime; - -thread_group_cputime (p, &cputime); - - *ut = cputime.utime; - *st = cputime.stime; -} -#else -#ifndef nsecs_to_cputime# define nsecs_to_cputime (__nsecs) nsecs_to_jiffies(__nsecs)#endif
@@ -441,6 +425,12 @@ void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st) {cputime_t rtime, utime = p->utime, total = utime + p->stime; +if (accounting_vtime ()) { + *ut = p->utime; + *st = p->stime; +return ; + } +/* * Use CFS's precise accounting: */
@@ -476,6 +466,12 @@ void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)thread_group_cputime (p, &cputime); +if (accounting_vtime ()) { + *ut = cputime.utime; + *st = cputime.stime; +return ; + } + total = cputime.utime + cputime.stime; rtime =nsecs_to_cputime (cputime.sum_exec_runtime);
@@ -494,4 +490,3 @@ void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st) *ut = sig->prev_utime; *st = sig->prev_stime; } -#endif
kernel/time/tick-sched.c +13 -15
@@ -557,9 +557,7 @@ void tick_nohz_idle_exit(void) {int cpu =smp_processor_id ();struct tick_sched *ts = &per_cpu (tick_cpu_sched, cpu); -#ifndef CONFIG_VIRT_CPU_ACCOUNTING unsigned long ticks; -#endif ktime_t now;local_irq_disable ();
@@ -584,19 +582,19 @@ void tick_nohz_idle_exit(void)tick_do_update_jiffies64 (now);update_cpu_load_nohz (); -#ifndef CONFIG_VIRT_CPU_ACCOUNTING -/* -* We stopped the tick in idle. Update process times would miss the -* time we slept as update_process_times does only a 1 tick -* accounting. Enforce that this is accounted to idle ! -*/ - ticks = jiffies - ts->idle_jiffies; -/* -* We might be one off. Do not randomly account a huge number of ticks! -*/ -if (ticks && ticks <LONG_MAX ) -account_idle_ticks (ticks); -#endif +if (!accounting_vtime ()) { +/* +* We stopped the tick in idle. Update process times would miss the +* time we slept as update_process_times does only a 1 tick +* accounting. Enforce that this is accounted to idle ! +*/ + ticks = jiffies - ts->idle_jiffies; +/* +* We might be one off. Do not randomly account a huge number of ticks! +*/ +if (ticks && ticks <LONG_MAX ) +account_idle_ticks (ticks); + }calc_load_exit_idle ();touch_softlockup_watchdog ();
Signature · 1 line
1.7.5.4
PATCH 4/5 cputime: Rename account_system_vtime to account_vtime
In reply to Frederic Weisbeckeraccount_system_vtime() can be called from random places:
hard/softirq entry/exit, kvm guest entry/exit, and even context switches on powerpc.
Rename it to the even more generic account_vtime() name, this reflect well that we are in a random place in the kernel where we have either system, idle or user time to account.
This way we can reuse the "system" version and expand it with other domains such as "user" or "idle" to prepare for the user hooks based virtual cputime accounting that knows better when to flush the time for which domain of execution.
--- arch/ia64/kernel/time.c | 4 ++-- arch/powerpc/kernel/time.c | 8 ++++---- arch/s390/kernel/vtime.c | 4 ++-- include/linux/hardirq.h | 8 ++++---- include/linux/kvm_host.h | 4 ++-- kernel/sched/cputime.c | 8 ++++---- kernel/softirq.c | 6 +++--- 7 files changed, 21 insertions(+), 21 deletions(-)
arch/ia64/kernel/time.c +2 -2
@@ -116,7 +116,7 @@ void account_switch_vtime(struct task_struct *prev)* Account time for a transition between system, hard irq or soft irq state. * Note that this function is called with interrupts enabled. */ -void account_system_vtime (struct task_struct *tsk) +void account_vtime (struct task_struct *tsk) {struct thread_info *ti =task_thread_info (tsk);unsigned long flags;
@@ -138,7 +138,7 @@ void account_system_vtime(struct task_struct *tsk)local_irq_restore (flags); } -EXPORT_SYMBOL_GPL (account_system_vtime); +EXPORT_SYMBOL_GPL (account_vtime);/* * Called from the timer interrupt handler to charge accumulated user time
arch/powerpc/kernel/time.c +4 -4
@@ -291,7 +291,7 @@ static inline u64 calculate_stolen_time(u64 stop_tb)* Account time for a transition between system, hard irq * or soft irq state. */ -void account_system_vtime (struct task_struct *tsk) +void account_vtime (struct task_struct *tsk) {u64 now, nowscaled, delta, deltascaled;unsigned long flags;
@@ -343,14 +343,14 @@ void account_system_vtime(struct task_struct *tsk) }local_irq_restore (flags); } -EXPORT_SYMBOL_GPL (account_system_vtime); +EXPORT_SYMBOL_GPL (account_vtime);/* * Transfer the user and system times accumulated in the paca * by the exception entry and exit code to the generic process * user and system time records. * Must be called with interrupts disabled. -* Assumes that account_system_vtime() has been called recently +* Assumes that account_vtime() has been called recently * (i.e. since the last entry from usermode) so that * get_paca()->user_time_scaled is up to date. */
@@ -368,7 +368,7 @@ void account_process_tick_vtime(struct task_struct *tsk, int user_tick)void account_switch_vtime (struct task_struct *prev) { -account_system_vtime (prev); +account_vtime (prev);account_process_tick (prev,0 ); }
arch/s390/kernel/vtime.c +2 -2
@@ -108,7 +108,7 @@ void account_process_tick_vtime(struct task_struct *tsk, int user_tick)* Update process times based on virtual cpu times stored by entry.S * to the lowcore fields user_timer, system_timer & steal_clock. */ -void account_system_vtime (struct task_struct *tsk) +void account_vtime (struct task_struct *tsk) {struct thread_info *ti =task_thread_info (tsk);__u64 timer, system;
@@ -122,7 +122,7 @@ void account_system_vtime(struct task_struct *tsk) ti->system_timer = S390_lowcore.system_timer;account_system_time (tsk,0 , system, system); } -EXPORT_SYMBOL_GPL (account_system_vtime); +EXPORT_SYMBOL_GPL (account_vtime);void __kprobes vtime_stop_cpu (void ) {
include/linux/hardirq.h +4 -4
@@ -132,11 +132,11 @@ extern void synchronize_irq(unsigned int irq);struct task_struct ;#if !defined(CONFIG_VIRT_CPU_ACCOUNTING ) && !defined(CONFIG_IRQ_TIME_ACCOUNTING ) -static inline void account_system_vtime (struct task_struct *tsk) +static inline void account_vtime (struct task_struct *tsk) { }#else -extern void account_system_vtime (struct task_struct *tsk); +extern void account_vtime (struct task_struct *tsk);#endif #if defined(CONFIG_TINY_RCU ) || defined(CONFIG_TINY_PREEMPT_RCU )
@@ -162,7 +162,7 @@ extern void rcu_nmi_exit(void);*/ #define __irq_enter () \ do { \ - account_system_vtime(current); \ + account_vtime(current); \ add_preempt_count(HARDIRQ_OFFSET); \ trace_hardirq_enter(); \ } while (0)
@@ -178,7 +178,7 @@ extern void irq_enter(void);#define __irq_exit () \ do { \ trace_hardirq_exit(); \ - account_system_vtime(current); \ + account_vtime(current); \ sub_preempt_count(HARDIRQ_OFFSET); \ } while (0)
include/linux/kvm_host.h +2 -2
@@ -676,7 +676,7 @@ static inline int kvm_deassign_device(struct kvm *kvm,static inline void kvm_guest_enter (void ) {BUG_ON (preemptible ()); -account_system_vtime (current); +account_vtime (current); current->flags |=PF_VCPU ;/* KVM does not hold any references to rcu protected data when it * switches CPU into a guest mode. In fact switching to a guest mode
@@ -690,7 +690,7 @@ static inline void kvm_guest_enter(void)static inline void kvm_guest_exit (void ) { -account_system_vtime (current); +account_vtime (current); current->flags &= ~PF_VCPU ; }
kernel/sched/cputime.c +4 -4
@@ -9,11 +9,11 @@/* * There are no locks covering percpu hardirq/softirq time. -* They are only modified in account_system_vtime, on corresponding CPU +* They are only modified in account_vtime, on corresponding CPU * with interrupts disabled. So, writes are safe. * They are read and saved off onto struct rq in update_rq_clock(). * This may result in other CPU reading this CPU's irq time and can -* race with irq/account_system_vtime on this CPU. We would either get old +* race with irq/account_vtime on this CPU. We would either get old * or new value with a side effect of accounting a slice of irq time to wrong * task when irq is in progress while we read rq->clock. That is a worthy * compromise in place of having locks on each irq in account_system_time.
@@ -42,7 +42,7 @@ DEFINE_PER_CPU(seqcount_t, irq_time_seq);* Called before incrementing preempt_count on {soft,}irq_enter * and before decrementing preempt_count on {soft,}irq_exit. */ -void account_system_vtime (struct task_struct *curr) +void account_vtime (struct task_struct *curr) {unsigned long flags;s64 delta;
@@ -72,7 +72,7 @@ void account_system_vtime(struct task_struct *curr)irq_time_write_end ();local_irq_restore (flags); } -EXPORT_SYMBOL_GPL (account_system_vtime); +EXPORT_SYMBOL_GPL (account_vtime);static int irqtime_account_hi_update (void ) {
kernel/softirq.c +3 -3
@@ -212,7 +212,7 @@ asmlinkage void __do_softirq(void)int cpu; pending =local_softirq_pending (); -account_system_vtime (current); +account_vtime (current);__local_bh_disable ((unsigned long )__builtin_return_address (0 ),SOFTIRQ_OFFSET );
@@ -263,7 +263,7 @@ restart:lockdep_softirq_exit (); -account_system_vtime (current); +account_vtime (current);__local_bh_enable (SOFTIRQ_OFFSET ); }
@@ -331,7 +331,7 @@ static inline void invoke_softirq(void)*/ void irq_exit (void ) { -account_system_vtime (current); +account_vtime (current);trace_hardirq_exit ();sub_preempt_count (IRQ_EXIT_OFFSET );if (!in_interrupt () &&local_softirq_pending ())
Signature · 1 line
1.7.5.4
PATCH 5/5 cputime: Generic on-demand virtual cputime accounting
In reply to Frederic WeisbeckerIf we want to stop the tick further idle, we need to be able to account the cputime without using the tick.
Virtual based cputime accounting solves that problem by hooking into kernel/user boundaries.
However implementing CONFIG_VIRT_CPU_ACCOUNTING requires to set low level hooks and involves more overhead. But we already have a generic user hooks subsystem that is required for RCU needs by archs which will want to shut down the tick outside idle.
This patch implements a generic virtual based cputime accounting that relies on these generic user hooks.
There are some upsides of doing this:
- This requires no arch code to implement CONFIG_VIRT_CPU_ACCOUNTING if user hooks are already built (already necessary for RCU in full tickless mode).
- We can rely on the generic user hooks subsystem to dynamically (de)activate the hooks, so that we can switch anytime between virtual and tick based accounting. This way we don't have the overhead of the virtual accounting when the tick is running periodically.
And a few downsides:
- It relies on jiffies and the hooks are set in high level code. This results in less precise cputime accounting than with a true native virtual based cputime accounting which hooks on low level code and use a cpu hardware clock. Precision is not the goal of this though.
- There is probably more overhead than a native virtual based cputime accounting. But this relies on hooks that are already set anyway.
--- include/asm-generic/cputime.h | 2 + include/linux/kernel_stat.h | 12 ++++- include/linux/user_hooks.h | 18 +++++++ init/Kconfig | 11 ++++- kernel/sched/cputime.c | 112 +++++++++++++++++++++++++++++++++++++++++ kernel/user_hooks.c | 10 ++-- 6 files changed, 158 insertions(+), 7 deletions(-)
include/asm-generic/cputime.h +2 -0
@@ -66,9 +66,11 @@ typedef u64 __nocast cputime64_t;#define cputime64_to_clock_t (__ct) \ jiffies_64_to_clock_t(cputime64_to_jiffies64(__ct)) +#ifndef CONFIG_VIRT_CPU_ACCOUNTING_GEN static inline bool accounting_vtime (void ) {return false; } +#endif #endif
include/linux/kernel_stat.h +11 -1
@@ -130,13 +130,23 @@ extern void account_process_tick(struct task_struct *, int user);extern void account_steal_ticks (unsigned long ticks);extern void account_idle_ticks (unsigned long ticks); +#ifdef CONFIG_VIRT_CPU_ACCOUNTING extern void account_switch_vtime (struct task_struct *prev); -extern bool account_process_tick_vtime (struct task_struct *p,int user_tick); +extern void account_process_tick_vtime (struct task_struct *p,int user_tick);#else static inline void account_switch_vtime (struct task_struct *prev) { }static inline void account_process_tick_vtime (struct task_struct *p,int user_tick) { }#endif +#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN +void account_system_vtime (struct task_struct *tsk); +void account_user_vtime (struct task_struct *tsk); +bool accounting_vtime (void ); +#else +static inline void account_system_vtime (struct task_struct *tsk) { } +static inline void account_user_vtime (struct task_struct *tsk) { } +#endif +#endif /* _LINUX_KERNEL_STAT_H */
include/linux/user_hooks.h +18 -0
@@ -3,6 +3,24 @@#ifdef CONFIG_USER_HOOKS #include <linux/sched.h> +#include <linux/percpu.h> + +struct user_hooks { +bool hooking; +bool in_user; +}; + +DECLARE_PER_CPU(struct user_hooks, user_hooks); + +static inline bool in_user (void ) +{ +return __get_cpu_var (user_hooks).in_user; +} + +static inline bool user_hooks_hooking (void ) +{ +return __get_cpu_var (user_hooks).hooking; +}extern void user_enter (void );extern void user_exit (void );
init/Kconfig +10 -1
@@ -269,7 +269,9 @@ config POSIX_MQUEUE_SYSCTL config VIRT_CPU_ACCOUNTING bool "Deterministic task and CPU time accounting" - depends on HAVE_VIRT_CPU_ACCOUNTING && !IRQ_TIME_ACCOUNTING + depends on HAVE_VIRT_CPU_ACCOUNTING || HAVE_USER_HOOKS + depends on !IRQ_TIME_ACCOUNTING + select VIRT_CPU_ACCOUNTING_GEN if !HAVE_VIRT_CPU_ACCOUNTING default y if PPC64 help Select this option to enable more accurate task and CPU time
@@ -280,6 +282,13 @@ config VIRT_CPU_ACCOUNTING stolen time on logically-partitioned systems running on IBM POWER5-based machines. +config VIRT_CPU_ACCOUNTING_GEN + select USER_HOOKS + bool + help + Implement a generic virtual based cputime accounting by using + the user hooks subsystem. + config BSD_PROCESS_ACCT bool "BSD Process Accounting" help
kernel/sched/cputime.c +112 -0
121 lines not on this page · raw message
kernel/user_hooks.c +5 -5
@@ -1,12 +1,8 @@#include <linux/user_hooks.h> #include <linux/rcupdate.h> #include <linux/sched.h> -#include <linux/percpu.h> +#include <linux/kernel_stat.h> -struct user_hooks { -bool hooking; -bool in_user; -}; DEFINE_PER_CPU(struct user_hooks, user_hooks) = {#ifdef CONFIG_USER_HOOKS_FORCE
@@ -24,6 +20,8 @@ void user_enter(void) uh = &__get_cpu_var (user_hooks);if (uh->hooking && !uh->in_user) { uh->in_user = true; +if (accounting_vtime ()) +account_system_vtime (current);rcu_user_enter (); }local_irq_restore (flags);
@@ -39,6 +37,8 @@ void user_exit(void)if (uh->in_user) { uh->in_user = false;rcu_user_exit (); +if (accounting_vtime ()) +account_user_vtime (current); }local_irq_restore (flags); }
Signature · 1 line
1.7.5.4