Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp18760148ybl; Fri, 3 Jan 2020 08:32:13 -0800 (PST) X-Google-Smtp-Source: APXvYqxRflrbhI72gbumsNgDAjnRi57T+04202o71rS9Dv8lL2k9DA/FVejcQUFZTFHJ6WnRyI2+ X-Received: by 2002:aca:f1d4:: with SMTP id p203mr4173898oih.116.1578069133372; Fri, 03 Jan 2020 08:32:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578069133; cv=none; d=google.com; s=arc-20160816; b=QoMK34BgFLcLimndde3jtFW7NyA0u4wqDGWCG6isGn9dOa6MIocC7qHRRBEeIaYIhR ubmfGkUcf3WEIJnHoz2vtygUNFdoTSW+mfFJTgkjSwqW5a+mYGNz2VQgAIKYQn35lYuN dQUTNeDnjfCulJ1qz07SPfM7AKH1WGAg+x5Jr0XM/YtZzx1XpLH9Nqi8R7vSxzQ1/MvM lr2lmP4Kqdx8ce2GWVp+Ym9RJwxz4ADE3AoQFrmMBm0ZJUc+4+zwss1AxS689MswNnmS 5LsOPOjUlr+Wb1tkqiI6POFFQt4vxos/GkFE6maJpgb9boZbjMRki3JOUwwNM9Za5uar ehjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=eB+H+wKtr9wg6rNJfWFUAgXbI+ShVm182xgfnmGZIDU=; b=rjeA+Ii4gDE6o8Qgw53mEU98RDsAuubn5lj+HQtG09ykqZRB4123ZNoHfYtgC4oIJV JzBLM2PBCC5M8RsSpoLyatePncdaseY8pPjUK31fyWtwgX6uDI3NKDniYlRsR3HnbkZB Dmtfv0LvWX3hnFzIoObbYiDv9L6B2D3jBl0H9OulLZxNJ5YOkQTJaY7z/98CDawEY8UU IR0zbBmDHUKtOh4DaRhEHUo/oPTOIjeOnqqU1V6KDUpxgaG4XqhGfw/sPJD8MsFqllyI We+iOp7QEd3nkiTua3P8+aYbL3QbxG58bofwm6A7RPwLrhSKv6mZCTlM+cVbPdGyvyY3 4z4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=f85XQOnz; 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 p184si28172140oib.144.2020.01.03.08.32.01; Fri, 03 Jan 2020 08:32:13 -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=f85XQOnz; 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 S1728074AbgACQaG (ORCPT + 99 others); Fri, 3 Jan 2020 11:30:06 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:35377 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727928AbgACQ3q (ORCPT ); Fri, 3 Jan 2020 11:29:46 -0500 Received: by mail-pl1-f193.google.com with SMTP id g6so19236248plt.2 for ; Fri, 03 Jan 2020 08:29:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eB+H+wKtr9wg6rNJfWFUAgXbI+ShVm182xgfnmGZIDU=; b=f85XQOnziddmbXWnevVH8tqyCb+Bv2paNJFQ//lYFR8+YL/nNkZiibCRRb/B/qs1mE /2m5HAW5x50jSsqxUBv2+MTVdX0fGsBR5uGv0xqc1dRr+LZyZvmxrUm35nMNmgfgi3T1 +12bJ0bbhU+M/ssVM8JzR0SYVBMvPfhwBxz1E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eB+H+wKtr9wg6rNJfWFUAgXbI+ShVm182xgfnmGZIDU=; b=LpSM37cTXK5ki/hq2T8tJzqyM/rf0qgh3YwRfx1xxOdXnKO4xhy00pnOUy6wborH2q PoSKbMvw0UAn74P8vtgkS7A1GMotdffypEvY1c8yz/gGeD54l+IqntmCg+eVpdHFmY/j /+lkZ6TkoUydJAG6xmZXHhpaxzZ54G2hDAOIzxjfICWJM24QwIyffsP1EpLs+e2fCLoj JqXNAk+I7/CyHi9/eWcdmQulSu/18GIaSQBMkdRSYJNkIrsbuXStrzVupBz2SH2RvuBh RdA8vmnLXJvvIB0XIu8/yL4DdLmrp8xskCPzBjAhL0On2o0saLDUFRzk3IoDZwFrax/6 jgOg== X-Gm-Message-State: APjAAAWbq9dMaJk+gZh/5D/DxagIefjYnXEiWMnYu4xYk+zHIZ0F6mHW JfCAnpcX8MFW8AQFgjNQr8IHLjzOkz1gv4c2 X-Received: by 2002:a17:90a:fc82:: with SMTP id ci2mr27417376pjb.13.1578068985365; Fri, 03 Jan 2020 08:29:45 -0800 (PST) Received: from ubuntu.netflix.com (203.20.25.136.in-addr.arpa. [136.25.20.203]) by smtp.gmail.com with ESMTPSA id m22sm67373970pgn.8.2020.01.03.08.29.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Jan 2020 08:29:44 -0800 (PST) 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: Sargun Dhillon , 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 v8 1/3] vfs, fdtable: Add get_task_file helper Date: Fri, 3 Jan 2020 08:29:26 -0800 Message-Id: <20200103162928.5271-2-sargun@sargun.me> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200103162928.5271-1-sargun@sargun.me> References: <20200103162928.5271-1-sargun@sargun.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. This patch is based on Oleg Nesterov's (cf. [1]) patch from September 2018. [1]: Link: https://lore.kernel.org/r/20180915160423.GA31461@redhat.com Signed-off-by: Sargun Dhillon Suggested-by: Oleg Nesterov Acked-by: Christian Brauner --- 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..2fc5eeef54a4 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,12 @@ static struct file *__fget(unsigned int fd, fmode_t mask, unsigned int refs) return file; } +static inline 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 +752,18 @@ 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