From patchwork Tue Feb 18 08:53:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11388053 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 B276E13A4 for ; Tue, 18 Feb 2020 08:54:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7509A24658 for ; Tue, 18 Feb 2020 08:54:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="qeBuXNoj" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7509A24658 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 AABAD6B0007; Tue, 18 Feb 2020 03:54:16 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A81A46B0008; Tue, 18 Feb 2020 03:54:16 -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 9BE996B000A; Tue, 18 Feb 2020 03:54:16 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0109.hostedemail.com [216.40.44.109]) by kanga.kvack.org (Postfix) with ESMTP id 845416B0007 for ; Tue, 18 Feb 2020 03:54:16 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 23C5C181AEF1F for ; Tue, 18 Feb 2020 08:54:16 +0000 (UTC) X-FDA: 76502636112.16.tin84_81baca4afe48 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=310eb5d20=sjpark@amazon.com,:akpm@linux-foundation.org:sjpark@amazon.de:acme@kernel.org:alexander.shishkin@linux.intel.com:amit@kernel.org:brendan.d.gregg@gmail.com:brendanhiggins@google.com:cai@lca.pw:colin.king@canonical.com:corbet@lwn.net:dwmw@amazon.com:jolsa@redhat.com:kirill@shutemov.name:mark.rutland@arm.com:mgorman@suse.de:minchan@kernel.org:mingo@redhat.com:namhyung@kernel.org:peterz@infradead.org:rdunlap@infradead.org:rostedt@goodmis.org:shuah@kernel.org:sj38.park@gmail.com:vdavydov.dev@gmail.com::linux-doc@vger.kernel.org:linux-kernel@vger.kernel.org,RULES_HIT:30012:30054:30064,0,RBL:52.95.49.90:@amazon.com:.lbl8.mailshell.net-62.18.0.100 64.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:24,LUA_SUMMARY:none X-HE-Tag: tin84_81baca4afe48 X-Filterd-Recvd-Size: 4493 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Tue, 18 Feb 2020 08:54:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582016056; x=1613552056; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=kAdTeVn39NF6MtyWxbSfFrMuEluavNfxQ8uqZzEFpes=; b=qeBuXNojWCRUNrG8QTE2trrb2KmSzw/aIgvJ+SxUacaPLvyNiX9kiJSP +CKhBrG+Hcai5VUrvNromg1GOuLsHixwfuLacRjh3ySPS9zJlcfPO1IGg he3ZN48e2zXz4PkuEsHoeDFWpRe2UylVoT36fjSSYnetDvoH3ChD2oMAN 4=; IronPort-SDR: Pb82G17sW4eGRVbXOfAxd5GeDDF8+bHfbeUvAjRbHWBmyYBUQ4VQ1EY4aFZgLXXnxzxqyNxvvG tWGVo+pHZmeg== X-IronPort-AV: E=Sophos;i="5.70,456,1574121600"; d="scan'208";a="16791750" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1a-16acd5e0.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 18 Feb 2020 08:54:04 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1a-16acd5e0.us-east-1.amazon.com (Postfix) with ESMTPS id 52519A259C; Tue, 18 Feb 2020 08:53:55 +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, 18 Feb 2020 08:53:55 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.108) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 18 Feb 2020 08:53:45 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v2 1/4] mm/madvise: Export madvise_common() to mm internal code Date: Tue, 18 Feb 2020 09:53:06 +0100 Message-ID: <20200218085309.18346-2-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200218085309.18346-1-sjpark@amazon.com> References: <20200218085309.18346-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.160.108] X-ClientProxiedBy: EX13D10UWB004.ant.amazon.com (10.43.161.121) 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 exports ``madvise_common()`` to ``mm/`` code for future reuse. Signed-off-by: SeongJae Park --- mm/internal.h | 4 ++++ mm/madvise.c | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/mm/internal.h b/mm/internal.h index 3cf20ab3ca01..dcdfe00e02ff 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -576,4 +576,8 @@ static inline bool is_migrate_highatomic_page(struct page *page) void setup_zone_pageset(struct zone *zone); extern struct page *alloc_new_node_page(struct page *page, unsigned long node); + + +int madvise_common(struct task_struct *task, struct mm_struct *mm, + unsigned long start, size_t len_in, int behavior); #endif /* __MM_INTERNAL_H */ diff --git a/mm/madvise.c b/mm/madvise.c index 0c901de531e4..4fa9dfc770bc 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1005,7 +1005,7 @@ madvise_behavior_valid(int behavior) * @task could be a zombie leader if it calls sys_exit so accessing mm_struct * via task->mm is prohibited. Please use @mm instead of task->mm. */ -static int madvise_common(struct task_struct *task, struct mm_struct *mm, +int madvise_common(struct task_struct *task, struct mm_struct *mm, unsigned long start, size_t len_in, int behavior) { unsigned long end, tmp; @@ -1103,6 +1103,7 @@ static int madvise_common(struct task_struct *task, struct mm_struct *mm, return error; } +EXPORT_SYMBOL_GPL(madvise_common); /* * The madvise(2) system call. From patchwork Tue Feb 18 08:53:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11388051 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 17470139A for ; Tue, 18 Feb 2020 08:54:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CB9E9206E2 for ; Tue, 18 Feb 2020 08:54:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="AmGJLQ0p" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CB9E9206E2 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 1391D6B0006; Tue, 18 Feb 2020 03:54:09 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id EC8596B0007; Tue, 18 Feb 2020 03:54:08 -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 D67996B0008; Tue, 18 Feb 2020 03:54:08 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0026.hostedemail.com [216.40.44.26]) by kanga.kvack.org (Postfix) with ESMTP id C00136B0006 for ; Tue, 18 Feb 2020 03:54:08 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 6BBE15828 for ; Tue, 18 Feb 2020 08:54:08 +0000 (UTC) X-FDA: 76502635776.16.plant17_6f47eaf51b2d X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=310eb5d20=sjpark@amazon.com,:akpm@linux-foundation.org:sjpark@amazon.de:acme@kernel.org:alexander.shishkin@linux.intel.com:amit@kernel.org:brendan.d.gregg@gmail.com:brendanhiggins@google.com:cai@lca.pw:colin.king@canonical.com:corbet@lwn.net:dwmw@amazon.com:jolsa@redhat.com:kirill@shutemov.name:mark.rutland@arm.com:mgorman@suse.de:minchan@kernel.org:mingo@redhat.com:namhyung@kernel.org:peterz@infradead.org:rdunlap@infradead.org:rostedt@goodmis.org:shuah@kernel.org:sj38.park@gmail.com:vdavydov.dev@gmail.com::linux-doc@vger.kernel.org:linux-kernel@vger.kernel.org,RULES_HIT:30001:30003:30004:30054:30064:30070,0,RBL:72.21.198.25:@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:23,LUA_SUMMARY:none X-HE-Tag: plant17_6f47eaf51b2d X-Filterd-Recvd-Size: 8104 Received: from smtp-fw-4101.amazon.com (smtp-fw-4101.amazon.com [72.21.198.25]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Tue, 18 Feb 2020 08:54:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582016048; x=1613552048; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=P5oJYeyFQOUDd8t9m1JM5i5/82v7OIpG5I1z4dDMNr8=; b=AmGJLQ0pP3uzEqg5Zv4br2hz/riA87EV0ozaGV1ufoi0THI5II5wj9fH Co1nlsEOVQfQn/Boce07MJDoYeGNsMQyeE0WmHhreGNGybkPjCMbj8+6C Gpkb88Fq26W4OlV1qFZZAbnRAnPpA7Ch7xtZ0lGZsoe6+1Tn1Dt0TFPyN E=; IronPort-SDR: Byr2AeyT9wxnIqMp8FyM7+qQBHoJdK04KU+8BBuUZaA+DO7OtCY2J5ebGSQgmiWlP+y7YYqY/G PZVD1jrlV/wQ== X-IronPort-AV: E=Sophos;i="5.70,455,1574121600"; d="scan'208";a="17440567" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1a-16acd5e0.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 18 Feb 2020 08:54:07 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1a-16acd5e0.us-east-1.amazon.com (Postfix) with ESMTPS id A54F4A3144; Tue, 18 Feb 2020 08:54:05 +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, 18 Feb 2020 08:54:05 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.108) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 18 Feb 2020 08:53:54 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v2 2/4] mm/damon: Account age of target regions Date: Tue, 18 Feb 2020 09:53:07 +0100 Message-ID: <20200218085309.18346-3-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200218085309.18346-1-sjpark@amazon.com> References: <20200218085309.18346-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.160.108] X-ClientProxiedBy: EX13D10UWB004.ant.amazon.com (10.43.161.121) 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 can be used as a primitive for data access pattern awared memory maangement optimizations. However, users who want such optimizations should run DAMON, read the monitoring results, analyze it, plan a new memory management scheme, and apply the new scheme by themselves. It would not be too hard, but still require some level of efforts. For complicated optimizations, this effort is inevitable. That said, in many cases, users would simply want to apply an actions to a memory region of a specific size having a specific access frequency for a specific time. For example, "page out a memory region larger than 100 MiB but having a low access frequency more than 10 minutes", or "Use THP for a memory region larger than 2 MiB having a high access frequency for more than 2 seconds". For such optimizations, users will need to first account the age of each region themselves. To reduce such efforts, this commit implements a simple age account of each region in DAMON. For each aggregation step, DAMON compares the access frequency and start/end address of each region with those from last aggregation and reset the age of the region if the change is significant. Else, the age is incremented. Signed-off-by: SeongJae Park --- include/linux/damon.h | 5 ++++ mm/damon.c | 58 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 78785cb88d42..50fbe308590e 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -22,6 +22,11 @@ struct damon_region { unsigned long sampling_addr; unsigned int nr_accesses; struct list_head list; + + unsigned int age; + unsigned long last_vm_start; + unsigned long last_vm_end; + unsigned int last_nr_accesses; }; /* Represents a monitoring target task */ diff --git a/mm/damon.c b/mm/damon.c index ca9a32ecc61d..c9bce7c97709 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -87,6 +87,10 @@ static struct damon_region *damon_new_region(struct damon_ctx *ctx, ret->sampling_addr = damon_rand(ctx, vm_start, vm_end); INIT_LIST_HEAD(&ret->list); + ret->age = 0; + ret->last_vm_start = vm_start; + ret->last_vm_end = vm_end; + return ret; } @@ -600,11 +604,44 @@ static void kdamond_flush_aggregated(struct damon_ctx *c) damon_write_rbuf(c, &r->vm_end, sizeof(r->vm_end)); damon_write_rbuf(c, &r->nr_accesses, sizeof(r->nr_accesses)); + r->last_nr_accesses = r->nr_accesses; r->nr_accesses = 0; } } } +#define diff_of(a, b) (a > b ? a - b : b - a) + +/* + * Increase or reset the age of the given monitoring target region + * + * If the area or '->nr_accesses' has changed significantly, reset the '->age'. + * Else, increase the age. + */ +static void damon_do_count_age(struct damon_region *r, unsigned int threshold) +{ + unsigned long sz_threshold = (r->vm_end - r->vm_start) / 5; + + if (diff_of(r->vm_start, r->last_vm_start) + + diff_of(r->vm_end, r->last_vm_end) > sz_threshold) + r->age = 0; + else if (diff_of(r->nr_accesses, r->last_nr_accesses) > threshold) + r->age = 0; + else + r->age++; +} + +static void kdamond_count_age(struct damon_ctx *c, unsigned int threshold) +{ + struct damon_task *t; + struct damon_region *r; + + damon_for_each_task(c, t) { + damon_for_each_region(r, t) + damon_do_count_age(r, threshold); + } +} + #define sz_damon_region(r) (r->vm_end - r->vm_start) /* @@ -613,15 +650,19 @@ static void kdamond_flush_aggregated(struct damon_ctx *c) static void damon_merge_two_regions(struct damon_region *l, struct damon_region *r) { - l->nr_accesses = (l->nr_accesses * sz_damon_region(l) + - r->nr_accesses * sz_damon_region(r)) / - (sz_damon_region(l) + sz_damon_region(r)); + unsigned long sz_l = sz_damon_region(l), sz_r = sz_damon_region(r); + + l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) / + (sz_l + sz_r); + l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r); + if (sz_l < sz_r) { + l->last_vm_start = r->last_vm_start; + l->last_vm_end = r->last_vm_end; + } l->vm_end = r->vm_end; damon_destroy_region(r); } -#define diff_of(a, b) (a > b ? a - b : b - a) - /* * Merge adjacent regions having similar access frequencies * @@ -674,6 +715,12 @@ static void damon_split_region_at(struct damon_ctx *ctx, struct damon_region *new; new = damon_new_region(ctx, r->vm_start + sz_r, r->vm_end); + new->age = r->age; + new->last_vm_start = r->vm_start; + new->last_nr_accesses = r->last_nr_accesses; + + r->last_vm_start = r->vm_start; + r->last_vm_end = r->vm_end; r->vm_end = new->vm_start; damon_add_region(new, r, damon_next_region(r)); @@ -865,6 +912,7 @@ static int kdamond_fn(void *data) if (kdamond_aggregate_interval_passed(ctx)) { kdamond_merge_regions(ctx, max_nr_accesses / 10); + kdamond_count_age(ctx, max_nr_accesses / 10); if (ctx->aggregate_cb) ctx->aggregate_cb(ctx); kdamond_flush_aggregated(ctx); From patchwork Tue Feb 18 08:53:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11388055 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 A035313A4 for ; Tue, 18 Feb 2020 08:54:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5FF7421D7D for ; Tue, 18 Feb 2020 08:54:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="VlokznAw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5FF7421D7D 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 860186B0008; Tue, 18 Feb 2020 03:54:23 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8104C6B000A; Tue, 18 Feb 2020 03:54:23 -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 7270E6B000C; Tue, 18 Feb 2020 03:54:23 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0004.hostedemail.com [216.40.44.4]) by kanga.kvack.org (Postfix) with ESMTP id 5B4996B0008 for ; Tue, 18 Feb 2020 03:54:23 -0500 (EST) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E7D165831 for ; Tue, 18 Feb 2020 08:54:22 +0000 (UTC) X-FDA: 76502636364.02.tree35_9175f99ce33b X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=310eb5d20=sjpark@amazon.com,:akpm@linux-foundation.org:sjpark@amazon.de:acme@kernel.org:alexander.shishkin@linux.intel.com:amit@kernel.org:brendan.d.gregg@gmail.com:brendanhiggins@google.com:cai@lca.pw:colin.king@canonical.com:corbet@lwn.net:dwmw@amazon.com:jolsa@redhat.com:kirill@shutemov.name:mark.rutland@arm.com:mgorman@suse.de:minchan@kernel.org:mingo@redhat.com:namhyung@kernel.org:peterz@infradead.org:rdunlap@infradead.org:rostedt@goodmis.org:shuah@kernel.org:sj38.park@gmail.com:vdavydov.dev@gmail.com::linux-doc@vger.kernel.org:linux-kernel@vger.kernel.org,RULES_HIT:30004:30012:30034:30054:30064:30070,0,RBL:52.95.49.90:@amazon.com:.lbl8.mailshell.net-62.18.0.100 64.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:28,LUA_SUMMARY:none X-HE-Tag: tree35_9175f99ce33b X-Filterd-Recvd-Size: 10761 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by imf44.hostedemail.com (Postfix) with ESMTP for ; Tue, 18 Feb 2020 08:54:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582016063; x=1613552063; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=FYR2C4EnmWcPnxV/kmONab3mwGTZ7LTN+dd8zv0t/+U=; b=VlokznAw6kePHwwnkR+tCWJ/jA7KFE/pYxAbRw8UMUK+/DXv533+NS4/ ePQ/pW6klhgSVvGqkNEUriiH9lFhw1wW5L6k6uUuL9eSeNmktIBEbwzHQ y8+mwgd+2eoKdVf6TAIY82oCoc9OpRz38P3KLlY0EkqKC5kTOvxDT6qpx 0=; IronPort-SDR: vaqtnIJju8f9nhkaxMEqyCC8Mxrep9BUEXIcOa+hKe6mxLxR/bmC0Ou1thGMeAsVgWAQUTYevR SHl2iUPVq0Jw== X-IronPort-AV: E=Sophos;i="5.70,456,1574121600"; d="scan'208";a="16791784" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1d-98acfc19.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 18 Feb 2020 08:54:22 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1d-98acfc19.us-east-1.amazon.com (Postfix) with ESMTPS id B6E58A2C4F; Tue, 18 Feb 2020 08:54:14 +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, 18 Feb 2020 08:54:14 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.108) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 18 Feb 2020 08:54:04 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v2 3/4] mm/damon: Implement data access monitoring-based operation schemes Date: Tue, 18 Feb 2020 09:53:08 +0100 Message-ID: <20200218085309.18346-4-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200218085309.18346-1-sjpark@amazon.com> References: <20200218085309.18346-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.160.108] X-ClientProxiedBy: EX13D10UWB004.ant.amazon.com (10.43.161.121) 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 In many cases, users might use DAMON for simple data access awared memory management optimizations such as applying an operation scheme to a memory region of a specific size having a specific access frequency for a specific time. For example, "page out a memory region larger than 100 MiB but having a low access frequency more than 10 minutes", or "Use THP for a memory region larger than 2 MiB having a high access frequency for more than 2 seconds". To minimize users from spending their time for implementation of such simple data access monitoring-based operation schemes, this commit makes DAMON to handle such schemes directly. With this commit, users can simply specify their desired schemes to DAMON. Each of the schemes is composed with conditions for filtering of the target memory regions and desired memory management action for the target. In specific, the format is:: The filtering conditions are size of memory region, number of accesses to the region monitored by DAMON, and the age of the region. The age of region is incremented periodically but reset when its addresses or access frequency has significanly changed or the action of a scheme has applied. For the action, current implementation supports only a few of madvise() hints, ``MADV_WILLNEED``, ``MADV_COLD``, ``MADV_PAGEOUT``, ``MADV_HUGEPAGE``, and ``MADV_NOHUGEPAGE``. Signed-off-by: SeongJae Park --- include/linux/damon.h | 24 ++++++++ mm/damon.c | 131 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) diff --git a/include/linux/damon.h b/include/linux/damon.h index 50fbe308590e..8cb2452579ee 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -36,6 +36,27 @@ struct damon_task { struct list_head list; }; +/* Data Access Monitoring-based Operation Scheme */ +enum damos_action { + DAMOS_WILLNEED, + DAMOS_COLD, + DAMOS_PAGEOUT, + DAMOS_HUGEPAGE, + DAMOS_NOHUGEPAGE, + DAMOS_ACTION_LEN, +}; + +struct damos { + 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 list_head list; +}; + struct damon_ctx { unsigned long sample_interval; unsigned long aggr_interval; @@ -58,6 +79,7 @@ struct damon_ctx { struct rnd_state rndseed; struct list_head tasks_list; /* 'damon_task' objects */ + struct list_head schemes_list; /* 'damos' objects */ /* callbacks */ void (*sample_cb)(struct damon_ctx *context); @@ -66,6 +88,8 @@ struct damon_ctx { int damon_set_pids(struct damon_ctx *ctx, unsigned long *pids, ssize_t nr_pids); +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_set_attrs(struct damon_ctx *ctx, unsigned long s, unsigned long a, diff --git a/mm/damon.c b/mm/damon.c index c9bce7c97709..45139d344f58 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -11,6 +11,7 @@ #define CREATE_TRACE_POINTS +#include #include #include #include @@ -24,6 +25,8 @@ #include #include +#include "internal.h" + #define damon_get_task_struct(t) \ (get_pid_task(find_vpid(t->pid), PIDTYPE_PID)) @@ -45,6 +48,12 @@ #define damon_for_each_task_safe(ctx, t, next) \ list_for_each_entry_safe(t, next, &(ctx)->tasks_list, list) +#define damon_for_each_schemes(ctx, r) \ + list_for_each_entry(r, &(ctx)->schemes_list, list) + +#define damon_for_each_schemes_safe(ctx, r, next) \ + list_for_each_entry_safe(r, next, &(ctx)->schemes_list, list) + #define MAX_RFILE_PATH_LEN 256 /* Get a random number in [l, r) */ @@ -190,6 +199,27 @@ static void damon_destroy_task(struct damon_task *t) damon_free_task(t); } +static void damon_add_scheme(struct damon_ctx *ctx, struct damos *s) +{ + list_add_tail(&s->list, &ctx->schemes_list); +} + +static void damon_del_scheme(struct damos *s) +{ + list_del(&s->list); +} + +static void damon_free_scheme(struct damos *s) +{ + kfree(s); +} + +static void damon_destroy_scheme(struct damos *s) +{ + damon_del_scheme(s); + damon_free_scheme(s); +} + /* * Returns number of monitoring target tasks */ @@ -642,6 +672,84 @@ static void kdamond_count_age(struct damon_ctx *c, unsigned int threshold) } } +static int damos_do_action(struct damon_task *task, struct damon_region *r, + enum damos_action action) +{ + struct task_struct *t; + struct mm_struct *mm; + int madv_action; + int ret = -EINVAL; + + switch (action) { + case DAMOS_WILLNEED: + madv_action = MADV_WILLNEED; + break; + case DAMOS_COLD: + madv_action = MADV_COLD; + break; + case DAMOS_PAGEOUT: + madv_action = MADV_PAGEOUT; + break; + case DAMOS_HUGEPAGE: + madv_action = MADV_HUGEPAGE; + break; + case DAMOS_NOHUGEPAGE: + madv_action = MADV_NOHUGEPAGE; + break; + default: + pr_warn("Wrong action %d\n", action); + goto out; + } + + t = damon_get_task_struct(task); + if (!t) + goto out; + mm = damon_get_mm(task); + if (!mm) + goto put_task_out; + + ret = madvise_common(t, mm, PAGE_ALIGN(r->vm_start), + PAGE_ALIGN(r->vm_end - r->vm_start), madv_action); + + mmput(mm); +put_task_out: + put_task_struct(t); +out: + return ret; +} + +static void damon_do_apply_schemes(struct damon_ctx *c, struct damon_task *t, + struct damon_region *r) +{ + struct damos *s; + unsigned long sz; + + damon_for_each_schemes(c, s) { + sz = r->vm_end - r->vm_start; + if (sz < s->min_sz_region || s->max_sz_region < sz) + continue; + if (r->nr_accesses < s->min_nr_accesses || + s->max_nr_accesses < r->nr_accesses) + continue; + if (r->age < s->min_age_region || + s->max_age_region < r->age) + continue; + damos_do_action(t, r, s->action); + r->age = 0; + } +} + +static void kdamond_apply_schemes(struct damon_ctx *c) +{ + struct damon_task *t; + struct damon_region *r; + + damon_for_each_task(c, t) { + damon_for_each_region(r, t) + damon_do_apply_schemes(c, t, r); + } +} + #define sz_damon_region(r) (r->vm_end - r->vm_start) /* @@ -915,6 +1023,7 @@ static int kdamond_fn(void *data) kdamond_count_age(ctx, max_nr_accesses / 10); if (ctx->aggregate_cb) ctx->aggregate_cb(ctx); + kdamond_apply_schemes(ctx); kdamond_flush_aggregated(ctx); kdamond_split_regions(ctx); } @@ -989,6 +1098,27 @@ int damon_stop(struct damon_ctx *ctx) return damon_turn_kdamond(ctx, false); } +/* + * Set the data access monitoring oriented schemes + * + * NOTE: This function should not be called while the kdamond of the context is + * running. + * + * Returns 0 if success, or negative error code otherwise. + */ +int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes, + ssize_t nr_schemes) +{ + struct damos *s, *next; + ssize_t i; + + damon_for_each_schemes_safe(ctx, s, next) + damon_destroy_scheme(s); + for (i = 0; i < nr_schemes; i++) + damon_add_scheme(ctx, schemes[i]); + return 0; +} + /* * This function should not be called while the kdamond is running. */ @@ -1431,6 +1561,7 @@ static int __init damon_init_user_ctx(void) prandom_seed_state(&ctx->rndseed, 42); INIT_LIST_HEAD(&ctx->tasks_list); + INIT_LIST_HEAD(&ctx->schemes_list); ctx->sample_cb = NULL; ctx->aggregate_cb = NULL; From patchwork Tue Feb 18 08:53:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11388057 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 27F2B13A4 for ; Tue, 18 Feb 2020 08:54:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DC719206E2 for ; Tue, 18 Feb 2020 08:54:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="R/ceHIXf" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DC719206E2 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 220E06B000A; Tue, 18 Feb 2020 03:54:38 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1F6526B000C; Tue, 18 Feb 2020 03:54: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 10C986B0070; Tue, 18 Feb 2020 03:54:38 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0164.hostedemail.com [216.40.44.164]) by kanga.kvack.org (Postfix) with ESMTP id EE9656B000A for ; Tue, 18 Feb 2020 03:54:37 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 8FB798248047 for ; Tue, 18 Feb 2020 08:54:37 +0000 (UTC) X-FDA: 76502636994.21.trail28_b30ab1c92317 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=310eb5d20=sjpark@amazon.com,:akpm@linux-foundation.org:sjpark@amazon.de:acme@kernel.org:alexander.shishkin@linux.intel.com:amit@kernel.org:brendan.d.gregg@gmail.com:brendanhiggins@google.com:cai@lca.pw:colin.king@canonical.com:corbet@lwn.net:dwmw@amazon.com:jolsa@redhat.com:kirill@shutemov.name:mark.rutland@arm.com:mgorman@suse.de:minchan@kernel.org:mingo@redhat.com:namhyung@kernel.org:peterz@infradead.org:rdunlap@infradead.org:rostedt@goodmis.org:shuah@kernel.org:sj38.park@gmail.com:vdavydov.dev@gmail.com::linux-doc@vger.kernel.org:linux-kernel@vger.kernel.org,RULES_HIT:30004:30051:30054:30064:30070,0,RBL:207.171.190.10:@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:26,LUA_SUMMARY:none X-HE-Tag: trail28_b30ab1c92317 X-Filterd-Recvd-Size: 9016 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Tue, 18 Feb 2020 08:54:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582016078; x=1613552078; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=adALBkNXH0eWFKDu+TJ3ZoBrkHljhbeL1cdArgJQwAQ=; b=R/ceHIXfMV9Ql6EGw64v0CWY31K4RcgkaMDPFDo+kBM7eNZjGuGq3crO uWt1znI6ui2dClk2bn3cH7rHgeFxUn6ExMCR7UDD1bdQxd8cLyfAQOUEt YprjNqNcs+RieWuaft1bp8TnA3nX5UxsdPK6hRILAM1cLIoIDTS3OMnHV g=; IronPort-SDR: mytlnP7L8JBYAJql1QFbn56Qc9R2CBU8Yl1dtRjZ+ddE7clSBS9S6WMJkYkV5RwNA8fPBWa+Ko l7fA0SZrMHew== X-IronPort-AV: E=Sophos;i="5.70,455,1574121600"; d="scan'208";a="27053928" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-37fd6b3d.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 18 Feb 2020 08:54:33 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1d-37fd6b3d.us-east-1.amazon.com (Postfix) with ESMTPS id 268EE286AB9; Tue, 18 Feb 2020 08:54:24 +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, 18 Feb 2020 08:54:24 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.108) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 18 Feb 2020 08:54:13 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v2 4/4] mm/damon/schemes: Implement a debugfs interface Date: Tue, 18 Feb 2020 09:53:09 +0100 Message-ID: <20200218085309.18346-5-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200218085309.18346-1-sjpark@amazon.com> References: <20200218085309.18346-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.160.108] X-ClientProxiedBy: EX13D10UWB004.ant.amazon.com (10.43.161.121) 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 privileged user space programs. Users 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 45139d344f58..de3e2b343139 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); @@ -1273,6 +1296,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 error; + } + + scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a, + min_age, max_age, action); + if (!scheme) + goto error; + + schemes[*nr_schemes] = scheme; + *nr_schemes += 1; + } + return schemes; +error: + 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, ret; + + 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); + if (!schemes) { + ret = -EINVAL; + goto out; + } + + spin_lock(&ctx->kdamond_lock); + if (ctx->kdamond) + goto monitor_running; + + damon_set_schemes(ctx, schemes, nr_schemes); + spin_unlock(&ctx->kdamond_lock); + goto free_schemes_out; + +monitor_running: + spin_unlock(&ctx->kdamond_lock); + pr_err("%s: kdamond is running. Turn it off first.\n", __func__); + ret = -EINVAL; +free_schemes_out: + 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; @@ -1502,6 +1663,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, @@ -1518,10 +1685,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);