[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20221018090858.never.941-kees@kernel.org>
Date: Tue, 18 Oct 2022 02:10:11 -0700
From: Kees Cook <keescook@...omium.org>
To: Sumit Semwal <sumit.semwal@...aro.org>
Cc: Kees Cook <keescook@...omium.org>, linux-media@...r.kernel.org,
dri-devel@...ts.freedesktop.org, linaro-mm-sig@...ts.linaro.org,
Christian König <christian.koenig@....com>,
linux-kernel@...r.kernel.org, linux-hardening@...r.kernel.org
Subject: [PATCH v3] dma-buf: 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.
Cc: Sumit Semwal <sumit.semwal@...aro.org>
Cc: linux-media@...r.kernel.org
Cc: dri-devel@...ts.freedesktop.org
Cc: linaro-mm-sig@...ts.linaro.org
Reviewed-by: Christian König <christian.koenig@....com>
Signed-off-by: Kees Cook <keescook@...omium.org>
---
v3: added reviewed-by, rebase to v6.1-rc1
v2: https://lore.kernel.org/lkml/20220923202822.2667581-9-keescook@chromium.org/
---
drivers/dma-buf/dma-resv.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
index e3885c90a3ac..1c76aed8e262 100644
--- a/drivers/dma-buf/dma-resv.c
+++ b/drivers/dma-buf/dma-resv.c
@@ -98,12 +98,17 @@ static void dma_resv_list_set(struct dma_resv_list *list,
static struct dma_resv_list *dma_resv_list_alloc(unsigned int max_fences)
{
struct dma_resv_list *list;
+ size_t size;
- list = kmalloc(struct_size(list, table, max_fences), GFP_KERNEL);
+ /* Round up to the next kmalloc bucket size. */
+ size = kmalloc_size_roundup(struct_size(list, table, max_fences));
+
+ list = kmalloc(size, GFP_KERNEL);
if (!list)
return NULL;
- list->max_fences = (ksize(list) - offsetof(typeof(*list), table)) /
+ /* Given the resulting bucket size, recalculated max_fences. */
+ list->max_fences = (size - offsetof(typeof(*list), table)) /
sizeof(*list->table);
return list;
--
2.34.1
Powered by blists - more mailing lists