tools/testing/selftests/arm64/mte/check_hugetlb_options.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/arm64/mte/check_hugetlb_options.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/arm64/mte/check_hugetlb_options.c
Extension
.c
Size
8931 bytes
Lines
297
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

if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
			hps <<= 10;
			break;
		}
	}

	free(line);
	fclose(f);
	return hps;
}

static bool is_hugetlb_allocated(void)
{
	unsigned long hps = 0;
	char *line = NULL;
	size_t linelen = 0;
	FILE *f = fopen("/proc/meminfo", "r");

	if (!f)
		return false;
	while (getline(&line, &linelen, f) > 0) {
		if (sscanf(line, "Hugetlb:       %lu kB", &hps) == 1) {
			hps <<= 10;
			break;
		}
	}

	free(line);
	fclose(f);

	if (hps > 0)
		return true;

	return false;
}

static void write_sysfs(char *str, unsigned long val)
{
	FILE *f;

	f = fopen(str, "w");
	if (!f) {
		ksft_print_msg("ERR: missing %s\n", str);
		return;
	}
	fprintf(f, "%lu", val);
	fclose(f);
}

static void allocate_hugetlb()
{
	write_sysfs("/proc/sys/vm/nr_hugepages", 2);
}

static void free_hugetlb()
{
	write_sysfs("/proc/sys/vm/nr_hugepages", 0);
}

static int check_child_tag_inheritance(char *ptr, int size, int mode)
{
	int i, parent_tag, child_tag, fault, child_status;
	pid_t child;

	parent_tag = MT_FETCH_TAG((uintptr_t)ptr);
	fault = 0;

	child = fork();
	if (child == -1) {
		ksft_print_msg("FAIL: child process creation\n");
		return KSFT_FAIL;
	} else if (child == 0) {
		mte_initialize_current_context(mode, (uintptr_t)ptr, size);
		/* Do copy on write */
		memset(ptr, '1', size);
		mte_wait_after_trig();
		if (cur_mte_cxt.fault_valid == true) {
			fault = 1;
			goto check_child_tag_inheritance_err;
		}
		for (i = 0; i < size; i += MT_GRANULE_SIZE) {
			child_tag = MT_FETCH_TAG((uintptr_t)(mte_get_tag_address(ptr + i)));
			if (parent_tag != child_tag) {
				ksft_print_msg("FAIL: child mte tag (%d) mismatch\n", i);
				fault = 1;
				goto check_child_tag_inheritance_err;
			}
		}
check_child_tag_inheritance_err:
		_exit(fault);

Annotation

Implementation Notes