Skip to contentSkip to conversation

Clean up expand_fdtable() and expand_files().

PATCH In review Vadim Lobanov 2 participants 4 messages Sep 5, 2006 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Clean up expand_fdtable() and expand_files().

Hi,

This patch performs a code cleanup against the expand_fdtable() and expand_files() functions inside fs/file.c. It aims to make the flow of code within these functions simpler and easier to understand, via added comments and modest refactoring. The patch was generated against 2.6.18-rc5-mm1, and was successfully run live. Please apply.

(I'm trying out KMail for this patch. I tested this mailer beforehand to make sure the patch will come out unmangled, but, as with all things software, success is far from guaranteed. :) Please yell if the patch is borked.)

fs/file.c +25 -36
diff -Npru linux-a/fs/file.c linux-b/fs/file.c
@@ -296,37 +296,30 @@ static int expand_fdtable(struct files_s
 	__releases(files->file_lock)
 	__acquires(files->file_lock)
 {
-	int error = 0;
-	struct fdtable *fdt;
-	struct fdtable *nfdt = NULL;
+	struct fdtable *new_fdt, *cur_fdt;
 
 	spin_unlock(&files->file_lock);
-	nfdt = alloc_fdtable(nr);
-	if (!nfdt) {
-		error = -ENOMEM;
-		spin_lock(&files->file_lock);
-		goto out;
-	}
-
+	new_fdt = alloc_fdtable(nr);
 	spin_lock(&files->file_lock);
-	fdt = files_fdtable(files);
+	if (!new_fdt)
+		return -ENOMEM;
 	/*
-	 * Check again since another task may have expanded the
-	 * fd table while we dropped the lock
+	 * Check again since another task may have expanded the fd table while
+	 * we dropped the lock
 	 */
-	if (nr >= fdt->max_fds || nr >= fdt->max_fdset) {
-		copy_fdtable(nfdt, fdt);
+	cur_fdt = files_fdtable(files);
+	if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) {
+		/* Continue as planned */
+		copy_fdtable(new_fdt, cur_fdt);
+		rcu_assign_pointer(files->fdt, new_fdt);
+		free_fdtable(cur_fdt);
 	} else {
-		/* Somebody expanded while we dropped file_lock */
+		/* Somebody else expanded, so undo our attempt */
 		spin_unlock(&files->file_lock);
-		__free_fdtable(nfdt);
+		__free_fdtable(new_fdt);
 		spin_lock(&files->file_lock);
-		goto out;
 	}
-	rcu_assign_pointer(files->fdt, nfdt);
-	free_fdtable(fdt);
-out:
-	return error;
+	return 1;
 }
 
 /*
@@ -336,23 +329,19 @@ out:
  */
 int expand_files(struct files_struct *files, int nr)
 {
-	int err, expand = 0;
 	struct fdtable *fdt;
 
 	fdt = files_fdtable(files);
-	if (nr >= fdt->max_fdset || nr >= fdt->max_fds) {
-		if (fdt->max_fdset >= NR_OPEN ||
-			fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) {
-			err = -EMFILE;
-			goto out;
-		}
-		expand = 1;
-		if ((err = expand_fdtable(files, nr)))
-			goto out;
-	}
-	err = expand;
-out:
-	return err;
+	/* Do we need to expand? */
+	if (nr < fdt->max_fdset && nr < fdt->max_fds)
+		return 0;
+	/* Can we expand? */
+	if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN ||
+	    nr >= NR_OPEN)
+		return -EMFILE;
+
+	/* All good, so we try */
+	return expand_fdtable(files, nr);
 }
 
 static void __devinit fdtable_defer_list_init(int cpu)
#1 lore raw
Quoted 11 lines · On Mon, 4 Sep 2006 22:08:36 -0700 Vadim Lobanov wrote:

Hi,

This patch performs a code cleanup against the expand_fdtable() and expand_files() functions inside fs/file.c. It aims to make the flow of code within these functions simpler and easier to understand, via added comments and modest refactoring. The patch was generated against 2.6.18-rc5-mm1, and was successfully run live. Please apply.

(I'm trying out KMail for this patch. I tested this mailer beforehand to make sure the patch will come out unmangled, but, as with all things software, success is far from guaranteed. :) Please yell if the patch is borked.)

It's not (mechanically) b0rked.

Quoted 45 lines

diff -Npru linux-a/fs/file.c linux-b/fs/file.c

fs/file.c +13 -16
@@ -296,37 +296,30 @@ static int expand_fdtable(struct files_s
 	__releases(files->file_lock)
 	__acquires(files->file_lock)
 {
-	int error = 0;
-	struct fdtable *fdt;
-	struct fdtable *nfdt = NULL;
+	struct fdtable *new_fdt, *cur_fdt;
 
 	spin_unlock(&files->file_lock);
-	nfdt = alloc_fdtable(nr);
-	if (!nfdt) {
-		error = -ENOMEM;
-		spin_lock(&files->file_lock);
-		goto out;
-	}
-
+	new_fdt = alloc_fdtable(nr);
 	spin_lock(&files->file_lock);
-	fdt = files_fdtable(files);
+	if (!new_fdt)
+		return -ENOMEM;
 	/*
-	 * Check again since another task may have expanded the
-	 * fd table while we dropped the lock
+	 * Check again since another task may have expanded the fd table while
+	 * we dropped the lock
 	 */
-	if (nr >= fdt->max_fds || nr >= fdt->max_fdset) {
-		copy_fdtable(nfdt, fdt);
+	cur_fdt = files_fdtable(files);
+	if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) {
+		/* Continue as planned */
+		copy_fdtable(new_fdt, cur_fdt);
+		rcu_assign_pointer(files->fdt, new_fdt);
+		free_fdtable(cur_fdt);
 	} else {
-		/* Somebody expanded while we dropped file_lock */
+		/* Somebody else expanded, so undo our attempt */
 		spin_unlock(&files->file_lock);

fs/file.c static int expand_fdtable(struct files_s · line 322

Quoted 10 lines
fs/file.c +2 -6
-		__free_fdtable(nfdt);
+		__free_fdtable(new_fdt);
 		spin_lock(&files->file_lock);
-		goto out;
 	}
-	rcu_assign_pointer(files->fdt, nfdt);
-	free_fdtable(fdt);
-out:
-	return error;
+	return 1;

This function didn't previously return a value of 1. If it can do so now, please document it in the function comments "header". Using kernel-doc would be good too.

Quoted 27 lines
fs/file.c +3 -14
 }
 
 /*
@@ -336,23 +329,19 @@ out:
  */
 int expand_files(struct files_struct *files, int nr)
 {
-	int err, expand = 0;
 	struct fdtable *fdt;
 
 	fdt = files_fdtable(files);
-	if (nr >= fdt->max_fdset || nr >= fdt->max_fds) {
-		if (fdt->max_fdset >= NR_OPEN ||
-			fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) {
-			err = -EMFILE;
-			goto out;
-		}
-		expand = 1;
-		if ((err = expand_fdtable(files, nr)))
-			goto out;
-	}
-	err = expand;
-out:
-	return err;
+	/* Do we need to expand? */
+	if (nr < fdt->max_fdset && nr < fdt->max_fds)
+		return 0;

fs/file.c out: · line 347

Quoted 10 lines
fs/file.c +7 -0
+	/* Can we expand? */
+	if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN ||
+	    nr >= NR_OPEN)
+		return -EMFILE;
+
+	/* All good, so we try */
+	return expand_fdtable(files, nr);
 }
 
 static void __devinit fdtable_defer_list_init(int cpu)

---
~Randy

#2 lore raw
Quoted 67 lines · Randy.Dunlap
Quoted 8 lines · On Mon, 4 Sep 2006 22:08:36 -0700 Vadim Lobanov wrote:

Hi,

This patch performs a code cleanup against the expand_fdtable() and expand_files() functions inside fs/file.c. It aims to make the flow of code within these functions simpler and easier to understand, via added comments and modest refactoring. The patch was generated against 2.6.18-rc5-mm1, and was successfully run live. Please apply.

Quoted 4 lines

(I'm trying out KMail for this patch. I tested this mailer beforehand to make sure the patch will come out unmangled, but, as with all things software, success is far from guaranteed. :) Please yell if the patch is borked.)

It's not (mechanically) b0rked.

Quoted 51 lines

diff -Npru linux-a/fs/file.c linux-b/fs/file.c

fs/file.c +14 -19
@@ -296,37 +296,30 @@ static int expand_fdtable(struct files_s
 	__releases(files->file_lock)
 	__acquires(files->file_lock)
 {
-	int error = 0;
-	struct fdtable *fdt;
-	struct fdtable *nfdt = NULL;
+	struct fdtable *new_fdt, *cur_fdt;
 
 	spin_unlock(&files->file_lock);
-	nfdt = alloc_fdtable(nr);
-	if (!nfdt) {
-		error = -ENOMEM;
-		spin_lock(&files->file_lock);
-		goto out;
-	}
-
+	new_fdt = alloc_fdtable(nr);
 	spin_lock(&files->file_lock);
-	fdt = files_fdtable(files);
+	if (!new_fdt)
+		return -ENOMEM;
 	/*
-	 * Check again since another task may have expanded the
-	 * fd table while we dropped the lock
+	 * Check again since another task may have expanded the fd table while
+	 * we dropped the lock
 	 */
-	if (nr >= fdt->max_fds || nr >= fdt->max_fdset) {
-		copy_fdtable(nfdt, fdt);
+	cur_fdt = files_fdtable(files);
+	if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) {
+		/* Continue as planned */
+		copy_fdtable(new_fdt, cur_fdt);
+		rcu_assign_pointer(files->fdt, new_fdt);
+		free_fdtable(cur_fdt);
 	} else {
-		/* Somebody expanded while we dropped file_lock */
+		/* Somebody else expanded, so undo our attempt */
 		spin_unlock(&files->file_lock);
-		__free_fdtable(nfdt);
+		__free_fdtable(new_fdt);
 		spin_lock(&files->file_lock);
-		goto out;
 	}
-	rcu_assign_pointer(files->fdt, nfdt);
Quoted 8 lines

fs/file.c static int expand_fdtable(struct files_s · line 322

Quoted 4 lines
fs/file.c +1 -3
-	free_fdtable(fdt);
-out:
-	return error;
+	return 1;

This function didn't previously return a value of 1. If it can do so now, please document it in the function comments "header". Using kernel-doc would be good too.

More comments on the function headers. Gotcha. Will resend.

The problem with kernel-doc in this particular instance is that none of the other functions in that file have comments in that particular style; they all currently use the mostly-unstructured C comments. If anything, it'd be far simpler and cleaner to get this particular patch merged first, and then add kernel-doc comments to _all_ the functions in this file at once in a later patch.

Quoted 3 lines

---
~Randy

-- Vadim Lobanov

#3 lore raw
Quoted 77 lines · On Tue, 5 Sep 2006 11:56:49 -0700 Vadim Lobanov wrote:
Quoted 67 lines · Randy.Dunlap
Quoted 8 lines · On Mon, 4 Sep 2006 22:08:36 -0700 Vadim Lobanov wrote:

Hi,

This patch performs a code cleanup against the expand_fdtable() and expand_files() functions inside fs/file.c. It aims to make the flow of code within these functions simpler and easier to understand, via added comments and modest refactoring. The patch was generated against 2.6.18-rc5-mm1, and was successfully run live. Please apply.

Quoted 4 lines

(I'm trying out KMail for this patch. I tested this mailer beforehand to make sure the patch will come out unmangled, but, as with all things software, success is far from guaranteed. :) Please yell if the patch is borked.)

It's not (mechanically) b0rked.

Quoted 51 lines

diff -Npru linux-a/fs/file.c linux-b/fs/file.c

fs/file.c +14 -19
@@ -296,37 +296,30 @@ static int expand_fdtable(struct files_s
 	__releases(files->file_lock)
 	__acquires(files->file_lock)
 {
-	int error = 0;
-	struct fdtable *fdt;
-	struct fdtable *nfdt = NULL;
+	struct fdtable *new_fdt, *cur_fdt;
 
 	spin_unlock(&files->file_lock);
-	nfdt = alloc_fdtable(nr);
-	if (!nfdt) {
-		error = -ENOMEM;
-		spin_lock(&files->file_lock);
-		goto out;
-	}
-
+	new_fdt = alloc_fdtable(nr);
 	spin_lock(&files->file_lock);
-	fdt = files_fdtable(files);
+	if (!new_fdt)
+		return -ENOMEM;
 	/*
-	 * Check again since another task may have expanded the
-	 * fd table while we dropped the lock
+	 * Check again since another task may have expanded the fd table while
+	 * we dropped the lock
 	 */
-	if (nr >= fdt->max_fds || nr >= fdt->max_fdset) {
-		copy_fdtable(nfdt, fdt);
+	cur_fdt = files_fdtable(files);
+	if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) {
+		/* Continue as planned */
+		copy_fdtable(new_fdt, cur_fdt);
+		rcu_assign_pointer(files->fdt, new_fdt);
+		free_fdtable(cur_fdt);
 	} else {
-		/* Somebody expanded while we dropped file_lock */
+		/* Somebody else expanded, so undo our attempt */
 		spin_unlock(&files->file_lock);
-		__free_fdtable(nfdt);
+		__free_fdtable(new_fdt);
 		spin_lock(&files->file_lock);
-		goto out;
 	}
-	rcu_assign_pointer(files->fdt, nfdt);
Quoted 8 lines

fs/file.c static int expand_fdtable(struct files_s · line 322

Quoted 4 lines
fs/file.c +1 -3
-	free_fdtable(fdt);
-out:
-	return error;
+	return 1;

This function didn't previously return a value of 1. If it can do so now, please document it in the function comments "header". Using kernel-doc would be good too.

Quoted 8 lines

More comments on the function headers. Gotcha. Will resend.

The problem with kernel-doc in this particular instance is that none of the other functions in that file have comments in that particular style; they all currently use the mostly-unstructured C comments. If anything, it'd be far simpler and cleaner to get this particular patch merged first, and then add kernel-doc comments to _all_ the functions in this file at once in a later patch.

Sure. The kernel-doc comment was certainly secondary.

---
~Randy