Skip to content

Commit 12f54c3

Browse files
author
Paul E. McKenney
committed
rcu/nocb: Provide separate no-CBs grace-period kthreads
Currently, there is one no-CBs rcuo kthread per CPU, and these kthreads are divided into groups. The first rcuo kthread to come online in a given group is that group's leader, and the leader both waits for grace periods and invokes its CPU's callbacks. The non-leader rcuo kthreads only invoke callbacks. This works well in the real-time/embedded environments for which it was intended because such environments tend not to generate all that many callbacks. However, given huge floods of callbacks, it is possible for the leader kthread to be stuck invoking callbacks while its followers wait helplessly while their callbacks pile up. This is a good recipe for an OOM, and rcutorture's new callback-flood capability does generate such OOMs. One strategy would be to wait until such OOMs start happening in production, but similar OOMs have in fact happened starting in 2018. It would therefore be wise to take a more proactive approach. This commit therefore features per-CPU rcuo kthreads that do nothing but invoke callbacks. Instead of having one of these kthreads act as leader, each group has a separate rcog kthread that handles grace periods for its group. Because these rcuog kthreads do not invoke callbacks, callback floods on one CPU no longer block callbacks from reaching the rcuc callback-invocation kthreads on other CPUs. This change does introduce additional kthreads, however: 1. The number of additional kthreads is about the square root of the number of CPUs, so that a 4096-CPU system would have only about 64 additional kthreads. Note that recent changes decreased the number of rcuo kthreads by a factor of two (CONFIG_PREEMPT=n) or even three (CONFIG_PREEMPT=y), so this still represents a significant improvement on most systems. 2. The leading "rcuo" of the rcuog kthreads should allow existing scripting to affinity these additional kthreads as needed, the same as for the rcuop and rcuos kthreads. (There are no longer any rcuob kthreads.) 3. A state-machine approach was considered and rejected. Although this would allow the rcuo kthreads to continue their dual leader/follower roles, it complicates callback invocation and makes it more difficult to consolidate rcuo callback invocation with existing softirq callback invocation. The introduction of rcuog kthreads should thus be acceptable. Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>
1 parent 6484fe5 commit 12f54c3

2 files changed

Lines changed: 61 additions & 60 deletions

File tree

‎kernel/rcu/tree.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ struct rcu_data {
200200
atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
201201
struct rcu_head *nocb_cb_head; /* CBs ready to invoke. */
202202
struct rcu_head **nocb_cb_tail;
203-
struct swait_queue_head nocb_wq; /* For nocb kthreads to sleep on. */
204-
struct task_struct *nocb_cb_kthread;
203+
struct swait_queue_head nocb_cb_wq; /* For nocb kthreads to sleep on. */
204+
struct task_struct *nocb_gp_kthread;
205205
raw_spinlock_t nocb_lock; /* Guard following pair of fields. */
206206
int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
207207
struct timer_list nocb_timer; /* Enforce finite deferral. */
@@ -211,6 +211,8 @@ struct rcu_data {
211211
/* CBs waiting for GP. */
212212
struct rcu_head **nocb_gp_tail;
213213
bool nocb_gp_sleep; /* Is the nocb GP thread asleep? */
214+
struct swait_queue_head nocb_gp_wq; /* For nocb kthreads to sleep on. */
215+
struct task_struct *nocb_cb_kthread;
214216
struct rcu_data *nocb_next_cb_rdp;
215217
/* Next rcu_data in wakeup chain. */
216218

‎kernel/rcu/tree_plugin.h‎

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ static void __wake_nocb_leader(struct rcu_data *rdp, bool force,
15311531
struct rcu_data *rdp_leader = rdp->nocb_gp_rdp;
15321532

15331533
lockdep_assert_held(&rdp->nocb_lock);
1534-
if (!READ_ONCE(rdp_leader->nocb_cb_kthread)) {
1534+
if (!READ_ONCE(rdp_leader->nocb_gp_kthread)) {
15351535
raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
15361536
return;
15371537
}
@@ -1541,7 +1541,7 @@ static void __wake_nocb_leader(struct rcu_data *rdp, bool force,
15411541
del_timer(&rdp->nocb_timer);
15421542
raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
15431543
smp_mb(); /* ->nocb_gp_sleep before swake_up_one(). */
1544-
swake_up_one(&rdp_leader->nocb_wq);
1544+
swake_up_one(&rdp_leader->nocb_gp_wq);
15451545
} else {
15461546
raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
15471547
}
@@ -1646,7 +1646,7 @@ static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
16461646
smp_mb__after_atomic(); /* Store *old_rhpp before _wake test. */
16471647

16481648
/* If we are not being polled and there is a kthread, awaken it ... */
1649-
t = READ_ONCE(rdp->nocb_cb_kthread);
1649+
t = READ_ONCE(rdp->nocb_gp_kthread);
16501650
if (rcu_nocb_poll || !t) {
16511651
trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
16521652
TPS("WakeNotPoll"));
@@ -1786,20 +1786,18 @@ static void rcu_nocb_wait_gp(struct rcu_data *rdp)
17861786
* No-CBs GP kthreads come here to wait for additional callbacks to show up.
17871787
* This function does not return until callbacks appear.
17881788
*/
1789-
static void nocb_leader_wait(struct rcu_data *my_rdp)
1789+
static void nocb_gp_wait(struct rcu_data *my_rdp)
17901790
{
17911791
bool firsttime = true;
17921792
unsigned long flags;
17931793
bool gotcbs;
17941794
struct rcu_data *rdp;
17951795
struct rcu_head **tail;
17961796

1797-
wait_again:
1798-
17991797
/* Wait for callbacks to appear. */
18001798
if (!rcu_nocb_poll) {
18011799
trace_rcu_nocb_wake(rcu_state.name, my_rdp->cpu, TPS("Sleep"));
1802-
swait_event_interruptible_exclusive(my_rdp->nocb_wq,
1800+
swait_event_interruptible_exclusive(my_rdp->nocb_gp_wq,
18031801
!READ_ONCE(my_rdp->nocb_gp_sleep));
18041802
raw_spin_lock_irqsave(&my_rdp->nocb_lock, flags);
18051803
my_rdp->nocb_gp_sleep = true;
@@ -1838,7 +1836,7 @@ static void nocb_leader_wait(struct rcu_data *my_rdp)
18381836
trace_rcu_nocb_wake(rcu_state.name, my_rdp->cpu,
18391837
TPS("WokeEmpty"));
18401838
}
1841-
goto wait_again;
1839+
return;
18421840
}
18431841

18441842
/* Wait for one grace period. */
@@ -1862,34 +1860,47 @@ static void nocb_leader_wait(struct rcu_data *my_rdp)
18621860
rdp->nocb_cb_tail = rdp->nocb_gp_tail;
18631861
*tail = rdp->nocb_gp_head;
18641862
raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1865-
if (rdp != my_rdp && tail == &rdp->nocb_cb_head) {
1863+
if (tail == &rdp->nocb_cb_head) {
18661864
/* List was empty, so wake up the kthread. */
1867-
swake_up_one(&rdp->nocb_wq);
1865+
swake_up_one(&rdp->nocb_cb_wq);
18681866
}
18691867
}
1868+
}
18701869

1871-
/* If we (the GP kthreads) don't have CBs, go wait some more. */
1872-
if (!my_rdp->nocb_cb_head)
1873-
goto wait_again;
1870+
/*
1871+
* No-CBs grace-period-wait kthread. There is one of these per group
1872+
* of CPUs, but only once at least one CPU in that group has come online
1873+
* at least once since boot. This kthread checks for newly posted
1874+
* callbacks from any of the CPUs it is responsible for, waits for a
1875+
* grace period, then awakens all of the rcu_nocb_cb_kthread() instances
1876+
* that then have callback-invocation work to do.
1877+
*/
1878+
static int rcu_nocb_gp_kthread(void *arg)
1879+
{
1880+
struct rcu_data *rdp = arg;
1881+
1882+
for (;;)
1883+
nocb_gp_wait(rdp);
1884+
return 0;
18741885
}
18751886

18761887
/*
18771888
* No-CBs CB kthreads come here to wait for additional callbacks to show up.
1878-
* This function does not return until callbacks appear.
1889+
* This function returns true ("keep waiting") until callbacks appear and
1890+
* then false ("stop waiting") when callbacks finally do appear.
18791891
*/
1880-
static void nocb_follower_wait(struct rcu_data *rdp)
1892+
static bool nocb_follower_wait(struct rcu_data *rdp)
18811893
{
1882-
for (;;) {
1883-
trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("FollowerSleep"));
1884-
swait_event_interruptible_exclusive(rdp->nocb_wq,
1885-
READ_ONCE(rdp->nocb_cb_head));
1886-
if (smp_load_acquire(&rdp->nocb_cb_head)) {
1887-
/* ^^^ Ensure CB invocation follows _head test. */
1888-
return;
1889-
}
1890-
WARN_ON(signal_pending(current));
1891-
trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeEmpty"));
1894+
trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("FollowerSleep"));
1895+
swait_event_interruptible_exclusive(rdp->nocb_cb_wq,
1896+
READ_ONCE(rdp->nocb_cb_head));
1897+
if (smp_load_acquire(&rdp->nocb_cb_head)) { /* VVV */
1898+
/* ^^^ Ensure CB invocation follows _head test. */
1899+
return false;
18921900
}
1901+
WARN_ON(signal_pending(current));
1902+
trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeEmpty"));
1903+
return true;
18931904
}
18941905

18951906
/*
@@ -1899,7 +1910,7 @@ static void nocb_follower_wait(struct rcu_data *rdp)
18991910
* have to do quite so many wakeups (as in they only need to wake the
19001911
* no-CBs GP kthreads, not the CB kthreads).
19011912
*/
1902-
static int rcu_nocb_kthread(void *arg)
1913+
static int rcu_nocb_cb_kthread(void *arg)
19031914
{
19041915
int c, cl;
19051916
unsigned long flags;
@@ -1911,10 +1922,8 @@ static int rcu_nocb_kthread(void *arg)
19111922
/* Each pass through this loop invokes one batch of callbacks */
19121923
for (;;) {
19131924
/* Wait for callbacks. */
1914-
if (rdp->nocb_gp_rdp == rdp)
1915-
nocb_leader_wait(rdp);
1916-
else
1917-
nocb_follower_wait(rdp);
1925+
while (nocb_follower_wait(rdp))
1926+
continue;
19181927

19191928
/* Pull the ready-to-invoke callbacks onto local list. */
19201929
raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
@@ -2048,58 +2057,48 @@ void __init rcu_init_nohz(void)
20482057
static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
20492058
{
20502059
rdp->nocb_tail = &rdp->nocb_head;
2051-
init_swait_queue_head(&rdp->nocb_wq);
2060+
init_swait_queue_head(&rdp->nocb_cb_wq);
2061+
init_swait_queue_head(&rdp->nocb_gp_wq);
20522062
rdp->nocb_cb_tail = &rdp->nocb_cb_head;
20532063
raw_spin_lock_init(&rdp->nocb_lock);
20542064
timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
20552065
}
20562066

20572067
/*
20582068
* If the specified CPU is a no-CBs CPU that does not already have its
2059-
* rcuo kthread, spawn it. If the CPUs are brought online out of order,
2060-
* this can require re-organizing the GP-CB relationships.
2069+
* rcuo CB kthread, spawn it. Additionally, if the rcuo GP kthread
2070+
* for this CPU's group has not yet been created, spawn it as well.
20612071
*/
20622072
static void rcu_spawn_one_nocb_kthread(int cpu)
20632073
{
2064-
struct rcu_data *rdp;
2065-
struct rcu_data *rdp_last;
2066-
struct rcu_data *rdp_old_leader;
2067-
struct rcu_data *rdp_spawn = per_cpu_ptr(&rcu_data, cpu);
2074+
struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
2075+
struct rcu_data *rdp_gp;
20682076
struct task_struct *t;
20692077

20702078
/*
20712079
* If this isn't a no-CBs CPU or if it already has an rcuo kthread,
20722080
* then nothing to do.
20732081
*/
2074-
if (!rcu_is_nocb_cpu(cpu) || rdp_spawn->nocb_cb_kthread)
2082+
if (!rcu_is_nocb_cpu(cpu) || rdp->nocb_cb_kthread)
20752083
return;
20762084

20772085
/* If we didn't spawn the GP kthread first, reorganize! */
2078-
rdp_old_leader = rdp_spawn->nocb_gp_rdp;
2079-
if (rdp_old_leader != rdp_spawn && !rdp_old_leader->nocb_cb_kthread) {
2080-
rdp_last = NULL;
2081-
rdp = rdp_old_leader;
2082-
do {
2083-
rdp->nocb_gp_rdp = rdp_spawn;
2084-
if (rdp_last && rdp != rdp_spawn)
2085-
rdp_last->nocb_next_cb_rdp = rdp;
2086-
if (rdp == rdp_spawn) {
2087-
rdp = rdp->nocb_next_cb_rdp;
2088-
} else {
2089-
rdp_last = rdp;
2090-
rdp = rdp->nocb_next_cb_rdp;
2091-
rdp_last->nocb_next_cb_rdp = NULL;
2092-
}
2093-
} while (rdp);
2094-
rdp_spawn->nocb_next_cb_rdp = rdp_old_leader;
2086+
rdp_gp = rdp->nocb_gp_rdp;
2087+
if (!rdp_gp->nocb_gp_kthread) {
2088+
t = kthread_run(rcu_nocb_gp_kthread, rdp_gp,
2089+
"rcuog/%d", rdp_gp->cpu);
2090+
if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo GP kthread, OOM is now expected behavior\n", __func__))
2091+
return;
2092+
WRITE_ONCE(rdp_gp->nocb_gp_kthread, t);
20952093
}
20962094

20972095
/* Spawn the kthread for this CPU. */
2098-
t = kthread_run(rcu_nocb_kthread, rdp_spawn,
2096+
t = kthread_run(rcu_nocb_cb_kthread, rdp,
20992097
"rcuo%c/%d", rcu_state.abbr, cpu);
2100-
if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo kthread, OOM is now expected behavior\n", __func__))
2098+
if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo CB kthread, OOM is now expected behavior\n", __func__))
21012099
return;
2102-
WRITE_ONCE(rdp_spawn->nocb_cb_kthread, t);
2100+
WRITE_ONCE(rdp->nocb_cb_kthread, t);
2101+
WRITE_ONCE(rdp->nocb_gp_kthread, rdp_gp->nocb_gp_kthread);
21032102
}
21042103

21052104
/*

0 commit comments

Comments
 (0)