Skip to contentSkip to conversation

kexec: increase max of kexec segments and use dynamic allocation

PATCH Superseded Amerigo Wang 3 participants 5 messages Jul 27, 2010 – Aug 5, 2010

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

PATCH v2 1/1 kexec: increase max of kexec segments and use dynamic allocation

Currently KEXEC_SEGMENT_MAX is only 16 which is too small for machine with many memory ranges. When hibernate on a machine with disjoint memory we do need one segment for each memory region. Increase this hard limit to 16K which is reasonably large.

And change ->segment from a static array to a dynamically allocated memory.

Cc Neil Horman <[email protected]>
Cc huang ying <[email protected]>
Cc Eric W. Biederman <[email protected]>

---

arch/powerpc/kernel/machine_kexec_64.c +6 -7
index ed31a29..f115585 100644
@@ -131,10 +131,7 @@ static void copy_segments(unsigned long ind)
 void kexec_copy_flush(struct kimage *image)
 {
 	long i, nr_segments = image->nr_segments;
-	struct  kexec_segment ranges[KEXEC_SEGMENT_MAX];
-
-	/* save the ranges on the stack to efficiently flush the icache */
-	memcpy(ranges, image->segment, sizeof(ranges));
+	struct  kexec_segment range;
 
 	/*
 	 * After this call we may not use anything allocated in dynamic
@@ -148,9 +145,11 @@ void kexec_copy_flush(struct kimage *image)
 	 * we need to clear the icache for all dest pages sometime,
 	 * including ones that were in place on the original copy
 	 */
-	for (i = 0; i < nr_segments; i++)
-		flush_icache_range((unsigned long)__va(ranges[i].mem),
-			(unsigned long)__va(ranges[i].mem + ranges[i].memsz));
+	for (i = 0; i < nr_segments; i++) {
+		memcpy(&range, &image->segment[i], sizeof(range));
+		flush_icache_range((unsigned long)__va(range.mem),
+			(unsigned long)__va(range.mem + range.memsz));
+	}
 }
 
 #ifdef CONFIG_SMP
include/linux/kexec.h +2 -2
index 03e8e8d..26b70ff 100644
@@ -57,7 +57,7 @@ typedef unsigned long kimage_entry_t;
 #define IND_DONE         0x4
 #define IND_SOURCE       0x8
 
-#define KEXEC_SEGMENT_MAX 16
+#define KEXEC_SEGMENT_MAX (1024*16)
 struct kexec_segment {
 	void __user *buf;
 	size_t bufsz;
@@ -86,7 +86,7 @@ struct kimage {
 	struct page *swap_page;
 
 	unsigned long nr_segments;
-	struct kexec_segment segment[KEXEC_SEGMENT_MAX];
+	struct kexec_segment *segment;
 
 	struct list_head control_pages;
 	struct list_head dest_pages;
kernel/kexec.c +15 -3
index 131b170..3f97309 100644
@@ -131,6 +131,11 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
 	if (!image)
 		goto out;
 
+	image->segment = kzalloc(nr_segments * sizeof(struct kexec_segment),
+				 GFP_KERNEL);
+	if (!image->segment)
+		goto out;
+
 	image->head = 0;
 	image->entry = &image->head;
 	image->last_entry = &image->head;
@@ -216,8 +221,10 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
 out:
 	if (result == 0)
 		*rimage = image;
-	else
+	else if (image) {
+		kfree(image->segment);
 		kfree(image);
+	}
 
 	return result;
 
@@ -261,8 +268,10 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
  out:
 	if (result == 0)
 		*rimage = image;
-	else
+	else if (image) {
+		kfree(image->segment);
 		kfree(image);
+	}
 
 	return result;
 }
@@ -330,8 +339,10 @@ static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
 out:
 	if (result == 0)
 		*rimage = image;
-	else
+	else if (image) {
+		kfree(image->segment);
 		kfree(image);
+	}
 
 	return result;
 }
@@ -656,6 +667,7 @@ static void kimage_free(struct kimage *image)
 
 	/* Free the kexec control pages... */
 	kimage_free_page_list(&image->control_pages);
+	kfree(image->segment);
 	kfree(image);
 }
 
#1 lore raw

[ Added kexec at lists.infradead.org and [email protected] ]

Quoted 19 lines

Currently KEXEC_SEGMENT_MAX is only 16 which is too small for machine with many memory ranges. When hibernate on a machine with disjoint memory we do need one segment for each memory region. Increase this hard limit to 16K which is reasonably large.

And change ->segment from a static array to a dynamically allocated memory.

Cc Neil Horman <[email protected]>
Cc huang ying <[email protected]>
Cc Eric W. Biederman <[email protected]>

---

arch/powerpc/kernel/machine_kexec_64.c +0 -0
index ed31a29..f115585 100644

arch/powerpc/kernel/machine_kexec_64.c static void copy_segments(unsigned long ind) · line 134

Quoted 8 lines
arch/powerpc/kernel/machine_kexec_64.c +1 -4
@@ -131,10 +131,7 @@ static void copy_segments(unsigned long ind)
 void kexec_copy_flush(struct kimage *image)
 {
 	long i, nr_segments = image->nr_segments;
-	struct  kexec_segment ranges[KEXEC_SEGMENT_MAX];
-
-	/* save the ranges on the stack to efficiently flush the icache */
-	memcpy(ranges, image->segment, sizeof(ranges));
+	struct  kexec_segment range;

I'm glad you found our copy on the stack and removed the stack overflow that comes with this bump, but ...

Quoted 6 lines
arch/powerpc/kernel/machine_kexec_64.c +0 -0
 	/*
 	 * After this call we may not use anything allocated in dynamic
@@ -148,9 +145,11 @@ void kexec_copy_flush(struct kimage *image)
 	 * we need to clear the icache for all dest pages sometime,
 	 * including ones that were in place on the original copy

arch/powerpc/kernel/machine_kexec_64.c void kexec_copy_flush(struct kimage *image) · line 153

Quoted 10 lines
arch/powerpc/kernel/machine_kexec_64.c +5 -3
 	 */
-	for (i = 0; i < nr_segments; i++)
-		flush_icache_range((unsigned long)__va(ranges[i].mem),
-			(unsigned long)__va(ranges[i].mem + ranges[i].memsz));
+	for (i = 0; i < nr_segments; i++) {
+		memcpy(&range, &image->segment[i], sizeof(range));
+		flush_icache_range((unsigned long)__va(range.mem),
+			(unsigned long)__va(range.mem + range.memsz));
+	}
 }

This is executed after the copy, so as it says, "we may not use anything allocated in dynamic memory".

We could allocate control pages to copy the segment list into. Actually ppc64 doesn't use the existing control page, but that is only 4kB today.

We need the list to icache flush all the pages in all the segments. The as the indirect list doesn't have pages that were allocated at their destination.

Or maybe the icache flush should be done in the generic code like it does for crash load segments?

Quoted 77 lines
arch/powerpc/kernel/machine_kexec_64.c +0 -0
 #ifdef CONFIG_SMP
include/linux/kexec.h +2 -2
index 03e8e8d..26b70ff 100644
@@ -57,7 +57,7 @@ typedef unsigned long kimage_entry_t;
 #define IND_DONE         0x4
 #define IND_SOURCE       0x8
 
-#define KEXEC_SEGMENT_MAX 16
+#define KEXEC_SEGMENT_MAX (1024*16)
 struct kexec_segment {
 	void __user *buf;
 	size_t bufsz;
@@ -86,7 +86,7 @@ struct kimage {
 	struct page *swap_page;
 
 	unsigned long nr_segments;
-	struct kexec_segment segment[KEXEC_SEGMENT_MAX];
+	struct kexec_segment *segment;
 
 	struct list_head control_pages;
 	struct list_head dest_pages;
kernel/kexec.c +14 -3
index 131b170..3f97309 100644
@@ -131,6 +131,11 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
 	if (!image)
 		goto out;
 
+	image->segment = kzalloc(nr_segments * sizeof(struct kexec_segment),
+				 GFP_KERNEL);
+	if (!image->segment)
+		goto out;
+
 	image->head = 0;
 	image->entry = &image->head;
 	image->last_entry = &image->head;
@@ -216,8 +221,10 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
 out:
 	if (result == 0)
 		*rimage = image;
-	else
+	else if (image) {
+		kfree(image->segment);
 		kfree(image);
+	}
 
 	return result;
 
@@ -261,8 +268,10 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
  out:
 	if (result == 0)
 		*rimage = image;
-	else
+	else if (image) {
+		kfree(image->segment);
 		kfree(image);
+	}
 
 	return result;
 }
@@ -330,8 +339,10 @@ static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
 out:
 	if (result == 0)
 		*rimage = image;
-	else
+	else if (image) {
+		kfree(image->segment);
 		kfree(image);
+	}
 
 	return result;
 }

kernel/kexec.c static void kimage_free(struct kimage *image) · line 673

Quoted 7 lines
kernel/kexec.c +1 -0
@@ -656,6 +667,7 @@ static void kimage_free(struct kimage *image)
 
 	/* Free the kexec control pages... */
 	kimage_free_page_list(&image->control_pages);
+	kfree(image->segment);
 	kfree(image);
 }
 

milton

#2 lore raw
Quoted 53 lines · Milton Miller

[ Added kexec at lists.infradead.org and [email protected] ]

Quoted 19 lines

Currently KEXEC_SEGMENT_MAX is only 16 which is too small for machine with many memory ranges. When hibernate on a machine with disjoint memory we do need one segment for each memory region. Increase this hard limit to 16K which is reasonably large.

And change ->segment from a static array to a dynamically allocated memory.

Cc Neil Horman <[email protected]>
Cc huang ying <[email protected]>
Cc Eric W. Biederman <[email protected]>

---

arch/powerpc/kernel/machine_kexec_64.c +0 -0
index ed31a29..f115585 100644

arch/powerpc/kernel/machine_kexec_64.c static void copy_segments(unsigned long ind) · line 134

Quoted 8 lines
arch/powerpc/kernel/machine_kexec_64.c +1 -4
@@ -131,10 +131,7 @@ static void copy_segments(unsigned long ind)
 void kexec_copy_flush(struct kimage *image)
 {
 	long i, nr_segments = image->nr_segments;
-	struct  kexec_segment ranges[KEXEC_SEGMENT_MAX];
-
-	/* save the ranges on the stack to efficiently flush the icache */
-	memcpy(ranges, image->segment, sizeof(ranges));
+	struct  kexec_segment range;

I'm glad you found our copy on the stack and removed the stack overflow that comes with this bump, but ...

Quoted 6 lines
arch/powerpc/kernel/machine_kexec_64.c +0 -0
 	/*
 	 * After this call we may not use anything allocated in dynamic
@@ -148,9 +145,11 @@ void kexec_copy_flush(struct kimage *image)
 	 * we need to clear the icache for all dest pages sometime,
 	 * including ones that were in place on the original copy

arch/powerpc/kernel/machine_kexec_64.c void kexec_copy_flush(struct kimage *image) · line 153

Quoted 10 lines
arch/powerpc/kernel/machine_kexec_64.c +5 -3
 	 */
-	for (i = 0; i < nr_segments; i++)
-		flush_icache_range((unsigned long)__va(ranges[i].mem),
-			(unsigned long)__va(ranges[i].mem + ranges[i].memsz));
+	for (i = 0; i < nr_segments; i++) {
+		memcpy(&range, &image->segment[i], sizeof(range));
+		flush_icache_range((unsigned long)__va(range.mem),
+			(unsigned long)__va(range.mem + range.memsz));
+	}
 }

This is executed after the copy, so as it says, "we may not use anything allocated in dynamic memory".

Quoted 7 lines

We could allocate control pages to copy the segment list into. Actually ppc64 doesn't use the existing control page, but that is only 4kB today.

We need the list to icache flush all the pages in all the segments. The as the indirect list doesn't have pages that were allocated at their destination.

An interesting point.

Quoted 2 lines

Or maybe the icache flush should be done in the generic code like it does for crash load segments?

Please. I don't quite understand the icache flush requirement. But we really should not be looking at the segments in the architecture specific code.

Ideally we would only keep the segment information around for the duration of the kexec_load syscall and not have it when it comes time to start the second kernel.

I am puzzled. We should be completely replacing the page tables so can't we just do a global flush? Perhaps I am being naive about what is required for a ppc flush.

Eric

author
#3 lore raw
In reply to Milton Miller
Quoted 57 lines · Milton Miller

[ Added kexec at lists.infradead.org and [email protected] ]

Quoted 19 lines

Currently KEXEC_SEGMENT_MAX is only 16 which is too small for machine with many memory ranges. When hibernate on a machine with disjoint memory we do need one segment for each memory region. Increase this hard limit to 16K which is reasonably large.

And change ->segment from a static array to a dynamically allocated memory.

Cc Neil Horman<[email protected]>
Cc huang ying<[email protected]>
Cc Eric W. Biederman<[email protected]>

---

arch/powerpc/kernel/machine_kexec_64.c +0 -0
index ed31a29..f115585 100644

arch/powerpc/kernel/machine_kexec_64.c static void copy_segments(unsigned long ind) · line 134

Quoted 8 lines
arch/powerpc/kernel/machine_kexec_64.c +1 -4
@@ -131,10 +131,7 @@ static void copy_segments(unsigned long ind)
  void kexec_copy_flush(struct kimage *image)
  {
  	long i, nr_segments = image->nr_segments;
-	struct  kexec_segment ranges[KEXEC_SEGMENT_MAX];
-
-	/* save the ranges on the stack to efficiently flush the icache */
-	memcpy(ranges, image->segment, sizeof(ranges));
+	struct  kexec_segment range;

I'm glad you found our copy on the stack and removed the stack overflow that comes with this bump, but ...

Quoted 6 lines
arch/powerpc/kernel/machine_kexec_64.c +0 -0
  	/*
  	 * After this call we may not use anything allocated in dynamic
@@ -148,9 +145,11 @@ void kexec_copy_flush(struct kimage *image)
  	 * we need to clear the icache for all dest pages sometime,
  	 * including ones that were in place on the original copy

arch/powerpc/kernel/machine_kexec_64.c void kexec_copy_flush(struct kimage *image) · line 153

Quoted 10 lines
arch/powerpc/kernel/machine_kexec_64.c +5 -3
  	 */
-	for (i = 0; i<  nr_segments; i++)
-		flush_icache_range((unsigned long)__va(ranges[i].mem),
-			(unsigned long)__va(ranges[i].mem + ranges[i].memsz));
+	for (i = 0; i<  nr_segments; i++) {
+		memcpy(&range,&image->segment[i], sizeof(range));
+		flush_icache_range((unsigned long)__va(range.mem),
+			(unsigned long)__va(range.mem + range.memsz));
+	}
  }

This is executed after the copy, so as it says, "we may not use anything allocated in dynamic memory".

We could allocate control pages to copy the segment list into. Actually ppc64 doesn't use the existing control page, but that is only 4kB today.

Quoted 7 lines

We need the list to icache flush all the pages in all the segments. The as the indirect list doesn't have pages that were allocated at their destination.

Or maybe the icache flush should be done in the generic code like it does for crash load segments?

I don't get the point here, according to the comments, it is copied into stack because of efficiency.

Signature · 2 lines
The opposite of love is not hate, it's indifference.
 - Elie Wiesel
author
#4 lore raw

(Ping Milton...)

Quoted 73 lines · Cong Wang
Quoted 61 lines · Milton Miller

[ Added kexec at lists.infradead.org and [email protected] ]

Quoted 23 lines

Currently KEXEC_SEGMENT_MAX is only 16 which is too small for machine with
many memory ranges. When hibernate on a machine with disjoint memory we do
need one segment for each memory region. Increase this hard limit to 16K which is reasonably large.

And change ->segment from a static array to a dynamically allocated memory.

Cc Neil Horman<[email protected]>
Cc huang ying<[email protected]>
Cc Eric W. Biederman<[email protected]>

---

arch/powerpc/kernel/machine_kexec_64.c +0 -0
index ed31a29..f115585 100644

arch/powerpc/kernel/machine_kexec_64.c static void copy_segments(unsigned long ind) · line 134

Quoted 8 lines
arch/powerpc/kernel/machine_kexec_64.c +1 -4
@@ -131,10 +131,7 @@ static void copy_segments(unsigned long ind)
 void kexec_copy_flush(struct kimage *image)
 {
 long i, nr_segments = image->nr_segments;
- struct kexec_segment ranges[KEXEC_SEGMENT_MAX];
-
- /* save the ranges on the stack to efficiently flush the icache */
- memcpy(ranges, image->segment, sizeof(ranges));
+ struct kexec_segment range;

I'm glad you found our copy on the stack and removed the stack overflow that comes with this bump, but ...

Quoted 6 lines
arch/powerpc/kernel/machine_kexec_64.c +0 -0
 /*
 * After this call we may not use anything allocated in dynamic
@@ -148,9 +145,11 @@ void kexec_copy_flush(struct kimage *image)
 * we need to clear the icache for all dest pages sometime,
 * including ones that were in place on the original copy

arch/powerpc/kernel/machine_kexec_64.c void kexec_copy_flush(struct kimage *image) · line 153

Quoted 10 lines
arch/powerpc/kernel/machine_kexec_64.c +5 -3
 */
- for (i = 0; i< nr_segments; i++)
- flush_icache_range((unsigned long)__va(ranges[i].mem),
- (unsigned long)__va(ranges[i].mem + ranges[i].memsz));
+ for (i = 0; i< nr_segments; i++) {
+ memcpy(&range,&image->segment[i], sizeof(range));
+ flush_icache_range((unsigned long)__va(range.mem),
+ (unsigned long)__va(range.mem + range.memsz));
+ }
 }

This is executed after the copy, so as it says, "we may not use anything allocated in dynamic memory".

We could allocate control pages to copy the segment list into. Actually ppc64 doesn't use the existing control page, but that is only 4kB today.

Quoted 7 lines

We need the list to icache flush all the pages in all the segments. The as the indirect list doesn't have pages that were allocated at their destination.

Or maybe the icache flush should be done in the generic code like it does for crash load segments?

I don't get the point here, according to the comments, it is copied into stack because of efficiency.

Signature · 2 lines
The opposite of love is not hate, it's indifference.
 - Elie Wiesel