From patchwork Fri Mar 5 11:12:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neil Roberts X-Patchwork-Id: 12118339 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B2D1C433DB for ; Fri, 5 Mar 2021 11:13:17 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BF56264FF0 for ; Fri, 5 Mar 2021 11:13:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BF56264FF0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=igalia.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2305D6E1D6; Fri, 5 Mar 2021 11:13:16 +0000 (UTC) Received: from fanzine.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 63A296E1CD; Fri, 5 Mar 2021 11:13:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=9IMtNTWCQ0cP72Gz+Jj7FrniCUzLw8jtsPxfq8pOVno=; b=RYvV9WjH3zRvC/dKbDBQUWJJx8TVrrcSw8BKvYDfXy3aUsEOCUUT0DuXITRnVQevNza28CWoTq2pEqoYoB10eZxi4A0wSE0LMyezTg6qCLJlRBC9Ptc4dSF0id6wHxUEreHwJN6KOQxxUlzPqwmpHA0BY/iRP6xrTzVknbdQ7WumsNT3cCDckSjoqSJfEEIrwLb933h7ZPHbG5Py4WFdRleIACEsE4l1MrGfsW81Z8Csvsl7ZH6h4qWhOAl/5fr98GXwNXLBEuOTTqPnHzIVEIyvYUKdJcTvTsh9HAdSm6wDPRJy9kd/cskTSpsLenUj+FJPdFHqUTrEpLbB7r4n3Q==; Received: from lneuilly-657-1-8-171.w81-250.abo.wanadoo.fr ([81.250.147.171] helo=masxo.routerf36dc8.com) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1lI8OO-0008UE-T9; Fri, 05 Mar 2021 12:13:13 +0100 From: Neil Roberts To: igt-dev@lists.freedesktop.org Subject: [PATCH i-g-t 1/2] lib/panfrost: Add a utility to madvise a buffer Date: Fri, 5 Mar 2021 12:12:45 +0100 Message-Id: <20210305111246.1381965-2-nroberts@igalia.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210305111246.1381965-1-nroberts@igalia.com> References: <20210305111246.1381965-1-nroberts@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Signed-off-by: Neil Roberts --- lib/igt_panfrost.c | 12 ++++++++++++ lib/igt_panfrost.h | 1 + 2 files changed, 13 insertions(+) diff --git a/lib/igt_panfrost.c b/lib/igt_panfrost.c index 8b0c2b77..ffce66a2 100644 --- a/lib/igt_panfrost.c +++ b/lib/igt_panfrost.c @@ -127,6 +127,18 @@ void igt_panfrost_bo_mmap(int fd, struct panfrost_bo *bo) igt_assert(bo->map); } +bool igt_panfrost_bo_madvise(int fd, struct panfrost_bo *bo, uint32_t madv) +{ + struct drm_panfrost_madvise madvise = { + .handle = bo->handle, + .madv = madv, + }; + + do_ioctl(fd, DRM_IOCTL_PANFROST_MADVISE, &madvise); + + return madvise.retained; +} + struct panfrost_submit *igt_panfrost_trivial_job(int fd, bool do_crash, int width, int height, uint32_t color) { struct panfrost_submit *submit; diff --git a/lib/igt_panfrost.h b/lib/igt_panfrost.h index cc7998dc..df326a6c 100644 --- a/lib/igt_panfrost.h +++ b/lib/igt_panfrost.h @@ -56,5 +56,6 @@ uint32_t igt_panfrost_get_param(int fd, int param); void *igt_panfrost_mmap_bo(int fd, uint32_t handle, uint32_t size, unsigned prot); void igt_panfrost_bo_mmap(int fd, struct panfrost_bo *bo); +bool igt_panfrost_bo_madvise(int fd, struct panfrost_bo *bo, uint32_t madv); #endif /* IGT_PANFROST_H */ From patchwork Fri Mar 5 11:12:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Neil Roberts X-Patchwork-Id: 12118341 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0957EC433E0 for ; Fri, 5 Mar 2021 11:13:22 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8158D64FF0 for ; Fri, 5 Mar 2021 11:13:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8158D64FF0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=igalia.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C983B6EB70; Fri, 5 Mar 2021 11:13:20 +0000 (UTC) Received: from fanzine.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 867286E1D7; Fri, 5 Mar 2021 11:13:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=zeaHymYFWxgLf4tYpNNrD9eXf7I9c8t5x0dRgtpcBD0=; b=H/092IGgpl+RcaLWdAYcEReOUpuaAgt+TCKpERDzczZbd2ea7jsfmKxANq/hnZrjyGd5d/XAU3a+h5Sgzo/TdWOS3A4h78c8kUvOc7aKw728pBgnrlglZ1AV8qtqH/Q3ClwfknlKU1jCqb2NeBE83s428CnpKb7ITJmezbYNISAYTVG3UrgKw7rkcBk1iioDAvkGvg0YHwfcRNcU8FnPGMx5G/wYz7W8YxWknvsFi0GOpL5AlX0XOfjALKAbM5p5SUavceNnYSt9Acy7iepLjOwi7x046wLpYqa1u9N4OxWyAhtEaLaD4lqyBhb5LvtxSH+r8NOnI67zP3cV4636Yw==; Received: from lneuilly-657-1-8-171.w81-250.abo.wanadoo.fr ([81.250.147.171] helo=masxo.routerf36dc8.com) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1lI8OU-0008UE-0n; Fri, 05 Mar 2021 12:13:18 +0100 From: Neil Roberts To: igt-dev@lists.freedesktop.org Subject: [PATCH i-g-t 2/2] tests/panfrost: Add a test for accessing a purged buffer Date: Fri, 5 Mar 2021 12:12:46 +0100 Message-Id: <20210305111246.1381965-3-nroberts@igalia.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210305111246.1381965-1-nroberts@igalia.com> References: <20210305111246.1381965-1-nroberts@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The test creates a buffer, sets it as DONTNEED via madvise, tries to trigger a purge of buffers and then accesses the buffer via a user-space mapping. This should generate a bus error, but due to a bug in the kernel driver it fails to invalidate the mapping when the buffer is purged. Signed-off-by: Neil Roberts --- tests/meson.build | 1 + tests/panfrost_purgemap.c | 146 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 tests/panfrost_purgemap.c diff --git a/tests/meson.build b/tests/meson.build index 825e0183..af24427a 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -82,6 +82,7 @@ test_progs = [ 'panfrost_get_param', 'panfrost_gem_new', 'panfrost_prime', + 'panfrost_purgemap', 'panfrost_submit', 'prime_busy', 'prime_mmap', diff --git a/tests/panfrost_purgemap.c b/tests/panfrost_purgemap.c new file mode 100644 index 00000000..096fb95a --- /dev/null +++ b/tests/panfrost_purgemap.c @@ -0,0 +1,146 @@ +/* + * Copyright © 2021 Igalia + * + * 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_panfrost.h" +#include "igt_syncobj.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "panfrost-job.h" +#include "panfrost_drm.h" + +IGT_TEST_DESCRIPTION("Checks that accessing an mmapping of a buffer that has " + "been purged causes a bus fault signal."); + +#define BUF_SIZE (32 * 1024 * 1024) + +struct buf_link { + struct panfrost_bo *bo; + struct buf_link *next; +}; + +static jmp_buf jmp; + +__noreturn static void sigtrap(int sig) +{ + siglongjmp(jmp, sig); +} + +static void +try_writing_to_mmap(uint8_t *ptr) +{ + sighandler_t old_sigsegv, old_sigbus; + + old_sigsegv = signal(SIGSEGV, sigtrap); + old_sigbus = signal(SIGBUS, sigtrap); + + switch (sigsetjmp(jmp, SIGBUS | SIGSEGV)) { + case SIGBUS: + break; + case 0: + *ptr = 0; + default: + igt_assert(!"reached"); + break; + } + + signal(SIGBUS, old_sigsegv); + signal(SIGSEGV, old_sigbus); +} + +static void +trigger_purge(int fd, struct panfrost_bo *madv_bo) +{ + struct buf_link *buf_list = NULL, *next, *buf; + bool retained; + + /* Try to trigger a purge by allocating buffers until the main + * buffer is no longer retained. This should probably be + * replaced with a more reliable way to do this such as a + * debugfs trigger. + */ + + while (true) { + /* Mark the buffer as purgeable */ + retained = igt_panfrost_bo_madvise(fd, + madv_bo, + PANFROST_MADV_DONTNEED); + igt_assert(retained); + + buf = malloc(sizeof(*buf)); + buf->bo = igt_panfrost_gem_new(fd, BUF_SIZE); + buf->next = buf_list; + buf_list = buf; + + /* Check if that caused the buffer to be purged */ + retained = igt_panfrost_bo_madvise(fd, + madv_bo, + PANFROST_MADV_WILLNEED); + + if (!retained) + break; + } + + /* Free all the temporary buffers that we created */ + for (buf = buf_list; buf; buf = next) { + next = buf->next; + igt_panfrost_free_bo(fd, buf->bo); + free(buf); + } +} + +igt_simple_main +{ + int fd; + struct panfrost_bo *bo; + + fd = drm_open_driver(DRIVER_PANFROST); + + bo = igt_panfrost_gem_new(fd, BUF_SIZE); + + igt_panfrost_bo_mmap(fd, bo); + + /* Write to the buffer to make sure the first page is + * paged in + */ + memset(bo->map, 42, 64); + + /* Try to cause the buffer to be purged */ + trigger_purge(fd, bo); + + /* Write through the mapping. This should cause a bus fault. */ + try_writing_to_mmap(bo->map); + + igt_panfrost_free_bo(fd, bo); + + close(fd); +}