* [PATCH] perf test amd ibs: avoid using executable heap
@ 2026-07-01 5:48 Ondrej Mosnacek
2026-07-01 5:53 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Ondrej Mosnacek @ 2026-07-01 5:48 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, James Clark, Ravi Bangoria, linux-perf-users,
selinux, linux-kernel
Making [parts of] the heap executable is dangerous and is blocked by
SELinux on Fedora/RHEL even for an unconfined user. Replace the malloc()
+ mprotect() combo with just mmap(), creating a private anonymous rwx
mapping, which only requires the more commonly allowed "execmem"
permission under SELinux (things like JIT or regex compilation need it
as well). mmap() with MAP_ANONYMOUS will give us a zeroed mapping that
begins on a page boundary, so the result is equivalent to the original
code even without a memset() or the page-alignment dance.
Verified that the test still passes on a machine with an AMD CPU that
has the "ibs" CPU flag.
Fixes: 35db59fa8ea2 ("perf test amd ibs: Add sample period unit test")
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
---
tools/perf/arch/x86/tests/amd-ibs-period.c | 20 ++++++--------------
1 file changed, 6 insertions(+), 14 deletions(-)
diff --git a/tools/perf/arch/x86/tests/amd-ibs-period.c b/tools/perf/arch/x86/tests/amd-ibs-period.c
index 6a92b3a23ed7a..b2210b1aa09fb 100644
--- a/tools/perf/arch/x86/tests/amd-ibs-period.c
+++ b/tools/perf/arch/x86/tests/amd-ibs-period.c
@@ -46,7 +46,6 @@ static int dummy_workload_1(unsigned long count)
{
int (*func)(void);
int ret = 0;
- char *p;
char insn1[] = {
0xb8, 0x01, 0x00, 0x00, 0x00, /* mov 1,%eax */
0xc3, /* ret */
@@ -59,18 +58,11 @@ static int dummy_workload_1(unsigned long count)
0xcc, /* int 3 */
};
- p = calloc(2, page_size);
- if (!p) {
- printf("malloc() failed. %m");
- return 1;
- }
-
- func = (void *)((unsigned long)(p + page_size - 1) & ~(page_size - 1));
-
- ret = mprotect(func, page_size, PROT_READ | PROT_WRITE | PROT_EXEC);
- if (ret) {
- printf("mprotect() failed. %m");
- goto out;
+ func = mmap(NULL, page_size, PROT_READ | PROT_WRITE | PROT_EXEC,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (!func) {
+ pr_debug("mmap() failed. %m\n");
+ return -1;
}
if (count < 100000)
@@ -93,7 +85,7 @@ static int dummy_workload_1(unsigned long count)
}
out:
- free(p);
+ munmap(func, page_size);
return ret;
}
--
2.54.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] perf test amd ibs: avoid using executable heap
2026-07-01 5:48 [PATCH] perf test amd ibs: avoid using executable heap Ondrej Mosnacek
@ 2026-07-01 5:53 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-07-01 5:53 UTC (permalink / raw)
To: Ondrej Mosnacek; +Cc: selinux, linux-perf-users
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incorrect error check for mmap() return value leads to segmentation fault.
--
commit 40ff2c8c34df3809a114852f1d0dd5630a45771c
Author: Ondrej Mosnacek <omosnace@redhat.com>
perf test amd ibs: avoid using executable heap
This commit replaces the malloc() and mprotect() combination with a single
mmap() call for creating executable memory in the AMD IBS test. This change
avoids SELinux executable heap restrictions by directly creating an
anonymous rwx mapping.
> diff --git a/tools/perf/arch/x86/tests/amd-ibs-period.c b/tools/perf/arch/x86/tests/amd-ibs-period.c
> --- a/tools/perf/arch/x86/tests/amd-ibs-period.c
> +++ b/tools/perf/arch/x86/tests/amd-ibs-period.c
[ ... ]
> @@ -59,18 +58,11 @@ static int dummy_workload_1(unsigned long count)
> + func = mmap(NULL, page_size, PROT_READ | PROT_WRITE | PROT_EXEC,
> + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> + if (!func) {
[Severity: Medium]
Does this error check properly handle mmap() failures? mmap() returns
MAP_FAILED ((void *)-1) rather than NULL when it fails.
If mmap() fails (e.g., due to SELinux denying execmem), this check will
evaluate to false, and the code will fall through to memcpy() into an
invalid address, triggering a segmentation fault.
> + pr_debug("mmap() failed. %m\n");
> + return -1;
> }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260701054807.508994-1-omosnace@redhat.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-01 5:53 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-01 5:48 [PATCH] perf test amd ibs: avoid using executable heap Ondrej Mosnacek
2026-07-01 5:53 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox