// SPDX-License-Identifier: GPL-2.0 /* * KUnit test suite for GEM objects backed by shmem buffers * * Copyright (C) 2023 Red Hat, Inc. * * Author: Marco Pagani <[email protected]> */ #include <linux/dma-buf.h> #include <linux/iosys-map.h> #include <linux/sizes.h> #include <kunit/test.h> #include <drm/drm_device.h> #include <drm/drm_drv.h> #include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.h> #include <drm/drm_kunit_helpers.h> #define TEST_SIZE … #define TEST_BYTE … /* * Wrappers to avoid cast warnings when passing action functions * directly to kunit_add_action(). */ KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *); KUNIT_DEFINE_ACTION_WRAPPER(sg_free_table_wrapper, sg_free_table, struct sg_table *); KUNIT_DEFINE_ACTION_WRAPPER(drm_gem_shmem_free_wrapper, drm_gem_shmem_free, struct drm_gem_shmem_object *); /* * Test creating a shmem GEM object backed by shmem buffer. The test * case succeeds if the GEM object is successfully allocated with the * shmem file node and object functions attributes set, and the size * attribute is equal to the correct size. */ static void drm_gem_shmem_test_obj_create(struct kunit *test) { … } /* * Test creating a shmem GEM object from a scatter/gather table exported * via a DMA-BUF. The test case succeed if the GEM object is successfully * created with the shmem file node attribute equal to NULL and the sgt * attribute pointing to the scatter/gather table that has been imported. */ static void drm_gem_shmem_test_obj_create_private(struct kunit *test) { … } /* * Test pinning backing pages for a shmem GEM object. The test case * succeeds if a suitable number of backing pages are allocated, and * the pages table counter attribute is increased by one. */ static void drm_gem_shmem_test_pin_pages(struct kunit *test) { … } /* * Test creating a virtual mapping for a shmem GEM object. The test * case succeeds if the backing memory is mapped and the reference * counter for virtual mapping is increased by one. Moreover, the test * case writes and then reads a test pattern over the mapped memory. */ static void drm_gem_shmem_test_vmap(struct kunit *test) { … } /* * Test exporting a scatter/gather table of pinned pages suitable for * PRIME usage from a shmem GEM object. The test case succeeds if a * scatter/gather table large enough to accommodate the backing memory * is successfully exported. */ static void drm_gem_shmem_test_get_pages_sgt(struct kunit *test) { … } /* * Test pinning pages and exporting a scatter/gather table suitable for * driver usage from a shmem GEM object. The test case succeeds if the * backing pages are pinned and a scatter/gather table large enough to * accommodate the backing memory is successfully exported. */ static void drm_gem_shmem_test_get_sg_table(struct kunit *test) { … } /* * Test updating the madvise state of a shmem GEM object. The test * case checks that the function for setting madv updates it only if * its current value is greater or equal than zero and returns false * if it has a negative value. */ static void drm_gem_shmem_test_madvise(struct kunit *test) { … } /* * Test purging a shmem GEM object. First, assert that a newly created * shmem GEM object is not purgeable. Then, set madvise to a positive * value and call drm_gem_shmem_get_pages_sgt() to pin and dma-map the * backing pages. Finally, assert that the shmem GEM object is now * purgeable and purge it. */ static void drm_gem_shmem_test_purge(struct kunit *test) { … } static int drm_gem_shmem_test_init(struct kunit *test) { … } static struct kunit_case drm_gem_shmem_test_cases[] = …; static struct kunit_suite drm_gem_shmem_suite = …; kunit_test_suite(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;