Linux SPI subsystem development
 help / color / mirror / Atom feed
From: Ma Ke <make_ruc2021@163.com>
To: broonie@kernel.org, aaro.koskinen@nokia.com
Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org,
	Ma Ke <make_ruc2021@163.com>,
	stable@vger.kernel.org
Subject: [PATCH] spi: fix reference leak in spi_register_controller()
Date: Tue, 17 Dec 2024 15:31:33 +0800	[thread overview]
Message-ID: <20241217073133.2908052-1-make_ruc2021@163.com> (raw)

Once device_add() failed, we should call put_device() to decrement
reference count for cleanup. Or it could cause memory leak.

Found by code review.

Cc: stable@vger.kernel.org
Fixes: f9981d4f50b4 ("spi: spi_register_controller(): free bus id on error paths")
Signed-off-by: Ma Ke <make_ruc2021@163.com>
---
 drivers/spi/spi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index ff1add2ecb91..6477c2f4ae82 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -3339,8 +3339,10 @@ int spi_register_controller(struct spi_controller *ctlr)
 		ctlr->last_cs[idx] = SPI_INVALID_CS;
 
 	status = device_add(&ctlr->dev);
-	if (status < 0)
+	if (status < 0) {
+		put_device(&ctlr->dev);
 		goto free_bus_id;
+	}
 	dev_dbg(dev, "registered %s %s\n",
 			spi_controller_is_target(ctlr) ? "target" : "host",
 			dev_name(&ctlr->dev));
@@ -3356,6 +3358,7 @@ int spi_register_controller(struct spi_controller *ctlr)
 		status = spi_controller_initialize_queue(ctlr);
 		if (status) {
 			device_del(&ctlr->dev);
+			put_device(&ctlr->dev);
 			goto free_bus_id;
 		}
 	}
-- 
2.25.1


             reply	other threads:[~2024-12-17  7:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-17  7:31 Ma Ke [this message]
2024-12-17 12:16 ` [PATCH] spi: fix reference leak in spi_register_controller() Markus Elfring
2024-12-17 15:23 ` Mark Brown

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=20241217073133.2908052-1-make_ruc2021@163.com \
    --to=make_ruc2021@163.com \
    --cc=aaro.koskinen@nokia.com \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /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