public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Salah Triki <salah.triki@gmail.com>
To: Peter Rosin <peda@axentia.se>
Cc: linux-kernel@vger.kernel.org, Salah Triki <salah.triki@gmail.com>
Subject: [PATCH] mux: core: fix reference count leak in mux_chip_register()
Date: Sat, 31 Jan 2026 13:09:48 +0100	[thread overview]
Message-ID: <20260131120948.254947-1-salah.triki@gmail.com> (raw)

Once `mux_chip_alloc()` is called, the underlying `struct device` is
initialized via `device_initialize()`, and its reference count is set
to 1. Any error path occurring after this point must call `put_device()`
to ensure proper cleanup of the device and its associated resources.

Currently, if `mux_control_set()` fails or if `device_add()` fails, the
function returns an error code directly. This leaves the `mux_chip`
structure and its internal device's memory leaking, as the release
callback is never triggered.

Fix this by ensuring that `put_device()` is called on all error paths
within `mux_chip_register()`.

Fixes: a3b02a9c6591c ("mux: minimal mux subsystem")

Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
 drivers/mux/core.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/mux/core.c b/drivers/mux/core.c
index a3840fe0995f..2ffb175bbbf6 100644
--- a/drivers/mux/core.c
+++ b/drivers/mux/core.c
@@ -173,14 +173,19 @@ int mux_chip_register(struct mux_chip *mux_chip)
 		ret = mux_control_set(mux, mux->idle_state);
 		if (ret < 0) {
 			dev_err(&mux_chip->dev, "unable to set idle state\n");
-			return ret;
+			goto err_put_device;
 		}
 	}
 
 	ret = device_add(&mux_chip->dev);
-	if (ret < 0)
+	if (ret < 0) {
 		dev_err(&mux_chip->dev,
 			"device_add failed in %s: %d\n", __func__, ret);
+		goto err_put_device;
+	}
+
+err_put_device:
+	put_device(&mux_chip->dev);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(mux_chip_register);
-- 
2.43.0


             reply	other threads:[~2026-01-31 12:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-31 12:09 Salah Triki [this message]
2026-01-31 22:03 ` [PATCH] mux: core: fix reference count leak in mux_chip_register() Peter Rosin
2026-02-04 20:54   ` Salah Triki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260131120948.254947-1-salah.triki@gmail.com \
    --to=salah.triki@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peda@axentia.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox