From patchwork Sat Jan 17 01:14:41 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1579 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768612585; bh=d9JcXvaHzyS+nXYP77Os/J3g8zeJ9TaPUjpHMNRR68k=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=lQE25fKVWQXF/jvnmjk0EpzrXfDUh5Z/YwPy1EdUPmOSiib/y/APL1cB+zF9Hr3kB nJ0NjEdI5tozU0EtLUFcNZmezr4AvCOD2DIoV9NiHod+WxSo3xsLMIZ2pJy9i6qW+l hzkL9JJTwHmtccedzjP4iO/2DMbtgjfF5f/aRMu4Y1O86q4OndHin2/Q73LeJgaQGl 3NMPN/VkODTT5IzRAFrLi8kKnhcJ5STxLTKXqlfouQXSxDimV3P7eA2nzljr/j/144 7CtSN6TeekEtE0+aBdYdWU1ZthQ2sJiksIXtP8xLLCx/VdBHJ6sQyKGNH+UHqlHjWb bHOPznerpjIzA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1C56469405 for ; Fri, 16 Jan 2026 18:16:25 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id 8azxDotJxX_z for ; Fri, 16 Jan 2026 18:16:25 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768612585; bh=d9JcXvaHzyS+nXYP77Os/J3g8zeJ9TaPUjpHMNRR68k=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=lQE25fKVWQXF/jvnmjk0EpzrXfDUh5Z/YwPy1EdUPmOSiib/y/APL1cB+zF9Hr3kB nJ0NjEdI5tozU0EtLUFcNZmezr4AvCOD2DIoV9NiHod+WxSo3xsLMIZ2pJy9i6qW+l hzkL9JJTwHmtccedzjP4iO/2DMbtgjfF5f/aRMu4Y1O86q4OndHin2/Q73LeJgaQGl 3NMPN/VkODTT5IzRAFrLi8kKnhcJ5STxLTKXqlfouQXSxDimV3P7eA2nzljr/j/144 7CtSN6TeekEtE0+aBdYdWU1ZthQ2sJiksIXtP8xLLCx/VdBHJ6sQyKGNH+UHqlHjWb bHOPznerpjIzA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 0C9D3693D6 for ; Fri, 16 Jan 2026 18:16:25 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768612582; bh=i28AqIzvNLJfMQKYjIZcm6X19zO8SRBuA2NzZHsbkFk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IQUIY4+74MENse+Z2UrQM1JVk8Lp2+/oWJu1UiStF4LXlcP3CrTnZjxEqKBsVDbIh Kjigxy753S5f58d1WSqCVsYcave1/wquN18olBYXe5EA0UO2ZS6/9OLhmohWQrd8rR MZmbrbWCC0aUFTUF+ck47NAyIfQ0OYQsPrhu3fE/83sPfnCK9OgdO0T1kQz16sxgIT OY509LtGsL8OkYqqz2RvYOtUFVdXgJM+xDa3YsxfZLCCZNuuf1iFpE9calhe3NzTJ7 oqIDy4yIHJBUDsu/1HsPVmcAk/Jl4b94kGD/RsxLCbUcLbr+3yQCVwjGq3LqcHx+gg b6Q4yVzYNOw+Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id C8C9D6940B; Fri, 16 Jan 2026 18:16:22 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id TCSx4MbccWwo; Fri, 16 Jan 2026 18:16:22 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768612577; bh=kaGX52uB4/O2wEbSjzoCmp2B0iFKLl/TumBAkaduOEE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YC/n75sJ9BA8CEAvac8JrbreyH1Za+FeTBHzzPSaNUK7dbBmFU/dXlPDXacaEWM5T e7knm9mYrk7vs4siphqo053clM35+trtxUKMIvu8Rk2U+nyXSdQgMVTxDDFj38ZdCQ a/ChF/U1vE82kbCZJbPJMQk3d5pCndfXIDnStSQuPI2c5mGxigSBJNr6SiD2B9mexA I1KyscumETG+JnUT9m01nPBvS5U433DNav9p6TEavQfyxtv7MDmHc2iN51bsn3I9Rf p2eMw6RIJxQCLPi44Y8eshY34oACqqjK5tMCzYWOWUvfcj1I4MGBS24ovOZmHn65vO pwCn9Laoqj6zw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id D643369405; Fri, 16 Jan 2026 18:16:16 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Fri, 16 Jan 2026 18:14:41 -0700 Message-ID: <20260117011448.3007171-20-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260117011448.3007171-1-sjg@u-boot.org> References: <20260117011448.3007171-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: 4SIMIUA3VTBK7BS7S3NQVGR43WVVTBKU X-Message-ID-Hash: 4SIMIUA3VTBK7BS7S3NQVGR43WVVTBKU X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass , "Claude Opus 4 . 5" X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 19/19] asm-generic: atomic: Add xchg() and try_cmpxchg() macros List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Move xchg and try_cmpxchg macros to asm-generic/atomic.h alongside cmpxchg, as they are all atomic exchange operations. Add: - try_cmpxchg() - compare and exchange with boolean return and old value update on failure - xchg() - atomic exchange, stores new value and returns old These are single-threaded implementations for U-Boot. Add #ifndef guards to all three macros (cmpxchg, try_cmpxchg, xchg) to avoid redefinition warnings on architectures like MIPS that provide their own implementations. Signed-off-by: Simon Glass Co-developed-by: Claude Opus 4.5 --- fs/ext4l/ext4_uboot.h | 12 ++---------- include/asm-generic/atomic.h | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/fs/ext4l/ext4_uboot.h b/fs/ext4l/ext4_uboot.h index a4d863b86e1..a4baac6fc86 100644 --- a/fs/ext4l/ext4_uboot.h +++ b/fs/ext4l/ext4_uboot.h @@ -949,14 +949,7 @@ typedef unsigned int projid_t; * Additional stubs for inode.c */ -/* try_cmpxchg - compare and exchange with return value */ -#define try_cmpxchg(ptr, old, new) ({ \ - typeof(*(old)) __old = *(old); \ - typeof(*(ptr)) __ret = cmpxchg(ptr, __old, (new)); \ - if (__ret != __old) \ - *(old) = __ret; \ - __ret == __old; \ -}) +/* try_cmpxchg is now in asm-generic/atomic.h */ /* hash_64 - simple 64-bit hash */ #define hash_64(val, bits) ((unsigned long)((val) >> (64 - (bits)))) @@ -1743,8 +1736,7 @@ static inline unsigned long ext4_find_next_bit_le(const void *addr, /* refcount operations are now in linux/refcount.h */ -/* xchg - exchange value atomically */ -#define xchg(ptr, new) ({ typeof(*(ptr)) __old = *(ptr); *(ptr) = (new); __old; }) +/* xchg is now in asm-generic/atomic.h */ /* printk_ratelimited is in linux/printk.h */ diff --git a/include/asm-generic/atomic.h b/include/asm-generic/atomic.h index 3fa749bf012..3f72ae39ee1 100644 --- a/include/asm-generic/atomic.h +++ b/include/asm-generic/atomic.h @@ -209,6 +209,7 @@ static inline void atomic64_dec(volatile atomic64_t *v) * Single-threaded version for U-Boot. Atomically compares *ptr with old * and if equal, stores new. Returns the original value of *ptr. */ +#ifndef cmpxchg #define cmpxchg(ptr, old, new) ({ \ typeof(*(ptr)) __cmpxchg_old = (old); \ typeof(*(ptr)) __cmpxchg_new = (new); \ @@ -217,5 +218,41 @@ static inline void atomic64_dec(volatile atomic64_t *v) *(ptr) = __cmpxchg_new; \ __cmpxchg_ret; \ }) +#endif + +/** + * try_cmpxchg - compare and exchange with boolean return + * @ptr: pointer to the value + * @oldp: pointer to expected old value (updated on failure) + * @new: new value to store if current equals old + * + * Returns true if exchange succeeded, false otherwise. + * On failure, *oldp is updated with the current value. + */ +#ifndef try_cmpxchg +#define try_cmpxchg(ptr, oldp, new) ({ \ + typeof(*(oldp)) __old = *(oldp); \ + typeof(*(ptr)) __ret = cmpxchg(ptr, __old, (new)); \ + if (__ret != __old) \ + *(oldp) = __ret; \ + __ret == __old; \ +}) +#endif + +/** + * xchg - exchange value atomically + * @ptr: pointer to the value + * @new: new value to store + * + * Atomically stores new value and returns the old value. + * Single-threaded version for U-Boot. + */ +#ifndef xchg +#define xchg(ptr, new) ({ \ + typeof(*(ptr)) __xchg_old = *(ptr); \ + *(ptr) = (new); \ + __xchg_old; \ +}) +#endif #endif