linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, viro@ZenIV.linux.org.uk
Cc: hpa@zytor.com, torvalds@osdl.org, arnd@arndb.de, dhowells@redhat.com
Subject: [PATCH 1/3] Wrap accesses to the fd_sets in struct fdtable
Date: Thu, 16 Feb 2012 17:49:42 +0000	[thread overview]
Message-ID: <20120216174942.23314.1364.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <20120216174930.23314.69764.stgit@warthog.procyon.org.uk>

Wrap accesses to the fd_sets in struct fdtable (for recording open files and
close-on-exec flags) so that we can move away from using fd_sets since we
abuse the fd_set structs by not allocating the full-sized structure under
normal circumstances and by non-core code looking at the internals of the
fd_sets.

The first abuse means that use of FD_ZERO() on these fd_sets is not permitted,
since that cannot be told about their abnormal lengths.

This introduces six wrapper functions for setting, clearing and testing
close-on-exec flags and fd-is-open flags:

	void __set_close_on_exec(int fd, struct fdtable *fdt);
	void __clear_close_on_exec(int fd, struct fdtable *fdt);
	bool close_on_exec(int fd, const struct fdtable *fdt);
	void __set_open_fd(int fd, struct fdtable *fdt);
	void __clear_open_fd(int fd, struct fdtable *fdt);
	bool fd_is_open(int fd, const struct fdtable *fdt);

Note that I've prepended '__' to the names of the set/clear functions because
they require the caller to hold a lock to use them.

Note also that I haven't added wrappers for looking behind the scenes at the
the array.  Possibly that should exist too.

Signed-off-by: David Howells <dhowells@redhat.com>
---

 Documentation/filesystems/files.txt          |    4 ++-
 arch/powerpc/platforms/cell/spufs/coredump.c |    2 +-
 drivers/staging/android/binder.c             |   10 ++++-----
 fs/autofs4/dev-ioctl.c                       |    2 +-
 fs/exec.c                                    |    4 ++-
 fs/fcntl.c                                   |   18 ++++++++--------
 fs/file.c                                    |    8 +++----
 fs/open.c                                    |    4 ++-
 fs/proc/base.c                               |    2 +-
 include/linux/fdtable.h                      |   30 ++++++++++++++++++++++++++
 10 files changed, 57 insertions(+), 27 deletions(-)

diff --git a/Documentation/filesystems/files.txt b/Documentation/filesystems/files.txt
index ac2facc..46dfc6b 100644
--- a/Documentation/filesystems/files.txt
+++ b/Documentation/filesystems/files.txt
@@ -113,8 +113,8 @@ the fdtable structure -
 	if (fd >= 0) {
 		/* locate_fd() may have expanded fdtable, load the ptr */
 		fdt = files_fdtable(files);
-		FD_SET(fd, fdt->open_fds);
-		FD_CLR(fd, fdt->close_on_exec);
+		__set_open_fd(fd, fdt);
+		__clear_close_on_exec(fd, fdt);
 		spin_unlock(&files->file_lock);
 	.....
 
diff --git a/arch/powerpc/platforms/cell/spufs/coredump.c b/arch/powerpc/platforms/cell/spufs/coredump.c
index 03c5fce..c2c5b07 100644
--- a/arch/powerpc/platforms/cell/spufs/coredump.c
+++ b/arch/powerpc/platforms/cell/spufs/coredump.c
@@ -122,7 +122,7 @@ static struct spu_context *coredump_next_context(int *fd)
 	struct spu_context *ctx = NULL;
 
 	for (; *fd < fdt->max_fds; (*fd)++) {
-		if (!FD_ISSET(*fd, fdt->open_fds))
+		if (!fd_is_open(*fd, fdt))
 			continue;
 
 		file = fcheck(*fd);
diff --git a/drivers/staging/android/binder.c b/drivers/staging/android/binder.c
index f0b7e66..347bd6e 100644
--- a/drivers/staging/android/binder.c
+++ b/drivers/staging/android/binder.c
@@ -409,11 +409,11 @@ repeat:
 		goto repeat;
 	}
 
-	FD_SET(fd, fdt->open_fds);
+	__set_open_fd(fd, fdt);
 	if (flags & O_CLOEXEC)
-		FD_SET(fd, fdt->close_on_exec);
+		__set_close_on_exec(fd, fdt);
 	else
-		FD_CLR(fd, fdt->close_on_exec);
+		__clear_close_on_exec(fd, fdt);
 	files->next_fd = fd + 1;
 #if 1
 	/* Sanity check */
@@ -454,7 +454,7 @@ static void task_fd_install(
 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
 {
 	struct fdtable *fdt = files_fdtable(files);
-	__FD_CLR(fd, fdt->open_fds);
+	__clear_open_fd(fd, fdt);
 	if (fd < files->next_fd)
 		files->next_fd = fd;
 }
@@ -480,7 +480,7 @@ static long task_close_fd(struct binder_proc *proc, unsigned int fd)
 	if (!filp)
 		goto out_unlock;
 	rcu_assign_pointer(fdt->fd[fd], NULL);
-	FD_CLR(fd, fdt->close_on_exec);
+	__clear_close_on_exec(fd, fdt);
 	__put_unused_fd(files, fd);
 	spin_unlock(&files->file_lock);
 	retval = filp_close(filp, files);
diff --git a/fs/autofs4/dev-ioctl.c b/fs/autofs4/dev-ioctl.c
index 76741d8..3dfd615 100644
--- a/fs/autofs4/dev-ioctl.c
+++ b/fs/autofs4/dev-ioctl.c
@@ -230,7 +230,7 @@ static void autofs_dev_ioctl_fd_install(unsigned int fd, struct file *file)
 	fdt = files_fdtable(files);
 	BUG_ON(fdt->fd[fd] != NULL);
 	rcu_assign_pointer(fdt->fd[fd], file);
-	FD_SET(fd, fdt->close_on_exec);
+	__set_close_on_exec(fd, fdt);
 	spin_unlock(&files->file_lock);
 }
 
diff --git a/fs/exec.c b/fs/exec.c
index 92ce83a..22cc38d 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -2078,8 +2078,8 @@ static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
 	fd_install(0, rp);
 	spin_lock(&cf->file_lock);
 	fdt = files_fdtable(cf);
-	FD_SET(0, fdt->open_fds);
-	FD_CLR(0, fdt->close_on_exec);
+	__set_open_fd(0, fdt);
+	__clear_close_on_exec(0, fdt);
 	spin_unlock(&cf->file_lock);
 
 	/* and disallow core files too */
diff --git a/fs/fcntl.c b/fs/fcntl.c
index 22764c7..75e7c1f 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -32,20 +32,20 @@ void set_close_on_exec(unsigned int fd, int flag)
 	spin_lock(&files->file_lock);
 	fdt = files_fdtable(files);
 	if (flag)
-		FD_SET(fd, fdt->close_on_exec);
+		__set_close_on_exec(fd, fdt);
 	else
-		FD_CLR(fd, fdt->close_on_exec);
+		__clear_close_on_exec(fd, fdt);
 	spin_unlock(&files->file_lock);
 }
 
-static int get_close_on_exec(unsigned int fd)
+static bool get_close_on_exec(unsigned int fd)
 {
 	struct files_struct *files = current->files;
 	struct fdtable *fdt;
-	int res;
+	bool res;
 	rcu_read_lock();
 	fdt = files_fdtable(files);
-	res = FD_ISSET(fd, fdt->close_on_exec);
+	res = close_on_exec(fd, fdt);
 	rcu_read_unlock();
 	return res;
 }
@@ -90,15 +90,15 @@ SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
 	err = -EBUSY;
 	fdt = files_fdtable(files);
 	tofree = fdt->fd[newfd];
-	if (!tofree && FD_ISSET(newfd, fdt->open_fds))
+	if (!tofree && fd_is_open(newfd, fdt))
 		goto out_unlock;
 	get_file(file);
 	rcu_assign_pointer(fdt->fd[newfd], file);
-	FD_SET(newfd, fdt->open_fds);
+	__set_open_fd(newfd, fdt);
 	if (flags & O_CLOEXEC)
-		FD_SET(newfd, fdt->close_on_exec);
+		__set_close_on_exec(newfd, fdt);
 	else
-		FD_CLR(newfd, fdt->close_on_exec);
+		__clear_close_on_exec(newfd, fdt);
 	spin_unlock(&files->file_lock);
 
 	if (tofree)
diff --git a/fs/file.c b/fs/file.c
index 4c6992d..114fea0 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -366,7 +366,7 @@ struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
 			 * is partway through open().  So make sure that this
 			 * fd is available to the new process.
 			 */
-			FD_CLR(open_files - i, new_fdt->open_fds);
+			__clear_open_fd(open_files - i, new_fdt);
 		}
 		rcu_assign_pointer(*new_fds++, f);
 	}
@@ -460,11 +460,11 @@ repeat:
 	if (start <= files->next_fd)
 		files->next_fd = fd + 1;
 
-	FD_SET(fd, fdt->open_fds);
+	__set_open_fd(fd, fdt);
 	if (flags & O_CLOEXEC)
-		FD_SET(fd, fdt->close_on_exec);
+		__set_close_on_exec(fd, fdt);
 	else
-		FD_CLR(fd, fdt->close_on_exec);
+		__clear_close_on_exec(fd, fdt);
 	error = fd;
 #if 1
 	/* Sanity check */
diff --git a/fs/open.c b/fs/open.c
index 77becc0..5720854 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -836,7 +836,7 @@ EXPORT_SYMBOL(dentry_open);
 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
 {
 	struct fdtable *fdt = files_fdtable(files);
-	__FD_CLR(fd, fdt->open_fds);
+	__clear_open_fd(fd, fdt);
 	if (fd < files->next_fd)
 		files->next_fd = fd;
 }
@@ -1080,7 +1080,7 @@ SYSCALL_DEFINE1(close, unsigned int, fd)
 	if (!filp)
 		goto out_unlock;
 	rcu_assign_pointer(fdt->fd[fd], NULL);
-	FD_CLR(fd, fdt->close_on_exec);
+	__clear_close_on_exec(fd, fdt);
 	__put_unused_fd(files, fd);
 	spin_unlock(&files->file_lock);
 	retval = filp_close(filp, files);
diff --git a/fs/proc/base.c b/fs/proc/base.c
index d4548dd..db6ab4b 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -1754,7 +1754,7 @@ static int proc_fd_info(struct inode *inode, struct path *path, char *info)
 
 			fdt = files_fdtable(files);
 			f_flags = file->f_flags & ~O_CLOEXEC;
-			if (FD_ISSET(fd, fdt->close_on_exec))
+			if (close_on_exec(fd, fdt))
 				f_flags |= O_CLOEXEC;
 
 			if (path) {
diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h
index 82163c4..7675da2 100644
--- a/include/linux/fdtable.h
+++ b/include/linux/fdtable.h
@@ -38,6 +38,36 @@ struct fdtable {
 	struct fdtable *next;
 };
 
+static inline void __set_close_on_exec(int fd, struct fdtable *fdt)
+{
+	FD_SET(fd, fdt->close_on_exec);
+}
+
+static inline void __clear_close_on_exec(int fd, struct fdtable *fdt)
+{
+	FD_CLR(fd, fdt->close_on_exec);
+}
+
+static inline bool close_on_exec(int fd, const struct fdtable *fdt)
+{
+	return FD_ISSET(fd, fdt->close_on_exec);
+}
+
+static inline void __set_open_fd(int fd, struct fdtable *fdt)
+{
+	FD_SET(fd, fdt->open_fds);
+}
+
+static inline void __clear_open_fd(int fd, struct fdtable *fdt)
+{
+	FD_CLR(fd, fdt->open_fds);
+}
+
+static inline bool fd_is_open(int fd, const struct fdtable *fdt)
+{
+	return FD_ISSET(fd, fdt->open_fds);
+}
+
 /*
  * Open file table structure
  */


  reply	other threads:[~2012-02-16 19:08 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-08  5:08 [PATCH 00/21] RFC: Make all arches use <asm-generic/posix_types.h> H. Peter Anvin
2012-02-08  5:08 ` H. Peter Anvin
2012-02-08  5:08 ` [PATCH 01/21] posix_types: Make __kernel_[ug]id32_t default to unsigned int H. Peter Anvin
2012-02-20 20:57   ` [tip:core/types] posix_types: Make __kernel_[ug] id32_t " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 02/21] posix_types: Make it possible to override __kernel_fsid_t H. Peter Anvin
2012-02-20 20:58   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 03/21] alpha: Use generic posix_types.h H. Peter Anvin
2012-02-20 20:59   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 04/21] arm: " H. Peter Anvin
2012-02-09  0:57   ` Russell King - ARM Linux
2012-02-20 21:00   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 05/21] avr32: " H. Peter Anvin
2012-02-20 21:01   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 06/21] cris: " H. Peter Anvin
2012-02-08 13:21   ` Jesper Nilsson
2012-02-20 21:01   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 07/21] frv: " H. Peter Anvin
2012-02-20 21:02   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 08/21] h8300: " H. Peter Anvin
2012-02-20 21:03   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 09/21] ia64: " H. Peter Anvin
2012-02-20 21:04   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 10/21] m32r: " H. Peter Anvin
2012-02-20 21:05   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 11/21] m68k: " H. Peter Anvin
2012-02-19 10:28   ` Geert Uytterhoeven
2012-02-20 21:05   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 12/21] mips: " H. Peter Anvin
2012-02-20 21:06   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 13/21] mn10300: " H. Peter Anvin
2012-02-20 21:07   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 14/21] parisc: " H. Peter Anvin
2012-02-20 21:08   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:08 ` [PATCH 15/21] powerpc: " H. Peter Anvin
2012-02-09  6:14   ` Benjamin Herrenschmidt
2012-02-20 21:09   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:09 ` [PATCH 16/21] s390: " H. Peter Anvin
2012-02-08  9:04   ` Martin Schwidefsky
2012-02-08 16:55     ` H. Peter Anvin
2012-02-08 18:01       ` Martin Schwidefsky
2012-02-20 21:09   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:09 ` [PATCH 17/21] sh: Remove unnecessary posix_types.h type overrides H. Peter Anvin
2012-02-20 21:10   ` [tip:core/types] sh: Remove unnecessary posix_types. h " tip-bot for H. Peter Anvin
2012-02-08  5:09 ` [PATCH 18/21] sparc: Use generic posix_types.h H. Peter Anvin
2012-02-09  1:27   ` David Miller
2012-02-20 21:11   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:09 ` [PATCH 19/21] x86: " H. Peter Anvin
2012-02-20 21:12   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:09 ` [PATCH 20/21] xtensa: " H. Peter Anvin
2012-02-20 21:13   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08  5:09 ` [PATCH 21/21] posix_types: Remove fd_set macros H. Peter Anvin
2012-02-20 21:13   ` [tip:core/types] " tip-bot for H. Peter Anvin
2012-02-08 12:20 ` [PATCH 21/21] " David Howells
2012-02-08 16:57   ` H. Peter Anvin
2012-02-08 21:24   ` David Howells
2012-02-08 21:30     ` H. Peter Anvin
2012-02-14 18:59       ` Tony Luck
2012-02-14 19:18       ` David Howells
2012-02-14 19:44         ` H. Peter Anvin
2012-02-14 20:14           ` H. Peter Anvin
2012-02-16 13:42 ` [PATCH 20/21] xtensa: Use generic posix_types.h David Howells
2012-02-16 17:45   ` Marc Gauthier
2012-02-16 13:44 ` [PATCH 13/21] mn10300: " David Howells
2012-02-16 13:44 ` [PATCH 15/21] powerpc: " David Howells
2012-02-16 20:26   ` Benjamin Herrenschmidt
2012-02-16 20:58     ` H. Peter Anvin
2012-02-16 17:49 ` [PATCH 0/3] Eliminating __FD_*() functions from the kernel David Howells
2012-02-16 17:49   ` David Howells [this message]
2012-02-20 21:14     ` [tip:core/types] Wrap accesses to the fd_sets in struct fdtable tip-bot for David Howells
2012-02-16 17:49   ` [PATCH 2/3] Replace the fd_sets in struct fdtable with an array of unsigned longs David Howells
2012-02-20 21:15     ` [tip:core/types] " tip-bot for David Howells
2012-02-16 17:50   ` [PATCH 3/3] Delete the __FD_*() funcs for operating on fd_set from linux/time.h David Howells
2012-02-20 21:16     ` [tip:core/types] " tip-bot for David Howells
2012-02-20 21:12 ` [PATCH 13/21] mn10300: Use generic posix_types.h David Howells
2012-02-20 21:12 ` [PATCH 07/21] frv: " David Howells

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120216174942.23314.1364.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=arnd@arndb.de \
    --cc=hpa@zytor.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@osdl.org \
    --cc=viro@ZenIV.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).