Skip to contentSkip to conversation

sched/cpuacct: Fix cpuacct charge

RFC PATCH In review Li RongQing 4 participants 8 messages Jul 20, 2021 – Aug 18, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 sched/cpuacct: Fix cpuacct charge

get_irq_regs only work for current running cpu, but the task, whose cpuacct will be charged, maybe run different cpu, like Cpu 2 wake up a kernel thread to CPU 3, cause CPU 3 task are charged with the following stack

  cpuacct_charge+0xd8/0x100
  update_curr+0xe1/0x1e0
  enqueue_entity+0x144/0x6e0
  enqueue_task_fair+0x93/0x900
  ttwu_do_activate+0x4b/0x90
  try_to_wake_up+0x20b/0x530
  ? update_dl_rq_load_avg+0x10f/0x210
  swake_up_locked.part.1+0x13/0x40
  swake_up_one+0x27/0x40
  rcu_process_callbacks+0x484/0x4f0
  ? run_rebalance_domains_bt+0x5a/0x180
  __do_softirq+0xe3/0x308
  irq_exit+0xf0/0x100
  smp_apic_timer_interrupt+0x74/0x160
  apic_timer_interrupt+0xf/0x20
  </IRQ>
 RIP: 0033:0x456947

so define a get_irq_regs_cpu which returns the required cpu irq registers

BUT it should be not safe, and do not know what it should be like in MIPS?

Fixes dbe9337109c2 "(sched/cpuacct: Fix charge cpuacct.usage_sys)"
---
 include/asm-generic/irq_regs.h | 5 +++++
 kernel/sched/cpuacct.c         | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)
include/asm-generic/irq_regs.h +5 -0
index 2e7c6e8..93e2579 100644
@@ -21,6 +21,11 @@ static inline struct pt_regs *get_irq_regs(void)
 	return __this_cpu_read(__irq_regs);
 }
 
+static inline struct pt_regs *get_irq_regs_cpu(int cpu)
+{
+	return per_cpu(__irq_regs, cpu);
+}
+
 static inline struct pt_regs *set_irq_regs(struct pt_regs *new_regs)
 {
 	struct pt_regs *old_regs;
kernel/sched/cpuacct.c +2 -1
index 893eece..8b96058 100644
@@ -340,7 +340,8 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 {
 	struct cpuacct *ca;
 	int index = CPUACCT_STAT_SYSTEM;
-	struct pt_regs *regs = get_irq_regs() ? : task_pt_regs(tsk);
+	int cpu = task_cpu(tsk);
+	struct pt_regs *regs = get_irq_regs_cpu(cpu) ? : task_pt_regs(tsk);
 
 	if (regs && user_mode(regs))
 		index = CPUACCT_STAT_USER;
Signature · 1 line
2.9.4
#1 lore raw
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Li,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on asm-generic/master]
[also build test WARNING on tip/sched/core tip/master linux/master linus/master v5.14-rc2 next-20210720] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Li-RongQing/sched-cpuacct-Fix-cpuacct-charge/20210720-150427
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arnd/asm-generic.git master
config: mips-allyesconfig (attached as .config)
compiler: mips-linux-gcc (GCC) 10.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/b7cbc83d1da5b3b55713e72269c478683f02c4eb
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Li-RongQing/sched-cpuacct-Fix-cpuacct-charge/20210720-150427 git checkout b7cbc83d1da5b3b55713e72269c478683f02c4eb
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-10.3.0 make.cross O=build_dir ARCH=mips SHELL=/bin/bash kernel/sched/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

kernel/sched/cpuacct.c: In function 'cpuacct_charge':
kernel/sched/cpuacct.c:344:25: error: implicit declaration of function 'get_irq_regs_cpu'; did you mean 'get_irq_regs'? [-Werror=implicit-function-declaration]

     344 |  struct pt_regs *regs = get_irq_regs_cpu(cpu) ? : task_pt_regs(tsk);
         |                         ^~~~~~~~~~~~~~~~
         |                         get_irq_regs
>> kernel/sched/cpuacct.c:344:49: warning: pointer/integer type mismatch in conditional expression
     344 |  struct pt_regs *regs = get_irq_regs_cpu(cpu) ? : task_pt_regs(tsk);
         |                                                 ^

cc1: some warnings being treated as errors

vim +344 kernel/sched/cpuacct.c

333
334 /*
335 * charge this task's execution time to its accounting group.
336 *
337 * called with rq->lock held.
338 */
339 void cpuacct_charge(struct task_struct *tsk, u64 cputime)
340 {
341 struct cpuacct *ca;
342 int index = CPUACCT_STAT_SYSTEM;
343 int cpu = task_cpu(tsk);

 > 344		struct pt_regs *regs = get_irq_regs_cpu(cpu) ? : task_pt_regs(tsk);

345
346 if (regs && user_mode(regs))
347 index = CPUACCT_STAT_USER;
348
349 rcu_read_lock();
350
351 for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
352 __this_cpu_add(ca->cpuusage->usages[index], cputime);
353
354 rcu_read_unlock();
355 }
356

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#2 lore raw
In reply to Li RongQing
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Li,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on asm-generic/master]
[also build test ERROR on tip/sched/core tip/master linux/master linus/master v5.14-rc2 next-20210720] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Li-RongQing/sched-cpuacct-Fix-cpuacct-charge/20210720-150427
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arnd/asm-generic.git master
config: mips-allyesconfig (attached as .config)
compiler: mips-linux-gcc (GCC) 10.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/b7cbc83d1da5b3b55713e72269c478683f02c4eb
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Li-RongQing/sched-cpuacct-Fix-cpuacct-charge/20210720-150427 git checkout b7cbc83d1da5b3b55713e72269c478683f02c4eb
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-10.3.0 make.cross ARCH=mips

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

kernel/sched/cpuacct.c: In function 'cpuacct_charge':

>> kernel/sched/cpuacct.c:344:25: error: implicit declaration of function 'get_irq_regs_cpu'; did you mean 'get_irq_regs'? [-Werror=implicit-function-declaration]
     344 |  struct pt_regs *regs = get_irq_regs_cpu(cpu) ? : task_pt_regs(tsk);
         |                         ^~~~~~~~~~~~~~~~
         |                         get_irq_regs
   kernel/sched/cpuacct.c:344:49: warning: pointer/integer type mismatch in conditional expression
     344 |  struct pt_regs *regs = get_irq_regs_cpu(cpu) ? : task_pt_regs(tsk);
         |                                                 ^

cc1: some warnings being treated as errors

vim +344 kernel/sched/cpuacct.c

333
334 /*
335 * charge this task's execution time to its accounting group.
336 *
337 * called with rq->lock held.
338 */
339 void cpuacct_charge(struct task_struct *tsk, u64 cputime)
340 {
341 struct cpuacct *ca;
342 int index = CPUACCT_STAT_SYSTEM;
343 int cpu = task_cpu(tsk);

 > 344		struct pt_regs *regs = get_irq_regs_cpu(cpu) ? : task_pt_regs(tsk);

345
346 if (regs && user_mode(regs))
347 index = CPUACCT_STAT_USER;
348
349 rcu_read_lock();
350
351 for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
352 __this_cpu_add(ca->cpuusage->usages[index], cputime);
353
354 rcu_read_unlock();
355 }
356

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#3 lore raw
In reply to Li RongQing
Quoted 41 lines · Li RongQing

get_irq_regs only work for current running cpu, but the task, whose cpuacct will be charged, maybe run different cpu, like Cpu 2 wake up a kernel thread to CPU 3, cause CPU 3 task are charged with the following stack

  cpuacct_charge+0xd8/0x100
  update_curr+0xe1/0x1e0
  enqueue_entity+0x144/0x6e0
  enqueue_task_fair+0x93/0x900
  ttwu_do_activate+0x4b/0x90
  try_to_wake_up+0x20b/0x530
  ? update_dl_rq_load_avg+0x10f/0x210
  swake_up_locked.part.1+0x13/0x40
  swake_up_one+0x27/0x40
  rcu_process_callbacks+0x484/0x4f0
  ? run_rebalance_domains_bt+0x5a/0x180
  __do_softirq+0xe3/0x308
  irq_exit+0xf0/0x100
  smp_apic_timer_interrupt+0x74/0x160
  apic_timer_interrupt+0xf/0x20
  </IRQ>
 RIP: 0033:0x456947

so define a get_irq_regs_cpu which returns the required cpu irq registers

BUT it should be not safe, and do not know what it should be like in MIPS?

Fixes dbe9337109c2 "(sched/cpuacct: Fix charge cpuacct.usage_sys)"
---
 include/asm-generic/irq_regs.h | 5 +++++
 kernel/sched/cpuacct.c         | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)
include/asm-generic/irq_regs.h +0 -0
index 2e7c6e8..93e2579 100644

include/asm-generic/irq_regs.h static inline struct pt_regs *get_irq_regs(void) · line 27

Quoted 7 lines
include/asm-generic/irq_regs.h +4 -0
@@ -21,6 +21,11 @@ static inline struct pt_regs *get_irq_regs(void)
 	return __this_cpu_read(__irq_regs);
 }
 
+static inline struct pt_regs *get_irq_regs_cpu(int cpu)
+{
+	return per_cpu(__irq_regs, cpu);
+}

This primitive just cannot be right... it'll get you some random data.

#4 lore raw
Quoted 58 lines

-----邮件原件-----
发件人: Peter Zijlstra <[email protected]>
发送时间: 2021年7月29日 18:20
收件人: Li,Rongqing <[email protected]>
抄送: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
主题: Re: [PATCH][RFC] sched/cpuacct: Fix cpuacct charge

Quoted 45 lines · Li RongQing

get_irq_regs only work for current running cpu, but the task, whose cpuacct will be charged, maybe run different cpu, like Cpu 2 wake up a kernel thread to CPU 3, cause CPU 3 task are charged with the following stack

  cpuacct_charge+0xd8/0x100
  update_curr+0xe1/0x1e0
  enqueue_entity+0x144/0x6e0
  enqueue_task_fair+0x93/0x900
  ttwu_do_activate+0x4b/0x90
  try_to_wake_up+0x20b/0x530
  ? update_dl_rq_load_avg+0x10f/0x210
  swake_up_locked.part.1+0x13/0x40
  swake_up_one+0x27/0x40
  rcu_process_callbacks+0x484/0x4f0
  ? run_rebalance_domains_bt+0x5a/0x180
  __do_softirq+0xe3/0x308
  irq_exit+0xf0/0x100
  smp_apic_timer_interrupt+0x74/0x160
  apic_timer_interrupt+0xf/0x20
  </IRQ>
 RIP: 0033:0x456947

so define a get_irq_regs_cpu which returns the required cpu irq registers

BUT it should be not safe, and do not know what it should be like in MIPS?

Fixes dbe9337109c2 "(sched/cpuacct: Fix charge cpuacct.usage_sys)"
---
 include/asm-generic/irq_regs.h | 5 +++++
 kernel/sched/cpuacct.c         | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/include/asm-generic/irq_regs.h index 2e7c6e8..93e2579 100644

include/asm-generic/irq_regs.h +0 -0
@@ -21,6 +21,11 @@ static inline struct pt_regs *get_irq_regs(void)
 	return __this_cpu_read(__irq_regs);
 }
 
Quoted 5 lines

include/asm-generic/irq_regs.h static inline struct pt_regs *get_irq_regs(void) · line 26

Quoted 3 lines
include/asm-generic/irq_regs.h +3 -0
+static inline struct pt_regs *get_irq_regs_cpu(int cpu) {
+	return per_cpu(__irq_regs, cpu);
+}

This primitive just cannot be right... it'll get you some random data.

True

Seem no easy to fix. How about a partial fix

kernel/sched/cpuacct.c +6 -1
index 893eece..48b117e 100644
@@ -340,7 +340,12 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 {
        struct cpuacct *ca;
        int index = CPUACCT_STAT_SYSTEM;
-       struct pt_regs *regs = get_irq_regs() ? : task_pt_regs(tsk);
+       struct pt_regs *regs;
+
+       if (task_cpu(tsk) == raw_smp_processor_id())
+               regs = get_irq_regs() ? : task_pt_regs(tsk);
+       else
+               regs = task_pt_regs(tsk);
 
        if (regs && user_mode(regs))
                index = CPUACCT_STAT_USER;
#5 lore raw
Quoted 64 lines · Li,Rongqing
Quoted 46 lines · Li RongQing
Quoted 45 lines

get_irq_regs only work for current running cpu, but the task, whose cpuacct will be charged, maybe run different cpu, like Cpu 2 wake up a kernel thread to CPU 3, cause CPU 3 task are charged with the following stack

  cpuacct_charge+0xd8/0x100
  update_curr+0xe1/0x1e0
  enqueue_entity+0x144/0x6e0
  enqueue_task_fair+0x93/0x900
  ttwu_do_activate+0x4b/0x90
  try_to_wake_up+0x20b/0x530
  ? update_dl_rq_load_avg+0x10f/0x210
  swake_up_locked.part.1+0x13/0x40
  swake_up_one+0x27/0x40
  rcu_process_callbacks+0x484/0x4f0
  ? run_rebalance_domains_bt+0x5a/0x180
  __do_softirq+0xe3/0x308
  irq_exit+0xf0/0x100
  smp_apic_timer_interrupt+0x74/0x160
  apic_timer_interrupt+0xf/0x20
  </IRQ>
 RIP: 0033:0x456947

so define a get_irq_regs_cpu which returns the required cpu irq registers

BUT it should be not safe, and do not know what it should be like in MIPS?

Fixes dbe9337109c2 "(sched/cpuacct: Fix charge cpuacct.usage_sys)"
---
 include/asm-generic/irq_regs.h | 5 +++++
 kernel/sched/cpuacct.c         | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/include/asm-generic/irq_regs.h index 2e7c6e8..93e2579 100644

include/asm-generic/irq_regs.h +0 -0
@@ -21,6 +21,11 @@ static inline struct pt_regs *get_irq_regs(void)
  return __this_cpu_read(__irq_regs);
 }
 
Quoted 5 lines

include/asm-generic/irq_regs.h static inline struct pt_regs *get_irq_regs(void) · line 26

Quoted 3 lines
include/asm-generic/irq_regs.h +3 -0
+static inline struct pt_regs *get_irq_regs_cpu(int cpu) {
+ return per_cpu(__irq_regs, cpu);
+}

This primitive just cannot be right... it'll get you some random data.

True

Seem no easy to fix. How about a partial fix

kernel/sched/cpuacct.c +0 -0
index 893eece..48b117e 100644
@@ -340,7 +340,12 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 {
        struct cpuacct *ca;
        int index = CPUACCT_STAT_SYSTEM;

kernel/sched/cpuacct.c void cpuacct_charge(struct task_struct *tsk, u64 cputime) · line 351

Quoted 10 lines
kernel/sched/cpuacct.c +6 -1
-       struct pt_regs *regs = get_irq_regs() ? : task_pt_regs(tsk);
+       struct pt_regs *regs;
+
+       if (task_cpu(tsk) == raw_smp_processor_id())
+               regs = get_irq_regs() ? : task_pt_regs(tsk);
+       else
+               regs = task_pt_regs(tsk);
 
        if (regs && user_mode(regs))
                index = CPUACCT_STAT_USER;

It still suffers from task_pt_regs().

Why not make cpuacct use cgroup2's approach? Remember only delta_exec here, then on reading cpuacct.usage_*, use cputime_adjust() to scale the user/sys from cpuacct_account_field().

It's arguably more than just a fix for cgroup1, but there have been a few complaints about this function lately.

Quoted 6 lines
        rcu_read_lock();

        for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
              __this_cpu_add(ca->cpuusage->usages[index], cputime);

        rcu_read_unlock();

By the way, I think the __this_cpu_add() can be wrong in cases like you originally describe. Seems like a bug in 73e6aafd9ea8 ("sched/cpuacct:
Simplify the cpuacct code").

#6 lore raw
Quoted 16 lines
Quoted 16 lines
kernel/sched/cpuacct.c index renamed from kernel/sched/cpuacct.c +0 -0

893eece..48b117e 100644

kernel/sched/cpuacct.c +6 -1
@@ -340,7 +340,12 @@ void cpuacct_charge(struct task_struct *tsk, u64
 cputime)  {
        struct cpuacct *ca;
        int index = CPUACCT_STAT_SYSTEM;
-       struct pt_regs *regs = get_irq_regs() ? : task_pt_regs(tsk);
+       struct pt_regs *regs;
+
+       if (task_cpu(tsk) == raw_smp_processor_id())
+               regs = get_irq_regs() ? : task_pt_regs(tsk);
+       else
+               regs = task_pt_regs(tsk);
 
Quoted 9 lines

kernel/sched/cpuacct.c void cpuacct_charge(struct task_struct *tsk, u64 cputime) · line 351

Quoted 2 lines
kernel/sched/cpuacct.c +0 -0
        if (regs && user_mode(regs))
                index = CPUACCT_STAT_USER;

It still suffers from task_pt_regs().

Why not make cpuacct use cgroup2's approach? Remember only delta_exec here, then on reading cpuacct.usage_*, use cputime_adjust() to scale the user/sys from cpuacct_account_field().

I think your suggestion is reasonable, Could you send a patch

Quoted 5 lines

It's arguably more than just a fix for cgroup1, but there have been a few complaints about this function lately.

Quoted 2 lines
        rcu_read_lock();
Quoted 8 lines
Quoted 4 lines
        for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
              __this_cpu_add(ca->cpuusage->usages[index], cputime);

        rcu_read_unlock();

By the way, I think the __this_cpu_add() can be wrong in cases like you originally describe. Seems like a bug in 73e6aafd9ea8 ("sched/cpuacct:
Simplify the cpuacct code").

We find this issue too.

-Li

#7 lore raw
Quoted 21 lines · Li,Rongqing
Quoted 21 lines
Quoted 8 lines
kernel/sched/cpuacct.c index renamed from kernel/sched/cpuacct.c +0 -0

893eece..48b117e 100644

kernel/sched/cpuacct.c +0 -0
@@ -340,7 +340,12 @@ void cpuacct_charge(struct task_struct *tsk, u64
 cputime)  {
        struct cpuacct *ca;
        int index = CPUACCT_STAT_SYSTEM;

kernel/sched/cpuacct.c void cpuacct_charge(struct task_struct *tsk, u64 · line 351

Quoted 10 lines
kernel/sched/cpuacct.c +6 -1
-       struct pt_regs *regs = get_irq_regs() ? : task_pt_regs(tsk);
+       struct pt_regs *regs;
+
+       if (task_cpu(tsk) == raw_smp_processor_id())
+               regs = get_irq_regs() ? : task_pt_regs(tsk);
+       else
+               regs = task_pt_regs(tsk);
 
        if (regs && user_mode(regs))
                index = CPUACCT_STAT_USER;

It still suffers from task_pt_regs().

Quoted 6 lines
Quoted 4 lines

Why not make cpuacct use cgroup2's approach? Remember only delta_exec here, then on reading cpuacct.usage_*, use cputime_adjust() to scale the user/sys from cpuacct_account_field().

I think your suggestion is reasonable, Could you send a patch

I'll leave that to someone else, got other things going on for now.