[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 02/15] mm: assert that we preserve header vs region alignment
From: |
Daniel Axtens |
Subject: |
[PATCH v3 02/15] mm: assert that we preserve header vs region alignment |
Date: |
Thu, 21 Apr 2022 15:24:14 +1000 |
grub_mm_region_init() does:
h = (grub_mm_header_t) (r + 1);
where h is a grub_mm_header_t and r is a grub_mm_region_t.
Cells are supposed to be GRUB_MM_ALIGN aligned, but while grub_mm_dump
ensures this vs the region header, grub_mm_region_init() does not.
It's better to be explicit than implicit here: rather than changing
grub_mm_region_init() to ALIGN_UP(), require that the struct is
explictly a multiple of the header size.
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
---
v2: Thanks Daniel K for feedback.
---
include/grub/mm_private.h | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h
index 203533cc3d42..d3f2321e14fb 100644
--- a/include/grub/mm_private.h
+++ b/include/grub/mm_private.h
@@ -20,6 +20,7 @@
#define GRUB_MM_PRIVATE_H 1
#include <grub/mm.h>
+#include <grub/misc.h>
/* For context, see kern/mm.c */
@@ -89,4 +90,17 @@ typedef struct grub_mm_region
extern grub_mm_region_t EXPORT_VAR (grub_mm_base);
#endif
+static inline void
+grub_mm_size_sanity_check (void) {
+ /* Ensure we preserve alignment when doing h = (grub_mm_header_t) (r + 1) */
+ COMPILE_TIME_ASSERT ((sizeof (struct grub_mm_region) %
+ sizeof (struct grub_mm_header)) == 0);
+
+ /*
+ * GRUB_MM_ALIGN is supposed to represent cell size, and a mm_header is
+ * supposed to be 1 cell.
+ */
+ COMPILE_TIME_ASSERT (sizeof (struct grub_mm_header) == GRUB_MM_ALIGN);
+}
+
#endif
--
2.32.0
- [PATCH v3 00/15] Dynamic allocation of memory regions and IBM vTPM v2, Daniel Axtens, 2022/04/21
- [PATCH v3 01/15] grub-shell: only pass SeaBIOS fw_opt in for x86 BIOS platforms, Daniel Axtens, 2022/04/21
- [PATCH v3 02/15] mm: assert that we preserve header vs region alignment,
Daniel Axtens <=
- [PATCH v3 03/15] mm: when adding a region, merge with region after as well as before, Daniel Axtens, 2022/04/21
- [PATCH v3 04/15] mm: debug support for region operations, Daniel Axtens, 2022/04/21
- [PATCH v3 05/15] mm: Drop unused unloading of modules on OOM, Daniel Axtens, 2022/04/21
- [PATCH v3 06/15] mm: Allow dynamically requesting additional memory regions, Daniel Axtens, 2022/04/21
- [PATCH v3 07/15] efi: mm: Always request a fixed number of pages on init, Daniel Axtens, 2022/04/21
- [PATCH v3 08/15] efi: mm: Extract function to add memory regions, Daniel Axtens, 2022/04/21
- [PATCH v3 09/15] efi: mm: Pass up errors from `add_memory_regions ()`, Daniel Axtens, 2022/04/21
- [PATCH v3 10/15] efi: mm: Implement runtime addition of pages, Daniel Axtens, 2022/04/21