Skip to content

Commit

Permalink
mm/hotplug: make remove_memory() interface usable
Browse files Browse the repository at this point in the history
Presently the remove_memory() interface is inherently broken.  It tries
to remove memory but panics if some memory is not offline.  The problem
is that it is impossible to ensure that all memory blocks are offline as
this function also takes lock_device_hotplug that is required to change
memory state via sysfs.

So, between calling this function and offlining all memory blocks there
is always a window when lock_device_hotplug is released, and therefore,
there is always a chance for a panic during this window.

Make this interface to return an error if memory removal fails.  This
way it is safe to call this function without panicking machine, and also
makes it symmetric to add_memory() which already returns an error.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Pavel Tatashin <[email protected]>
Reviewed-by: David Hildenbrand <[email protected]>
Acked-by: Michal Hocko <[email protected]>
Cc: Bjorn Helgaas <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dan Williams <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Dave Jiang <[email protected]>
Cc: Fengguang Wu <[email protected]>
Cc: Huang Ying <[email protected]>
Cc: James Morris <[email protected]>
Cc: Jérôme Glisse <[email protected]>
Cc: Keith Busch <[email protected]>
Cc: Ross Zwisler <[email protected]>
Cc: Sasha Levin <[email protected]>
Cc: Takashi Iwai <[email protected]>
Cc: Tom Lendacky <[email protected]>
Cc: Vishal Verma <[email protected]>
Cc: Yaowei Bai <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
soleen authored and torvalds committed Jul 17, 2019
1 parent 31e4ca9 commit eca499a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
8 changes: 6 additions & 2 deletions include/linux/memory_hotplug.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ static inline void pgdat_resize_init(struct pglist_data *pgdat) {}
extern bool is_mem_section_removable(unsigned long pfn, unsigned long nr_pages);
extern void try_offline_node(int nid);
extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages);
extern void remove_memory(int nid, u64 start, u64 size);
extern int remove_memory(int nid, u64 start, u64 size);
extern void __remove_memory(int nid, u64 start, u64 size);

#else
Expand All @@ -341,7 +341,11 @@ static inline int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
return -EINVAL;
}

static inline void remove_memory(int nid, u64 start, u64 size) {}
static inline int remove_memory(int nid, u64 start, u64 size)
{
return -EBUSY;
}

static inline void __remove_memory(int nid, u64 start, u64 size) {}
#endif /* CONFIG_MEMORY_HOTREMOVE */

Expand Down
64 changes: 43 additions & 21 deletions mm/memory_hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1734,9 +1734,10 @@ static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
&beginpa, &endpa);
}

return ret;
return -EBUSY;
}
return 0;
}

static int check_cpu_on_node(pg_data_t *pgdat)
Expand Down Expand Up @@ -1819,33 +1820,23 @@ static void __release_memory_resource(resource_size_t start,
}
}

/**
* remove_memory
* @nid: the node ID
* @start: physical address of the region to remove
* @size: size of the region to remove
*
* NOTE: The caller must call lock_device_hotplug() to serialize hotplug
* and online/offline operations before this call, as required by
* try_offline_node().
*/
void __ref __remove_memory(int nid, u64 start, u64 size)
static int __ref try_remove_memory(int nid, u64 start, u64 size)
{
int ret;
int rc = 0;

BUG_ON(check_hotplug_memory_range(start, size));

mem_hotplug_begin();

/*
* All memory blocks must be offlined before removing memory. Check
* whether all memory blocks in question are offline and trigger a BUG()
* whether all memory blocks in question are offline and return error
* if this is not the case.
*/
ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
check_memblock_offlined_cb);
if (ret)
BUG();
rc = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
check_memblock_offlined_cb);
if (rc)
goto done;

/* remove memmap entry */
firmware_map_remove(start, start + size, "System RAM");
Expand All @@ -1857,14 +1848,45 @@ void __ref __remove_memory(int nid, u64 start, u64 size)

try_offline_node(nid);

done:
mem_hotplug_done();
return rc;
}

void remove_memory(int nid, u64 start, u64 size)
/**
* remove_memory
* @nid: the node ID
* @start: physical address of the region to remove
* @size: size of the region to remove
*
* NOTE: The caller must call lock_device_hotplug() to serialize hotplug
* and online/offline operations before this call, as required by
* try_offline_node().
*/
void __remove_memory(int nid, u64 start, u64 size)
{

/*
* trigger BUG() is some memory is not offlined prior to calling this
* function
*/
if (try_remove_memory(nid, start, size))
BUG();
}

/*
* Remove memory if every memory block is offline, otherwise return -EBUSY is
* some memory is not offline
*/
int remove_memory(int nid, u64 start, u64 size)
{
int rc;

lock_device_hotplug();
__remove_memory(nid, start, size);
rc = try_remove_memory(nid, start, size);
unlock_device_hotplug();

return rc;
}
EXPORT_SYMBOL_GPL(remove_memory);
#endif /* CONFIG_MEMORY_HOTREMOVE */

0 comments on commit eca499a

Please sign in to comment.