[PATCH 2/4] software node: allow passing reference args to PROPERTY_ENTRY_REF

Dmitry Torokhov posted 4 patches 1 week, 5 days ago
There is a newer version of this series
[PATCH 2/4] software node: allow passing reference args to PROPERTY_ENTRY_REF
Posted by Dmitry Torokhov 1 week, 5 days ago
When dynamically creating software nodes and properties for subsequent
use with software_node_register() current implementation of
PROPERTY_ENTRY_REF is not suitable because it creates a temporary
instance of struct software_node_ref_args on stack which will later
disappear, and software_node_register() only does shallow copy of
properties.

Fix this by allowing to pass address of reference arguments structure
directly into PROPERTY_ENTRY_REF(), so that caller can manage lifetime
of the object properly.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 include/linux/property.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/include/linux/property.h b/include/linux/property.h
index e30ef23a9af3..942657e76993 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -471,12 +471,19 @@ struct property_entry {
 #define PROPERTY_ENTRY_STRING(_name_, _val_)				\
 	__PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
 
+#define __PROPERTY_ENTRY_REF_ARGS(_ref_, ...)				\
+	_Generic(_ref_,							\
+		 const struct software_node_ref_args *: _ref_,		\
+		 struct software_node_ref_args *: _ref_,		\
+		 default: &SOFTWARE_NODE_REFERENCE(_ref_,		\
+						   ##__VA_ARGS__))
+
 #define PROPERTY_ENTRY_REF(_name_, _ref_, ...)				\
 (struct property_entry) {						\
 	.name = _name_,							\
 	.length = sizeof(struct software_node_ref_args),		\
 	.type = DEV_PROP_REF,						\
-	{ .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), },	\
+	{ .pointer = __PROPERTY_ENTRY_REF_ARGS(_ref_, ##__VA_ARGS__) },	\
 }
 
 #define PROPERTY_ENTRY_BOOL(_name_)		\

-- 
2.53.0.1018.g2bb0e51243-goog
Re: [PATCH 2/4] software node: allow passing reference args to PROPERTY_ENTRY_REF
Posted by Andy Shevchenko 1 week, 5 days ago
On Mon, Mar 23, 2026 at 05:39:38PM -0700, Dmitry Torokhov wrote:
> When dynamically creating software nodes and properties for subsequent
> use with software_node_register() current implementation of
> PROPERTY_ENTRY_REF is not suitable because it creates a temporary
> instance of struct software_node_ref_args on stack which will later
> disappear, and software_node_register() only does shallow copy of
> properties.

Isn't that the problem and we should take into account how to dup the reference
inside property core?

> Fix this by allowing to pass address of reference arguments structure
> directly into PROPERTY_ENTRY_REF(), so that caller can manage lifetime
> of the object properly.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH 2/4] software node: allow passing reference args to PROPERTY_ENTRY_REF
Posted by Dmitry Torokhov 1 week, 5 days ago
On Tue, Mar 24, 2026 at 02:30:54PM +0200, Andy Shevchenko wrote:
> On Mon, Mar 23, 2026 at 05:39:38PM -0700, Dmitry Torokhov wrote:
> > When dynamically creating software nodes and properties for subsequent
> > use with software_node_register() current implementation of
> > PROPERTY_ENTRY_REF is not suitable because it creates a temporary
> > instance of struct software_node_ref_args on stack which will later
> > disappear, and software_node_register() only does shallow copy of
> > properties.
> 
> Isn't that the problem and we should take into account how to dup the reference
> inside property core?

Sorry, I actually need to drop the end of that sentence.
software_node_register() expects the caller to maintain the lifetime and
therefore does not do any kind of copying of properties. The majority of
users define static const properties and we do not want to make an extra
copy of them.

Thanks.

-- 
Dmitry