From nobody Sun May 24 22:35:58 2026 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC2483546CA for ; Wed, 20 May 2026 15:56:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779292573; cv=none; b=an5BEt4scXHDLjA6PYPXzJ7pxRA91snXlgwz8vJA05uEo7sZQha9nNiuWCPUkfobuYl/SrKSddvdzHnSyLBPPzgBFeecKvJ0iuERWHJ+ayZMAjddEmduc7s7rv7zrjq9hLMZRLfXyWq7gM5dcqKkpE3XvRuvpB32iw24pZIVxWY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779292573; c=relaxed/simple; bh=waoLV/3bEXLRYpEwx6fHog68kuidaBtU4PbGxQ5fRhQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RXuRo+TPI5ZkVBoNKQJF2I0PLTSu0LkDrC0t9ncActJVBQ7dkQDNmnmMxlFolGq7FjO9NP8iGAe4kK6I/1cPQOGQ26aGutBacA7NDxmab75veQZJ+mah2zeK0V9MTCUZITdvRwziIPANJFptHhJyb33OcULj5W3FpMEX7J6dP10= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SEfifVXB; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SEfifVXB" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-48984d29fe3so56152015e9.0 for ; Wed, 20 May 2026 08:56:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779292569; x=1779897369; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mBpbLd/H5G6gmbkig9FYBk9kTUwsFZEKMdkIA0bLw8E=; b=SEfifVXBB0NmMfdSzCxTS73frZnlrIaT5tJoU9Ejsn2GZxUB8tJNE+zlFEF3BAmdcP g3FlzADsODoz8h9DIpGPHS60ogwWWxU1O3bbkfpT50Fhb5pAMlg4rhz+U7MV5BcaItNL CaznmVqKDi2hKfjhv/f/T50zf04KXKzwjxJwkWok4KZKjq47ls/ob1zO7W7X+GxcO9pD bi0K/olqcs2IIFGhNKoc4XImCaSQyoYCY7DCKNV7hfS08Aty6NqHlAV6D7CAfFb34mE2 k9FJWzFfVqOTKQSvUWihedH9EChV/wiZbmAdcHTZDtWbbSnEGaj2ppRL34UPJ8EWs7ld hmRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779292569; x=1779897369; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=mBpbLd/H5G6gmbkig9FYBk9kTUwsFZEKMdkIA0bLw8E=; b=VCTaboaT9gYtwteoqk0yDJ0cj/dvRotVu5QKvrjgLyZZ/AHJKFUS+9bQH+xLrFnk+w brCX0tPzqrKorwF7lVuBRZEy2Gt/IVdvGhF2idJ/dHpWX4S+3p+mkWLt7LTipjfZeerW aOVgWDkywiiPnTZwwUXeYJldrqyQhpJyEvkAFY/7atRQyb1mhVQw4UsSGcZ6ffbwv9hw k6eJtdvR1PrbFi7Tej33LAvDve+31qHEyv/KBn6iWntkrqNRt0RHtQ7V39LofpQRv7RF MVrwTzqZsUGd/QXmhEjE0xmTo5Pr7LKAGRMhnDyRQ+A0nvvYGDC5YnJl2fcSkL+zNLYL FQqw== X-Forwarded-Encrypted: i=1; AFNElJ8jzpjnItR79l1aeq16NUrn9W4P9lxsk+fPx4QAhnSiOVtcrMicc9s3DX8jPMa+Pt05SqwCMq0z4QCn0x4=@vger.kernel.org X-Gm-Message-State: AOJu0YwObHYommKVx05/euoa4cgjbs/yphd1BdeX/32l6vOf1NPm64k2 WJDRMaIKrsJdHBYYeEVF54KvCP07P9CGEpdOloxiz4A7/gbz9xs6ubjf X-Gm-Gg: Acq92OFNf0EsQG2a6QhZKGVr7Q2LBXhkCOffWefuE79DAhfJq+g65HoWQOn0wJohVz6 VYUk0bJVC8V3BYphqILCVOQkP4D+NyHSRctx8qH7tn0umxGsSEj9uPNuJuEwzpkndDCvQuZNyii Vs1zzjsh0pM5bhr/kgBUoKcoflIENnY2bFk7q6NsQrej6C263uPG/CLV31oLPpHjZY0AgIPVSZ6 W8L+59/Oy0LHear2AuQEQC3mxq1MgApVqCij4pIxSVKO8GmX38bLWU61c6egB7fAQyL+6RQ0rGk mlFWAUlQtcK7QMUxwjU19LmP5If/dpMhvFJE8JgCr3cfQZSM/hp1ZmYc7a1uRXkGK6vCnX9ujp0 y70TlKL7m6u9+bDqxFiYfFy825BGvvJC0J62vQ6M/Ao/AQ5xBJNeolfPzKlm69A8MpiM9JQ6K34 Wyx8j/y46H2ARLW3//HAUfnqaZrEIx5SllhrK3bDX1GMdP/EiSRexN X-Received: by 2002:a05:600c:491e:b0:48e:978f:c45a with SMTP id 5b1f17b1804b1-48fed455991mr235621165e9.19.1779292568861; Wed, 20 May 2026 08:56:08 -0700 (PDT) Received: from RTRKW671-LIN.domain.local ([89.216.37.146]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48feab290e1sm215565025e9.2.2026.05.20.08.56.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 08:56:08 -0700 (PDT) From: Milan Tripkovic To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Kees Cook Cc: Alexandre Ghiti , Andy Shevchenko , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Dusan Stojkovic , Milan Tripkovic Subject: [PATCH 1/2] riscv: lib: add memchr() implementation Date: Wed, 20 May 2026 17:55:30 +0200 Message-ID: <20260520155532.1139471-2-milant2002@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260520155532.1139471-1-milant2002@gmail.com> References: <20260520155532.1139471-1-milant2002@gmail.com> 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" From: Milan Tripkovic This implementation leverages word-at-a-time comparisons and the RISC-V Zbb extension to accelerate character searching, falling back to a generic byte-by-byte loop on systems without Zbb support. Benchmark results (QEMU TCG, rv64): | len | zbb | Wozbb | default | % Wozbb | % zbb | |------|--------|-------|---------|---------|---------| | 1 | 22.9 | 26 | 22.1 | 17.64 % | 3.62 %| | 7 | 115.5 | 130.6 | 100.1 | 30.46 % | 15.38 %| | 8 | 184.3 | 143.3 | 117.2 | 22.26 % | 57.25 %| | 16 | 322 | 205.1 | 179.7 | 14.13 % | 79.19 %| | 31 | 361 | 247.4 | 221.7 | 11.59 % | 62.83 %| | 64 | 880 | 280.6 | 233.2 | 20.32 % | 277.35 %| | 127 | 961.3 | 307.9 | 271.4 | 13.44 % | 254.20 %| | 512 | 1812.9 | 325.6 | 294.1 | 10.71 % | 516.42 %| | 1024 | 1973.2 | 335.4 | 273.8 | 22.49 % | 620.67 %| | 3173 | 2245.9 | 338.7 | 288.6 | 17.35 % | 678.20 %| | 4096 | 2327.5 | 345 | 317.5 | 8.661 % | 633.07 %| Signed-off-by: Milan Tripkovic --- arch/riscv/include/asm/string.h | 3 +- arch/riscv/lib/Makefile | 1 + arch/riscv/lib/memchr.S | 126 ++++++++++++++++++++++++++++++++ arch/riscv/purgatory/Makefile | 5 +- 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/lib/memchr.S diff --git a/arch/riscv/include/asm/string.h b/arch/riscv/include/asm/strin= g.h index 764ffe8f6479..cfcf1193b446 100644 --- a/arch/riscv/include/asm/string.h +++ b/arch/riscv/include/asm/string.h @@ -18,7 +18,8 @@ extern asmlinkage void *__memcpy(void *, const void *, si= ze_t); #define __HAVE_ARCH_MEMMOVE extern asmlinkage void *memmove(void *, const void *, size_t); extern asmlinkage void *__memmove(void *, const void *, size_t); - +#define __HAVE_ARCH_MEMCHR +extern asmlinkage void *memchr(const void *s, int c, size_t n); #if !(defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) #define __HAVE_ARCH_STRCMP extern asmlinkage int strcmp(const char *cs, const char *ct); diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile index 6f767b2a349d..bbc54dabbb81 100644 --- a/arch/riscv/lib/Makefile +++ b/arch/riscv/lib/Makefile @@ -3,6 +3,7 @@ lib-y +=3D delay.o lib-y +=3D memcpy.o lib-y +=3D memset.o lib-y +=3D memmove.o +lib-y +=3D memchr.o ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) lib-y +=3D strcmp.o lib-y +=3D strlen.o diff --git a/arch/riscv/lib/memchr.S b/arch/riscv/lib/memchr.S new file mode 100644 index 000000000000..0e971cf51410 --- /dev/null +++ b/arch/riscv/lib/memchr.S @@ -0,0 +1,126 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include +#include +#include + +/* void *memchr(const void *s, int c, size_t n) */ +SYM_FUNC_START(memchr) + + __ALTERNATIVE_CFG("nop", "j memchr_zbb", 0, RISCV_ISA_EXT_ZBB, + IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && IS_ENABLED(CONFIG_TOOLCHAIN_HAS_ZBB)) +/* + * + * Parameters + * a0 - Pointer to memory area (s), also return value + * a1 - Character to search for (c) + * a2 - Number of bytes to search (n) + * + * Returns + * a0 - Pointer to the matched character, or NULL (0) if not found + * + * Clobbers + * t0, t1 + */ + beqz a2, 3f + andi a1, a1, 0xff + add t1, a0, a2 + +1: + lbu t0, 0(a0) + beq t0, a1, 4f + addi a0, a0, 1 + bne a0, t1, 1b + +3: + li a0, 0 +4: + ret + +#if defined(CONFIG_RISCV_ISA_ZBB) && defined(CONFIG_TOOLCHAIN_HAS_ZBB) +.balign 4 +memchr_zbb: +.option push +.option arch, +zbb +/* + * Parameters + * a0 - Pointer to memory area (s), also return value + * a1 - Character to search for (c) + * a2 - Number of bytes to search (n) + * + * Returns + * a0 - Pointer to the matched character, or NULL (0) if not found + * + * Clobbers + * t0, t1, t2, t3, t4, t5, t6 + */ + beqz a2, 3b + add t6, a0, a2 + andi t3, a1, 0xff + + li t0, SZREG + bltu a2, t0, 8f + +#if __riscv_xlen =3D=3D 64 + li t0, 0x0101010101010101 +#else + li t0, 0x01010101 +#endif + mul a1, t3, t0 + + andi t1, a0, SZREG - 1 + beqz t1, 5f + +1: + lbu t2, 0(a0) + beq t2, t3, 4b + addi a0, a0, 1 + andi t1, a0, SZREG - 1 + bnez t1, 1b + +5: + sub t5, t6, a0 + andi t5, t5, -SZREG + add t5, a0, t5 + + li t4, -1 + + beq a0, t5, 8f + +6: + REG_L t0, 0(a0) + xor t1, t0, a1 + orc.b t1, t1 + bne t1, t4, 7f + addi a0, a0, SZREG + bltu a0, t5, 6b + +8: + beq a0, t6, 3b + +2: + lbu t0, 0(a0) + beq t0, t3, 4b + addi a0, a0, 1 + bltu a0, t6, 2b + + j 3b + +7: + not t1, t1 +#ifndef CONFIG_CPU_BIG_ENDIAN + ctz t1, t1 +#else + clz t1, t1 +#endif + srli t1, t1, 3 + add a0, a0, t1 + ret + +.option pop +#endif + +SYM_FUNC_END(memchr) +SYM_FUNC_ALIAS(__pi_memchr, memchr) +EXPORT_SYMBOL(memchr) diff --git a/arch/riscv/purgatory/Makefile b/arch/riscv/purgatory/Makefile index b0358a78f11a..42e840fad6df 100644 --- a/arch/riscv/purgatory/Makefile +++ b/arch/riscv/purgatory/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 =20 -purgatory-y :=3D purgatory.o sha256.o entry.o string.o ctype.o memcpy.o me= mset.o +purgatory-y :=3D purgatory.o sha256.o entry.o string.o ctype.o memcpy.o me= mset.o memchr.o ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) purgatory-y +=3D strcmp.o strlen.o strncmp.o strnlen.o strchr.o strrchr.o endif @@ -17,6 +17,9 @@ $(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE $(obj)/memcpy.o: $(srctree)/arch/riscv/lib/memcpy.S FORCE $(call if_changed_rule,as_o_S) =20 +$(obj)/memchr.o: $(srctree)/arch/riscv/lib/memchr.S FORCE + $(call if_changed_rule,as_o_S) + $(obj)/memset.o: $(srctree)/arch/riscv/lib/memset.S FORCE $(call if_changed_rule,as_o_S) =20 --=20 2.43.0 From nobody Sun May 24 22:35:58 2026 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECE503ED3CF for ; Wed, 20 May 2026 15:56:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779292574; cv=none; b=SjFr+MOeLI/isRd19iWv6afZkXubZKz1wt0RQSOFPtE4ORi93YGu6UYgnQFoOiy/jId8PRTiwLHkOt0jSkGPToQ/8XZ+SB6DV3G2ooBcEYA/RcxybaaHLmiQXOkPmxk7DlNPeIyVxo7brUCmEu1vvxqZmJY3LKTarMXLpIc2834= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779292574; c=relaxed/simple; bh=52pIhwO1sx/8MqjYI640DB/wIvQ/icsM6v73JsrwAmg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tg4UApbOx/t36l9E1fL36QQ+IYn7wLiXZcnIHXGZ3vHY8JfCZh6MkaNXSviGoIKMFPHZRamYGQjNuJICCkhgm8NeVjS4zxYZPKW92U4YMBo9HSbdsvubAUUmbFmYLYJ8lQ9Tf0IGJ6liv51ewRgFQhfbWfLM658hHaVMB+ZPgYg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=n62vpcue; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="n62vpcue" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-488b150559bso39181515e9.1 for ; Wed, 20 May 2026 08:56:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779292570; x=1779897370; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1T2Qb9RrZsz7qcSCkhnlZDPbWojGSBsbannTJKJ4R7U=; b=n62vpcueUnZXvic5IH+FPs2AYL0o4gqVtOYJdpv81YI40bWQ/nKOy3pht2AWFV3sfH R9TUniAydlqEI03aEOF9RrBTJX4qqAQmaYy+iesbYmnYRX+fJaepPPPXepBtK8B5cyVE oPMfLMn5OLt9ZqcborixaJEkPof8Q3sh68VQYyQrr9YRYowh5cH+r3Tv1BEOSzk5Uf+t K4kZYXEYtV6TfodqsE19OBDxq3y4aQ+OYjn7wLAiZuRrkqn8GzQWfQuL4o9JEd3T6mhg M6dofZaaXxfwze6foLho/oYHOi3QLPNj+wog3tBvCdvH4CQu3dopvD7eB5ecwf3174gy 2MCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779292570; x=1779897370; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=1T2Qb9RrZsz7qcSCkhnlZDPbWojGSBsbannTJKJ4R7U=; b=gGX+VUb1O/lZ/n78G5UPwLgexInKYGuMu9i/qVgv8uu6cNJJCwb7MiTCsv1iSzX+jo sekPvokI+jMPHO+n9aHNB3DNnf+SC7YT8IAH6fgLAbSjeEX8OirrLs2PLlr/BUBzbbIN Qtjt6JJ2ms0PjICfy/jHgJTyH8lBQkJgxVFve/qDMFrpRaXf6os8FsRM8P0Io0GkSyPk A4W9KgiZkSOX/bUVcYvtMM8OcXkyPlkyYGQg0PQF5SGIVGeOphUqAI/bFVgOLrxMOJ0q giKn80hDipzxLo5QqTs+FUNYteGBSr7QD429EvrxT9AwC1uu1f9tdpYEirvbEMB+z6P3 QYIQ== X-Forwarded-Encrypted: i=1; AFNElJ/D+ZoVLR7tI51lR5RoLzfCx5YLuo7rubuZLUbzAMx5L+EM8smtpcIbEKjEzpVDaXxCmiTY0VmWP/wzAHE=@vger.kernel.org X-Gm-Message-State: AOJu0YwI9kVGzw+GFlRMRjhCO/ozxxz/F+PH63+U8GXk/rlpDY+HPiCk /QPB1+Auf/Gnjlc64fLtPwqz72hAGSIdA7Mc4f8Zs+jZ8U/VC0BN15d0 X-Gm-Gg: Acq92OFF38xMafFIHt5IdpfL7DvBj7HbZI7Pk4/+SLj8j56KEFA5C62NwzWZRHnKS9N vqH0YrfF82hUkp7NvwwXHovk3AMHPUnSrLOhZNHYt0uBusb7WH02zTP1OFzrvRAlDBup+Ai9MJ5 1shWBC78PbxApqWt4YXxMiTyTN0ptjY5Qsjjcv+UoUTlMARwzYq+j9L7e1NszF8s4X03ELykR1P LZh9C6or9J3ksFJh79Oui8Np1zvOQLwlL8CstZuWXdglrI9FMAHu2AZMbPqK4v9qeW9KbzkMZVo iiPxFYSR1uS+P7taqyTtsD9DN5vVuKG/nzYKpq0VEvSwq8brWumPhSLWjU7c1csTK58vdRg2/Ap X48fqTeoJB0LxAOKM7QZgYv+/eQEP5wpslxG6QWYcQnZcVh/1qQEEIW1cJrcIKRJ3r+R7JOGaPn bnaetxyMBdw2Eb2eow1i7rvgdLv+elIAgaplESxn/Oig== X-Received: by 2002:a05:600c:470e:b0:489:1c32:210d with SMTP id 5b1f17b1804b1-48fe60f195amr364284635e9.15.1779292570096; Wed, 20 May 2026 08:56:10 -0700 (PDT) Received: from RTRKW671-LIN.domain.local ([89.216.37.146]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48feab290e1sm215565025e9.2.2026.05.20.08.56.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 08:56:09 -0700 (PDT) From: Milan Tripkovic To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Kees Cook Cc: Alexandre Ghiti , Andy Shevchenko , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Dusan Stojkovic , Milan Tripkovic Subject: [PATCH 2/2] lib/string_kunit: extend benchmarks and unit test to memchr() Date: Wed, 20 May 2026 17:55:31 +0200 Message-ID: <20260520155532.1139471-3-milant2002@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260520155532.1139471-1-milant2002@gmail.com> References: <20260520155532.1139471-1-milant2002@gmail.com> 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" From: Milan Tripkovic Extend the string benchmarking suite to include memchr(). Extend the string unit test to include memchr() Signed-off-by: Milan Tripkovic --- lib/tests/string_kunit.c | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index 0819ace5b027..3a7d96e292da 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -881,6 +881,57 @@ static void string_bench_strrchr(struct kunit *test) STRING_BENCH_BUF(test, buf, len, strrchr, buf, '\0'); } =20 +static void string_test_memchr(struct kunit *test) +{ + char *buf; + size_t max_len =3D 128; + size_t off, len, match_pos; + char *res; + char target =3D 0x88; + + buf =3D kunit_kzalloc(test, max_len + 64, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); + + memset(buf, 'A', max_len + 64); + + for (off =3D 0; off < 16; off++) { + for (len =3D 1; len < max_len; len++) { + match_pos =3D len / 2; + buf[off + match_pos] =3D target; + + res =3D memchr(buf + off, target, len); + KUNIT_EXPECT_PTR_EQ_MSG(test, res, buf + off + match_pos, + "Failed at:%zu len:%zu pos:%zu", off, len, + match_pos); + + buf[off + match_pos] =3D 'A'; + } + } + + memset(buf, 'A', max_len); + buf[10] =3D 'B'; + buf[20] =3D 'B'; + res =3D memchr(buf, 'B', max_len); + KUNIT_EXPECT_PTR_EQ(test, res, &buf[10]); + + buf[50] =3D (char)0xFF; + res =3D memchr(buf, 0xFF, max_len); + KUNIT_EXPECT_PTR_EQ(test, res, &buf[50]); + + buf[60] =3D '\0'; + res =3D memchr(buf, '\0', max_len); + KUNIT_EXPECT_PTR_EQ(test, res, &buf[60]); + + buf[max_len - 1] =3D 'Z'; + res =3D memchr(buf, 'Z', max_len); + KUNIT_EXPECT_PTR_EQ(test, res, &buf[max_len - 1]); +} + +static void string_bench_memchr(struct kunit *test) +{ + STRING_BENCH_BUF(test, buf, len, memchr, buf, '\0', len); +} + static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_test_memset16), KUNIT_CASE(string_test_memset32), @@ -910,6 +961,8 @@ static struct kunit_case string_test_cases[] =3D { KUNIT_CASE(string_bench_strnlen), KUNIT_CASE(string_bench_strchr), KUNIT_CASE(string_bench_strrchr), + KUNIT_CASE(string_test_memchr), + KUNIT_CASE(string_bench_memchr), {} }; =20 --=20 2.43.0