@@ -246,7 +246,6 @@ static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)
246
246
ssp -> srcu_sup -> node = NULL ;
247
247
mutex_init (& ssp -> srcu_sup -> srcu_cb_mutex );
248
248
mutex_init (& ssp -> srcu_sup -> srcu_gp_mutex );
249
- ssp -> srcu_idx = 0 ;
250
249
ssp -> srcu_sup -> srcu_gp_seq = SRCU_GP_SEQ_INITIAL_VAL ;
251
250
ssp -> srcu_sup -> srcu_barrier_seq = 0 ;
252
251
mutex_init (& ssp -> srcu_sup -> srcu_barrier_mutex );
@@ -510,38 +509,39 @@ static bool srcu_readers_active_idx_check(struct srcu_struct *ssp, int idx)
510
509
* If the locks are the same as the unlocks, then there must have
511
510
* been no readers on this index at some point in this function.
512
511
* But there might be more readers, as a task might have read
513
- * the current ->srcu_idx but not yet have incremented its CPU's
512
+ * the current ->srcu_ctrp but not yet have incremented its CPU's
514
513
* ->srcu_ctrs[idx].srcu_locks counter. In fact, it is possible
515
514
* that most of the tasks have been preempted between fetching
516
- * ->srcu_idx and incrementing ->srcu_ctrs[idx].srcu_locks. And there
517
- * could be almost (ULONG_MAX / sizeof(struct task_struct)) tasks
518
- * in a system whose address space was fully populated with memory.
519
- * Call this quantity Nt.
515
+ * ->srcu_ctrp and incrementing ->srcu_ctrs[idx].srcu_locks. And
516
+ * there could be almost (ULONG_MAX / sizeof(struct task_struct))
517
+ * tasks in a system whose address space was fully populated
518
+ * with memory. Call this quantity Nt.
520
519
*
521
- * So suppose that the updater is preempted at this point in the
522
- * code for a long time. That now-preempted updater has already
523
- * flipped ->srcu_idx (possibly during the preceding grace period),
524
- * done an smp_mb() (again, possibly during the preceding grace
525
- * period), and summed up the ->srcu_ctrs[idx].srcu_unlocks counters.
526
- * How many times can a given one of the aforementioned Nt tasks
527
- * increment the old ->srcu_idx value's ->srcu_ctrs[idx].srcu_locks
528
- * counter, in the absence of nesting?
520
+ * So suppose that the updater is preempted at this
521
+ * point in the code for a long time. That now-preempted
522
+ * updater has already flipped ->srcu_ctrp (possibly during
523
+ * the preceding grace period), done an smp_mb() (again,
524
+ * possibly during the preceding grace period), and summed up
525
+ * the ->srcu_ctrs[idx].srcu_unlocks counters. How many times
526
+ * can a given one of the aforementioned Nt tasks increment the
527
+ * old ->srcu_ctrp value's ->srcu_ctrs[idx].srcu_locks counter,
528
+ * in the absence of nesting?
529
529
*
530
530
* It can clearly do so once, given that it has already fetched
531
- * the old value of ->srcu_idx and is just about to use that
531
+ * the old value of ->srcu_ctrp and is just about to use that
532
532
* value to index its increment of ->srcu_ctrs[idx].srcu_locks.
533
533
* But as soon as it leaves that SRCU read-side critical section,
534
534
* it will increment ->srcu_ctrs[idx].srcu_unlocks, which must
535
- * follow the updater's above read from that same value. Thus,
536
- * as soon the reading task does an smp_mb() and a later fetch from
537
- * ->srcu_idx , that task will be guaranteed to get the new index.
535
+ * follow the updater's above read from that same value. Thus,
536
+ as soon the reading task does an smp_mb() and a later fetch from
537
+ * ->srcu_ctrp , that task will be guaranteed to get the new index.
538
538
* Except that the increment of ->srcu_ctrs[idx].srcu_unlocks
539
539
* in __srcu_read_unlock() is after the smp_mb(), and the fetch
540
- * from ->srcu_idx in __srcu_read_lock() is before the smp_mb().
541
- * Thus, that task might not see the new value of ->srcu_idx until
540
+ * from ->srcu_ctrp in __srcu_read_lock() is before the smp_mb().
541
+ * Thus, that task might not see the new value of ->srcu_ctrp until
542
542
* the -second- __srcu_read_lock(), which in turn means that this
543
543
* task might well increment ->srcu_ctrs[idx].srcu_locks for the
544
- * old value of ->srcu_idx twice, not just once.
544
+ * old value of ->srcu_ctrp twice, not just once.
545
545
*
546
546
* However, it is important to note that a given smp_mb() takes
547
547
* effect not just for the task executing it, but also for any
@@ -1095,7 +1095,7 @@ static void srcu_funnel_gp_start(struct srcu_struct *ssp, struct srcu_data *sdp,
1095
1095
/*
1096
1096
* Wait until all readers counted by array index idx complete, but
1097
1097
* loop an additional time if there is an expedited grace period pending.
1098
- * The caller must ensure that ->srcu_idx is not changed while checking.
1098
+ * The caller must ensure that ->srcu_ctrp is not changed while checking.
1099
1099
*/
1100
1100
static bool try_check_zero (struct srcu_struct * ssp , int idx , int trycount )
1101
1101
{
@@ -1113,30 +1113,30 @@ static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount)
1113
1113
}
1114
1114
1115
1115
/*
1116
- * Increment the ->srcu_idx counter so that future SRCU readers will
1116
+ * Increment the ->srcu_ctrp counter so that future SRCU readers will
1117
1117
* use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
1118
1118
* us to wait for pre-existing readers in a starvation-free manner.
1119
1119
*/
1120
1120
static void srcu_flip (struct srcu_struct * ssp )
1121
1121
{
1122
1122
/*
1123
- * Because the flip of ->srcu_idx is executed only if the
1123
+ * Because the flip of ->srcu_ctrp is executed only if the
1124
1124
* preceding call to srcu_readers_active_idx_check() found that
1125
1125
* the ->srcu_ctrs[].srcu_unlocks and ->srcu_ctrs[].srcu_locks sums
1126
1126
* matched and because that summing uses atomic_long_read(),
1127
1127
* there is ordering due to a control dependency between that
1128
1128
* summing and the WRITE_ONCE() in this call to srcu_flip().
1129
1129
* This ordering ensures that if this updater saw a given reader's
1130
1130
* increment from __srcu_read_lock(), that reader was using a value
1131
- * of ->srcu_idx from before the previous call to srcu_flip(),
1131
+ * of ->srcu_ctrp from before the previous call to srcu_flip(),
1132
1132
* which should be quite rare. This ordering thus helps forward
1133
1133
* progress because the grace period could otherwise be delayed
1134
1134
* by additional calls to __srcu_read_lock() using that old (soon
1135
- * to be new) value of ->srcu_idx .
1135
+ * to be new) value of ->srcu_ctrp .
1136
1136
*
1137
1137
* This sum-equality check and ordering also ensures that if
1138
1138
* a given call to __srcu_read_lock() uses the new value of
1139
- * ->srcu_idx , this updater's earlier scans cannot have seen
1139
+ * ->srcu_ctrp , this updater's earlier scans cannot have seen
1140
1140
* that reader's increments, which is all to the good, because
1141
1141
* this grace period need not wait on that reader. After all,
1142
1142
* if those earlier scans had seen that reader, there would have
@@ -1151,7 +1151,6 @@ static void srcu_flip(struct srcu_struct *ssp)
1151
1151
*/
1152
1152
smp_mb (); /* E */ /* Pairs with B and C. */
1153
1153
1154
- WRITE_ONCE (ssp -> srcu_idx , ssp -> srcu_idx + 1 ); // Flip the counter.
1155
1154
WRITE_ONCE (ssp -> srcu_ctrp ,
1156
1155
& ssp -> sda -> srcu_ctrs [!(ssp -> srcu_ctrp - & ssp -> sda -> srcu_ctrs [0 ])]);
1157
1156
@@ -1466,8 +1465,9 @@ EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
1466
1465
*
1467
1466
* Wait for the count to drain to zero of both indexes. To avoid the
1468
1467
* possible starvation of synchronize_srcu(), it waits for the count of
1469
- * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
1470
- * and then flip the srcu_idx and wait for the count of the other index.
1468
+ * the index=!(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0]) to drain to zero
1469
+ * at first, and then flip the ->srcu_ctrp and wait for the count of the
1470
+ * other index.
1471
1471
*
1472
1472
* Can block; must be called from process context.
1473
1473
*
@@ -1693,7 +1693,7 @@ static void srcu_advance_state(struct srcu_struct *ssp)
1693
1693
1694
1694
/*
1695
1695
* Because readers might be delayed for an extended period after
1696
- * fetching ->srcu_idx for their index, at any point in time there
1696
+ * fetching ->srcu_ctrp for their index, at any point in time there
1697
1697
* might well be readers using both idx=0 and idx=1. We therefore
1698
1698
* need to wait for readers to clear from both index values before
1699
1699
* invoking a callback.
@@ -1721,7 +1721,7 @@ static void srcu_advance_state(struct srcu_struct *ssp)
1721
1721
}
1722
1722
1723
1723
if (rcu_seq_state (READ_ONCE (ssp -> srcu_sup -> srcu_gp_seq )) == SRCU_STATE_SCAN1 ) {
1724
- idx = 1 ^ (ssp -> srcu_idx & 1 );
1724
+ idx = ! (ssp -> srcu_ctrp - & ssp -> sda -> srcu_ctrs [ 0 ] );
1725
1725
if (!try_check_zero (ssp , idx , 1 )) {
1726
1726
mutex_unlock (& ssp -> srcu_sup -> srcu_gp_mutex );
1727
1727
return ; /* readers present, retry later. */
@@ -1739,7 +1739,7 @@ static void srcu_advance_state(struct srcu_struct *ssp)
1739
1739
* SRCU read-side critical sections are normally short,
1740
1740
* so check at least twice in quick succession after a flip.
1741
1741
*/
1742
- idx = 1 ^ (ssp -> srcu_idx & 1 );
1742
+ idx = ! (ssp -> srcu_ctrp - & ssp -> sda -> srcu_ctrs [ 0 ] );
1743
1743
if (!try_check_zero (ssp , idx , 2 )) {
1744
1744
mutex_unlock (& ssp -> srcu_sup -> srcu_gp_mutex );
1745
1745
return ; /* readers present, retry later. */
@@ -1897,7 +1897,7 @@ void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf)
1897
1897
int ss_state = READ_ONCE (ssp -> srcu_sup -> srcu_size_state );
1898
1898
int ss_state_idx = ss_state ;
1899
1899
1900
- idx = ssp -> srcu_idx & 0x1 ;
1900
+ idx = ssp -> srcu_ctrp - & ssp -> sda -> srcu_ctrs [ 0 ] ;
1901
1901
if (ss_state < 0 || ss_state >= ARRAY_SIZE (srcu_size_state_name ))
1902
1902
ss_state_idx = ARRAY_SIZE (srcu_size_state_name ) - 1 ;
1903
1903
pr_alert ("%s%s Tree SRCU g%ld state %d (%s)" ,
0 commit comments