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=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable 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 25D8DC433E1 for ; Tue, 19 May 2020 14:03:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B2920207FB for ; Tue, 19 May 2020 14:03:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="YKbFIrKw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B2920207FB 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 3496780005; Tue, 19 May 2020 10:03:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D1FA900002; Tue, 19 May 2020 10:03:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19A3A80005; Tue, 19 May 2020 10:03:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0074.hostedemail.com [216.40.44.74]) by kanga.kvack.org (Postfix) with ESMTP id F3311900002 for ; Tue, 19 May 2020 10:03:00 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 9208D62D0 for ; Tue, 19 May 2020 14:03:00 +0000 (UTC) X-FDA: 76833634920.28.honey46_8e95c980d015e X-HE-Tag: honey46_8e95c980d015e X-Filterd-Recvd-Size: 6454 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Tue, 19 May 2020 14:02:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1589896979; x=1621432979; h=from:to:cc:subject:date:message-id:in-reply-to: mime-version; bh=AQl+dr9nZ3Mk+ejEyAqg6iIyzMLzrKoY1gkMdRpI11g=; b=YKbFIrKw930fvSjRbWEjaAvQNeeYYjicu9oC5mqo6OovxcfVF35o9w2e /o69S1r5X0oSRamyAufmUlnEFcaU7Bay7Eo++FKIocwb/aoU/d/q/2hs7 XrWrFembfdOa/dvAuFfOhWOGsKTUf6upxA+iFHohhdYI5zLHcGAkCLHkU I=; IronPort-SDR: AGJKKvdzcGhHJPjMXj32XQsUiZNiP5za3svdJ3d3DXZHiZKSrwvPTLIVC7YMQy2zGQGyYcehA2 ACS257MS1gAQ== X-IronPort-AV: E=Sophos;i="5.73,410,1583193600"; d="scan'208";a="44391322" Received: from sea32-co-svc-lb4-vlan2.sea.corp.amazon.com (HELO email-inbound-relay-2a-e7be2041.us-west-2.amazon.com) ([10.47.23.34]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 19 May 2020 14:02:56 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2a-e7be2041.us-west-2.amazon.com (Postfix) with ESMTPS id 470D4A227C; Tue, 19 May 2020 14:02:54 +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, 19 May 2020 14:02:53 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.65) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 19 May 2020 14:02:38 +0000 From: SeongJae Park To: SeongJae Park CC: , SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: Re: [PATCH v12 10/16] mm/damon: Add debugfs interface Date: Tue, 19 May 2020 16:02:21 +0200 Message-ID: <20200519140221.4038-1-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200518100018.2293-11-sjpark@amazon.com> (raw) MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.160.65] X-ClientProxiedBy: EX13D10UWA002.ant.amazon.com (10.43.160.228) 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, 18 May 2020 12:00:12 +0200 SeongJae Park wrote: > From: SeongJae Park > > This commit adds a debugfs interface for DAMON. [...] > diff --git a/mm/damon.c b/mm/damon.c > index ddd78843f79a..f31310536c79 100644 > --- a/mm/damon.c > +++ b/mm/damon.c > @@ -10,6 +10,7 @@ > #define pr_fmt(fmt) "damon: " fmt > [...] > + > +static ssize_t damon_sprint_pids(struct damon_ctx *ctx, char *buf, ssize_t len) > +{ > + struct damon_task *t; > + int written = 0; > + int rc; > + > + damon_for_each_task(t, ctx) { > + rc = snprintf(&buf[written], len - written, "%d ", t->pid); > + if (!rc) > + return -ENOMEM; > + written += rc; > + } > + if (written) > + written -= 1; > + written += snprintf(&buf[written], len - written, "\n"); > + return written; > +} > + > +static ssize_t debugfs_pids_read(struct file *file, > + char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = &damon_user_ctx; > + ssize_t len; > + char pids_buf[320]; > + > + len = damon_sprint_pids(ctx, pids_buf, 320); This could race with concurrent pids debugfs file writers. Should be synchronized. Same to other debugfs files except 'monitor_on', which is already synchronized with corresponding writers. I will enclose this function call with the context mutex in next revision. Thanks, SeongJae Park > + if (len < 0) > + return len; > + > + return simple_read_from_buffer(buf, count, ppos, pids_buf, len); > +} > + > +/* > + * Converts a string into an array of unsigned long integers > + * > + * Returns an array of unsigned long integers if the conversion success, or > + * NULL otherwise. > + */ > +static int *str_to_pids(const char *str, ssize_t len, ssize_t *nr_pids) > +{ > + int *pids; > + const int max_nr_pids = 32; > + int pid; > + int pos = 0, parsed, ret; > + > + *nr_pids = 0; > + pids = kmalloc_array(max_nr_pids, sizeof(pid), GFP_KERNEL); > + if (!pids) > + return NULL; > + while (*nr_pids < max_nr_pids && pos < len) { > + ret = sscanf(&str[pos], "%d%n", &pid, &parsed); > + pos += parsed; > + if (ret != 1) > + break; > + pids[*nr_pids] = pid; > + *nr_pids += 1; > + } > + if (*nr_pids == 0) { > + kfree(pids); > + pids = NULL; > + } > + > + return pids; > +} > + > +static ssize_t debugfs_pids_write(struct file *file, > + const char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = &damon_user_ctx; > + char *kbuf; > + int *targets; > + ssize_t nr_targets; > + ssize_t ret; > + int err; > + > + kbuf = kmalloc(count, GFP_KERNEL); > + if (!kbuf) > + return -ENOMEM; > + > + ret = simple_write_to_buffer(kbuf, count, ppos, buf, count); > + if (ret < 0) > + goto out; > + > + targets = str_to_pids(kbuf, ret, &nr_targets); > + if (!targets) { > + ret = -ENOMEM; > + goto out; > + } > + > + mutex_lock(&ctx->kdamond_lock); > + if (ctx->kdamond) { > + ret = -EINVAL; > + goto unlock_out; > + } > + > + err = damon_set_pids(ctx, targets, nr_targets); > + if (err) > + ret = err; > +unlock_out: > + mutex_unlock(&ctx->kdamond_lock); > + kfree(targets); > +out: > + kfree(kbuf); > + return ret; > +} [...]