From patchwork Fri Jul 24 09:22:34 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Winiarski?= X-Patchwork-Id: 6858561 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E37D79F380 for ; Fri, 24 Jul 2015 09:23:04 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0EA49205F1 for ; Fri, 24 Jul 2015 09:23:04 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id ED5D5205EE for ; Fri, 24 Jul 2015 09:23:02 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0A9FF6E1C5; Fri, 24 Jul 2015 02:23:02 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTP id CCE5C6E190 for ; Fri, 24 Jul 2015 02:23:00 -0700 (PDT) Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP; 24 Jul 2015 02:22:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.15,537,1432623600"; d="scan'208";a="529505920" Received: from irsmsx103.ger.corp.intel.com ([163.33.3.157]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2015 02:22:41 -0700 Received: from mwiniars-desk1.igk.intel.com (172.28.173.140) by IRSMSX103.ger.corp.intel.com (163.33.3.157) with Microsoft SMTP Server id 14.3.224.2; Fri, 24 Jul 2015 10:22:38 +0100 From: =?UTF-8?q?Micha=C5=82=20Winiarski?= To: Date: Fri, 24 Jul 2015 11:22:34 +0200 Message-ID: <1437729754-8683-1-git-send-email-michal.winiarski@intel.com> X-Mailer: git-send-email 2.4.3 MIME-Version: 1.0 X-Originating-IP: [172.28.173.140] Cc: =?UTF-8?q?Rafa=C5=82=20Sapa=C5=82a?= Subject: [Intel-gfx] [PATCH libdrm] intel: Serialize drmPrimeFDToHandle with struct_mutex X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Rafa? Sapa?a 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 Signed-off-by: Micha? Winiarski Reviewed-by: Chris Wilson --- 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; }