public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] driver core: simplify __device_set_driver_override() clearing logic
@ 2026-03-25  9:09 Gui-Dong Han
  2026-03-30 13:55 ` Danilo Krummrich
  0 siblings, 1 reply; 2+ messages in thread
From: Gui-Dong Han @ 2026-03-25  9:09 UTC (permalink / raw)
  To: gregkh, rafael, dakr
  Cc: driver-core, linux-kernel, akaieurus, Gui-Dong Han,
	Geert Uytterhoeven

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] driver core: simplify __device_set_driver_override() clearing logic
  2026-03-25  9:09 [PATCH] driver core: simplify __device_set_driver_override() clearing logic Gui-Dong Han
@ 2026-03-30 13:55 ` Danilo Krummrich
  0 siblings, 0 replies; 2+ messages in thread
From: Danilo Krummrich @ 2026-03-30 13:55 UTC (permalink / raw)
  To: Gui-Dong Han
  Cc: gregkh, rafael, driver-core, linux-kernel, akaieurus,
	Geert Uytterhoeven

On Wed Mar 25, 2026 at 10:09 AM CET, Gui-Dong Han wrote:
> 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>

Applied to driver-core-testing, thanks!

    [ Narrow cp's scope to the newline handling block; use scoped_guard().
      - Danilo ]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-03-30 13:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-25  9:09 [PATCH] driver core: simplify __device_set_driver_override() clearing logic Gui-Dong Han
2026-03-30 13:55 ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox