From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.176 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796423068; b=j4Abmgq0njHxoLykrFidJFxt+Y3+blTMmdfMtUd+kO5gcuRwCzKWn6IBFFgpky+ZA7j0 d7MJhSti3IKKdPYjur2NZyFxTWiTYlcWNTOjpHA0/qYqzUsv+F9uXGEChXrhzEjgJ7nBS ido5M7VXeCmUtsZGqEFj3qEliysIgyEMsK8SXdh+M7Tr8JsDrj2MNjA8/eUgAUp3yU9nc gmOviVFtR70iFQyoOyztkvDV6Y25uITS4lceaHV6194BoSm/ls5rrVFoPGZWEqMf1U4Ck CUvXc4hcyDYlahq/Kux0FTu8upwzt7L8muwgRgwsbGd+for2hq5TC52WszIb46EfRPQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796423068; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=O1XmvJBNMIbqiag5EsA8gFwOuOPBzZhBQa5/wbpmtVc=; b=2EA1vk6SbOXuAY+U7XMp/Rhvp3oJyYaFFqcNm1V7Jcwnqugcw6fBNiNA/QF6VOeXVLm5 6v5lG360Gdsm5kjnDTd/W6ScCJ/+35w6SxusjCjmCP/HL8WtfWL1tYkWnPLgvW8cvE9c9 g6x54p7OQ55qSxXvoknNe3RgC4ColBC3So14NESa/wn/KA0DYrMkte/Zl5PGQhvgbCFhe SsMo3+Yh71KS7wp48xERlThHQQLb0anRKzbSO0NF1r3Pp0nnjJJl6AefK5zgOQLMlWKbI 7ESkhkseXR3ZIq1c3djb3OK/TFbbni+q21o3mACgWygRHTca8tTUTEakWYnoVWVTtGA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=aAXRiOa+; arc=none smtp.remote-ip=74.125.82.176 Received: from mail-dy1-f176.google.com (mail-dy1-f176.google.com [74.125.82.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBAC7606A32C for ; Fri, 4 Dec 2026 22:24:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CBAC7606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=aAXRiOa+ Received: by mail-dy1-f176.google.com with SMTP id 5a478bee46e88-33bf40b0c8bso3232964eec.0 for ; Fri, 04 Dec 2026 14:24:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796423067; x=1797027867; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=O1XmvJBNMIbqiag5EsA8gFwOuOPBzZhBQa5/wbpmtVc=; b=aAXRiOa+Nc/QTgolw9KHwOXUlKMa5AKcopnHDbsc8+WowbKgS63SqVgLlHs0vgknts 3OMXanFUU4mkWJKrKa+4xX4zgBYICw7qJpbzzh4jHGH6lhr8Fogm9ce7sT+Zu7guY3LV hsGrlhqSE7I/rStMTPGDfYODeqK0XGR/QwvzB8xgSGP/bHvrdHRGi/pMKhzAi0jzG+W1 pSTnC1GcqtEK1bF2H+2XUxAZZMK4pZMuwrEwXi1otpdBTibSUCkQJsikcpy+nay5S71z Ne2UWf6tAEkPU3sthHhlL7zKC4S6MzNSxrJZE1aNWY8PhM3EbcpV2R5M73h3mdBEx/lZ T3Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796423067; x=1797027867; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=O1XmvJBNMIbqiag5EsA8gFwOuOPBzZhBQa5/wbpmtVc=; b=PwUmgr8YEpDlz34nVnwv0q4Uhb20tH1SCaRubENeP2jZm0k7+Sl2Kh3fPARGq0iS2Q TDE1ilFZbftuGGbf/6V6o93lFnSzv0wfzEAG+7yG6Wr9LPdZuiKaOl7+cK6EkBZkJs0C i8RYuwBrU7UbqnpMe/6tInDiK3tyfNbRMwzP/97SSxaKlFkuuvqf4cP/6xUNBToqpU5L ruVIJ9bkxT2I5hId1AUBKMblAafU5X71THvd6zz/CRBRh4gq0vcYs0feD+agyWAYIVDb HOJ37FzsS01eFVT8HPXOppHsR6wAKl+fCf7YYmZn0s2lyVpOBAOpe2gcqfbO5NdB3xhF aElw== X-Forwarded-Encrypted: i=1; AL+hErFel+n901v1tP9PnWJVDebm02UxjyMXMUAT6k2mmybZhtnJxRy+MiPZP4yf/UIXfSmI53Ffuso=@vger.kernel.org X-Gm-Message-State: AB4q+uHDpdwB0UQVIDC3Y6t2gDL5V7yMeVVbQmDJ2bm4/AktXYnqCms4 m7J772Tibc5MeJRpf5vcBTuCCcnn7K+k4Rei6yLqip0bZ2QmBKpuXXyT X-Gm-Gg: AZFdTZcB7yV/FQCrqpKxfPrw5KreqkpZ/qT/eWRZjACg0d1mkbxAL10tMPcfsoIkw5V fiD1Y/+V55LuuAzoVdFD5ij9uDYnAk4GmYt1yo+Hjxz0STNhgiw53CFK+1DCzvk3sOewF5ufcOc rMFTLGjBIp79gXTwIXiQ5LAkkDO3ZE6435kFL+22xnkV4XWcd5a29vCTCaTQqGxs1ijuNCV+Xie h2WDZn4hcMMk4+jVTR4ANXOsvLrfs/2qZZjdNcDiIA5Z1+0F000rU+e3UT083Iq7M0te9dwjOz8 zPUMwX8msz6obFSvhv0uJodwqAAf1TBdhXoSAO4cPU/qNuD6Dj7/ZCP2Ppt6VwYZ/g9XNdaEWQu FCV4se2oTn2ozAtNyW5U71gvzw1vzW6sbWVBe9kWGmDbyin0BVOa3J1kFw+JoxLF5DW+22iSQWj YexqbkFqJzU3fSuCXc4aMJ2G5PBHqAHlFAqdqzIjgC8vg6cYrW6pgA7r0MHyaZjsWBaAU/6rLZK ad+JhcxjgprIP6B+QPBZKp+kOl6/ZPKyJMIUFBm6v0dv7hHPDy2g3V4M0jjZ/x8P9AAfu0EjDS6 FIiD0Gtg8J5JkJ7Vz5fGhIKLjmTUxTn6HF1R3pOYwOqQ+wT5HyosIZcoTKyj77YfI7rDbh6ldd0 a8mUPCNQBHb9G X-Received: by 2002:a05:7300:cf0f:b0:386:abe8:32b0 with SMTP id 5a478bee46e88-387520d05d0mr3301737eec.8.1796423066642; Fri, 04 Dec 2026 14:24:26 -0800 (PST) Received: from localhost.localdomain (wood.seclab.cs.ucsb.edu. [128.111.49.37]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521e5c89sm17016640eec.18.2026.12.04.14.24.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 14:24:26 -0800 (PST) From: Praneeth Balasubramanian To: =?UTF-8?q?Bj=C3=B8rn=20Mork?= , Andrew Lunn , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: netdev@vger.kernel.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Praneeth Balasubramanian , 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 Message-Id: <20261204222419.1869694-1-praneethb2002@gmail.com> X-Mailer: git-send-email 2.25.1 X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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