From patchwork Mon Oct 11 13:32:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12549987 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66933C433F5 for ; Mon, 11 Oct 2021 13:33:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E70A060F4B for ; Mon, 11 Oct 2021 13:33:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E70A060F4B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 65FD96B0071; Mon, 11 Oct 2021 09:33:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 60F3D900002; Mon, 11 Oct 2021 09:33:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D70D6B0073; Mon, 11 Oct 2021 09:33:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0134.hostedemail.com [216.40.44.134]) by kanga.kvack.org (Postfix) with ESMTP id 3F4696B0071 for ; Mon, 11 Oct 2021 09:33:19 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id F15AF18023434 for ; Mon, 11 Oct 2021 13:33:18 +0000 (UTC) X-FDA: 78684248076.39.A6EFBE3 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf27.hostedemail.com (Postfix) with ESMTP id 92CB970148E0 for ; Mon, 11 Oct 2021 13:33:18 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id CAE1060EB4; Mon, 11 Oct 2021 13:33:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1633959197; bh=WASKuunDznop2Gt/EdpxjsrUicUNeg8J9P/l7yA5PxU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CRkszoWCS9yTpum+x1Z2Fc1fWXKXROiuAMG7a9hHu06wsH1GoSnSvmQ0FR//QeVL5 kCIXLEolyLGI1MNF/DTC80qJZZNdvJVo53gY5J6GqX3LfMFfOQXiYc5dUiMnyf4fgA no4OadSF7RKNPQU9C0vX9FalLyzUNxHO2hRY38DAqrYg3wOEg0jZd86QADUvQchhVZ o4PnTEhYIYeKmDl7tAjC/173wloUYME7gHicAyPvJa2vfZlMCZWZfWfMi55og+wXKJ FfQPfpk+5ThhrNA6msBH9mbrp2HxvbzIaZeFiR5HktDFAsipzcLl+1ucK+G5A/8iYo WgHbui53XDxBQ== From: Christian Brauner To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Vlastimil Babka , Suren Baghdasaryan , Matthew Bobrowski , Alexander Duyck , David Hildenbrand , Jan Kara , Christian Brauner , Minchan Kim Subject: [PATCH v2 1/2] pid: add pidfd_get_task() helper Date: Mon, 11 Oct 2021 15:32:44 +0200 Message-Id: <20211011133245.1703103-2-brauner@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211011133245.1703103-1-brauner@kernel.org> References: <20211011133245.1703103-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2954; h=from:subject; bh=kXvY2YwbxVX1cpVZZpdvJqM2O3omsoRZqEBDc+r93v8=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMSSm2PzMjw7YsKabR231Ot1T29evm6u34VRRfOTl/Z8MmB9z Bwre7ShlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZjIhZWMDCvtLOuXRmfm5l8I7sjd/p tZYIVy5PMZ6/x2r47im5wt5sHIcHn2U10Tu02KU1htz3fvux1bs56lxGk+v+Fj1RrdXX2H2QE= X-Developer-Key: i=christian.brauner@ubuntu.com; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 92CB970148E0 X-Stat-Signature: 9kod4nn55jmc9a7tgbxt3kus39fysw67 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CRkszoWC; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf27.hostedemail.com: domain of brauner@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=brauner@kernel.org X-HE-Tag: 1633959198-121137 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: Christian Brauner The number of system calls making use of pidfds is constantly increasing. Some of those new system calls duplicate the code to turn a pidfd into task_struct it refers to. Give them a simple helper for this. Link: https://lore.kernel.org/r/20211004125050.1153693-2-christian.brauner@ubuntu.com Cc: Vlastimil Babka Cc: Suren Baghdasaryan Cc: Matthew Bobrowski Cc: Alexander Duyck Cc: David Hildenbrand Cc: Jan Kara Cc: Minchan Kim Reviewed-by: Matthew Bobrowski Signed-off-by: Christian Brauner Acked-by: David Hildenbrand --- /* v2 */ - David Hildenbrand : - Also document that the caller is expected to decrease the reference count on the returned task. --- include/linux/pid.h | 1 + kernel/pid.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/linux/pid.h b/include/linux/pid.h index af308e15f174..343abf22092e 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -78,6 +78,7 @@ struct file; extern struct pid *pidfd_pid(const struct file *file); struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags); +struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags); int pidfd_create(struct pid *pid, unsigned int flags); static inline struct pid *get_pid(struct pid *pid) diff --git a/kernel/pid.c b/kernel/pid.c index efe87db44683..2fc0a16ec77b 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -539,6 +539,42 @@ struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags) return pid; } +/** + * pidfd_get_task() - Get the task associated with a pidfd + * + * @pidfd: pidfd for which to get the task + * @flags: flags associated with this pidfd + * + * Return the task associated with @pidfd. The function takes a reference on + * the returned task. The caller is responsible for releasing that reference. + * + * Currently, the process identified by @pidfd is always a thread-group leader. + * This restriction currently exists for all aspects of pidfds including pidfd + * creation (CLONE_PIDFD cannot be used with CLONE_THREAD) and pidfd polling + * (only supports thread group leaders). + * + * Return: On success, the task_struct associated with the pidfd. + * On error, a negative errno number will be returned. + */ +struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags) +{ + unsigned int f_flags; + struct pid *pid; + struct task_struct *task; + + pid = pidfd_get_pid(pidfd, &f_flags); + if (IS_ERR(pid)) + return ERR_CAST(pid); + + task = get_pid_task(pid, PIDTYPE_TGID); + put_pid(pid); + if (!task) + return ERR_PTR(-ESRCH); + + *flags = f_flags; + return task; +} + /** * pidfd_create() - Create a new pid file descriptor. *