Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp3536075ybk; Tue, 19 May 2020 07:05:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzLDTxh4PRXiEZSREbS+cBLloiH4bAgMLcoeNlb8DNw4vCvhuLYVMHNqIXwuKq8qpf7NEff X-Received: by 2002:a50:f402:: with SMTP id r2mr10111936edm.27.1589897133780; Tue, 19 May 2020 07:05:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589897133; cv=none; d=google.com; s=arc-20160816; b=EPB8p7f9KcwCgBy3Q24W47rgcldHZP/azEgbN9TcuDOiuN4OHJ6Le2D6OJtIzBUL7T SjwSg7pveUt/UTbpkLZ8R9oiKVRpHeekLFwkqfSNbZCEqqFk6cZpSMeAuH7Oc+cTCiy4 KWuTp6hwXiAxHBOdbu6r+V8387XsLqQH1UJ4TSUERzD0oEZ0ZFijsWVjWVCSPZx6upk+ z5fqVKQ/d4wG5kZ6Ak74JFUdl06X5+rKTAnAKj/8nndv358j7YU43Y4btOZ6IjZQSbRJ zVeY02v1hoGXZi7arQduIPJZMjlafwNcpB8gkoJI95oD+PWp/41fSesJ2t3MXnrHq3LL rbnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:in-reply-to:message-id:date :subject:cc:to:from:ironport-sdr:dkim-signature; bh=AQl+dr9nZ3Mk+ejEyAqg6iIyzMLzrKoY1gkMdRpI11g=; b=UiXzqBZB6mGGNiPwdyE16J5kXrhslrnXtcP4qG8nh9QX1oCxMSfS3BMjmG4PwpfGaL /zMuPDpDC56tV2fUYHy5O/gvnVnXYCjVmC+s4dnQPpTTIzO1WI71PPYRBpEauiYxuvDA +BmdREOKjec5FkFDDZONIlXZxyWrXbBqVwJjBrciBu8opz00jwwUfUk/xfSLjepCor32 XzhdFuzqIgbBE2y99ub+v/zBpVOWG58/ST28l8Uxp7VTTduRS7Lfiti0wnMhRFYVqJ/D TUhs/oKGpiwTjY0lT/TaO1Jfw2LUiZ5KFqPEdOMSDJOkiMTgxYM2f5HD+67AA61UR+En +5ZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=HfkCYvgM; 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 dj17si7868985edb.556.2020.05.19.07.05.09; Tue, 19 May 2020 07:05:33 -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=HfkCYvgM; 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 S1728927AbgESODA (ORCPT + 99 others); Tue, 19 May 2020 10:03:00 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:43849 "EHLO smtp-fw-9102.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727057AbgESOC7 (ORCPT ); Tue, 19 May 2020 10:02:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1589896978; x=1621432978; h=from:to:cc:subject:date:message-id:in-reply-to: mime-version; bh=AQl+dr9nZ3Mk+ejEyAqg6iIyzMLzrKoY1gkMdRpI11g=; b=HfkCYvgMk9cCvsvM4GV15pZxVhtmE+VszxlA8+7IjEvMnO2xWgqcjYIQ 0Sx0i9dsWtrMA3UsrWcCVkTl5i3rw11ujQ1cE0JGFxRkJGhvW6eNh0+xN zNKkumQIMX3zzXIHYeVjsv5NFLauJuJstOidkqZ6NbBYOrtZJmUBs/KPQ 0=; 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) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; > +} [...]