Skip to contentSkip to conversation

fs/proc/vmcore.c: Put if tests in the top of the while loop to reduce duplication

PATCH In review Zhang Yanfei 3 participants 4 messages Feb 3, 2013 – Feb 8, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/proc/vmcore.c: Put if tests in the top of the while loop to reduce duplication

From: Zhang Yanfei <[email protected]>

In function read_vmcore, two if tests are duplicate. Change the position of them could reduce the duplication. This change does not affect the behaviour of the function.

---
 fs/proc/vmcore.c |   21 ++++++++-------------
 1 files changed, 8 insertions(+), 13 deletions(-)
fs/proc/vmcore.c +8 -13
index 0d5071d..c74a901 100644
@@ -175,15 +175,16 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,
 	start = map_offset_to_paddr(*fpos, &vmcore_list, &curr_m);
 	if (!curr_m)
         	return -EINVAL;
-	if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
-		tsz = buflen;
-
-	/* Calculate left bytes in current memory segment. */
-	nr_bytes = (curr_m->size - (start - curr_m->paddr));
-	if (tsz > nr_bytes)
-		tsz = nr_bytes;
 
 	while (buflen) {
+		if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
+			tsz = buflen;
+
+		/* Calculate left bytes in current memory segment. */
+		nr_bytes = (curr_m->size - (start - curr_m->paddr));
+		if (tsz > nr_bytes)
+			tsz = nr_bytes;
+
 		tmp = read_from_oldmem(buffer, tsz, &start, 1);
 		if (tmp < 0)
 			return tmp;
@@ -198,12 +199,6 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,
 						struct vmcore, list);
 			start = curr_m->paddr;
 		}
-		if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
-			tsz = buflen;
-		/* Calculate left bytes in current memory segment. */
-		nr_bytes = (curr_m->size - (start - curr_m->paddr));
-		if (tsz > nr_bytes)
-			tsz = nr_bytes;
 	}
 	return acc;
 }
Signature · 1 line
1.7.1
maintainer
#1 lore raw
Quoted 6 lines · On Sun, 03 Feb 2013 21:13:36 +0800 Zhang Yanfei <[email protected]>…

From: Zhang Yanfei <[email protected]>

In function read_vmcore, two if tests are duplicate. Change the position of them could reduce the duplication. This change does not affect the behaviour of the function.

hm, yes.

Quoted 27 lines
fs/proc/vmcore.c +8 -7
@@ -175,15 +175,16 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,
 	start = map_offset_to_paddr(*fpos, &vmcore_list, &curr_m);
 	if (!curr_m)
         	return -EINVAL;
-	if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
-		tsz = buflen;
-
-	/* Calculate left bytes in current memory segment. */
-	nr_bytes = (curr_m->size - (start - curr_m->paddr));
-	if (tsz > nr_bytes)
-		tsz = nr_bytes;
 
 	while (buflen) {
+		if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
+			tsz = buflen;
+
+		/* Calculate left bytes in current memory segment. */
+		nr_bytes = (curr_m->size - (start - curr_m->paddr));
+		if (tsz > nr_bytes)
+			tsz = nr_bytes;
+
 		tmp = read_from_oldmem(buffer, tsz, &start, 1);
 		if (tmp < 0)
 			return tmp;

fs/proc/vmcore.c static ssize_t read_vmcore(struct file *file, char __user *buffer, · line 200

Quoted 2 lines
fs/proc/vmcore.c +0 -0
@@ -198,12 +199,6 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,
 						struct vmcore, list);
 			start = curr_m->paddr;

`start' gets changed here

fs/proc/vmcore.c static ssize_t read_vmcore(struct file *file, char __user *buffer, · line 206

Quoted 7 lines
fs/proc/vmcore.c +0 -6
 		}
-		if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
-			tsz = buflen;
-		/* Calculate left bytes in current memory segment. */
-		nr_bytes = (curr_m->size - (start - curr_m->paddr));
-		if (tsz > nr_bytes)
-			tsz = nr_bytes;

So this code can calcualte a different value of `tsz'. But that won't get used. Looks right to me.

fs/proc/vmcore.c static ssize_t read_vmcore(struct file *file, char __user *buffer, · line 204

Quoted 3 lines
fs/proc/vmcore.c +0 -0
 	}
 	return acc;
 }

While we're there, let's squish that assignment-in-a-if pestilence and use max_t().

fs/proc/vmcore.c renamed from fs/proc/vmcore.c~fs-proc-vmcorec-put-if-tests-in-the-top-of-the-while-loop-to-reduce-duplication-fix +1 -2
@@ -178,8 +178,7 @@ static ssize_t read_vmcore(struct file *
         	return -EINVAL;
 
 	while (buflen) {
-		if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
-			tsz = buflen;
+		tsz = max_t(size_t, buflen, PAGE_SIZE - (start & ~PAGE_MASK));
 
 		/* Calculate left bytes in current memory segment. */
 		nr_bytes = (curr_m->size - (start - curr_m->paddr));

_

#2 lore raw
Quoted 11 lines · Andrew Morton
Quoted 5 lines · On Sun, 03 Feb 2013 21:13:36 +0800 Zhang Yanfei <[email protected]>…

From: Zhang Yanfei <[email protected]>

In function read_vmcore, two if tests are duplicate. Change the position of them could reduce the duplication. This change does not affect the behaviour of the function.

While we're there, let's squish that assignment-in-a-if pestilence and use max_t().

min_t please. Or do you mean to change the behavior of the code?

Quoted 13 lines
fs/proc/vmcore.c renamed from fs/proc/vmcore.c~fs-proc-vmcorec-put-if-tests-in-the-top-of-the-while-loop-to-reduce-duplication-fix +1 -2
@@ -178,8 +178,7 @@ static ssize_t read_vmcore(struct file *
         	return -EINVAL;
 
 	while (buflen) {
-		if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
-			tsz = buflen;
+		tsz = max_t(size_t, buflen, PAGE_SIZE - (start & ~PAGE_MASK));
 
 		/* Calculate left bytes in current memory segment. */
 		nr_bytes = (curr_m->size - (start - curr_m->paddr));

_

maintainer
#3 lore raw
Quoted 7 lines · On Fri, 08 Feb 2013 06:25:39 -0800 [email protected] (Eric W. Biederman)…
Quoted 5 lines · Andrew Morton
Quoted 2 lines · On Sun, 03 Feb 2013 21:13:36 +0800 Zhang Yanfei <[email protected]>…

From: Zhang Yanfei <[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

In function read_vmcore, two if tests are duplicate. Change the position of them could reduce the duplication. This change does not affect the behaviour of the function.

While we're there, let's squish that assignment-in-a-if pestilence and use max_t().

min_t please. Or do you mean to change the behavior of the code?

oops.

Quoted 13 lines
Quoted 13 lines
fs/proc/vmcore.c renamed from fs/proc/vmcore.c~fs-proc-vmcorec-put-if-tests-in-the-top-of-the-while-loop-to-reduce-duplication-fix +1 -2
@@ -178,8 +178,7 @@ static ssize_t read_vmcore(struct file *
         	return -EINVAL;
 
 	while (buflen) {
-		if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
-			tsz = buflen;
+		tsz = max_t(size_t, buflen, PAGE_SIZE - (start & ~PAGE_MASK));
 
 		/* Calculate left bytes in current memory segment. */
 		nr_bytes = (curr_m->size - (start - curr_m->paddr));

_