Skip to content

Commit ee6f509

Browse files
minchanktorvalds
authored andcommitted
mm: factor out memory isolate functions
mm/page_alloc.c has some memory isolation functions but they are used only when we enable CONFIG_{CMA|MEMORY_HOTPLUG|MEMORY_FAILURE}. So let's make it configurable by new CONFIG_MEMORY_ISOLATION so that it can reduce binary size and we can check it simple by CONFIG_MEMORY_ISOLATION, not if defined CONFIG_{CMA|MEMORY_HOTPLUG|MEMORY_FAILURE}. Signed-off-by: Minchan Kim <minchan@kernel.org> Cc: Andi Kleen <andi@firstfloor.org> Cc: Marek Szyprowski <m.szyprowski@samsung.com> Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: Mel Gorman <mgorman@suse.de> Cc: Michal Hocko <mhocko@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 876aafb commit ee6f509

File tree

6 files changed

+93
-82
lines changed

6 files changed

+93
-82
lines changed

drivers/base/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ config CMA
196196
bool "Contiguous Memory Allocator (EXPERIMENTAL)"
197197
depends on HAVE_DMA_CONTIGUOUS && HAVE_MEMBLOCK && EXPERIMENTAL
198198
select MIGRATION
199+
select MEMORY_ISOLATION
199200
help
200201
This enables the Contiguous Memory Allocator which allows drivers
201202
to allocate big physically-contiguous blocks of memory for use with

include/linux/page-isolation.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#ifndef __LINUX_PAGEISOLATION_H
22
#define __LINUX_PAGEISOLATION_H
33

4+
5+
bool has_unmovable_pages(struct zone *zone, struct page *page, int count);
6+
void set_pageblock_migratetype(struct page *page, int migratetype);
7+
int move_freepages_block(struct zone *zone, struct page *page,
8+
int migratetype);
49
/*
510
* Changes migrate type in [start_pfn, end_pfn) to be MIGRATE_ISOLATE.
611
* If specified range includes migrate types other than MOVABLE or CMA,
@@ -10,15 +15,15 @@
1015
* free all pages in the range. test_page_isolated() can be used for
1116
* test it.
1217
*/
13-
extern int
18+
int
1419
start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
1520
unsigned migratetype);
1621

1722
/*
1823
* Changes MIGRATE_ISOLATE to MIGRATE_MOVABLE.
1924
* target range is [start_pfn, end_pfn)
2025
*/
21-
extern int
26+
int
2227
undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
2328
unsigned migratetype);
2429

@@ -30,8 +35,8 @@ int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn);
3035
/*
3136
* Internal functions. Changes pageblock's migrate type.
3237
*/
33-
extern int set_migratetype_isolate(struct page *page);
34-
extern void unset_migratetype_isolate(struct page *page, unsigned migratetype);
38+
int set_migratetype_isolate(struct page *page);
39+
void unset_migratetype_isolate(struct page *page, unsigned migratetype);
3540

3641

3742
#endif

mm/Kconfig

+5
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ config ARCH_DISCARD_MEMBLOCK
140140
config NO_BOOTMEM
141141
boolean
142142

143+
config MEMORY_ISOLATION
144+
boolean
145+
143146
# eventually, we can have this option just 'select SPARSEMEM'
144147
config MEMORY_HOTPLUG
145148
bool "Allow for memory hot-add"
149+
select MEMORY_ISOLATION
146150
depends on SPARSEMEM || X86_64_ACPI_NUMA
147151
depends on HOTPLUG && ARCH_ENABLE_MEMORY_HOTPLUG
148152
depends on (IA64 || X86 || PPC_BOOK3S_64 || SUPERH || S390)
@@ -272,6 +276,7 @@ config MEMORY_FAILURE
272276
depends on MMU
273277
depends on ARCH_SUPPORTS_MEMORY_FAILURE
274278
bool "Enable recovery from hardware memory errors"
279+
select MEMORY_ISOLATION
275280
help
276281
Enables code to recover from some memory failures on systems
277282
with MCA recovery. This allows a system to continue running

mm/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
1515
maccess.o page_alloc.o page-writeback.o \
1616
readahead.o swap.o truncate.o vmscan.o shmem.o \
1717
prio_tree.o util.o mmzone.o vmstat.o backing-dev.o \
18-
page_isolation.o mm_init.o mmu_context.o percpu.o \
19-
compaction.o slab_common.o $(mmu-y)
18+
mm_init.o mmu_context.o percpu.o slab_common.o \
19+
compaction.o $(mmu-y)
2020

2121
obj-y += init-mm.o
2222

@@ -56,3 +56,4 @@ obj-$(CONFIG_HWPOISON_INJECT) += hwpoison-inject.o
5656
obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o
5757
obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o
5858
obj-$(CONFIG_CLEANCACHE) += cleancache.o
59+
obj-$(CONFIG_MEMORY_ISOLATION) += page_isolation.o

mm/page_alloc.c

+4-76
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#include <linux/page_cgroup.h>
5252
#include <linux/debugobjects.h>
5353
#include <linux/kmemleak.h>
54-
#include <linux/memory.h>
5554
#include <linux/compaction.h>
5655
#include <trace/events/kmem.h>
5756
#include <linux/ftrace_event.h>
@@ -219,7 +218,7 @@ EXPORT_SYMBOL(nr_online_nodes);
219218

220219
int page_group_by_mobility_disabled __read_mostly;
221220

222-
static void set_pageblock_migratetype(struct page *page, int migratetype)
221+
void set_pageblock_migratetype(struct page *page, int migratetype)
223222
{
224223

225224
if (unlikely(page_group_by_mobility_disabled))
@@ -954,7 +953,7 @@ static int move_freepages(struct zone *zone,
954953
return pages_moved;
955954
}
956955

957-
static int move_freepages_block(struct zone *zone, struct page *page,
956+
int move_freepages_block(struct zone *zone, struct page *page,
958957
int migratetype)
959958
{
960959
unsigned long start_pfn, end_pfn;
@@ -5463,8 +5462,7 @@ void set_pageblock_flags_group(struct page *page, unsigned long flags,
54635462
* MIGRATE_MOVABLE block might include unmovable pages. It means you can't
54645463
* expect this function should be exact.
54655464
*/
5466-
static bool
5467-
__has_unmovable_pages(struct zone *zone, struct page *page, int count)
5465+
bool has_unmovable_pages(struct zone *zone, struct page *page, int count)
54685466
{
54695467
unsigned long pfn, iter, found;
54705468
int mt;
@@ -5541,77 +5539,7 @@ bool is_pageblock_removable_nolock(struct page *page)
55415539
zone->zone_start_pfn + zone->spanned_pages <= pfn)
55425540
return false;
55435541

5544-
return !__has_unmovable_pages(zone, page, 0);
5545-
}
5546-
5547-
int set_migratetype_isolate(struct page *page)
5548-
{
5549-
struct zone *zone;
5550-
unsigned long flags, pfn;
5551-
struct memory_isolate_notify arg;
5552-
int notifier_ret;
5553-
int ret = -EBUSY;
5554-
5555-
zone = page_zone(page);
5556-
5557-
spin_lock_irqsave(&zone->lock, flags);
5558-
5559-
pfn = page_to_pfn(page);
5560-
arg.start_pfn = pfn;
5561-
arg.nr_pages = pageblock_nr_pages;
5562-
arg.pages_found = 0;
5563-
5564-
/*
5565-
* It may be possible to isolate a pageblock even if the
5566-
* migratetype is not MIGRATE_MOVABLE. The memory isolation
5567-
* notifier chain is used by balloon drivers to return the
5568-
* number of pages in a range that are held by the balloon
5569-
* driver to shrink memory. If all the pages are accounted for
5570-
* by balloons, are free, or on the LRU, isolation can continue.
5571-
* Later, for example, when memory hotplug notifier runs, these
5572-
* pages reported as "can be isolated" should be isolated(freed)
5573-
* by the balloon driver through the memory notifier chain.
5574-
*/
5575-
notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
5576-
notifier_ret = notifier_to_errno(notifier_ret);
5577-
if (notifier_ret)
5578-
goto out;
5579-
/*
5580-
* FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
5581-
* We just check MOVABLE pages.
5582-
*/
5583-
if (!__has_unmovable_pages(zone, page, arg.pages_found))
5584-
ret = 0;
5585-
/*
5586-
* Unmovable means "not-on-lru" pages. If Unmovable pages are
5587-
* larger than removable-by-driver pages reported by notifier,
5588-
* we'll fail.
5589-
*/
5590-
5591-
out:
5592-
if (!ret) {
5593-
set_pageblock_migratetype(page, MIGRATE_ISOLATE);
5594-
move_freepages_block(zone, page, MIGRATE_ISOLATE);
5595-
}
5596-
5597-
spin_unlock_irqrestore(&zone->lock, flags);
5598-
if (!ret)
5599-
drain_all_pages();
5600-
return ret;
5601-
}
5602-
5603-
void unset_migratetype_isolate(struct page *page, unsigned migratetype)
5604-
{
5605-
struct zone *zone;
5606-
unsigned long flags;
5607-
zone = page_zone(page);
5608-
spin_lock_irqsave(&zone->lock, flags);
5609-
if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
5610-
goto out;
5611-
set_pageblock_migratetype(page, migratetype);
5612-
move_freepages_block(zone, page, migratetype);
5613-
out:
5614-
spin_unlock_irqrestore(&zone->lock, flags);
5542+
return !has_unmovable_pages(zone, page, 0);
56155543
}
56165544

56175545
#ifdef CONFIG_CMA

mm/page_isolation.c

+71
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,79 @@
55
#include <linux/mm.h>
66
#include <linux/page-isolation.h>
77
#include <linux/pageblock-flags.h>
8+
#include <linux/memory.h>
89
#include "internal.h"
910

11+
int set_migratetype_isolate(struct page *page)
12+
{
13+
struct zone *zone;
14+
unsigned long flags, pfn;
15+
struct memory_isolate_notify arg;
16+
int notifier_ret;
17+
int ret = -EBUSY;
18+
19+
zone = page_zone(page);
20+
21+
spin_lock_irqsave(&zone->lock, flags);
22+
23+
pfn = page_to_pfn(page);
24+
arg.start_pfn = pfn;
25+
arg.nr_pages = pageblock_nr_pages;
26+
arg.pages_found = 0;
27+
28+
/*
29+
* It may be possible to isolate a pageblock even if the
30+
* migratetype is not MIGRATE_MOVABLE. The memory isolation
31+
* notifier chain is used by balloon drivers to return the
32+
* number of pages in a range that are held by the balloon
33+
* driver to shrink memory. If all the pages are accounted for
34+
* by balloons, are free, or on the LRU, isolation can continue.
35+
* Later, for example, when memory hotplug notifier runs, these
36+
* pages reported as "can be isolated" should be isolated(freed)
37+
* by the balloon driver through the memory notifier chain.
38+
*/
39+
notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
40+
notifier_ret = notifier_to_errno(notifier_ret);
41+
if (notifier_ret)
42+
goto out;
43+
/*
44+
* FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
45+
* We just check MOVABLE pages.
46+
*/
47+
if (!has_unmovable_pages(zone, page, arg.pages_found))
48+
ret = 0;
49+
50+
/*
51+
* immobile means "not-on-lru" paes. If immobile is larger than
52+
* removable-by-driver pages reported by notifier, we'll fail.
53+
*/
54+
55+
out:
56+
if (!ret) {
57+
set_pageblock_migratetype(page, MIGRATE_ISOLATE);
58+
move_freepages_block(zone, page, MIGRATE_ISOLATE);
59+
}
60+
61+
spin_unlock_irqrestore(&zone->lock, flags);
62+
if (!ret)
63+
drain_all_pages();
64+
return ret;
65+
}
66+
67+
void unset_migratetype_isolate(struct page *page, unsigned migratetype)
68+
{
69+
struct zone *zone;
70+
unsigned long flags;
71+
zone = page_zone(page);
72+
spin_lock_irqsave(&zone->lock, flags);
73+
if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
74+
goto out;
75+
set_pageblock_migratetype(page, migratetype);
76+
move_freepages_block(zone, page, migratetype);
77+
out:
78+
spin_unlock_irqrestore(&zone->lock, flags);
79+
}
80+
1081
static inline struct page *
1182
__first_valid_page(unsigned long pfn, unsigned long nr_pages)
1283
{

0 commit comments

Comments
 (0)