Skip to contentSkip to conversation

kexec: Use min_t to simplify logic

PATCH In review Zhang Yanfei 4 participants 5 messages Feb 24, 2013 – Feb 26, 2013

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

PATCH v2 1/1 kexec: Use min_t to simplify logic

From: Zhang Yanfei <[email protected]>

This is just a tweak: using min_t to simplify logic of variable assignments.

v2:
- Rewrite patch description as Simon suggested.
- Fix an inappropriate if test introduced by v1. Thanks Simon.

Cc "Eric W. Biederman" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Simon Horman <[email protected]>
---
 kernel/kexec.c |   22 ++++++----------------
 1 files changed, 6 insertions(+), 16 deletions(-)
kernel/kexec.c +6 -16
index 2436ffc..effd655 100644
@@ -822,13 +822,8 @@ static int kimage_load_normal_segment(struct kimage *image,
 		/* Start with a clear page */
 		clear_page(ptr);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
-		uchunk = mchunk;
-		if (uchunk > ubytes)
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);
 
 		result = copy_from_user(ptr, buf, uchunk);
 		kunmap(page);
@@ -874,13 +869,9 @@ static int kimage_load_crash_segment(struct kimage *image,
 		}
 		ptr = kmap(page);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
-		uchunk = mchunk;
-		if (uchunk > ubytes) {
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);
+		if (mchunk > uchunk) {
 			/* Zero the trailing part of the page */
 			memset(ptr + uchunk, 0, mchunk - uchunk);
 		}
@@ -1461,8 +1452,7 @@ void vmcoreinfo_append_str(const char *fmt, ...)
 	r = vsnprintf(buf, sizeof(buf), fmt, args);
 	va_end(args);
 
-	if (r + vmcoreinfo_size > vmcoreinfo_max_size)
-		r = vmcoreinfo_max_size - vmcoreinfo_size;
+	r = min_t(size_t, r, vmcoreinfo_max_size - vmcoreinfo_size);
 
 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
 
Signature · 1 line
1.7.1
#1 lore raw
Quoted 8 lines · Zhang Yanfei

From: Zhang Yanfei <[email protected]>

This is just a tweak: using min_t to simplify logic of variable assignments.

v2:
- Rewrite patch description as Simon suggested.
- Fix an inappropriate if test introduced by v1. Thanks Simon.

Hi Zhang,

thanks for the update.

Quoted 63 lines
Cc "Eric W. Biederman" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Simon Horman <[email protected]>
---
 kernel/kexec.c |   22 ++++++----------------
 1 files changed, 6 insertions(+), 16 deletions(-)
kernel/kexec.c +6 -16
index 2436ffc..effd655 100644
@@ -822,13 +822,8 @@ static int kimage_load_normal_segment(struct kimage *image,
 		/* Start with a clear page */
 		clear_page(ptr);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
-		uchunk = mchunk;
-		if (uchunk > ubytes)
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);
 
 		result = copy_from_user(ptr, buf, uchunk);
 		kunmap(page);
@@ -874,13 +869,9 @@ static int kimage_load_crash_segment(struct kimage *image,
 		}
 		ptr = kmap(page);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
-		uchunk = mchunk;
-		if (uchunk > ubytes) {
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);
+		if (mchunk > uchunk) {
 			/* Zero the trailing part of the page */
 			memset(ptr + uchunk, 0, mchunk - uchunk);
 		}
@@ -1461,8 +1452,7 @@ void vmcoreinfo_append_str(const char *fmt, ...)
 	r = vsnprintf(buf, sizeof(buf), fmt, args);
 	va_end(args);
 
-	if (r + vmcoreinfo_size > vmcoreinfo_max_size)
-		r = vmcoreinfo_max_size - vmcoreinfo_size;
+	r = min_t(size_t, r, vmcoreinfo_max_size - vmcoreinfo_size);
 
 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
 
Signature · 6 lines
1.7.1

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#2 lore raw
Quoted 10 lines · On Mon, 25 Feb 2013 09:36:51 +0900 Simon Horman <[email protected]> wrote:
Quoted 8 lines · Zhang Yanfei

From: Zhang Yanfei <[email protected]>

This is just a tweak: using min_t to simplify logic of variable assignments.

v2:
- Rewrite patch description as Simon suggested.
- Fix an inappropriate if test introduced by v1. Thanks Simon.

Quoted 5 lines

Hi Zhang,

thanks for the update.

Signed-off-by: implies that you were involved in the development or patch delivery. Were you? If not, an Acked-by or Reviewed-by is more appropriate.

Also, the need to use min_t rather than min is a sign that the types are screwed up. Let's take a look.

Quoted 10 lines
Quoted 10 lines
kernel/kexec.c +0 -1
index 2436ffc..effd655 100644
@@ -822,13 +822,8 @@ static int kimage_load_normal_segment(struct kimage *image,
 		/* Start with a clear page */
 		clear_page(ptr);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
Quoted 8 lines

kernel/kexec.c static int kimage_load_normal_segment(struct kimage *image, · line 826

Quoted 8 lines
kernel/kexec.c +2 -6
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
-		uchunk = mchunk;
-		if (uchunk > ubytes)
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);

The types of ubytes and mbytes are clearly wrong. They are initialised from a size_t and they are manipulated alongside size_t's.

The types of PAGE_SIZE and PAGE_MASK are vague - iirc they once had different types on different architectures, so some form of casting is unavoidable here.

Quoted 11 lines
Quoted 11 lines
kernel/kexec.c +0 -5
 		result = copy_from_user(ptr, buf, uchunk);
 		kunmap(page);
@@ -874,13 +869,9 @@ static int kimage_load_crash_segment(struct kimage *image,
 		}
 		ptr = kmap(page);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
-		uchunk = mchunk;
Quoted 8 lines

kernel/kexec.c static int kimage_load_crash_segment(struct kimage *image, · line 877

Quoted 8 lines
kernel/kexec.c +3 -2
-		if (uchunk > ubytes) {
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);
+		if (mchunk > uchunk) {
 			/* Zero the trailing part of the page */
 			memset(ptr + uchunk, 0, mchunk - uchunk);
 		}

Again, mybtes and ubytes have the wrong type.

Quoted 9 lines

kernel/kexec.c void vmcoreinfo_append_str(const char *fmt, ...) · line 1457

Quoted 9 lines
kernel/kexec.c +1 -2
@@ -1461,8 +1452,7 @@ void vmcoreinfo_append_str(const char *fmt, ...)
 	r = vsnprintf(buf, sizeof(buf), fmt, args);
 	va_end(args);
 
-	if (r + vmcoreinfo_size > vmcoreinfo_max_size)
-		r = vmcoreinfo_max_size - vmcoreinfo_size;
+	r = min_t(size_t, r, vmcoreinfo_max_size - vmcoreinfo_size);
 
 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);

vmcoreinfo_max_size is a size_t and vmcoreinfo_size is a size_t and memcpy's size' argument is a size_t. Therefore the type of r' should be... what? int! bzzzzt.

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

Reviewed-by
#3 lore raw
Quoted 15 lines · Andrew Morton
Quoted 12 lines · On Mon, 25 Feb 2013 09:36:51 +0900 Simon Horman <[email protected]> wrote:
Quoted 8 lines · Zhang Yanfei

From: Zhang Yanfei <[email protected]>

This is just a tweak: using min_t to simplify logic of variable assignments.

v2:
- Rewrite patch description as Simon suggested.
- Fix an inappropriate if test introduced by v1. Thanks Simon.

Hi Zhang,

Quoted 7 lines
Quoted 3 lines

thanks for the update.

Signed-off-by: implies that you were involved in the development or patch delivery. Were you? If not, an Acked-by or Reviewed-by is more appropriate.

My bad, I meant:

Quoted 65 lines

Also, the need to use min_t rather than min is a sign that the types are screwed up. Let's take a look.

Quoted 10 lines
Quoted 10 lines
kernel/kexec.c +0 -1
index 2436ffc..effd655 100644
@@ -822,13 +822,8 @@ static int kimage_load_normal_segment(struct kimage *image,
 		/* Start with a clear page */
 		clear_page(ptr);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
Quoted 8 lines

kernel/kexec.c static int kimage_load_normal_segment(struct kimage *image, · line 826

Quoted 8 lines
kernel/kexec.c +2 -6
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
-		uchunk = mchunk;
-		if (uchunk > ubytes)
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);

The types of ubytes and mbytes are clearly wrong. They are initialised from a size_t and they are manipulated alongside size_t's.

The types of PAGE_SIZE and PAGE_MASK are vague - iirc they once had different types on different architectures, so some form of casting is unavoidable here.

Quoted 11 lines
Quoted 11 lines
kernel/kexec.c +0 -5
 		result = copy_from_user(ptr, buf, uchunk);
 		kunmap(page);
@@ -874,13 +869,9 @@ static int kimage_load_crash_segment(struct kimage *image,
 		}
 		ptr = kmap(page);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
-		uchunk = mchunk;
Quoted 8 lines

kernel/kexec.c static int kimage_load_crash_segment(struct kimage *image, · line 877

Quoted 8 lines
kernel/kexec.c +3 -2
-		if (uchunk > ubytes) {
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);
+		if (mchunk > uchunk) {
 			/* Zero the trailing part of the page */
 			memset(ptr + uchunk, 0, mchunk - uchunk);
 		}

Again, mybtes and ubytes have the wrong type.

Quoted 9 lines

kernel/kexec.c void vmcoreinfo_append_str(const char *fmt, ...) · line 1457

Quoted 9 lines
kernel/kexec.c +1 -2
@@ -1461,8 +1452,7 @@ void vmcoreinfo_append_str(const char *fmt, ...)
 	r = vsnprintf(buf, sizeof(buf), fmt, args);
 	va_end(args);
 
-	if (r + vmcoreinfo_size > vmcoreinfo_max_size)
-		r = vmcoreinfo_max_size - vmcoreinfo_size;
+	r = min_t(size_t, r, vmcoreinfo_max_size - vmcoreinfo_size);
 
 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);

vmcoreinfo_max_size is a size_t and vmcoreinfo_size is a size_t and memcpy's size' argument is a size_t. Therefore the type of r' should be... what? int! bzzzzt.

#4 lore raw
In reply to Andrew Morton
Quoted 39 lines · Andrew Morton
Quoted 10 lines · On Mon, 25 Feb 2013 09:36:51 +0900 Simon Horman <[email protected]> wrote:
Quoted 8 lines · Zhang Yanfei

From: Zhang Yanfei <[email protected]>

This is just a tweak: using min_t to simplify logic of variable assignments.

v2:
- Rewrite patch description as Simon suggested.
- Fix an inappropriate if test introduced by v1. Thanks Simon.

Quoted 5 lines

Hi Zhang,

thanks for the update.

Signed-off-by: implies that you were involved in the development or patch delivery. Were you? If not, an Acked-by or Reviewed-by is more appropriate.

Also, the need to use min_t rather than min is a sign that the types are screwed up. Let's take a look.

Quoted 13 lines
Quoted 13 lines
kernel/kexec.c +0 -4
index 2436ffc..effd655 100644
@@ -822,13 +822,8 @@ static int kimage_load_normal_segment(struct kimage *image,
 		/* Start with a clear page */
 		clear_page(ptr);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
Quoted 8 lines
Quoted 5 lines

kernel/kexec.c static int kimage_load_normal_segment(struct kimage *image, · line 826

Quoted 5 lines
kernel/kexec.c +2 -3
-		uchunk = mchunk;
-		if (uchunk > ubytes)
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);

The types of ubytes and mbytes are clearly wrong. They are initialised from a size_t and they are manipulated alongside size_t's.

Ah, yes. I didn't realize this before.

Quoted 4 lines

The types of PAGE_SIZE and PAGE_MASK are vague - iirc they once had different types on different architectures, so some form of casting is unavoidable here.

Yes.

Quoted 31 lines
Quoted 12 lines
Quoted 12 lines
kernel/kexec.c +0 -5
 		result = copy_from_user(ptr, buf, uchunk);
 		kunmap(page);
@@ -874,13 +869,9 @@ static int kimage_load_crash_segment(struct kimage *image,
 		}
 		ptr = kmap(page);
 		ptr += maddr & ~PAGE_MASK;
-		mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
-		if (mchunk > mbytes)
-			mchunk = mbytes;
-
-		uchunk = mchunk;
Quoted 8 lines

kernel/kexec.c static int kimage_load_crash_segment(struct kimage *image, · line 877

Quoted 8 lines
kernel/kexec.c +3 -2
-		if (uchunk > ubytes) {
-			uchunk = ubytes;
+		mchunk = min_t(size_t, mbytes, PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min_t(size_t, ubytes, mchunk);
+		if (mchunk > uchunk) {
 			/* Zero the trailing part of the page */
 			memset(ptr + uchunk, 0, mchunk - uchunk);
 		}

Again, mybtes and ubytes have the wrong type.

Quoted 8 lines
Quoted 8 lines
kernel/kexec.c +1 -2
@@ -1461,8 +1452,7 @@ void vmcoreinfo_append_str(const char *fmt, ...)
 	r = vsnprintf(buf, sizeof(buf), fmt, args);
 	va_end(args);
 
-	if (r + vmcoreinfo_size > vmcoreinfo_max_size)
-		r = vmcoreinfo_max_size - vmcoreinfo_size;
+	r = min_t(size_t, r, vmcoreinfo_max_size - vmcoreinfo_size);
 
Quoted 5 lines
Quoted 1 line

kernel/kexec.c void vmcoreinfo_append_str(const char *fmt, ...) · line 1457

Quoted 1 line
kernel/kexec.c +0 -0
 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);

vmcoreinfo_max_size is a size_t and vmcoreinfo_size is a size_t and memcpy's size' argument is a size_t. Therefore the type of r' should be... what? int! bzzzzt.

yes.

So I will split the patch into two, first fix the wrong types here. Then use min_t to simply the logic.

Thanks
Zhang

Quoted 7 lines

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec