Skip to content

Commit ee92d4d

Browse files
rientjesJiri Slaby
authored andcommitted
mm, migration: add destination page freeing callback
commit 68711a7 upstream. Memory migration uses a callback defined by the caller to determine how to allocate destination pages. When migration fails for a source page, however, it frees the destination page back to the system. This patch adds a memory migration callback defined by the caller to determine how to free destination pages. If a caller, such as memory compaction, builds its own freelist for migration targets, this can reuse already freed memory instead of scanning additional memory. If the caller provides a function to handle freeing of destination pages, it is called when page migration fails. If the caller passes NULL then freeing back to the system will be handled as usual. This patch introduces no functional change. Signed-off-by: David Rientjes <rientjes@google.com> Reviewed-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Acked-by: Mel Gorman <mgorman@suse.de> Acked-by: Vlastimil Babka <vbabka@suse.cz> Cc: Greg Thelen <gthelen@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Mel Gorman <mgorman@suse.de> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent e644c10 commit ee92d4d

7 files changed

Lines changed: 53 additions & 27 deletions

File tree

‎include/linux/migrate.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include <linux/mempolicy.h>
66
#include <linux/migrate_mode.h>
77

8-
typedef struct page *new_page_t(struct page *, unsigned long private, int **);
8+
typedef struct page *new_page_t(struct page *page, unsigned long private,
9+
int **reason);
10+
typedef void free_page_t(struct page *page, unsigned long private);
911

1012
/*
1113
* Return values from addresss_space_operations.migratepage():
@@ -39,7 +41,7 @@ extern void putback_lru_pages(struct list_head *l);
3941
extern void putback_movable_pages(struct list_head *l);
4042
extern int migrate_page(struct address_space *,
4143
struct page *, struct page *, enum migrate_mode);
42-
extern int migrate_pages(struct list_head *l, new_page_t x,
44+
extern int migrate_pages(struct list_head *l, new_page_t new, free_page_t free,
4345
unsigned long private, enum migrate_mode mode, int reason);
4446

4547
extern int fail_migrate_page(struct address_space *,
@@ -61,8 +63,9 @@ extern int migrate_page_move_mapping(struct address_space *mapping,
6163

6264
static inline void putback_lru_pages(struct list_head *l) {}
6365
static inline void putback_movable_pages(struct list_head *l) {}
64-
static inline int migrate_pages(struct list_head *l, new_page_t x,
65-
unsigned long private, enum migrate_mode mode, int reason)
66+
static inline int migrate_pages(struct list_head *l, new_page_t new,
67+
free_page_t free, unsigned long private, enum migrate_mode mode,
68+
int reason)
6669
{ return -ENOSYS; }
6770

6871
static inline int migrate_prep(void) { return -ENOSYS; }

‎mm/compaction.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ static int compact_zone(struct zone *zone, struct compact_control *cc)
10141014
}
10151015

10161016
nr_migrate = cc->nr_migratepages;
1017-
err = migrate_pages(&cc->migratepages, compaction_alloc,
1017+
err = migrate_pages(&cc->migratepages, compaction_alloc, NULL,
10181018
(unsigned long)cc,
10191019
cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC,
10201020
MR_COMPACTION);

‎mm/memory-failure.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ static int soft_offline_huge_page(struct page *page, int flags)
15541554

15551555
/* Keep page count to indicate a given hugepage is isolated. */
15561556
list_move(&hpage->lru, &pagelist);
1557-
ret = migrate_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL,
1557+
ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
15581558
MIGRATE_SYNC, MR_MEMORY_FAILURE);
15591559
if (ret) {
15601560
pr_info("soft offline: %#lx: migration failed %d, type %lx\n",
@@ -1635,7 +1635,7 @@ static int __soft_offline_page(struct page *page, int flags)
16351635
inc_zone_page_state(page, NR_ISOLATED_ANON +
16361636
page_is_file_cache(page));
16371637
list_add(&page->lru, &pagelist);
1638-
ret = migrate_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL,
1638+
ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
16391639
MIGRATE_SYNC, MR_MEMORY_FAILURE);
16401640
if (ret) {
16411641
putback_lru_pages(&pagelist);

‎mm/memory_hotplug.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
13211321
* alloc_migrate_target should be improooooved!!
13221322
* migrate_pages returns # of failed pages.
13231323
*/
1324-
ret = migrate_pages(&source, alloc_migrate_target, 0,
1324+
ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
13251325
MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
13261326
if (ret)
13271327
putback_movable_pages(&source);

‎mm/mempolicy.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ static int migrate_to_node(struct mm_struct *mm, int source, int dest,
10601060
flags | MPOL_MF_DISCONTIG_OK, &pagelist);
10611061

10621062
if (!list_empty(&pagelist)) {
1063-
err = migrate_pages(&pagelist, new_node_page, dest,
1063+
err = migrate_pages(&pagelist, new_node_page, NULL, dest,
10641064
MIGRATE_SYNC, MR_SYSCALL);
10651065
if (err)
10661066
putback_movable_pages(&pagelist);
@@ -1306,7 +1306,7 @@ static long do_mbind(unsigned long start, unsigned long len,
13061306

13071307
if (!list_empty(&pagelist)) {
13081308
WARN_ON_ONCE(flags & MPOL_MF_LAZY);
1309-
nr_failed = migrate_pages(&pagelist, new_page,
1309+
nr_failed = migrate_pages(&pagelist, new_page, NULL,
13101310
start, MIGRATE_SYNC, MR_MEMPOLICY_MBIND);
13111311
if (nr_failed)
13121312
putback_movable_pages(&pagelist);

‎mm/migrate.c‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,9 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
867867
* Obtain the lock on page, remove all ptes and migrate the page
868868
* to the newly allocated page in newpage.
869869
*/
870-
static int unmap_and_move(new_page_t get_new_page, unsigned long private,
871-
struct page *page, int force, enum migrate_mode mode)
870+
static int unmap_and_move(new_page_t get_new_page, free_page_t put_new_page,
871+
unsigned long private, struct page *page, int force,
872+
enum migrate_mode mode)
872873
{
873874
int rc = 0;
874875
int *result = NULL;
@@ -912,11 +913,17 @@ static int unmap_and_move(new_page_t get_new_page, unsigned long private,
912913
page_is_file_cache(page));
913914
putback_lru_page(page);
914915
}
916+
915917
/*
916-
* Move the new page to the LRU. If migration was not successful
917-
* then this will free the page.
918+
* If migration was not successful and there's a freeing callback, use
919+
* it. Otherwise, putback_lru_page() will drop the reference grabbed
920+
* during isolation.
918921
*/
919-
putback_lru_page(newpage);
922+
if (rc != MIGRATEPAGE_SUCCESS && put_new_page)
923+
put_new_page(newpage, private);
924+
else
925+
putback_lru_page(newpage);
926+
920927
if (result) {
921928
if (rc)
922929
*result = rc;
@@ -945,8 +952,9 @@ static int unmap_and_move(new_page_t get_new_page, unsigned long private,
945952
* will wait in the page fault for migration to complete.
946953
*/
947954
static int unmap_and_move_huge_page(new_page_t get_new_page,
948-
unsigned long private, struct page *hpage,
949-
int force, enum migrate_mode mode)
955+
free_page_t put_new_page, unsigned long private,
956+
struct page *hpage, int force,
957+
enum migrate_mode mode)
950958
{
951959
int rc = 0;
952960
int *result = NULL;
@@ -982,20 +990,30 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
982990
if (!page_mapped(hpage))
983991
rc = move_to_new_page(new_hpage, hpage, 1, mode);
984992

985-
if (rc)
993+
if (rc != MIGRATEPAGE_SUCCESS)
986994
remove_migration_ptes(hpage, hpage);
987995

988996
if (anon_vma)
989997
put_anon_vma(anon_vma);
990998

991-
if (!rc)
999+
if (rc == MIGRATEPAGE_SUCCESS)
9921000
hugetlb_cgroup_migrate(hpage, new_hpage);
9931001

9941002
unlock_page(hpage);
9951003
out:
9961004
if (rc != -EAGAIN)
9971005
putback_active_hugepage(hpage);
998-
put_page(new_hpage);
1006+
1007+
/*
1008+
* If migration was not successful and there's a freeing callback, use
1009+
* it. Otherwise, put_page() will drop the reference grabbed during
1010+
* isolation.
1011+
*/
1012+
if (rc != MIGRATEPAGE_SUCCESS && put_new_page)
1013+
put_new_page(new_hpage, private);
1014+
else
1015+
put_page(new_hpage);
1016+
9991017
if (result) {
10001018
if (rc)
10011019
*result = rc;
@@ -1012,6 +1030,8 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
10121030
* @from: The list of pages to be migrated.
10131031
* @get_new_page: The function used to allocate free pages to be used
10141032
* as the target of the page migration.
1033+
* @put_new_page: The function used to free target pages if migration
1034+
* fails, or NULL if no special handling is necessary.
10151035
* @private: Private data to be passed on to get_new_page()
10161036
* @mode: The migration mode that specifies the constraints for
10171037
* page migration, if any.
@@ -1025,7 +1045,8 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
10251045
* Returns the number of pages that were not migrated, or an error code.
10261046
*/
10271047
int migrate_pages(struct list_head *from, new_page_t get_new_page,
1028-
unsigned long private, enum migrate_mode mode, int reason)
1048+
free_page_t put_new_page, unsigned long private,
1049+
enum migrate_mode mode, int reason)
10291050
{
10301051
int retry = 1;
10311052
int nr_failed = 0;
@@ -1047,10 +1068,11 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
10471068

10481069
if (PageHuge(page))
10491070
rc = unmap_and_move_huge_page(get_new_page,
1050-
private, page, pass > 2, mode);
1071+
put_new_page, private, page,
1072+
pass > 2, mode);
10511073
else
1052-
rc = unmap_and_move(get_new_page, private,
1053-
page, pass > 2, mode);
1074+
rc = unmap_and_move(get_new_page, put_new_page,
1075+
private, page, pass > 2, mode);
10541076

10551077
switch(rc) {
10561078
case -ENOMEM:
@@ -1194,7 +1216,7 @@ static int do_move_page_to_node_array(struct mm_struct *mm,
11941216

11951217
err = 0;
11961218
if (!list_empty(&pagelist)) {
1197-
err = migrate_pages(&pagelist, new_page_node,
1219+
err = migrate_pages(&pagelist, new_page_node, NULL,
11981220
(unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
11991221
if (err)
12001222
putback_movable_pages(&pagelist);
@@ -1643,7 +1665,8 @@ int migrate_misplaced_page(struct page *page, int node)
16431665

16441666
list_add(&page->lru, &migratepages);
16451667
nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
1646-
node, MIGRATE_ASYNC, MR_NUMA_MISPLACED);
1668+
NULL, node, MIGRATE_ASYNC,
1669+
MR_NUMA_MISPLACED);
16471670
if (nr_remaining) {
16481671
putback_lru_pages(&migratepages);
16491672
isolated = 0;

‎mm/page_alloc.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6204,7 +6204,7 @@ static int __alloc_contig_migrate_range(struct compact_control *cc,
62046204
cc->nr_migratepages -= nr_reclaimed;
62056205

62066206
ret = migrate_pages(&cc->migratepages, alloc_migrate_target,
6207-
0, MIGRATE_SYNC, MR_CMA);
6207+
NULL, 0, MIGRATE_SYNC, MR_CMA);
62086208
}
62096209
if (ret < 0) {
62106210
putback_movable_pages(&cc->migratepages);

0 commit comments

Comments
 (0)