summaryrefslogtreecommitdiff
path: root/drivers/dma-buf
diff options
context:
space:
mode:
authorKees Cook <kees@kernel.org>2026-02-21 10:49:23 +0300
committerKees Cook <kees@kernel.org>2026-02-21 12:02:28 +0300
commit69050f8d6d075dc01af7a5f2f550a8067510366f (patch)
treebb265f94d9dfa7876c06a5d9f88673d496a15341 /drivers/dma-buf
parentd39a1d7486d98668dd34aaa6732aad7977c45f5a (diff)
downloadlinux-69050f8d6d075dc01af7a5f2f550a8067510366f.tar.xz
treewide: Replace kmalloc with kmalloc_obj for non-scalar types
This is the result of running the Coccinelle script from scripts/coccinelle/api/kmalloc_objs.cocci. The script is designed to avoid scalar types (which need careful case-by-case checking), and instead replace kmalloc-family calls that allocate struct or union object instances: Single allocations: kmalloc(sizeof(TYPE), ...) are replaced with: kmalloc_obj(TYPE, ...) Array allocations: kmalloc_array(COUNT, sizeof(TYPE), ...) are replaced with: kmalloc_objs(TYPE, COUNT, ...) Flex array allocations: kmalloc(struct_size(PTR, FAM, COUNT), ...) are replaced with: kmalloc_flex(*PTR, FAM, COUNT, ...) (where TYPE may also be *VAR) The resulting allocations no longer return "void *", instead returning "TYPE *". Signed-off-by: Kees Cook <kees@kernel.org>
Diffstat (limited to 'drivers/dma-buf')
-rw-r--r--drivers/dma-buf/dma-buf-mapping.c4
-rw-r--r--drivers/dma-buf/dma-buf.c4
-rw-r--r--drivers/dma-buf/dma-fence-array.c2
-rw-r--r--drivers/dma-buf/dma-fence-unwrap.c2
-rw-r--r--drivers/dma-buf/dma-fence.c4
-rw-r--r--drivers/dma-buf/dma-heap.c2
-rw-r--r--drivers/dma-buf/heaps/cma_heap.c8
-rw-r--r--drivers/dma-buf/heaps/system_heap.c4
-rw-r--r--drivers/dma-buf/st-dma-fence-chain.c8
-rw-r--r--drivers/dma-buf/st-dma-fence-unwrap.c4
-rw-r--r--drivers/dma-buf/st-dma-resv.c2
-rw-r--r--drivers/dma-buf/sw_sync.c4
-rw-r--r--drivers/dma-buf/sync_file.c2
-rw-r--r--drivers/dma-buf/udmabuf.c13
14 files changed, 30 insertions, 33 deletions
diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
index 174677faa577..d9374f2ff0ca 100644
--- a/drivers/dma-buf/dma-buf-mapping.c
+++ b/drivers/dma-buf/dma-buf-mapping.c
@@ -108,7 +108,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
/* This function is supposed to work on MMIO memory only */
return ERR_PTR(-EINVAL);
- dma = kzalloc(sizeof(*dma), GFP_KERNEL);
+ dma = kzalloc_obj(*dma, GFP_KERNEL);
if (!dma)
return ERR_PTR(-ENOMEM);
@@ -119,7 +119,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
*/
break;
case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
- dma->state = kzalloc(sizeof(*dma->state), GFP_KERNEL);
+ dma->state = kzalloc_obj(*dma->state, GFP_KERNEL);
if (!dma->state) {
ret = -ENOMEM;
goto err_free_dma;
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 77555096e4c7..71d5e7e42653 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -866,7 +866,7 @@ static int dma_buf_wrap_sg_table(struct sg_table **sg_table)
* sg_table without copying the page_link and give only the copy back to
* the importer.
*/
- to = kzalloc(sizeof(*to), GFP_KERNEL);
+ to = kzalloc_obj(*to, GFP_KERNEL);
if (!to)
return -ENOMEM;
@@ -1020,7 +1020,7 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
if (WARN_ON(importer_ops && !importer_ops->move_notify))
return ERR_PTR(-EINVAL);
- attach = kzalloc(sizeof(*attach), GFP_KERNEL);
+ attach = kzalloc_obj(*attach, GFP_KERNEL);
if (!attach)
return ERR_PTR(-ENOMEM);
diff --git a/drivers/dma-buf/dma-fence-array.c b/drivers/dma-buf/dma-fence-array.c
index 6657d4b30af9..eb27fcc0aad5 100644
--- a/drivers/dma-buf/dma-fence-array.c
+++ b/drivers/dma-buf/dma-fence-array.c
@@ -179,7 +179,7 @@ struct dma_fence_array *dma_fence_array_alloc(int num_fences)
{
struct dma_fence_array *array;
- return kzalloc(struct_size(array, callbacks, num_fences), GFP_KERNEL);
+ return kzalloc_flex(*array, callbacks, num_fences, GFP_KERNEL);
}
EXPORT_SYMBOL(dma_fence_array_alloc);
diff --git a/drivers/dma-buf/dma-fence-unwrap.c b/drivers/dma-buf/dma-fence-unwrap.c
index a495d8a6c2e3..dc2525b2a03f 100644
--- a/drivers/dma-buf/dma-fence-unwrap.c
+++ b/drivers/dma-buf/dma-fence-unwrap.c
@@ -155,7 +155,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
dma_fence_put(unsignaled);
- array = kmalloc_array(count, sizeof(*array), GFP_KERNEL);
+ array = kmalloc_objs(*array, count, GFP_KERNEL);
if (!array)
return NULL;
diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
index 21c5c30b4f34..97f0f150e49a 100644
--- a/drivers/dma-buf/dma-fence.c
+++ b/drivers/dma-buf/dma-fence.c
@@ -156,7 +156,7 @@ struct dma_fence *dma_fence_allocate_private_stub(ktime_t timestamp)
{
struct dma_fence *fence;
- fence = kzalloc(sizeof(*fence), GFP_KERNEL);
+ fence = kzalloc_obj(*fence, GFP_KERNEL);
if (fence == NULL)
return NULL;
@@ -905,7 +905,7 @@ dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
return 0;
}
- cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
+ cb = kzalloc_objs(struct default_wait_cb, count, GFP_KERNEL);
if (cb == NULL) {
ret = -ENOMEM;
goto err_free_cb;
diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c
index d230ddeb24e0..e0b9eb871c35 100644
--- a/drivers/dma-buf/dma-heap.c
+++ b/drivers/dma-buf/dma-heap.c
@@ -244,7 +244,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
return ERR_PTR(-EINVAL);
}
- heap = kzalloc(sizeof(*heap), GFP_KERNEL);
+ heap = kzalloc_obj(*heap, GFP_KERNEL);
if (!heap)
return ERR_PTR(-ENOMEM);
diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c
index 49cc45fb42dd..06ed40c9d528 100644
--- a/drivers/dma-buf/heaps/cma_heap.c
+++ b/drivers/dma-buf/heaps/cma_heap.c
@@ -74,7 +74,7 @@ static int cma_heap_attach(struct dma_buf *dmabuf,
struct dma_heap_attachment *a;
int ret;
- a = kzalloc(sizeof(*a), GFP_KERNEL);
+ a = kzalloc_obj(*a, GFP_KERNEL);
if (!a)
return -ENOMEM;
@@ -308,7 +308,7 @@ static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,
int ret = -ENOMEM;
pgoff_t pg;
- buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
+ buffer = kzalloc_obj(*buffer, GFP_KERNEL);
if (!buffer)
return ERR_PTR(-ENOMEM);
@@ -346,7 +346,7 @@ static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,
memset(page_address(cma_pages), 0, size);
}
- buffer->pages = kmalloc_array(pagecount, sizeof(*buffer->pages), GFP_KERNEL);
+ buffer->pages = kmalloc_objs(*buffer->pages, pagecount, GFP_KERNEL);
if (!buffer->pages) {
ret = -ENOMEM;
goto free_cma;
@@ -391,7 +391,7 @@ static int __init __add_cma_heap(struct cma *cma, const char *name)
struct dma_heap_export_info exp_info;
struct cma_heap *cma_heap;
- cma_heap = kzalloc(sizeof(*cma_heap), GFP_KERNEL);
+ cma_heap = kzalloc_obj(*cma_heap, GFP_KERNEL);
if (!cma_heap)
return -ENOMEM;
cma_heap->cma = cma;
diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c
index 4049d042afa1..27ba8e55d909 100644
--- a/drivers/dma-buf/heaps/system_heap.c
+++ b/drivers/dma-buf/heaps/system_heap.c
@@ -77,7 +77,7 @@ static int system_heap_attach(struct dma_buf *dmabuf,
struct dma_heap_attachment *a;
int ret;
- a = kzalloc(sizeof(*a), GFP_KERNEL);
+ a = kzalloc_obj(*a, GFP_KERNEL);
if (!a)
return -ENOMEM;
@@ -354,7 +354,7 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
struct page *page, *tmp_page;
int i, ret = -ENOMEM;
- buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
+ buffer = kzalloc_obj(*buffer, GFP_KERNEL);
if (!buffer)
return ERR_PTR(-ENOMEM);
diff --git a/drivers/dma-buf/st-dma-fence-chain.c b/drivers/dma-buf/st-dma-fence-chain.c
index ed4b323886e4..4ae7a6257e57 100644
--- a/drivers/dma-buf/st-dma-fence-chain.c
+++ b/drivers/dma-buf/st-dma-fence-chain.c
@@ -116,13 +116,11 @@ static int fence_chains_init(struct fence_chains *fc, unsigned int count,
unsigned int i;
int err = 0;
- fc->chains = kvmalloc_array(count, sizeof(*fc->chains),
- GFP_KERNEL | __GFP_ZERO);
+ fc->chains = kvmalloc_objs(*fc->chains, count, GFP_KERNEL | __GFP_ZERO);
if (!fc->chains)
return -ENOMEM;
- fc->fences = kvmalloc_array(count, sizeof(*fc->fences),
- GFP_KERNEL | __GFP_ZERO);
+ fc->fences = kvmalloc_objs(*fc->fences, count, GFP_KERNEL | __GFP_ZERO);
if (!fc->fences) {
err = -ENOMEM;
goto err_chains;
@@ -452,7 +450,7 @@ static int find_race(void *arg)
if (err)
return err;
- threads = kmalloc_array(ncpus, sizeof(*threads), GFP_KERNEL);
+ threads = kmalloc_objs(*threads, ncpus, GFP_KERNEL);
if (!threads) {
err = -ENOMEM;
goto err;
diff --git a/drivers/dma-buf/st-dma-fence-unwrap.c b/drivers/dma-buf/st-dma-fence-unwrap.c
index a3be888ae2e8..56802a39874e 100644
--- a/drivers/dma-buf/st-dma-fence-unwrap.c
+++ b/drivers/dma-buf/st-dma-fence-unwrap.c
@@ -32,7 +32,7 @@ static struct dma_fence *__mock_fence(u64 context, u64 seqno)
{
struct mock_fence *f;
- f = kmalloc(sizeof(*f), GFP_KERNEL);
+ f = kmalloc_obj(*f, GFP_KERNEL);
if (!f)
return NULL;
@@ -54,7 +54,7 @@ static struct dma_fence *mock_array(unsigned int num_fences, ...)
va_list valist;
int i;
- fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL);
+ fences = kzalloc_objs(*fences, num_fences, GFP_KERNEL);
if (!fences)
goto error_put;
diff --git a/drivers/dma-buf/st-dma-resv.c b/drivers/dma-buf/st-dma-resv.c
index 15dbea1462ed..fad024820f85 100644
--- a/drivers/dma-buf/st-dma-resv.c
+++ b/drivers/dma-buf/st-dma-resv.c
@@ -27,7 +27,7 @@ static struct dma_fence *alloc_fence(void)
{
struct dma_fence *f;
- f = kmalloc(sizeof(*f), GFP_KERNEL);
+ f = kmalloc_obj(*f, GFP_KERNEL);
if (!f)
return NULL;
diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
index 6f09d13be6b6..d04f479edaee 100644
--- a/drivers/dma-buf/sw_sync.c
+++ b/drivers/dma-buf/sw_sync.c
@@ -101,7 +101,7 @@ static struct sync_timeline *sync_timeline_create(const char *name)
{
struct sync_timeline *obj;
- obj = kzalloc(sizeof(*obj), GFP_KERNEL);
+ obj = kzalloc_obj(*obj, GFP_KERNEL);
if (!obj)
return NULL;
@@ -252,7 +252,7 @@ static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
{
struct sync_pt *pt;
- pt = kzalloc(sizeof(*pt), GFP_KERNEL);
+ pt = kzalloc_obj(*pt, GFP_KERNEL);
if (!pt)
return NULL;
diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
index 747e377fb954..ad2c3e9e23bc 100644
--- a/drivers/dma-buf/sync_file.c
+++ b/drivers/dma-buf/sync_file.c
@@ -24,7 +24,7 @@ static struct sync_file *sync_file_alloc(void)
{
struct sync_file *sync_file;
- sync_file = kzalloc(sizeof(*sync_file), GFP_KERNEL);
+ sync_file = kzalloc_obj(*sync_file, GFP_KERNEL);
if (!sync_file)
return NULL;
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 40399c26e6be..816ee45f4f99 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -115,7 +115,7 @@ static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
dma_resv_assert_held(buf->resv);
- pages = kvmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL);
+ pages = kvmalloc_objs(*pages, ubuf->pagecount, GFP_KERNEL);
if (!pages)
return -ENOMEM;
@@ -150,7 +150,7 @@ static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf,
unsigned int i = 0;
int ret;
- sg = kzalloc(sizeof(*sg), GFP_KERNEL);
+ sg = kzalloc_obj(*sg, GFP_KERNEL);
if (!sg)
return ERR_PTR(-ENOMEM);
@@ -207,17 +207,16 @@ static void unpin_all_folios(struct udmabuf *ubuf)
static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt)
{
- ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL);
+ ubuf->folios = kvmalloc_objs(*ubuf->folios, pgcnt, GFP_KERNEL);
if (!ubuf->folios)
return -ENOMEM;
- ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL);
+ ubuf->offsets = kvzalloc_objs(*ubuf->offsets, pgcnt, GFP_KERNEL);
if (!ubuf->offsets)
return -ENOMEM;
- ubuf->pinned_folios = kvmalloc_array(pgcnt,
- sizeof(*ubuf->pinned_folios),
- GFP_KERNEL);
+ ubuf->pinned_folios = kvmalloc_objs(*ubuf->pinned_folios, pgcnt,
+ GFP_KERNEL);
if (!ubuf->pinned_folios)
return -ENOMEM;