Skip to content

Commit

Permalink
net: ipa: Proactively round up to kmalloc bucket size
Browse files Browse the repository at this point in the history
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.

Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: [email protected]
Reviewed-by: Alex Elder <[email protected]>
Link: https://lore.kernel.org/lkml/[email protected]
Signed-off-by: Kees Cook <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Paolo Abeni <[email protected]>
  • Loading branch information
kees authored and Paolo Abeni committed Oct 20, 2022
1 parent ab3f782 commit 36875a0
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions drivers/net/ipa/gsi_trans.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down

0 comments on commit 36875a0

Please sign in to comment.