From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (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 785B54611CC for ; Wed, 21 Jan 2026 07:25:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768980302; cv=none; b=u0lcOUVmyVFHOIJMdDo4emLRl7XisNkaRpFq22wSEOqnH9NHramyQpjUf0m8qar5uFF2x7R9Zz/N+4I1uK5w7oSqR8raojZP4ai2et+w+0rysaMkduUKCz66TrRNerx4C4W2KC3aSGFI4htIdy+jkOID/vm7PUZVWRlLNnaPvis= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768980302; c=relaxed/simple; bh=XxESfXtP1UxhRcdzYWCY7uAdi9grgPxkwc9/eOzYRX4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Funk9gRdtuz0XgP4HXXWrWo2bOY/OxepDOVFIC6fmbx3W0gC4OaCT/NnUE6x0RIy79Jrdhzb2CC8DAUYJsBxa8cZbKmAzp5Ywu7DuIpaW3Qki2RdQFLQI37QD/PBdW8khj9KEfHTiROQ9Q1y0GIZIQQRteyXCsMOWtJiTXivjcw= 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=f4diKZR+; arc=none smtp.client-ip=74.125.82.171 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="f4diKZR+" Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-2ae2eb49b4bso12682637eec.0 for ; Tue, 20 Jan 2026 23:25:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768980299; x=1769585099; 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=xd7TVVWwSVOrQ+WPeeZgk4vcwRxKlZMNBlvPshOCml4=; b=f4diKZR+FC08WFXXIL1t75JhnzxFGBDnNiA7hA4kPDUCXMIv8B+IclSCWDQoH57lGV 7gWxsU4DH42NGAYLqF7hH6IyeTuYZ6XtYmzc+EZLdNF4u/+bb11NLCzA4KSwx3JpiAPm 0ifJOvUlncDyB6gLNRjXxLGCjDILEsSdzYSfFrJp5oD7AQq8Ua50vW7XBorQClRSr/zU MkdCBu06vXGsHcjoGDO0GyXfQah6/Se+S/f6G9hHWlzP8164YeKNlAeCNkbcthWBlpZf OJnPR4Wf1sG9oYPHLOuTtj/mpUQFvAWZH0Q62341vgYZ3Q+iJlbUowwC5GUJWX6CQ1m+ W+RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768980299; x=1769585099; 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=xd7TVVWwSVOrQ+WPeeZgk4vcwRxKlZMNBlvPshOCml4=; b=NcAn8UBMnIoXdJbz2G9isFiRe2t0V6EjmD25MB0zmwbcDKHHK6SP/FD26GMJEuCeQZ HaRnO0FSq2HlM0YUy4qGDU1XcP/vyw6BpoOQ78Oy7GlVSsetT2bdGLIkdkgsdPzsphwC /VhHJ6WRUwBeNU2Ms+TVvLzwKer+PvWBJKK/6ciwSCYrIyKc3zlQjvj8PsZ06z7+/Jri igDuq60EnuDmxXVtQUqds5GLSirs4/ZXhS7kzTmD1+5RISreq6jxp8nDrDJsKFezq2p1 Qx0uaeVQ9ya0ljJh5GmhI85vlXlv6hcJwMTostEHTIQADS+W5zs/+I4bqi4tcJkXfeN1 u8fA== X-Forwarded-Encrypted: i=1; AJvYcCViv643YMa61x2Zed8KD+fZOhhjTEDh1LkAQ19PiLfHbIGld/JFVPXwbaB6tYW9/e7kksixKIE2FpF4BMs=@vger.kernel.org X-Gm-Message-State: AOJu0YxiWbdT25SMItBjQwoTQ0XJA57QfPjyqszfrYuYd/ewQkbvi+bX q2qAFb2PvCfLC93z4INmf9XHcXC3hymWEzkeEZ8XwUbfRlFG2lEiJQrI X-Gm-Gg: AZuq6aJDZjIwOxEwtLzv/srViCWDM9m7gqxhB6ZR7eTwjI2Qxaf+iI2yGNptJXDkyp4 27aeekbU5eBG4tQSKQDotPcKDZ+jlCIbashRAw/VupUoxj67QELcNLVv0cqmR2c5voBLSQOlWOI uVIHGzFx0whJZD4sVT8wykcHfr6h8s8tktFHC0+kXtLc+TljZtywSBkYyyhRBf5huT0pNIixJ04 prGJ3GiS2UcKPM1zKHnqYnrnHw2RPqXI0A7LuxOMxBDk0XloJgayYocqapgQ70LUd5/LOCmTjXK o0TeX87i7yG+WBMuy3ZcQTe2FWlkDwrvRuSIi8LS8ZEoCW1BCS7nF+dtwgA+ueko/Y0FkZ0lBz1 ukNyOETCT3/KitMW96/FLijQeYFxt/b8pUVbQxHwVNytBSOUX0fVQi4HIN0q521I= X-Received: by 2002:a05:7301:658a:b0:2b6:affb:e02d with SMTP id 5a478bee46e88-2b6fdc7cba6mr2904960eec.28.1768980298950; Tue, 20 Jan 2026 23:24:58 -0800 (PST) Received: from gmail.com ([2a09:bac1:19e0:20::4:33f]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2b70d4324f5sm2724899eec.8.2026.01.20.23.24.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jan 2026 23:24:58 -0800 (PST) From: Qingfang Deng To: Feng Jiang Cc: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Joel Stanley Subject: Re: [PATCH v3 6/8] riscv: lib: add strnlen implementation Date: Wed, 21 Jan 2026 15:24:38 +0800 Message-ID: <20260121072438.6646-1-dqfext@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260120065852.166857-7-jiangfeng@kylinos.cn> References: <20260120065852.166857-1-jiangfeng@kylinos.cn> <20260120065852.166857-7-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: 8bit On Tue, 20 Jan 2026 14:58:50 +0800, Feng Jiang wrote: > diff --git a/arch/riscv/lib/strnlen.S b/arch/riscv/lib/strnlen.S Branches that test maxlen can be replaced with Zbb minu instruction. (see below) > + /* > + * 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 minu a0, a0, a1 > + > + bgtu t3, a0, 2f > + > + /* Prepare for the word comparison loop. */ > + addi t2, t0, SZREG > + li t3, -1 > + > + /* > + * Our critical loop is 4 instructions and processes data in > + * 4 byte or 8 byte chunks. > + */ > + .p2align 3 > +1: > + REG_L t1, SZREG(t0) > + addi t0, t0, SZREG > + orc.b t1, t1 > + bgeu t0, t4, 4f > + beq t1, t3, 1b > +4: > + not t1, t1 > + CZ t1, t1 > + srli t1, t1, 3 > + > + /* Get number of processed bytes. */ > + sub t2, t0, t2 > + > + /* Add number of characters in the first word. */ > + add a0, a0, t2 > + > + /* Add number of characters in the last word. */ > + add a0, a0, t1 > + > + /* Ensure the final result does not exceed maxlen. */ > + bgeu a0, a1, 3f minu a0, a0, a1 > +2: > + ret > +3: > + mv a0, a1 > + ret > + > +.option pop -- Qingfang