Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3771933ybl; Fri, 20 Dec 2019 15:30:06 -0800 (PST) X-Google-Smtp-Source: APXvYqx7f4uwf4PR4ofuqqplNpOWB90FCcGrWk2Et2+t5vtTaMRcqFBTWgY52pK4GwjSPKbUKMaZ X-Received: by 2002:a05:6830:1b7b:: with SMTP id d27mr18403896ote.78.1576884606786; Fri, 20 Dec 2019 15:30:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576884606; cv=none; d=google.com; s=arc-20160816; b=HnAOneH40Mne5KfQAVgBBZ10A9ml/8qYg46OwrTfdJpYdH6WcfoptbdQ5fT6Q6D/rk G9ctARKJ9XBIdj2SyR4dpl+s1OJ1nksYNmW2VRtpG3lK7C4XqVyPt4OSU1w6q8/Pnjkw 1B56qtVl+ieAzTIQOhYdgMR9Dsu+z1Gdf0KFMxwvuhI5RmDcBPx2w2cAYcxK5sxWdlV4 mrNNcJiNnjUUTEhxkGEYoBGZr1B9ZPegLH95cawL0d33eHQgondZw1xtvnEqdYY4bnex WfZitGtIcu3ym1Kv+8nu1sf1WH88leU9uNJfiZnm3RIeGpfCkC5KgXz9GMQMr5VPKYk7 DIhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=CX/EMM8AnWfaI4rNz03pFAf5OWqiTpv8JaYTV9Mz/lc=; b=fwa3oSAMl8gKgUZys9Y81mqwM2UmU22Xu+ZgP9PncspKqKbGj+8APN4enhilAitTmj 763nh9f5rdJE91Zf09rAT12P2TgeRo90di9q/eFtcqN2DpbxRvZrizh8BEmetXBVcbaA aDXb7c5GeBtzGVlqQhCs+kZcgbs1LK5H1UQaXtT0ADuG83g6EFPwLbwwPsE58FUlKY+H Sgc8oBEryHbcBycOoye12LAQYoQoGNDOaR9TOcEHmiHfa25EG0+snDSrNY+p2ereAupC 6iekKqSFI1dOVBYxg6xawcyTL696trrj4eOFv6AFk9e3sbVH0X1sjrL1h0vtSefn75T6 9Hhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=uK3AhW1c; 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 f21si6207669otp.56.2019.12.20.15.29.54; Fri, 20 Dec 2019 15:30: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; dkim=pass header.i=@sargun.me header.s=google header.b=uK3AhW1c; 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 S1726716AbfLTX2E (ORCPT + 99 others); Fri, 20 Dec 2019 18:28:04 -0500 Received: from mail-io1-f68.google.com ([209.85.166.68]:44424 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726666AbfLTX2E (ORCPT ); Fri, 20 Dec 2019 18:28:04 -0500 Received: by mail-io1-f68.google.com with SMTP id b10so11030302iof.11 for ; Fri, 20 Dec 2019 15:28:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=CX/EMM8AnWfaI4rNz03pFAf5OWqiTpv8JaYTV9Mz/lc=; b=uK3AhW1cGSB2r++u32XTz9HtiQf9DqoAJjZTRWoiXrjhVWY6c5A2GADwJdSXWsp2/X FsGsLqdPVE3e+/7ZjXCDprcZpUcC7yZ4UWf6Jr6Czg9nUCF0j6YtxlOzYegNvAokHGMa IvrQIWpnkjoATVBm197EtU7IuXBoy83PBzK4c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=CX/EMM8AnWfaI4rNz03pFAf5OWqiTpv8JaYTV9Mz/lc=; b=uTqsfHYGgM9115j+HMRvEgPyICcpyPzLXXWwrxIsM46k9+r05YsDxoKMQjguxvnwjo dD6XydPyK2xPkjtvrNFP8h2Ssl/zK4Ke4VJWlVBy6IKtVQKT7lQzuuy7lNAvxlGHvZzZ m57uDvA67c+RXN14FtxYDKnkjyNjP3w38ZV8mS1xtdLHWR14ZciwsD+QTyR1HCL/ViHR /tayKA8Y1RfWAjBBekeAqc4OUp3d/h/1RDuPRO827PIDFWiY4Q0ZECU++mBr50bWZB8M 1aVtfrlWdX0YKBM7XkcwNm6saOftMFKuMc9q/hegLsSU6W1kk2z9vr9G37hQCnT3qIgH 4otg== X-Gm-Message-State: APjAAAW06A6iBzqJxMr0/QLfOjQwYuTOGUPnq0/loa5qnLvf0qCKT+7B kw7/frPGBcLwa9huzZGQORBhSkZMJGtSeQ== X-Received: by 2002:a5e:924c:: with SMTP id z12mr12049921iop.296.1576884482976; Fri, 20 Dec 2019 15:28:02 -0800 (PST) Received: from ircssh-2.c.rugged-nimbus-611.internal (80.60.198.104.bc.googleusercontent.com. [104.198.60.80]) by smtp.gmail.com with ESMTPSA id a12sm3941787ion.73.2019.12.20.15.28.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 20 Dec 2019 15:28:02 -0800 (PST) Date: Fri, 20 Dec 2019 23:28:01 +0000 From: Sargun Dhillon To: linux-kernel@vger.kernel.org, containers@lists.linux-foundation.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: tycho@tycho.ws, jannh@google.com, cyphar@cyphar.com, christian.brauner@ubuntu.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: [PATCH v5 1/3] vfs, fdtable: Add get_task_file helper Message-ID: <20191220232758.GA20224@ircssh-2.c.rugged-nimbus-611.internal> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); + + return file; +} + /* * Lightweight file lookup - no refcnt increment if fd table isn't shared. * diff --git a/include/linux/file.h b/include/linux/file.h index 3fcddff56bc4..c6c7b24ea9f7 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -16,6 +16,7 @@ extern void fput(struct file *); extern void fput_many(struct file *, unsigned int); struct file_operations; +struct task_struct; struct vfsmount; struct dentry; struct inode; @@ -47,6 +48,7 @@ static inline void fdput(struct fd fd) extern struct file *fget(unsigned int fd); extern struct file *fget_many(unsigned int fd, unsigned int refs); extern struct file *fget_raw(unsigned int fd); +extern struct file *fget_task(struct task_struct *task, unsigned int fd); extern unsigned long __fdget(unsigned int fd); extern unsigned long __fdget_raw(unsigned int fd); extern unsigned long __fdget_pos(unsigned int fd); -- 2.20.1