lib/string_helpers: Introduce tokenize_user_input()
Subsystems (5)
v3 cover lib/string_helpers: Introduce tokenize_user_input()
Continuation of recent upstream discussion [1] regarding user string tokenization.
First, tokenize_user_input() 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 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 thourough review
[1]: https://lore.kernel.org/alsa-devel/[email protected]/
Cezary Rojewski (2):
lib/string_helpers: Introduce tokenize_user_input()
ASoC: SOF: Remove strsplit_u32() and tokenize_input()
include/linux/string_helpers.h | 2 + lib/string_helpers.c | 45 +++++++++++++++ sound/soc/sof/sof-client-probes.c | 93 ++++--------------------------- 3 files changed, 59 insertions(+), 81 deletions(-)
Signature · 1 line
2.25.1
PATCH v3 1/2 lib/string_helpers: Introduce tokenize_user_input()
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.
--- include/linux/string_helpers.h | 2 ++ lib/string_helpers.c | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)
include/linux/string_helpers.h +2 -0
@@ -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 tokenize_user_input (const char __user *from,size_t count,int **tkns); +#define UNESCAPE_SPACE BIT(0)#define UNESCAPE_OCTAL BIT(1)#define UNESCAPE_HEX BIT(2)
lib/string_helpers.c +45 -0
51 lines not on this page · raw message
Signature · 1 line
2.25.1
Quoted 5 lines · 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.
Thanks! In general it looks good to me, but a few minor comments below.
...
Quoted 1 line
+int
tokenize_user_input(const char __user *from, size_t count, int **tkns);
Not sure how I can deduct from the name what function is actually doing. Suggested new name: int_array_parse_user().
int int_array_parse_user(const char __user *from, size_t count, int **array);
(Note that we have _user suffix for many APIs in the kernel that does interact with user space memory)
...
Quoted 1 line
+ * @tkns: Returned pointer to sequence of integers
array
...
If you are okay with this, you may add my
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
On Thu, Sep 1, 2022 at 8:40 PM Cezary Rojewski
<[email protected]> wrote:
...
Quoted 9 lines
Quoted 1 line
+int
tokenize_user_input(const char __user *from, size_t count, int **tkns);Not sure how I can deduct from the name what function is actually doing. Suggested new name:
int_array_parse_user().int
int_array_parse_user(const char __user *from, size_t count, int **array);(Note that we have
_usersuffix for many APIs in the kernel that does interact with user space memory)
That's why I've added '_user_' in the middle! Anyway, I guess the expectation is that it's a suffix - precisely at the end of the name.
Could we reorder it a bit: "parse_int_array_user"?
Quoted 3 lines
Quoted 1 line
+ * @tkns: Returned pointer to sequence of integers
array
Ack.
Quoted 3 lines
If you are okay with this, you may add my
Reviewed-by Andy Shevchenko <[email protected]>
Quoted 3 lines · Cezary Rojewski
Quoted 2 lines · Andy Shevchenko
On Thu, Sep 1, 2022 at 8:40 PM Cezary Rojewski
<[email protected]> wrote:
...
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
+int
tokenize_user_input(const char __user *from, size_t count, int **tkns);Not sure how I can deduct from the name what function is actually doing. Suggested new name:
int_array_parse_user().int
int_array_parse_user(const char __user *from, size_t count, int **array);
Quoted 7 lines
Quoted 2 lines
(Note that we have
_usersuffix for many APIs in the kernel that does interact with user space memory)That's why I've added '_user_' in the middle! Anyway, I guess the expectation is that it's a suffix - precisely at the end of the name.
Could we reorder it a bit: "
parse_int_array_user"?
Most of the exported functions that have 'parse' word, have it after namespace, but in this case there is no dedicated namespace and it also will be in alignment with parse_options_str. That said, go for it.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v3 2/2 ASoC: SOF: Remove strsplit_u32() and tokenize_input()
In reply to Cezary RojewskiMake use of global user input tokenization helper instead of the internal one as both serve same purpose. With that, both strsplit_u32() and tokenize_input() become unused so remove them.
--- sound/soc/sof/sof-client-probes.c | 93 ++++--------------------------- 1 file changed, 12 insertions(+), 81 deletions(-)
sound/soc/sof/sof-client-probes.c +12 -81
@@ -12,6 +12,7 @@#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 +411,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 +476,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; +size_t bytes; +u32 num_tkns, *tkns;int ret, err;if (priv->extractor_stream_tag ==SOF_PROBES_INVALID_NODE_ID ) {
@@ -557,16 +485,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 =tokenize_user_input (from, count, (int **)&tkns);if (ret <0 )return ret; + + num_tkns = *tkns; bytes =sizeof (*tkns) * num_tkns;if (!num_tkns || (bytes %sizeof (*desc))) { ret = -EINVAL ; goto exit; } - desc = (struct sof_probe_point_desc *)tkns; + desc = (struct sof_probe_point_desc *)&tkns[1 ]; ret =pm_runtime_resume_and_get (dev);if (ret <0 && ret != -EACCES ) {
@@ -603,8 +533,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_tkns, *tkns;int ret, err;if (priv->extractor_stream_tag ==SOF_PROBES_INVALID_NODE_ID ) {
@@ -612,9 +541,11 @@ 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 =tokenize_user_input (from, count, (int **)&tkns);if (ret <0 )return ret; + + num_tkns = *tkns;if (!num_tkns) { ret = -EINVAL ; goto exit;
@@ -626,7 +557,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, &tkns[1 ], num_tkns);if (!ret) ret = count;
Signature · 1 line
2.25.1
Re: [PATCH v3 2/2] ASoC: SOF: Remove strsplit_u32() and tokenize_input()
Quoted 3 lines · Cezary Rojewski
Make use of global user input tokenization helper instead of the internal one as both serve same purpose. With that, both
strsplit_u32()
the same
Quoted 1 line
and
tokenize_input()become unused so remove them.
...
Quoted 4 lines
#include <
linux/debugfs.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
+#include <linux/string_helpers.h>
I believe either blank line needs to be here (to split the sound / SOF group of headers) or this should be put after 'so*'-ones
Quoted 3 lines
#include <
sound/soc.h>
#include <sound/sof/header.h>
#include "sof-client.h"
...
sound/soc/sof/sof-client-probes.c sof_probes_dfs_points_write(struct file *file, const char __user *from, · line 480
Quoted 7 lines
struct sof_probes_priv *priv = cdev->data; struct device *dev = &cdev->auxdev.dev; struct sof_probe_point_desc *desc;sound/soc/sof/sof-client-probes.c +2
-2-size_t num_tkns, bytes; -u32 *tkns; +size_t bytes; +u32 num_tkns, *tkns;
I would expect a longer line first.
Quoted 1 line
int ret, err;
...
With the above addressed
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 2/2] ASoC: SOF: Remove strsplit_u32() and tokenize_input()
Quoted 9 lines · Andy Shevchenko
Quoted 3 lines · Cezary Rojewski
Make use of global user input tokenization helper instead of the internal one as both serve same purpose. With that, both
strsplit_u32()the same
Quoted 1 line
and
tokenize_input()become unused so remove them.
...
Quoted 7 lines
Quoted 4 lines
#include <
linux/debugfs.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
+#include <linux/string_helpers.h>I believe either blank line needs to be here (to split the sound / SOF group of headers) or this should be put after 'so*'-ones
Newline it is then.
Quoted 3 lines
Quoted 3 lines
#include <sound/soc.h> #include <sound/sof/header.h> #include "sof-client.h"
...
Quoted 9 lines
sound/soc/sof/sof-client-probes.c sof_probes_dfs_points_write(struct file *file, const char __user *from, · line 480
Quoted 7 lines
struct sof_probes_priv *priv = cdev->data; struct device *dev = &cdev->auxdev.dev; struct sof_probe_point_desc *desc;sound/soc/sof/sof-client-probes.c +2
-2-size_t num_tkns, bytes; -u32 *tkns; +size_t bytes; +u32 num_tkns, *tkns;I would expect a longer line first.
Ack.
Quoted 3 lines
With the above addressed
Reviewed-by Andy Shevchenko <[email protected]>