Skip to content

Commit c402062

Browse files
paulmckrcufbq
authored andcommitted
srcu: Add SRCU-fast readers
This commit adds srcu_read_{,un}lock_fast(), which is similar to srcu_read_{,un}lock_lite(), but avoids the array-indexing and pointer-following overhead. On a microbenchmark featuring tight loops around empty readers, this results in about a 20% speedup compared to RCU Tasks Trace on my x86 laptop. Please note that SRCU-fast has drawbacks compared to RCU Tasks Trace, including: o Lack of CPU stall warnings. o SRCU-fast readers permitted only where rcu_is_watching(). o A pointer-sized return value from srcu_read_lock_fast() must be passed to the corresponding srcu_read_unlock_fast(). o In the absence of readers, a synchronize_srcu() having _fast() readers will incur the latency of at least two normal RCU grace periods. o RCU Tasks Trace priority boosting could be easily added. Boosting SRCU readers is more difficult. SRCU-fast also has a drawback compared to SRCU-lite, namely that the return value from srcu_read_lock_fast()-fast is a 64-bit pointer and that from srcu_read_lock_lite() is only a 32-bit int. [ paulmck: Apply feedback from Akira Yokosawa. ] Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Andrii Nakryiko <andrii@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Kent Overstreet <kent.overstreet@linux.dev> Cc: <bpf@vger.kernel.org> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
1 parent 4439711 commit c402062

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

include/linux/srcu.h

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ int init_srcu_struct(struct srcu_struct *ssp);
4747
#define SRCU_READ_FLAVOR_NORMAL 0x1 // srcu_read_lock().
4848
#define SRCU_READ_FLAVOR_NMI 0x2 // srcu_read_lock_nmisafe().
4949
#define SRCU_READ_FLAVOR_LITE 0x4 // srcu_read_lock_lite().
50+
#define SRCU_READ_FLAVOR_FAST 0x8 // srcu_read_lock_fast().
5051
#define SRCU_READ_FLAVOR_ALL (SRCU_READ_FLAVOR_NORMAL | SRCU_READ_FLAVOR_NMI | \
51-
SRCU_READ_FLAVOR_LITE) // All of the above.
52-
#define SRCU_READ_FLAVOR_SLOWGP SRCU_READ_FLAVOR_LITE
52+
SRCU_READ_FLAVOR_LITE | SRCU_READ_FLAVOR_FAST) // All of the above.
53+
#define SRCU_READ_FLAVOR_SLOWGP (SRCU_READ_FLAVOR_LITE | SRCU_READ_FLAVOR_FAST)
5354
// Flavors requiring synchronize_rcu()
5455
// instead of smp_mb().
5556
void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases(ssp);
@@ -253,6 +254,33 @@ static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp)
253254
return retval;
254255
}
255256

257+
/**
258+
* srcu_read_lock_fast - register a new reader for an SRCU-protected structure.
259+
* @ssp: srcu_struct in which to register the new reader.
260+
*
261+
* Enter an SRCU read-side critical section, but for a light-weight
262+
* smp_mb()-free reader. See srcu_read_lock() for more information.
263+
*
264+
* If srcu_read_lock_fast() is ever used on an srcu_struct structure,
265+
* then none of the other flavors may be used, whether before, during,
266+
* or after. Note that grace-period auto-expediting is disabled for _fast
267+
* srcu_struct structures because auto-expedited grace periods invoke
268+
* synchronize_rcu_expedited(), IPIs and all.
269+
*
270+
* Note that srcu_read_lock_fast() can be invoked only from those contexts
271+
* where RCU is watching, that is, from contexts where it would be legal
272+
* to invoke rcu_read_lock(). Otherwise, lockdep will complain.
273+
*/
274+
static inline struct srcu_ctr __percpu *srcu_read_lock_fast(struct srcu_struct *ssp) __acquires(ssp)
275+
{
276+
struct srcu_ctr __percpu *retval;
277+
278+
srcu_check_read_flavor_force(ssp, SRCU_READ_FLAVOR_FAST);
279+
retval = __srcu_read_lock_fast(ssp);
280+
rcu_try_lock_acquire(&ssp->dep_map);
281+
return retval;
282+
}
283+
256284
/**
257285
* srcu_read_lock_lite - register a new reader for an SRCU-protected structure.
258286
* @ssp: srcu_struct in which to register the new reader.
@@ -356,6 +384,21 @@ static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx)
356384
__srcu_read_unlock(ssp, idx);
357385
}
358386

387+
/**
388+
* srcu_read_unlock_fast - unregister a old reader from an SRCU-protected structure.
389+
* @ssp: srcu_struct in which to unregister the old reader.
390+
* @scp: return value from corresponding srcu_read_lock_fast().
391+
*
392+
* Exit a light-weight SRCU read-side critical section.
393+
*/
394+
static inline void srcu_read_unlock_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp)
395+
__releases(ssp)
396+
{
397+
srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST);
398+
srcu_lock_release(&ssp->dep_map);
399+
__srcu_read_unlock_fast(ssp, scp);
400+
}
401+
359402
/**
360403
* srcu_read_unlock_lite - unregister a old reader from an SRCU-protected structure.
361404
* @ssp: srcu_struct in which to unregister the old reader.

include/linux/srcutiny.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ static inline int __srcu_read_lock(struct srcu_struct *ssp)
7171
return idx;
7272
}
7373

74+
struct srcu_ctr;
75+
76+
static inline bool __srcu_ptr_to_ctr(struct srcu_struct *ssp, struct srcu_ctr __percpu *scpp)
77+
{
78+
return (int)(intptr_t)(struct srcu_ctr __force __kernel *)scpp;
79+
}
80+
81+
static inline struct srcu_ctr __percpu *__srcu_ctr_to_ptr(struct srcu_struct *ssp, int idx)
82+
{
83+
return (struct srcu_ctr __percpu *)(intptr_t)idx;
84+
}
85+
86+
static inline struct srcu_ctr __percpu *__srcu_read_lock_fast(struct srcu_struct *ssp)
87+
{
88+
return __srcu_ctr_to_ptr(ssp, __srcu_read_lock(ssp));
89+
}
90+
91+
static inline void __srcu_read_unlock_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp)
92+
{
93+
__srcu_read_unlock(ssp, __srcu_ptr_to_ctr(ssp, scp));
94+
}
95+
7496
#define __srcu_read_lock_lite __srcu_read_lock
7597
#define __srcu_read_unlock_lite __srcu_read_unlock
7698

include/linux/srcutree.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,44 @@ static inline struct srcu_ctr __percpu *__srcu_ctr_to_ptr(struct srcu_struct *ss
226226
return &ssp->sda->srcu_ctrs[idx];
227227
}
228228

229+
/*
230+
* Counts the new reader in the appropriate per-CPU element of the
231+
* srcu_struct. Returns a pointer that must be passed to the matching
232+
* srcu_read_unlock_fast().
233+
*
234+
* Note that this_cpu_inc() is an RCU read-side critical section either
235+
* because it disables interrupts, because it is a single instruction,
236+
* or because it is a read-modify-write atomic operation, depending on
237+
* the whims of the architecture.
238+
*/
239+
static inline struct srcu_ctr __percpu *__srcu_read_lock_fast(struct srcu_struct *ssp)
240+
{
241+
struct srcu_ctr __percpu *scp = READ_ONCE(ssp->srcu_ctrp);
242+
243+
RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_fast().");
244+
this_cpu_inc(scp->srcu_locks.counter); /* Y */
245+
barrier(); /* Avoid leaking the critical section. */
246+
return scp;
247+
}
248+
249+
/*
250+
* Removes the count for the old reader from the appropriate
251+
* per-CPU element of the srcu_struct. Note that this may well be a
252+
* different CPU than that which was incremented by the corresponding
253+
* srcu_read_lock_fast(), but it must be within the same task.
254+
*
255+
* Note that this_cpu_inc() is an RCU read-side critical section either
256+
* because it disables interrupts, because it is a single instruction,
257+
* or because it is a read-modify-write atomic operation, depending on
258+
* the whims of the architecture.
259+
*/
260+
static inline void __srcu_read_unlock_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp)
261+
{
262+
barrier(); /* Avoid leaking the critical section. */
263+
this_cpu_inc(scp->srcu_unlocks.counter); /* Z */
264+
RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_unlock_fast().");
265+
}
266+
229267
/*
230268
* Counts the new reader in the appropriate per-CPU element of the
231269
* srcu_struct. Returns an index that must be passed to the matching

0 commit comments

Comments
 (0)