On 10/7/25 08:34, Christoph Hellwig wrote: > On Wed, Sep 10, 2025 at 10:01:02AM +0200, Vlastimil Babka wrote: >> Hi, >> >> I'm sending full v8 due to more changes in the middle of the series that >> resulted in later patches being fixed up due to conflicts (details in >> the changelog below). >> The v8 will replace+extend the v7 in slab/for-next. > > So I've been reading through this and wonder how the preallocation > using shaves is to be used. Do you have example code for that > somewhere?
Maple tree uses this but through its internal layers it's not super clear. Basically it's for situations where you have an upper bound on the objects you might need to allocate in some restricted context where you can't fail but also can't reclaim etc. The steps are: - kmem_cache_create() with kmem_cache_args.sheaf_capacity to be enough for any reasonable upper bounds you might need (but also at least e.g. 32 to get the performance effect of sheaves in general - this will be autotuned in the followup work). If there's a possibility you might need more than this capacity in some rare cases, it's fine, just will be slower when it happens. - kmem_cache_prefill_sheaf() with size being your upper bound for the following critical section operation, you might get more but not less if that succeeds (enter critical section) - kmem_cache_alloc_from_sheaf(gfp, sheaf) - this is guaranteed to succeed as many times as the size you requested in your prefill. gfp is there only for __GFP_ZERO or __GFP_ACCOUNT, where the latter will breach the memcg limit instead of failing - kmem_cache_sheaf_size() tells you how much is left in the prefilled sheaf (exit critical section) - kmem_cache_return_sheaf() will return the sheaf with unused objects later freeing the objects allocated via prefilled sheaf is done normally - alternatively kmem_cache_refill_sheaf() for another round The whole point compared to preallocation via bulk alloc/bulk free is if you have a large upper bound for some corner case but only need few objects typically, this way will be more effective on average. If you preallocate and know you will use everything, you can keep using kmem_cache_alloc_bulk() and this won't give you extra benefits.