Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6848637ybl; Mon, 23 Dec 2019 13:11:41 -0800 (PST) X-Google-Smtp-Source: APXvYqw0/I/lPKSljXoVB6pL2b7MmJOgVLCBaoUdceLcOfkyaLEGmWDWJ7uDkMHsGIJCpznSadEb X-Received: by 2002:a9d:5e9a:: with SMTP id f26mr34449181otl.307.1577135501417; Mon, 23 Dec 2019 13:11:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577135501; cv=none; d=google.com; s=arc-20160816; b=vOm7VzXMEkEO3/6Wvumkooe2oi/3sj1Q4P2P48eRmTYwJlNNIT4p93j0Mw0bVaw+8k 63K/dzmeaivSooZaXlIUURRwk7QuvAifNrHz6kocps5VArdRr1IWEMxVpCA/JVHN1mpQ haIPnIRze5EZYjzg8c0PO3qvBrcY/gqHLa6hDcHG217Qf1ofiCpjUtlCrN+5cF9mi7TE SH8YilAhFciT+++/mosJ/p3tJ73+/3RHvU/XeiwKWJjpG/BAsKbFUV/TXUCAMTFDfdaq sqOhqccDXr8xa3LzHvoPb07SzYdzcY29M7QxlYGamxuxT4xAklFz/7QhesnAKjtfkpDx LmSg== 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=fUsZxgqrWN7ojLQ0VEG886rkJZ/kZ1jcogWp+GONoSPU8PIf8j+VO9XdMsGE75NrcD 2elL3zT7n2r88uqrbiptJLhSOuM597DyJGF7w8ciWBJAI2FOzJjbXCpcFo4z5zagrrBl DV7Yqz33vnZ/BQtfSCIjhmyBjzg1U+xU2hfLmD5NqXqkL+E1pao6dfskZgoV495iFTLN TQHR1HMhEaLMjGw8kVQ8reRu7JFGP5kKviOaShPiTHyCYAG0M/1C7WbeFBua5kCCAnTe VChN65BFcVwZpvv1Dg0SYUIz+u5n9PZmLKoWJMR1pIm2TU3oRiCnB0q8yPbzYa6LYO4w Z37g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=RP+Gp26f; 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 l8si11669495otq.75.2019.12.23.13.11.00; Mon, 23 Dec 2019 13:11:41 -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=RP+Gp26f; 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 S1726885AbfLWVI7 (ORCPT + 99 others); Mon, 23 Dec 2019 16:08:59 -0500 Received: from mail-io1-f65.google.com ([209.85.166.65]:38037 "EHLO mail-io1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726877AbfLWVI6 (ORCPT ); Mon, 23 Dec 2019 16:08:58 -0500 Received: by mail-io1-f65.google.com with SMTP id v3so17401212ioj.5 for ; Mon, 23 Dec 2019 13:08:58 -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=RP+Gp26fE7zmIx/lxWxOU5NDvBBwKUBVTvmluoGZdVoZzxmnL4q/jEUO/yoodyjEOY 1RU3FU//wLSW9nuw4ZD4yQfAf3jRp+o+i16WGEI+t/COb11uGvU9g28RpLK132XrJGfu IUkx/apzaH4xHfJ/PGGgORat0xD2kMBRbRVi0= 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=LVyoQwZJ6PVLxnc1dq3ZYck2+cuiJGU0zx/jaDLf3HEbdZIKy4KO7A+H8BuE5Eun/a skTendp87um2Eq/G2aSGCUOiY5C8YbB2QVNcxe3b9DZEVzIh59tCSQVYXVr0pFLyIF8f Uj+Sv4/COw11wWFl48c6IIqvUGPXOhmXGryKtV1aFNA3ddkYkI+bjqKlITYVxzJW2Q1P 9MxYx6y/2yKFQwoFk1AaVsIAqw71qOmIYP+ALD6UASyEV8paweV1OhOrhbxIR9n26713 EPQdROGGHSM9qL3kWvzlMd8X3AkAC4X76BtzuWV+UDBPvnab+yB+dki7+4RyA/1XOSmb zGhA== X-Gm-Message-State: APjAAAV88mYsnL/fcw860HKIlY9Xq7HYuQLnsG2SE66iHPhkm3WfVF+d S6maYUcAQDo4NpMRoAaRQKIZnz2cYG3Orw== X-Received: by 2002:a5d:8b04:: with SMTP id k4mr22120558ion.229.1577135337433; Mon, 23 Dec 2019 13:08:57 -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 s8sm6554840iom.46.2019.12.23.13.08.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Dec 2019 13:08:56 -0800 (PST) Date: Mon, 23 Dec 2019 21:08:55 +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 v6 1/3] vfs, fdtable: Add get_task_file helper Message-ID: <20191223210852.GA25101@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