Skip to contentSkip to conversation

Re: 回复:回复:Re:

DISCUSSION Paul E. McKenney 1 participant 1 message Oct 6, 2022 1 message not in the archive RCU

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Paul E. McKenney

author maintainer
#1 lore raw
Quoted 9 lines · On Fri, Oct 07, 2022 at 12:38:56AM +0800, 5486 wrote:

In fact suppose there are for cpuscpu0 cpu1 cpu2 cpu3

the value of rcu_barrier_cpu_count may be

1  cpu0 2 -> 1 cpu1 2 -> 1 cpu2 2 -> 1
then on_each_cpu finished rcu_barrier_cpu_count decreased to be 0
then complete(&rcu_barrier_completion); rcu_barrier finished.but rcu_barrier_func of cpu3 may not have been executed.

Note that the last argument to on_each_cpu() is "1", that is, on_each_cpu() does not return until rcu_barrier_func() has executed on all CPUs.

Can your scenario happen?

Thanx, Paul

Quoted 106 lines

原始邮件

发件人:"Paul E. McKenney"< [email protected] &gt;;

发件时间:2022/10/6 19:57

收件人:"5486"< [email protected] &gt;;

抄送人:"rcu"< [email protected] &gt;;

主题:回复:Re:

On Thu, Oct 06, 2022 at 05:56:35PM +0800, 5486 wrote:
&gt; My problem is &nbsp;some grace period can take very little time shorter than the one iteration of on_each_cpu itself。So the complete() can execute on one cpu before the last of all. &nbsp;

And that is the bug. Very good!

&gt; but now
&gt; The key is on_each_cpu &nbsp;,it is really clever that on_each_cpu &nbsp;will prevent completion of any grace periods. Concealed Mechanism! &gt; Is it?
&gt; In this ancient version code have &nbsp;real race-condition bugs .I will take some time to study to find it and reply to you. &gt; it is about on_each_cpu have not prohibit preemption? ,I did not cover more things related to the operationg system.

Suppose the code instead read as follows?

void rcu_barrier(void)

{
	BUG_ON(in_interrupt());
	/* Take cpucontrol semaphore to protect against CPU hotplug */
	down(&amp;rcu_barrier_sema);
	init_completion(&amp;rcu_barrier_completion);
	atomic_set(&amp;rcu_barrier_cpu_count, 1);
	on_each_cpu(rcu_barrier_func, NULL, 0, 1);
	if (atomic_dec_and_test(&amp;rcu_barrier_cpu_count))
		complete(&amp;rcu_barrier_completion);
	wait_for_completion(&amp;rcu_barrier_completion);
	up(&amp;rcu_barrier_sema);
}

Would that work? Or are there additional bugs?

Thanx, Paul

&gt; Chao,Li
&gt;
&gt;
&gt; 原始邮件
&gt;
&gt;
&gt;
&gt; 发件人:"Paul E. McKenney"< [email protected] &gt;;
&gt;
&gt; 发件时间:2022/10/5 21:01
&gt;
&gt; 收件人:"5486"< [email protected] &gt;;
&gt;
&gt; 抄送人:"rcu"< [email protected] &gt;;
&gt;
&gt; 主题:Re: Dear Pualmack help i have a problem with rcu code
&gt;
&gt;
&gt; On Tue, Oct 04, 2022 at 03:04:19AM +0800, 5486 wrote:
&gt; &gt; Dear&nbsp;Pualmack i have problem.
&gt; &gt; I have been reading the patch
&gt; &gt; https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/kernel?id=ab4720ec76b756e1f8705e207a7b392b0453afd6
&gt; &gt;
&gt; &gt;
&gt; &gt;
&gt; &gt; + wait_for_completion(&amp;rcu_barrier_completion);
&gt; &gt;
&gt; &gt; How this guarantees all completes to be done ?
&gt; &gt; I learn one wait_for_completion corresponds to one&nbsp;x-&gt;done++ once a time
&gt;
&gt; 2005! That takes me back a bit!
&gt;
&gt; Adding the rcu email list on CC to archive this and for others to
&gt; weigh in.
&gt;
&gt; The trick is that rcu_barrier_callback() is an RCU callback that executes
&gt; on each CPU (see the on_each_cpu() in the rcu_barrier() function.)
&gt;
&gt; This RCU callback function is queued using call_rcu(), which queues
&gt; that callback at the end of the CPU's callback list. Thus, all
&gt; RCU callback functions that were already queued on that CPU will
&gt; be invoked before rcu_barrier_callback() in invoked.
&gt;
&gt; The rcu_barrier_callback() does an atomic_dec_and_test() operation on the
&gt; rcu_barrier_cpu_count global variable, and only when this variable becomes
&gt; zero does it invoke complete(). This atomic_dec_and_test() operation is
&gt; fully ordered, and thus ensures that all the earlier atomic_dec_and_test()
&gt; operations on this variable are globally seen to happen before the last
&gt; such operation (the one that decrements rcu_barrier_cpu_count to zero).
&gt;
&gt; This last rcu_barrier_callback() will then invoke complete(). This will
&gt; ensure that the wakeup in wait_for_completion() happens only after all
&gt; previously queued RCU callbacks are invoked.
&gt;
&gt; Almost, anyway.
&gt;
&gt; There are rare but real race-condition bugs in this ancient version
&gt; of rcu_barrier(). Can you tell me what they are?
&gt;
&gt; Thanx, Paul