* [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user()
@ 2022-09-02 13:32 Cezary Rojewski
2022-09-02 13:32 ` [PATCH v5 1/2] " Cezary Rojewski
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Cezary Rojewski @ 2022-09-02 13:32 UTC (permalink / raw)
To: alsa-devel, broonie
Cc: tiwai, perex, amadeuszx.slawinski, pierre-louis.bossart, hdegoede,
lgirdwood, kai.vehmanen, peter.ujfalusi, ranjani.sridharan,
yung-chuan.liao, willy, linux-kernel, andy, intel-poland,
andy.shevchenko, Cezary Rojewski
Continuation of recent upstream discussion [1] regarding user string
tokenization.
First, parse_int_array_user() is introduced to allow for splitting
specified user string into a sequence of integers. Makes use of
get_options() internally so the parsing logic is not duplicated.
With that done, redundant parts of the sound driver are removed.
Originally similar functionality was added for the SOF sound driver. As
more users are on the horizon, it is desirable to update existing
string_helpers code and provide a unified solution.
Changes in v5:
- fixed kernel doc for parse_int_array_user()
Changes in v4:
- renamed the function to parse_int_array_user()
- at the name several local variable names have been reworded to match
the above
Changes in v3:
- relocated tokenize_user_input() implementation to string_helpers as
requested by Matthew
Changes in v2:
- reused get_options() so no parsing logic is duplicated
- simplified __user variant with help of memdup_user_nul()
Both suggested by Andy, thanks for thorough review
[1]: https://lore.kernel.org/alsa-devel/20220707091301.1282291-1-cezary.rojewski@intel.com/
Cezary Rojewski (2):
lib/string_helpers: Introduce parse_int_array_user()
ASoC: SOF: Remove strsplit_u32() and tokenize_input()
include/linux/string_helpers.h | 2 +
lib/string_helpers.c | 44 +++++++++++++
sound/soc/sof/sof-client-probes.c | 104 ++++++------------------------
3 files changed, 64 insertions(+), 86 deletions(-)
--
2.25.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v5 1/2] lib/string_helpers: Introduce parse_int_array_user()
2022-09-02 13:32 [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user() Cezary Rojewski
@ 2022-09-02 13:32 ` Cezary Rojewski
2022-09-02 13:32 ` [PATCH v5 2/2] ASoC: SOF: Remove strsplit_u32() and tokenize_input() Cezary Rojewski
2022-09-02 14:16 ` [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user() Andy Shevchenko
2 siblings, 0 replies; 7+ messages in thread
From: Cezary Rojewski @ 2022-09-02 13:32 UTC (permalink / raw)
To: alsa-devel, broonie
Cc: tiwai, perex, amadeuszx.slawinski, pierre-louis.bossart, hdegoede,
lgirdwood, kai.vehmanen, peter.ujfalusi, ranjani.sridharan,
yung-chuan.liao, willy, linux-kernel, andy, intel-poland,
andy.shevchenko, Cezary Rojewski
Add new helper function to allow for splitting specified user string
into a sequence of integers. Internally it makes use of get_options() so
the returned sequence contains the integers extracted plus an additional
element that begins the sequence and specifies the integers count.
Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com>
---
include/linux/string_helpers.h | 2 ++
lib/string_helpers.c | 44 ++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+)
diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 4d72258d42fd..dc2e726fd820 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -21,6 +21,8 @@ enum string_size_units {
void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
char *buf, int len);
+int parse_int_array_user(const char __user *from, size_t count, int **array);
+
#define UNESCAPE_SPACE BIT(0)
#define UNESCAPE_OCTAL BIT(1)
#define UNESCAPE_HEX BIT(2)
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 5ed3beb066e6..230020a2e076 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -131,6 +131,50 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
}
EXPORT_SYMBOL(string_get_size);
+/**
+ * parse_int_array_user - Split string into a sequence of integers
+ * @from: The user space buffer to read from
+ * @count: The maximum number of bytes to read
+ * @array: Returned pointer to sequence of integers
+ *
+ * On success @array is allocated and initialized with a sequence of
+ * integers extracted from the @from plus an additional element that
+ * begins the sequence and specifies the integers count.
+ *
+ * Caller takes responsibility for freeing @array when it is no longer
+ * needed.
+ */
+int parse_int_array_user(const char __user *from, size_t count, int **array)
+{
+ int *ints, nints;
+ char *buf;
+ int ret = 0;
+
+ buf = memdup_user_nul(from, count);
+ if (IS_ERR(buf))
+ return PTR_ERR(buf);
+
+ get_options(buf, 0, &nints);
+ if (!nints) {
+ ret = -ENOENT;
+ goto free_buf;
+ }
+
+ ints = kcalloc(nints + 1, sizeof(*ints), GFP_KERNEL);
+ if (!ints) {
+ ret = -ENOMEM;
+ goto free_buf;
+ }
+
+ get_options(buf, nints + 1, ints);
+ *array = ints;
+
+free_buf:
+ kfree(buf);
+ return ret;
+}
+EXPORT_SYMBOL(parse_int_array_user);
+
static bool unescape_space(char **src, char **dst)
{
char *p = *dst, *q = *src;
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v5 2/2] ASoC: SOF: Remove strsplit_u32() and tokenize_input()
2022-09-02 13:32 [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user() Cezary Rojewski
2022-09-02 13:32 ` [PATCH v5 1/2] " Cezary Rojewski
@ 2022-09-02 13:32 ` Cezary Rojewski
2022-09-02 14:25 ` Andy Shevchenko
2022-09-02 14:16 ` [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user() Andy Shevchenko
2 siblings, 1 reply; 7+ messages in thread
From: Cezary Rojewski @ 2022-09-02 13:32 UTC (permalink / raw)
To: alsa-devel, broonie
Cc: tiwai, perex, amadeuszx.slawinski, pierre-louis.bossart, hdegoede,
lgirdwood, kai.vehmanen, peter.ujfalusi, ranjani.sridharan,
yung-chuan.liao, willy, linux-kernel, andy, intel-poland,
andy.shevchenko, Cezary Rojewski
Make use of global integer-array parsing helper instead of the internal
one as both serve same purpose. With that, both strsplit_u32() and
tokenize_input() become unused so remove them.
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com>
---
sound/soc/sof/sof-client-probes.c | 104 ++++++------------------------
1 file changed, 18 insertions(+), 86 deletions(-)
diff --git a/sound/soc/sof/sof-client-probes.c b/sound/soc/sof/sof-client-probes.c
index eb246b823461..aad7fdc665d7 100644
--- a/sound/soc/sof/sof-client-probes.c
+++ b/sound/soc/sof/sof-client-probes.c
@@ -12,6 +12,8 @@
#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
+#include <linux/string_helpers.h>
+
#include <sound/soc.h>
#include <sound/sof/header.h>
#include "sof-client.h"
@@ -410,79 +412,6 @@ static const struct snd_compress_ops sof_probes_compressed_ops = {
.copy = sof_probes_compr_copy,
};
-/**
- * strsplit_u32 - Split string into sequence of u32 tokens
- * @buf: String to split into tokens.
- * @delim: String containing delimiter characters.
- * @tkns: Returned u32 sequence pointer.
- * @num_tkns: Returned number of tokens obtained.
- */
-static int strsplit_u32(char *buf, const char *delim, u32 **tkns, size_t *num_tkns)
-{
- char *s;
- u32 *data, *tmp;
- size_t count = 0;
- size_t cap = 32;
- int ret = 0;
-
- *tkns = NULL;
- *num_tkns = 0;
- data = kcalloc(cap, sizeof(*data), GFP_KERNEL);
- if (!data)
- return -ENOMEM;
-
- while ((s = strsep(&buf, delim)) != NULL) {
- ret = kstrtouint(s, 0, data + count);
- if (ret)
- goto exit;
- if (++count >= cap) {
- cap *= 2;
- tmp = krealloc(data, cap * sizeof(*data), GFP_KERNEL);
- if (!tmp) {
- ret = -ENOMEM;
- goto exit;
- }
- data = tmp;
- }
- }
-
- if (!count)
- goto exit;
- *tkns = kmemdup(data, count * sizeof(*data), GFP_KERNEL);
- if (!(*tkns)) {
- ret = -ENOMEM;
- goto exit;
- }
- *num_tkns = count;
-
-exit:
- kfree(data);
- return ret;
-}
-
-static int tokenize_input(const char __user *from, size_t count,
- loff_t *ppos, u32 **tkns, size_t *num_tkns)
-{
- char *buf;
- int ret;
-
- buf = kmalloc(count + 1, GFP_KERNEL);
- if (!buf)
- return -ENOMEM;
-
- ret = simple_write_to_buffer(buf, count, ppos, from, count);
- if (ret != count) {
- ret = ret >= 0 ? -EIO : ret;
- goto exit;
- }
-
- buf[count] = '\0';
- ret = strsplit_u32(buf, ",", tkns, num_tkns);
-exit:
- kfree(buf);
- return ret;
-}
-
static ssize_t sof_probes_dfs_points_read(struct file *file, char __user *to,
size_t count, loff_t *ppos)
{
@@ -548,8 +477,8 @@ sof_probes_dfs_points_write(struct file *file, const char __user *from,
struct sof_probes_priv *priv = cdev->data;
struct device *dev = &cdev->auxdev.dev;
struct sof_probe_point_desc *desc;
- size_t num_tkns, bytes;
- u32 *tkns;
+ u32 num_elems, *array;
+ size_t bytes;
int ret, err;
if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
@@ -557,16 +486,18 @@ sof_probes_dfs_points_write(struct file *file, const char __user *from,
return -ENOENT;
}
- ret = tokenize_input(from, count, ppos, &tkns, &num_tkns);
+ ret = parse_int_array_user(from, count, (int **)&array);
if (ret < 0)
return ret;
- bytes = sizeof(*tkns) * num_tkns;
- if (!num_tkns || (bytes % sizeof(*desc))) {
+
+ num_elems = *array;
+ bytes = sizeof(*array) * num_elems;
+ if (!num_elems || (bytes % sizeof(*desc))) {
ret = -EINVAL;
goto exit;
}
- desc = (struct sof_probe_point_desc *)tkns;
+ desc = (struct sof_probe_point_desc *)&array[1];
ret = pm_runtime_resume_and_get(dev);
if (ret < 0 && ret != -EACCES) {
@@ -583,7 +514,7 @@ sof_probes_dfs_points_write(struct file *file, const char __user *from,
if (err < 0)
dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
exit:
- kfree(tkns);
+ kfree(array);
return ret;
}
@@ -603,8 +534,7 @@ sof_probes_dfs_points_remove_write(struct file *file, const char __user *from,
struct sof_client_dev *cdev = file->private_data;
struct sof_probes_priv *priv = cdev->data;
struct device *dev = &cdev->auxdev.dev;
- size_t num_tkns;
- u32 *tkns;
+ u32 num_elems, *array;
int ret, err;
if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
@@ -612,10 +542,12 @@ sof_probes_dfs_points_remove_write(struct file *file, const char __user *from,
return -ENOENT;
}
- ret = tokenize_input(from, count, ppos, &tkns, &num_tkns);
+ ret = parse_int_array_user(from, count, (int **)&array);
if (ret < 0)
return ret;
- if (!num_tkns) {
+
+ num_elems = *array;
+ if (!num_elems) {
ret = -EINVAL;
goto exit;
}
@@ -626,7 +558,7 @@ sof_probes_dfs_points_remove_write(struct file *file, const char __user *from,
goto exit;
}
- ret = sof_probes_points_remove(cdev, tkns, num_tkns);
+ ret = sof_probes_points_remove(cdev, &array[1], num_elems);
if (!ret)
ret = count;
@@ -635,7 +567,7 @@ sof_probes_dfs_points_remove_write(struct file *file, const char __user *from,
if (err < 0)
dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
exit:
- kfree(tkns);
+ kfree(array);
return ret;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user()
2022-09-02 13:32 [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user() Cezary Rojewski
2022-09-02 13:32 ` [PATCH v5 1/2] " Cezary Rojewski
2022-09-02 13:32 ` [PATCH v5 2/2] ASoC: SOF: Remove strsplit_u32() and tokenize_input() Cezary Rojewski
@ 2022-09-02 14:16 ` Andy Shevchenko
2022-09-04 10:21 ` Cezary Rojewski
2 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2022-09-02 14:16 UTC (permalink / raw)
To: Cezary Rojewski
Cc: alsa-devel, broonie, tiwai, perex, amadeuszx.slawinski,
pierre-louis.bossart, hdegoede, lgirdwood, kai.vehmanen,
peter.ujfalusi, ranjani.sridharan, yung-chuan.liao, willy,
linux-kernel, andy, intel-poland
On Fri, Sep 02, 2022 at 03:32:54PM +0200, Cezary Rojewski wrote:
> Continuation of recent upstream discussion [1] regarding user string
> tokenization.
>
> First, parse_int_array_user() is introduced to allow for splitting
> specified user string into a sequence of integers. Makes use of
> get_options() internally so the parsing logic is not duplicated.
>
> With that done, redundant parts of the sound driver are removed.
>
> Originally similar functionality was added for the SOF sound driver. As
> more users are on the horizon, it is desirable to update existing
> string_helpers code and provide a unified solution.
Feel free to route it via SOF or related tree since you have my tag
for the library part.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v5 2/2] ASoC: SOF: Remove strsplit_u32() and tokenize_input()
2022-09-02 13:32 ` [PATCH v5 2/2] ASoC: SOF: Remove strsplit_u32() and tokenize_input() Cezary Rojewski
@ 2022-09-02 14:25 ` Andy Shevchenko
2022-09-04 10:09 ` Cezary Rojewski
0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2022-09-02 14:25 UTC (permalink / raw)
To: Cezary Rojewski
Cc: alsa-devel, broonie, tiwai, perex, amadeuszx.slawinski,
pierre-louis.bossart, hdegoede, lgirdwood, kai.vehmanen,
peter.ujfalusi, ranjani.sridharan, yung-chuan.liao, willy,
linux-kernel, andy, intel-poland
On Fri, Sep 02, 2022 at 03:32:56PM +0200, Cezary Rojewski wrote:
> Make use of global integer-array parsing helper instead of the internal
> one as both serve same purpose. With that, both strsplit_u32() and
> tokenize_input() become unused so remove them.
More minor comments...
...
> + ret = parse_int_array_user(from, count, (int **)&array);
> if (ret < 0)
> return ret;
> + num_elems = *array;
> + bytes = sizeof(*array) * num_elems;
> + if (!num_elems || (bytes % sizeof(*desc))) {
!num_elems is a dup since previous already does this check.
...
> + ret = parse_int_array_user(from, count, (int **)&array);
> if (ret < 0)
> return ret;
> +
> + num_elems = *array;
> + if (!num_elems) {
> ret = -EINVAL;
> goto exit;
> }
Ditto.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v5 2/2] ASoC: SOF: Remove strsplit_u32() and tokenize_input()
2022-09-02 14:25 ` Andy Shevchenko
@ 2022-09-04 10:09 ` Cezary Rojewski
0 siblings, 0 replies; 7+ messages in thread
From: Cezary Rojewski @ 2022-09-04 10:09 UTC (permalink / raw)
To: Andy Shevchenko
Cc: alsa-devel, broonie, tiwai, perex, amadeuszx.slawinski,
pierre-louis.bossart, hdegoede, lgirdwood, kai.vehmanen,
peter.ujfalusi, ranjani.sridharan, yung-chuan.liao, willy,
linux-kernel, andy, intel-poland
On 2022-09-02 4:25 PM, Andy Shevchenko wrote:
> On Fri, Sep 02, 2022 at 03:32:56PM +0200, Cezary Rojewski wrote:
>> Make use of global integer-array parsing helper instead of the internal
>> one as both serve same purpose. With that, both strsplit_u32() and
>> tokenize_input() become unused so remove them.
>
> More minor comments...
...
>> + ret = parse_int_array_user(from, count, (int **)&array);
>> if (ret < 0)
>> return ret;
>
>> + num_elems = *array;
>> + bytes = sizeof(*array) * num_elems;
>> + if (!num_elems || (bytes % sizeof(*desc))) {
>
> !num_elems is a dup since previous already does this check.
>> + ret = parse_int_array_user(from, count, (int **)&array);
>> if (ret < 0)
>> return ret;
>> +
>> + num_elems = *array;
>
>> + if (!num_elems) {
>> ret = -EINVAL;
>> goto exit;
>> }
>
> Ditto.
Ack for both.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user()
2022-09-02 14:16 ` [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user() Andy Shevchenko
@ 2022-09-04 10:21 ` Cezary Rojewski
0 siblings, 0 replies; 7+ messages in thread
From: Cezary Rojewski @ 2022-09-04 10:21 UTC (permalink / raw)
To: Andy Shevchenko
Cc: alsa-devel, broonie, tiwai, perex, amadeuszx.slawinski,
pierre-louis.bossart, hdegoede, lgirdwood, kai.vehmanen,
peter.ujfalusi, ranjani.sridharan, yung-chuan.liao, willy,
linux-kernel, andy, intel-poland
On 2022-09-02 4:16 PM, Andy Shevchenko wrote:
> On Fri, Sep 02, 2022 at 03:32:54PM +0200, Cezary Rojewski wrote:
...
>> Originally similar functionality was added for the SOF sound driver. As
>> more users are on the horizon, it is desirable to update existing
>> string_helpers code and provide a unified solution.
>
> Feel free to route it via SOF or related tree since you have my tag
> for the library part.
Thanks for the review. Intention is have both patches routes through
Mark's tree (thus the explicit To's).
Regards,
Czarek
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-09-04 10:21 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-02 13:32 [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user() Cezary Rojewski
2022-09-02 13:32 ` [PATCH v5 1/2] " Cezary Rojewski
2022-09-02 13:32 ` [PATCH v5 2/2] ASoC: SOF: Remove strsplit_u32() and tokenize_input() Cezary Rojewski
2022-09-02 14:25 ` Andy Shevchenko
2022-09-04 10:09 ` Cezary Rojewski
2022-09-02 14:16 ` [PATCH v5 0/2] lib/string_helpers: Introduce parse_int_array_user() Andy Shevchenko
2022-09-04 10:21 ` Cezary Rojewski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox