Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp140325lqs; Thu, 13 Jun 2024 06:21:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX6LkZfzs22lsIxpdkkeMvFceXmoqLcLT+CSw1ZR70Ea4cb7/Dj2CFpkVrrmqVTGDEcvBP1eqaVZ0+TwRQqP4VA0wcE755zv3s1yeXVHg== X-Google-Smtp-Source: AGHT+IE7dv+dCBi95dUtFJR4y7CCogYmWy8rCD3B2Kd1KLsGYL1W19a0OFbEBpbcoBWDmkbC676U X-Received: by 2002:a05:6870:5492:b0:254:a1c0:eed3 with SMTP id 586e51a60fabf-25514fb4f4dmr5430986fac.51.1718284878689; Thu, 13 Jun 2024 06:21:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718284878; cv=pass; d=google.com; s=arc-20160816; b=Ra/n1aAKgCULrW8Wa7Gc/0kLRaLskMn0MOuxczR/IttfcI/Xi1nbT4kxd7g4odDyR5 TKmcbHyuTiImu1m2yaaGM2fjOMgpr3SZom4woao1mb3cIz/LYo1j3+UpCZ2BT9EMK4nG y62Hi3K2FpCwDMcCwUaXhZ9KFwYkMxxWto6Nf6FZQuitY7BCsOcMM77TwzOoDAeQcDXC xx6CAoTUNMOH+S9nszBiDENH+M4hxl4r/rYksvwkk+xqw1icXwOXyIn7GIml0r7nHms7 LFEk3K33W7W0reBT7uqq70/cXbl6PEfQZ+/WAqt0fA27+hbkISYAbfhI4nsaDAuFTT9Q ItFg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=nv1oWni34khPhdRw64DblZPBH5T/Fb3rcF3xoWmpT6k=; fh=MK3lPJLblaaz0D+bRupTsFqdcs/7k4SZO5NQGKm4FWY=; b=bftfUzpY7gw9j3gQCzx5KR1C5cu5ChPSCk4Chkna8DAxNr39GNqIfQSYRN2qWA3Nw9 kRt5peNlHUjD5YiedpAEfnXG4qmQMXVOdrTzxKaRUh8mDtsW75eBizvFoHKyFG4R5uYu Zivb2DYH8kg+x3rY5X2Rou5tA6pP/FztQkYxhopdh/6ZNa1yjJHUtIVfq4rfCnkxlQqA Gnk0Bp/yKVkbt0ao+U7CHqdMeuFkN8fqZG2TT4HH7HTG3RAlht8MwciZ8XHyb1DnWMov Accri5NEy+8M5S0K6hjgztJ4N12Jixpqhrf3W9sdap4DThCjdV3yAqjHt9Dn390eoRxS F6Fw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=sk.com); spf=pass (google.com: domain of linux-kernel+bounces-213283-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-213283-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-705ccb3e4absi1381195b3a.169.2024.06.13.06.21.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 06:21:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-213283-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sk.com); spf=pass (google.com: domain of linux-kernel+bounces-213283-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-213283-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 9A1A0285447 for ; Thu, 13 Jun 2024 13:19:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 43068145A1B; Thu, 13 Jun 2024 13:18:17 +0000 (UTC) Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED5661448FF; Thu, 13 Jun 2024 13:18:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=166.125.252.92 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718284696; cv=none; b=ohEqWqUVwBW7GQkH4fa7Bzzf/1OVWCaBWQPccaUxAsP78M8lKPz2W/FeEzzMjKSkS58/4KbIYItd7KVhkBwck0rFtU/+Z+YX7ElsbyTn1D3/NTCMrtAiUVOXbWoATOypNDi5bLPJSXShwDzDzktqBl+xAeYV1E5N8PeZeqQtJRs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718284696; c=relaxed/simple; bh=gK7IYwCVAC/BaeHUelw2TAZSnFTHiuHeoYJ/b8GUzHc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=K0xbZonEOAgtVKGr8HI0iayEVUQNT4o+VDmR2Uar7In5aKlP4hCeLMsdoMb5EFWEmhgEvVp/be6C2txDqWaJiZKd8U42qxnkjj0KEVPCrY4F8PJg0NXR1xVnErkIC45on4dcPdhvv4mqkScnIkOySNdSW7KQ25MroJOj/LEYZCk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sk.com; spf=pass smtp.mailfrom=sk.com; arc=none smtp.client-ip=166.125.252.92 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sk.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sk.com X-AuditID: a67dfc5b-d6dff70000001748-40-666af1901b75 From: Honggyu Kim To: SeongJae Park , damon@lists.linux.dev Cc: Andrew Morton , Masami Hiramatsu , Mathieu Desnoyers , Steven Rostedt , Gregory Price , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, 42.hyeyoo@gmail.com, art.jeongseob@gmail.com, kernel_team@skhynix.com, Honggyu Kim , Hyeongtak Ji Subject: [PATCH 5/8] mm/damon/paddr: introduce DAMOS_MIGRATE_COLD action for demotion Date: Thu, 13 Jun 2024 22:17:36 +0900 Message-ID: <20240613131741.513-6-honggyu.kim@sk.com> X-Mailer: git-send-email 2.43.0.windows.1 In-Reply-To: <20240613131741.513-1-honggyu.kim@sk.com> References: <20240613131741.513-1-honggyu.kim@sk.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrBLMWRmVeSWpSXmKPExsXC9ZZnke6Ej1lpBjdW8llM7DGwmLN+DZvF /Qev2S2e/P/NatHQ9IjF4vKuOWwW99b8Z7U4sv4si8Xms2eYLRYvV7PY1/GAyeLw1zdMDjwe S0+/YfPYOesuu0fLvlvsHptWdbJ5bPo0id3jxIzfLB4vNs9k9Nj48T+7x+dNcgGcUVw2Kak5 mWWpRfp2CVwZ7w8/ZSnYZF2x9vdq9gbGpQZdjJwcEgImEtsOnWTuYuQAs+9c0wAJswmoSVx5 OYkJJCwiYCUxbUdsFyMXB7PANWaJ5c2LwOLCAqESLcscQMpZBFQl/vSdZAWxeQVMJWa132KD mK4p8Xj7T3YQm1PATGLdt/uMILYQUE3H/2fMEPWCEidnPmEBsZkF5CWat85mhuj9zCbxcqYW hC0pcXDFDZYJjPyzkLTMQtKygJFpFaNQZl5ZbmJmjoleRmVeZoVecn7uJkZg+C+r/RO9g/HT heBDjAIcjEo8vB7PstKEWBPLiitzDzFKcDArifDOWggU4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T024s/ K12zezWVWIozEg21mIuKEwGItZOkcQIAAA== X-CFilter-Loop: Reflected 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/ $ cat contexts//schemes//action migrate_cold $ echo 2 > contexts//schemes//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(). Signed-off-by: Honggyu Kim Signed-off-by: Hyeongtak Ji Signed-off-by: SeongJae Park --- include/linux/damon.h | 2 + mm/damon/paddr.c | 154 +++++++++++++++++++++++++++++++++++++++ mm/damon/sysfs-schemes.c | 1 + 3 files changed, 157 insertions(+) diff --git a/include/linux/damon.h b/include/linux/damon.h index 21d6b69a015c..56714b6eb0d7 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -105,6 +105,7 @@ struct damon_target { * @DAMOS_NOHUGEPAGE: Call ``madvise()`` for the region with MADV_NOHUGEPAGE. * @DAMOS_LRU_PRIO: Prioritize the region on its LRU lists. * @DAMOS_LRU_DEPRIO: Deprioritize the region on its LRU lists. + * @DAMOS_MIGRATE_COLD: Migrate the regions prioritizing colder regions. * @DAMOS_STAT: Do nothing but count the stat. * @NR_DAMOS_ACTIONS: Total number of DAMOS actions * @@ -122,6 +123,7 @@ enum damos_action { DAMOS_NOHUGEPAGE, DAMOS_LRU_PRIO, DAMOS_LRU_DEPRIO, + DAMOS_MIGRATE_COLD, DAMOS_STAT, /* Do nothing but only record the stat */ NR_DAMOS_ACTIONS, }; diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c index 18797c1b419b..882ae54af829 100644 --- a/mm/damon/paddr.c +++ b/mm/damon/paddr.c @@ -12,6 +12,9 @@ #include #include #include +#include +#include +#include #include "../internal.h" #include "ops-common.h" @@ -325,6 +328,153 @@ static unsigned long damon_pa_deactivate_pages(struct damon_region *r, return damon_pa_mark_accessed_or_deactivate(r, s, false); } +static unsigned int __damon_pa_migrate_folio_list( + struct list_head *migrate_folios, struct pglist_data *pgdat, + int target_nid) +{ + unsigned int nr_succeeded; + nodemask_t allowed_mask = NODE_MASK_NONE; + struct migration_target_control mtc = { + /* + * Allocate from 'node', or fail quickly and quietly. + * When this happens, 'page' will likely just be discarded + * instead of migrated. + */ + .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | + __GFP_NOWARN | __GFP_NOMEMALLOC | GFP_NOWAIT, + .nid = target_nid, + .nmask = &allowed_mask + }; + + if (pgdat->node_id == target_nid || target_nid == NUMA_NO_NODE) + return 0; + + if (list_empty(migrate_folios)) + return 0; + + /* Migration ignores all cpuset and mempolicy settings */ + migrate_pages(migrate_folios, alloc_migrate_folio, NULL, + (unsigned long)&mtc, MIGRATE_ASYNC, MR_DAMON, + &nr_succeeded); + + return nr_succeeded; +} + +static unsigned int damon_pa_migrate_folio_list(struct list_head *folio_list, + struct pglist_data *pgdat, + int target_nid) +{ + unsigned int nr_migrated = 0; + struct folio *folio; + LIST_HEAD(ret_folios); + LIST_HEAD(migrate_folios); + + while (!list_empty(folio_list)) { + struct folio *folio; + + cond_resched(); + + folio = lru_to_folio(folio_list); + list_del(&folio->lru); + + if (!folio_trylock(folio)) + goto keep; + + /* Relocate its contents to another node. */ + list_add(&folio->lru, &migrate_folios); + folio_unlock(folio); + continue; +keep: + list_add(&folio->lru, &ret_folios); + } + /* 'folio_list' is always empty here */ + + /* Migrate folios selected for migration */ + nr_migrated += __damon_pa_migrate_folio_list( + &migrate_folios, pgdat, target_nid); + /* + * Folios that could not be migrated are still in @migrate_folios. Add + * those back on @folio_list + */ + if (!list_empty(&migrate_folios)) + list_splice_init(&migrate_folios, folio_list); + + try_to_unmap_flush(); + + list_splice(&ret_folios, folio_list); + + while (!list_empty(folio_list)) { + folio = lru_to_folio(folio_list); + list_del(&folio->lru); + folio_putback_lru(folio); + } + + return nr_migrated; +} + +static unsigned long damon_pa_migrate_pages(struct list_head *folio_list, + int target_nid) +{ + int nid; + unsigned long nr_migrated = 0; + LIST_HEAD(node_folio_list); + unsigned int noreclaim_flag; + + if (list_empty(folio_list)) + return nr_migrated; + + noreclaim_flag = memalloc_noreclaim_save(); + + nid = folio_nid(lru_to_folio(folio_list)); + do { + struct folio *folio = lru_to_folio(folio_list); + + if (nid == folio_nid(folio)) { + list_move(&folio->lru, &node_folio_list); + continue; + } + + nr_migrated += damon_pa_migrate_folio_list(&node_folio_list, + NODE_DATA(nid), + target_nid); + nid = folio_nid(lru_to_folio(folio_list)); + } while (!list_empty(folio_list)); + + nr_migrated += damon_pa_migrate_folio_list(&node_folio_list, + NODE_DATA(nid), + target_nid); + + memalloc_noreclaim_restore(noreclaim_flag); + + return nr_migrated; +} + +static unsigned long damon_pa_migrate(struct damon_region *r, struct damos *s) +{ + unsigned long addr, applied; + LIST_HEAD(folio_list); + + for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) { + struct folio *folio = damon_get_folio(PHYS_PFN(addr)); + + if (!folio) + continue; + + if (damos_pa_filter_out(s, folio)) + goto put_folio; + + if (!folio_isolate_lru(folio)) + goto put_folio; + list_add(&folio->lru, &folio_list); +put_folio: + folio_put(folio); + } + applied = damon_pa_migrate_pages(&folio_list, s->target_nid); + cond_resched(); + return applied * PAGE_SIZE; +} + + static unsigned long damon_pa_apply_scheme(struct damon_ctx *ctx, struct damon_target *t, struct damon_region *r, struct damos *scheme) @@ -336,6 +486,8 @@ static unsigned long damon_pa_apply_scheme(struct damon_ctx *ctx, return damon_pa_mark_accessed(r, scheme); case DAMOS_LRU_DEPRIO: return damon_pa_deactivate_pages(r, scheme); + case DAMOS_MIGRATE_COLD: + return damon_pa_migrate(r, scheme); case DAMOS_STAT: break; default: @@ -356,6 +508,8 @@ static int damon_pa_scheme_score(struct damon_ctx *context, return damon_hot_score(context, r, scheme); case DAMOS_LRU_DEPRIO: return damon_cold_score(context, r, scheme); + case DAMOS_MIGRATE_COLD: + return damon_cold_score(context, r, scheme); default: break; } diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c index 0632d28b67f8..880015d5b5ea 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -1458,6 +1458,7 @@ static const char * const damon_sysfs_damos_action_strs[] = { "nohugepage", "lru_prio", "lru_deprio", + "migrate_cold", "stat", }; -- 2.34.1