Skip to contentSkip to conversation

x86/sgx: Use vmalloc_array() instead of vmalloc()

PATCH RESEND Superseded Thorsten Blum 2 participants 4 messages Oct 26, 2024 – Oct 28, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 x86/sgx: Use vmalloc_array() instead of vmalloc()

Use vmalloc_array() instead of vmalloc() to calculate the number of bytes to allocate.

---
 arch/x86/kernel/cpu/sgx/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/main.c +1 -1
index 9ace84486499..1a59e5956f4b 100644
@@ -630,7 +630,7 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
 	if (!section->virt_addr)
 		return false;
 
-	section->pages = vmalloc(nr_pages * sizeof(struct sgx_epc_page));
+	section->pages = vmalloc_array(nr_pages, sizeof(struct sgx_epc_page));
 	if (!section->pages) {
 		memunmap(section->virt_addr);
 		return false;
Signature · 1 line
2.47.0
Acked-by on v1 1/1
#1 lore raw
Quoted 2 lines · Thorsten Blum

Use vmalloc_array() instead of vmalloc() to calculate the number of bytes to allocate.

This says nothing about _why_. Is it because we want to take advantage of the multiplication overflow check inside the vmalloc_array()?

I don't know whether it is implied we should always use vmalloc_array() for array allocation like this, i.e., when we see vmalloc() is used for array allocation in the kernel we can just write a patch to replace it with vmalloc_array() and send to upstream.

I am fine with the code change, though. So if you can add one more sentence to explain why (it's always good to do so), feel free to add:

Quoted 20 lines
---
  arch/x86/kernel/cpu/sgx/main.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/main.c +1 -1
index 9ace84486499..1a59e5956f4b 100644
@@ -630,7 +630,7 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
  	if (!section->virt_addr)
  		return false;
 
-	section->pages = vmalloc(nr_pages * sizeof(struct sgx_epc_page));
+	section->pages = vmalloc_array(nr_pages, sizeof(struct sgx_epc_page));
  	if (!section->pages) {
  		memunmap(section->virt_addr);
  		return false;
#2 lore raw
Quoted 7 lines · Kai
Quoted 2 lines · Thorsten Blum

Use vmalloc_array() instead of vmalloc() to calculate the number of bytes to allocate.

This says nothing about _why_. Is it because we want to take advantage of the multiplication overflow check inside the vmalloc_array()?

I don't know whether it is implied we should always use vmalloc_array() for array allocation like this, i.e., when we see vmalloc() is used for array allocation in the kernel we can just write a patch to replace it with vmalloc_array() and send to upstream.

It's discouraged to use open-coded arithmetic in allocator arguments:

https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

Happy to add this, but I assumed it's obvious.

#3 lore raw
Quoted 7 lines · Thorsten Blum
Quoted 6 lines · Kai
Quoted 2 lines · Thorsten Blum

Use vmalloc_array() instead of vmalloc() to calculate the number of bytes to allocate.

This says nothing about _why_. Is it because we want to take advantage of the multiplication overflow check inside the vmalloc_array()?

Quoted 7 lines
Quoted 1 line

I don't know whether it is implied we should always use vmalloc_array() for array allocation like this, i.e., when we see vmalloc() is used for array allocation in the kernel we can just write a patch to replace it with vmalloc_array() and send to upstream.

It's discouraged to use open-coded arithmetic in allocator arguments:

https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

Happy to add this, but I assumed it's obvious.

Thanks for the info.