From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796518699; b=N/ROoKWd5gSPAZH7I6pu3PG1B9HrnVZKxjkK1pv2LnWQ7BEHCyhXv6jiC+hlw1KoG6xd Nvue18UMRfbe0kxeS77YQCcdKWySKB4qLfAnPrgaPmj4nPCgcirZBV3iW3FJbDVix8E/w g23F690b8sDRIUzMXiOmgCjSoxd/hRe7X3P5jDtymkt50FcnuWlSPgPYTEZUkXKCkKIrP Pxrmul+u2FCxwpwybuO4RZ8Wp82H6kHJDbAkw5hqpKv1o8L+438yUwvpiKdg2CyZhjxZw aiObEovCZrb7BnaK19B/7PrVXC6xKvx/MLHjTK3DAeLiH2iITfXWgbtm4kg1feMSOcA== 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=qAhtiSSRq3wmph5avDYFMcme85d56gkFQI95skPjDMVbqGk2KxX94K0bqXrD7u0kwVew Dxjs//kTI1rvZh4eOLUT4ChTZALrUxVZTuGUPWNhroEjSXrPn5pimpDxgTeCuQXzif6bh JyT+9UePNwO8J05zQkx1/VkPMSf0zBnX4yu8590v16aa47lva0GfzvgGFHZ9dIPf8iUSJ Zs6SbV78k2QzP+lNmc6dQwC3Sals6js5s2NuQJonEhdWfft0Y37cC05axag60koSLWDPB NNya265/VQX1omW2yXadkyLp8h6W/g9utTloAwWScQAqTyt5Xho6wULUtMVORRO6OZw== 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.45 Received: from mail-dl1-f45.google.com (mail-dl1-f45.google.com [74.125.82.45]) (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 66B12606A31F for ; Sun, 6 Dec 2026 00:58:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 66B12606A31F 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-f45.google.com with SMTP id a92af1059eb24-1b5d5f0ba39so2503999c88.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=LlAtz7BOSBt++MT96ub7Kv4PkeTZAop6ERcbkcSLvM/WwTMe3wuWqleeNgvv/aQ1HU nPB0QtvKGJLGzLizwqmtIVM+qBAZJk6pOV7ZLf3td3nCQ9oBTFcs20A4h0e0OlnVb6Ot WR+RxaOFrs3tkO++r+fOg9OQhh2wLp3oPNG99eEO3w+E1GEp3PHo1PqGoAFHw2py5qZA popmx2sRqJAE7T56arGXxOtcqAwNY5vbpx69JH1paMUjEPXp2lj6tFw7kCmvhY7ysH95 NRQUBPZO1KLZzgBIj8DI0Sy2Pz+FlTFSdhjY2Ud034H/IxNESl13/w4n9F9KlgHdLszS 5NYQ== X-Forwarded-Encrypted: i=1; AL+hErGLjuAlJ4QO71IV738P87w6MfQpUS/FBKHXJOgcbZuR2uoCUhTYgstEl53opbOw9Cd8zh2JK0ZH/kNm@vger.kernel.org X-Gm-Message-State: APuU4Z/g5W6cHo+0KCIr5VQD4BFB2QTLH//oC3Ldv0n32QyUjSSgj/d9 SLUVojpnXkollXmCwuu+iF3VYEFE0b4798Jo87d7Wu8/+mNAk1lRLtvj X-Gm-Gg: AZFdTZeNfGGfaDS3M2sBxMGCacYZPqZtgt1DDmPSMh9fiUYRmmb3MNjI3tFqYJAR0Jj HC5qRVWoVNdCd5EKoXaUH0R4LgrdxXOJZwBz6UoO50OOIzBWVB+gGbwFfSCfCKfuTQItcsQUk/M jzjTeWY1BU8n01Uv2co9nl2cW+Errij5w2skEHhqDeYjLKSb9RF4Q/uRuDl96eAv43bCZsE5GSS diLbUk/ugVWa0u3heDrz5BH8SxxG9XvZbkGbjEZAAUPrqYetoC9xkedvo0nWwS7i/CZJMUEBoDq 2vv004HjEd28tiZvwCtO2I/bl7Ks0rj/bvC+kP5trgx8YX+WoRW9xVbmn8C/vwgVk0BUpr//L7f KEivrI9nL4t3tGE2qQDlKPTdMfIP6rNlwHZWQaG0QHOnYi8eCDZOdwgAm/oY0Fg6hdgQ55xlgyz eBVWhso3nP0eeaxsfjX8yT9wQ5AqsxtbMkxwLEM1VhjSmrF2cJfqnM/ZwWmNXcVZrAkBGh0C2yY S4n9BGMcRRmJkOHuSXEVibAzbfnZ/BpUgmo1+XFgsu904uuAGuyyVVOJWz4rQbCwZLxMGQobBi5 h+GqX7sNDn3v7TMSmwy/ 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-acpi@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