* [PATCH] efi/libstub: Make efi_random_alloc() allocate below 4 GB on 32-bit
@ 2016-11-15 15:53 Ard Biesheuvel
0 siblings, 0 replies; 2+ messages in thread
From: Ard Biesheuvel @ 2016-11-15 15:53 UTC (permalink / raw)
To: linux-efi-u79uwXL29TY76Z2rM5mHXA,
matt-mF/unelCI9GS6iBeEJttW/XRex20P6io, arnd-r2nGTMty4D4
Cc: Ard Biesheuvel
The UEFI stub executes in the context of the firmware, which identity
maps the available system RAM, which implies that only memory below
4 GB can be used for allocations on 32-bit architectures, even on [L]PAE
capable hardware.
So ignore any reported memory above 4 GB in efi_random_alloc(). This
also fixes a reported build problem on ARM under -Os, where the 64-bit
logical shift relies on a software routine that the ARM decompressor does
not provide.
A second [minor] issue is also fixed, where the '+ 1' is moved out of
the shift, where it belongs: the reason for its presence is that a
memory region where start == end should count as a single slot, given
that end takes the the desired size and alignment of the allocation into
account.
Reported-by: Arnd Bergmann <arnd-r2nGTMty4D4@public.gmane.org>
Cc: Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/XRex20P6io@public.gmane.org>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
---
drivers/firmware/efi/libstub/random.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/efi/libstub/random.c b/drivers/firmware/efi/libstub/random.c
index 3a3feacc329f..c95f1f65d0ed 100644
--- a/drivers/firmware/efi/libstub/random.c
+++ b/drivers/firmware/efi/libstub/random.c
@@ -54,10 +54,13 @@ static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
end = round_down(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - size,
align);
+ if (end > ULONG_MAX)
+ end = ULONG_MAX + 1;
+
if (start > end)
return 0;
- return (end - start + 1) >> align_shift;
+ return ((unsigned long)(end - start) >> align_shift) + 1;
}
/*
--
2.7.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH] efi/libstub: Make efi_random_alloc() allocate below 4 GB on 32-bit
2016-11-24 18:02 [GIT PULL] one more EFI patch for v4.10 Ard Biesheuvel
@ 2016-11-24 18:02 ` Ard Biesheuvel
0 siblings, 0 replies; 2+ messages in thread
From: Ard Biesheuvel @ 2016-11-24 18:02 UTC (permalink / raw)
To: Ingo Molnar, Thomas Gleixner, H . Peter Anvin
Cc: Ard Biesheuvel, linux-kernel, linux-efi, Matt Fleming
The UEFI stub executes in the context of the firmware, which identity
maps the available system RAM, which implies that only memory below
4 GB can be used for allocations on 32-bit architectures, even on [L]PAE
capable hardware.
So ignore any reported memory above 4 GB in efi_random_alloc(). This
also fixes a reported build problem on ARM under -Os, where the 64-bit
logical shift relies on a software routine that the ARM decompressor does
not provide.
A second [minor] issue is also fixed, where the '+ 1' is moved out of
the shift, where it belongs: the reason for its presence is that a
memory region where start == end should count as a single slot, given
that 'end' takes the desired size and alignment of the allocation into
account.
To clarify the code in this regard, rename start/end to 'first_slot' and
'last_slot', respectively, and introduce 'region_end' to describe the
last usable address of the current region.
Reported-by: Arnd Bergmann <arnd@arndb.de>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
drivers/firmware/efi/libstub/random.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/drivers/firmware/efi/libstub/random.c b/drivers/firmware/efi/libstub/random.c
index 3a3feacc329f..63cd3f262b6e 100644
--- a/drivers/firmware/efi/libstub/random.c
+++ b/drivers/firmware/efi/libstub/random.c
@@ -45,19 +45,21 @@ static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
unsigned long align_shift)
{
unsigned long align = 1UL << align_shift;
- u64 start, end;
+ u64 first_slot, last_slot, region_end;
if (md->type != EFI_CONVENTIONAL_MEMORY)
return 0;
- start = round_up(md->phys_addr, align);
- end = round_down(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - size,
- align);
+ region_end = min((u64)ULONG_MAX,
+ md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1);
- if (start > end)
+ first_slot = round_up(md->phys_addr, align);
+ last_slot = round_down(region_end - size + 1, align);
+
+ if (first_slot > last_slot)
return 0;
- return (end - start + 1) >> align_shift;
+ return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
}
/*
--
2.7.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2016-11-24 18:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-11-15 15:53 [PATCH] efi/libstub: Make efi_random_alloc() allocate below 4 GB on 32-bit Ard Biesheuvel
-- strict thread matches above, loose matches on Subject: below --
2016-11-24 18:02 [GIT PULL] one more EFI patch for v4.10 Ard Biesheuvel
2016-11-24 18:02 ` [PATCH] efi/libstub: Make efi_random_alloc() allocate below 4 GB on 32-bit Ard Biesheuvel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).