Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <christian.koenig@amd.com>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: https://patchwork.freedesktop.org/patch/msgid/20210611120301.10595-3-christian.koenig@amd.com

+38 -25
+4 -12
drivers/dma-buf/st-dma-fence-chain.c
··· 58 58 return &f->base; 59 59 } 60 60 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 - 67 61 static struct dma_fence *mock_chain(struct dma_fence *prev, 68 62 struct dma_fence *fence, 69 63 u64 seqno) 70 64 { 71 - struct mock_chain *f; 65 + struct dma_fence_chain *f; 72 66 73 - f = kmalloc(sizeof(*f), GFP_KERNEL); 67 + f = dma_fence_chain_alloc(); 74 68 if (!f) 75 69 return NULL; 76 70 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), 80 72 seqno); 81 73 82 - return &f->base.base; 74 + return &f->base; 83 75 } 84 76 85 77 static int sanitycheck(void *arg)
+2 -2
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
··· 1109 1109 1110 1110 dep->chain = NULL; 1111 1111 if (syncobj_deps[i].point) { 1112 - dep->chain = kmalloc(sizeof(*dep->chain), GFP_KERNEL); 1112 + dep->chain = dma_fence_chain_alloc(); 1113 1113 if (!dep->chain) 1114 1114 return -ENOMEM; 1115 1115 } ··· 1117 1117 dep->syncobj = drm_syncobj_find(p->filp, 1118 1118 syncobj_deps[i].handle); 1119 1119 if (!dep->syncobj) { 1120 - kfree(dep->chain); 1120 + dma_fence_chain_free(dep->chain); 1121 1121 return -EINVAL; 1122 1122 } 1123 1123 dep->point = syncobj_deps[i].point;
+3 -3
drivers/gpu/drm/drm_syncobj.c
··· 861 861 &fence); 862 862 if (ret) 863 863 goto err; 864 - chain = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL); 864 + chain = dma_fence_chain_alloc(); 865 865 if (!chain) { 866 866 ret = -ENOMEM; 867 867 goto err1; ··· 1402 1402 goto err_points; 1403 1403 } 1404 1404 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(); 1406 1406 if (!chains[i]) { 1407 1407 for (j = 0; j < i; j++) 1408 - kfree(chains[j]); 1408 + dma_fence_chain_free(chains[j]); 1409 1409 ret = -ENOMEM; 1410 1410 goto err_chains; 1411 1411 }
+2 -4
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c
··· 2983 2983 while (n--) { 2984 2984 drm_syncobj_put(ptr_mask_bits(fences[n].syncobj, 2)); 2985 2985 dma_fence_put(fences[n].dma_fence); 2986 - kfree(fences[n].chain_fence); 2986 + dma_fence_chain_free(fences[n].chain_fence); 2987 2987 } 2988 2988 kvfree(fences); 2989 2989 } ··· 3097 3097 return -EINVAL; 3098 3098 } 3099 3099 3100 - f->chain_fence = 3101 - kmalloc(sizeof(*f->chain_fence), 3102 - GFP_KERNEL); 3100 + f->chain_fence = dma_fence_chain_alloc(); 3103 3101 if (!f->chain_fence) { 3104 3102 drm_syncobj_put(syncobj); 3105 3103 dma_fence_put(fence);
+2 -4
drivers/gpu/drm/msm/msm_gem_submit.c
··· 586 586 break; 587 587 } 588 588 589 - post_deps[i].chain = 590 - kmalloc(sizeof(*post_deps[i].chain), 591 - GFP_KERNEL); 589 + post_deps[i].chain = dma_fence_chain_alloc(); 592 590 if (!post_deps[i].chain) { 593 591 ret = -ENOMEM; 594 592 break; ··· 603 605 604 606 if (ret) { 605 607 for (j = 0; j <= i; ++j) { 606 - kfree(post_deps[j].chain); 608 + dma_fence_chain_free(post_deps[j].chain); 607 609 if (post_deps[j].syncobj) 608 610 drm_syncobj_put(post_deps[j].syncobj); 609 611 }
+25
include/linux/dma-fence-chain.h
··· 12 12 13 13 #include <linux/dma-fence.h> 14 14 #include <linux/irq_work.h> 15 + #include <linux/slab.h> 15 16 16 17 /** 17 18 * struct dma_fence_chain - fence to represent an node of a fence chain ··· 66 65 67 66 return container_of(fence, struct dma_fence_chain, base); 68 67 } 68 + 69 + /** 70 + * dma_fence_chain_alloc 71 + * 72 + * Returns a new struct dma_fence_chain object or NULL on failure. 73 + */ 74 + static inline struct dma_fence_chain *dma_fence_chain_alloc(void) 75 + { 76 + return kmalloc(sizeof(struct dma_fence_chain), GFP_KERNEL); 77 + }; 78 + 79 + /** 80 + * dma_fence_chain_free 81 + * @chain: chain node to free 82 + * 83 + * Frees up an allocated but not used struct dma_fence_chain object. This 84 + * doesn't need an RCU grace period since the fence was never initialized nor 85 + * published. After dma_fence_chain_init() has been called the fence must be 86 + * released by calling dma_fence_put(), and not through this function. 87 + */ 88 + static inline void dma_fence_chain_free(struct dma_fence_chain *chain) 89 + { 90 + kfree(chain); 91 + }; 69 92 70 93 /** 71 94 * dma_fence_chain_for_each - iterate over all fences in chain