Skip to contentSkip to conversation

net: ipa: Proactively round up to kmalloc bucket size

PATCH Applied Kees Cook 2 participants 2 messages Oct 18, 2022 – Oct 20, 2022

Subsystems (3)

v3 of 5 Applied 1/1 Latest is v5 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v3 1/1 net: ipa: Proactively round up to kmalloc bucket size

Instead of discovering the kmalloc bucket size _after_ allocation, round up proactively so the allocation is explicitly made for the full size, allowing the compiler to correctly reason about the resulting size of the buffer through the existing __alloc_size() hint.

---
v3: rebase to v6.1-rc1
v2: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/net/ipa/gsi_trans.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/ipa/gsi_trans.c +5 -2
index 26b7f683a3e1..0f52c068c46d 100644
@@ -87,6 +87,7 @@ struct gsi_tre {
 int gsi_trans_pool_init(struct gsi_trans_pool *pool, size_t size, u32 count,
 			u32 max_alloc)
 {
+	size_t alloc_size;
 	void *virt;
 
 	if (!size)
@@ -103,13 +104,15 @@ int gsi_trans_pool_init(struct gsi_trans_pool *pool, size_t size, u32 count,
 	 * If there aren't enough entries starting at the free index,
 	 * we just allocate free entries from the beginning of the pool.
 	 */
-	virt = kcalloc(count + max_alloc - 1, size, GFP_KERNEL);
+	alloc_size = size_mul(count + max_alloc - 1, size);
+	alloc_size = kmalloc_size_roundup(alloc_size);
+	virt = kzalloc(alloc_size, GFP_KERNEL);
 	if (!virt)
 		return -ENOMEM;
 
 	pool->base = virt;
 	/* If the allocator gave us any extra memory, use it */
-	pool->count = ksize(pool->base) / size;
+	pool->count = alloc_size / size;
 	pool->free = 0;
 	pool->max_alloc = max_alloc;
 	pool->size = size;
Signature · 1 line
2.34.1
#1 lore raw
This patch was applied to netdev/net-next.git (master) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (master) by Paolo Abeni <[email protected]>:

Quoted 14 lines · On Tue, 18 Oct 2022 02:28:27 -0700 you wrote:

Instead of discovering the kmalloc bucket size _after_ allocation, round up proactively so the allocation is explicitly made for the full size, allowing the compiler to correctly reason about the resulting size of the buffer through the existing __alloc_size() hint.

Quoted 1 line

[...]

Here is the summary with links:
- [v3] net: ipa: Proactively round up to kmalloc bucket size

    https://git.kernel.org/netdev/net-next/c/36875a063b5e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html