tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
Extension
.c
Size
10071 bytes
Lines
443
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct dma_heap_allocation_data_smaller {
		__u64 len;
		__u32 fd;
		__u32 fd_flags;
	} data = {
		.len = len,
		.fd = 0,
		.fd_flags = O_RDWR | O_CLOEXEC,
	};

	older_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,
				  struct dma_heap_allocation_data_smaller);
	if (!dmabuf_fd)
		return -EINVAL;

	ret = ioctl(fd, older_alloc_ioctl, &data);
	if (ret < 0)
		return ret;
	*dmabuf_fd = (int)data.fd;
	return ret;
}

/* Test the ioctl version compatibility w/ a larger structure then expected */
static int dmabuf_heap_alloc_newer(int fd, size_t len, unsigned int flags,
				   int *dmabuf_fd)
{
	int ret;
	unsigned int newer_alloc_ioctl;
	struct dma_heap_allocation_data_bigger {
		__u64 len;
		__u32 fd;
		__u32 fd_flags;
		__u64 heap_flags;
		__u64 garbage1;
		__u64 garbage2;
		__u64 garbage3;
	} data = {
		.len = len,
		.fd = 0,
		.fd_flags = O_RDWR | O_CLOEXEC,
		.heap_flags = flags,
		.garbage1 = 0xffffffff,
		.garbage2 = 0x88888888,
		.garbage3 = 0x11111111,
	};

	newer_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,
				  struct dma_heap_allocation_data_bigger);
	if (!dmabuf_fd)
		return -EINVAL;

	ret = ioctl(fd, newer_alloc_ioctl, &data);
	if (ret < 0)
		return ret;

	*dmabuf_fd = (int)data.fd;
	return ret;
}

static void test_alloc_compat(char *heap_name)
{
	int ret, heap_fd = -1, dmabuf_fd = -1;

	heap_fd = dmabuf_heap_open(heap_name);

	ksft_print_msg("Testing (theoretical) older alloc compat:\n");
	ret = dmabuf_heap_alloc_older(heap_fd, ONE_MEG, 0, &dmabuf_fd);
	if (dmabuf_fd >= 0)
		close(dmabuf_fd);
	ksft_test_result(!ret, "dmabuf_heap_alloc_older\n");

	ksft_print_msg("Testing (theoretical) newer alloc compat:\n");
	ret = dmabuf_heap_alloc_newer(heap_fd, ONE_MEG, 0, &dmabuf_fd);
	if (dmabuf_fd >= 0)
		close(dmabuf_fd);
	ksft_test_result(!ret, "dmabuf_heap_alloc_newer\n");

	close(heap_fd);
}

static void test_alloc_errors(char *heap_name)
{
	int heap_fd = -1, dmabuf_fd = -1;
	int ret;

	heap_fd = dmabuf_heap_open(heap_name);

	ksft_print_msg("Testing expected error cases:\n");
	ret = dmabuf_heap_alloc(0, ONE_MEG, 0x111111, &dmabuf_fd);
	ksft_test_result(ret, "Error expected on invalid fd %d\n", ret);

Annotation

Implementation Notes