Skip to content

Commit 795e7ef

Browse files
paulmckrcufbq
authored andcommitted
srcu: Make SRCU readers use ->srcu_ctrs for counter selection
This commit causes SRCU readers to use ->srcu_ctrs for counter selection instead of ->srcu_idx. This takes another step towards array-indexing-free SRCU readers. [ paulmck: Apply kernel test robot feedback. ] Co-developed-by: Z qiang <[email protected]> Signed-off-by: Z qiang <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]> Tested-by: kernel test robot <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Kent Overstreet <[email protected]> Cc: <[email protected]> Signed-off-by: Boqun Feng <[email protected]>
1 parent 56eb8be commit 795e7ef

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

include/linux/srcutree.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ struct srcu_usage {
101101
*/
102102
struct srcu_struct {
103103
unsigned int srcu_idx; /* Current rdr array element. */
104+
struct srcu_ctr __percpu *srcu_ctrp;
104105
struct srcu_data __percpu *sda; /* Per-CPU srcu_data array. */
105106
struct lockdep_map dep_map;
106107
struct srcu_usage *srcu_sup; /* Update-side data. */
@@ -167,6 +168,7 @@ struct srcu_struct {
167168
#define __SRCU_STRUCT_INIT(name, usage_name, pcpu_name) \
168169
{ \
169170
.sda = &pcpu_name, \
171+
.srcu_ctrp = &pcpu_name.srcu_ctrs[0], \
170172
__SRCU_STRUCT_INIT_COMMON(name, usage_name) \
171173
}
172174

@@ -222,13 +224,12 @@ void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
222224
*/
223225
static inline int __srcu_read_lock_lite(struct srcu_struct *ssp)
224226
{
225-
int idx;
227+
struct srcu_ctr __percpu *scp = READ_ONCE(ssp->srcu_ctrp);
226228

227229
RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_lite().");
228-
idx = READ_ONCE(ssp->srcu_idx) & 0x1;
229-
this_cpu_inc(ssp->sda->srcu_ctrs[idx].srcu_locks.counter); /* Y */
230+
this_cpu_inc(scp->srcu_locks.counter); /* Y */
230231
barrier(); /* Avoid leaking the critical section. */
231-
return idx;
232+
return scp - &ssp->sda->srcu_ctrs[0];
232233
}
233234

234235
/*

kernel/rcu/srcutree.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,10 @@ static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)
253253
atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 0);
254254
INIT_DELAYED_WORK(&ssp->srcu_sup->work, process_srcu);
255255
ssp->srcu_sup->sda_is_static = is_static;
256-
if (!is_static)
256+
if (!is_static) {
257257
ssp->sda = alloc_percpu(struct srcu_data);
258+
ssp->srcu_ctrp = &ssp->sda->srcu_ctrs[0];
259+
}
258260
if (!ssp->sda)
259261
goto err_free_sup;
260262
init_srcu_struct_data(ssp);
@@ -742,12 +744,11 @@ EXPORT_SYMBOL_GPL(__srcu_check_read_flavor);
742744
*/
743745
int __srcu_read_lock(struct srcu_struct *ssp)
744746
{
745-
int idx;
747+
struct srcu_ctr __percpu *scp = READ_ONCE(ssp->srcu_ctrp);
746748

747-
idx = READ_ONCE(ssp->srcu_idx) & 0x1;
748-
this_cpu_inc(ssp->sda->srcu_ctrs[idx].srcu_locks.counter);
749+
this_cpu_inc(scp->srcu_locks.counter);
749750
smp_mb(); /* B */ /* Avoid leaking the critical section. */
750-
return idx;
751+
return scp - &ssp->sda->srcu_ctrs[0];
751752
}
752753
EXPORT_SYMBOL_GPL(__srcu_read_lock);
753754

@@ -772,13 +773,12 @@ EXPORT_SYMBOL_GPL(__srcu_read_unlock);
772773
*/
773774
int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
774775
{
775-
int idx;
776-
struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
776+
struct srcu_ctr __percpu *scpp = READ_ONCE(ssp->srcu_ctrp);
777+
struct srcu_ctr *scp = raw_cpu_ptr(scpp);
777778

778-
idx = READ_ONCE(ssp->srcu_idx) & 0x1;
779-
atomic_long_inc(&sdp->srcu_ctrs[idx].srcu_locks);
779+
atomic_long_inc(&scp->srcu_locks);
780780
smp_mb__after_atomic(); /* B */ /* Avoid leaking the critical section. */
781-
return idx;
781+
return scpp - &ssp->sda->srcu_ctrs[0];
782782
}
783783
EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe);
784784

@@ -1152,6 +1152,8 @@ static void srcu_flip(struct srcu_struct *ssp)
11521152
smp_mb(); /* E */ /* Pairs with B and C. */
11531153

11541154
WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); // Flip the counter.
1155+
WRITE_ONCE(ssp->srcu_ctrp,
1156+
&ssp->sda->srcu_ctrs[!(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0])]);
11551157

11561158
/*
11571159
* Ensure that if the updater misses an __srcu_read_unlock()
@@ -2000,6 +2002,7 @@ static int srcu_module_coming(struct module *mod)
20002002
ssp->sda = alloc_percpu(struct srcu_data);
20012003
if (WARN_ON_ONCE(!ssp->sda))
20022004
return -ENOMEM;
2005+
ssp->srcu_ctrp = &ssp->sda->srcu_ctrs[0];
20032006
}
20042007
return 0;
20052008
}

0 commit comments

Comments
 (0)