History

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.

Pavel Tatashin committed 7 years ago

Commit eca499a1 parent 31e4ca92 files · +49 −23Browse files

Unified · Split

2 files
  1. include/linux/memory_hotplug.h +6 −2
  2. mm/memory_hotplug.c +43 −21
include/linux/memory_hotplug.h+6−2
@@ -324,7 +324,7 @@ static inline void pgdat_resize_init(struct pglist_data *pgdat) {} · 323 unmodified lines
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) · 10 unmodified lines
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+43−21
@@ -1734,9 +1734,10 @@ static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) · 1733 unmodified lines
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,19 +1820,9 @@ static void __release_memory_resource(resource_size_t start, · 76 unmodified lines
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
@@ -1839,13 +1830,13 @@ void __ref __remove_memory(int nid, u64 start, u64 size) · 1 unmodified line
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) · 5 unmodified lines
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 */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t