From patchwork Tue Mar 15 16:37:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: haoxin X-Patchwork-Id: 12781645 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 458FCC433EF for ; Tue, 15 Mar 2022 16:37:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A3938D0006; Tue, 15 Mar 2022 12:37:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E3898D0005; Tue, 15 Mar 2022 12:37:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 67F928D0006; Tue, 15 Mar 2022 12:37:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id 45BDD8D0001 for ; Tue, 15 Mar 2022 12:37:18 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 11F3460ED6 for ; Tue, 15 Mar 2022 16:37:18 +0000 (UTC) X-FDA: 79247175756.14.2EE1AA4 Received: from out30-42.freemail.mail.aliyun.com (out30-42.freemail.mail.aliyun.com [115.124.30.42]) by imf10.hostedemail.com (Postfix) with ESMTP id 1E4FFC0015 for ; Tue, 15 Mar 2022 16:37:16 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R301e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e01424;MF=xhao@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0V7IiZEe_1647362233; Received: from localhost.localdomain(mailfrom:xhao@linux.alibaba.com fp:SMTPD_---0V7IiZEe_1647362233) by smtp.aliyun-inc.com(127.0.0.1); Wed, 16 Mar 2022 00:37:13 +0800 From: Xin Hao To: sj@kernel.org Cc: xhao@linux.alibaba.com, rongwei.wang@linux.alibaba.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH V1 3/3] mm/damon/sysfs: Add CMA memory monitoring Date: Wed, 16 Mar 2022 00:37:07 +0800 Message-Id: <0325c53c46291f96e6d99223fc4d2d8454de5d97.1647378112.git.xhao@linux.alibaba.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 1E4FFC0015 X-Rspam-User: Authentication-Results: imf10.hostedemail.com; dkim=none; spf=pass (imf10.hostedemail.com: domain of xhao@linux.alibaba.com designates 115.124.30.42 as permitted sender) smtp.mailfrom=xhao@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com X-Stat-Signature: 9913z7ibf758c97xwdxem1rjxgt6hzko X-Rspamd-Server: rspam07 X-HE-Tag: 1647362236-193700 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Users can do the CMA memory monitoring by writing a special keyword 'cma' to the 'operations' sysfs file. Then, DAMON will check the special keyword and configure the monitoring context to run with the CMA reserved physical address space. Unlike other physical memorys monitoring, the monitoring target region will be automatically set. Signed-off-by: Xin Hao --- include/linux/damon.h | 1 + mm/damon/Makefile | 2 +- mm/damon/paddr-cma.c | 104 ++++++++++++++++++++++++++++++++++++++++++ mm/damon/sysfs.c | 1 + 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 mm/damon/paddr-cma.c diff --git a/include/linux/damon.h b/include/linux/damon.h index f23cbfa4248d..27eaa6d6c43a 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -266,6 +266,7 @@ struct damos { enum damon_ops_id { DAMON_OPS_VADDR, DAMON_OPS_PADDR, + DAMON_OPS_CMA, NR_DAMON_OPS, }; diff --git a/mm/damon/Makefile b/mm/damon/Makefile index dbf7190b4144..d32048f70f6d 100644 --- a/mm/damon/Makefile +++ b/mm/damon/Makefile @@ -2,7 +2,7 @@ obj-y := core.o obj-$(CONFIG_DAMON_VADDR) += ops-common.o vaddr.o -obj-$(CONFIG_DAMON_PADDR) += ops-common.o paddr.o +obj-$(CONFIG_DAMON_PADDR) += ops-common.o paddr.o paddr-cma.o obj-$(CONFIG_DAMON_SYSFS) += sysfs.o obj-$(CONFIG_DAMON_DBGFS) += dbgfs.o obj-$(CONFIG_DAMON_RECLAIM) += reclaim.o diff --git a/mm/damon/paddr-cma.c b/mm/damon/paddr-cma.c new file mode 100644 index 000000000000..ad422854c8c6 --- /dev/null +++ b/mm/damon/paddr-cma.c @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * DAMON Primitives for The CMA Physical Address Space + * + * Author: Xin Hao + */ +#ifdef CONFIG_CMA + +#define pr_fmt(fmt) "damon-cma: " fmt + +#include + +#include "ops-common.h" +#include "../cma.h" + +static int damon_cma_area_regions(struct damon_addr_range *regions, int nr_cma_area) +{ + int i; + + if (!nr_cma_area || !regions) + return -EINVAL; + + for (i = 0; i < nr_cma_area; i++) { + phys_addr_t base = cma_get_base(&cma_areas[i]); + + regions[i].start = base; + regions[i].end = base + cma_get_size(&cma_areas[i]); + } + + return 0; +} + +static void __damon_cma_init_regions(struct damon_ctx *ctx, + struct damon_target *t) +{ + struct damon_target *ti; + struct damon_region *r; + struct damon_addr_range regions[MAX_CMA_AREAS]; + unsigned long sz = 0, nr_pieces; + int i, tidx = 0; + + if (damon_cma_area_regions(regions, cma_area_count)) { + damon_for_each_target(ti, ctx) { + if (ti == t) + break; + tidx++; + } + pr_err("Failed to get CMA regions of %dth target\n", tidx); + return; + } + + for (i = 0; i < cma_area_count; i++) + sz += regions[i].end - regions[i].start; + if (ctx->min_nr_regions) + sz /= ctx->min_nr_regions; + if (sz < DAMON_MIN_REGION) + sz = DAMON_MIN_REGION; + + /* Set the initial three regions of the target */ + for (i = 0; i < cma_area_count; i++) { + r = damon_new_region(regions[i].start, regions[i].end); + if (!r) { + pr_err("%d'th init region creation failed\n", i); + return; + } + damon_add_region(r, t); + + nr_pieces = (regions[i].end - regions[i].start) / sz; + damon_evenly_split_region(t, r, nr_pieces); + } +} + +static void damon_cma_init(struct damon_ctx *ctx) +{ + struct damon_target *t; + + damon_for_each_target(t, ctx) { + /* the user may set the target regions as they want */ + if (!damon_nr_regions(t)) + __damon_cma_init_regions(ctx, t); + } +} + +static int __init damon_cma_initcall(void) +{ + struct damon_operations ops = { + .id = DAMON_OPS_CMA, + .init = damon_cma_init, + .update = NULL, + .prepare_access_checks = damon_pa_prepare_access_checks, + .check_accesses = damon_pa_check_accesses, + .reset_aggregated = NULL, + .target_valid = NULL, + .cleanup = NULL, + .apply_scheme = damon_pa_apply_scheme, + .get_scheme_score = damon_pa_scheme_score, + }; + + return damon_register_ops(&ops); +}; + +subsys_initcall(damon_cma_initcall); + +#endif /* CONFIG_CMA */ diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index d39f74969469..8a34880cc2c4 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1761,6 +1761,7 @@ static struct kobj_type damon_sysfs_attrs_ktype = { static const char * const damon_sysfs_ops_strs[] = { "vaddr", "paddr", + "cma", }; struct damon_sysfs_context {