Linux USB
 help / color / mirror / Atom feed
From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: "Bjørn Mork" <bjorn@mork.no>,
	"Andrew Lunn" <andrew+netdev@lunn.ch>,
	"David S . Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>
Cc: netdev@vger.kernel.org, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Praneeth Balasubramanian <praneethb2002@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH net] net: usb: qmi_wwan: unlink mux devices outside the RCU read-side section
Date: Fri,  4 Dec 2026 14:24:19 -0800	[thread overview]
Message-ID: <20261204222419.1869694-1-praneethb2002@gmail.com> (raw)

qmi_wwan_disconnect() walks the upper devices of the QMAP parent under
rcu_read_lock() and calls qmimux_unregister_device() for each of them.
That calls netdev_upper_dev_unlink(), which can sleep. It removes the
adjacency sysfs links and runs the NETDEV_CHANGEUPPER notifiers, and
some of them take rwsems. With CONFIG_DEBUG_ATOMIC_SLEEP, disconnecting
a modem that has a mux device gives:

  BUG: sleeping function called from invalid context at kernel/locking/rwsem.c:1573
  ...
  RCU nest depth: 1, expected: 0
  ...
  Call Trace:
   __might_resched
   down_read
   kernfs_remove_by_name_ns
   __netdev_adjacent_dev_remove
   __netdev_upper_dev_unlink
   netdev_upper_dev_unlink
   qmi_wwan_disconnect
   usb_unbind_interface

Lockdep also reports "Invalid wait context" for the same path.

The unlink first frees, with kfree_rcu(), the adjacency entry that the
iterator is standing on. Then it sleeps again, to remove the reverse link
and to run the notifiers. Without preemptible RCU, a sleep there ends the
read-side critical section. The entry can then be freed before the
iterator reads its next pointer.

Commit a8fdde1cb830 ("qmi_wwan: avoid RCU stalls on device disconnect
when in QMAP mode") moved the unregistration out of the RCU section,
but the unlink stayed inside.

Only queue the mux devices for unregistration during the walk, which
does not sleep. Unlink them after rcu_read_unlock(), still under RTNL,
and then unregister them as before. del_mux_store() still unlinks and
unregisters a single device.

This was found while reviewing the callers of
netdev_for_each_upper_dev_rcu(), as part of a study of the locking
rules in kernel comments. It was triggered with a QMI device emulated
through dummy_hcd and raw-gadget. With this patch the same test
disconnects without a warning.

Fixes: c6adf77953bc ("net: usb: qmi_wwan: add qmap mux protocol support")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found as part of research at UCSB SecLab on locking rules in
    kernel comments. A runtime check of the kernel-doc rule of
    netdev_upper_get_next_dev_rcu() led to a review of the callers of
    netdev_for_each_upper_dev_rcu(), and this one sleeps inside the walk. An
    LLM was used to turn the comment into the check. The patch and
    changelog were drafted with Claude Opus 5.5. I reviewed the patch and
    take responsibility for it.
    
    Testing on next-20261204, x86_64, in QEMU with PROVE_LOCKING and
    DEBUG_ATOMIC_SLEEP (PREEMPT_RCU=y). I do not have a QMI modem. The
    device was emulated with dummy_hcd and raw-gadget (a syzkaller program).
    The test runs twice per boot. Each run adds mux ids 1, 2 and 3, deletes 2
    through del_mux, and then disconnects the device.
    - Without the patch, every disconnect gives the warning above, and
      lockdep reports "Invalid wait context".
    - With the patch there is no warning. del_mux removes qmimux1, and the
      disconnect removes qmimux0, qmimux2 and wwan0.
    - Builds without warnings with W=1 (x86_64 allmodconfig, clang 23).
    
    The use-after-free in the walk, without preemptible RCU, follows from
    the code. I did not trigger it.
    
    One small change in del_mux_store(): it now drops the real_dev
    reference just before unregistering the mux device instead of just
    after. RTNL is held across both.

 drivers/net/usb/qmi_wwan.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/net/usb/qmi_wwan.c b/drivers/net/usb/qmi_wwan.c
index e9873dc1d531..3ca908885c9a 100644
--- a/drivers/net/usb/qmi_wwan.c
+++ b/drivers/net/usb/qmi_wwan.c
@@ -290,19 +290,23 @@ static int qmimux_register_device(struct net_device *real_dev, u8 mux_id)
 	return err;
 }
 
-static void qmimux_unregister_device(struct net_device *dev,
-				     struct list_head *head)
+static void qmimux_unlink_device(struct net_device *dev)
 {
 	struct qmimux_priv *priv = netdev_priv(dev);
 	struct net_device *real_dev = priv->real_dev;
 
 	netdev_upper_dev_unlink(real_dev, dev);
-	unregister_netdevice_queue(dev, head);
 
 	/* Get rid of the reference to real_dev */
 	dev_put(real_dev);
 }
 
+static void qmimux_unregister_device(struct net_device *dev)
+{
+	qmimux_unlink_device(dev);
+	unregister_netdevice(dev);
+}
+
 static void qmi_wwan_netdev_setup(struct net_device *net)
 {
 	struct usbnet *dev = netdev_priv(net);
@@ -464,7 +468,7 @@ static ssize_t del_mux_store(struct device *d,  struct device_attribute *attr, c
 		ret = -EINVAL;
 		goto err;
 	}
-	qmimux_unregister_device(del_dev, NULL);
+	qmimux_unregister_device(del_dev);
 
 	if (!qmimux_has_slaves(dev))
 		info->flags &= ~QMI_WWAN_FLAG_MUX;
@@ -1598,10 +1602,13 @@ static void qmi_wwan_disconnect(struct usb_interface *intf)
 			restart_syscall();
 			return;
 		}
+		/* Unlinking can sleep, so only collect the devices under RCU */
 		rcu_read_lock();
 		netdev_for_each_upper_dev_rcu(dev->net, ldev, iter)
-			qmimux_unregister_device(ldev, &list);
+			unregister_netdevice_queue(ldev, &list);
 		rcu_read_unlock();
+		list_for_each_entry(ldev, &list, unreg_list)
+			qmimux_unlink_device(ldev);
 		unregister_netdevice_many(&list);
 		rtnl_unlock();
 		info->flags &= ~QMI_WWAN_FLAG_MUX;
-- 
2.25.1


                 reply	other threads:[~2026-12-04 22:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204222419.1869694-1-praneethb2002@gmail.com \
    --to=praneethb2002@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=bjorn@mork.no \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox