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. * From patchwork Mon Oct 11 13:32:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12549989 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 12586C433EF for ; Mon, 11 Oct 2021 13:33:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BAB3C60F6E for ; Mon, 11 Oct 2021 13:33:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BAB3C60F6E 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 5743C6B0072; Mon, 11 Oct 2021 09:33:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 524466B0073; Mon, 11 Oct 2021 09:33:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3ECD66B0074; Mon, 11 Oct 2021 09:33:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0018.hostedemail.com [216.40.44.18]) by kanga.kvack.org (Postfix) with ESMTP id 311946B0072 for ; Mon, 11 Oct 2021 09:33:25 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id D8A65181AF5C1 for ; Mon, 11 Oct 2021 13:33:24 +0000 (UTC) X-FDA: 78684248328.37.1546078 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf12.hostedemail.com (Postfix) with ESMTP id 811931001127 for ; Mon, 11 Oct 2021 13:33:24 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 7FF7460F38; Mon, 11 Oct 2021 13:33:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1633959202; bh=DXIzCSpi0RMG4dxsT+13/zfQG6b0wwvFu8K9jUKsz4o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=boN89nL87b+2RXpe4mOrWkPGADimTy3zB2vYJVOVXzZe9mJTtGw1E5nYKI3FU8g77 lWdg437+a7T1oKsakmN7/slXtLUMHypqF3acPLY9bXdB1ULVngI66/kHaGzrXMBOC+ xcnHkg1DnHJs8g49J9jGjt9Fr9cE/8Lh7cRWZ15UXEzGPWb0gtHZ0XUQ4O1JdqlpIP tCCSw3ga+K5OzvQB8jUGZkIdjBMdUUIscvGxe3U4pPvfLvKPO0Dy0Y8P7ErQNJNwn8 ttOK4+d9xZ/+HRSf0IpdxKW9AEIfbe9E8ogHcZ16Lh6puUH0q7ZaVJscFAMVBOwFLm qksEaMJ0KeyPw== 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 2/2] mm: use pidfd_get_task() Date: Mon, 11 Oct 2021 15:32:45 +0200 Message-Id: <20211011133245.1703103-3-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=2998; h=from:subject; bh=hPRC+QAhxeSGyr5q6D0CanWJl4f/7BbqBOnGLZetuAE=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMSSm2PxMcnkzIST7bdw6hkkrbZqTuN5e7t6/sOWp07cFZnGX bXTVO0pZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACbyajkjw4KFN67P+/nA+6990t3uc0 lfd5/65RHpzheW8SP+jUWb6iqG/6Xp8+cuaYv/Yd+yxWPCBMWrZQIZH5a9/tys1G4smTV/DwMA X-Developer-Key: i=christian.brauner@ubuntu.com; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 811931001127 X-Stat-Signature: i8tabfz879w4h3x7ybrzp3t8dfjdw3to Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=boN89nL8; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of brauner@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=brauner@kernel.org X-HE-Tag: 1633959204-635630 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 Instead of duplicating the same code in two places use the newly added pidfd_get_task() helper. This fixes an (unimportant for now) bug where PIDTYPE_PID is used whereas PIDTYPE_TGID should have been used. Link: https://lore.kernel.org/r/20211004125050.1153693-3-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 */ unchanged --- mm/madvise.c | 15 +++------------ mm/oom_kill.c | 15 +++------------ 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 0734db8d53a7..8c927202bbe6 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1235,7 +1235,6 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, struct iovec iovstack[UIO_FASTIOV], iovec; struct iovec *iov = iovstack; struct iov_iter iter; - struct pid *pid; struct task_struct *task; struct mm_struct *mm; size_t total_len; @@ -1250,18 +1249,12 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, if (ret < 0) goto out; - pid = pidfd_get_pid(pidfd, &f_flags); - if (IS_ERR(pid)) { - ret = PTR_ERR(pid); + task = pidfd_get_task(pidfd, &f_flags); + if (IS_ERR(task)) { + ret = PTR_ERR(task); goto free_iov; } - task = get_pid_task(pid, PIDTYPE_PID); - if (!task) { - ret = -ESRCH; - goto put_pid; - } - if (!process_madvise_behavior_valid(behavior)) { ret = -EINVAL; goto release_task; @@ -1301,8 +1294,6 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, mmput(mm); release_task: put_task_struct(task); -put_pid: - put_pid(pid); free_iov: kfree(iov); out: diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 831340e7ad8b..70d399d5817e 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -1151,21 +1151,14 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) struct task_struct *p; unsigned int f_flags; bool reap = true; - struct pid *pid; long ret = 0; if (flags) return -EINVAL; - pid = pidfd_get_pid(pidfd, &f_flags); - if (IS_ERR(pid)) - return PTR_ERR(pid); - - task = get_pid_task(pid, PIDTYPE_TGID); - if (!task) { - ret = -ESRCH; - goto put_pid; - } + task = pidfd_get_task(pidfd, &f_flags); + if (IS_ERR(task)) + return PTR_ERR(task); /* * Make sure to choose a thread which still has a reference to mm @@ -1204,8 +1197,6 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) mmdrop(mm); put_task: put_task_struct(task); -put_pid: - put_pid(pid); return ret; #else return -ENOSYS;