Skip to content

Commit a0ad40f

Browse files
Peter Zijlstranunojsa
authored andcommitted
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) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20231102110706.460851167%40infradead.org
1 parent 3efa3f2 commit a0ad40f

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
@@ -92,25 +92,55 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
9292
* trivial wrapper around DEFINE_CLASS() above specifically
9393
* for locks.
9494
*
95+
* DEFINE_GUARD_COND(name, ext, condlock)
96+
* wrapper around EXTEND_CLASS above to add conditional lock
97+
* variants to a base class, eg. mutex_trylock() or
98+
* mutex_lock_interruptible().
99+
*
95100
* guard(name):
96-
* an anonymous instance of the (guard) class
101+
* an anonymous instance of the (guard) class, not recommended for
102+
* conditional locks.
97103
*
98104
* scoped_guard (name, args...) { }:
99105
* similar to CLASS(name, scope)(args), except the variable (with the
100106
* explicit name 'scope') is declard in a for-loop such that its scope is
101107
* bound to the next (compound) statement.
102108
*
109+
* for conditional locks the loop body is skipped when the lock is not
110+
* acquired.
111+
*
112+
* scoped_cond_guard (name, fail, args...) { }:
113+
* similar to scoped_guard(), except it does fail when the lock
114+
* acquire fails.
115+
*
103116
*/
104117

105118
#define DEFINE_GUARD(_name, _type, _lock, _unlock) \
106-
DEFINE_CLASS(_name, _type, _unlock, ({ _lock; _T; }), _type _T)
119+
DEFINE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), _type _T); \
120+
static inline void * class_##_name##_lock_ptr(class_##_name##_t *_T) \
121+
{ return *_T; }
122+
123+
#define DEFINE_GUARD_COND(_name, _ext, _condlock) \
124+
EXTEND_CLASS(_name, _ext, \
125+
({ void *_t = _T; if (_T && !(_condlock)) _t = NULL; _t; }), \
126+
class_##_name##_t _T) \
127+
static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \
128+
{ return class_##_name##_lock_ptr(_T); }
107129

108130
#define guard(_name) \
109131
CLASS(_name, __UNIQUE_ID(guard))
110132

133+
#define __guard_ptr(_name) class_##_name##_lock_ptr
134+
111135
#define scoped_guard(_name, args...) \
112136
for (CLASS(_name, scope)(args), \
113-
*done = NULL; !done; done = (void *)1)
137+
*done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)
138+
139+
#define scoped_cond_guard(_name, _fail, args...) \
140+
for (CLASS(_name, scope)(args), \
141+
*done = NULL; !done; done = (void *)1) \
142+
if (!__guard_ptr(_name)(&scope)) _fail; \
143+
else
114144

115145
/*
116146
* Additional helper macros for generating lock guards with types, either for
@@ -119,6 +149,7 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
119149
*
120150
* DEFINE_LOCK_GUARD_0(name, lock, unlock, ...)
121151
* DEFINE_LOCK_GUARD_1(name, type, lock, unlock, ...)
152+
* DEFINE_LOCK_GUARD_1_COND(name, ext, condlock)
122153
*
123154
* will result in the following type:
124155
*
@@ -140,6 +171,11 @@ typedef struct { \
140171
static inline void class_##_name##_destructor(class_##_name##_t *_T) \
141172
{ \
142173
if (_T->lock) { _unlock; } \
174+
} \
175+
\
176+
static inline void *class_##_name##_lock_ptr(class_##_name##_t *_T) \
177+
{ \
178+
return _T->lock; \
143179
}
144180

145181

@@ -168,4 +204,14 @@ __DEFINE_LOCK_GUARD_1(_name, _type, _lock)
168204
__DEFINE_UNLOCK_GUARD(_name, void, _unlock, __VA_ARGS__) \
169205
__DEFINE_LOCK_GUARD_0(_name, _lock)
170206

207+
#define DEFINE_LOCK_GUARD_1_COND(_name, _ext, _condlock) \
208+
EXTEND_CLASS(_name, _ext, \
209+
({ class_##_name##_t _t = { .lock = l }, *_T = &_t;\
210+
if (_T->lock && !(_condlock)) _T->lock = NULL; \
211+
_t; }), \
212+
typeof_member(class_##_name##_t, lock) l) \
213+
static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \
214+
{ return class_##_name##_lock_ptr(_T); }
215+
216+
171217
#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
@@ -498,6 +498,8 @@ DEFINE_LOCK_GUARD_1(raw_spinlock, raw_spinlock_t,
498498
raw_spin_lock(_T->lock),
499499
raw_spin_unlock(_T->lock))
500500

501+
DEFINE_LOCK_GUARD_1_COND(raw_spinlock, _try, raw_spin_trylock(_T->lock))
502+
501503
DEFINE_LOCK_GUARD_1(raw_spinlock_nested, raw_spinlock_t,
502504
raw_spin_lock_nested(_T->lock, SINGLE_DEPTH_NESTING),
503505
raw_spin_unlock(_T->lock))
@@ -506,23 +508,36 @@ DEFINE_LOCK_GUARD_1(raw_spinlock_irq, raw_spinlock_t,
506508
raw_spin_lock_irq(_T->lock),
507509
raw_spin_unlock_irq(_T->lock))
508510

511+
DEFINE_LOCK_GUARD_1_COND(raw_spinlock_irq, _try, raw_spin_trylock_irq(_T->lock))
512+
509513
DEFINE_LOCK_GUARD_1(raw_spinlock_irqsave, raw_spinlock_t,
510514
raw_spin_lock_irqsave(_T->lock, _T->flags),
511515
raw_spin_unlock_irqrestore(_T->lock, _T->flags),
512516
unsigned long flags)
513517

518+
DEFINE_LOCK_GUARD_1_COND(raw_spinlock_irqsave, _try,
519+
raw_spin_trylock_irqsave(_T->lock, _T->flags))
520+
514521
DEFINE_LOCK_GUARD_1(spinlock, spinlock_t,
515522
spin_lock(_T->lock),
516523
spin_unlock(_T->lock))
517524

525+
DEFINE_LOCK_GUARD_1_COND(spinlock, _try, spin_trylock(_T->lock))
526+
518527
DEFINE_LOCK_GUARD_1(spinlock_irq, spinlock_t,
519528
spin_lock_irq(_T->lock),
520529
spin_unlock_irq(_T->lock))
521530

531+
DEFINE_LOCK_GUARD_1_COND(spinlock_irq, _try,
532+
spin_trylock_irq(_T->lock))
533+
522534
DEFINE_LOCK_GUARD_1(spinlock_irqsave, spinlock_t,
523535
spin_lock_irqsave(_T->lock, _T->flags),
524536
spin_unlock_irqrestore(_T->lock, _T->flags),
525537
unsigned long flags)
526538

539+
DEFINE_LOCK_GUARD_1_COND(spinlock_irqsave, _try,
540+
spin_trylock_irqsave(_T->lock, _T->flags))
541+
527542
#undef __LINUX_INSIDE_SPINLOCK_H
528543
#endif /* __LINUX_SPINLOCK_H */

0 commit comments

Comments
 (0)