From nobody Sat Feb 7 07:26:01 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 3988B35EDBE; Fri, 23 Jan 2026 08:58:58 +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=1769158744; cv=none; b=RoPYj5MTIR9YcLJDKNwB/MNI2se8C2rwMvoEgMeVAjOwQYvYinjzXrjL15Vgn1+gNr9K0rVHPAXxIM1RsjMF7GR4d4M1bN2FS3B2b0RTEsu4OzHck6wWq/WSU0hxXzis08QRoYbK/i/iIUYSM26cfVBNtV9czaxVrY8EgMnhZYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158744; c=relaxed/simple; bh=UAYuDoQb/2EtPLdVdp2ivjNPfKg9V9mMj2QQmFZrq5o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=O1jun6cU46HRouHlPNRzYgJ9OkR6iZS/+RLYWr5mLY/G+jPYca4P+vuI1PdYGunnyWDDxd7hU5bst7CWGU8zZ9p7vkl3J+joI/Ovua63M49OYXkuCuT82Lx0RzEsKFzQMI14oT/IY966p+IDwCaj5Vyhr8uNH0gdFjG6m260ArI= 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: bb7f5f28f83911f0b0f03b4cfa9209d1-20260123 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:145c7081-f834-4771-90ee-0565a4e86cc0,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:5424b9d9a093af57cb0c42207d8a9775,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: bb7f5f28f83911f0b0f03b4cfa9209d1-20260123 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 109544292; Fri, 23 Jan 2026 16:58:49 +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, mingo@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, samuel.holland@sifive.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 v4 1/8] lib/string_kunit: add correctness test for strlen() Date: Fri, 23 Jan 2026 16:58:34 +0800 Message-Id: <20260123085841.212468-2-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260123085841.212468-1-jiangfeng@kylinos.cn> References: <20260123085841.212468-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. Signed-off-by: Feng Jiang Acked-by: Andy Shevchenko Tested-by: Joel Stanley --- 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..bc5130c6e5e9 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) +{ + const size_t buf_size =3D STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + = 1; + size_t len, offset; + char *s; + + 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 Sat Feb 7 07:26:01 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 63F50355034; Fri, 23 Jan 2026 08:59:03 +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=1769158749; cv=none; b=cGEajAInHKMi2C82Op6fGpn4gZKw8TutImbO02XMlapwkpbHDdDGZS4w/5BaBugvMhnSboqDtCplEnxihPwEpY5RljpS+UrmAJDI2WX5B6eIP9q9UOyj+OBuFI9CX73scX2oAdR7bLGXFilAxkepbpQ2aOFvU2HxicvALLqxbU0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158749; c=relaxed/simple; bh=G3JduIQ0bYDMiO7BzZxEuhqqJowH3AYuMESNCYPa5mg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fvWEW2ehdWGlfwDwZLHmkpaspBAkbsjwew8V9SkRfjKBISiHeWxC4i6Y5r7yztS3XJBfwLBxB0Hq1h4Ru0XJ7fPry/3L9l2Zwq+Co/8K28ep2U0sk1TT3nVwfaokb2zF9a45uS5LzpxH9AGvFbCe8RAE0RCZs9I0LMdkQ4GwZyk= 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: bbe859c4f83911f0b0f03b4cfa9209d1-20260123 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:7b511442-f8cd-4e88-81b6-1d4bd35b595b,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:e30dc2a21e628c0af74cb2e043ddf3ea,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: bbe859c4f83911f0b0f03b4cfa9209d1-20260123 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 1554780634; Fri, 23 Jan 2026 16:58:49 +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, mingo@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, samuel.holland@sifive.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 v4 2/8] lib/string_kunit: add correctness test for strnlen() Date: Fri, 23 Jan 2026 16:58:35 +0800 Message-Id: <20260123085841.212468-3-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260123085841.212468-1-jiangfeng@kylinos.cn> References: <20260123085841.212468-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 Signed-off-by: Feng Jiang Acked-by: Andy Shevchenko Tested-by: Joel Stanley --- 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 bc5130c6e5e9..991f01d52eb2 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) +{ + const size_t buf_size =3D STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + = 1; + size_t len, offset; + char *s; + + 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 Sat Feb 7 07:26:01 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 62DA133ADAC; Fri, 23 Jan 2026 08:59:02 +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=1769158748; cv=none; b=AN7G9xQbTbOVOm62pqd2ToMj96OXajvyj6gwwuZ9+sHilaTv6DqP/LaArbzbawKmlBTHuv9pCXleMgsVPglsIkQ83duKi3UdFLyr2H3RUJE1lzSo82LgdS+aHq1eRI9ReUbqOXichlkELO6rE1/gJlbKK4vCLzzt3NiU/ALz1I8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158748; c=relaxed/simple; bh=gdp0TkFXB6tIKP75KCs0gmk3wZBq+K9AMFnNVtN0+zc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LC9QM+ObfCVDuLCK8GKAaevb/xDM0l/7fW1r9BVYYdtpuWVgPiqJvm2QbWEyaUMtW/HGdhlehAWj3ka/YKn97r9j90ELQrogk7eCe5YMsmby6507ciaQDvMCcBLyQ7LYMNtXym1bwDhBqQcxhpjgodPsC+Lr1pRimNBOK21X61A= 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: bc54094ef83911f0b0f03b4cfa9209d1-20260123 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:202381a5-b0e6-41e7-85eb-8be49477ad59,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:7adacdfc704780914d4afd00aea2fc10,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: bc54094ef83911f0b0f03b4cfa9209d1-20260123 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 91718921; Fri, 23 Jan 2026 16:58:50 +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, mingo@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, samuel.holland@sifive.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 v4 3/8] lib/string_kunit: add correctness test for strrchr() Date: Fri, 23 Jan 2026 16:58:36 +0800 Message-Id: <20260123085841.212468-4-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260123085841.212468-1-jiangfeng@kylinos.cn> References: <20260123085841.212468-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. Signed-off-by: Feng Jiang Acked-by: Andy Shevchenko Tested-by: Joel Stanley --- 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 991f01d52eb2..7f1e2bf6a352 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) +{ + const size_t buf_size =3D STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + = 1; + size_t offset, len; + char *buf; + + 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 Sat Feb 7 07:26:01 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 A7F1E360728; Fri, 23 Jan 2026 08:58:59 +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=1769158743; cv=none; b=fKerauGVW8CizykDOtyY0FGcUNwaLQ7CtPVxA7GPCy2maFDGU1AseNpw/me/qRTkbwSHRDLfFTTVkXVatTD3M/KXbeJBG6+2NB52Z11WIubyONLFKvtlg0MhVIAeaqFuEpIHDYt+gRnz4XTGzEdaOF0zVuoc3BEf04CJrjzVxkM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158743; c=relaxed/simple; bh=6ZrnKTzpiWjmT+adBRlB3wl1MnT39vtpTwZv22jFhzg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KtR3tfv/MgUGVSgLEqVh1CNTQr+sLQ5E/R2kgcs/RsUae4G3Xo+ct5GzOfVVxc3UYp5xF0ASpP1rGmhaolHVb7aZw85QW9oSSBEwPiliuQ92slp6uTsEp+op8inulC00Tt0I8CJAC/jTdJEKvxH7zb4f5BbBXdwMsl0j7lNNiEQ= 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: bcb91302f83911f0b0f03b4cfa9209d1-20260123 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:b899f6a1-1e9b-4402-826e-8ac7b618a117,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:cafa3f54b4ba1725d8a95b5766bd95e9,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: bcb91302f83911f0b0f03b4cfa9209d1-20260123 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 1148596265; Fri, 23 Jan 2026 16:58:51 +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, mingo@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, samuel.holland@sifive.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v4 4/8] lib/string_kunit: add performance benchmark for strlen() Date: Fri, 23 Jan 2026 16:58:37 +0800 Message-Id: <20260123085841.212468-5-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260123085841.212468-1-jiangfeng@kylinos.cn> References: <20260123085841.212468-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 framework allows for comparing different implementations (e.g., generic C vs. architecture-optimized assembly) within the KUnit environment. Initially, provide a benchmark for strlen(). Suggested-by: Andy Shevchenko Suggested-by: Eric Biggers Signed-off-by: Feng Jiang --- lib/Kconfig.debug | 11 +++ lib/tests/string_kunit.c | 158 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 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 7f1e2bf6a352..52798f426aa9 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -6,10 +6,13 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 #include +#include #include +#include #include #include #include +#include =20 #define STRCMP_LARGE_BUF_LEN 2048 #define STRCMP_CHANGE_POINT 1337 @@ -20,6 +23,9 @@ #define STRING_TEST_MAX_LEN 128 #define STRING_TEST_MAX_OFFSET 16 =20 +#define STRING_BENCH_SEED 888 +#define STRING_BENCH_WORKLOAD (1 * MEGA) + static void string_test_memset16(struct kunit *test) { unsigned i, j, k; @@ -700,6 +706,157 @@ 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 NUL character). + * + * 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) +{ + size_t i, max_len =3D 0; + void *buf; + + for (i =3D 0; i < count; i++) { + if (max_len < lens[i]) + max_len =3D lens[i]; + } + + /* Add space for NUL character */ + max_len +=3D 1; + + buf =3D kunit_kzalloc(test, max_len, GFP_KERNEL); + if (!buf) + return NULL; + + if (buf_len) + *buf_len =3D max_len; + + return buf; +} + +/** + * fill_random_string() - Populate a buffer with a random NUL-terminated s= tring. + * @buf: Buffer to fill. + * @len: Length of the buffer in bytes. + * + * Fills the buffer with random non-NUL bytes and ensures the string is + * properly NUL-terminated. + */ +static void fill_random_string(char *buf, size_t len) +{ + struct rnd_state state; + size_t i; + + if (!buf || !len) + return; + + /* Use a fixed seed to ensure deterministic benchmark results */ + prandom_seed_state(&state, STRING_BENCH_SEED); + prandom_bytes_state(&state, buf, len); + + /* Replace NUL characters 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, ...) \ +({ \ + /* Volatile function pointer prevents dead code elimination */ \ + typeof(func) (* volatile __func) =3D (func); \ + size_t __bn_iters =3D (iters); \ + size_t __bn_warm_iters; \ + size_t __bn_i; \ + u64 __bn_t; \ + \ + __bn_warm_iters =3D max(__bn_iters / 10, 50U); \ + \ + 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, NUL-terminated buffer and iterates through lengt= hs + * in bench_lens, defining @buf_name and @buf_size in each loop. + */ +#define STRING_BENCH_BUF(test, buf_name, buf_size, func, ...) \ +do { \ + size_t buf_size, _bn_i, _bn_iters, _bn_size =3D 0; \ + u64 _bn_t, _bn_mbps =3D 0, _bn_lat =3D 0; \ + char *buf_name, *_bn_buf; \ + \ + 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); \ + \ + 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(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 +882,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 Sat Feb 7 07:26:01 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 C3D6133D6E1; Fri, 23 Jan 2026 08:59:01 +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=1769158744; cv=none; b=J0SAIZxyndSPuDmgyIjiYVWecixibDtXcUuE7hRlIRZ9nTxii28QscAHX/XeZGHUM16fayRpIFmkS88yalxLvnQCj8EHQOMJ1Xh4A5UTt1UpNAH0Xo6P0tknkMomotRGh2cewfZxTsRgTL3guefiGm1q+AdulM+sOuNQNpuEcbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158744; c=relaxed/simple; bh=+giwXz5go4rjM1zaJC2IzpQCxDuiwrGxPZNkTobTMI4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ek1gnQJpdIOZpVTlkwLLckZSkXeBMvV17ADoaDnY07yvh+VomwXImAovc/gNraqMHwladWprru49d8gONlMpdDTb8DdJf44Ra2kiFfqa41100cz+qp5a9xu6HIjXmqXea9FkoodjpFnfT8RsMAvH10PCAfkMF7uZIhaNt+7Cgao= 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: bd1f2a5cf83911f0b0f03b4cfa9209d1-20260123 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:b00e071a-dd14-4847-9045-bba7e2ba74fa,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:c826670df8c0c99825d5f32d8fca22d5,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: bd1f2a5cf83911f0b0f03b4cfa9209d1-20260123 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 532124729; Fri, 23 Jan 2026 16:58:51 +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, mingo@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, samuel.holland@sifive.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v4 5/8] lib/string_kunit: extend benchmarks to strnlen() and chr searches Date: Fri, 23 Jan 2026 16:58:38 +0800 Message-Id: <20260123085841.212468-6-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260123085841.212468-1-jiangfeng@kylinos.cn> References: <20260123085841.212468-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 NUL character. 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 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 52798f426aa9..3d540ad78cda 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -857,6 +857,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), @@ -883,6 +898,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 Sat Feb 7 07:26:01 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 399C435F8B2; Fri, 23 Jan 2026 08:58:58 +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=1769158745; cv=none; b=FSlpTz8kuzw7++zx7X0JPb62zPCVj6lU+gmRnGl8C0BzkOIVj8LcFc8vxaSn6U9diCFn6nxHlxuz02Sw+hdMlSX67rF9uhg+9yfbvsfT6Nw2XJ4CWEoWWu/nTGzxYh9/dTAJgla910a5Dku89sr9vIhsPHALOH/DfcK/jF6BPvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158745; c=relaxed/simple; bh=1ixGsQ7sfK1X4gS4aEkMwinXQxzfZ1EEl2WeoNQDpX0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iK0eY/1pWmzHBUWOcW5EDFmexkDc2pAb5HrawMELJvWaZ12ke6V13v81Abg9hipgWXFRzyBcbM/9ImiayTzp8ad2roW1+z7BP7fHboz2uAxFQ1AcQQgXp8GI6bWd4nH+8YYRLoiD6LMkg9v+W+OdeJ6T9/obW937GmVcCF8K4U8= 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: bd88e0d2f83911f0b0f03b4cfa9209d1-20260123 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:018d4f37-376f-4880-8b00-c2c6c6c5e036,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:6357475fada63b320c593d77e51b9844,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: bd88e0d2f83911f0b0f03b4cfa9209d1-20260123 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 1697569482; Fri, 23 Jan 2026 16:58:52 +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, mingo@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, samuel.holland@sifive.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Qingfang Deng Subject: [PATCH v4 6/8] riscv: lib: add strnlen() implementation Date: Fri, 23 Jan 2026 16:58:39 +0800 Message-Id: <20260123085841.212468-7-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260123085841.212468-1-jiangfeng@kylinos.cn> References: <20260123085841.212468-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 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 | 179 | 309 | +72.6% 512 B | 347 | 1562 | +350.1% 4096 B | 356 | 1878 | +427.5% Suggested-by: Qingfang Deng 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..53afa7b5b314 --- /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. */ + minu a0, a0, a1 + + 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. */ + minu a0, a0, a1 +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 Sat Feb 7 07:26:01 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 C3DED340D90; Fri, 23 Jan 2026 08:59:01 +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=1769158746; cv=none; b=nxozH//kIyI7yYFuEoWkXFkvGdBH0A1sUEbqNdd8XFOrVbI/I5ycO2H+CMooKbh7WnpADfl1xPugLib4LSGf6UEtucyZdvEHr3R82PgTjyadylIELa9DnmkaMmmyTMoIFi0MX9vDS7mViAayQxLNlPFWbkTx5pJmFI7eLoGNeCw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158746; c=relaxed/simple; bh=Y6GJ7o6hnScHiuF5KQktliDsiQiZ/nPkIG3vT4UIBSU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=d8fIDD/5r114xewv5OlUSHAySNBabLoYHkeXzLq31lB5UYAC45Ql2ENVl28SsI9uvqJh520SI0jmzZSRE4NIfZ9lrxgX7NogMxmRna5Buizus5uy4C2bwvOTdhXexAEmD7FAPd81jfd5JiuN3Ez82A6OTajxeY17gZZgSVgAHc8= 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: bded2a6af83911f0b0f03b4cfa9209d1-20260123 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:2fcb8d32-1a7f-4eba-bb86-aa2497527ecf,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:8f528513219fe4ac10d220b5fbe1c1a3,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: bded2a6af83911f0b0f03b4cfa9209d1-20260123 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 1907831174; Fri, 23 Jan 2026 16:58:53 +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, mingo@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, samuel.holland@sifive.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v4 7/8] riscv: lib: add strchr() implementation Date: Fri, 23 Jan 2026 16:58:40 +0800 Message-Id: <20260123085841.212468-8-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260123085841.212468-1-jiangfeng@kylinos.cn> References: <20260123085841.212468-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 string length increases, performance converges with the generic C implementation. Benchmark results (QEMU TCG, rv64): Length | Original (MB/s) | Optimized (MB/s) | Improvement -------|-----------------|------------------|------------ 1 B | 21 | 22 | +4.8% 7 B | 113 | 121 | +7.1% 16 B | 195 | 202 | +3.6% 512 B | 376 | 389 | +3.5% 4096 B | 394 | 393 | -0.3% 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 Sat Feb 7 07:26:01 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 E1293348452; Fri, 23 Jan 2026 08:59:00 +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=1769158745; cv=none; b=e7mU5AZC14SWpu/OzmY6/+X9YrlihPye9oNLuIfmrgxQr32Qr4CnzTkNwCqKB3VSbS0L5SDZ3O1ojstSvINDaQkBJ7V1HaCB0/STA39V3DiqQCkNVgCz6BCBGSNg4sxFB+W6k/qIryACvvAkOAK8LmUYCv76hQTeC9l0nrfg3/s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158745; c=relaxed/simple; bh=AMLyJP13lq5q9yfPKhQ9OMAfBqXzPWDIhUThsyomljo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=U7VI8FymWgq/LsNsggmyRHXzC43pLB5XtIan1xaQSMTLmkeppDIJgO1NieqCYD358CY+1gU+egXtF6izazrBQ6aWzEophpJh8FG80uTg4JBSR4iXYlaR3gI240o+4DHY9W/tzvF9/ANZRCasa2rul+dkhUeMtK67xLHEsCa/0QY= 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: be51dffaf83911f0b0f03b4cfa9209d1-20260123 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:e7b31b3c-b674-4a1c-b5d2-71cc865d3b67,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:317e3d8f933718520d277b6c7368d8c6,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: be51dffaf83911f0b0f03b4cfa9209d1-20260123 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 647105484; Fri, 23 Jan 2026 16:58:53 +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, mingo@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, samuel.holland@sifive.com, linus.walleij@linaro.org, nathan@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v4 8/8] riscv: lib: add strrchr() implementation Date: Fri, 23 Jan 2026 16:58:41 +0800 Message-Id: <20260123085841.212468-9-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260123085841.212468-1-jiangfeng@kylinos.cn> References: <20260123085841.212468-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 | 20 | 21 | +5.0% 7 B | 111 | 120 | +8.1% 16 B | 189 | 199 | +5.3% 512 B | 361 | 382 | +5.8% 4096 B | 388 | 391 | +0.8% 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