From: Yury Norov <yury.norov@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Matthew Wilcox <willy@infradead.org>,
"Liam R. Howlett" <liam@infradead.org>
Cc: Yury Norov <ynorov@nvidia.com>, Chris Li <sparse@chrisli.org>,
Alice Ryhl <aliceryhl@google.com>,
Andrew Ballance <andrewjballance@gmail.com>,
Yury Norov <yury.norov@gmail.com>,
linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
maple-tree@lists.infradead.org
Subject: [PATCH 3/3] maple_tree: assert static storage for DEFINE_MTREE()
Date: Fri, 11 Sep 2026 18:14:43 -0400 [thread overview]
Message-ID: <20260911221444.1523311-4-ynorov@nvidia.com> (raw)
In-Reply-To: <20260911221444.1523311-1-ynorov@nvidia.com>
DEFINE_MTREE() uses MTREE_INIT(), which initializes the tree's embedded
lock with a static spinlock initializer. If the tree is an automatic
local object, lockdep rejects its address as a non-static class key and
disables locking validation on the first lock acquisition.
Apply ASSERT_STATIC_STORAGE() to DEFINE_MTREE() to catch automatic
local definitions at compile time. For example:
void example(void)
{
DEFINE_MTREE(mt);
mtree_destroy(&mt);
}
GCC reports:
error: initializer element is not constant
name##_storage_check = &(name)
^
note: in expansion of macro 'ASSERT_STATIC_STORAGE'
ASSERT_STATIC_STORAGE(name)
note: in expansion of macro 'DEFINE_MTREE'
DEFINE_MTREE(mt);
File-scope definitions and static local trees remain valid. Automatic
local trees must instead use mt_init() or mt_init_flags(). Direct uses of
MTREE_INIT() and MTREE_INIT_EXT() are unchanged.
Replace the local DEFINE_MTREE() in the interval-tree span test with a
plain declaration; the test already initializes the tree with
mt_init_flags() before use. Convert the three local Maple Trees in the
userspace radix-tree tests to mt_init().
Validated file-scope and static local definitions with a kernel object
build, and confirmed that an automatic local definition fails to compile.
The Maple Tree test and region allocation benchmark objects also build
with lockdep enabled.
Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
include/linux/maple_tree.h | 4 +++-
lib/interval_tree_test.c | 2 +-
tools/testing/radix-tree/maple.c | 12 +++++++++---
3 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h
index e595ae5cd0ee..e30e57f5f3df 100644
--- a/include/linux/maple_tree.h
+++ b/include/linux/maple_tree.h
@@ -9,6 +9,7 @@
*/
#include <linux/kernel.h>
+#include <linux/compiler.h>
#include <linux/rcupdate.h>
#include <linux/spinlock.h>
@@ -297,7 +298,8 @@ struct maple_tree {
#endif
#define DEFINE_MTREE(name) \
- struct maple_tree name = MTREE_INIT(name, 0)
+ struct maple_tree name = MTREE_INIT(name, 0); \
+ ASSERT_STATIC_STORAGE(name)
#define mtree_lock(mt) spin_lock((&(mt)->ma_lock))
#define mtree_lock_nested(mas, subclass) \
diff --git a/lib/interval_tree_test.c b/lib/interval_tree_test.c
index b0b07270ce7c..06f77fb3179f 100644
--- a/lib/interval_tree_test.c
+++ b/lib/interval_tree_test.c
@@ -244,7 +244,7 @@ static int span_iteration_check(void)
unsigned long start, last;
struct interval_tree_span_iter span, mas_span;
- DEFINE_MTREE(tree);
+ struct maple_tree tree;
MA_STATE(mas, &tree, 0, 0);
diff --git a/tools/testing/radix-tree/maple.c b/tools/testing/radix-tree/maple.c
index d967e76a3c06..bfe4b8626c42 100644
--- a/tools/testing/radix-tree/maple.c
+++ b/tools/testing/radix-tree/maple.c
@@ -36022,10 +36022,12 @@ static noinline void __init check_erase_rebalance(struct maple_tree *mt)
static noinline void __init check_mtree_dup(struct maple_tree *mt)
{
- DEFINE_MTREE(new);
+ struct maple_tree new;
int i, j, ret, count = 0;
unsigned int rand_seed = 17, rand;
+ mt_init(&new);
+
/* store a value at [0, 0] */
mt_init_flags(mt, 0);
mtree_store_range(mt, 0, 0, xa_mk_value(0), GFP_KERNEL);
@@ -36319,7 +36321,9 @@ static inline int check_vma_modification(struct maple_tree *mt)
void farmer_tests(void)
{
struct maple_node *node;
- DEFINE_MTREE(tree);
+ struct maple_tree tree;
+
+ mt_init(&tree);
mt_dump(&tree, mt_dump_dec);
@@ -36432,9 +36436,11 @@ static unsigned long get_last_index(struct ma_state *mas)
static void test_spanning_store_regression(void)
{
unsigned long from = 0, to = 0;
- DEFINE_MTREE(tree);
+ struct maple_tree tree;
MA_STATE(mas, &tree, 0, 0);
+ mt_init(&tree);
+
/*
* Build a 3-level tree. We require a parent node below the root node
* and 2 leaf nodes under it, so we can span the entirety of the right
--
2.53.0
prev parent reply other threads:[~2026-09-11 22:14 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-11 22:14 [PATCH 0/3] Catch automatic storage in IDA and Maple Tree definitions Yury Norov
2026-09-11 22:14 ` [PATCH 1/3] compiler.h: add ASSERT_STATIC_STORAGE() Yury Norov
2026-09-12 5:57 ` Andrew Morton
2026-09-12 22:31 ` Yury Norov
2026-09-11 22:14 ` [PATCH 2/3] idr: assert static storage for DEFINE_IDA() Yury Norov
2026-09-11 22:14 ` Yury Norov [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260911221444.1523311-4-ynorov@nvidia.com \
--to=yury.norov@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=aliceryhl@google.com \
--cc=andrewjballance@gmail.com \
--cc=liam@infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-sparse@vger.kernel.org \
--cc=maple-tree@lists.infradead.org \
--cc=sparse@chrisli.org \
--cc=willy@infradead.org \
--cc=ynorov@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®