[PATCH v4 05/13] perf bench mem: Switch from zalloc() to mmap()

From: Ankur Arora
Date: Mon Jun 16 2025 - 01:23:42 EST


Using mmap() ensures that the buffer is always aligned at a fixed
boundary. Switch to that to remove one source of variability.

Since we always want to read/write from the the allocated buffers map
with pagetables pre-populated.

Signed-off-by: Ankur Arora <ankur.a.arora@xxxxxxxxxx>
---
tools/perf/bench/mem-functions.c | 27 ++++++++++++++++++++++-----
1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/tools/perf/bench/mem-functions.c b/tools/perf/bench/mem-functions.c
index 06d3ee6f5d69..914f9048d982 100644
--- a/tools/perf/bench/mem-functions.c
+++ b/tools/perf/bench/mem-functions.c
@@ -22,9 +22,9 @@
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
+#include <sys/mman.h>
#include <errno.h>
#include <linux/time64.h>
-#include <linux/zalloc.h>

#define K 1024

@@ -285,16 +285,33 @@ static int do_memcpy(const struct function *r, struct bench_params *p,
return 0;
}

+static void *bench_mmap(size_t size, bool populate)
+{
+ void *p;
+ int extra = populate ? MAP_POPULATE : 0;
+
+ p = mmap(NULL, size, PROT_READ|PROT_WRITE,
+ extra | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+
+ return p == MAP_FAILED ? NULL : p;
+}
+
+static void bench_munmap(void *p, size_t size)
+{
+ if (p)
+ munmap(p, size);
+}
+
static bool mem_alloc(struct bench_mem_info *info, struct bench_params *p,
void **src, void **dst)
{
bool failed;

- *dst = zalloc(p->size);
+ *dst = bench_mmap(p->size, true);
failed = *dst == NULL;

if (info->alloc_src) {
- *src = zalloc(p->size);
+ *src = bench_mmap(p->size, true);
failed = failed || *src == NULL;
}

@@ -305,8 +322,8 @@ static void mem_free(struct bench_mem_info *info __maybe_unused,
struct bench_params *p __maybe_unused,
void **src, void **dst)
{
- free(*dst);
- free(*src);
+ bench_munmap(*dst, p->size);
+ bench_munmap(*src, p->size);

*dst = *src = NULL;
}
--
2.31.1