From patchwork Tue Mar 3 12:14:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11417857 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 64E7714BC for ; Tue, 3 Mar 2020 12:16:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 246AF20857 for ; Tue, 3 Mar 2020 12:16:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="t1n/Hy8J" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 246AF20857 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 6CCD66B0006; Tue, 3 Mar 2020 07:16:38 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 67B896B0007; Tue, 3 Mar 2020 07:16:38 -0500 (EST) 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 591A16B0008; Tue, 3 Mar 2020 07:16:38 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0036.hostedemail.com [216.40.44.36]) by kanga.kvack.org (Postfix) with ESMTP id 408BE6B0006 for ; Tue, 3 Mar 2020 07:16:38 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 0F00E16972 for ; Tue, 3 Mar 2020 12:16:38 +0000 (UTC) X-FDA: 76553949276.28.cent72_740f987fc0002 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=3245da543=sjpark@amazon.com,,RULES_HIT:30004:30051:30054:30064:30070,0,RBL:207.171.184.29:@amazon.com:.lbl8.mailshell.net-62.18.0.100 66.10.201.10,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:20,LUA_SUMMARY:none X-HE-Tag: cent72_740f987fc0002 X-Filterd-Recvd-Size: 9137 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Tue, 3 Mar 2020 12:16:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1583237798; x=1614773798; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=wWPNf3nGw9wlLtyaJpYAIf9ULI1iJPBTHwepVG7o1EI=; b=t1n/Hy8JlTtk1AeAbxiiNuA6ZlgivVcj+h2aJxzLUMRny9vX4Pihs8P7 Xaa7rM9NDbojPDfC9G9QglG0QnqPtP42mFBjdB+9z7ejzQdVbdBSq1yuk BVWjpM3yAmD9e4iNqfIB8N3tqe9wLzOvQb7ileYfwuOJ59kx9l+LGZajt E=; IronPort-SDR: bvWcvymk+veNqpYIDRaU2mB2aXbaxU48TaRv3c3AXUDdsM19lo4se8JKQ5wPKxB9L+Xqrj2djV oEWaVta6sAqg== X-IronPort-AV: E=Sophos;i="5.70,511,1574121600"; d="scan'208";a="28845743" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2c-168cbb73.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 03 Mar 2020 12:16:36 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2c-168cbb73.us-west-2.amazon.com (Postfix) with ESMTPS id 8DFC3A264F; Tue, 3 Mar 2020 12:16:34 +0000 (UTC) Received: from EX13D31EUA001.ant.amazon.com (10.43.165.15) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Tue, 3 Mar 2020 12:16:34 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.16) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 3 Mar 2020 12:16:22 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v4 4/7] mm/damon/schemes: Implement a debugfs interface Date: Tue, 3 Mar 2020 13:14:03 +0100 Message-ID: <20200303121406.20954-5-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200303121406.20954-1-sjpark@amazon.com> References: <20200303121406.20954-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.160.16] X-ClientProxiedBy: EX13D22UWB002.ant.amazon.com (10.43.161.28) 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 This commit implements a debugfs interface for the data access monitoring oriented memory management schemes. It is supposed to be used by administrators and/or privileged user space programs. Users can read and update the rules using ``/damon/schemes`` file. The format is:: Signed-off-by: SeongJae Park --- mm/damon.c | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 2 deletions(-) diff --git a/mm/damon.c b/mm/damon.c index 338e7ea76c7f..c573a0290234 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -199,6 +199,29 @@ static void damon_destroy_task(struct damon_task *t) damon_free_task(t); } +static struct damos *damon_new_scheme( + unsigned int min_sz_region, unsigned int max_sz_region, + unsigned int min_nr_accesses, unsigned int max_nr_accesses, + unsigned int min_age_region, unsigned int max_age_region, + enum damos_action action) +{ + struct damos *ret; + + ret = kmalloc(sizeof(struct damos), GFP_KERNEL); + if (!ret) + return NULL; + ret->min_sz_region = min_sz_region; + ret->max_sz_region = max_sz_region; + ret->min_nr_accesses = min_nr_accesses; + ret->max_nr_accesses = max_nr_accesses; + ret->min_age_region = min_age_region; + ret->max_age_region = max_age_region; + ret->action = action; + INIT_LIST_HEAD(&ret->list); + + return ret; +} + static void damon_add_scheme(struct damon_ctx *ctx, struct damos *s) { list_add_tail(&s->list, &ctx->schemes_list); @@ -1306,6 +1329,144 @@ static ssize_t debugfs_monitor_on_write(struct file *file, return ret; } +static ssize_t sprint_schemes(struct damon_ctx *c, char *buf, ssize_t len) +{ + struct damos *s; + int written = 0; + int rc; + + damon_for_each_schemes(c, s) { + rc = snprintf(&buf[written], len - written, + "%u %u %u %u %u %u %d\n", + s->min_sz_region, s->max_sz_region, + s->min_nr_accesses, s->max_nr_accesses, + s->min_age_region, s->max_age_region, + s->action); + if (!rc) + return -ENOMEM; + written += rc; + } + return written; +} + +static ssize_t debugfs_schemes_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct damon_ctx *ctx = &damon_user_ctx; + char *kbuf; + ssize_t ret; + + kbuf = kmalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + ret = sprint_schemes(ctx, kbuf, count); + if (ret < 0) + goto out; + ret = simple_read_from_buffer(buf, count, ppos, kbuf, ret); + +out: + kfree(kbuf); + return ret; +} + +static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes) +{ + ssize_t i; + + for (i = 0; i < nr_schemes; i++) + kfree(schemes[i]); + kfree(schemes); +} + +/* + * Converts a string into an array of struct damos pointers + * + * Returns an array of struct damos pointers that converted if the conversion + * success, or NULL otherwise. + */ +static struct damos **str_to_schemes(const char *str, ssize_t len, + ssize_t *nr_schemes) +{ + struct damos *scheme, **schemes; + const int max_nr_schemes = 256; + int pos = 0, parsed, ret; + unsigned int min_sz, max_sz, min_nr_a, max_nr_a, min_age, max_age; + int action; + + schemes = kmalloc_array(max_nr_schemes, sizeof(struct damos *), + GFP_KERNEL); + if (!schemes) + return NULL; + + *nr_schemes = 0; + while (pos < len && *nr_schemes < max_nr_schemes) { + ret = sscanf(&str[pos], "%u %u %u %u %u %u %d%n", + &min_sz, &max_sz, &min_nr_a, &max_nr_a, + &min_age, &max_age, &action, &parsed); + pos += parsed; + if (ret != 7) + break; + if (action >= DAMOS_ACTION_LEN) { + pr_err("wrong action %d\n", action); + goto fail; + } + + scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a, + min_age, max_age, action); + if (!scheme) + goto fail; + + schemes[*nr_schemes] = scheme; + *nr_schemes += 1; + } + if (!*nr_schemes) + goto fail; + return schemes; +fail: + free_schemes_arr(schemes, *nr_schemes); + return NULL; +} + +static ssize_t debugfs_schemes_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct damon_ctx *ctx = &damon_user_ctx; + char *kbuf; + struct damos **schemes; + ssize_t nr_schemes = 0, ret; + + if (*ppos) + return -EINVAL; + + kbuf = kmalloc_array(count, sizeof(char), GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + ret = simple_write_to_buffer(kbuf, count, ppos, buf, count); + if (ret < 0) + goto out; + + schemes = str_to_schemes(kbuf, ret, &nr_schemes); + + spin_lock(&ctx->kdamond_lock); + if (ctx->kdamond) + goto monitor_running; + + damon_set_schemes(ctx, schemes, nr_schemes); + spin_unlock(&ctx->kdamond_lock); + goto out; + +monitor_running: + spin_unlock(&ctx->kdamond_lock); + pr_err("%s: kdamond is running. Turn it off first.\n", __func__); + ret = -EINVAL; + free_schemes_arr(schemes, nr_schemes); +out: + kfree(kbuf); + return ret; +} + static ssize_t damon_sprint_pids(struct damon_ctx *ctx, char *buf, ssize_t len) { struct damon_task *t; @@ -1536,6 +1697,12 @@ static const struct file_operations pids_fops = { .write = debugfs_pids_write, }; +static const struct file_operations schemes_fops = { + .owner = THIS_MODULE, + .read = debugfs_schemes_read, + .write = debugfs_schemes_write, +}; + static const struct file_operations record_fops = { .owner = THIS_MODULE, .read = debugfs_record_read, @@ -1552,10 +1719,10 @@ static struct dentry *debugfs_root; static int __init debugfs_init(void) { - const char * const file_names[] = {"attrs", "record", + const char * const file_names[] = {"attrs", "record", "schemes", "pids", "monitor_on"}; const struct file_operations *fops[] = {&attrs_fops, &record_fops, - &pids_fops, &monitor_on_fops}; + &schemes_fops, &pids_fops, &monitor_on_fops}; int i; debugfs_root = debugfs_create_dir("damon", NULL);