public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Filippo Storniolo <fstornio@redhat.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH] syscalls/mlock05: add mlock test for locking and pre-faulting of memory
Date: Mon, 29 Apr 2024 12:00:18 +0200	[thread overview]
Message-ID: <Zi9vsoAzBnKi6aLh@yuki> (raw)
In-Reply-To: <20240426134802.2607150-1-fstornio@redhat.com>

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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

      reply	other threads:[~2024-04-29 10:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-26 13:48 [LTP] [PATCH] syscalls/mlock05: add mlock test for locking and pre-faulting of memory Filippo Storniolo
2024-04-29 10:00 ` Cyril Hrubis [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zi9vsoAzBnKi6aLh@yuki \
    --to=chrubis@suse.cz \
    --cc=fstornio@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox