[PATCH] w1: Use container_of_const() when all types are const

Krzysztof Kozlowski posted 1 patch 5 days, 4 hours ago
drivers/w1/w1.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] w1: Use container_of_const() when all types are const
Posted by Krzysztof Kozlowski 5 days, 4 hours ago
Use container_of_const(), which is preferred over container_of(), when
the argument 'ptr' and returned pointer are already const, for better
code safety and readability.

Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
---
 drivers/w1/w1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index 002d2639aa12..3b48d15390be 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -575,11 +575,11 @@ static int w1_uevent(const struct device *dev, struct kobj_uevent_env *env)
 	int err = 0;
 
 	if (dev->driver == &w1_master_driver) {
-		md = container_of(dev, struct w1_master, dev);
+		md = container_of_const(dev, struct w1_master, dev);
 		event_owner = "master";
 		name = md->name;
 	} else if (dev->driver == &w1_slave_driver) {
-		sl = container_of(dev, struct w1_slave, dev);
+		sl = container_of_const(dev, struct w1_slave, dev);
 		event_owner = "slave";
 		name = sl->name;
 	} else {
-- 
2.48.1
Re: [PATCH] w1: Use container_of_const() when all types are const
Posted by david laight 5 days, 2 hours ago
On Wed, 26 Nov 2025 18:18:42 +0100
Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com> wrote:

> Use container_of_const(), which is preferred over container_of(), when
> the argument 'ptr' and returned pointer are already const, for better
> code safety and readability.

I thought there was a plan to make container_of() return a const pointer
when the input is const?

Can't be done yet because a few things need some 'const' sprinkles
to compile.

	David

> 
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
> ---
>  drivers/w1/w1.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
> index 002d2639aa12..3b48d15390be 100644
> --- a/drivers/w1/w1.c
> +++ b/drivers/w1/w1.c
> @@ -575,11 +575,11 @@ static int w1_uevent(const struct device *dev, struct kobj_uevent_env *env)
>  	int err = 0;
>  
>  	if (dev->driver == &w1_master_driver) {
> -		md = container_of(dev, struct w1_master, dev);
> +		md = container_of_const(dev, struct w1_master, dev);
>  		event_owner = "master";
>  		name = md->name;
>  	} else if (dev->driver == &w1_slave_driver) {
> -		sl = container_of(dev, struct w1_slave, dev);
> +		sl = container_of_const(dev, struct w1_slave, dev);
>  		event_owner = "slave";
>  		name = sl->name;
>  	} else {
Re: [PATCH] w1: Use container_of_const() when all types are const
Posted by Krzysztof Kozlowski 4 days, 15 hours ago
On 26/11/2025 20:27, david laight wrote:
> On Wed, 26 Nov 2025 18:18:42 +0100
> Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com> wrote:
> 
>> Use container_of_const(), which is preferred over container_of(), when
>> the argument 'ptr' and returned pointer are already const, for better
>> code safety and readability.
> 
> I thought there was a plan to make container_of() return a const pointer
> when the input is const?
> 
> Can't be done yet because a few things need some 'const' sprinkles
> to compile.
> 

You mean to use _Generic for container_of, basically exactly how
container_of_const is created? Do you have any indications that this is
happening, because current code does not suggest that.

Best regards,
Krzysztof