mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH-next] lib: parser: optimize match_NUMER apis to use local array
@ 2022-12-09  6:34 Li Lingfeng
  2022-12-09 16:57 ` Tejun Heo
  0 siblings, 1 reply; 4+ messages in thread
From: Li Lingfeng @ 2022-12-09  6:34 UTC (permalink / raw)
  To: linux-kernel
  Cc: tj, jack, bingjingc, ebiggers, james.smart, houtao1, yi.zhang,
	yangerkun, yukuai3, lilingfeng3

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.

Introducing a helper which use local array to store substring_t to remove the
restriction.

Link: https://lore.kernel.org/all/20221104023938.2346986-5-yukuai1@huaweicloud.com/
Signed-off-by: Li Lingfeng <lilingfeng3@huawei.com>
---
 lib/parser.c | 56 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 38 insertions(+), 18 deletions(-)

diff --git a/lib/parser.c b/lib/parser.c
index bcb23484100e..d4f4c81ff653 100644
--- a/lib/parser.c
+++ b/lib/parser.c
@@ -11,6 +11,30 @@
 #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 U64_MAX_SIZE
+ */
+#define U64_MAX_SIZE 23
+
+static int match_strdup_local(const substring_t *s, char *buf)
+{
+	size_t len = s->to - s->from;
+
+	if (!s->from)
+		return -EINVAL;
+
+	if (len > U64_MAX_SIZE)
+		return -ERANGE;
+
+	memcpy(buf, s->from, len);
+	buf[len] = '\0';
+	return 0;
+}
+
 /**
  * match_one - Determines if a string matches a simple pattern
  * @s: the string to examine for presence of the pattern
@@ -129,15 +153,13 @@ EXPORT_SYMBOL(match_token);
 static int match_number(substring_t *s, int *result, int base)
 {
 	char *endp;
-	char *buf;
+	char buf[U64_MAX_SIZE + 1];
 	int ret;
 	long val;
 
-	buf = match_strdup(s);
-	if (!buf)
-		return -ENOMEM;
-
-	ret = 0;
+	ret = match_strdup_local(s, buf);
+	if (ret)
+		return ret;
 	val = simple_strtol(buf, &endp, base);
 	if (endp == buf)
 		ret = -EINVAL;
@@ -145,7 +167,6 @@ static int match_number(substring_t *s, int *result, int base)
 		ret = -ERANGE;
 	else
 		*result = (int) val;
-	kfree(buf);
 	return ret;
 }
 
@@ -163,18 +184,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[U64_MAX_SIZE + 1];
 	int ret;
 	u64 val;
 
-	buf = match_strdup(s);
-	if (!buf)
-		return -ENOMEM;
-
+	ret = match_strdup_local(s, buf);
+	if (ret)
+		return ret;
 	ret = kstrtoull(buf, base, &val);
 	if (!ret)
 		*result = val;
-	kfree(buf);
 	return ret;
 }
 
@@ -207,12 +226,13 @@ EXPORT_SYMBOL(match_int);
 int match_uint(substring_t *s, unsigned int *result)
 {
 	int err = -ENOMEM;
-	char *buf = match_strdup(s);
+	char buf[U64_MAX_SIZE + 1];
 
-	if (buf) {
-		err = kstrtouint(buf, 10, result);
-		kfree(buf);
-	}
+	err = match_strdup_local(s, buf);
+	if (err)
+		return err;
+
+	err = kstrtouint(buf, 10, result);
 	return err;
 }
 EXPORT_SYMBOL(match_uint);
-- 
2.31.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-12-10 18:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-09  6:34 [PATCH-next] lib: parser: optimize match_NUMER apis to use local array Li Lingfeng
2022-12-09 16:57 ` Tejun Heo
2022-12-10  2:51   ` lilingfeng (A)
2022-12-10 18:37     ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®