Skip to content

Commit f831948

Browse files
Peter Zijlstradchinner
authored andcommitted
locking/lockdep: Provide a type check for lock_is_held
Christoph requested lockdep_assert_held() variants that distinguish between held-for-read or held-for-write. Provide: int lock_is_held_type(struct lockdep_map *lock, int read) which takes the same argument as lock_acquire(.read) and matches it to the held_lock instance. Use of this function should be gated by the debug_locks variable. When that is 0 the return value of the lock_is_held_type() function is undefined. This is done to allow both negative and positive tests for holding locks. By default we provide (positive) lockdep_assert_held{,_exclusive,_read}() macros. Requested-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Tested-by: Jens Axboe <axboe@fb.com> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Dave Chinner <david@fromorbit.com>
1 parent 3816199 commit f831948

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

include/linux/lockdep.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,18 @@ extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
338338
extern void lock_release(struct lockdep_map *lock, int nested,
339339
unsigned long ip);
340340

341-
#define lockdep_is_held(lock) lock_is_held(&(lock)->dep_map)
341+
/*
342+
* Same "read" as for lock_acquire(), except -1 means any.
343+
*/
344+
extern int lock_is_held_type(struct lockdep_map *lock, int read);
345+
346+
static inline int lock_is_held(struct lockdep_map *lock)
347+
{
348+
return lock_is_held_type(lock, -1);
349+
}
342350

343-
extern int lock_is_held(struct lockdep_map *lock);
351+
#define lockdep_is_held(lock) lock_is_held(&(lock)->dep_map)
352+
#define lockdep_is_held_type(lock, r) lock_is_held_type(&(lock)->dep_map, (r))
344353

345354
extern void lock_set_class(struct lockdep_map *lock, const char *name,
346355
struct lock_class_key *key, unsigned int subclass,
@@ -372,6 +381,14 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
372381
WARN_ON(debug_locks && !lockdep_is_held(l)); \
373382
} while (0)
374383

384+
#define lockdep_assert_held_exclusive(l) do { \
385+
WARN_ON(debug_locks && !lockdep_is_held_type(l, 0)); \
386+
} while (0)
387+
388+
#define lockdep_assert_held_read(l) do { \
389+
WARN_ON(debug_locks && !lockdep_is_held_type(l, 1)); \
390+
} while (0)
391+
375392
#define lockdep_assert_held_once(l) do { \
376393
WARN_ON_ONCE(debug_locks && !lockdep_is_held(l)); \
377394
} while (0)
@@ -428,7 +445,11 @@ struct lock_class_key { };
428445

429446
#define lockdep_depth(tsk) (0)
430447

448+
#define lockdep_is_held_type(l, r) (1)
449+
431450
#define lockdep_assert_held(l) do { (void)(l); } while (0)
451+
#define lockdep_assert_held_exclusive(l) do { (void)(l); } while (0)
452+
#define lockdep_assert_held_read(l) do { (void)(l); } while (0)
432453
#define lockdep_assert_held_once(l) do { (void)(l); } while (0)
433454

434455
#define lockdep_recursing(tsk) (0)

kernel/locking/lockdep.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,7 +3188,7 @@ print_lock_nested_lock_not_held(struct task_struct *curr,
31883188
return 0;
31893189
}
31903190

3191-
static int __lock_is_held(struct lockdep_map *lock);
3191+
static int __lock_is_held(struct lockdep_map *lock, int read);
31923192

31933193
/*
31943194
* This gets called for every mutex_lock*()/spin_lock*() operation.
@@ -3329,7 +3329,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
33293329
}
33303330
chain_key = iterate_chain_key(chain_key, class_idx);
33313331

3332-
if (nest_lock && !__lock_is_held(nest_lock))
3332+
if (nest_lock && !__lock_is_held(nest_lock, -1))
33333333
return print_lock_nested_lock_not_held(curr, hlock, ip);
33343334

33353335
if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
@@ -3576,16 +3576,20 @@ __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
35763576
return 1;
35773577
}
35783578

3579-
static int __lock_is_held(struct lockdep_map *lock)
3579+
static int __lock_is_held(struct lockdep_map *lock, int read)
35803580
{
35813581
struct task_struct *curr = current;
35823582
int i;
35833583

35843584
for (i = 0; i < curr->lockdep_depth; i++) {
35853585
struct held_lock *hlock = curr->held_locks + i;
35863586

3587-
if (match_held_lock(hlock, lock))
3588-
return 1;
3587+
if (match_held_lock(hlock, lock)) {
3588+
if (read == -1 || hlock->read == read)
3589+
return 1;
3590+
3591+
return 0;
3592+
}
35893593
}
35903594

35913595
return 0;
@@ -3769,7 +3773,7 @@ void lock_release(struct lockdep_map *lock, int nested,
37693773
}
37703774
EXPORT_SYMBOL_GPL(lock_release);
37713775

3772-
int lock_is_held(struct lockdep_map *lock)
3776+
int lock_is_held_type(struct lockdep_map *lock, int read)
37733777
{
37743778
unsigned long flags;
37753779
int ret = 0;
@@ -3781,13 +3785,13 @@ int lock_is_held(struct lockdep_map *lock)
37813785
check_flags(flags);
37823786

37833787
current->lockdep_recursion = 1;
3784-
ret = __lock_is_held(lock);
3788+
ret = __lock_is_held(lock, read);
37853789
current->lockdep_recursion = 0;
37863790
raw_local_irq_restore(flags);
37873791

37883792
return ret;
37893793
}
3790-
EXPORT_SYMBOL_GPL(lock_is_held);
3794+
EXPORT_SYMBOL_GPL(lock_is_held_type);
37913795

37923796
struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
37933797
{

0 commit comments

Comments
 (0)