Skip to content

Commit 4dfbb9d

Browse files
Peter ZijlstraDmitry Torokhov
authored andcommitted
Lockdep: add lockdep_set_class_and_subclass() and lockdep_set_subclass()
This annotation makes it possible to assign a subclass on lock init. This annotation is meant to reduce the _nested() annotations by assigning a default subclass. One could do without this annotation and rely on lockdep_set_class() exclusively, but that would require a manual stack of struct lock_class_key objects. Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 86255d9 commit 4dfbb9d

7 files changed

Lines changed: 23 additions & 14 deletions

File tree

‎include/linux/lockdep.h‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ extern int lockdep_internal(void);
202202
*/
203203

204204
extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
205-
struct lock_class_key *key);
205+
struct lock_class_key *key, int subclass);
206206

207207
/*
208208
* Reinitialize a lock key - for cases where there is special locking or
@@ -211,9 +211,14 @@ extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
211211
* or they are too narrow (they suffer from a false class-split):
212212
*/
213213
#define lockdep_set_class(lock, key) \
214-
lockdep_init_map(&(lock)->dep_map, #key, key)
214+
lockdep_init_map(&(lock)->dep_map, #key, key, 0)
215215
#define lockdep_set_class_and_name(lock, key, name) \
216-
lockdep_init_map(&(lock)->dep_map, name, key)
216+
lockdep_init_map(&(lock)->dep_map, name, key, 0)
217+
#define lockdep_set_class_and_subclass(lock, key, sub) \
218+
lockdep_init_map(&(lock)->dep_map, #key, key, sub)
219+
#define lockdep_set_subclass(lock, sub) \
220+
lockdep_init_map(&(lock)->dep_map, #lock, \
221+
(lock)->dep_map.key, sub)
217222

218223
/*
219224
* Acquire a lock.
@@ -257,10 +262,12 @@ static inline int lockdep_internal(void)
257262
# define lock_release(l, n, i) do { } while (0)
258263
# define lockdep_init() do { } while (0)
259264
# define lockdep_info() do { } while (0)
260-
# define lockdep_init_map(lock, name, key) do { (void)(key); } while (0)
265+
# define lockdep_init_map(lock, name, key, sub) do { (void)(key); } while (0)
261266
# define lockdep_set_class(lock, key) do { (void)(key); } while (0)
262267
# define lockdep_set_class_and_name(lock, key, name) \
263268
do { (void)(key); } while (0)
269+
#define lockdep_set_class_and_subclass(lock, key, sub) \
270+
do { (void)(key); } while (0)
264271
# define INIT_LOCKDEP
265272
# define lockdep_reset() do { debug_locks = 1; } while (0)
266273
# define lockdep_free_key_range(start, size) do { } while (0)

‎kernel/lockdep.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
11771177
* itself, so actual lookup of the hash should be once per lock object.
11781178
*/
11791179
static inline struct lock_class *
1180-
register_lock_class(struct lockdep_map *lock, unsigned int subclass)
1180+
register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
11811181
{
11821182
struct lockdep_subclass_key *key;
11831183
struct list_head *hash_head;
@@ -1249,7 +1249,7 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass)
12491249
out_unlock_set:
12501250
__raw_spin_unlock(&hash_lock);
12511251

1252-
if (!subclass)
1252+
if (!subclass || force)
12531253
lock->class_cache = class;
12541254

12551255
DEBUG_LOCKS_WARN_ON(class->subclass != subclass);
@@ -1937,7 +1937,7 @@ void trace_softirqs_off(unsigned long ip)
19371937
* Initialize a lock instance's lock-class mapping info:
19381938
*/
19391939
void lockdep_init_map(struct lockdep_map *lock, const char *name,
1940-
struct lock_class_key *key)
1940+
struct lock_class_key *key, int subclass)
19411941
{
19421942
if (unlikely(!debug_locks))
19431943
return;
@@ -1957,6 +1957,8 @@ void lockdep_init_map(struct lockdep_map *lock, const char *name,
19571957
lock->name = name;
19581958
lock->key = key;
19591959
lock->class_cache = NULL;
1960+
if (subclass)
1961+
register_lock_class(lock, subclass, 1);
19601962
}
19611963

19621964
EXPORT_SYMBOL_GPL(lockdep_init_map);
@@ -1995,7 +1997,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
19951997
* Not cached yet or subclass?
19961998
*/
19971999
if (unlikely(!class)) {
1998-
class = register_lock_class(lock, subclass);
2000+
class = register_lock_class(lock, subclass, 0);
19992001
if (!class)
20002002
return 0;
20012003
}

‎kernel/mutex-debug.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void debug_mutex_init(struct mutex *lock, const char *name,
9191
* Make sure we are not reinitializing a held lock:
9292
*/
9393
debug_check_no_locks_freed((void *)lock, sizeof(*lock));
94-
lockdep_init_map(&lock->dep_map, name, key);
94+
lockdep_init_map(&lock->dep_map, name, key, 0);
9595
#endif
9696
lock->owner = NULL;
9797
lock->magic = lock;

‎lib/rwsem-spinlock.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void __init_rwsem(struct rw_semaphore *sem, const char *name,
2828
* Make sure we are not reinitializing a held semaphore:
2929
*/
3030
debug_check_no_locks_freed((void *)sem, sizeof(*sem));
31-
lockdep_init_map(&sem->dep_map, name, key);
31+
lockdep_init_map(&sem->dep_map, name, key, 0);
3232
#endif
3333
sem->activity = 0;
3434
spin_lock_init(&sem->wait_lock);

‎lib/rwsem.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void __init_rwsem(struct rw_semaphore *sem, const char *name,
1919
* Make sure we are not reinitializing a held semaphore:
2020
*/
2121
debug_check_no_locks_freed((void *)sem, sizeof(*sem));
22-
lockdep_init_map(&sem->dep_map, name, key);
22+
lockdep_init_map(&sem->dep_map, name, key, 0);
2323
#endif
2424
sem->count = RWSEM_UNLOCKED_VALUE;
2525
spin_lock_init(&sem->wait_lock);

‎lib/spinlock_debug.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void __spin_lock_init(spinlock_t *lock, const char *name,
2020
* Make sure we are not reinitializing a held lock:
2121
*/
2222
debug_check_no_locks_freed((void *)lock, sizeof(*lock));
23-
lockdep_init_map(&lock->dep_map, name, key);
23+
lockdep_init_map(&lock->dep_map, name, key, 0);
2424
#endif
2525
lock->raw_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2626
lock->magic = SPINLOCK_MAGIC;
@@ -38,7 +38,7 @@ void __rwlock_init(rwlock_t *lock, const char *name,
3838
* Make sure we are not reinitializing a held lock:
3939
*/
4040
debug_check_no_locks_freed((void *)lock, sizeof(*lock));
41-
lockdep_init_map(&lock->dep_map, name, key);
41+
lockdep_init_map(&lock->dep_map, name, key, 0);
4242
#endif
4343
lock->raw_lock = (raw_rwlock_t) __RAW_RW_LOCK_UNLOCKED;
4444
lock->magic = RWLOCK_MAGIC;

‎net/core/sock.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ static void inline sock_lock_init(struct sock *sk)
823823
af_family_slock_key_strings[sk->sk_family]);
824824
lockdep_init_map(&sk->sk_lock.dep_map,
825825
af_family_key_strings[sk->sk_family],
826-
af_family_keys + sk->sk_family);
826+
af_family_keys + sk->sk_family, 0);
827827
}
828828

829829
/**

0 commit comments

Comments
 (0)