From nobody Sun Feb 8 17:03:56 2026 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 183E737A49C; Tue, 20 Jan 2026 06:59:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892357; cv=none; b=dIAcOaEewg/o/cuygLcOCHrgbBScWZ8Pf5tEERY+sp3Rds3FOztDodAg16Mm0mzvXNfGv2K22NmiguuzKQpJ4OzWzXgFhj0xLhym5IAy6tspyA50KpJVDVS6F6rrYSz1/Pkd0FWnaKN5GWeX7wGAaKJF0mct+CxD77M3z5FguTI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892357; c=relaxed/simple; bh=E4SNZytbhkJgmeZCkP4s6niTOoTmDqj758gCC8dkano=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e4CsDyLT90nKw6Wp/2MtfAUQRfyKF54ttwlMN6biRQKWa7fC53Tnt3KLPL/us9Vzt1n1YCQeo9sAIa3NkIBzly0lfIOEqGIF44AtW/xuyvlZQU1XXGGwIgDctIYOlEKJNo3sFeA8J2et2MtbWjcAaEXcBmWbN18EpckFDPC4DnU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 7e56caaaf5cd11f0b0f03b4cfa9209d1-20260120 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:7c7b3d0f-0de2-45f6-8d98-b583270c1234,IP:0,UR L:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:-25 X-CID-META: VersionHash:a9d874c,CLOUDID:c221de7e3d13d1b5b960f3103e7d49ad,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 7e56caaaf5cd11f0b0f03b4cfa9209d1-20260120 X-User: jiangfeng@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 321619247; Tue, 20 Jan 2026 14:58:58 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Joel Stanley Subject: [PATCH v3 1/8] lib/string_kunit: add correctness test for strlen Date: Tue, 20 Jan 2026 14:58:45 +0800 Message-Id: <20260120065852.166857-2-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260120065852.166857-1-jiangfeng@kylinos.cn> References: <20260120065852.166857-1-jiangfeng@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a KUnit test for strlen() to verify correctness across different string lengths and memory alignments. Suggested-by: Andy Shevchenko Tested-by: Joel Stanley Signed-off-by: Feng Jiang Acked-by: Andy Shevchenko --- lib/tests/string_kunit.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index f9a8e557ba77..88da97e50c8e 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -17,6 +17,9 @@ #define STRCMP_TEST_EXPECT_LOWER(test, fn, ...) KUNIT_EXPECT_LT(test, fn(_= _VA_ARGS__), 0) #define STRCMP_TEST_EXPECT_GREATER(test, fn, ...) KUNIT_EXPECT_GT(test, fn= (__VA_ARGS__), 0) =20 +#define STRING_TEST_MAX_LEN 128 +#define STRING_TEST_MAX_OFFSET 16 + static void string_test_memset16(struct kunit *test) { unsigned i, j, k; @@ -104,6 +107,28 @@ static void string_test_memset64(struct kunit *test) } } =20 +static void string_test_strlen(struct kunit *test) +{ + char *s; + size_t len, offset; + const size_t buf_size =3D STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + = 1; + + s =3D kunit_kzalloc(test, buf_size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, s); + + memset(s, 'A', buf_size); + s[buf_size - 1] =3D '\0'; + + for (offset =3D 0; offset < STRING_TEST_MAX_OFFSET; offset++) { + for (len =3D 0; len <=3D STRING_TEST_MAX_LEN; len++) { + s[offset + len] =3D '\0'; + KUNIT_EXPECT_EQ_MSG(test, strlen(s + offset), len, + "offset:%zu len:%zu", offset, len); + s[offset + len] =3D 'A'; + } + } +} + static void string_test_strchr(struct kunit *test) { const char *test_string =3D "abcdefghijkl"; @@ -618,6 +643,7 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_memset16), KUNIT_CASE(string_test_memset32), KUNIT_CASE(string_test_memset64), + KUNIT_CASE(string_test_strlen), KUNIT_CASE(string_test_strchr), KUNIT_CASE(string_test_strnchr), KUNIT_CASE(string_test_strspn), --=20 2.25.1 From nobody Sun Feb 8 17:03:56 2026 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E3A837F8D9; Tue, 20 Jan 2026 06:59:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892365; cv=none; b=oq3gBmXaInktBJI5tNeiLzN2OwqbBGL+ckd5ZF0fryYQ4D4nalaYyxW5zki3OSxagDrMGQQUjlrBbQQdjPXMraYqdvjBZgOW3ZCG1Lk+RuLgnnbzUgLIHudWcBxxFaoI+0tsRUX32YC3/VUgHUetz+P6k2W57B8UJnDjk+2rQIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892365; c=relaxed/simple; bh=VJADM3myJptQqJf4nWuZoX0mt8lLezXCI516a825Ax0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=odlV+dI7S5tKjpNUaLW7lnvjKmjuWZlxiKU94qug5c4jXlAU4obx0GBrvhotjdP82vxJp17kcx7TTCHm0eWWjkU5FpSY66FDiOFI6tpL3LaDZU5Z5KxlDDkvOVvCfixGk9G/ogui9rE1E4/zEBHto4kMJIle3fON75oBfHCeFhU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 7eb66bc2f5cd11f0b0f03b4cfa9209d1-20260120 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:387eabd2-a60a-4dcf-b223-485ff9e28976,IP:0,UR L:0,TC:0,Content:-5,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:-5 X-CID-META: VersionHash:a9d874c,CLOUDID:6bad49aac750704cd06a10398751cdbc,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 7eb66bc2f5cd11f0b0f03b4cfa9209d1-20260120 X-User: jiangfeng@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 849742615; Tue, 20 Jan 2026 14:58:59 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Joel Stanley Subject: [PATCH v3 2/8] lib/string_kunit: add correctness test for strnlen Date: Tue, 20 Jan 2026 14:58:46 +0800 Message-Id: <20260120065852.166857-3-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260120065852.166857-1-jiangfeng@kylinos.cn> References: <20260120065852.166857-1-jiangfeng@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a KUnit test for strnlen() to verify correctness across different string lengths and memory alignments. Suggested-by: Andy Shevchenko Tested-by: Joel Stanley Signed-off-by: Feng Jiang Acked-by: Andy Shevchenko --- lib/tests/string_kunit.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index 88da97e50c8e..546cf403317c 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -129,6 +129,38 @@ static void string_test_strlen(struct kunit *test) } } =20 +static void string_test_strnlen(struct kunit *test) +{ + char *s; + size_t len, offset; + const size_t buf_size =3D STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + = 1; + + s =3D kunit_kzalloc(test, buf_size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, s); + + memset(s, 'A', buf_size); + s[buf_size - 1] =3D '\0'; + + for (offset =3D 0; offset < STRING_TEST_MAX_OFFSET; offset++) { + for (len =3D 0; len <=3D STRING_TEST_MAX_LEN; len++) { + s[offset + len] =3D '\0'; + + if (len > 0) + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len - 1), len - 1); + if (len > 1) + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len - 2), len - 2); + + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len), len); + + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len + 1), len); + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len + 2), len); + KUNIT_EXPECT_EQ(test, strnlen(s + offset, len + 10), len); + + s[offset + len] =3D 'A'; + } + } +} + static void string_test_strchr(struct kunit *test) { const char *test_string =3D "abcdefghijkl"; @@ -644,6 +676,7 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_memset32), KUNIT_CASE(string_test_memset64), KUNIT_CASE(string_test_strlen), + KUNIT_CASE(string_test_strnlen), KUNIT_CASE(string_test_strchr), KUNIT_CASE(string_test_strnchr), KUNIT_CASE(string_test_strspn), --=20 2.25.1 From nobody Sun Feb 8 17:03:56 2026 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8FA673815EA; Tue, 20 Jan 2026 06:59:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892370; cv=none; b=j2ENPaSQnmvQKgKfPU7aZHnWaK3GQsPShffeC15Ts1kkEzIOJnr7SH1zSxb4yuUfMWlwLBlXSZHW12gNeCELiUeaLLIompZG/HD46zMm9uTWxiELA/bUp2B6mxPV0xzdCWW/UqRSQJ9i4o/3DPs+Iz4IvbBOTvkGsCNTKUbXWXE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892370; c=relaxed/simple; bh=ihW5KIDS4jZierxN+mzABnojOugwz/pn2ZHcesYE2pM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=elL8o72WhU8Gbjh0DCtyAf/rdc/5oc7e4dNXkJvgarSZCHeWewhpgIC8Sm5p/rsBtKVAzSocFyyJOUKlzHwoBm8nNvtyADkXu7FxiSi1c2x3YlDnvxTcpODZSGJ9+QYzW7auWJHQrR713vNIK5LqQJyePGyM9q+tqxFD68w3Eb4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 7f16a884f5cd11f0b0f03b4cfa9209d1-20260120 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:8083bd0f-5248-4d51-aa75-5e12eea1a8ce,IP:0,UR L:0,TC:0,Content:-5,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:-5 X-CID-META: VersionHash:a9d874c,CLOUDID:c60e5b45e1ca02fbdaab10a4ebff9529,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 7f16a884f5cd11f0b0f03b4cfa9209d1-20260120 X-User: jiangfeng@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 2083211889; Tue, 20 Jan 2026 14:58:59 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Joel Stanley Subject: [PATCH v3 3/8] lib/string_kunit: add correctness test for strrchr() Date: Tue, 20 Jan 2026 14:58:47 +0800 Message-Id: <20260120065852.166857-4-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260120065852.166857-1-jiangfeng@kylinos.cn> References: <20260120065852.166857-1-jiangfeng@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a KUnit test to verify strrchr() across various memory alignments and character positions. This ensures the implementation correctly identifies the last occurrence of a character. Suggested-by: Andy Shevchenko Tested-by: Joel Stanley Signed-off-by: Feng Jiang Acked-by: Andy Shevchenko --- lib/tests/string_kunit.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index 546cf403317c..8f836847a80e 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -184,6 +184,35 @@ static void string_test_strchr(struct kunit *test) KUNIT_ASSERT_NULL(test, result); } =20 +static void string_test_strrchr(struct kunit *test) +{ + char *buf; + size_t offset, len; + const size_t buf_size =3D STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + = 1; + + buf =3D kunit_kzalloc(test, buf_size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); + + memset(buf, 'A', buf_size); + buf[buf_size - 1] =3D '\0'; + + for (offset =3D 0; offset < STRING_TEST_MAX_OFFSET; offset++) { + for (len =3D 0; len <=3D STRING_TEST_MAX_LEN; len++) { + buf[offset + len] =3D '\0'; + + KUNIT_EXPECT_PTR_EQ(test, strrchr(buf + offset, 'Z'), NULL); + + if (len > 0) + KUNIT_EXPECT_PTR_EQ(test, strrchr(buf + offset, 'A'), + buf + offset + len - 1); + else + KUNIT_EXPECT_PTR_EQ(test, strrchr(buf + offset, 'A'), NULL); + + buf[offset + len] =3D 'A'; + } + } +} + static void string_test_strnchr(struct kunit *test) { const char *test_string =3D "abcdefghijkl"; @@ -679,6 +708,7 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_strnlen), KUNIT_CASE(string_test_strchr), KUNIT_CASE(string_test_strnchr), + KUNIT_CASE(string_test_strrchr), KUNIT_CASE(string_test_strspn), KUNIT_CASE(string_test_strcmp), KUNIT_CASE(string_test_strcmp_long_strings), --=20 2.25.1 From nobody Sun Feb 8 17:03:56 2026 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D49632D47EA; Tue, 20 Jan 2026 06:59:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892369; cv=none; b=I5c53fzNl5EqhEde806mmcmEwJl6sslDX1V2KLs+26qXW0jcG9MiRqEDCA2TOautiNJdtaFSJLR93YWwpektMQ6djlQoYOMUdtv3xECljrjet35ihPO9QnJO8dE5IliQIpfPyEcWS+8WwblMwjX153kat+LJ42pkfOMZleugn54= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892369; c=relaxed/simple; bh=Jsi1e7C//bWQNdJn4UsweF9WkeTYnrxFL4Y45i9D2pQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KVRgKYflq2/yiyomh9EtQvBmSiz0KFOK51b1kg0UpwARmPapH7q8NcOYX+Ix4eeukJmVR8INjBgVxgZAEVzFDbhC0CntiiSdn56hQMvrMyZ5iPInpUyzulaqs+plmapij8kb87QxU6R1w9hiNdBRuQlicsgL7chbf1TnsBPLia0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 7f779af4f5cd11f0b0f03b4cfa9209d1-20260120 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:0ce710d8-dd33-4617-be18-b53201a1f140,IP:0,UR L:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:-25 X-CID-META: VersionHash:a9d874c,CLOUDID:8ee1a22fec7a92a4a5b841f6a89d7dcc,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 7f779af4f5cd11f0b0f03b4cfa9209d1-20260120 X-User: jiangfeng@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 863038979; Tue, 20 Jan 2026 14:59:00 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Joel Stanley Subject: [PATCH v3 4/8] lib/string_kunit: add performance benchmarks for strlen Date: Tue, 20 Jan 2026 14:58:48 +0800 Message-Id: <20260120065852.166857-5-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260120065852.166857-1-jiangfeng@kylinos.cn> References: <20260120065852.166857-1-jiangfeng@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a benchmarking framework to the string_kunit test suite to measure the execution efficiency of string functions. The implementation is inspired by crc_benchmark(), measuring throughput (MB/s) and latency (ns/call) across a range of string lengths. It includes a warm-up phase, disables preemption during measurement, and uses a fixed seed for reproducible results. This allows for comparing different implementations (e.g., generic C vs. architecture-optimized assembly) within the KUnit environment. Initially, provide benchmarks for strlen(). Suggested-by: Andy Shevchenko Suggested-by: Eric Biggers Tested-by: Joel Stanley Signed-off-by: Feng Jiang --- lib/Kconfig.debug | 11 +++ lib/tests/string_kunit.c | 151 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index ba36939fda79..21b058ae815f 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2475,6 +2475,17 @@ config STRING_HELPERS_KUNIT_TEST depends on KUNIT default KUNIT_ALL_TESTS =20 +config STRING_KUNIT_BENCH + bool "Benchmark string functions at runtime" + depends on STRING_KUNIT_TEST + help + Enable performance measurement for string functions. + + This measures the execution efficiency of string functions + during the KUnit test run. + + If unsure, say N. + config FFS_KUNIT_TEST tristate "KUnit test ffs-family functions at runtime" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index 8f836847a80e..e20e924d1c67 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -6,7 +6,9 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 #include +#include #include +#include #include #include #include @@ -20,6 +22,9 @@ #define STRING_TEST_MAX_LEN 128 #define STRING_TEST_MAX_OFFSET 16 =20 +#define STRING_BENCH_SEED 888 +#define STRING_BENCH_WORKLOAD 1000000UL + static void string_test_memset16(struct kunit *test) { unsigned i, j, k; @@ -700,6 +705,151 @@ static void string_test_strends(struct kunit *test) KUNIT_EXPECT_TRUE(test, strends("", "")); } =20 +/* Target string lengths for benchmarking */ +static const size_t bench_lens[] =3D { + 0, 1, 7, 8, 16, 31, 64, 127, 512, 1024, 3173, 4096 +}; + +/** + * alloc_max_bench_buffer() - Allocate buffer for the max test case. + * @test: KUnit context for managed allocation. + * @lens: Array of lengths used in the benchmark cases. + * @count: Number of elements in the @lens array. + * @buf_len: [out] Pointer to store the actually allocated buffer + * size (including null). + * + * Return: Pointer to the allocated memory, or NULL on failure. + */ +static void *alloc_max_bench_buffer(struct kunit *test, + const size_t *lens, size_t count, size_t *buf_len) +{ + void *buf; + size_t i, max_len =3D 0; + + for (i =3D 0; i < count; i++) { + if (max_len < lens[i]) + max_len =3D lens[i]; + } + + /* Add space for NUL terminator */ + max_len +=3D 1; + + buf =3D kunit_kzalloc(test, max_len, GFP_KERNEL); + if (buf && buf_len) + *buf_len =3D max_len; + + return buf; +} + +/** + * fill_random_string() - Fill buffer with random non-null bytes. + * @buf: Buffer to fill. + * @len: Number of bytes to fill. + */ +static void fill_random_string(char *buf, size_t len) +{ + size_t i; + struct rnd_state state; + + if (!buf || !len) + return; + + /* Use a fixed seed to ensure deterministic benchmark results */ + prandom_seed_state(&state, 888); + prandom_bytes_state(&state, buf, len); + + /* Replace null bytes to avoid early string termination */ + for (i =3D 0; i < len; i++) { + if (buf[i] =3D=3D '\0') + buf[i] =3D 0x01; + } + + buf[len - 1] =3D '\0'; +} + +/** + * STRING_BENCH() - Benchmark string functions. + * @iters: Number of iterations to run. + * @func: Function to benchmark. + * @...: Variable arguments passed to @func. + * + * Disables preemption and measures the total time in nanoseconds to execu= te + * @func(@__VA_ARGS__) for @iters times, including a small warm-up phase. + * + * Context: Disables preemption during measurement. + * Return: Total execution time in nanoseconds (u64). + */ +#define STRING_BENCH(iters, func, ...) \ +({ \ + u64 __bn_t; \ + size_t __bn_i; \ + size_t __bn_iters =3D (iters); \ + size_t __bn_warm_iters =3D max_t(size_t, __bn_iters / 10, 50U); \ + /* Volatile function pointer prevents dead code elimination */ \ + typeof(func) (* volatile __func) =3D (func); \ + \ + for (__bn_i =3D 0; __bn_i < __bn_warm_iters; __bn_i++) \ + (void)__func(__VA_ARGS__); \ + \ + preempt_disable(); \ + __bn_t =3D ktime_get_ns(); \ + for (__bn_i =3D 0; __bn_i < __bn_iters; __bn_i++) \ + (void)__func(__VA_ARGS__); \ + __bn_t =3D ktime_get_ns() - __bn_t; \ + preempt_enable(); \ + __bn_t; \ +}) + +/** + * STRING_BENCH_BUF() - Benchmark harness for single-buffer functions. + * @test: KUnit context. + * @buf_name: Local char * variable name to be defined. + * @buf_size: Local size_t variable name to be defined. + * @func: Function to benchmark. + * @...: Extra arguments for @func. + * + * Prepares a randomized, null-terminated buffer and iterates through leng= ths + * in bench_lens, defining @buf_name and @buf_size in each loop. + */ +#define STRING_BENCH_BUF(test, buf_name, buf_size, func, ...) \ +do { \ + char *buf_name, *_bn_buf; \ + size_t buf_size, _bn_i, _bn_iters, _bn_size =3D 0; \ + u64 _bn_t, _bn_mbps =3D 0, _bn_lat =3D 0; \ + \ + if (!IS_ENABLED(CONFIG_STRING_KUNIT_BENCH)) \ + kunit_skip(test, "not enabled"); \ + \ + _bn_buf =3D alloc_max_bench_buffer(test, bench_lens, \ + ARRAY_SIZE(bench_lens), &_bn_size); \ + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, _bn_buf); \ + \ + fill_random_string(_bn_buf, _bn_size); \ + _bn_buf[_bn_size - 1] =3D '\0'; \ + \ + for (_bn_i =3D 0; _bn_i < ARRAY_SIZE(bench_lens); _bn_i++) { \ + buf_size =3D bench_lens[_bn_i]; \ + buf_name =3D _bn_buf + _bn_size - buf_size - 1; \ + _bn_iters =3D STRING_BENCH_WORKLOAD / \ + max_t(size_t, buf_size, 1U); \ + \ + _bn_t =3D STRING_BENCH(_bn_iters, func, ##__VA_ARGS__); \ + \ + if (_bn_t > 0) { \ + _bn_mbps =3D (u64)(buf_size) * _bn_iters * 1000; \ + _bn_mbps =3D div64_u64(_bn_mbps, _bn_t); \ + _bn_lat =3D div64_u64(_bn_t, _bn_iters); \ + } \ + kunit_info(test, "len=3D%zu: %llu MB/s (%llu ns/call)\n", \ + buf_size, _bn_mbps, _bn_lat); \ + } \ +} while (0) + +static void string_bench_strlen(struct kunit *test) +{ + STRING_BENCH_BUF(test, buf, len, strlen, buf); +} + static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_memset16), KUNIT_CASE(string_test_memset32), @@ -725,6 +875,7 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_strtomem), KUNIT_CASE(string_test_memtostr), KUNIT_CASE(string_test_strends), + KUNIT_CASE(string_bench_strlen), {} }; =20 --=20 2.25.1 From nobody Sun Feb 8 17:03:56 2026 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25CDF36D50C; Tue, 20 Jan 2026 06:59:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892363; cv=none; b=Bye2zLxsJE78grwDYNief2hO5PHk/52j3t3CJRQt7DCQEJDtl9ZjyXZwV/7+WPZGx/f0IBcODo79ZKL5tioy1/wA0wQnzyyvkyNfEoi3X5qPgM692GgJypBkTMmcQYUD0Z+LL4ND9yKkFjrsypH/hmc4XJGkUcZXa8Il1ibN1VE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892363; c=relaxed/simple; bh=EHTTkGLFXEnfunpZKeHo2ZesAMhSvesC7B4I6Ou9QVA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Pwh/Z91xRw6sA+tKsHmeGZ8S5FxzvEAsJ5p6dlLCLBnNIzPwZGvdiEgbeKPI+4x664pvPQNLmRXxrkP9W/Cx+Jog6B4ecEprL0P8WmPL8n/uzF6BzZEScb50I6dCVIqnpZ/adxjHd/nHUkwUMBNyRdAdnTvyaCZTMF19kFWg+Og= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 7fd7f476f5cd11f0b0f03b4cfa9209d1-20260120 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:371fc91c-e141-471d-a76b-04cd05d99013,IP:0,UR L:0,TC:0,Content:-5,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:-5 X-CID-META: VersionHash:a9d874c,CLOUDID:fa9532f1ab45c6330a745d8bca5b8388,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 7fd7f476f5cd11f0b0f03b4cfa9209d1-20260120 X-User: jiangfeng@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 895029098; Tue, 20 Jan 2026 14:59:01 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Joel Stanley Subject: [PATCH v3 5/8] lib/string_kunit: extend benchmarks to strnlen and chr searches Date: Tue, 20 Jan 2026 14:58:49 +0800 Message-Id: <20260120065852.166857-6-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260120065852.166857-1-jiangfeng@kylinos.cn> References: <20260120065852.166857-1-jiangfeng@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the string benchmarking suite to include strnlen(), strchr(), and strrchr(). For character search functions (strchr and strrchr), the benchmark targets the null terminator. This ensures the entire string is scanned, providing a consistent measure of full-length processing efficiency comparable to strlen(). Suggested-by: Andy Shevchenko Suggested-by: Eric Biggers Tested-by: Joel Stanley Signed-off-by: Feng Jiang Acked-by: Andy Shevchenko --- lib/tests/string_kunit.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index e20e924d1c67..9d76777ad753 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -850,6 +850,21 @@ static void string_bench_strlen(struct kunit *test) STRING_BENCH_BUF(test, buf, len, strlen, buf); } =20 +static void string_bench_strnlen(struct kunit *test) +{ + STRING_BENCH_BUF(test, buf, len, strnlen, buf, len); +} + +static void string_bench_strchr(struct kunit *test) +{ + STRING_BENCH_BUF(test, buf, len, strchr, buf, '\0'); +} + +static void string_bench_strrchr(struct kunit *test) +{ + STRING_BENCH_BUF(test, buf, len, strrchr, buf, '\0'); +} + static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_memset16), KUNIT_CASE(string_test_memset32), @@ -876,6 +891,9 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_memtostr), KUNIT_CASE(string_test_strends), KUNIT_CASE(string_bench_strlen), + KUNIT_CASE(string_bench_strnlen), + KUNIT_CASE(string_bench_strchr), + KUNIT_CASE(string_bench_strrchr), {} }; =20 --=20 2.25.1 From nobody Sun Feb 8 17:03:56 2026 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 964993815D7; Tue, 20 Jan 2026 06:59:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892362; cv=none; b=f9YcCo+Xm7f+sqnWXM8fnrP3ShpY09kISiyNNEFLTFJR2ho6trqdzEbfSAfu3kr+TbEBGTt/Vjv5cKjj95TGSpvU/b51C9qR8xRrBKp9wETV1oF1u7HsRVkmmoZq00WF/I3hwH3GTf26+gGyl1fqlXt6ZqOThZ3XyLNv1kyL658= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892362; c=relaxed/simple; bh=GdH2pY8+pZSHM0wE5KVnv6Gq0OIhbIOgqthm9S4aHhQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KPtTcqikwAYc8MStbvBE1RbWIJask0oAoYxwNEg+9Jv7pCqvWBIFMXRJoOtTnZ9erOyRdZNqRb1qLMkvpMoo6g1NrhLVbQdPbqqdPaq/qe/z24oBWOn82TU88oAG8xCAgJPFyqTumZMjSFUbNRD7FqbDhqXwQvnVPzmPpmuwQU8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 8058d94cf5cd11f0b0f03b4cfa9209d1-20260120 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:445d1891-85fb-4743-a8c0-d2f96e8f4ace,IP:0,UR L:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:-25 X-CID-META: VersionHash:a9d874c,CLOUDID:0377b55296ab42505135e9029bd3ae39,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 8058d94cf5cd11f0b0f03b4cfa9209d1-20260120 X-User: jiangfeng@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 336366237; Tue, 20 Jan 2026 14:59:01 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Joel Stanley Subject: [PATCH v3 6/8] riscv: lib: add strnlen implementation Date: Tue, 20 Jan 2026 14:58:50 +0800 Message-Id: <20260120065852.166857-7-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260120065852.166857-1-jiangfeng@kylinos.cn> References: <20260120065852.166857-1-jiangfeng@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an optimized strnlen() implementation for RISC-V. This version includes a generic word-at-a-time optimization and a Zbb-powered optimization using the 'orc.b' instruction, derived from the strlen implementation. Benchmark results (QEMU TCG, rv64): Length | Original (MB/s) | Optimized (MB/s) | Improvement -------|-----------------|------------------|------------ 16 B | 189 | 310 | +64.0% 512 B | 344 | 1535 | +346.2% 4096 B | 363 | 1854 | +410.7% Suggested-by: Andy Shevchenko Tested-by: Joel Stanley Signed-off-by: Feng Jiang --- arch/riscv/include/asm/string.h | 3 + arch/riscv/lib/Makefile | 1 + arch/riscv/lib/strnlen.S | 164 ++++++++++++++++++++++++++++++++ arch/riscv/purgatory/Makefile | 5 +- 4 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/lib/strnlen.S diff --git a/arch/riscv/include/asm/string.h b/arch/riscv/include/asm/strin= g.h index 5ba77f60bf0b..16634d67c217 100644 --- a/arch/riscv/include/asm/string.h +++ b/arch/riscv/include/asm/string.h @@ -28,6 +28,9 @@ extern asmlinkage __kernel_size_t strlen(const char *); =20 #define __HAVE_ARCH_STRNCMP extern asmlinkage int strncmp(const char *cs, const char *ct, size_t count= ); + +#define __HAVE_ARCH_STRNLEN +extern asmlinkage __kernel_size_t strnlen(const char *, size_t); #endif =20 /* For those files which don't want to check by kasan. */ diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile index bbc031124974..0969d8136df0 100644 --- a/arch/riscv/lib/Makefile +++ b/arch/riscv/lib/Makefile @@ -7,6 +7,7 @@ ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) lib-y +=3D strcmp.o lib-y +=3D strlen.o lib-y +=3D strncmp.o +lib-y +=3D strnlen.o endif lib-y +=3D csum.o ifeq ($(CONFIG_MMU), y) diff --git a/arch/riscv/lib/strnlen.S b/arch/riscv/lib/strnlen.S new file mode 100644 index 000000000000..4af0df9442f1 --- /dev/null +++ b/arch/riscv/lib/strnlen.S @@ -0,0 +1,164 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* + * Base on arch/riscv/lib/strlen.S + * + * Copyright (C) Feng Jiang + */ + +#include +#include +#include +#include + +/* size_t strnlen(const char *s, size_t count) */ +SYM_FUNC_START(strnlen) + + __ALTERNATIVE_CFG("nop", "j strnlen_zbb", 0, RISCV_ISA_EXT_ZBB, + IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && IS_ENABLED(CONFIG_TOOLCHAIN_HAS_ZBB)) + + + /* + * Returns + * a0 - String length + * + * Parameters + * a0 - String to measure + * a1 - Max length of string + * + * Clobbers + * t0, t1, t2 + */ + addi t1, a0, -1 + add t2, a0, a1 +1: + addi t1, t1, 1 + beq t1, t2, 2f + lbu t0, 0(t1) + bnez t0, 1b +2: + sub a0, t1, a0 + ret + + +/* + * Variant of strnlen using the ZBB extension if available + */ +#if defined(CONFIG_RISCV_ISA_ZBB) && defined(CONFIG_TOOLCHAIN_HAS_ZBB) +strnlen_zbb: + +#ifdef CONFIG_CPU_BIG_ENDIAN +# define CZ clz +# define SHIFT sll +#else +# define CZ ctz +# define SHIFT srl +#endif + +.option push +.option arch,+zbb + + /* + * Returns + * a0 - String length + * + * Parameters + * a0 - String to measure + * a1 - Max length of string + * + * Clobbers + * t0, t1, t2, t3, t4 + */ + + /* If maxlen is 0, return 0. */ + beqz a1, 3f + + /* Number of irrelevant bytes in the first word. */ + andi t2, a0, SZREG-1 + + /* Align pointer. */ + andi t0, a0, -SZREG + + li t3, SZREG + sub t3, t3, t2 + slli t2, t2, 3 + + /* Aligned boundary. */ + add t4, a0, a1 + andi t4, t4, -SZREG + + /* Get the first word. */ + REG_L t1, 0(t0) + + /* + * Shift away the partial data we loaded to remove the irrelevant bytes + * preceding the string with the effect of adding NUL bytes at the + * end of the string's first word. + */ + SHIFT t1, t1, t2 + + /* Convert non-NUL into 0xff and NUL into 0x00. */ + orc.b t1, t1 + + /* Convert non-NUL into 0x00 and NUL into 0xff. */ + not t1, t1 + + /* + * Search for the first set bit (corresponding to a NUL byte in the + * original chunk). + */ + CZ t1, t1 + + /* + * The first chunk is special: compare against the number + * of valid bytes in this chunk. + */ + srli a0, t1, 3 + + /* Limit the result by maxlen. */ + bleu a1, a0, 3f + + bgtu t3, a0, 2f + + /* Prepare for the word comparison loop. */ + addi t2, t0, SZREG + li t3, -1 + + /* + * Our critical loop is 4 instructions and processes data in + * 4 byte or 8 byte chunks. + */ + .p2align 3 +1: + REG_L t1, SZREG(t0) + addi t0, t0, SZREG + orc.b t1, t1 + bgeu t0, t4, 4f + beq t1, t3, 1b +4: + not t1, t1 + CZ t1, t1 + srli t1, t1, 3 + + /* Get number of processed bytes. */ + sub t2, t0, t2 + + /* Add number of characters in the first word. */ + add a0, a0, t2 + + /* Add number of characters in the last word. */ + add a0, a0, t1 + + /* Ensure the final result does not exceed maxlen. */ + bgeu a0, a1, 3f +2: + ret +3: + mv a0, a1 + ret + +.option pop +#endif +SYM_FUNC_END(strnlen) +SYM_FUNC_ALIAS(__pi_strnlen, strnlen) +EXPORT_SYMBOL(strnlen) diff --git a/arch/riscv/purgatory/Makefile b/arch/riscv/purgatory/Makefile index 530e497ca2f9..d7c0533108be 100644 --- a/arch/riscv/purgatory/Makefile +++ b/arch/riscv/purgatory/Makefile @@ -2,7 +2,7 @@ =20 purgatory-y :=3D purgatory.o sha256.o entry.o string.o ctype.o memcpy.o me= mset.o ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) -purgatory-y +=3D strcmp.o strlen.o strncmp.o +purgatory-y +=3D strcmp.o strlen.o strncmp.o strnlen.o endif =20 targets +=3D $(purgatory-y) @@ -32,6 +32,9 @@ $(obj)/strncmp.o: $(srctree)/arch/riscv/lib/strncmp.S FOR= CE $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE $(call if_changed_rule,cc_o_c) =20 +$(obj)/strnlen.o: $(srctree)/arch/riscv/lib/strnlen.S FORCE + $(call if_changed_rule,as_o_S) + CFLAGS_sha256.o :=3D -D__DISABLE_EXPORTS -D__NO_FORTIFY CFLAGS_string.o :=3D -D__DISABLE_EXPORTS CFLAGS_ctype.o :=3D -D__DISABLE_EXPORTS --=20 2.25.1 From nobody Sun Feb 8 17:03:56 2026 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3830836CE09; Tue, 20 Jan 2026 06:59:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892358; cv=none; b=GGSvsdqDAVsRnC0YEBklKAFmuicqokhZKklSCDlS4OKNU+6S8vqVqN3n7R7vU/rA+JM3zyVrfEudboaz9M7ytYmNgb3YexqnmJhUHuUmlvZq1yt9SABQvt9K1kCqIV/VEFKCBcZPAxMhzO+BigmeWhejmHWskPWchFSYKmsOwE0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892358; c=relaxed/simple; bh=ooASpuE5SVcrAQwxOqML888hGoxD0LMMu13uOE+J0Pg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BwdMi6DzyyHofPfFkKRCCpzvXh6nCO9uL56ifoL8p4yEZee0NvdQngMqiqr3Ipz+uNqQp4kkq/rSJaHLYBS0Y6PcGrt9JZmFUPEJ+bZS3ZddGojm+TinkZvFBTnljgJa+CNxmbZijELb2xLstPsiy2izA7wOzF0E4RvJO7bP4QY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 80b9629ef5cd11f0b0f03b4cfa9209d1-20260120 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:7e07f889-3341-4ec2-9ba3-4f5e53858523,IP:0,UR L:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:-25 X-CID-META: VersionHash:a9d874c,CLOUDID:9c7339732eeeec021b2862115766649c,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 80b9629ef5cd11f0b0f03b4cfa9209d1-20260120 X-User: jiangfeng@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 1645312883; Tue, 20 Jan 2026 14:59:02 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Joel Stanley Subject: [PATCH v3 7/8] riscv: lib: add strchr implementation Date: Tue, 20 Jan 2026 14:58:51 +0800 Message-Id: <20260120065852.166857-8-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260120065852.166857-1-jiangfeng@kylinos.cn> References: <20260120065852.166857-1-jiangfeng@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an assembly implementation of strchr() for RISC-V. By eliminating stack frame management (prologue/epilogue) and optimizing the function entries, the assembly version provides significant relative gains for short strings where the fixed overhead of the C function is most prominent. As the string length increases, the performance converges with the byte-oriented scan logic. Benchmark results (QEMU TCG, rv64): Length | Original (MB/s) | Optimized (MB/s) | Improvement -------|-----------------|------------------|------------ 1 B | 21 | 23 | +9.5% 7 B | 118 | 126 | +6.7% 16 B | 200 | 208 | +4.0% 512 B | 375 | 399 | +6.4% 4096 B | 395 | 401 | +1.5% Suggested-by: Andy Shevchenko Tested-by: Joel Stanley Signed-off-by: Feng Jiang --- arch/riscv/include/asm/string.h | 3 +++ arch/riscv/lib/Makefile | 1 + arch/riscv/lib/strchr.S | 35 +++++++++++++++++++++++++++++++++ arch/riscv/purgatory/Makefile | 5 ++++- 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/lib/strchr.S diff --git a/arch/riscv/include/asm/string.h b/arch/riscv/include/asm/strin= g.h index 16634d67c217..ca3ade82b124 100644 --- a/arch/riscv/include/asm/string.h +++ b/arch/riscv/include/asm/string.h @@ -31,6 +31,9 @@ extern asmlinkage int strncmp(const char *cs, const char = *ct, size_t count); =20 #define __HAVE_ARCH_STRNLEN extern asmlinkage __kernel_size_t strnlen(const char *, size_t); + +#define __HAVE_ARCH_STRCHR +extern asmlinkage char *strchr(const char *, int); #endif =20 /* For those files which don't want to check by kasan. */ diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile index 0969d8136df0..b7f804dce1c3 100644 --- a/arch/riscv/lib/Makefile +++ b/arch/riscv/lib/Makefile @@ -8,6 +8,7 @@ lib-y +=3D strcmp.o lib-y +=3D strlen.o lib-y +=3D strncmp.o lib-y +=3D strnlen.o +lib-y +=3D strchr.o endif lib-y +=3D csum.o ifeq ($(CONFIG_MMU), y) diff --git a/arch/riscv/lib/strchr.S b/arch/riscv/lib/strchr.S new file mode 100644 index 000000000000..48c3a9da53e3 --- /dev/null +++ b/arch/riscv/lib/strchr.S @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* + * Copyright (C) 2025 Feng Jiang + */ + +#include +#include + +/* char *strchr(const char *s, int c) */ +SYM_FUNC_START(strchr) + /* + * Parameters + * a0 - The string to be searched + * a1 - The character to search for + * + * Returns + * a0 - Address of first occurrence of 'c' or 0 + * + * Clobbers + * t0 + */ + andi a1, a1, 0xff +1: + lbu t0, 0(a0) + beq t0, a1, 2f + addi a0, a0, 1 + bnez t0, 1b + li a0, 0 +2: + ret +SYM_FUNC_END(strchr) + +SYM_FUNC_ALIAS_WEAK(__pi_strchr, strchr) +EXPORT_SYMBOL(strchr) diff --git a/arch/riscv/purgatory/Makefile b/arch/riscv/purgatory/Makefile index d7c0533108be..e7b3d748c913 100644 --- a/arch/riscv/purgatory/Makefile +++ b/arch/riscv/purgatory/Makefile @@ -2,7 +2,7 @@ =20 purgatory-y :=3D purgatory.o sha256.o entry.o string.o ctype.o memcpy.o me= mset.o ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) -purgatory-y +=3D strcmp.o strlen.o strncmp.o strnlen.o +purgatory-y +=3D strcmp.o strlen.o strncmp.o strnlen.o strchr.o endif =20 targets +=3D $(purgatory-y) @@ -35,6 +35,9 @@ $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE $(obj)/strnlen.o: $(srctree)/arch/riscv/lib/strnlen.S FORCE $(call if_changed_rule,as_o_S) =20 +$(obj)/strchr.o: $(srctree)/arch/riscv/lib/strchr.S FORCE + $(call if_changed_rule,as_o_S) + CFLAGS_sha256.o :=3D -D__DISABLE_EXPORTS -D__NO_FORTIFY CFLAGS_string.o :=3D -D__DISABLE_EXPORTS CFLAGS_ctype.o :=3D -D__DISABLE_EXPORTS --=20 2.25.1 From nobody Sun Feb 8 17:03:56 2026 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A537B37E307; Tue, 20 Jan 2026 06:59:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892361; cv=none; b=d7KysXN19ze8kuSRFhW2ZIqXy3I603TTWbftsR6GH2cwm8A1/T4T86LQaelLu1Di16k3wdC0r/8mwP3gd88/BOW62zQzxQCllBADkeOzol761cj4mOozT8vrdkCM6kBhx53ni8ohfY9gV8cUJh86FUQwiiG49BSBr1bGfr90H3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768892361; c=relaxed/simple; bh=Pey4FY0JLLd2C3FK2v5bBZX62FqrhpB92C449D6t3VY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EBfHMFP2T/mls0rOt+IBzeeH5w64L1s2we7OBHmAb2MuITYdZ3OBdU8bkKuOxTaaEgCrzOtZPLXwfw2buf2NUEoyjpK+gawFplQJcj3RpbTxgcAa58A0bMf2oAl12Zxp182K9mgUkzysLofGENNCpXMvdinuTLB/zMYsfSMrtKo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 811afaaef5cd11f0b0f03b4cfa9209d1-20260120 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:56069a1a-b44a-4c38-a798-2abbc3fcc918,IP:0,UR L:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:-25 X-CID-META: VersionHash:a9d874c,CLOUDID:3837b84ef2212740f4955e3344fc799f,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 811afaaef5cd11f0b0f03b4cfa9209d1-20260120 X-User: jiangfeng@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 1050199038; Tue, 20 Jan 2026 14:59:03 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Joel Stanley Subject: [PATCH v3 8/8] riscv: lib: add strrchr implementation Date: Tue, 20 Jan 2026 14:58:52 +0800 Message-Id: <20260120065852.166857-9-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260120065852.166857-1-jiangfeng@kylinos.cn> References: <20260120065852.166857-1-jiangfeng@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an assembly implementation of strrchr() for RISC-V. This implementation minimizes instruction count and avoids unnecessary memory access to the stack. The performance benefits are most visible on small workloads (1-16 bytes) where the architectural savings in function overhead outweigh the execution time of the scan loop. Benchmark results (QEMU TCG, rv64): Length | Original (MB/s) | Optimized (MB/s) | Improvement -------|-----------------|------------------|------------ 1 B | 21 | 22 | +4.7% 7 B | 116 | 122 | +5.1% 16 B | 195 | 208 | +6.6% 512 B | 388 | 399 | +2.8% 4096 B | 411 | 411 | +0.0% Suggested-by: Andy Shevchenko Tested-by: Joel Stanley Signed-off-by: Feng Jiang --- arch/riscv/include/asm/string.h | 3 +++ arch/riscv/lib/Makefile | 1 + arch/riscv/lib/strrchr.S | 37 +++++++++++++++++++++++++++++++++ arch/riscv/purgatory/Makefile | 5 ++++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/lib/strrchr.S diff --git a/arch/riscv/include/asm/string.h b/arch/riscv/include/asm/strin= g.h index ca3ade82b124..764ffe8f6479 100644 --- a/arch/riscv/include/asm/string.h +++ b/arch/riscv/include/asm/string.h @@ -34,6 +34,9 @@ extern asmlinkage __kernel_size_t strnlen(const char *, s= ize_t); =20 #define __HAVE_ARCH_STRCHR extern asmlinkage char *strchr(const char *, int); + +#define __HAVE_ARCH_STRRCHR +extern asmlinkage char *strrchr(const char *, int); #endif =20 /* For those files which don't want to check by kasan. */ diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile index b7f804dce1c3..735d0b665536 100644 --- a/arch/riscv/lib/Makefile +++ b/arch/riscv/lib/Makefile @@ -9,6 +9,7 @@ lib-y +=3D strlen.o lib-y +=3D strncmp.o lib-y +=3D strnlen.o lib-y +=3D strchr.o +lib-y +=3D strrchr.o endif lib-y +=3D csum.o ifeq ($(CONFIG_MMU), y) diff --git a/arch/riscv/lib/strrchr.S b/arch/riscv/lib/strrchr.S new file mode 100644 index 000000000000..ac58b20ca21d --- /dev/null +++ b/arch/riscv/lib/strrchr.S @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* + * Copyright (C) 2025 Feng Jiang + */ + +#include +#include + +/* char *strrchr(const char *s, int c) */ +SYM_FUNC_START(strrchr) + /* + * Parameters + * a0 - The string to be searched + * a1 - The character to seaerch for + * + * Returns + * a0 - Address of last occurrence of 'c' or 0 + * + * Clobbers + * t0, t1 + */ + andi a1, a1, 0xff + mv t1, a0 + li a0, 0 +1: + lbu t0, 0(t1) + bne t0, a1, 2f + mv a0, t1 +2: + addi t1, t1, 1 + bnez t0, 1b + ret +SYM_FUNC_END(strrchr) + +SYM_FUNC_ALIAS_WEAK(__pi_strrchr, strrchr) +EXPORT_SYMBOL(strrchr) diff --git a/arch/riscv/purgatory/Makefile b/arch/riscv/purgatory/Makefile index e7b3d748c913..b0358a78f11a 100644 --- a/arch/riscv/purgatory/Makefile +++ b/arch/riscv/purgatory/Makefile @@ -2,7 +2,7 @@ =20 purgatory-y :=3D purgatory.o sha256.o entry.o string.o ctype.o memcpy.o me= mset.o ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) -purgatory-y +=3D strcmp.o strlen.o strncmp.o strnlen.o strchr.o +purgatory-y +=3D strcmp.o strlen.o strncmp.o strnlen.o strchr.o strrchr.o endif =20 targets +=3D $(purgatory-y) @@ -38,6 +38,9 @@ $(obj)/strnlen.o: $(srctree)/arch/riscv/lib/strnlen.S FOR= CE $(obj)/strchr.o: $(srctree)/arch/riscv/lib/strchr.S FORCE $(call if_changed_rule,as_o_S) =20 +$(obj)/strrchr.o: $(srctree)/arch/riscv/lib/strrchr.S FORCE + $(call if_changed_rule,as_o_S) + CFLAGS_sha256.o :=3D -D__DISABLE_EXPORTS -D__NO_FORTIFY CFLAGS_string.o :=3D -D__DISABLE_EXPORTS CFLAGS_ctype.o :=3D -D__DISABLE_EXPORTS --=20 2.25.1