[PATCH drm-next v2 04/16] maple_tree: add flag MT_FLAGS_LOCK_NONE

Danilo Krummrich
2023-02-17T13:45:49Z

Generic components making use of the maple tree (such as the
DRM GPUVA Manager) delegate the responsibility of ensuring mutual
exclusion to their users.

While such components could inherit the concept of an external lock,
some users might just serialize the access to the component and hence to
the internal maple tree.

In order to allow such use cases, add a new flag MT_FLAGS_LOCK_NONE to
indicate not to do any internal lockdep checks.

Signed-off-by: Danilo Krummrich <dakr@redhat.com>
---
include/linux/maple_tree.h | 20 +++++++++++++++-----
lib/maple_tree.c | 7 ++++---
2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h
index ca04c900e51a..f795e5def8d0 100644
--- a/include/linux/maple_tree.h
+++ b/include/linux/maple_tree.h
@@ -170,10 +170,11 @@ enum maple_type {
#define MT_FLAGS_USE_RCU 0x02
#define MT_FLAGS_HEIGHT_OFFSET 0x02
#define MT_FLAGS_HEIGHT_MASK 0x7C
-#define MT_FLAGS_LOCK_MASK 0x300
+#define MT_FLAGS_LOCK_MASK 0x700
#define MT_FLAGS_LOCK_IRQ 0x100
#define MT_FLAGS_LOCK_BH 0x200
#define MT_FLAGS_LOCK_EXTERN 0x300
+#define MT_FLAGS_LOCK_NONE 0x400

#define MAPLE_HEIGHT_MAX 31

@@ -559,11 +560,16 @@ static inline void mas_set(struct ma_state *mas, unsigned long index)
mas_set_range(mas, index, index);
}

-static inline bool mt_external_lock(const struct maple_tree *mt)
+static inline bool mt_lock_external(const struct maple_tree *mt)
{
return (mt->ma_flags & MT_FLAGS_LOCK_MASK) == MT_FLAGS_LOCK_EXTERN;
}

+static inline bool mt_lock_none(const struct maple_tree *mt)
+{
+ return (mt->ma_flags & MT_FLAGS_LOCK_MASK) == MT_FLAGS_LOCK_NONE;
+}
+
/**
* mt_init_flags() - Initialise an empty maple tree with flags.
* @mt: Maple Tree
@@ -577,7 +583,7 @@ static inline bool mt_external_lock(const struct maple_tree *mt)
static inline void mt_init_flags(struct maple_tree *mt, unsigned int flags)
{
mt->ma_flags = flags;
- if (!mt_external_lock(mt))
+ if (!mt_lock_external(mt) && !mt_lock_none(mt))
spin_lock_init(&mt->ma_lock);
rcu_assign_pointer(mt->ma_root, NULL);
}
@@ -612,9 +618,11 @@ static inline void mt_clear_in_rcu(struct maple_tree *mt)
if (!mt_in_rcu(mt))
return;

- if (mt_external_lock(mt)) {
+ if (mt_lock_external(mt)) {
BUG_ON(!mt_lock_is_held(mt));
mt->ma_flags &= ~MT_FLAGS_USE_RCU;
+ } else if (mt_lock_none(mt)) {
+ mt->ma_flags &= ~MT_FLAGS_USE_RCU;
} else {
mtree_lock(mt);
mt->ma_flags &= ~MT_FLAGS_USE_RCU;
@@ -631,9 +639,11 @@ static inline void mt_set_in_rcu(struct maple_tree *mt)
if (mt_in_rcu(mt))
return;

- if (mt_external_lock(mt)) {
+ if (mt_lock_external(mt)) {
BUG_ON(!mt_lock_is_held(mt));
mt->ma_flags |= MT_FLAGS_USE_RCU;
+ } else if (mt_lock_none(mt)) {
+ mt->ma_flags |= MT_FLAGS_USE_RCU;
} else {
mtree_lock(mt);
mt->ma_flags |= MT_FLAGS_USE_RCU;
diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index 26e2045d3cda..f51c0fd4eaad 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -802,8 +802,8 @@ static inline void __rcu **ma_slots(struct maple_node *mn, enum maple_type mt)

static inline bool mt_locked(const struct maple_tree *mt)
{
- return mt_external_lock(mt) ? mt_lock_is_held(mt) :
- lockdep_is_held(&mt->ma_lock);
+ return mt_lock_external(mt) ? mt_lock_is_held(mt) :
+ mt_lock_none(mt) ? true : lockdep_is_held(&mt->ma_lock);
}

static inline void *mt_slot(const struct maple_tree *mt,
@@ -6120,7 +6120,8 @@ bool mas_nomem(struct ma_state *mas, gfp_t gfp)
return false;
}

- if (gfpflags_allow_blocking(gfp) && !mt_external_lock(mas->tree)) {
+ if (gfpflags_allow_blocking(gfp) &&
+ !mt_lock_external(mas->tree) && !mt_lock_none(mas->tree)) {
mtree_unlock(mas->tree);
mas_alloc_nodes(mas, gfp);
mtree_lock(mas->tree);
--
2.39.1