This adds a test for Panfrost to make sure that accessing a buffer that has been madvised and then purged causes a bus fault. It is intended to test the fix provided by this series:
https://patchwork.freedesktop.org/series/87324/
The series has now been merged into drm-misc-fixes (thanks!)
In order to trigger the purge, the test just tries to allocate a bunch of buffers until one of them causes the original buffer to be purged. During the review of the kernel patch series, Daniel Vetter suggested it would be better to use debugfs or vm_drop_caches file from proc. However, I noticed that there are other tests that use the same method as in this series (eg, igt_vc4_trigger_purge). Seeing as there is already a precedent for IGT tests to do this, I wonder if we could leave that as a later change for someone who is more active in Panfrost development.
Neil Roberts (2): lib/panfrost: Add a utility to madvise a buffer tests/panfrost: Add a test for accessing a purged buffer
lib/igt_panfrost.c | 12 ++++ lib/igt_panfrost.h | 1 + tests/meson.build | 1 + tests/panfrost_purgemap.c | 146 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+) create mode 100644 tests/panfrost_purgemap.c
Signed-off-by: Neil Roberts nroberts@igalia.com --- 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 */
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 nroberts@igalia.com --- 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 <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <inttypes.h> +#include <errno.h> +#include <sys/stat.h> +#include <sys/ioctl.h> +#include <setjmp.h> +#include <signal.h> +#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); +}
dri-devel@lists.freedesktop.org