($INBOX_DIR/description missing)
 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 v4] syscalls/mlock05: add mlock test for locking and pre-faulting of memory
Date: Thu, 9 May 2024 16:49:20 +0200	[thread overview]
Message-ID: <ZjzicK0BunOFmjHn@yuki> (raw)
In-Reply-To: <20240509133712.3383293-1-fstornio@redhat.com>

Hi!
> +static unsigned long get_proc_smaps_field(unsigned long desired_mapping_address, char *desired_field)
> +{
> +	bool mapping_found = false;
> +	char buffer[LINELEN] = "";
> +	FILE *file = NULL;
> +	int ret = 0;

There is no point in initializing buffer, file and ret these are written
to before we use the value.

> +	file = fopen("/proc/self/smaps", "r");
> +	if (file == NULL) {
> +		tst_brk(TBROK | TERRNO, "cannot open file proc/self/smaps");
> +		return 0;
> +	}

Use SAFE_FOPEN() please.

> +	// find desired mapping
> +	while (fgets(buffer, LINELEN, file) != NULL) {
> +		unsigned long mapping_address;
> +
> +		// check the starting address

I do not find this comment to be useful, we do have a rule in LTP not to
comment obvious and I would argue that this comment does so.

> +		ret = sscanf(buffer, "%lx[^-]", &mapping_address);
> +
> +		if ((ret == 1) && (mapping_address == desired_mapping_address)) {
> +			mapping_found = true;
> +			break;
> +		}
> +	}
> +
> +	if (!mapping_found) {
> +		fclose(file);

SAFE_FCLOSE() please and in the rest of the cases below.

> +		tst_brk(TBROK, "cannot find mapping %lx in proc/self/smaps", desired_mapping_address);
> +		return 0;
> +	}
> +
> +	// find desired field
> +	while (fgets(buffer, LINELEN, file) != NULL) {
> +		if (strstr(buffer, desired_field) != NULL) {

Can we use strncmp() to match the prefix rather a substring? At the
moment "Locked" and "Rss" are not substrings of other keys in that file
but that may change at some point.

> +			unsigned long desired_value;
> +
> +			// extract the value for the requested field

Here as well, commenting the obvious.

> +			ret = sscanf(buffer, "%*[^0-9]%lu%*[^0-9]", &desired_value);

The key value is divided by : and thge entries we are interested in are
in kB so "%*[^:]%lu kB" should be a bit safer.

> +			fclose(file);
> +
> +			if (ret != 1) {
> +				tst_brk(TBROK, "failure occured while reading field %s", desired_field);
> +				return 0;
> +			}
> +
> +			return desired_value;
> +		}
> +	}
> +
> +	fclose(file);
> +	tst_brk(TBROK, "cannot find %s field", desired_field);
> +
> +	return 0;
> +}
> +
> +static void verify_mlock(void)
> +{
> +	unsigned long Locked;
> +	unsigned long Rss;
> +	char *buf;
> +
> +	buf = SAFE_MMAP(NULL, MMAPLEN, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +	SAFE_MLOCK(buf, MMAPLEN);
> +
> +	Rss = get_proc_smaps_field((unsigned long)buf, "Rss");
> +	Locked = get_proc_smaps_field((unsigned long)buf, "Locked");

Hmm, we do parse the whole file twice here, just the get to a nearly
same line. Why can't we just point two pointrs to unsigned long to the
function and collect both while we read the file?

> +	// Convertion from KiB to B
> +	Rss *= 1024;
> +	Locked *= 1024;
> +
> +	TST_EXP_EQ_LU(Rss, MMAPLEN);
> +	TST_EXP_EQ_LU(Locked, MMAPLEN);
> +
> +	SAFE_MUNLOCK(buf, MMAPLEN);
> +	SAFE_MUNMAP(buf, MMAPLEN);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = verify_mlock,
> +};

-- 
Cyril Hrubis
chrubis@suse.cz

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

      reply	other threads:[~2024-05-09 14:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-09 13:37 [LTP] [PATCH v4] syscalls/mlock05: add mlock test for locking and pre-faulting of memory Filippo Storniolo
2024-05-09 14:49 ` 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=ZjzicK0BunOFmjHn@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).