tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c
Extension
.c
Size
8187 bytes
Lines
323
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 DmabufInfo {
	unsigned long inode;
	unsigned long size;
	char name[DMA_BUF_NAME_LEN];
	char exporter[32];
};

static bool check_dmabuf_info(const struct DmabufInfo *bufinfo,
			      unsigned long size,
			      const char *name, const char *exporter)
{
	return size == bufinfo->size &&
	       !strcmp(name, bufinfo->name) &&
	       !strcmp(exporter, bufinfo->exporter);
}

static void subtest_dmabuf_iter_check_no_infinite_reads(struct dmabuf_iter *skel)
{
	int iter_fd;
	char buf[256];

	iter_fd = bpf_iter_create(bpf_link__fd(skel->links.dmabuf_collector));
	if (!ASSERT_OK_FD(iter_fd, "iter_create"))
		return;

	while (read(iter_fd, buf, sizeof(buf)) > 0)
		; /* Read out all contents */

	/* Next reads should return 0 */
	ASSERT_EQ(read(iter_fd, buf, sizeof(buf)), 0, "read");

	close(iter_fd);
}

static void subtest_dmabuf_iter_check_default_iter(struct dmabuf_iter *skel)
{
	bool found_test_sysheap_dmabuf = false;
	bool found_test_udmabuf = false;
	struct DmabufInfo bufinfo;
	size_t linesize = 0;
	char *line = NULL;
	FILE *iter_file;
	int iter_fd, f = INODE;

	iter_fd = bpf_iter_create(bpf_link__fd(skel->links.dmabuf_collector));
	if (!ASSERT_OK_FD(iter_fd, "iter_create"))
		return;

	iter_file = fdopen(iter_fd, "r");
	if (!ASSERT_OK_PTR(iter_file, "fdopen"))
		goto close_iter_fd;

	while (getline(&line, &linesize, iter_file) != -1) {
		if (f % FIELD_COUNT == INODE) {
			ASSERT_EQ(sscanf(line, "%ld", &bufinfo.inode), 1,
				  "read inode");
		} else if (f % FIELD_COUNT == SIZE) {
			ASSERT_EQ(sscanf(line, "%ld", &bufinfo.size), 1,
				  "read size");
		} else if (f % FIELD_COUNT == NAME) {
			ASSERT_EQ(sscanf(line, "%s", bufinfo.name), 1,
				  "read name");
		} else if (f % FIELD_COUNT == EXPORTER) {
			ASSERT_EQ(sscanf(line, "%31s", bufinfo.exporter), 1,
				  "read exporter");

			if (check_dmabuf_info(&bufinfo,
					      sysheap_test_buffer_size,
					      sysheap_test_buffer_name,
					      "system"))
				found_test_sysheap_dmabuf = true;
			else if (check_dmabuf_info(&bufinfo,
						   udmabuf_test_buffer_size,
						   udmabuf_test_buffer_name,
						   "udmabuf"))
				found_test_udmabuf = true;
		}
		++f;
	}

	ASSERT_EQ(f % FIELD_COUNT, INODE, "number of fields");

	ASSERT_TRUE(found_test_sysheap_dmabuf, "found_test_sysheap_dmabuf");
	ASSERT_TRUE(found_test_udmabuf, "found_test_udmabuf");

	free(line);
	fclose(iter_file);
close_iter_fd:
	close(iter_fd);
}

Annotation

Implementation Notes