* [PATCH v2 0/4] tools: testing: Use existing atomic.h for vma/maple tests
@ 2025-08-28 12:27 Brendan Jackman
2025-08-28 12:27 ` [PATCH v2 1/4] tools/include: Implement a couple of atomic_t ops Brendan Jackman
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Brendan Jackman @ 2025-08-28 12:27 UTC (permalink / raw)
To: Liam R. Howlett, Andrew Morton, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato
Cc: linux-kernel, maple-tree, linux-mm, Brendan Jackman
De-duplicating this lets us delete a bit of code.
Ulterior motive: I'm working on a new set of the userspace-based unit
tests, which will need the atomics API too. That would involve even more
duplication, so while the win in this patchset alone is very minimal, it
looks a lot more significant with my other WIP patchset.
I've tested these commands:
make -C tools/testing/vma -j
tools/testing/vma/vma
make -C tools/testing/radix-tree -j
tools/testing/radix-tree/maple
Note the EXTRA_CFLAGS patch is actually orthogonal, let me know if you'd
prefer I send it separately.
Signed-off-by: Brendan Jackman <jackmanb@google.com>
---
Changes in v2:
- Added some new operations to atomic.h (in support of the point below).
- Fixed garbage synchronization in mapping_map_writable(), thanks Pedro.
- Clarified commit messages:
- Mention where $(SRCARCH) comes from
- Don't talk about radix-tree for maple tree specifics stuff
- Link to v1: https://lore.kernel.org/r/20250827-b4-vma-no-atomic-h-v1-0-5d3a94ae670f@google.com
---
Brendan Jackman (4):
tools/include: Implement a couple of atomic_t ops
tools: testing: Allow importing arch headers in shared.mk
tools: testing: Support EXTRA_CFLAGS in shared.mk
tools: testing: Use existing atomic.h for vma/maple tests
tools/include/linux/atomic.h | 22 ++++++++++++++++++++++
tools/testing/shared/linux/maple_tree.h | 6 ++----
tools/testing/shared/shared.mk | 6 +++++-
tools/testing/vma/linux/atomic.h | 17 -----------------
tools/testing/vma/vma_internal.h | 12 +++---------
5 files changed, 32 insertions(+), 31 deletions(-)
---
base-commit: efa7612003b44c220551fd02466bfbad5180fc83
change-id: 20250827-b4-vma-no-atomic-h-0f8ebc2fe4f9
Best regards,
--
Brendan Jackman <jackmanb@google.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/4] tools/include: Implement a couple of atomic_t ops
2025-08-28 12:27 [PATCH v2 0/4] tools: testing: Use existing atomic.h for vma/maple tests Brendan Jackman
@ 2025-08-28 12:27 ` Brendan Jackman
2025-09-01 10:27 ` Pedro Falcato
2025-08-28 12:27 ` [PATCH v2 2/4] tools: testing: Allow importing arch headers in shared.mk Brendan Jackman
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Brendan Jackman @ 2025-08-28 12:27 UTC (permalink / raw)
To: Liam R. Howlett, Andrew Morton, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato
Cc: linux-kernel, maple-tree, linux-mm, Brendan Jackman
The VMA tests need an operation equivalent to
atomic_inc_unless_negative() to implement a fake mapping_map_writable().
Adding it will enable them to switch to the shared atomic headers and
simplify that fake implementation.
In order to add that, also add atomic_try_cmpxchg() which can be used to
implement it. This is copied from Documentation/atomic_t.txt. Then,
implement atomic_inc_unless_negative() itself based on the
raw_atomic_dec_unless_positive() in
include/linux/atomic/atomic-arch-fallback.h.
There's no present need for a highly-optimised version of this (nor any
reason to think this implementation is sub-optimal on x86) so just
implement this with generic C, no x86-specifics.
Signed-off-by: Brendan Jackman <jackmanb@google.com>
---
tools/include/linux/atomic.h | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/tools/include/linux/atomic.h b/tools/include/linux/atomic.h
index 01907b33537e04c5e860dd2cd61a61dfab6ea8f1..50c66ba9ada58cf05125e2e2472734bf3b0f8595 100644
--- a/tools/include/linux/atomic.h
+++ b/tools/include/linux/atomic.h
@@ -12,4 +12,26 @@ void atomic_long_set(atomic_long_t *v, long i);
#define atomic_cmpxchg_release atomic_cmpxchg
#endif /* atomic_cmpxchg_relaxed */
+static inline bool atomic_try_cmpxchg(atomic_t *ptr, int *oldp, int new)
+{
+ int ret, old = *oldp;
+
+ ret = atomic_cmpxchg(ptr, old, new);
+ if (ret != old)
+ *oldp = ret;
+ return ret == old;
+}
+
+static inline bool atomic_inc_unless_negative(atomic_t *v)
+{
+ int c = atomic_read(v);
+
+ do {
+ if (unlikely(c < 0))
+ return false;
+ } while (!atomic_try_cmpxchg(v, &c, c + 1));
+
+ return true;
+}
+
#endif /* __TOOLS_LINUX_ATOMIC_H */
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/4] tools: testing: Allow importing arch headers in shared.mk
2025-08-28 12:27 [PATCH v2 0/4] tools: testing: Use existing atomic.h for vma/maple tests Brendan Jackman
2025-08-28 12:27 ` [PATCH v2 1/4] tools/include: Implement a couple of atomic_t ops Brendan Jackman
@ 2025-08-28 12:27 ` Brendan Jackman
2025-08-28 12:28 ` [PATCH v2 3/4] tools: testing: Support EXTRA_CFLAGS " Brendan Jackman
2025-08-28 12:28 ` [PATCH v2 4/4] tools: testing: Use existing atomic.h for vma/maple tests Brendan Jackman
3 siblings, 0 replies; 7+ messages in thread
From: Brendan Jackman @ 2025-08-28 12:27 UTC (permalink / raw)
To: Liam R. Howlett, Andrew Morton, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato
Cc: linux-kernel, maple-tree, linux-mm, Brendan Jackman
There is an arch/ tree under tools. This contains some useful stuff, to
make that available, add it to the -I flags. This requires $(SRCARCH),
which is provided by Makefile.arch, so include that..
There still aren't that many headers so also just smush all of them into
SHARED_DEPS instead of starting to do any header dependency hocus pocus.
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Acked-by: Pedro Falcato <pfalcato@suse.de>
Signed-off-by: Brendan Jackman <jackmanb@google.com>
---
tools/testing/shared/shared.mk | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/tools/testing/shared/shared.mk b/tools/testing/shared/shared.mk
index 923ee2492256b693c5cf16cc014d9d2410be5457..937aaa7623320da1085a8e0f43f6a728ddd3ab1c 100644
--- a/tools/testing/shared/shared.mk
+++ b/tools/testing/shared/shared.mk
@@ -1,6 +1,8 @@
# SPDX-License-Identifier: GPL-2.0
+include ../../scripts/Makefile.arch
-CFLAGS += -I../shared -I. -I../../include -I../../../lib -g -Og -Wall \
+CFLAGS += -I../shared -I. -I../../include -I../../arch/$(SRCARCH)/include \
+ -I../../../lib -g -Og -Wall \
-D_LGPL_SOURCE -fsanitize=address -fsanitize=undefined
LDFLAGS += -fsanitize=address -fsanitize=undefined
LDLIBS += -lpthread -lurcu
@@ -11,6 +13,7 @@ SHARED_DEPS = Makefile ../shared/shared.mk ../shared/*.h generated/map-shift.h \
generated/bit-length.h generated/autoconf.h \
../../include/linux/*.h \
../../include/asm/*.h \
+ ../../arch/$(SRCARCH)/include/asm/*.h \
../../../include/linux/xarray.h \
../../../include/linux/maple_tree.h \
../../../include/linux/radix-tree.h \
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 3/4] tools: testing: Support EXTRA_CFLAGS in shared.mk
2025-08-28 12:27 [PATCH v2 0/4] tools: testing: Use existing atomic.h for vma/maple tests Brendan Jackman
2025-08-28 12:27 ` [PATCH v2 1/4] tools/include: Implement a couple of atomic_t ops Brendan Jackman
2025-08-28 12:27 ` [PATCH v2 2/4] tools: testing: Allow importing arch headers in shared.mk Brendan Jackman
@ 2025-08-28 12:28 ` Brendan Jackman
2025-08-28 12:28 ` [PATCH v2 4/4] tools: testing: Use existing atomic.h for vma/maple tests Brendan Jackman
3 siblings, 0 replies; 7+ messages in thread
From: Brendan Jackman @ 2025-08-28 12:28 UTC (permalink / raw)
To: Liam R. Howlett, Andrew Morton, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato
Cc: linux-kernel, maple-tree, linux-mm, Brendan Jackman
This allows the user to set cflags when building tests that use this
shared build infrastructure.
For example, it enables building with -Werror so that patch-check
scripts will fail:
make -C tools/testing/vma -j EXTRA_CFLAGS=-Werror
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Acked-by: Pedro Falcato <pfalcato@suse.de>
Signed-off-by: Brendan Jackman <jackmanb@google.com>
---
tools/testing/shared/shared.mk | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/testing/shared/shared.mk b/tools/testing/shared/shared.mk
index 937aaa7623320da1085a8e0f43f6a728ddd3ab1c..5bcdf26c8a9d51ab2cbd264f2f8a7445d7c036e3 100644
--- a/tools/testing/shared/shared.mk
+++ b/tools/testing/shared/shared.mk
@@ -4,6 +4,7 @@ include ../../scripts/Makefile.arch
CFLAGS += -I../shared -I. -I../../include -I../../arch/$(SRCARCH)/include \
-I../../../lib -g -Og -Wall \
-D_LGPL_SOURCE -fsanitize=address -fsanitize=undefined
+CFLAGS += $(EXTRA_CFLAGS)
LDFLAGS += -fsanitize=address -fsanitize=undefined
LDLIBS += -lpthread -lurcu
LIBS := slab.o find_bit.o bitmap.o hweight.o vsprintf.o
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 4/4] tools: testing: Use existing atomic.h for vma/maple tests
2025-08-28 12:27 [PATCH v2 0/4] tools: testing: Use existing atomic.h for vma/maple tests Brendan Jackman
` (2 preceding siblings ...)
2025-08-28 12:28 ` [PATCH v2 3/4] tools: testing: Support EXTRA_CFLAGS " Brendan Jackman
@ 2025-08-28 12:28 ` Brendan Jackman
2025-09-01 10:29 ` Pedro Falcato
3 siblings, 1 reply; 7+ messages in thread
From: Brendan Jackman @ 2025-08-28 12:28 UTC (permalink / raw)
To: Liam R. Howlett, Andrew Morton, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, Pedro Falcato
Cc: linux-kernel, maple-tree, linux-mm, Brendan Jackman
The shared userspace logic used for unit-testing maple tree and VMA code
currently has its own replacements for atomics helpers. This is not
needed as the necessary APIs already have userspace implementations in
the tools tree. Switching over to that allows deleting a bit of code.
Note that the implementation is different; while the version being
deleted here is implemented using liburcu, the existing version in tools
uses either x86 asm or compiler builtins. It's assumed that both are
equally likely to be correct.
The tools tree's version of atomic_t is a struct type while the version
being deleted was just a typedef of an integer. This means it's no
longer valid to call __sync_bool_compare_and_swap() directly on it. One
option would be to just peek into the struct and call it on the field,
but it seems a little cleaner to just use the corresponding atomic.h
API whic has been added recently. Now the fake mapping_map_writable() is
copied from the real one.
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Signed-off-by: Brendan Jackman <jackmanb@google.com>
---
tools/testing/shared/linux/maple_tree.h | 6 ++----
tools/testing/vma/linux/atomic.h | 17 -----------------
tools/testing/vma/vma_internal.h | 12 +++---------
3 files changed, 5 insertions(+), 30 deletions(-)
diff --git a/tools/testing/shared/linux/maple_tree.h b/tools/testing/shared/linux/maple_tree.h
index f67d47d32857cee296c2784da57825c9a31cd340..7d0fadef0f11624dbb110ad351aabdc79a19dcd2 100644
--- a/tools/testing/shared/linux/maple_tree.h
+++ b/tools/testing/shared/linux/maple_tree.h
@@ -1,7 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0+ */
-#define atomic_t int32_t
-#define atomic_inc(x) uatomic_inc(x)
-#define atomic_read(x) uatomic_read(x)
-#define atomic_set(x, y) uatomic_set(x, y)
+#include <linux/atomic.h>
+
#define U8_MAX UCHAR_MAX
#include "../../../../include/linux/maple_tree.h"
diff --git a/tools/testing/vma/linux/atomic.h b/tools/testing/vma/linux/atomic.h
deleted file mode 100644
index 788c597c4fdea7392307de93ff4459453b96179b..0000000000000000000000000000000000000000
--- a/tools/testing/vma/linux/atomic.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-or-later */
-
-#ifndef _LINUX_ATOMIC_H
-#define _LINUX_ATOMIC_H
-
-#define atomic_t int32_t
-#define atomic_inc(x) uatomic_inc(x)
-#define atomic_read(x) uatomic_read(x)
-#define atomic_set(x, y) uatomic_set(x, y)
-#define U8_MAX UCHAR_MAX
-
-#ifndef atomic_cmpxchg_relaxed
-#define atomic_cmpxchg_relaxed uatomic_cmpxchg
-#define atomic_cmpxchg_release uatomic_cmpxchg
-#endif /* atomic_cmpxchg_relaxed */
-
-#endif /* _LINUX_ATOMIC_H */
diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h
index f13354bf0a1e3839327da7927c4a7da1530a1693..437d2a1013be418c6d3e77534fa19b0ee1518c9b 100644
--- a/tools/testing/vma/vma_internal.h
+++ b/tools/testing/vma/vma_internal.h
@@ -21,6 +21,7 @@
#include <stdlib.h>
+#include <linux/atomic.h>
#include <linux/list.h>
#include <linux/maple_tree.h>
#include <linux/mm.h>
@@ -1398,15 +1399,8 @@ static inline bool map_deny_write_exec(unsigned long old, unsigned long new)
static inline int mapping_map_writable(struct address_space *mapping)
{
- int c = atomic_read(&mapping->i_mmap_writable);
-
- /* Derived from the raw_atomic_inc_unless_negative() implementation. */
- do {
- if (c < 0)
- return -EPERM;
- } while (!__sync_bool_compare_and_swap(&mapping->i_mmap_writable, c, c+1));
-
- return 0;
+ return atomic_inc_unless_negative(&mapping->i_mmap_writable) ?
+ 0 : -EPERM;
}
static inline unsigned long move_page_tables(struct pagetable_move_control *pmc)
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/4] tools/include: Implement a couple of atomic_t ops
2025-08-28 12:27 ` [PATCH v2 1/4] tools/include: Implement a couple of atomic_t ops Brendan Jackman
@ 2025-09-01 10:27 ` Pedro Falcato
0 siblings, 0 replies; 7+ messages in thread
From: Pedro Falcato @ 2025-09-01 10:27 UTC (permalink / raw)
To: Brendan Jackman
Cc: Liam R. Howlett, Andrew Morton, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, linux-kernel, maple-tree, linux-mm
On Thu, Aug 28, 2025 at 12:27:58PM +0000, Brendan Jackman wrote:
> The VMA tests need an operation equivalent to
> atomic_inc_unless_negative() to implement a fake mapping_map_writable().
> Adding it will enable them to switch to the shared atomic headers and
> simplify that fake implementation.
>
> In order to add that, also add atomic_try_cmpxchg() which can be used to
> implement it. This is copied from Documentation/atomic_t.txt. Then,
> implement atomic_inc_unless_negative() itself based on the
> raw_atomic_dec_unless_positive() in
> include/linux/atomic/atomic-arch-fallback.h.
>
> There's no present need for a highly-optimised version of this (nor any
> reason to think this implementation is sub-optimal on x86) so just
> implement this with generic C, no x86-specifics.
>
> Signed-off-by: Brendan Jackman <jackmanb@google.com>
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
LGTM, thanks!
--
Pedro
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 4/4] tools: testing: Use existing atomic.h for vma/maple tests
2025-08-28 12:28 ` [PATCH v2 4/4] tools: testing: Use existing atomic.h for vma/maple tests Brendan Jackman
@ 2025-09-01 10:29 ` Pedro Falcato
0 siblings, 0 replies; 7+ messages in thread
From: Pedro Falcato @ 2025-09-01 10:29 UTC (permalink / raw)
To: Brendan Jackman
Cc: Liam R. Howlett, Andrew Morton, Lorenzo Stoakes, Vlastimil Babka,
Jann Horn, linux-kernel, maple-tree, linux-mm
On Thu, Aug 28, 2025 at 12:28:01PM +0000, Brendan Jackman wrote:
> The shared userspace logic used for unit-testing maple tree and VMA code
> currently has its own replacements for atomics helpers. This is not
> needed as the necessary APIs already have userspace implementations in
> the tools tree. Switching over to that allows deleting a bit of code.
>
> Note that the implementation is different; while the version being
> deleted here is implemented using liburcu, the existing version in tools
> uses either x86 asm or compiler builtins. It's assumed that both are
> equally likely to be correct.
>
> The tools tree's version of atomic_t is a struct type while the version
> being deleted was just a typedef of an integer. This means it's no
> longer valid to call __sync_bool_compare_and_swap() directly on it. One
> option would be to just peek into the struct and call it on the field,
> but it seems a little cleaner to just use the corresponding atomic.h
> API whic has been added recently. Now the fake mapping_map_writable() is
> copied from the real one.
>
> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> Signed-off-by: Brendan Jackman <jackmanb@google.com>
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
> ---
> tools/testing/shared/linux/maple_tree.h | 6 ++----
> tools/testing/vma/linux/atomic.h | 17 -----------------
> tools/testing/vma/vma_internal.h | 12 +++---------
> 3 files changed, 5 insertions(+), 30 deletions(-)
Yay, less code! :)
--
Pedro
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-09-01 10:29 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-28 12:27 [PATCH v2 0/4] tools: testing: Use existing atomic.h for vma/maple tests Brendan Jackman
2025-08-28 12:27 ` [PATCH v2 1/4] tools/include: Implement a couple of atomic_t ops Brendan Jackman
2025-09-01 10:27 ` Pedro Falcato
2025-08-28 12:27 ` [PATCH v2 2/4] tools: testing: Allow importing arch headers in shared.mk Brendan Jackman
2025-08-28 12:28 ` [PATCH v2 3/4] tools: testing: Support EXTRA_CFLAGS " Brendan Jackman
2025-08-28 12:28 ` [PATCH v2 4/4] tools: testing: Use existing atomic.h for vma/maple tests Brendan Jackman
2025-09-01 10:29 ` Pedro Falcato
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).