Skip to content

Commit 2ebc45c

Browse files
Frederic Weisbeckerpaulmckrcu
authored andcommitted
rcu/nocb: Remove rcu_node structure from nocb list when de-offloaded
The nocb_gp_wait() function iterates over all CPUs in its group, including even those CPUs that have been de-offloaded. This is of course suboptimal, especially if none of the CPUs within the group are currently offloaded. This will become even more of a problem once a nocb kthread is created for all possible CPUs. Therefore use a standard double linked list to link all the offloaded rcu_data structures and safely add or delete these structure as we offload or de-offload them, respectively. Reviewed-by: Neeraj Upadhyay <quic_neeraju@quicinc.com> Signed-off-by: Frederic Weisbecker <frederic@kernel.org> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Uladzislau Rezki <urezki@gmail.com> Cc: Josh Triplett <josh@joshtriplett.org> Cc: Joel Fernandes <joel@joelfernandes.org> Tested-by: Juri Lelli <juri.lelli@redhat.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 parent 0598a4d commit 2ebc45c

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

kernel/rcu/tree.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,11 @@ struct rcu_data {
227227
struct swait_queue_head nocb_gp_wq; /* For nocb kthreads to sleep on. */
228228
bool nocb_cb_sleep; /* Is the nocb CB thread asleep? */
229229
struct task_struct *nocb_cb_kthread;
230-
struct rcu_data *nocb_next_cb_rdp;
231-
/* Next rcu_data in wakeup chain. */
230+
struct list_head nocb_head_rdp; /*
231+
* Head of rcu_data list in wakeup chain,
232+
* if rdp_gp.
233+
*/
234+
struct list_head nocb_entry_rdp; /* rcu_data node in wakeup chain. */
232235

233236
/* The following fields are used by CB kthread, hence new cacheline. */
234237
struct rcu_data *nocb_gp_rdp ____cacheline_internodealigned_in_smp;

kernel/rcu/tree_nocb.h

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,21 @@ static void nocb_gp_wait(struct rcu_data *my_rdp)
625625
* and the global grace-period kthread are awakened if needed.
626626
*/
627627
WARN_ON_ONCE(my_rdp->nocb_gp_rdp != my_rdp);
628-
for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_cb_rdp) {
628+
/*
629+
* An rcu_data structure is removed from the list after its
630+
* CPU is de-offloaded and added to the list before that CPU is
631+
* (re-)offloaded. If the following loop happens to be referencing
632+
* that rcu_data structure during the time that the corresponding
633+
* CPU is de-offloaded and then immediately re-offloaded, this
634+
* loop's rdp pointer will be carried to the end of the list by
635+
* the resulting pair of list operations. This can cause the loop
636+
* to skip over some of the rcu_data structures that were supposed
637+
* to have been scanned. Fortunately a new iteration through the
638+
* entire loop is forced after a given CPU's rcu_data structure
639+
* is added to the list, so the skipped-over rcu_data structures
640+
* won't be ignored for long.
641+
*/
642+
list_for_each_entry_rcu(rdp, &my_rdp->nocb_head_rdp, nocb_entry_rdp, 1) {
629643
bool needwake_state = false;
630644

631645
if (!nocb_gp_enabled_cb(rdp))
@@ -1003,6 +1017,8 @@ static long rcu_nocb_rdp_deoffload(void *arg)
10031017
swait_event_exclusive(rdp->nocb_state_wq,
10041018
!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB |
10051019
SEGCBLIST_KTHREAD_GP));
1020+
/* Stop nocb_gp_wait() from iterating over this structure. */
1021+
list_del_rcu(&rdp->nocb_entry_rdp);
10061022
/*
10071023
* Lock one last time to acquire latest callback updates from kthreads
10081024
* so we can later handle callbacks locally without locking.
@@ -1066,6 +1082,17 @@ static long rcu_nocb_rdp_offload(void *arg)
10661082
return -EINVAL;
10671083

10681084
pr_info("Offloading %d\n", rdp->cpu);
1085+
1086+
/*
1087+
* Cause future nocb_gp_wait() invocations to iterate over
1088+
* structure, resetting ->nocb_gp_sleep and waking up the related
1089+
* "rcuog". Since nocb_gp_wait() in turn locks ->nocb_gp_lock
1090+
* before setting ->nocb_gp_sleep again, we are guaranteed to
1091+
* iterate this newly added structure before "rcuog" goes to
1092+
* sleep again.
1093+
*/
1094+
list_add_tail_rcu(&rdp->nocb_entry_rdp, &rdp->nocb_gp_rdp->nocb_head_rdp);
1095+
10691096
/*
10701097
* Can't use rcu_nocb_lock_irqsave() before SEGCBLIST_LOCKING
10711098
* is set.
@@ -1268,7 +1295,6 @@ static void __init rcu_organize_nocb_kthreads(void)
12681295
int nl = 0; /* Next GP kthread. */
12691296
struct rcu_data *rdp;
12701297
struct rcu_data *rdp_gp = NULL; /* Suppress misguided gcc warn. */
1271-
struct rcu_data *rdp_prev = NULL;
12721298

12731299
if (!cpumask_available(rcu_nocb_mask))
12741300
return;
@@ -1288,8 +1314,8 @@ static void __init rcu_organize_nocb_kthreads(void)
12881314
/* New GP kthread, set up for CBs & next GP. */
12891315
gotnocbs = true;
12901316
nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
1291-
rdp->nocb_gp_rdp = rdp;
12921317
rdp_gp = rdp;
1318+
INIT_LIST_HEAD(&rdp->nocb_head_rdp);
12931319
if (dump_tree) {
12941320
if (!firsttime)
12951321
pr_cont("%s\n", gotnocbscbs
@@ -1302,12 +1328,11 @@ static void __init rcu_organize_nocb_kthreads(void)
13021328
} else {
13031329
/* Another CB kthread, link to previous GP kthread. */
13041330
gotnocbscbs = true;
1305-
rdp->nocb_gp_rdp = rdp_gp;
1306-
rdp_prev->nocb_next_cb_rdp = rdp;
13071331
if (dump_tree)
13081332
pr_cont(" %d", cpu);
13091333
}
1310-
rdp_prev = rdp;
1334+
rdp->nocb_gp_rdp = rdp_gp;
1335+
list_add_tail(&rdp->nocb_entry_rdp, &rdp_gp->nocb_head_rdp);
13111336
}
13121337
if (gotnocbs && dump_tree)
13131338
pr_cont("%s\n", gotnocbscbs ? "" : " (self only)");
@@ -1369,18 +1394,24 @@ static void show_rcu_nocb_state(struct rcu_data *rdp)
13691394
{
13701395
char bufw[20];
13711396
char bufr[20];
1397+
struct rcu_data *nocb_next_rdp;
13721398
struct rcu_segcblist *rsclp = &rdp->cblist;
13731399
bool waslocked;
13741400
bool wassleep;
13751401

13761402
if (rdp->nocb_gp_rdp == rdp)
13771403
show_rcu_nocb_gp_state(rdp);
13781404

1405+
nocb_next_rdp = list_next_or_null_rcu(&rdp->nocb_gp_rdp->nocb_head_rdp,
1406+
&rdp->nocb_entry_rdp,
1407+
typeof(*rdp),
1408+
nocb_entry_rdp);
1409+
13791410
sprintf(bufw, "%ld", rsclp->gp_seq[RCU_WAIT_TAIL]);
13801411
sprintf(bufr, "%ld", rsclp->gp_seq[RCU_NEXT_READY_TAIL]);
13811412
pr_info(" CB %d^%d->%d %c%c%c%c%c%c F%ld L%ld C%d %c%c%s%c%s%c%c q%ld %c CPU %d%s\n",
13821413
rdp->cpu, rdp->nocb_gp_rdp->cpu,
1383-
rdp->nocb_next_cb_rdp ? rdp->nocb_next_cb_rdp->cpu : -1,
1414+
nocb_next_rdp ? nocb_next_rdp->cpu : -1,
13841415
"kK"[!!rdp->nocb_cb_kthread],
13851416
"bB"[raw_spin_is_locked(&rdp->nocb_bypass_lock)],
13861417
"cC"[!!atomic_read(&rdp->nocb_lock_contended)],

0 commit comments

Comments
 (0)