Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6862057ybl; Mon, 23 Dec 2019 13:29:06 -0800 (PST) X-Google-Smtp-Source: APXvYqyizvpzq7FWu4xsOHpCqPTJQHMoctrezpkJso7lno+Zzs8zPu3oAT1gXNGRYxhnsM5EIkkF X-Received: by 2002:a9d:4e99:: with SMTP id v25mr35585346otk.363.1577136546074; Mon, 23 Dec 2019 13:29:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577136546; cv=none; d=google.com; s=arc-20160816; b=nxdlX7crZKH801G8BDcyWxIly3zWGvhgk/EKYx5UO1zf2uC7pEFPjfdRkLVMQypfIi J+azrpUJdExXarlD9xKvPRvEvJ7GNiKV3/NcfXFyYSwNfJOpvtglrTMMoCBIRWlr1iWs 1ROw5euwLvxhwBLt4Z5lmA0JkmqBkUwBS0hnGematMIq6OTkaTb37tmBeYY4AaphMcg+ Q9K6dEG2uoxwAPqC1iuFe1c5lqJ2pDHCIJE+eMYK01RKPXCc7+1XxsfLJr5S3XLjbsr3 daHs9WWejx7evwl7J3lK0trT7nSvUDEDKs4WpNMzVzTzemRz3sVPxOuVsKnVb2wrbRDr xu1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=NCZ9Xn1qSsRpRrqMa4G+KR7xmUufgwsOP7o6PfwZRmE=; b=dyfdH6GK6/jC6QmId1Sb4YFTz2aYEGx8WsKt3jaHhSLGeMflXaoO7Q7H4DFVTbBBQs 8JotW+AowPyrIMVtU/3Qot0kc5551fo0LsplzOPd0g0ggHC4NRe1qeFPzFFD/KmjB9pl 05q+qKgPHcU/Up0AUd5PZQJSSMsrSHgEjx6TRpps4hxWnTpB2PMgugq2N1axnPnStCqt s4Zd8DPovsaLX0Ob/xUkEkFwXBToIvQBrPBdx5++Rv3Sjy/CnjvXe9UMbT9pOfMDTvd4 Ng8XmcPGqCAR5BGSRMz4/2ecuvwE+Bf/Hu0dKvcdHUBkiK5J3TAhiC6kqNU8s0EQYWwk w4xg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l20si11098241otd.292.2019.12.23.13.28.53; Mon, 23 Dec 2019 13:29:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726885AbfLWV0v (ORCPT + 99 others); Mon, 23 Dec 2019 16:26:51 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:40144 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726817AbfLWV0v (ORCPT ); Mon, 23 Dec 2019 16:26:51 -0500 Received: from p5b2a6d78.dip0.t-ipconnect.de ([91.42.109.120] helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1ijVDz-0003a4-Q4; Mon, 23 Dec 2019 21:26:47 +0000 Date: Mon, 23 Dec 2019 22:26:46 +0100 From: Christian Brauner To: Sargun Dhillon Cc: linux-kernel@vger.kernel.org, containers@lists.linux-foundation.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, tycho@tycho.ws, jannh@google.com, cyphar@cyphar.com, oleg@redhat.com, luto@amacapital.net, viro@zeniv.linux.org.uk, gpascutto@mozilla.com, ealvarez@mozilla.com, fweimer@redhat.com, jld@mozilla.com, arnd@arndb.de Subject: Re: [PATCH v6 1/3] vfs, fdtable: Add get_task_file helper Message-ID: <20191223212645.3qw7my4u4rjihxjf@wittgenstein> References: <20191223210852.GA25101@ircssh-2.c.rugged-nimbus-611.internal> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20191223210852.GA25101@ircssh-2.c.rugged-nimbus-611.internal> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 23, 2019 at 09:08:55PM +0000, Sargun Dhillon wrote: > This introduces a function which can be used to fetch a file, given an > arbitrary task. As long as the user holds a reference (refcnt) to the > task_struct it is safe to call, and will either return NULL on failure, > or a pointer to the file, with a refcnt. > > Signed-off-by: Sargun Dhillon > --- > fs/file.c | 22 ++++++++++++++++++++-- > include/linux/file.h | 2 ++ > 2 files changed, 22 insertions(+), 2 deletions(-) > > diff --git a/fs/file.c b/fs/file.c > index 2f4fcf985079..0ceeb046f4f3 100644 > --- a/fs/file.c > +++ b/fs/file.c > @@ -706,9 +706,9 @@ void do_close_on_exec(struct files_struct *files) > spin_unlock(&files->file_lock); > } > > -static struct file *__fget(unsigned int fd, fmode_t mask, unsigned int refs) > +static struct file *__fget_files(struct files_struct *files, unsigned int fd, > + fmode_t mask, unsigned int refs) > { > - struct files_struct *files = current->files; > struct file *file; > > rcu_read_lock(); > @@ -729,6 +729,11 @@ static struct file *__fget(unsigned int fd, fmode_t mask, unsigned int refs) > return file; > } > > +static struct file *__fget(unsigned int fd, fmode_t mask, unsigned int refs) > +{ > + return __fget_files(current->files, fd, mask, refs); > +} > + > struct file *fget_many(unsigned int fd, unsigned int refs) > { > return __fget(fd, FMODE_PATH, refs); > @@ -746,6 +751,19 @@ struct file *fget_raw(unsigned int fd) > } > EXPORT_SYMBOL(fget_raw); > > +struct file *fget_task(struct task_struct *task, unsigned int fd) > +{ > + struct file *file = NULL; > + > + task_lock(task); > + if (task->files) > + file = __fget_files(task->files, fd, 0, 1); > + > + task_unlock(task); Nit: remove the \n: task_lock(task); if (task->files) file = __fget_files(task->files, fd, 0, 1); task_unlock(task); Christian