Skip to contentSkip to conversation

lib: parser: optimize match_NUMER apis to use local array

PATCH Superseded Li Lingfeng 2 participants 4 messages Dec 13, 2022 – Jan 10, 2023 Library code

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lib: parser: optimize match_NUMER apis to use local array

Memory will be allocated to store substring_t in match_strdup(), which means the caller of match_strdup() may need to be scheduled out to wait for reclaiming memory.

Using local array to store substring_t to remove the restriction.

---
v1->v2:
change the name of buffer's length
use match_strlcpy() to copy string and keep string length check

 lib/parser.c | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)
lib/parser.c +23 -19
index bcb23484100e..0d00f0adb063 100644
@@ -11,6 +11,15 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 
+/*
+ * max size needed by diffrent bases to express U64
+ * HEX: "0xFFFFFFFFFFFFFFFF" --> 18
+ * DEC: "18446744073709551615" --> 20
+ * OCT: "01777777777777777777777" --> 23
+ * pick the max one to define NUMBER_BUF_LEN
+ */
+#define NUMBER_BUF_LEN 24
+
 /**
  * match_one - Determines if a string matches a simple pattern
  * @s: the string to examine for presence of the pattern
@@ -129,14 +138,13 @@ EXPORT_SYMBOL(match_token);
 static int match_number(substring_t *s, int *result, int base)
 {
 	char *endp;
-	char *buf;
+	char buf[NUMBER_BUF_LEN];
 	int ret;
 	long val;
 
-	buf = match_strdup(s);
-	if (!buf)
-		return -ENOMEM;
-
+	if ((s->to - s->from) >= NUMBER_BUF_LEN)
+		return -ERANGE;
+	match_strlcpy(buf, s, NUMBER_BUF_LEN);
 	ret = 0;
 	val = simple_strtol(buf, &endp, base);
 	if (endp == buf)
@@ -145,7 +153,6 @@ static int match_number(substring_t *s, int *result, int base)
 		ret = -ERANGE;
 	else
 		*result = (int) val;
-	kfree(buf);
 	return ret;
 }
 
@@ -163,18 +170,16 @@ static int match_number(substring_t *s, int *result, int base)
  */
 static int match_u64int(substring_t *s, u64 *result, int base)
 {
-	char *buf;
+	char buf[NUMBER_BUF_LEN];
 	int ret;
 	u64 val;
 
-	buf = match_strdup(s);
-	if (!buf)
-		return -ENOMEM;
-
+	if ((s->to - s->from) >= NUMBER_BUF_LEN)
+		return -ERANGE;
+	match_strlcpy(buf, s, NUMBER_BUF_LEN);
 	ret = kstrtoull(buf, base, &val);
 	if (!ret)
 		*result = val;
-	kfree(buf);
 	return ret;
 }
 
@@ -206,14 +211,13 @@ EXPORT_SYMBOL(match_int);
  */
 int match_uint(substring_t *s, unsigned int *result)
 {
-	int err = -ENOMEM;
-	char *buf = match_strdup(s);
+	char buf[NUMBER_BUF_LEN];
 
-	if (buf) {
-		err = kstrtouint(buf, 10, result);
-		kfree(buf);
-	}
-	return err;
+	if ((s->to - s->from) >= NUMBER_BUF_LEN)
+		return -ERANGE;
+	match_strlcpy(buf, s, NUMBER_BUF_LEN);
+
+	return kstrtouint(buf, 10, result);
 }
 EXPORT_SYMBOL(match_uint);
 
Signature · 1 line
2.31.1
Acked-by on v2 1/1
#1 lore raw
Quoted 8 lines · Li Lingfeng

Memory will be allocated to store substring_t in match_strdup(), which means the caller of match_strdup() may need to be scheduled out to wait for reclaiming memory.

Using local array to store substring_t to remove the restriction.

Thanks.

Signature · 1 line
tejun
#2 lore raw
In reply to Li Lingfeng

lib/parser.c

Quoted 16 lines · Li Lingfeng

Memory will be allocated to store substring_t in match_strdup(), which means the caller of match_strdup() may need to be scheduled out to wait for reclaiming memory.

Using local array to store substring_t to remove the restriction.

---
v1->v2:
change the name of buffer's length
use match_strlcpy() to copy string and keep string length check

  lib/parser.c | 42 +++++++++++++++++++++++-------------------
  1 file changed, 23 insertions(+), 19 deletions(-)
lib/parser.c +0 -0

I can't find the maintainer of this module through the script, does anyone know?

[lilingfeng@ceph-admin linux-next]$ ./scripts/get_maintainer.pl 0001-lib-parser-optimize-match_NUMER-apis-to-use-local-ar.patch

Bad divisor in main::vcs_assign: 0
"GitAuthor: Li Lingfeng" <[email protected]> (authored:1/1=100%,added_lines:23/23=100%,removed_lines:19/19=100%) [email protected] (open list)

Thanks.

Quoted 90 lines

index bcb23484100e..0d00f0adb063 100644

lib/parser.c +23 -19
@@ -11,6 +11,15 @@
  #include <linux/slab.h>
  #include <linux/string.h>
 
+/*
+ * max size needed by diffrent bases to express U64
+ * HEX: "0xFFFFFFFFFFFFFFFF" --> 18
+ * DEC: "18446744073709551615" --> 20
+ * OCT: "01777777777777777777777" --> 23
+ * pick the max one to define NUMBER_BUF_LEN
+ */
+#define NUMBER_BUF_LEN 24
+
  /**
   * match_one - Determines if a string matches a simple pattern
   * @s: the string to examine for presence of the pattern
@@ -129,14 +138,13 @@ EXPORT_SYMBOL(match_token);
  static int match_number(substring_t *s, int *result, int base)
  {
  	char *endp;
-	char *buf;
+	char buf[NUMBER_BUF_LEN];
  	int ret;
  	long val;
 
-	buf = match_strdup(s);
-	if (!buf)
-		return -ENOMEM;
-
+	if ((s->to - s->from) >= NUMBER_BUF_LEN)
+		return -ERANGE;
+	match_strlcpy(buf, s, NUMBER_BUF_LEN);
  	ret = 0;
  	val = simple_strtol(buf, &endp, base);
  	if (endp == buf)
@@ -145,7 +153,6 @@ static int match_number(substring_t *s, int *result, int base)
  		ret = -ERANGE;
  	else
  		*result = (int) val;
-	kfree(buf);
  	return ret;
  }
 
@@ -163,18 +170,16 @@ static int match_number(substring_t *s, int *result, int base)
   */
  static int match_u64int(substring_t *s, u64 *result, int base)
  {
-	char *buf;
+	char buf[NUMBER_BUF_LEN];
  	int ret;
  	u64 val;
 
-	buf = match_strdup(s);
-	if (!buf)
-		return -ENOMEM;
-
+	if ((s->to - s->from) >= NUMBER_BUF_LEN)
+		return -ERANGE;
+	match_strlcpy(buf, s, NUMBER_BUF_LEN);
  	ret = kstrtoull(buf, base, &val);
  	if (!ret)
  		*result = val;
-	kfree(buf);
  	return ret;
  }
 
@@ -206,14 +211,13 @@ EXPORT_SYMBOL(match_int);
   */
  int match_uint(substring_t *s, unsigned int *result)
  {
-	int err = -ENOMEM;
-	char *buf = match_strdup(s);
+	char buf[NUMBER_BUF_LEN];
 
-	if (buf) {
-		err = kstrtouint(buf, 10, result);
-		kfree(buf);
-	}
-	return err;
+	if ((s->to - s->from) >= NUMBER_BUF_LEN)
+		return -ERANGE;
+	match_strlcpy(buf, s, NUMBER_BUF_LEN);
+
+	return kstrtouint(buf, 10, result);
  }
  EXPORT_SYMBOL(match_uint);
 
#3 lore raw
Quoted 17 lines · lilingfeng (A)
Quoted 15 lines · Li Lingfeng

Memory will be allocated to store substring_t in match_strdup(), which means the caller of match_strdup() may need to be scheduled out to wait for reclaiming memory.

Using local array to store substring_t to remove the restriction.

---
v1->v2:
change the name of buffer's length
use match_strlcpy() to copy string and keep string length check

  lib/parser.c | 42 +++++++++++++++++++++++-------------------
  1 file changed, 23 insertions(+), 19 deletions(-)
Quoted 4 lines

lib/parser.c

Quoted 1 line
lib/parser.c +0 -0

I can't find the maintainer of this module through the script, does anyone know?

Oh, send it to Andrew Morton <[email protected]>.

Thanks.

Signature · 1 line
tejun