Skip to content

Commit 47118af

Browse files
mina86mszyprow
authored andcommitted
mm: mmzone: MIGRATE_CMA migration type added
The MIGRATE_CMA migration type has two main characteristics: (i) only movable pages can be allocated from MIGRATE_CMA pageblocks and (ii) page allocator will never change migration type of MIGRATE_CMA pageblocks. This guarantees (to some degree) that page in a MIGRATE_CMA page block can always be migrated somewhere else (unless there's no memory left in the system). It is designed to be used for allocating big chunks (eg. 10MiB) of physically contiguous memory. Once driver requests contiguous memory, pages from MIGRATE_CMA pageblocks may be migrated away to create a contiguous block. To minimise number of migrations, MIGRATE_CMA migration type is the last type tried when page allocator falls back to other migration types when requested. Signed-off-by: Michal Nazarewicz <mina86@mina86.com> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com> Acked-by: Mel Gorman <mel@csn.ul.ie> Reviewed-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Tested-by: Rob Clark <rob.clark@linaro.org> Tested-by: Ohad Ben-Cohen <ohad@wizery.com> Tested-by: Benjamin Gaignard <benjamin.gaignard@linaro.org> Tested-by: Robert Nelson <robertcnelson@gmail.com> Tested-by: Barry Song <Baohua.Song@csr.com>
1 parent 6d4a491 commit 47118af

6 files changed

Lines changed: 106 additions & 27 deletions

File tree

‎include/linux/gfp.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ static inline bool pm_suspended_storage(void)
397397
extern int alloc_contig_range(unsigned long start, unsigned long end);
398398
extern void free_contig_range(unsigned long pfn, unsigned nr_pages);
399399

400+
/* CMA stuff */
401+
extern void init_cma_reserved_pageblock(struct page *page);
402+
400403
#endif
401404

402405
#endif /* __LINUX_GFP_H */

‎include/linux/mmzone.h‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,37 @@
3535
*/
3636
#define PAGE_ALLOC_COSTLY_ORDER 3
3737

38-
#define MIGRATE_UNMOVABLE 0
39-
#define MIGRATE_RECLAIMABLE 1
40-
#define MIGRATE_MOVABLE 2
41-
#define MIGRATE_PCPTYPES 3 /* the number of types on the pcp lists */
42-
#define MIGRATE_RESERVE 3
43-
#define MIGRATE_ISOLATE 4 /* can't allocate from here */
44-
#define MIGRATE_TYPES 5
38+
enum {
39+
MIGRATE_UNMOVABLE,
40+
MIGRATE_RECLAIMABLE,
41+
MIGRATE_MOVABLE,
42+
MIGRATE_PCPTYPES, /* the number of types on the pcp lists */
43+
MIGRATE_RESERVE = MIGRATE_PCPTYPES,
44+
#ifdef CONFIG_CMA
45+
/*
46+
* MIGRATE_CMA migration type is designed to mimic the way
47+
* ZONE_MOVABLE works. Only movable pages can be allocated
48+
* from MIGRATE_CMA pageblocks and page allocator never
49+
* implicitly change migration type of MIGRATE_CMA pageblock.
50+
*
51+
* The way to use it is to change migratetype of a range of
52+
* pageblocks to MIGRATE_CMA which can be done by
53+
* __free_pageblock_cma() function. What is important though
54+
* is that a range of pageblocks must be aligned to
55+
* MAX_ORDER_NR_PAGES should biggest page be bigger then
56+
* a single pageblock.
57+
*/
58+
MIGRATE_CMA,
59+
#endif
60+
MIGRATE_ISOLATE, /* can't allocate from here */
61+
MIGRATE_TYPES
62+
};
63+
64+
#ifdef CONFIG_CMA
65+
# define is_migrate_cma(migratetype) unlikely((migratetype) == MIGRATE_CMA)
66+
#else
67+
# define is_migrate_cma(migratetype) false
68+
#endif
4569

4670
#define for_each_migratetype_order(order, type) \
4771
for (order = 0; order < MAX_ORDER; order++) \

‎mm/Kconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ config COMPACTION
198198
config MIGRATION
199199
bool "Page migration"
200200
def_bool y
201-
depends on NUMA || ARCH_ENABLE_MEMORY_HOTREMOVE || COMPACTION
201+
depends on NUMA || ARCH_ENABLE_MEMORY_HOTREMOVE || COMPACTION || CMA
202202
help
203203
Allows the migration of the physical location of pages of processes
204204
while the virtual addresses are not changed. This is useful in

‎mm/compaction.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ static void map_pages(struct list_head *list)
4545
}
4646
}
4747

48+
static inline bool migrate_async_suitable(int migratetype)
49+
{
50+
return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51+
}
52+
4853
/*
4954
* Isolate free pages onto a private freelist. Caller must hold zone->lock.
5055
* If @strict is true, will abort returning 0 on any invalid PFNs or non-free
@@ -299,7 +304,7 @@ isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
299304
*/
300305
pageblock_nr = low_pfn >> pageblock_order;
301306
if (!cc->sync && last_pageblock_nr != pageblock_nr &&
302-
get_pageblock_migratetype(page) != MIGRATE_MOVABLE) {
307+
!migrate_async_suitable(get_pageblock_migratetype(page))) {
303308
low_pfn += pageblock_nr_pages;
304309
low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
305310
last_pageblock_nr = pageblock_nr;
@@ -367,8 +372,8 @@ static bool suitable_migration_target(struct page *page)
367372
if (PageBuddy(page) && page_order(page) >= pageblock_order)
368373
return true;
369374

370-
/* If the block is MIGRATE_MOVABLE, allow migration */
371-
if (migratetype == MIGRATE_MOVABLE)
375+
/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
376+
if (migrate_async_suitable(migratetype))
372377
return true;
373378

374379
/* Otherwise skip the block */

‎mm/page_alloc.c‎

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,24 @@ void __meminit __free_pages_bootmem(struct page *page, unsigned int order)
750750
__free_pages(page, order);
751751
}
752752

753+
#ifdef CONFIG_CMA
754+
/* Free whole pageblock and set it's migration type to MIGRATE_CMA. */
755+
void __init init_cma_reserved_pageblock(struct page *page)
756+
{
757+
unsigned i = pageblock_nr_pages;
758+
struct page *p = page;
759+
760+
do {
761+
__ClearPageReserved(p);
762+
set_page_count(p, 0);
763+
} while (++p, --i);
764+
765+
set_page_refcounted(page);
766+
set_pageblock_migratetype(page, MIGRATE_CMA);
767+
__free_pages(page, pageblock_order);
768+
totalram_pages += pageblock_nr_pages;
769+
}
770+
#endif
753771

754772
/*
755773
* The order of subdivision here is critical for the IO subsystem.
@@ -875,10 +893,15 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
875893
* This array describes the order lists are fallen back to when
876894
* the free lists for the desirable migrate type are depleted
877895
*/
878-
static int fallbacks[MIGRATE_TYPES][3] = {
879-
[MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
880-
[MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
881-
[MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
896+
static int fallbacks[MIGRATE_TYPES][4] = {
897+
[MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
898+
[MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
899+
#ifdef CONFIG_CMA
900+
[MIGRATE_MOVABLE] = { MIGRATE_CMA, MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
901+
[MIGRATE_CMA] = { MIGRATE_RESERVE }, /* Never used */
902+
#else
903+
[MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
904+
#endif
882905
[MIGRATE_RESERVE] = { MIGRATE_RESERVE }, /* Never used */
883906
[MIGRATE_ISOLATE] = { MIGRATE_RESERVE }, /* Never used */
884907
};
@@ -995,11 +1018,18 @@ __rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
9951018
* pages to the preferred allocation list. If falling
9961019
* back for a reclaimable kernel allocation, be more
9971020
* aggressive about taking ownership of free pages
1021+
*
1022+
* On the other hand, never change migration
1023+
* type of MIGRATE_CMA pageblocks nor move CMA
1024+
* pages on different free lists. We don't
1025+
* want unmovable pages to be allocated from
1026+
* MIGRATE_CMA areas.
9981027
*/
999-
if (unlikely(current_order >= (pageblock_order >> 1)) ||
1000-
start_migratetype == MIGRATE_RECLAIMABLE ||
1001-
page_group_by_mobility_disabled) {
1002-
unsigned long pages;
1028+
if (!is_migrate_cma(migratetype) &&
1029+
(unlikely(current_order >= pageblock_order / 2) ||
1030+
start_migratetype == MIGRATE_RECLAIMABLE ||
1031+
page_group_by_mobility_disabled)) {
1032+
int pages;
10031033
pages = move_freepages_block(zone, page,
10041034
start_migratetype);
10051035

@@ -1017,11 +1047,14 @@ __rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
10171047
rmv_page_order(page);
10181048

10191049
/* Take ownership for orders >= pageblock_order */
1020-
if (current_order >= pageblock_order)
1050+
if (current_order >= pageblock_order &&
1051+
!is_migrate_cma(migratetype))
10211052
change_pageblock_range(page, current_order,
10221053
start_migratetype);
10231054

1024-
expand(zone, page, order, current_order, area, migratetype);
1055+
expand(zone, page, order, current_order, area,
1056+
is_migrate_cma(migratetype)
1057+
? migratetype : start_migratetype);
10251058

10261059
trace_mm_page_alloc_extfrag(page, order, current_order,
10271060
start_migratetype, migratetype);
@@ -1072,7 +1105,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
10721105
unsigned long count, struct list_head *list,
10731106
int migratetype, int cold)
10741107
{
1075-
int i;
1108+
int mt = migratetype, i;
10761109

10771110
spin_lock(&zone->lock);
10781111
for (i = 0; i < count; ++i) {
@@ -1093,7 +1126,12 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
10931126
list_add(&page->lru, list);
10941127
else
10951128
list_add_tail(&page->lru, list);
1096-
set_page_private(page, migratetype);
1129+
if (IS_ENABLED(CONFIG_CMA)) {
1130+
mt = get_pageblock_migratetype(page);
1131+
if (!is_migrate_cma(mt) && mt != MIGRATE_ISOLATE)
1132+
mt = migratetype;
1133+
}
1134+
set_page_private(page, mt);
10971135
list = &page->lru;
10981136
}
10991137
__mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
@@ -1373,8 +1411,12 @@ int split_free_page(struct page *page)
13731411

13741412
if (order >= pageblock_order - 1) {
13751413
struct page *endpage = page + (1 << order) - 1;
1376-
for (; page < endpage; page += pageblock_nr_pages)
1377-
set_pageblock_migratetype(page, MIGRATE_MOVABLE);
1414+
for (; page < endpage; page += pageblock_nr_pages) {
1415+
int mt = get_pageblock_migratetype(page);
1416+
if (mt != MIGRATE_ISOLATE && !is_migrate_cma(mt))
1417+
set_pageblock_migratetype(page,
1418+
MIGRATE_MOVABLE);
1419+
}
13781420
}
13791421

13801422
return 1 << order;
@@ -5414,14 +5456,16 @@ static int
54145456
__count_immobile_pages(struct zone *zone, struct page *page, int count)
54155457
{
54165458
unsigned long pfn, iter, found;
5459+
int mt;
5460+
54175461
/*
54185462
* For avoiding noise data, lru_add_drain_all() should be called
54195463
* If ZONE_MOVABLE, the zone never contains immobile pages
54205464
*/
54215465
if (zone_idx(zone) == ZONE_MOVABLE)
54225466
return true;
5423-
5424-
if (get_pageblock_migratetype(page) == MIGRATE_MOVABLE)
5467+
mt = get_pageblock_migratetype(page);
5468+
if (mt == MIGRATE_MOVABLE || is_migrate_cma(mt))
54255469
return true;
54265470

54275471
pfn = page_to_pfn(page);

‎mm/vmstat.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ static char * const migratetype_names[MIGRATE_TYPES] = {
613613
"Reclaimable",
614614
"Movable",
615615
"Reserve",
616+
#ifdef CONFIG_CMA
617+
"CMA",
618+
#endif
616619
"Isolate",
617620
};
618621

0 commit comments

Comments
 (0)