hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2016 Philippe Gerum  <rpm@xenomai.org>.
 */
#ifndef __LINUX_SPINLOCK_PIPELINE_H
#define __LINUX_SPINLOCK_PIPELINE_H
 
#ifndef __LINUX_SPINLOCK_H
# error "Please don't include this file directly. Use spinlock.h."
#endif
 
#include <dovetail/spinlock.h>
 
#define hard_spin_lock_irqsave(__rlock, __flags)        \
   do {                            \
       (__flags) = __hard_spin_lock_irqsave(__rlock);    \
   } while (0)
 
#define hard_spin_trylock_irqsave(__rlock, __flags)            \
   ({                                \
       int __locked;                        \
       (__flags) = __hard_spin_trylock_irqsave(__rlock, &__locked); \
       __locked;                        \
   })
 
#define hybrid_spin_lock_init(__rlock)    hard_spin_lock_init(__rlock)
 
/*
 * CAUTION: We don't want the hand-coded irq-enable of
 * do_raw_spin_lock_flags(), hard locked sections assume that
 * interrupts are not re-enabled during lock-acquire.
 */
#define hard_lock_acquire(__rlock, __try, __ip)                \
   do {                                \
       hard_spin_lock_prepare(__rlock);            \
       if (irq_pipeline_debug_locking()) {            \
           spin_acquire(&(__rlock)->dep_map, 0, __try, __ip); \
           LOCK_CONTENDED(__rlock, do_raw_spin_trylock, do_raw_spin_lock); \
       } else {                        \
           do_raw_spin_lock(__rlock);            \
       }                            \
   } while (0)
 
#define hard_lock_acquire_nested(__rlock, __subclass, __ip)        \
   do {                                \
       hard_spin_lock_prepare(__rlock);            \
       if (irq_pipeline_debug_locking()) {            \
           spin_acquire(&(__rlock)->dep_map, __subclass, 0, __ip); \
           LOCK_CONTENDED(__rlock, do_raw_spin_trylock, do_raw_spin_lock); \
       } else {                        \
           do_raw_spin_lock(__rlock);            \
       }                            \
   } while (0)
 
#define hard_trylock_acquire(__rlock, __try, __ip)            \
   do {                                \
       if (irq_pipeline_debug_locking())            \
           spin_acquire(&(__rlock)->dep_map, 0, __try, __ip); \
   } while (0)
 
#define hard_lock_release(__rlock, __ip)                \
   do {                                \
       if (irq_pipeline_debug_locking())            \
           spin_release(&(__rlock)->dep_map, __ip);    \
       do_raw_spin_unlock(__rlock);                \
       hard_spin_unlock_finish(__rlock);            \
   } while (0)
 
#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
 
#ifdef CONFIG_DEBUG_SPINLOCK
/*
 * Hard spinlocks are not checked for invalid wait context in-band
 * wise (LD_WAIT_INV). We could be smarter with handling a specific
 * wait type for them, so that we could detect hard_spin_lock ->
 * {raw_}spin_lock for instance, but we already have
 * check_inband_stage() calls all over the place in the latter API, so
 * that kind of misusage would be detected regardless.
 */
#define hard_spin_lock_init(__lock)                \
   do {                            \
       static struct lock_class_key __key;        \
       __raw_spin_lock_init((raw_spinlock_t *)__lock, #__lock, &__key, LD_WAIT_INV); \
   } while (0)
#else
#define hard_spin_lock_init(__rlock)                \
   do { *(__rlock) = __RAW_SPIN_LOCK_UNLOCKED(__rlock); } while (0)
#endif
 
/*
 * XXX: no preempt_enable/disable when hard locking.
 */
 
static inline
void hard_spin_lock(struct raw_spinlock *rlock)
{
   hard_lock_acquire(rlock, 0, _THIS_IP_);
}
 
#ifdef CONFIG_DEBUG_LOCK_ALLOC
static inline
void hard_spin_lock_nested(struct raw_spinlock *rlock, int subclass)
{
   hard_lock_acquire_nested(rlock, subclass, _THIS_IP_);
}
#else
static inline
void hard_spin_lock_nested(struct raw_spinlock *rlock, int subclass)
{
   hard_spin_lock(rlock);
}
#endif
 
static inline
void hard_spin_unlock(struct raw_spinlock *rlock)
{
   hard_lock_release(rlock, _THIS_IP_);
}
 
static inline
void hard_spin_lock_irq(struct raw_spinlock *rlock)
{
   hard_local_irq_disable();
   hard_lock_acquire(rlock, 0, _THIS_IP_);
}
 
static inline
void hard_spin_unlock_irq(struct raw_spinlock *rlock)
{
   hard_lock_release(rlock, _THIS_IP_);
   hard_local_irq_enable();
}
 
static inline
void hard_spin_unlock_irqrestore(struct raw_spinlock *rlock,
                unsigned long flags)
{
   hard_lock_release(rlock, _THIS_IP_);
   hard_local_irq_restore(flags);
}
 
static inline
unsigned long __hard_spin_lock_irqsave(struct raw_spinlock *rlock)
{
   unsigned long flags = hard_local_irq_save();
 
   hard_lock_acquire(rlock, 0, _THIS_IP_);
 
   return flags;
}
 
static inline
int hard_spin_trylock(struct raw_spinlock *rlock)
{
   hard_spin_trylock_prepare(rlock);
 
   if (do_raw_spin_trylock(rlock)) {
       hard_trylock_acquire(rlock, 1, _THIS_IP_);
       return 1;
   }
 
   hard_spin_trylock_fail(rlock);
 
   return 0;
}
 
static inline
unsigned long __hard_spin_trylock_irqsave(struct raw_spinlock *rlock,
                     int *locked)
{
   unsigned long flags = hard_local_irq_save();
   *locked = hard_spin_trylock(rlock);
   return *locked ? flags : ({ hard_local_irq_restore(flags); flags; });
}
 
static inline
int hard_spin_trylock_irq(struct raw_spinlock *rlock)
{
   hard_local_irq_disable();
   return hard_spin_trylock(rlock) ? : ({ hard_local_irq_enable(); 0; });
}
 
static inline
int hard_spin_is_locked(struct raw_spinlock *rlock)
{
   return arch_spin_is_locked(&rlock->raw_lock);
}
 
static inline
int hard_spin_is_contended(struct raw_spinlock *rlock)
{
#ifdef CONFIG_GENERIC_LOCKBREAK
   return rlock->break_lock;
#elif defined(arch_spin_is_contended)
   return arch_spin_is_contended(&rlock->raw_lock);
#else
   return 0;
#endif
}
 
#else  /* !SMP && !DEBUG_SPINLOCK */
 
#define hard_spin_lock_init(__rlock)    do { (void)(__rlock); } while (0)
#define hard_spin_lock(__rlock)        __HARD_LOCK(__rlock)
#define hard_spin_lock_nested(__rlock, __subclass)  \
   do { __HARD_LOCK(__rlock); (void)(__subclass); } while (0)
#define hard_spin_unlock(__rlock)    __HARD_UNLOCK(__rlock)
#define hard_spin_lock_irq(__rlock)    __HARD_LOCK_IRQ(__rlock)
#define hard_spin_unlock_irq(__rlock)    __HARD_UNLOCK_IRQ(__rlock)
#define hard_spin_unlock_irqrestore(__rlock, __flags)    \
   __HARD_UNLOCK_IRQRESTORE(__rlock, __flags)
#define __hard_spin_lock_irqsave(__rlock)        \
   ({                        \
       unsigned long __flags;            \
       __HARD_LOCK_IRQSAVE(__rlock, __flags);    \
       __flags;                \
   })
#define __hard_spin_trylock_irqsave(__rlock, __locked)    \
   ({                        \
       unsigned long __flags;            \
       __HARD_LOCK_IRQSAVE(__rlock, __flags);    \
       *(__locked) = 1;            \
       __flags;                \
   })
#define hard_spin_trylock(__rlock)    ({ __HARD_LOCK(__rlock); 1; })
#define hard_spin_trylock_irq(__rlock)    ({ __HARD_LOCK_IRQ(__rlock); 1; })
#define hard_spin_is_locked(__rlock)    ((void)(__rlock), 0)
#define hard_spin_is_contended(__rlock)    ((void)(__rlock), 0)
#endif    /* !SMP && !DEBUG_SPINLOCK */
 
/*
 * In the pipeline entry context, the regular preemption and root
 * stall logic do not apply since we may actually have preempted any
 * critical section of the kernel which is protected by regular
 * locking (spin or stall), or we may even have preempted the
 * out-of-band stage. Therefore, we just need to grab the raw spinlock
 * underlying a hybrid spinlock to exclude other CPUs.
 *
 * NOTE: When entering the pipeline, IRQs are already hard disabled.
 */
 
void __hybrid_spin_lock(struct raw_spinlock *rlock);
void __hybrid_spin_lock_nested(struct raw_spinlock *rlock, int subclass);
 
static inline void hybrid_spin_lock(struct raw_spinlock *rlock)
{
   if (in_pipeline())
       hard_lock_acquire(rlock, 0, _THIS_IP_);
   else
       __hybrid_spin_lock(rlock);
}
 
#ifdef CONFIG_DEBUG_LOCK_ALLOC
static inline
void hybrid_spin_lock_nested(struct raw_spinlock *rlock, int subclass)
{
   if (in_pipeline())
       hard_lock_acquire_nested(rlock, subclass, _THIS_IP_);
   else
       __hybrid_spin_lock_nested(rlock, subclass);
}
#else
static inline
void hybrid_spin_lock_nested(struct raw_spinlock *rlock, int subclass)
{
   hybrid_spin_lock(rlock);
}
#endif
 
void __hybrid_spin_unlock(struct raw_spinlock *rlock);
 
static inline void hybrid_spin_unlock(struct raw_spinlock *rlock)
{
   if (in_pipeline())
       hard_lock_release(rlock, _THIS_IP_);
   else
       __hybrid_spin_unlock(rlock);
}
 
void __hybrid_spin_lock_irq(struct raw_spinlock *rlock);
 
static inline void hybrid_spin_lock_irq(struct raw_spinlock *rlock)
{
   if (in_pipeline())
       hard_lock_acquire(rlock, 0, _THIS_IP_);
   else
       __hybrid_spin_lock_irq(rlock);
}
 
void __hybrid_spin_unlock_irq(struct raw_spinlock *rlock);
 
static inline void hybrid_spin_unlock_irq(struct raw_spinlock *rlock)
{
   if (in_pipeline())
       hard_lock_release(rlock, _THIS_IP_);
   else
       __hybrid_spin_unlock_irq(rlock);
}
 
unsigned long __hybrid_spin_lock_irqsave(struct raw_spinlock *rlock);
 
#define hybrid_spin_lock_irqsave(__rlock, __flags)            \
   do {                                \
       if (in_pipeline()) {                    \
           hard_lock_acquire(__rlock, 0, _THIS_IP_);    \
           (__flags) = hard_local_save_flags();        \
       } else                            \
           (__flags) = __hybrid_spin_lock_irqsave(__rlock); \
   } while (0)
 
void __hybrid_spin_unlock_irqrestore(struct raw_spinlock *rlock,
                     unsigned long flags);
 
static inline void hybrid_spin_unlock_irqrestore(struct raw_spinlock *rlock,
                         unsigned long flags)
{
 
   if (in_pipeline())
       hard_lock_release(rlock, _THIS_IP_);
   else
       __hybrid_spin_unlock_irqrestore(rlock, flags);
}
 
int __hybrid_spin_trylock(struct raw_spinlock *rlock);
 
static inline int hybrid_spin_trylock(struct raw_spinlock *rlock)
{
   if (in_pipeline()) {
       hard_spin_trylock_prepare(rlock);
       if (do_raw_spin_trylock(rlock)) {
           hard_trylock_acquire(rlock, 1, _THIS_IP_);
           return 1;
       }
       hard_spin_trylock_fail(rlock);
       return 0;
   }
 
   return __hybrid_spin_trylock(rlock);
}
 
int __hybrid_spin_trylock_irqsave(struct raw_spinlock *rlock,
                  unsigned long *flags);
 
#define hybrid_spin_trylock_irqsave(__rlock, __flags)            \
   ({                                \
       int __ret = 1;                        \
       if (in_pipeline()) {                    \
           hard_spin_trylock_prepare(__rlock);        \
           if (do_raw_spin_trylock(__rlock)) {        \
               hard_trylock_acquire(__rlock, 1, _THIS_IP_); \
               (__flags) = hard_local_save_flags();    \
           } else {                    \
               hard_spin_trylock_fail(__rlock);    \
               __ret = 0;                \
           }                        \
       } else {                        \
           __ret = __hybrid_spin_trylock_irqsave(__rlock, &(__flags)); \
       }                            \
       __ret;                            \
   })
 
static inline int hybrid_spin_trylock_irq(struct raw_spinlock *rlock)
{
   unsigned long flags;
   return hybrid_spin_trylock_irqsave(rlock, flags);
}
 
static inline
int hybrid_spin_is_locked(struct raw_spinlock *rlock)
{
   return hard_spin_is_locked(rlock);
}
 
static inline
int hybrid_spin_is_contended(struct raw_spinlock *rlock)
{
   return hard_spin_is_contended(rlock);
}
 
#ifdef CONFIG_DEBUG_IRQ_PIPELINE
void check_spinlock_context(void);
#else
static inline void check_spinlock_context(void) { }
#endif
 
#endif /* __LINUX_SPINLOCK_PIPELINE_H */