Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3599788pxb; Mon, 4 Oct 2021 05:55:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+2A6fNhENS4qH8jzxbRJNvUwTCufRFO21ktSxD3+obC7KGdykj/GJf9NMPkVDogvF5PdS X-Received: by 2002:a17:90a:de16:: with SMTP id m22mr36333985pjv.38.1633352143105; Mon, 04 Oct 2021 05:55:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633352143; cv=none; d=google.com; s=arc-20160816; b=Hs48ix/LqK0Fcit6gFV3atdJSmxHtBMuO/XJxgfQGOduVKcfZsV2Xa0KtYJ228LX8W wYvbb3sClZML/nQw1w2rcsUkJVHBYRVKr1e95gnCChWMFsm0xUU2XXzbKx+sQEsOk3KN 0jB89FNHXwrsrUhessI5IuKdL4V2GvxjuM+K1Po0t1dWm0XsexrSWR4HEWePQqjbRryz 98EMC3tEJI5rfuHwIaaR2xX4vQSbz0tA5xjGvc5Vu8jZTe55oHlI+2ZzfwDVa/+55g8Q 5A5scxutbDcEQDUlZqM7K4mgFF1LDyMzTBiwS0Eiq6FyKjdCci4YpSsMSV1iYvi6+7bN S/yA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=/08OI439LRdrbjJ6SFetFLe2cMhxPiP3qhGNx4LVarU=; b=UOqaHZ/P8tWVQdHUXgsfP3nacmABgOsvtq6coaymLuj+sr+jl6xsmcdFXRn0PoAKDV BXDav0wdV8f1quUsQAARVPy/9Y3meeCog3LUnnnvZsHYEMNa4YXLFr/0Nf29l1S+qlJd gqZsOJx/OyvEOz4Y85vuiJKzPmcYNrbf4qyBdkPxsJrEsrFnc2MmoRApBYIYpGmRb9lp e56zhrS6SfrK7opCirR1Jyismt1jOllNQWTVNgpQxjoeKIWeLqXraBs5x67SJHed8gd6 oxOMa37Fhm6A7s/C6p6+UUyrZdWRcjEZ1cdgZ6vVGAbZ/byFVbukLa6jzBPS6HoznCZ2 HF9g== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j10si13059247plx.300.2021.10.04.05.55.09; Mon, 04 Oct 2021 05:55:43 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233289AbhJDMxd (ORCPT + 99 others); Mon, 4 Oct 2021 08:53:33 -0400 Received: from smtp-relay-canonical-1.canonical.com ([185.125.188.121]:39638 "EHLO smtp-relay-canonical-1.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231765AbhJDMxc (ORCPT ); Mon, 4 Oct 2021 08:53:32 -0400 Received: from wittgenstein.fritz.box (ip5f5bd0d9.dynamic.kabel-deutschland.de [95.91.208.217]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-1.canonical.com (Postfix) with ESMTPSA id 47D9441A72; Mon, 4 Oct 2021 12:51:42 +0000 (UTC) From: Christian Brauner To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Vlastimil Babka , Suren Baghdasaryan , Matthew Bobrowski , Alexander Duyck , Jan Kara , Christian Brauner , Minchan Kim Subject: [PATCH 1/2] pid: add pidfd_get_task() helper Date: Mon, 4 Oct 2021 14:50:49 +0200 Message-Id: <20211004125050.1153693-2-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211004125050.1153693-1-christian.brauner@ubuntu.com> References: <20211004125050.1153693-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Cc: Vlastimil Babka Cc: Suren Baghdasaryan Cc: Matthew Bobrowski Cc: Alexander Duyck Cc: Jan Kara Cc: Minchan Kim Signed-off-by: Christian Brauner --- include/linux/pid.h | 1 + kernel/pid.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 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..2ffbb87b2ce8 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -539,6 +539,40 @@ 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 the given pidfd. + * 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. * -- 2.30.2