Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4213293ybb; Tue, 7 Apr 2020 03:04:49 -0700 (PDT) X-Google-Smtp-Source: APiQypILEAHnLFhK0yM/UwjtsmzPCX/ZHMWvtlxH9yyWOUfkllB3rqVzr37YJxQ+9rCrNO92Wetf X-Received: by 2002:aca:d705:: with SMTP id o5mr1080592oig.67.1586253888789; Tue, 07 Apr 2020 03:04:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586253888; cv=none; d=google.com; s=arc-20160816; b=HrGugyKwR8VGtK5sfmXtAJiATiGJ5fSKJtYIwad+sqKMmyWjFkvUvfl+rF2TphmCSI I+ZmkG8TtOUfEy5F91n42yDa7+zGANYjdT1ZOdw1QPbmE2+TQVLS9sGsr4Jaorm6WBK9 HIDjMr6tS50Ra89SJcg4s+uVhbP7Adt5wjL1OtRPtDyNXw4jTsVraOz3ZQEMy0nEmnAS 6X7sOYNYJmGAM5dc80b4aRnZZl/JgpTjrYKjJbihQxToVyqn9DCEtCzgjNnsC0dUoegr VGfzmC5mxszp3iU7C4yVvkic910haA1kshvSTuN7/JdgQD2TjgdE8Yubs/BMp/ILwKKl 4OCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:ironport-sdr:dkim-signature; bh=D+STFnpGXD/4Y9ML30yttOR7ram3iPKns/QOdaRmGsc=; b=Pyeb2Xe3g0fn4Ny/TqAu93KlPhuCyEInQoVMKkr+AHz6hUFRlyKoB3K3cWHol7JY3Y oVnEnWwsjd+UKH8NANKKao40qTlS2U4sLkWD35sL7dX8N8wOGtGZW2+p//p+JpE1EQH3 6FLBcuFjkNY9Z+PTv0ncCV5665d3TQgKoFfnPJOQNUUKZpcQc9w98EFo0WDzvSU9FBfk qrQ5CK0vwcj1MAqpRpj/HBW46QY/bMWfMKjTNYjrgrv3fO5HNQ5MQw4aOfV7dND6xsr7 DEDaAGT9YjM/ruKZmRTVpz4wLUrhxnVHdHDJv/h2YBikgEDnJkhNXfhvFOdOdNpsjcRP 2BSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="jW/3fX6w"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i184si947991oia.14.2020.04.07.03.04.36; Tue, 07 Apr 2020 03:04:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="jW/3fX6w"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728182AbgDGKCr (ORCPT + 99 others); Tue, 7 Apr 2020 06:02:47 -0400 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:43712 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726725AbgDGKCr (ORCPT ); Tue, 7 Apr 2020 06:02:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1586253766; x=1617789766; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=D+STFnpGXD/4Y9ML30yttOR7ram3iPKns/QOdaRmGsc=; b=jW/3fX6wm+WlcEHAR3UdhKwhoWDX2WUuAHJpUQqGuQl9D1o7wfYz8eF7 LpCZdIg0RHfTAiWxxfIx/ickjjGSJ72OJYEVoSg6hMuu2ja02bqfccagG zyL3kPwK0hlAirr0TIZ1x07sIBwg+hjxaKH+pAUmVQSORUgueZh62/Mu6 M=; IronPort-SDR: UDrtgw7oR0y5RbG5g2/h+OirvIyAiAMPG3w/+RqnK3bz0YBEf7OW63rNE/HwTKbef4FvrsfA2W Foe6yYZ6zzjQ== X-IronPort-AV: E=Sophos;i="5.72,353,1580774400"; d="scan'208";a="27348697" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2b-55156cd4.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 07 Apr 2020 10:02:45 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2b-55156cd4.us-west-2.amazon.com (Postfix) with ESMTPS id 1FE4BA1B12; Tue, 7 Apr 2020 10:02:43 +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.1497.2; Tue, 7 Apr 2020 10:02:42 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.171) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 7 Apr 2020 10:02:28 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v6 4/7] mm/damon/schemes: Implement a debugfs interface Date: Tue, 7 Apr 2020 12:00:03 +0200 Message-ID: <20200407100007.3894-5-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200407100007.3894-1-sjpark@amazon.com> References: <20200407100007.3894-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.162.171] X-ClientProxiedBy: EX13D34UWA002.ant.amazon.com (10.43.160.245) To EX13D31EUA001.ant.amazon.com (10.43.165.15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 172 insertions(+), 2 deletions(-) diff --git a/mm/damon.c b/mm/damon.c index 3c10ff3a487c..3f93da898d72 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -180,6 +180,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 *scheme; + + scheme = kmalloc(sizeof(*scheme), GFP_KERNEL); + if (!scheme) + return NULL; + scheme->min_sz_region = min_sz_region; + scheme->max_sz_region = max_sz_region; + scheme->min_nr_accesses = min_nr_accesses; + scheme->max_nr_accesses = max_nr_accesses; + scheme->min_age_region = min_age_region; + scheme->max_age_region = max_age_region; + scheme->action = action; + INIT_LIST_HEAD(&scheme->list); + + return scheme; +} + static void damon_add_scheme(struct damon_ctx *ctx, struct damos *s) { list_add_tail(&s->list, &ctx->schemes_list); @@ -1392,6 +1415,147 @@ 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 len; + + kbuf = kmalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + len = sprint_schemes(ctx, kbuf, count); + if (len < 0) + goto out; + len = simple_read_from_buffer(buf, count, ppos, kbuf, len); + +out: + kfree(kbuf); + return len; +} + +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(scheme), + 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); + if (ret != 7) + break; + if (action >= DAMOS_ACTION_LEN) { + pr_err("wrong action %d\n", action); + goto fail; + } + + pos += parsed; + 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; + int err; + + if (*ppos) + return -EINVAL; + + kbuf = kmalloc(count, 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); + + mutex_lock(&ctx->kdamond_lock); + if (ctx->kdamond) { + ret = -EBUSY; + goto unlock_out; + } + + err = damon_set_schemes(ctx, schemes, nr_schemes); + if (err) + ret = err; + else + nr_schemes = 0; +unlock_out: + mutex_unlock(&ctx->kdamond_lock); + 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; @@ -1618,6 +1782,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, @@ -1634,10 +1804,10 @@ static struct dentry *debugfs_root; static int __init damon_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); -- 2.17.1