Merge tag 'drm-fixes-2022-11-25' of git://anongit.freedesktop.org/drm/drm
[sfrench/cifs-2.6.git] / drivers / dma-buf / dma-buf.c
index dd0f83ee505b7f34efec89fae1f06c176ce85213..e6f36c014c4cd25845a8b92e3e9d06f6b571c922 100644 (file)
@@ -15,6 +15,7 @@
 #include <linux/slab.h>
 #include <linux/dma-buf.h>
 #include <linux/dma-fence.h>
+#include <linux/dma-fence-unwrap.h>
 #include <linux/anon_inodes.h>
 #include <linux/export.h>
 #include <linux/debugfs.h>
@@ -391,8 +392,10 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf,
                                     const void __user *user_data)
 {
        struct dma_buf_import_sync_file arg;
-       struct dma_fence *fence;
+       struct dma_fence *fence, *f;
        enum dma_resv_usage usage;
+       struct dma_fence_unwrap iter;
+       unsigned int num_fences;
        int ret = 0;
 
        if (copy_from_user(&arg, user_data, sizeof(arg)))
@@ -411,13 +414,21 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf,
        usage = (arg.flags & DMA_BUF_SYNC_WRITE) ? DMA_RESV_USAGE_WRITE :
                                                   DMA_RESV_USAGE_READ;
 
-       dma_resv_lock(dmabuf->resv, NULL);
+       num_fences = 0;
+       dma_fence_unwrap_for_each(f, &iter, fence)
+               ++num_fences;
 
-       ret = dma_resv_reserve_fences(dmabuf->resv, 1);
-       if (!ret)
-               dma_resv_add_fence(dmabuf->resv, fence, usage);
+       if (num_fences > 0) {
+               dma_resv_lock(dmabuf->resv, NULL);
 
-       dma_resv_unlock(dmabuf->resv);
+               ret = dma_resv_reserve_fences(dmabuf->resv, num_fences);
+               if (!ret) {
+                       dma_fence_unwrap_for_each(f, &iter, fence)
+                               dma_resv_add_fence(dmabuf->resv, f, usage);
+               }
+
+               dma_resv_unlock(dmabuf->resv);
+       }
 
        dma_fence_put(fence);