Skip to content

Commit

Permalink
stress-madvise: exercise MADV_GUARD_* flags
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
  • Loading branch information
ColinIanKing committed Jan 23, 2025
1 parent 5559b85 commit 04687ff
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion stress-madvise.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ static const int madvise_options[] = {
#if defined(MADV_POPULATE_WRITE)
MADV_POPULATE_WRITE,
#endif
/* Linux 6.12 */
#if defined(MADV_GUARD_INSTALL) && \
defined(MADV_NORMAL)
MADV_GUARD_INSTALL,
#endif
#if defined(MADV_GUARD_REMOVE)
MADV_GUARD_REMOVE,
#endif
/* OpenBSD */
#if defined(MADV_SPACEAVAIL)
MADV_SPACEAVAIL,
Expand Down Expand Up @@ -359,6 +367,11 @@ static void *stress_madvise_pages(void *arg)
#if defined(MADV_FREE)
if (advise == MADV_FREE)
stress_read_proc_smaps(ctxt->smaps);
#endif
#if defined(MADV_GUARD_INSTALL) && defined(MADV_NORMAL)
/* avoid segfaults by setting back to normal */
if (advise == MADV_GUARD_INSTALL)
(void)shim_madvise(ptr, page_size, MADV_NORMAL);
#endif
(void)shim_msync(ptr, page_size, MS_ASYNC);
}
Expand All @@ -368,6 +381,11 @@ static void *stress_madvise_pages(void *arg)
const int advise = stress_random_advise(args, ptr, page_size, ctxt->hwpoison);

(void)shim_madvise(ptr, page_size, advise);
#if defined(MADV_GUARD_INSTALL) && defined(MADV_NORMAL)
/* avoid segfaults by setting back to normal */
if (advise == MADV_GUARD_INSTALL)
(void)shim_madvise(ptr, page_size, MADV_NORMAL);
#endif
(void)shim_msync(ptr, page_size, MS_ASYNC);
}

Expand Down Expand Up @@ -572,7 +590,7 @@ static int stress_madvise(stress_args_t *args)
MAP_PRIVATE, fd, 0);
} else {
buf = (uint8_t *)stress_mmap_populate(NULL, sz, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS, -1, 0);
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
}
if (buf == MAP_FAILED) {
/* Force MAP_POPULATE off, just in case */
Expand Down

0 comments on commit 04687ff

Please sign in to comment.