Re: [PATCH v4 03/19] kho, memfd_luo: Reference struct kho_vmalloc via serialized pointer
From: Pratyush Yadav
Date: Wed Nov 25 2026 - 09:44:02 EST
On Tue, Nov 24 2026, David Matlack wrote:
> On Fri Nov 20, 2026 at 10:19 AM PST, Pratyush Yadav wrote:
> [...]
>
>>> +static size_t memfd_luo_ser_size(void)
>>> +{
>>> + return sizeof(struct memfd_luo_ser) + sizeof(struct kho_vmalloc);
>>> +}
>>> +
>>> +static struct memfd_luo_ser *memfd_luo_ser_init(void *mem)
>>> +{
>>> + struct kho_vmalloc *kho_vmalloc = mem + sizeof(struct memfd_luo_ser);
>>> + struct memfd_luo_ser *ser = mem;
>>> +
>>> + KHOSER_STORE_PTR(ser->folios, kho_vmalloc);
>>> + return ser;
>>> +}
>>> +
>>
>> Hmm, I am not a huge fan of this. This makes doing such allocations
>> quite annoying, and I reckon we will be seeing a fair bit of them in
>> many places.
>>
>> The key observation here is that most file handlers need to allocate a
>> bunch of preserved memory, but they usually can free it all at once. So
>> we can get away with a very simple allocator that hands out memory
>> linearly.
>>
>> I have sketched out some code for this, take a look at the patch below.
>> Note that it is only compile tested.
>>
>> Perhaps give it a try and see if it does the job better.
>
> I agree the manual packing in memfd_luo_ser_init() isn't great, but I'm
> not sure we need an arena. This seems like something KHO slab support
> will solve properly, and since kho_alloc_preserve() already takes a
> size, KHO slab can be dropped in underneath it later without changing
> any callers.
While it would be nice to have, I peeked at what we need for slab
support and it sounds overkill. For one, a slab cache is for fixed size
objects. So you'd need a bucket of slabs for different sizes, similar to
what kmalloc() does. You probably don't want to preserve objects spread
around in kmalloc's buckets, so you would need a set of buckets
dedicated for LUO/KHO.
For another, slab cache is great when you are allocating and freeing a
bunch of objects of varying sizes. But that isn't really the workload
preservation has. Each preservation hook needs to allocate a handful of
objects, and then hold them until unpreserve. This arena style allocator
does the job pretty well.
And finally, the object layout within a page/slab of the cache can
change based on configs or kernel versions. See SLAB_RED_ZONE or KASAN
for example. We would need to standardize the layout without
compromising the non-KHO slab users. We can probably solve these
problems, but I don't expect it to be easy.
So I wouldn't want to support slab until we have file handlers that
churn through a lot of preserved objects.
>
> Until then, maybe we should just allocate struct kho_vmalloc separately
> with kho_alloc_preserve() and accept wasting a folio per object. For
> memfd that's one extra page per preserved file.
Yeah, that is fine too I suppose, at least until we start to run into
problems with the memory overhead. I think the arena style allocator is
fairly straightforward and makes the memory savings easy so to me it
sounds promising.
--
Regards,
Pratyush Yadav