Skip to contentSkip to conversation

ppc64: Restrict memory allocations for kernel and initrd

PATCH Superseded Stefan Berger 3 participants 26 messages Oct 31, 2023 – Nov 10, 2023

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover ppc64: Restrict memory allocations for kernel and initrd

This series of patches converts the PowerPC ieee1275 memory allocator for kernel and initrd to use the (PowerPC) regions_claim memory allocator that takes into account memory regions that are not allowed to be used, such as the gap between 640MB and 768MB as well as memory regions beyond an address in case an fadump is present. Otherwise those two could be loaded into restricted memory regions and either cause a crash or corruption of the fadump.

I adjusted the kernel and initrd load in loader/powerpc/ieee1275 to use the new memory allocator only on PowerVM and PowerKVM since this code is shared with other platforms, such as old PowerMACs.

Regards,
Stefan

Stefan Berger (9):
kern/ieee1275/init: ppc64: Introduce a request for regions_claim kern/ieee1275/init: ppc64: Decide by request whether to initialize
region
kern/ieee1275/init: ppc64: Return allocated address and len using
context
kern/ieee1275/init: ppc64: Check early for sufficiently large chunk kern/ieee1275/init: ppc64: Add support for alignment requirements kern/ieee1275/init: ppc64: Implement alloc_mem_restricted kern/ieee1275/cmain: ppc64: Introduce flags to identify Power VM and
KVM
loader/powerpc/ieee1275: Use new allocation function for kernel and
initrd
kern/ieee1275/ieee1275.c: debug: Display successful memory claims

 grub-core/Makefile.am                     |  1 +
 grub-core/kern/ieee1275/cmain.c           |  6 +-
 grub-core/kern/ieee1275/ieee1275.c        |  2 +
 grub-core/kern/ieee1275/init.c            | 76 +++++++++++++++++++----
 grub-core/loader/powerpc/ieee1275/linux.c | 50 ++++++++++++---
 include/grub/ieee1275/ieee1275.h          |  4 ++
 include/grub/powerpc/ieee1275/alloc.h     | 20 ++++++
 7 files changed, 139 insertions(+), 20 deletions(-)
 create mode 100644 include/grub/powerpc/ieee1275/alloc.h
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH 1/9 kern/ieee1275/init: ppc64: Introduce a request for regions_claim

The regions_claim() function limits the allocation of memory regions by excluding certain memory areas from being used by grub. This for example includes a gap between 640MB and 768MB as well as an upper limit beyond which no memory may be used when an fadump is present. However, the ieee1275 loader for kernel and initrd currently does not current use regions_claim() for memory allocation on PowerVM and PowerKVM and therefore may allocate memory in those areas that it should not use.

To make the regions_claim() function more flexible and ulimately usable for the ieee1275 loader, introduce a request structure to pass various parameters to the regions_claim() function that describe the properties of requested memory chunks. In a first step, move the total and flags variables into this structure.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/Makefile.am                 |  1 +
 grub-core/kern/ieee1275/init.c        | 43 ++++++++++++++++++++-------
 include/grub/powerpc/ieee1275/alloc.h | 11 +++++++
 3 files changed, 45 insertions(+), 10 deletions(-)
 create mode 100644 include/grub/powerpc/ieee1275/alloc.h
grub-core/Makefile.am +1 -0
index f0cb2f2cc..4f9f3f963 100644
@@ -240,6 +240,7 @@ endif
 
 if COND_powerpc_ieee1275
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/powerpc/ieee1275/alloc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
grub-core/kern/ieee1275/init.c +33 -10

85 lines not on this page · raw message

include/grub/powerpc/ieee1275/alloc.h new file +11 -0
new file mode 100644
index 000000000..97b8c2683
@@ -0,0 +1,11 @@
+#ifndef GRUB_POWERPC_IEEE1275_ALLOC_HEADER
+#define GRUB_POWERPC_IEEE1275_ALLOC_HEADER	1
+
+#include <grub/memory.h>
+
+struct regions_claim_request {
+  unsigned int flags;     /* GRUB_MM_ADD_REGION_(NONE|CONSECUTIVE) */
+  grub_uint32_t total;    /* number of requested bytes */
+};
+
+#endif /* GRUB_POWERPC_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

Re: [PATCH 1/9] kern/ieee1275/init: ppc64: Introduce a request for regions_claim

include/grub/powerpc/ieee1275/alloc.h

Quoted 141 lines · Stefan Berger

The regions_claim() function limits the allocation of memory regions by excluding certain memory areas from being used by grub. This for example includes a gap between 640MB and 768MB as well as an upper limit beyond which no memory may be used when an fadump is present. However, the ieee1275 loader for kernel and initrd currently does not current use regions_claim() for memory allocation on PowerVM and PowerKVM and therefore may allocate memory in those areas that it should not use.

To make the regions_claim() function more flexible and ulimately usable for the ieee1275 loader, introduce a request structure to pass various parameters to the regions_claim() function that describe the properties of requested memory chunks. In a first step, move the total and flags variables into this structure.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/Makefile.am                 |  1 +
 grub-core/kern/ieee1275/init.c        | 43 ++++++++++++++++++++-------
 include/grub/powerpc/ieee1275/alloc.h | 11 +++++++
 3 files changed, 45 insertions(+), 10 deletions(-)
 create mode 100644 include/grub/powerpc/ieee1275/alloc.h
grub-core/Makefile.am +1 -0
index f0cb2f2cc..4f9f3f963 100644
@@ -240,6 +240,7 @@ endif
 
 if COND_powerpc_ieee1275
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/powerpc/ieee1275/alloc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
grub-core/kern/ieee1275/init.c +33 -10
index d6c9c9049..061c28c98 100644
@@ -46,6 +46,9 @@
 #ifdef __sparc__
 #include <grub/machine/kernel.h>
 #endif
+#ifdef __powerpc__
+#include <grub/powerpc/ieee1275/alloc.h>
+#endif
 
 /* The maximum heap size we're going to claim at boot. Not used by sparc. */
 #ifdef __i386__
@@ -317,9 +320,9 @@ count_free (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
 
 static int
 regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
-	      unsigned int flags, void *data)
+	       void *data)
 {
-  grub_uint32_t total = *(grub_uint32_t *) data;
+  struct regions_claim_request *rcr = data;
   grub_uint64_t linux_rmo_save;
 
   if (type != GRUB_MEMORY_AVAILABLE)
@@ -499,11 +502,11 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
             }
         }
     }
-  if (flags & GRUB_MM_ADD_REGION_CONSECUTIVE && len < total)
+  if (rcr->flags & GRUB_MM_ADD_REGION_CONSECUTIVE && len < rcr->total)
     return 0;
 
-  if (len > total)
-    len = total;
+  if (len > rcr->total)
+    len = rcr->total;
 
   if (len)
     {
@@ -513,12 +516,12 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
       if (err)
 	return err;
       grub_mm_init_region ((void *) (grub_addr_t) addr, len);
-      total -= len;
+      rcr->total -= len;
     }
 
-  *(grub_uint32_t *) data = total;
+  *(grub_uint32_t *) data = rcr->total;
 
-  if (total == 0)
+  if (rcr->total == 0)
     return 1;
 
   return 0;
@@ -528,14 +531,34 @@ static int
 heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
 	   void *data)
 {
-  return regions_claim (addr, len, type, GRUB_MM_ADD_REGION_NONE, data);
+  struct regions_claim_request rcr = {
+    .flags = GRUB_MM_ADD_REGION_NONE,
+    .total = *(grub_uint32_t *) data,
+  };
+  int ret;
+
+  ret = regions_claim (addr, len, type, &rcr);
+
+  *(grub_uint32_t *) data = rcr.total;
+
+  return ret;
 }
 
 static int
 region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
 	   void *data)
 {
-  return regions_claim (addr, len, type, GRUB_MM_ADD_REGION_CONSECUTIVE, data);
+  struct regions_claim_request rcr = {
+    .flags = GRUB_MM_ADD_REGION_CONSECUTIVE,
+    .total = *(grub_uint32_t *) data,
+  };
+  int ret;
+
+  ret = regions_claim (addr, len, type, &rcr);
+
+  *(grub_uint32_t *) data = rcr.total;
+
+  return ret;
 }
 
 static grub_err_t
include/grub/powerpc/ieee1275/alloc.h new file +0 -0
new file mode 100644
index 000000000..97b8c2683
@@ -0,0 +1,11 @@

Missing standard license header. Please add it.

include/grub/powerpc/ieee1275/alloc.h line 11

Quoted 11 lines
include/grub/powerpc/ieee1275/alloc.h new file +11 -0
+#ifndef GRUB_POWERPC_IEEE1275_ALLOC_HEADER
+#define GRUB_POWERPC_IEEE1275_ALLOC_HEADER	1
+
+#include <grub/memory.h>
+
+struct regions_claim_request {
+  unsigned int flags;     /* GRUB_MM_ADD_REGION_(NONE|CONSECUTIVE) */
+  grub_uint32_t total;    /* number of requested bytes */
+};
+
+#endif /* GRUB_POWERPC_IEEE1275_ALLOC_HEADER */

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw

Re: [PATCH 1/9] kern/ieee1275/init: ppc64: Introduce a request for regions_claim

In reply to Stefan Berger
Quoted 2 lines · Stefan Berger

The regions_claim() function limits the allocation of memory regions by excluding certain memory areas from being used by grub. This for

Nit, s/grub/GRUB/... Please fix this in all commit messages and comments.

Quoted 54 lines

example includes a gap between 640MB and 768MB as well as an upper limit beyond which no memory may be used when an fadump is present. However, the ieee1275 loader for kernel and initrd currently does not current use regions_claim() for memory allocation on PowerVM and PowerKVM and therefore may allocate memory in those areas that it should not use.

To make the regions_claim() function more flexible and ulimately usable for the ieee1275 loader, introduce a request structure to pass various parameters to the regions_claim() function that describe the properties of requested memory chunks. In a first step, move the total and flags variables into this structure.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/Makefile.am                 |  1 +
 grub-core/kern/ieee1275/init.c        | 43 ++++++++++++++++++++-------
 include/grub/powerpc/ieee1275/alloc.h | 11 +++++++
 3 files changed, 45 insertions(+), 10 deletions(-)
 create mode 100644 include/grub/powerpc/ieee1275/alloc.h
grub-core/Makefile.am +1 -0
index f0cb2f2cc..4f9f3f963 100644
@@ -240,6 +240,7 @@ endif
 
 if COND_powerpc_ieee1275
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/powerpc/ieee1275/alloc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
grub-core/kern/ieee1275/init.c +3 -0
index d6c9c9049..061c28c98 100644
@@ -46,6 +46,9 @@
 #ifdef __sparc__
 #include <grub/machine/kernel.h>
 #endif
+#ifdef __powerpc__
+#include <grub/powerpc/ieee1275/alloc.h>
+#endif
 
 /* The maximum heap size we're going to claim at boot. Not used by sparc. */
 #ifdef __i386__

grub-core/kern/ieee1275/init.c count_free (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 323

Quoted 5 lines
grub-core/kern/ieee1275/init.c +1 -1
@@ -317,9 +320,9 @@ count_free (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
 
 static int
 regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
-	      unsigned int flags, void *data)
+	       void *data)

Why not "struct regions_claim_request *rcr" instead of "void *data"? Then you do not need extra assignment below...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw

Re: [PATCH 1/9] kern/ieee1275/init: ppc64: Introduce a request for regions_claim

In reply to Stefan Berger
Quoted 6 lines · Stefan Berger

The regions_claim() function limits the allocation of memory regions by excluding certain memory areas from being used by grub. This for example includes a gap between 640MB and 768MB as well as an upper limit beyond which no memory may be used when an fadump is present. However, the ieee1275 loader for kernel and initrd currently does not current use regions_claim() for memory allocation on PowerVM and

s/currently does not current use/currently does not use/

Thanks
Hari

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw

PATCH 2/9 kern/ieee1275/init: ppc64: Decide by request whether to initialize region

In reply to Stefan Berger

Let the regions_claim() request structure's init_region determine whether to call grub_mm_init_region() on it. This allows for adding memory to grub's memory heap if init_region is set to true, or direct usage of the memory otherwise. Set all current caller's init_region to true since they want to add memory regions to grub's heap.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c        | 5 ++++-
 include/grub/powerpc/ieee1275/alloc.h | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
grub-core/kern/ieee1275/init.c +4 -1
index 061c28c98..217f25fae 100644
@@ -515,7 +515,8 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
       err = grub_claimmap (addr, len);
       if (err)
 	return err;
-      grub_mm_init_region ((void *) (grub_addr_t) addr, len);
+      if (rcr->init_region)
+          grub_mm_init_region ((void *) (grub_addr_t) addr, len);
       rcr->total -= len;
     }
 
@@ -534,6 +535,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   struct regions_claim_request rcr = {
     .flags = GRUB_MM_ADD_REGION_NONE,
     .total = *(grub_uint32_t *) data,
+    .init_region = true,
   };
   int ret;
 
@@ -551,6 +553,7 @@ region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   struct regions_claim_request rcr = {
     .flags = GRUB_MM_ADD_REGION_CONSECUTIVE,
     .total = *(grub_uint32_t *) data,
+    .init_region = true,
   };
   int ret;
 
include/grub/powerpc/ieee1275/alloc.h +3 -0
index 97b8c2683..4bc362d36 100644
@@ -1,11 +1,14 @@
 #ifndef GRUB_POWERPC_IEEE1275_ALLOC_HEADER
 #define GRUB_POWERPC_IEEE1275_ALLOC_HEADER	1
 
+#include <stdbool.h>
+
 #include <grub/memory.h>
 
 struct regions_claim_request {
   unsigned int flags;     /* GRUB_MM_ADD_REGION_(NONE|CONSECUTIVE) */
   grub_uint32_t total;    /* number of requested bytes */
+  bool init_region;       /* whether to add memory to the heap using grub_mm_init_region() */
 };
 
 #endif /* GRUB_POWERPC_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#6 lore raw

PATCH 3/9 kern/ieee1275/init: ppc64: Return allocated address and len using context

In reply to Stefan Berger

Return the allocated address and the length of the memory block in the request structure if a memory allocation was actually done. Leave the address untouched otherwise. This enables a caller who wants to use the allocated memory directly (rather than adding the memory to the heap) to see where memory was allocated. None of the current callers needs this but the converted ieee1275 loader will make use of it.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c        | 3 +++
 include/grub/powerpc/ieee1275/alloc.h | 2 ++
 2 files changed, 5 insertions(+)
grub-core/kern/ieee1275/init.c +3 -0
index 217f25fae..e09db4192 100644
@@ -518,6 +518,9 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
       if (rcr->init_region)
           grub_mm_init_region ((void *) (grub_addr_t) addr, len);
       rcr->total -= len;
+
+      rcr->addr = addr;
+      rcr->len = len;
     }
 
   *(grub_uint32_t *) data = rcr->total;
include/grub/powerpc/ieee1275/alloc.h +2 -0
index 4bc362d36..ed0c76e2d 100644
@@ -9,6 +9,8 @@ struct regions_claim_request {
   unsigned int flags;     /* GRUB_MM_ADD_REGION_(NONE|CONSECUTIVE) */
   grub_uint32_t total;    /* number of requested bytes */
   bool init_region;       /* whether to add memory to the heap using grub_mm_init_region() */
+  grub_uint64_t addr;     /* result address */
+  grub_uint64_t len;      /* number of bytes allocated */
 };
 
 #endif /* GRUB_POWERPC_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#7 lore raw

Re: [PATCH 3/9] kern/ieee1275/init: ppc64: Return allocated address and len using context

Quoted 5 lines · Stefan Berger

Return the allocated address and the length of the memory block in the request structure if a memory allocation was actually done. Leave the address untouched otherwise. This enables a caller who wants to use the allocated memory directly (rather than adding the memory to the heap) to see where memory was allocated. None of the current callers needs this

s/callers needs this/callers need this/

Thanks
Hari

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#8 lore raw

PATCH 4/9 kern/ieee1275/init: ppc64: Check early for sufficiently large chunk

In reply to Stefan Berger

If a single memory block is requested (GRUB_MM_ADD_REGION_CONSECUTIVE) then check early on whether the given block from the memory map is big enough to satisfy the request.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c | 4 ++++
 1 file changed, 4 insertions(+)
grub-core/kern/ieee1275/init.c +4 -0
index e09db4192..a0ce9d55e 100644
@@ -328,6 +328,10 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   if (type != GRUB_MEMORY_AVAILABLE)
     return 0;
 
+  /* If a specific size was requested: is this chunk big enough? */
+  if (rcr->flags & GRUB_MM_ADD_REGION_CONSECUTIVE && len < rcr->total)
+    return 0;
+
   /* Do not consider memory beyond 4GB */
   if (addr > 0xffffffffULL)
     return 0;
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#9 lore raw

Re: [PATCH 4/9] kern/ieee1275/init: ppc64: Check early for sufficiently large chunk

Quoted 20 lines · Stefan Berger

If a single memory block is requested (GRUB_MM_ADD_REGION_CONSECUTIVE) then check early on whether the given block from the memory map is big enough to satisfy the request.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c | 4 ++++
 1 file changed, 4 insertions(+)
grub-core/kern/ieee1275/init.c +0 -0
index e09db4192..a0ce9d55e 100644

grub-core/kern/ieee1275/init.c regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 333

Quoted 6 lines
grub-core/kern/ieee1275/init.c +3 -0
@@ -328,6 +328,10 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   if (type != GRUB_MEMORY_AVAILABLE)
     return 0;
 
+  /* If a specific size was requested: is this chunk big enough? */
+  if (rcr->flags & GRUB_MM_ADD_REGION_CONSECUTIVE && len < rcr->total)
+    return 0;

Do we really need that? Same check is a few lines below. If we need both checks the commit message should explain why. And I would add comments before ifs explaining what they do and why they are needed/different/...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#10 lore raw

Re: [PATCH 4/9] kern/ieee1275/init: ppc64: Check early for sufficiently large chunk

Quoted 24 lines · Daniel Kiper
Quoted 23 lines · Stefan Berger

If a single memory block is requested (GRUB_MM_ADD_REGION_CONSECUTIVE) then check early on whether the given block from the memory map is big enough to satisfy the request.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
  grub-core/kern/ieee1275/init.c | 4 ++++
  1 file changed, 4 insertions(+)
grub-core/kern/ieee1275/init.c +0 -0
index e09db4192..a0ce9d55e 100644
@@ -328,6 +328,10 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
    if (type != GRUB_MEMORY_AVAILABLE)
      return 0;
 
Quoted 7 lines

grub-core/kern/ieee1275/init.c regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 333

Quoted 3 lines
grub-core/kern/ieee1275/init.c +3 -0
+  /* If a specific size was requested: is this chunk big enough? */
+  if (rcr->flags & GRUB_MM_ADD_REGION_CONSECUTIVE && len < rcr->total)
+    return 0;

Do we really need that? Same check is a few lines below. If we need both checks the commit message should explain why. And I would add comments before ifs explaining what they do and why they are needed/different/...

I could drop this one. It's an optimization to not even look at a free memory chunk if it's too small to fulfill the request just to begin with. The check later on will catch it then as well.

Stefan

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#11 lore raw

PATCH 5/9 kern/ieee1275/init: ppc64: Add support for alignment requirements

In reply to Stefan Berger

Add support for memory alignment requirements and adjust a candidate address to it before checking whether the block is large enough. This must be done in this order since the alignment adjustment can make a block smaller than what was requested.

None of the current callers has memory alignment requirements but the ieee1275 loader for kernel and initrd will use it to convey them.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c        | 14 ++++++++++++++
 include/grub/powerpc/ieee1275/alloc.h |  1 +
 2 files changed, 15 insertions(+)
grub-core/kern/ieee1275/init.c +14 -0
index a0ce9d55e..542a4c595 100644
@@ -506,6 +506,20 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
             }
         }
     }
+
+  /* Honor alignment restrictions on candidate addr */
+  if (rcr->align)
+    {
+      grub_uint64_t align_addr = ALIGN_UP (addr, rcr->align);
+      grub_uint64_t d = align_addr - addr;
+
+      if (d > len)
+        return 0;
+
+      len -= d;
+      addr = align_addr;
+    }
+
   if (rcr->flags & GRUB_MM_ADD_REGION_CONSECUTIVE && len < rcr->total)
     return 0;
 
include/grub/powerpc/ieee1275/alloc.h +1 -0
index ed0c76e2d..6bfd3d986 100644
@@ -11,6 +11,7 @@ struct regions_claim_request {
   bool init_region;       /* whether to add memory to the heap using grub_mm_init_region() */
   grub_uint64_t addr;     /* result address */
   grub_uint64_t len;      /* number of bytes allocated */
+  grub_size_t align;      /* alignment restrictions */
 };
 
 #endif /* GRUB_POWERPC_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#12 lore raw

Re: [PATCH 5/9] kern/ieee1275/init: ppc64: Add support for alignment requirements

Quoted 30 lines · Stefan Berger

Add support for memory alignment requirements and adjust a candidate address to it before checking whether the block is large enough. This must be done in this order since the alignment adjustment can make a block smaller than what was requested.

None of the current callers has memory alignment requirements but the ieee1275 loader for kernel and initrd will use it to convey them.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
  grub-core/kern/ieee1275/init.c        | 14 ++++++++++++++
  include/grub/powerpc/ieee1275/alloc.h |  1 +
  2 files changed, 15 insertions(+)
grub-core/kern/ieee1275/init.c +2 -0
index a0ce9d55e..542a4c595 100644
@@ -506,6 +506,20 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
              }
          }
      }
+
+  /* Honor alignment restrictions on candidate addr */

grub-core/kern/ieee1275/init.c regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 520

Quoted 10 lines
grub-core/kern/ieee1275/init.c +9 -0
+  if (rcr->align)
+    {
+      grub_uint64_t align_addr = ALIGN_UP (addr, rcr->align);
+      grub_uint64_t d = align_addr - addr;
+
+      if (d > len)
+        return 0;
+
 
+      len -= d;

Curious.. Is 'len' here length of the region or the claim request size? If latter, why should 'len' have any impact here??

Thanks
Hari

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#13 lore raw

Re: [PATCH 5/9] kern/ieee1275/init: ppc64: Add support for alignment requirements

Quoted 43 lines · Hari Bathini
Quoted 39 lines · Stefan Berger

Add support for memory alignment requirements and adjust a candidate address to it before checking whether the block is large enough. This must be done in this order since the alignment adjustment can make a block smaller than what was requested.

None of the current callers has memory alignment requirements but the ieee1275 loader for kernel and initrd will use it to convey them.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>

---
  grub-core/kern/ieee1275/init.c        | 14 ++++++++++++++ include/grub/powerpc/ieee1275/alloc.h |  1 +

  2 files changed, 15 insertions(+)
grub-core/kern/ieee1275/init.c +10 -0
index a0ce9d55e..542a4c595 100644
@@ -506,6 +506,20 @@ regions_claim (grub_uint64_t addr, grub_uint64_t 
 len, grub_memory_type_t type,
               }
           }
       }
+
+  /* Honor alignment restrictions on candidate addr */
+  if (rcr->align)
+    {
+      grub_uint64_t align_addr = ALIGN_UP (addr, rcr->align);
+      grub_uint64_t d = align_addr - addr;
+
+      if (d > len)
+        return 0;
+
Quoted 3 lines

grub-core/kern/ieee1275/init.c regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 519

Quoted 1 line
grub-core/kern/ieee1275/init.c +1 -0
+      len -= d;

Curious.. Is 'len' here length of the region or the claim request size?

len is the length of the requested size of a memory region

Quoted 4 lines

If latter, why should 'len' have any impact here??

Thanks
Hari

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#14 lore raw

Re: [PATCH 5/9] kern/ieee1275/init: ppc64: Add support for alignment requirements

Quoted 46 lines · Stefan Berger
Quoted 43 lines · Hari Bathini
Quoted 39 lines · Stefan Berger

Add support for memory alignment requirements and adjust a candidate address to it before checking whether the block is large enough. This must be done in this order since the alignment adjustment can make a block smaller than what was requested.

None of the current callers has memory alignment requirements but the ieee1275 loader for kernel and initrd will use it to convey them.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>

---
  grub-core/kern/ieee1275/init.c        | 14 ++++++++++++++ include/grub/powerpc/ieee1275/alloc.h |  1 +

  2 files changed, 15 insertions(+)
grub-core/kern/ieee1275/init.c +10 -0
index a0ce9d55e..542a4c595 100644
@@ -506,6 +506,20 @@ regions_claim (grub_uint64_t addr, grub_uint64_t 
 len, grub_memory_type_t type,
               }
           }
       }
+
+  /* Honor alignment restrictions on candidate addr */
+  if (rcr->align)
+    {
+      grub_uint64_t align_addr = ALIGN_UP (addr, rcr->align);
+      grub_uint64_t d = align_addr - addr;
+
+      if (d > len)
+        return 0;
+
Quoted 5 lines
Quoted 3 lines

grub-core/kern/ieee1275/init.c regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 519

Quoted 1 line
grub-core/kern/ieee1275/init.c +1 -0
+      len -= d;

Curious.. Is 'len' here length of the region or the claim request size?

len is the length of the requested size of a memory region

Actually, the above statement was not correct. It is the length of a free memory region that starts at addr with length len. If addr is now aligned-up the memory region that we can use becomes smaller by the number of bytes that we lost by the the alignment (align-up!), ot an extent that the resulting address can even become unusable ( if (d > len) ) if it is beyond that free region. Finally, we have to decrease the size of the length of the memory region by 'd'.

Quoted 10 lines
Quoted 5 lines

If latter, why should 'len' have any impact here??

Thanks
Hari

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#15 lore raw

PATCH 6/9 kern/ieee1275/init: ppc64: Implement alloc_mem_restricted

In reply to Stefan Berger

Implement alloc_mem_restricted that uses regions_claim() for memory allocation. The ieee1275 loader will use this function on PowerVM and PowerKVM and thus avoid usage of memory that it is not allowed to use.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/kern/ieee1275/init.c        | 7 +++++++
 include/grub/powerpc/ieee1275/alloc.h | 3 +++
 2 files changed, 10 insertions(+)
grub-core/kern/ieee1275/init.c +7 -0
index 542a4c595..6a3532aa7 100644
@@ -585,6 +585,13 @@ region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   return ret;
 }
 
+int
+alloc_mem_restricted (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
+                      void *ctx)
+{
+  return regions_claim (addr, len, type, ctx);
+}
+
 static grub_err_t
 grub_ieee1275_mm_add_region (grub_size_t size, unsigned int flags)
 {
include/grub/powerpc/ieee1275/alloc.h +3 -0
index 6bfd3d986..14e37908e 100644
@@ -14,4 +14,7 @@ struct regions_claim_request {
   grub_size_t align;      /* alignment restrictions */
 };
 
+int EXPORT_FUNC(alloc_mem_restricted) (grub_uint64_t addr, grub_uint64_t len,
+                                       grub_memory_type_t type, void *ctx);
+
 #endif /* GRUB_POWERPC_IEEE1275_ALLOC_HEADER */
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#16 lore raw

Re: [PATCH 6/9] kern/ieee1275/init: ppc64: Implement alloc_mem_restricted

Quoted 40 lines · Stefan Berger

Implement alloc_mem_restricted that uses regions_claim() for memory allocation. The ieee1275 loader will use this function on PowerVM and PowerKVM and thus avoid usage of memory that it is not allowed to use.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
  grub-core/kern/ieee1275/init.c        | 7 +++++++
  include/grub/powerpc/ieee1275/alloc.h | 3 +++
  2 files changed, 10 insertions(+)
grub-core/kern/ieee1275/init.c +7 -0
index 542a4c595..6a3532aa7 100644
@@ -585,6 +585,13 @@ region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
    return ret;
  }
 
+int
+alloc_mem_restricted (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
+                      void *ctx)
+{
+  return regions_claim (addr, len, type, ctx);
+}
+
  static grub_err_t
  grub_ieee1275_mm_add_region (grub_size_t size, unsigned int flags)
  {
include/grub/powerpc/ieee1275/alloc.h +0 -0
index 6bfd3d986..14e37908e 100644

include/grub/powerpc/ieee1275/alloc.h struct regions_claim_request { · line 19

Quoted 6 lines
include/grub/powerpc/ieee1275/alloc.h +3 -0
@@ -14,4 +14,7 @@ struct regions_claim_request {
    grub_size_t align;      /* alignment restrictions */
  };
 
+int EXPORT_FUNC(alloc_mem_restricted) (grub_uint64_t addr, grub_uint64_t len,
+                                       grub_memory_type_t type, void *ctx);
+

Why not export regions_claim() itself instead of introducing this new function?

Thanks
Hari

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#17 lore raw

Re: [PATCH 6/9] kern/ieee1275/init: ppc64: Implement alloc_mem_restricted

Quoted 40 lines · Hari Bathini
Quoted 39 lines · Stefan Berger

Implement alloc_mem_restricted that uses regions_claim() for memory allocation. The ieee1275 loader will use this function on PowerVM and PowerKVM and thus avoid usage of memory that it is not allowed to use.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
  grub-core/kern/ieee1275/init.c        | 7 +++++++
  include/grub/powerpc/ieee1275/alloc.h | 3 +++
  2 files changed, 10 insertions(+)
grub-core/kern/ieee1275/init.c +7 -0
index 542a4c595..6a3532aa7 100644
@@ -585,6 +585,13 @@ region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
    return ret;
  }
+int
+alloc_mem_restricted (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
+                      void *ctx)
+{
+  return regions_claim (addr, len, type, ctx);
+}
+
  static grub_err_t
  grub_ieee1275_mm_add_region (grub_size_t size, unsigned int flags)
  {
include/grub/powerpc/ieee1275/alloc.h +0 -0
index 6bfd3d986..14e37908e 100644
@@ -14,4 +14,7 @@ struct regions_claim_request {
Quoted 8 lines

include/grub/powerpc/ieee1275/alloc.h struct regions_claim_request { · line 19

Quoted 5 lines
include/grub/powerpc/ieee1275/alloc.h +3 -0
    grub_size_t align;      /* alignment restrictions */
  };
+int EXPORT_FUNC(alloc_mem_restricted) (grub_uint64_t addr, grub_uint64_t len,
+                                       grub_memory_type_t type, void *ctx);
+

Why not export regions_claim() itself instead of introducing this new function?

Yeah, good point. By the way, all exported functions have to be prefixed with "grub_".

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#18 lore raw

Re: [PATCH 6/9] kern/ieee1275/init: ppc64: Implement alloc_mem_restricted

Quoted 50 lines · Daniel Kiper
Quoted 40 lines · Hari Bathini
Quoted 39 lines · Stefan Berger

Implement alloc_mem_restricted that uses regions_claim() for memory allocation. The ieee1275 loader will use this function on PowerVM and PowerKVM and thus avoid usage of memory that it is not allowed to use.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
   grub-core/kern/ieee1275/init.c        | 7 +++++++
   include/grub/powerpc/ieee1275/alloc.h | 3 +++
   2 files changed, 10 insertions(+)
grub-core/kern/ieee1275/init.c +7 -0
index 542a4c595..6a3532aa7 100644
@@ -585,6 +585,13 @@ region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
     return ret;
   }
+int
+alloc_mem_restricted (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
+                      void *ctx)
+{
+  return regions_claim (addr, len, type, ctx);
+}
+
   static grub_err_t
   grub_ieee1275_mm_add_region (grub_size_t size, unsigned int flags)
   {
include/grub/powerpc/ieee1275/alloc.h +0 -0
index 6bfd3d986..14e37908e 100644
@@ -14,4 +14,7 @@ struct regions_claim_request {
Quoted 8 lines

include/grub/powerpc/ieee1275/alloc.h struct regions_claim_request { · line 19

Quoted 5 lines
include/grub/powerpc/ieee1275/alloc.h +3 -0
     grub_size_t align;      /* alignment restrictions */
   };
+int EXPORT_FUNC(alloc_mem_restricted) (grub_uint64_t addr, grub_uint64_t len,
+                                       grub_memory_type_t type, void *ctx);
+

Why not export regions_claim() itself instead of introducing this new function?

Quoted 2 lines

Yeah, good point. By the way, all exported functions have to be prefixed with "grub_".

I will prefix this one with grub_ then in v4.

Quoted 7 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#19 lore raw

Re: [PATCH 6/9] kern/ieee1275/init: ppc64: Implement alloc_mem_restricted

In reply to Daniel Kiper
Quoted 50 lines · Daniel Kiper
Quoted 40 lines · Hari Bathini
Quoted 39 lines · Stefan Berger

Implement alloc_mem_restricted that uses regions_claim() for memory allocation. The ieee1275 loader will use this function on PowerVM and PowerKVM and thus avoid usage of memory that it is not allowed to use.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
   grub-core/kern/ieee1275/init.c        | 7 +++++++
   include/grub/powerpc/ieee1275/alloc.h | 3 +++
   2 files changed, 10 insertions(+)
grub-core/kern/ieee1275/init.c +7 -0
index 542a4c595..6a3532aa7 100644
@@ -585,6 +585,13 @@ region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
     return ret;
   }
+int
+alloc_mem_restricted (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
+                      void *ctx)
+{
+  return regions_claim (addr, len, type, ctx);
+}
+
   static grub_err_t
   grub_ieee1275_mm_add_region (grub_size_t size, unsigned int flags)
   {
include/grub/powerpc/ieee1275/alloc.h +0 -0
index 6bfd3d986..14e37908e 100644
@@ -14,4 +14,7 @@ struct regions_claim_request {
Quoted 8 lines

include/grub/powerpc/ieee1275/alloc.h struct regions_claim_request { · line 19

Quoted 5 lines
include/grub/powerpc/ieee1275/alloc.h +3 -0
     grub_size_t align;      /* alignment restrictions */
   };
+int EXPORT_FUNC(alloc_mem_restricted) (grub_uint64_t addr, grub_uint64_t len,
+                                       grub_memory_type_t type, void *ctx);
+

Why not export regions_claim() itself instead of introducing this new function?

Quoted 2 lines

Yeah, good point. By the way, all exported functions have to be prefixed with "grub_".

Ok, convinced, exporting grub_regions_claim() now.

Quoted 7 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#20 lore raw

Re: [PATCH 6/9] kern/ieee1275/init: ppc64: Implement alloc_mem_restricted

In reply to Hari Bathini
Quoted 46 lines · Hari Bathini
Quoted 43 lines · Stefan Berger

Implement alloc_mem_restricted that uses regions_claim() for memory allocation. The ieee1275 loader will use this function on PowerVM and PowerKVM and thus avoid usage of memory that it is not allowed to use.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>

---
  grub-core/kern/ieee1275/init.c        | 7 +++++++ include/grub/powerpc/ieee1275/alloc.h | 3 +++

  2 files changed, 10 insertions(+)
grub-core/kern/ieee1275/init.c +7 -0
index 542a4c595..6a3532aa7 100644
@@ -585,6 +585,13 @@ region_claim (grub_uint64_t addr, grub_uint64_t 
 len, grub_memory_type_t type,
     return ret;
   }
+int
+alloc_mem_restricted (grub_uint64_t addr, grub_uint64_t len, 
 grub_memory_type_t type,
+                      void *ctx)
+{
+  return regions_claim (addr, len, type, ctx);
+}
+

  static grub_err_t
  grub_ieee1275_mm_add_region (grub_size_t size, unsigned int flags) {

include/grub/powerpc/ieee1275/alloc.h +0 -0
index 6bfd3d986..14e37908e 100644
@@ -14,4 +14,7 @@ struct regions_claim_request {

    grub_size_t align;      /* alignment restrictions */ };

Quoted 8 lines
Quoted 5 lines

+int EXPORT_FUNC(alloc_mem_restricted) (grub_uint64_t addr, grub_uint64_t len,
+                                       grub_memory_type_t type, void *ctx);
+

Why not export regions_claim() itself instead of introducing this new function?

Something needs to be exported and I wanted all callers to regions_claim() to be local to init.c. So, I exported the new function.

Quoted 8 lines

Thanks
Hari

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#21 lore raw

PATCH 7/9 kern/ieee1275/cmain: ppc64: Introduce flags to identify Power VM and KVM

In reply to Stefan Berger

Introduce flags to identify PowerVM and PowerKVM and set them where each type of host has been detected.

---
 grub-core/kern/ieee1275/cmain.c  | 6 +++++-
 include/grub/ieee1275/ieee1275.h | 4 ++++
 2 files changed, 9 insertions(+), 1 deletion(-)
grub-core/kern/ieee1275/cmain.c +5 -1
index 400d85d28..cc7c32873 100644
@@ -126,7 +126,10 @@ grub_ieee1275_find_options (void)
 
 #if defined(__powerpc__)
       if (grub_strncmp (tmp, "IBM,", 4) == 0)
-	grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY);
+	{
+	  grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY);
+	  grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_POWER_VM);
+	}
 #endif
     }
 
@@ -193,6 +196,7 @@ grub_ieee1275_find_options (void)
       grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM);
 
       grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_HAS_CURSORONOFF);
+      grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_POWER_KVM);
     }
 }
 
include/grub/ieee1275/ieee1275.h +4 -0
index 9f669fbee..5b7a36ad8 100644
@@ -140,6 +140,10 @@ enum grub_ieee1275_flag
    * only 256MB in size, try asking for more with CAS.
    */
   GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY,
+
+  GRUB_IEEE1275_FLAG_POWER_VM,
+
+  GRUB_IEEE1275_FLAG_POWER_KVM,
 #endif
 };
 
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#22 lore raw

PATCH 8/9 loader/powerpc/ieee1275: Use new allocation function for kernel and initrd

In reply to Stefan Berger

On PowerVM and PowerKVM use the new memory allocation function that honors restrictions on which memory grub can actually use. In the request structure indicate the request for a single memory block along with address alignment restrictions. Request direct usage of the memory block by setting init_region to false (prevent it from being added to grub's heap). Initialize the found addr to '-1', so that '-1' will be returned to the loader in case no memory could be allocated.

Cc Hari Bathini <[email protected]>
Cc Pavithra Prakash <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Carolyn Scherrer <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Sourabh Jain <[email protected]>
---
 grub-core/loader/powerpc/ieee1275/linux.c | 50 +++++++++++++++++++----
 1 file changed, 42 insertions(+), 8 deletions(-)
grub-core/loader/powerpc/ieee1275/linux.c +42 -8
index e6d071508..bc293104c 100644
@@ -30,6 +30,7 @@
 #include <grub/lib/cmdline.h>
 #include <grub/cache.h>
 #include <grub/linux.h>
+#include <grub/powerpc/ieee1275/alloc.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -116,6 +117,22 @@ grub_linux_claimmap_iterate (grub_addr_t target, grub_size_t size,
   return ctx.found_addr;
 }
 
+static grub_addr_t
+grub_linux_claimmap_iterate_restricted (grub_size_t size, grub_size_t align)
+{
+  struct regions_claim_request rcr = {
+    .flags = GRUB_MM_ADD_REGION_CONSECUTIVE,
+    .total = size,
+    .init_region = false,
+    .addr = (grub_uint64_t) -1,
+    .align = align,
+  };
+
+  grub_machine_mmap_iterate (alloc_mem_restricted, &rcr);
+
+  return rcr.addr;
+}
+
 static grub_err_t
 grub_linux_boot (void)
 {
@@ -227,10 +244,18 @@ grub_linux_load64 (grub_elf_t elf, const char *filename)
   offset = entry - base_addr;
   /* Linux's incorrectly contains a virtual address.  */
 
-  /* On some systems, firmware occupies the memory we're trying to use.
-   * Happily, Linux can be loaded anywhere (it relocates itself).  Iterate
-   * until we find an open area.  */
-  seg_addr = grub_linux_claimmap_iterate (base_addr & ~ELF64_LOADMASK, linux_size, align);
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_POWER_VM) ||
+      grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_POWER_KVM))
+    {
+      seg_addr = grub_linux_claimmap_iterate_restricted (linux_size, align);
+    }
+  else
+    {
+      /* On some systems, firmware occupies the memory we're trying to use.
+       * Happily, Linux can be loaded anywhere (it relocates itself).  Iterate
+       * until we find an open area.  */
+      seg_addr = grub_linux_claimmap_iterate (base_addr & ~ELF64_LOADMASK, linux_size, align);
+    }
   if (seg_addr == (grub_addr_t) -1)
     return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
 
@@ -339,11 +364,20 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
 
   size = grub_get_initrd_size (&initrd_ctx);
 
-  first_addr = linux_addr + linux_size;
 
-  /* Attempt to claim at a series of addresses until successful in
-     the same way that grub_rescue_cmd_linux does.  */
-  addr = grub_linux_claimmap_iterate (first_addr, size, 0x100000);
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_POWER_VM) ||
+      grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_POWER_KVM))
+    {
+      addr = grub_linux_claimmap_iterate_restricted (size, 0x100000);
+    }
+  else
+    {
+      /* Attempt to claim at a series of addresses until successful in
+         the same way that grub_rescue_cmd_linux does.  */
+      first_addr = linux_addr + linux_size;
+      addr = grub_linux_claimmap_iterate (first_addr, size, 0x100000);
+    }
+
   if (addr == (grub_addr_t) -1)
      goto fail;
 
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#23 lore raw

PATCH 9/9 kern/ieee1275/ieee1275.c: debug: Display successful memory claims

In reply to Stefan Berger

Display successful memory claims with exact address and rounded-down MiB location and rounded-up size in MiB.

---
 grub-core/kern/ieee1275/ieee1275.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/kern/ieee1275/ieee1275.c +2 -0
index db870db47..fef3ccf1a 100644
@@ -590,6 +590,8 @@ grub_ieee1275_claim (grub_addr_t addr, grub_size_t size, unsigned int align,
     *result = args.base;
   if (args.base == IEEE1275_CELL_INVALID)
     return -1;
+  grub_dprintf ("mmap", "CLAIMED: 0x%x (%d MiB)  size: %d MiB\n",
+		args.base, args.base >> 20, ALIGN_UP(size, 1 << 20) >> 20);
   return 0;
 }
 
Signature · 7 lines
2.25.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#24 lore raw
In reply to Stefan Berger
Quoted 11 lines · Stefan Berger

This series of patches converts the PowerPC ieee1275 memory allocator for kernel and initrd to use the (PowerPC) regions_claim memory allocator that takes into account memory regions that are not allowed to be used, such as the gap between 640MB and 768MB as well as memory regions beyond an address in case an fadump is present. Otherwise those two could be loaded into restricted memory regions and either cause a crash or corruption of the fadump.

I adjusted the kernel and initrd load in loader/powerpc/ieee1275 to use the new memory allocator only on PowerVM and PowerKVM since this code is shared with other platforms, such as old PowerMACs.

I have sent you some more comments. Mostly nits. If you take them into account feel free to add my RB to all patches.

Thanks,

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#25 lore raw
Quoted 9 lines · Daniel Kiper
Quoted 8 lines · Stefan Berger

This series of patches converts the PowerPC ieee1275 memory allocator for kernel and initrd to use the (PowerPC) regions_claim memory allocator that takes into account memory regions that are not allowed to be used, such as the gap between 640MB and 768MB as well as memory regions beyond an address in case an fadump is present. Otherwise those two could be loaded into restricted memory regions and either cause a crash or corruption of the fadump.

Quoted 7 lines
Quoted 3 lines

I adjusted the kernel and initrd load in loader/powerpc/ieee1275 to use the new memory allocator only on PowerVM and PowerKVM since this code is shared with other platforms, such as old PowerMACs.

I have sent you some more comments. Mostly nits. If you take them into account feel free to add my RB to all patches.

Thanks for looking at this series. I will repost soon.

Stefan

Quoted 8 lines

Thanks,

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel