Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp254332pxu; Tue, 6 Oct 2020 05:46:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0WhuSm9BuZv7bm+q+NDuXngmimv9Sh3DW2gJdKosNH9CCACsbzqMJmKpx1z6f32z7ZgSA X-Received: by 2002:aa7:de06:: with SMTP id h6mr5189252edv.31.1601988386974; Tue, 06 Oct 2020 05:46:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601988386; cv=none; d=google.com; s=arc-20160816; b=gcdzwtWkE3D4XXj/sjTAVNZwht+/KxATt8xNOpZuVNO7b0b1g2G8HHO5Exn8wFjhcd wLKxyZcDd+xyZrNnJqyofXAnfdgOu7UaOU94x45u/vqR0Axd5RySwHuwJavzooTS03FI +C12B1uPpy4IvnmOuX8ZVeEI3xUzYzSH9E5eWXrOcMTii2cpX3niMgZUy4X0WLIRBVfZ NAc5r83bGzJHXhyKbYiQkGJZnKIGc5JI8Bv8zZnvXfJarVBUGwWGzvwjNdJpYBa9eNvn mdrrSeBXESDdsMT702lHoyKR6OD2bXMAeez8Jg/5FkZKyz8SB9FwdjbkBuhzigkZ3iCk ExHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Xh/XFoPyX0qjBaIe3sAFmmh4BMprHpdZfyRo++w6HqA=; b=eEB0X39BFzYg38cwh0E7C3AfcM3RgBNFCJyrGuYt5r20OrHv6gu+elOMz5Hv/JaC/k 8SkGTt/HzDQyb0heTfg6hgqJctYZi0r8m1mhoej1PzHWW74wcTxh6tug6JqdmVuhKw/4 JOnXvCXHDlGp1WDRogbFROKTBYxDyh4AGO24QI2ksNDpEOOZiFv3GpvzUr1zvlMk4QWL lZuspYdkzauEGHW5LEb3NVoQi2Sx1uU9V0qNE4P+vUePg9zzb3THvuCDDPwDlFgVPRfp 4Xh2W3+8B3vWPsWyk2U4Rn1IF83XiM8Z6UxbnOUSSFQynr+W/2icEC4FLrIF8ee1Urfm 9zgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=lufc+WpW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id e9si1906891ejc.175.2020.10.06.05.46.03; Tue, 06 Oct 2020 05:46:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=lufc+WpW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1726663AbgJFMmL (ORCPT + 99 others); Tue, 6 Oct 2020 08:42:11 -0400 Received: from smtp-fw-2101.amazon.com ([72.21.196.25]:65411 "EHLO smtp-fw-2101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726439AbgJFMmL (ORCPT ); Tue, 6 Oct 2020 08:42:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1601988130; x=1633524130; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=Xh/XFoPyX0qjBaIe3sAFmmh4BMprHpdZfyRo++w6HqA=; b=lufc+WpWrRL2YczthGSOaVeWSuJjbKOiKtxwOt/3W4enHOXA/nLL/DKr Mwq5V5BOvpBHEljHy8PrYmKK/JgsXQu+6GW+FFp5iTkxuXKhbQPKvD8EP RnxGv1m9s2OwGfP9uEG4Z42wDYtQRCxNijKbfB4qSMoPVY1nCDPuOraxU M=; X-IronPort-AV: E=Sophos;i="5.77,343,1596499200"; d="scan'208";a="58084516" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-2c-76e0922c.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP; 06 Oct 2020 12:41:57 +0000 Received: from EX13D31EUA004.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2c-76e0922c.us-west-2.amazon.com (Postfix) with ESMTPS id 95593A7E41; Tue, 6 Oct 2020 12:41:54 +0000 (UTC) Received: from u3f2cd687b01c55.ant.amazon.com (10.43.161.237) by EX13D31EUA004.ant.amazon.com (10.43.165.161) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 6 Oct 2020 12:41:35 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v15 4/8] mm/damon/dbgfs: Support DAMON-based Operation Schemes Date: Tue, 6 Oct 2020 14:39:27 +0200 Message-ID: <20201006123931.5847-5-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201006123931.5847-1-sjpark@amazon.com> References: <20201006123931.5847-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.237] X-ClientProxiedBy: EX13D18UWA002.ant.amazon.com (10.43.160.199) To EX13D31EUA004.ant.amazon.com (10.43.165.161) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: SeongJae Park This commit makes 'damon-dbgfs' to support the data access monitoring oriented memory management schemes. Users can read and update the schemes using ``/damon/schemes`` file. The format is:: Signed-off-by: SeongJae Park --- mm/damon/dbgfs.c | 169 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 165 insertions(+), 4 deletions(-) diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c index df807ffb283e..54bdccd0af71 100644 --- a/mm/damon/dbgfs.c +++ b/mm/damon/dbgfs.c @@ -220,6 +220,159 @@ static ssize_t dbgfs_record_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_scheme(s, c) { + rc = scnprintf(&buf[written], len - written, + "%lu %lu %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 dbgfs_schemes_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct damon_ctx *ctx = file->private_data; + char *kbuf; + ssize_t len; + + kbuf = kmalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + mutex_lock(&ctx->kdamond_lock); + len = sprint_schemes(ctx, kbuf, count); + mutex_unlock(&ctx->kdamond_lock); + 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); +} + +static bool damos_action_valid(int action) +{ + switch (action) { + case DAMOS_WILLNEED: + case DAMOS_COLD: + case DAMOS_PAGEOUT: + case DAMOS_HUGEPAGE: + case DAMOS_NOHUGEPAGE: + return true; + default: + return false; + } +} + +/* + * 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 long min_sz, max_sz; + unsigned int min_nr_a, max_nr_a, min_age, max_age; + unsigned 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], "%lu %lu %u %u %u %u %u%n", + &min_sz, &max_sz, &min_nr_a, &max_nr_a, + &min_age, &max_age, &action, &parsed); + if (ret != 7) + break; + if (!damos_action_valid(action)) { + 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; + } + return schemes; +fail: + free_schemes_arr(schemes, *nr_schemes); + return NULL; +} + +static ssize_t dbgfs_schemes_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct damon_ctx *ctx = file->private_data; + char *kbuf; + struct damos **schemes; + ssize_t nr_schemes = 0, ret = count; + int err; + + kbuf = user_input_str(buf, count, ppos); + if (IS_ERR(kbuf)) + return PTR_ERR(kbuf); + + schemes = str_to_schemes(kbuf, ret, &nr_schemes); + if (!schemes) { + ret = -EINVAL; + goto out; + } + + 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; +} + #define targetid_is_pid(ctx) \ (ctx->primitive.target_valid == damon_va_target_valid) @@ -416,6 +569,13 @@ static const struct file_operations record_fops = { .write = dbgfs_record_write, }; +static const struct file_operations schemes_fops = { + .owner = THIS_MODULE, + .open = damon_dbgfs_open, + .read = dbgfs_schemes_read, + .write = dbgfs_schemes_write, +}; + static const struct file_operations target_ids_fops = { .owner = THIS_MODULE, .open = damon_dbgfs_open, @@ -431,10 +591,11 @@ static const struct file_operations kdamond_pid_fops = { static int dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx) { - const char * const file_names[] = {"attrs", "record", "target_ids", - "kdamond_pid"}; - const struct file_operations *fops[] = {&attrs_fops, &record_fops, - &target_ids_fops, &kdamond_pid_fops}; + const char * const file_names[] = {"attrs", "record", "schemes", + "target_ids", "kdamond_pid"}; + const struct file_operations *fops[] = {&attrs_fops, + &record_fops, &schemes_fops, &target_ids_fops, + &kdamond_pid_fops}; int i; for (i = 0; i < ARRAY_SIZE(file_names); i++) { -- 2.17.1