diff mbox series

[net-next,1/3] device property: Introduce fwnode_for_each_available_child_node_scoped()

Message ID 20241008-mv88e6xxx_leds_fwnode_put-v1-1-cfd7758cd176@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net, device property: fix led node releases in mv88e6xxx with new macro | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 246 this patch: 246
netdev/build_tools success Errors and warnings before: 0 (+2) this patch: 0 (+2)
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 363 this patch: 363
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 6514 this patch: 6514
netdev/checkpatch warning CHECK: Macro argument 'child' may be better as '(child)' to avoid precedence issues
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 5 this patch: 5
netdev/source_inline success Was 0 now: 0

Commit Message

Javier Carrasco Oct. 8, 2024, 4:10 p.m. UTC
Introduce the scoped variant of the
fwnode_for_each_available_child_node() to automatically decrement the
child's refcount when it goes out of scope, removing the need for
explicit calls to fwnode_handle_put().

Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
---
 include/linux/property.h | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Andy Shevchenko Oct. 10, 2024, 2:32 p.m. UTC | #1
On Tue, Oct 08, 2024 at 06:10:27PM +0200, Javier Carrasco wrote:
> Introduce the scoped variant of the
> fwnode_for_each_available_child_node() to automatically decrement the
> child's refcount when it goes out of scope, removing the need for
> explicit calls to fwnode_handle_put().

...

> +#define fwnode_for_each_available_child_node_scoped(fwnode, child)	       \
> +	for (struct fwnode_handle *child __free(fwnode_handle) =	       \
> +		fwnode_get_next_available_child_node(fwnode, NULL); child;     \
> +	     child = fwnode_get_next_available_child_node(fwnode, child))

I like the wrapping you have done here.
Can you align the device_for_each_child_node_scoped() to follow your variant?

(probably in an additional patch)

For this one
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Sakari Ailus Oct. 11, 2024, 5:39 a.m. UTC | #2
Hi Javier,

On Tue, Oct 08, 2024 at 06:10:27PM +0200, Javier Carrasco wrote:
> Introduce the scoped variant of the
> fwnode_for_each_available_child_node() to automatically decrement the
> child's refcount when it goes out of scope, removing the need for
> explicit calls to fwnode_handle_put().
> 
> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
> ---
>  include/linux/property.h | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/include/linux/property.h b/include/linux/property.h
> index 61fc20e5f81f..b37508ecf606 100644
> --- a/include/linux/property.h
> +++ b/include/linux/property.h
> @@ -168,6 +168,11 @@ struct fwnode_handle *fwnode_get_next_available_child_node(
>  	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
>  	     child = fwnode_get_next_available_child_node(fwnode, child))
>  
> +#define fwnode_for_each_available_child_node_scoped(fwnode, child)	       \
> +	for (struct fwnode_handle *child __free(fwnode_handle) =	       \
> +		fwnode_get_next_available_child_node(fwnode, NULL); child;     \
> +	     child = fwnode_get_next_available_child_node(fwnode, child))
> +

On OF, the implementation of the .get_next_child_node() fwnode op is:

static struct fwnode_handle *
of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
                              struct fwnode_handle *child)
{
        return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
                                                            to_of_node(child)));
}

On ACPI we currently have .device_is_available() returning false but that
probably should be returning true instead (it's been virtually unused
previously).

That makes fwnode_get_next_available_child_node() and
fwnode_get_next_child_node() equivalent on both ACPI and OF. Presumably
creating unavailable nodes would be useless on swnode, too.

So my question is: what do we gain by adding all these fwnode_*available()
helpers?

>  struct fwnode_handle *device_get_next_child_node(const struct device *dev,
>  						 struct fwnode_handle *child);
Javier Carrasco Oct. 11, 2024, 8:34 a.m. UTC | #3
On 11/10/2024 07:39, Sakari Ailus wrote:
> Hi Javier,
> 
> On Tue, Oct 08, 2024 at 06:10:27PM +0200, Javier Carrasco wrote:
>> Introduce the scoped variant of the
>> fwnode_for_each_available_child_node() to automatically decrement the
>> child's refcount when it goes out of scope, removing the need for
>> explicit calls to fwnode_handle_put().
>>
>> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
>> ---
>>  include/linux/property.h | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/include/linux/property.h b/include/linux/property.h
>> index 61fc20e5f81f..b37508ecf606 100644
>> --- a/include/linux/property.h
>> +++ b/include/linux/property.h
>> @@ -168,6 +168,11 @@ struct fwnode_handle *fwnode_get_next_available_child_node(
>>  	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
>>  	     child = fwnode_get_next_available_child_node(fwnode, child))
>>  
>> +#define fwnode_for_each_available_child_node_scoped(fwnode, child)	       \
>> +	for (struct fwnode_handle *child __free(fwnode_handle) =	       \
>> +		fwnode_get_next_available_child_node(fwnode, NULL); child;     \
>> +	     child = fwnode_get_next_available_child_node(fwnode, child))
>> +
> 
> On OF, the implementation of the .get_next_child_node() fwnode op is:
> 
> static struct fwnode_handle *
> of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
>                               struct fwnode_handle *child)
> {
>         return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
>                                                             to_of_node(child)));
> }
> 
> On ACPI we currently have .device_is_available() returning false but that
> probably should be returning true instead (it's been virtually unused
> previously).
> 
> That makes fwnode_get_next_available_child_node() and
> fwnode_get_next_child_node() equivalent on both ACPI and OF. Presumably
> creating unavailable nodes would be useless on swnode, too.
> 
> So my question is: what do we gain by adding all these fwnode_*available()
> helpers?
> 
>>  struct fwnode_handle *device_get_next_child_node(const struct device *dev,
>>  						 struct fwnode_handle *child);
> 

Hi Sakari, thanks for your feedback.

I thought that the difference is not in OF (which either way ends up
calling __of_device_is_available()), but in ACPI.

For fwnode_for_each_child_node(), the ACPI callback is
acpi_get_next_subnode(), and I don't see that the device_is_available()
callback is used in that case.

For fwnode_for_each_available_child_node(),
fwnode_get_next_available_child_node() is used, which checks
fwnode_device_is_available(), which then calls device_is_available().

What's the catch?

Thanks again and best regards,
Javier Carrasco
Sakari Ailus Oct. 11, 2024, 9:54 a.m. UTC | #4
Hi Javier,

On Fri, Oct 11, 2024 at 10:34:32AM +0200, Javier Carrasco wrote:
> On 11/10/2024 07:39, Sakari Ailus wrote:
> > Hi Javier,
> > 
> > On Tue, Oct 08, 2024 at 06:10:27PM +0200, Javier Carrasco wrote:
> >> Introduce the scoped variant of the
> >> fwnode_for_each_available_child_node() to automatically decrement the
> >> child's refcount when it goes out of scope, removing the need for
> >> explicit calls to fwnode_handle_put().
> >>
> >> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
> >> ---
> >>  include/linux/property.h | 5 +++++
> >>  1 file changed, 5 insertions(+)
> >>
> >> diff --git a/include/linux/property.h b/include/linux/property.h
> >> index 61fc20e5f81f..b37508ecf606 100644
> >> --- a/include/linux/property.h
> >> +++ b/include/linux/property.h
> >> @@ -168,6 +168,11 @@ struct fwnode_handle *fwnode_get_next_available_child_node(
> >>  	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
> >>  	     child = fwnode_get_next_available_child_node(fwnode, child))
> >>  
> >> +#define fwnode_for_each_available_child_node_scoped(fwnode, child)	       \
> >> +	for (struct fwnode_handle *child __free(fwnode_handle) =	       \
> >> +		fwnode_get_next_available_child_node(fwnode, NULL); child;     \
> >> +	     child = fwnode_get_next_available_child_node(fwnode, child))
> >> +
> > 
> > On OF, the implementation of the .get_next_child_node() fwnode op is:
> > 
> > static struct fwnode_handle *
> > of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
> >                               struct fwnode_handle *child)
> > {
> >         return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
> >                                                             to_of_node(child)));
> > }
> > 
> > On ACPI we currently have .device_is_available() returning false but that
> > probably should be returning true instead (it's been virtually unused
> > previously).
> > 
> > That makes fwnode_get_next_available_child_node() and
> > fwnode_get_next_child_node() equivalent on both ACPI and OF. Presumably
> > creating unavailable nodes would be useless on swnode, too.
> > 
> > So my question is: what do we gain by adding all these fwnode_*available()
> > helpers?
> > 
> >>  struct fwnode_handle *device_get_next_child_node(const struct device *dev,
> >>  						 struct fwnode_handle *child);
> > 
> 
> Hi Sakari, thanks for your feedback.
> 
> I thought that the difference is not in OF (which either way ends up
> calling __of_device_is_available()), but in ACPI.
> 
> For fwnode_for_each_child_node(), the ACPI callback is
> acpi_get_next_subnode(), and I don't see that the device_is_available()
> callback is used in that case.

fwnode_get_next_available_child_node() also calls
fwnode_device_is_available() and that returns false on all non-device nodes
right now. As noted above, fwnode_device_is_available() should probably
return true for non-device nodes on ACPI. I'll post a patch.

> 
> For fwnode_for_each_available_child_node(),
> fwnode_get_next_available_child_node() is used, which checks
> fwnode_device_is_available(), which then calls device_is_available().
> 
> What's the catch?
Javier Carrasco Oct. 11, 2024, 12:04 p.m. UTC | #5
On 11/10/2024 11:54, Sakari Ailus wrote:
> Hi Javier,
> 
> On Fri, Oct 11, 2024 at 10:34:32AM +0200, Javier Carrasco wrote:
>> On 11/10/2024 07:39, Sakari Ailus wrote:
>>> Hi Javier,
>>>
>>> On Tue, Oct 08, 2024 at 06:10:27PM +0200, Javier Carrasco wrote:
>>>> Introduce the scoped variant of the
>>>> fwnode_for_each_available_child_node() to automatically decrement the
>>>> child's refcount when it goes out of scope, removing the need for
>>>> explicit calls to fwnode_handle_put().
>>>>
>>>> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
>>>> ---
>>>>  include/linux/property.h | 5 +++++
>>>>  1 file changed, 5 insertions(+)
>>>>
>>>> diff --git a/include/linux/property.h b/include/linux/property.h
>>>> index 61fc20e5f81f..b37508ecf606 100644
>>>> --- a/include/linux/property.h
>>>> +++ b/include/linux/property.h
>>>> @@ -168,6 +168,11 @@ struct fwnode_handle *fwnode_get_next_available_child_node(
>>>>  	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
>>>>  	     child = fwnode_get_next_available_child_node(fwnode, child))
>>>>  
>>>> +#define fwnode_for_each_available_child_node_scoped(fwnode, child)	       \
>>>> +	for (struct fwnode_handle *child __free(fwnode_handle) =	       \
>>>> +		fwnode_get_next_available_child_node(fwnode, NULL); child;     \
>>>> +	     child = fwnode_get_next_available_child_node(fwnode, child))
>>>> +
>>>
>>> On OF, the implementation of the .get_next_child_node() fwnode op is:
>>>
>>> static struct fwnode_handle *
>>> of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
>>>                               struct fwnode_handle *child)
>>> {
>>>         return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
>>>                                                             to_of_node(child)));
>>> }
>>>
>>> On ACPI we currently have .device_is_available() returning false but that
>>> probably should be returning true instead (it's been virtually unused
>>> previously).
>>>
>>> That makes fwnode_get_next_available_child_node() and
>>> fwnode_get_next_child_node() equivalent on both ACPI and OF. Presumably
>>> creating unavailable nodes would be useless on swnode, too.
>>>
>>> So my question is: what do we gain by adding all these fwnode_*available()
>>> helpers?
>>>
>>>>  struct fwnode_handle *device_get_next_child_node(const struct device *dev,
>>>>  						 struct fwnode_handle *child);
>>>
>>
>> Hi Sakari, thanks for your feedback.
>>
>> I thought that the difference is not in OF (which either way ends up
>> calling __of_device_is_available()), but in ACPI.
>>
>> For fwnode_for_each_child_node(), the ACPI callback is
>> acpi_get_next_subnode(), and I don't see that the device_is_available()
>> callback is used in that case.
> 
> fwnode_get_next_available_child_node() also calls
> fwnode_device_is_available() and that returns false on all non-device nodes
> right now. As noted above, fwnode_device_is_available() should probably
> return true for non-device nodes on ACPI. I'll post a patch.
> 

fwnode_device_is_available() is indeed called in
fwnode_get_next_available_child_node(), as I stated a couple of lines below.

My question on the other hand was how that is called in
fwnode_for_each_child_node(), as I could not see any call to check
availability in acpi_get_next_subnode().
That is what confused me about the _available_ macros being the same as
their counterparts without the _available_.

Could you please clarify that? Thanks again.

>>
>> For fwnode_for_each_available_child_node(),
>> fwnode_get_next_available_child_node() is used, which checks
>> fwnode_device_is_available(), which then calls device_is_available().
>>
>> What's the catch?
>
diff mbox series

Patch

diff --git a/include/linux/property.h b/include/linux/property.h
index 61fc20e5f81f..b37508ecf606 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -168,6 +168,11 @@  struct fwnode_handle *fwnode_get_next_available_child_node(
 	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
 	     child = fwnode_get_next_available_child_node(fwnode, child))
 
+#define fwnode_for_each_available_child_node_scoped(fwnode, child)	       \
+	for (struct fwnode_handle *child __free(fwnode_handle) =	       \
+		fwnode_get_next_available_child_node(fwnode, NULL); child;     \
+	     child = fwnode_get_next_available_child_node(fwnode, child))
+
 struct fwnode_handle *device_get_next_child_node(const struct device *dev,
 						 struct fwnode_handle *child);