Skip to content

Commit e4ab322

Browse files
author
Peter Zijlstra
committed
cleanup: Add conditional guard support
Adds: - DEFINE_GUARD_COND() / DEFINE_LOCK_GUARD_1_COND() to extend existing guards with conditional lock primitives, eg. mutex_trylock(), mutex_lock_interruptible(). nb. both primitives allow NULL 'locks', which cause the lock to fail (obviously). - extends scoped_guard() to not take the body when the the conditional guard 'fails'. eg. scoped_guard (mutex_intr, &task->signal_cred_guard_mutex) { ... } will only execute the body when the mutex is held. - provides scoped_cond_guard(name, fail, args...); which extends scoped_guard() to do fail when the lock-acquire fails. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/20231102110706.460851167%40infradead.org
1 parent b85ea95 commit e4ab322

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

include/linux/cleanup.h

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,55 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
125125
* trivial wrapper around DEFINE_CLASS() above specifically
126126
* for locks.
127127
*
128+
* DEFINE_GUARD_COND(name, ext, condlock)
129+
* wrapper around EXTEND_CLASS above to add conditional lock
130+
* variants to a base class, eg. mutex_trylock() or
131+
* mutex_lock_interruptible().
132+
*
128133
* guard(name):
129-
* an anonymous instance of the (guard) class
134+
* an anonymous instance of the (guard) class, not recommended for
135+
* conditional locks.
130136
*
131137
* scoped_guard (name, args...) { }:
132138
* similar to CLASS(name, scope)(args), except the variable (with the
133139
* explicit name 'scope') is declard in a for-loop such that its scope is
134140
* bound to the next (compound) statement.
135141
*
142+
* for conditional locks the loop body is skipped when the lock is not
143+
* acquired.
144+
*
145+
* scoped_cond_guard (name, fail, args...) { }:
146+
* similar to scoped_guard(), except it does fail when the lock
147+
* acquire fails.
148+
*
136149
*/
137150

138151
#define DEFINE_GUARD(_name, _type, _lock, _unlock) \
139-
DEFINE_CLASS(_name, _type, _unlock, ({ _lock; _T; }), _type _T)
152+
DEFINE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), _type _T); \
153+
static inline void * class_##_name##_lock_ptr(class_##_name##_t *_T) \
154+
{ return *_T; }
155+
156+
#define DEFINE_GUARD_COND(_name, _ext, _condlock) \
157+
EXTEND_CLASS(_name, _ext, \
158+
({ void *_t = _T; if (_T && !(_condlock)) _t = NULL; _t; }), \
159+
class_##_name##_t _T) \
160+
static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \
161+
{ return class_##_name##_lock_ptr(_T); }
140162

141163
#define guard(_name) \
142164
CLASS(_name, __UNIQUE_ID(guard))
143165

166+
#define __guard_ptr(_name) class_##_name##_lock_ptr
167+
144168
#define scoped_guard(_name, args...) \
145169
for (CLASS(_name, scope)(args), \
146-
*done = NULL; !done; done = (void *)1)
170+
*done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)
171+
172+
#define scoped_cond_guard(_name, _fail, args...) \
173+
for (CLASS(_name, scope)(args), \
174+
*done = NULL; !done; done = (void *)1) \
175+
if (!__guard_ptr(_name)(&scope)) _fail; \
176+
else
147177

148178
/*
149179
* Additional helper macros for generating lock guards with types, either for
@@ -152,6 +182,7 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
152182
*
153183
* DEFINE_LOCK_GUARD_0(name, lock, unlock, ...)
154184
* DEFINE_LOCK_GUARD_1(name, type, lock, unlock, ...)
185+
* DEFINE_LOCK_GUARD_1_COND(name, ext, condlock)
155186
*
156187
* will result in the following type:
157188
*
@@ -173,6 +204,11 @@ typedef struct { \
173204
static inline void class_##_name##_destructor(class_##_name##_t *_T) \
174205
{ \
175206
if (_T->lock) { _unlock; } \
207+
} \
208+
\
209+
static inline void *class_##_name##_lock_ptr(class_##_name##_t *_T) \
210+
{ \
211+
return _T->lock; \
176212
}
177213

178214

@@ -201,4 +237,14 @@ __DEFINE_LOCK_GUARD_1(_name, _type, _lock)
201237
__DEFINE_UNLOCK_GUARD(_name, void, _unlock, __VA_ARGS__) \
202238
__DEFINE_LOCK_GUARD_0(_name, _lock)
203239

240+
#define DEFINE_LOCK_GUARD_1_COND(_name, _ext, _condlock) \
241+
EXTEND_CLASS(_name, _ext, \
242+
({ class_##_name##_t _t = { .lock = l }, *_T = &_t;\
243+
if (_T->lock && !(_condlock)) _T->lock = NULL; \
244+
_t; }), \
245+
typeof_member(class_##_name##_t, lock) l) \
246+
static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \
247+
{ return class_##_name##_lock_ptr(_T); }
248+
249+
204250
#endif /* __LINUX_GUARDS_H */

include/linux/mutex.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ extern void mutex_unlock(struct mutex *lock);
221221
extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
222222

223223
DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
224-
DEFINE_FREE(mutex, struct mutex *, if (_T) mutex_unlock(_T))
224+
DEFINE_GUARD_COND(mutex, _try, mutex_trylock(_T))
225+
DEFINE_GUARD_COND(mutex, _intr, mutex_lock_interruptible(_T) == 0)
225226

226227
#endif /* __LINUX_MUTEX_H */

include/linux/rwsem.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ extern void up_read(struct rw_semaphore *sem);
203203
extern void up_write(struct rw_semaphore *sem);
204204

205205
DEFINE_GUARD(rwsem_read, struct rw_semaphore *, down_read(_T), up_read(_T))
206-
DEFINE_GUARD(rwsem_write, struct rw_semaphore *, down_write(_T), up_write(_T))
207-
208-
DEFINE_FREE(up_read, struct rw_semaphore *, if (_T) up_read(_T))
209-
DEFINE_FREE(up_write, struct rw_semaphore *, if (_T) up_write(_T))
206+
DEFINE_GUARD_COND(rwsem_read, _try, down_read_trylock(_T))
207+
DEFINE_GUARD_COND(rwsem_read, _intr, down_read_interruptible(_T) == 0)
210208

209+
DEFINE_GUARD(rwsem_write, struct rw_semaphore *, down_write(_T), up_write(_T))
210+
DEFINE_GUARD_COND(rwsem_write, _try, down_write_trylock(_T))
211211

212212
/*
213213
* downgrade write lock to read lock

include/linux/spinlock.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,8 @@ DEFINE_LOCK_GUARD_1(raw_spinlock, raw_spinlock_t,
507507
raw_spin_lock(_T->lock),
508508
raw_spin_unlock(_T->lock))
509509

510+
DEFINE_LOCK_GUARD_1_COND(raw_spinlock, _try, raw_spin_trylock(_T->lock))
511+
510512
DEFINE_LOCK_GUARD_1(raw_spinlock_nested, raw_spinlock_t,
511513
raw_spin_lock_nested(_T->lock, SINGLE_DEPTH_NESTING),
512514
raw_spin_unlock(_T->lock))
@@ -515,23 +517,36 @@ DEFINE_LOCK_GUARD_1(raw_spinlock_irq, raw_spinlock_t,
515517
raw_spin_lock_irq(_T->lock),
516518
raw_spin_unlock_irq(_T->lock))
517519

520+
DEFINE_LOCK_GUARD_1_COND(raw_spinlock_irq, _try, raw_spin_trylock_irq(_T->lock))
521+
518522
DEFINE_LOCK_GUARD_1(raw_spinlock_irqsave, raw_spinlock_t,
519523
raw_spin_lock_irqsave(_T->lock, _T->flags),
520524
raw_spin_unlock_irqrestore(_T->lock, _T->flags),
521525
unsigned long flags)
522526

527+
DEFINE_LOCK_GUARD_1_COND(raw_spinlock_irqsave, _try,
528+
raw_spin_trylock_irqsave(_T->lock, _T->flags))
529+
523530
DEFINE_LOCK_GUARD_1(spinlock, spinlock_t,
524531
spin_lock(_T->lock),
525532
spin_unlock(_T->lock))
526533

534+
DEFINE_LOCK_GUARD_1_COND(spinlock, _try, spin_trylock(_T->lock))
535+
527536
DEFINE_LOCK_GUARD_1(spinlock_irq, spinlock_t,
528537
spin_lock_irq(_T->lock),
529538
spin_unlock_irq(_T->lock))
530539

540+
DEFINE_LOCK_GUARD_1_COND(spinlock_irq, _try,
541+
spin_trylock_irq(_T->lock))
542+
531543
DEFINE_LOCK_GUARD_1(spinlock_irqsave, spinlock_t,
532544
spin_lock_irqsave(_T->lock, _T->flags),
533545
spin_unlock_irqrestore(_T->lock, _T->flags),
534546
unsigned long flags)
535547

548+
DEFINE_LOCK_GUARD_1_COND(spinlock_irqsave, _try,
549+
spin_trylock_irqsave(_T->lock, _T->flags))
550+
536551
#undef __LINUX_INSIDE_SPINLOCK_H
537552
#endif /* __LINUX_SPINLOCK_H */

0 commit comments

Comments
 (0)