[LTP] [PATCH v2] Migrating the libhugetlbfs/testcases/slbpacaflush.c test

Shirisha ganta shirisha@linux.ibm.com
Fri Aug 22 07:51:31 CEST 2025


On Mon, 2024-06-24 at 13:57 +0200, Cyril Hrubis wrote:
> Hi!
> > +#define _GNU_SOURCE
> > +#define SYSFS_CPU_ONLINE_FMT
> > "/sys/devices/system/cpu/cpu%d/online"
> > +#define MNTPOINT "hugetlbfs/"
> > +#define SINGLE_CPU (CPU_SETSIZE/8)
> > +#include "hugetlb.h"
> > +
> > +
> > +#include <stdio.h>
> > +#include <sched.h>
> > +
> > +
> > +long hpage_size;
> > +int fd;
> > +void *p;
> > +volatile unsigned long *q;
> > +int online_cpus[2], err;
> > +cpu_set_t cpu0, cpu1;
> 
> All these should be static.
will take care in V3
> 
> > +void check_online_cpus(int online_cpus[], int nr_cpus_needed)
> > +{
> > +	char cpu_state, path_buf[64];
> > +	int cpu_idx, fd, ret, i;
> > +
> > +	cpu_idx = 0;
> > +
> > +	if (get_nprocs() < nr_cpus_needed)
> > +		tst_brk(TCONF, "minimum  %d online cpus are required",
> > nr_cpus_needed);
> > +
> > +	for (i = 0; i < get_nprocs_conf() && cpu_idx < nr_cpus_needed;
> > i++) {
> > +		errno = 0;
> > +		sprintf(path_buf, SYSFS_CPU_ONLINE_FMT, i);
> > +		fd = open(path_buf, O_RDONLY);
> > +		if (fd < 0) {
> > +			/* If 'online' is absent, the cpu cannot be
> > offlined */
> > +			if (errno == ENOENT) {
> > +				online_cpus[cpu_idx] = i;
> > +				cpu_idx++;
> > +				continue;
> > +			} else {
> > +				tst_res(TFAIL | TERRNO, "Unable to open
> > %s", path_buf);
> > +			}
> > +		}
> > +
> > +		ret = read(fd, &cpu_state, 1);
> > +		if (ret < 1)
> > +			tst_res(TFAIL | TERRNO, "Unable to read %s",
> > path_buf);
> > +
> > +		if (cpu_state == '1') {
> > +			online_cpus[cpu_idx] = i;
> > +			cpu_idx++;
> > +		}
> > +
> > +		if (fd >= 0)
> > +			SAFE_CLOSE(fd);
> > +	}
> > +
> > +	if (cpu_idx < nr_cpus_needed)
> > +		tst_brk(TCONF, "minimum %d online cpus were not found",
> > nr_cpus_needed);
> > +}
> 
> There seems to be an easier method, recently we needed to find online
> CPU for the startvation tests and all you need to do is to get the
> current thread affinity and then look for non-zero bits in that, see
> commit:
> 
> commit 1800e635783b69cacdce9f654ecd730a8f30915b
> Author: Edward Liaw via ltp <ltp@lists.linux.it>
> Date:   Wed Jun 19 16:28:07 2024 +0000
> 
> And I suppose that it would make sense to put this function in the
> test
> library so taht we do not have to repeat it over in tests, but we
> would
> have to make it return the actual number of CPUs found and do the
> tst_brk(TCONF, ...) in the tests instead, so we would add:
> 
> unsigned int tst_get_online_cpus(int online_cpus[], unsigned int
> online_cpus_cnt);
> 
> And the test would do:
> 
> 	if (tst_get_online_cpus(online_cpus, 2) != 2)
> 		tst_brK(TCONF, "Require at least 2 online CPUs.");
will take care in V3
> 
> > +static void run_test(void)
> > +{
> > +	check_online_cpus(online_cpus, 2);
> > +	CPU_ZERO(&cpu0);
> > +	CPU_SET(online_cpus[0], &cpu0);
> > +	CPU_ZERO(&cpu1);
> > +	CPU_SET(online_cpus[1], &cpu1);
> > +
> > +	err = sched_setaffinity(getpid(), SINGLE_CPU, &cpu0);
> > +	if (err != 0)
> > +		tst_res(TFAIL | TERRNO, "sched_setaffinity(cpu%d)",
> > online_cpus[0]);
> > +
> > +	err = sched_setaffinity(getpid(), SINGLE_CPU, &cpu1);
> > +
> > +	if (err != 0)
> > +		tst_res(TFAIL | TERRNO, "sched_setaffinity(cpu%d)",
> > online_cpus[1]);
> > +	p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE,
> > MAP_SHARED, fd, 0);
> > +
> > +	err = sched_setaffinity(getpid(), SINGLE_CPU, &cpu0);
> > +	if (err != 0)
> > +		tst_res(TFAIL, "sched_setaffinity(cpu%d)",
> > online_cpus[0]);
> > +	q = (volatile unsigned long *)(p + getpagesize());
> > +	*q = 0xdeadbeef;
> 
> I suppose that the test crashes here, when the entries are not
> propagated, right?
> 
> > +	tst_res(TPASS, "Test Passed inconclusive");
> 
> We usually print something as tst_res(TPASS, "Nothing bad happend,
> probably");
> in the case that we haven't managed to crash the system.
> 
> Also we have to unmap the p here so that the test works properly with
> -i
> parameter.
sure. will take care in v3
> 
> > +}
> > +
> > +static void setup(void)
> > +{
> > +	hpage_size = tst_get_hugepage_size();
> > +	fd = tst_creat_unlinked(MNTPOINT, 0);
> > +}
> > +
> > +void cleanup(void)
> > +{
> > +	if (fd > 0)
> > +		SAFE_CLOSE(fd);
> > +}
> > +
> > +static struct tst_test test = {
> > +	.needs_root = 1,
> > +	.mntpoint = MNTPOINT,
> > +	.needs_hugetlbfs = 1,
> > +	.needs_tmpdir = 1,
> > +	.setup = setup,
> > +	.cleanup = cleanup,
> > +	.test_all = run_test,
> > +	.hugepages = {1, TST_NEEDS},
> > +};
> > -- 
> > 2.39.3
> > 
> > 
> > -- 
> > Mailing list info: https://lists.linux.it/listinfo/ltp



More information about the ltp mailing list