Skip to content

Commit 809d9c7

Browse files
committed
dma-buf: Move dma_buf_attach() to dynamic locking specification
Move dma-buf attachment API functions to the dynamic locking specification by taking the reservation lock around the mapping operations. The strict locking convention prevents deadlock situations for dma-buf importers and exporters. Acked-by: Sumit Semwal <[email protected]> Reviewed-by: Christian König <[email protected]> Signed-off-by: Dmitry Osipenko <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 34c7797 commit 809d9c7

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

drivers/dma-buf/dma-buf.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,8 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
859859
dma_buf_is_dynamic(dmabuf)) {
860860
struct sg_table *sgt;
861861

862+
dma_resv_lock(attach->dmabuf->resv, NULL);
862863
if (dma_buf_is_dynamic(attach->dmabuf)) {
863-
dma_resv_lock(attach->dmabuf->resv, NULL);
864864
ret = dmabuf->ops->pin(attach);
865865
if (ret)
866866
goto err_unlock;
@@ -873,8 +873,7 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
873873
ret = PTR_ERR(sgt);
874874
goto err_unpin;
875875
}
876-
if (dma_buf_is_dynamic(attach->dmabuf))
877-
dma_resv_unlock(attach->dmabuf->resv);
876+
dma_resv_unlock(attach->dmabuf->resv);
878877
attach->sgt = sgt;
879878
attach->dir = DMA_BIDIRECTIONAL;
880879
}
@@ -890,8 +889,7 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
890889
dmabuf->ops->unpin(attach);
891890

892891
err_unlock:
893-
if (dma_buf_is_dynamic(attach->dmabuf))
894-
dma_resv_unlock(attach->dmabuf->resv);
892+
dma_resv_unlock(attach->dmabuf->resv);
895893

896894
dma_buf_detach(dmabuf, attach);
897895
return ERR_PTR(ret);
@@ -937,21 +935,19 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
937935
if (WARN_ON(!dmabuf || !attach))
938936
return;
939937

938+
dma_resv_lock(attach->dmabuf->resv, NULL);
939+
940940
if (attach->sgt) {
941-
if (dma_buf_is_dynamic(attach->dmabuf))
942-
dma_resv_lock(attach->dmabuf->resv, NULL);
943941

944942
__unmap_dma_buf(attach, attach->sgt, attach->dir);
945943

946-
if (dma_buf_is_dynamic(attach->dmabuf)) {
944+
if (dma_buf_is_dynamic(attach->dmabuf))
947945
dmabuf->ops->unpin(attach);
948-
dma_resv_unlock(attach->dmabuf->resv);
949-
}
950946
}
951-
952-
dma_resv_lock(dmabuf->resv, NULL);
953947
list_del(&attach->node);
948+
954949
dma_resv_unlock(dmabuf->resv);
950+
955951
if (dmabuf->ops->detach)
956952
dmabuf->ops->detach(dmabuf, attach);
957953

0 commit comments

Comments
 (0)