[PATCH 3/4] perf bench mem: Add --prot option

From: Pierre-Clément Tosi

Date: Tue Dec 01 2026 - 07:12:48 EST


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@xxxxxxxxxx>
---
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