From nobody Mon Feb 9 06:30:56 2026 Received: from pv50p00im-ztdg10021801.me.com (pv50p00im-ztdg10021801.me.com [17.58.6.56]) (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 2DEE6253F1F for ; Mon, 7 Apr 2025 13:15:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.6.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744031751; cv=none; b=MmlDB1hrtzrIM333HOLIm9RAoijRGHReZsBclL6QNkvRX12bP9hmj6rt+iEdEbpXvU1SpB2C257Z3qbWxYg89dhdxBiATjQBr+mMUdrNQvrjEEM6c01EWSpX7vNKCsQaXVCBzL09reE4pt50kPC/vxfLceQAK2Z85BzECk9gP2I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744031751; c=relaxed/simple; bh=GHNtTngkS1/wEEJahVgx/TmOQbOiuDZCZjQORypgLVQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=J/cCSwcu7Fw3Q3Dph30X+ovhqFMAoTuide9pYmNsCrPxHaybheoEXr0hMlnuvjjrLXMAG3rYYy5USp9F/ZPKWE6rpHsiX3uzm7os6n15Ic/VzAoaInsTTVEwpDpvUgmc0yzAY8EGNNNoP5mXVdlFdQU/DfAcICwvQzGmLD3jbKs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=ufZH2mgr; arc=none smtp.client-ip=17.58.6.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="ufZH2mgr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; bh=vtDqL/q0KP2hj02Cs2anspKNhY3dgQE5PgTeatZ2yk4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=ufZH2mgrRcm/IK/h6lJ+iw597k5Kf14MSbvwkj/7auewPXQOQx7IoYImQOSZabTnA g6YvD6YPzZp45SNK9EQ2F6efiIq9+h4SuEpNn6uwmnPKc/Jf3FW+YOxnqZy+WyOsKw mf1vfch4Z1ZRy3VmLI+oMHAYzfD1oapI2RpqhIFwKrze1Cow203CsbLZ3nwQuWYB/p g65Fy5nRQL+aIIQ08w5Dn0oBKp9spBqTq2yctgLzV4HJj0z29fCWVuGPKAi8LBEvNY CRh1hRlZZD3pFD648BxWKrslCHf5ECJvjrCRIdXhV2knpU4Xey4Gr7c6EHgL6oewMv oirDMRvF7bLfQ== Received: from [192.168.1.26] (pv50p00im-dlb-asmtp-mailmevip.me.com [17.56.9.10]) by pv50p00im-ztdg10021801.me.com (Postfix) with ESMTPSA id 38FE22010320; Mon, 7 Apr 2025 13:15:43 +0000 (UTC) From: Zijun Hu Date: Mon, 07 Apr 2025 21:15:04 +0800 Subject: [PATCH 1/2] lib/string: Improve strstarts() performance Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250407-imp_str_perf-v1-1-ed95d52964a4@quicinc.com> References: <20250407-imp_str_perf-v1-0-ed95d52964a4@quicinc.com> In-Reply-To: <20250407-imp_str_perf-v1-0-ed95d52964a4@quicinc.com> To: Kees Cook , Andy Shevchenko , Andrew Morton Cc: Zijun Hu , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Zijun Hu , "Rob Herring (Arm)" X-Mailer: b4 0.14.2 X-Proofpoint-GUID: 9MdJPuTmMJHKkAxInAZpou0XkpHhLgq1 X-Proofpoint-ORIG-GUID: 9MdJPuTmMJHKkAxInAZpou0XkpHhLgq1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1095,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-04-07_04,2025-04-03_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 spamscore=0 mlxlogscore=703 mlxscore=0 phishscore=0 clxscore=1015 suspectscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2504070093 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu strstarts() is frequently invoked to test if a string has another string as prefix, but its performance is degraded by the strlen() loop contained. Improve its performance by eliminating the strlen() loop. Link: https://lore.kernel.org/all/20250113234643.GA3631169-robh@kernel.org Signed-off-by: Zijun Hu Cc: Rob Herring (Arm) --- include/linux/string.h | 10 +--------- lib/string.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 01621ad0f598ccdfae819b1a5058607e79d8a751..e5f7defa277572719e1dbfdd264= f3de6ef8544f1 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -345,15 +345,7 @@ extern ssize_t memory_read_from_buffer(void *to, size_= t count, loff_t *ppos, =20 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out); =20 -/** - * strstarts - does @str start with @prefix? - * @str: string to examine - * @prefix: prefix to look for. - */ -static inline bool strstarts(const char *str, const char *prefix) -{ - return strncmp(str, prefix, strlen(prefix)) =3D=3D 0; -} +bool strstarts(const char *str, const char *prefix); =20 size_t memweight(const void *ptr, size_t bytes); =20 diff --git a/lib/string.c b/lib/string.c index eb4486ed40d259e43dfb63d76c2bd57ef74e2fd1..ea52c8509328358e436766b1186= a82419d45089d 100644 --- a/lib/string.c +++ b/lib/string.c @@ -285,6 +285,28 @@ int strcmp(const char *cs, const char *ct) EXPORT_SYMBOL(strcmp); #endif =20 +/** + * strstarts - does @str start with @prefix? + * @str: string to examine + * @prefix: prefix to look for. + */ +bool strstarts(const char *str, const char *prefix) +{ + unsigned char c1, c2; + + do { + c1 =3D *str++; + c2 =3D *prefix++; + + if (c1 !=3D c2) + return c2 =3D=3D '\0'; + + } while (c2 !=3D '\0'); + + return true; +} +EXPORT_SYMBOL(strstarts); + #ifndef __HAVE_ARCH_STRNCMP /** * strncmp - Compare two length-limited strings --=20 2.34.1 From nobody Mon Feb 9 06:30:56 2026 Received: from pv50p00im-ztdg10021801.me.com (pv50p00im-ztdg10021801.me.com [17.58.6.56]) (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 4328B253B53 for ; Mon, 7 Apr 2025 13:15:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.6.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744031757; cv=none; b=Vrdo+QLD6yXCEX+FLib+wDx8P3Fc1ghiSRtS55H1lqgmPvvlSoCiOvYFI6kP9iY47CBiaoNHRMNTCW4cDxt6O9icFTU8FTWNXCR8g1bc6+pzpurJ/AqkSirUvdFWY53LIIBPJEVb1hyIS/QLp5REAoyhlyUEFPQStOrw8TLYfZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744031757; c=relaxed/simple; bh=XrX/Z+AOhfXmEUSaVhxUD9cmEn5LGPbbovm8Fw8fiFs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gtSLV8mtgFbLPuWn+U7TmC7aT5l9wTOzVPex9kkjsX6rI/eaRLERwSnFWb72D50tDmFSj3FaYzmaD2hTdlb5IqBi5A8/Pbw+eOcRWtGbFLdwhFiL6U3bkgEvy6bu/7KHKIcTyo9bG+B65xpJkax91VGJQgcR1gBbrL2fJDIigDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=0Nlrh5hF; arc=none smtp.client-ip=17.58.6.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="0Nlrh5hF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; bh=oFXzI48UtHlC5gVTKT4LJShhvIch9BxoEqw0fKpqk/4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=0Nlrh5hF70k5PHoBhb2eYeJt2px45N3je3kaQ5F9mridzEOH8j118rRFE2nc7gwPD q4AkEcyfwcwsKPFP38IAXj1W2rhT5FI2OiSqKZRgI0X0R7XLpUfHnfBeGW4fjH9P45 BUsw9JfeFwstjvf3YJZOzM/e6YK+DKtFfVVul5HE+kSPAjPqW0NPVbx7+jcy8//YhR oqunN5O2b/XfOM60659TwJWmjWxqFfDYKI0rPUBf48Ylmd/yleeZsDLrYZm0wBDzx8 aJvxfh0OtA8fg1DohpJ7MfaO3KMDLoN9DZv+HUsd7Dwp7rzRpMpszus7fjODqMdu0d STFU4pgWYJhYA== Received: from [192.168.1.26] (pv50p00im-dlb-asmtp-mailmevip.me.com [17.56.9.10]) by pv50p00im-ztdg10021801.me.com (Postfix) with ESMTPSA id 69C4420103AB; Mon, 7 Apr 2025 13:15:49 +0000 (UTC) From: Zijun Hu Date: Mon, 07 Apr 2025 21:15:05 +0800 Subject: [PATCH 2/2] lib/string: Improve str_has_prefix() performance Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250407-imp_str_perf-v1-2-ed95d52964a4@quicinc.com> References: <20250407-imp_str_perf-v1-0-ed95d52964a4@quicinc.com> In-Reply-To: <20250407-imp_str_perf-v1-0-ed95d52964a4@quicinc.com> To: Kees Cook , Andy Shevchenko , Andrew Morton Cc: Zijun Hu , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Zijun Hu , "Rob Herring (Arm)" X-Mailer: b4 0.14.2 X-Proofpoint-GUID: 2pNzYH1mkE44dWijZpEUiyG1UerlZF5X X-Proofpoint-ORIG-GUID: 2pNzYH1mkE44dWijZpEUiyG1UerlZF5X X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1095,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-04-07_04,2025-04-03_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 spamscore=0 mlxlogscore=843 mlxscore=0 phishscore=0 clxscore=1015 suspectscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2504070093 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu str_has_prefix() is frequently invoked to return length of the prefix string if a string has another string as prefix, but its performance is degraded by the strlen() loop contained. Improve its performance by eliminating the strlen() loop. Link: https://lore.kernel.org/all/20250113234643.GA3631169-robh@kernel.org Signed-off-by: Zijun Hu Cc: Rob Herring (Arm) --- include/linux/string.h | 21 +-------------------- lib/string.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index e5f7defa277572719e1dbfdd264f3de6ef8544f1..394b76666ece0924c50264aaca3= 9784d5630a2fe 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -533,25 +533,6 @@ void memcpy_and_pad(void *dest, size_t dest_len, const= void *src, size_t count, sizeof(*(obj)) - offsetof(typeof(*(obj)), member)); \ }) =20 -/** - * str_has_prefix - Test if a string has a given prefix - * @str: The string to test - * @prefix: The string to see if @str starts with - * - * A common way to test a prefix of a string is to do: - * strncmp(str, prefix, sizeof(prefix) - 1) - * - * But this can lead to bugs due to typos, or if prefix is a pointer - * and not a constant. Instead use str_has_prefix(). - * - * Returns: - * * strlen(@prefix) if @str starts with @prefix - * * 0 if @str does not start with @prefix - */ -static __always_inline size_t str_has_prefix(const char *str, const char *= prefix) -{ - size_t len =3D strlen(prefix); - return strncmp(str, prefix, len) =3D=3D 0 ? len : 0; -} +size_t str_has_prefix(const char *str, const char *prefix); =20 #endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index ea52c8509328358e436766b1186a82419d45089d..17f1a070b190debc3eaeff6d1ae= 45b55bae69b29 100644 --- a/lib/string.c +++ b/lib/string.c @@ -332,6 +332,39 @@ int strncmp(const char *cs, const char *ct, size_t cou= nt) EXPORT_SYMBOL(strncmp); #endif =20 +/** + * str_has_prefix - Test if a string has a given prefix + * @str: The string to test + * @prefix: The string to see if @str starts with + * + * A common way to test a prefix of a string is to do: + * strncmp(str, prefix, sizeof(prefix) - 1) + * + * But this can lead to bugs due to typos, or if prefix is a pointer + * and not a constant. Instead use str_has_prefix(). + * + * Returns: + * * strlen(@prefix) if @str starts with @prefix + * * 0 if @str does not start with @prefix + */ +size_t str_has_prefix(const char *str, const char *prefix) +{ + const char *p =3D prefix; + unsigned char c1, c2; + + do { + c1 =3D *str++; + c2 =3D *p++; + + if (c1 !=3D c2) + return c2 =3D=3D '\0' ? p - 1 - prefix : 0; + + } while (c2 !=3D '\0'); + + return p - 1 - prefix; +} +EXPORT_SYMBOL(str_has_prefix); + #ifndef __HAVE_ARCH_STRCHR /** * strchr - Find the first occurrence of a character in a string --=20 2.34.1