Linux Perf Users
 help / color / mirror / Atom feed
From: "Pierre-Clément Tosi" <ptosi@google.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	 Ian Rogers <irogers@google.com>,
	James Clark <james.clark@linaro.org>
Cc: "Ankur Arora" <ankur.a.arora@oracle.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Pierre-Clément Tosi" <ptosi@google.com>
Subject: [PATCH 3/4] perf bench mem: Add --prot option
Date: Tue, 01 Dec 2026 13:11:52 +0100	[thread overview]
Message-ID: <20261201-perf-prot-mte-v1-3-4dc4e1da7460@google.com> (raw)
In-Reply-To: <20261201-perf-prot-mte-v1-0-4dc4e1da7460@google.com>

Allow callers to control the prot passed to mmap(), defaulting to R/W.

Adapt the page accesses within the benchmark accordingly: load only if
the page is readable (triggers fault-in) and store only when writable
(triggers CoW). Add 'exec' for completeness, although mappings that are
neither readable nor writable are not accessed.

Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
---
 tools/perf/Documentation/perf-bench.txt | 18 +++++++++++++
 tools/perf/bench/mem-functions.c        | 46 ++++++++++++++++++++++++++++++---
 2 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/tools/perf/Documentation/perf-bench.txt b/tools/perf/Documentation/perf-bench.txt
index c65dd8f4131f..0a3ad2284f19 100644
--- a/tools/perf/Documentation/perf-bench.txt
+++ b/tools/perf/Documentation/perf-bench.txt
@@ -209,6 +209,10 @@ Repeat memcpy invocation this number of times.
 --cycles::
 Use perf's cpu-cycles event instead of gettimeofday syscall.
 
+--prot=<PROT>::
+Specify memory protection for mapping memory buffers (default:
+read,write). See the *mmap* options.
+
 *memset*::
 Suite for evaluating performance of simple memory set in various ways.
 
@@ -243,6 +247,10 @@ Repeat memset invocation this number of times.
 --cycles::
 Use perf's cpu-cycles event instead of gettimeofday syscall.
 
+--prot=<PROT>::
+Specify memory protection for mapping memory buffers (default:
+read,write). See the *mmap* options.
+
 *mmap*::
 Suite for evaluating memory subsystem performance for mmap()'d memory.
 
@@ -281,6 +289,16 @@ Use perf's cpu-cycles event instead of gettimeofday syscall.
 --threads=<NUM>::
 Create multiple threads to call mmap/munmap concurrently.
 
+--prot=<PROT>::
+Specify memory protection for mapping memory buffers (default:
+read,write). Available values are none, read, write and exec, as a
+comma-separated list.
+The benchmark loads from each page of a readable mapping and stores to
+each page of a writable one: 'read' maps the zero page, 'write' takes a
+single write fault per page, and 'read,write' maps the zero page before
+breaking copy-on-write. Mappings that are neither readable nor writable
+are not accessed.
+
 SUITES FOR 'numa'
 ~~~~~~~~~~~~~~~~~
 *mem*::
diff --git a/tools/perf/bench/mem-functions.c b/tools/perf/bench/mem-functions.c
index 92f41820137f..84247f83da59 100644
--- a/tools/perf/bench/mem-functions.c
+++ b/tools/perf/bench/mem-functions.c
@@ -39,6 +39,7 @@ static const char	*size_str	= "1MB";
 static const char	*function_str	= "all";
 static const char	*page_size_str	= "4KB";
 static const char	*chunk_size_str	= "0";
+static const char	*prot_str	= "read,write";
 static unsigned int	nr_loops	= 1;
 static bool		use_cycles;
 static int		cycles_fd;
@@ -63,6 +64,9 @@ static const struct option bench_common_options[] = {
 	OPT_BOOLEAN('c', "cycles", &use_cycles,
 		    "Use a cycles event instead of gettimeofday() to measure performance"),
 
+	OPT_STRING(0, "prot", &prot_str, "read,write",
+		   "Memory protection: none, read, write, exec (comma-separated)"),
+
 	OPT_END()
 };
 
@@ -86,6 +90,7 @@ struct bench_params {
 	unsigned int	nr_loops;
 	unsigned int	page_shift;
 	unsigned int	seed;
+	int		prot;
 	bool		populate;
 };
 
@@ -265,6 +270,30 @@ static void __bench_mem_function(struct bench_mem_info *info, struct bench_param
 	goto out_free;
 }
 
+static int parse_arg_prot(const char *str)
+{
+	int prot = 0;
+
+	for (;;) {
+		size_t len = strcspn(str, ",");
+
+		if (len == strlen("none") && !strncmp(str, "none", len))
+			prot |= PROT_NONE;
+		else if (len == strlen("read") && !strncmp(str, "read", len))
+			prot |= PROT_READ;
+		else if (len == strlen("write") && !strncmp(str, "write", len))
+			prot |= PROT_WRITE;
+		else if (len == strlen("exec") && !strncmp(str, "exec", len))
+			prot |= PROT_EXEC;
+		else
+			return -1;
+
+		if (!str[len])
+			return prot;
+		str += len + 1;
+	}
+}
+
 static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *info)
 {
 	int i;
@@ -281,6 +310,12 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
 		}
 	}
 
+	p.prot = parse_arg_prot(prot_str);
+	if (p.prot < 0) {
+		fprintf(stderr, "Invalid prot:%s\n", prot_str);
+		return 1;
+	}
+
 	p.nr_loops = nr_loops;
 	p.size = (size_t)perf_atoll((char *)size_str);
 
@@ -376,7 +411,7 @@ static void *bench_mmap(const struct bench_params *p)
 	if (p->page_shift != PAGE_SHIFT_4KB)
 		flags |= MAP_HUGETLB | (p->page_shift << MAP_HUGE_SHIFT);
 
-	addr = mmap(NULL, p->size, PROT_READ | PROT_WRITE, flags, 0, 0);
+	addr = mmap(NULL, p->size, p->prot, flags, 0, 0);
 
 	return addr == MAP_FAILED ? NULL : addr;
 }
@@ -513,6 +548,9 @@ static void mmap_page_touch(void *dst, const struct bench_params *p)
 	unsigned long page_size = 1UL << p->page_shift;
 	unsigned long npages = p->size / page_size;
 
+	if (!(p->prot & (PROT_READ | PROT_WRITE)))
+		return;
+
 	for (unsigned long i = 0; i < npages; i++) {
 		char *addr = (char *)dst + page_size * i;
 		char val = (char)i;
@@ -520,8 +558,10 @@ static void mmap_page_touch(void *dst, const struct bench_params *p)
 		if (p->seed)
 			addr += rand() % page_size;
 
-		val += READ_ONCE(*addr);
-		WRITE_ONCE(*addr, val);
+		if (p->prot & PROT_READ)
+			val += READ_ONCE(*addr);
+		if (p->prot & PROT_WRITE)
+			WRITE_ONCE(*addr, val);
 	}
 }
 

-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-12-01 12:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:11 [PATCH 0/4] perf bench mem: Add support for PROT_MTE Pierre-Clément Tosi
2026-12-01 12:11 ` [PATCH 1/4] perf bench mem: Report why mmap() failed Pierre-Clément Tosi
2026-12-01 12:11 ` [PATCH 2/4] perf bench mem: Pass bench_params to bench_mmap() Pierre-Clément Tosi
2026-12-01 12:11 ` Pierre-Clément Tosi [this message]
2026-12-01 12:28   ` [PATCH 3/4] perf bench mem: Add --prot option sashiko-bot
2026-12-01 12:11 ` [PATCH 4/4] perf bench mem: Support PROT_MTE on arm64 Pierre-Clément Tosi
2026-12-01 12:28 ` [PATCH 0/4] perf bench mem: Add support for PROT_MTE sashiko-bot

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=20261201-perf-prot-mte-v1-3-4dc4e1da7460@google.com \
    --to=ptosi@google.com \
    --cc=acme@kernel.org \
    --cc=ankur.a.arora@oracle.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=namhyung@kernel.org \
    /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