* [PATCH v2 0/4] clk: Add kstrdup_and_replace() helper and use it
@ 2023-08-04 14:39 Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 1/4] lib/string_helpers: Add kstrdup_and_replace() helper Andy Shevchenko
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Andy Shevchenko @ 2023-08-04 14:39 UTC (permalink / raw)
To: Greg Kroah-Hartman, Saravana Kannan, Andy Shevchenko,
Stephen Boyd, Dario Binacchi, Tony Lindgren, linux-kernel,
linux-clk, linux-tegra, linux-omap, linux-hardening
Cc: Rafael J. Wysocki, Peter De Schrijver, Prashant Gaikwad,
Michael Turquette, Thierry Reding, Jonathan Hunter, Tero Kristo,
Kees Cook, Andy Shevchenko
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(-)
--
2.40.0.1.gaa8946217a0b
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/4] lib/string_helpers: Add kstrdup_and_replace() helper
2023-08-04 14:39 [PATCH v2 0/4] clk: Add kstrdup_and_replace() helper and use it Andy Shevchenko
@ 2023-08-04 14:39 ` Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 2/4] driver core: Replace kstrdup() + strreplace() with kstrdup_and_replace() Andy Shevchenko
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2023-08-04 14:39 UTC (permalink / raw)
To: Greg Kroah-Hartman, Saravana Kannan, Andy Shevchenko,
Stephen Boyd, Dario Binacchi, Tony Lindgren, linux-kernel,
linux-clk, linux-tegra, linux-omap, linux-hardening
Cc: Rafael J. Wysocki, Peter De Schrijver, Prashant Gaikwad,
Michael Turquette, Thierry Reding, Jonathan Hunter, Tero Kristo,
Kees Cook, Andy Shevchenko
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().
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
include/linux/string_helpers.h | 2 ++
lib/string_helpers.c | 15 +++++++++++++++
2 files changed, 17 insertions(+)
diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 789ab30045da..9d1f5bb74dd5 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -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);
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index d3b1dd718daf..9982344cca34 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -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
--
2.40.0.1.gaa8946217a0b
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 2/4] driver core: Replace kstrdup() + strreplace() with kstrdup_and_replace()
2023-08-04 14:39 [PATCH v2 0/4] clk: Add kstrdup_and_replace() helper and use it Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 1/4] lib/string_helpers: Add kstrdup_and_replace() helper Andy Shevchenko
@ 2023-08-04 14:39 ` Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 3/4] clk: tegra: " Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 4/4] clk: ti: " Andy Shevchenko
3 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2023-08-04 14:39 UTC (permalink / raw)
To: Greg Kroah-Hartman, Saravana Kannan, Andy Shevchenko,
Stephen Boyd, Dario Binacchi, Tony Lindgren, linux-kernel,
linux-clk, linux-tegra, linux-omap, linux-hardening
Cc: Rafael J. Wysocki, Peter De Schrijver, Prashant Gaikwad,
Michael Turquette, Thierry Reding, Jonathan Hunter, Tero Kristo,
Kees Cook, Andy Shevchenko
Replace open coded functionality of kstrdup_and_replace() with a call.
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
drivers/base/core.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 46d1d78c5beb..704ba73e1459 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -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;
}
--
2.40.0.1.gaa8946217a0b
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 3/4] clk: tegra: Replace kstrdup() + strreplace() with kstrdup_and_replace()
2023-08-04 14:39 [PATCH v2 0/4] clk: Add kstrdup_and_replace() helper and use it Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 1/4] lib/string_helpers: Add kstrdup_and_replace() helper Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 2/4] driver core: Replace kstrdup() + strreplace() with kstrdup_and_replace() Andy Shevchenko
@ 2023-08-04 14:39 ` Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 4/4] clk: ti: " Andy Shevchenko
3 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2023-08-04 14:39 UTC (permalink / raw)
To: Greg Kroah-Hartman, Saravana Kannan, Andy Shevchenko,
Stephen Boyd, Dario Binacchi, Tony Lindgren, linux-kernel,
linux-clk, linux-tegra, linux-omap, linux-hardening
Cc: Rafael J. Wysocki, Peter De Schrijver, Prashant Gaikwad,
Michael Turquette, Thierry Reding, Jonathan Hunter, Tero Kristo,
Kees Cook, Andy Shevchenko
Replace open coded functionality of kstrdup_and_replace() with a call.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
drivers/clk/tegra/clk.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/drivers/clk/tegra/clk.c b/drivers/clk/tegra/clk.c
index 333a3ff0db98..19037346f522 100644
--- a/drivers/clk/tegra/clk.c
+++ b/drivers/clk/tegra/clk.c
@@ -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;
--
2.40.0.1.gaa8946217a0b
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 4/4] clk: ti: Replace kstrdup() + strreplace() with kstrdup_and_replace()
2023-08-04 14:39 [PATCH v2 0/4] clk: Add kstrdup_and_replace() helper and use it Andy Shevchenko
` (2 preceding siblings ...)
2023-08-04 14:39 ` [PATCH v2 3/4] clk: tegra: " Andy Shevchenko
@ 2023-08-04 14:39 ` Andy Shevchenko
3 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2023-08-04 14:39 UTC (permalink / raw)
To: Greg Kroah-Hartman, Saravana Kannan, Andy Shevchenko,
Stephen Boyd, Dario Binacchi, Tony Lindgren, linux-kernel,
linux-clk, linux-tegra, linux-omap, linux-hardening
Cc: Rafael J. Wysocki, Peter De Schrijver, Prashant Gaikwad,
Michael Turquette, Thierry Reding, Jonathan Hunter, Tero Kristo,
Kees Cook, Andy Shevchenko
Replace open coded functionality of kstrdup_and_replace() with a call.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
drivers/clk/ti/clk.c | 4 ++--
drivers/clk/ti/clkctrl.c | 9 +++++----
2 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/drivers/clk/ti/clk.c b/drivers/clk/ti/clk.c
index 3d636938a739..1862958ab412 100644
--- a/drivers/clk/ti/clk.c
+++ b/drivers/clk/ti/clk.c
@@ -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) {
diff --git a/drivers/clk/ti/clkctrl.c b/drivers/clk/ti/clkctrl.c
index 8c40f10280b7..607e34d8e289 100644
--- a/drivers/clk/ti/clkctrl.c
+++ b/drivers/clk/ti/clkctrl.c
@@ -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;
}
--
2.40.0.1.gaa8946217a0b
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-08-04 14:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-04 14:39 [PATCH v2 0/4] clk: Add kstrdup_and_replace() helper and use it Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 1/4] lib/string_helpers: Add kstrdup_and_replace() helper Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 2/4] driver core: Replace kstrdup() + strreplace() with kstrdup_and_replace() Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 3/4] clk: tegra: " Andy Shevchenko
2023-08-04 14:39 ` [PATCH v2 4/4] clk: ti: " Andy Shevchenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).