Skip to contentSkip to conversation

madvice: new case for madvise(WILLNEED)

PATCH Superseded Li Wang 2 participants 4 messages Mar 29, 2016 – Apr 1, 2016

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH v2 1/1 madvice: new case for madvise(WILLNEED)

Page fault occurs in spite that madvise(WILLNEED) system call is called to prefetch the page. This issue is reproduced by running a program which sequentially accesses to a shared memory and calls madvise(WILLNEED) to the next page on a page fault.

Fixed by commit:
55231e5c898 mm: madvise: fix MADV_WILLNEED on shmem swapouts

---

Notes:
V1 --> V2
* set the TST_TOTAL = 3, run with '-i 100' can work
* declare the addr array as char *dst[100]
* create sysinfo struct sys_buf on the stack in setup()
* use mmap() with 'MAP_SHARED | MAP_ANONYMOUS' instead shmat()
* take use of the assigment-supression operator '*' of fscanf
* remove fflush(stdout), free_shm(), cleanup()

 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/.gitignore          |   1 +
 testcases/kernel/syscalls/madvise/madvise06.c | 172 ++++++++++++++++++++++++++
 3 files changed, 174 insertions(+)
 create mode 100644 testcases/kernel/syscalls/madvise/madvise06.c
runtest/syscalls +1 -0
index b41c927..732c2ca 100644
@@ -743,6 +743,7 @@ madvise02 madvise02
 madvise03 madvise03
 madvise04 madvise04
 madvise05 madvise05
+madvise06 madvise06
 
 newuname01 newuname01
 
testcases/kernel/syscalls/.gitignore +1 -0
index 0540928..ffa5db1 100644
@@ -504,6 +504,7 @@
 /madvise/madvise03
 /madvise/madvise04
 /madvise/madvise05
+/madvise/madvise06
 /mallopt/mallopt01
 /mbind/mbind01
 /memcmp/memcmp01
testcases/kernel/syscalls/madvise/madvise06.c new file +172 -0

172 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/1 madvice: new case for madvise(WILLNEED)

Hi!

Quoted 27 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +27 -0
+/*
+ * Copyright (c) 2016 Red Hat, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * DESCRIPTION
+ *
+ *   Page fault occurs in spite that madvise(WILLNEED) system call is called
+ *   to prefetch the page. This issue is reproduced by running a program
+ *   which sequentially accesses to a shared memory and calls madvise(WILLNEED)
+ *   to the next page on a page fault.
+ *
+ *   This bug is present in all RHEL7 versions. It looks like this was fixed in
+ *   mainline kernel > v3.15 by the following patch:

testcases/kernel/syscalls/madvise/madvise06.c line 37

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +10 -0
+ *
+ *   commit 55231e5c898c5c03c14194001e349f40f59bd300
+ *   Author: Johannes Weiner <[email protected]>
+ *   Date:   Thu May 22 11:54:17 2014 -0700
+ *
+ *       mm: madvise: fix MADV_WILLNEED on shmem swapouts
+ */
+
+#include <stdio.h>
+#include <sys/shm.h>

We don't need this header now, right?

Quoted 23 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +23 -0
+#include <errno.h>
+#include <sys/sysinfo.h>
+
+#include "test.h"
+#include "safe_macros.h"
+
+char *TCID = "madvise06";
+int TST_TOTAL = 3;
+
+#ifdef __x86_64__
+
+#define GB_SZ  (1024*1024*1024)
+#define PG_SZ  (4*1024)
+
+static long dst_num;
+
+static void setup(void);
+static int  get_page_fault_num(void);
+static void test_advice_willneed(void);
+
+int main(int argc, char *argv[])
+{
+	int i, lc;

testcases/kernel/syscalls/madvise/madvise06.c line 70

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +10 -0
+
+	tst_parse_opts(argc, argv, NULL, NULL);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+		tst_count = 0;
+
+		for(i = 0; i < TST_TOTAL; i++)
+			test_advice_willneed();

So we do three iterations of the test to increase the likehood of hitting the bug, right? In that case we should just add -i 3 to the runtest file.

testcases/kernel/syscalls/madvise/madvise06.c line 83

Quoted 13 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +13 -0
+	}
+
+	tst_exit();
+}
+
+static void setup(void)
+{
+	struct sysinfo sys_buf;
+
+	sysinfo(&sys_buf);
+
+	dst_num = sys_buf.totalram / GB_SZ;
+	tst_resm(TINFO, "dst_num =  %ld", dst_num);

The variable should be named as dst_max instead of dst_num. Which would make the message more understandable...

Quoted 28 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +28 -0
+	tst_sig(NOFORK, DEF_HANDLER, NULL);
+	if (tst_kvercmp(3, 9, 0) < 0)
+		tst_brkm(TCONF, NULL, "madvise(MADV_WILLNEED) swap file"
+			"prefetch available only since 3.9");
+
+	if (sys_buf.totalram < 2L * GB_SZ)
+		tst_brkm(TCONF, NULL, "Test requires more than 2GB of RAM.");
+	if (sys_buf.totalram > 100L * GB_SZ)
+		tst_brkm(TCONF, NULL, "System RAM is too large, skip this test.");
+
+	TEST_PAUSE;
+}
+
+static int get_page_fault_num(void)
+{
+	int pg;
+
+	SAFE_FILE_SCANF(NULL, "/proc/self/stat",
+			"%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %d",
+			&pg);
+
+	return pg;
+}
+
+static void test_advice_willneed(void)
+{
+	int i;
+	char *src_1gb;

testcases/kernel/syscalls/madvise/madvise06.c line 122

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +10 -0
+	char *dst[100];
+	int page_fault_num1;
+	int page_fault_num2;
+
+	/* allocate source memory (1GB only) */
+	src_1gb = mmap(NULL, 1 * GB_SZ, PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_ANONYMOUS,
+			-1, 0);
+	if (src_1gb == MAP_FAILED)
+		tst_brkm(TFAIL | TERRNO, NULL, "mmap failed");

Use SAFE_MMAP()

testcases/kernel/syscalls/madvise/madvise06.c line 130

Quoted 7 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +7 -0
+	/* allocate destination memory (array) */
+	for (i = 0; i < dst_num; ++i) {
+		dst[i] = mmap(NULL, 1 * GB_SZ, PROT_READ | PROT_WRITE,
+				MAP_SHARED | MAP_ANONYMOUS,
+				-1, 0);
+		if (dst[i] == MAP_FAILED)
+			tst_brkm(TFAIL | TERRNO, NULL, "mmap failed");

Here as well.

testcases/kernel/syscalls/madvise/madvise06.c line 143

Quoted 13 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +13 -0
+	}
+
+	/* memmove  source to each destination memories (for SWAP-OUT) */
+	for (i = 0; i < dst_num; ++i)
+		memmove(dst[i], src_1gb, 1 * GB_SZ);
+
+	tst_resm(TINFO, "PageFault(before madvice): %d", get_page_fault_num());
+
+	/* Do madvice() to dst[0] */
+	TEST(madvise(dst[0], PG_SZ, MADV_WILLNEED));
+	if (TEST_RETURN == -1)
+		tst_brkm(TBROK | TERRNO, NULL, "madvise failed");
+	usleep(1000);  /* wait for read from SWAP */

Again, what exactly do we wait here for?

"wait for read from SWAP" is a bit too vague.

testcases/kernel/syscalls/madvise/madvise06.c line 146

Quoted 2 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +2 -0
+	page_fault_num1 = get_page_fault_num();
+	tst_resm(TINFO, "PageFault(after madvice / before Mem Access): %d", page_fault_num1);

This line is over 80 chars. Try making the message shorter.

You can use checkpatch.pl (shipped with Linux kernel) to check patches before submission.

testcases/kernel/syscalls/madvise/madvise06.c line 150

Quoted 3 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +3 -0
+	*dst[0] = 'a';
+	page_fault_num2 = get_page_fault_num();
+	tst_resm(TINFO, "PageFault(after madvice / after Mem Access): %d", page_fault_num2);

Here as well.

testcases/kernel/syscalls/madvise/madvise06.c line 155

Quoted 5 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +5 -0
+
+	if (page_fault_num1 != page_fault_num2)
+		tst_resm(TFAIL, "Bug has been reproduced!");
+	else
+		tst_resm(TPASS, "Regression test pass!");

No need for the ! in the TPASS message.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (munmap(src_1gb, 1 * GB_SZ) == -1)
+		tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");

Use SAFE_MUNMAP()

testcases/kernel/syscalls/madvise/madvise06.c line 162

Quoted 3 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +3 -0
+	for (i = 0; i < dst_num; ++i) {
+		if (munmap(dst[i], 1 * GB_SZ) == -1)
+			tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");

Here as well.

Quoted 13 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +10 -0
+	}
+}
+
+
+#else
+int main(void)
+{
+	tst_brkm(TCONF, NULL, "Only test on x86_64.");
+}
+#endif
Signature · 1 line
1.8.3.1
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH v2 1/1 madvice: new case for madvise(WILLNEED)

Hi,

Quoted 7 lines · Cyril Hrubis

Hi!
...

testcases/kernel/syscalls/madvise/madvise06.c line 37

Quoted 2 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +2 -0
+#include <stdio.h>
+#include <sys/shm.h>

We don't need this header now, right?

right! I forgot to remove that.

Quoted 30 lines
Quoted 30 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +29 -0
+#include <errno.h>
+#include <sys/sysinfo.h>
+
+#include "test.h"
+#include "safe_macros.h"
+
+char *TCID = "madvise06";
+int TST_TOTAL = 3;
+
+#ifdef __x86_64__
+
+#define GB_SZ  (1024*1024*1024)
+#define PG_SZ  (4*1024)
+
+static long dst_num;
+
+static void setup(void);
+static int  get_page_fault_num(void);
+static void test_advice_willneed(void);
+
+int main(int argc, char *argv[])
+{
+     int i, lc;
+
+     tst_parse_opts(argc, argv, NULL, NULL);
+
+     setup();
+
+     for (lc = 0; TEST_LOOPING(lc); lc++) {
Quoted 9 lines

testcases/kernel/syscalls/madvise/madvise06.c line 70

Quoted 4 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +4 -0
+             tst_count = 0;
+
+             for(i = 0; i < TST_TOTAL; i++)
+                     test_advice_willneed();

So we do three iterations of the test to increase the likehood of hitting the bug, right? In that case we should just add -i 3 to the runtest file.

ok. Even no need to add -i 3 in runtest file, It is easy to reproduce the issue with un-patched kernel. I will set the TST_TOTAL = 1.

Quoted 18 lines
Quoted 4 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +3 -0
+     }
+
+     tst_exit();

testcases/kernel/syscalls/madvise/madvise06.c line 83

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +10 -0
+}
+
+static void setup(void)
+{
+     struct sysinfo sys_buf;
+
+     sysinfo(&sys_buf);
+
+     dst_num = sys_buf.totalram / GB_SZ;
+     tst_resm(TINFO, "dst_num =  %ld", dst_num);

The variable should be named as dst_max instead of dst_num. Which would make the message more understandable...

agreed.

Quoted 69 lines

testcases/kernel/syscalls/madvise/madvise06.c

Quoted 8 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +7 -0
+     tst_sig(NOFORK, DEF_HANDLER, NULL);
+     if (tst_kvercmp(3, 9, 0) < 0)
+             tst_brkm(TCONF, NULL, "madvise(MADV_WILLNEED) swap file"
+                     "prefetch available only since 3.9");
+
+     if (sys_buf.totalram < 2L * GB_SZ)
+             tst_brkm(TCONF, NULL, "Test requires more than 2GB of

RAM.");

Review

Quoted 2 lines
(quoted diff) +2 -0
+     if (sys_buf.totalram > 100L * GB_SZ)
+             tst_brkm(TCONF, NULL, "System RAM is too large, skip this

test.");

Quoted 19 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +19 -0
+
+     TEST_PAUSE;
+}
+
+static int get_page_fault_num(void)
+{
+     int pg;
+
+     SAFE_FILE_SCANF(NULL, "/proc/self/stat",
+                     "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %d",
+                     &pg);
+
+     return pg;
+}
+
+static void test_advice_willneed(void)
+{
+     int i;
+     char *src_1gb;

testcases/kernel/syscalls/madvise/madvise06.c line 122

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +10 -0
+     char *dst[100];
+     int page_fault_num1;
+     int page_fault_num2;
+
+     /* allocate source memory (1GB only) */
+     src_1gb = mmap(NULL, 1 * GB_SZ, PROT_READ | PROT_WRITE,
+                     MAP_SHARED | MAP_ANONYMOUS,
+                     -1, 0);
+     if (src_1gb == MAP_FAILED)
+             tst_brkm(TFAIL | TERRNO, NULL, "mmap failed");

Use SAFE_MMAP()

testcases/kernel/syscalls/madvise/madvise06.c line 130

Quoted 7 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +7 -0
+     /* allocate destination memory (array) */
+     for (i = 0; i < dst_num; ++i) {
+             dst[i] = mmap(NULL, 1 * GB_SZ, PROT_READ | PROT_WRITE,
+                             MAP_SHARED | MAP_ANONYMOUS,
+                             -1, 0);
+             if (dst[i] == MAP_FAILED)
+                     tst_brkm(TFAIL | TERRNO, NULL, "mmap failed");

Here as well.

testcases/kernel/syscalls/madvise/madvise06.c

Quoted 7 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +7 -0
+     }
+
+     /* memmove  source to each destination memories (for SWAP-OUT) */
+     for (i = 0; i < dst_num; ++i)
+             memmove(dst[i], src_1gb, 1 * GB_SZ);
+
+     tst_resm(TINFO, "PageFault(before madvice): %d",

get_page_fault_num());

testcases/kernel/syscalls/madvise/madvise06.c line 143

Quoted 6 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +6 -0
+
+     /* Do madvice() to dst[0] */
+     TEST(madvise(dst[0], PG_SZ, MADV_WILLNEED));
+     if (TEST_RETURN == -1)
+             tst_brkm(TBROK | TERRNO, NULL, "madvise failed");
+     usleep(1000);  /* wait for read from SWAP */
Quoted 4 lines

Again, what exactly do we wait here for?

"wait for read from SWAP" is a bit too vague.

okay, will remove the sleep() function.

Quoted 28 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+     page_fault_num1 = get_page_fault_num();
+     tst_resm(TINFO, "PageFault(after madvice / before Mem Access):

%d", page_fault_num1);

This line is over 80 chars. Try making the message shorter.

You can use checkpatch.pl (shipped with Linux kernel) to check patches before submission.

testcases/kernel/syscalls/madvise/madvise06.c

Quoted 3 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +3 -0
+     *dst[0] = 'a';
+     page_fault_num2 = get_page_fault_num();
+     tst_resm(TINFO, "PageFault(after madvice / after Mem Access): %d",

page_fault_num2);

Here as well.

testcases/kernel/syscalls/madvise/madvise06.c line 155

Quoted 5 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +5 -0
+
+     if (page_fault_num1 != page_fault_num2)
+             tst_resm(TFAIL, "Bug has been reproduced!");
+     else
+             tst_resm(TPASS, "Regression test pass!");

No need for the ! in the TPASS message.

Review

Quoted 2 lines
(quoted diff) +2 -0
+     if (munmap(src_1gb, 1 * GB_SZ) == -1)
+             tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
Quoted 9 lines

Use SAFE_MUNMAP()

testcases/kernel/syscalls/madvise/madvise06.c line 162

Quoted 3 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +3 -0
+     for (i = 0; i < dst_num; ++i) {
+             if (munmap(dst[i], 1 * GB_SZ) == -1)
+                     tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");

Here as well.

accepted all the other suggestions, will fix the format isssue in patch V3.

Thank you.

Signature · 6 lines
Regards,
Li Wang
Email: [email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20160401/bade8ec9/attachment.html>
author
#3 lore raw

PATCH v2 1/1 madvice: new case for madvise(WILLNEED)

Hello,

Quoted 6 lines

+ if (tst_kvercmp(3, 9, 0) < 0)

Quoted 4 lines
Quoted 3 lines
testcases/kernel/syscalls/madvise/madvise06.c new file +3 -0
+             tst_brkm(TCONF, NULL, "madvise(MADV_WILLNEED) swap file"
+                     "prefetch available only since 3.9");
+

I take use of these lines since I misunderstood that 'MASV_WILLNEED' is involved since 3.9 kernel when I took a look at madvise05.c. But after reading the MADVISE(2) in Linux Programmer's Manual, I realized that I was wrong.

Then I tried this madvise06 testcase on both 2.6.18 and 2.6.32 kernel, it also works well. So I decide to remove the lines in Patch V3.

Signature · 6 lines
Regards,
Li Wang
Email: [email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20160401/4d72b77b/attachment.html>