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 A9C88C433EF for ; Fri, 8 Oct 2021 08:47:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 388BC60F46 for ; Fri, 8 Oct 2021 08:47:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 388BC60F46 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 CE1566B0071; Fri, 8 Oct 2021 04:47:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C91B2900002; Fri, 8 Oct 2021 04:47:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B59DE6B0073; Fri, 8 Oct 2021 04:47:41 -0400 (EDT) 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 A33776B0071 for ; Fri, 8 Oct 2021 04:47:41 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 5E5F22BFAC for ; Fri, 8 Oct 2021 08:47:41 +0000 (UTC) X-FDA: 78672641922.03.C505A9F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf07.hostedemail.com (Postfix) with ESMTP id EB0F11001B1F for ; Fri, 8 Oct 2021 08:47:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1633682860; 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=9WYUCm3uu3x5VEZX6tiSMSzUMl8aPSDJKTcW8O5kKkk=; b=LcroS770xHIxfMbs5ldtIDHHdUX+oZwlAXA+0bUOKODj/jW1kQkm0wFcUixjikmb4HTD+m 5aV5UFhc7G8Pb7YnlZkVjSJ4AOV7k/W6ZUlfQvCRtINsgt/BxfzKXGGQMmxyzfw89j4Ylm vDuIjEX7zJe86LYfdLe9nTE7EzCEn48= 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-578-pUQO03p3Poa_diXaP-qnVQ-1; Fri, 08 Oct 2021 04:47:39 -0400 X-MC-Unique: pUQO03p3Poa_diXaP-qnVQ-1 Received: by mail-wr1-f69.google.com with SMTP id c4-20020a5d6cc4000000b00160edc8bb28so945971wrc.9 for ; Fri, 08 Oct 2021 01:47:39 -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=9WYUCm3uu3x5VEZX6tiSMSzUMl8aPSDJKTcW8O5kKkk=; b=fkyGdPyklnEOGGH1ga01yA68S5IIoXXXYJEbuU8Ih0oFfx7ysXIKTcatW4j+qZBn7Q ryi/OMosuyhDxoiyR9GquFcwoCZQUPo0yKmKuzy/nZYF+Wfn8EtwNkaz/lAcdXb19/iR StxcUvkRLR7Z+j2P1npj7/Qr13LRIvezVm5+K525I40fF5fstFyNjxZcjgt0di5KKbjX GutfH1x9bPYH1rlmho9/VPf/7RWN6riPwYqA2jatJ6jsyF+lVTTjlZZGr9hPixYq9mp4 vlvhm/YaPmMccmL+PYdw8jletwn/lkIOzo6WHUPdiudTW68pSuLKacrSCpe/6GzjDy4t izWg== X-Gm-Message-State: AOAM530+vKdZspKPRlATQUPnIeYfdDJu983PPlAMjCRMYzuoMxlysPaZ iiYPiczKkEalytokbjVYYB8E9zCcjVbvFgi79gH4+4lki0EaxNv2oqlQkHApVwbDueHJemLljBG /2EKlnldq1bU= X-Received: by 2002:adf:9f05:: with SMTP id l5mr2392657wrf.181.1633682858162; Fri, 08 Oct 2021 01:47:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgaKyK69rtKZVDZp9AMMm8t6YHzrkIn4jdU6PRUqG5dwT3K0RrdkSd6xu+7TKAN98/K8kEaQ== X-Received: by 2002:adf:9f05:: with SMTP id l5mr2392638wrf.181.1633682858006; Fri, 08 Oct 2021 01:47:38 -0700 (PDT) Received: from [192.168.3.132] (p5b0c676e.dip0.t-ipconnect.de. [91.12.103.110]) by smtp.gmail.com with ESMTPSA id p3sm11313861wmp.43.2021.10.08.01.47.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 08 Oct 2021 01:47:37 -0700 (PDT) Subject: Re: [PATCH 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 , Minchan Kim References: <20211004125050.1153693-1-christian.brauner@ubuntu.com> <20211004125050.1153693-2-christian.brauner@ubuntu.com> From: David Hildenbrand Organization: Red Hat Message-ID: Date: Fri, 8 Oct 2021 10:47:36 +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: <20211004125050.1153693-2-christian.brauner@ubuntu.com> 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 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: EB0F11001B1F X-Stat-Signature: 171es5qjorqpxh9gznftp7ehat3jnu8h Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=LcroS770; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf07.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com X-HE-Tag: 1633682860-542351 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 04.10.21 14:50, Christian Brauner wrote: > 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. Nice doc. You might want to document what callers of this function are expected to do to clean up. > + */ > +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); The code to be replaced always does the put_pid() after the put_task_struct(). Is this new ordering safe? (didn't check) > + if (!task) > + return ERR_PTR(-ESRCH); > + > + *flags = f_flags; > + return task; > +} > + > /** > * pidfd_create() - Create a new pid file descriptor. > * > I'd have squashed this into the second patch, makes it a lot easier to review and it's only a MM cleanup at this point. -- Thanks, David / dhildenb