Skip to contentSkip to conversation

lib/relocator: always enforce the requested alignment in malloc_in_range()

PATCH In review Roger Pau Monne 2 participants 2 messages May 12, 2023 – May 16, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lib/relocator: always enforce the requested alignment in malloc_in_range()

On failure to allocate from grub_relocator_firmware_alloc_region() in malloc_in_range() the function would stop enforcing the alignment, and the following was returned:

lib/relocator.c:431: trying to allocate in 0x200000-0xffbf9fff aligned 0x200000 size 0x406000 lib/relocator.c:1197: allocated: 0x74de2000+0x406000
lib/relocator.c:1407: allocated 0x74de2000/0x74de2000

Fix this by making sure that target always contains a suitably aligned address. After the change the return from the function is:

lib/relocator.c:431: trying to allocate in 0x200000-0xffb87fff aligned 0x200000 size 0x478000 lib/relocator.c:1204: allocated: 0x74c00000+0x478000
lib/relocator.c:1414: allocated 0x74c00000/0x74c00000

Fixes 3a5768645c05 ('First version of allocation from firmware')

---
Changes since v1:
- Fix one instance to use ALIGN_DOWN().

---
 grub-core/lib/relocator.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/lib/relocator.c +2 -2
index bfcc70dac3cc..725bca7c3c10 100644
@@ -744,7 +744,7 @@ malloc_in_range (struct grub_relocator *rel,
 	    {
 	      target = starta;
 	      if (target < start)
-		target = start;
+		target = ALIGN_UP (start, align);
 	      if (target + size <= end && target + size <= events[j].pos)
 		/* Found an usable address.  */
 		goto found;
@@ -761,7 +761,7 @@ malloc_in_range (struct grub_relocator *rel,
 	    {
 	      target = starta - size;
 	      if (target > end - size)
-		target = end - size;
+		target = ALIGN_DOWN (end - size, align);
 	      if (target >= start && target >= events[j].pos)
 		goto found;
 	    }
Signature · 1 line
2.40.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Roger Pau Monne via Grub-devel

On failure to allocate from grub_relocator_firmware_alloc_region() in malloc_in_range() the function would stop enforcing the alignment, and the following was returned:

lib/relocator.c:431: trying to allocate in 0x200000-0xffbf9fff aligned 0x200000 size 0x406000 lib/relocator.c:1197: allocated: 0x74de2000+0x406000
lib/relocator.c:1407: allocated 0x74de2000/0x74de2000

Fix this by making sure that target always contains a suitably aligned address. After the change the return from the function is:

Quoted 6 lines

lib/relocator.c:431: trying to allocate in 0x200000-0xffb87fff aligned 0x200000 size 0x478000 lib/relocator.c:1204: allocated: 0x74c00000+0x478000
lib/relocator.c:1414: allocated 0x74c00000/0x74c00000

Fixes 3a5768645c05 ('First version of allocation from firmware')

Daniel