rcu: fix race condition in synchronize_sched_expedited()
authorPaul E. McKenney <paulmck@linux.vnet.ibm.com>
Mon, 25 Oct 2010 14:39:22 +0000 (07:39 -0700)
committerPaul E. McKenney <paulmck@linux.vnet.ibm.com>
Tue, 30 Nov 2010 06:02:00 +0000 (22:02 -0800)
commitdb3a8920995484e5e9a0abaf3bad2c7311b163db
treee183de92a908466dd5568df151507b2331ff29a5
parent2d999e03b7c8305b4385dd20992e4ed3e827177b
rcu: fix race condition in synchronize_sched_expedited()

The new (early 2010) implementation of synchronize_sched_expedited() uses
try_stop_cpu() to force a context switch on every CPU.  It also permits
concurrent calls to synchronize_sched_expedited() to share a single call
to try_stop_cpu() through use of an atomically incremented
synchronize_sched_expedited_count variable.  Unfortunately, this is
subject to failure as follows:

o Task A invokes synchronize_sched_expedited(), try_stop_cpus()
succeeds, but Task A is preempted before getting to the atomic
increment of synchronize_sched_expedited_count.

o Task B also invokes synchronize_sched_expedited(), with exactly
the same outcome as Task A.

o Task C also invokes synchronize_sched_expedited(), again with
exactly the same outcome as Tasks A and B.

o Task D also invokes synchronize_sched_expedited(), but only
gets as far as acquiring the mutex within try_stop_cpus()
before being preempted, interrupted, or otherwise delayed.

o Task E also invokes synchronize_sched_expedited(), but only
gets to the snapshotting of synchronize_sched_expedited_count.

o Tasks A, B, and C all increment synchronize_sched_expedited_count.

o Task E fails to get the mutex, so checks the new value
of synchronize_sched_expedited_count.  It finds that the
value has increased, so (wrongly) assumes that its work
has been done, returning despite there having been no
expedited grace period since it began.

The solution is to have the lowest-numbered CPU atomically increment
the synchronize_sched_expedited_count variable within the
synchronize_sched_expedited_cpu_stop() function, which is under
the protection of the mutex acquired by try_stop_cpus().  However, this
also requires that piggybacking tasks wait for three rather than two
instances of try_stop_cpu(), because we cannot control the order in
which the per-CPU callback function occur.

Cc: Tejun Heo <tj@kernel.org>
Cc: Lai Jiangshan <laijs@cn.fujitsu.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
kernel/rcutree_plugin.h