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]>
---
 drivers/net/ipa/gsi_trans.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ipa/gsi_trans.c b/drivers/net/ipa/gsi_trans.c
index 18e7e8c405be..eeec149b5d89 100644
--- a/drivers/net/ipa/gsi_trans.c
+++ b/drivers/net/ipa/gsi_trans.c
@@ -88,6 +88,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)
@@ -104,13 +105,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;
-- 
2.34.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to