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=1796261746; b=qNgrWrAgL8OHOu+89PjBnpPdFhzH8IrGWKT9E6/pMux3knVfdstK5z8wjfZhmVpvCEW+ 0VcZmIGJ2b9r27RzRKZEIEZYpokpCESgp4nqX8nXx8mFL+RNwYciAw2Mz8MZpmXP+oF1F V7BT46z2Oxd9vpDMMqHMcvFaXWcH++UPUR3uIBw9X4OVOQ4d+jAOd7IRCcXl7Q5sZPUQ3 MLlZ6JWz+YrL6gbOsbs7cFKoy8ax7ACW2a36xtPK78QVFs/bDPh4OS8lOh3eazt+udnod NSTsK2JJ/gEWKuCo/l5qMqEV5cDQRTZGyYISGc2RZJtDIjV4K2XQXzhD1n4khhK/ujQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796261746; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer; bh=YsrkcpGpZ2+Xv5X1tqRVFPg8fU1rYQ5MnV820cpaEl4=; b=W4i+ItG50q3AGTT1AyJnstHZICSjsdpCNHjQacuDbYcOp5nFrlnzCHuPHS4fcvimfCzB Ckd0eA6q2SKZ98g38pOYTLGRZfmcupbJe7x675QKscUXeCtNkWvpcmetpDE+LkAlci4si VixhPf8WvI9c/4vZb8puAxzrh+5Kgk+Kgsnqi/CiU2DtxrxGTR7lj9+bXL+dlXdRzSdbm IYvXtct/S+b0r1nuQr97IGwMT4L5cYe91MgMMHUv3pc/nw7VgCKaRt5TiWWeGMCZ0fx+6 02+T4ZEQxlZLArSavU+mKKDX7ykVLf76WQ1+i5CgfETYVm/rt8p+MmjEVfrsgQUo3pw== 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=Cf2oKsQu; 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 AEAB3606A32F for ; Thu, 3 Dec 2026 01:35:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AEAB3606A32F 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=Cf2oKsQu Received: by mail-dy1-f176.google.com with SMTP id 5a478bee46e88-385c497bf94so3660986eec.1 for ; Wed, 02 Dec 2026 17:35:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796261745; x=1796866545; darn=lists.linux.dev; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=YsrkcpGpZ2+Xv5X1tqRVFPg8fU1rYQ5MnV820cpaEl4=; b=Cf2oKsQuN6VXa/bIVaDp8UiPLwiPDZVZw8HTr8Tq4lrUZnTlDOxf+89P7uD0D4kqy6 XXFlT5vAWyblloj/1kLp2OpTfYtVU2CIuF72a8h0V1FR6UH/u7cN2Pz5xr94zeFXFLNS rD1FjHeZfJZRgij0/PLlytPmqbUBCACo7QUcHoNfvkz1Y8jX3ogLXnm9VoUub2sFbYvk ArZ2Yn+gknYAOr9roi97EdRs1iBLyZaFBaAjs03UVv9OJECnyK92cu2rRMZz6XUgMyGf I5vG7Rb4vuc3+zmOSg0AoCf/ui0/nnRE7JJYiMsp78b7e6itjfuobziLwSRZpun/T117 IfuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796261745; x=1796866545; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=YsrkcpGpZ2+Xv5X1tqRVFPg8fU1rYQ5MnV820cpaEl4=; b=HyxMuSl07Y7mTAnmtrYl+5Bpex6JsFY6jTHMGdvvvZDxr2M2tuq+Lc/JW5mSAHLfvx mYS3iuAfdEtw0A+mzfv81B1GNZ0KP4MO7VADR53D1jraRswzGSnoK+8zYxy7hxtHtb3H JJhtzfh0IP6sarWWBE+GxaTBqphNqtAy/bv4wKPmTnfQeXgB0CFPnOU9gf40AeGRlPle POfvHpkHo0NgZjXfMRKZ0suwXJY9W5NEYxIbyo0V/T/RdNqCkyNgHEPtMEuda0Wsdmkv W/02MyY+Qshu+qg2mPAxdg2SI0+Jhlo1uvHAUfk6oDpeAaqs25SrKJSoXvKwDcvJTUMG VLPQ== X-Forwarded-Encrypted: i=1; AL+hErH/USNxx6gNWPdC23EiAOQzKhEOm6u+ObFGwSkuE4IqSBb7wtH7d9fN/ShW1nTK5RWdeXGwfM6wJCbPKw==@lists.linux.dev X-Gm-Message-State: APuU4Z+Ou2ev8gEKoHxTzODoONWRB36pAd7DWBsftAtT9meFf6P5Q51W 7nkWGvNXs06jAGJvhSjZNI4yhke5QaHHXgLD6MX+9aVly7nmh8HyKWNM X-Gm-Gg: AZFdTZeqOK2NYGxb+cxHIAbBo8ImmR9AQ8GK4ZbpUS8pIeoeG0NuSAYhlFsM6c4C6Tu oB7QFDcPDkRo8yKc5FQ63b8Qev4PlO4sQSuSlKJd9YBp8eR+TszbL9pRuwi4uNfkc6vcQXOkze+ MAKUAYGL2fZM4YO7adykrKrWpdWBT4QfIaf3xPVGZ1ShDVZykc0JzSJdz9w2pVgOqdBQXEjch6V z+gA6YvRQe+RoEe8TabIKZb6PXJ/3BuIpsTbYf4ymlBok//RO+BHWyzLVkU00fbXliduoLYHfyu TrYIoKgViLlMKxJyzaExAwPQca/gZO2P2aAxaQs2VIMlep8JiG/03bz5v8UHrrQYXyYLl5YeyAF iXIQa9j6m+HThmLAyscLG+90x7rroHuFwpdcYbMZrrx1gFqEk6lWI7VeRr3QpfHOQ7Xp7XzrKYc I44RmIJV8ho7J4/3t9L06ez14I0CH7tSe1vCkuTlF/6MoAqkOrjkohZCk/NCMZk1Ej5IKqyv7Bv b5Q9x03BjJRuzwgtMFQIIIP1N5d/X76P9N5sXoX+CCpjUb9c3g5kFW1XEmDhvLjv7OYaCpWD7cO 3ctrvbQolQIFOvPZKJrEHmws8oHXOXxdbSMDEbGcBLHHvf59CuK01ztmPRs0CXtNCVaz/qF4Ajl 2jXTd2oh0C+K1anaCokZtOscSEcPcV2Zc+/xHcQ== X-Received: by 2002:a05:693c:87cc:20b0:358:3426:d601 with SMTP id 5a478bee46e88-3856c0d0961mr41617512eec.2.1796261744642; Wed, 02 Dec 2026 17:35:44 -0800 (PST) Received: from dtor-ws.sjc.corp.google.com ([2a00:79e0:2ebe:8:5b9e:6509:7a57:220]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-38566cd46c5sm155432860eec.11.2026.12.02.17.35.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 17:35:44 -0800 (PST) From: Dmitry Torokhov Date: Wed, 02 Dec 2026 17:34:49 -0800 Subject: [PATCH 1/3] driver core: swnode: only release owned secondary software nodes X-Mailing-List: driver-core@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261202-swnode-shared-primary-fix-v1-1-7ff23caaa1ed@gmail.com> References: <20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com> In-Reply-To: <20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich Cc: Johan Hovold , Andy Shevchenko , Daniel Scally , Heikki Krogerus , Sakari Ailus , Bartosz Golaszewski , driver-core@lists.linux.dev, linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org X-Mailer: b4 0.17-dev When a child device shares its parent's primary firmware node carrying a secondary software node, detaching an ACPI-shared primary node via set_primary_fwnode() leaves the child pointing to the parent's secondary node without clearing the shared secondary link. That causes platform_device_release() to drop an extra reference per child (via device_remove_software_node(), or via fwnode_handle_put() even if removal is skipped), leading to underflow and use-after-free. When a non-ACPI shared primary node stays attached through release, device_remove_software_node() unconditionally clears the shared secondary link and drops one of the parent's references, prematurely detaching the parent's software node and leaking the remaining reference and sysfs link. Track secondary software node attachment per device with a new device flag. In set_primary_fwnode(), only retain a shared primary node's secondary node on the child when the child attached it, clearing the primary node's secondary link; otherwise clear the child's firmware node pointer without touching the secondary link. Gate device_remove_software_node() on the same flag before notifying or detaching, and gate managed node release in software_node_notify_remove() so calling device_remove_software_node() on a registered managed device (such as during i2c_unregister_device() or spi_unregister_device()) does not double-put. Fixes: 99aed9227073 ("device property: Don't clear secondary pointer for shared primary firmware node") Fixes: 7b5b51b2b05c ("driver core: platform: count references to all kinds of firmware nodes") Fixes: 7d80509ff0da ("driver core: platform: unify release path") Cc: stable@vger.kernel.org Signed-off-by: Dmitry Torokhov --- drivers/base/core.c | 15 +++++++++++---- drivers/base/swnode.c | 14 ++++++++++---- include/linux/device.h | 7 +++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 44f79f427b6b..673dc5941019 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5250,12 +5250,19 @@ void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) dev->fwnode = fwnode; } else { if (fwnode_is_primary(fn)) { - dev->fwnode = fn->secondary; - - /* Skip nullifying fn->secondary if the primary is shared */ - if (parent && fn == parent->fwnode) + /* + * If the primary fwnode is shared with the parent and + * this device did not attach the secondary node, clear + * dev->fwnode without leaving it pointing to the + * parent's secondary node or modifying fn->secondary. + */ + if (parent && fn == parent->fwnode && + !dev_swnode_added(dev)) { + dev->fwnode = NULL; return; + } + dev->fwnode = fn->secondary; /* Set fn->secondary = NULL, so fn remains the primary fwnode */ fn->secondary = NULL; } else { diff --git a/drivers/base/swnode.c b/drivers/base/swnode.c index bc51a92adcb8..da36f91f6434 100644 --- a/drivers/base/swnode.c +++ b/drivers/base/swnode.c @@ -1073,6 +1073,7 @@ int device_add_software_node(struct device *dev, const struct software_node *nod } set_secondary_fwnode(dev, &swnode->fwnode); + dev_set_swnode_added(dev); /* * If the device has been fully registered by the time this function is @@ -1088,11 +1089,12 @@ int device_add_software_node(struct device *dev, const struct software_node *nod EXPORT_SYMBOL_GPL(device_add_software_node); /** - * device_remove_software_node - Remove device's software node + * device_remove_software_node - Remove device's secondary software node * @dev: The device with the software node. * - * Remove a software node added by device_add_software_node() and - * deregister it if this is the last reference. + * Remove a secondary software node attached by device_add_software_node() + * or device_create_managed_software_node() and deregister it if this is + * the last reference. */ void device_remove_software_node(struct device *dev) { @@ -1105,6 +1107,9 @@ void device_remove_software_node(struct device *dev) if (fwnode_is_primary(&swnode->fwnode)) return; + if (!dev_test_and_clear_swnode_added(dev)) + return; + if (device_is_registered(dev)) software_node_notify_remove(dev); @@ -1145,6 +1150,7 @@ int device_create_managed_software_node(struct device *dev, to_swnode(fwnode)->owner = dev; set_secondary_fwnode(dev, fwnode); + dev_set_swnode_added(dev); if (device_is_registered(dev)) software_node_notify(dev); @@ -1208,7 +1214,7 @@ void software_node_notify_remove(struct device *dev) if (dev_test_and_clear_swnode_notified(dev)) swnode_put(swnode); - if (swnode->owner == dev) { + if (swnode->owner == dev && dev_test_and_clear_swnode_added(dev)) { set_secondary_fwnode(dev, NULL); swnode_put(swnode); } diff --git a/include/linux/device.h b/include/linux/device.h index 6b082c638d45..bdad371b177a 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -608,6 +608,11 @@ struct device_physical_location { * device and took a reference to the device's software node. * Indicates to software_node_notify_remove() that it should drop * the reference. + * @DEV_FLAG_SWNODE_ADDED: Set if a secondary software node was attached to + * this device via device_add_software_node() or + * device_create_managed_software_node(). Indicates that this + * device owns the attach reference and is allowed to detach and + * drop it. * @DEV_FLAG_COUNT: Number of defined struct_device_flags. */ enum struct_device_flags { @@ -622,6 +627,7 @@ enum struct_device_flags { DEV_FLAG_OFFLINE_DISABLED = 8, DEV_FLAG_OFFLINE = 9, DEV_FLAG_SWNODE_NOTIFIED = 10, + DEV_FLAG_SWNODE_ADDED = 11, DEV_FLAG_COUNT }; @@ -844,6 +850,7 @@ __create_dev_flag_accessors(of_node_reused, DEV_FLAG_OF_NODE_REUSED); __create_dev_flag_accessors(offline_disabled, DEV_FLAG_OFFLINE_DISABLED); __create_dev_flag_accessors(offline, DEV_FLAG_OFFLINE); __create_dev_flag_accessors(swnode_notified, DEV_FLAG_SWNODE_NOTIFIED); +__create_dev_flag_accessors(swnode_added, DEV_FLAG_SWNODE_ADDED); #undef __create_dev_flag_accessors -- 2.56.1.915.g69d5ec7298-goog