Skip to content

Commit 5c755e9

Browse files
Badari Pulavartytorvalds
authored andcommitted
memory-hotplug: add sysfs removable attribute for hotplug memory remove
Memory may be hot-removed on a per-memory-block basis, particularly on POWER where the SPARSEMEM section size often matches the memory-block size. A user-level agent must be able to identify which sections of memory are likely to be removable before attempting the potentially expensive operation. This patch adds a file called "removable" to the memory directory in sysfs to help such an agent. In this patch, a memory block is considered removable if; o It contains only MOVABLE pageblocks o It contains only pageblocks with free pages regardless of pageblock type On the other hand, a memory block starting with a PageReserved() page will never be considered removable. Without this patch, the user-agent is forced to choose a memory block to remove randomly. Sample output of the sysfs files: ./memory/memory0/removable: 0 ./memory/memory1/removable: 0 ./memory/memory2/removable: 0 ./memory/memory3/removable: 0 ./memory/memory4/removable: 0 ./memory/memory5/removable: 0 ./memory/memory6/removable: 0 ./memory/memory7/removable: 1 ./memory/memory8/removable: 0 ./memory/memory9/removable: 0 ./memory/memory10/removable: 0 ./memory/memory11/removable: 0 ./memory/memory12/removable: 0 ./memory/memory13/removable: 0 ./memory/memory14/removable: 0 ./memory/memory15/removable: 0 ./memory/memory16/removable: 0 ./memory/memory17/removable: 1 ./memory/memory18/removable: 1 ./memory/memory19/removable: 1 ./memory/memory20/removable: 1 ./memory/memory21/removable: 1 ./memory/memory22/removable: 1 Signed-off-by: Badari Pulavarty <[email protected]> Signed-off-by: Mel Gorman <[email protected]> Acked-by: KAMEZAWA Hiroyuki <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 2f7f24e commit 5c755e9

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
What: /sys/devices/system/memory
2+
Date: June 2008
3+
Contact: Badari Pulavarty <[email protected]>
4+
Description:
5+
The /sys/devices/system/memory contains a snapshot of the
6+
internal state of the kernel memory blocks. Files could be
7+
added or removed dynamically to represent hot-add/remove
8+
operations.
9+
10+
Users: hotplug memory add/remove tools
11+
https://w3.opensource.ibm.com/projects/powerpc-utils/
12+
13+
What: /sys/devices/system/memory/memoryX/removable
14+
Date: June 2008
15+
Contact: Badari Pulavarty <[email protected]>
16+
Description:
17+
The file /sys/devices/system/memory/memoryX/removable
18+
indicates whether this memory block is removable or not.
19+
This is useful for a user-level agent to determine
20+
identify removable sections of the memory before attempting
21+
potentially expensive hot-remove memory operation
22+
23+
Users: hotplug memory remove tools
24+
https://w3.opensource.ibm.com/projects/powerpc-utils/

drivers/base/memory.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ static ssize_t show_mem_phys_index(struct sys_device *dev,
100100
return sprintf(buf, "%08lx\n", mem->phys_index);
101101
}
102102

103+
/*
104+
* Show whether the section of memory is likely to be hot-removable
105+
*/
106+
static ssize_t show_mem_removable(struct sys_device *dev, char *buf)
107+
{
108+
unsigned long start_pfn;
109+
int ret;
110+
struct memory_block *mem =
111+
container_of(dev, struct memory_block, sysdev);
112+
113+
start_pfn = section_nr_to_pfn(mem->phys_index);
114+
ret = is_mem_section_removable(start_pfn, PAGES_PER_SECTION);
115+
return sprintf(buf, "%d\n", ret);
116+
}
117+
103118
/*
104119
* online, offline, going offline, etc.
105120
*/
@@ -262,6 +277,7 @@ static ssize_t show_phys_device(struct sys_device *dev,
262277
static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
263278
static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
264279
static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
280+
static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL);
265281

266282
#define mem_create_simple_file(mem, attr_name) \
267283
sysdev_create_file(&mem->sysdev, &attr_##attr_name)
@@ -350,6 +366,8 @@ static int add_memory_block(unsigned long node_id, struct mem_section *section,
350366
ret = mem_create_simple_file(mem, state);
351367
if (!ret)
352368
ret = mem_create_simple_file(mem, phys_device);
369+
if (!ret)
370+
ret = mem_create_simple_file(mem, removable);
353371

354372
return ret;
355373
}
@@ -394,6 +412,7 @@ int remove_memory_block(unsigned long node_id, struct mem_section *section,
394412
mem_remove_simple_file(mem, phys_index);
395413
mem_remove_simple_file(mem, state);
396414
mem_remove_simple_file(mem, phys_device);
415+
mem_remove_simple_file(mem, removable);
397416
unregister_memory(mem, section);
398417

399418
return 0;

include/linux/memory_hotplug.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ extern int walk_memory_resource(unsigned long start_pfn,
199199
unsigned long nr_pages, void *arg,
200200
int (*func)(unsigned long, unsigned long, void *));
201201

202+
#ifdef CONFIG_MEMORY_HOTREMOVE
203+
204+
extern int is_mem_section_removable(unsigned long pfn, unsigned long nr_pages);
205+
206+
#else
207+
static inline int is_mem_section_removable(unsigned long pfn,
208+
unsigned long nr_pages)
209+
{
210+
return 0;
211+
}
212+
#endif /* CONFIG_MEMORY_HOTREMOVE */
213+
202214
extern int add_memory(int nid, u64 start, u64 size);
203215
extern int arch_add_memory(int nid, u64 start, u64 size);
204216
extern int remove_memory(u64 start, u64 size);

mm/memory_hotplug.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,66 @@ int add_memory(int nid, u64 start, u64 size)
522522
EXPORT_SYMBOL_GPL(add_memory);
523523

524524
#ifdef CONFIG_MEMORY_HOTREMOVE
525+
/*
526+
* A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
527+
* set and the size of the free page is given by page_order(). Using this,
528+
* the function determines if the pageblock contains only free pages.
529+
* Due to buddy contraints, a free page at least the size of a pageblock will
530+
* be located at the start of the pageblock
531+
*/
532+
static inline int pageblock_free(struct page *page)
533+
{
534+
return PageBuddy(page) && page_order(page) >= pageblock_order;
535+
}
536+
537+
/* Return the start of the next active pageblock after a given page */
538+
static struct page *next_active_pageblock(struct page *page)
539+
{
540+
int pageblocks_stride;
541+
542+
/* Ensure the starting page is pageblock-aligned */
543+
BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
544+
545+
/* Move forward by at least 1 * pageblock_nr_pages */
546+
pageblocks_stride = 1;
547+
548+
/* If the entire pageblock is free, move to the end of free page */
549+
if (pageblock_free(page))
550+
pageblocks_stride += page_order(page) - pageblock_order;
551+
552+
return page + (pageblocks_stride * pageblock_nr_pages);
553+
}
554+
555+
/* Checks if this range of memory is likely to be hot-removable. */
556+
int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
557+
{
558+
int type;
559+
struct page *page = pfn_to_page(start_pfn);
560+
struct page *end_page = page + nr_pages;
561+
562+
/* Check the starting page of each pageblock within the range */
563+
for (; page < end_page; page = next_active_pageblock(page)) {
564+
type = get_pageblock_migratetype(page);
565+
566+
/*
567+
* A pageblock containing MOVABLE or free pages is considered
568+
* removable
569+
*/
570+
if (type != MIGRATE_MOVABLE && !pageblock_free(page))
571+
return 0;
572+
573+
/*
574+
* A pageblock starting with a PageReserved page is not
575+
* considered removable.
576+
*/
577+
if (PageReserved(page))
578+
return 0;
579+
}
580+
581+
/* All pageblocks in the memory block are likely to be hot-removable */
582+
return 1;
583+
}
584+
525585
/*
526586
* Confirm all pages in a range [start, end) is belongs to the same zone.
527587
*/

0 commit comments

Comments
 (0)