Skip to contentSkip to conversation

mux: core: Replace manual put_device() with __free(put_device)

PATCH Superseded Maxwell Doose 1 participant 2 messages Jun 2 Multiplexer

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mux: core: Replace manual put_device() with __free(put_device)

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).

---
 drivers/mux/core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

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.

drivers/mux/core.c +4 -3
index 23538de2c91b..7c9180dfaff6 100644
@@ -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);
 	}
 
+	no_free_ptr(mux_dev);
+
 	return &mux_chip->mux[controller];
 }
 
Signature · 1 line
2.54.0
#1 lore raw
Quoted 52 lines · On Tue, 2 Jun 2026 11:28:33 -0500 Maxwell Doose <[email protected]> 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).

---
 drivers/mux/core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

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.

drivers/mux/core.c +2 -2
index 23538de2c91b..7c9180dfaff6 100644
@@ -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",

drivers/mux/core.c static struct mux_control *mux_get(struct device *dev, const char *mux_name, · line 629

Quoted 10 lines
drivers/mux/core.c +2 -1
 			np, controller, args.np);
-		put_device(&mux_chip->dev);
 		return ERR_PTR(-EINVAL);
 	}
 
+	no_free_ptr(mux_dev);
+
 	return &mux_chip->mux[controller];
 }
 

Disregard, I accidentally forgot to build and now GCC is screaming at me with "ignoring return value of..." errors.

Signature · 2 lines
best regards,
max