Re: [PATCH v3 6/7] mm/memblock: Use KSTATE instead of kho to preserve preserved_mem_table

Andrey Ryabinin
2025-09-18T19:00:39Z

On 9/15/25 1:47 PM, Jason Gunthorpe wrote:
> On Tue, Sep 09, 2025 at 10:14:41PM +0200, Andrey Ryabinin wrote:
>> +static int kstate_preserve_phys(struct kstate_stream *stream, void *obj,
>> + const struct kstate_field *field)
>> +{
>> + struct reserve_mem_table *map = obj;
>> +
>> + return kho_preserve_phys(map->start, map->size);
>> +}
>> +
>> +struct kstate_description kstate_reserve_mem = {
>> + .name = "reserved_mem",
>> + .id = KSTATE_RESERVED_MEM_ID,
>> + .fields = (const struct kstate_field[]) {
>> + KSTATE_BASE_TYPE(name, struct reserve_mem_table,
>> + char[RESERVE_MEM_NAME_SIZE]),
>> + KSTATE_BASE_TYPE(start, struct reserve_mem_table, phys_addr_t),
>> + KSTATE_BASE_TYPE(size, struct reserve_mem_table, phys_addr_t),
>> + {
>> + .name = "phys_range",
>> + .flags = KS_CUSTOM,
>> + .save = kstate_preserve_phys,
>> + },
>> + KSTATE_END_OF_LIST(),
>> + },
>> +};
>>
>> static int __init reserve_mem_init(void)
>> {
>> int err;
>> + int i;
>>
>> if (!kho_is_enabled() || !reserved_mem_count)
>> return 0;
>>
>> + for (i = 0; i < reserved_mem_count; i++) {
>> + struct reserve_mem_table *map = &reserved_mem_table[i];
>>
>> + err = kstate_register(&kstate_reserve_mem,
>> + map, crc32(~0, map->name, RESERVE_MEM_NAME_SIZE));
>> + if (err)
>> + goto out;
>> }
>
> As I've said to the other proposals, this doesn't seem to be bringing
> that much value compared to just using a normal struct:

We expect to have many such ABI maps across the kernel.
These maps will share common elements - simple types, folios, and preserved
regions.

With the approach you're suggesting, we'd need to re-implement the same
preserve/unpreserve/recover logic, error handling, and unwind code for
every individual ABI map. That quickly becomes repetitive and error-prone.

By contrast, KSTATE centralizes this logic. It avoids duplicating code
and lets us express the preservation details declaratively instead
of re-implementing them per struct.

> for (i = 0; i < reserved_mem_count; i++) {
> struct reserve_mem_table *map = &reserved_mem_table[i];
> struct khoser_reserve_mem_table abi_map = {.name = map->name. .start = map->start, .size = map->size};
>
> err = kho_preserve_phys(map->start, map->size);
> if (err)
> return err; // Should unwind the other preservations!
>
> luo_preserve_key(luo_obj, map->name, &abi_map, sizeof(abi_map), VERSION_0);

On the versioning side:
With this approach, introducing a new ABI version (say, abi_map_v1)
would require us to maintain restore logic for each supported version,
and carefully handle upgrades between them.

With KSTATE, versioning is built in. For example, adding a new field can
simply be expressed as:
KSTATE_BASE_TYPE_V(new_field, struct reserve_mem_table, int, 1);
This way, the framework handles compatibility, and we don’t need to manually
write version-specific restore paths for each ABI map.