Skip to contentSkip to conversation

clk: Add kstrdup_and_replace() helper and use it

PATCH New Andy Shevchenko 1 participant 5 messages Aug 4, 2023

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v2 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.

Changelog v2:
- added tag to patch 2 (Greg KH)
- fixed typo in three commit messages

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 v2 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 v2 2/4 driver core: Replace kstrdup() + strreplace() with kstrdup_and_replace()

In reply to Andy Shevchenko

Replace open coded functionality 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 46d1d78c5beb..704ba73e1459 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 */
@@ -3908,10 +3908,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
#3 lore raw

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

In reply to Andy Shevchenko

Replace open coded functionality 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 333a3ff0db98..19037346f522 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
#4 lore raw

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

In reply to Andy Shevchenko

Replace open coded functionality 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