Skip to content

Commit c2d158a

Browse files
Frederic Weisbeckerpaulmckrcu
authored andcommitted
srcu: Debug NMI safety even on archs that don't require it
Currently the NMI safety debugging is only performed on architectures that don't support NMI-safe this_cpu_inc(). Reorder the code so that other architectures like x86 also detect bad uses. [ paulmck: Apply kernel test robot, Stephen Rothwell, and Zqiang feedback. ] Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 628bda7 commit c2d158a

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

include/linux/srcu.h

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ int init_srcu_struct(struct srcu_struct *ssp);
5252
#else
5353
/* Dummy definition for things like notifiers. Actual use gets link error. */
5454
struct srcu_struct { };
55-
int __srcu_read_lock_nmisafe(struct srcu_struct *ssp, bool chknmisafe) __acquires(ssp);
56-
void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx, bool chknmisafe) __releases(ssp);
5755
#endif
5856

5957
void call_srcu(struct srcu_struct *ssp, struct rcu_head *head,
@@ -66,6 +64,20 @@ unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp);
6664
unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp);
6765
bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie);
6866

67+
#ifdef CONFIG_NEED_SRCU_NMI_SAFE
68+
int __srcu_read_lock_nmisafe(struct srcu_struct *ssp) __acquires(ssp);
69+
void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) __releases(ssp);
70+
#else
71+
static inline int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
72+
{
73+
return __srcu_read_lock(ssp);
74+
}
75+
static inline void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
76+
{
77+
__srcu_read_unlock(ssp, idx);
78+
}
79+
#endif /* CONFIG_NEED_SRCU_NMI_SAFE */
80+
6981
#ifdef CONFIG_SRCU
7082
void srcu_init(void);
7183
#else /* #ifdef CONFIG_SRCU */
@@ -106,6 +118,18 @@ static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
106118

107119
#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
108120

121+
#define SRCU_NMI_UNKNOWN 0x0
122+
#define SRCU_NMI_UNSAFE 0x1
123+
#define SRCU_NMI_SAFE 0x2
124+
125+
#if defined(CONFIG_PROVE_RCU) && defined(CONFIG_TREE_SRCU)
126+
void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe);
127+
#else
128+
static inline void srcu_check_nmi_safety(struct srcu_struct *ssp,
129+
bool nmi_safe) { }
130+
#endif
131+
132+
109133
/**
110134
* srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
111135
* @p: the pointer to fetch and protect for later dereferencing
@@ -163,6 +187,7 @@ static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp)
163187
{
164188
int retval;
165189

190+
srcu_check_nmi_safety(ssp, false);
166191
retval = __srcu_read_lock(ssp);
167192
rcu_lock_acquire(&(ssp)->dep_map);
168193
return retval;
@@ -179,10 +204,8 @@ static inline int srcu_read_lock_nmisafe(struct srcu_struct *ssp) __acquires(ssp
179204
{
180205
int retval;
181206

182-
if (IS_ENABLED(CONFIG_NEED_SRCU_NMI_SAFE))
183-
retval = __srcu_read_lock_nmisafe(ssp, true);
184-
else
185-
retval = __srcu_read_lock(ssp);
207+
srcu_check_nmi_safety(ssp, true);
208+
retval = __srcu_read_lock_nmisafe(ssp);
186209
rcu_lock_acquire(&(ssp)->dep_map);
187210
return retval;
188211
}
@@ -193,6 +216,7 @@ srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp)
193216
{
194217
int retval;
195218

219+
srcu_check_nmi_safety(ssp, false);
196220
retval = __srcu_read_lock(ssp);
197221
return retval;
198222
}
@@ -208,6 +232,7 @@ static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx)
208232
__releases(ssp)
209233
{
210234
WARN_ON_ONCE(idx & ~0x1);
235+
srcu_check_nmi_safety(ssp, false);
211236
rcu_lock_release(&(ssp)->dep_map);
212237
__srcu_read_unlock(ssp, idx);
213238
}
@@ -223,17 +248,16 @@ static inline void srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
223248
__releases(ssp)
224249
{
225250
WARN_ON_ONCE(idx & ~0x1);
251+
srcu_check_nmi_safety(ssp, true);
226252
rcu_lock_release(&(ssp)->dep_map);
227-
if (IS_ENABLED(CONFIG_NEED_SRCU_NMI_SAFE))
228-
__srcu_read_unlock_nmisafe(ssp, idx, true);
229-
else
230-
__srcu_read_unlock(ssp, idx);
253+
__srcu_read_unlock_nmisafe(ssp, idx);
231254
}
232255

233256
/* Used by tracing, cannot be traced and cannot call lockdep. */
234257
static inline notrace void
235258
srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp)
236259
{
260+
srcu_check_nmi_safety(ssp, false);
237261
__srcu_read_unlock(ssp, idx);
238262
}
239263

include/linux/srcutiny.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,4 @@ static inline void srcu_torture_stats_print(struct srcu_struct *ssp,
8989
data_race(READ_ONCE(ssp->srcu_idx)),
9090
data_race(READ_ONCE(ssp->srcu_idx_max)));
9191
}
92-
93-
static inline int __srcu_read_lock_nmisafe(struct srcu_struct *ssp, bool chknmisafe)
94-
{
95-
BUG();
96-
return 0;
97-
}
98-
99-
static inline void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx, bool chknmisafe)
100-
{
101-
BUG();
102-
}
103-
10492
#endif

include/linux/srcutree.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ struct srcu_data {
4343
struct srcu_struct *ssp;
4444
};
4545

46-
#define SRCU_NMI_UNKNOWN 0x0
47-
#define SRCU_NMI_NMI_UNSAFE 0x1
48-
#define SRCU_NMI_NMI_SAFE 0x2
49-
5046
/*
5147
* Node in SRCU combining tree, similar in function to rcu_data.
5248
*/
@@ -159,7 +155,4 @@ void synchronize_srcu_expedited(struct srcu_struct *ssp);
159155
void srcu_barrier(struct srcu_struct *ssp);
160156
void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
161157

162-
int __srcu_read_lock_nmisafe(struct srcu_struct *ssp, bool chknmisafe) __acquires(ssp);
163-
void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx, bool chknmisafe) __releases(ssp);
164-
165158
#endif

kernel/rcu/srcutree.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -631,17 +631,16 @@ void cleanup_srcu_struct(struct srcu_struct *ssp)
631631
}
632632
EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
633633

634+
#ifdef CONFIG_PROVE_RCU
634635
/*
635636
* Check for consistent NMI safety.
636637
*/
637-
static void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe)
638+
void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe)
638639
{
639640
int nmi_safe_mask = 1 << nmi_safe;
640641
int old_nmi_safe_mask;
641642
struct srcu_data *sdp;
642643

643-
if (!IS_ENABLED(CONFIG_PROVE_RCU))
644-
return;
645644
/* NMI-unsafe use in NMI is a bad sign */
646645
WARN_ON_ONCE(!nmi_safe && in_nmi());
647646
sdp = raw_cpu_ptr(ssp->sda);
@@ -652,6 +651,8 @@ static void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe)
652651
}
653652
WARN_ONCE(old_nmi_safe_mask != nmi_safe_mask, "CPU %d old state %d new state %d\n", sdp->cpu, old_nmi_safe_mask, nmi_safe_mask);
654653
}
654+
EXPORT_SYMBOL_GPL(srcu_check_nmi_safety);
655+
#endif /* CONFIG_PROVE_RCU */
655656

656657
/*
657658
* Counts the new reader in the appropriate per-CPU element of the
@@ -665,7 +666,6 @@ int __srcu_read_lock(struct srcu_struct *ssp)
665666
idx = READ_ONCE(ssp->srcu_idx) & 0x1;
666667
this_cpu_inc(ssp->sda->srcu_lock_count[idx].counter);
667668
smp_mb(); /* B */ /* Avoid leaking the critical section. */
668-
srcu_check_nmi_safety(ssp, false);
669669
return idx;
670670
}
671671
EXPORT_SYMBOL_GPL(__srcu_read_lock);
@@ -679,7 +679,6 @@ void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
679679
{
680680
smp_mb(); /* C */ /* Avoid leaking the critical section. */
681681
this_cpu_inc(ssp->sda->srcu_unlock_count[idx].counter);
682-
srcu_check_nmi_safety(ssp, false);
683682
}
684683
EXPORT_SYMBOL_GPL(__srcu_read_unlock);
685684

@@ -690,16 +689,14 @@ EXPORT_SYMBOL_GPL(__srcu_read_unlock);
690689
* srcu_struct, but in an NMI-safe manner using RMW atomics.
691690
* Returns an index that must be passed to the matching srcu_read_unlock().
692691
*/
693-
int __srcu_read_lock_nmisafe(struct srcu_struct *ssp, bool chknmisafe)
692+
int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
694693
{
695694
int idx;
696695
struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
697696

698697
idx = READ_ONCE(ssp->srcu_idx) & 0x1;
699698
atomic_long_inc(&sdp->srcu_lock_count[idx]);
700699
smp_mb__after_atomic(); /* B */ /* Avoid leaking the critical section. */
701-
if (chknmisafe)
702-
srcu_check_nmi_safety(ssp, true);
703700
return idx;
704701
}
705702
EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe);
@@ -709,14 +706,12 @@ EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe);
709706
* element of the srcu_struct. Note that this may well be a different
710707
* CPU than that which was incremented by the corresponding srcu_read_lock().
711708
*/
712-
void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx, bool chknmisafe)
709+
void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
713710
{
714711
struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
715712

716713
smp_mb__before_atomic(); /* C */ /* Avoid leaking the critical section. */
717714
atomic_long_inc(&sdp->srcu_unlock_count[idx]);
718-
if (chknmisafe)
719-
srcu_check_nmi_safety(ssp, true);
720715
}
721716
EXPORT_SYMBOL_GPL(__srcu_read_unlock_nmisafe);
722717

@@ -1163,7 +1158,7 @@ static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
11631158
* SRCU read-side critical section so that the grace-period
11641159
* sequence number cannot wrap around in the meantime.
11651160
*/
1166-
idx = __srcu_read_lock_nmisafe(ssp, false);
1161+
idx = __srcu_read_lock_nmisafe(ssp);
11671162
ss_state = smp_load_acquire(&ssp->srcu_size_state);
11681163
if (ss_state < SRCU_SIZE_WAIT_CALL)
11691164
sdp = per_cpu_ptr(ssp->sda, 0);
@@ -1196,7 +1191,7 @@ static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
11961191
srcu_funnel_gp_start(ssp, sdp, s, do_norm);
11971192
else if (needexp)
11981193
srcu_funnel_exp_start(ssp, sdp_mynode, s);
1199-
__srcu_read_unlock_nmisafe(ssp, idx, false);
1194+
__srcu_read_unlock_nmisafe(ssp, idx);
12001195
return s;
12011196
}
12021197

@@ -1500,13 +1495,13 @@ void srcu_barrier(struct srcu_struct *ssp)
15001495
/* Initial count prevents reaching zero until all CBs are posted. */
15011496
atomic_set(&ssp->srcu_barrier_cpu_cnt, 1);
15021497

1503-
idx = __srcu_read_lock_nmisafe(ssp, false);
1498+
idx = __srcu_read_lock_nmisafe(ssp);
15041499
if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
15051500
srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, 0));
15061501
else
15071502
for_each_possible_cpu(cpu)
15081503
srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, cpu));
1509-
__srcu_read_unlock_nmisafe(ssp, idx, false);
1504+
__srcu_read_unlock_nmisafe(ssp, idx);
15101505

15111506
/* Remove the initial count, at which point reaching zero can happen. */
15121507
if (atomic_dec_and_test(&ssp->srcu_barrier_cpu_cnt))

0 commit comments

Comments
 (0)