Skip to contentSkip to conversation

efi/libstub: Cast away type warning in use of max()

PATCH In review Ard Biesheuvel 3 participants 5 messages Mar 26, 2024 – Mar 28, 2024 EFI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi/libstub: Cast away type warning in use of max()

From: Ard Biesheuvel <[email protected]>

Add a missing (u64) cast to alloc_min, which is passed into efi_random_alloc() as unsigned long, while efi_physical_addr_t is u64.

Fixes 3cb4a4827596abc82e ("efi/libstub: fix efi_random_alloc() ...")
---
 drivers/firmware/efi/libstub/randomalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/libstub/randomalloc.c +1 -1
index 7e1852859550..fa81528150fe 100644
@@ -120,7 +120,7 @@ efi_status_t efi_random_alloc(unsigned long size,
 			continue;
 		}
 
-		target = round_up(max(md->phys_addr, alloc_min), align) + target_slot * align;
+		target = round_up(max(md->phys_addr, (u64)alloc_min), align) + target_slot * align;
 		pages = size / EFI_PAGE_SIZE;
 
 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
#1 lore raw
Quoted 2 lines · Ard Biesheuvel

Add a missing (u64) cast to alloc_min, which is passed into efi_random_alloc() as unsigned long, while efi_physical_addr_t is u64.

[...]

drivers/firmware/efi/libstub/randomalloc.c efi_status_t efi_random_alloc(unsigned long size, · line 123

Quoted 8 lines
drivers/firmware/efi/libstub/randomalloc.c +1 -1
@@ -120,7 +120,7 @@ efi_status_t efi_random_alloc(unsigned long size,
 			continue;
 		}
 
-		target = round_up(max(md->phys_addr, alloc_min), align) + target_slot * align;
+		target = round_up(max(md->phys_addr, (u64)alloc_min), align) + target_slot * align;

Why not

    max_t(u64, md->phys_addr, alloc_min)

?

maintainer
#2 lore raw
Quoted 11 lines · Lukas Wunner
Quoted 2 lines · Ard Biesheuvel

Add a missing (u64) cast to alloc_min, which is passed into efi_random_alloc() as unsigned long, while efi_physical_addr_t is u64.

[...]

Quoted 6 lines
drivers/firmware/efi/libstub/randomalloc.c +0 -0
@@ -120,7 +120,7 @@ efi_status_t efi_random_alloc(unsigned long size,
                      continue;
              }
 
Quoted 7 lines

drivers/firmware/efi/libstub/randomalloc.c efi_status_t efi_random_alloc(unsigned long size, · line 123

Quoted 2 lines
drivers/firmware/efi/libstub/randomalloc.c +1 -1
-             target = round_up(max(md->phys_addr, alloc_min), align) + target_slot * align;
+             target = round_up(max(md->phys_addr, (u64)alloc_min), align) + target_slot * align;

Why not

    max_t(u64, md->phys_addr, alloc_min)

Why is that better?

#3 lore raw
Quoted 11 lines · Ard Biesheuvel
Quoted 10 lines · Lukas Wunner
Quoted 2 lines · Ard Biesheuvel

Add a missing (u64) cast to alloc_min, which is passed into efi_random_alloc() as unsigned long, while efi_physical_addr_t is u64.

[...]

Quoted 6 lines
drivers/firmware/efi/libstub/randomalloc.c +0 -0
@@ -120,7 +120,7 @@ efi_status_t efi_random_alloc(unsigned long size,
                      continue;
              }
 
Quoted 8 lines
Quoted 6 lines

drivers/firmware/efi/libstub/randomalloc.c efi_status_t efi_random_alloc(unsigned long size, · line 123

Quoted 2 lines
drivers/firmware/efi/libstub/randomalloc.c +1 -1
-             target = round_up(max(md->phys_addr, alloc_min), align) + target_slot * align;
+             target = round_up(max(md->phys_addr, (u64)alloc_min), align) + target_slot * align;

Why not

    max_t(u64, md->phys_addr, alloc_min)

Why is that better?

It just seems to be the idiomatic way to handle these casts in the kernel.

It's also what checkpatch suggests, so by not using it you risk getting "helpful" fixup patches from the usual suspects.

It's your call buddy. :)

maintainer
#4 lore raw
Quoted 18 lines · Lukas Wunner
Quoted 16 lines · Ard Biesheuvel
Quoted 15 lines · Lukas Wunner
Quoted 2 lines · Ard Biesheuvel

Add a missing (u64) cast to alloc_min, which is passed into efi_random_alloc() as unsigned long, while efi_physical_addr_t is u64.

[...]

drivers/firmware/efi/libstub/randomalloc.c efi_status_t efi_random_alloc(unsigned long size, · line 123

Quoted 8 lines
drivers/firmware/efi/libstub/randomalloc.c +1 -1
@@ -120,7 +120,7 @@ efi_status_t efi_random_alloc(unsigned long size,
                      continue;
              }
 
-             target = round_up(max(md->phys_addr, alloc_min), align) + target_slot * align;
+             target = round_up(max(md->phys_addr, (u64)alloc_min), align) + target_slot * align;

Why not

Quoted 6 lines
Quoted 3 lines
Quoted 1 line
    max_t(u64, md->phys_addr, alloc_min)

Why is that better?

It just seems to be the idiomatic way to handle these casts in the kernel.

In this particular case, I prefer max() with the cast, because it matches the other occurrence, where alloc_min is also used but there it is u64 not unsigned long.

Quoted 3 lines

It's also what checkpatch suggests, so by not using it you risk getting "helpful" fixup patches from the usual suspects.

Ugh yeah good point.

Quoted 1 line

It's your call buddy. :)

Thanks for the head's up