mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ma Ke <make24@iscas.ac.cn>
To: alexandre.belloni@bootlin.com
Cc: linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org,
	akpm@linux-foundation.org, Ma Ke <make24@iscas.ac.cn>,
	stable@vger.kernel.org
Subject: [PATCH] rtc: Fix error handling in devm_rtc_allocate_device
Date: Sun,  9 Nov 2025 14:08:17 +0800	[thread overview]
Message-ID: <20251109060817.5620-1-make24@iscas.ac.cn> (raw)

In rtc_allocate_device(), device_initialize() sets the reference count
to 1. In rtc_allocate_device(), when devm_add_action_or_reset() or
dev_set_name() fails after successful device initialization via
device_initialize(), rtc_allocate_device() returns an error without
properly calling put_device() and releasing the reference count.

Add proper error handling that calls put_device() in all error paths
after device_initialize(), ensuring proper resource cleanup.

Found by code review.

Cc: stable@vger.kernel.org
Fixes: 3068a254d551 ("rtc: introduce new registration method")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 drivers/rtc/class.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index b1a2be1f9e3b..db5f33a22b14 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -379,13 +379,17 @@ struct rtc_device *devm_rtc_allocate_device(struct device *dev)
 	rtc->dev.parent = dev;
 	err = devm_add_action_or_reset(dev, devm_rtc_release_device, rtc);
 	if (err)
-		return ERR_PTR(err);
+		goto err_put_device;
 
 	err = dev_set_name(&rtc->dev, "rtc%d", id);
 	if (err)
-		return ERR_PTR(err);
+		goto err_put_device;
 
 	return rtc;
+
+err_put_device:
+	put_device(&rtc->dev);
+	return ERR_PTR(err);
 }
 EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
 
-- 
2.17.1


             reply	other threads:[~2025-11-09  6:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-09  6:08 Ma Ke [this message]
2025-11-09  8:59 ` Christophe JAILLET

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=20251109060817.5620-1-make24@iscas.ac.cn \
    --to=make24@iscas.ac.cn \
    --cc=akpm@linux-foundation.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@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

all inboxes | Powered by JetHome®