public inbox for intel-gfx@lists.freedesktop.org
 help / color / mirror / Atom feed
* [PATCH libdrm] intel: Serialize drmPrimeFDToHandle with struct_mutex
@ 2015-07-24  9:22 Michał Winiarski
  2015-07-24  9:24 ` [PATCH i-g-t] tests/drm_import_export: Add tests for prime/flink sharing races Michał Winiarski
  2015-07-24 10:51 ` [PATCH libdrm] intel: Serialize drmPrimeFDToHandle with struct_mutex Chris Wilson
  0 siblings, 2 replies; 7+ messages in thread
From: Michał Winiarski @ 2015-07-24  9:22 UTC (permalink / raw)
  To: intel-gfx; +Cc: Rafał Sapała

From: Rafał Sapała <rafal.a.sapala@intel.com>

It is possible to hit a race condition in create_from_prime, when trying
to import a BO that's currently being freed. In case of prime sharing
we'll succesfully get a handle, but fail on get_tiling call, potentially
confusing the caller (and requiring different locking scheme than with
sharing using flink). Wrap fd_to_handle with struct_mutex to force
a more consistent behaviour between prime/flink, convert fprintf to DBG
when handling errors.

Testcase: igt/drm_import_export/import-close-race-prime
Signed-off-by: Rafał Sapała <rafal.a.sapala@intel.com>
Signed-off-by: Michał Winiarski <michal.winiarski@intel.com>
---
 intel/intel_bufmgr_gem.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/intel/intel_bufmgr_gem.c b/intel/intel_bufmgr_gem.c
index b1c3b3a..ed4ffd2 100644
--- a/intel/intel_bufmgr_gem.c
+++ b/intel/intel_bufmgr_gem.c
@@ -2728,14 +2728,19 @@ drm_intel_bo_gem_create_from_prime(drm_intel_bufmgr *bufmgr, int prime_fd, int s
 	struct drm_i915_gem_get_tiling get_tiling;
 	drmMMListHead *list;
 
+	pthread_mutex_lock(&bufmgr_gem->lock);
 	ret = drmPrimeFDToHandle(bufmgr_gem->fd, prime_fd, &handle);
+	if (ret) {
+		DBG("create_from_prime: failed to obtain handle from fd: %s\n", strerror(errno));
+		pthread_mutex_unlock(&bufmgr_gem->lock);
+		return NULL;
+	}
 
 	/*
 	 * See if the kernel has already returned this buffer to us. Just as
 	 * for named buffers, we must not create two bo's pointing at the same
 	 * kernel object
 	 */
-	pthread_mutex_lock(&bufmgr_gem->lock);
 	for (list = bufmgr_gem->named.next;
 	     list != &bufmgr_gem->named;
 	     list = list->next) {
@@ -2747,12 +2752,6 @@ drm_intel_bo_gem_create_from_prime(drm_intel_bufmgr *bufmgr, int prime_fd, int s
 		}
 	}
 
-	if (ret) {
-	  fprintf(stderr,"ret is %d %d\n", ret, errno);
-	  pthread_mutex_unlock(&bufmgr_gem->lock);
-		return NULL;
-	}
-
 	bo_gem = calloc(1, sizeof(*bo_gem));
 	if (!bo_gem) {
 		pthread_mutex_unlock(&bufmgr_gem->lock);
@@ -2793,6 +2792,7 @@ drm_intel_bo_gem_create_from_prime(drm_intel_bufmgr *bufmgr, int prime_fd, int s
 		       DRM_IOCTL_I915_GEM_GET_TILING,
 		       &get_tiling);
 	if (ret != 0) {
+		DBG("create_from_prime: failed to get tiling: %s\n", strerror(errno));
 		drm_intel_gem_bo_unreference(&bo_gem->bo);
 		return NULL;
 	}
-- 
2.4.3

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-08-21 13:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-24  9:22 [PATCH libdrm] intel: Serialize drmPrimeFDToHandle with struct_mutex Michał Winiarski
2015-07-24  9:24 ` [PATCH i-g-t] tests/drm_import_export: Add tests for prime/flink sharing races Michał Winiarski
2015-07-24 13:28   ` Thomas Wood
2015-07-24 14:43   ` [PATCH i-g-t v2] " Michał Winiarski
2015-07-24 14:57     ` Thomas Wood
2015-07-24 10:51 ` [PATCH libdrm] intel: Serialize drmPrimeFDToHandle with struct_mutex Chris Wilson
2015-08-21 13:46   ` Damien Lespiau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox