From patchwork Fri Jul 24 14:43:33 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: 6860901 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id CA545C05AC for ; Fri, 24 Jul 2015 14:44:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D899F205DF for ; Fri, 24 Jul 2015 14:44:00 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 61F0A205DE for ; Fri, 24 Jul 2015 14:43:55 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 01C0D6EE8C; Fri, 24 Jul 2015 07:43:54 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by gabe.freedesktop.org (Postfix) with ESMTP id BAAD56EE8C for ; Fri, 24 Jul 2015 07:43:52 -0700 (PDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga102.jf.intel.com with ESMTP; 24 Jul 2015 07:43:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.15,538,1432623600"; d="scan'208";a="769004968" Received: from irsmsx151.ger.corp.intel.com ([163.33.192.59]) by fmsmga002.fm.intel.com with ESMTP; 24 Jul 2015 07:43:51 -0700 Received: from mwiniars-desk1.igk.intel.com (172.28.173.140) by IRSMSX151.ger.corp.intel.com (163.33.192.59) with Microsoft SMTP Server id 14.3.224.2; Fri, 24 Jul 2015 15:43:50 +0100 From: =?UTF-8?q?Micha=C5=82=20Winiarski?= To: Date: Fri, 24 Jul 2015 16:43:33 +0200 Message-ID: <1437749013-28299-1-git-send-email-michal.winiarski@intel.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1437729886-9308-1-git-send-email-michal.winiarski@intel.com> References: <1437729886-9308-1-git-send-email-michal.winiarski@intel.com> MIME-Version: 1.0 X-Originating-IP: [172.28.173.140] Cc: Thomas Wood Subject: [Intel-gfx] [PATCH i-g-t v2] tests/drm_import_export: Add tests for prime/flink sharing races 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 It is possible to race between unreference of the underlying BO and importing it from prime_fd/name. Verify that the behaviour of libdrm is consistent for prime/flink. v2: more comments in source file, dropped extra whitespace Signed-off-by: Micha? Winiarski Cc: Thomas Wood --- tests/drm_import_export.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/tests/drm_import_export.c b/tests/drm_import_export.c index 57b13dd..e24e0df 100644 --- a/tests/drm_import_export.c +++ b/tests/drm_import_export.c @@ -131,6 +131,108 @@ static void * test_thread(void * par) return NULL; } +#define IMPORT_RACE_LOOPS 100000 + +struct import_race_thread_data { + int prime_fd; + uint32_t flink_name; + unsigned int stop; + pthread_mutex_t mutex; +}; + +/* + * Attempt to import the bo. It is possible that GEM_CLOSE was already called + * in different thread and from i915 point of view the handle is no longer + * valid (thus create_from_prime/name should fail). + */ +static void *import_close_thread(void *data) +{ + struct import_race_thread_data *t = (struct import_race_thread_data *)data; + drm_intel_bo *bo; + pthread_mutex_lock(&t->mutex); + while (!t->stop) { + pthread_mutex_unlock(&t->mutex); + bo = NULL; + if (use_flink) + bo = drm_intel_bo_gem_create_from_name(bufmgr, "buf-shared", t->flink_name); + else { + pthread_mutex_lock(&t->mutex); + if (t->prime_fd != -1) { + bo = drm_intel_bo_gem_create_from_prime(bufmgr, t->prime_fd, 4096); + pthread_mutex_unlock(&t->mutex); + } + else + /* We take the lock right after entering the loop */ + continue; + } + if (bo == NULL) { + /* + * If the bo is NULL it means that we've unreferenced in other + * thread - therefore we should expect ENOENT + */ + igt_assert_eq(errno, ENOENT); + continue; + } + + drm_intel_bo_unreference(bo); + + pthread_mutex_lock(&t->mutex); + } + pthread_mutex_unlock(&t->mutex); + + return NULL; +} + +/* + * It is possible to race between unreference of the underlying BO and importing + * it from prime_fd/name. Verify that the behaviour of libdrm is consistent for + * prime/flink. + */ +static void test_import_close_race(void) +{ + pthread_t t; + unsigned int loops = IMPORT_RACE_LOOPS; + drm_intel_bo *bo; + struct import_race_thread_data t_data; + + memset(&t_data, 0, sizeof(t_data)); + pthread_mutex_init(&t_data.mutex, NULL); + t_data.prime_fd = -1; + + igt_assert_eq(pthread_create(&t, NULL, import_close_thread , &t_data), 0); + + while (loops--) { + bo = drm_intel_bo_alloc(bufmgr, "buf-shared", 4096, 4096); + igt_assert(bo != NULL); + /* + * We setup the test in such way, that create_from_* can race between + * unreference. If we're using prime, prime_fd is always a valid fd. + */ + if (use_flink) + igt_assert_eq(drm_intel_bo_flink(bo, &(t_data.flink_name)), 0); + else { + pthread_mutex_lock(&t_data.mutex); + igt_assert_eq(drm_intel_bo_gem_export_to_prime(bo, &(t_data.prime_fd)), 0); + igt_assert(t_data.prime_fd != -1); + pthread_mutex_unlock(&t_data.mutex); + } + + drm_intel_bo_unreference(bo); + + pthread_mutex_lock(&t_data.mutex); + close(t_data.prime_fd); + t_data.prime_fd = -1; + pthread_mutex_unlock(&t_data.mutex); + } + + pthread_mutex_lock(&t_data.mutex); + t_data.stop = 1; + pthread_mutex_unlock(&t_data.mutex); + + pthread_join(t, NULL); + pthread_mutex_destroy(&t_data.mutex); +} + pthread_t test_thread_id1; pthread_t test_thread_id2; pthread_t test_thread_id3; @@ -153,6 +255,16 @@ igt_main { drm_intel_bufmgr_gem_enable_reuse(bufmgr); } + igt_subtest("import-close-race-flink") { + use_flink = true; + test_import_close_race(); + } + + igt_subtest("import-close-race-prime") { + use_flink = false; + test_import_close_race(); + } + igt_subtest("flink") { use_flink = true;