From nobody Thu Sep 18 07:19:06 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D679C4332F for ; Fri, 9 Dec 2022 06:13:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229790AbiLIGNr (ORCPT ); Fri, 9 Dec 2022 01:13:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229592AbiLIGNo (ORCPT ); Fri, 9 Dec 2022 01:13:44 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F538750B8 for ; Thu, 8 Dec 2022 22:13:43 -0800 (PST) Received: from dggpemm500011.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NT10M1rVGzJqGs; Fri, 9 Dec 2022 14:12:51 +0800 (CST) Received: from huawei.com (10.175.127.227) by dggpemm500011.china.huawei.com (7.185.36.110) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 9 Dec 2022 14:13:40 +0800 From: Li Lingfeng To: CC: , , , , , , , , , Subject: [PATCH-next] lib: parser: optimize match_NUMER apis to use local array Date: Fri, 9 Dec 2022 14:34:34 +0800 Message-ID: <20221209063434.1566682-1-lilingfeng3@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500011.china.huawei.com (7.185.36.110) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 recla= iming memory. Introducing a helper which use local array to store substring_t to remove t= he restriction. Link: https://lore.kernel.org/all/20221104023938.2346986-5-yukuai1@huaweicl= oud.com/ Signed-off-by: Li Lingfeng --- 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 #include =20 +/* + * 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 =3D s->to - s->from; + + if (!s->from) + return -EINVAL; + + if (len > U64_MAX_SIZE) + return -ERANGE; + + memcpy(buf, s->from, len); + buf[len] =3D '\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; =20 - buf =3D match_strdup(s); - if (!buf) - return -ENOMEM; - - ret =3D 0; + ret =3D match_strdup_local(s, buf); + if (ret) + return ret; val =3D simple_strtol(buf, &endp, base); if (endp =3D=3D buf) ret =3D -EINVAL; @@ -145,7 +167,6 @@ static int match_number(substring_t *s, int *result, in= t base) ret =3D -ERANGE; else *result =3D (int) val; - kfree(buf); return ret; } =20 @@ -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; =20 - buf =3D match_strdup(s); - if (!buf) - return -ENOMEM; - + ret =3D match_strdup_local(s, buf); + if (ret) + return ret; ret =3D kstrtoull(buf, base, &val); if (!ret) *result =3D val; - kfree(buf); return ret; } =20 @@ -207,12 +226,13 @@ EXPORT_SYMBOL(match_int); int match_uint(substring_t *s, unsigned int *result) { int err =3D -ENOMEM; - char *buf =3D match_strdup(s); + char buf[U64_MAX_SIZE + 1]; =20 - if (buf) { - err =3D kstrtouint(buf, 10, result); - kfree(buf); - } + err =3D match_strdup_local(s, buf); + if (err) + return err; + + err =3D kstrtouint(buf, 10, result); return err; } EXPORT_SYMBOL(match_uint); --=20 2.31.1