Re: [PATCH] drivers/base/memory: make memory block get/put explicit

From: David Hildenbrand (Arm)

Date: Mon May 11 2026 - 08:22:39 EST


On 5/11/26 13:18, Muchun Song wrote:
> Rename the memory block lookup helper to make the acquired reference
> explicit, add memory_block_put() to wrap put_device(), and collapse the
> redundant section-number wrapper into a single block-id based lookup
> interface.
>
> This makes it clearer to callers that a successful lookup holds a
> reference that must be dropped, reducing the chance of forgetting the
> matching put and leaking the memory block device reference.

Better mention some of the other changes here, like removing find_memory_block().

[...]

> unlock_device_hotplug();
> diff --git a/include/linux/memory.h b/include/linux/memory.h
> index 5bb5599c6b2b..29edef1f975c 100644
> --- a/include/linux/memory.h
> +++ b/include/linux/memory.h
> @@ -158,7 +158,11 @@ int create_memory_block_devices(unsigned long start, unsigned long size,
> void remove_memory_block_devices(unsigned long start, unsigned long size);
> extern void memory_dev_init(void);
> extern int memory_notify(enum memory_block_state state, void *v);
> -extern struct memory_block *find_memory_block(unsigned long section_nr);
> +extern struct memory_block *memory_block_get(unsigned long block_id);

While at it, please drop the "extern".

> +static inline void memory_block_put(struct memory_block *mem)
> +{
> + put_device(&mem->dev);
> +}
> typedef int (*walk_memory_blocks_func_t)(struct memory_block *, void *);
> extern int walk_memory_blocks(unsigned long start, unsigned long size,
> void *arg, walk_memory_blocks_func_t func);
> @@ -171,7 +175,6 @@ struct memory_group *memory_group_find_by_id(int mgid);
> typedef int (*walk_memory_groups_func_t)(struct memory_group *, void *);
> int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,
> struct memory_group *excluded, void *arg);
> -struct memory_block *find_memory_block_by_id(unsigned long block_id);
> #define hotplug_memory_notifier(fn, pri) ({ \
> static __meminitdata struct notifier_block fn##_mem_nb =\
> { .notifier_call = fn, .priority = pri };\
> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
> index 462d8dcd636d..890c6453e887 100644
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> @@ -1417,14 +1417,13 @@ static void remove_memory_blocks_and_altmaps(u64 start, u64 size)
> struct vmem_altmap *altmap = NULL;
> struct memory_block *mem;
>
> - mem = find_memory_block(pfn_to_section_nr(PFN_DOWN(cur_start)));
> + mem = memory_block_get(phys_to_block_id(cur_start));
> if (WARN_ON_ONCE(!mem))
> continue;
>
> altmap = mem->altmap;
> mem->altmap = NULL;
> - /* drop the ref. we got via find_memory_block() */
> - put_device(&mem->dev);
> + memory_block_put(mem);

Would guards come in handy here?

In general

Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

--
Cheers,

David