Skip to contentSkip to conversation

etnaviv: prevent deadlock in error path

PATCH Superseded Philipp Zabel 3 participants 5 messages Aug 21, 2017

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 etnaviv: prevent deadlock in error path

If drmPrimeFDToHandle fails in etna_bo_from_dmabuf, the function must not return with the table_lock mutex held. There is no reason to call drmPrimeFDToHandle under that lock, so just take the lock after trying to obtain the handle.

Based on ceb70a6b1015 ("freedreno: prevent deadlock in error path").

---
 etnaviv/etnaviv_bo.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
etnaviv/etnaviv_bo.c +2 -2
index 75669572..6c07d27f 100644
@@ -225,13 +225,13 @@ struct etna_bo *etna_bo_from_dmabuf(struct etna_device *dev, int fd)
 	int ret, size;
 	uint32_t handle;
 
-	pthread_mutex_lock(&table_lock);
-
 	ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
 	if (ret) {
 		return NULL;
 	}
 
+	pthread_mutex_lock(&table_lock);
+
 	bo = lookup_bo(dev->handle_table, handle);
 	if (bo)
 		goto out_unlock;
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 2/2 freedreno: simplify locking in fd_bo_from_dmabuf

There should be no reason to call drmPrimeFDToHandle under the table_lock mutex. Instead of taking the lock early and releasing it in two places, just take it after trying to obtain the handle.

---
 freedreno/freedreno_bo.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
freedreno/freedreno_bo.c +2 -2
index 7f8ea59c..2eb38e45 100644
@@ -135,13 +135,13 @@ fd_bo_from_dmabuf(struct fd_device *dev, int fd)
 	uint32_t handle;
 	struct fd_bo *bo;
 
-	pthread_mutex_lock(&table_lock);
 	ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
 	if (ret) {
-		pthread_mutex_unlock(&table_lock);
 		return NULL;
 	}
 
+	pthread_mutex_lock(&table_lock);
+
 	bo = lookup_bo(dev->handle_table, handle);
 	if (bo)
 		goto out_unlock;
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/2
#2 lore raw
In reply to Philipp Zabel

2017-08-21 12:57 GMT+02:00 Philipp Zabel <[email protected]>:

Quoted 8 lines

If drmPrimeFDToHandle fails in etna_bo_from_dmabuf, the function must not return with the table_lock mutex held. There is no reason to call drmPrimeFDToHandle under that lock, so just take the lock after trying to obtain the handle.

Based on ceb70a6b1015 ("freedreno: prevent deadlock in error path").

Quoted 27 lines
---
 etnaviv/etnaviv_bo.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
etnaviv/etnaviv_bo.c +2 -3
index 75669572..6c07d27f 100644
@@ -225,13 +225,13 @@ struct etna_bo *etna_bo_from_dmabuf(struct etna_device *dev, int fd)
        int ret, size;
        uint32_t handle;
 
-       pthread_mutex_lock(&table_lock);
-
        ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
        if (ret) {
                return NULL;
        }
 
+       pthread_mutex_lock(&table_lock);
+
        bo = lookup_bo(dev->handle_table, handle);
        if (bo)
                goto out_unlock;
--

2.11.0

Quoted 4 lines

_______________________________________________
etnaviv mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/etnaviv

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw
In reply to Philipp Zabel
Quoted 7 lines · Philipp Zabel

If drmPrimeFDToHandle fails in etna_bo_from_dmabuf, the function must not return with the table_lock mutex held. There is no reason to call drmPrimeFDToHandle under that lock, so just take the lock after trying to obtain the handle.

Based on ceb70a6b1015 ("freedreno: prevent deadlock in error path").

Can you skim through cf40cf05a4d7f3945d534790e7768a048adc3ab0 and it's commit message.

Doesn't a similar issue apply here as well?

-Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
Quoted 6 lines · Emil Velikov
Quoted 6 lines · Philipp Zabel

If drmPrimeFDToHandle fails in etna_bo_from_dmabuf, the function must not return with the table_lock mutex held. There is no reason to call drmPrimeFDToHandle under that lock, so just take the lock after trying to obtain the handle.

Quoted 7 lines
Quoted 2 lines

Based on ceb70a6b1015 ("freedreno: prevent deadlock in error path").

Can you skim through cf40cf05a4d7f3945d534790e7768a048adc3ab0 and it's commit message.

Doesn't a similar issue apply here as well?

I think you are right. If fd_bo_del is called at the same time as fd_bo_from_dmabuf with an fd that maps to the same BO, bo_del may call GEM_CLOSE with the same handle that drmPrimeFDToHandle just returned, possibly invalidating it before fd_bo_from_dmabuf can take the lock.

regards
Philipp
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel