mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC v3 1/3] file: Introduce iterate_fd_locked
@ 2023-03-24  5:15 aloktiagi
  2023-03-24  5:15 ` [RFC v3 2/3] file: allow callers to free the old file descriptor after dup2 aloktiagi
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: aloktiagi @ 2023-03-24  5:15 UTC (permalink / raw)
  To: viro, brauner, David.Laight, linux-fsdevel, linux-kernel
  Cc: keescook, hch, aloktiagi, Tycho Andersen

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

Signed-off-by: aloktiagi <aloktiagi@gmail.com>
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-03-24  5:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-24  5:15 [RFC v3 1/3] file: Introduce iterate_fd_locked aloktiagi
2023-03-24  5:15 ` [RFC v3 2/3] file: allow callers to free the old file descriptor after dup2 aloktiagi
2023-03-24  5:28   ` Matthew Wilcox
2023-03-24  5:58     ` Alok Tiagi
2023-03-24  5:15 ` [RFC v3 3/3] file, epoll: Implement do_replace() and eventpoll_replace() aloktiagi
2023-03-24  5:24   ` Matthew Wilcox
2023-03-24  5:56     ` Alok Tiagi
2023-03-24  5:20 ` [RFC v3 1/3] file: Introduce iterate_fd_locked Matthew Wilcox
2023-03-24  5:52   ` Alok Tiagi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®