rust: add support for maple trees
Subsystems (4)
Andrew Ballance
reviewercover rust: add support for maple trees
This RFC adds an initial implementation of maple tree abstractions. I would like any feedback possible.
I would like to upstream this eventually but, it's probably going to be a bit until there is a user.
Andrew Ballance (2):
maple_tree: add __mtree_insert_range function
rust: add maple tree abstractions
include/linux/maple_tree.h | 2 + lib/maple_tree.c | 37 ++++ rust/helpers/helpers.c | 1 + rust/helpers/maple_tree.c | 25 +++ rust/kernel/lib.rs | 1 + rust/kernel/maple_tree.rs | 340 +++++++++++++++++++++++++++++++++++++ 6 files changed, 406 insertions(+) create mode 100644 rust/helpers/maple_tree.c create mode 100644 rust/kernel/maple_tree.rs
base-commit: 38fec10eb60d687e30c8c6b5420d86e8149f7557
Signature · 1 line
2.49.0
Andrew Ballance
reviewerRFC PATCH 1/2 maple_tree: add __mtree_insert_range function
adds the __mtree_insert_range which is identical to mtree_insert_range but does not aquire ma_lock.
This function is needed for the rust bindings for maple trees because the locking is handled on the rust side.
--- include/linux/maple_tree.h | 2 ++ lib/maple_tree.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+)
include/linux/maple_tree.h +2 -0
@@ -329,6 +329,8 @@ int mtree_insert(struct maple_tree *mt, unsigned long index, void *entry, gfp_t gfp);int mtree_insert_range (struct maple_tree *mt,unsigned long first,unsigned long last,void *entry,gfp_t gfp); +int __mtree_insert_range (struct maple_tree *mt,unsigned long first, +unsigned long last,void *entry,gfp_t gfp);int mtree_alloc_range (struct maple_tree *mt,unsigned long *startp,void *entry,unsigned long size,unsigned long min,unsigned long max,gfp_t gfp);
lib/maple_tree.c +37 -0
@@ -6387,6 +6387,43 @@ int mtree_insert_range(struct maple_tree *mt, unsigned long first,return ret; }EXPORT_SYMBOL (mtree_insert_range); +/** +* __mtree_insert_range() - Insert an entry at a given range if there is no value. without locking +* @mt: The maple tree +* @first: The start of the range +* @last: The end of the range +* @entry: The entry to store +* @gfp: The GFP_FLAGS to use for allocations. +* +* Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid +* request, -ENOMEM if memory could not be allocated. +* Note that the user needs to manually lock the tree. +*/ +int __mtree_insert_range (struct maple_tree *mt,unsigned long first, +unsigned long last,void *entry,gfp_t gfp) +{ +MA_STATE (ms, mt, first, last); +int ret =0 ; + +if (WARN_ON_ONCE (xa_is_advanced (entry))) +return -EINVAL ; + +if (first > last) +return -EINVAL ; + +retry: +mas_insert (&ms, entry); +if (mas_nomem (&ms, gfp)) + goto retry; + +if (mas_is_err (&ms)) + ret =xa_err (ms.node); + +mas_destroy (&ms); +return ret; + +} +EXPORT_SYMBOL (__mtree_insert_range);/** * mtree_insert() - Insert an entry at a given index if there is no value.
Signature · 1 line
2.49.0
Re: [RFC PATCH 1/2] maple_tree: add __mtree_insert_range function
Quoted 4 lines · Andrew Ballance
adds the
__mtree_insert_rangewhich is identical tomtree_insert_rangebut does not aquirema_lock.
This function is needed for the rust bindings for maple trees because the locking is handled on the rust side.
No.
The support for external locking is a TEMPORARY HACK. I've talked before about why this is and don't feel like explaining it again.
Re: [RFC PATCH 1/2] maple_tree: add __mtree_insert_range function
Quoted 10 lines · Matthew Wilcox
Quoted 4 lines · Andrew Ballance
adds the
__mtree_insert_rangewhich is identical tomtree_insert_rangebut does not aquirema_lock.
This function is needed for the rust bindings for maple trees because the locking is handled on the rust side.No.
The support for external locking is a TEMPORARY HACK. I've talked before about why this is and don't feel like explaining it again.
Does it mean that ideally maple trees should not support external locking, i.e. it should use its own locking?
(BTw, people usually add some documentation if they don't want to repeat themselves ;-))
Regards,
Boqun
Andrew Ballance
reviewerRe: [RFC PATCH 1/2] maple_tree: add __mtree_insert_range function
In reply to Matthew WilcoxQuoted 10 lines · Matthew Wilcox
Quoted 4 lines · Andrew Ballance
adds the
__mtree_insert_rangewhich is identical tomtree_insert_rangebut does not aquirema_lock.
This function is needed for the rust bindings for maple trees because the locking is handled on the rust side.No.
The support for external locking is a TEMPORARY HACK. I've talked before about why this is and don't feel like explaining it again.
this does use the maple_tree's internal ma_lock. the locking is done on the rust side using bindings to mtree_lock(). it is done this way so that rust can track the lifetimes of any values from mtree_load.
the easiest way to do this was to add code to the c side. for the v2 I can make changes so that this does not touch the c side.
Andrew Ballance
reviewerRFC PATCH 2/2 rust: add maple tree abstractions
In reply to Andrew Ballancemaple trees are sparse array like data structure that maps non-overlapping ranges to pointers.
implements basic abstractions for maple trees in rust.
--- rust/helpers/helpers.c | 1 + rust/helpers/maple_tree.c | 25 +++ rust/kernel/lib.rs | 1 + rust/kernel/maple_tree.rs | 340 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 367 insertions(+) create mode 100644 rust/helpers/maple_tree.c create mode 100644 rust/kernel/maple_tree.rs
rust/helpers/helpers.c +1 -0
@@ -18,6 +18,7 @@#include "io.c" #include "jump_label.c" #include "kunit.c" +#include "maple_tree.c" #include "mutex.c" #include "page.c" #include "platform.c"
rust/helpers/maple_tree.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/maple_tree.h> + + +void rust_helper_mt_init_flags (struct maple_tree *mt,unsigned int flags) +{ +mt_init_flags (mt, flags); +} + +void rust_helper_mtree_lock (struct maple_tree *mt) +{ +mtree_lock (mt); +} + +void rust_helper_mtree_unlock (struct maple_tree *mt) +{ +mtree_unlock (mt); +} + +struct ma_state rust_helper_MA_STATE (struct maple_tree *mt,unsigned long start,unsigned long end) +{ +MA_STATE (mas, mt, start, end); +return mas; +}
rust/kernel/lib.rs +1 -0
@@ -57,6 +57,7 @@#[cfg( CONFIG_KUNIT )] pub mod kunit;pub mod list; +pub mod maple_tree;pub mod miscdevice;#[cfg( CONFIG_NET )] pub mod net;
rust/kernel/maple_tree.rs new file +340 -0
340 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [RFC PATCH 2/2] rust: add maple tree abstractions
Hi Andrew,
Some doc-related notes for future submissions... I hope this helps.
rust/helpers/maple_tree.c line 5
Quoted 4 lines · Andrew Ballance
rust/helpers/maple_tree.c new file +3
-0+#include <linux/maple_tree.h> + +
Nit: double newline.
Review
Quoted 2 lines
(quoted diff) +2
-0+//! # Examples +//! ```
There should be an empty doc line between these.
rust/kernel/maple_tree.rs line 32
Quoted 2 lines
rust/kernel/maple_tree.rs new file +2
-0+// TODO and missing features +// - the C version suports external locking this only supports the internal spinlock
(Multiple instances) Typo -- you may want to use e.g. checkpatch.pl with --codespell.
rust/kernel/maple_tree.rs line 46
Quoted 8 lines
rust/kernel/maple_tree.rs new file +8
-0+// SAFETY: +// we cannot derefrence any pointers without holding the spinlock because +// all returned pointers are guaranteed to have been inserted by the user +// but the pointers are not guaranteed to be still be valid +// another thread may have already removed and dropped the pointers +// so to safely deref the returned pointers the user must +// have exclusive write access to the `MapleTree` +// or rcu_read_lock() is held and updater side uses a synchronize_rcu()
SAFETY comments are intended to go attached to an unsafe block or implementation, explaining why they satisfy the requirements.
Clippy should be warning here, but it doesn't likely due to this Clippy issue I just filed:
https://github.com/rust-lang/rust-clippy/issues/14554
(plus other three I filed while looking into this one).
Quoted 1 line
+/// A
MapleTreeis a tree like data structure that is optimized for storing
(Multiple instances) Please use intra-doc links wherever they work.
Quoted 1 line
+// # Invariants
(Multiple instances) Please make this section part of the docs. We may change how we document private invariants in the future (or we may expose private fields so that it is clearer) -- for the moment, please add a note if you really want users to never rely on a particular invariant.
Quoted 1 line
+ /// creates a new
MapleTreewith the specifiedflags
(Multiple instances) Please follow the usual style, e.g. Markdown and intra-doc links where possible, start with uppercase, period at the end of sentences, etc.
rust/kernel/maple_tree.rs line 88
Quoted 3 lines
rust/kernel/maple_tree.rs new file +3
-0+// SAFETY: +// - mt is valid because of ffi_init +// - maple_tree contains a lock which should be pinned
(Multiple instances) Please format with Markdown in comments.
rust/kernel/maple_tree.rs line 100
Quoted 3 lines
rust/kernel/maple_tree.rs new file +3
-0+/// helper for internal use. +/// returns an iterator through the maple tree +/// # Safety
(Multiple instances) Newlines are needed here -- rustdoc uses the first paragraph as the "short description", too.
rust/kernel/maple_tree.rs line 318
Quoted 2 lines
rust/kernel/maple_tree.rs new file +2
-0+#[derive( Clone , Copy , PartialEq )] +/// flags to be used with [`MapleTree::new`].
We write attributes below the documentation.
rust/kernel/maple_tree.rs line 227
Quoted 4 lines
rust/kernel/maple_tree.rs new file +4
-0+if ptr.is_null () { +return None ; + } +// SAFETY:
We typically leave a newline after a loop or a branch.
Thanks for the patch!
Cheers,
Miguel
Re: [RFC PATCH 2/2] rust: add maple tree abstractions
In reply to Andrew BallanceQuoted 2 lines · Andrew Ballance
maple trees are sparse array like data structure that maps non-overlapping ranges to pointers.
Why do you think the maple tree is a spare array like data structure?
...
Andrew Ballance
reviewerRe: [RFC PATCH 2/2] rust: add maple tree abstractions
Quoted 6 lines · Liam R. Howlett
Quoted 2 lines · Andrew Ballance
maple trees are sparse array like data structure that maps non-overlapping ranges to pointers.
Why do you think the maple tree is a spare array like data structure?
I called the maple tree "sparse array like" because indexes that have no entry map to null and there can be gaps between ranges. I did not mean to imply that a maple tree was literally a sparse array.
Would you like me to reword this?
Re: [RFC PATCH 2/2] rust: add maple tree abstractions
Quoted 11 lines · Andrew Ballance
Quoted 6 lines · Liam R. Howlett
Quoted 2 lines · Andrew Ballance
maple trees are sparse array like data structure that maps non-overlapping ranges to pointers.
Why do you think the maple tree is a spare array like data structure?
I called the maple tree "sparse array like" because indexes that have no entry map to null and there can be gaps between ranges. I did not mean to imply that a maple tree was literally a sparse array.
Yes, I understood what you said to mean it was "like a sparse array", I just don't see how it is like a sparse array.
My understanding is that a sparse array is sparse because not every value is represented in the underlying storage, where as the maple tree defines every range to an entry (including single entries, and NULL entries). It does combine multiple NULL entries into a single range.
There is a non-node store of a single entry of range [0,0] when [1, ULONG_MAX] is NULL, or the entire empty set - but that's an optimisation.
I haven't implemented a sparse array, so perhaps I'm missing how they are alike.
Quoted 3 lines
Would you like me to reword this?
No, I don't think it is worth having a rust implementation right now as there are no users and I could cause issues on the rust side without knowing.
I was wondering if you read that it was like a sparse array somewhere and the reason behind it. There is a plan for a sparse node type, but there are a number of things that need to happen before I get there.
I've always said it was a b-tree variant (probably b+) for storing ranges with a branching factor of 10 or 16 (for now, anyways).
Thanks,
Liam
Danilo Krummrich
reviewerRe: [RFC PATCH 2/2] rust: add maple tree abstractions
Quoted 3 lines · Liam R. Howlett
No, I don't think it is worth having a rust implementation right now as there are no users and I could cause issues on the rust side without knowing.
I may have a user for this in the future, but we still need to investigate other options for this.
If you remember, I'd have loved to use this for DRM GPUVM, but unfortunately failed since we would need to be able to pre-allocate multiple new entries upfront, before knowing any details about the entries and before modifying the tree.
With nova-core we may get another option to use it as VRAM allocator. But as mentioned, we're still investigating.
- Danilo