From: "Danilo Krummrich" <dakr@kernel.org>
To: "Elijah Wright" <git@elijahs.space>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>,
"Vlastimil Babka" <vbabka@suse.cz>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
"Uladzislau Rezki" <urezki@gmail.com>,
linux-mm@kvack.org
Subject: Re: [PATCH] rust: slab: add basic slab module
Date: Thu, 25 Sep 2025 11:54:22 +0200 [thread overview]
Message-ID: <DD1SGLU4180C.361W5XLH76XNC@kernel.org> (raw)
In-Reply-To: <20250924193643.4001-1-git@elijahs.space>
(+Cc: Lorenzo, Vlastimil, Liam, Uladzislau, MM)
On Wed Sep 24, 2025 at 9:36 PM CEST, Elijah Wright wrote:
> this patch adds a basic slab module for kmem_cache, primarily wrapping
> kmem_cache_create, kmem_cache_alloc, kmem_cache_free, and kmem_cache_destroy.
What's the motivation?
I mean, we will need kmem_cache soon. But the users will all be drivers, e.g.
the GPU drivers that people work on currently.
Drivers shouldn't use "raw" allocators (such as Kmalloc [1] or Vmalloc [2]), but
the corresponding "managed" allocation primitives, such as KBox [3], VBox [4],
KVec, etc.
Therefore, the code below shouldn't be used by drivers directly, hence the
question for motivation.
In any case, kmem_cache is a special allocator (special as in it can have a
non-static lifetime in contrast to other kernel allocators) and should be
integrated with the existing infrastructure in rust/kernel/alloc/.
I think there are multiple options for that; (1) isn't really an option, but I
think it's good to mention anyways:
(1) Allow for non-zero sized implementations of the Allocator trait [3], such
that we can store a reference count to the KmemCache. This is necessary to
ensure that a Box<T, KmemCache> can't out-live the KmemCache itself.
The reason why I said it's not really an option is because it discards the
option for dynamic dispatch of the generic Box type.
(2) Same as (1), but with a custom Box type. This keeps dynamic dispatch for
the generic Box type (i.e. KBox, VBox, KVBox), but duplicates quite some
code and still doesn't allow for dynamic dispatch for the KmemCacheBox.
(3) Implement a macro to generate a custom KmemCache Allocator trait
implementation for every KmemCache instance with a static lifetime.
This makes KmemCache technically equivalent to the other allocators, such
as Kmalloc, etc. but obviously has the downside that the KmemCache might
live much longer than required.
Technically, most KmemCache instances live for the whole module lifetime,
so it might be fine though.
(This is what I think Alice proposed.)
(4) Solve the problem on the C side and let kmem_cache_alloc() take care of
acquiring a reference count to the backing kmem_cache. The main question
here would be where to store the pointer for decreasing the reference
count on kmem_cache_free().
Theoretically, it could be stored within the allocation itself, but it's a
bit of a yikes.
However, it would resolve all the mentioned problems above.
I'd like to see (3) or (4), also depending on what the MM folks think.
- Danilo
[1] https://rust.docs.kernel.org/kernel/alloc/allocator/struct.Kmalloc.html
[2] https://rust.docs.kernel.org/kernel/alloc/allocator/struct.Vmalloc.html
[3] https://rust.docs.kernel.org/kernel/alloc/kbox/type.KBox.html
[4] https://rust.docs.kernel.org/kernel/alloc/kbox/type.VBox.html
[5] https://rust.docs.kernel.org/kernel/alloc/trait.Allocator.html
next prev parent reply other threads:[~2025-09-25 9:54 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-24 19:36 [PATCH] rust: slab: add basic slab module Elijah Wright
2025-09-25 2:17 ` John Hubbard
2025-09-25 8:01 ` Benno Lossin
2025-09-25 9:54 ` Danilo Krummrich [this message]
2025-09-25 17:20 ` Elijah
2025-09-25 17:43 ` Danilo Krummrich
2025-09-25 18:02 ` Liam R. Howlett
2025-09-25 18:08 ` Danilo Krummrich
2025-09-25 18:11 ` Boqun Feng
2025-09-25 18:05 ` Elijah
2025-09-25 18:15 ` Danilo Krummrich
2025-09-25 18:17 ` Danilo Krummrich
[not found] ` <74b3ef24-a307-4d3c-891a-8c5283448b20@elijahs.space>
2025-09-25 18:52 ` Elijah
2025-09-25 17:54 ` Alice Ryhl
2025-09-26 15:00 ` Vlastimil Babka
2025-09-26 15:33 ` Danilo Krummrich
2025-09-26 15:55 ` Danilo Krummrich
2025-09-26 16:32 ` Vlastimil Babka
2025-09-26 16:58 ` Danilo Krummrich
2025-09-26 17:11 ` Vlastimil Babka
2025-09-26 19:01 ` Danilo Krummrich
2025-09-28 14:47 ` Danilo Krummrich
2025-09-29 7:14 ` Vlastimil Babka
2025-09-29 14:11 ` Elijah
2025-09-29 20:32 ` Danilo Krummrich
2025-10-01 4:44 ` [PATCH v2] " Elijah Wright
2025-11-06 7:53 ` Alice Ryhl
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=DD1SGLU4180C.361W5XLH76XNC@kernel.org \
--to=dakr@kernel.org \
--cc=Liam.Howlett@oracle.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=git@elijahs.space \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--cc=vbabka@suse.cz \
/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;
as well as URLs for NNTP newsgroup(s).