Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp1632460lqo; Sun, 12 May 2024 10:55:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXz0znR+DfRcoQc3+MaslxJmen/GmrQifq8Yl+/R4gQDI0JwnNOIOMDBUbbuJ6mTR/E1sFM/j2fY8YmmYzXP/Qeacltj3hhbFTyRoQ8qQ== X-Google-Smtp-Source: AGHT+IHEnbA8IclZKR9946u7HJw++IoqmIRuBX56KRo00S//r+QVjdLY2lR7W1rT7acxifuwgeHf X-Received: by 2002:a05:6358:63a4:b0:18f:8613:12b8 with SMTP id e5c5f4694b2df-193bb3fd3e1mr960279755d.5.1715536535455; Sun, 12 May 2024 10:55:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715536535; cv=pass; d=google.com; s=arc-20160816; b=pUhmkuoQY1Nyxn7wWueBrKComDxHx+5/AraLTKvS9Vv6rbYIi8kKM9O/J5kINLJIT9 N8BcvWX9sbOCWqW1qC51EGYvGD4g8vOGQCe09WTl1YtL3qT2IhX2fQXq5kTAe2aVBnDa bfsHIE8aUA0XzGmfGI2jN9zNGl1epJlqxZKZmPfbVKsP7jrZDkaSeRz3k5KwvhTFswxi 5I6oMtQPREUBvlZomYITMhDJRKCCZJGsdphXgSi0vwlRBvFrT2YTFyn+9N67KYxde2vl DoDysGrhiAsDxK80IRqCPlbcUUL0TVuvqQsSVdbAv2nktNmGEygEFkK53am4GkzP70SK AEuw== 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:dkim-signature; bh=VJpsqidB+sPCTOeJSLqcSxbS/rS/jby608SgB8npLVQ=; fh=vjfZ+sSk9asSRFhBCScujTrseooEPPUWOQ91ARsCvTI=; b=ZrUNmKUpLKqvsAiB3FgVHelAWO1DaHHDQE+5UodI+tYPbS81gLtbX46gS9keGN8bvV lzt/5/vr8hwWAfS4R6ly0ExKSp4nVdL3ItZOWCBSZEzywDpC1dTCjXAg4F2DLuZZsVTp d9LCQ7PPlnKZE75jiZNz+gw4nfxY8Yl+n38iGHIFJGYdpReHl9YoZ77F1Wu5Wy3c55us jrCmfgcORsOwcLY+47HXkEFCjKQdcEMUGGDAeD8lOh8iJeAyfKjlSXUrgHSZCViKX+dd F3duX8KvE6MzXKiEu5k3FZc9jeYvMpcKDLwg/epZvSVjK1ashkSAx9+thW2wmRJGKF/2 nuMw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Cc70FMlK; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-176963-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176963-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43df56afec7si78452881cf.561.2024.05.12.10.55.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 May 2024 10:55:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-176963-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Cc70FMlK; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-176963-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176963-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 23EA41C2081B for ; Sun, 12 May 2024 17:55:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 10262548E7; Sun, 12 May 2024 17:54:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Cc70FMlK" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0393524A6; Sun, 12 May 2024 17:54:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715536497; cv=none; b=XrSNfi7UozdHOzX3MR4ffNa3t6WkOKXTlXF7jadv5jI27vSTJUzjYEjQ0vUVuOvSZCdhVuWnEA1HsDoUtWifMFeZKqC++JUZ3rsyA6cQiasIC4BR9LvY8hJT9OHrStsJzR7iwzSk7l7jMY4BQdoH2NlNytKspaQ+1X/8lqu9LDA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715536497; c=relaxed/simple; bh=pJ6vzvCtZUPWSFq3LZUFfl+zSubCIvDIafn053zECpU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=csRQUqaP515vWa2mgUoB5mxmc0oOoHy64v9jVOlFQRvbOlZCzVuNqNQa6XBfcQ4WXMFXbFqqzu+TIhRTqCVzTuFvbW30kr7T21ybFg3A07tCU9HyVvmoXa/162XVD2idi+nwmIEUkOx/TLJCSJhhRzvnzYAFjf2fwPXcCEROPuo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Cc70FMlK; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC9FFC4AF07; Sun, 12 May 2024 17:54:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715536497; bh=pJ6vzvCtZUPWSFq3LZUFfl+zSubCIvDIafn053zECpU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cc70FMlKxLi1JuD51HmpqEsx8453y79uRyuCpKWF1nFmbmpT3wquNTxEgmigdAGeP vOBagAGtYXvLeuZDb7OAbeB7Bg/rsRmffuka3NZMTyvrPgBuKUCuH6zS64yc8uIegg eo9hlLQMmqvopMR8rbRj2YnhWwtjtZotQ5AgJi26Y5BHdKGHt7b7iqvcnrIV+0XH5h 5Ay0c7jBCrdo0JwwpVwLuqqcqJQx6Ur+54tZHTranalXvTOFgw1NHZQYObYyribLXj HySwsRHduSQvTrYwk/d08i69oLkf8HWJkllCj9u+M2yPaM6ABrwmmwrU9Wo0GCzIZN 3zOv8UL9vgZ0w== From: SeongJae Park To: Cc: Honggyu Kim , SeongJae Park , Andrew Morton , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hyeongtak Ji Subject: [RFC PATCH v4 4/5] mm/damon/paddr: introduce DAMOS_MIGRATE_COLD action for demotion Date: Sun, 12 May 2024 10:54:46 -0700 Message-Id: <20240512175447.75943-5-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240512175447.75943-1-sj@kernel.org> References: <20240512175447.75943-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Honggyu Kim 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..a468792d9162 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_demote_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.39.2