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.
Showing the changes to include/linux (1 of 2 files). Show all
Unified · Split
| @@ -324,7 +324,7 @@ static inline void pgdat_resize_init(struct pglist_data *pgdat) {} · 323 unmodified lines | |||
| 324 | extern bool is_mem_section_removable(unsigned long pfn, unsigned long nr_pages); | 324 | extern bool is_mem_section_removable(unsigned long pfn, unsigned long nr_pages); |
| 325 | extern void try_offline_node(int nid); | 325 | extern void try_offline_node(int nid); |
| 326 | extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages); | 326 | 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); |
| 328 | extern void __remove_memory(int nid, u64 start, u64 size); | 328 | extern void __remove_memory(int nid, u64 start, u64 size); |
| 329 | 329 | ||
| 330 | #else | 330 | #else |
| @@ -341,7 +341,11 @@ static inline int offline_pages(unsigned long start_pfn, unsigned long nr_pages) · 10 unmodified lines | |||
| 341 | return -EINVAL; | 341 | return -EINVAL; |
| 342 | } | 342 | } |
| 343 | 343 | ||
| 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 | |||
| 345 | static inline void __remove_memory(int nid, u64 start, u64 size) {} | 349 | static inline void __remove_memory(int nid, u64 start, u64 size) {} |
| 346 | #endif /* CONFIG_MEMORY_HOTREMOVE */ | 350 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
| 347 | 351 | ||