diff mbox series

[v3,1/2] lib/string_helpers: Introduce tokenize_user_input()

Message ID 20220901175022.334824-2-cezary.rojewski@intel.com (mailing list archive)
State New, archived
Headers show
Series lib/string_helpers: Introduce tokenize_user_input() | expand

Commit Message

Cezary Rojewski Sept. 1, 2022, 5:50 p.m. UTC
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>
Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com>
---
 include/linux/string_helpers.h |  2 ++
 lib/string_helpers.c           | 45 ++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

Comments

Andy Shevchenko Sept. 1, 2022, 7:34 p.m. UTC | #1
On Thu, Sep 1, 2022 at 8:40 PM Cezary Rojewski
<cezary.rojewski@intel.com> wrote:
>
> 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.

...

> +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)

...

> + * @tkns:      Returned pointer to sequence of integers

array

...

If you are okay with this, you may add my
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Cezary Rojewski Sept. 2, 2022, 7:46 a.m. UTC | #2
On 2022-09-01 9:34 PM, Andy Shevchenko wrote:
> On Thu, Sep 1, 2022 at 8:40 PM Cezary Rojewski
> <cezary.rojewski@intel.com> wrote:

...

>> +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)


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"?

>> + * @tkns:      Returned pointer to sequence of integers
> 
> array

Ack.

> If you are okay with this, you may add my
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
>
Andy Shevchenko Sept. 2, 2022, 9:04 a.m. UTC | #3
On Fri, Sep 2, 2022 at 10:46 AM Cezary Rojewski
<cezary.rojewski@intel.com> wrote:
> On 2022-09-01 9:34 PM, Andy Shevchenko wrote:
> > On Thu, Sep 1, 2022 at 8:40 PM Cezary Rojewski
> > <cezary.rojewski@intel.com> wrote:

...

> >> +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)
>
> 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.
diff mbox series

Patch

diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 4d72258d42fd..97583dae556f 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 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)
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 5ed3beb066e6..f878afccab4c 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -131,6 +131,51 @@  void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
 }
 EXPORT_SYMBOL(string_get_size);
 
+/**
+ * tokenize_user_input - Split string into a sequence of integers
+ * @from:	The user space buffer to read from
+ * @ppos:	The current position in the buffer
+ * @count:	The maximum number of bytes to read
+ * @tkns:	Returned pointer to sequence of integers
+ *
+ * On success @tkns 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 @tkns when it is no longer
+ * needed.
+ */
+int tokenize_user_input(const char __user *from, size_t count, int **tkns)
+{
+	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);
+	*tkns = ints;
+
+free_buf:
+	kfree(buf);
+	return ret;
+}
+EXPORT_SYMBOL(tokenize_user_input);
+
 static bool unescape_space(char **src, char **dst)
 {
 	char *p = *dst, *q = *src;