From nobody Sun Feb 8 20:34:47 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 93C6037FF54; Tue, 13 Jan 2026 08:28: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=1768292885; cv=none; b=MutA8ll3HFpeGGx8BjZLh6CTIWcKPltKtzimz5VylCKp9QalqYoJI8/o7qtSmGZoQ9Xg4R2aIupbP2q4wqBeD9bp0ggsNdcS2V8X1qIcrNMfveFu8NvxsYLwXDe5UD1wIFJNlFRIU5I54wsL4lazkRhMJhGUoNaBW8/dEpWQPow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292885; c=relaxed/simple; bh=1oxpHVilVK2SqG0Y4rp6MdIJ6tAAy+IiuCUJDNS6QrE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sMkuxvTDm/R1xqQFttuO1vqmJSdwDtBpP0sgOlQYo6HFT3+P5rLqiF8NAz+3JE2IY/WBK/7HVqdJaQzTTt9Vsr1m5UBb+nKIGeo5BKP5K/+u8moUk/VMzQ/W9cQJGftF/McUESYAY8N/YR0TFeEeQXAkVdMIvoxsk7Z/ZOEoe2Y= 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: c4320040f05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:e346155f-0f91-4c2e-a8a1-5393a154b924,IP:0,UR L:0,TC:0,Content:-5,EDM:25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:20 X-CID-META: VersionHash:a9d874c,CLOUDID:1ac8a1be40bd3ada1e363909550e78e0,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:5,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OS A: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: c4320040f05911f0a38c85956e01ac42-20260113 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 659851752; Tue, 13 Jan 2026 16:27:58 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 01/14] lib/string: extract generic strlen() into __generic_strlen() Date: Tue, 13 Jan 2026 16:27:35 +0800 Message-Id: <20260113082748.250916-2-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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" To support performance benchmarking in KUnit tests, extract the generic C implementation of strlen() into a standalone function __generic_strlen(). This allows tests to compare architecture-optimized versions against the generic baseline without duplicating code. Suggested-by: Andy Shevchenko Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- include/linux/string.h | 1 + lib/string.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 1b564c36d721..961645633b4d 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -197,6 +197,7 @@ extern char * strstr(const char *, const char *); #ifndef __HAVE_ARCH_STRNSTR extern char * strnstr(const char *, const char *, size_t); #endif +extern __kernel_size_t __generic_strlen(const char *); #ifndef __HAVE_ARCH_STRLEN extern __kernel_size_t strlen(const char *); #endif diff --git a/lib/string.c b/lib/string.c index b632c71df1a5..047ecb38e09b 100644 --- a/lib/string.c +++ b/lib/string.c @@ -412,8 +412,7 @@ char *strnchr(const char *s, size_t count, int c) EXPORT_SYMBOL(strnchr); #endif =20 -#ifndef __HAVE_ARCH_STRLEN -size_t strlen(const char *s) +size_t __generic_strlen(const char *s) { const char *sc; =20 @@ -421,6 +420,13 @@ size_t strlen(const char *s) /* nothing */; return sc - s; } +EXPORT_SYMBOL(__generic_strlen); + +#ifndef __HAVE_ARCH_STRLEN +size_t strlen(const char *s) +{ + return __generic_strlen(s); +} EXPORT_SYMBOL(strlen); #endif =20 --=20 2.25.1 From nobody Sun Feb 8 20:34:47 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 C5F0B3803C3; Tue, 13 Jan 2026 08:28:05 +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=1768292890; cv=none; b=LeY3yK4QY2lAO2PhfnaRXfB/WEpyzODX7m2JL2IUKjvTbPtLJ41nvmlt5V3iEWj0K9ai44vbEIA6F5kfo76WflTX3K8pqFdMPB+AselG26dmjf/0gHHV8VpH0+zQ/cHPhK0YhuqHqkT+M6TuJYPcd+eRhGHUT12NPmBPrCPOcBQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292890; c=relaxed/simple; bh=4rwKNCUClCAqHxThqil2XPf3BjWSOecFe0OTRGIbON4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eUfzBXKDXRJTEu+B2i2n9DPcK1fnFyCQ2EqEXI7UprUDRvv+ecFNC8YhKyyruCOQiEgMlAlz02ley160TVLNq6T+On6vThrTzsGbH6TGp27NT11aJjZe/qnY5z6nSz2XWJxDrCWMf4bAokohUQYMcXa1CAqqmHg7eF2ZCRGRHKM= 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: c4e7f422f05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:cbb74efb-63c1-4bb7-a8f0-49e301156545,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:c8f361b3fbd1107a830e3721ae491ec1,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: c4e7f422f05911f0a38c85956e01ac42-20260113 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 1597642201; Tue, 13 Jan 2026 16:27:59 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 02/14] lib/string: extract generic strnlen() into __generic_strnlen() Date: Tue, 13 Jan 2026 16:27:36 +0800 Message-Id: <20260113082748.250916-3-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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" To support performance benchmarking in KUnit tests, extract the generic C implementation of strnlen() into a standalone function __generic_strnlen(). This allows tests to compare architecture-optimized versions against the generic baseline without duplicating code. Suggested-by: Andy Shevchenko Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- include/linux/string.h | 1 + lib/string.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 961645633b4d..04b5fef7a4ec 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -201,6 +201,7 @@ extern __kernel_size_t __generic_strlen(const char *); #ifndef __HAVE_ARCH_STRLEN extern __kernel_size_t strlen(const char *); #endif +extern __kernel_size_t __generic_strnlen(const char *, __kernel_size_t); #ifndef __HAVE_ARCH_STRNLEN extern __kernel_size_t strnlen(const char *,__kernel_size_t); #endif diff --git a/lib/string.c b/lib/string.c index 047ecb38e09b..9dfe7177e290 100644 --- a/lib/string.c +++ b/lib/string.c @@ -430,8 +430,7 @@ size_t strlen(const char *s) EXPORT_SYMBOL(strlen); #endif =20 -#ifndef __HAVE_ARCH_STRNLEN -size_t strnlen(const char *s, size_t count) +size_t __generic_strnlen(const char *s, size_t count) { const char *sc; =20 @@ -439,6 +438,13 @@ size_t strnlen(const char *s, size_t count) /* nothing */; return sc - s; } +EXPORT_SYMBOL(__generic_strnlen); + +#ifndef __HAVE_ARCH_STRNLEN +size_t strnlen(const char *s, size_t count) +{ + return __generic_strnlen(s, count); +} EXPORT_SYMBOL(strnlen); #endif =20 --=20 2.25.1 From nobody Sun Feb 8 20:34:47 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 BB90D3803CF; Tue, 13 Jan 2026 08:28:05 +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=1768292890; cv=none; b=BdC3LhCExZhsqTGxVocWC83u86c7eU+J1y9+3rqGPOAL83YYkZwf7Rsq/sjtUypVsBs9vvZGBjX3scRSlxQ0rUNNRKTMHlbmMZCNPVJAnofB89u3b4ZpL54LGZYi1+YWCX9JHhNrdCAMENSxxHh75yMAwSEMPZt0juv1N4/HTGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292890; c=relaxed/simple; bh=3ZHpmIjN0ZjnHCV57ZC+7ads3azFUMAfVmCYFRN6u5s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tCpghhr9uQbwkcQFcduYp0eX4edj+qcmJtH54ROoT7FSp4tVpGLG7ls1/j33z3WeweZ2bypBVDuoBJ+Hcjsr4QGTsqYuQKctXx44B+sWTIMjGrGtV1jPJYy2wZEDA/xF07adc2uGZiaxoYfkAE8c4LR7kYjRqb7QxzwQzeETwTE= 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: c5e11412f05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:5ab30080-ae50-45b0-aff3-7a6bef4d9733,IP:0,UR L:0,TC:0,Content:-5,EDM:25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:20 X-CID-META: VersionHash:a9d874c,CLOUDID:03724828cc3a3bcdb5f4d61e365e7909,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|850|898,TC:nil,Content:0|15|50 ,EDM:5,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OS A: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: c5e11412f05911f0a38c85956e01ac42-20260113 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 1982222664; Tue, 13 Jan 2026 16:28:01 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 03/14] lib/string: extract generic strchr() into __generic_strchr() Date: Tue, 13 Jan 2026 16:27:37 +0800 Message-Id: <20260113082748.250916-4-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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" To support performance benchmarking in KUnit tests, extract the generic C implementation of strchr() into a standalone function __generic_strchr(). This allows tests to compare architecture-optimized versions against the generic baseline without duplicating code. Suggested-by: Andy Shevchenko Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- include/linux/string.h | 1 + lib/string.c | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 04b5fef7a4ec..57f8bf543891 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -169,6 +169,7 @@ extern int strcasecmp(const char *s1, const char *s2); #ifndef __HAVE_ARCH_STRNCASECMP extern int strncasecmp(const char *s1, const char *s2, size_t n); #endif +extern char *__generic_strchr(const char *, int); #ifndef __HAVE_ARCH_STRCHR extern char * strchr(const char *,int); #endif diff --git a/lib/string.c b/lib/string.c index 9dfe7177e290..8ad9b73ffe4e 100644 --- a/lib/string.c +++ b/lib/string.c @@ -317,6 +317,15 @@ int strncmp(const char *cs, const char *ct, size_t cou= nt) EXPORT_SYMBOL(strncmp); #endif =20 +char *__generic_strchr(const char *s, int c) +{ + for (; *s !=3D (char)c; ++s) + if (*s =3D=3D '\0') + return NULL; + return (char *)s; +} +EXPORT_SYMBOL(__generic_strchr); + #ifndef __HAVE_ARCH_STRCHR /** * strchr - Find the first occurrence of a character in a string @@ -328,10 +337,7 @@ EXPORT_SYMBOL(strncmp); */ char *strchr(const char *s, int c) { - for (; *s !=3D (char)c; ++s) - if (*s =3D=3D '\0') - return NULL; - return (char *)s; + return __generic_strchr(s, c); } EXPORT_SYMBOL(strchr); #endif --=20 2.25.1 From nobody Sun Feb 8 20:34:47 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 6963B37FF54; Tue, 13 Jan 2026 08:28:08 +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=1768292890; cv=none; b=ciS8YemwIyjQevKEgnL2Lm6h+eaGcTASpWo5orO05+nnFRmfsbfzq9LII8UhIl+F1GOTd64sctsUkQqvA+3ItlgWtfJaNmUNcKC3ehX/gSo+RPDJiIP2ULjv0N5dzYicZMpy544BkreKPSwejHEarwDFk+ITGUTWBj/yJfQRlzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292890; c=relaxed/simple; bh=onN357XwpgnJOIWqgd1HTRRX1fgbE0mRqBNoEJ26aos=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZX0iugnM4igbFhyfrqbFPaBrPx+APGV5UcHB5TxXWwd2tCkbn3XWFDWBDZJd62xedv8EERs3voUxSXDMWCQJ6MNwc/S8+nZJr9xBwZFM2DMRFveEQ8XOkQOXsx9tYhUS0W2PvLm31vqo4xLTz769zsMAIIBMBtjmw2A2w/Ra6uk= 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: c676938ef05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:ab728788-6248-41af-a67e-08eadb41e789,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:eeaa946d4107ed81f13ae713443562e6,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: c676938ef05911f0a38c85956e01ac42-20260113 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 954323636; Tue, 13 Jan 2026 16:28:02 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 04/14] lib/string: extract generic strrchr() into __generic_strrchr() Date: Tue, 13 Jan 2026 16:27:38 +0800 Message-Id: <20260113082748.250916-5-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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" To support performance benchmarking in KUnit tests, extract the generic C implementation of strrchr() into a standalone function __generic_strrchr(). This allows tests to compare architecture-optimized versions against the generic baseline without duplicating code. Suggested-by: Andy Shevchenko Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- include/linux/string.h | 1 + lib/string.c | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 57f8bf543891..2ede3ff0865a 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -180,6 +180,7 @@ extern char * strnchrnul(const char *, size_t, int); #ifndef __HAVE_ARCH_STRNCHR extern char * strnchr(const char *, size_t, int); #endif +extern char *__generic_strrchr(const char *, int); #ifndef __HAVE_ARCH_STRRCHR extern char * strrchr(const char *,int); #endif diff --git a/lib/string.c b/lib/string.c index 8ad9b73ffe4e..4405a042eee7 100644 --- a/lib/string.c +++ b/lib/string.c @@ -377,6 +377,18 @@ char *strnchrnul(const char *s, size_t count, int c) return (char *)s; } =20 +char *__generic_strrchr(const char *s, int c) +{ + const char *last =3D NULL; + + do { + if (*s =3D=3D (char)c) + last =3D s; + } while (*s++); + return (char *)last; +} +EXPORT_SYMBOL(__generic_strrchr); + #ifndef __HAVE_ARCH_STRRCHR /** * strrchr - Find the last occurrence of a character in a string @@ -385,12 +397,7 @@ char *strnchrnul(const char *s, size_t count, int c) */ char *strrchr(const char *s, int c) { - const char *last =3D NULL; - do { - if (*s =3D=3D (char)c) - last =3D s; - } while (*s++); - return (char *)last; + return __generic_strrchr(s, c); } EXPORT_SYMBOL(strrchr); #endif --=20 2.25.1 From nobody Sun Feb 8 20:34:48 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 C541D37FF68; Tue, 13 Jan 2026 08:28:23 +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=1768292905; cv=none; b=Axtnd0cTFEDZu8JxRlzIwHApcR7tEgKoELapsJWgmK8ridjNMWgvZcGcQySAu0wjEkGAQDtVSp0PQX8cgIwEm7FLAf10SIkDU08zSUzFv/2f3nbWcQ5wEbhX25ePO1dvMsD7ABlm0AFS5gTmhY6xRtHJKyyqt9xSKGFiuI+azGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292905; c=relaxed/simple; bh=oGyDLUMFF4RuavOs3+hGo5dMR+svt6IDcZqk16aoF0Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fIhZZU1Y1NWWdngb75w19necdPTMSj61kCiRhPOYGdyYC5Wz+HktvN9eB1PqC8rXRivRC1TbFbhwm3L9b0Eqbk1VKFik8c0+la+xiSdYr9ZZOXn73/WZSGY4M0Nv2qZetNgASyksx160ibu3b60Ph5HSYL4RB3Kv8Zr+MLhXt+0= 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: cf83af7af05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:6d592cf2-844d-4c9b-8d33-fd072e1a86d3,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:56091ccb82104e93479814b9c1a7a845,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: cf83af7af05911f0a38c85956e01ac42-20260113 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 1060331112; Tue, 13 Jan 2026 16:28:17 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 05/14] lib/string_kunit: add correctness test for strlen Date: Tue, 13 Jan 2026 16:27:39 +0800 Message-Id: <20260113082748.250916-6-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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 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..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 20:34:48 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 C51AF37FF61; Tue, 13 Jan 2026 08:28:29 +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=1768292912; cv=none; b=a0vdoJppQG2DmtsIvBC0VP2kHMqAHphmPcmzMv2U1sqFPQD1gA14FZkXyNMcFgsWDqL0UNOQPzS+YDDJ8Kr0XWuS7f4YsPNJaVdq0scJhrvFgAjdbiLBIzINGv9crb1MGrkPfsSccK2VToV63wP9PP/Vo5OQlWIDbHqn6dIkmpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292912; c=relaxed/simple; bh=toqItkcZpcsWefHahyIGxjjAbQRZ1APKrEQ2rJ9MVGA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=B6BFlZgM+8hcxyZqE3yYdWCjAl24WZtQwgjqF8zUADIey0OtZ6G5pxh7j5BLs6Yf7IGDRiG4ljpkVVblMbYImZTKPS5ATZpCghXrGhAiDWPcPaGuf2ImbN8eGddnqPVY0g1gGflVObwu9i1Dc8jy16HvRblbIc1SkubS46YK7QQ= 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: d45f9ca2f05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:39197dc9-ddb2-4e6e-a488-8721d5adc264,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:43240f47065a33ce9c3bff626d67e50b,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: d45f9ca2f05911f0a38c85956e01ac42-20260113 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 1685290973; Tue, 13 Jan 2026 16:28:25 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 06/14] lib/string_kunit: add correctness test for strnlen Date: Tue, 13 Jan 2026 16:27:40 +0800 Message-Id: <20260113082748.250916-7-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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. Signed-off-by: Feng Jiang 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 88da97e50c8e..89fe52378442 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -129,6 +129,35 @@ 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); + + 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 + 10), len); + + s[offset + len] =3D 'A'; + } + } +} + static void string_test_strchr(struct kunit *test) { const char *test_string =3D "abcdefghijkl"; @@ -644,6 +673,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 20:34:48 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 58B2137FF72; Tue, 13 Jan 2026 08:28:32 +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=1768292914; cv=none; b=A/u5YC7GqgFl1P2t+1cS0G+NoUl4R/arVkKkHblSjJIurOl1Br0qz5iewk8ejnkgEFHe0a7zozpkOuwxKZ6t6S1xH2MARyeYtN56yjghfuknP505gFuUNv9h7jFQFZGXZ9ebOTL273KyTozHOM9CVPn8dWIdtJl+N+qhN27Af+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292914; c=relaxed/simple; bh=XZwkccJPmA1Go1id6UupKg3JYvR6Yz1IgENYQF3Djbc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=P7fu7phOk+Zor/KmwJePCG1jmSUunDAK8BIUG/rymtXzyjw82FhaQ0oXWUz653Jq7OQl1dcJ9ZU6urXeQKB+Gy0Jq5XoCol9A4FRLI8poCUWbC8rPewo80tY63w3qYNToK5ecRRZTZvvIwxlqDRRZOaf8Ii81N7WUGBt4342U0I= 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: d4fb3e3cf05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:1f34ae33-be50-4d98-8f5d-67c81cb0648d,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:fe853eb09517b7e1e69fa5ffe0a37079,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: d4fb3e3cf05911f0a38c85956e01ac42-20260113 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 1357815399; Tue, 13 Jan 2026 16:28:26 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 07/14] lib/string_kunit: add correctness test for strrchr() Date: Tue, 13 Jan 2026 16:27:41 +0800 Message-Id: <20260113082748.250916-8-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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 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 89fe52378442..8eb095404b95 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -181,6 +181,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"; @@ -676,6 +705,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 20:34:48 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 6B4C137FF74; Tue, 13 Jan 2026 08:28:32 +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=1768292914; cv=none; b=A+mTVOndEH3G49A3b6pViYyBYJXzoOUIl7zjHdtf0pXZMAjP2/4j2pYylI9g7ZK5sxvphXlzcobhnmkXnpi8YOMApHlp2f7JlFba4KSUWDdvNnbxslOmMGTMt/7HRXcpXtH6Jtm54Te2YQW3IsxB/QumrRDKLBCqt500kcST6jQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292914; c=relaxed/simple; bh=4kYWroEgbC/TDWO7MGq5upkrX8k6YjNkSysCjVjfssc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eKVjEXKXF+E4ugehELMMvay9SoHC1zGRa548nhurxjb+eBvaj2UcvQnWrTB/GqooDaeIGN7sca4naTf8ikWZ3Jt2nOVJwFpVAdQMQHqO7agmh6oDnxWHNPdo4heJdodTc60+7pM1Z7VK49yWwvxSqBB02XVvcKHBM30FtP3w2j0= 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: d5918680f05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:50dd73d7-9d29-46a9-882c-f5c7f518d278,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:2bf60e9b387b1767bb21e51aed26440f,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: d5918680f05911f0a38c85956e01ac42-20260113 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 2104796398; Tue, 13 Jan 2026 16:28:27 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 08/14] lib/string_kunit: add performance benchmark for strlen() Date: Tue, 13 Jan 2026 16:27:42 +0800 Message-Id: <20260113082748.250916-9-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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 benchmark to compare the architecture-optimized strlen() implementation against the generic C version (__generic_strlen). The benchmark uses a table-driven approach to evaluate performance across different string lengths (short, medium, and long). It employs ktime_get() for timing and get_random_bytes() followed by null-byte filtering to generate test data that prevents early termination. This helps in quantifying the performance gains of architecture-specific optimizations on various platforms. Suggested-by: Andy Shevchenko Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- lib/tests/string_kunit.c | 117 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index 8eb095404b95..2266954ae5e0 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -20,6 +20,77 @@ #define STRING_TEST_MAX_LEN 128 #define STRING_TEST_MAX_OFFSET 16 =20 +#if defined(__HAVE_ARCH_STRLEN) +#define STRING_BENCH_ENABLED +#endif + +#ifdef STRING_BENCH_ENABLED +/* Configuration for string benchmark scenarios */ +struct string_bench_case { + const char *name; + size_t len; + unsigned int iterations; +}; + +static const struct string_bench_case bench_cases[] =3D { + {"short", 8, 100000}, + {"medium", 64, 100000}, + {"long", 2048, 10000}, +}; + +/** + * get_max_bench_len() - Get the maximum length from benchmark cases + * @cases: array of test cases + * @count: number of cases + */ +static size_t get_max_bench_len(const struct string_bench_case *cases, siz= e_t count) +{ + size_t i, max_len =3D 0; + + for (i =3D 0; i < count; i++) { + if (cases[i].len > max_len) + max_len =3D cases[i].len; + } + + return max_len; +} + +/** + * get_random_nonzero_bytes() - Fill buffer with random non-null bytes + * @buf: buffer to fill + * @len: number of bytes to fill + */ +static void get_random_nonzero_bytes(void *buf, size_t len) +{ + u8 *s =3D (u8 *)buf; + + get_random_bytes(buf, len); + + /* Replace null bytes to avoid early string termination */ + for (size_t i =3D 0; i < len; i++) { + if (s[i] =3D=3D '\0') + s[i] =3D 0x01; + } +} + +static void string_bench_report(struct kunit *test, const char *func, + const struct string_bench_case *bc, + u64 time_arch, u64 time_generic) +{ + u64 ratio_int, ratio_frac; + + /* Calculate speedup ratio with 2 decimal places. */ + ratio_int =3D div64_u64(time_generic, time_arch); + ratio_frac =3D div64_u64((time_generic % time_arch) * 100, time_arch); + + kunit_info(test, "%s performance (%s, len: %zu, iters: %u):\n", + func, bc->name, bc->len, bc->iterations); + kunit_info(test, " arch-optimized: %llu ns\n", time_arch); + kunit_info(test, " generic C: %llu ns\n", time_generic); + kunit_info(test, " speedup: %llu.%02llux\n", ratio_int, ratio_fra= c); +} +#endif /* STRING_BENCH_ENABLED */ + static void string_test_memset16(struct kunit *test) { unsigned i, j, k; @@ -129,6 +200,49 @@ static void string_test_strlen(struct kunit *test) } } =20 +#ifdef __HAVE_ARCH_STRLEN +static void string_test_strlen_bench(struct kunit *test) +{ + char *buf; + size_t buf_len, iters; + ktime_t start, end; + u64 time_arch, time_generic; + + buf_len =3D get_max_bench_len(bench_cases, ARRAY_SIZE(bench_cases)) + 1; + + buf =3D kunit_kzalloc(test, buf_len, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); + + for (size_t i =3D 0; i < ARRAY_SIZE(bench_cases); i++) { + get_random_nonzero_bytes(buf, bench_cases[i].len); + buf[bench_cases[i].len] =3D '\0'; + + iters =3D bench_cases[i].iterations; + + /* 1. Benchmark the architecture-optimized version */ + start =3D ktime_get(); + for (unsigned int j =3D 0; j < iters; j++) { + OPTIMIZER_HIDE_VAR(buf); + (void)strlen(buf); + } + end =3D ktime_get(); + time_arch =3D ktime_to_ns(ktime_sub(end, start)); + + /* 2. Benchmark the generic C version */ + start =3D ktime_get(); + for (unsigned int j =3D 0; j < iters; j++) { + OPTIMIZER_HIDE_VAR(buf); + (void)__generic_strlen(buf); + } + end =3D ktime_get(); + time_generic =3D ktime_to_ns(ktime_sub(end, start)); + + string_bench_report(test, "strlen", &bench_cases[i], + time_arch, time_generic); + } +} +#endif + static void string_test_strnlen(struct kunit *test) { char *s; @@ -702,6 +816,9 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_memset32), KUNIT_CASE(string_test_memset64), KUNIT_CASE(string_test_strlen), +#ifdef __HAVE_ARCH_STRLEN + KUNIT_CASE(string_test_strlen_bench), +#endif KUNIT_CASE(string_test_strnlen), KUNIT_CASE(string_test_strchr), KUNIT_CASE(string_test_strnchr), --=20 2.25.1 From nobody Sun Feb 8 20:34:48 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 4BCDC3803C3; Tue, 13 Jan 2026 08:28:33 +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=1768292919; cv=none; b=na5Rvdxxai+8a5LOZ4CD6Z5yp0+Hgt13y4WkIx7xf+22kio+sL67rr3WLMjhbCmkZhEncgdMHteRgBx2wnNtCCwa6Tc5IctWK1r2mvJbWpfDDUgbBiZ+1uf4deHAKKrMr9WoJiw0uhjI67NUoU+m+FQgYhQSlC1a5Edi8upja4k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292919; c=relaxed/simple; bh=lGKnYIpD5iPajFU0H5v2enpysWFcaB3u6bhBnKsgqQw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hgTN6vk37yrBZEXUhx6q+FXypFHwtSClyoSpLJPEhQBwULSVa+agF6l7OIRU6BoDQpmJjh0lHSJfDWHSJA63DtsFqwcti8zwxgedBNZOAyXRKFAll+XoEpU0I2uurwXXBn03hlxDSm7AqOE5Qig+P+19WJybiCePLDVBLcSTXVI= 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: d61ef7b8f05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:20aca996-3ffb-4c7c-936d-54af30a3665c,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:30b8f83c4b3fdd7a718fa5b9b3b72304,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: d61ef7b8f05911f0a38c85956e01ac42-20260113 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 307928599; Tue, 13 Jan 2026 16:28:28 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 09/14] lib/string_kunit: add performance benchmark for strnlen() Date: Tue, 13 Jan 2026 16:27:43 +0800 Message-Id: <20260113082748.250916-10-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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 benchmark to compare the architecture-optimized strnlen() implementation against the generic C version (__generic_strnlen). Suggested-by: Andy Shevchenko Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- lib/tests/string_kunit.c | 48 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index 2266954ae5e0..6578b36213bc 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -20,7 +20,7 @@ #define STRING_TEST_MAX_LEN 128 #define STRING_TEST_MAX_OFFSET 16 =20 -#if defined(__HAVE_ARCH_STRLEN) +#if defined(__HAVE_ARCH_STRLEN) || defined(__HAVE_ARCH_STRNLEN) #define STRING_BENCH_ENABLED #endif =20 @@ -272,6 +272,49 @@ static void string_test_strnlen(struct kunit *test) } } =20 +#ifdef __HAVE_ARCH_STRNLEN +static void string_test_strnlen_bench(struct kunit *test) +{ + char *buf; + size_t buf_len, iters; + ktime_t start, end; + u64 time_arch, time_generic; + + buf_len =3D get_max_bench_len(bench_cases, ARRAY_SIZE(bench_cases)) + 1; + + buf =3D kunit_kzalloc(test, buf_len, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); + + for (size_t i =3D 0; i < ARRAY_SIZE(bench_cases); i++) { + get_random_nonzero_bytes(buf, bench_cases[i].len); + buf[bench_cases[i].len] =3D '\0'; + + iters =3D bench_cases[i].iterations; + + /* 1. Benchmark the architecture-optimized version */ + start =3D ktime_get(); + for (unsigned int j =3D 0; j < iters; j++) { + OPTIMIZER_HIDE_VAR(buf); + (void)strnlen(buf, buf_len); + } + end =3D ktime_get(); + time_arch =3D ktime_to_ns(ktime_sub(end, start)); + + /* 2. Benchmark the generic C version */ + start =3D ktime_get(); + for (unsigned int j =3D 0; j < iters; j++) { + OPTIMIZER_HIDE_VAR(buf); + (void)__generic_strnlen(buf, buf_len); + } + end =3D ktime_get(); + time_generic =3D ktime_to_ns(ktime_sub(end, start)); + + string_bench_report(test, "strnlen", &bench_cases[i], + time_arch, time_generic); + } +} +#endif + static void string_test_strchr(struct kunit *test) { const char *test_string =3D "abcdefghijkl"; @@ -820,6 +863,9 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_strlen_bench), #endif KUNIT_CASE(string_test_strnlen), +#ifdef __HAVE_ARCH_STRNLEN + KUNIT_CASE(string_test_strnlen_bench), +#endif KUNIT_CASE(string_test_strchr), KUNIT_CASE(string_test_strnchr), KUNIT_CASE(string_test_strrchr), --=20 2.25.1 From nobody Sun Feb 8 20:34:48 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 5FA653803E1; Tue, 13 Jan 2026 08:28:34 +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=1768292919; cv=none; b=vFlTuVgNs57TWRcVAKQ6m7IgT3lkhClTv6K89QMoM2GD8cxMMA07h3BrUBohqAijQd/tVPClNbZIXIekoFdtK1YtpDpslHalwIQHgU/kcJCIsMBj4UhebjAAwONYOUtUEtMPoQVA71cnPw7hlebDpymMfDNH9rYcItKyl2DF/Fo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292919; c=relaxed/simple; bh=a8MhfH0dkoY1bJbFVRaBUvbs0Zd9HlE02zyqMjwB26M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Nv6F3U63gHBX2oekDEKmwgKN+v1hk2HDBStPos9nI0tk5h7W+Zd4EpAmCzrTFcQxxkG9pLE77XA6ZdFmKH6ZZOMF9jc2qVnkd83N3m43B8nYXY1818zTa5b7QeE1UhoC611c+wJDGzBuYxnoxFu7hQ18XNdLwO5dVUf1IwQRf+Q= 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: d6ae1ddaf05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:0e4d3c4f-a374-47d5-9fb6-8c45f2790522,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:d528e2ec9eed477e95c28352806b8da3,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: d6ae1ddaf05911f0a38c85956e01ac42-20260113 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 1386593411; Tue, 13 Jan 2026 16:28:29 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 10/14] lib/string_kunit: add performance benchmark for strchr() Date: Tue, 13 Jan 2026 16:27:44 +0800 Message-Id: <20260113082748.250916-11-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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 benchmark to compare the architecture-optimized strchr() implementation against the generic C version (__generic_strchr). Suggested-by: Andy Shevchenko Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- lib/tests/string_kunit.c | 49 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index 6578b36213bc..e8d527336030 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -20,7 +20,8 @@ #define STRING_TEST_MAX_LEN 128 #define STRING_TEST_MAX_OFFSET 16 =20 -#if defined(__HAVE_ARCH_STRLEN) || defined(__HAVE_ARCH_STRNLEN) +#if defined(__HAVE_ARCH_STRLEN) || defined(__HAVE_ARCH_STRNLEN) || \ + defined(__HAVE_ARCH_STRCHR) #define STRING_BENCH_ENABLED #endif =20 @@ -367,6 +368,49 @@ static void string_test_strrchr(struct kunit *test) } } =20 +#ifdef __HAVE_ARCH_STRCHR +static void string_test_strchr_bench(struct kunit *test) +{ + char *buf; + size_t buf_len, iters; + ktime_t start, end; + u64 time_arch, time_generic; + + buf_len =3D get_max_bench_len(bench_cases, ARRAY_SIZE(bench_cases)) + 1; + + buf =3D kunit_kzalloc(test, buf_len, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); + + for (size_t i =3D 0; i < ARRAY_SIZE(bench_cases); i++) { + get_random_nonzero_bytes(buf, bench_cases[i].len); + buf[bench_cases[i].len] =3D '\0'; + + iters =3D bench_cases[i].iterations; + + /* 1. Benchmark the architecture-optimized version */ + start =3D ktime_get(); + for (unsigned int j =3D 0; j < iters; j++) { + OPTIMIZER_HIDE_VAR(buf); + (void)strchr(buf, '\0'); + } + end =3D ktime_get(); + time_arch =3D ktime_to_ns(ktime_sub(end, start)); + + /* 2. Benchmark the generic C version */ + start =3D ktime_get(); + for (unsigned int j =3D 0; j < iters; j++) { + OPTIMIZER_HIDE_VAR(buf); + (void)__generic_strchr(buf, '\0'); + } + end =3D ktime_get(); + time_generic =3D ktime_to_ns(ktime_sub(end, start)); + + string_bench_report(test, "strchr", &bench_cases[i], + time_arch, time_generic); + } +} +#endif + static void string_test_strnchr(struct kunit *test) { const char *test_string =3D "abcdefghijkl"; @@ -867,6 +911,9 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_strnlen_bench), #endif KUNIT_CASE(string_test_strchr), +#ifdef __HAVE_ARCH_STRCHR + KUNIT_CASE(string_test_strchr_bench), +#endif KUNIT_CASE(string_test_strnchr), KUNIT_CASE(string_test_strrchr), KUNIT_CASE(string_test_strspn), --=20 2.25.1 From nobody Sun Feb 8 20:34:48 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 DEC5137FF7C; Tue, 13 Jan 2026 08:28:34 +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=1768292918; cv=none; b=WXM4U90aXFcPnZWAppETOjIWLXnLSVDOQFq16wwgPa05sYr07hvXMfNvWm//SDVd9K6KERqjdanQhE86KPEAtdxNipqGYWUZWMGgsQ3AjxQSe+BO5ZKob7XtelP0dUlbeuIbq/iGmzZjrey4FEKFxkoEMClBOb19yThoKaasYoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292918; c=relaxed/simple; bh=cd2Au7zD0NRrQcN2SISsvZ4+SPhtuTLZeSAzYWRt8kg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=khcescb+YsUnnmlNx7qudcP+puN9o0S4l8Un3zeSNS8IQTv0MK/EXj3oRj2cq9QIxMkUqD7nRvIu03iQaDa7/zzRBHubFCPGhs4f9O0ai68QYsH1nRmwf6uBRSEDcBYBvGCKDloDfnrZLY11qin9g1wcyFrJCl8mpPnrJ4DjAho= 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: d741bc8ef05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:9154393b-57e9-4d90-9535-5debdf92c8db,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:3ab1f4e58411f53bcc71218cd9959621,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: d741bc8ef05911f0a38c85956e01ac42-20260113 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 1298105090; Tue, 13 Jan 2026 16:28:30 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 11/14] lib/string_kunit: add performance benchmark for strrchr() Date: Tue, 13 Jan 2026 16:27:45 +0800 Message-Id: <20260113082748.250916-12-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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 benchmark to compare the architecture-optimized strrchr() implementation against the generic C version (__generic_strrchr). Suggested-by: Andy Shevchenko Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- lib/tests/string_kunit.c | 50 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index e8d527336030..89b583cec2c8 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -21,7 +21,7 @@ #define STRING_TEST_MAX_OFFSET 16 =20 #if defined(__HAVE_ARCH_STRLEN) || defined(__HAVE_ARCH_STRNLEN) || \ - defined(__HAVE_ARCH_STRCHR) + defined(__HAVE_ARCH_STRCHR) || defined(__HAVE_ARCH_STRRCHR) #define STRING_BENCH_ENABLED #endif =20 @@ -444,6 +444,51 @@ static void string_test_strnchr(struct kunit *test) KUNIT_ASSERT_NULL(test, result); } =20 +#ifdef __HAVE_ARCH_STRRCHR +static void string_test_strrchr_bench(struct kunit *test) +{ + char *buf; + size_t buf_len, iters; + ktime_t start, end; + u64 time_arch, time_generic; + + buf_len =3D get_max_bench_len(bench_cases, ARRAY_SIZE(bench_cases)) + 1; + + buf =3D kunit_kzalloc(test, buf_len, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); + + for (size_t i =3D 0; i < ARRAY_SIZE(bench_cases); i++) { + get_random_nonzero_bytes(buf, bench_cases[i].len); + if (bench_cases[i].len > 0) + buf[bench_cases[i].len - 1] =3D 'A'; + buf[bench_cases[i].len] =3D '\0'; + + iters =3D bench_cases[i].iterations; + + /* 1. Benchmark the architecture-optimized version */ + start =3D ktime_get(); + for (unsigned int j =3D 0; j < iters; j++) { + OPTIMIZER_HIDE_VAR(buf); + (void)strrchr(buf, 'A'); + } + end =3D ktime_get(); + time_arch =3D ktime_to_ns(ktime_sub(end, start)); + + /* 2. Benchmark the generic C version */ + start =3D ktime_get(); + for (unsigned int j =3D 0; j < iters; j++) { + OPTIMIZER_HIDE_VAR(buf); + (void)__generic_strrchr(buf, 'A'); + } + end =3D ktime_get(); + time_generic =3D ktime_to_ns(ktime_sub(end, start)); + + string_bench_report(test, "strrchr", &bench_cases[i], + time_arch, time_generic); + } +} +#endif + static void string_test_strspn(struct kunit *test) { static const struct strspn_test { @@ -916,6 +961,9 @@ static struct kunit_case string_test_cases[] =3D { #endif KUNIT_CASE(string_test_strnchr), KUNIT_CASE(string_test_strrchr), +#ifdef __HAVE_ARCH_STRRCHR + KUNIT_CASE(string_test_strrchr_bench), +#endif 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 20:34:48 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 3F70B3803C5; Tue, 13 Jan 2026 08:28:35 +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=1768292922; cv=none; b=I4VuX/w8PO2DmxKv9fbiqs2TdljBaVOpDN6GU/XOV2KLbtNt7TVTjgMvDgVNym4N7r8HdctoZqKk/e3Qp/9UKWtUwTJSf7s6hQIuAONXMTTKZTKWn7V812JUocxAmQ61iWoNbcpD78TJnsSFlMVkYqeXPPSPtXcM5iH/NKNNv5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292922; c=relaxed/simple; bh=QYna/9InUonud79iQh6HbeX+d/PLiJ2pkZ46TiDIJ9E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rZFfvsHfQHNd/yVJWozCb6ggrYXzM1bvCW6Tuxkydlydy15NXj7QrU9yLDGZnlssh9v9/TEX9ejf3JgZARaYQajOuRYDevn3BxGDz/ZOnb0EZdcukl8BWIa0aZzzL7Lfyb42N0mla3pkxMcbOaQbGeQf+tRcSoF4ZNOQesDrSW0= 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: d7f6b364f05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:dcc7d49c-58dc-4c4c-b5f8-99c5c72c3da4,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:a214e34485e2101a22352d4d17012f31,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: d7f6b364f05911f0a38c85956e01ac42-20260113 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 1908451639; Tue, 13 Jan 2026 16:28:31 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 12/14] riscv: lib: add strnlen implementation Date: Tue, 13 Jan 2026 16:27:46 +0800 Message-Id: <20260113082748.250916-13-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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 strnlen() implementation for RISC-V with both generic and Zbb-optimized versions, derived from strlen.S. Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- 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 20:34:48 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 6C2873803D9; Tue, 13 Jan 2026 08:28:43 +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=1768292928; cv=none; b=IV9GpG4kugg9r+9gBBXDeXZtprgmRnUe15IHdcjFhznhgIkyPt7D838oUoL4+q5S9aNnbEI+i2tLXw6QoYigvionGFqxNCrlBw+eHZtQGOTjkWbXNA8LK4/dxc237Xlwz5LyLYDYnwKJNA/ZeZqMp29wWWVbD7YO3ZACnvn7C0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292928; c=relaxed/simple; bh=afcj8/Yktki+F49rgq+aLQjXw0XH0lkknvdoVZd8CO8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HtI4xf2Z6f+lrwYNfvpwK8WamsXVYDkraLWvPUNabIY68BWD5t0nO5FZbmKnNx6Jy/pqp+nEBxi3RJ+IqY2p7mGjdA4Wrj6023ixhbqWrMcAA71jiu9EfrWeDo/qighVpJNmY1oS+saObkdyski45FoSUneJopkw9l0EvTH9ldQ= 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: d89a2cecf05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:e19622ed-3017-495c-ac1e-4f6fae6d5620,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:c6c1247977cc40706e05238dd5d60eee,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: d89a2cecf05911f0a38c85956e01ac42-20260113 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 627804380; Tue, 13 Jan 2026 16:28:32 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 13/14] riscv: lib: add strchr implementation Date: Tue, 13 Jan 2026 16:27:47 +0800 Message-Id: <20260113082748.250916-14-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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 basic assembly implementation of strchr() for RISC-V. This provides a functional byte-by-byte version as a foundation for future optimizations. Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- 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 20:34:48 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 776383803D1; Tue, 13 Jan 2026 08:28:38 +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=1768292924; cv=none; b=ujISNEnwhIheJdR63N4GOd5Gw7PCF7YJtDOJQzUhaG/KVPlyVIgJsCecj5Pxe02AmeKMGoB6xOnHx7w9jVrGR/bCHHw1kI/RLDAAgUmJnYJz3UNvm5ZzylSpnw/IBXVfm0lJG3lInjjMlBXhHbk/ondH15AS1cem9gNFPpoe7OU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768292924; c=relaxed/simple; bh=8VOL0Zs2hpEUWFrxw5LD+5zqYAq782+6NeoCBbvPCt0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IOmrcwftmvBDG2uaIRTKRdru1jb9PhYwfkCyUZKPd2MidQCzHVetlSBMS08x4rvrYTSklvp0XEvJ8bDRVQcddHaLfusBFhHh6bDlz/w3KiePc31wSj1Mm0IX0wbH0bWIQ0IpragEJS8jGk4eShrv8cM+kTJC01X8+ViubkAB0nI= 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: d9845f42f05911f0a38c85956e01ac42-20260113 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.6,REQID:92f5d7f6-195d-4090-835b-3de0df47975e,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:45b9916ce57b837e6c90bded6d5c2a3c,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: d9845f42f05911f0a38c85956e01ac42-20260113 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 1937285911; Tue, 13 Jan 2026 16:28:34 +0800 From: Feng Jiang To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, andy@kernel.org, akpm@linux-foundation.org, jiangfeng@kylinos.cn, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, ajones@ventanamicro.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 v2 14/14] riscv: lib: add strrchr implementation Date: Tue, 13 Jan 2026 16:27:48 +0800 Message-Id: <20260113082748.250916-15-jiangfeng@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260113082748.250916-1-jiangfeng@kylinos.cn> References: <20260113082748.250916-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 basic assembly implementation of strrchr() for RISC-V. This provides a functional byte-by-byte version as a foundation for future optimizations. Signed-off-by: Feng Jiang Tested-by: Joel Stanley --- 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