Skip to contentSkip to conversation

Fix restored pipe usage counts

PATCH In review Dan Smith 3 participants 4 messages Feb 1, 2011 – Feb 3, 2011 2 messages not in the archive VFS

lore
Share & tools
lore

Dan Smith

author
#0 lore raw

PATCH 1/1 Fix restored pipe usage counts

Oren's version of my patch leaks pipe objects due to the way the fget() and fput() operations are structured. The intent was to avoid getting and putting references more than necessary, but the result was a more confusing (IMHO) pattern that resulted in keeping an extra reference in some situations.

This patch changes the pattern to something more easily understood and verified, and also allows several of my tests to pass again, as they did before the recent patch.

---
 fs/pipe.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
fs/pipe.c +6 -6
index 908b8bc..f9ad0f2 100644
@@ -968,6 +968,7 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)
 		if (!file)	/* this should _never_ happen ! */
 			return ERR_PTR(-EBADF);
 		ret = restore_pipe(ctx, file);
+		fput(file);
 		if (ret < 0)
 			goto out;
 
@@ -978,13 +979,12 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)
 		 * other side of the pipe to the hash, to be picked up
 		 * when that side is restored.
 		 */
-		if (which == 1) {	/* the 'other' size */
-			fput(file);
-			file = fget(fds[0]);
-			if (!file)	/* this should _never_ happen ! */
-				return ERR_PTR(-EBADF);
-		}
+		file = fget(fds[1-which]);
+		if (!file)	/* this should _never_ happen ! */
+			return ERR_PTR(-EBADF);
+
 		ret = ckpt_obj_insert(ctx, file, h->pipe_objref, CKPT_OBJ_FILE);
+		fput(file);
 		if (ret < 0)
 			goto out;
 
Signature · 1 line
1.7.2.2

A message that is not in the archive (sent off-list or not yet received): 1296586032-5658-1-git-send-email-danms-r/[email protected]

#2 lore raw

Thanks, pushed to v23-rc1.

Oren

Quoted 45 lines · Dan Smith

Oren's version of my patch leaks pipe objects due to the way the fget() and fput() operations are structured. The intent was to avoid getting and putting references more than necessary, but the result was a more confusing (IMHO) pattern that resulted in keeping an extra reference in some situations.

This patch changes the pattern to something more easily understood and verified, and also allows several of my tests to pass again, as they did before the recent patch.

---
 fs/pipe.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
fs/pipe.c +6 -6
index 908b8bc..f9ad0f2 100644
@@ -968,6 +968,7 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)
 		if (!file)	/* this should _never_ happen ! */
 			return ERR_PTR(-EBADF);
 		ret = restore_pipe(ctx, file);
+		fput(file);
 		if (ret < 0)
 			goto out;
 
@@ -978,13 +979,12 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)
 		 * other side of the pipe to the hash, to be picked up
 		 * when that side is restored.
 		 */
-		if (which == 1) {	/* the 'other' size */
-			fput(file);
-			file = fget(fds[0]);
-			if (!file)	/* this should _never_ happen ! */
-				return ERR_PTR(-EBADF);
-		}
+		file = fget(fds[1-which]);
+		if (!file)	/* this should _never_ happen ! */
+			return ERR_PTR(-EBADF);
+
 		ret = ckpt_obj_insert(ctx, file, h->pipe_objref, CKPT_OBJ_FILE);
+		fput(file);
 		if (ret < 0)
 			goto out;
 
#3 lore raw
In reply to a message not in the archive
Quoted 29 lines · Dan Smith

Oren's version of my patch leaks pipe objects due to the way the fget() and fput() operations are structured. The intent was to avoid getting and putting references more than necessary, but the result was a more confusing (IMHO) pattern that resulted in keeping an extra reference in some situations.

This patch changes the pattern to something more easily understood and verified, and also allows several of my tests to pass again, as they did before the recent patch.

---
 fs/pipe.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
fs/pipe.c +1 -0
index 908b8bc..f9ad0f2 100644
@@ -968,6 +968,7 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)
 		if (!file)	/* this should _never_ happen ! */
 			return ERR_PTR(-EBADF);
 		ret = restore_pipe(ctx, file);
+		fput(file);
 		if (ret < 0)
 			goto out;
 
@@ -978,13 +979,12 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)
 		 * other side of the pipe to the hash, to be picked up
 		 * when that side is restored.

fs/pipe.c struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr) · line 984

Quoted 10 lines
fs/pipe.c +2 -6
 		 */
-		if (which == 1) {	/* the 'other' size */
-			fput(file);
-			file = fget(fds[0]);
-			if (!file)	/* this should _never_ happen ! */
-				return ERR_PTR(-EBADF);
-		}
 
+		file = fget(fds[1-which]);
+		if (!file)	/* this should _never_ happen ! */

If it should never happen then this should be:

		BUG_ON(!file);

that way we see the bug as soon as it's detected rather than having to dig through the stack and 'step' through the code to figure out the source of EBADF. I noticed Oren applied this patch and the same never assertion was made in a few other places so I've inlined a patch.

fs/pipe.c struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr) · line 990

Quoted 6 lines
fs/pipe.c +3 -0
+			return ERR_PTR(-EBADF);
+
 		ret = ckpt_obj_insert(ctx, file, h->pipe_objref, CKPT_OBJ_FILE);
+		fput(file);
 		if (ret < 0)
 			goto out;

pipe c/r: Use BUG_ON() instead of returning

BUG_ON is appropriate for things which should never happen. These fd lookups should always return non-NULL file pointers because we just successfully created them and no other control flow path exists that could modify the file table of a restarting task.

fs/pipe.c +3 -6
index d79ad44..1237f14 100644
@@ -980,8 +980,7 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)
 
 		/* use the "write" side to restore the pipe's contents */
 		file = fget(fds[1]);
-		if (!file)	/* this should _never_ happen ! */
-			return ERR_PTR(-EBADF);
+		BUG_ON(!file);
 		ret = restore_pipe(ctx, file);
 		fput(file);
 		if (ret < 0)
@@ -995,8 +994,7 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)
 		 * when that side is restored.
 		 */
 		file = fget(fds[1-which]);
-		if (!file)	/* this should _never_ happen ! */
-			return ERR_PTR(-EBADF);
+		BUG_ON(!file);
 
 		ret = ckpt_obj_insert(ctx, file, h->pipe_objref, CKPT_OBJ_FILE);
 		fput(file);
@@ -1004,8 +1002,7 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)
 			goto out;
 
 		file = fget(fds[which]);	/* 'this' side */
-		if (!file)	/* this should _never_ happen ! */
-			return ERR_PTR(-EBADF);
+		BUG_ON(!file);
 
 		/* get rid of the file descriptors (caller sets that) */
 		sys_close(fds[0]);

A message that is not in the archive (sent off-list or not yet received): 20110203034756.GR16432-52DBMbEzqgQ/wnmkkaCWp/[email protected]

#5 lore raw

[...]

Quoted 11 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		file = fget(fds[1-which]);
+		if (!file)	/* this should _never_ happen ! */

If it should never happen then this should be:

		BUG_ON(!file);

that way we see the bug as soon as it's detected rather than having to dig through the stack and 'step' through the code to figure out the source of EBADF. I noticed Oren applied this patch and the same never assertion was made in a few other places so I've inlined a patch.

Ok. Queued for v23-rc2.

Oren.