Skip to contentSkip to conversation

kexec: fix wrong types of some local variables

PATCH In review Zhang Yanfei 4 participants 8 messages Feb 26, 2013 – Mar 1, 2013

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

PATCH v3 1/2 kexec: fix wrong types of some local variables

The types of the following local variables:
- ubytes/mbytes in kimage_load_crash_segment()/kimage_load_normal_segment()
- r in vmcoreinfo_append_str()
are wrong, so fix them.

Cc "Eric W. Biederman" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Simon Horman <[email protected]>
---
 kernel/kexec.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
kernel/kexec.c +3 -3
index 2436ffc..3cbfcc7 100644
@@ -789,7 +789,7 @@ static int kimage_load_normal_segment(struct kimage *image,
 					 struct kexec_segment *segment)
 {
 	unsigned long maddr;
-	unsigned long ubytes, mbytes;
+	size_t ubytes, mbytes;
 	int result;
 	unsigned char __user *buf;
 
@@ -853,7 +853,7 @@ static int kimage_load_crash_segment(struct kimage *image,
 	 * We do things a page at a time for the sake of kmap.
 	 */
 	unsigned long maddr;
-	unsigned long ubytes, mbytes;
+	size_t ubytes, mbytes;
 	int result;
 	unsigned char __user *buf;
 
@@ -1455,7 +1455,7 @@ void vmcoreinfo_append_str(const char *fmt, ...)
 {
 	va_list args;
 	char buf[0x50];
-	int r;
+	size_t r;
 
 	va_start(args, fmt);
 	r = vsnprintf(buf, sizeof(buf), fmt, args);
Signature · 6 lines
1.7.1

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

PATCH v3 2/2 kexec: Use min_t to simplify logic

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

v3:
- cast type of (PAGE_SIZE - (maddr & ~PAGE_MASK)) into size_t.

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]>
---
 kernel/kexec.c |   24 ++++++++----------------
 1 files changed, 8 insertions(+), 16 deletions(-)
kernel/kexec.c +8 -16
index 3cbfcc7..b6b16f9 100644
@@ -822,13 +822,9 @@ 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,
+			       (size_t)(PAGE_SIZE - (maddr & ~PAGE_MASK)));
+		uchunk = min_t(size_t, ubytes, mchunk);
 
 		result = copy_from_user(ptr, buf, uchunk);
 		kunmap(page);
@@ -874,13 +870,10 @@ 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,
+			       (size_t)(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 +1454,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
#2 lore raw

Re: [PATCH v3 2/2] kexec: Use min_t to simplify logic

Quoted 5 lines · Zhang Yanfei

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

v3:
- cast type of (PAGE_SIZE - (maddr & ~PAGE_MASK)) into size_t.

Why? Isn't this just a redundant cast?

kernel/kexec.c

Quoted 1 line
kernel/kexec.c +0 -0

[]

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

Quoted 1 line
kernel/kexec.c +0 -0
@@ -822,13 +822,9 @@ static int kimage_load_normal_segment(struct kimage *image,

[]

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

Quoted 2 lines
kernel/kexec.c +2 -0
+		mchunk = min_t(size_t, mbytes,
+			       (size_t)(PAGE_SIZE - (maddr & ~PAGE_MASK)));
#define min_t(type, x, y) ({			\
	type __min1 = (x);			\
	type __min2 = (y);			\
	__min1 < __min2 ? __min1: __min2; })
#3 lore raw

Re: [PATCH v3 2/2] kexec: Use min_t to simplify logic

Quoted 13 lines · Joe Perches
Quoted 5 lines · Zhang Yanfei

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

v3:
- cast type of (PAGE_SIZE - (maddr & ~PAGE_MASK)) into size_t.

Why? Isn't this just a redundant cast?

kernel/kexec.c

Quoted 1 line
kernel/kexec.c +0 -0

[]

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

Quoted 1 line
kernel/kexec.c +0 -0
@@ -822,13 +822,9 @@ static int kimage_load_normal_segment(struct kimage *image,

[]

Quoted 10 lines

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

Quoted 2 lines
kernel/kexec.c +2 -0
+		mchunk = min_t(size_t, mbytes,
+			       (size_t)(PAGE_SIZE - (maddr & ~PAGE_MASK)));
#define min_t(type, x, y) ({			\
	type __min1 = (x);			\
	type __min2 = (y);			\
	__min1 < __min2 ? __min1: __min2; })

Hmm, from the definition, the cast is redundant.

Maybe I misunderstood what Andrew meant in the mail related to v2:

"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."

Andrew, could you please explain the casting you meant above?

Thanks
Zhang

maintainer
#4 lore raw

Re: [PATCH v3 2/2] kexec: Use min_t to simplify logic

Quoted 17 lines · On Tue, 26 Feb 2013 16:49:02 +0800 Zhang Yanfei <[email protected]>…
Quoted 4 lines

kernel/kexec.c

Quoted 1 line
kernel/kexec.c +0 -0

[]

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

Quoted 1 line
kernel/kexec.c +0 -0
@@ -822,13 +822,9 @@ static int kimage_load_normal_segment(struct kimage *image,

[]

Quoted 10 lines

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

Quoted 2 lines
kernel/kexec.c +2 -0
+		mchunk = min_t(size_t, mbytes,
+			       (size_t)(PAGE_SIZE - (maddr & ~PAGE_MASK)));
#define min_t(type, x, y) ({			\
	type __min1 = (x);			\
	type __min2 = (y);			\
	__min1 < __min2 ? __min1: __min2; })

Hmm, from the definition, the cast is redundant.

Quoted 7 lines

Maybe I misunderstood what Andrew meant in the mail related to v2:

"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."

Andrew, could you please explain the casting you meant above?

I mean that a cast (or min_t, which is a cast) will be needed. The code you have here casts the same thing two times, which isn't necessary.

#5 lore raw

Re: [PATCH v3 2/2] kexec: Use min_t to simplify logic

Quoted 25 lines · Andrew Morton
Quoted 23 lines · On Tue, 26 Feb 2013 16:49:02 +0800 Zhang Yanfei <[email protected]>…
Quoted 4 lines

kernel/kexec.c

Quoted 1 line
kernel/kexec.c +0 -0

[]

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

Quoted 1 line
kernel/kexec.c +0 -0
@@ -822,13 +822,9 @@ static int kimage_load_normal_segment(struct kimage *image,

[]

Quoted 10 lines

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

Quoted 2 lines
kernel/kexec.c +2 -0
+		mchunk = min_t(size_t, mbytes,
+			       (size_t)(PAGE_SIZE - (maddr & ~PAGE_MASK)));
#define min_t(type, x, y) ({			\
	type __min1 = (x);			\
	type __min2 = (y);			\
	__min1 < __min2 ? __min1: __min2; })

Hmm, from the definition, the cast is redundant.

Maybe I misunderstood what Andrew meant in the mail related to v2:

"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 7 lines
Quoted 1 line

Andrew, could you please explain the casting you meant above?

I mean that a cast (or min_t, which is a cast) will be needed. The code you have here casts the same thing two times, which isn't necessary.

Thanks.

Should I resend the patch again? After removing this redundant cast, it is the same with v2.

Zhang

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

maintainer
#6 lore raw

Re: [PATCH v3 2/2] kexec: Use min_t to simplify logic

Quoted 34 lines · On Tue, 26 Feb 2013 16:55:57 +0800 Zhang Yanfei <[email protected]>…

___ 2013___02___26___ 16:53, Andrew Morton ______:

Quoted 25 lines · On Tue, 26 Feb 2013 16:49:02 +0800 Zhang Yanfei <[email protected]>…
Quoted 24 lines
Quoted 5 lines

kernel/kexec.c

Quoted 2 lines
kernel/kexec.c +0 -0

[]

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

Quoted 1 line
kernel/kexec.c +0 -0
@@ -822,13 +822,9 @@ static int kimage_load_normal_segment(struct kimage *image,

[]

Quoted 10 lines

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

Quoted 2 lines
kernel/kexec.c +2 -0
+		mchunk = min_t(size_t, mbytes,
+			       (size_t)(PAGE_SIZE - (maddr & ~PAGE_MASK)));
#define min_t(type, x, y) ({			\
	type __min1 = (x);			\
	type __min2 = (y);			\
	__min1 < __min2 ? __min1: __min2; })

Hmm, from the definition, the cast is redundant.

Maybe I misunderstood what Andrew meant in the mail related to v2:

"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 7 lines
Quoted 1 line

Andrew, could you please explain the casting you meant above?

I mean that a cast (or min_t, which is a cast) will be needed. The code you have here casts the same thing two times, which isn't necessary.

Quoted 4 lines

Thanks.

Should I resend the patch again? After removing this redundant cast, it is the same with v2.

That patch was pretty busted. This should fix everything up:

kernel/kexec.c renamed from kernel/kexec.c~kexec-use-min_t-to-simplify-logic-fix +5 -5
@@ -820,8 +820,8 @@ static int kimage_load_normal_segment(st
 		clear_page(ptr);
 		ptr += maddr & ~PAGE_MASK;
 		mchunk = min_t(size_t, mbytes,
-			       (size_t)(PAGE_SIZE - (maddr & ~PAGE_MASK)));
-		uchunk = min_t(size_t, ubytes, mchunk);
+				PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min(ubytes, mchunk);
 
 		result = copy_from_user(ptr, buf, uchunk);
 		kunmap(page);
@@ -868,8 +868,8 @@ static int kimage_load_crash_segment(str
 		ptr = kmap(page);
 		ptr += maddr & ~PAGE_MASK;
 		mchunk = min_t(size_t, mbytes,
-			       (size_t)(PAGE_SIZE - (maddr & ~PAGE_MASK)));
-		uchunk = min_t(size_t, ubytes, mchunk);
+				PAGE_SIZE - (maddr & ~PAGE_MASK));
+		uchunk = min(ubytes, mchunk);
 		if (mchunk > uchunk) {
 			/* Zero the trailing part of the page */
 			memset(ptr + uchunk, 0, mchunk - uchunk);
@@ -1451,7 +1451,7 @@ void vmcoreinfo_append_str(const char *f
 	r = vsnprintf(buf, sizeof(buf), fmt, args);
 	va_end(args);
 
-	r = min_t(size_t, r, vmcoreinfo_max_size - vmcoreinfo_size);
+	r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
 
 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
 

_

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

Reviewed-by on v3 1/2
#7 lore raw
In reply to Zhang Yanfei
Quoted 9 lines · Zhang Yanfei

The types of the following local variables:
- ubytes/mbytes in kimage_load_crash_segment()/kimage_load_normal_segment()
- r in vmcoreinfo_append_str()
are wrong, so fix them.

Cc "Eric W. Biederman" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Simon Horman <[email protected]>

No complaints here.

Quoted 38 lines
---
 kernel/kexec.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
kernel/kexec.c +3 -3
index 2436ffc..3cbfcc7 100644
@@ -789,7 +789,7 @@ static int kimage_load_normal_segment(struct kimage *image,
 					 struct kexec_segment *segment)
 {
 	unsigned long maddr;
-	unsigned long ubytes, mbytes;
+	size_t ubytes, mbytes;
 	int result;
 	unsigned char __user *buf;
 
@@ -853,7 +853,7 @@ static int kimage_load_crash_segment(struct kimage *image,
 	 * We do things a page at a time for the sake of kmap.
 	 */
 	unsigned long maddr;
-	unsigned long ubytes, mbytes;
+	size_t ubytes, mbytes;
 	int result;
 	unsigned char __user *buf;
 
@@ -1455,7 +1455,7 @@ void vmcoreinfo_append_str(const char *fmt, ...)
 {
 	va_list args;
 	char buf[0x50];
-	int r;
+	size_t r;
 
 	va_start(args, fmt);
 	r = vsnprintf(buf, sizeof(buf), fmt, args);
Signature · 1 line
1.7.1