Skip to contentSkip to conversation

Fix lockdep warning with nested instances of i2c-mux

PATCH In review Peter Rosin 4 participants 9 messages Jul 20, 2018 – Sep 11, 2018

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

v4 cover Fix lockdep warning with nested instances of i2c-mux

Hi!

Ping. This is almost a resend of v3, but I added tested-by tags from John and unwrapped a line in the commit message, so it's a v4.

How is this going to find its way to Linus? I can move this through the i2c-mux tree, but I obviously need a tag for patch 1/2 for that...

Cheers,
Peter

Changes since v3 https://lkml.org/lkml/2018/5/24/994
- Added Tested-by tags from John.
- Unwrapped commit message for patch 2/2.

Changes since v2 https://lkml.org/lkml/2018/5/24/176
- EXPORT_SYMBOL_GPL(rt_mutex_lock_nested) is more appropriate (the rt_ prefix was missing).

Changes since v1 https://lkml.org/lkml/2018/5/24/93
- Further compile tests indicated a missing #define for rt_mutex_lock with lockdep enabled, so that one is added.
- I have verified that I don't get any lockdep splat for a local i2c-mux setup with these patches applied, and that I do without them.

Cheers,
Peter

Peter Rosin (2):
rtmutex: allow specifying a subclass for nested locking
i2c: mux: annotate the nested rt_mutex usage

 drivers/i2c/i2c-core-base.c |  2 +-
 drivers/i2c/i2c-mux.c       |  4 ++--
 include/linux/rtmutex.h     |  7 +++++++
 kernel/locking/rtmutex.c    | 29 +++++++++++++++++++++++++----
 4 files changed, 35 insertions(+), 7 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH v4 1/2 rtmutex: allow specifying a subclass for nested locking

Needed for annotating rt_mutex locks.

---
 include/linux/rtmutex.h  |  7 +++++++
 kernel/locking/rtmutex.c | 29 +++++++++++++++++++++++++----
 2 files changed, 32 insertions(+), 4 deletions(-)
include/linux/rtmutex.h +7 -0
index 1b92a28dd672..6fd615a0eea9 100644
@@ -106,7 +106,14 @@ static inline int rt_mutex_is_locked(struct rt_mutex *lock)
 extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
 extern void rt_mutex_destroy(struct rt_mutex *lock);
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
+#define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
+#else
 extern void rt_mutex_lock(struct rt_mutex *lock);
+#define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
+#endif
+
 extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
 extern int rt_mutex_timed_lock(struct rt_mutex *lock,
 			       struct hrtimer_sleeper *timeout);
kernel/locking/rtmutex.c +25 -4
index 4f014be7a4b8..2823d4163a37 100644
@@ -1465,6 +1465,29 @@ rt_mutex_fastunlock(struct rt_mutex *lock,
 		rt_mutex_postunlock(&wake_q);
 }
 
+static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
+{
+	might_sleep();
+
+	mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
+	rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
+}
+
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+/**
+ * rt_mutex_lock_nested - lock a rt_mutex
+ *
+ * @lock: the rt_mutex to be locked
+ * @subclass: the lockdep subclass
+ */
+void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
+{
+	__rt_mutex_lock(lock, subclass);
+}
+EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
+#endif
+
+#ifndef CONFIG_DEBUG_LOCK_ALLOC
 /**
  * rt_mutex_lock - lock a rt_mutex
  *
@@ -1472,12 +1495,10 @@ rt_mutex_fastunlock(struct rt_mutex *lock,
  */
 void __sched rt_mutex_lock(struct rt_mutex *lock)
 {
-	might_sleep();
-
-	mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
-	rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
+	__rt_mutex_lock(lock, 0);
 }
 EXPORT_SYMBOL_GPL(rt_mutex_lock);
+#endif
 
 /**
  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
Signature · 1 line
2.11.0
#2 lore raw

[tip:locking/urgent] locking/rtmutex: Allow specifying a subclass for nested locking

locking/rtmutex: Allow specifying a subclass for nested locking
Commit-ID:  62cedf3e60af03e47849fe2bd6a03ec179422a8a
Gitweb:     https://git.kernel.org/tip/62cedf3e60af03e47849fe2bd6a03ec179422a8a
Author:     Peter Rosin <[email protected]>
AuthorDate: Fri, 20 Jul 2018 10:39:13 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 25 Jul 2018 11:22:19 +0200

locking/rtmutex: Allow specifying a subclass for nested locking

Needed for annotating rt_mutex locks.

Cc Davidlohr Bueso <[email protected]>
Cc Deepa Dinamani <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Chang <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Will Deacon <[email protected]>
Cc Wolfram Sang <[email protected]>
---
 include/linux/rtmutex.h  |  7 +++++++
 kernel/locking/rtmutex.c | 29 +++++++++++++++++++++++++----
 2 files changed, 32 insertions(+), 4 deletions(-)
include/linux/rtmutex.h +7 -0
index 1b92a28dd672..6fd615a0eea9 100644
@@ -106,7 +106,14 @@ static inline int rt_mutex_is_locked(struct rt_mutex *lock)
 extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
 extern void rt_mutex_destroy(struct rt_mutex *lock);
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
+#define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
+#else
 extern void rt_mutex_lock(struct rt_mutex *lock);
+#define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
+#endif
+
 extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
 extern int rt_mutex_timed_lock(struct rt_mutex *lock,
 			       struct hrtimer_sleeper *timeout);
kernel/locking/rtmutex.c +25 -4
index 4f014be7a4b8..2823d4163a37 100644
@@ -1465,6 +1465,29 @@ rt_mutex_fastunlock(struct rt_mutex *lock,
 		rt_mutex_postunlock(&wake_q);
 }
 
+static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
+{
+	might_sleep();
+
+	mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
+	rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
+}
+
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+/**
+ * rt_mutex_lock_nested - lock a rt_mutex
+ *
+ * @lock: the rt_mutex to be locked
+ * @subclass: the lockdep subclass
+ */
+void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
+{
+	__rt_mutex_lock(lock, subclass);
+}
+EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
+#endif
+
+#ifndef CONFIG_DEBUG_LOCK_ALLOC
 /**
  * rt_mutex_lock - lock a rt_mutex
  *
@@ -1472,12 +1495,10 @@ rt_mutex_fastunlock(struct rt_mutex *lock,
  */
 void __sched rt_mutex_lock(struct rt_mutex *lock)
 {
-	might_sleep();
-
-	mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
-	rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
+	__rt_mutex_lock(lock, 0);
 }
 EXPORT_SYMBOL_GPL(rt_mutex_lock);
+#endif
 
 /**
  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
#3 lore raw

Re: [PATCH v4 1/2] rtmutex: allow specifying a subclass for nested locking

In reply to Peter Rosin

I just noticed this patch because it conflicted with the PREEMPT_RT v4.14 patch.

Quoted 51 lines · Peter Rosin

Needed for annotating rt_mutex locks.

---
 include/linux/rtmutex.h  |  7 +++++++
 kernel/locking/rtmutex.c | 29 +++++++++++++++++++++++++----
 2 files changed, 32 insertions(+), 4 deletions(-)
include/linux/rtmutex.h +7 -0
index 1b92a28dd672..6fd615a0eea9 100644
@@ -106,7 +106,14 @@ static inline int rt_mutex_is_locked(struct rt_mutex *lock)
 extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
 extern void rt_mutex_destroy(struct rt_mutex *lock);
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
+#define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
+#else
 extern void rt_mutex_lock(struct rt_mutex *lock);
+#define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
+#endif
+
 extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
 extern int rt_mutex_timed_lock(struct rt_mutex *lock,
 			       struct hrtimer_sleeper *timeout);
kernel/locking/rtmutex.c +15 -0
index 4f014be7a4b8..2823d4163a37 100644
@@ -1465,6 +1465,29 @@ rt_mutex_fastunlock(struct rt_mutex *lock,
 		rt_mutex_postunlock(&wake_q);
 }
 
+static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
+{
+	might_sleep();
+
+	mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
+	rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
+}
+
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+/**
+ * rt_mutex_lock_nested - lock a rt_mutex
+ *
+ * @lock: the rt_mutex to be locked
+ * @subclass: the lockdep subclass
+ */

kernel/locking/rtmutex.c rt_mutex_fastunlock(struct rt_mutex *lock, · line 1492

Quoted 10 lines
kernel/locking/rtmutex.c +8 -0
+void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
+{
+	__rt_mutex_lock(lock, subclass);
+}
+EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
 
 
+#endif
+
+#ifndef CONFIG_DEBUG_LOCK_ALLOC

Why this:

#ifdef CONFIG_DEBUG_LOCK_ALLOC
[..]
#endif

#ifndef CONFIG_DEBUG_LOCK_ALLOC
[..]
#endif

???

This should use #else.

-- Steve

Quoted 20 lines
kernel/locking/rtmutex.c +0 -0
 /**

* rt_mutex_lock - lock a rt_mutex
*

kernel/locking/rtmutex.c +2 -4
@@ -1472,12 +1495,10 @@ rt_mutex_fastunlock(struct rt_mutex *lock,
  */
 void __sched rt_mutex_lock(struct rt_mutex *lock)
 {
-	might_sleep();
-
-	mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
-	rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
+	__rt_mutex_lock(lock, 0);
 }
 EXPORT_SYMBOL_GPL(rt_mutex_lock);
+#endif
 
 /**
  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
Signature · 1 line
2.11.0
#4 lore raw

Re: [PATCH v4 1/2] rtmutex: allow specifying a subclass for nested locking

Quoted 7 lines · Steven Rostedt

Why this:

#ifdef CONFIG_DEBUG_LOCK_ALLOC
[..]
#endif

Quoted 7 lines

#ifndef CONFIG_DEBUG_LOCK_ALLOC
[..]
#endif

???

This should use #else.

Yes. Water under the bridge...

Cheers,
Peter

#5 lore raw

PATCH v4 2/2 i2c: mux: annotate the nested rt_mutex usage

In reply to Peter Rosin

If an i2c topology has instances of nested muxes, then a lockdep splat is produced when when i2c_parent_lock_bus() is called. Here is an example:

============================================
WARNING: possible recursive locking detected
--------------------------------------------
insmod/68159 is trying to acquire lock:
(i2c_register_adapter#2){+.+.}, at: i2c_parent_lock_bus+0x32/0x50 [i2c_mux]

but task is already holding lock:
(i2c_register_adapter#2){+.+.}, at: i2c_parent_lock_bus+0x32/0x50 [i2c_mux]

other info that might help us debug this:

  Possible unsafe locking scenario:
        CPU0
        ----

lock(i2c_register_adapter#2);
lock(i2c_register_adapter#2);

*** DEADLOCK ***

May be due to missing lock nesting notation

1 lock held by insmod/68159:
#0: (i2c_register_adapter#2){+.+.}, at: i2c_parent_lock_bus+0x32/0x50 [i2c_mux]

stack backtrace:
CPU: 13 PID: 68159 Comm: insmod Tainted: G           O
Call Trace:
  dump_stack+0x67/0x98
  __lock_acquire+0x162e/0x1780
  lock_acquire+0xba/0x200
  rt_mutex_lock+0x44/0x60
  i2c_parent_lock_bus+0x32/0x50 [i2c_mux]
  i2c_parent_lock_bus+0x3e/0x50 [i2c_mux]
  i2c_smbus_xfer+0xf0/0x700
  i2c_smbus_read_byte+0x42/0x70
  my2c_init+0xa2/0x1000 [my2c]
  do_one_initcall+0x51/0x192
  do_init_module+0x62/0x216
  load_module+0x20f9/0x2b50
  SYSC_init_module+0x19a/0x1c0
  SyS_init_module+0xe/0x10
  do_syscall_64+0x6c/0x1a0
  entry_SYSCALL_64_after_hwframe+0x42/0xb7
---
 drivers/i2c/i2c-core-base.c | 2 +-
 drivers/i2c/i2c-mux.c       | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/i2c/i2c-core-base.c +1 -1
index f5ec6ec6776f..1157a64c7be3 100644
@@ -615,7 +615,7 @@ static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
 static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
 				 unsigned int flags)
 {
-	rt_mutex_lock(&adapter->bus_lock);
+	rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
 }
 
 /**
drivers/i2c/i2c-mux.c +2 -2
index 9669ca4937b8..7ba31f6bf148 100644
@@ -144,7 +144,7 @@ static void i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
 	struct i2c_mux_priv *priv = adapter->algo_data;
 	struct i2c_adapter *parent = priv->muxc->parent;
 
-	rt_mutex_lock(&parent->mux_lock);
+	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
 	if (!(flags & I2C_LOCK_ROOT_ADAPTER))
 		return;
 	i2c_lock_bus(parent, flags);
@@ -181,7 +181,7 @@ static void i2c_parent_lock_bus(struct i2c_adapter *adapter,
 	struct i2c_mux_priv *priv = adapter->algo_data;
 	struct i2c_adapter *parent = priv->muxc->parent;
 
-	rt_mutex_lock(&parent->mux_lock);
+	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
 	i2c_lock_bus(parent, flags);
 }
 
Signature · 1 line
2.11.0
#6 lore raw

[tip:locking/urgent] i2c/mux, locking/core: Annotate the nested rt_mutex usage

i2c/mux, locking/core: Annotate the nested rt_mutex usage
Commit-ID:  7b94ea50514d1a0dc94f02723b603c27bc0ea597
Gitweb:     https://git.kernel.org/tip/7b94ea50514d1a0dc94f02723b603c27bc0ea597
Author:     Peter Rosin <[email protected]>
AuthorDate: Fri, 20 Jul 2018 10:39:14 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 25 Jul 2018 11:22:20 +0200

i2c/mux, locking/core: Annotate the nested rt_mutex usage

If an i2c topology has instances of nested muxes, then a lockdep splat is produced when when i2c_parent_lock_bus() is called. Here is an example:

============================================
WARNING: possible recursive locking detected
--------------------------------------------
insmod/68159 is trying to acquire lock:

    (i2c_register_adapter#2){+.+.}, at: i2c_parent_lock_bus+0x32/0x50 [i2c_mux]

but task is already holding lock:

    (i2c_register_adapter#2){+.+.}, at: i2c_parent_lock_bus+0x32/0x50 [i2c_mux]

other info that might help us debug this:

    Possible unsafe locking scenario:
          CPU0
          ----
     lock(i2c_register_adapter#2);
     lock(i2c_register_adapter#2);

    *** DEADLOCK ***

    May be due to missing lock nesting notation

1 lock held by insmod/68159:

    #0:  (i2c_register_adapter#2){+.+.}, at: i2c_parent_lock_bus+0x32/0x50 [i2c_mux]
  stack backtrace:
  CPU: 13 PID: 68159 Comm: insmod Tainted: G           O
  Call Trace:
    dump_stack+0x67/0x98
    __lock_acquire+0x162e/0x1780
    lock_acquire+0xba/0x200
    rt_mutex_lock+0x44/0x60
    i2c_parent_lock_bus+0x32/0x50 [i2c_mux]
    i2c_parent_lock_bus+0x3e/0x50 [i2c_mux]
    i2c_smbus_xfer+0xf0/0x700
    i2c_smbus_read_byte+0x42/0x70
    my2c_init+0xa2/0x1000 [my2c]
    do_one_initcall+0x51/0x192
    do_init_module+0x62/0x216
    load_module+0x20f9/0x2b50
    SYSC_init_module+0x19a/0x1c0
    SyS_init_module+0xe/0x10
    do_syscall_64+0x6c/0x1a0
    entry_SYSCALL_64_after_hwframe+0x42/0xb7
Cc Davidlohr Bueso <[email protected]>
Cc Deepa Dinamani <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Chang <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Will Deacon <[email protected]>
Cc Wolfram Sang <[email protected]>
---
 drivers/i2c/i2c-core-base.c | 2 +-
 drivers/i2c/i2c-mux.c       | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/i2c/i2c-core-base.c +1 -1
index 301285c54603..15c95aaa484c 100644
@@ -624,7 +624,7 @@ static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
 static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
 				 unsigned int flags)
 {
-	rt_mutex_lock(&adapter->bus_lock);
+	rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
 }
 
 /**
drivers/i2c/i2c-mux.c +2 -2
index 300ab4b672e4..29646aa6132e 100644
@@ -144,7 +144,7 @@ static void i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
 	struct i2c_mux_priv *priv = adapter->algo_data;
 	struct i2c_adapter *parent = priv->muxc->parent;
 
-	rt_mutex_lock(&parent->mux_lock);
+	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
 	if (!(flags & I2C_LOCK_ROOT_ADAPTER))
 		return;
 	i2c_lock_bus(parent, flags);
@@ -181,7 +181,7 @@ static void i2c_parent_lock_bus(struct i2c_adapter *adapter,
 	struct i2c_mux_priv *priv = adapter->algo_data;
 	struct i2c_adapter *parent = priv->muxc->parent;
 
-	rt_mutex_lock(&parent->mux_lock);
+	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
 	i2c_lock_bus(parent, flags);
 }
 
#7 lore raw

Re: [tip:locking/urgent] i2c/mux, locking/core: Annotate the nested rt_mutex usage

Quoted 8 lines · tip-bot for Peter Rosin
Commit-ID:  7b94ea50514d1a0dc94f02723b603c27bc0ea597
Gitweb:     https://git.kernel.org/tip/7b94ea50514d1a0dc94f02723b603c27bc0ea597
Author:     Peter Rosin <[email protected]>
AuthorDate: Fri, 20 Jul 2018 10:39:14 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 25 Jul 2018 11:22:20 +0200

i2c/mux, locking/core: Annotate the nested rt_mutex usage

Hi!

I'm a bit curious as to why the subject line was changed on this patch?

(it was "[PATCH v4 2/2] i2c: mux: annotate the nested rt_mutex usage")

I thought the subject as I wrote it was just perfect. Was it so bad that it had to be edited?

The patch has no impact on "locking/core" so where is that coming from? The "i2c/mux" prefix is not in tune with the tradition of previous commits to drivers/i2c/i2c-mux.c ...

i2c: mux: improve error message for failed symlink
i2c: mux: only print failure message on error
i2c: mux: provide more info on failure in i2c_mux_add_adapter
i2c: add missing of_node_put in i2c_mux_del_adapters
i2c: move locking operations to their own struct
Merge branch 'i2c-mux-dt-3' of https://github.com/peda-r/i2c-mux into i2c/for-4.9
i2c: mux: add support for 'i2c-mux', 'i2c-arb' and 'i2c-gate' DT subnodes
i2c: add i2c_trylock_bus wrapper, use it
i2c: mux: relax locking of the top i2c adapter during mux-locked muxing
i2c: muxes always lock the parent adapter
i2c: mux: drop old unused i2c-mux api
i2c: mux: add common data for every i2c-mux instance
i2c: i2c-mux: sort includes
i2c: add ACPI support for I2C mux ports
i2c: mux: Use __i2c_transfer() instead of calling parent's master_xfer()
i2c: use parent adapter quirks in mux
i2c: mux: use proper dev when removing "channel-X" symlinks
i2c: mux: create "channel-n" symlinks for child segments in the mux device
i2c: mux: create symlink to actual mux device
...

... and my subject followed the canonical subject format as described in section 14 of Documentation/process/submitting-patches.rst ...

Subject: [PATCH 001/123] subsystem: summary phrase

... I.e. I don't see why you insist on starting the summary phrase of the subject with a capitalized "Annotate". As I have understood it, the summary phrase is not a sentence (or it should end with a period).

Three "bad" decisions on one line is a bit much, I think. I don't care deeply, though (but apparently it bugs me enough to write this, and the reason for that is probably that one will get abundant feedback on such syntax during review that it is a pest to then have all that hard-learned "experience" eradicated on what seems like a whim). Anyway, as I said, the change sparked my curiosity...

And I want *answers* :-)

Cheers,
Peter

Quoted 106 lines

If an i2c topology has instances of nested muxes, then a lockdep splat is produced when when i2c_parent_lock_bus() is called. Here is an example:

============================================
WARNING: possible recursive locking detected
--------------------------------------------
insmod/68159 is trying to acquire lock:

    (i2c_register_adapter#2){+.+.}, at: i2c_parent_lock_bus+0x32/0x50 [i2c_mux]

but task is already holding lock:

    (i2c_register_adapter#2){+.+.}, at: i2c_parent_lock_bus+0x32/0x50 [i2c_mux]

other info that might help us debug this:

    Possible unsafe locking scenario:
          CPU0
          ----
     lock(i2c_register_adapter#2);
     lock(i2c_register_adapter#2);

    *** DEADLOCK ***

    May be due to missing lock nesting notation

1 lock held by insmod/68159:

    #0:  (i2c_register_adapter#2){+.+.}, at: i2c_parent_lock_bus+0x32/0x50 [i2c_mux]
  stack backtrace:
  CPU: 13 PID: 68159 Comm: insmod Tainted: G           O
  Call Trace:
    dump_stack+0x67/0x98
    __lock_acquire+0x162e/0x1780
    lock_acquire+0xba/0x200
    rt_mutex_lock+0x44/0x60
    i2c_parent_lock_bus+0x32/0x50 [i2c_mux]
    i2c_parent_lock_bus+0x3e/0x50 [i2c_mux]
    i2c_smbus_xfer+0xf0/0x700
    i2c_smbus_read_byte+0x42/0x70
    my2c_init+0xa2/0x1000 [my2c]
    do_one_initcall+0x51/0x192
    do_init_module+0x62/0x216
    load_module+0x20f9/0x2b50
    SYSC_init_module+0x19a/0x1c0
    SyS_init_module+0xe/0x10
    do_syscall_64+0x6c/0x1a0
    entry_SYSCALL_64_after_hwframe+0x42/0xb7
Cc Davidlohr Bueso <[email protected]>
Cc Deepa Dinamani <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Chang <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Will Deacon <[email protected]>
Cc Wolfram Sang <[email protected]>
---
 drivers/i2c/i2c-core-base.c | 2 +-
 drivers/i2c/i2c-mux.c       | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/i2c/i2c-core-base.c +1 -1
index 301285c54603..15c95aaa484c 100644
@@ -624,7 +624,7 @@ static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
 static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
 				 unsigned int flags)
 {
-	rt_mutex_lock(&adapter->bus_lock);
+	rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
 }
 
 /**
drivers/i2c/i2c-mux.c +2 -2
index 300ab4b672e4..29646aa6132e 100644
@@ -144,7 +144,7 @@ static void i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
 	struct i2c_mux_priv *priv = adapter->algo_data;
 	struct i2c_adapter *parent = priv->muxc->parent;
 
-	rt_mutex_lock(&parent->mux_lock);
+	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
 	if (!(flags & I2C_LOCK_ROOT_ADAPTER))
 		return;
 	i2c_lock_bus(parent, flags);
@@ -181,7 +181,7 @@ static void i2c_parent_lock_bus(struct i2c_adapter *adapter,
 	struct i2c_mux_priv *priv = adapter->algo_data;
 	struct i2c_adapter *parent = priv->muxc->parent;
 
-	rt_mutex_lock(&parent->mux_lock);
+	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
 	i2c_lock_bus(parent, flags);
 }
 
#8 lore raw

Re: [tip:locking/urgent] i2c/mux, locking/core: Annotate the nested rt_mutex usage

Quoted 10 lines · Peter Rosin
Quoted 8 lines · tip-bot for Peter Rosin
Commit-ID:  7b94ea50514d1a0dc94f02723b603c27bc0ea597
Gitweb:     https://git.kernel.org/tip/7b94ea50514d1a0dc94f02723b603c27bc0ea597
Author:     Peter Rosin <[email protected]>
AuthorDate: Fri, 20 Jul 2018 10:39:14 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 25 Jul 2018 11:22:20 +0200

i2c/mux, locking/core: Annotate the nested rt_mutex usage

Quoted 8 lines

Hi!

I'm a bit curious as to why the subject line was changed on this patch?

(it was "[PATCH v4 2/2] i2c: mux: annotate the nested rt_mutex usage")

I thought the subject as I wrote it was just perfect. Was it so bad that it had to be edited?

It wasn't "bad", I improved it to signal that it has a new locking API dependency, in particular that's it's dependent on this commit:

62cedf3e60af: locking/rtmutex: Allow specifying a subclass for nested locking

This also clarified it why this i2c commit is in locking/urgent.

Thanks,

Ingo