From nobody Fri Jun 26 20:23:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1D904C433F5 for ; Fri, 18 Feb 2022 10:27:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232785AbiBRK1N (ORCPT ); Fri, 18 Feb 2022 05:27:13 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:37674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232440AbiBRK1K (ORCPT ); Fri, 18 Feb 2022 05:27:10 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB55D64F8 for ; Fri, 18 Feb 2022 02:26:54 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id x4so6817184plb.4 for ; Fri, 18 Feb 2022 02:26:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ajou.ac.kr; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=48kB5lAYgSfFmsICPZUMRy13W44Wv8CdeSdM5zh36Ro=; b=W3astCwwAyNQranzslt+8SIVB9svslnNne9hAGV/MpxQw0cP0JbyeBOEZWHKEgcYDw eOrC4GquiAZGyMgCCpI9y1/lx0HkTTsehtY3KV73v6w/8qmqjQnPvlTQIvSLjiI0eRu/ P6UFKhgUola5sBEkHxBF5NPbMKzYmwHN05py0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=48kB5lAYgSfFmsICPZUMRy13W44Wv8CdeSdM5zh36Ro=; b=YC1eNQ9G0aUIVcjqAqzAUmFveuqs/gkQhfuMJvJlHGZ36dw0iwRxtZsTTPMvv/+Qww zqjnBM69klcgM6Ygem3QCLqa5+j4bwb7VPyaEoJN+s7wUpxJy++u8Udfw5jUhj15DSYb SRQaRy6XfIAAhKa5mj+Q+TqSaWe7XoCQ+AB8xGWV+QoLgCawqWmt26FSeh3gr5eA3WCi IaNX7Bbg+vWFSAaiJHP4Dkt0FTtc8G5yNl8/1gyfnfNd/V0UDQhQ9vnjycCmZMT4GZAQ MaNyeZQfpkgW+WHHaESTCDTrEYbYdRxUx6aokk88vV2vDRxB5YzFH2oKprIXHgVMhqto Fi0g== X-Gm-Message-State: AOAM530hZWdwuhaNs1RNB9mtBKr9slV2KOAuGiUmcn5qQTfaGIySKRdw fJkJKb9Vqu0jD2/dC+pI7z6XAg== X-Google-Smtp-Source: ABdhPJw7BKEUMMQFo15QZFrb31FFqmsj+eQYpuHpVdwZyvtZncX1W/2Fxnh8PllN3pxemTfjNuGIkQ== X-Received: by 2002:a17:902:6902:b0:14d:6aa4:f3f5 with SMTP id j2-20020a170902690200b0014d6aa4f3f5mr6987246plk.20.1645180013550; Fri, 18 Feb 2022 02:26:53 -0800 (PST) Received: from localhost.localdomain ([210.107.197.32]) by smtp.googlemail.com with ESMTPSA id k13sm2767696pfc.176.2022.02.18.02.26.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 02:26:53 -0800 (PST) From: Jonghyeon Kim To: akpm@linux-foundation.org Cc: Jonghyeon Kim , Jonathan.Cameron@Huawei.com, amit@kernel.org, benh@kernel.crashing.org, corbet@lwn.net, david@redhat.com, dwmw@amazon.com, elver@google.com, foersleo@amazon.de, gthelen@google.com, markubo@amazon.de, rientjes@google.com, shakeelb@google.com, shuah@kernel.org, linux-damon@amazon.com, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v1 1/3] mm/damon: Rebase damos watermarks for NUMA systems Date: Fri, 18 Feb 2022 19:26:09 +0900 Message-Id: <20220218102611.31895-2-tome01@ajou.ac.kr> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220218102611.31895-1-tome01@ajou.ac.kr> References: <20220218102611.31895-1-tome01@ajou.ac.kr> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" For NUMA systems, there is a need to allow damos to select watermark options for monitoring each NUMA node or whole system free memory. Even if we do not use NUMA, since the default NUMA node number is 0, we can monitor the whole system memory without any configuration. Signed-off-by: Jonghyeon Kim --- include/linux/damon.h | 2 ++ mm/damon/core.c | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 49c4a11ecf20..c0adf1566603 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -170,6 +170,7 @@ enum damos_wmark_metric { * @high: High watermark. * @mid: Middle watermark. * @low: Low watermark. + * @node: NUMA node for the watermarks. * * If &metric is &DAMOS_WMARK_NONE, the scheme is always active. Being ac= tive * means DAMON does monitoring and applying the action of the scheme to @@ -186,6 +187,7 @@ struct damos_watermarks { unsigned long high; unsigned long mid; unsigned long low; + int node; =20 /* private: */ bool activated; diff --git a/mm/damon/core.c b/mm/damon/core.c index 82e0a4620c4f..290c9c0535ee 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -179,6 +179,7 @@ struct damos *damon_new_scheme( scheme->wmarks.high =3D wmarks->high; scheme->wmarks.mid =3D wmarks->mid; scheme->wmarks.low =3D wmarks->low; + scheme->wmarks.node =3D wmarks->node; scheme->wmarks.activated =3D true; =20 return scheme; @@ -951,14 +952,15 @@ static bool kdamond_need_stop(struct damon_ctx *ctx) return true; } =20 -static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metr= ic) +static unsigned long damos_wmark_metric_value(struct damos_watermarks wmar= ks) { - struct sysinfo i; + unsigned long nr_total, nr_free; =20 - switch (metric) { + switch (wmarks.metric) { case DAMOS_WMARK_FREE_MEM_RATE: - si_meminfo(&i); - return i.freeram * 1000 / i.totalram; + nr_total =3D node_present_pages(wmarks.node); + nr_free =3D sum_zone_node_page_state(wmarks.node, NR_FREE_PAGES); + return nr_free * 1000 / nr_total; default: break; } @@ -976,7 +978,7 @@ static unsigned long damos_wmark_wait_us(struct damos *= scheme) if (scheme->wmarks.metric =3D=3D DAMOS_WMARK_NONE) return 0; =20 - metric =3D damos_wmark_metric_value(scheme->wmarks.metric); + metric =3D damos_wmark_metric_value(scheme->wmarks); /* higher than high watermark or lower than low watermark */ if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) { if (scheme->wmarks.activated) --=20 2.17.1 From nobody Fri Jun 26 20:23:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA1C2C433FE for ; Fri, 18 Feb 2022 10:27:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233035AbiBRK1Z (ORCPT ); Fri, 18 Feb 2022 05:27:25 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:37934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234050AbiBRK1T (ORCPT ); Fri, 18 Feb 2022 05:27:19 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFBF512770 for ; Fri, 18 Feb 2022 02:27:01 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id qe15so8159404pjb.3 for ; Fri, 18 Feb 2022 02:27:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ajou.ac.kr; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+T3KpWhNKa0tR65ONmcJl30b/m+LmztZvFh+zw/KtCk=; b=CSBsjQACvcRO/sFAdwgQWs3JH/MmMWvyiVBNhla3nPtD9bgbk93JwZ2ntMsBUKq32T UIB8q3Ud3Aljxzwv3VHVhqrkqAdydQhA7KNG3IKuxZjpV67WfFmat9sN/omK6iuwGB4C IPB1CVoscYCyZtSboNE8VGsLqW3UjP5cZIHcA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+T3KpWhNKa0tR65ONmcJl30b/m+LmztZvFh+zw/KtCk=; b=0mYEE1d/lv7N1YeGSh2XxEFg3u5DVfgl0HUrf9XfFm41ycCVpnKrWJ+BBIdeFEFZ3c 727MlMY+/am8GUwyAEqrYvzlzILlrWSCBaDCRgGUOFtpGopcB1FPv1UV6Qaz/KoJpoQp 9ni5HMSFBMl57l9cu5WkI8t6rJ7xe6naVo2RLLFZ4vrHpHe0in1NI51OCIKuR+UJ7eoT P7oKFOu7YgC41kUUKbCNyieYTgoRurT/onF09YFFBazGuzcNomy9cl0erCOfsNqSE00h IM4B0PeUgICPRMo/orfSC4XQW9av/akHmKtQ+iAVpW9sOje6mlhEVfoKZ43n4Vkihwch msyw== X-Gm-Message-State: AOAM533c8iMcRYQh+7u78rkoictne6eSaPx/QDj7RzfChziMl/xHiwAs /Wl7NIMRwANMlZFrx8gNdZ5NVQ== X-Google-Smtp-Source: ABdhPJwmd0fVZO0Edt92aYqkcns0NPA3othJELbRqEVsjcuckc3gLs6OopDmtExBFLlQwLVPribCdw== X-Received: by 2002:a17:90b:38ce:b0:1b9:e0dd:50ec with SMTP id nn14-20020a17090b38ce00b001b9e0dd50ecmr11885292pjb.163.1645180021147; Fri, 18 Feb 2022 02:27:01 -0800 (PST) Received: from localhost.localdomain ([210.107.197.32]) by smtp.googlemail.com with ESMTPSA id k13sm2767696pfc.176.2022.02.18.02.26.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 02:27:00 -0800 (PST) From: Jonghyeon Kim To: akpm@linux-foundation.org Cc: Jonghyeon Kim , Jonathan.Cameron@Huawei.com, amit@kernel.org, benh@kernel.crashing.org, corbet@lwn.net, david@redhat.com, dwmw@amazon.com, elver@google.com, foersleo@amazon.de, gthelen@google.com, markubo@amazon.de, rientjes@google.com, shakeelb@google.com, shuah@kernel.org, linux-damon@amazon.com, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v1 2/3] mm/damon/core: Add damon_start_one() Date: Fri, 18 Feb 2022 19:26:10 +0900 Message-Id: <20220218102611.31895-3-tome01@ajou.ac.kr> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220218102611.31895-1-tome01@ajou.ac.kr> References: <20220218102611.31895-1-tome01@ajou.ac.kr> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" damon_start() function is designed to start multiple damon monitoring contexts. But, sometimes we need to start monitoring one context. Although __damon_start() could be considered to start for one monitoring context, it seems reasonable to adopt a new function that does not need to handle 'damon_lock' from the caller. Signed-off-by: Jonghyeon Kim --- include/linux/damon.h | 1 + mm/damon/core.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/linux/damon.h b/include/linux/damon.h index c0adf1566603..069577477662 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -511,6 +511,7 @@ int damon_register_ops(struct damon_operations *ops); int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id); =20 int damon_start(struct damon_ctx **ctxs, int nr_ctxs); +int damon_start_one(struct damon_ctx *ctx); int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); =20 #endif /* CONFIG_DAMON */ diff --git a/mm/damon/core.c b/mm/damon/core.c index 290c9c0535ee..e43f138a3489 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -466,6 +466,31 @@ int damon_start(struct damon_ctx **ctxs, int nr_ctxs) return err; } =20 +/** + * damon_start_one() - Starts the monitorings for one context. + * @ctx: monitoring context + * + * This function starts one monitoring thread for only one monitoring cont= ext + * handling damon_lock. + * + * Return: 0 on success, negative error code otherwise. + */ +int damon_start_one(struct damon_ctx *ctx) +{ + int err =3D 0; + + mutex_lock(&damon_lock); + err =3D __damon_start(ctx); + if (err) { + mutex_unlock(&damon_lock); + return err; + } + nr_running_ctxs++; + mutex_unlock(&damon_lock); + + return err; +} + /* * __damon_stop() - Stops monitoring of given context. * @ctx: monitoring context --=20 2.17.1 From nobody Fri Jun 26 20:23:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1383DC433F5 for ; Fri, 18 Feb 2022 10:27:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234079AbiBRK1d (ORCPT ); Fri, 18 Feb 2022 05:27:33 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:38482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233420AbiBRK1Z (ORCPT ); Fri, 18 Feb 2022 05:27:25 -0500 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82E6B1C933 for ; Fri, 18 Feb 2022 02:27:08 -0800 (PST) Received: by mail-pl1-x62b.google.com with SMTP id l9so6853144plg.0 for ; Fri, 18 Feb 2022 02:27:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ajou.ac.kr; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hqHpu7GNiQ7sov4kYNjqEALFPOD9/Tm3brs65rAgImQ=; b=WemAjxdvDJxi/gdw2o4cpnBr8UVsDgC7LoJYR2HABYI9q2Yigfb4FQH5fOL2C71Uvf xIQf4gjVALQLZiKGayJPcVxNBRVo4QLQ/NJs2iJMZwwd7Mqtrp/+2IfhnTYQgCqnHsqN xSSTfLBn3aMEA6feuwqU6IpJW9KLZpKGBMkXo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=hqHpu7GNiQ7sov4kYNjqEALFPOD9/Tm3brs65rAgImQ=; b=8FbjtzaybxZtW+0AFtalTjPLyX8lbob1/Ku7uL/iwrvQZs+NZwsil0jYnj8C1Sldfq dHZiyWG6bQx5HI9fnIiScL9feY/Le8JULleeSc5XpElDOU4i+e1pHEf3SjIBp0nkANtP FslQZgIoh6GN6laS3aEGwgqLv+78zPgeZLED1Q7rsPDVaF356aPf7hpQp+2HjPwHRYrQ a21A9PA0wmhEVCGt8MoaIlDO1BUUJ+aRIKfnh6ZLlq8Cnw5HsCHXYB2N50ZXY8NFFuKi ofqcaK5SBQI6dudcJm21jXzjHvW+e2/8AmIx89WyUrs2tIah3sJpeDjRlJgUQbN8SZVl oanw== X-Gm-Message-State: AOAM530wbDu3tCycJLf7/j02R2UyU5WQ+mK8upRb0t3k6YAsXxLBBM8u XAz8h5PSngPZhXI3u/fJtQHJrQ== X-Google-Smtp-Source: ABdhPJzE6KorrXyvR8/DrCYcXC6lVMeubBsEjGkeOFacWDvrtAQrxwge/nOKRo3E2hg0SgOLGXOVmA== X-Received: by 2002:a17:902:b78b:b0:14f:69f:d6e1 with SMTP id e11-20020a170902b78b00b0014f069fd6e1mr6883593pls.85.1645180027444; Fri, 18 Feb 2022 02:27:07 -0800 (PST) Received: from localhost.localdomain ([210.107.197.32]) by smtp.googlemail.com with ESMTPSA id k13sm2767696pfc.176.2022.02.18.02.27.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 02:27:07 -0800 (PST) From: Jonghyeon Kim To: akpm@linux-foundation.org Cc: Jonghyeon Kim , Jonathan.Cameron@Huawei.com, amit@kernel.org, benh@kernel.crashing.org, corbet@lwn.net, david@redhat.com, dwmw@amazon.com, elver@google.com, foersleo@amazon.de, gthelen@google.com, markubo@amazon.de, rientjes@google.com, shakeelb@google.com, shuah@kernel.org, linux-damon@amazon.com, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v1 3/3] mm/damon/reclaim: Add per NUMA node proactive reclamation by DAMON_RECLAIM. Date: Fri, 18 Feb 2022 19:26:11 +0900 Message-Id: <20220218102611.31895-4-tome01@ajou.ac.kr> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220218102611.31895-1-tome01@ajou.ac.kr> References: <20220218102611.31895-1-tome01@ajou.ac.kr> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" To add DAMON_RECLAIM worker threads(kdamond) that do proactive reclamation per NUMA node, each node must have its own context. 'per_node' is added to enable it. If 'per_node' is true, kdamonds as online NUMA node will be waked up and start monitoring to proactively reclaim memory. If 'per_node' is false, only one kdamond thread will start monitoring for all system memory. Signed-off-by: Jonghyeon Kim --- mm/damon/reclaim.c | 147 ++++++++++++++++++++++++++++++++------------- 1 file changed, 104 insertions(+), 43 deletions(-) diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c index b53d9c22fad1..85e8f97dd599 100644 --- a/mm/damon/reclaim.c +++ b/mm/damon/reclaim.c @@ -177,13 +177,27 @@ static unsigned long monitor_region_end __read_mostly; module_param(monitor_region_end, ulong, 0600); =20 /* - * PID of the DAMON thread + * Enable monitoring memory regions per NUMA node. * - * If DAMON_RECLAIM is enabled, this becomes the PID of the worker thread. + * By default, watermarks consist of based on total system memory. + */ +static bool per_node __read_mostly; +module_param(per_node, bool, 0600); + +/* + * Number of currently running DAMON worker threads + */ +static unsigned long nr_kdamond __read_mostly; +module_param(nr_kdamond, ulong, 0400); + +/* + * First PID of the DAMON threads + * + * If DAMON_RECLAIM is enabled, this becomes the first PID of the worker t= hreads. * Else, -1. */ -static int kdamond_pid __read_mostly =3D -1; -module_param(kdamond_pid, int, 0400); +static int kdamond_start_pid __read_mostly =3D -1; +module_param(kdamond_start_pid, int, 0400); =20 /* * Number of memory regions that tried to be reclaimed. @@ -215,8 +229,8 @@ module_param(bytes_reclaimed_regions, ulong, 0400); static unsigned long nr_quota_exceeds __read_mostly; module_param(nr_quota_exceeds, ulong, 0400); =20 -static struct damon_ctx *ctx; -static struct damon_target *target; +static struct damon_ctx *ctxs[MAX_NUMNODES]; +static struct damon_target *targets[MAX_NUMNODES]; =20 struct damon_reclaim_ram_walk_arg { unsigned long start; @@ -251,7 +265,7 @@ static bool get_monitoring_region(unsigned long *start,= unsigned long *end) return true; } =20 -static struct damos *damon_reclaim_new_scheme(void) +static struct damos *damon_reclaim_new_scheme(int node) { struct damos_watermarks wmarks =3D { .metric =3D DAMOS_WMARK_FREE_MEM_RATE, @@ -259,6 +273,7 @@ static struct damos *damon_reclaim_new_scheme(void) .high =3D wmarks_high, .mid =3D wmarks_mid, .low =3D wmarks_low, + .node =3D node, }; struct damos_quota quota =3D { /* @@ -290,56 +305,99 @@ static struct damos *damon_reclaim_new_scheme(void) return scheme; } =20 -static int damon_reclaim_turn(bool on) +static int damon_reclaim_start(int nid) { struct damon_region *region; struct damos *scheme; int err; + unsigned long start, end; =20 - if (!on) { - err =3D damon_stop(&ctx, 1); - if (!err) - kdamond_pid =3D -1; - return err; - } - - err =3D damon_set_attrs(ctx, sample_interval, aggr_interval, 0, + err =3D damon_set_attrs(ctxs[nid], sample_interval, aggr_interval, 0, min_nr_regions, max_nr_regions); if (err) return err; =20 - if (monitor_region_start > monitor_region_end) - return -EINVAL; - if (!monitor_region_start && !monitor_region_end && - !get_monitoring_region(&monitor_region_start, - &monitor_region_end)) - return -EINVAL; + if (per_node) { + monitor_region_start =3D monitor_region_end =3D 0; + + start =3D PFN_PHYS(node_start_pfn(nid)); + end =3D PFN_PHYS(node_start_pfn(nid) + node_present_pages(nid) - 1); + if (end <=3D start) + return -EINVAL; + } else { + if (!monitor_region_start && !monitor_region_end && + !get_monitoring_region(&monitor_region_start, + &monitor_region_end)) + return -EINVAL; + start =3D monitor_region_start; + end =3D monitor_region_end; + } + /* DAMON will free this on its own when finish monitoring */ - region =3D damon_new_region(monitor_region_start, monitor_region_end); + region =3D damon_new_region(start, end); if (!region) return -ENOMEM; - damon_add_region(region, target); + damon_add_region(region, targets[nid]); =20 /* Will be freed by 'damon_set_schemes()' below */ - scheme =3D damon_reclaim_new_scheme(); + scheme =3D damon_reclaim_new_scheme(nid); if (!scheme) { err =3D -ENOMEM; goto free_region_out; } - err =3D damon_set_schemes(ctx, &scheme, 1); + + err =3D damon_set_schemes(ctxs[nid], &scheme, 1); if (err) goto free_scheme_out; =20 - err =3D damon_start(&ctx, 1); + err =3D damon_start_one(ctxs[nid]); if (!err) { - kdamond_pid =3D ctx->kdamond->pid; + if (kdamond_start_pid =3D=3D -1) + kdamond_start_pid =3D ctxs[nid]->kdamond->pid; + nr_kdamond++; return 0; } =20 free_scheme_out: damon_destroy_scheme(scheme); free_region_out: - damon_destroy_region(region, target); + damon_destroy_region(region, targets[nid]); + + return err; +} + +static int damon_reclaim_start_all(void) +{ + int nid, err; + + if (!per_node) + return damon_reclaim_start(0); + + for_each_online_node(nid) { + err =3D damon_reclaim_start(nid); + if (err) + break; + } + + return err; +} + +static int damon_reclaim_turn(bool on) +{ + int err; + + if (!on) { + err =3D damon_stop(ctxs, nr_kdamond); + if (!err) { + kdamond_start_pid =3D -1; + nr_kdamond =3D 0; + monitor_region_start =3D 0; + monitor_region_end =3D 0; + } + return err; + } + + err =3D damon_reclaim_start_all(); return err; } =20 @@ -380,21 +438,24 @@ static int damon_reclaim_after_aggregation(struct dam= on_ctx *c) =20 static int __init damon_reclaim_init(void) { - ctx =3D damon_new_ctx(); - if (!ctx) - return -ENOMEM; - - if (damon_select_ops(ctx, DAMON_OPS_PADDR)) - return -EINVAL; - - ctx->callback.after_aggregation =3D damon_reclaim_after_aggregation; - - target =3D damon_new_target(); - if (!target) { - damon_destroy_ctx(ctx); - return -ENOMEM; + int nid; + + for_each_node(nid) { + ctxs[nid] =3D damon_new_ctx(); + if (!ctxs[nid]) + return -ENOMEM; + + if (damon_select_ops(ctxs[nid], DAMON_OPS_PADDR)) + return -EINVAL; + ctxs[nid]->callback.after_aggregation =3D damon_reclaim_after_aggregatio= n; + + targets[nid] =3D damon_new_target(); + if (!targets[nid]) { + damon_destroy_ctx(ctxs[nid]); + return -ENOMEM; + } + damon_add_target(ctxs[nid], targets[nid]); } - damon_add_target(ctx, target); =20 schedule_delayed_work(&damon_reclaim_timer, 0); return 0; --=20 2.17.1