From patchwork Tue Aug 4 09:14:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11700079 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 D5E411392 for ; Tue, 4 Aug 2020 09:18:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D2FAC22CB2 for ; Tue, 4 Aug 2020 09:18:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="cXiAM6a+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D2FAC22CB2 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 E9F9F8D013F; Tue, 4 Aug 2020 05:18:29 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E766C8D0081; Tue, 4 Aug 2020 05:18:29 -0400 (EDT) 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 D65D68D013F; Tue, 4 Aug 2020 05:18:29 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0083.hostedemail.com [216.40.44.83]) by kanga.kvack.org (Postfix) with ESMTP id BFFCD8D0081 for ; Tue, 4 Aug 2020 05:18:29 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 86BDEBBC5 for ; Tue, 4 Aug 2020 09:18:29 +0000 (UTC) X-FDA: 77112335538.09.turn54_2406b6f26fa5 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 62E98180AD806 for ; Tue, 4 Aug 2020 09:18:29 +0000 (UTC) X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=478224244=sjpark@amazon.com,,RULES_HIT:30034:30051:30054:30064:30070:30080,0,RBL:72.21.198.25:@amazon.com:.lbl8.mailshell.net-66.10.201.10 62.18.0.100;04ygsgrei7qe75j76ua4ay3331m9qycjyxs83qcwpyo19ap4btzcnbqws4y56jt.e8t9o791dqsk6t6dm1o738y37zbcud87xbupphycy785181owd5j6p9ahgxbgem.g-lbl8.mailshell.net-223.238.255.100,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: turn54_2406b6f26fa5 X-Filterd-Recvd-Size: 5992 Received: from smtp-fw-4101.amazon.com (smtp-fw-4101.amazon.com [72.21.198.25]) by imf34.hostedemail.com (Postfix) with ESMTP for ; Tue, 4 Aug 2020 09:18:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1596532709; x=1628068709; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=ZahW3cCbZDrLljIKQak41NNjLpTyRpmxCU8KvV7M7WM=; b=cXiAM6a+chvH1P5oEjurt5J4BYEOIBPrdoCrkWCbSsrGkvYm7kMvje3q pMfClME/7eHG0aZOqj28K9RVaO4ArvTJRy/Uoa829wKJl0cdcM4ASLVPZ xs+/wE8QKu03aNBebSbrNwTVR5qJow3S4HMWDE5ZLy9IjIBsDXa4ubrme w=; IronPort-SDR: 22njl7WK5u7wZL+YBVWGd5Ed7AyOo1x+MSZCy0jWJcIbWKWQutJylA156F7OJZmdG1Yxcffo0m G28rz6UqhyNg== X-IronPort-AV: E=Sophos;i="5.75,433,1589241600"; d="scan'208";a="46006402" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-2a-60ce1996.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 04 Aug 2020 09:18:24 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan3.pdx.amazon.com [10.170.41.166]) by email-inbound-relay-2a-60ce1996.us-west-2.amazon.com (Postfix) with ESMTPS id 89B11A313F; Tue, 4 Aug 2020 09:18:22 +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, 4 Aug 2020 09:18:21 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.248) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 4 Aug 2020 09:18:04 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v19 10/15] damon/debugfs: Support pidfd target id Date: Tue, 4 Aug 2020 11:14:11 +0200 Message-ID: <20200804091416.31039-11-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200804091416.31039-1-sjpark@amazon.com> References: <20200804091416.31039-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.162.248] X-ClientProxiedBy: EX13D36UWA004.ant.amazon.com (10.43.160.175) To EX13D31EUA001.ant.amazon.com (10.43.165.15) X-Rspamd-Queue-Id: 62E98180AD806 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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 programming interface users are safe from pid recycling problem since it uses 'struct pid *' as target id. However, debugfs interface users might still get the problem since they should describe the target ids with pid numbers. This commit makes the debugfs interface to support pidfd target ids. Using it, debugfs interface users can also easily be safe from the races. That said, the pid numbers are stil supported for the simple command line uses. The pid numbers target ids usages are same to the old one. Therefore, old users don't need to change anything. To use pidfd target ids, users should add a prefix, 'pidfd ' in front of the numbers. Because pidfd is process private, reading 'target_ids' file again will show pid numbers of the target processes. Signed-off-by: SeongJae Park --- mm/damon.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/mm/damon.c b/mm/damon.c index 01003a1ff551..61d93f1ea974 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -1450,11 +1450,32 @@ static unsigned long *str_to_target_ids(const char *str, ssize_t len, return ids; } +/* Returns pid for the given pidfd if it's valid, or NULL otherwise. */ +static struct pid *damon_get_pidfd_pid(unsigned int pidfd) +{ + struct fd f; + struct pid *pid; + + f = fdget(pidfd); + if (!f.file) + return NULL; + + pid = pidfd_pid(f.file); + if (!IS_ERR(pid)) + get_pid(pid); + else + pid = NULL; + + fdput(f); + return pid; +} + static ssize_t debugfs_target_ids_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct damon_ctx *ctx = &damon_user_ctx; - char *kbuf; + char *kbuf, *nrs; + bool received_pidfds = false; unsigned long *targets; ssize_t nr_targets; ssize_t ret = count; @@ -1466,13 +1487,23 @@ static ssize_t debugfs_target_ids_write(struct file *file, if (IS_ERR(kbuf)) return PTR_ERR(kbuf); - targets = str_to_target_ids(kbuf, ret, &nr_targets); + nrs = kbuf; + if (!strncmp(kbuf, "pidfd ", 6)) { + received_pidfds = true; + nrs = &kbuf[6]; + } + + targets = str_to_target_ids(nrs, ret, &nr_targets); if (!targets) { ret = -ENOMEM; goto out; } - if (targetid_is_pid(ctx)) { + if (received_pidfds) { + for (i = 0; i < nr_targets; i++) + targets[i] = (unsigned long)damon_get_pidfd_pid( + (unsigned int)targets[i]); + } else if (targetid_is_pid(ctx)) { for (i = 0; i < nr_targets; i++) targets[i] = (unsigned long)find_get_pid( (int)targets[i]);