2020-11-20 23:22:02

by Eric W. Biederman

[permalink] [raw]
Subject: [PATCH v2 11/24] file: Implement task_lookup_fd_rcu

As a companion to lookup_fd_rcu implement task_lookup_fd_rcu for
querying an arbitrary process about a specific file.

Acked-by: Christian Brauner <[email protected]>
v1: https://lkml.kernel.org/r/20200818103713.aw46m7vprsy4vlve@wittgenstein
Signed-off-by: "Eric W. Biederman" <[email protected]>
---
fs/file.c | 15 +++++++++++++++
include/linux/fdtable.h | 2 ++
2 files changed, 17 insertions(+)

diff --git a/fs/file.c b/fs/file.c
index 5861c4f89419..6448523ca29e 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -865,6 +865,21 @@ struct file *fget_task(struct task_struct *task, unsigned int fd)
return file;
}

+struct file *task_lookup_fd_rcu(struct task_struct *task, unsigned int fd)
+{
+ /* Must be called with rcu_read_lock held */
+ struct files_struct *files;
+ struct file *file = NULL;
+
+ task_lock(task);
+ files = task->files;
+ if (files)
+ file = files_lookup_fd_rcu(files, fd);
+ task_unlock(task);
+
+ return file;
+}
+
/*
* Lightweight file lookup - no refcnt increment if fd table isn't shared.
*
diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h
index 2a4a8fed536e..a0558ae9b40c 100644
--- a/include/linux/fdtable.h
+++ b/include/linux/fdtable.h
@@ -110,6 +110,8 @@ static inline struct file *lookup_fd_rcu(unsigned int fd)
return files_lookup_fd_rcu(current->files, fd);
}

+struct file *task_lookup_fd_rcu(struct task_struct *task, unsigned int fd);
+
struct task_struct;

struct files_struct *get_files_struct(struct task_struct *);
--
2.25.0


2020-11-21 18:22:39

by Cyrill Gorcunov

[permalink] [raw]
Subject: Re: [PATCH v2 11/24] file: Implement task_lookup_fd_rcu

On Fri, Nov 20, 2020 at 05:14:28PM -0600, Eric W. Biederman wrote:
>
> diff --git a/fs/file.c b/fs/file.c
> index 5861c4f89419..6448523ca29e 100644
> --- a/fs/file.c
> +++ b/fs/file.c
> @@ -865,6 +865,21 @@ struct file *fget_task(struct task_struct *task, unsigned int fd)
> return file;
> }
>
> +struct file *task_lookup_fd_rcu(struct task_struct *task, unsigned int fd)
> +{
> + /* Must be called with rcu_read_lock held */

Eric, maybe worth to have something like

RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
"suspicious task_lookup_fd_rcu() usage");

here?

2020-11-22 13:57:49

by Cyrill Gorcunov

[permalink] [raw]
Subject: Re: [PATCH v2 11/24] file: Implement task_lookup_fd_rcu

On Sun, Nov 22, 2020 at 07:00:20AM -0600, Eric W. Biederman wrote:
> Cyrill Gorcunov <[email protected]> writes:
...
> That is present in files_lookup_fd_rcu, so this code should
> be good from the warning side.

Indeed, thanks!