From patchwork Wed Sep 27 11:51:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Boris BREZILLON X-Patchwork-Id: 9973855 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A9E24602D6 for ; Wed, 27 Sep 2017 11:51:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9826C28ABE for ; Wed, 27 Sep 2017 11:51:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8CECA28CE2; Wed, 27 Sep 2017 11:51:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0750428ABE for ; Wed, 27 Sep 2017 11:51:37 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7C5C06E6EF; Wed, 27 Sep 2017 11:51:35 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mail.free-electrons.com (mail.free-electrons.com [62.4.15.54]) by gabe.freedesktop.org (Postfix) with ESMTP id 08A186E6E6 for ; Wed, 27 Sep 2017 11:51:34 +0000 (UTC) Received: by mail.free-electrons.com (Postfix, from userid 110) id 90C62208CE; Wed, 27 Sep 2017 13:51:32 +0200 (CEST) Received: from localhost.localdomain (LStLambert-657-1-97-87.w90-63.abo.wanadoo.fr [90.63.216.87]) by mail.free-electrons.com (Postfix) with ESMTPSA id 429EF208B3; Wed, 27 Sep 2017 13:51:22 +0200 (CEST) From: Boris Brezillon To: Intel GFX discussion Date: Wed, 27 Sep 2017 13:51:18 +0200 Message-Id: <20170927115118.23170-3-boris.brezillon@free-electrons.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170927115118.23170-1-boris.brezillon@free-electrons.com> References: <20170927115118.23170-1-boris.brezillon@free-electrons.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t 2/2] igt: Add VC4 purgeable BO tests 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-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Boris Brezillon --- tests/Makefile.sources | 1 + tests/vc4_purgeable_bo.c | 274 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 275 insertions(+) create mode 100644 tests/vc4_purgeable_bo.c diff --git a/tests/Makefile.sources b/tests/Makefile.sources index 0adc28a014d2..c78ac9d27921 100644 --- a/tests/Makefile.sources +++ b/tests/Makefile.sources @@ -8,6 +8,7 @@ VC4_TESTS = \ vc4_create_bo \ vc4_dmabuf_poll \ vc4_lookup_fail \ + vc4_purgeable_bo \ vc4_wait_bo \ vc4_wait_seqno \ $(NULL) diff --git a/tests/vc4_purgeable_bo.c b/tests/vc4_purgeable_bo.c new file mode 100644 index 000000000000..e3eaf0c24563 --- /dev/null +++ b/tests/vc4_purgeable_bo.c @@ -0,0 +1,274 @@ +/* + * Copyright © 2017 Broadcom + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "igt.h" +#include "igt_vc4.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "vc4_drm.h" + +struct igt_vc4_bo { + struct igt_list node; + int handle; + void *map; + size_t size; +}; + +static jmp_buf jmp; + +static void __attribute__((noreturn)) sigtrap(int sig) +{ + longjmp(jmp, sig); +} + +static void igt_vc4_alloc_mmap_max_bo(int fd, struct igt_list *list, + size_t size) +{ + struct igt_vc4_bo *bo; + struct drm_vc4_create_bo create = { + .size = size, + }; + + while (true) { + if (igt_ioctl(fd, DRM_IOCTL_VC4_CREATE_BO, &create)) + break; + + bo = malloc(sizeof(*bo)); + igt_assert(bo); + bo->handle = create.handle; + bo->size = create.size; + bo->map = igt_vc4_mmap_bo(fd, bo->handle, bo->size, + PROT_READ | PROT_WRITE); + igt_list_add_tail(&bo->node, list); + } +} + +static void igt_vc4_unmap_free_bo_pool(int fd, struct igt_list *list) +{ + struct igt_vc4_bo *bo; + + while (!igt_list_empty(list)) { + bo = igt_list_first_entry(list, bo, node); + igt_assert(bo); + igt_list_del(&bo->node); + munmap(bo->map, bo->size); + gem_close(fd, bo->handle); + free(bo); + } +} + +static void igt_vc4_trigger_purge(int fd) +{ + struct igt_list list; + + igt_list_init(&list); + + /* Try to allocate as much as we can to trigger a purge. */ + igt_vc4_alloc_mmap_max_bo(fd, &list, 64 * 1024); + igt_assert(!igt_list_empty(&list)); + igt_vc4_unmap_free_bo_pool(fd, &list); +} + +igt_main +{ + struct igt_vc4_bo *bo; + struct igt_list list; + uint32_t *map; + int fd, ret; + + igt_fixture { + fd = drm_open_driver(DRIVER_VC4); + igt_list_init(&list); + + igt_vc4_alloc_mmap_max_bo(fd, &list, 64 * 1024); + igt_assert(!igt_list_empty(&list)); + } + + igt_subtest("mark-purgeable") { + igt_list_for_each(bo, &list, node) + igt_vc4_purgeable_bo(fd, bo->handle, true); + + igt_list_for_each(bo, &list, node) + igt_vc4_purgeable_bo(fd, bo->handle, false); + } + + igt_subtest("mark-purgeable-twice") { + bo = igt_list_first_entry(&list, bo, node); + igt_vc4_purgeable_bo(fd, bo->handle, true); + igt_vc4_purgeable_bo(fd, bo->handle, true); + igt_vc4_purgeable_bo(fd, bo->handle, false); + } + + igt_subtest("mark-unpurgeable-twice") { + bo = igt_list_first_entry(&list, bo, node); + igt_vc4_purgeable_bo(fd, bo->handle, true); + igt_vc4_purgeable_bo(fd, bo->handle, false); + igt_vc4_purgeable_bo(fd, bo->handle, false); + } + + igt_subtest("access-purgeable-bo-mem") { + bo = igt_list_first_entry(&list, bo, node); + map = (uint32_t *)bo->map; + + /* Mark the BO as purgeable, but do not try to allocate a new + * BO. This should leave the BO in a non-purged state unless + * someone else tries to allocated a new BO. + */ + igt_vc4_purgeable_bo(fd, bo->handle, true); + + /* Accessing a purgeable BO may generate a SIGBUS event if the + * BO has been purged by the system in the meantime. + */ + signal(SIGSEGV, sigtrap); + signal(SIGBUS, sigtrap); + ret = setjmp(jmp); + if (!ret) + *map = 0xdeadbeef; + else + igt_assert(ret == SIGBUS); + signal(SIGBUS, SIG_DFL); + signal(SIGSEGV, SIG_DFL); + } + + igt_subtest("access-purged-bo-mem") { + struct igt_list tmplist; + + igt_list_init(&tmplist); + + /* Mark the first BO in our list as purgeable and try to + * allocate a new one. This should trigger a purge and render + * the first BO inaccessible. + */ + bo = igt_list_first_entry(&list, bo, node); + map = (uint32_t *)bo->map; + igt_vc4_purgeable_bo(fd, bo->handle, true); + + /* Trigger a purge. */ + igt_vc4_trigger_purge(fd); + + /* Accessing a purged BO should generate a SIGBUS event. */ + signal(SIGSEGV, sigtrap); + signal(SIGBUS, sigtrap); + ret = setjmp(jmp); + if (!ret) + *map = 0; + else + igt_assert(ret == SIGBUS); + signal(SIGBUS, SIG_DFL); + signal(SIGSEGV, SIG_DFL); + igt_vc4_purgeable_bo(fd, bo->handle, false); + } + + igt_subtest("mark-unpurgeable-not-purged") { + igt_list_for_each(bo, &list, node) { + map = (uint32_t *)bo->map; + *map = 0xdeadbeef; + igt_vc4_purgeable_bo(fd, bo->handle, true); + } + + igt_list_for_each(bo, &list, node) { + map = (uint32_t *)bo->map; + igt_assert(igt_vc4_purgeable_bo(fd, bo->handle, false)); + igt_assert(*map == 0xdeadbeef); + } + } + + igt_subtest("mark-unpurgeable-purged") { + igt_list_for_each(bo, &list, node) { + map = (uint32_t *)bo->map; + *map = 0xdeadbeef; + igt_vc4_purgeable_bo(fd, bo->handle, true); + } + + /* Trigger a purge. */ + igt_vc4_trigger_purge(fd); + + igt_list_for_each(bo, &list, node) { + map = (uint32_t *)bo->map; + igt_assert(!igt_vc4_purgeable_bo(fd, bo->handle, false)); + igt_assert(*map != 0xdeadbeef); + } + } + + igt_subtest("mark-unpurgeable-nomem") { + struct igt_list tmplist; + + igt_list_for_each(bo, &list, node) + igt_vc4_purgeable_bo(fd, bo->handle, true); + + /* Deplete the CMA pool by allocating as much as we can. This + * should trigger a purge. + */ + igt_list_init(&tmplist); + igt_vc4_alloc_mmap_max_bo(fd, &tmplist, 64 * 1024); + igt_assert(!igt_list_empty(&tmplist)); + + /* Now try to mark all BOs in the intial pool as unpurgeable. + * We shoud see a failure at some point. + */ + igt_list_for_each(bo, &list, node) { + struct drm_vc4_gem_madvise arg = { + .handle = bo->handle, + .madv = VC4_MADV_WILLNEED, + }; + + ret = igt_ioctl(fd, DRM_IOCTL_VC4_GEM_MADVISE, &arg); + if (ret) + break; + } + + igt_assert(ret && errno == ENOMEM); + + /* Release all BOs from the tmplist and try again. This time it + * should succeed. + */ + igt_vc4_unmap_free_bo_pool(fd, &tmplist); + igt_list_for_each(bo, &list, node) + igt_vc4_purgeable_bo(fd, bo->handle, false); + } + + igt_subtest("free-purged-bo") { + bo = igt_list_first_entry(&list, bo, node); + igt_vc4_purgeable_bo(fd, bo->handle, true); + + /* Trigger a purge. */ + igt_vc4_trigger_purge(fd); + + igt_list_del(&bo->node); + munmap(bo->map, bo->size); + gem_close(fd, bo->handle); + free(bo); + } + + igt_fixture { + igt_vc4_unmap_free_bo_pool(fd, &list); + close(fd); + } +}