[PATCH] devres: Move remaining devm_alloc_percpu and devm_device_add_group to devres.h

Pei Xiao posted 1 patch 6 months, 3 weeks ago
include/linux/device.h        | 21 ---------------------
include/linux/device/devres.h | 24 ++++++++++++++++++++++++
2 files changed, 24 insertions(+), 21 deletions(-)
[PATCH] devres: Move remaining devm_alloc_percpu and devm_device_add_group to devres.h
Posted by Pei Xiao 6 months, 3 weeks ago
Since commit f5e5631df596("devres: Move devm_*_action*() APIs to
devres.h"), But devm_alloc_percpu() and devm_device_add_group didn't be
moved.

so move it.The changes improve header organization by keeping all
resource-managed device APIs in the dedicated devres.h header,
reducing cross-header dependencies and making the interfaces
easier to locate.

Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
---
 include/linux/device.h        | 21 ---------------------
 include/linux/device/devres.h | 24 ++++++++++++++++++++++++
 2 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/include/linux/device.h b/include/linux/device.h
index 4940db137fff..6dd6ae8f405b 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -281,25 +281,6 @@ int __must_check device_create_bin_file(struct device *dev,
 void device_remove_bin_file(struct device *dev,
 			    const struct bin_attribute *attr);
 
-/**
- * devm_alloc_percpu - Resource-managed alloc_percpu
- * @dev: Device to allocate per-cpu memory for
- * @type: Type to allocate per-cpu memory for
- *
- * Managed alloc_percpu. Per-cpu memory allocated with this function is
- * automatically freed on driver detach.
- *
- * RETURNS:
- * Pointer to allocated memory on success, NULL on failure.
- */
-#define devm_alloc_percpu(dev, type)      \
-	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
-						      __alignof__(type)))
-
-void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
-				   size_t align);
-void devm_free_percpu(struct device *dev, void __percpu *pdata);
-
 struct device_dma_parameters {
 	/*
 	 * a low level driver may set these to teach IOMMU code about
@@ -1127,8 +1108,6 @@ static inline void device_remove_group(struct device *dev,
 	device_remove_groups(dev, groups);
 }
 
-int __must_check devm_device_add_group(struct device *dev,
-				       const struct attribute_group *grp);
 
 /*
  * get_device - atomically increment the reference count for the device.
diff --git a/include/linux/device/devres.h b/include/linux/device/devres.h
index ae696d10faff..624ba81d4d8b 100644
--- a/include/linux/device/devres.h
+++ b/include/linux/device/devres.h
@@ -7,8 +7,10 @@
 #include <linux/numa.h>
 #include <linux/overflow.h>
 #include <linux/stdarg.h>
+#include <linux/sysfs.h>
 #include <linux/types.h>
 #include <asm/bug.h>
+#include <asm/percpu.h>
 
 struct device;
 struct device_node;
@@ -167,4 +169,26 @@ static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(
 
 bool devm_is_action_added(struct device *dev, void (*action)(void *), void *data);
 
+/**
+ * devm_alloc_percpu - Resource-managed alloc_percpu
+ * @dev: Device to allocate per-cpu memory for
+ * @type: Type to allocate per-cpu memory for
+ *
+ * Managed alloc_percpu. Per-cpu memory allocated with this function is
+ * automatically freed on driver detach.
+ *
+ * RETURNS:
+ * Pointer to allocated memory on success, NULL on failure.
+ */
+#define devm_alloc_percpu(dev, type)      \
+	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
+						      __alignof__(type)))
+
+void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
+				   size_t align);
+void devm_free_percpu(struct device *dev, void __percpu *pdata);
+
+int __must_check devm_device_add_group(struct device *dev,
+				       const struct attribute_group *grp);
+
 #endif /* _DEVICE_DEVRES_H_ */
-- 
2.25.1
Re: [PATCH] devres: Move remaining devm_alloc_percpu and devm_device_add_group to devres.h
Posted by Andy Shevchenko 6 months, 3 weeks ago
On Thu, May 22, 2025 at 05:01:26PM +0800, Pei Xiao wrote:
> Since commit f5e5631df596("devres: Move devm_*_action*() APIs to
> devres.h"), But devm_alloc_percpu() and devm_device_add_group didn't be
> moved.
> 
> so move it.The changes improve header organization by keeping all
> resource-managed device APIs in the dedicated devres.h header,
> reducing cross-header dependencies and making the interfaces
> easier to locate.

Thanks for the patch, my comments below.

...

> -/**
> - * devm_alloc_percpu - Resource-managed alloc_percpu
> - * @dev: Device to allocate per-cpu memory for
> - * @type: Type to allocate per-cpu memory for
> - *
> - * Managed alloc_percpu. Per-cpu memory allocated with this function is
> - * automatically freed on driver detach.
> - *
> - * RETURNS:
> - * Pointer to allocated memory on success, NULL on failure.
> - */
> -#define devm_alloc_percpu(dev, type)      \
> -	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
> -						      __alignof__(type)))
> -
> -void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
> -				   size_t align);
> -void devm_free_percpu(struct device *dev, void __percpu *pdata);

Don't you need to cleanup the header inclusions as well?

...

> -int __must_check devm_device_add_group(struct device *dev,
> -				       const struct attribute_group *grp);

I'm not sure about this. The percpu seems standalone piece, but this has
relations with the other group related definitions just above.

...

> +#include <linux/sysfs.h>

> +#include <asm/percpu.h>

What for are these new inclusions, please?

...

> +/**
> + * devm_alloc_percpu - Resource-managed alloc_percpu
> + * @dev: Device to allocate per-cpu memory for
> + * @type: Type to allocate per-cpu memory for
> + *
> + * Managed alloc_percpu. Per-cpu memory allocated with this function is
> + * automatically freed on driver detach.
> + *
> + * RETURNS:

Please, check that all of the kernel-doc are in align (using same style).

> + * Pointer to allocated memory on success, NULL on failure.
> + */
> +#define devm_alloc_percpu(dev, type)      \
> +	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
> +						      __alignof__(type)))

Just make it one line. it will be less than 100.

> +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
> +				   size_t align);

Ditto.

> +void devm_free_percpu(struct device *dev, void __percpu *pdata);

Please, take your time to understand what is behind the __percpu and
why the asm/percpu.h is redundant here.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] devres: Move remaining devm_alloc_percpu and devm_device_add_group to devres.h
Posted by Pei Xiao 6 months, 2 weeks ago
在 2025/5/22 20:54, Andy Shevchenko 写道:
> On Thu, May 22, 2025 at 05:01:26PM +0800, Pei Xiao wrote:
>> Since commit f5e5631df596("devres: Move devm_*_action*() APIs to
>> devres.h"), But devm_alloc_percpu() and devm_device_add_group didn't be
>> moved.
>>
>> so move it.The changes improve header organization by keeping all
>> resource-managed device APIs in the dedicated devres.h header,
>> reducing cross-header dependencies and making the interfaces
>> easier to locate.
> Thanks for the patch, my comments below.
>
> ...
>
>> -/**
>> - * devm_alloc_percpu - Resource-managed alloc_percpu
>> - * @dev: Device to allocate per-cpu memory for
>> - * @type: Type to allocate per-cpu memory for
>> - *
>> - * Managed alloc_percpu. Per-cpu memory allocated with this function is
>> - * automatically freed on driver detach.
>> - *
>> - * RETURNS:
>> - * Pointer to allocated memory on success, NULL on failure.
>> - */
>> -#define devm_alloc_percpu(dev, type)      \
>> -	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
>> -						      __alignof__(type)))
>> -
>> -void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
>> -				   size_t align);
>> -void devm_free_percpu(struct device *dev, void __percpu *pdata);
> Don't you need to cleanup the header inclusions as well?

no, I think we don't need to cleanup .

#include <linux/lockdep.h> include #include <asm/percpu.h>

only move #include <asm/percpu.h> to linux/device/devres.h for

build error.

> ...
>
>> -int __must_check devm_device_add_group(struct device *dev,
>> -				       const struct attribute_group *grp);
> I'm not sure about this. The percpu seems standalone piece, but this has
> relations with the other group related definitions just above.

I'm also not sure if this devm_device_add_group needs to be moved, 

which is why I haven't replied to you yet

> ...
>
>> +#include <linux/sysfs.h>
it's redundant.
>> +#include <asm/percpu.h>
> What for are these new inclusions, please?
for percpu.
> ...
>
>> +/**
>> + * devm_alloc_percpu - Resource-managed alloc_percpu
>> + * @dev: Device to allocate per-cpu memory for
>> + * @type: Type to allocate per-cpu memory for
>> + *
>> + * Managed alloc_percpu. Per-cpu memory allocated with this function is
>> + * automatically freed on driver detach.
>> + *
>> + * RETURNS:
> Please, check that all of the kernel-doc are in align (using same style).
ok.
>> + * Pointer to allocated memory on success, NULL on failure.
>> + */
>> +#define devm_alloc_percpu(dev, type)      \
>> +	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
>> +						      __alignof__(type)))
> Just make it one line. it will be less than 100.
get it.
>> +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
>> +				   size_t align);
> Ditto.
>
get it.
>> +void devm_free_percpu(struct device *dev, void __percpu *pdata);
> Please, take your time to understand what is behind the __percpu and
> why the asm/percpu.h is redundant here.

If this header file is missing, there will be a compilation error. 

Which header file should I include?

I've found that including <asm/percpu.h> does resolve my compilation issue.

Thanks for you help.

Pei.
Re: [PATCH] devres: Move remaining devm_alloc_percpu and devm_device_add_group to devres.h
Posted by Andy Shevchenko 6 months, 2 weeks ago
On Tue, May 27, 2025 at 05:41:21PM +0800, Pei Xiao wrote:
> 在 2025/5/22 20:54, Andy Shevchenko 写道:
> > On Thu, May 22, 2025 at 05:01:26PM +0800, Pei Xiao wrote:

...

> >> -void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
> >> -				   size_t align);
> >> -void devm_free_percpu(struct device *dev, void __percpu *pdata);
> > Don't you need to cleanup the header inclusions as well?
> 
> no, I think we don't need to cleanup .
> 
> #include <linux/lockdep.h> include #include <asm/percpu.h>
> 
> only move #include <asm/percpu.h> to linux/device/devres.h for
> 
> build error.

It's not needed for the build error fixing as far as I understood it. __percpu
is defined in another header.

...

> >> -int __must_check devm_device_add_group(struct device *dev,
> >> -				       const struct attribute_group *grp);
> > I'm not sure about this. The percpu seems standalone piece, but this has
> > relations with the other group related definitions just above.
> 
> I'm also not sure if this devm_device_add_group needs to be moved, 
> 
> which is why I haven't replied to you yet

Fair enough.

...

> >> +#include <linux/sysfs.h>
> it's redundant.
> >> +#include <asm/percpu.h>
> > What for are these new inclusions, please?
> for percpu.

It does not define __percpu.

> >> +void devm_free_percpu(struct device *dev, void __percpu *pdata);
> > Please, take your time to understand what is behind the __percpu and
> > why the asm/percpu.h is redundant here.
> 
> If this header file is missing, there will be a compilation error. 

Right. But this is wrong header for the purpose.

> Which header file should I include?

The one that defines it, hint:
https://elixir.bootlin.com/linux/v6.15/A/ident/__percpu

> I've found that including <asm/percpu.h> does resolve my compilation issue.

-- 
With Best Regards,
Andy Shevchenko


Re: [PATCH] devres: Move remaining devm_alloc_percpu and devm_device_add_group to devres.h
Posted by Pei Xiao 6 months, 2 weeks ago
在 2025/5/27 19:09, Andy Shevchenko 写道:
> On Tue, May 27, 2025 at 05:41:21PM +0800, Pei Xiao wrote:
>> 在 2025/5/22 20:54, Andy Shevchenko 写道:
>>> On Thu, May 22, 2025 at 05:01:26PM +0800, Pei Xiao wrote:
> ...
>
>>>> -void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
>>>> -				   size_t align);
>>>> -void devm_free_percpu(struct device *dev, void __percpu *pdata);
>>> Don't you need to cleanup the header inclusions as well?
>> no, I think we don't need to cleanup .
>>
>> #include <linux/lockdep.h> include #include <asm/percpu.h>
>>
>> only move #include <asm/percpu.h> to linux/device/devres.h for
>>
>> build error.
> It's not needed for the build error fixing as far as I understood it. __percpu
> is defined in another header.

compiler_types.h or compiler.h 

I tried before send patch, but it all resulted in compilation failures on my arm64 machine.


> ...
>
>>>> -int __must_check devm_device_add_group(struct device *dev,
>>>> -				       const struct attribute_group *grp);
>>> I'm not sure about this. The percpu seems standalone piece, but this has
>>> relations with the other group related definitions just above.
>> I'm also not sure if this devm_device_add_group needs to be moved, 
>>
>> which is why I haven't replied to you yet
> Fair enough.
>
> ...
>
>>>> +#include <linux/sysfs.h>
>> it's redundant.
>>>> +#include <asm/percpu.h>
>>> What for are these new inclusions, please?
>> for percpu.
> It does not define __percpu.
>
>>>> +void devm_free_percpu(struct device *dev, void __percpu *pdata);
>>> Please, take your time to understand what is behind the __percpu and
>>> why the asm/percpu.h is redundant here.
>> If this header file is missing, there will be a compilation error. 
> Right. But this is wrong header for the purpose.
>
>> Which header file should I include?
> The one that defines it, hint:
> https://elixir.bootlin.com/linux/v6.15/A/ident/__percpu

compiler_types.h or compiler.h 

I tried before send patch, but it all resulted in compilation failures on my arm64 machine.

 You can give it a try.

when i add a error in compiler_types.h

+++ b/include/linux/compiler_types.h
@@ -26,6 +26,7 @@
 
 /* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
 #ifdef __CHECKER__
+sasasas
 /* address spaces */
 # define __kernel      __attribute__((address_space(0)))
 # define __user                __attribute__((noderef, address_space(__user)

it  still build success,have no build error.

#include <linux/lockdep.h> include <asm/percpu.h> ,so have no build error before modidy,I think.

>> I've found that including <asm/percpu.h> does resolve my compilation issue.
thanks!
Re: [PATCH] devres: Move remaining devm_alloc_percpu and devm_device_add_group to devres.h
Posted by Andy Shevchenko 6 months, 2 weeks ago
On Wed, May 28, 2025 at 11:07:43AM +0800, Pei Xiao wrote:
> 
> 在 2025/5/27 19:09, Andy Shevchenko 写道:
> > On Tue, May 27, 2025 at 05:41:21PM +0800, Pei Xiao wrote:
> >> 在 2025/5/22 20:54, Andy Shevchenko 写道:
> >>> On Thu, May 22, 2025 at 05:01:26PM +0800, Pei Xiao wrote:

...

> >> include #include <asm/percpu.h>
> >>
> >> only move #include <asm/percpu.h> to linux/device/devres.h for
> >>
> >> build error.
> > It's not needed for the build error fixing as far as I understood it. __percpu
> > is defined in another header.
> 
> compiler_types.h or compiler.h 
> 
> I tried before send patch, but it all resulted in compilation failures on my arm64 machine.

You need to investigate those. But header doesn't use anything fancy AFAICS, so
compiler_types.h should be enough. I think you need to move asm/percpu.h to
devres.c (C file).

...

> >>>> +#include <linux/sysfs.h>
> >> it's redundant.
> >>>> +#include <asm/percpu.h>
> >>> What for are these new inclusions, please?
> >> for percpu.
> > It does not define __percpu.
> >
> >>>> +void devm_free_percpu(struct device *dev, void __percpu *pdata);
> >>> Please, take your time to understand what is behind the __percpu and
> >>> why the asm/percpu.h is redundant here.
> >> If this header file is missing, there will be a compilation error. 
> > Right. But this is wrong header for the purpose.
> >
> >> Which header file should I include?
> > The one that defines it, hint:
> > https://elixir.bootlin.com/linux/v6.15/A/ident/__percpu
> 
> compiler_types.h or compiler.h 
> 
> I tried before send patch, but it all resulted in compilation failures on my arm64 machine.
> 
>  You can give it a try.

Sure.

> when i add a error in compiler_types.h
> 
> +++ b/include/linux/compiler_types.h
> @@ -26,6 +26,7 @@
>  
>  /* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
>  #ifdef __CHECKER__
> +sasasas
>  /* address spaces */
>  # define __kernel      __attribute__((address_space(0)))
>  # define __user                __attribute__((noderef, address_space(__user)
> 
> it  still build success,have no build error.
> 
> #include <linux/lockdep.h> include <asm/percpu.h> ,so have no build error before modidy,I think.
> 
> >> I've found that including <asm/percpu.h> does resolve my compilation issue.
> thanks!

-- 
With Best Regards,
Andy Shevchenko


Re: [PATCH] devres: Move remaining devm_alloc_percpu and devm_device_add_group to devres.h
Posted by Pei Xiao 6 months, 3 weeks ago
在 2025/5/22 20:54, Andy Shevchenko 写道:
> On Thu, May 22, 2025 at 05:01:26PM +0800, Pei Xiao wrote:
>> Since commit f5e5631df596("devres: Move devm_*_action*() APIs to
>> devres.h"), But devm_alloc_percpu() and devm_device_add_group didn't be
>> moved.
>>
>> so move it.The changes improve header organization by keeping all
>> resource-managed device APIs in the dedicated devres.h header,
>> reducing cross-header dependencies and making the interfaces
>> easier to locate.
> Thanks for the patch, my comments below.
>
> ...
>
>> -/**
>> - * devm_alloc_percpu - Resource-managed alloc_percpu
>> - * @dev: Device to allocate per-cpu memory for
>> - * @type: Type to allocate per-cpu memory for
>> - *
>> - * Managed alloc_percpu. Per-cpu memory allocated with this function is
>> - * automatically freed on driver detach.
>> - *
>> - * RETURNS:
>> - * Pointer to allocated memory on success, NULL on failure.
>> - */
>> -#define devm_alloc_percpu(dev, type)      \
>> -	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
>> -						      __alignof__(type)))
>> -
>> -void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
>> -				   size_t align);
>> -void devm_free_percpu(struct device *dev, void __percpu *pdata);
> Don't you need to cleanup the header inclusions as well?
>
> ...
>
>> -int __must_check devm_device_add_group(struct device *dev,
>> -				       const struct attribute_group *grp);
> I'm not sure about this. The percpu seems standalone piece, but this has
> relations with the other group related definitions just above.
>
> ...
>
>> +#include <linux/sysfs.h>
>> +#include <asm/percpu.h>
> What for are these new inclusions, please?

I add these for build error:

In file included from drivers/gpio/gpiolib-devres.c:9:
./include/linux/device/devres.h:189:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  189 | void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
      |               ^
./include/linux/device/devres.h:191:57: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
  191 | void devm_free_percpu(struct device *dev, void __percpu *pdata);
      |                                                         ^
./include/linux/device/devres.h:194:25: warning: ‘struct attribute_group’ declared inside parameter list will not be visible outside of this definition or declaration
  194 |            const struct attribute_group *grp);
      |                         ^~~~~~~~~~~~~~~

> ...
>
>> +/**
>> + * devm_alloc_percpu - Resource-managed alloc_percpu
>> + * @dev: Device to allocate per-cpu memory for
>> + * @type: Type to allocate per-cpu memory for
>> + *
>> + * Managed alloc_percpu. Per-cpu memory allocated with this function is
>> + * automatically freed on driver detach.
>> + *
>> + * RETURNS:
> Please, check that all of the kernel-doc are in align (using same style).
ok,get it
>> + * Pointer to allocated memory on success, NULL on failure.
>> + */
>> +#define devm_alloc_percpu(dev, type)      \
>> +	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
>> +						      __alignof__(type)))
> Just make it one line. it will be less than 100.
ok
>> +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
>> +				   size_t align);
> Ditto.
ok.
>> +void devm_free_percpu(struct device *dev, void __percpu *pdata);
> Please, take your time to understand what is behind the __percpu and
> why the asm/percpu.h is redundant here.
>
Re: [PATCH] devres: Move remaining devm_alloc_percpu and devm_device_add_group to devres.h
Posted by Andy Shevchenko 6 months, 3 weeks ago
On Fri, May 23, 2025 at 11:08:09AM +0800, Pei Xiao wrote:
> 在 2025/5/22 20:54, Andy Shevchenko 写道:
> > On Thu, May 22, 2025 at 05:01:26PM +0800, Pei Xiao wrote:

...

> > Don't you need to cleanup the header inclusions as well?

Any answer here?

...

> >> -int __must_check devm_device_add_group(struct device *dev,
> >> -				       const struct attribute_group *grp);
> > I'm not sure about this. The percpu seems standalone piece, but this has
> > relations with the other group related definitions just above.

And here? You should either agree on non-commented points (and remove them
when replying) or answer what's wrong with the suggestions. Ignoring is not
an option.

...

> >> +#include <linux/sysfs.h>
> >> +#include <asm/percpu.h>
> > What for are these new inclusions, please?
> 
> I add these for build error:
> 
> In file included from drivers/gpio/gpiolib-devres.c:9:
> ./include/linux/device/devres.h:189:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
>   189 | void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
>       |               ^
> ./include/linux/device/devres.h:191:57: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
>   191 | void devm_free_percpu(struct device *dev, void __percpu *pdata);
>       |                                                         ^

Nice, and where is the __percpu defined?

> ./include/linux/device/devres.h:194:25: warning: ‘struct attribute_group’ declared inside parameter list will not be visible outside of this definition or declaration
>   194 |            const struct attribute_group *grp);
>       |                         ^~~~~~~~~~~~~~~

And why do you need the sysfs.h to resolve this? I don't see the use of
the struct attribute_group here. (The error message has the hint in it
on how to proceed)

...

> > Please, take your time to understand what is behind the __percpu and
> > why the asm/percpu.h is redundant here.

-- 
With Best Regards,
Andy Shevchenko