From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 100FEC433DB for ; Tue, 2 Feb 2021 10:01:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7E00964F59 for ; Tue, 2 Feb 2021 10:00:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7E00964F59 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 DF6406B007B; Tue, 2 Feb 2021 05:00:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D7E296B007D; Tue, 2 Feb 2021 05:00:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C47826B007E; Tue, 2 Feb 2021 05:00:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0178.hostedemail.com [216.40.44.178]) by kanga.kvack.org (Postfix) with ESMTP id AF9F36B007B for ; Tue, 2 Feb 2021 05:00:58 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 6EBB58249980 for ; Tue, 2 Feb 2021 10:00:58 +0000 (UTC) X-FDA: 77772884196.12.beef87_191751f275ca Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id 429651801DBD7 for ; Tue, 2 Feb 2021 10:00:58 +0000 (UTC) X-HE-Tag: beef87_191751f275ca X-Filterd-Recvd-Size: 10590 Received: from smtp-fw-4101.amazon.com (smtp-fw-4101.amazon.com [72.21.198.25]) by imf26.hostedemail.com (Postfix) with ESMTP for ; Tue, 2 Feb 2021 10:00:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1612260058; x=1643796058; h=from:to:cc:subject:date:message-id:in-reply-to: mime-version; bh=mLcP0izKWDNsLPjNz5dQdzdBpJthpi2P8wIHv6ID5/w=; b=grhum8/Sze3OyPRu3tGSIfOk8vapp0eY852JeGXUcU8yzxWRBTC2I4EV 57UngL3VYxpg4wGcTdE7RZzBn70CYA/S7zbXN42L0kY9jswfV3FMZtCI9 tc65UKF+36lU/S24lQZzR7hzFiXQSEAadXOcNfrHjlaN9DVWruI+CI5Xl Q=; X-IronPort-AV: E=Sophos;i="5.79,394,1602547200"; d="scan'208";a="79240678" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-2a-f14f4a47.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 02 Feb 2021 10:00:48 +0000 Received: from EX13D31EUA001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-2a-f14f4a47.us-west-2.amazon.com (Postfix) with ESMTPS id 5B40AA2A0E; Tue, 2 Feb 2021 10:00:45 +0000 (UTC) Received: from u3f2cd687b01c55.ant.amazon.com (10.43.162.94) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 2 Feb 2021 10:00:28 +0000 From: SeongJae Park To: Shakeel Butt CC: SeongJae Park , SeongJae Park , , Andrea Arcangeli , , , , , , Brendan Higgins , Qian Cai , Colin Ian King , Jonathan Corbet , "David Hildenbrand" , , Marco Elver , "Du, Fan" , , "Greg Thelen" , Ian Rogers , , "Kirill A. Shutemov" , Mark Rutland , Mel Gorman , Minchan Kim , Ingo Molnar , , "Peter Zijlstra (Intel)" , Randy Dunlap , Rik van Riel , David Rientjes , Steven Rostedt , Mike Rapoport , , Shuah Khan , , , Vlastimil Babka , Vladimir Davydov , Yang Shi , Huang Ying , , , Linux MM , , LKML Subject: Re: [PATCH v23 07/15] mm/damon: Implement a debugfs-based user space interface Date: Tue, 2 Feb 2021 11:00:13 +0100 Message-ID: <20210202100013.9764-1-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.162.94] X-ClientProxiedBy: EX13D31UWA002.ant.amazon.com (10.43.160.82) 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: On Mon, 1 Feb 2021 09:37:39 -0800 Shakeel Butt wrote: > On Tue, Dec 15, 2020 at 3:59 AM SeongJae Park wrote: > > > > From: SeongJae Park > > > > DAMON is designed to be used by kernel space code such as the memory > > management subsystems, and therefore it provides only kernel space API. > > Which kernel space APIs are being referred here? The symbols in 'include/linux/damon.h' > > > That said, letting the user space control DAMON could provide some > > benefits to them. For example, it will allow user space to analyze > > their specific workloads and make their own special optimizations. > > > > For such cases, this commit implements a simple DAMON application kernel > > module, namely 'damon-dbgfs', which merely wraps the DAMON api and > > exports those to the user space via the debugfs. > > [...] > > > > Signed-off-by: SeongJae Park > > Reviewed-by: Leonard Foerster > > --- > > include/linux/damon.h | 3 + > > mm/damon/Kconfig | 9 ++ > > mm/damon/Makefile | 1 + > > mm/damon/core.c | 45 ++++++ > > mm/damon/dbgfs.c | 366 ++++++++++++++++++++++++++++++++++++++++++ > > 5 files changed, 424 insertions(+) > > create mode 100644 mm/damon/dbgfs.c > > > > diff --git a/include/linux/damon.h b/include/linux/damon.h > > index 39b4d6d3ddee..f9e0d4349352 100644 > > --- a/include/linux/damon.h > > +++ b/include/linux/damon.h > > @@ -265,9 +265,12 @@ unsigned int damon_nr_regions(struct damon_target *t); > > > > struct damon_ctx *damon_new_ctx(enum damon_target_type type); > > void damon_destroy_ctx(struct damon_ctx *ctx); > > +int damon_set_targets(struct damon_ctx *ctx, > > + unsigned long *ids, ssize_t nr_ids); > > int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, > > unsigned long aggr_int, unsigned long regions_update_int, > > unsigned long min_nr_reg, unsigned long max_nr_reg); > > +int damon_nr_running_ctxs(void); > > > > int damon_start(struct damon_ctx **ctxs, int nr_ctxs); > > int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); > > diff --git a/mm/damon/Kconfig b/mm/damon/Kconfig > > index 8ae080c52950..72f1683ba0ee 100644 > > --- a/mm/damon/Kconfig > > +++ b/mm/damon/Kconfig > > @@ -21,4 +21,13 @@ config DAMON_VADDR > > This builds the default data access monitoring primitives for DAMON > > that works for virtual address spaces. > > > > +config DAMON_DBGFS > > + bool "DAMON debugfs interface" > > + depends on DAMON_VADDR && DEBUG_FS > > + help > > + This builds the debugfs interface for DAMON. The user space admins > > + can use the interface for arbitrary data access monitoring. > > + > > + If unsure, say N. > > + > > endmenu > > diff --git a/mm/damon/Makefile b/mm/damon/Makefile > > index 6ebbd08aed67..fed4be3bace3 100644 > > --- a/mm/damon/Makefile > > +++ b/mm/damon/Makefile > > @@ -2,3 +2,4 @@ > > > > obj-$(CONFIG_DAMON) := core.o > > obj-$(CONFIG_DAMON_VADDR) += vaddr.o > > +obj-$(CONFIG_DAMON_DBGFS) += dbgfs.o > > diff --git a/mm/damon/core.c b/mm/damon/core.c > > index 5ca9f79ccbb6..b9575a6bebff 100644 > > --- a/mm/damon/core.c > > +++ b/mm/damon/core.c > > @@ -166,6 +166,37 @@ void damon_destroy_ctx(struct damon_ctx *ctx) > > kfree(ctx); > > } > > > > +/** > > + * damon_set_targets() - Set monitoring targets. > > + * @ctx: monitoring context > > + * @ids: array of target ids > > + * @nr_ids: number of entries in @ids > > + * > > + * This function should not be called while the kdamond is running. > > + * > > + * Return: 0 on success, negative error code otherwise. > > + */ > > +int damon_set_targets(struct damon_ctx *ctx, > > + unsigned long *ids, ssize_t nr_ids) > > +{ > > + ssize_t i; > > + struct damon_target *t, *next; > > + > > + damon_for_each_target_safe(t, next, ctx) > > + damon_destroy_target(t); > > You need to put the reference on the target before destroying. Oops, you're right. I will fix this in the next version. > > > + > > + for (i = 0; i < nr_ids; i++) { > > + t = damon_new_target(ids[i]); > > + if (!t) { > > + pr_err("Failed to alloc damon_target\n"); > > + return -ENOMEM; > > + } > > + damon_add_target(ctx, t); > > + } > > + > > + return 0; > > +} > > + > > /** > > * damon_set_attrs() - Set attributes for the monitoring. > > * @ctx: monitoring context > > @@ -206,6 +237,20 @@ int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, > > return 0; > > } > > > > +/** > > + * damon_nr_running_ctxs() - Return number of currently running contexts. > > + */ > > +int damon_nr_running_ctxs(void) > > +{ > > + int nr_ctxs; > > + > > + mutex_lock(&damon_lock); > > + nr_ctxs = nr_running_ctxs; > > + mutex_unlock(&damon_lock); > > READ_ONCE(nr_running_ctxs) ? I'd like to keep the code simpler to read, unless this turns out to be a real performance bottleneck. > > > + > > + return nr_ctxs; > > +} > > + [...] > > + > > +static ssize_t dbgfs_target_ids_write(struct file *file, > > + const char __user *buf, size_t count, loff_t *ppos) > > +{ > > + struct damon_ctx *ctx = file->private_data; > > + char *kbuf, *nrs; > > + unsigned long *targets; > > + ssize_t nr_targets; > > + ssize_t ret = count; > > + int i; > > + int err; > > + > > + kbuf = user_input_str(buf, count, ppos); > > + if (IS_ERR(kbuf)) > > + return PTR_ERR(kbuf); > > + > > + nrs = kbuf; > > + > > + targets = str_to_target_ids(nrs, ret, &nr_targets); > > + if (!targets) { > > + ret = -ENOMEM; > > + goto out; > > + } > > + > > + if (targetid_is_pid(ctx)) { > > + for (i = 0; i < nr_targets; i++) > > + targets[i] = (unsigned long)find_get_pid( > > + (int)targets[i]); > > + } > > + > > + mutex_lock(&ctx->kdamond_lock); > > + if (ctx->kdamond) { > > + ret = -EINVAL; > > + goto unlock_out; > > You need to put_pid on the targets array. Good catch! > > > + } > > + > > + err = damon_set_targets(ctx, targets, nr_targets); > > + if (err) > > + ret = err; > > You need to handle the partial failure from damon_set_targets(). My intention is to keep partial success as is. > > > > +unlock_out: > > + mutex_unlock(&ctx->kdamond_lock); > > + kfree(targets); > > +out: > > + kfree(kbuf); > > + return ret; > > +} [...] Thanks, SeongJae Park