[LTP] [PATCH] syscalls/mlock05: add mlock test for locking and pre-faulting of memory

Cyril Hrubis chrubis@suse.cz
Mon Apr 29 12:00:18 CEST 2024


Hi!
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright Red Hat
> + * Author: Filippo Storniolo <fstornio@redhat.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Verify mlock() causes pre-faulting of PTEs and prevent memory to be swapped out.
> + *
> + * Checks the variables VmRSS and VmLck in /proc/$pid/status after the
> + * mlock syscall:
> + * - VmRSS size should be at least as big as the memory allocation
> + * - VmLck size should be equal to the size of the memory allocation
> + */
> +
> +#include "tst_test.h"
> +
> +#define MMAPLEN			(1UL<<20)
> +
> +static void verify_mlock(void)
> +{
> +	unsigned long VmRSS_before;
> +	unsigned long VmRSS_after;
> +	unsigned long VmLck_before;
> +	unsigned long VmLck_after;
> +	unsigned long VmRSS;
> +	unsigned long VmLck;
> +	char *buf;
> +
> +	buf = SAFE_MMAP(NULL, MMAPLEN, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +
> +	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmRSS: %lu", &VmRSS_before);
> +	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &VmLck_before);
> +
> +	SAFE_MLOCK(buf, MMAPLEN);
> +
> +	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmRSS: %lu", &VmRSS_after);
> +	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &VmLck_after);
> +
> +	VmRSS = VmRSS_after - VmRSS_before;
> +	VmLck = VmLck_after - VmLck_before;
> +
> +	// Convertion from KiB to B
> +	VmRSS *= 1024;
> +	VmLck *= 1024;
> +
> +	if (VmRSS >= MMAPLEN) {
> +		tst_res(TPASS, "Pre-allocation functionality of mlock() successful");
> +	} else {
> +		tst_brk(TBROK, "Expected pre-allocation of %lu bytes, "
> +					"get %lu pre-allocated", MMAPLEN, VmRSS);

This is TFAIL. TBROK is used when a test is broken because a test setup
has failed, not when test a assertion wasn't true.

Also simplify this to TST_EXP_EXPR(VmRSS >= MMAPLEN);

> +	}
> +
> +	if (VmLck == MMAPLEN) {
> +		tst_res(TPASS, "Locking functionality of mlock() successful");
> +	} else {
> +		tst_brk(TBROK, "Expected locking of %lu bytes, "
> +					"get %lu locked", MMAPLEN, VmLck);
> +	}

This could be simplified to a single TST_EXP_EQ_LU(VmLCK, MMAPLEN);

> +	SAFE_MUNLOCK(buf, MMAPLEN);
> +	SAFE_MUNMAP(buf, MMAPLEN);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = verify_mlock,
> +};

The rest looks good.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list