lib/string_helpers: Introduce strsplit_u32()
Subsystems (5)
PATCH 1/2 lib/string_helpers: Introduce strsplit_u32()
Add strsplit_u32() and its __user variant to allow for splitting specified string into array of u32 tokens.
Originally this functionality was added for the SOF sound driver. As more users are on the horizon, relocate it so it becomes a common good.
--- include/linux/string_helpers.h | 3 + lib/string_helpers.c | 96 +++++++++++++++++++++++++++++++ sound/soc/sof/sof-client-probes.c | 51 +--------------- 3 files changed, 100 insertions(+), 50 deletions(-)
include/linux/string_helpers.h +3 -0
@@ -126,4 +126,7 @@ static inline const char *str_enabled_disabled(bool v) return v ? "enabled" : "disabled"; } +int strsplit_u32 (const char *str,const char *delim,u32 **tkns,size_t *num_tkns); +int strsplit_u32_user (const char __user *from,size_t count,loff_t *ppos,const char *delim, +u32 **tkns,size_t *num_tkns);#endif
lib/string_helpers.c +96 -0
99 lines not on this page · raw message
sound/soc/sof/sof-client-probes.c +1 -50
63 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 2/2 ASoC: SOF: Remove tokenize_input()
Now that we have strsplit_u32_user() tokenize_input() is needed no longer. Remove it and update all occurrences of its usage.
--- sound/soc/sof/sof-client-probes.c | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-)
sound/soc/sof/sof-client-probes.c +2 -25
@@ -411,29 +411,6 @@ static const struct snd_compress_ops sof_probes_compressed_ops = { .copy = sof_probes_compr_copy, }; -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) {
@@ -508,7 +485,7 @@ sof_probes_dfs_points_write(struct file *file, const char __user *from,return -ENOENT ; } - ret =tokenize_input (from, count, ppos, &tkns, &num_tkns); + ret =strsplit_u32_user (from, count, ppos,"," , &tkns, &num_tkns);if (ret <0 )return ret; bytes =sizeof (*tkns) * num_tkns;
@@ -563,7 +540,7 @@ 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 =strsplit_u32_user (from, count, ppos,"," , &tkns, &num_tkns);if (ret <0 )return ret;if (!num_tkns) {
Signature · 1 line
2.25.1
Péter Ujfalusi
maintainerQuoted 57 lines · Cezary Rojewski
Add
strsplit_u32()and its__uservariant to allow for splitting specified string into array of u32 tokens.Originally this functionality was added for the SOF sound driver. As more users are on the horizon, relocate it so it becomes a common good.
Signed-off-by Cezary Rojewski <[email protected]>--- include/linux/string_helpers.h | 3 + lib/string_helpers.c | 96 +++++++++++++++++++++++++++++++ sound/soc/sof/sof-client-probes.c | 51 +--------------- 3 files changed, 100 insertions(+), 50 deletions(-)include/linux/string_helpers.h +3
-0@@ -126,4 +126,7 @@ static inline const char *str_enabled_disabled(bool v) return v ? "enabled" : "disabled"; } +int strsplit_u32 (const char *str,const char *delim,u32 **tkns,size_t *num_tkns); +int strsplit_u32_user (const char __user *from,size_t count,loff_t *ppos,const char *delim, +u32 **tkns,size_t *num_tkns);#endif lib/string_helpers.c +24
-0@@ -984,3 +984,99 @@ void fortify_panic(const char *name) }EXPORT_SYMBOL (fortify_panic);#endif /* CONFIG_FORTIFY_SOURCE */ + +/** +* strsplit_u32 - Split string into sequence of u32 tokens +* @str: The string to split into tokens. +* @delim: The string containing delimiter characters. +* @tkns: Returned u32 sequence pointer. +* @num_tkns: Returned number of tokens obtained. +* +* On success @num_tkns and @tkns are assigned the number of tokens extracted +* and the array itself respectively. +* Caller takes responsibility for freeing @tkns when no longer needed. +*/ +int strsplit_u32 (const char *str,const char *delim,u32 **tkns,size_t *num_tkns) +{ +size_t max_count =32 ; +size_t count =0 ; +char *s, **p; +u32 *buf, *tmp; +int ret =0 ; + + p = (char **)&str; + *tkns =NULL ; + *num_tkns =0 ; +
lib/string_helpers.c void fortify_panic(const char *name) · line 1020
Quoted 10 lines
lib/string_helpers.c +10
-0+ buf =kcalloc (max_count,sizeof (*buf),GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + +while ((s =strsep (p, delim)) !=NULL ) { + ret =kstrtouint (s,0 , buf + count); +if (ret) + goto free_buf; + +if (++count > max_count) {
I think this should be as it was originally:
if (++count >= max_count) {
Otherwise when we reach the max_count we would not realloc to get more space and the data + max_count is pointing outside of the allocated area.
Quoted 131 lines
lib/string_helpers.c +62
-0+ max_count *=2 ; + tmp =krealloc (buf, max_count *sizeof (*buf),GFP_KERNEL ); +if (!tmp) { + ret = -ENOMEM ; + goto free_buf; + } + buf = tmp; + } + } + +if (!count) + goto free_buf; + *tkns =kmemdup (buf, count *sizeof (*buf),GFP_KERNEL ); +if (*tkns ==NULL ) { + ret = -ENOMEM ; + goto free_buf; + } + *num_tkns = count; + +free_buf: +kfree (buf); +return ret; +} +EXPORT_SYMBOL (strsplit_u32); + +/** +* strsplit_u32_user - Split string into sequence of u32 tokens +* @from: The user space buffer to read from +* @ppos: The current position in the buffer +* @count: The maximum number of bytes to read +* @delim: The string containing delimiter characters. +* @tkns: Returned u32 sequence pointer. +* @num_tkns: Returned number of tokens obtained. +* +* On success @num_tkns and @tkns are assigned the number of tokens extracted +* and the array itself respectively. +* Caller takes responsibility for freeing @tkns when no longer needed. +*/ +int strsplit_u32_user (const char __user *from,size_t count,loff_t *ppos,const char *delim, +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 ) ? ret : -EIO ; + goto free_buf; + } + + buf[count] ='\0' ; + ret =strsplit_u32 (buf, delim, tkns, num_tkns); + +free_buf: +kfree (buf); +return ret; +} +EXPORT_SYMBOL (strsplit_u32_user);sound/soc/sof/sof-client-probes.c +1
-50@@ -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,56 +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) {
Signature · 1 line
Péter
Quoted 1 line · Péter Ujfalusi
On 07/07/2022 12:13, Cezary Rojewski wrote:
...
Quoted 26 lines
Quoted 12 lines
lib/string_helpers.c +12
-0+int strsplit_u32 (const char *str,const char *delim,u32 **tkns,size_t *num_tkns) +{ +size_t max_count =32 ; +size_t count =0 ; +char *s, **p; +u32 *buf, *tmp; +int ret =0 ; + + p = (char **)&str; + *tkns =NULL ; + *num_tkns =0 ; +lib/string_helpers.c void fortify_panic(const char *name) · line 1020
Quoted 10 lines
lib/string_helpers.c +10
-0+ buf =kcalloc (max_count,sizeof (*buf),GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + +while ((s =strsep (p, delim)) !=NULL ) { + ret =kstrtouint (s,0 , buf + count); +if (ret) + goto free_buf; + +if (++count > max_count) {I think this should be as it was originally:
if (++count >=max_count) {
Quoted 2 lines
Otherwise when we reach the
max_countwe would not realloc to get more space and the data +max_countis pointing outside of the allocated area.
I believe you're right. Will change in v2.
Regards,
Czarek
Quoted 3 lines · Cezary Rojewski
Add
strsplit_u32()and its__uservariant to allow for splitting specified string into array of u32 tokens.
And I believe we have more of this done in old code. Since all callers use ',' as a delimiter, have you considered using get_options()?
Quoted 2 lines
Originally this functionality was added for the SOF sound driver. As more users are on the horizon, relocate it so it becomes a common good.
Maybe it can be fixed just there.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Cezary Rojewski
Add
strsplit_u32()and its__uservariant to allow for splitting specified string into array of u32 tokens.
Quoted 8 lines
And I believe we have more of this done in old code. Since all callers use ',' as a delimiter, have you considered using
get_options()?Quoted 2 lines
Originally this functionality was added for the SOF sound driver. As more users are on the horizon, relocate it so it becomes a common good.
Maybe it can be fixed just there.
Forgot to add that we (trying to) don't accept new code in the lib w.o. test cases. get_options() is somehow covered. If you have different test cases in mind, do not hesitate to add!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 9 lines · Andy Shevchenko
Quoted 3 lines · Cezary Rojewski
Add
strsplit_u32()and its__uservariant to allow for splitting specified string into array of u32 tokens.And I believe we have more of this done in old code. Since all callers use ',' as a delimiter, have you considered using
get_options()?
Thanks for your input, Andy.
When I'd written the very first version of this function many months ago, get_options() looked as it does not fulfill our needs. It seems to be true even today: caller needs to know the number of elements in an array upfront. Also, kstrtox() takes into account '0x' and modifies the base accordingly if that's the case. simple_strtoull() looks as not capable of doing the same thing.
The goal is to be able to parse input such as:
0x1000003,0,0,0x1000004,0,0
into a sequence of 6 uints, filling the *tkns and *num_tkns for the caller.
Quoted 4 lines
Quoted 2 lines
Originally this functionality was added for the SOF sound driver. As more users are on the horizon, relocate it so it becomes a common good.
Maybe it can be fixed just there.
avs-driver, which is also part of the ASoC framework has very similar debug-interface. I believe there's no need to duplicate the functions - move them to common code instead.
Regards,
Czarek
Quoted 12 lines · Cezary Rojewski
Quoted 9 lines · Andy Shevchenko
Quoted 3 lines · Cezary Rojewski
Add
strsplit_u32()and its__uservariant to allow for splitting specified string into array of u32 tokens.And I believe we have more of this done in old code. Since all callers use ',' as a delimiter, have you considered using
get_options()?
Quoted 6 lines
Thanks for your input, Andy.
When I'd written the very first version of this function many months ago,
get_options()looked as it does not fulfill our needs. It seems to be true even today: caller needs to know the number of elements in an array upfront.
Have you read a kernel doc for it? It does return the number of elements at the first pass.
Quoted 3 lines
Also,
kstrtox()takes into account '0x' and modifies the base accordingly if that's the case.simple_strtoull()looks as not capable of doing the same thing.
How come?! It does parse all known prefixes: 0x, 0, +, -.
Quoted 5 lines
The goal is to be able to parse input such as:
0x1000003,0,0,0x1000004,0,0
into a sequence of 6 uints, filling the *tkns and
*num_tknsfor the caller.
Yes. Have you checked the test cases for get_options()?
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Originally this functionality was added for the SOF sound driver. As more users are on the horizon, relocate it so it becomes a common good.
Maybe it can be fixed just there.
avs-driver, which is also part of the ASoC framework has very similar debug-interface. I believe there's no need to duplicate the functions - move them to common code instead.
Taking the above into account, please try to use get_options() and then tell me what's not working with it. If so, we will add test cases to get_options() and fix it.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 1:33 PM Cezary Rojewski
<[email protected]> wrote:
...
Quoted 3 lines
Taking the above into account, please try to use
get_options()and then tell me what's not working with it. If so, we will add test cases toget_options()and fix it.
That said, I would probably expect new cases for hexdecimal input along with using unsigned int * as an acceptor to see that there are no bugs related to signed vs. unsigned.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 1:33 PM Cezary Rojewski
<[email protected]> wrote:
...
Quoted 7 lines
Quoted 4 lines
When I'd written the very first version of this function many months ago,
get_options()looked as it does not fulfill our needs. It seems to be true even today: caller needs to know the number of elements in an array upfront.Have you read a kernel doc for it? It does return the number of elements at the first pass.
Yes, I've checked several parts of it. Perhaps I did miss something but simple_strtoull() doc reads: use kstrtox() instead. Thus the strsplit_u32() makes use of kstrtox().
Quoted 5 lines
Quoted 3 lines
Also,
kstrtox()takes into account '0x' and modifies the base accordingly if that's the case.simple_strtoull()looks as not capable of doing the same thing.How come?! It does parse all known prefixes: 0x, 0, +, -.
Hmm.. doc says that it stops at the first non-digit character. Will re-check.
Quoted 8 lines
Quoted 5 lines
The goal is to be able to parse input such as:
0x1000003,0,0,0x1000004,0,0
into a sequence of 6 uints, filling the *tkns and
*num_tknsfor the caller.Yes. Have you checked the test cases for
get_options()?
...
Quoted 7 lines
Quoted 3 lines
avs-driver, which is also part of the ASoC framework has very similar debug-interface. I believe there's no need to duplicate the functions - move them to common code instead.
Taking the above into account, please try to use
get_options()and then tell me what's not working with it. If so, we will add test cases toget_options()and fix it.
There is a difference:
// get_options int ints[5]; s = get_options(str, ARRAY_SIZE(ints), ints); // strsplit_u32() u32 *tkns, num_tkns; ret = strsplit_u32(str, delim, &tkns, &num_tkns);
Nothing has been told upfront for in the second case.
Quoted 3 lines · Cezary Rojewski
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 1:33 PM Cezary Rojewski
<[email protected]> wrote:
...
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
When I'd written the very first version of this function many months ago,
get_options()looked as it does not fulfill our needs. It seems to be true even today: caller needs to know the number of elements in an array upfront.Have you read a kernel doc for it? It does return the number of elements at the first pass.
Yes, I've checked several parts of it. Perhaps I did miss something but
simple_strtoull()doc reads: usekstrtox()instead.
Doc was fixed to make clearer that in some cases it's okay to use simple_strtox(). And this was exactly due to obfuscation code with this recommendation. Yes, in general one supposed to use kstrtox(), but it's not 100% obligatory.
Quoted 2 lines
Thus the
strsplit_u32()
makes use ofkstrtox().
Yeah...
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Also,
kstrtox()takes into account '0x' and modifies the base accordingly if that's the case.simple_strtoull()looks as not capable of doing the same thing.How come?! It does parse all known prefixes: 0x, 0, +, -.
Hmm.. doc says that it stops at the first non-digit character. Will re-check.
Yes, but under non-digit implies the standard prefixes of digits. simple_strtox() and kstrotox() use the very same function for prefixes.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
The goal is to be able to parse input such as:
0x1000003,0,0,0x1000004,0,0
into a sequence of 6 uints, filling the *tkns and
*num_tknsfor the caller.Yes. Have you checked the test cases for
get_options()?
(1)
...
Quoted 20 lines
Quoted 7 lines
Quoted 3 lines
avs-driver, which is also part of the ASoC framework has very similar debug-interface. I believe there's no need to duplicate the functions - move them to common code instead.
Taking the above into account, please try to use
get_options()and then tell me what's not working with it. If so, we will add test cases toget_options()and fix it.There is a difference:
// get_options int ints[5]; s = get_options(str, ARRAY_SIZE(ints), ints); // strsplit_u32() u32 *tkns, num_tkns; ret = strsplit_u32(str, delim, &tkns, &num_tkns);
Quoted 1 line
Nothing has been told upfront for in the second case.
It seems you are missing the (1). The code has checks for the case where you can do get number upfront, it would just require two passes, but it's nothing in comparison of heave realloc().
unsigned int *tokens;
char *p;
int num;
p = get_options(str, 0, &num);
if (num == 0)
// No numbers in the string!
tokens = kcalloc(num + 1, sizeof(*tokens), GFP_KERNEL); if (!tokens)
return -ENOMEM;
p = get_oprions(str, num, &tokens);
if (*p)
// String was parsed only partially! // assuming it's not a fatal error
return tokens;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Péter Ujfalusi
maintainerQuoted 88 lines · Andy Shevchenko
Quoted 3 lines · Cezary Rojewski
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 1:33 PM Cezary Rojewski
<[email protected]> wrote:...
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
When I'd written the very first version of this function many months ago,
get_options()looked as it does not fulfill our needs. It seems to be true even today: caller needs to know the number of elements in an array upfront.Have you read a kernel doc for it? It does return the number of elements at the first pass.
Yes, I've checked several parts of it. Perhaps I did miss something but
simple_strtoull()doc reads: usekstrtox()instead.Doc was fixed to make clearer that in some cases it's okay to use
simple_strtox(). And this was exactly due to obfuscation code with this recommendation. Yes, in general one supposed to usekstrtox(), but it's not 100% obligatory.Quoted 2 lines
Thus the
strsplit_u32()
makes use ofkstrtox().Yeah...
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Also,
kstrtox()takes into account '0x' and modifies the base accordingly if that's the case.simple_strtoull()looks as not capable of doing the same thing.How come?! It does parse all known prefixes: 0x, 0, +, -.
Hmm.. doc says that it stops at the first non-digit character. Will re-check.
Yes, but under non-digit implies the standard prefixes of digits.
simple_strtox()andkstrotox()use the very same function for prefixes.Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
The goal is to be able to parse input such as:
0x1000003,0,0,0x1000004,0,0
into a sequence of 6 uints, filling the *tkns and
*num_tknsfor the caller.Yes. Have you checked the test cases for
get_options()?(1)
...
Quoted 20 lines
Quoted 7 lines
Quoted 3 lines
avs-driver, which is also part of the ASoC framework has very similar debug-interface. I believe there's no need to duplicate the functions - move them to common code instead.
Taking the above into account, please try to use
get_options()and then tell me what's not working with it. If so, we will add test cases toget_options()and fix it.There is a difference:
// get_options int ints[5]; s = get_options(str, ARRAY_SIZE(ints), ints); // strsplit_u32() u32 *tkns, num_tkns; ret = strsplit_u32(str, delim, &tkns, &num_tkns);Quoted 1 line
Nothing has been told upfront for in the second case.
It seems you are missing the (1). The code has checks for the case where you can do get number upfront, it would just require two passes, but it's nothing in comparison of heave
realloc().unsigned int *tokens;
char *p;
int num;p =
get_options(str, 0, &num);
if (num == 0)// No numbers in the string!tokens =
kcalloc(num + 1, sizeof(*tokens), GFP_KERNEL); if (!tokens)
Quoted 9 lines
return -ENOMEM;p =
get_oprions(str, num, &tokens);
if (*p)// String was parsed only partially! // assuming it's not a fatal errorreturn tokens;
This diff is tested and works:
sound/soc/sof/sof-client-probes.c +28 -54
@@ -410,61 +410,11 @@ 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) { +int ret, nints, i, *ints;char *buf; -int ret; buf =kmalloc (count +1 ,GFP_KERNEL );if (!buf)
@@ -473,12 +423,36 @@ static int tokenize_input(const char __user *from, size_t count, ret =simple_write_to_buffer (buf, count, ppos, from, count);if (ret != count) { ret = ret >=0 ? -EIO : ret; - goto exit; + goto free_buf; } buf[count] ='\0' ; - ret =strsplit_u32 (buf,"," , tkns, num_tkns); -exit: +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; + } + + *tkns =kcalloc (nints,sizeof (u32),GFP_KERNEL ); +if (!(*tkns)) { + ret = -ENOMEM ; + goto free_ints; + } + +get_options (buf, nints +1 , ints); + *num_tkns = nints; +for (i =0 ; i < nints; i++) + (*tkns)[i] = ints[i +1 ]; + +free_ints: +kfree (ints); +free_buf:kfree (buf);return ret; }
Could be made nicer with some brain work put to it, we need strict u32 within the IPC message for the array...
Signature · 1 line
Péter
Quoted 2 lines · Péter Ujfalusi
Quoted 1 line · Andy Shevchenko
On Fri, Jul 08, 2022 at 02:13:14PM +0200, Cezary Rojewski wrote:
...
Quoted 16 lines
Quoted 16 lines
It seems you are missing the (1). The code has checks for the case where you can do get number upfront, it would just require two passes, but it's nothing in comparison of heave
realloc().unsigned int *tokens;
char *p;
int num;p =
get_options(str, 0, &num);
if (num == 0)// No numbers in the string!tokens = kcalloc(num + 1, sizeof(*tokens), GFP_KERNEL); if (!tokens) return -ENOMEM;
Quoted 8 lines
Quoted 6 lines
p =
get_oprions(str, num, &tokens);
if (*p)// String was parsed only partially! // assuming it's not a fatal errorreturn tokens;
This diff is tested and works:
Thanks, Peter!
But at least you can memove() to avoid second allocation. ideally to refactor that the result of get_options is consumed as is (it may be casted to struct tokens { int n; u32 v[]; })
...
Quoted 1 line
Could be made nicer with some brain work put to it, we need strict u32 within the IPC message for the array...
True, it needs to be thought through. But I guess you got the idea of how to use existing library routines.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi
<[email protected]> wrote:
...
Quoted 1 line
(it may be casted to struct tokens { int n; u32 v[]; })
On second thought it is better not to do this (it will work on x86, but in general it may be surprising on BE-64).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi
<[email protected]> wrote:
...
Quoted 23 lines
Quoted 22 lines
Quoted 22 lines
It seems you are missing the (1). The code has checks for the case where you can do get number upfront, it would just require two passes, but it's nothing in comparison of heave
realloc().unsigned int *tokens; char *p; int num; p = get_options(str, 0, &num); if (num == 0) // No numbers in the string! tokens = kcalloc(num + 1, sizeof(*tokens), GFP_KERNEL); if (!tokens) return -ENOMEM; p = get_oprions(str, num, &tokens); if (*p) // String was parsed only partially! // assuming it's not a fatal error return tokens;
Quoted 7 lines
Quoted 1 line
This diff is tested and works:
Thanks, Peter!
But at least you can
memove()to avoid second allocation. ideally to refactor that the result ofget_optionsis consumed as is (it may be casted to struct tokens { int n; u32 v[]; })
A long shot, but what if we were to modify get_options() so it takes additional element-size parameter instead? Something like below - I've ignored get_range() though. Will re-visit if this option is viable.
lib/cmdline.c +20 -6
@@ -53,7 +53,7 @@ static int get_range(char **str, int *pint, int n)* for the sake of simplification. */ -int get_option (char **str,int *pint) +int get_num_option (char **str,void *pint,size_t nsize) {char *cur = *str;int value;
@@ -65,7 +65,7 @@ int get_option(char **str, int *pint)else value =simple_strtoull (cur, str,0 );if (pint) - *pint = value; +memcpy (pint, &value,min (nsize,sizeof (value)));if (cur == *str)return 0 ;if (**str ==',' ) {
@@ -77,6 +77,12 @@ int get_option(char **str, int *pint)return 1 ; } +EXPORT_SYMBOL (get_num_option); + +int get_option (char **str,int *pint) +{ +return get_num_option (str, pint,sizeof (*pint)); +}EXPORT_SYMBOL (get_option);/**
@@ -104,15 +110,15 @@ EXPORT_SYMBOL(get_option);* completely parseable). */ -char *get_options (const char *str,int nints,int *ints) +char *get_num_options (const char *str,int nints,void *ints,size_t nsize) {bool validate = (nints ==0 );int res, i =1 ;while (i < nints || validate) { -int *pint = validate ? ints : ints + i; +int *pint = validate ? ints : ints + (i * nsize); - res =get_option ((char **)&str, pint); + res =get_num_option ((char **)&str, pint, nsize);if (res ==0 )break ;if (res ==3 ) {
@@ -133,9 +139,17 @@ char *get_options(const char *str, int nints, int *ints)if (res ==1 )break ; } - ints[0 ] = i -1 ; + --i; +memcpy (ints, &i,min (nsize,sizeof (i)));return (char *)str; } +EXPORT_SYMBOL (get_num_options); + +char *get_options (const char *str,int nints,int *ints) +{ +return get_num_options (str, nints, ints,sizeof (*ints)); +} +EXPORT_SYMBOL (get_options);
Quoted 7 lines · Cezary Rojewski
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi
<[email protected]> wrote:A long shot, but what if we were to modify
get_options()so it takes additional element-size parameter instead?
But why? int / unsigned int, u32 / s32 are all compatible in the current cases.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 11 lines · Andy Shevchenko
Quoted 7 lines · Cezary Rojewski
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi
<[email protected]> wrote:A long shot, but what if we were to modify
get_options()so it takes additional element-size parameter instead?But why? int / unsigned int, u32 / s32 are all compatible in the current cases.
I'd like to avoid any additional operations, so that the retrieved payload can be provided to the IPC handler directly. The IPC handlers for AudioDSP drivers are expecting payload in u32s.
// u32 **tkns, size_t *num_tkns as foo() arguments // u32 *ints, int nints as locals
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_num_options(buf, nints + 1, ints, sizeof(*ints));
*tkns = ints;
*num_tkns = nints;No additional operations in between. The intermediate IPC handler can later refer to the actual payload via &tkns[1] before passing it to the generic one.
Casting int array into u32 array does not feel right, or perhaps I'm missing something like in the doc case.
Regards,
Czarek
Quoted 37 lines · Cezary Rojewski
Quoted 11 lines · Andy Shevchenko
Quoted 7 lines · Cezary Rojewski
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi
<[email protected]> wrote:A long shot, but what if we were to modify
get_options()so it takes additional element-size parameter instead?But why? int / unsigned int, u32 / s32 are all compatible in the current cases.
I'd like to avoid any additional operations, so that the retrieved payload can be provided to the IPC handler directly. The IPC handlers for AudioDSP drivers are expecting payload in u32s.
// u32 **tkns,
size_t*num_tknsasfoo()arguments // u32 *ints, int nints as localsget_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_num_options(buf, nints + 1, ints, sizeof(*ints)); *tkns = ints; *num_tkns = nints;
Quoted 6 lines
No additional operations in between. The intermediate IPC handler can later refer to the actual payload via &tkns[1] before passing it to the generic one.
Casting int array into u32 array does not feel right, or perhaps I'm missing something like in the doc case.
C standard.
int to unsigned int is not promoted. And standard says that "The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any."
I don't know why one needs to have an additional churn here. int and unsigned int are interoperable with the adjustment to the sign when the other argument is signed or lesser rank of.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 47 lines · Andy Shevchenko
Quoted 37 lines · Cezary Rojewski
Quoted 11 lines · Andy Shevchenko
Quoted 7 lines · Cezary Rojewski
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi
<[email protected]> wrote:A long shot, but what if we were to modify
get_options()so it takes additional element-size parameter instead?But why? int / unsigned int, u32 / s32 are all compatible in the current cases.
I'd like to avoid any additional operations, so that the retrieved payload can be provided to the IPC handler directly. The IPC handlers for AudioDSP drivers are expecting payload in u32s.
// u32 **tkns,
size_t*num_tknsasfoo()arguments // u32 *ints, int nints as localsget_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_num_options(buf, nints + 1, ints, sizeof(*ints)); *tkns = ints; *num_tkns = nints;Quoted 6 lines
No additional operations in between. The intermediate IPC handler can later refer to the actual payload via &tkns[1] before passing it to the generic one.
Casting int array into u32 array does not feel right, or perhaps I'm missing something like in the doc case.
C standard.
Quoted 7 lines
int to unsigned int is not promoted. And standard says that "The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any."
I don't know why one needs to have an additional churn here. int and unsigned int are interoperable with the adjustment to the sign when the other argument is signed or lesser rank of.
I still believe that casting blindly is not the way to go. I did explicitly ask about int vs u32, not int vs unsigned int. Please note that these values are later passed to the IPC handlers, and this changes the context a bit. If hw expects u32, then u32 it shall be.
Please correct me if I'm wrong, but there is no guarantee that int is always 32bits long. What is guaranteed though, is that int holds at least -/+ 32,767. Also, values larger than INT_MAX are allowed in the IPC payload.
Regards,
Czarek
Quoted 8 lines · Cezary Rojewski
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Cezary Rojewski
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Cezary Rojewski
Quoted 2 lines · Andy Shevchenko
On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi
<[email protected]> wrote:
...
Quoted 42 lines
Quoted 42 lines
Quoted 29 lines
Quoted 4 lines
Quoted 2 lines
A long shot, but what if we were to modify
get_options()so it takes additional element-size parameter instead?But why? int / unsigned int, u32 / s32 are all compatible in the current cases.
I'd like to avoid any additional operations, so that the retrieved payload can be provided to the IPC handler directly. The IPC handlers for AudioDSP drivers are expecting payload in u32s.
// u32 **tkns,
size_t*num_tknsasfoo()arguments // u32 *ints, int nints as localsget_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_num_options(buf, nints + 1, ints, sizeof(*ints)); *tkns = ints; *num_tkns = nints;Quoted 6 lines
No additional operations in between. The intermediate IPC handler can later refer to the actual payload via &tkns[1] before passing it to the generic one.
Casting int array into u32 array does not feel right, or perhaps I'm missing something like in the doc case.
C standard.
int to unsigned int is not promoted. And standard says that "The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any."
Quoted 6 lines
Quoted 3 lines
I don't know why one needs to have an additional churn here. int and unsigned int are interoperable with the adjustment to the sign when the other argument is signed or lesser rank of.
I still believe that casting blindly is not the way to go. I did explicitly ask about int vs u32,
There is no such type in the C standard.
Quoted 3 lines
not int vs unsigned int. Please note
that these values are later passed to the IPC handlers, and this changes the context a bit. If hw expects u32, then u32 it shall be.
H/W doesn't expect u32, HW expects bytes or group of bytes with:
1) dedicated address alignment (if required);
2) dedicated byte order;
3) dedicated padding (if required).
Correct me if I'm wrong.
Quoted 2 lines
Please correct me if I'm wrong, but there is no guarantee that int is always 32bits long.
There is no guarantee by the C standard, indeed, but there is an upper level guarantee, by the Linux kernel.
Quoted 3 lines
What is guaranteed though, is that int holds at
least -/+ 32,767. Also, values larger thanINT_MAXare allowed in the IPC payload.
Yeah... this is binary protocol, right? So, what limits are you talking about here if they are not applicable there anyway. It's simply different dimension of limits (i.e. bytes and bits and not C language types).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Mark Brown
maintainerQuoted 3 lines · Cezary Rojewski
Please correct me if I'm wrong, but there is no guarantee that int is always 32bits long. What is guaranteed though, is that int holds at least -/+ 32,767. Also, values larger than
INT_MAXare allowed in the IPC payload.
Right, int is just the natural size for an integer on the platform.
Quoted 1 line · Cezary Rojewski
On 2022-07-09 10:42 PM, Andy Shevchenko wrote:
...
Quoted 4 lines
I still believe that casting blindly is not the way to go. I did explicitly ask about int vs u32, not int vs unsigned int. Please note that these values are later passed to the IPC handlers, and this changes the context a bit. If hw expects u32, then u32 it shall be.
What you can do is probably utilize _Generic() which will reduce the code base and allow to use the same template for different types.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 1 line · Cezary Rojewski
On 2022-07-09 10:42 PM, Andy Shevchenko wrote:
...
Quoted 7 lines
Quoted 4 lines
I still believe that casting blindly is not the way to go. I did explicitly ask about int vs u32, not int vs unsigned int. Please note that these values are later passed to the IPC handlers, and this changes the context a bit. If hw expects u32, then u32 it shall be.
What you can do is probably utilize
_Generic()which will reduce the code base and allow to use the same template for different types.
Writing to let you know that the feedback is not ignored, just my TODO queue is large. Will come back once _Generic() idea is verified on my side or when I have more questions. Thanks once again for your input Andy!
Regards,
Czarek
Quoted 5 lines · Andy Shevchenko
Quoted 1 line · Cezary Rojewski
On 2022-07-09 10:42 PM, Andy Shevchenko wrote:
...
Quoted 7 lines
Quoted 4 lines
I still believe that casting blindly is not the way to go. I did explicitly ask about int vs u32, not int vs unsigned int. Please note that these values are later passed to the IPC handlers, and this changes the context a bit. If hw expects u32, then u32 it shall be.
What you can do is probably utilize
_Generic()which will reduce the code base and allow to use the same template for different types.
Hello,
I've spent some time analyzing possible utilization of _Generic() in context of get_options() but in my opinion get_range() complicates things enough that get_range() and get_option() would basically need a copy per type.
If Linux kernel guarantees that sizeof(int), sizeof(unsigned int), sizeof(s32) and sizeof(u32) are all equal (given the currently supported arch set), then indeed modifying get_options() may not be necessary. This plus shamelessly casting (u32 *) to (int *) of course.
What's left to do is the __user helper function. What I have in mind is:
int tokenize_user_input(const char __user *from, size_t count, loff_t *ppos, int **tkns)
{
int *ints, nints;
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) ? ret : -EIO;
goto free_buf;
}
buf[count] = '\0';
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);
*tkns = ints;
ret = 0;free_buf:
kfree(buf); return ret; }
Usage:
u32 *tkns; ret = tokenize_user_input(from, count, ppos, (int **)&tkns);
as a part of fs/libfs.c not lib/cmdline.c. Is such approach acceptable?
Regards,
Czarek
Quoted 1 line · Cezary Rojewski
On 2022-07-12 4:24 PM, Andy Shevchenko wrote:
...
Quoted 4 lines
I've spent some time analyzing possible utilization of
_Generic()in context ofget_options()but in my opinionget_range()complicates things enough thatget_range()andget_option()would basically need a copy per type.
Thanks for keeping us updated.
Quoted 4 lines
If Linux kernel guarantees that sizeof(int), sizeof(unsigned int), sizeof(s32) and sizeof(u32) are all equal (given the currently supported arch set), then indeed modifying
get_options()may not be necessary. This plus shamelessly casting (u32 *) to (int *) of course.
I think as long as Linux kernel states that it requires (at least) 32-bit architecture to run, we are fine. I have heard of course about a funny project of running Linux on 8-bit microcontrollers, but it's such a fun niche, which by the way uses emulation without changing actual 32-bit code, that I won't even talk about.
Quoted 4 lines
What's left to do is the
__userhelper function. What I have in mind is:int
tokenize_user_input(const char __user *from, size_t count, loff_t *ppos, int **tkns)
Quoted 16 lines
{
int *ints, nints;
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) ? ret : -EIO;
goto free_buf;
}
buf[count] = '\0';I guess this may be simplified with memdup_user(). Otherwise it looks like that.
Quoted 1 line
get_options(buf, 0, &nints);
(You don't use ppos here, so it's pointless to use simple_write_to_buffer(), right? I have noticed this pattern in SOF code, which might be simplified the same way as I suggested above)
Quoted 19 lines
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); *tkns = ints; ret = 0;
free_buf:kfree(buf); return ret; }
...
Quoted 1 line
as a part of
fs/libfs.cnotlib/cmdline.c. Is such approach acceptable?
I think so.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
On Tue, Aug 9, 2022 at 11:55 AM Cezary Rojewski
<[email protected]> wrote:
...
Quoted 2 lines
I guess this may be simplified with
memdup_user(). Otherwise it looks like that.
...
Quoted 3 lines
(You don't use ppos here, so it's pointless to use
simple_write_to_buffer(), right? I have noticed this pattern in SOF code, which might be simplified the same way as I suggested above)
Hello Andy,
Given the two major suggestions (memdup_user() and re-using get_options()) that had a major impact on the patch are both provided by you, would you like me to add any tags to the commit message? I'm speaking of Suggested-by or Co-developed-by and such. In you choose 'yes', please specify tags to be added.
By the way, I've provided 'the final form' on thesofproject/linux as PR [1] to see if no regression is caused in basic scenarios.
[1]: https://github.com/thesofproject/linux/pull/3812
Regards,
Czarek
Quoted 1 line · Cezary Rojewski
On 2022-08-09 5:23 PM, Andy Shevchenko wrote:
...
Quoted 5 lines
Given the two major suggestions (
memdup_user()and re-usingget_options()) that had a major impact on the patch are both provided by you, would you like me to add any tags to the commit message? I'm speaking of Suggested-by or Co-developed-by and such. In you choose 'yes', please specify tags to be added.
Suggested-by would be enough.
Quoted 2 lines
By the way, I've provided 'the final form' on thesofproject/linux as PR [1] to see if no regression is caused in basic scenarios.
When you will be ready, send it for upstream review. It would be easier for the kernel community to look at and comment on.
Quoted 1 line
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Cezary Rojewski
On 2022-08-09 5:23 PM, Andy Shevchenko wrote:
...
Quoted 7 lines
Suggested-by would be enough.
Quoted 2 lines
By the way, I've provided 'the final form' on thesofproject/linux as PR [1] to see if no regression is caused in basic scenarios.
When you will be ready, send it for upstream review. It would be easier for the kernel community to look at and comment on.
On its way. Was awaiting your replay so I do not need to send the series twice : ) The PR was there to check if there is no regression on SOF side, at least on a BAT (Basic Acceptable Tests) level.
Regards,
Czarek
lib/cmdline.c int get_option(char **str, int *pint) · line 68
Quoted 3 lines
if (pint)
lib/cmdline.c +1
-1- *pint = value; +memcpy (pint, &value,min (nsize,sizeof (value)));
That is just soooooo broken.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 6 lines · David Laight
lib/cmdline.c int get_option(char **str, int *pint) · line 68
Quoted 4 lines
if (pint)
lib/cmdline.c +1
-1- *pint = value; +memcpy (pint, &value,min (nsize,sizeof (value)));That is just soooooo broken.
OK.
Signature · 2 lines
With Best Regards, Andy Shevchenko