lockdep: constify usage of struct lock_class_key where possible
PATCH 1/1 lockdep: constify usage of struct lock_class_key where possible
Constify usage of struct lock_class_key where possible. This requires to constify also usage of struct lockdep_subclass_key in two places.
Especially relevant is constification of lockdep_map.key, as it makes clear that the key isn't changed during lifetime of struct lockdep_map.
--- include/linux/lockdep.h | 18 ++++++++++-------- include/linux/lockdep_types.h | 2 +- kernel/locking/lockdep.c | 12 ++++++------ 3 files changed, 17 insertions(+), 15 deletions(-)
include/linux/lockdep.h +10 -8
@@ -125,25 +125,27 @@ extern void lockdep_unregister_key(struct lock_class_key *key);* to lockdep: */ -extern void lockdep_init_map_type (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass,u8 inner,u8 outer,u8 lock_type); +void lockdep_init_map_type (struct lockdep_map *lock,const char *name, +const struct lock_class_key *key,int subclass, +u8 inner,u8 outer,u8 lock_type);static inline void lockdep_init_map_waits (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass,u8 inner,u8 outer) +const struct lock_class_key *key,int subclass, +u8 inner,u8 outer) {lockdep_init_map_type (lock, name, key, subclass, inner, outer,LD_LOCK_NORMAL ); }static inline void lockdep_init_map_wait (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass,u8 inner) +const struct lock_class_key *key,int subclass,u8 inner) {lockdep_init_map_waits (lock, name, key, subclass, inner,LD_WAIT_INV ); }static inline void lockdep_init_map (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass) +const struct lock_class_key *key,int subclass) {lockdep_init_map_wait (lock, name, key, subclass,LD_WAIT_INV ); }
@@ -252,9 +254,9 @@ static inline int lock_is_held(const struct lockdep_map *lock)#define lockdep_is_held (lock) lock_is_held(&(lock)->dep_map)#define lockdep_is_held_type (lock, r) lock_is_held_type(&(lock)->dep_map, (r)) -extern void lock_set_class (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,unsigned int subclass, -unsigned long ip); +void lock_set_class (struct lockdep_map *lock,const char *name, +const struct lock_class_key *key,unsigned int subclass, +unsigned long ip);#define lock_set_novalidate_class (l, n, i) \ lock_set_class(l, n, &__lockdep_no_validate__, 0, i)
include/linux/lockdep_types.h +1 -1
@@ -184,7 +184,7 @@ void clear_lock_stats(struct lock_class *class);* This is embedded into specific lock instances: */ struct lockdep_map { -struct lock_class_key *key; +const struct lock_class_key *key;struct lock_class *class_cache[NR_LOCKDEP_CACHING_CLASSES ];const char *name;u8 wait_type_outer;/* can be taken in this context */
kernel/locking/lockdep.c +6 -6
@@ -888,7 +888,7 @@ static int count_matching_names(struct lock_class *new_class) static noinstr struct lock_class *look_up_lock_class (const struct lockdep_map *lock,unsigned int subclass) { -struct lockdep_subclass_key *key; +const struct lockdep_subclass_key *key;struct hlist_head *hash_head;struct lock_class *class;
@@ -1286,7 +1286,7 @@ static bool is_dynamic_key(const struct lock_class_key *key)static struct lock_class *register_lock_class (struct lockdep_map *lock,unsigned int subclass,int force) { -struct lockdep_subclass_key *key; +const struct lockdep_subclass_key *key;struct hlist_head *hash_head;struct lock_class *class;int idx;
@@ -4937,8 +4937,8 @@ static inline int check_wait_context(struct task_struct *curr,* Initialize a lock instance's lock-class mapping info: */ void lockdep_init_map_type (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass, -u8 inner,u8 outer,u8 lock_type) +const struct lock_class_key *key,int subclass, +u8 inner,u8 outer,u8 lock_type) {int i;
@@ -5408,7 +5408,7 @@ static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,static int __lock_set_class (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,unsigned int subclass, +const struct lock_class_key *key,unsigned int subclass,unsigned long ip) {struct task_struct *curr = current;
@@ -5733,7 +5733,7 @@ static noinstr void check_flags(unsigned long flags) }void lock_set_class (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,unsigned int subclass, +const struct lock_class_key *key,unsigned int subclass,unsigned long ip) {unsigned long flags;
Signature · 1 line
2.54.0
Waiman Long
reviewerQuoted 5 lines · Heiner Kallweit
Constify usage of struct
lock_class_keywhere possible. This requires to constify also usage of structlockdep_subclass_keyin two places.Especially relevant is constification of lockdep_map.key, as it makes clear that the key isn't changed during lifetime of struct
lockdep_map.
Other than making clear that the lock_class_key isn't being modified, is there other benefit of making this change like helping compiler to generate better code?
Cheers,
Longman
Quoted 122 lines
Signed-off-by Heiner Kallweit <[email protected]>--- include/linux/lockdep.h | 18 ++++++++++-------- include/linux/lockdep_types.h | 2 +- kernel/locking/lockdep.c | 12 ++++++------ 3 files changed, 17 insertions(+), 15 deletions(-)include/linux/lockdep.h +10
-8@@ -125,25 +125,27 @@ extern void lockdep_unregister_key(struct lock_class_key *key);* to lockdep: */ -extern void lockdep_init_map_type (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass,u8 inner,u8 outer,u8 lock_type); +void lockdep_init_map_type (struct lockdep_map *lock,const char *name, +const struct lock_class_key *key,int subclass, +u8 inner,u8 outer,u8 lock_type);static inline void lockdep_init_map_waits (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass,u8 inner,u8 outer) +const struct lock_class_key *key,int subclass, +u8 inner,u8 outer) {lockdep_init_map_type (lock, name, key, subclass, inner, outer,LD_LOCK_NORMAL ); }static inline void lockdep_init_map_wait (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass,u8 inner) +const struct lock_class_key *key,int subclass,u8 inner) {lockdep_init_map_waits (lock, name, key, subclass, inner,LD_WAIT_INV ); }static inline void lockdep_init_map (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass) +const struct lock_class_key *key,int subclass) {lockdep_init_map_wait (lock, name, key, subclass,LD_WAIT_INV ); }@@ -252,9 +254,9 @@ static inline int lock_is_held(const struct lockdep_map *lock)#define lockdep_is_held (lock) lock_is_held(&(lock)->dep_map)#define lockdep_is_held_type (lock, r) lock_is_held_type(&(lock)->dep_map, (r)) -extern void lock_set_class (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,unsigned int subclass, -unsigned long ip); +void lock_set_class (struct lockdep_map *lock,const char *name, +const struct lock_class_key *key,unsigned int subclass, +unsigned long ip);#define lock_set_novalidate_class (l, n, i) \ lock_set_class(l, n, &__lockdep_no_validate__, 0, i)include/linux/lockdep_types.h +1
-1@@ -184,7 +184,7 @@ void clear_lock_stats(struct lock_class *class);* This is embedded into specific lock instances: */ struct lockdep_map { -struct lock_class_key *key; +const struct lock_class_key *key;struct lock_class *class_cache[NR_LOCKDEP_CACHING_CLASSES ];const char *name;u8 wait_type_outer;/* can be taken in this context */ kernel/locking/lockdep.c +6
-6@@ -888,7 +888,7 @@ static int count_matching_names(struct lock_class *new_class) static noinstr struct lock_class *look_up_lock_class (const struct lockdep_map *lock,unsigned int subclass) { -struct lockdep_subclass_key *key; +const struct lockdep_subclass_key *key;struct hlist_head *hash_head;struct lock_class *class;@@ -1286,7 +1286,7 @@ static bool is_dynamic_key(const struct lock_class_key *key)static struct lock_class *register_lock_class (struct lockdep_map *lock,unsigned int subclass,int force) { -struct lockdep_subclass_key *key; +const struct lockdep_subclass_key *key;struct hlist_head *hash_head;struct lock_class *class;int idx;@@ -4937,8 +4937,8 @@ static inline int check_wait_context(struct task_struct *curr,* Initialize a lock instance's lock-class mapping info: */ void lockdep_init_map_type (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass, -u8 inner,u8 outer,u8 lock_type) +const struct lock_class_key *key,int subclass, +u8 inner,u8 outer,u8 lock_type) {int i;@@ -5408,7 +5408,7 @@ static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,static int __lock_set_class (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,unsigned int subclass, +const struct lock_class_key *key,unsigned int subclass,unsigned long ip) {struct task_struct *curr = current;@@ -5733,7 +5733,7 @@ static noinstr void check_flags(unsigned long flags) }void lock_set_class (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,unsigned int subclass, +const struct lock_class_key *key,unsigned int subclass,unsigned long ip) {unsigned long flags;
Quoted 9 lines · Waiman Long
Quoted 5 lines · Heiner Kallweit
Constify usage of struct
lock_class_keywhere possible. This requires to constify also usage of structlockdep_subclass_keyin two places.Especially relevant is constification of lockdep_map.key, as it makes clear that the key isn't changed during lifetime of struct
lockdep_map.Other than making clear that the
lock_class_keyisn't being modified, is there other benefit of making this change like helping compiler to generate better code?
Doing an objdump -o kernel/locking/lockdep.o I see no difference in section sizes. Hence at least with gcc it doesn't seem to affect code generation. I didn't check whether it affects callers of e.g. lockdep_init_map_wait().
Quoted 116 lines
Cheers,
LongmanQuoted 112 lines
Signed-off-by Heiner Kallweit <[email protected]>---
include/linux/lockdep.h | 18 ++++++++++--------include/linux/lockdep_types.h| 2 +- kernel/locking/lockdep.c | 12 ++++++------3 files changed, 17 insertions(+), 15 deletions(-)include/linux/lockdep.h +3
-1@@ -125,25 +125,27 @@ extern void lockdep_unregister_key(struct lock_class_key *key);* to lockdep: */ -extern void lockdep_init_map_type(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, int subclass, u8 inner, u8 outer, u8 lock_type); +void lockdep_init_map_type (struct lockdep_map *lock,const char *name, + const struct lock_class_key *key, int subclass, + u8 inner, u8 outer, u8 lock_type);static inline void
lockdep_init_map_waits(structlockdep_map*lock, const char *name,
- structlock_class_key*key, int subclass, u8 inner, u8 outer) + const structlock_class_key*key, int subclass,
+ u8 inner, u8 outer)
{
lockdep_init_map_type(lock, name, key, subclass, inner, outer, LD_LOCK_NORMAL); }
static inline void
lockdep_init_map_wait(structlockdep_map*lock, const char *name,
- structlock_class_key*key, int subclass, u8 inner)
+ const structlock_class_key*key, int subclass, u8 inner) {
lockdep_init_map_waits(lock, name, key, subclass, inner, LD_WAIT_INV); }
static inline void lockdep_init_map(structlockdep_map*lock, const char *name, - structlock_class_key*key, int subclass)
+ const structlock_class_key*key, int subclass)
{
lockdep_init_map_wait(lock, name, key, subclass, LD_WAIT_INV);
}include/linux/lockdep.h +3
-2@@ -252,9 +254,9 @@ static inline int lock_is_held(const struct lockdep_map *lock) #define lockdep_is_held(lock) lock_is_held(&(lock)->dep_map) #define lockdep_is_held_type(lock, r) lock_is_held_type(&(lock)->dep_map, (r)) -extern void lock_set_class(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, unsigned int subclass, - unsigned long ip); +void lock_set_class (struct lockdep_map *lock,const char *name, + const struct lock_class_key *key, unsigned int subclass, + unsigned long ip); #define lock_set_novalidate_class(l, n, i) \ lock_set_class(l, n, &__lockdep_no_validate__, 0, i)include/linux/lockdep_types.h +1
-1@@ -184,7 +184,7 @@ void clear_lock_stats(struct lock_class *class);* This is embedded into specific lock instances: */ struct lockdep_map { - struct lock_class_key *key; + const struct lock_class_key *key; struct lock_class *class_cache[NR_LOCKDEP_CACHING_CLASSES]; const char *name;u8
wait_type_outer; /* can be taken in this context */kernel/locking/lockdep.c +0
-0@@ -888,7 +888,7 @@ static int count_matching_names(struct lock_class *new_class)static noinstr struct
lock_class*
look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass){
- structlockdep_subclass_key*key;
+ const structlockdep_subclass_key*key;
structhlist_head*hash_head;
structlock_class*class;
@@ -1286,7 +1286,7 @@ static boolis_dynamic_key(const struct lock_class_key *key)static structlock_class*
register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force){
- structlockdep_subclass_key*key;
+ const structlockdep_subclass_key*key;
structhlist_head*hash_head;
structlock_class*class;
int idx;kernel/locking/lockdep.c +2
-2@@ -4937,8 +4937,8 @@ static inline int check_wait_context(struct task_struct *curr,* Initialize a lock instance's lock-class mapping info: */ void lockdep_init_map_type(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, int subclass, - u8 inner, u8 outer, u8 lock_type) + const struct lock_class_key *key, int subclass, + u8 inner, u8 outer, u8 lock_type) { int i; @@ -5408,7 +5408,7 @@ static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,static int
__lock_set_class(structlockdep_map*lock, const char *name, - structlock_class_key*key, unsigned int subclass, + const structlock_class_key*key, unsigned int subclass, unsigned long ip)
{
structtask_struct*curr = current;kernel/locking/lockdep.c +1
-1@@ -5733,7 +5733,7 @@ static noinstr void check_flags(unsigned long flags) } void lock_set_class(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, unsigned int subclass, + const struct lock_class_key *key, unsigned int subclass, unsigned long ip) { unsigned long flags;
Waiman Long
reviewerQuoted 5 lines · Heiner Kallweit
Quoted 4 lines · Waiman Long
Quoted 3 lines · Heiner Kallweit
Constify usage of struct
lock_class_keywhere possible. This requires to constify also usage of structlockdep_subclass_keyin two places.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Especially relevant is constification of lockdep_map.key, as it makes clear that the key isn't changed during lifetime of struct
lockdep_map.Other than making clear that the
lock_class_keyisn't being modified, is there other benefit of making this change like helping compiler to generate better code?Doing an objdump -o
kernel/locking/lockdep.oI see no difference in section sizes. Hence at least with gcc it doesn't seem to affect code generation. I didn't check whether it affects callers of e.g.lockdep_init_map_wait().
I asked because just stating the nature of the lock_class_key doesn't contribute much to the lockdep code. I am not against this change, but it is up others to decide if they will take the patch.
Cheers,
Longman
Quoted 115 lines
Quoted 115 lines
Cheers,
LongmanQuoted 111 lines
Signed-off-by Heiner Kallweit <[email protected]>--- include/linux/lockdep.h | 18 ++++++++++-------- include/linux/lockdep_types.h | 2 +- kernel/locking/lockdep.c | 12 ++++++------ 3 files changed, 17 insertions(+), 15 deletions(-)include/linux/lockdep.h +10
-6@@ -125,25 +125,27 @@ extern void lockdep_unregister_key(struct lock_class_key *key);* to lockdep: */ -extern void lockdep_init_map_type(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, int subclass, u8 inner, u8 outer, u8 lock_type); +void lockdep_init_map_type (struct lockdep_map *lock,const char *name, + const struct lock_class_key *key, int subclass, + u8 inner, u8 outer, u8 lock_type); static inline void lockdep_init_map_waits(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, int subclass, u8 inner, u8 outer) + const struct lock_class_key *key, int subclass, + u8 inner, u8 outer) { lockdep_init_map_type(lock, name, key, subclass, inner, outer, LD_LOCK_NORMAL); } static inline void lockdep_init_map_wait(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, int subclass, u8 inner) + const struct lock_class_key *key, int subclass, u8 inner) { lockdep_init_map_waits(lock, name, key, subclass, inner, LD_WAIT_INV); } static inline void lockdep_init_map(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, int subclass) + const struct lock_class_key *key, int subclass) { lockdep_init_map_wait(lock, name, key, subclass, LD_WAIT_INV); }@@ -252,9 +254,9 @@ static inline int lock_is_held(const struct lockdep_map *lock) #define lockdep_is_held(lock) lock_is_held(&(lock)->dep_map) #define lockdep_is_held_type(lock, r) lock_is_held_type(&(lock)->dep_map, (r)) -extern void lock_set_class(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, unsigned int subclass, - unsigned long ip); +void lock_set_class (struct lockdep_map *lock,const char *name, + const struct lock_class_key *key, unsigned int subclass, + unsigned long ip); #define lock_set_novalidate_class(l, n, i) \ lock_set_class(l, n, &__lockdep_no_validate__, 0, i)include/linux/lockdep_types.h +1
-1@@ -184,7 +184,7 @@ void clear_lock_stats(struct lock_class *class);* This is embedded into specific lock instances: */ struct lockdep_map { - struct lock_class_key *key; + const struct lock_class_key *key; struct lock_class *class_cache[NR_LOCKDEP_CACHING_CLASSES]; const char *name; u8 wait_type_outer; /* can be taken in this context */kernel/locking/lockdep.c +1
-1@@ -888,7 +888,7 @@ static int count_matching_names(struct lock_class *new_class) static noinstr struct lock_class * look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass) { - struct lockdep_subclass_key *key; + const struct lockdep_subclass_key *key; struct hlist_head *hash_head; struct lock_class *class; @@ -1286,7 +1286,7 @@ static bool is_dynamic_key(const struct lock_class_key *key)static struct
lock_class*
register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force){
- structlockdep_subclass_key*key;
+ const structlockdep_subclass_key*key;
structhlist_head*hash_head;
structlock_class*class;
int idx;kernel/locking/lockdep.c +2
-2@@ -4937,8 +4937,8 @@ static inline int check_wait_context(struct task_struct *curr,* Initialize a lock instance's lock-class mapping info: */ void lockdep_init_map_type(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, int subclass, - u8 inner, u8 outer, u8 lock_type) + const struct lock_class_key *key, int subclass, + u8 inner, u8 outer, u8 lock_type) { int i; @@ -5408,7 +5408,7 @@ static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,static int
__lock_set_class(structlockdep_map*lock, const char *name,
- structlock_class_key*key, unsigned int subclass, + const structlock_class_key*key, unsigned int subclass,
unsigned long ip)
{
structtask_struct*curr = current;kernel/locking/lockdep.c +1
-1@@ -5733,7 +5733,7 @@ static noinstr void check_flags(unsigned long flags) } void lock_set_class(struct lockdep_map *lock, const char *name, - struct lock_class_key *key, unsigned int subclass, + const struct lock_class_key *key, unsigned int subclass, unsigned long ip) { unsigned long flags;
Peter Zijlstra
maintainerQuoted 28 lines · Heiner Kallweit
Constify usage of struct
lock_class_keywhere possible. This requires to constify also usage of structlockdep_subclass_keyin two places.Especially relevant is constification of lockdep_map.key, as it makes clear that the key isn't changed during lifetime of struct
lockdep_map.Signed-off-by Heiner Kallweit <[email protected]>--- include/linux/lockdep.h | 18 ++++++++++-------- include/linux/lockdep_types.h | 2 +- kernel/locking/lockdep.c | 12 ++++++------ 3 files changed, 17 insertions(+), 15 deletions(-)include/linux/lockdep.h +3
-2@@ -125,25 +125,27 @@ extern void lockdep_unregister_key(struct lock_class_key *key);* to lockdep: */ -extern void lockdep_init_map_type (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass,u8 inner,u8 outer,u8 lock_type); +void lockdep_init_map_type (struct lockdep_map *lock,const char *name, +const struct lock_class_key *key,int subclass, +u8 inner,u8 outer,u8 lock_type);
include/linux/lockdep.h static inline int lock_is_held(const struct lockdep_map *lock) · line 260
Quoted 10 lines
include/linux/lockdep.h +3
-3@@ -252,9 +254,9 @@ static inline int lock_is_held(const struct lockdep_map *lock)#define lockdep_is_held (lock) lock_is_held(&(lock)->dep_map)#define lockdep_is_held_type (lock, r) lock_is_held_type(&(lock)->dep_map, (r)) -extern void lock_set_class (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,unsigned int subclass, -unsigned long ip); +void lock_set_class (struct lockdep_map *lock,const char *name, +const struct lock_class_key *key,unsigned int subclass, +unsigned long ip);
It is also silently removing extern, why?
Quoted 32 lines · Peter Zijlstra
Quoted 31 lines · Heiner Kallweit
Constify usage of struct
lock_class_keywhere possible. This requires to constify also usage of structlockdep_subclass_keyin two places.Especially relevant is constification of lockdep_map.key, as it makes clear that the key isn't changed during lifetime of struct
lockdep_map.Signed-off-by Heiner Kallweit <[email protected]>--- include/linux/lockdep.h | 18 ++++++++++-------- include/linux/lockdep_types.h | 2 +- kernel/locking/lockdep.c | 12 ++++++------ 3 files changed, 17 insertions(+), 15 deletions(-)include/linux/lockdep.h +3
-2@@ -125,25 +125,27 @@ extern void lockdep_unregister_key(struct lock_class_key *key);* to lockdep: */ -extern void lockdep_init_map_type (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,int subclass,u8 inner,u8 outer,u8 lock_type); +void lockdep_init_map_type (struct lockdep_map *lock,const char *name, +const struct lock_class_key *key,int subclass, +u8 inner,u8 outer,u8 lock_type);@@ -252,9 +254,9 @@ static inline int lock_is_held(const struct lockdep_map *lock)#define lockdep_is_held (lock) lock_is_held(&(lock)->dep_map)#define lockdep_is_held_type (lock, r) lock_is_held_type(&(lock)->dep_map, (r))
Quoted 9 lines
include/linux/lockdep.h static inline int lock_is_held(const struct lockdep_map *lock) · line 260
Quoted 7 lines
include/linux/lockdep.h +3
-3-extern void lock_set_class (struct lockdep_map *lock,const char *name, -struct lock_class_key *key,unsigned int subclass, -unsigned long ip); +void lock_set_class (struct lockdep_map *lock,const char *name, +const struct lock_class_key *key,unsigned int subclass, +unsigned long ip);It is also silently removing extern, why?
When touching this function prototype anyway, make it compliant with coding standard. See https://www.kernel.org/doc/html/v7.0/process/coding-style.html, section 6.1. "Do not use the extern keyword with function declarations as this makes lines longer and isn’t strictly necessary."