From patchwork Tue Feb 28 18:42:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 13155123 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 975B4C64EC7 for ; Tue, 28 Feb 2023 18:42:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=9IaqMkTvyj3/KAYMJIGT1NR+lTkciNuMOl6BGfG4asg=; b=z/SKuzVEpJngR6 IPOK+vr5131fXZapl/LdH3S/wQtgrMBxBibUxntidiuKjCy57gftl4wB6YxbbeyytDYIluewODE8h 154vall0EYfWAPtoijtpAp0QdrPY13AmYEpCBnFrXECWEDif1YtqvYXI8ZsP1lO/j7+ahK8HZSFoR ZZOt1qUI3u107xK6WQkC1fWYQfHHkZMpYjFzMMZ3c8JWllxNebVXeS9kp7eBrA3XF1BdqTohkCMQl c3OlJzgJnc07ZxB6ZActAcfyH8i3f2cmJ41NA3G9kXew75X9k4osa8h0uvaHRHoiov66MHkWS8VRy u8ppZk/2au8FsCtU8dbg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pX4vh-00E0Vf-5n; Tue, 28 Feb 2023 18:42:25 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pX4vd-00E0VF-St for linux-riscv@lists.infradead.org; Tue, 28 Feb 2023 18:42:23 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 8454FB80EAA; Tue, 28 Feb 2023 18:42:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62B14C433D2; Tue, 28 Feb 2023 18:42:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677609738; bh=3r56zFR8DAm2cyXEU98XUbrPHeEkAipKBoGQrgpbMxQ=; h=From:To:Cc:Subject:Date:From; b=Gii9JFyfLeXbd4wd+isitRYXtMlT55PkIo7BUE+zVXSWvbzDsk1Xkk1S3nnqJj1zX 07KGLvJE4rZejNd1G+STfTAqH4edT1R8Z8IjY4SlEAYeuuUGEjWx/bjpXht/Yp+Bfw 2NU74DMmMSezfLMtmJOKy0fjjF+PtWSF35/PWB9pXd+Dv9HM93+PC1PTqjd2F/zXz9 91kaIyRY6Gze2Kyma4T9B6kurLtaf6tvqDO4I14+xT/D3j0+KgzlhnMmyX0ERF8qWS J5mfZsTdw0YGNHGN4yopm2VP8aNZvvxKRd1IPp/hGQ+GYGJ/G6hHlHzTOBEH+W4UyP alkHAUwcR4S0A== From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, Christoph Muellner , Heiko Stuebner Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Conor Dooley , Guenter Roeck , Andrew Jones Subject: [PATCH] riscv, lib: Fix Zbb strncmp Date: Tue, 28 Feb 2023 19:42:10 +0100 Message-Id: <20230228184211.1585641-1-bjorn@kernel.org> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230228_104222_125569_8CE5EDD0 X-CRM114-Status: GOOD ( 12.22 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Björn Töpel The Zbb optimized strncmp has two parts; a fast path that does XLEN/8B per iteration, and a slow that does one byte per iteration. The idea is to compare aligned XLEN chunks for most of strings, and do the remainder tail in the slow path. The Zbb strncmp has two issues in the fast path: Incorrect remainder handling (wrong compare): Assume that the string length is 9. On 64b systems, the fast path should do one iteration, and one iteration in the slow path. Instead, both were done in the fast path, which lead to incorrect results. An example: strncmp("/dev/vda", "/dev/", 5); Correct by changing "bgt" to "bge". Missing NULL checks in the second string: This could lead to incorrect results for: strncmp("/dev/vda", "/dev/vda\0", 8); Correct by adding an additional check. Fixes: b6fcdb191e36 ("RISC-V: add zbb support to string functions") Suggested-by: Heiko Stuebner Signed-off-by: Björn Töpel Tested-by: Conor Dooley Reported-by: Guenter Roeck Tested-by: Guenter Roeck --- arch/riscv/lib/strncmp.S | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) base-commit: eb9be8310c58c166f9fae3b71c0ad9d6741b4897 diff --git a/arch/riscv/lib/strncmp.S b/arch/riscv/lib/strncmp.S index ee49595075be..efd3f3150a54 100644 --- a/arch/riscv/lib/strncmp.S +++ b/arch/riscv/lib/strncmp.S @@ -78,11 +78,13 @@ strncmp_zbb: /* Main loop for aligned string. */ .p2align 3 1: - bgt a0, t6, 3f + bge a0, t6, 3f REG_L t0, 0(a0) REG_L t1, 0(a1) orc.b t3, t0 bne t3, t5, 2f + orc.b t3, t1 + bne t3, t5, 2f addi a0, a0, SZREG addi a1, a1, SZREG beq t0, t1, 1b