mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Gui-Dong Han <hanguidong02@gmail.com>
To: gregkh@linuxfoundation.org, rafael@kernel.org, dakr@kernel.org
Cc: driver-core@lists.linux.dev, linux-kernel@vger.kernel.org,
	akaieurus@qq.com, Gui-Dong Han <hanguidong02@gmail.com>,
	Geert Uytterhoeven <geert@linux-m68k.org>
Subject: [PATCH] driver core: simplify __device_set_driver_override() clearing logic
Date: Wed, 25 Mar 2026 17:09:05 +0800	[thread overview]
Message-ID: <20260325090905.169000-1-hanguidong02@gmail.com> (raw)

Currently, __device_set_driver_override() handles clearing the override
via empty string ("") and newline ("\n") in two separate paths. The "\n"
case also performs an unnecessary memory allocation and immediate free.

Simplify the logic by initializing 'new' to NULL and only allocating
memory if the string length remains non-zero after stripping the
trailing newline.

Reduce code size, improve readability, and avoid unnecessary memory
operations.

No functional change intended.

Suggested-by: Geert Uytterhoeven <geert@linux-m68k.org>
Link: https://lore.kernel.org/driver-core/DGS82WWLXPJ0.2EH4VJSF30UR5@kernel.org/
Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>
---
 drivers/base/dd.c | 43 ++++++++++++++++---------------------------
 1 file changed, 16 insertions(+), 27 deletions(-)

diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 37c7e54e0e4c..1a8f93051470 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -383,7 +383,7 @@ __exitcall(deferred_probe_exit);
 
 int __device_set_driver_override(struct device *dev, const char *s, size_t len)
 {
-	const char *new, *old;
+	const char *new = NULL, *old;
 	char *cp;
 
 	if (!s)
@@ -404,37 +404,26 @@ int __device_set_driver_override(struct device *dev, const char *s, size_t len)
 	 */
 	len = strlen(s);
 
-	if (!len) {
-		/* Empty string passed - clear override */
-		spin_lock(&dev->driver_override.lock);
-		old = dev->driver_override.name;
-		dev->driver_override.name = NULL;
-		spin_unlock(&dev->driver_override.lock);
-		kfree(old);
-
-		return 0;
+	/* Handle trailing newline */
+	if (len) {
+		cp = strnchr(s, len, '\n');
+		if (cp)
+			len = cp - s;
 	}
 
-	cp = strnchr(s, len, '\n');
-	if (cp)
-		len = cp - s;
-
-	new = kstrndup(s, len, GFP_KERNEL);
-	if (!new)
-		return -ENOMEM;
+	/* If empty string or "\n" passed, new remains NULL, clearing
+	 * the driver_override.name.
+	 */
+	if (len) {
+		new = kstrndup(s, len, GFP_KERNEL);
+		if (!new)
+			return -ENOMEM;
+	}
 
 	spin_lock(&dev->driver_override.lock);
 	old = dev->driver_override.name;
-	if (cp != s) {
-		dev->driver_override.name = new;
-		spin_unlock(&dev->driver_override.lock);
-	} else {
-		/* "\n" passed - clear override */
-		dev->driver_override.name = NULL;
-		spin_unlock(&dev->driver_override.lock);
-
-		kfree(new);
-	}
+	dev->driver_override.name = new;
+	spin_unlock(&dev->driver_override.lock);
 	kfree(old);
 
 	return 0;
-- 
2.43.0


             reply	other threads:[~2026-03-25  9:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-25  9:09 Gui-Dong Han [this message]
2026-03-30 13:55 ` Danilo Krummrich

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=20260325090905.169000-1-hanguidong02@gmail.com \
    --to=hanguidong02@gmail.com \
    --cc=akaieurus@qq.com \
    --cc=dakr@kernel.org \
    --cc=driver-core@lists.linux.dev \
    --cc=geert@linux-m68k.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@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®