Skip to contentSkip to conversation

serio lockdep annotation

PATCH In review Peter Zijlstra 4 participants 8 messages Sep 26, 2006

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover serio lockdep annotation

Hi,

These patches should quiet lockdep down for serio. Ideas were reaped from the previous thread on this subject.
http://lkml.org/lkml/2006/9/13/256

I hope this version does satisfy all parties.

Unfortunately I do not appear to have offending hardware, so if anybody could confirm that they indeed do as advertised...

Peter
--

#4 lore raw

PATCH 2/2 serio: lockdep annotation for ps2dev->cmd_mutex and serio->lock

Based ideas from Jiri Kosina, this patch tracks the nesting depth and uses the new lockdep_set_class_and_subclass() annotation to store this information in the lock objects.

---
 drivers/input/serio/libps2.c |    4 ++++
 drivers/input/serio/serio.c  |    9 ++++++++-
 include/linux/serio.h        |    1 +
 3 files changed, 13 insertions(+), 1 deletion(-)
drivers/input/serio/libps2.c +4 -0
Index: linux-2.6-mm/drivers/input/serio/libps2.c
===================================================================
@@ -280,6 +280,8 @@ int ps2_schedule_command(struct ps2dev *
 	return 0;
 }
 
+static struct lock_class_key ps2_mutex_key;
+
 /*
  * ps2_init() initializes ps2dev structure
  */
@@ -287,6 +289,8 @@ int ps2_schedule_command(struct ps2dev *
 void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
 {
 	mutex_init(&ps2dev->cmd_mutex);
+	lockdep_set_class_and_subclass(&ps2dev->cmd_mutex, &ps2_mutex_key,
+				       serio->depth);
 	init_waitqueue_head(&ps2dev->wait);
 	ps2dev->serio = serio;
 }
drivers/input/serio/serio.c +8 -1
Index: linux-2.6-mm/drivers/input/serio/serio.c
===================================================================
@@ -521,6 +521,8 @@ static void serio_release_port(struct de
 	module_put(THIS_MODULE);
 }
 
+static struct lock_class_key serio_lock_key;
+
 /*
  * Prepare serio port for registration.
  */
@@ -538,8 +540,13 @@ static void serio_init_port(struct serio
 		 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
 	serio->dev.bus = &serio_bus;
 	serio->dev.release = serio_release_port;
-	if (serio->parent)
+	if (serio->parent) {
 		serio->dev.parent = &serio->parent->dev;
+		serio->depth = serio->parent->depth + 1;
+	} else
+		serio->depth = 0;
+	lockdep_set_class_and_subclass(&serio->lock, &serio_lock_key,
+				       serio->depth);
 }
 
 /*
include/linux/serio.h +1 -0
Index: linux-2.6-mm/include/linux/serio.h
===================================================================
@@ -41,6 +41,7 @@ struct serio {
 	void (*stop)(struct serio *);
 
 	struct serio *parent, *child;
+	unsigned int depth;		/* level of nesting in serio hierarchy */
 
 	struct serio_driver *drv;	/* accessed from interrupt, must be protected by serio->lock and serio->sem */
 	struct mutex drv_mutex;		/* protects serio->drv so attributes can pin driver */

--

#1 lore raw

PATCH 1/2 lockdep: lockdep_set_class_and_subclass

In reply to Peter Zijlstra

Add lockdep_set_class_and_subclass() to the lockdep annotations.

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.

---
 include/linux/lockdep.h |   12 ++++++++----
 kernel/lockdep.c        |   10 ++++++----
 kernel/mutex-debug.c    |    2 +-
 lib/rwsem-spinlock.c    |    2 +-
 lib/rwsem.c             |    2 +-
 lib/spinlock_debug.c    |    4 ++--
 net/core/sock.c         |    2 +-
 7 files changed, 20 insertions(+), 14 deletions(-)
include/linux/lockdep.h +8 -4
Index: linux-2.6-mm/include/linux/lockdep.h
===================================================================
@@ -202,7 +202,7 @@ extern int lockdep_internal(void);
  */
 
 extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
-			     struct lock_class_key *key);
+			     struct lock_class_key *key, int subclass);
 
 /*
  * Reinitialize a lock key - for cases where there is special locking or
@@ -211,9 +211,11 @@ extern void lockdep_init_map(struct lock
  * or they are too narrow (they suffer from a false class-split):
  */
 #define lockdep_set_class(lock, key) \
-		lockdep_init_map(&(lock)->dep_map, #key, key)
+		lockdep_init_map(&(lock)->dep_map, #key, key, 0)
 #define lockdep_set_class_and_name(lock, key, name) \
-		lockdep_init_map(&(lock)->dep_map, name, key)
+		lockdep_init_map(&(lock)->dep_map, name, key, 0)
+#define lockdep_set_class_and_subclass(lock, key, sub) \
+		lockdep_init_map(&(lock)->dep_map, #key, key, sub)
 
 /*
  * Acquire a lock.
@@ -257,10 +259,12 @@ static inline int lockdep_internal(void)
 # define lock_release(l, n, i)			do { } while (0)
 # define lockdep_init()				do { } while (0)
 # define lockdep_info()				do { } while (0)
-# define lockdep_init_map(lock, name, key)	do { (void)(key); } while (0)
+# define lockdep_init_map(lock, name, key, sub)	do { (void)(key); } while (0)
 # define lockdep_set_class(lock, key)		do { (void)(key); } while (0)
 # define lockdep_set_class_and_name(lock, key, name) \
 		do { (void)(key); } while (0)
+#define lockdep_set_class_and_subclass(lock, key, sub) \
+		do { (void)(key); } while (0)
 # define INIT_LOCKDEP
 # define lockdep_reset()		do { debug_locks = 1; } while (0)
 # define lockdep_free_key_range(start, size)	do { } while (0)
kernel/lockdep.c +6 -4
Index: linux-2.6-mm/kernel/lockdep.c
===================================================================
@@ -1177,7 +1177,7 @@ look_up_lock_class(struct lockdep_map *l
  * itself, so actual lookup of the hash should be once per lock object.
  */
 static inline struct lock_class *
-register_lock_class(struct lockdep_map *lock, unsigned int subclass)
+register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
 {
 	struct lockdep_subclass_key *key;
 	struct list_head *hash_head;
@@ -1249,7 +1249,7 @@ register_lock_class(struct lockdep_map *
 out_unlock_set:
 	__raw_spin_unlock(&hash_lock);
 
-	if (!subclass)
+	if (!subclass || force)
 		lock->class_cache = class;
 
 	DEBUG_LOCKS_WARN_ON(class->subclass != subclass);
@@ -1937,7 +1937,7 @@ void trace_softirqs_off(unsigned long ip
  * Initialize a lock instance's lock-class mapping info:
  */
 void lockdep_init_map(struct lockdep_map *lock, const char *name,
-		      struct lock_class_key *key)
+		      struct lock_class_key *key, int subclass)
 {
 	if (unlikely(!debug_locks))
 		return;
@@ -1957,6 +1957,8 @@ void lockdep_init_map(struct lockdep_map
 	lock->name = name;
 	lock->key = key;
 	lock->class_cache = NULL;
+	if (subclass)
+		register_lock_class(lock, subclass, 1);
 }
 
 EXPORT_SYMBOL_GPL(lockdep_init_map);
@@ -1995,7 +1997,7 @@ static int __lock_acquire(struct lockdep
 	 * Not cached yet or subclass?
 	 */
 	if (unlikely(!class)) {
-		class = register_lock_class(lock, subclass);
+		class = register_lock_class(lock, subclass, 0);
 		if (!class)
 			return 0;
 	}
kernel/mutex-debug.c +1 -1
Index: linux-2.6-mm/kernel/mutex-debug.c
===================================================================
@@ -91,7 +91,7 @@ void debug_mutex_init(struct mutex *lock
 	 * Make sure we are not reinitializing a held lock:
 	 */
 	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
-	lockdep_init_map(&lock->dep_map, name, key);
+	lockdep_init_map(&lock->dep_map, name, key, 0);
 #endif
 	lock->owner = NULL;
 	lock->magic = lock;
lib/rwsem-spinlock.c +1 -1
Index: linux-2.6-mm/lib/rwsem-spinlock.c
===================================================================
@@ -28,7 +28,7 @@ void __init_rwsem(struct rw_semaphore *s
 	 * Make sure we are not reinitializing a held semaphore:
 	 */
 	debug_check_no_locks_freed((void *)sem, sizeof(*sem));
-	lockdep_init_map(&sem->dep_map, name, key);
+	lockdep_init_map(&sem->dep_map, name, key, 0);
 #endif
 	sem->activity = 0;
 	spin_lock_init(&sem->wait_lock);
lib/rwsem.c +1 -1
Index: linux-2.6-mm/lib/rwsem.c
===================================================================
@@ -19,7 +19,7 @@ void __init_rwsem(struct rw_semaphore *s
 	 * Make sure we are not reinitializing a held semaphore:
 	 */
 	debug_check_no_locks_freed((void *)sem, sizeof(*sem));
-	lockdep_init_map(&sem->dep_map, name, key);
+	lockdep_init_map(&sem->dep_map, name, key, 0);
 #endif
 	sem->count = RWSEM_UNLOCKED_VALUE;
 	spin_lock_init(&sem->wait_lock);
lib/spinlock_debug.c +2 -2
Index: linux-2.6-mm/lib/spinlock_debug.c
===================================================================
@@ -20,7 +20,7 @@ void __spin_lock_init(spinlock_t *lock, 
 	 * Make sure we are not reinitializing a held lock:
 	 */
 	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
-	lockdep_init_map(&lock->dep_map, name, key);
+	lockdep_init_map(&lock->dep_map, name, key, 0);
 #endif
 	lock->raw_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
 	lock->magic = SPINLOCK_MAGIC;
@@ -38,7 +38,7 @@ void __rwlock_init(rwlock_t *lock, const
 	 * Make sure we are not reinitializing a held lock:
 	 */
 	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
-	lockdep_init_map(&lock->dep_map, name, key);
+	lockdep_init_map(&lock->dep_map, name, key, 0);
 #endif
 	lock->raw_lock = (raw_rwlock_t) __RAW_RW_LOCK_UNLOCKED;
 	lock->magic = RWLOCK_MAGIC;
net/core/sock.c +1 -1
Index: linux-2.6-mm/net/core/sock.c
===================================================================
@@ -823,7 +823,7 @@ static void inline sock_lock_init(struct
 				   af_family_slock_key_strings[sk->sk_family]);
 	lockdep_init_map(&sk->sk_lock.dep_map,
 			 af_family_key_strings[sk->sk_family],
-			 af_family_keys + sk->sk_family);
+			 af_family_keys + sk->sk_family, 0);
 }
 
 /**

--

Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] lockdep: lockdep_set_class_and_subclass

Quoted 11 lines · Peter Zijlstra

Add lockdep_set_class_and_subclass() to the lockdep annotations.

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.

thanks, this extension to lockdep.c looks good to me - provided it solves the problem :-)

Ingo

#3 lore raw

Re: [PATCH 1/2] lockdep: lockdep_set_class_and_subclass

Quoted 13 lines · Ingo Molnar
Quoted 10 lines · Peter Zijlstra

Add lockdep_set_class_and_subclass() to the lockdep annotations.

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.

Quoted 7 lines
Quoted 1 line

thanks, this extension to lockdep.c looks good to me - provided it solves the problem :-)

I will try testing it tonight - I have a Synaptics with a pass-through port. Anyway, it really looks good now as far as serio code concerned.

Signature · 1 line
Dmitry
#5 lore raw

Re: [PATCH 2/2] serio: lockdep annotation for ps2dev->cmd_mutex and serio->lock

In reply to Peter Zijlstra
Quoted 3 lines · Peter Zijlstra

Based ideas from Jiri Kosina, this patch tracks the nesting depth and uses the new lockdep_set_class_and_subclass() annotation to store this information in the lock objects.

Hi,

the lockdep part of the patch (1/2) is OK. The second part, specifically the libps2.c changes, are not complete - the originally (wrongly) introduced mutex_lock_nested() has to be changed back to mutex_lock(), otherwise we will get spurious warning from lockdep about ps2_mutex_key.

Below is the fixed version of the patch. I confirm that this (together with Peter's original changes in lockdep, already acked by Ingo) fixes the synpatics passthrough port lockdep warnings.

So, as long as you, Dmitry, seem to be convenient with this approach, please apply. Thanks.

drivers/input/serio/libps2.c +5 -1
Index: linux-2.6-mm/drivers/input/serio/libps2.c
===================================================================
@@ -182,7 +182,7 @@ int ps2_command(struct ps2dev *ps2dev, u
 		return -1;
 	}
 
-	mutex_lock_nested(&ps2dev->cmd_mutex, SINGLE_DEPTH_NESTING);
+	mutex_lock(&ps2dev->cmd_mutex);
 
 	serio_pause_rx(ps2dev->serio);
 	ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
@@ -280,6 +280,8 @@ int ps2_schedule_command(struct ps2dev *
 	return 0;
 }
 
+static struct lock_class_key ps2_mutex_key;
+
 /*
  * ps2_init() initializes ps2dev structure
  */
@@ -287,6 +289,8 @@ int ps2_schedule_command(struct ps2dev *
 void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
 {
 	mutex_init(&ps2dev->cmd_mutex);
+	lockdep_set_class_and_subclass(&ps2dev->cmd_mutex, &ps2_mutex_key,
+				       serio->depth);
 	init_waitqueue_head(&ps2dev->wait);
 	ps2dev->serio = serio;
 }
drivers/input/serio/serio.c +8 -1
Index: linux-2.6-mm/drivers/input/serio/serio.c
===================================================================
@@ -521,6 +521,8 @@ static void serio_release_port(struct de
 	module_put(THIS_MODULE);
 }
 
+static struct lock_class_key serio_lock_key;
+
 /*
  * Prepare serio port for registration.
  */
@@ -538,8 +540,13 @@ static void serio_init_port(struct serio
 		 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
 	serio->dev.bus = &serio_bus;
 	serio->dev.release = serio_release_port;
-	if (serio->parent)
+	if (serio->parent) {
 		serio->dev.parent = &serio->parent->dev;
+		serio->depth = serio->parent->depth + 1;
+	} else
+		serio->depth = 0;
+	lockdep_set_class_and_subclass(&serio->lock, &serio_lock_key,
+				       serio->depth);
 }
 
 /*
include/linux/serio.h +1 -0
Index: linux-2.6-mm/include/linux/serio.h
===================================================================
@@ -41,6 +41,7 @@ struct serio {
 	void (*stop)(struct serio *);
 
 	struct serio *parent, *child;
+	unsigned int depth;		/* level of nesting in serio hierarchy */
 
 	struct serio_driver *drv;	/* accessed from interrupt, must be protected by serio->lock and serio->sem */
 	struct mutex drv_mutex;		/* protects serio->drv so attributes can pin driver */
Signature · 1 line
Jiri Kosina
author Acked-by
#6 lore raw

Re: [PATCH 2/2] serio: lockdep annotation for ps2dev->cmd_mutex and serio->lock

Quoted 4 lines · Jiri Kosina

the lockdep part of the patch (1/2) is OK. The second part, specifically the libps2.c changes, are not complete - the originally (wrongly) introduced mutex_lock_nested() has to be changed back to mutex_lock(), otherwise we will get spurious warning from lockdep about ps2_mutex_key.

Ah, missed that that part was actually in the tree ;-)

Quoted 8 lines

Below is the fixed version of the patch. I confirm that this (together with Peter's original changes in lockdep, already acked by Ingo) fixes the synpatics passthrough port lockdep warnings.

So, as long as you, Dmitry, seem to be convenient with this approach, please apply. Thanks.

Thanks for testing

Acked-by
#7 lore raw

Re: [PATCH 2/2] serio: lockdep annotation for ps2dev->cmd_mutex and serio->lock

Quoted 9 lines · Peter Zijlstra
Quoted 8 lines

Below is the fixed version of the patch. I confirm that this (together with Peter's original changes in lockdep, already acked by Ingo) fixes the synpatics passthrough port lockdep warnings.

So, as long as you, Dmitry, seem to be convenient with this approach, please apply. Thanks.

great. Re-ack:

Ingo