Skip to content

Commit b51820e

Browse files
honggyukimakpm00
authored andcommitted
mm/damon/paddr: introduce DAMOS_MIGRATE_COLD action for demotion
This patch introduces DAMOS_MIGRATE_COLD action, which is similar to DAMOS_PAGEOUT, but migrate folios to the given 'target_nid' in the sysfs instead of swapping them out. The 'target_nid' sysfs knob informs the migration target node ID. Here is one of the example usage of this 'migrate_cold' action. $ cd /sys/kernel/mm/damon/admin/kdamonds/<N> $ cat contexts/<N>/schemes/<N>/action migrate_cold $ echo 2 > contexts/<N>/schemes/<N>/target_nid $ echo commit > state $ numactl -p 0 ./hot_cold 500M 600M & $ numastat -c -p hot_cold Per-node process memory usage (in MBs) PID Node 0 Node 1 Node 2 Total -------------- ------ ------ ------ ----- 701 (hot_cold) 501 0 601 1101 Since there are some common routines with pageout, many functions have similar logics between pageout and migrate cold. damon_pa_migrate_folio_list() is a minimized version of shrink_folio_list(). Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Honggyu Kim <[email protected]> Signed-off-by: Hyeongtak Ji <[email protected]> Signed-off-by: SeongJae Park <[email protected]> Cc: Gregory Price <[email protected]> Cc: Hyeonggon Yoo <[email protected]> Cc: Masami Hiramatsu (Google) <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Rakie Kim <[email protected]> Cc: Steven Rostedt (Google) <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent ced816a commit b51820e

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

include/linux/damon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct damon_target {
105105
* @DAMOS_NOHUGEPAGE: Call ``madvise()`` for the region with MADV_NOHUGEPAGE.
106106
* @DAMOS_LRU_PRIO: Prioritize the region on its LRU lists.
107107
* @DAMOS_LRU_DEPRIO: Deprioritize the region on its LRU lists.
108+
* @DAMOS_MIGRATE_COLD: Migrate the regions prioritizing colder regions.
108109
* @DAMOS_STAT: Do nothing but count the stat.
109110
* @NR_DAMOS_ACTIONS: Total number of DAMOS actions
110111
*
@@ -122,6 +123,7 @@ enum damos_action {
122123
DAMOS_NOHUGEPAGE,
123124
DAMOS_LRU_PRIO,
124125
DAMOS_LRU_DEPRIO,
126+
DAMOS_MIGRATE_COLD,
125127
DAMOS_STAT, /* Do nothing but only record the stat */
126128
NR_DAMOS_ACTIONS,
127129
};

mm/damon/paddr.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <linux/pagemap.h>
1313
#include <linux/rmap.h>
1414
#include <linux/swap.h>
15+
#include <linux/memory-tiers.h>
16+
#include <linux/migrate.h>
17+
#include <linux/mm_inline.h>
1518

1619
#include "../internal.h"
1720
#include "ops-common.h"
@@ -325,6 +328,153 @@ static unsigned long damon_pa_deactivate_pages(struct damon_region *r,
325328
return damon_pa_mark_accessed_or_deactivate(r, s, false);
326329
}
327330

331+
static unsigned int __damon_pa_migrate_folio_list(
332+
struct list_head *migrate_folios, struct pglist_data *pgdat,
333+
int target_nid)
334+
{
335+
unsigned int nr_succeeded;
336+
nodemask_t allowed_mask = NODE_MASK_NONE;
337+
struct migration_target_control mtc = {
338+
/*
339+
* Allocate from 'node', or fail quickly and quietly.
340+
* When this happens, 'page' will likely just be discarded
341+
* instead of migrated.
342+
*/
343+
.gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) |
344+
__GFP_NOWARN | __GFP_NOMEMALLOC | GFP_NOWAIT,
345+
.nid = target_nid,
346+
.nmask = &allowed_mask
347+
};
348+
349+
if (pgdat->node_id == target_nid || target_nid == NUMA_NO_NODE)
350+
return 0;
351+
352+
if (list_empty(migrate_folios))
353+
return 0;
354+
355+
/* Migration ignores all cpuset and mempolicy settings */
356+
migrate_pages(migrate_folios, alloc_migrate_folio, NULL,
357+
(unsigned long)&mtc, MIGRATE_ASYNC, MR_DAMON,
358+
&nr_succeeded);
359+
360+
return nr_succeeded;
361+
}
362+
363+
static unsigned int damon_pa_migrate_folio_list(struct list_head *folio_list,
364+
struct pglist_data *pgdat,
365+
int target_nid)
366+
{
367+
unsigned int nr_migrated = 0;
368+
struct folio *folio;
369+
LIST_HEAD(ret_folios);
370+
LIST_HEAD(migrate_folios);
371+
372+
while (!list_empty(folio_list)) {
373+
struct folio *folio;
374+
375+
cond_resched();
376+
377+
folio = lru_to_folio(folio_list);
378+
list_del(&folio->lru);
379+
380+
if (!folio_trylock(folio))
381+
goto keep;
382+
383+
/* Relocate its contents to another node. */
384+
list_add(&folio->lru, &migrate_folios);
385+
folio_unlock(folio);
386+
continue;
387+
keep:
388+
list_add(&folio->lru, &ret_folios);
389+
}
390+
/* 'folio_list' is always empty here */
391+
392+
/* Migrate folios selected for migration */
393+
nr_migrated += __damon_pa_migrate_folio_list(
394+
&migrate_folios, pgdat, target_nid);
395+
/*
396+
* Folios that could not be migrated are still in @migrate_folios. Add
397+
* those back on @folio_list
398+
*/
399+
if (!list_empty(&migrate_folios))
400+
list_splice_init(&migrate_folios, folio_list);
401+
402+
try_to_unmap_flush();
403+
404+
list_splice(&ret_folios, folio_list);
405+
406+
while (!list_empty(folio_list)) {
407+
folio = lru_to_folio(folio_list);
408+
list_del(&folio->lru);
409+
folio_putback_lru(folio);
410+
}
411+
412+
return nr_migrated;
413+
}
414+
415+
static unsigned long damon_pa_migrate_pages(struct list_head *folio_list,
416+
int target_nid)
417+
{
418+
int nid;
419+
unsigned long nr_migrated = 0;
420+
LIST_HEAD(node_folio_list);
421+
unsigned int noreclaim_flag;
422+
423+
if (list_empty(folio_list))
424+
return nr_migrated;
425+
426+
noreclaim_flag = memalloc_noreclaim_save();
427+
428+
nid = folio_nid(lru_to_folio(folio_list));
429+
do {
430+
struct folio *folio = lru_to_folio(folio_list);
431+
432+
if (nid == folio_nid(folio)) {
433+
list_move(&folio->lru, &node_folio_list);
434+
continue;
435+
}
436+
437+
nr_migrated += damon_pa_migrate_folio_list(&node_folio_list,
438+
NODE_DATA(nid),
439+
target_nid);
440+
nid = folio_nid(lru_to_folio(folio_list));
441+
} while (!list_empty(folio_list));
442+
443+
nr_migrated += damon_pa_migrate_folio_list(&node_folio_list,
444+
NODE_DATA(nid),
445+
target_nid);
446+
447+
memalloc_noreclaim_restore(noreclaim_flag);
448+
449+
return nr_migrated;
450+
}
451+
452+
static unsigned long damon_pa_migrate(struct damon_region *r, struct damos *s)
453+
{
454+
unsigned long addr, applied;
455+
LIST_HEAD(folio_list);
456+
457+
for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {
458+
struct folio *folio = damon_get_folio(PHYS_PFN(addr));
459+
460+
if (!folio)
461+
continue;
462+
463+
if (damos_pa_filter_out(s, folio))
464+
goto put_folio;
465+
466+
if (!folio_isolate_lru(folio))
467+
goto put_folio;
468+
list_add(&folio->lru, &folio_list);
469+
put_folio:
470+
folio_put(folio);
471+
}
472+
applied = damon_pa_migrate_pages(&folio_list, s->target_nid);
473+
cond_resched();
474+
return applied * PAGE_SIZE;
475+
}
476+
477+
328478
static unsigned long damon_pa_apply_scheme(struct damon_ctx *ctx,
329479
struct damon_target *t, struct damon_region *r,
330480
struct damos *scheme)
@@ -336,6 +486,8 @@ static unsigned long damon_pa_apply_scheme(struct damon_ctx *ctx,
336486
return damon_pa_mark_accessed(r, scheme);
337487
case DAMOS_LRU_DEPRIO:
338488
return damon_pa_deactivate_pages(r, scheme);
489+
case DAMOS_MIGRATE_COLD:
490+
return damon_pa_migrate(r, scheme);
339491
case DAMOS_STAT:
340492
break;
341493
default:
@@ -356,6 +508,8 @@ static int damon_pa_scheme_score(struct damon_ctx *context,
356508
return damon_hot_score(context, r, scheme);
357509
case DAMOS_LRU_DEPRIO:
358510
return damon_cold_score(context, r, scheme);
511+
case DAMOS_MIGRATE_COLD:
512+
return damon_cold_score(context, r, scheme);
359513
default:
360514
break;
361515
}

mm/damon/sysfs-schemes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,7 @@ static const char * const damon_sysfs_damos_action_strs[] = {
14581458
"nohugepage",
14591459
"lru_prio",
14601460
"lru_deprio",
1461+
"migrate_cold",
14611462
"stat",
14621463
};
14631464

0 commit comments

Comments
 (0)