Skip to contentSkip to conversation

sched/cpuacct: fix charge percpu cpuusage

PATCH Applied Chengming Zhou 6 participants 18 messages Feb 20, 2022 – Mar 12, 2022

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

PATCH v3 1/3 sched/cpuacct: fix charge percpu cpuusage

The cpuacct_account_field() is always called by the current task itself, so it's ok to use __this_cpu_add() to charge the tick time.

But cpuacct_charge() maybe called by update_curr() in load_balance() on a random CPU, different from the CPU on which the task is running. So __this_cpu_add() will charge that cputime to a random incorrect CPU.

Fixes 73e6aafd9ea8 ("sched/cpuacct: Simplify the cpuacct code")
---
 kernel/sched/cpuacct.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/cpuacct.c +2 -1
index 3d06c5e4220d..307800586ac8 100644
@@ -334,12 +334,13 @@ static struct cftype files[] = {
  */
 void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 {
+	unsigned int cpu = task_cpu(tsk);
 	struct cpuacct *ca;
 
 	rcu_read_lock();
 
 	for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
-		__this_cpu_add(*ca->cpuusage, cputime);
+		*per_cpu_ptr(ca->cpuusage, cpu) += cputime;
 
 	rcu_read_unlock();
 }
Signature · 1 line
2.20.1
#1 lore raw

PATCH v3 2/3 sched/cpuacct: optimize away RCU read lock

Since cpuacct_charge() is called from the scheduler update_curr(), we must already have rq lock held, then the RCU read lock can be optimized away.

And do the same thing in it's wrapper cgroup_account_cputime(), but we can't use lockdep_assert_rq_held() there, which defined in kernel/sched/sched.h.

---
 include/linux/cgroup.h | 2 --
 kernel/sched/cpuacct.c | 4 +---
 2 files changed, 1 insertion(+), 5 deletions(-)
include/linux/cgroup.h +0 -2
index 75c151413fda..9a109c6ac0e0 100644
@@ -791,11 +791,9 @@ static inline void cgroup_account_cputime(struct task_struct *task,
 
 	cpuacct_charge(task, delta_exec);
 
-	rcu_read_lock();
 	cgrp = task_dfl_cgroup(task);
 	if (cgroup_parent(cgrp))
 		__cgroup_account_cputime(cgrp, delta_exec);
-	rcu_read_unlock();
 }
 
 static inline void cgroup_account_cputime_field(struct task_struct *task,
kernel/sched/cpuacct.c +1 -3
index 307800586ac8..f79f88456d72 100644
@@ -337,12 +337,10 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 	unsigned int cpu = task_cpu(tsk);
 	struct cpuacct *ca;
 
-	rcu_read_lock();
+	lockdep_assert_rq_held(cpu_rq(cpu));
 
 	for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
 		*per_cpu_ptr(ca->cpuusage, cpu) += cputime;
-
-	rcu_read_unlock();
 }
 
 /*
Signature · 1 line
2.20.1
merge bot Applied
#2 lore raw

[tip: sched/core] sched/cpuacct: Optimize away RCU read lock

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     dc6e0818bc9a0336d9accf3ea35d146d72aa7a18
Gitweb:        https://git.kernel.org/tip/dc6e0818bc9a0336d9accf3ea35d146d72aa7a18
Author:        Chengming Zhou <[email protected]>
AuthorDate:    Sun, 20 Feb 2022 13:14:25 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 01 Mar 2022 16:18:38 +01:00

sched/cpuacct: Optimize away RCU read lock

Since cpuacct_charge() is called from the scheduler update_curr(), we must already have rq lock held, then the RCU read lock can be optimized away.

And do the same thing in it's wrapper cgroup_account_cputime(), but we can't use lockdep_assert_rq_held() there, which defined in kernel/sched/sched.h.

---
 include/linux/cgroup.h | 2 --
 kernel/sched/cpuacct.c | 4 +---
 2 files changed, 1 insertion(+), 5 deletions(-)
include/linux/cgroup.h +0 -2
index 75c1514..9a109c6 100644
@@ -791,11 +791,9 @@ static inline void cgroup_account_cputime(struct task_struct *task,
 
 	cpuacct_charge(task, delta_exec);
 
-	rcu_read_lock();
 	cgrp = task_dfl_cgroup(task);
 	if (cgroup_parent(cgrp))
 		__cgroup_account_cputime(cgrp, delta_exec);
-	rcu_read_unlock();
 }
 
 static inline void cgroup_account_cputime_field(struct task_struct *task,
kernel/sched/cpuacct.c +1 -3
index 3078005..f79f884 100644
@@ -337,12 +337,10 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 	unsigned int cpu = task_cpu(tsk);
 	struct cpuacct *ca;
 
-	rcu_read_lock();
+	lockdep_assert_rq_held(cpu_rq(cpu));
 
 	for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
 		*per_cpu_ptr(ca->cpuusage, cpu) += cputime;
-
-	rcu_read_unlock();
 }
 
 /*
#3 lore raw

Re: [PATCH v3 2/3] sched/cpuacct: optimize away RCU read lock

In reply to Chengming Zhou
Quoted 10 lines · Chengming Zhou

Since cpuacct_charge() is called from the scheduler update_curr(), we must already have rq lock held, then the RCU read lock can be optimized away.

And do the same thing in it's wrapper cgroup_account_cputime(), but we can't use lockdep_assert_rq_held() there, which defined in kernel/sched/sched.h.

This patch landed recently in linux-next as commit dc6e0818bc9a ("sched/cpuacct: Optimize away RCU read lock"). On my test systems I found that it triggers a following warning in the early boot stage:

Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear) Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00050-gdc6e0818bc9a #11458 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 1
2 locks held by kthreadd/2:
 #0: c1d7972c (&p->pi_lock){....}-{2:2}, at: task_rq_lock+0x30/0x118 #1: ef7b52d0 (&rq->__lock){-...}-{2:2}, at:

raw_spin_rq_lock_nested+0x24/0x34

stack backtrace:
CPU: 0 PID: 2 Comm: kthreadd Not tainted 5.17.0-rc5-00050-gdc6e0818bc9a 
#11458
Hardware name: Samsung Exynos (Flattened Device Tree)
  unwind_backtrace from show_stack+0x10/0x14
  show_stack from dump_stack_lvl+0x58/0x70
  dump_stack_lvl from update_curr+0x1bc/0x35c
  update_curr from dequeue_task_fair+0xb0/0x8e8
  dequeue_task_fair from __do_set_cpus_allowed+0x19c/0x258
  __do_set_cpus_allowed from __set_cpus_allowed_ptr_locked+0x130/0x1d8
  __set_cpus_allowed_ptr_locked from __set_cpus_allowed_ptr+0x48/0x64
  __set_cpus_allowed_ptr from kthreadd+0x44/0x16c
  kthreadd from ret_from_fork+0x14/0x2c
Exception stack(0xc1cb9fb0 to 0xc1cb9ff8)

9fa0:                                     00000000 00000000 00000000 00000000
9fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
9fe0: 00000000 00000000 00000000 00000000 00000013 00000000
CPU0: thread -1, cpu 0, socket 9, mpidr 80000900
cblist_init_generic: Setting adjustable number of callback queues.
cblist_init_generic: Setting shift to 1 and lim to 1. Running RCU-tasks wait API self tests
Setting up static identity map for 0x40100000 - 0x40100060
rcu: Hierarchical SRCU implementation.

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00050-gdc6e0818bc9a #11458 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 1
1 lock held by migration/0/13:
 #0: ef7b52d0 (&rq->__lock){-...}-{2:2}, at:

raw_spin_rq_lock_nested+0x24/0x34

stack backtrace:
CPU: 0 PID: 13 Comm: migration/0 Not tainted 
5.17.0-rc5-00050-gdc6e0818bc9a #11458
Hardware name: Samsung Exynos (Flattened Device Tree)

Stopper: 0x0 <- 0x0
 unwind_backtrace from show_stack+0x10/0x14 show_stack from dump_stack_lvl+0x58/0x70 dump_stack_lvl from put_prev_task_stop+0x16c/0x25c put_prev_task_stop from __schedule+0x698/0x964 __schedule from schedule+0x54/0xe0 schedule from smpboot_thread_fn+0x218/0x288 smpboot_thread_fn from kthread+0xf0/0x134 kthread from ret_from_fork+0x14/0x2c

Exception stack(0xc1ccffb0 to 0xc1ccfff8)

ffa0:                                     00000000 00000000 00000000 00000000
ffc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
ffe0: 00000000 00000000 00000000 00000000 00000013 00000000
smp: Bringing up secondary CPUs ...
CPU1: thread -1, cpu 1, socket 9, mpidr 80000901
CPU1: Spectre v2: using BPIALL workaround
smp: Brought up 1 node, 2 CPUs
SMP: Total of 2 processors activated (96.00 BogoMIPS).

The above log comes from ARM 32bit Samsung Exnyos4210 based Trats board.

Quoted 29 lines
---
  include/linux/cgroup.h | 2 --
  kernel/sched/cpuacct.c | 4 +---
  2 files changed, 1 insertion(+), 5 deletions(-)
include/linux/cgroup.h +0 -2
index 75c151413fda..9a109c6ac0e0 100644
@@ -791,11 +791,9 @@ static inline void cgroup_account_cputime(struct task_struct *task,
 
  	cpuacct_charge(task, delta_exec);
 
-	rcu_read_lock();
  	cgrp = task_dfl_cgroup(task);
  	if (cgroup_parent(cgrp))
  		__cgroup_account_cputime(cgrp, delta_exec);
-	rcu_read_unlock();
  }
 
  static inline void cgroup_account_cputime_field(struct task_struct *task,
kernel/sched/cpuacct.c +0 -0
index 307800586ac8..f79f88456d72 100644
@@ -337,12 +337,10 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
  	unsigned int cpu = task_cpu(tsk);
  	struct cpuacct *ca;
 

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

Quoted 10 lines
kernel/sched/cpuacct.c +1 -3
-	rcu_read_lock();
+	lockdep_assert_rq_held(cpu_rq(cpu));
 
  	for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
  		*per_cpu_ptr(ca->cpuusage, cpu) += cputime;
-
-	rcu_read_unlock();
  }
 
  /*

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
maintainer
#4 lore raw

Re: [PATCH v3 2/3] sched/cpuacct: optimize away RCU read lock

Quoted 24 lines · Marek Szyprowski
Quoted 10 lines · Chengming Zhou

Since cpuacct_charge() is called from the scheduler update_curr(), we must already have rq lock held, then the RCU read lock can be optimized away.

And do the same thing in it's wrapper cgroup_account_cputime(), but we can't use lockdep_assert_rq_held() there, which defined in kernel/sched/sched.h.

This patch landed recently in linux-next as commit dc6e0818bc9a ("sched/cpuacct: Optimize away RCU read lock"). On my test systems I found that it triggers a following warning in the early boot stage:

Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear) Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround

Quoted 5 lines

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00050-gdc6e0818bc9a #11458 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

Arguably, with the flavours folded again, rcu_dereference_check() ought to default include rcu_read_lock_sched_held() or its equivalent I suppose.

Paul?

#5 lore raw

Re: [PATCH v3 2/3] sched/cpuacct: optimize away RCU read lock

Quoted 31 lines · Peter Zijlstra
Quoted 24 lines · Marek Szyprowski
Quoted 10 lines · Chengming Zhou

Since cpuacct_charge() is called from the scheduler update_curr(), we must already have rq lock held, then the RCU read lock can be optimized away.

And do the same thing in it's wrapper cgroup_account_cputime(), but we can't use lockdep_assert_rq_held() there, which defined in kernel/sched/sched.h.

This patch landed recently in linux-next as commit dc6e0818bc9a ("sched/cpuacct: Optimize away RCU read lock"). On my test systems I found that it triggers a following warning in the early boot stage:

Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear) Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround

Quoted 5 lines

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00050-gdc6e0818bc9a #11458 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

Quoted 5 lines

Arguably, with the flavours folded again, rcu_dereference_check() ought to default include rcu_read_lock_sched_held() or its equivalent I suppose.

Paul?

That would reduce the number of warnings, but it also would hide bugs.

So, are you sure you really want this?

Thanx, Paul

#6 lore raw

Re: [PATCH v3 2/3] sched/cpuacct: optimize away RCU read lock

Quoted 36 lines · Paul E. McKenney
Quoted 35 lines · Peter Zijlstra
Quoted 24 lines · Marek Szyprowski
Quoted 10 lines · Chengming Zhou

Since cpuacct_charge() is called from the scheduler update_curr(), we must already have rq lock held, then the RCU read lock can be optimized away.

And do the same thing in it's wrapper cgroup_account_cputime(), but we can't use lockdep_assert_rq_held() there, which defined in kernel/sched/sched.h.

This patch landed recently in linux-next as commit dc6e0818bc9a ("sched/cpuacct: Optimize away RCU read lock"). On my test systems I found that it triggers a following warning in the early boot stage:

Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear) Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround

Quoted 5 lines

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00050-gdc6e0818bc9a #11458 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

Arguably, with the flavours folded again, rcu_dereference_check() ought to default include rcu_read_lock_sched_held() or its equivalent I suppose.

Quoted 5 lines
Quoted 1 line

Paul?

That would reduce the number of warnings, but it also would hide bugs.

So, are you sure you really want this?

Of course, if you are designing a use case that really expects multiple types of readers...

Another approach might be rcu_dereference_brs(), but hopefully with a better name, that checks for either rcu_read_lock(), disabled BH, or disabled preemption. Or if you are looking only for rcu_read_lock() or disabled preemption, rcu_dereference_rs(), again hopefully with a better name.

Is that what you had in mind?

Thanx, Paul

maintainer
#7 lore raw

Re: [PATCH v3 2/3] sched/cpuacct: optimize away RCU read lock

In reply to Paul E. McKenney
Quoted 36 lines · Paul E. McKenney
Quoted 35 lines · Peter Zijlstra
Quoted 24 lines · Marek Szyprowski
Quoted 10 lines · Chengming Zhou

Since cpuacct_charge() is called from the scheduler update_curr(), we must already have rq lock held, then the RCU read lock can be optimized away.

And do the same thing in it's wrapper cgroup_account_cputime(), but we can't use lockdep_assert_rq_held() there, which defined in kernel/sched/sched.h.

This patch landed recently in linux-next as commit dc6e0818bc9a ("sched/cpuacct: Optimize away RCU read lock"). On my test systems I found that it triggers a following warning in the early boot stage:

Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear) Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround

Quoted 5 lines

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00050-gdc6e0818bc9a #11458 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

Arguably, with the flavours folded again, rcu_dereference_check() ought to default include rcu_read_lock_sched_held() or its equivalent I suppose.

Quoted 5 lines
Quoted 1 line

Paul?

That would reduce the number of warnings, but it also would hide bugs.

So, are you sure you really want this?

I don't understand... Since the flavours got merged regular RCU has it's quescent state held off by preempt_disable. So how can relying on that cause bugs?

And if we can rely on that, then surely rcu_dereferenced_check() ought to play by the same rules, otherwise we get silly warnings like these at hand.

Specifically, we removed the rcu_read_lock() here because this has rq->lock held, which is a raw_spinlock_t which very much implies preempt disable, on top of that, it's also an IRQ-safe lock and thus IRQs will be disabled.

There is no possible way for RCU to make progress.

#8 lore raw

Re: [PATCH v3 2/3] sched/cpuacct: optimize away RCU read lock

Quoted 39 lines · Peter Zijlstra
Quoted 38 lines · Paul E. McKenney
Quoted 36 lines · Peter Zijlstra
Quoted 24 lines · Marek Szyprowski
Quoted 10 lines · Chengming Zhou

Since cpuacct_charge() is called from the scheduler update_curr(), we must already have rq lock held, then the RCU read lock can be optimized away.

And do the same thing in it's wrapper cgroup_account_cputime(), but we can't use lockdep_assert_rq_held() there, which defined in kernel/sched/sched.h.

This patch landed recently in linux-next as commit dc6e0818bc9a ("sched/cpuacct: Optimize away RCU read lock"). On my test systems I found that it triggers a following warning in the early boot stage:

Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear) Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround

Quoted 5 lines

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00050-gdc6e0818bc9a #11458 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

Arguably, with the flavours folded again, rcu_dereference_check() ought to default include rcu_read_lock_sched_held() or its equivalent I suppose.

Paul?

Quoted 7 lines
Quoted 3 lines

That would reduce the number of warnings, but it also would hide bugs.

So, are you sure you really want this?

I don't understand... Since the flavours got merged regular RCU has it's quescent state held off by preempt_disable. So how can relying on that cause bugs?

Somene forgets an rcu_read_lock() and there happens to be something like a preempt_disable() that by coincidence covers that particular rcu_dereference(). The kernel therefore doesn't complain. That someone goes on to other things, maybe even posthumously. Then some time later the preempt_disable() goes away, for good and sufficient reasons.

Good luck figuring out where to put the needed rcu_read_lock() and rcu_read_unlock().

Quoted 10 lines

And if we can rely on that, then surely rcu_dereferenced_check() ought to play by the same rules, otherwise we get silly warnings like these at hand.

Specifically, we removed the rcu_read_lock() here because this has rq->lock held, which is a raw_spinlock_t which very much implies preempt disable, on top of that, it's also an IRQ-safe lock and thus IRQs will be disabled.

There is no possible way for RCU to make progress.

Then let's have that particular rcu_dereference_check() explicitly state what it needs, which seems to be either rcu_read_lock() on the one hand. Right now, that could be just this:

	p = rcu_dereference_check(gp, rcu_read_lock_sched_held());

Or am I missing something here?

Thanx, Paul

maintainer
#9 lore raw

Re: [PATCH v3 2/3] sched/cpuacct: optimize away RCU read lock

Quoted 16 lines · Paul E. McKenney
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 1 line

./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

Arguably, with the flavours folded again, rcu_dereference_check() ought to default include rcu_read_lock_sched_held() or its equivalent I suppose.

Paul?

Quoted 7 lines
Quoted 3 lines

That would reduce the number of warnings, but it also would hide bugs.

So, are you sure you really want this?

I don't understand... Since the flavours got merged regular RCU has it's quescent state held off by preempt_disable. So how can relying on that cause bugs?

Quoted 8 lines

Somene forgets an rcu_read_lock() and there happens to be something like a preempt_disable() that by coincidence covers that particular rcu_dereference(). The kernel therefore doesn't complain. That someone goes on to other things, maybe even posthumously. Then some time later the preempt_disable() goes away, for good and sufficient reasons.

Good luck figuring out where to put the needed rcu_read_lock() and rcu_read_unlock().

Well, that's software engineering for you. Also in that case the warning will work as expected. Then figuring out how to fix it is not the problem of the warning -- that worked as advertised.

(also, I don't think it'll be too hard, you just gotta figure out which object is rcu protected -- the warning gives you this, where the lookup happens -- again the warning helps, and how long it's used for, all relatively well definted things)

I don't see a problem. No bugs hidden.

Quoted 11 lines
Quoted 10 lines

And if we can rely on that, then surely rcu_dereferenced_check() ought to play by the same rules, otherwise we get silly warnings like these at hand.

Specifically, we removed the rcu_read_lock() here because this has rq->lock held, which is a raw_spinlock_t which very much implies preempt disable, on top of that, it's also an IRQ-safe lock and thus IRQs will be disabled.

There is no possible way for RCU to make progress.

Quoted 7 lines

Then let's have that particular rcu_dereference_check() explicitly state what it needs, which seems to be either rcu_read_lock() on the one hand. Right now, that could be just this:

	p = rcu_dereference_check(gp, rcu_read_lock_sched_held());

Or am I missing something here?

That will work; I just don't agree with it. Per the rules of RCU it is entirely correct to mix rcu_read_lock() and preempt_disable() (or anything that implies the same). So I strongly feel that rcu_dereference() should not warn about obviously correct code. Why would we need to special case this ?

#10 lore raw

Re: [PATCH v3 2/3] sched/cpuacct: optimize away RCU read lock

Quoted 24 lines · Peter Zijlstra
Quoted 22 lines · Paul E. McKenney
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 1 line

./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

Arguably, with the flavours folded again, rcu_dereference_check() ought to default include rcu_read_lock_sched_held() or its equivalent I suppose.

Paul?

Quoted 7 lines
Quoted 3 lines

That would reduce the number of warnings, but it also would hide bugs.

So, are you sure you really want this?

I don't understand... Since the flavours got merged regular RCU has it's quescent state held off by preempt_disable. So how can relying on that cause bugs?

Somene forgets an rcu_read_lock() and there happens to be something like a preempt_disable() that by coincidence covers that particular rcu_dereference(). The kernel therefore doesn't complain. That someone goes on to other things, maybe even posthumously. Then some time later the preempt_disable() goes away, for good and sufficient reasons.

Quoted 4 lines
Quoted 2 lines

Good luck figuring out where to put the needed rcu_read_lock() and rcu_read_unlock().

Well, that's software engineering for you.

My point exactly!!!

Quoted 8 lines

Also in that case the warning
will work as expected. Then figuring out how to fix it is not the problem of the warning -- that worked as advertised.

(also, I don't think it'll be too hard, you just gotta figure out which object is rcu protected -- the warning gives you this, where the lookup happens -- again the warning helps, and how long it's used for, all relatively well definted things)

Without in any way agreeing with that assessment of difficulty, especially in the general case... It is -way- easier just to tell RCU what your design rules are for the code in question.

Quoted 1 line

I don't see a problem. No bugs hidden.

C'mon, Peter!

There really was a bug hidden. That someone intended to add some calls to rcu_read_lock() and rcu_read_unlock() in the proper places. Their failure to add them really was a bug.

That bug was hidden by: (1) There being a preempt_disable() or whatever that by coincidence happened to be covering the part of the code containing the rcu_dereference() and (2) Your proposed change that would make rcu_dereference() unable to detect that bug.

And that bug can be quite bad. Given your proposed change, RCU cannot detect this bug:

	/* Preemption is enabled. */
	/* There should be an rcu_read_lock() here. */
	preempt_disable();
	p = rcu_dereference(gp);
	do_something_with(p);
	preempt_enable();
	/* Without the rcu_read_lock(), *p is history. */
	do_something_else_with(p);
	/* There should be an rcu_read_unlock() here. */
Quoted 17 lines
Quoted 17 lines
Quoted 10 lines

And if we can rely on that, then surely rcu_dereferenced_check() ought to play by the same rules, otherwise we get silly warnings like these at hand.

Specifically, we removed the rcu_read_lock() here because this has rq->lock held, which is a raw_spinlock_t which very much implies preempt disable, on top of that, it's also an IRQ-safe lock and thus IRQs will be disabled.

There is no possible way for RCU to make progress.

Then let's have that particular rcu_dereference_check() explicitly state what it needs, which seems to be either rcu_read_lock() on the one hand. Right now, that could be just this:

	p = rcu_dereference_check(gp, rcu_read_lock_sched_held());
Quoted 7 lines
Quoted 1 line

Or am I missing something here?

That will work; I just don't agree with it. Per the rules of RCU it is entirely correct to mix rcu_read_lock() and preempt_disable() (or anything that implies the same). So I strongly feel that rcu_dereference() should not warn about obviously correct code. Why would we need to special case this ?

This use case might well be entirely correct, but it is most certainly not the common case.

Therefore, my answer to this requested chance in rcu_dereference() semantics is "no".

Thanx, Paul

#11 lore raw

Re: [External] Re: [PATCH v3 2/3] sched/cpuacct: optimize away RCU read lock

In reply to Marek Szyprowski
Quoted 9 lines · Marek Szyprowski
Quoted 8 lines · Chengming Zhou

Since cpuacct_charge() is called from the scheduler update_curr(), we must already have rq lock held, then the RCU read lock can be optimized away.

And do the same thing in it's wrapper cgroup_account_cputime(), but we can't use lockdep_assert_rq_held() there, which defined in kernel/sched/sched.h.

Quoted 6 lines
Quoted 2 lines

This patch landed recently in linux-next as commit dc6e0818bc9a ("sched/cpuacct: Optimize away RCU read lock"). On my test systems I found that it triggers a following warning in the early boot stage:

Hi, thanks for the report. I've send a fix patch[1] for review.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 132 lines

Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear) Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00050-gdc6e0818bc9a #11458 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 1
2 locks held by kthreadd/2:
 #0: c1d7972c (&p->pi_lock){....}-{2:2}, at: task_rq_lock+0x30/0x118 #1: ef7b52d0 (&rq->__lock){-...}-{2:2}, at:

raw_spin_rq_lock_nested+0x24/0x34

stack backtrace:
CPU: 0 PID: 2 Comm: kthreadd Not tainted 5.17.0-rc5-00050-gdc6e0818bc9a 
#11458
Hardware name: Samsung Exynos (Flattened Device Tree)
  unwind_backtrace from show_stack+0x10/0x14
  show_stack from dump_stack_lvl+0x58/0x70
  dump_stack_lvl from update_curr+0x1bc/0x35c
  update_curr from dequeue_task_fair+0xb0/0x8e8
  dequeue_task_fair from __do_set_cpus_allowed+0x19c/0x258
  __do_set_cpus_allowed from __set_cpus_allowed_ptr_locked+0x130/0x1d8
  __set_cpus_allowed_ptr_locked from __set_cpus_allowed_ptr+0x48/0x64
  __set_cpus_allowed_ptr from kthreadd+0x44/0x16c
  kthreadd from ret_from_fork+0x14/0x2c
Exception stack(0xc1cb9fb0 to 0xc1cb9ff8)

9fa0:                                     00000000 00000000 00000000 00000000
9fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
9fe0: 00000000 00000000 00000000 00000000 00000013 00000000
CPU0: thread -1, cpu 0, socket 9, mpidr 80000900
cblist_init_generic: Setting adjustable number of callback queues.
cblist_init_generic: Setting shift to 1 and lim to 1. Running RCU-tasks wait API self tests
Setting up static identity map for 0x40100000 - 0x40100060
rcu: Hierarchical SRCU implementation.

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00050-gdc6e0818bc9a #11458 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 1
1 lock held by migration/0/13:
 #0: ef7b52d0 (&rq->__lock){-...}-{2:2}, at:

raw_spin_rq_lock_nested+0x24/0x34

stack backtrace:
CPU: 0 PID: 13 Comm: migration/0 Not tainted 
5.17.0-rc5-00050-gdc6e0818bc9a #11458
Hardware name: Samsung Exynos (Flattened Device Tree)

Stopper: 0x0 <- 0x0
 unwind_backtrace from show_stack+0x10/0x14 show_stack from dump_stack_lvl+0x58/0x70 dump_stack_lvl from put_prev_task_stop+0x16c/0x25c put_prev_task_stop from __schedule+0x698/0x964 __schedule from schedule+0x54/0xe0 schedule from smpboot_thread_fn+0x218/0x288 smpboot_thread_fn from kthread+0xf0/0x134 kthread from ret_from_fork+0x14/0x2c

Exception stack(0xc1ccffb0 to 0xc1ccfff8)

ffa0:                                     00000000 00000000 00000000 00000000
ffc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
ffe0: 00000000 00000000 00000000 00000000 00000013 00000000
smp: Bringing up secondary CPUs ...
CPU1: thread -1, cpu 1, socket 9, mpidr 80000901
CPU1: Spectre v2: using BPIALL workaround
smp: Brought up 1 node, 2 CPUs
SMP: Total of 2 processors activated (96.00 BogoMIPS).

The above log comes from ARM 32bit Samsung Exnyos4210 based Trats board.

Quoted 29 lines
---
  include/linux/cgroup.h | 2 --
  kernel/sched/cpuacct.c | 4 +---
  2 files changed, 1 insertion(+), 5 deletions(-)
include/linux/cgroup.h +0 -2
index 75c151413fda..9a109c6ac0e0 100644
@@ -791,11 +791,9 @@ static inline void cgroup_account_cputime(struct task_struct *task,
 
  	cpuacct_charge(task, delta_exec);
 
-	rcu_read_lock();
  	cgrp = task_dfl_cgroup(task);
  	if (cgroup_parent(cgrp))
  		__cgroup_account_cputime(cgrp, delta_exec);
-	rcu_read_unlock();
  }
 
  static inline void cgroup_account_cputime_field(struct task_struct *task,
kernel/sched/cpuacct.c +0 -0
index 307800586ac8..f79f88456d72 100644
@@ -337,12 +337,10 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
  	unsigned int cpu = task_cpu(tsk);
  	struct cpuacct *ca;
 

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

Quoted 10 lines
kernel/sched/cpuacct.c +1 -3
-	rcu_read_lock();
+	lockdep_assert_rq_held(cpu_rq(cpu));
 
  	for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
  		*per_cpu_ptr(ca->cpuusage, cpu) += cputime;
-
-	rcu_read_unlock();
  }
 
  /*

Best regards

#12 lore raw

PATCH v3 3/3 sched/cpuacct: remove redundant RCU read lock

In reply to Chengming Zhou

The cpuacct_account_field() and it's cgroup v2 wrapper cgroup_account_cputime_field() is only called from cputime in task_group_account_field(), which is already in RCU read-side critical section. So remove these redundant RCU read lock.

---
 include/linux/cgroup.h | 2 --
 kernel/sched/cpuacct.c | 2 --
 2 files changed, 4 deletions(-)
include/linux/cgroup.h +0 -2
index 9a109c6ac0e0..1e356c222756 100644
@@ -804,11 +804,9 @@ static inline void cgroup_account_cputime_field(struct task_struct *task,
 
 	cpuacct_account_field(task, index, delta_exec);
 
-	rcu_read_lock();
 	cgrp = task_dfl_cgroup(task);
 	if (cgroup_parent(cgrp))
 		__cgroup_account_cputime_field(cgrp, index, delta_exec);
-	rcu_read_unlock();
 }
 
 #else	/* CONFIG_CGROUPS */
kernel/sched/cpuacct.c +0 -2
index f79f88456d72..d269ede84e85 100644
@@ -352,10 +352,8 @@ void cpuacct_account_field(struct task_struct *tsk, int index, u64 val)
 {
 	struct cpuacct *ca;
 
-	rcu_read_lock();
 	for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
 		__this_cpu_add(ca->cpustat->cpustat[index], val);
-	rcu_read_unlock();
 }
 
 struct cgroup_subsys cpuacct_cgrp_subsys = {
Signature · 1 line
2.20.1
merge bot Applied
#13 lore raw

[tip: sched/core] sched/cpuacct: Remove redundant RCU read lock

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     3eba0505d03a9c1eb30d40c2330c0880b22d1b3a
Gitweb:        https://git.kernel.org/tip/3eba0505d03a9c1eb30d40c2330c0880b22d1b3a
Author:        Chengming Zhou <[email protected]>
AuthorDate:    Sun, 20 Feb 2022 13:14:26 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 01 Mar 2022 16:18:38 +01:00

sched/cpuacct: Remove redundant RCU read lock

The cpuacct_account_field() and it's cgroup v2 wrapper cgroup_account_cputime_field() is only called from cputime in task_group_account_field(), which is already in RCU read-side critical section. So remove these redundant RCU read lock.

---
 include/linux/cgroup.h | 2 --
 kernel/sched/cpuacct.c | 2 --
 2 files changed, 4 deletions(-)
include/linux/cgroup.h +0 -2
index 9a109c6..1e356c2 100644
@@ -804,11 +804,9 @@ static inline void cgroup_account_cputime_field(struct task_struct *task,
 
 	cpuacct_account_field(task, index, delta_exec);
 
-	rcu_read_lock();
 	cgrp = task_dfl_cgroup(task);
 	if (cgroup_parent(cgrp))
 		__cgroup_account_cputime_field(cgrp, index, delta_exec);
-	rcu_read_unlock();
 }
 
 #else	/* CONFIG_CGROUPS */
kernel/sched/cpuacct.c +0 -2
index f79f884..d269ede 100644
@@ -352,10 +352,8 @@ void cpuacct_account_field(struct task_struct *tsk, int index, u64 val)
 {
 	struct cpuacct *ca;
 
-	rcu_read_lock();
 	for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
 		__this_cpu_add(ca->cpustat->cpustat[index], val);
-	rcu_read_unlock();
 }
 
 struct cgroup_subsys cpuacct_cgrp_subsys = {
#14 lore raw

Re: [PATCH v3 3/3] sched/cpuacct: remove redundant RCU read lock

In reply to Chengming Zhou
Quoted 7 lines · Chengming Zhou

The cpuacct_account_field() and it's cgroup v2 wrapper cgroup_account_cputime_field() is only called from cputime in task_group_account_field(), which is already in RCU read-side critical section. So remove these redundant RCU read lock.

This patch landed recently in linux-next as commit 3eba0505d03a ("sched/cpuacct: Remove redundant RCU read lock"). On my test systems I found that it triggers a following warning in the early boot stage:

Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear) Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround
CPU0: thread -1, cpu 0, socket 9, mpidr 80000900
cblist_init_generic: Setting adjustable number of callback queues.

=============================
WARNING: suspicious RCU usage 5.17.0-rc5-00052-g167ee9b08bed #11459 Not tainted
-----------------------------
./include/linux/cgroup.h:481 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 1
no locks held by swapper/0/1.

stack backtrace:
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.17.0-rc5-00052-g167ee9b08bed 
#11459
Hardware name: Samsung Exynos (Flattened Device Tree)
  unwind_backtrace from show_stack+0x10/0x14
  show_stack from dump_stack_lvl+0x58/0x70
  dump_stack_lvl from account_system_index_time+0x13c/0x148
  account_system_index_time from account_system_time+0x78/0xa0
  account_system_time from update_process_times+0x50/0xc4
  update_process_times from tick_handle_periodic+0x1c/0x94
  tick_handle_periodic from exynos4_mct_tick_isr+0x30/0x38
  exynos4_mct_tick_isr from __handle_irq_event_percpu+0x74/0x3ac
  __handle_irq_event_percpu from handle_irq_event_percpu+0xc/0x38
  handle_irq_event_percpu from handle_irq_event+0x38/0x5c
  handle_irq_event from handle_fasteoi_irq+0xc4/0x180
  handle_fasteoi_irq from generic_handle_domain_irq+0x44/0x88
  generic_handle_domain_irq from gic_handle_irq+0x88/0xac
  gic_handle_irq from generic_handle_arch_irq+0x58/0x78
  generic_handle_arch_irq from __irq_svc+0x54/0x88
Exception stack(0xc1cafea0 to 0xc1cafee8)

fea0: 00000000 c0f33ac4 00000001 00000129 60000053 c127b458 00000008 2e64e000
fec0: ef7bd5fc c127af84 c1209048 c1208f10 00000000 c1cafef0 c0b7e25c c0b8976c
fee0: 20000053 ffffffff
 __irq_svc from _raw_spin_unlock_irqrestore+0x2c/0x60 _raw_spin_unlock_irqrestore from

cblist_init_generic.constprop.14+0x298/0x328
  cblist_init_generic.constprop.14 from rcu_init_tasks_generic+0x18/0x110
  rcu_init_tasks_generic from kernel_init_freeable+0xa0/0x214
  kernel_init_freeable from kernel_init+0x18/0x12c
  kernel_init from ret_from_fork+0x14/0x2c
Exception stack(0xc1caffb0 to 0xc1cafff8)

ffa0:                                     00000000 00000000 00000000 00000000
ffc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
ffe0: 00000000 00000000 00000000 00000000 00000013 00000000
cblist_init_generic: Setting shift to 1 and lim to 1. Running RCU-tasks wait API self tests
Setting up static identity map for 0x40100000 - 0x40100060
rcu: Hierarchical SRCU implementation.
smp: Bringing up secondary CPUs ...
CPU1: thread -1, cpu 1, socket 9, mpidr 80000901
CPU1: Spectre v2: using BPIALL workaround
smp: Brought up 1 node, 2 CPUs
SMP: Total of 2 processors activated (96.00 BogoMIPS).
CPU: All CPU(s) started in SVC mode.

The above log comes from ARM 32bit Samsung Exnyos4210 based Trats board. To get above log I reverted commit dc6e0818bc9a ("sched/cpuacct:
Optimize away RCU read lock"), because it triggered a similar warning.

Quoted 26 lines
---
  include/linux/cgroup.h | 2 --
  kernel/sched/cpuacct.c | 2 --
  2 files changed, 4 deletions(-)
include/linux/cgroup.h +0 -2
index 9a109c6ac0e0..1e356c222756 100644
@@ -804,11 +804,9 @@ static inline void cgroup_account_cputime_field(struct task_struct *task,
 
  	cpuacct_account_field(task, index, delta_exec);
 
-	rcu_read_lock();
  	cgrp = task_dfl_cgroup(task);
  	if (cgroup_parent(cgrp))
  		__cgroup_account_cputime_field(cgrp, index, delta_exec);
-	rcu_read_unlock();
  }
 
  #else	/* CONFIG_CGROUPS */
kernel/sched/cpuacct.c +0 -0
index f79f88456d72..d269ede84e85 100644

kernel/sched/cpuacct.c void cpuacct_account_field(struct task_struct *tsk, int index, u64 val) · line 359

Quoted 10 lines
kernel/sched/cpuacct.c +0 -2
@@ -352,10 +352,8 @@ void cpuacct_account_field(struct task_struct *tsk, int index, u64 val)
  {
  	struct cpuacct *ca;
 
-	rcu_read_lock();
  	for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
  		__this_cpu_add(ca->cpustat->cpustat[index], val);
-	rcu_read_unlock();
  }
 
  struct cgroup_subsys cpuacct_cgrp_subsys = {

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
maintainer Acked-by on v3 1/3
#15 lore raw
In reply to Chengming Zhou
Quoted 10 lines · Chengming Zhou

The cpuacct_account_field() is always called by the current task itself, so it's ok to use __this_cpu_add() to charge the tick time.

But cpuacct_charge() maybe called by update_curr() in load_balance() on a random CPU, different from the CPU on which the task is running. So __this_cpu_add() will charge that cputime to a random incorrect CPU.

Fixes 73e6aafd9ea8 ("sched/cpuacct: Simplify the cpuacct code")

For all three patches,

Thanks.

Signature · 1 line
tejun
maintainer
#16 lore raw
Quoted 8 lines · Tejun Heo
Quoted 7 lines · Chengming Zhou

The cpuacct_account_field() is always called by the current task itself, so it's ok to use __this_cpu_add() to charge the tick time.

But cpuacct_charge() maybe called by update_curr() in load_balance() on a random CPU, different from the CPU on which the task is running. So __this_cpu_add() will charge that cputime to a random incorrect CPU.

Quoted 7 lines
Quoted 3 lines
Fixes 73e6aafd9ea8 ("sched/cpuacct: Simplify the cpuacct code")

For all three patches,

Thanks!

merge bot Applied
#17 lore raw

[tip: sched/core] sched/cpuacct: Fix charge percpu cpuusage

In reply to Chengming Zhou
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     248cc9993d1cc12b8e9ed716cc3fc09f6c3517dd
Gitweb:        https://git.kernel.org/tip/248cc9993d1cc12b8e9ed716cc3fc09f6c3517dd
Author:        Chengming Zhou <[email protected]>
AuthorDate:    Sun, 20 Feb 2022 13:14:24 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 01 Mar 2022 16:18:37 +01:00

sched/cpuacct: Fix charge percpu cpuusage

The cpuacct_account_field() is always called by the current task itself, so it's ok to use __this_cpu_add() to charge the tick time.

But cpuacct_charge() maybe called by update_curr() in load_balance() on a random CPU, different from the CPU on which the task is running. So __this_cpu_add() will charge that cputime to a random incorrect CPU.

---
 kernel/sched/cpuacct.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/cpuacct.c +2 -1
index 3d06c5e..3078005 100644
@@ -334,12 +334,13 @@ static struct cftype files[] = {
  */
 void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 {
+	unsigned int cpu = task_cpu(tsk);
 	struct cpuacct *ca;
 
 	rcu_read_lock();
 
 	for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
-		__this_cpu_add(*ca->cpuusage, cputime);
+		*per_cpu_ptr(ca->cpuusage, cpu) += cputime;
 
 	rcu_read_unlock();
 }