[RFC 1/3] file: Introduce iterate_fd_locked

From: aloktiagi
Date: Thu Mar 02 2023 - 13:22:22 EST


Callers holding the files->file_lock lock can call iterate_fd_locked instead of
iterate_fd

Signed-off-by: aloktiagi <aloktiagi@xxxxxxxxx>
Reviewed-by: Tycho Andersen <tycho@tycho.pizza>
---
fs/file.c | 21 +++++++++++++++------
include/linux/fdtable.h | 3 +++
2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/fs/file.c b/fs/file.c
index c942c89ca4cd..4b2346b8a5ee 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -1295,15 +1295,12 @@ int f_dupfd(unsigned int from, struct file *file, unsigned flags)
return err;
}

-int iterate_fd(struct files_struct *files, unsigned n,
- int (*f)(const void *, struct file *, unsigned),
- const void *p)
+int iterate_fd_locked(struct files_struct *files, unsigned n,
+ int (*f)(const void *, struct file *, unsigned),
+ const void *p)
{
struct fdtable *fdt;
int res = 0;
- if (!files)
- return 0;
- spin_lock(&files->file_lock);
for (fdt = files_fdtable(files); n < fdt->max_fds; n++) {
struct file *file;
file = rcu_dereference_check_fdtable(files, fdt->fd[n]);
@@ -1313,6 +1310,18 @@ int iterate_fd(struct files_struct *files, unsigned n,
if (res)
break;
}
+ return res;
+}
+
+int iterate_fd(struct files_struct *files, unsigned n,
+ int (*f)(const void *, struct file *, unsigned),
+ const void *p)
+{
+ int res = 0;
+ if (!files)
+ return 0;
+ spin_lock(&files->file_lock);
+ res = iterate_fd_locked(files, n, f, p);
spin_unlock(&files->file_lock);
return res;
}
diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h
index e066816f3519..14882520d1fe 100644
--- a/include/linux/fdtable.h
+++ b/include/linux/fdtable.h
@@ -122,6 +122,9 @@ void do_close_on_exec(struct files_struct *);
int iterate_fd(struct files_struct *, unsigned,
int (*)(const void *, struct file *, unsigned),
const void *);
+int iterate_fd_locked(struct files_struct *, unsigned,
+ int (*)(const void *, struct file *, unsigned),
+ const void *);

extern int close_fd(unsigned int fd);
extern int __close_range(unsigned int fd, unsigned int max_fd, unsigned int flags);
--
2.34.1