Skip to contentSkip to conversation

Reduce synchronize_rcu() latency(v5)

PATCH Superseded Uladzislau Rezki (Sony) 5 participants 27 messages Feb 20, 2024 – Mar 5, 2024 1 message not in the archive

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Uladzislau Rezki (Sony)

author maintainer
#0 lore raw

v5 cover Reduce synchronize_rcu() latency(v5)

This is a v5 that tends to improve synchronize_rcu() call in terms of latency reduction. This has been developed together with Neeraj Upadhyay. The delta between previous v4 and v5 is rather small. Main difference are cosmetic changes related to patch squashing and data structures splitting.

It is based on Paul's dev branch.

v4 -> v5:
- furthers squashing to reduce number of patches;
- remove the CONFIG_RCU_SR_NORMAL_DEBUG_GP Kconfig option and reuse already existing debug option which is CONFIG_PROVE_RCU;
- add data structures in a separate patch.

v4: https://lore.kernel.org/lkml/[email protected]/T/
v3: https://lore.kernel.org/lkml/cd45b0b5-f86b-43fb-a5f3-47d340cd4f9f@paulmck-laptop/T/
v2: https://lore.kernel.org/all/[email protected]/T/
v1: https://lore.kernel.org/lkml/[email protected]/T/

Uladzislau Rezki (Sony) (4):
rcu: Add data structures for synchronize_rcu()
rcu: Reduce synchronize_rcu() latency
rcu: Add a trace event for synchronize_rcu_normal()
rcu: Support direct wake-up of synchronize_rcu() users

 .../admin-guide/kernel-parameters.txt         |  14 +
 include/trace/events/rcu.h                    |  27 ++
 kernel/rcu/tree.c                             | 363 +++++++++++++++++-
 kernel/rcu/tree.h                             |  20 +
 kernel/rcu/tree_exp.h                         |   2 +-
 5 files changed, 424 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH v5 1/4 rcu: Add data structures for synchronize_rcu()

The synchronize_rcu() call is going to be reworked, thus this patch adds dedicated fields into the rcu_state structure.

---
 kernel/rcu/tree.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
kernel/rcu/tree.h +14 -0
index df48160b3136..b942b9437438 100644
@@ -315,6 +315,13 @@ do {									\
 	__set_current_state(TASK_RUNNING);				\
 } while (0)
 
+#define SR_NORMAL_GP_WAIT_HEAD_MAX 5
+
+struct sr_wait_node {
+	atomic_t inuse;
+	struct llist_node node;
+};
+
 /*
  * RCU global state, including node hierarchy.  This hierarchy is
  * represented in "heap" form in a dense array.  The root (first level)
@@ -400,6 +407,13 @@ struct rcu_state {
 						/* Synchronize offline with */
 						/*  GP pre-initialization. */
 	int nocb_is_setup;			/* nocb is setup from boot */
+
+	/* synchronize_rcu() part. */
+	struct llist_head srs_next;	/* request a GP users. */
+	struct llist_node *srs_wait_tail; /* wait for GP users. */
+	struct llist_node *srs_done_tail; /* ready for GP users. */
+	struct sr_wait_node srs_wait_nodes[SR_NORMAL_GP_WAIT_HEAD_MAX];
+	struct work_struct srs_cleanup_work;
 };
 
 /* Values for rcu_state structure's gp_flags field. */
Signature · 1 line
2.39.2
author maintainer
#2 lore raw

PATCH v5 2/4 rcu: Reduce synchronize_rcu() latency

In reply to Uladzislau Rezki (Sony)

A call to a synchronize_rcu() can be optimized from a latency point of view. Workloads which depend on this can benefit of it.

The delay of wakeme_after_rcu() callback, which unblocks a waiter, depends on several factors:

- how fast a process of offloading is started. Combination of:
- !CONFIG_RCU_NOCB_CPU/CONFIG_RCU_NOCB_CPU;
- !CONFIG_RCU_LAZY/CONFIG_RCU_LAZY;
- other.
- when started, invoking path is interrupted due to:
- time limit;
- need_resched();
- if limit is reached.
- where in a nocb list it is located;
- how fast previous callbacks completed;

Example:

1. On our embedded devices i can easily trigger the scenario when it is a last in the list out of ~3600 callbacks:

<snip>
<...>-29 [001] d..1. 21950.145313: rcu_batch_start: rcu_preempt CBs=3613 bl=28
...

  <...>-29      [001] ..... 21950.152578: rcu_invoke_callback: rcu_preempt rhp=00000000b2d6dee8 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152579: rcu_invoke_callback: rcu_preempt rhp=00000000a446f607 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152580: rcu_invoke_callback: rcu_preempt rhp=00000000a5cab03b func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152581: rcu_invoke_callback: rcu_preempt rhp=0000000013b7e5ee func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152582: rcu_invoke_callback: rcu_preempt rhp=000000000a8ca6f9 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152583: rcu_invoke_callback: rcu_preempt rhp=000000008f162ca8 func=wakeme_after_rcu.cfi_jt
  <...>-29      [001] d..1. 21950.152625: rcu_batch_end: rcu_preempt CBs-invoked=3612 idle=....

<snip>

2. We use cpuset/cgroup to classify tasks and assign them into different cgroups. For example "backgrond" group which binds tasks only to little CPUs or "foreground" which makes use of all CPUs. Tasks can be migrated between groups by a request if an acceleration is needed.

See below an example how "surfaceflinger" task gets migrated. Initially it is located in the "system-background" cgroup which allows to run only on little cores. In order to speed it up it can be temporary moved into "foreground" cgroup which allows to use big/all CPUs:

cgroup_attach_task():
-> cgroup_migrate_execute()
-> cpuset_can_attach()

     -> percpu_down_write()
       -> rcu_sync_enter()
         -> synchronize_rcu()

-> now move tasks to the new cgroup.
-> cgroup_migrate_finish()

<snip>

         rcuop/1-29      [000] .....  7030.528570: rcu_invoke_callback: rcu_preempt rhp=00000000461605e0 func=wakeme_after_rcu.cfi_jt
    PERFD-SERVER-1855    [000] d..1.  7030.530293: cgroup_attach_task: dst_root=3 dst_id=22 dst_level=1 dst_path=/foreground pid=1900 comm=surfaceflinger
   TimerDispatch-2768    [002] d..5.  7030.537542: sched_migrate_task: comm=surfaceflinger pid=1900 prio=98 orig_cpu=0 dest_cpu=4

<snip>

"Boosting a task" depends on synchronize_rcu() latency:

- first trace shows a completion of synchronize_rcu();
- second shows attaching a task to a new group;
- last shows a final step when migration occurs.

3. To address this drawback, maintain a separate track that consists of synchronize_rcu() callers only. After completion of a grace period users are deferred to a dedicated worker to process requests.

4. This patch reduces the latency of synchronize_rcu() approximately by ~30-40% on synthetic tests. The real test case, camera launch time, shows(time is in milliseconds):

1-run 542 vs 489 improvement 9%
2-run 540 vs 466 improvement 13%
3-run 518 vs 468 improvement 9%
4-run 531 vs 457 improvement 13%
5-run 548 vs 475 improvement 13%
6-run 509 vs 484 improvement 4%

Synthetic test(no "noise" from other callbacks):
Hardware: x86_64 64 CPUs, 64GB of memory
Linux-6.6

- 10K tasks(simultaneous);
- each task does(1000 loops)

     synchronize_rcu();
     kfree(p);

default: CONFIG_RCU_NOCB_CPU: takes 54 seconds to complete all users;
patch: CONFIG_RCU_NOCB_CPU: takes 35 seconds to complete all users.

Running 60K gives approximately same results on my setup. Please note it is without any interaction with another type of callbacks, otherwise it will impact a lot a default case.

5. By default it is disabled. To enable this perform one of the below sequence:

echo 1 > /sys/module/rcutree/parameters/rcu_normal_wake_from_gp or pass a boot parameter "rcutree.rcu_normal_wake_from_gp=1"

---
 .../admin-guide/kernel-parameters.txt         |  14 +
 kernel/rcu/tree.c                             | 331 +++++++++++++++++-
 kernel/rcu/tree_exp.h                         |   2 +-
 3 files changed, 345 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +14 -0
index 2244aa0a013b..7ca84cf7b4f4 100644
@@ -5059,6 +5059,20 @@
 			delay, memory pressure or callback list growing too
 			big.
 
+	rcutree.rcu_normal_wake_from_gp= [KNL]
+			Reduces a latency of synchronize_rcu() call. This approach
+			maintains its own track of synchronize_rcu() callers, so it
+			does not interact with regular callbacks because it does not
+			use a call_rcu[_hurry]() path. Please note, this is for a
+			normal grace period.
+
+			How to enable it:
+
+			echo 1 > /sys/module/rcutree/parameters/rcu_normal_wake_from_gp
+			or pass a boot parameter "rcutree.rcu_normal_wake_from_gp=1"
+
+			Default is 0.
+
 	rcuscale.gp_async= [KNL]
 			Measure performance of asynchronous
 			grace-period primitives such as call_rcu().
kernel/rcu/tree.c +330 -1

383 lines not on this page · raw message

kernel/rcu/tree_exp.h +1 -1
index 6b83537480b1..8a1d9c8bd9f7 100644
@@ -930,7 +930,7 @@ void synchronize_rcu_expedited(void)
 
 	/* If expedited grace periods are prohibited, fall back to normal. */
 	if (rcu_gp_is_normal()) {
-		wait_rcu_gp(call_rcu_hurry);
+		synchronize_rcu_normal();
 		return;
 	}
 
Signature · 1 line
2.39.2
#3 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 12 lines · Uladzislau Rezki (Sony)
kernel/rcu/tree.c +12 -0
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
+{
+	struct llist_node *done, *rcu, *next, *head;
+
+	/*
+	 * This work execution can potentially execute
+	 * while a new done tail is being updated by
+	 * grace period kthread in rcu_sr_normal_gp_cleanup().
+	 * So, read and updates of done tail need to
+	 * follow acq-rel semantics.
+	 *
+	 * Given that wq semantics guarantees that a single work

kernel/rcu/tree.c static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) · line 1621

Quoted 10 lines
kernel/rcu/tree.c +10 -0
+	 * cannot execute concurrently by multiple kworkers,
+	 * the done tail list manipulations are protected here.
+	 */
+	done = smp_load_acquire(&rcu_state.srs_done_tail);
+	if (!done)
+		return;
+
+	WARN_ON_ONCE(!rcu_sr_is_wait_head(done));
+	head = done->next;
+	done->next = NULL;

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)
    ...

// A few more GPs later

rcu_sr_normal_gp_init() {
     HEAD2 = rcu_sr_get_wait_head();
     llist_add(HEAD2, &rcu_state.srs_next);
                                                          // head == rcu_state.srs_next
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // EXECUTE CALLBACKS TOO EARLY!!!
                                                          }
                                                      }
#4 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 70 lines · Uladzislau Rezki (Sony)
Quoted 12 lines
kernel/rcu/tree.c +12 -0
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
+{
+     struct llist_node *done, *rcu, *next, *head;
+
+     /*
+      * This work execution can potentially execute
+      * while a new done tail is being updated by
+      * grace period kthread in rcu_sr_normal_gp_cleanup().
+      * So, read and updates of done tail need to
+      * follow acq-rel semantics.
+      *
+      * Given that wq semantics guarantees that a single work

kernel/rcu/tree.c static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) · line 1621

Quoted 10 lines
kernel/rcu/tree.c +10 -0
+      * cannot execute concurrently by multiple kworkers,
+      * the done tail list manipulations are protected here.
+      */
+     done = smp_load_acquire(&rcu_state.srs_done_tail);
+     if (!done)
+             return;
+
+     WARN_ON_ONCE(!rcu_sr_is_wait_head(done));
+     head = done->next;
+     done->next = NULL;

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
Quoted 4 lines

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)

It seems that we should move rcu_sr_put_wait_head() from rcu_sr_normal_gp_cleanup() to rcu_sr_normal_gp_cleanup_work(), if find wait_head->next == NULL, invoke rcu_sr_put_wait_head() to release wait_head.

Thanks
Zqiang

Quoted 14 lines
    ...

// A few more GPs later

rcu_sr_normal_gp_init() {
     HEAD2 = rcu_sr_get_wait_head();
     llist_add(HEAD2, &rcu_state.srs_next);
                                                          // head == rcu_state.srs_next
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // EXECUTE CALLBACKS TOO EARLY!!!
                                                          }
                                                      }
#5 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 50 lines · Z qiang
Quoted 45 lines

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
Quoted 4 lines

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)
Quoted 4 lines

It seems that we should move rcu_sr_put_wait_head() from rcu_sr_normal_gp_cleanup() to rcu_sr_normal_gp_cleanup_work(), if find wait_head->next == NULL, invoke rcu_sr_put_wait_head() to release wait_head.

Well, rcu_sr_normal_gp_cleanup_work() already put all the wait heads that are _after_ srs_done_tail. But it can't put the srs_done_tail itself without introducing even worse races...

#6 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 51 lines · Frederic Weisbecker
Quoted 50 lines · Z qiang
Quoted 49 lines

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)
Quoted 9 lines
Quoted 4 lines

It seems that we should move rcu_sr_put_wait_head() from rcu_sr_normal_gp_cleanup() to rcu_sr_normal_gp_cleanup_work(), if find wait_head->next == NULL, invoke rcu_sr_put_wait_head() to release wait_head.

Well, rcu_sr_normal_gp_cleanup_work() already put all the wait heads that are _after_ srs_done_tail. But it can't put the srs_done_tail itself without introducing even worse races...

(I forgot to mention this race actually concerns the last patch (4/4))

author maintainer
#7 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

In reply to Frederic Weisbecker
Quoted 77 lines · Uladzislau Rezki (Sony)
Quoted 12 lines
kernel/rcu/tree.c +12 -0
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
+{
+	struct llist_node *done, *rcu, *next, *head;
+
+	/*
+	 * This work execution can potentially execute
+	 * while a new done tail is being updated by
+	 * grace period kthread in rcu_sr_normal_gp_cleanup().
+	 * So, read and updates of done tail need to
+	 * follow acq-rel semantics.
+	 *
+	 * Given that wq semantics guarantees that a single work

kernel/rcu/tree.c static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) · line 1621

Quoted 10 lines
kernel/rcu/tree.c +10 -0
+	 * cannot execute concurrently by multiple kworkers,
+	 * the done tail list manipulations are protected here.
+	 */
+	done = smp_load_acquire(&rcu_state.srs_done_tail);
+	if (!done)
+		return;
+
+	WARN_ON_ONCE(!rcu_sr_is_wait_head(done));
+	head = done->next;
+	done->next = NULL;

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)
    ...

// A few more GPs later

Quoted 10 lines
rcu_sr_normal_gp_init() {
     HEAD2 = rcu_sr_get_wait_head();
     llist_add(HEAD2, &rcu_state.srs_next);
                                                          // head == rcu_state.srs_next
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // EXECUTE CALLBACKS TOO EARLY!!!
                                                          }
                                                      }

Let me check it tomorrow!

--
Uladzislau Rezki

author maintainer
#8 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

In reply to Frederic Weisbecker
Quoted 76 lines · Frederic Weisbecker
Quoted 12 lines · Uladzislau Rezki (Sony)
kernel/rcu/tree.c +12 -0
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
+{
+	struct llist_node *done, *rcu, *next, *head;
+
+	/*
+	 * This work execution can potentially execute
+	 * while a new done tail is being updated by
+	 * grace period kthread in rcu_sr_normal_gp_cleanup().
+	 * So, read and updates of done tail need to
+	 * follow acq-rel semantics.
+	 *
+	 * Given that wq semantics guarantees that a single work

kernel/rcu/tree.c static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) · line 1621

Quoted 10 lines
kernel/rcu/tree.c +10 -0
+	 * cannot execute concurrently by multiple kworkers,
+	 * the done tail list manipulations are protected here.
+	 */
+	done = smp_load_acquire(&rcu_state.srs_done_tail);
+	if (!done)
+		return;
+
+	WARN_ON_ONCE(!rcu_sr_is_wait_head(done));
+	head = done->next;
+	done->next = NULL;

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)
    ...

// A few more GPs later

Quoted 10 lines
rcu_sr_normal_gp_init() {
     HEAD2 = rcu_sr_get_wait_head();
     llist_add(HEAD2, &rcu_state.srs_next);
                                                          // head == rcu_state.srs_next
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // EXECUTE CALLBACKS TOO EARLY!!!
                                                          }
                                                      }

Looks like that. To address this, we should not release the head in the GP kthread.

Thanks!

--
Uladzislau Rezki

#9 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 87 lines · Uladzislau Rezki
Quoted 76 lines · Frederic Weisbecker
Quoted 12 lines · Uladzislau Rezki (Sony)
kernel/rcu/tree.c +12 -0
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
+{
+	struct llist_node *done, *rcu, *next, *head;
+
+	/*
+	 * This work execution can potentially execute
+	 * while a new done tail is being updated by
+	 * grace period kthread in rcu_sr_normal_gp_cleanup().
+	 * So, read and updates of done tail need to
+	 * follow acq-rel semantics.
+	 *
+	 * Given that wq semantics guarantees that a single work

kernel/rcu/tree.c static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) · line 1621

Quoted 10 lines
kernel/rcu/tree.c +10 -0
+	 * cannot execute concurrently by multiple kworkers,
+	 * the done tail list manipulations are protected here.
+	 */
+	done = smp_load_acquire(&rcu_state.srs_done_tail);
+	if (!done)
+		return;
+
+	WARN_ON_ONCE(!rcu_sr_is_wait_head(done));
+	head = done->next;
+	done->next = NULL;

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)
    ...

// A few more GPs later

Quoted 10 lines
rcu_sr_normal_gp_init() {
     HEAD2 = rcu_sr_get_wait_head();
     llist_add(HEAD2, &rcu_state.srs_next);
                                                          // head == rcu_state.srs_next
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // EXECUTE CALLBACKS TOO EARLY!!!
                                                          }
                                                      }
Quoted 2 lines

Looks like that. To address this, we should not release the head in the GP

Quoted 1 line

kthread.

But then you have to unconditionally schedule the work, right? Otherwise the HEADs are not released. And that means dropping this patch (right now I don't have a better idea).

author maintainer
#10 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 88 lines · Frederic Weisbecker
Quoted 87 lines · Uladzislau Rezki
Quoted 86 lines · Frederic Weisbecker
Quoted 12 lines · Uladzislau Rezki (Sony)
(quoted diff) +12 -0
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
+{
+	struct llist_node *done, *rcu, *next, *head;
+
+	/*
+	 * This work execution can potentially execute
+	 * while a new done tail is being updated by
+	 * grace period kthread in rcu_sr_normal_gp_cleanup().
+	 * So, read and updates of done tail need to
+	 * follow acq-rel semantics.
+	 *
+	 * Given that wq semantics guarantees that a single work

Review

Quoted 10 lines
(quoted diff) +10 -0
+	 * cannot execute concurrently by multiple kworkers,
+	 * the done tail list manipulations are protected here.
+	 */
+	done = smp_load_acquire(&rcu_state.srs_done_tail);
+	if (!done)
+		return;
+
+	WARN_ON_ONCE(!rcu_sr_is_wait_head(done));
+	head = done->next;
+	done->next = NULL;

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)
    ...

// A few more GPs later

rcu_sr_normal_gp_init() {
     HEAD2 = rcu_sr_get_wait_head();
     llist_add(HEAD2, &rcu_state.srs_next);
                                                          // head == rcu_state.srs_next
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // EXECUTE CALLBACKS TOO EARLY!!!
                                                          }
                                                      }
Quoted 7 lines
Quoted 2 lines

Looks like that. To address this, we should not release the head in the GP

Quoted 1 line

kthread.

But then you have to unconditionally schedule the work, right? Otherwise the HEADs are not released. And that means dropping this patch (right now I don't have a better idea).

The easiest way is to drop the patch. To address it we can go with:

<snip>

kernel/rcu/tree.c +1 -9
index 31f3a61f9c38..9aa2cd46583e 100644
@@ -1661,16 +1661,8 @@ static void rcu_sr_normal_gp_cleanup(void)
 	 * wait-head is released if last. The worker is not kicked.
 	 */
 	llist_for_each_safe(rcu, next, wait_tail->next) {
-		if (rcu_sr_is_wait_head(rcu)) {
-			if (!rcu->next) {
-				rcu_sr_put_wait_head(rcu);
-				wait_tail->next = NULL;
-			} else {
-				wait_tail->next = rcu;
-			}
-
+		if (rcu_sr_is_wait_head(rcu))
 			break;
-		}
 
 		rcu_sr_normal_complete(rcu);
 		// It can be last, update a next on this step.

<snip>

i.e. the process of users from GP is still there. The work is triggered to perform a final complete(if there are users) + releasing wait-heads so we do not race anymore.

I am OK with both cases. Dropping the patch will make it more simple for sure.

--
Uladzislau Rezki

maintainer
#11 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 123 lines · Uladzislau Rezki
Quoted 88 lines · Frederic Weisbecker
Quoted 87 lines · Uladzislau Rezki
Quoted 86 lines · Frederic Weisbecker
Quoted 12 lines · Uladzislau Rezki (Sony)
(quoted diff) +12 -0
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
+{
+	struct llist_node *done, *rcu, *next, *head;
+
+	/*
+	 * This work execution can potentially execute
+	 * while a new done tail is being updated by
+	 * grace period kthread in rcu_sr_normal_gp_cleanup().
+	 * So, read and updates of done tail need to
+	 * follow acq-rel semantics.
+	 *
+	 * Given that wq semantics guarantees that a single work

Review

Quoted 10 lines
(quoted diff) +10 -0
+	 * cannot execute concurrently by multiple kworkers,
+	 * the done tail list manipulations are protected here.
+	 */
+	done = smp_load_acquire(&rcu_state.srs_done_tail);
+	if (!done)
+		return;
+
+	WARN_ON_ONCE(!rcu_sr_is_wait_head(done));
+	head = done->next;
+	done->next = NULL;

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)
    ...

// A few more GPs later

rcu_sr_normal_gp_init() {
     HEAD2 = rcu_sr_get_wait_head();
     llist_add(HEAD2, &rcu_state.srs_next);
                                                          // head == rcu_state.srs_next
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // EXECUTE CALLBACKS TOO EARLY!!!
                                                          }
                                                      }
Quoted 7 lines
Quoted 2 lines

Looks like that. To address this, we should not release the head in the GP

Quoted 1 line

kthread.

But then you have to unconditionally schedule the work, right? Otherwise the HEADs are not released. And that means dropping this patch (right now I don't have a better idea).

The easiest way is to drop the patch. To address it we can go with:

<snip>

kernel/rcu/tree.c +1 -9
index 31f3a61f9c38..9aa2cd46583e 100644
@@ -1661,16 +1661,8 @@ static void rcu_sr_normal_gp_cleanup(void)
 	 * wait-head is released if last. The worker is not kicked.
 	 */
 	llist_for_each_safe(rcu, next, wait_tail->next) {
-		if (rcu_sr_is_wait_head(rcu)) {
-			if (!rcu->next) {
-				rcu_sr_put_wait_head(rcu);
-				wait_tail->next = NULL;
-			} else {
-				wait_tail->next = rcu;
-			}
-
+		if (rcu_sr_is_wait_head(rcu))
 			break;
-		}
 
 		rcu_sr_normal_complete(rcu);
 		// It can be last, update a next on this step.
<snip>
Quoted 6 lines

i.e. the process of users from GP is still there. The work is triggered to perform a final complete(if there are users) + releasing wait-heads so we do not race anymore.

I am OK with both cases. Dropping the patch will make it more simple for sure.

Please feel free to repost a fixed-up patch series. I can easily replace the commits currently in -rcu with new ones. Just let me know.

Thanx, Paul

author maintainer
#12 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 128 lines · Paul E. McKenney
Quoted 127 lines · Uladzislau Rezki
Quoted 88 lines · Frederic Weisbecker
Quoted 87 lines · Uladzislau Rezki
Quoted 86 lines · Frederic Weisbecker
Quoted 12 lines · Uladzislau Rezki (Sony)
(quoted diff) +12 -0
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
+{
+	struct llist_node *done, *rcu, *next, *head;
+
+	/*
+	 * This work execution can potentially execute
+	 * while a new done tail is being updated by
+	 * grace period kthread in rcu_sr_normal_gp_cleanup().
+	 * So, read and updates of done tail need to
+	 * follow acq-rel semantics.
+	 *
+	 * Given that wq semantics guarantees that a single work

Review

Quoted 10 lines
(quoted diff) +10 -0
+	 * cannot execute concurrently by multiple kworkers,
+	 * the done tail list manipulations are protected here.
+	 */
+	done = smp_load_acquire(&rcu_state.srs_done_tail);
+	if (!done)
+		return;
+
+	WARN_ON_ONCE(!rcu_sr_is_wait_head(done));
+	head = done->next;
+	done->next = NULL;

Can the following race happen?

CPU 0 CPU 1
----- -----

// wait_tail == HEAD1

rcu_sr_normal_gp_cleanup() {
    // has passed SR_MAX_USERS_WAKE_FROM_GP
    wait_tail->next = next;
    // done_tail = HEAD1
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
// new GP, wait_tail == HEAD2
rcu_sr_normal_gp_cleanup() {
    // executes all completion, but stop at HEAD1
    wait_tail->next = HEAD1;
    // done_tail = HEAD2
    smp_store_release(&rcu_state.srs_done_tail, wait_tail);
    queue_work() {
        test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)
        __queue_work()
    }
}
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);
                                                          // head = HEAD1
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // completes all callbacks, release HEAD1
                                                          }
                                                      }
                                                      // Process second queue
                                                      set_work_pool_and_clear_pending()
                                                      rcu_sr_normal_gp_cleanup_work() {
                                                          // done = HEAD2
                                                          done = smp_load_acquire(&rcu_state.srs_done_tail);

// new GP, wait_tail == HEAD3

rcu_sr_normal_gp_cleanup() {
    // Finds HEAD2 with ->next == NULL at the end
    rcu_sr_put_wait_head(HEAD2)
    ...

// A few more GPs later

rcu_sr_normal_gp_init() {
     HEAD2 = rcu_sr_get_wait_head();
     llist_add(HEAD2, &rcu_state.srs_next);
                                                          // head == rcu_state.srs_next
                                                          head = done->next;
                                                          done->next = NULL;
                                                          llist_for_each_safe() {
                                                              // EXECUTE CALLBACKS TOO EARLY!!!
                                                          }
                                                      }
Quoted 7 lines
Quoted 2 lines

Looks like that. To address this, we should not release the head in the GP

Quoted 1 line

kthread.

But then you have to unconditionally schedule the work, right? Otherwise the HEADs are not released. And that means dropping this patch (right now I don't have a better idea).

The easiest way is to drop the patch. To address it we can go with:

<snip>

kernel/rcu/tree.c +1 -9
index 31f3a61f9c38..9aa2cd46583e 100644
@@ -1661,16 +1661,8 @@ static void rcu_sr_normal_gp_cleanup(void)
 	 * wait-head is released if last. The worker is not kicked.
 	 */
 	llist_for_each_safe(rcu, next, wait_tail->next) {
-		if (rcu_sr_is_wait_head(rcu)) {
-			if (!rcu->next) {
-				rcu_sr_put_wait_head(rcu);
-				wait_tail->next = NULL;
-			} else {
-				wait_tail->next = rcu;
-			}
-
+		if (rcu_sr_is_wait_head(rcu))
 			break;
-		}
 
 		rcu_sr_normal_complete(rcu);
 		// It can be last, update a next on this step.
<snip>

i.e. the process of users from GP is still there. The work is triggered to perform a final complete(if there are users) + releasing wait-heads so we do not race anymore.

Quoted 6 lines
Quoted 2 lines

I am OK with both cases. Dropping the patch will make it more simple for sure.

Please feel free to repost a fixed-up patch series. I can easily replace the commits currently in -rcu with new ones. Just let me know.

I will submit a fix patch for the race also i will submit a patch related to switching to our own wq. that will have WQ_MEM_RECLAIM flag.

--
Uladzislau Rezki

#13 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

In reply to Uladzislau Rezki
Quoted 28 lines · Uladzislau Rezki

On Mon, Mar 04, 2024 at 12:55:47PM +0100, Frederic Weisbecker wrote:
The easiest way is to drop the patch. To address it we can go with:

<snip>

kernel/rcu/tree.c +1 -9
index 31f3a61f9c38..9aa2cd46583e 100644
@@ -1661,16 +1661,8 @@ static void rcu_sr_normal_gp_cleanup(void)
 	 * wait-head is released if last. The worker is not kicked.
 	 */
 	llist_for_each_safe(rcu, next, wait_tail->next) {
-		if (rcu_sr_is_wait_head(rcu)) {
-			if (!rcu->next) {
-				rcu_sr_put_wait_head(rcu);
-				wait_tail->next = NULL;
-			} else {
-				wait_tail->next = rcu;
-			}
-
+		if (rcu_sr_is_wait_head(rcu))
 			break;
-		}
 
 		rcu_sr_normal_complete(rcu);
 		// It can be last, update a next on this step.
<snip>
Quoted 3 lines

i.e. the process of users from GP is still there. The work is triggered to perform a final complete(if there are users) + releasing wait-heads so we do not race anymore.

It's worth mentioning that this doesn't avoid scheduling the workqueue. Except perhaps for the very first time rcu_sr_normal_gp_cleanup() is called, the workqueue will always have to be scheduled at least in order to release the wait_tail of the previous rcu_sr_normal_gp_cleanup() call.

But indeed you keep the optimization that performs the completions themselves synchronously from the GP kthread if there aren't too many of them (which probably is the case most of the time).

Quoted 2 lines

I am OK with both cases. Dropping the patch will make it more simple for sure.

I am ok with both cases as well :-)

You choose. But note that the time spent doing the completions from the GP kthread may come at the expense of delaying the start of the next grace period, on which further synchronous RCU calls may in turn depend on...

Thanks.

Quoted 5 lines

--
Uladzislau Rezki

author maintainer
#14 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 29 lines · Frederic Weisbecker
Quoted 28 lines · Uladzislau Rezki

On Mon, Mar 04, 2024 at 12:55:47PM +0100, Frederic Weisbecker wrote:
The easiest way is to drop the patch. To address it we can go with:

<snip>

kernel/rcu/tree.c +1 -9
index 31f3a61f9c38..9aa2cd46583e 100644
@@ -1661,16 +1661,8 @@ static void rcu_sr_normal_gp_cleanup(void)
 	 * wait-head is released if last. The worker is not kicked.
 	 */
 	llist_for_each_safe(rcu, next, wait_tail->next) {
-		if (rcu_sr_is_wait_head(rcu)) {
-			if (!rcu->next) {
-				rcu_sr_put_wait_head(rcu);
-				wait_tail->next = NULL;
-			} else {
-				wait_tail->next = rcu;
-			}
-
+		if (rcu_sr_is_wait_head(rcu))
 			break;
-		}
 
 		rcu_sr_normal_complete(rcu);
 		// It can be last, update a next on this step.
<snip>
Quoted 9 lines
Quoted 3 lines

i.e. the process of users from GP is still there. The work is triggered to perform a final complete(if there are users) + releasing wait-heads so we do not race anymore.

It's worth mentioning that this doesn't avoid scheduling the workqueue. Except perhaps for the very first time rcu_sr_normal_gp_cleanup() is called, the workqueue will always have to be scheduled at least in order to release the wait_tail of the previous rcu_sr_normal_gp_cleanup() call.

No, it does not avoid for sure :) I will add more explanation.

Quoted 4 lines

But indeed you keep the optimization that performs the completions themselves synchronously from the GP kthread if there aren't too many of them (which probably is the case most of the time).

Quoted 9 lines
Quoted 2 lines

I am OK with both cases. Dropping the patch will make it more simple for sure.

I am ok with both cases as well :-)

You choose. But note that the time spent doing the completions from the GP kthread may come at the expense of delaying the start of the next grace period, on which further synchronous RCU calls may in turn depend on...

That is a true point. Therefore we do it with a fixed number which should not influence on a GP.

--
Uladzislau Rezki

#15 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 48 lines · Uladzislau Rezki
Quoted 29 lines · Frederic Weisbecker
Quoted 28 lines · Uladzislau Rezki

On Mon, Mar 04, 2024 at 12:55:47PM +0100, Frederic Weisbecker wrote:
The easiest way is to drop the patch. To address it we can go with:

<snip>

kernel/rcu/tree.c +1 -9
index 31f3a61f9c38..9aa2cd46583e 100644
@@ -1661,16 +1661,8 @@ static void rcu_sr_normal_gp_cleanup(void)
 	 * wait-head is released if last. The worker is not kicked.
 	 */
 	llist_for_each_safe(rcu, next, wait_tail->next) {
-		if (rcu_sr_is_wait_head(rcu)) {
-			if (!rcu->next) {
-				rcu_sr_put_wait_head(rcu);
-				wait_tail->next = NULL;
-			} else {
-				wait_tail->next = rcu;
-			}
-
+		if (rcu_sr_is_wait_head(rcu))
 			break;
-		}
 
 		rcu_sr_normal_complete(rcu);
 		// It can be last, update a next on this step.
<snip>
Quoted 9 lines
Quoted 3 lines

i.e. the process of users from GP is still there. The work is triggered to perform a final complete(if there are users) + releasing wait-heads so we do not race anymore.

It's worth mentioning that this doesn't avoid scheduling the workqueue. Except perhaps for the very first time rcu_sr_normal_gp_cleanup() is called, the workqueue will always have to be scheduled at least in order to release the wait_tail of the previous rcu_sr_normal_gp_cleanup() call.

No, it does not avoid for sure :) I will add more explanation.

Quoted 7 lines

But indeed you keep the optimization that performs the completions themselves synchronously from the GP kthread if there aren't too many of them (which probably is the case most of the time).

Quoted 2 lines

I am OK with both cases. Dropping the patch will make it more simple for sure.

Quoted 8 lines
Quoted 6 lines

I am ok with both cases as well :-)

You choose. But note that the time spent doing the completions from the GP kthread may come at the expense of delaying the start of the next grace period, on which further synchronous RCU calls may in turn depend on...

That is a true point. Therefore we do it with a fixed number which should not influence on a GP.

Sounds good!

#16 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

In reply to Uladzislau Rezki (Sony)
Quoted 9 lines · Uladzislau Rezki (Sony)
kernel/rcu/tree.c +9 -0
+static struct llist_node *rcu_sr_get_wait_head(void)
+{
+	struct sr_wait_node *sr_wn;
+	int i;
+
+	for (i = 0; i < SR_NORMAL_GP_WAIT_HEAD_MAX; i++) {
+		sr_wn = &(rcu_state.srs_wait_nodes)[i];
+
+		if (!atomic_cmpxchg_acquire(&sr_wn->inuse, 0, 1))

kernel/rcu/tree.c static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) · line 1578

Quoted 10 lines
kernel/rcu/tree.c +10 -0
+			return &sr_wn->node;
+	}
+
+	return NULL;
+}
+
+static void rcu_sr_put_wait_head(struct llist_node *node)
+{
+	struct sr_wait_node *sr_wn = container_of(node, struct sr_wait_node, node);
+	atomic_set_release(&sr_wn->inuse, 0);

Can we comment this release/acquire pair?

IIUC, the point is that we must make sure that the wait_head->next fetch happens before it gets reused and rewritten to something else.

Thanks.

#17 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

In reply to Uladzislau Rezki (Sony)
Quoted 10 lines · Uladzislau Rezki (Sony)
kernel/rcu/tree.c +10 -0
+/*
+ * Helper function for rcu_gp_cleanup().
+ */
+static void rcu_sr_normal_gp_cleanup(void)
+{
+	struct llist_node *wait_tail;
+
+	wait_tail = rcu_state.srs_wait_tail;
+	if (wait_tail == NULL)
+		return;

kernel/rcu/tree.c static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) · line 1661

Quoted 10 lines
kernel/rcu/tree.c +10 -0
+
+	rcu_state.srs_wait_tail = NULL;
+	ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_wait_tail);
+
+	// concurrent sr_normal_gp_cleanup work might observe this update.
+	smp_store_release(&rcu_state.srs_done_tail, wait_tail);
+	ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_done_tail);
+
+	if (wait_tail)
+		queue_work(system_highpri_wq, &rcu_state.srs_cleanup_work);

You can queue unconditionally here, wait_tail is non-NULL at this point.

Thanks.

#18 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

In reply to Uladzislau Rezki (Sony)
Quoted 46 lines · Uladzislau Rezki (Sony)

A call to a synchronize_rcu() can be optimized from a latency point of view. Workloads which depend on this can benefit of it.

The delay of wakeme_after_rcu() callback, which unblocks a waiter, depends on several factors:

- how fast a process of offloading is started. Combination of:
- !CONFIG_RCU_NOCB_CPU/CONFIG_RCU_NOCB_CPU;
- !CONFIG_RCU_LAZY/CONFIG_RCU_LAZY;
- other.
- when started, invoking path is interrupted due to:
- time limit;
- need_resched();
- if limit is reached.
- where in a nocb list it is located;
- how fast previous callbacks completed;

Example:

1. On our embedded devices i can easily trigger the scenario when it is a last in the list out of ~3600 callbacks:

<snip>
<...>-29 [001] d..1. 21950.145313: rcu_batch_start: rcu_preempt CBs=3613 bl=28
...

  <...>-29      [001] ..... 21950.152578: rcu_invoke_callback: rcu_preempt rhp=00000000b2d6dee8 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152579: rcu_invoke_callback: rcu_preempt rhp=00000000a446f607 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152580: rcu_invoke_callback: rcu_preempt rhp=00000000a5cab03b func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152581: rcu_invoke_callback: rcu_preempt rhp=0000000013b7e5ee func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152582: rcu_invoke_callback: rcu_preempt rhp=000000000a8ca6f9 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152583: rcu_invoke_callback: rcu_preempt rhp=000000008f162ca8 func=wakeme_after_rcu.cfi_jt
  <...>-29      [001] d..1. 21950.152625: rcu_batch_end: rcu_preempt CBs-invoked=3612 idle=....

<snip>

2. We use cpuset/cgroup to classify tasks and assign them into different cgroups. For example "backgrond" group which binds tasks only to little CPUs or "foreground" which makes use of all CPUs. Tasks can be migrated between groups by a request if an acceleration is needed.

See below an example how "surfaceflinger" task gets migrated. Initially it is located in the "system-background" cgroup which allows to run only on little cores. In order to speed it up it can be temporary moved into "foreground" cgroup which allows to use big/all CPUs:

Quoted 6 lines

cgroup_attach_task():
-> cgroup_migrate_execute()
-> cpuset_can_attach()

     -> percpu_down_write()
       -> rcu_sync_enter()
         -> synchronize_rcu()

We should do this patch but I wonder also if cgroup_attach_task() usage of synchronize_rcu() should actually be using the _expedited() variant (via some possible flag to the percpu rwsem / rcu_sync).

If the user assumes it a slow path, then usage of _expedited() should probably be OK. If it is assumed a fast path, then it is probably hurting latency anyway without the enablement of this patch's rcu_normal_wake_from_gp.

Thoughts?

Then it becomes a matter of how to plumb the expeditedness down the stack.

Also, speaking of percpu rwsem, I noticed that percpu refcounts don't use rcu_sync. I haven't looked closely why, but something I hope to get time to look into is if it can be converted over and what benefits would that entail if any.

Also will continue reviewing the patch. Thanks.

- Joel

author maintainer
#19 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 60 lines · Joel Fernandes
Quoted 46 lines · Uladzislau Rezki (Sony)

A call to a synchronize_rcu() can be optimized from a latency point of view. Workloads which depend on this can benefit of it.

The delay of wakeme_after_rcu() callback, which unblocks a waiter, depends on several factors:

- how fast a process of offloading is started. Combination of:
- !CONFIG_RCU_NOCB_CPU/CONFIG_RCU_NOCB_CPU;
- !CONFIG_RCU_LAZY/CONFIG_RCU_LAZY;
- other.
- when started, invoking path is interrupted due to:
- time limit;
- need_resched();
- if limit is reached.
- where in a nocb list it is located;
- how fast previous callbacks completed;

Example:

1. On our embedded devices i can easily trigger the scenario when it is a last in the list out of ~3600 callbacks:

<snip>
<...>-29 [001] d..1. 21950.145313: rcu_batch_start: rcu_preempt CBs=3613 bl=28
...

  <...>-29      [001] ..... 21950.152578: rcu_invoke_callback: rcu_preempt rhp=00000000b2d6dee8 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152579: rcu_invoke_callback: rcu_preempt rhp=00000000a446f607 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152580: rcu_invoke_callback: rcu_preempt rhp=00000000a5cab03b func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152581: rcu_invoke_callback: rcu_preempt rhp=0000000013b7e5ee func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152582: rcu_invoke_callback: rcu_preempt rhp=000000000a8ca6f9 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152583: rcu_invoke_callback: rcu_preempt rhp=000000008f162ca8 func=wakeme_after_rcu.cfi_jt
  <...>-29      [001] d..1. 21950.152625: rcu_batch_end: rcu_preempt CBs-invoked=3612 idle=....

<snip>

2. We use cpuset/cgroup to classify tasks and assign them into different cgroups. For example "backgrond" group which binds tasks only to little CPUs or "foreground" which makes use of all CPUs. Tasks can be migrated between groups by a request if an acceleration is needed.

See below an example how "surfaceflinger" task gets migrated. Initially it is located in the "system-background" cgroup which allows to run only on little cores. In order to speed it up it can be temporary moved into "foreground" cgroup which allows to use big/all CPUs:

Quoted 6 lines

cgroup_attach_task():
-> cgroup_migrate_execute()
-> cpuset_can_attach()

     -> percpu_down_write()
       -> rcu_sync_enter()
         -> synchronize_rcu()

We should do this patch but I wonder also if cgroup_attach_task() usage of synchronize_rcu() should actually be using the _expedited() variant (via some possible flag to the percpu rwsem / rcu_sync).

Quoted 6 lines

If the user assumes it a slow path, then usage of _expedited() should probably be OK. If it is assumed a fast path, then it is probably hurting latency anyway without the enablement of this patch's rcu_normal_wake_from_gp.

Thoughts?

How i see it, the rcu_normal_wake_from_gp is disabled so far. We need to work on this further to have it on by default. But we will move toward this.

Quoted 8 lines

Then it becomes a matter of how to plumb the expeditedness down the stack.

Also, speaking of percpu rwsem, I noticed that percpu refcounts don't use rcu_sync. I haven't looked closely why, but something I hope to get time to look into is if it can be converted over and what benefits would that entail if any.

Also will continue reviewing the patch. Thanks.

Thanks.

--
Uladzislau Rezki

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

#21 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 44 lines · Joel Fernandes
Quoted 40 lines · Uladzislau Rezki (Sony)

A call to a synchronize_rcu() can be optimized from a latency point of view. Workloads which depend on this can benefit of it.

The delay of wakeme_after_rcu() callback, which unblocks a waiter, depends on several factors:

- how fast a process of offloading is started. Combination of:
- !CONFIG_RCU_NOCB_CPU/CONFIG_RCU_NOCB_CPU;
- !CONFIG_RCU_LAZY/CONFIG_RCU_LAZY;
- other.
- when started, invoking path is interrupted due to:
- time limit;
- need_resched();
- if limit is reached.
- where in a nocb list it is located;
- how fast previous callbacks completed;

Example:

1. On our embedded devices i can easily trigger the scenario when it is a last in the list out of ~3600 callbacks:

<snip>
<...>-29 [001] d..1. 21950.145313: rcu_batch_start: rcu_preempt CBs=3613 bl=28
...

  <...>-29      [001] ..... 21950.152578: rcu_invoke_callback: rcu_preempt rhp=00000000b2d6dee8 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152579: rcu_invoke_callback: rcu_preempt rhp=00000000a446f607 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152580: rcu_invoke_callback: rcu_preempt rhp=00000000a5cab03b func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152581: rcu_invoke_callback: rcu_preempt rhp=0000000013b7e5ee func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152582: rcu_invoke_callback: rcu_preempt rhp=000000000a8ca6f9 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152583: rcu_invoke_callback: rcu_preempt rhp=000000008f162ca8 func=wakeme_after_rcu.cfi_jt
  <...>-29      [001] d..1. 21950.152625: rcu_batch_end: rcu_preempt CBs-invoked=3612 idle=....

<snip>

2. We use cpuset/cgroup to classify tasks and assign them into different cgroups. For example "backgrond" group which binds tasks only to little CPUs or "foreground" which makes use of all CPUs. Tasks can be migrated between groups by a request if an acceleration is needed.

[...]

Quoted 19 lines
Quoted 19 lines

* Initialize a new grace period. Return false if no grace period required. */

kernel/rcu/tree.c +2 -0
@@ -1432,6 +1711,7 @@ static noinline_for_stack bool rcu_gp_init(void)
      unsigned long mask;
      struct rcu_data *rdp;
      struct rcu_node *rnp = rcu_get_root();
+     bool start_new_poll;
 
      WRITE_ONCE(rcu_state.gp_activity, jiffies);
      raw_spin_lock_irq_rcu_node(rnp);
@@ -1456,10 +1736,24 @@ static noinline_for_stack bool rcu_gp_init(void)
      /* Record GP times before starting GP, hence rcu_seq_start(). */
      rcu_seq_start(&rcu_state.gp_seq);
      ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq);
+     start_new_poll = rcu_sr_normal_gp_init();
      trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
      rcu_poll_gp_seq_start(&rcu_state.gp_seq_polled_snap);
      raw_spin_unlock_irq_rcu_node(rnp);
 
Quoted 15 lines

kernel/rcu/tree.c static noinline_for_stack bool rcu_gp_init(void) · line 1756

Quoted 13 lines
kernel/rcu/tree.c +13 -0
+     /*
+      * The "start_new_poll" is set to true, only when this GP is not able
+      * to handle anything and there are outstanding users. It happens when
+      * the rcu_sr_normal_gp_init() function was not able to insert a dummy
+      * separator to the llist, because there were no left any dummy-nodes.
+      *
+      * Number of dummy-nodes is fixed, it could be that we are run out of
+      * them, if so we start a new pool request to repeat a try. It is rare
+      * and it means that a system is doing a slow processing of callbacks.
+      */
+     if (start_new_poll)
+             (void) start_poll_synchronize_rcu();
+

Optionally, does it make sense to print a warning if too many retries occurred?

For future work, I was wondering about slight modification to even avoid this "running out of nodes" issues, why not add a wait node to task_struct and use that. rcu_gp_init() can just use that. Then, there is no limit to how many callers or to the length of the list. And by definition, you cannot have more than 1 caller per task-struct. Would that not work?

So in rcu_gp_init(), use the wait node of the first task_struct on the top of the list, mark it as a "special node", perhaps with a flag that says it is also the dummy node.

But yeah the new design of the patch is really cool... if you are leaving it alone without going for above suggestion, I can add it to my backlog for future work.

Thanks.

#22 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 89 lines · Joel Fernandes
Quoted 44 lines · Joel Fernandes
Quoted 40 lines · Uladzislau Rezki (Sony)

A call to a synchronize_rcu() can be optimized from a latency point of view. Workloads which depend on this can benefit of it.

The delay of wakeme_after_rcu() callback, which unblocks a waiter, depends on several factors:

- how fast a process of offloading is started. Combination of:
- !CONFIG_RCU_NOCB_CPU/CONFIG_RCU_NOCB_CPU;
- !CONFIG_RCU_LAZY/CONFIG_RCU_LAZY;
- other.
- when started, invoking path is interrupted due to:
- time limit;
- need_resched();
- if limit is reached.
- where in a nocb list it is located;
- how fast previous callbacks completed;

Example:

1. On our embedded devices i can easily trigger the scenario when it is a last in the list out of ~3600 callbacks:

<snip>
<...>-29 [001] d..1. 21950.145313: rcu_batch_start: rcu_preempt CBs=3613 bl=28
...

  <...>-29      [001] ..... 21950.152578: rcu_invoke_callback: rcu_preempt rhp=00000000b2d6dee8 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152579: rcu_invoke_callback: rcu_preempt rhp=00000000a446f607 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152580: rcu_invoke_callback: rcu_preempt rhp=00000000a5cab03b func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152581: rcu_invoke_callback: rcu_preempt rhp=0000000013b7e5ee func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152582: rcu_invoke_callback: rcu_preempt rhp=000000000a8ca6f9 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152583: rcu_invoke_callback: rcu_preempt rhp=000000008f162ca8 func=wakeme_after_rcu.cfi_jt
  <...>-29      [001] d..1. 21950.152625: rcu_batch_end: rcu_preempt CBs-invoked=3612 idle=....

<snip>

2. We use cpuset/cgroup to classify tasks and assign them into different cgroups. For example "backgrond" group which binds tasks only to little CPUs or "foreground" which makes use of all CPUs. Tasks can be migrated between groups by a request if an acceleration is needed.

[...]

Quoted 19 lines
Quoted 19 lines

* Initialize a new grace period. Return false if no grace period required. */

kernel/rcu/tree.c +2 -0
@@ -1432,6 +1711,7 @@ static noinline_for_stack bool rcu_gp_init(void)
      unsigned long mask;
      struct rcu_data *rdp;
      struct rcu_node *rnp = rcu_get_root();
+     bool start_new_poll;
 
      WRITE_ONCE(rcu_state.gp_activity, jiffies);
      raw_spin_lock_irq_rcu_node(rnp);
@@ -1456,10 +1736,24 @@ static noinline_for_stack bool rcu_gp_init(void)
      /* Record GP times before starting GP, hence rcu_seq_start(). */
      rcu_seq_start(&rcu_state.gp_seq);
      ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq);
+     start_new_poll = rcu_sr_normal_gp_init();
      trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
      rcu_poll_gp_seq_start(&rcu_state.gp_seq_polled_snap);
      raw_spin_unlock_irq_rcu_node(rnp);
 
Quoted 15 lines

kernel/rcu/tree.c static noinline_for_stack bool rcu_gp_init(void) · line 1756

Quoted 13 lines
kernel/rcu/tree.c +13 -0
+     /*
+      * The "start_new_poll" is set to true, only when this GP is not able
+      * to handle anything and there are outstanding users. It happens when
+      * the rcu_sr_normal_gp_init() function was not able to insert a dummy
+      * separator to the llist, because there were no left any dummy-nodes.
+      *
+      * Number of dummy-nodes is fixed, it could be that we are run out of
+      * them, if so we start a new pool request to repeat a try. It is rare
+      * and it means that a system is doing a slow processing of callbacks.
+      */
+     if (start_new_poll)
+             (void) start_poll_synchronize_rcu();
+

Optionally, does it make sense to print a warning if too many retries occurred?

For future work, I was wondering about slight modification to even avoid this "running out of nodes" issues, why not add a wait node to task_struct and use that. rcu_gp_init() can just use that. Then, there is no limit to how many callers or to the length of the list. And by definition, you cannot have more than 1 caller per task-struct. Would that not work?

Quoted 7 lines

So in rcu_gp_init(), use the wait node of the first task_struct on the top of the list, mark it as a "special node", perhaps with a flag that says it is also the dummy node.

But yeah the new design of the patch is really cool... if you are leaving it alone without going for above suggestion, I can add it to my backlog for future work.

Ouch, let me clarify and sorry for so many messages, I meant use the same storage of the synchronous caller who's on top of the list (its rcu_synchronize node) as the "Dummy" demarkation in the list. Not sure if that makes sense or it will work, but I'm of the feeling that the limit of 5 might not be needed.

#23 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

In reply to Uladzislau Rezki (Sony)
Quoted 162 lines · Uladzislau Rezki (Sony)

A call to a synchronize_rcu() can be optimized from a latency point of view. Workloads which depend on this can benefit of it.

The delay of wakeme_after_rcu() callback, which unblocks a waiter, depends on several factors:

- how fast a process of offloading is started. Combination of:
- !CONFIG_RCU_NOCB_CPU/CONFIG_RCU_NOCB_CPU;
- !CONFIG_RCU_LAZY/CONFIG_RCU_LAZY;
- other.
- when started, invoking path is interrupted due to:
- time limit;
- need_resched();
- if limit is reached.
- where in a nocb list it is located;
- how fast previous callbacks completed;

Example:

1. On our embedded devices i can easily trigger the scenario when it is a last in the list out of ~3600 callbacks:

<snip>
<...>-29 [001] d..1. 21950.145313: rcu_batch_start: rcu_preempt CBs=3613 bl=28
...

  <...>-29      [001] ..... 21950.152578: rcu_invoke_callback: rcu_preempt rhp=00000000b2d6dee8 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152579: rcu_invoke_callback: rcu_preempt rhp=00000000a446f607 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152580: rcu_invoke_callback: rcu_preempt rhp=00000000a5cab03b func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152581: rcu_invoke_callback: rcu_preempt rhp=0000000013b7e5ee func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152582: rcu_invoke_callback: rcu_preempt rhp=000000000a8ca6f9 func=__free_vm_area_struct.cfi_jt
  <...>-29      [001] ..... 21950.152583: rcu_invoke_callback: rcu_preempt rhp=000000008f162ca8 func=wakeme_after_rcu.cfi_jt
  <...>-29      [001] d..1. 21950.152625: rcu_batch_end: rcu_preempt CBs-invoked=3612 idle=....

<snip>

2. We use cpuset/cgroup to classify tasks and assign them into different cgroups. For example "backgrond" group which binds tasks only to little CPUs or "foreground" which makes use of all CPUs. Tasks can be migrated between groups by a request if an acceleration is needed.

See below an example how "surfaceflinger" task gets migrated. Initially it is located in the "system-background" cgroup which allows to run only on little cores. In order to speed it up it can be temporary moved into "foreground" cgroup which allows to use big/all CPUs:

cgroup_attach_task():
-> cgroup_migrate_execute()
-> cpuset_can_attach()

     -> percpu_down_write()
       -> rcu_sync_enter()
         -> synchronize_rcu()

-> now move tasks to the new cgroup.
-> cgroup_migrate_finish()

<snip>

         rcuop/1-29      [000] .....  7030.528570: rcu_invoke_callback: rcu_preempt rhp=00000000461605e0 func=wakeme_after_rcu.cfi_jt
    PERFD-SERVER-1855    [000] d..1.  7030.530293: cgroup_attach_task: dst_root=3 dst_id=22 dst_level=1 dst_path=/foreground pid=1900 comm=surfaceflinger
   TimerDispatch-2768    [002] d..5.  7030.537542: sched_migrate_task: comm=surfaceflinger pid=1900 prio=98 orig_cpu=0 dest_cpu=4

<snip>

"Boosting a task" depends on synchronize_rcu() latency:

- first trace shows a completion of synchronize_rcu();
- second shows attaching a task to a new group;
- last shows a final step when migration occurs.

3. To address this drawback, maintain a separate track that consists of synchronize_rcu() callers only. After completion of a grace period users are deferred to a dedicated worker to process requests.

4. This patch reduces the latency of synchronize_rcu() approximately by ~30-40% on synthetic tests. The real test case, camera launch time, shows(time is in milliseconds):

1-run 542 vs 489 improvement 9%
2-run 540 vs 466 improvement 13%
3-run 518 vs 468 improvement 9%
4-run 531 vs 457 improvement 13%
5-run 548 vs 475 improvement 13%
6-run 509 vs 484 improvement 4%

Synthetic test(no "noise" from other callbacks):
Hardware: x86_64 64 CPUs, 64GB of memory
Linux-6.6

- 10K tasks(simultaneous);
- each task does(1000 loops)

     synchronize_rcu();
     kfree(p);

default: CONFIG_RCU_NOCB_CPU: takes 54 seconds to complete all users;
patch: CONFIG_RCU_NOCB_CPU: takes 35 seconds to complete all users.

Running 60K gives approximately same results on my setup. Please note it is without any interaction with another type of callbacks, otherwise it will impact a lot a default case.

5. By default it is disabled. To enable this perform one of the below sequence:

echo 1 > /sys/module/rcutree/parameters/rcu_normal_wake_from_gp or pass a boot parameter "rcutree.rcu_normal_wake_from_gp=1"

---
 .../admin-guide/kernel-parameters.txt         |  14 +
 kernel/rcu/tree.c                             | 331 +++++++++++++++++-
 kernel/rcu/tree_exp.h                         |   2 +-
 3 files changed, 345 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +14 -0
index 2244aa0a013b..7ca84cf7b4f4 100644
@@ -5059,6 +5059,20 @@
 			delay, memory pressure or callback list growing too
 			big.
 
+	rcutree.rcu_normal_wake_from_gp= [KNL]
+			Reduces a latency of synchronize_rcu() call. This approach
+			maintains its own track of synchronize_rcu() callers, so it
+			does not interact with regular callbacks because it does not
+			use a call_rcu[_hurry]() path. Please note, this is for a
+			normal grace period.
+
+			How to enable it:
+
+			echo 1 > /sys/module/rcutree/parameters/rcu_normal_wake_from_gp
+			or pass a boot parameter "rcutree.rcu_normal_wake_from_gp=1"
+
+			Default is 0.
+
 	rcuscale.gp_async= [KNL]
 			Measure performance of asynchronous
 			grace-period primitives such as call_rcu().
kernel/rcu/tree.c +3 -0
index c8980d76f402..1328da63c3cd 100644
@@ -75,6 +75,7 @@
 #define MODULE_PARAM_PREFIX "rcutree."
 
 /* Data structures. */
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *);
 
 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
 	.gpwrap = true,
@@ -93,6 +94,8 @@ static struct rcu_state rcu_state = {
 	.exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
 	.exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
 	.ofl_lock = __ARCH_SPIN_LOCK_UNLOCKED,
+	.srs_cleanup_work = __WORK_INITIALIZER(rcu_state.srs_cleanup_work,
+		rcu_sr_normal_gp_cleanup_work),
 };
 
 /* Dump rcu_node combining tree at boot to verify correct setup. */
@@ -1422,6 +1425,282 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)
 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
 }

[..]

kernel/rcu/tree.c static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) · line 1703

Quoted 5 lines
kernel/rcu/tree.c +5 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)
+{
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+}
+

I'm a bit concerned from a memory order PoV about this llist_add() happening possibly on a different CPU than the GP thread, and different than the kworker thread. Basically we can have 3 CPUs simultaneously modifying and reading the list, but only 2 CPUs have the acq-rel pair AFAICS.

Consider the following situation:

synchronize_rcu() user
----------------------
llist_add the user U - update srs_next list

rcu_gp_init() and rcu_gp_cleanup (SAME THREAD)
--------------------
insert dummy node in front of U, call it S
update wait_tail to U

and then cleanup:
read wait_tail to W
set wait_tail to NULL
set done_tail to W (RELEASE) -- this release ensures U and S are seen by worker.

workqueue handler
-----------------
read done_tail (ACQUIRE)
disconnect rest of list -- disconnected list guaranteed to have U and S,
if done_tail read was W.
---------------------------------

So llist_add() does this (assume new_first and new_last are same):

	struct llist_node *first = READ_ONCE(head->first);

	do {
		new_last->next = first;
	} while (!try_cmpxchg(&head->first, &first, new_first));

	return !first;

---

It reads head->first, then writes the new_last->next (call it new_first->next) to the old first, then sets head->first to the new_first if head->first did not change in the meanwhile.

The problem I guess happens if the update the head->first is seen *after* the update to the new_first->next.

This potentially means a corrupted list is seen in the workqueue handler.. because the "U" node is not yet seen pointing to the rest of the list (previously added nodes), but is already seen the head of the list.

I am not sure if this can happen, but AFAIK try_cmpxchg() doesn't imply ordering per-se. Maybe that try_cmpxchg() should be a try_cmpxchg_release() in llist_add() ?

thanks,

- Joel

#24 lore raw

Re: [PATCH v5 2/4] rcu: Reduce synchronize_rcu() latency

Quoted 3 lines · Joel Fernandes

On 2/20/2024 1:31 PM, Uladzislau Rezki (Sony) wrote:

[...]

Quoted 77 lines
Quoted 24 lines
kernel/rcu/tree.c +3 -0
index c8980d76f402..1328da63c3cd 100644
@@ -75,6 +75,7 @@
 #define MODULE_PARAM_PREFIX "rcutree."
 
 /* Data structures. */
+static void rcu_sr_normal_gp_cleanup_work(struct work_struct *);
 
 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
 	.gpwrap = true,
@@ -93,6 +94,8 @@ static struct rcu_state rcu_state = {
 	.exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
 	.exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
 	.ofl_lock = __ARCH_SPIN_LOCK_UNLOCKED,
+	.srs_cleanup_work = __WORK_INITIALIZER(rcu_state.srs_cleanup_work,
+		rcu_sr_normal_gp_cleanup_work),
 };
 
 /* Dump rcu_node combining tree at boot to verify correct setup. */
@@ -1422,6 +1425,282 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)
 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
 }

[..]

kernel/rcu/tree.c static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) · line 1703

Quoted 5 lines
kernel/rcu/tree.c +5 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)
+{
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+}
+

I'm a bit concerned from a memory order PoV about this llist_add() happening possibly on a different CPU than the GP thread, and different than the kworker thread. Basically we can have 3 CPUs simultaneously modifying and reading the list, but only 2 CPUs have the acq-rel pair AFAICS.

Consider the following situation:

synchronize_rcu() user
----------------------
llist_add the user U - update srs_next list

rcu_gp_init() and rcu_gp_cleanup (SAME THREAD)
--------------------
insert dummy node in front of U, call it S
update wait_tail to U

and then cleanup:
read wait_tail to W
set wait_tail to NULL
set done_tail to W (RELEASE) -- this release ensures U and S are seen by worker.

workqueue handler
-----------------
read done_tail (ACQUIRE)
disconnect rest of list -- disconnected list guaranteed to have U and S,
if done_tail read was W.
---------------------------------

So llist_add() does this (assume new_first and new_last are same):

	struct llist_node *first = READ_ONCE(head->first);

	do {
		new_last->next = first;
	} while (!try_cmpxchg(&head->first, &first, new_first));

	return !first;

---

It reads head->first, then writes the new_last->next (call it new_first->next) to the old first, then sets head->first to the new_first if head->first did not change in the meanwhile.

The problem I guess happens if the update the head->first is seen *after* the update to the new_first->next.

Quoted 6 lines

This potentially means a corrupted list is seen in the workqueue handler.. because the "U" node is not yet seen pointing to the rest of the list (previously added nodes), but is already seen the head of the list.

I am not sure if this can happen, but AFAIK try_cmpxchg() doesn't imply ordering per-se. Maybe that try_cmpxchg() should be a try_cmpxchg_release() in llist_add() ?

Everyone in the internal RCU crew corrected me offline that try_cmpxchg() has full ordering if the cmpxchg succeeded.

So I don't think the issue I mentioned can occur, So we can park this.

Thanks!

- Joel

author maintainer
#25 lore raw

PATCH v5 3/4 rcu: Add a trace event for synchronize_rcu_normal()

In reply to Uladzislau Rezki (Sony)

Add an rcu_sr_normal() trace event. It takes three arguments first one is the name of RCU flavour, second one is a user id which triggeres synchronize_rcu_normal() and last one is an event.

There are two traces in the synchronize_rcu_normal(). On entry, when a new request is registered and on exit point when request is completed.

Please note, CONFIG_RCU_TRACE=y is required to activate traces.

---
 include/trace/events/rcu.h | 27 +++++++++++++++++++++++++++
 kernel/rcu/tree.c          |  7 ++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)
include/trace/events/rcu.h +27 -0
index 2ef9c719772a..31b3e0d3e65f 100644
@@ -707,6 +707,33 @@ TRACE_EVENT_RCU(rcu_invoke_kfree_bulk_callback,
 		__entry->rcuname, __entry->p, __entry->nr_records)
 );
 
+/*
+ * Tracepoint for a normal synchronize_rcu() states. The first argument
+ * is the RCU flavor, the second argument is a pointer to rcu_head the
+ * last one is an event.
+ */
+TRACE_EVENT_RCU(rcu_sr_normal,
+
+	TP_PROTO(const char *rcuname, struct rcu_head *rhp, const char *srevent),
+
+	TP_ARGS(rcuname, rhp, srevent),
+
+	TP_STRUCT__entry(
+		__field(const char *, rcuname)
+		__field(void *, rhp)
+		__field(const char *, srevent)
+	),
+
+	TP_fast_assign(
+		__entry->rcuname = rcuname;
+		__entry->rhp = rhp;
+		__entry->srevent = srevent;
+	),
+
+	TP_printk("%s rhp=0x%p event=%s",
+		__entry->rcuname, __entry->rhp, __entry->srevent)
+);
+
 /*
  * Tracepoint for exiting rcu_do_batch after RCU callbacks have been
  * invoked.  The first argument is the name of the RCU flavor,
kernel/rcu/tree.c +6 -1
index 1328da63c3cd..3bf6b3c5ef05 100644
@@ -3863,9 +3863,11 @@ static void synchronize_rcu_normal(void)
 {
 	struct rcu_synchronize rs;
 
+	trace_rcu_sr_normal(rcu_state.name, &rs.head, TPS("request"));
+
 	if (!READ_ONCE(rcu_normal_wake_from_gp)) {
 		wait_rcu_gp(call_rcu_hurry);
-		return;
+		goto trace_complete_out;
 	}
 
 	init_rcu_head_on_stack(&rs.head);
@@ -3886,6 +3888,9 @@ static void synchronize_rcu_normal(void)
 	/* Now we can wait. */
 	wait_for_completion(&rs.completion);
 	destroy_rcu_head_on_stack(&rs.head);
+
+trace_complete_out:
+	trace_rcu_sr_normal(rcu_state.name, &rs.head, TPS("complete"));
 }
 
 /**
Signature · 1 line
2.39.2
author maintainer
#26 lore raw

PATCH v5 4/4 rcu: Support direct wake-up of synchronize_rcu() users

In reply to Uladzislau Rezki (Sony)

This patch introduces a small enhancement which allows to do a direct wake-up of synchronize_rcu() callers. It occurs after a completion of grace period, thus by the gp-kthread.

Number of clients is limited by the hard-coded maximum allowed threshold. The remaining part, if still exists is deferred to a main worker.

---
 kernel/rcu/tree.c | 31 +++++++++++++++++++++++++++++--
 kernel/rcu/tree.h |  6 ++++++
 2 files changed, 35 insertions(+), 2 deletions(-)
kernel/rcu/tree.c +29 -2
index 3bf6b3c5ef05..31f3a61f9c38 100644
@@ -1644,7 +1644,8 @@ static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
  */
 static void rcu_sr_normal_gp_cleanup(void)
 {
-	struct llist_node *wait_tail;
+	struct llist_node *wait_tail, *next, *rcu;
+	int done = 0;
 
 	wait_tail = rcu_state.srs_wait_tail;
 	if (wait_tail == NULL)
@@ -1652,12 +1653,38 @@ static void rcu_sr_normal_gp_cleanup(void)
 
 	rcu_state.srs_wait_tail = NULL;
 	ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_wait_tail);
+	WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail));
+
+	/*
+	 * Process (a) and (d) cases. See an illustration. Apart of
+	 * that it handles the scenario when all clients are done,
+	 * wait-head is released if last. The worker is not kicked.
+	 */
+	llist_for_each_safe(rcu, next, wait_tail->next) {
+		if (rcu_sr_is_wait_head(rcu)) {
+			if (!rcu->next) {
+				rcu_sr_put_wait_head(rcu);
+				wait_tail->next = NULL;
+			} else {
+				wait_tail->next = rcu;
+			}
+
+			break;
+		}
+
+		rcu_sr_normal_complete(rcu);
+		// It can be last, update a next on this step.
+		wait_tail->next = next;
+
+		if (++done == SR_MAX_USERS_WAKE_FROM_GP)
+			break;
+	}
 
 	// concurrent sr_normal_gp_cleanup work might observe this update.
 	smp_store_release(&rcu_state.srs_done_tail, wait_tail);
 	ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_done_tail);
 
-	if (wait_tail)
+	if (wait_tail->next)
 		queue_work(system_highpri_wq, &rcu_state.srs_cleanup_work);
 }
 
kernel/rcu/tree.h +6 -0
index b942b9437438..2832787cee1d 100644
@@ -315,6 +315,12 @@ do {									\
 	__set_current_state(TASK_RUNNING);				\
 } while (0)
 
+/*
+ * A max threshold for synchronize_rcu() users which are
+ * awaken directly by the rcu_gp_kthread(). Left part is
+ * deferred to the main worker.
+ */
+#define SR_MAX_USERS_WAKE_FROM_GP 5
 #define SR_NORMAL_GP_WAIT_HEAD_MAX 5
 
 struct sr_wait_node {
Signature · 1 line
2.39.2
maintainer
#27 lore raw
In reply to Uladzislau Rezki (Sony)
Quoted 7 lines · Uladzislau Rezki (Sony)

This is a v5 that tends to improve synchronize_rcu() call in terms of latency reduction. This has been developed together with Neeraj Upadhyay. The delta between previous v4 and v5 is rather small. Main difference are cosmetic changes related to patch squashing and data structures splitting.

It is based on Paul's dev branch.

Very good, thank you!

Queued for further review and testing.

Thanx, Paul

Quoted 27 lines

v4 -> v5:
- furthers squashing to reduce number of patches;
- remove the CONFIG_RCU_SR_NORMAL_DEBUG_GP Kconfig option and reuse already existing debug option which is CONFIG_PROVE_RCU;
- add data structures in a separate patch.

v4: https://lore.kernel.org/lkml/[email protected]/T/
v3: https://lore.kernel.org/lkml/cd45b0b5-f86b-43fb-a5f3-47d340cd4f9f@paulmck-laptop/T/
v2: https://lore.kernel.org/all/[email protected]/T/
v1: https://lore.kernel.org/lkml/[email protected]/T/

Uladzislau Rezki (Sony) (4):
rcu: Add data structures for synchronize_rcu()
rcu: Reduce synchronize_rcu() latency
rcu: Add a trace event for synchronize_rcu_normal()
rcu: Support direct wake-up of synchronize_rcu() users

 .../admin-guide/kernel-parameters.txt         |  14 +
 include/trace/events/rcu.h                    |  27 ++
 kernel/rcu/tree.c                             | 363 +++++++++++++++++-
 kernel/rcu/tree.h                             |  20 +
 kernel/rcu/tree_exp.h                         |   2 +-
 5 files changed, 424 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.2