Skip to content

Commit 440d0f1

Browse files
dma-buf: add dma_fence_chain_alloc/free v3
Add a common allocation helper. Cleaning up the mix of kzalloc/kmalloc and some unused code in the selftest. v2: polish kernel doc a bit v3: polish kernel doc even a bit more Signed-off-by: Christian König <[email protected]> Reviewed-by: Daniel Vetter <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 9c61e78 commit 440d0f1

File tree

6 files changed

+38
-25
lines changed

6 files changed

+38
-25
lines changed

drivers/dma-buf/st-dma-fence-chain.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,20 @@ static struct dma_fence *mock_fence(void)
5858
return &f->base;
5959
}
6060

61-
static inline struct mock_chain {
62-
struct dma_fence_chain base;
63-
} *to_mock_chain(struct dma_fence *f) {
64-
return container_of(f, struct mock_chain, base.base);
65-
}
66-
6761
static struct dma_fence *mock_chain(struct dma_fence *prev,
6862
struct dma_fence *fence,
6963
u64 seqno)
7064
{
71-
struct mock_chain *f;
65+
struct dma_fence_chain *f;
7266

73-
f = kmalloc(sizeof(*f), GFP_KERNEL);
67+
f = dma_fence_chain_alloc();
7468
if (!f)
7569
return NULL;
7670

77-
dma_fence_chain_init(&f->base,
78-
dma_fence_get(prev),
79-
dma_fence_get(fence),
71+
dma_fence_chain_init(f, dma_fence_get(prev), dma_fence_get(fence),
8072
seqno);
8173

82-
return &f->base.base;
74+
return &f->base;
8375
}
8476

8577
static int sanitycheck(void *arg)

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,15 +1109,15 @@ static int amdgpu_cs_process_syncobj_timeline_out_dep(struct amdgpu_cs_parser *p
11091109

11101110
dep->chain = NULL;
11111111
if (syncobj_deps[i].point) {
1112-
dep->chain = kmalloc(sizeof(*dep->chain), GFP_KERNEL);
1112+
dep->chain = dma_fence_chain_alloc();
11131113
if (!dep->chain)
11141114
return -ENOMEM;
11151115
}
11161116

11171117
dep->syncobj = drm_syncobj_find(p->filp,
11181118
syncobj_deps[i].handle);
11191119
if (!dep->syncobj) {
1120-
kfree(dep->chain);
1120+
dma_fence_chain_free(dep->chain);
11211121
return -EINVAL;
11221122
}
11231123
dep->point = syncobj_deps[i].point;

drivers/gpu/drm/drm_syncobj.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ static int drm_syncobj_transfer_to_timeline(struct drm_file *file_private,
861861
&fence);
862862
if (ret)
863863
goto err;
864-
chain = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL);
864+
chain = dma_fence_chain_alloc();
865865
if (!chain) {
866866
ret = -ENOMEM;
867867
goto err1;
@@ -1402,10 +1402,10 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,
14021402
goto err_points;
14031403
}
14041404
for (i = 0; i < args->count_handles; i++) {
1405-
chains[i] = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL);
1405+
chains[i] = dma_fence_chain_alloc();
14061406
if (!chains[i]) {
14071407
for (j = 0; j < i; j++)
1408-
kfree(chains[j]);
1408+
dma_fence_chain_free(chains[j]);
14091409
ret = -ENOMEM;
14101410
goto err_chains;
14111411
}

drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,7 +2983,7 @@ __free_fence_array(struct eb_fence *fences, unsigned int n)
29832983
while (n--) {
29842984
drm_syncobj_put(ptr_mask_bits(fences[n].syncobj, 2));
29852985
dma_fence_put(fences[n].dma_fence);
2986-
kfree(fences[n].chain_fence);
2986+
dma_fence_chain_free(fences[n].chain_fence);
29872987
}
29882988
kvfree(fences);
29892989
}
@@ -3097,9 +3097,7 @@ add_timeline_fence_array(struct i915_execbuffer *eb,
30973097
return -EINVAL;
30983098
}
30993099

3100-
f->chain_fence =
3101-
kmalloc(sizeof(*f->chain_fence),
3102-
GFP_KERNEL);
3100+
f->chain_fence = dma_fence_chain_alloc();
31033101
if (!f->chain_fence) {
31043102
drm_syncobj_put(syncobj);
31053103
dma_fence_put(fence);

drivers/gpu/drm/msm/msm_gem_submit.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,7 @@ static struct msm_submit_post_dep *msm_parse_post_deps(struct drm_device *dev,
586586
break;
587587
}
588588

589-
post_deps[i].chain =
590-
kmalloc(sizeof(*post_deps[i].chain),
591-
GFP_KERNEL);
589+
post_deps[i].chain = dma_fence_chain_alloc();
592590
if (!post_deps[i].chain) {
593591
ret = -ENOMEM;
594592
break;
@@ -605,7 +603,7 @@ static struct msm_submit_post_dep *msm_parse_post_deps(struct drm_device *dev,
605603

606604
if (ret) {
607605
for (j = 0; j <= i; ++j) {
608-
kfree(post_deps[j].chain);
606+
dma_fence_chain_free(post_deps[j].chain);
609607
if (post_deps[j].syncobj)
610608
drm_syncobj_put(post_deps[j].syncobj);
611609
}

include/linux/dma-fence-chain.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <linux/dma-fence.h>
1414
#include <linux/irq_work.h>
15+
#include <linux/slab.h>
1516

1617
/**
1718
* struct dma_fence_chain - fence to represent an node of a fence chain
@@ -66,6 +67,30 @@ to_dma_fence_chain(struct dma_fence *fence)
6667
return container_of(fence, struct dma_fence_chain, base);
6768
}
6869

70+
/**
71+
* dma_fence_chain_alloc
72+
*
73+
* Returns a new struct dma_fence_chain object or NULL on failure.
74+
*/
75+
static inline struct dma_fence_chain *dma_fence_chain_alloc(void)
76+
{
77+
return kmalloc(sizeof(struct dma_fence_chain), GFP_KERNEL);
78+
};
79+
80+
/**
81+
* dma_fence_chain_free
82+
* @chain: chain node to free
83+
*
84+
* Frees up an allocated but not used struct dma_fence_chain object. This
85+
* doesn't need an RCU grace period since the fence was never initialized nor
86+
* published. After dma_fence_chain_init() has been called the fence must be
87+
* released by calling dma_fence_put(), and not through this function.
88+
*/
89+
static inline void dma_fence_chain_free(struct dma_fence_chain *chain)
90+
{
91+
kfree(chain);
92+
};
93+
6994
/**
7095
* dma_fence_chain_for_each - iterate over all fences in chain
7196
* @iter: current fence

0 commit comments

Comments
 (0)