From patchwork Wed Nov 15 07:15:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiang W X-Patchwork-Id: 1864023 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=c0qXfB7H; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=ADwhVHGi; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SVZH13hJJz1yRG for ; Wed, 15 Nov 2023 18:17:00 +1100 (AEDT) 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=KrInwDo5bp/WWY1BOw6sApxyMKAti1On9Ea4SQWQDe4=; b=c0qXfB7H2xIym8 Gl8ijb+WsQgcX41+NL8bPM0LVvVnas82SLNlBGKW8WPH++LfIydbsEcYz8xwLG4Hr+uFc042ioKh0 PmyXLQJm15TIG+4DwLm3N0Hu4FcHESQMM2L1SWQpnaJyauo3/2IMj4Z9T6klbfC8J52F1x1bkZqv2 ITbNAxnixKSsgYNoY0lFJJNrHPrfXXsMwCdvJjC3SJoJlObEcEtuzg0rc85wJKvFgYqY71ikQOyAw SU8Tl2EIqrZw6GFDB2RBNCIRrUZH54EYR+axOZEzJUMivGiiU2QLtBP+p0bZFs9azvTnqUWyFy9xu WcI5V/0nP7pJNqQyE2ag==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3A8j-0007LQ-2v; Wed, 15 Nov 2023 07:16:45 +0000 Received: from m15.mail.126.com ([45.254.50.223]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3A8g-0007GQ-0s for opensbi@lists.infradead.org; Wed, 15 Nov 2023 07:16:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version; bh=jmDv+ Cko0DASbK2Gt3BE4cSm2JehZ1p+BqfuzdUXoaE=; b=ADwhVHGi+YN6E7N1B6PVf V57qDZmUrnGGoU2J8tYAV/qXcVIsU39KlNMXtBXlVbOU2AnyKwCgIS4r8dJLQzbx 7M9aHlokPF5VxYHjg11ebPHjU0W4zKVKrOFhslpKUehGld096xjczImrGCMSMm6R Kl09WgaFqNMcgSJ3Lrotuo= Received: from T490.lan (unknown [112.83.183.252]) by zwqz-smtp-mta-g0-1 (Coremail) with SMTP id _____wCnLz1IcFRlSxFrCg--.14567S2; Wed, 15 Nov 2023 15:16:26 +0800 (CST) From: Xiang W To: opensbi@lists.infradead.org Cc: Xiang W Subject: [PATCH] lib: sbi: Improved atomic bit operations Date: Wed, 15 Nov 2023 15:15:25 +0800 Message-ID: <20231115071622.465416-1-wxjstz@126.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-CM-TRANSID: _____wCnLz1IcFRlSxFrCg--.14567S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxAFWfuFW5GF4rXF15WF1fCrg_yoW5tF13pr 4Sqwnrtw4jqa47G3srC3y5X3W5Aw4qv345JrZrWr9093ZI9a1fGF4rKw15Aay5C348Zr1f ArW7JFZ5u34UJa7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pirb18UUUUU= X-Originating-IP: [112.83.183.252] X-CM-SenderInfo: pz0m23b26rjloofrz/xtbBcw4pOmSQAZC1EwAAst X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231114_231642_752693_086C3B95 X-CRM114-Status: UNSURE ( 8.65 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: The previous atomic operation returns the new value. After setting and clearing, the new value is determined. The value before modification should be returned. The previous code actually returns the w [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [45.254.50.223 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [wxjstz[at]126.com] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [45.254.50.223 listed in wl.mailspike.net] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The previous atomic operation returns the new value. After setting and clearing, the new value is determined. The value before modification should be returned. The previous code actually returns the word before modification, not the bit value. This patch improves these issues. Signed-off-by: Xiang W --- include/sbi/riscv_atomic.h | 8 ++++---- lib/sbi/riscv_atomic.c | 36 ++++++++---------------------------- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/include/sbi/riscv_atomic.h b/include/sbi/riscv_atomic.h index 3972e0b..31dc673 100644 --- a/include/sbi/riscv_atomic.h +++ b/include/sbi/riscv_atomic.h @@ -39,14 +39,14 @@ unsigned int atomic_raw_xchg_uint(volatile unsigned int *ptr, unsigned long atomic_raw_xchg_ulong(volatile unsigned long *ptr, unsigned long newval); /** - * Set a bit in an atomic variable and return the new value. + * Set a bit in an atomic variable and return the value of bit before modify. * @nr : Bit to set. * @atom: atomic variable to modify */ int atomic_set_bit(int nr, atomic_t *atom); /** - * Clear a bit in an atomic variable and return the new value. + * Clear a bit in an atomic variable and return the value of bit before modify. * @nr : Bit to set. * @atom: atomic variable to modify */ @@ -54,14 +54,14 @@ int atomic_set_bit(int nr, atomic_t *atom); int atomic_clear_bit(int nr, atomic_t *atom); /** - * Set a bit in any address and return the new value . + * Set a bit in any address and return the the value of bit before modify. * @nr : Bit to set. * @addr: Address to modify */ int atomic_raw_set_bit(int nr, volatile unsigned long *addr); /** - * Clear a bit in any address and return the new value . + * Clear a bit in any address and return the the value of bit before modify. * @nr : Bit to set. * @addr: Address to modify */ diff --git a/lib/sbi/riscv_atomic.c b/lib/sbi/riscv_atomic.c index 528686f..95c6ff7 100644 --- a/lib/sbi/riscv_atomic.c +++ b/lib/sbi/riscv_atomic.c @@ -206,40 +206,20 @@ unsigned long atomic_raw_xchg_ulong(volatile unsigned long *ptr, #endif } -#if (__SIZEOF_POINTER__ == 8) -#define __AMO(op) "amo" #op ".d" -#elif (__SIZEOF_POINTER__ == 4) -#define __AMO(op) "amo" #op ".w" -#else -#error "Unexpected __SIZEOF_POINTER__" -#endif - -#define __atomic_op_bit_ord(op, mod, nr, addr, ord) \ - ({ \ - unsigned long __res, __mask; \ - __mask = BIT_MASK(nr); \ - __asm__ __volatile__(__AMO(op) #ord " %0, %2, %1" \ - : "=r"(__res), "+A"(addr[BIT_WORD(nr)]) \ - : "r"(mod(__mask)) \ - : "memory"); \ - __res; \ - }) - -#define __atomic_op_bit(op, mod, nr, addr) \ - __atomic_op_bit_ord(op, mod, nr, addr, .aqrl) - -/* Bitmask modifiers */ -#define __NOP(x) (x) -#define __NOT(x) (~(x)) - inline int atomic_raw_set_bit(int nr, volatile unsigned long *addr) { - return __atomic_op_bit(or, __NOP, nr, addr); + unsigned long res, mask = BIT_MASK(nr); + volatile unsigned long *ptr = &addr[BIT_WORD(nr)]; + res = __atomic_fetch_and(ptr, ~mask, __ATOMIC_ACQ_REL); + return res & mask ? 1 : 0; } inline int atomic_raw_clear_bit(int nr, volatile unsigned long *addr) { - return __atomic_op_bit(and, __NOT, nr, addr); + unsigned long res, mask = BIT_MASK(nr); + volatile unsigned long *ptr = &addr[BIT_WORD(nr)]; + res = __atomic_fetch_or(ptr, mask, __ATOMIC_ACQ_REL); + return res & mask ? 1 : 0; } inline int atomic_set_bit(int nr, atomic_t *atom)