From patchwork Sat Jan 17 01:14:34 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1572 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=1768612555; bh=6gNsDx8kA1Nfnkjdgsa87rEvW+LSqHXAe9wApfmX08c=; 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=t/YNy+PY8QVWbmyfXyyVHda/c41lBDnn+vAqcqoGiJZHTNL4+W0w2twxPSIymHE78 sxEbbfQfJZOP4CUnMKTlofQe1emTssqg2C4tnM00rhKugQ0POgyrK9AO2j2LRfoyGV C6LE7C0FNHsh7aPiKcFHEwgsCOnb2yhGo5sNjnu+1MgNc5GtQwHUh7EuD+CNZBNrj2 OxVyjvHER7gp7CylJ4p7Kj1o3kGQmQ259mpvtnliw3ibKnkHRC6KOiwXNU4krEqqUB J1gCK4uFVsnWv+9bpET6gDUNyvWHny+vLQb1QkjOJOKveygE/l9qpEnri5gjgZzRzY M/vCZImJ83Y6A== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 0966069401 for ; Fri, 16 Jan 2026 18:15:55 -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 y4cgKqBV2J2t for ; Fri, 16 Jan 2026 18:15:54 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768612554; bh=6gNsDx8kA1Nfnkjdgsa87rEvW+LSqHXAe9wApfmX08c=; 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=H/u4F51l5joBgxJrIzuavUxp0JeSTYbI+EXZ9zTAn50U3ndjxyqxF1iz8lFjd99Ie WQ5j7w/BBt9RVygtZ/elQHJl6xZ2W1fADegrwohRiQBUvGWCqEV6nyiLVpY9sop9rh /thscCIOGcjm25rt9NIODVpyuZwhBlJAXtxzAfYh7KEPqJRx7DauDxvO/se3RJib0E bNi9s5gD7WIH1HQsR14ThwTnQx9OBBnFlazKGaTqbVZ2GnKl2QHoA4FICup1FNiVvO O/SQb3PpfPXiEYM3QaC0BwJF03Qy3lpSRClx4w/Fc17oIzxhBtAcxYz4KykldPoAG/ CtYho3M+us/BA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id ED489693E0 for ; Fri, 16 Jan 2026 18:15:54 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768612552; bh=zELaHMULVS2ZF7FQfnn3uK6RsRySW8K/NYExwHsK/24=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fm5AwY60TQ/qKBlQWs0FB51C8v6sYG4DpJ6rqiW7aU748y1XfGLTHW42wW6b9q2Ay wsc3LNSKzEKbmeEQ+CQ0xrQzcaAokIH8uv/5xl/q9hglEVnFyHWhovvQ0lC14jRTi7 p05DzLVilnIxCCXmxZJJoeD9NaZwHEoFJj2mcEe2UW6p7sQ9we79cPHmx2hGYtDSG6 qm9j6DBi+GRHadooCYlzQs5JVhuBY834yV6RDCXq8qSkewm0rPxWIzeTwQl0JhFo8y TEcLamyFLj6ZLQyUoj64Wf36igFFPVdK9hzJ6wWQxGZxS4TJ0QlABTEdFXbvYWGPI0 wrgtG8aN3riuQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id CB9776913F; Fri, 16 Jan 2026 18:15:52 -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 XiB_7GL20upe; Fri, 16 Jan 2026 18:15:52 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1768612548; bh=AxdfBya6XjwWCGvci71ztcb+n7NHGoC74H7N3chPoNA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wJOkKegVq9GFmo71Sy/aFV7+JOeokJmUxpN/3d0pubXM1Qsia+lTzYNvGfQTQS3ad ZKm1SZUp/ogqEjo6t0mrkR3SG3bIi0g00MUxvm9/AVUr/1i95ZXh0RCf2jyeZIA2bm 6gVIB1HHu5E7R4HYMZNejOOxQ1BQjme3z/p26Az5F3aZdHEJfZ/YVedUYirJV2zMok 9eb85T147+dTp+um/Rg/G9FhXeNnDrj5dQlbeX42YlBfxjKjpkjY8AtkjoHhw1ZEG/ cd1D11Ac0YFFtuD693k4Fa+GCngKLXc6SiyGWvMsySHS/qHqHZRoVD5ut7hR5eEuRf wHkehtG3LD66Q== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 78706693E0; Fri, 16 Jan 2026 18:15:48 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Fri, 16 Jan 2026 18:14:34 -0700 Message-ID: <20260117011448.3007171-13-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: BTKUD7NZC46VIPP4X4NBBR4CX73PSVM4 X-Message-ID-Hash: BTKUD7NZC46VIPP4X4NBBR4CX73PSVM4 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 12/19] linux: Add refcount.h header with refcount_t type 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 reference count type and operations to a dedicated header file that mirrors the Linux kernel organisation. refcount.h provides: - refcount_t type (wrapper around atomic_t) - REFCOUNT_INIT() macro for static initialisation - refcount_set(), refcount_read() - set/get value - refcount_inc(), refcount_dec() - increment/decrement - refcount_dec_and_test() - decrement and test if zero - refcount_inc_not_zero() - increment unless zero These are simplified versions for single-threaded U-Boot, mapping directly to atomic_t operations. Co-developed-by: Claude Opus 4.5 Signed-off-by: Simon Glass --- fs/ext4l/ext4_uboot.h | 10 ++---- include/linux/refcount.h | 70 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 include/linux/refcount.h diff --git a/fs/ext4l/ext4_uboot.h b/fs/ext4l/ext4_uboot.h index 57d967965da..4fe7d40c37f 100644 --- a/fs/ext4l/ext4_uboot.h +++ b/fs/ext4l/ext4_uboot.h @@ -89,13 +89,12 @@ #include #include #include +#include /* atomic_dec_if_positive, atomic_add_unless, etc. are now in asm-generic/atomic.h */ /* cmpxchg is now in asm-generic/atomic.h */ /* SMP stubs (raw_smp_processor_id, smp_*mb) are now in linux/smp.h */ - -/* Reference count type */ -typedef struct { atomic_t refs; } refcount_t; +/* refcount_t and operations are now in linux/refcount.h */ /* rwlock_t and read_lock/read_unlock are now in linux/spinlock.h */ #include @@ -1790,10 +1789,7 @@ static inline unsigned long ext4_find_next_bit_le(const void *addr, */ #include -/* refcount operations - map to atomic */ -#define refcount_set(r, v) atomic_set((atomic_t *)(r), v) -#define refcount_dec_and_test(r) atomic_dec_and_test((atomic_t *)(r)) -#define refcount_inc(r) atomic_inc((atomic_t *)(r)) +/* refcount operations are now in linux/refcount.h */ /* xchg - exchange value atomically */ #define xchg(ptr, new) ({ typeof(*(ptr)) __old = *(ptr); *(ptr) = (new); __old; }) diff --git a/include/linux/refcount.h b/include/linux/refcount.h new file mode 100644 index 00000000000..ce001b437bf --- /dev/null +++ b/include/linux/refcount.h @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Reference count type stubs for U-Boot + * + * Based on Linux refcount.h - simplified for single-threaded U-Boot. + */ +#ifndef _LINUX_REFCOUNT_H +#define _LINUX_REFCOUNT_H + +#include + +/** + * typedef refcount_t - reference count type + * + * Variant of atomic_t for reference counting with saturation semantics. + * In U-Boot this is a simple wrapper around atomic_t. + */ +typedef struct { + atomic_t refs; +} refcount_t; + +#define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), } + +/** + * refcount_set() - set a refcount's value + * @r: the refcount + * @n: value to set + */ +#define refcount_set(r, n) atomic_set(&(r)->refs, (n)) + +/** + * refcount_read() - get a refcount's value + * @r: the refcount + * + * Return: the refcount's value + */ +#define refcount_read(r) atomic_read(&(r)->refs) + +/** + * refcount_inc() - increment a refcount + * @r: the refcount to increment + */ +#define refcount_inc(r) atomic_inc(&(r)->refs) + +/** + * refcount_dec() - decrement a refcount + * @r: the refcount to decrement + */ +#define refcount_dec(r) atomic_dec(&(r)->refs) + +/** + * refcount_dec_and_test() - decrement a refcount and test if it is 0 + * @r: the refcount + * + * Return: true if the resulting refcount is 0, false otherwise + */ +#define refcount_dec_and_test(r) atomic_dec_and_test(&(r)->refs) + +/** + * refcount_inc_not_zero() - increment a refcount unless it is 0 + * @r: the refcount to increment + * + * Return: true if the increment succeeded, false if refcount was 0 + */ +static inline bool refcount_inc_not_zero(refcount_t *r) +{ + return atomic_add_unless(&r->refs, 1, 0); +} + +#endif /* _LINUX_REFCOUNT_H */