Skip to content

Commit eca499a

Browse files
soleentorvalds
authored andcommitted
mm/hotplug: make remove_memory() interface usable
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/20190517215438.6487-3-pasha.tatashin@soleen.com Signed-off-by: Pavel Tatashin <pasha.tatashin@soleen.com> Reviewed-by: David Hildenbrand <david@redhat.com> Acked-by: Michal Hocko <mhocko@suse.com> Cc: Bjorn Helgaas <bhelgaas@google.com> Cc: Borislav Petkov <bp@suse.de> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Dave Jiang <dave.jiang@intel.com> Cc: Fengguang Wu <fengguang.wu@intel.com> Cc: Huang Ying <ying.huang@intel.com> Cc: James Morris <jmorris@namei.org> Cc: Jérôme Glisse <jglisse@redhat.com> Cc: Keith Busch <keith.busch@intel.com> Cc: Ross Zwisler <zwisler@kernel.org> Cc: Sasha Levin <sashal@kernel.org> Cc: Takashi Iwai <tiwai@suse.de> Cc: Tom Lendacky <thomas.lendacky@amd.com> Cc: Vishal Verma <vishal.l.verma@intel.com> Cc: Yaowei Bai <baiyaowei@cmss.chinamobile.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 31e4ca9 commit eca499a

2 files changed

Lines changed: 49 additions & 23 deletions

File tree

‎include/linux/memory_hotplug.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ static inline void pgdat_resize_init(struct pglist_data *pgdat) {}
324324
extern bool is_mem_section_removable(unsigned long pfn, unsigned long nr_pages);
325325
extern void try_offline_node(int nid);
326326
extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages);
327-
extern void remove_memory(int nid, u64 start, u64 size);
327+
extern int remove_memory(int nid, u64 start, u64 size);
328328
extern void __remove_memory(int nid, u64 start, u64 size);
329329

330330
#else
@@ -341,7 +341,11 @@ static inline int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
341341
return -EINVAL;
342342
}
343343

344-
static inline void remove_memory(int nid, u64 start, u64 size) {}
344+
static inline int remove_memory(int nid, u64 start, u64 size)
345+
{
346+
return -EBUSY;
347+
}
348+
345349
static inline void __remove_memory(int nid, u64 start, u64 size) {}
346350
#endif /* CONFIG_MEMORY_HOTREMOVE */
347351

‎mm/memory_hotplug.c‎

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,9 +1734,10 @@ static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
17341734
endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
17351735
pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
17361736
&beginpa, &endpa);
1737-
}
17381737

1739-
return ret;
1738+
return -EBUSY;
1739+
}
1740+
return 0;
17401741
}
17411742

17421743
static int check_cpu_on_node(pg_data_t *pgdat)
@@ -1819,33 +1820,23 @@ static void __release_memory_resource(resource_size_t start,
18191820
}
18201821
}
18211822

1822-
/**
1823-
* remove_memory
1824-
* @nid: the node ID
1825-
* @start: physical address of the region to remove
1826-
* @size: size of the region to remove
1827-
*
1828-
* NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1829-
* and online/offline operations before this call, as required by
1830-
* try_offline_node().
1831-
*/
1832-
void __ref __remove_memory(int nid, u64 start, u64 size)
1823+
static int __ref try_remove_memory(int nid, u64 start, u64 size)
18331824
{
1834-
int ret;
1825+
int rc = 0;
18351826

18361827
BUG_ON(check_hotplug_memory_range(start, size));
18371828

18381829
mem_hotplug_begin();
18391830

18401831
/*
18411832
* All memory blocks must be offlined before removing memory. Check
1842-
* whether all memory blocks in question are offline and trigger a BUG()
1833+
* whether all memory blocks in question are offline and return error
18431834
* if this is not the case.
18441835
*/
1845-
ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
1846-
check_memblock_offlined_cb);
1847-
if (ret)
1848-
BUG();
1836+
rc = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
1837+
check_memblock_offlined_cb);
1838+
if (rc)
1839+
goto done;
18491840

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

18581849
try_offline_node(nid);
18591850

1851+
done:
18601852
mem_hotplug_done();
1853+
return rc;
18611854
}
18621855

1863-
void remove_memory(int nid, u64 start, u64 size)
1856+
/**
1857+
* remove_memory
1858+
* @nid: the node ID
1859+
* @start: physical address of the region to remove
1860+
* @size: size of the region to remove
1861+
*
1862+
* NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1863+
* and online/offline operations before this call, as required by
1864+
* try_offline_node().
1865+
*/
1866+
void __remove_memory(int nid, u64 start, u64 size)
1867+
{
1868+
1869+
/*
1870+
* trigger BUG() is some memory is not offlined prior to calling this
1871+
* function
1872+
*/
1873+
if (try_remove_memory(nid, start, size))
1874+
BUG();
1875+
}
1876+
1877+
/*
1878+
* Remove memory if every memory block is offline, otherwise return -EBUSY is
1879+
* some memory is not offline
1880+
*/
1881+
int remove_memory(int nid, u64 start, u64 size)
18641882
{
1883+
int rc;
1884+
18651885
lock_device_hotplug();
1866-
__remove_memory(nid, start, size);
1886+
rc = try_remove_memory(nid, start, size);
18671887
unlock_device_hotplug();
1888+
1889+
return rc;
18681890
}
18691891
EXPORT_SYMBOL_GPL(remove_memory);
18701892
#endif /* CONFIG_MEMORY_HOTREMOVE */

0 commit comments

Comments
 (0)