* [LTP] [PATCH v3] Add process_madvise01 test
@ 2023-08-29 7:38 Andrea Cervesato
2023-08-30 10:45 ` Cyril Hrubis
0 siblings, 1 reply; 8+ messages in thread
From: Andrea Cervesato @ 2023-08-29 7:38 UTC (permalink / raw)
To: ltp
From: Andrea Cervesato <andrea.cervesato@suse.com>
This test checks process_madvise support for MADV_PAGEOUT. It tests
if memory pages have been swapped out by looking at smaps information
after reclaiming memory using MADV_PAGEOUT.
This test supports kernel 5.10 or later.
Implements: https://github.com/linux-test-project/ltp/issues/909
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
runtest/syscalls | 2 +
.../syscalls/process_madvise/.gitignore | 1 +
.../kernel/syscalls/process_madvise/Makefile | 7 +
.../process_madvise/process_madvise.h | 101 ++++++++++++++
.../process_madvise/process_madvise01.c | 126 ++++++++++++++++++
5 files changed, 237 insertions(+)
create mode 100644 testcases/kernel/syscalls/process_madvise/.gitignore
create mode 100644 testcases/kernel/syscalls/process_madvise/Makefile
create mode 100644 testcases/kernel/syscalls/process_madvise/process_madvise.h
create mode 100644 testcases/kernel/syscalls/process_madvise/process_madvise01.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 119710d63..64907ff53 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1046,6 +1046,8 @@ process_vm_readv03 process_vm_readv03
process_vm_writev01 process_vm01
process_vm_writev02 process_vm_writev02
+process_madvise01 process_madvise01
+
prot_hsymlinks prot_hsymlinks
dirtyc0w dirtyc0w
dirtyc0w_shmem dirtyc0w_shmem
diff --git a/testcases/kernel/syscalls/process_madvise/.gitignore b/testcases/kernel/syscalls/process_madvise/.gitignore
new file mode 100644
index 000000000..93d2640f7
--- /dev/null
+++ b/testcases/kernel/syscalls/process_madvise/.gitignore
@@ -0,0 +1 @@
+/process_madvise01
diff --git a/testcases/kernel/syscalls/process_madvise/Makefile b/testcases/kernel/syscalls/process_madvise/Makefile
new file mode 100644
index 000000000..ad5b66061
--- /dev/null
+++ b/testcases/kernel/syscalls/process_madvise/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2023 Linux Test Project, Inc.
+
+top_srcdir ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/process_madvise/process_madvise.h b/testcases/kernel/syscalls/process_madvise/process_madvise.h
new file mode 100644
index 000000000..c4570e530
--- /dev/null
+++ b/testcases/kernel/syscalls/process_madvise/process_madvise.h
@@ -0,0 +1,101 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+#ifndef PROCESS_MADVISE_H__
+#define PROCESS_MADVISE_H__
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "tst_safe_stdio.h"
+
+struct addr_mapping {
+ int size;
+ int rss;
+ int pss;
+ int shared_clean;
+ int shared_dirty;
+ int private_clean;
+ int private_dirty;
+ int referenced;
+ int anonymous;
+ int anon_huge_pages;
+ int shmem_huge_pages;
+ int shmem_pmd_mapped;
+ int swap;
+ int kernel_page_size;
+ int mmu_page_size;
+ int locked;
+ int protection_key;
+};
+
+static inline void read_address_mapping(unsigned long address, struct addr_mapping *mapping)
+{
+ FILE *f;
+ int found = 0;
+ char label[BUFSIZ];
+ char line[BUFSIZ];
+ char smaps[BUFSIZ];
+ char ptr_str[BUFSIZ];
+ int value;
+
+ snprintf(smaps, BUFSIZ, "/proc/%i/smaps", getpid());
+ snprintf(ptr_str, BUFSIZ, "%lx", address);
+
+ f = SAFE_FOPEN(smaps, "r");
+
+ while (fgets(line, BUFSIZ, f) != NULL) {
+ if (strncmp(ptr_str, line, strlen(ptr_str)) == 0)
+ found = 1;
+
+ if (!found)
+ continue;
+
+ if (found && strcmp(line, "VmFlags") >= 0)
+ break;
+
+ if (sscanf(line, "%31[^:]: %d", label, &value) > 0) {
+ if (strcmp(label, "Size") == 0)
+ mapping->size = value;
+ else if (strcmp(label, "Rss") == 0)
+ mapping->rss = value;
+ else if (strcmp(label, "Pss") == 0)
+ mapping->pss = value;
+ else if (strcmp(label, "Shared_Clean") == 0)
+ mapping->shared_clean = value;
+ else if (strcmp(label, "Shared_Dirty") == 0)
+ mapping->shared_dirty = value;
+ else if (strcmp(label, "Private_Clean") == 0)
+ mapping->private_clean = value;
+ else if (strcmp(label, "Private_Dirty") == 0)
+ mapping->private_dirty = value;
+ else if (strcmp(label, "Referenced") == 0)
+ mapping->referenced = value;
+ else if (strcmp(label, "Anonymous") == 0)
+ mapping->anonymous = value;
+ else if (strcmp(label, "AnonHugePages") == 0)
+ mapping->anon_huge_pages = value;
+ else if (strcmp(label, "ShmemHugePages") == 0)
+ mapping->shmem_huge_pages = value;
+ else if (strcmp(label, "ShmemPmdMapped") == 0)
+ mapping->shmem_pmd_mapped = value;
+ else if (strcmp(label, "Swap") == 0)
+ mapping->swap = value;
+ else if (strcmp(label, "KernelPageSize") == 0)
+ mapping->kernel_page_size = value;
+ else if (strcmp(label, "MMUPageSize") == 0)
+ mapping->mmu_page_size = value;
+ else if (strcmp(label, "Locked") == 0)
+ mapping->locked = value;
+ else if (strcmp(label, "ProtectionKey") == 0)
+ mapping->protection_key = value;
+ }
+ }
+
+ SAFE_FCLOSE(f);
+}
+
+#endif
diff --git a/testcases/kernel/syscalls/process_madvise/process_madvise01.c b/testcases/kernel/syscalls/process_madvise/process_madvise01.c
new file mode 100644
index 000000000..d79da650c
--- /dev/null
+++ b/testcases/kernel/syscalls/process_madvise/process_madvise01.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Allocate anonymous memory pages inside child and reclaim it with
+ * MADV_PAGEOUT. Then check if memory pages have been swapped out by looking
+ * at smaps information.
+ *
+ * The advice might be ignored for some pages in the range when it is
+ * not applicable, so test passes if swap memory increases after
+ * reclaiming memory with MADV_PAGEOUT.
+ */
+
+#define _GNU_SOURCE
+
+#include <sys/mman.h>
+#include "tst_test.h"
+#include "lapi/mmap.h"
+#include "lapi/syscalls.h"
+#include "process_madvise.h"
+
+#define MEM_CHILD (1 * TST_MB)
+
+static void **data_ptr;
+
+static void child_alloc(void)
+{
+ char data[MEM_CHILD];
+ struct addr_mapping map_before;
+ struct addr_mapping map_after;
+
+ memset(data, 'a', MEM_CHILD);
+
+ tst_res(TINFO, "Allocate memory: %d bytes", MEM_CHILD);
+
+ *data_ptr = SAFE_MMAP(NULL, MEM_CHILD,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+
+ memset(*data_ptr, 'a', MEM_CHILD);
+
+ memset(&map_before, 0, sizeof(struct addr_mapping));
+ read_address_mapping((unsigned long)*data_ptr, &map_before);
+
+ TST_CHECKPOINT_WAKE_AND_WAIT(0);
+
+ memset(&map_after, 0, sizeof(struct addr_mapping));
+ read_address_mapping((unsigned long)*data_ptr, &map_after);
+
+ if (memcmp(*data_ptr, data, MEM_CHILD) != 0) {
+ tst_res(TFAIL, "Dirty memory after reclaiming it");
+ return;
+ }
+
+ SAFE_MUNMAP(*data_ptr, MEM_CHILD);
+ *data_ptr = NULL;
+
+ TST_EXP_EXPR(map_before.swap < map_after.swap,
+ "Most of the memory has been swapped out: %dkB out of %dkB",
+ map_after.swap,
+ MEM_CHILD / TST_KB);
+}
+
+static void setup(void)
+{
+ data_ptr = SAFE_MMAP(NULL, sizeof(void *),
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+}
+
+static void cleanup(void)
+{
+ if (*data_ptr)
+ SAFE_MUNMAP(*data_ptr, MEM_CHILD);
+
+ if (data_ptr)
+ SAFE_MUNMAP(data_ptr, sizeof(void *));
+}
+
+static void run(void)
+{
+ int ret;
+ int pidfd;
+ pid_t pid_alloc;
+ struct iovec vec;
+
+ pid_alloc = SAFE_FORK();
+ if (!pid_alloc) {
+ child_alloc();
+ return;
+ }
+
+ TST_CHECKPOINT_WAIT(0);
+
+ tst_res(TINFO, "Reclaim memory using MADV_PAGEOUT");
+
+ pidfd = SAFE_PIDFD_OPEN(pid_alloc, 0);
+
+ vec.iov_base = *data_ptr;
+ vec.iov_len = MEM_CHILD;
+
+ ret = tst_syscall(__NR_process_madvise, pidfd, &vec, 1UL,
+ MADV_PAGEOUT, 0UL);
+
+ if (ret == -1)
+ tst_brk(TBROK | TERRNO, "process_madvise failed");
+
+ if (ret != MEM_CHILD)
+ tst_brk(TBROK, "process_madvise reclaimed only %d bytes", ret);
+
+ TST_CHECKPOINT_WAKE(0);
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run,
+ .forks_child = 1,
+ .min_kver = "5.10",
+ .needs_checkpoints = 1,
+ .needs_root = 1,
+};
--
2.35.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v3] Add process_madvise01 test
2023-08-29 7:38 [LTP] [PATCH v3] Add process_madvise01 test Andrea Cervesato
@ 2023-08-30 10:45 ` Cyril Hrubis
2023-09-01 10:37 ` Petr Vorel
0 siblings, 1 reply; 8+ messages in thread
From: Cyril Hrubis @ 2023-08-30 10:45 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi!
> + TST_EXP_EXPR(map_before.swap < map_after.swap,
> + "Most of the memory has been swapped out: %dkB out of %dkB",
> + map_after.swap,
Although it's unlikely that map_before.swap will be non-zero we should
print the difference here, so I've changed this part to:
map_after.swap - map_before.swap
And pushed, thanks.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v3] Add process_madvise01 test
2023-08-30 10:45 ` Cyril Hrubis
@ 2023-09-01 10:37 ` Petr Vorel
2023-09-01 12:16 ` Cyril Hrubis
0 siblings, 1 reply; 8+ messages in thread
From: Petr Vorel @ 2023-09-01 10:37 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp
HI Andrea, Cyril,
> Hi!
> > + TST_EXP_EXPR(map_before.swap < map_after.swap,
> > + "Most of the memory has been swapped out: %dkB out of %dkB",
> > + map_after.swap,
> Although it's unlikely that map_before.swap will be non-zero we should
> print the difference here, so I've changed this part to:
> map_after.swap - map_before.swap
> And pushed, thanks.
FYI this new test is failing on current openSUSE Tumbleweed (kernel 6.4.12-1):
tst_test.c:1559: TINFO: Timeout per run is 0h 00m 30s
process_madvise01.c:38: TINFO: Allocate memory: 1048576 bytes
process_madvise01.c:99: TINFO: Reclaim memory using MADV_PAGEOUT
process_madvise01.c:62: TFAIL: Expect: Most of the memory has been swapped out: 0kB out of 1024kB
Summary:
passed 0
failed 1
broken 0
skipped 0
warnings 0
Test works well on current Debian unstable with kernel 6.4.11.
Could you please have a look if it's a test or a kernel bug?
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v3] Add process_madvise01 test
2023-09-01 10:37 ` Petr Vorel
@ 2023-09-01 12:16 ` Cyril Hrubis
2023-09-01 12:31 ` Petr Vorel
0 siblings, 1 reply; 8+ messages in thread
From: Cyril Hrubis @ 2023-09-01 12:16 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Hi!
> > > + TST_EXP_EXPR(map_before.swap < map_after.swap,
> > > + "Most of the memory has been swapped out: %dkB out of %dkB",
> > > + map_after.swap,
>
> > Although it's unlikely that map_before.swap will be non-zero we should
> > print the difference here, so I've changed this part to:
>
> > map_after.swap - map_before.swap
>
> > And pushed, thanks.
>
> FYI this new test is failing on current openSUSE Tumbleweed (kernel 6.4.12-1):
>
> tst_test.c:1559: TINFO: Timeout per run is 0h 00m 30s
> process_madvise01.c:38: TINFO: Allocate memory: 1048576 bytes
> process_madvise01.c:99: TINFO: Reclaim memory using MADV_PAGEOUT
> process_madvise01.c:62: TFAIL: Expect: Most of the memory has been swapped out: 0kB out of 1024kB
>
> Summary:
> passed 0
> failed 1
> broken 0
> skipped 0
> warnings 0
>
> Test works well on current Debian unstable with kernel 6.4.11.
> Could you please have a look if it's a test or a kernel bug?
Does the machine where it fails even have any swap?
I suppose that we should check for:
1) CONFIG_SWAP in the kernel .config
2) /proc/meminfo SwapFree: > $pagesize kB
I guess that the madvise kicks off the kernel swapper, but it may take
some time for the memory to be actually swapped.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v3] Add process_madvise01 test
2023-09-01 12:16 ` Cyril Hrubis
@ 2023-09-01 12:31 ` Petr Vorel
2023-09-01 12:36 ` Cyril Hrubis
0 siblings, 1 reply; 8+ messages in thread
From: Petr Vorel @ 2023-09-01 12:31 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp
> Hi!
> > > > + TST_EXP_EXPR(map_before.swap < map_after.swap,
> > > > + "Most of the memory has been swapped out: %dkB out of %dkB",
> > > > + map_after.swap,
> > > Although it's unlikely that map_before.swap will be non-zero we should
> > > print the difference here, so I've changed this part to:
> > > map_after.swap - map_before.swap
> > > And pushed, thanks.
> > FYI this new test is failing on current openSUSE Tumbleweed (kernel 6.4.12-1):
> > tst_test.c:1559: TINFO: Timeout per run is 0h 00m 30s
> > process_madvise01.c:38: TINFO: Allocate memory: 1048576 bytes
> > process_madvise01.c:99: TINFO: Reclaim memory using MADV_PAGEOUT
> > process_madvise01.c:62: TFAIL: Expect: Most of the memory has been swapped out: 0kB out of 1024kB
> > Summary:
> > passed 0
> > failed 1
> > broken 0
> > skipped 0
> > warnings 0
> > Test works well on current Debian unstable with kernel 6.4.11.
> > Could you please have a look if it's a test or a kernel bug?
> Does the machine where it fails even have any swap?
I has CONFIG_SWAP=y, but vmstat reports si and so as 0.
Therefore machine probably does not have any swap.
> I suppose that we should check for:
> 1) CONFIG_SWAP in the kernel .config
> 2) /proc/meminfo SwapFree: > $pagesize kB
+1.
> I guess that the madvise kicks off the kernel swapper, but it may take
> some time for the memory to be actually swapped.
Sounds reasonable.
BTW I wonder if we should add 'vmstat' or 'cat /proc/meminfo' to ver_linux.
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v3] Add process_madvise01 test
2023-09-01 12:31 ` Petr Vorel
@ 2023-09-01 12:36 ` Cyril Hrubis
2023-09-01 12:52 ` Andrea Cervesato via ltp
0 siblings, 1 reply; 8+ messages in thread
From: Cyril Hrubis @ 2023-09-01 12:36 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Hi!
> BTW I wonder if we should add 'vmstat' or 'cat /proc/meminfo' to ver_linux.
cat /proc/meminfo sounds like a good idea.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v3] Add process_madvise01 test
2023-09-01 12:36 ` Cyril Hrubis
@ 2023-09-01 12:52 ` Andrea Cervesato via ltp
2023-09-01 12:53 ` Cyril Hrubis
0 siblings, 1 reply; 8+ messages in thread
From: Andrea Cervesato via ltp @ 2023-09-01 12:52 UTC (permalink / raw)
To: Cyril Hrubis, Petr Vorel; +Cc: ltp
Hi!
On 9/1/23 14:36, Cyril Hrubis wrote:
> Hi!
>> BTW I wonder if we should add 'vmstat' or 'cat /proc/meminfo' to ver_linux.
> cat /proc/meminfo sounds like a good idea.
>
Maybe do we need a flag in tst_test to check if swap is available or
not? Something similar to .min_mem_avail.
Andrea Cervesato
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v3] Add process_madvise01 test
2023-09-01 12:52 ` Andrea Cervesato via ltp
@ 2023-09-01 12:53 ` Cyril Hrubis
0 siblings, 0 replies; 8+ messages in thread
From: Cyril Hrubis @ 2023-09-01 12:53 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi!
> >> BTW I wonder if we should add 'vmstat' or 'cat /proc/meminfo' to ver_linux.
> > cat /proc/meminfo sounds like a good idea.
> >
> Maybe do we need a flag in tst_test to check if swap is available or
> not? Something similar to .min_mem_avail.
Sounds reasonable to me, that way we would get the information
propagated into metadata as well. Feel free to send a patch.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2023-09-01 12:53 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-29 7:38 [LTP] [PATCH v3] Add process_madvise01 test Andrea Cervesato
2023-08-30 10:45 ` Cyril Hrubis
2023-09-01 10:37 ` Petr Vorel
2023-09-01 12:16 ` Cyril Hrubis
2023-09-01 12:31 ` Petr Vorel
2023-09-01 12:36 ` Cyril Hrubis
2023-09-01 12:52 ` Andrea Cervesato via ltp
2023-09-01 12:53 ` Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox