drivers/gpu/drm/tests/drm_gem_shmem_test.c
Source file repositories/reference/linux-study-clean/drivers/gpu/drm/tests/drm_gem_shmem_test.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/gpu/drm/tests/drm_gem_shmem_test.c- Extension
.c- Size
- 12285 bytes
- Lines
- 396
- Domain
- Driver Families
- Bucket
- drivers/gpu
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/dma-buf.hlinux/iosys-map.hlinux/sizes.hkunit/test.hdrm/drm_device.hdrm/drm_drv.hdrm/drm_gem.hdrm/drm_gem_shmem_helper.hdrm/drm_kunit_helpers.h
Detected Declarations
function drm_gem_shmem_test_obj_createfunction drm_gem_shmem_test_obj_create_privatefunction drm_gem_shmem_test_pin_pagesfunction drm_gem_shmem_test_vmapfunction drm_gem_shmem_test_get_sg_tablefunction for_each_sgtable_sgfunction drm_gem_shmem_test_get_pages_sgtfunction for_each_sgtable_sgfunction drm_gem_shmem_test_madvisefunction drm_gem_shmem_get_pages_sgtfunction drm_gem_shmem_test_init
Annotated Snippet
// 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 <marpagan@redhat.com>
*/
#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>
MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
#define TEST_SIZE SZ_1M
#define TEST_BYTE 0xae
/*
* 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 *);
KUNIT_DEFINE_ACTION_WRAPPER(drm_gem_shmem_unpin_wrapper, drm_gem_shmem_unpin,
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)
{
struct drm_device *drm_dev = test->priv;
struct drm_gem_shmem_object *shmem;
shmem = drm_gem_shmem_create(drm_dev, TEST_SIZE);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, shmem);
KUNIT_EXPECT_EQ(test, shmem->base.size, TEST_SIZE);
KUNIT_EXPECT_NOT_NULL(test, shmem->base.filp);
KUNIT_EXPECT_NOT_NULL(test, shmem->base.funcs);
drm_gem_shmem_free(shmem);
}
/*
* 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)
{
struct drm_device *drm_dev = test->priv;
struct drm_gem_shmem_object *shmem;
struct drm_gem_object *gem_obj;
struct dma_buf buf_mock;
struct dma_buf_attachment attach_mock;
struct sg_table *sgt;
char *buf;
int ret;
/* Create a mock scatter/gather table */
buf = kunit_kzalloc(test, TEST_SIZE, GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, buf);
sgt = kzalloc_obj(*sgt);
KUNIT_ASSERT_NOT_NULL(test, sgt);
ret = kunit_add_action_or_reset(test, kfree_wrapper, sgt);
KUNIT_ASSERT_EQ(test, ret, 0);
ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
KUNIT_ASSERT_EQ(test, ret, 0);
Annotation
- Immediate include surface: `linux/dma-buf.h`, `linux/iosys-map.h`, `linux/sizes.h`, `kunit/test.h`, `drm/drm_device.h`, `drm/drm_drv.h`, `drm/drm_gem.h`, `drm/drm_gem_shmem_helper.h`.
- Detected declarations: `function drm_gem_shmem_test_obj_create`, `function drm_gem_shmem_test_obj_create_private`, `function drm_gem_shmem_test_pin_pages`, `function drm_gem_shmem_test_vmap`, `function drm_gem_shmem_test_get_sg_table`, `function for_each_sgtable_sg`, `function drm_gem_shmem_test_get_pages_sgt`, `function for_each_sgtable_sg`, `function drm_gem_shmem_test_madvise`, `function drm_gem_shmem_get_pages_sgt`.
- Atlas domain: Driver Families / drivers/gpu.
- Implementation status: source implementation candidate.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.