Skip to contentSkip to conversation

clk: Add kstrdup_and_replace() helper and use it

PATCH Superseded Andy Shevchenko 3 participants 9 messages Jun 28, 2023 – Aug 4, 2023 1 message not in the archive

Subsystems (7)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover clk: Add kstrdup_and_replace() helper and use it

There are a few existing users and more might come which would like to have the kstrdup_and_replace() functionality.

Provide this new API and reuse it in a few users.

Since most of that is under CCF, perhaps it makes sense to route it via that tree.

Andy Shevchenko (4):
lib/string_helpers: Add kstrdup_and_replace() helper driver core: Replace kstrdup() + strreplace() with
kstrdup_and_replace()
clk: tegra: Replace kstrdup() + strreplace() with
kstrdup_and_replace()
clk: ti: Replace kstrdup() + strreplace() with kstrdup_and_replace()

 drivers/base/core.c            |  5 ++---
 drivers/clk/tegra/clk.c        |  6 ++----
 drivers/clk/ti/clk.c           |  4 ++--
 drivers/clk/ti/clkctrl.c       |  9 +++++----
 include/linux/string_helpers.h |  2 ++
 lib/string_helpers.c           | 15 +++++++++++++++
 6 files changed, 28 insertions(+), 13 deletions(-)
Signature · 1 line
2.40.0.1.gaa8946217a0b
#1 lore raw

PATCH 1/4 lib/string_helpers: Add kstrdup_and_replace() helper

Duplicate a NULL-terminated string and replace all occurrences of the old character with a new one. In other words, provide functionality of kstrdup() + strreplace().

---
 include/linux/string_helpers.h |  2 ++
 lib/string_helpers.c           | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
include/linux/string_helpers.h +2 -0
index 789ab30045da..9d1f5bb74dd5 100644
@@ -109,6 +109,8 @@ char *kstrdup_quotable(const char *src, gfp_t gfp);
 char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
 char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
 
+char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp);
+
 char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
 void kfree_strarray(char **array, size_t n);
 
lib/string_helpers.c +15 -0
index d3b1dd718daf..9982344cca34 100644
@@ -719,6 +719,21 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
 }
 EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
 
+/*
+ * Returns duplicate string in which the @old characters are replaced by @new.
+ */
+char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp)
+{
+	char *dst;
+
+	dst = kstrdup(src, gfp);
+	if (!dst)
+		return NULL;
+
+	return strreplace(dst, old, new);
+}
+EXPORT_SYMBOL_GPL(kstrdup_and_replace);
+
 /**
  * kasprintf_strarray - allocate and fill array of sequential strings
  * @gfp: flags for the slab allocator
Signature · 1 line
2.40.0.1.gaa8946217a0b
#2 lore raw

PATCH 2/4 driver core: Replace kstrdup() + strreplace() with kstrdup_and_replace()

In reply to Andy Shevchenko

Replace open coded functionalify of kstrdup_and_replace() with a call.

---
 drivers/base/core.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/base/core.c +2 -3
index 3dff5037943e..af0ee691520a 100644
@@ -17,7 +17,6 @@
 #include <linux/kstrtox.h>
 #include <linux/module.h>
 #include <linux/slab.h>
-#include <linux/string.h>
 #include <linux/kdev_t.h>
 #include <linux/notifier.h>
 #include <linux/of.h>
@@ -28,6 +27,7 @@
 #include <linux/netdevice.h>
 #include <linux/sched/signal.h>
 #include <linux/sched/mm.h>
+#include <linux/string_helpers.h>
 #include <linux/swiotlb.h>
 #include <linux/sysfs.h>
 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
@@ -3910,10 +3910,9 @@ const char *device_get_devnode(const struct device *dev,
 		return dev_name(dev);
 
 	/* replace '!' in the name with '/' */
-	s = kstrdup(dev_name(dev), GFP_KERNEL);
+	s = kstrdup_and_replace(dev_name(dev), '!', '/', GFP_KERNEL);
 	if (!s)
 		return NULL;
-	strreplace(s, '!', '/');
 	return *tmp = s;
 }
 
Signature · 1 line
2.40.0.1.gaa8946217a0b
maintainer Acked-by on v1 2/4
#3 lore raw

Re: [PATCH v1 2/4] driver core: Replace kstrdup() + strreplace() with kstrdup_and_replace()

Quoted 42 lines · Andy Shevchenko

Replace open coded functionalify of kstrdup_and_replace() with a call.

---
 drivers/base/core.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/base/core.c +2 -3
index 3dff5037943e..af0ee691520a 100644
@@ -17,7 +17,6 @@
 #include <linux/kstrtox.h>
 #include <linux/module.h>
 #include <linux/slab.h>
-#include <linux/string.h>
 #include <linux/kdev_t.h>
 #include <linux/notifier.h>
 #include <linux/of.h>
@@ -28,6 +27,7 @@
 #include <linux/netdevice.h>
 #include <linux/sched/signal.h>
 #include <linux/sched/mm.h>
+#include <linux/string_helpers.h>
 #include <linux/swiotlb.h>
 #include <linux/sysfs.h>
 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
@@ -3910,10 +3910,9 @@ const char *device_get_devnode(const struct device *dev,
 		return dev_name(dev);
 
 	/* replace '!' in the name with '/' */
-	s = kstrdup(dev_name(dev), GFP_KERNEL);
+	s = kstrdup_and_replace(dev_name(dev), '!', '/', GFP_KERNEL);
 	if (!s)
 		return NULL;
-	strreplace(s, '!', '/');
 	return *tmp = s;
 }
 
Signature · 1 line
2.40.0.1.gaa8946217a0b
#4 lore raw

Re: [PATCH v1 2/4] driver core: Replace kstrdup() + strreplace() with kstrdup_and_replace()

Quoted 3 lines · Greg Kroah-Hartman
Quoted 1 line · Andy Shevchenko

Replace open coded functionalify of kstrdup_and_replace() with a call.

Oh, here is a typo.

...

Quoted 1 line

Thank you, Greg!

Stephen, can you take the series now (okay, I think I need to send a new version with all tags and typos fixed)?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v1 2/4] driver core: Replace kstrdup() + strreplace() with kstrdup_and_replace()

Quoted 3 lines · Andy Shevchenko
Quoted 1 line · Greg Kroah-Hartman

On Wed, Jun 28, 2023 at 06:32:09PM +0300, Andy Shevchenko wrote:

...

Quoted 2 lines

Stephen, can you take the series now (okay, I think I need to send a new version with all tags and typos fixed)?

v2 has been sent: [email protected]

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

PATCH 3/4 clk: tegra: Replace kstrdup() + strreplace() with kstrdup_and_replace()

In reply to Andy Shevchenko

Replace open coded functionalify of kstrdup_and_replace() with a call.

---
 drivers/clk/tegra/clk.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/clk/tegra/clk.c +2 -4
index 26bda45813c0..071e72d1598a 100644
@@ -14,7 +14,7 @@
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/reset-controller.h>
-#include <linux/string.h>
+#include <linux/string_helpers.h>
 
 #include <soc/tegra/fuse.h>
 
@@ -384,12 +384,10 @@ static struct device_node *tegra_clk_get_of_node(struct clk_hw *hw)
 	struct device_node *np;
 	char *node_name;
 
-	node_name = kstrdup(hw->init->name, GFP_KERNEL);
+	node_name = kstrdup_and_replace(hw->init->name, '_', '-', GFP_KERNEL);
 	if (!node_name)
 		return NULL;
 
-	strreplace(node_name, '_', '-');
-
 	for_each_child_of_node(tegra_car_np, np) {
 		if (!strcmp(np->name, node_name))
 			break;
Signature · 1 line
2.40.0.1.gaa8946217a0b
#7 lore raw

PATCH 4/4 clk: ti: Replace kstrdup() + strreplace() with kstrdup_and_replace()

In reply to Andy Shevchenko

Replace open coded functionalify of kstrdup_and_replace() with a call.

---
 drivers/clk/ti/clk.c     | 4 ++--
 drivers/clk/ti/clkctrl.c | 9 +++++----
 2 files changed, 7 insertions(+), 6 deletions(-)
drivers/clk/ti/clk.c +2 -2
index 3d636938a739..1862958ab412 100644
@@ -16,6 +16,7 @@
 #include <linux/of_address.h>
 #include <linux/list.h>
 #include <linux/regmap.h>
+#include <linux/string_helpers.h>
 #include <linux/memblock.h>
 #include <linux/device.h>
 
@@ -123,10 +124,9 @@ static struct device_node *ti_find_clock_provider(struct device_node *from,
 	const char *n;
 	char *tmp;
 
-	tmp = kstrdup(name, GFP_KERNEL);
+	tmp = kstrdup_and_replace(name, '-', '_', GFP_KERNEL);
 	if (!tmp)
 		return NULL;
-	strreplace(tmp, '-', '_');
 
 	/* Node named "clock" with "clock-output-names" */
 	for_each_of_allnodes_from(from, np) {
drivers/clk/ti/clkctrl.c +5 -4
index 8c40f10280b7..607e34d8e289 100644
@@ -13,6 +13,7 @@
 #include <linux/of_address.h>
 #include <linux/clk/ti.h>
 #include <linux/delay.h>
+#include <linux/string_helpers.h>
 #include <linux/timekeeping.h>
 #include "clock.h"
 
@@ -473,11 +474,11 @@ static const char * __init clkctrl_get_name(struct device_node *np)
 	const int prefix_len = 11;
 	const char *compat;
 	const char *output;
+	const char *end;
 	char *name;
 
 	if (!of_property_read_string_index(np, "clock-output-names", 0,
 					   &output)) {
-		const char *end;
 		int len;
 
 		len = strlen(output);
@@ -491,13 +492,13 @@ static const char * __init clkctrl_get_name(struct device_node *np)
 
 	of_property_for_each_string(np, "compatible", prop, compat) {
 		if (!strncmp("ti,clkctrl-", compat, prefix_len)) {
+			end = compat + prefix_len;
 			/* Two letter minimum name length for l3, l4 etc */
-			if (strnlen(compat + prefix_len, 16) < 2)
+			if (strnlen(end, 16) < 2)
 				continue;
-			name = kasprintf(GFP_KERNEL, "%s", compat + prefix_len);
+			name = kstrdup_and_replace(end, '-', '_', GFP_KERNEL);
 			if (!name)
 				continue;
-			strreplace(name, '-', '_');
 
 			return name;
 		}
Signature · 1 line
2.40.0.1.gaa8946217a0b

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 11 lines · Stephen Boyd

Quoting Andy Shevchenko (2023-06-28 08:32:07)

Quoted 7 lines

There are a few existing users and more might come which would like to have the kstrdup_and_replace() functionality.

Provide this new API and reuse it in a few users.

Since most of that is under CCF, perhaps it makes sense to route it via that tree.

I'm happy to pick it up if you can get an ack from GregKH on the driver core bits.

It's also an option to skip that patch.
Greg, can you Ack the patch 2 in this series?

Signature · 2 lines
With Best Regards,
Andy Shevchenko