From patchwork Mon Sep 28 10:35:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11803367 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1A1806CB for ; Mon, 28 Sep 2020 10:37:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8CB8B21531 for ; Mon, 28 Sep 2020 10:37:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="gPQsB30u" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8CB8B21531 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amazon.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B0A0D6B0062; Mon, 28 Sep 2020 06:37:26 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id ABBD38E0001; Mon, 28 Sep 2020 06:37:26 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9AABB6B006E; Mon, 28 Sep 2020 06:37:26 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0172.hostedemail.com [216.40.44.172]) by kanga.kvack.org (Postfix) with ESMTP id 813E56B0062 for ; Mon, 28 Sep 2020 06:37:26 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 38779282D for ; Mon, 28 Sep 2020 10:37:26 +0000 (UTC) X-FDA: 77312118492.22.rule71_340948f27180 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id 0C5E918038E60 for ; Mon, 28 Sep 2020 10:37:26 +0000 (UTC) X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=5337e4f7a=sjpark@amazon.com,,RULES_HIT:30003:30012:30034:30051:30054:30064:30070:30075:30080,0,RBL:207.171.184.29:@amazon.com:.lbl8.mailshell.net-66.10.201.10 62.18.0.100;04y8hj1iyqanzgpycyk8kozyndfthypofo7q5747npt45a98yujttnomspkqe8k.ao57mkamqduijcbdz4sumu3qweboxbrcqff6r8ojryd5w5qwuptybprcy6xzm95.r-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: rule71_340948f27180 X-Filterd-Recvd-Size: 29980 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf40.hostedemail.com (Postfix) with ESMTP for ; Mon, 28 Sep 2020 10:37:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1601289446; x=1632825446; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=qvrhquTl8+AtxVLoOsq6PQfl9fxedokru3rzzkYbCIA=; b=gPQsB30uCoOh/gHfSUD90s2tVs/ETYF/A/DveaPfW5pJbC4Jw0yngC7y XnlibKmeZXUUVOGn4PhAWRUEMPXicyewwxtVmfypiFKfWcRZya56tHca5 4uRZgirUTGhS5vBzNOQ9hHOEPkUJJCyb9AgU2vzpoEW4zZXe5b1dL3txX c=; X-IronPort-AV: E=Sophos;i="5.77,313,1596499200"; d="scan'208";a="79768620" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-5dd976cd.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 28 Sep 2020 10:37:20 +0000 Received: from EX13D31EUA001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-1d-5dd976cd.us-east-1.amazon.com (Postfix) with ESMTPS id 8BE06A1CF1; Mon, 28 Sep 2020 10:37:06 +0000 (UTC) Received: from u3f2cd687b01c55.ant.amazon.com (10.43.161.237) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 28 Sep 2020 10:36:47 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 3/5] mm/damon: Move recording feature from core to dbgfs Date: Mon, 28 Sep 2020 12:35:26 +0200 Message-ID: <20200928103528.4256-4-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200928103528.4256-1-sjpark@amazon.com> References: <20200928103528.4256-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.161.237] X-ClientProxiedBy: EX13D10UWA003.ant.amazon.com (10.43.160.248) To EX13D31EUA001.ant.amazon.com (10.43.165.15) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: SeongJae Park DAMON passes the monitoring results to user space via two ways: 1) a tracepoint and 2) it's recording feature. The recording feature is for the users who want simplest use. However, as the feature is for the user space only while the core is fundamentally a framework for the kernel space, keeping the feature in the core would make no sense. Therefore, this commit moves the feature to the debugfs interface of DAMON. Signed-off-by: SeongJae Park --- include/linux/damon.h | 23 +--- mm/damon/core-test.h | 57 ++------- mm/damon/core.c | 150 +----------------------- mm/damon/dbgfs-test.h | 87 +++++++++++++- mm/damon/dbgfs.c | 264 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 359 insertions(+), 222 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 505e6261cefa..606e59f785a2 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -134,14 +134,6 @@ struct damos { * in case of virtual memory monitoring) and applies the changes for each * @regions_update_interval. All time intervals are in micro-seconds. * - * @rbuf: In-memory buffer for monitoring result recording. - * @rbuf_len: The length of @rbuf. - * @rbuf_offset: The offset for next write to @rbuf. - * @rfile_path: Record file path. - * - * If @rbuf, @rbuf_len, and @rfile_path are set, the monitored results are - * automatically stored in @rfile_path file. - * * @kdamond: Kernel thread who does the monitoring. * @kdamond_stop: Notifies whether kdamond should stop. * @kdamond_lock: Mutex for the synchronizations with @kdamond. @@ -164,6 +156,8 @@ struct damos { * @targets_list: Head of monitoring targets (&damon_target) list. * @schemes_list: Head of schemes (&damos) list. * + * @private Private user data. + * * @init_target_regions: Constructs initial monitoring target regions. * @update_target_regions: Updates monitoring target regions. * @prepare_access_checks: Prepares next access check of target regions. @@ -214,11 +208,6 @@ struct damon_ctx { struct timespec64 last_aggregation; struct timespec64 last_regions_update; - unsigned char *rbuf; - unsigned int rbuf_len; - unsigned int rbuf_offset; - char *rfile_path; - struct task_struct *kdamond; bool kdamond_stop; struct mutex kdamond_lock; @@ -226,6 +215,8 @@ struct damon_ctx { struct list_head targets_list; /* 'damon_target' objects */ struct list_head schemes_list; /* 'damos' objects */ + void *private; + /* callbacks */ void (*init_target_regions)(struct damon_ctx *context); void (*update_target_regions)(struct damon_ctx *context); @@ -241,10 +232,6 @@ struct damon_ctx { #ifdef CONFIG_DAMON -#define MIN_RECORD_BUFFER_LEN 1024 -#define MAX_RECORD_BUFFER_LEN (4 * 1024 * 1024) -#define MAX_RFILE_PATH_LEN 256 - #define damon_next_region(r) \ (container_of(r->list.next, struct damon_region, list)) @@ -298,8 +285,6 @@ int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, unsigned long min_nr_reg, unsigned long max_nr_reg); int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes, ssize_t nr_schemes); -int damon_set_recording(struct damon_ctx *ctx, - unsigned int rbuf_len, char *rfile_path); int damon_nr_running_ctxs(void); int damon_start(struct damon_ctx *ctxs, int nr_ctxs); diff --git a/mm/damon/core-test.h b/mm/damon/core-test.h index c916d773397a..b815dfbfb5fd 100644 --- a/mm/damon/core-test.h +++ b/mm/damon/core-test.h @@ -36,6 +36,17 @@ static void damon_test_regions(struct kunit *test) damon_free_target(t); } +static unsigned int nr_damon_targets(struct damon_ctx *ctx) +{ + struct damon_target *t; + unsigned int nr_targets = 0; + + damon_for_each_target(t, ctx) + nr_targets++; + + return nr_targets; +} + static void damon_test_target(struct kunit *test) { struct damon_ctx *c = damon_new_ctx(); @@ -54,23 +65,6 @@ static void damon_test_target(struct kunit *test) damon_destroy_ctx(c); } -static void damon_test_set_recording(struct kunit *test) -{ - struct damon_ctx *ctx = damon_new_ctx(); - int err; - - err = damon_set_recording(ctx, 42, "foo"); - KUNIT_EXPECT_EQ(test, err, -EINVAL); - damon_set_recording(ctx, 4242, "foo.bar"); - KUNIT_EXPECT_EQ(test, ctx->rbuf_len, 4242u); - KUNIT_EXPECT_STREQ(test, ctx->rfile_path, "foo.bar"); - damon_set_recording(ctx, 424242, "foo"); - KUNIT_EXPECT_EQ(test, ctx->rbuf_len, 424242u); - KUNIT_EXPECT_STREQ(test, ctx->rfile_path, "foo"); - - damon_destroy_ctx(ctx); -} - /* * Test kdamond_reset_aggregated() * @@ -91,9 +85,7 @@ static void damon_test_aggregate(struct kunit *test) struct damon_target *t; struct damon_region *r; int it, ir; - ssize_t sz, sr, sp; - damon_set_recording(ctx, 4242, "damon.data"); damon_set_targets(ctx, target_ids, 3); it = 0; @@ -121,31 +113,6 @@ static void damon_test_aggregate(struct kunit *test) /* targets also should be preserved */ KUNIT_EXPECT_EQ(test, 3, it); - /* The aggregated information should be written in the buffer */ - sr = sizeof(r->ar.start) + sizeof(r->ar.end) + sizeof(r->nr_accesses); - sp = sizeof(t->id) + sizeof(unsigned int) + 3 * sr; - sz = sizeof(struct timespec64) + sizeof(unsigned int) + 3 * sp; - KUNIT_EXPECT_EQ(test, (unsigned int)sz, ctx->rbuf_offset); - - damon_destroy_ctx(ctx); -} - -static void damon_test_write_rbuf(struct kunit *test) -{ - struct damon_ctx *ctx = damon_new_ctx(); - char *data; - - damon_set_recording(ctx, 4242, "damon.data"); - - data = "hello"; - damon_write_rbuf(ctx, data, strnlen(data, 256)); - KUNIT_EXPECT_EQ(test, ctx->rbuf_offset, 5u); - - damon_write_rbuf(ctx, data, 0); - KUNIT_EXPECT_EQ(test, ctx->rbuf_offset, 5u); - - KUNIT_EXPECT_STREQ(test, (char *)ctx->rbuf, data); - damon_destroy_ctx(ctx); } @@ -267,9 +234,7 @@ static void damon_test_split_regions_of(struct kunit *test) static struct kunit_case damon_test_cases[] = { KUNIT_CASE(damon_test_target), KUNIT_CASE(damon_test_regions), - KUNIT_CASE(damon_test_set_recording), KUNIT_CASE(damon_test_aggregate), - KUNIT_CASE(damon_test_write_rbuf), KUNIT_CASE(damon_test_split_at), KUNIT_CASE(damon_test_merge_two), KUNIT_CASE(damon_test_merge_regions_of), diff --git a/mm/damon/core.c b/mm/damon/core.c index ba52421a2673..ba0035d7a27a 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -208,11 +208,6 @@ struct damon_ctx *damon_new_ctx(void) ktime_get_coarse_ts64(&ctx->last_aggregation); ctx->last_regions_update = ctx->last_aggregation; - if (damon_set_recording(ctx, 0, "none")) { - kfree(ctx); - return NULL; - } - mutex_init(&ctx->kdamond_lock); INIT_LIST_HEAD(&ctx->targets_list); @@ -328,54 +323,6 @@ int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes, return 0; } -/** - * damon_set_recording() - Set attributes for the recording. - * @ctx: target kdamond context - * @rbuf_len: length of the result buffer - * @rfile_path: path to the monitor result files - * - * Setting 'rbuf_len' 0 disables recording. - * - * This function should not be called while the kdamond is running. - * - * Return: 0 on success, negative error code otherwise. - */ -int damon_set_recording(struct damon_ctx *ctx, - unsigned int rbuf_len, char *rfile_path) -{ - size_t rfile_path_len; - - if (rbuf_len && (rbuf_len > MAX_RECORD_BUFFER_LEN || - rbuf_len < MIN_RECORD_BUFFER_LEN)) { - pr_err("result buffer size (%u) is out of [%d,%d]\n", - rbuf_len, MIN_RECORD_BUFFER_LEN, - MAX_RECORD_BUFFER_LEN); - return -EINVAL; - } - rfile_path_len = strnlen(rfile_path, MAX_RFILE_PATH_LEN); - if (rfile_path_len >= MAX_RFILE_PATH_LEN) { - pr_err("too long (>%d) result file path %s\n", - MAX_RFILE_PATH_LEN, rfile_path); - return -EINVAL; - } - ctx->rbuf_len = rbuf_len; - kfree(ctx->rbuf); - ctx->rbuf = NULL; - kfree(ctx->rfile_path); - ctx->rfile_path = NULL; - - if (rbuf_len) { - ctx->rbuf = kvmalloc(rbuf_len, GFP_KERNEL); - if (!ctx->rbuf) - return -ENOMEM; - } - ctx->rfile_path = kmalloc(rfile_path_len + 1, GFP_KERNEL); - if (!ctx->rfile_path) - return -ENOMEM; - strncpy(ctx->rfile_path, rfile_path, rfile_path_len + 1); - return 0; -} - /** * damon_nr_running_ctxs() - Return number of currently running contexts. */ @@ -390,17 +337,6 @@ int damon_nr_running_ctxs(void) return nr_ctxs; } -static unsigned int nr_damon_targets(struct damon_ctx *ctx) -{ - struct damon_target *t; - unsigned int nr_targets = 0; - - damon_for_each_target(t, ctx) - nr_targets++; - - return nr_targets; -} - /* Returns the size upper limit for each monitoring region */ static unsigned long damon_region_sz_limit(struct damon_ctx *ctx) { @@ -613,87 +549,18 @@ static bool kdamond_aggregate_interval_passed(struct damon_ctx *ctx) } /* - * Flush the content in the result buffer to the result file - */ -static void damon_flush_rbuffer(struct damon_ctx *ctx) -{ - ssize_t sz; - loff_t pos = 0; - struct file *rfile; - - if (!ctx->rbuf_offset) - return; - - rfile = filp_open(ctx->rfile_path, - O_CREAT | O_RDWR | O_APPEND | O_LARGEFILE, 0644); - if (IS_ERR(rfile)) { - pr_err("Cannot open the result file %s\n", - ctx->rfile_path); - return; - } - - while (ctx->rbuf_offset) { - sz = kernel_write(rfile, ctx->rbuf, ctx->rbuf_offset, &pos); - if (sz < 0) - break; - ctx->rbuf_offset -= sz; - } - filp_close(rfile, NULL); -} - -/* - * Write a data into the result buffer - */ -static void damon_write_rbuf(struct damon_ctx *ctx, void *data, ssize_t size) -{ - if (!ctx->rbuf_len || !ctx->rbuf || !ctx->rfile_path) - return; - if (ctx->rbuf_offset + size > ctx->rbuf_len) - damon_flush_rbuffer(ctx); - if (ctx->rbuf_offset + size > ctx->rbuf_len) { - pr_warn("%s: flush failed, or wrong size given(%u, %zu)\n", - __func__, ctx->rbuf_offset, size); - return; - } - - memcpy(&ctx->rbuf[ctx->rbuf_offset], data, size); - ctx->rbuf_offset += size; -} - -/* - * Flush the aggregated monitoring results to the result buffer - * - * Stores current tracking results to the result buffer and reset 'nr_accesses' - * of each region. The format for the result buffer is as below: - * - *