[RFCv2 1/9] efi/libstub: Ask efi_random_alloc() to skip unusable memory

Pingfan Liu posted 9 patches 1 year, 4 months ago
[RFCv2 1/9] efi/libstub: Ask efi_random_alloc() to skip unusable memory
Posted by Pingfan Liu 1 year, 4 months ago
efi_random_alloc() demands EFI_ALLOCATE_ADDRESS when allocate_pages(),
but the current implement can not ensure the selected target locates
inside free area, that is to exclude EFI_BOOT_SERVICES_*,
EFI_RUNTIME_SERVICES_* etc.

Fix the issue by checking md->type.

Signed-off-by: Pingfan Liu <piliu@redhat.com>
Cc: Ard Biesheuvel <ardb@kernel.org>
To: linux-efi@vger.kernel.org
---
 drivers/firmware/efi/libstub/randomalloc.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c
index c41e7b2091cdd..7304e767688f2 100644
--- a/drivers/firmware/efi/libstub/randomalloc.c
+++ b/drivers/firmware/efi/libstub/randomalloc.c
@@ -79,6 +79,8 @@ efi_status_t efi_random_alloc(unsigned long size,
 		efi_memory_desc_t *md = (void *)map->map + map_offset;
 		unsigned long slots;
 
+		if (!(md->type & (EFI_CONVENTIONAL_MEMORY || EFI_PERSISTENT_MEMORY)))
+			continue;
 		slots = get_entry_num_slots(md, size, ilog2(align), alloc_min,
 					    alloc_max);
 		MD_NUM_SLOTS(md) = slots;
@@ -111,6 +113,9 @@ efi_status_t efi_random_alloc(unsigned long size,
 		efi_physical_addr_t target;
 		unsigned long pages;
 
+		if (!(md->type & (EFI_CONVENTIONAL_MEMORY || EFI_PERSISTENT_MEMORY)))
+			continue;
+
 		if (total_mirrored_slots > 0 &&
 		    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
 			continue;
-- 
2.41.0
Re: [RFCv2 1/9] efi/libstub: Ask efi_random_alloc() to skip unusable memory
Posted by Ard Biesheuvel 1 year, 3 months ago
On Mon, 19 Aug 2024 at 16:55, Pingfan Liu <piliu@redhat.com> wrote:
>
> efi_random_alloc() demands EFI_ALLOCATE_ADDRESS when allocate_pages(),
> but the current implement can not ensure the selected target locates
> inside free area, that is to exclude EFI_BOOT_SERVICES_*,
> EFI_RUNTIME_SERVICES_* etc.
>
> Fix the issue by checking md->type.
>
> Signed-off-by: Pingfan Liu <piliu@redhat.com>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> To: linux-efi@vger.kernel.org
> ---
>  drivers/firmware/efi/libstub/randomalloc.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c
> index c41e7b2091cdd..7304e767688f2 100644
> --- a/drivers/firmware/efi/libstub/randomalloc.c
> +++ b/drivers/firmware/efi/libstub/randomalloc.c
> @@ -79,6 +79,8 @@ efi_status_t efi_random_alloc(unsigned long size,
>                 efi_memory_desc_t *md = (void *)map->map + map_offset;
>                 unsigned long slots;
>
> +               if (!(md->type & (EFI_CONVENTIONAL_MEMORY || EFI_PERSISTENT_MEMORY)))
> +                       continue;

This is wrong in 3 different ways:
- md->type is not a bitmask
- || is not bitwise but boolean
- get_entry_num_slots() ignores all memory types except
EFI_CONVENTIONAL_MEMORY anyway.

So what exactly are you trying to fix here?


>                 slots = get_entry_num_slots(md, size, ilog2(align), alloc_min,
>                                             alloc_max);
>                 MD_NUM_SLOTS(md) = slots;
> @@ -111,6 +113,9 @@ efi_status_t efi_random_alloc(unsigned long size,
>                 efi_physical_addr_t target;
>                 unsigned long pages;
>
> +               if (!(md->type & (EFI_CONVENTIONAL_MEMORY || EFI_PERSISTENT_MEMORY)))
> +                       continue;
> +
>                 if (total_mirrored_slots > 0 &&
>                     !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
>                         continue;
> --
> 2.41.0
>
Re: [RFCv2 1/9] efi/libstub: Ask efi_random_alloc() to skip unusable memory
Posted by Jarkko Sakkinen 1 year, 4 months ago
On Mon Aug 19, 2024 at 5:53 PM EEST, Pingfan Liu wrote:
> efi_random_alloc() demands EFI_ALLOCATE_ADDRESS when allocate_pages(),
> but the current implement can not ensure the selected target locates
> inside free area, that is to exclude EFI_BOOT_SERVICES_*,
> EFI_RUNTIME_SERVICES_* etc.
>
> Fix the issue by checking md->type.

If it is a fix shouldn't this have a fixes tag?

>
> Signed-off-by: Pingfan Liu <piliu@redhat.com>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> To: linux-efi@vger.kernel.org
> ---
>  drivers/firmware/efi/libstub/randomalloc.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c
> index c41e7b2091cdd..7304e767688f2 100644
> --- a/drivers/firmware/efi/libstub/randomalloc.c
> +++ b/drivers/firmware/efi/libstub/randomalloc.c
> @@ -79,6 +79,8 @@ efi_status_t efi_random_alloc(unsigned long size,
>  		efi_memory_desc_t *md = (void *)map->map + map_offset;
>  		unsigned long slots;
>  

I'd add this inline comment:

/* Skip "unconventional" memory: */

> +		if (!(md->type & (EFI_CONVENTIONAL_MEMORY || EFI_PERSISTENT_MEMORY)))
> +			continue;
>  		slots = get_entry_num_slots(md, size, ilog2(align), alloc_min,
>  					    alloc_max);
>  		MD_NUM_SLOTS(md) = slots;
> @@ -111,6 +113,9 @@ efi_status_t efi_random_alloc(unsigned long size,
>  		efi_physical_addr_t target;
>  		unsigned long pages;
>  
> +		if (!(md->type & (EFI_CONVENTIONAL_MEMORY || EFI_PERSISTENT_MEMORY)))
> +			continue;
> +
>  		if (total_mirrored_slots > 0 &&
>  		    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
>  			continue;

BR, Jarkko
Re: [RFCv2 1/9] efi/libstub: Ask efi_random_alloc() to skip unusable memory
Posted by Pingfan Liu 1 year, 4 months ago
On Tue, Aug 20, 2024 at 2:00 AM Jarkko Sakkinen <jarkko@kernel.org> wrote:
>
> On Mon Aug 19, 2024 at 5:53 PM EEST, Pingfan Liu wrote:
> > efi_random_alloc() demands EFI_ALLOCATE_ADDRESS when allocate_pages(),
> > but the current implement can not ensure the selected target locates
> > inside free area, that is to exclude EFI_BOOT_SERVICES_*,
> > EFI_RUNTIME_SERVICES_* etc.
> >
> > Fix the issue by checking md->type.
>
> If it is a fix shouldn't this have a fixes tag?
>
Yes, I will supplement the following in the next version
Fixes: 2ddbfc81eac8 ("efi: stub: add implementation of efi_random_alloc()")

> >
> > Signed-off-by: Pingfan Liu <piliu@redhat.com>
> > Cc: Ard Biesheuvel <ardb@kernel.org>
> > To: linux-efi@vger.kernel.org
> > ---
> >  drivers/firmware/efi/libstub/randomalloc.c | 5 +++++
> >  1 file changed, 5 insertions(+)
> >
> > diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c
> > index c41e7b2091cdd..7304e767688f2 100644
> > --- a/drivers/firmware/efi/libstub/randomalloc.c
> > +++ b/drivers/firmware/efi/libstub/randomalloc.c
> > @@ -79,6 +79,8 @@ efi_status_t efi_random_alloc(unsigned long size,
> >               efi_memory_desc_t *md = (void *)map->map + map_offset;
> >               unsigned long slots;
> >
>
> I'd add this inline comment:
>
> /* Skip "unconventional" memory: */
>

Adopt.

Thanks for your kind review.

Best Regards,

Pingfan

> > +             if (!(md->type & (EFI_CONVENTIONAL_MEMORY || EFI_PERSISTENT_MEMORY)))
> > +                     continue;
> >               slots = get_entry_num_slots(md, size, ilog2(align), alloc_min,
> >                                           alloc_max);
> >               MD_NUM_SLOTS(md) = slots;
> > @@ -111,6 +113,9 @@ efi_status_t efi_random_alloc(unsigned long size,
> >               efi_physical_addr_t target;
> >               unsigned long pages;
> >
> > +             if (!(md->type & (EFI_CONVENTIONAL_MEMORY || EFI_PERSISTENT_MEMORY)))
> > +                     continue;
> > +
> >               if (total_mirrored_slots > 0 &&
> >                   !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
> >                       continue;
>
> BR, Jarkko
>