Skip to content

Commit 54a118f

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. Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 5dc7886 commit 54a118f

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-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: 9 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,7 @@ 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+
#endif /* CONFIG_PROVE_RCU */
655655

656656
/*
657657
* Counts the new reader in the appropriate per-CPU element of the
@@ -665,7 +665,6 @@ int __srcu_read_lock(struct srcu_struct *ssp)
665665
idx = READ_ONCE(ssp->srcu_idx) & 0x1;
666666
this_cpu_inc(ssp->sda->srcu_lock_count[idx].counter);
667667
smp_mb(); /* B */ /* Avoid leaking the critical section. */
668-
srcu_check_nmi_safety(ssp, false);
669668
return idx;
670669
}
671670
EXPORT_SYMBOL_GPL(__srcu_read_lock);
@@ -679,7 +678,6 @@ void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
679678
{
680679
smp_mb(); /* C */ /* Avoid leaking the critical section. */
681680
this_cpu_inc(ssp->sda->srcu_unlock_count[idx].counter);
682-
srcu_check_nmi_safety(ssp, false);
683681
}
684682
EXPORT_SYMBOL_GPL(__srcu_read_unlock);
685683

@@ -688,16 +686,14 @@ EXPORT_SYMBOL_GPL(__srcu_read_unlock);
688686
* srcu_struct, but in an NMI-safe manner using RMW atomics.
689687
* Returns an index that must be passed to the matching srcu_read_unlock().
690688
*/
691-
int __srcu_read_lock_nmisafe(struct srcu_struct *ssp, bool chknmisafe)
689+
int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
692690
{
693691
int idx;
694692
struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
695693

696694
idx = READ_ONCE(ssp->srcu_idx) & 0x1;
697695
atomic_long_inc(&sdp->srcu_lock_count[idx]);
698696
smp_mb__after_atomic(); /* B */ /* Avoid leaking the critical section. */
699-
if (chknmisafe)
700-
srcu_check_nmi_safety(ssp, true);
701697
return idx;
702698
}
703699
EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe);
@@ -707,14 +703,12 @@ EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe);
707703
* element of the srcu_struct. Note that this may well be a different
708704
* CPU than that which was incremented by the corresponding srcu_read_lock().
709705
*/
710-
void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx, bool chknmisafe)
706+
void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
711707
{
712708
struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
713709

714710
smp_mb__before_atomic(); /* C */ /* Avoid leaking the critical section. */
715711
atomic_long_inc(&sdp->srcu_unlock_count[idx]);
716-
if (chknmisafe)
717-
srcu_check_nmi_safety(ssp, true);
718712
}
719713
EXPORT_SYMBOL_GPL(__srcu_read_unlock_nmisafe);
720714

@@ -1159,7 +1153,7 @@ static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
11591153
* SRCU read-side critical section so that the grace-period
11601154
* sequence number cannot wrap around in the meantime.
11611155
*/
1162-
idx = __srcu_read_lock_nmisafe(ssp, false);
1156+
idx = __srcu_read_lock_nmisafe(ssp);
11631157
ss_state = smp_load_acquire(&ssp->srcu_size_state);
11641158
if (ss_state < SRCU_SIZE_WAIT_CALL)
11651159
sdp = per_cpu_ptr(ssp->sda, 0);
@@ -1192,7 +1186,7 @@ static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
11921186
srcu_funnel_gp_start(ssp, sdp, s, do_norm);
11931187
else if (needexp)
11941188
srcu_funnel_exp_start(ssp, sdp_mynode, s);
1195-
__srcu_read_unlock_nmisafe(ssp, idx, false);
1189+
__srcu_read_unlock_nmisafe(ssp, idx);
11961190
return s;
11971191
}
11981192

@@ -1496,13 +1490,13 @@ void srcu_barrier(struct srcu_struct *ssp)
14961490
/* Initial count prevents reaching zero until all CBs are posted. */
14971491
atomic_set(&ssp->srcu_barrier_cpu_cnt, 1);
14981492

1499-
idx = __srcu_read_lock_nmisafe(ssp, false);
1493+
idx = __srcu_read_lock_nmisafe(ssp);
15001494
if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
15011495
srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, 0));
15021496
else
15031497
for_each_possible_cpu(cpu)
15041498
srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, cpu));
1505-
__srcu_read_unlock_nmisafe(ssp, idx, false);
1499+
__srcu_read_unlock_nmisafe(ssp, idx);
15061500

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

0 commit comments

Comments
 (0)