From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796518699; b=A3ESTjAdYx7tDX0Muetrxy7zy2Xd08tA5B7gaAddEMsf4fhcRljCT5GhNiV8ovAdd1Ot pwCWi8yX3rnkPIHs/lOvyWwoDIHiNQoAkDJxKFo59LFRTCXGepDC17JjPrCPo1HkQhbRJ QzDgpCNYxIxw9zs1EMqTmNEMSL7eY8zyptnKY2GfTybRZGRo/KTKvx5M5L8Ma/k2iPCK8 O63AHgVXI7N0z1ZlEgORrs56De7s1+nnSHjcAfC1iTK5alKYumtZi/4ZDlmwjNrxnHs19 d8XlZR4/gRz4m3Xi3dkDbOtB/6KzMhnzi8w5Z6KsEW0a1umCzv4/Y1twEr6+3EdUokQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796518699; 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=nKAFCVUj25vr4LNM91fNEUof4E8h3KDKn1/TWnlJV3g=; b=1oENwCXga+oezE7WZJQ0YczwaiVSSoLws7WV2liR9Eog/mljYJQA0px01h6oGp67zo3N kmSDE/ZW/TcN4KmWCLMg+uJLHjvUhjDCqJJ8NtpmAH1Yf+2CgZ0EjB+8u01j6eYj7h/UB pWzXDJ/IoFOEIfbTvANx9/owGFlAXER2GL620jerPiJWtf3VwzDM70wcjPLIu6lWCkNLw ssZzDBEzZGWY4NLUpNn0cjQzzeX3LOa7C6qfKD0ZxexnJbV5mCg326SQxPIyID1krmzb6 KEdg7g/rEUK8dhRYe2eTxJN0H3aKKyO9L+GJX6wmXwnFoXFqhjBjS5XZ8Z2MiJeWW8Q== 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=jqDChZeC; arc=none smtp.remote-ip=74.125.82.44 Received: from mail-dl1-f44.google.com (mail-dl1-f44.google.com [74.125.82.44]) (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 71296606A322 for ; Sun, 6 Dec 2026 00:58:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 71296606A322 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=jqDChZeC Received: by mail-dl1-f44.google.com with SMTP id a92af1059eb24-1b5d5f0ba39so2503998c88.0 for ; Sat, 05 Dec 2026 16:58:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796518697; x=1797123497; 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=nKAFCVUj25vr4LNM91fNEUof4E8h3KDKn1/TWnlJV3g=; b=jqDChZeCuzmxXv+if/m4uZgzjiFa/tMo/5KzsMWfVEVFg77q3AjnEa2B9DO3SUp08r QHrdlLgwdO+7TKNu6MeHYJNokzGCqL/CJk1MmIj0I/3rsdJeYy/ouA0D43DObhBHQBCb fRue4bssowxY9JWm3soxnaQ51g/AUDWjZO15xiFEJB8vBBZw7MB4lGzlRbT2792WyKCX uxOYCgYXy1FwR2WD0DpAHfwah6HMkqnp5VYY5hrtzOnvPZOlfXWGR0Md4SNHm2Yol+RO LPCGunRn5mDIPIms3m+SpDOvevynaEzCjSLSN8ZwZBtUaheg/j7nUKGCawQAIDLXkzoP 2Niw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796518697; x=1797123497; 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=nKAFCVUj25vr4LNM91fNEUof4E8h3KDKn1/TWnlJV3g=; b=D0EDHBEHRrWU7xx3eOGy+iCSjmRt6MVf63YWJTHQKQm3XEJMmaLO+aJvh4VbOIdTlQ 1f/E+du/xyC0t6TA3NVKYKKrbjWgO78K42Zxd5cuhBJb9qRllgJWLKDrPgVSC22BMRYf nOyLCtI2aCw42Dl658AqzuCaVSDvU8qRQWoej/aoAnjBpR2dJ7JJ20VND5Ug3CzY+7im 8QD7U0paNNgVyxhzuOVwmp9o0Ny6Du9RyTcJbpOS73CPcNRoBPqCbyvQ38lxjjI9VsC/ IHrNQ64TVRdoWizqe8EglkKKwwt5pfhxG2X6qFWTXgM4ZOMNGrbIdQH74MlKhLQ7A/kU PBnw== X-Forwarded-Encrypted: i=1; AL+hErHdeyX3fPwNsWT4q+y01UFg7d6darjWA5LWBMT+Nax8Is9AfiBfv6qOwzBuufN5Hnjor8Scq5aU2mhp3cY=@vger.kernel.org X-Gm-Message-State: APuU4Z+y8PfKAERvp2D0YI/YjjEHggA5iRi4phxntMqtqqu1SzJ26RmX 1f+vXysDc+aNgpU0aelPvBtXe9OVS33eIM9fC1GnNAXZnrsefNX2qjyIK+RinTDb X-Gm-Gg: AZFdTZdJ+6iZ2i4XtW+IaMpoGz3hNcWlmZFLTdBnklnEBoChq+SYuLxk/GLb0pFI4Mh TpmpC++XCUCsXJzUCVc8+1yPyPL0ilGlcRim22f5lPMXqlHs8UkzsvDj2zUCiCEcvvG1tz145ux pPM9BW7tP9W3qrwE7xo39hEL4FrdAlB72m4JdwwnCxIcoVkMz/HoebAd1Gf3Af5+XRfLnmfUD+W kR206wMxGsgACa1fSr1IljmKfzzpHj2i1QSt+VPy0yGMERrwwHXciyLgENKR0PY+X7eYoQ7+NKT vIB9vJSuEjhyIgutaTN0OoSu1Go+EMFtJMGsZETP5tPbAHAhxgMODokBFII0IEBhDjMQ21BZyzg xhsRT/Qchc4Jm6XXtkL3dPBqkW111kOCyMa5xzpz+NHHx581V0T2GQGijfitlg9PpwPCj5cozFn swLZuJBpUVxsNBpD5OJoxZ/r7dGnZxLwoA2njEgFAZEY8MdVN8v6WKTrm28Ce3xxH4fXx5LI4sK hFYO848zFUZO+j1qsHmqtLO/NGUoSjGQ2ovQz5hORwm27im0RjbXizkL0L8Kt6erdE8FEVVLLiN Op3VVvXRMw/0M6B/KfLz X-Received: by 2002:a05:7022:5f0a:b0:1b5:330f:6209 with SMTP id a92af1059eb24-1b5d70e0ffdmr5394301c88.20.1796518697475; Sat, 05 Dec 2026 16:58:17 -0800 (PST) Received: from cachybull ([2a09:bac5:49f8:372d::57f:56]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d646ce0fsm19367296c88.12.2026.12.05.16.58.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 16:58:16 -0800 (PST) From: Cesar Cibrian To: Lee Jones , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J . Wysocki" , Johan Hovold Cc: Dmitry Torokhov , Andy Shevchenko , Heikki Krogerus , Bartosz Golaszewski , Sakari Ailus , Daniel Scally , mfd@lists.linux.dev, driver-core@lists.linux.dev, linux-i2c@vger.kernel.org, linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] mfd: core: Remove software node after unregistering platform device Date: Sat, 5 Dec 2026 16:57:55 -0800 Message-ID: <20261206005755.27703-1-cesarcibrian.ai@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-kernel@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 mfd_add_device() attaches cell->swnode before calling platform_device_add(), but mfd_remove_devices_fn() calls device_remove_software_node() before platform_device_unregister(). If the child driver propagates dev_fwnode() to a sub-device (for example, i2c-designware sharing its fwnode with the i2c_adapter device on Intel LPSS), removing the software node first clears fwnode->secondary while the sub-device is still registered. When platform_device_unregister() subsequently tears down the child, software_node_notify_remove() sees dev_to_swnode() == NULL on the sub-device and skips cleanup, leaking a swnode reference and leaving a stale sysfs symlink that triggers a warning on rebind: sysfs: cannot create duplicate filename '/kernel/software_nodes/node0/i2c-0' Move device_remove_software_node() after platform_device_unregister() so the secondary software node link stays intact until the child driver and any sub-devices have been torn down, while still detaching cell->swnode synchronously before mfd_remove_devices() returns rather than deferring to platform_device_release(). With DEV_FLAG_SWNODE_ADDED, set_primary_fwnode(dev, NULL) called from acpi_unbind_one() moves the secondary node attached by this device onto dev->fwnode and clears the shared fn->secondary, so the subsequent device_remove_software_node() drops the reference taken in mfd_add_device(). Without ACPI, dev->fwnode is not detached on unbind, so device_remove_software_node() clears the secondary node directly. This change strictly depends on "driver core: swnode: only release owned secondary software nodes"; without it, reordering leaves a shared ACPI primary node with a dangling fn->secondary (use-after-free on rebind). Fixes: 42e59982917a ("mfd: core: Add support for software nodes") Signed-off-by: Cesar Cibrian --- Danilo offered to take this patch through the driver-core tree on top of Dmitry's series if Lee is fine with providing an Acked-by [2]. Changes in v2: - Rebased on top of Dmitry Torokhov's series "driver core: swnode: fix release with shared primary fwnode" [1], which tracks secondary software node ownership via DEV_FLAG_SWNODE_ADDED and handles detaching shared primary fwnodes automatically in set_primary_fwnode(). - Dropped the shared_fwnode / set_secondary_fwnode(dev->parent, NULL) workaround from v1, which leaked cell->swnode on non-ACPI shared primary fwnodes. - Documented why the reorder is safe with DEV_FLAG_SWNODE_ADDED and its hard dependency on Dmitry's patch 1/3 in the commit message, and added an inline comment in mfd_remove_devices_fn(). - Tested on Lenovo IdeaPad Slim 5 16IRL8 (Intel LPSS / i2c-designware, ELAN06FA:00 touchpad: 10 intel-lpss + 3 i2c_designware unbind/bind cycles) and KUnit (platform-device-swnode, property-entry, software-node-links). v1: https://lore.kernel.org/r/20261204181605.22364-1-cesarcibrian.ai@gmail.com [1] https://lore.kernel.org/r/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com [2] https://lore.kernel.org/r/DND6CYGDKSSC.1ZXTPX17DK3GF@kernel.org drivers/mfd/mfd-core.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/mfd/mfd-core.c b/drivers/mfd/mfd-core.c index 16ad4fd26..c425efce7 100644 --- a/drivers/mfd/mfd-core.c +++ b/drivers/mfd/mfd-core.c @@ -368,9 +368,6 @@ static int mfd_remove_devices_fn(struct device *dev, void *data) if (level && cell->level > *level) return 0; - if (cell->swnode) - device_remove_software_node(&pdev->dev); - scoped_guard(mutex, &mfd_of_node_mutex) { list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list) if (of_entry->dev == &pdev->dev) { @@ -383,6 +380,14 @@ static int mfd_remove_devices_fn(struct device *dev, void *data) cell->num_parent_supplies); platform_device_unregister(pdev); + + /* + * Sub-devices may share our fwnode; drop the software node only after + * they are gone (device_for_each_child_reverse() holds a ref on dev). + */ + if (cell->swnode) + device_remove_software_node(&pdev->dev); + return 0; } base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8 prerequisite-patch-id: ed0efd312ca226c19c3c6916586712f824a1b169 prerequisite-patch-id: 81bc64d09d9fff08f990ad2644078b100f3ee30d prerequisite-patch-id: 411121a99a0e343557184b4b20615ca0b5ceab02 -- 2.56.1