Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp510650ybl; Tue, 7 Jan 2020 10:00:37 -0800 (PST) X-Google-Smtp-Source: APXvYqwA5LhpHdLsmyAOlwmwSe4Q4c9hg1JQ2Y9CdiTlOYE5JgDSMDtvRhI1Zr7wRY5Q+JCz7S1E X-Received: by 2002:a05:6830:1c81:: with SMTP id v1mr1033201otf.83.1578420037291; Tue, 07 Jan 2020 10:00:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578420037; cv=none; d=google.com; s=arc-20160816; b=WOg4AIxiBNFx1yjsbWie2QeAcyBcvrB7DrakPU9RcHLVNQfjboQKq5gK+9dPhzq6U3 /Mcf+jEsCZ567fg0dCqoerk+c+EC2bhoVu3O1r6gupcTAZ/eUl6Sv1I98jxZ+XqTjwPa R8RtF5UCpAYtFdUOAA9zaTDnBTenxxqypHWIom3BBzc/CQcwdbVDx5rnGanEmFYDzZNE j5GO7YczcDWFtNxZskhZRMz+TmxtPv5p5J3Peh9vZKEkWqazWOJ6S6/wpxMMmjVTm1gn TYRftUvw+Amx1SaZrTPZhsmq1Igigen0Io9tjSqX3AoVrK3cLkMwME3jo6o1mcPsxWJ+ k6qQ== 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=wjPstdOdPQznhEQFIh417dl7KP+FywX4Lr4hyigBkF3yL2qhIdDu2gLxwiKO/oS6s6 bBuySrkScL/BswueoCEyZEcubLEdRNyZVI+nNKvPQN94QgkYIAxOd55wYwLnERRR2UIi HxoDInDCJ2uC01Y1kadDDKhzD/W7p1mAT4sgAbtayS5hK18976pQ4hYdojlJUIa8mzqF QGreP7lA8mQa7FOeEmlN56p/RTnh6fwWHHx5fX7lB5TxJOGg+NM7VRuWKAF9TSBJ9mFj mTrW6DPLKGw59EzAuoSeG7TiHHMTjpDlfxA9oOMGHbCvBaxR+AY0fZqruTot+JSFSnU3 zJmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=lJ1N1mca; 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 v1si274557otf.161.2020.01.07.10.00.23; Tue, 07 Jan 2020 10:00:37 -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=lJ1N1mca; 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 S1728556AbgAGR7h (ORCPT + 99 others); Tue, 7 Jan 2020 12:59:37 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:45533 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728511AbgAGR7g (ORCPT ); Tue, 7 Jan 2020 12:59:36 -0500 Received: by mail-pl1-f193.google.com with SMTP id b22so10991pls.12 for ; Tue, 07 Jan 2020 09:59:36 -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=lJ1N1mcaGLZP4bT4c0ZIaXqktcuv43wBr2iHYz2PFdsVMaokOo5disBOQMODE4/gRJ TCfcLK+HIdcvx4gEttetYlyl7ISnn6L+kXOuovJTpc3K9Tj66KpnZoRs7iQAH/qXezQk OPGSn5Qdjuy13zHtBoxyUf6dcQzQ1+DrpbfBI= 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=XKqDYA1SQzT0uqPGIkGbbvhcLNAPxwtd1ABHnAgKBLQd1zqj5scX+K2IhIt8u2vG4/ e/61Bp9dgeu8XRqwuGuaPcTzL2s87p8TlbaZLshAI6SRvswW2AxfQlxNwBkIM3V0aBry r1xMegM9mQMAC0qzvwoF1sTvdDrLs+i2YOKexbL8cbklGXqDdFQfzJpSajgqLlElsxqi 1lD98SZzIztvdCShJ/+ok/Op0Yto4CI45Q9MIXvcVMmwuM05rTnm0NwyPeCH8yk8VWN3 LreV2mpo08pVcLQhxP7SGaA2RtEhNBUbY/kvtYk4eF3Yw44yxOriZLodfR6uIqnMakt/ QGTA== X-Gm-Message-State: APjAAAW2AXjMR+7d08BH8xbZK7cRPUYG9D+p8ihRSYuVlJEwNypcgrU1 a/dZh1l0HzeyiYIgYlbrPKxzKMurr1KY9g== X-Received: by 2002:a17:902:8b89:: with SMTP id ay9mr845427plb.309.1578419975544; Tue, 07 Jan 2020 09:59:35 -0800 (PST) Received: from ubuntu.netflix.com (166.sub-174-194-208.myvzw.com. [174.194.208.166]) by smtp.gmail.com with ESMTPSA id g7sm210324pfq.33.2020.01.07.09.59.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2020 09:59:34 -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 v9 1/4] vfs, fdtable: Add fget_task helper Date: Tue, 7 Jan 2020 09:59:24 -0800 Message-Id: <20200107175927.4558-2-sargun@sargun.me> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200107175927.4558-1-sargun@sargun.me> References: <20200107175927.4558-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