From mboxrd@z Thu Jan 1 00:00:00 1970 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 0FC13C433EF for ; Tue, 12 Oct 2021 14:12:01 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8EAD860EFE for ; Tue, 12 Oct 2021 14:12:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8EAD860EFE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id BC51F6B0071; Tue, 12 Oct 2021 10:11:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B745C6B0072; Tue, 12 Oct 2021 10:11:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A62FC6B0073; Tue, 12 Oct 2021 10:11:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0064.hostedemail.com [216.40.44.64]) by kanga.kvack.org (Postfix) with ESMTP id 9813D6B0071 for ; Tue, 12 Oct 2021 10:11:59 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 4B905243B1 for ; Tue, 12 Oct 2021 14:11:59 +0000 (UTC) X-FDA: 78687974358.12.17683BD Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf16.hostedemail.com (Postfix) with ESMTP id D44E7F00009A for ; Tue, 12 Oct 2021 14:11:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634047917; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OsimebWiWHiIoUpKbRqBPY9Rl7kaR7A4B63k1RE+UMA=; b=APSgExp28Q6BpOcW4PXVEQZUHFuuMIET7fhkxCDwwozEqBhYyhZX/ZbjdEw7b7pf5QEhY7 We256q0yDfYbvqLKqnQ5oP06wDHllKzX+Dt0yIjdLsIal5FvxSxUUwtoNG1rDnA8TNT9eZ pNoYikU8uvDaBQlub3jwM2pvBI7Z0Y8= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-345-2TSRob0ANRmZ-GykGZ5uKg-1; Tue, 12 Oct 2021 10:11:55 -0400 X-MC-Unique: 2TSRob0ANRmZ-GykGZ5uKg-1 Received: by mail-wr1-f69.google.com with SMTP id j19-20020adfb313000000b00160a9de13b3so15849681wrd.8 for ; Tue, 12 Oct 2021 07:11:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=OsimebWiWHiIoUpKbRqBPY9Rl7kaR7A4B63k1RE+UMA=; b=tklKkdlWX0nz08yflqcbcrfiJM1prY9p3DWXqsIghyiIpDmMQr5NVZ3Ga+oDP3XQxG FoO5GVRpZEsymjUy7rNnY1aWHfR4EuKOtj4bUNLQVgVUk29YZ4g5uhc4tQjH63KEL/ib vIVWON+gK0wDeXsqJ2jb+9k76M2TfItd1Q8C5/BIyF6iRVqh67/J/BPSLUG51upVf5jC oMhTF2jybANNWSpqba5/UgDoR5zo0dybdU4l4NXbhPQrCgST6EQEUPjdV7EKGqTB3GPt TUtSTkwkX0vySywu7obm7T5XArJgG63ijvsPc11dVeP41cXu4Vu9isPk2Z9aJzUX2vXn uFhg== X-Gm-Message-State: AOAM531xiTWeoPky4PjTNQkLDhSXX0VSll3NTyJupz6mgSh0CiaGeoNw Aa+AK56dE1+/8t58keKcUNSdbG3Cj40T0wyhH4eiRZTHJw6aMw4rq5M0LU/Wz+T+/f+D+YI22yR 2TpDckZm0eOU= X-Received: by 2002:a1c:2358:: with SMTP id j85mr5783113wmj.1.1634047914660; Tue, 12 Oct 2021 07:11:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvqRQrklWThEKlc50AA6u4uzIfi/vB/pwWCl+oe/S2LBpw2e7bnvnn8I9rpJJ7ta4bF5p5Ng== X-Received: by 2002:a1c:2358:: with SMTP id j85mr5783090wmj.1.1634047914456; Tue, 12 Oct 2021 07:11:54 -0700 (PDT) Received: from [192.168.3.132] (p5b0c6a12.dip0.t-ipconnect.de. [91.12.106.18]) by smtp.gmail.com with ESMTPSA id a2sm11046607wru.82.2021.10.12.07.11.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 Oct 2021 07:11:54 -0700 (PDT) Subject: Re: [PATCH v2 1/2] pid: add pidfd_get_task() helper To: Christian Brauner , linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Vlastimil Babka , Suren Baghdasaryan , Matthew Bobrowski , Alexander Duyck , Jan Kara , Christian Brauner , Minchan Kim References: <20211011133245.1703103-1-brauner@kernel.org> <20211011133245.1703103-2-brauner@kernel.org> From: David Hildenbrand Organization: Red Hat Message-ID: <9eaad83c-d7cb-b3ab-df28-0c6436f156f3@redhat.com> Date: Tue, 12 Oct 2021 16:11:53 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20211011133245.1703103-2-brauner@kernel.org> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=APSgExp2; spf=none (imf16.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D44E7F00009A X-Stat-Signature: w8dktr79z6det3dcc4d8dpxkjai8d1n7 X-HE-Tag: 1634047917-531397 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: On 11.10.21 15:32, Christian Brauner wrote: > 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 > --- > /* 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. > * > Acked-by: David Hildenbrand -- Thanks, David / dhildenb