From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796540878; b=qAANOdJUWm7QpG9nTV9RkZ7fDUVlrgUaRDJtj5HI98fN+VeWzmaHGyA3Loe9tnxYnxFf e5Ue7JErJAYntTipN80MS+gtbTaF6989m2ylwNeIuhC9sNm4MQx3meTLHLmUsdcTxc/VJ 3tfbzW/cHgztF1WaZHuv75eTAnfh7yQdoL08zk9vD+ZoBwS7DL3rXV8V13qn1zHb4rYCO GdvWRe/0u7kB0PVpi185sip8zeINyxZIkU5gPo8jVYTY/XoegPmtGReQHxpVW7DfRgB1r mlNM4hQF3FRg/a/I3+zLUH3zVUN9vtIgN7IH+sMTUnm6r/e5++gqUXJIf++FT2Mc3ZQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796540878; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type:Content-Transfer-Encoding: Content-Disposition; bh=G1lg4L8WuFri6efpvwoH6OtJVXEL5iHPHP50a4q4oOs=; b=GvAWm/x2ICHi3jGiqBnERRhK3dfUdiwKcWK1pAKy/gCaYT9vKWLq3ZuLWAE4Tg9bIHwa 4T4ErUlXj9eiJ07SYyQrFX6p5z5A5T38RGDylxwZ34YkVWjcKVwBHNRWVnvC5QpuzbQsV OeLPNwZ3GOVMXJwoGrgl4ilqpeSLb9X3XFzl6JQ14vvIPhVy5/HvxlF6aA0fNT4tpbvB4 irYMXrgi4oRjscKoW01B/4AdAGu7hdoptZtRa9EwiBGjS7rvE3ksagGgMldMNWn4eopco iWQEdrtPg2GvE/Jx+EpHO1nb2gjPqtPb/QUhuSn2kbccX5uJi0IY9z8wJvOBd98QZgA== 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=eB4Hf4Ei; arc=none smtp.remote-ip=74.125.82.43 Received: from mail-dl1-f43.google.com (mail-dl1-f43.google.com [74.125.82.43]) (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 4DECA606A32F for ; Sun, 6 Dec 2026 07:07:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4DECA606A32F 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=eB4Hf4Ei Received: by mail-dl1-f43.google.com with SMTP id a92af1059eb24-1b5ade5e0c1so3924530c88.1 for ; Sat, 05 Dec 2026 23:07:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796540876; x=1797145676; darn=vger.kernel.org; h=content-disposition:content-transfer-encoding:content-type :mime-version:references:in-reply-to:subject:cc:to:from:message-id :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=G1lg4L8WuFri6efpvwoH6OtJVXEL5iHPHP50a4q4oOs=; b=eB4Hf4EivwPMG3vXgzzTuPCBas0yp3Tq4KRML+wn7ucUILHq+BAN4FKt7nTsUhPjMd urQlL/M8iUQWUeleXf98jT13ez1DUUHAFe9QWz/1dYVkRrCuWzOq1LG7q8GJBJxDDEhH jiYzFwKRKN9nBmgmpD6rlOlxN8Ksf10sbcNH+f0aCbx7A1ljmLKX+s8x14ddsZkHgkzB bdflNTB4SfNXXnC4Uba25lCy+DrmxnGF6yQIZEitTCFjdz7229k0MmPgcLp7RbfU466X v69mHvvTatsK21B4sOIW1pa1bJZag8Rq7ayrYlM3CznQNiKvRUYwGaL32qFYQSbMfePZ EPAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796540876; x=1797145676; h=content-disposition:content-transfer-encoding:content-type :mime-version:references:in-reply-to:subject:cc:to:from:message-id :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=G1lg4L8WuFri6efpvwoH6OtJVXEL5iHPHP50a4q4oOs=; b=qA5E7GT8rkht+O350YVxYSSQbSUZl105/qbIlyzTH8NHJaBG9lMv/N9uzAS0BiX9b3 NVlKeXBVOv5fehitTopojk5MH/Y7rzS2yhZQhuuOgAyA49yA/MxVCG+MvIYinQCug1In WnoLBy/M9YQ8b8CU9c/Jo7LQuapJXb7E7pwCYZXmheGXN6Eo8NdFI9sJW0aPx+TKvKrs 85tPf0eYeqBK3yI90AkUUNcyEPjgvO+j5mVBDO+OeJ1kpUPGlHqlSarQRHkB4AUmyRI8 HzBn9EnL18dD4o+86/XmgoipUXG/6c3gw/nZHVwVW3CZFsZKbMjurHI84/vKhlmuurOi 7efw== X-Forwarded-Encrypted: i=1; AL+hErHRbiHLEpNI3X51wqFxBuYLi6mGRbH2rwRKhtPoLawwB2gQdW5JoKv/+wH74ymLnttoNwQu1XRIIcjt3MM=@vger.kernel.org X-Gm-Message-State: AB4q+uG3R78ZdIw16vwA6tbIVCzWFXoT4YYUiA8CS35ws9395LV7Dm6J ZNDIh4YgrUH2XWv4cUgjWm+Uxl31VOQ2oJyhlM1UiNuUB95D84+7HB/Y X-Gm-Gg: AZFdTZd8pMcdySOCU2TbTzb+8ZbRkqcEWCqbvngNMR32eR/mcZG7WVsmEbPd2M6QGi+ BY0Z5yk0rDb0jJlcEwXOFUTbZP7ZXVAxHDzQRC7LBkXWdYp/B0w+4QB6gakZHFlk2MYD2qLv0zD UJzh+7CLHt2hTQa1S3Syem2OXLZCKeO/zP60M8j8ZPp3axj6GolxufQMu17OeDOaie/cSsqJjdV Dw+r5rhtU2HWrbMwKfvVWlOi/T42o/QGzglRkCXD9o6YsZBeoqL6a1GI81BoUfqGobf/8amGbw2 FHQEfoZqZbdun10qF6Ndcc+BZUFIFQKKQvPd+ZI5B6EscZAsTG1SjbLnnngGHenI6CNtsWQhmpC KL2lHu4WbC9qxA8tPAyaPW2ZlB8IttbK0nt9JXvvG7fBq4vArU0wo+30NV5+iAm7Kio2gJ2Q+IF 43mpU7oE/PyKd1PTd+rJ3ZcAsgmaJ8ZfyYG5mdr4Qsax/RIDK+xDVQXDpce60AMFXZpeQuyNjeQ XACX1mey80F6EPW5kLy2a4DmN1B2raHWAamK6g/YJHUSoMCiCu2nVLpPmcYja6/oqAXFB97T2Nq ECzn8Ft/SDrgxa9e7NeWWhMkesCY2xpK3uoa6apkoIWXwdObFjsy76HDukUFqepXcivIrg== X-Received: by 2002:a05:693c:62dc:b0:386:c960:278 with SMTP id 5a478bee46e88-38752154359mr7297186eec.17.1796540876258; Sat, 05 Dec 2026 23:07:56 -0800 (PST) Received: from localhost ([2a00:79e0:2ebe:8:9c1c:d617:9ac7:42d5]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521c17a3sm30352912eec.11.2026.12.05.23.07.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 23:07:55 -0800 (PST) Date: Sat, 05 Dec 2026 23:07:54 -0800 Message-ID: <052097db57228acd881b7684032e7eb0@gmail.com> From: Dmitry Torokhov To: Danilo Krummrich Cc: Greg Kroah-Hartman , "Rafael J. Wysocki" , 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 Subject: Re: [PATCH 1/3] driver core: swnode: only release owned secondary software nodes In-Reply-To: References: <20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com> <20261202-swnode-shared-primary-fix-v1-1-7ff23caaa1ed@gmail.com> 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-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi Danilo, On Sun, Dec 06, 2026 at 12:03:54AM +0100, Danilo Krummrich wrote: > On Thu Dec 3, 2026 at 2:34 AM CET, Dmitry Torokhov wrote: > > 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; > > + } > > I think the Sashiko report [1] is valid (though it was present before this patch > as well). > > I think we need to preserve the borrowed secondary when detaching a non-shared > primary, and ensure device release drops only references the device actually > owns. > > [1] https://sashiko.dev/#/patchset/20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed%40gmail.com Right, I agree. set_primary_fwnode(dev, NULL) already preserves the borrowed secondary when "fn != parent->fwnode", so the gap is in platform_device_release() and i2c_unregister_device(), which call fwnode_handle_put(dev_fwnode(dev)) assuming device_remove_software_node() cleared any secondary node first. Before I send v2, which direction do you prefer? 1. Track borrowed dev->fwnode pointers with DEV_FLAG_FWNODE_BORROWED and add device_put_fwnode() in a prep patch (diff below, against the series base). This also covers ipu-bridge.c and mipi-disco-img.c, which write primary->secondary directly without ERR_PTR(-ENODEV). 2. Move fwnode_is_primary() to include/linux/fwnode.h and guard the put: device_remove_software_node(dev); fwnode = dev_fwnode(dev); if (fwnode_is_primary(fwnode)) fwnode_handle_put(fwnode); Smaller diff, though ipu-bridge.c and mipi-disco-img.c stay exposed until fixed separately. Those two can't set ERR_PTR(-ENODEV) ahead of DEV_FLAG_SWNODE_ADDED, since fwnode_is_primary() returning true is what keeps device_remove_software_node() from dropping their ref. I lean toward option 1 since it makes ownership explicit instead of relying on ERR_PTR(-ENODEV). Longer term it may be worth reworking how primary and secondary fwnodes are stacked and refcounted, though that is beyond a stable fix. Thanks. -- Dmitry diff --git a/drivers/base/core.c b/drivers/base/core.c index 44f79f427b6b..460dc2485160 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5252,16 +5252,18 @@ void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) if (fwnode_is_primary(fn)) { dev->fwnode = fn->secondary; - /* Skip nullifying fn->secondary if the primary is shared */ - if (parent && fn == parent->fwnode) - return; - - /* Set fn->secondary = NULL, so fn remains the primary fwnode */ - fn->secondary = NULL; + /* + * Set fn->secondary = NULL so fn remains the primary + * fwnode, unless the primary is shared with the parent. + */ + if (!(parent && fn == parent->fwnode)) + fn->secondary = NULL; } else { dev->fwnode = NULL; } } + + dev_assign_fwnode_borrowed(dev, dev->fwnode); } EXPORT_SYMBOL_GPL(set_primary_fwnode); @@ -5279,10 +5281,12 @@ void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) if (fwnode) fwnode->secondary = ERR_PTR(-ENODEV); - if (fwnode_is_primary(dev->fwnode)) + if (fwnode_is_primary(dev->fwnode)) { dev->fwnode->secondary = fwnode; - else + } else { dev->fwnode = fwnode; + dev_assign_fwnode_borrowed(dev, fwnode); + } } EXPORT_SYMBOL_GPL(set_secondary_fwnode); @@ -5367,9 +5371,29 @@ void device_set_node(struct device *dev, struct fwnode_handle *fwnode) { dev->fwnode = fwnode; dev->of_node = to_of_node(fwnode); + dev_clear_fwnode_borrowed(dev); } EXPORT_SYMBOL_GPL(device_set_node); +/** + * device_put_fwnode - Drop the reference to a device's firmware node. + * @dev: Device whose firmware node reference is to be dropped. + * + * Release the firmware node reference held by @dev, matching the lookup + * precedence of dev_fwnode(): if an OF node is attached, drop its reference + * via of_node_put(); otherwise drop the reference to dev->fwnode unless it was + * assigned as a borrowed reference via set_primary_fwnode() or + * set_secondary_fwnode(). + */ +void device_put_fwnode(struct device *dev) +{ + if (IS_ENABLED(CONFIG_OF) && dev->of_node) + of_node_put(dev->of_node); + else if (!dev_fwnode_borrowed(dev)) + fwnode_handle_put(dev->fwnode); +} +EXPORT_SYMBOL_GPL(device_put_fwnode); + /** * get_dev_from_fwnode - Obtain a reference count of the struct device the * struct fwnode_handle is associated with. diff --git a/drivers/base/platform.c b/drivers/base/platform.c index 9317bb2ece2a..b71c8149a553 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -604,7 +604,7 @@ static void platform_device_release(struct device *dev) * Must be done after device_remove_software_node() which may have * freed a secondary firmware node and cleared the fwnode pointer. */ - fwnode_handle_put(dev_fwnode(dev)); + device_put_fwnode(dev); kfree(pa->pdev.dev.platform_data); kfree(pa->pdev.mfd_cell); diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index 3f411a7ff592..55157e94e284 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -1071,7 +1071,7 @@ void i2c_unregister_device(struct i2c_client *client) * Must be done after device_remove_software_node() which may have * freed a secondary firmware node and cleared the fwnode pointer. */ - fwnode_handle_put(dev_fwnode(&client->dev)); + device_put_fwnode(&client->dev); device_unregister(&client->dev); } EXPORT_SYMBOL_GPL(i2c_unregister_device); diff --git a/include/linux/device.h b/include/linux/device.h index 6b082c638d45..5edb70d638e6 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -608,6 +608,10 @@ 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_FWNODE_BORROWED: Set if dev->fwnode was assigned without taking a + * reference via set_primary_fwnode() or set_secondary_fwnode(). + * Indicates to device_put_fwnode() that it should not drop a + * reference to dev->fwnode. * @DEV_FLAG_COUNT: Number of defined struct_device_flags. */ enum struct_device_flags { @@ -622,6 +626,7 @@ enum struct_device_flags { DEV_FLAG_OFFLINE_DISABLED = 8, DEV_FLAG_OFFLINE = 9, DEV_FLAG_SWNODE_NOTIFIED = 10, + DEV_FLAG_FWNODE_BORROWED = 11, DEV_FLAG_COUNT }; @@ -844,6 +849,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(fwnode_borrowed, DEV_FLAG_FWNODE_BORROWED); #undef __create_dev_flag_accessors @@ -1283,6 +1289,7 @@ int device_online(struct device *dev); void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode); void device_set_node(struct device *dev, struct fwnode_handle *fwnode); +void device_put_fwnode(struct device *dev); int device_add_of_node(struct device *dev, struct device_node *of_node); void device_remove_of_node(struct device *dev); void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);