From patchwork Tue Feb 3 19:13:56 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: 5771371 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 5A141BF440 for ; Tue, 3 Feb 2015 19:14:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6DE7C2021A for ; Tue, 3 Feb 2015 19:14:49 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 800D4201FB for ; Tue, 3 Feb 2015 19:14:48 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2ACE889DFC; Tue, 3 Feb 2015 11:14:48 -0800 (PST) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by gabe.freedesktop.org (Postfix) with ESMTP id 8777689DFC for ; Tue, 3 Feb 2015 11:14:46 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga103.jf.intel.com with ESMTP; 03 Feb 2015 11:09:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.09,514,1418112000"; d="scan'208";a="646817424" Received: from irsmsx101.ger.corp.intel.com ([163.33.3.153]) by orsmga001.jf.intel.com with ESMTP; 03 Feb 2015 11:14:10 -0800 Received: from mwiniars-desk1.igk.intel.com (172.28.173.140) by IRSMSX101.ger.corp.intel.com (163.33.3.153) with Microsoft SMTP Server id 14.3.195.1; Tue, 3 Feb 2015 19:14:06 +0000 From: =?UTF-8?q?Micha=C5=82=20Winiarski?= To: Date: Tue, 3 Feb 2015 20:13:56 +0100 Message-ID: <1422990836-4819-1-git-send-email-michal.winiarski@intel.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1422974357-25100-1-git-send-email-michal.winiarski@intel.com> References: <1422974357-25100-1-git-send-email-michal.winiarski@intel.com> MIME-Version: 1.0 X-Originating-IP: [172.28.173.140] Cc: Daniel Vetter Subject: [Intel-gfx] [PATCH v2] tests/gem_userptr_blits: Race between close and invalidate 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=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_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 was possible for invalidate range start mmu notifier callback to race with releasing userptr object. If the object is released prior to taking a spinlock in the callback, we'll encounter a null pointer dereference. v2: Moved expressions inside igt_assert(), added mem barrier (Chris) Cc: Chris Wilson Signed-off-by: Micha? Winiarski Reviewed-by: Chris Wilson --- tests/gem_userptr_blits.c | 71 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/tests/gem_userptr_blits.c b/tests/gem_userptr_blits.c index be2fdf9..9217c2a 100644 --- a/tests/gem_userptr_blits.c +++ b/tests/gem_userptr_blits.c @@ -1179,6 +1179,8 @@ static void test_unmap_cycles(int fd, int expected) test_unmap(fd, expected); } +#define MM_STRESS_LOOPS 100000 + struct stress_thread_data { unsigned int stop; int exit_code; @@ -1211,7 +1213,7 @@ static void test_stress_mm(int fd) { int ret; pthread_t t; - unsigned int loops = 100000; + unsigned int loops = MM_STRESS_LOOPS; uint32_t handle; void *ptr; struct stress_thread_data stdata; @@ -1239,6 +1241,65 @@ static void test_stress_mm(int fd) igt_assert(stdata.exit_code == 0); } +struct userptr_close_thread_data { + int fd; + void *ptr; + bool overlap; + bool stop; + pthread_mutex_t mutex; +}; + +static void *mm_userptr_close_thread(void *data) +{ + struct userptr_close_thread_data *t = (struct userptr_close_thread_data *)data; + int num_handles = t->overlap ? 2 : 1; + + uint32_t handle[num_handles]; + + /* Be pedantic and enforce the required memory barriers */ + pthread_mutex_lock(&t->mutex); + while (!t->stop) { + pthread_mutex_unlock(&t->mutex); + for (int i = 0; i < num_handles; i++) + igt_assert(gem_userptr(t->fd, t->ptr, PAGE_SIZE, 0, &handle[i]) == 0); + for (int i = 0; i < num_handles; i++) + gem_close(t->fd, handle[i]); + pthread_mutex_lock(&t->mutex); + } + pthread_mutex_unlock(&t->mutex); + + return NULL; +} + +static void test_invalidate_close_race(int fd, bool overlap) +{ + pthread_t t; + unsigned int loops = MM_STRESS_LOOPS; + struct userptr_close_thread_data t_data; + + memset(&t_data, 0, sizeof(t_data)); + t_data.fd = fd; + t_data.overlap = overlap; + igt_assert(posix_memalign(&t_data.ptr, PAGE_SIZE, PAGE_SIZE) == 0); + pthread_mutex_init(&t_data.mutex, NULL); + + igt_assert(pthread_create(&t, NULL, mm_userptr_close_thread, &t_data) == 0); + + while (loops--) { + mprotect(t_data.ptr, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC); + mprotect(t_data.ptr, PAGE_SIZE, PROT_READ | PROT_WRITE); + } + + 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); + free(t_data.ptr); +} + unsigned int total_ram; uint64_t aperture_size; int fd, count; @@ -1407,7 +1468,13 @@ int main(int argc, char **argv) test_unmap_after_close(fd); igt_subtest("stress-mm") - test_stress_mm(fd); + test_stress_mm(fd); + + igt_subtest("stress-mm-invalidate-close") + test_invalidate_close_race(fd, false); + + igt_subtest("stress-mm-invalidate-close-overlap") + test_invalidate_close_race(fd, true); igt_subtest("coherency-sync") test_coherency(fd, count);