[PATCH v3] mux: core: Replace manual put_device() with __free(put_device)

Maxwell Doose posted 1 patch 5 days, 10 hours ago
drivers/mux/core.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
[PATCH v3] mux: core: Replace manual put_device() with __free(put_device)
Posted by Maxwell Doose 5 days, 10 hours ago
The current code for returning on failure in mux_get() uses manual
put_device() calls. Refactor and replace them with a new variable for
the pointer to the underlying device and __free(put_device).

Signed-off-by: Maxwell Doose <m32285159@gmail.com>
---
 drivers/mux/core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/mux/core.c b/drivers/mux/core.c
index 23538de2c91b..2c8d380fd81a 100644
--- a/drivers/mux/core.c
+++ b/drivers/mux/core.c
@@ -9,6 +9,7 @@
 
 #define pr_fmt(fmt) "mux-core: " fmt
 
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/err.h>
@@ -586,13 +587,13 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,
 	if (!mux_chip)
 		return ERR_PTR(-EPROBE_DEFER);
 
+	struct device *mux_dev __free(put_device) = &mux_chip->dev;
 	controller = 0;
 	if (state) {
 		if (args.args_count > 2 || args.args_count == 0 ||
 		    (args.args_count < 2 && mux_chip->controllers > 1)) {
 			dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
 				np, args.np);
-			put_device(&mux_chip->dev);
 			return ERR_PTR(-EINVAL);
 		}
 
@@ -608,7 +609,6 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,
 		    (!args.args_count && mux_chip->controllers > 1)) {
 			dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
 				np, args.np);
-			put_device(&mux_chip->dev);
 			return ERR_PTR(-EINVAL);
 		}
 
@@ -619,10 +619,11 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,
 	if (controller >= mux_chip->controllers) {
 		dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
 			np, controller, args.np);
-		put_device(&mux_chip->dev);
 		return ERR_PTR(-EINVAL);
 	}
 
+	struct device *prev_mux_dev __maybe_unused = no_free_ptr(mux_dev);
+
 	return &mux_chip->mux[controller];
 }
 
-- 
2.54.0
Re: [PATCH v3] mux: core: Replace manual put_device() with __free(put_device)
Posted by Peter Rosin 5 days, 6 hours ago
On 2026-06-02 18:44, Maxwell Doose wrote:
> The current code for returning on failure in mux_get() uses manual
> put_device() calls. Refactor and replace them with a new variable for
> the pointer to the underlying device and __free(put_device).

I hinted at it in the reply to v1, and seeing it confirms it methinks.
For me, __free() works well for RAII, but the pattern here isn't RAII.
Acquisition happened before init and the scope of the variable isn't
the lifetime either. It feels a bit clumsy. So, this patch is not a
clear improvement and therefore not worth it. Sorry.

Cheers,
Peter

> Signed-off-by: Maxwell Doose <m32285159@gmail.com>
> ---
>   drivers/mux/core.c | 7 ++++---
>   1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/mux/core.c b/drivers/mux/core.c
> index 23538de2c91b..2c8d380fd81a 100644
> --- a/drivers/mux/core.c
> +++ b/drivers/mux/core.c
> @@ -9,6 +9,7 @@
>   
>   #define pr_fmt(fmt) "mux-core: " fmt
>   
> +#include <linux/cleanup.h>
>   #include <linux/delay.h>
>   #include <linux/device.h>
>   #include <linux/err.h>
> @@ -586,13 +587,13 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,
>   	if (!mux_chip)
>   		return ERR_PTR(-EPROBE_DEFER);
>   
> +	struct device *mux_dev __free(put_device) = &mux_chip->dev;
>   	controller = 0;
>   	if (state) {
>   		if (args.args_count > 2 || args.args_count == 0 ||
>   		    (args.args_count < 2 && mux_chip->controllers > 1)) {
>   			dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
>   				np, args.np);
> -			put_device(&mux_chip->dev);
>   			return ERR_PTR(-EINVAL);
>   		}
>   
> @@ -608,7 +609,6 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,
>   		    (!args.args_count && mux_chip->controllers > 1)) {
>   			dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
>   				np, args.np);
> -			put_device(&mux_chip->dev);
>   			return ERR_PTR(-EINVAL);
>   		}
>   
> @@ -619,10 +619,11 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,
>   	if (controller >= mux_chip->controllers) {
>   		dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
>   			np, controller, args.np);
> -		put_device(&mux_chip->dev);
>   		return ERR_PTR(-EINVAL);
>   	}
>   
> +	struct device *prev_mux_dev __maybe_unused = no_free_ptr(mux_dev);
> +
>   	return &mux_chip->mux[controller];
>   }
>
Re: [PATCH v3] mux: core: Replace manual put_device() with __free(put_device)
Posted by Maxwell Doose 5 days, 6 hours ago
On Tue, 2 Jun 2026 22:37:06 +0200
Peter Rosin <peda@lysator.liu.se> wrote:

> On 2026-06-02 18:44, Maxwell Doose wrote:
> > The current code for returning on failure in mux_get() uses manual
> > put_device() calls. Refactor and replace them with a new variable for
> > the pointer to the underlying device and __free(put_device).  
> 
> I hinted at it in the reply to v1, and seeing it confirms it methinks.
> For me, __free() works well for RAII, but the pattern here isn't RAII.
> Acquisition happened before init and the scope of the variable isn't
> the lifetime either. It feels a bit clumsy. So, this patch is not a
> clear improvement and therefore not worth it. Sorry.
> 
> Cheers,
> Peter
> 

That's fair. Have a good night though!

-- 
best regards,
max
Re: [PATCH v3] mux: core: Replace manual put_device() with __free(put_device)
Posted by Maxwell Doose 5 days, 10 hours ago
On Tue,  2 Jun 2026 11:44:07 -0500
Maxwell Doose <m32285159@gmail.com> wrote:

> The current code for returning on failure in mux_get() uses manual
> put_device() calls. Refactor and replace them with a new variable for
> the pointer to the underlying device and __free(put_device).
> 
> Signed-off-by: Maxwell Doose <m32285159@gmail.com>
> ---
>  drivers/mux/core.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/mux/core.c b/drivers/mux/core.c
> index 23538de2c91b..2c8d380fd81a 100644
> --- a/drivers/mux/core.c
> +++ b/drivers/mux/core.c
> @@ -9,6 +9,7 @@
>  
>  #define pr_fmt(fmt) "mux-core: " fmt
>  
> +#include <linux/cleanup.h>
>  #include <linux/delay.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
> @@ -586,13 +587,13 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,
>  	if (!mux_chip)
>  		return ERR_PTR(-EPROBE_DEFER);
>  
> +	struct device *mux_dev __free(put_device) = &mux_chip->dev;
>  	controller = 0;
>  	if (state) {
>  		if (args.args_count > 2 || args.args_count == 0 ||
>  		    (args.args_count < 2 && mux_chip->controllers > 1)) {
>  			dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
>  				np, args.np);
> -			put_device(&mux_chip->dev);
>  			return ERR_PTR(-EINVAL);
>  		}
>  
> @@ -608,7 +609,6 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,
>  		    (!args.args_count && mux_chip->controllers > 1)) {
>  			dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
>  				np, args.np);
> -			put_device(&mux_chip->dev);
>  			return ERR_PTR(-EINVAL);
>  		}
>  
> @@ -619,10 +619,11 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,
>  	if (controller >= mux_chip->controllers) {
>  		dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
>  			np, controller, args.np);
> -		put_device(&mux_chip->dev);
>  		return ERR_PTR(-EINVAL);
>  	}
>  
> +	struct device *prev_mux_dev __maybe_unused = no_free_ptr(mux_dev);
> +
>  	return &mux_chip->mux[controller];
>  }
>  

I forgot the changelog :(

 v2:
 - Add explicit cleanup.h header.
 - Add no_free_ptr() at the end of the happy path to prevent
   accidentially calling put_device() where it shouldn't.

 v3:
 - Fix compiler assertion where the return value of no_free_ptr() was
   ignored.

-- 
best regards,
max