Skip to contentSkip to conversation

msync04: Check disk content if dirty bit check failed

PATCH In review Wei Gao via ltp 3 participants 6 messages May 22, 2024 – May 29, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 msync04: Check disk content if dirty bit check failed

msync04 test is inherently racy and nothing guarantees that the page is not written out before get_dirty_page() manages to read the page state. Hence the test should be reworked to verify the page contents is on disk when it finds dirty bit isn't set anymore...

---
 testcases/kernel/syscalls/msync/msync04.c | 34 ++++++++++++++---------
 1 file changed, 21 insertions(+), 13 deletions(-)
testcases/kernel/syscalls/msync/msync04.c +21 -13
index 72ddc27a4..c296c8b20 100644
@@ -46,6 +46,7 @@ uint64_t get_dirty_bit(void *data)
 static void test_msync(void)
 {
 	uint64_t dirty;
+	char buffer[20];
 
 	test_fd = SAFE_OPEN("msync04/testfile", O_CREAT | O_TRUNC | O_RDWR,
 		0644);
@@ -56,20 +57,27 @@ static void test_msync(void)
 	mmaped_area[8] = 'B';
 	dirty = get_dirty_bit(mmaped_area);
 	if (!dirty) {
-		tst_res(TFAIL, "Expected dirty bit to be set after writing to"
-				" mmap()-ed area");
-		goto clean;
-	}
-	if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
-		tst_res(TFAIL | TERRNO, "msync() failed");
-		goto clean;
+		tst_res(TINFO, "Not see dirty bit so we check content of file instead");
+		test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY);
+		SAFE_READ(0, test_fd, buffer, 9);
+		if (buffer[8] == 'B')
+			tst_res(TCONF, "write file ok but msync couldn't be tested"
+				" because the storage was written to too quickly");
+		else
+			tst_res(TFAIL, "write file failed");
+	} else {
+		if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
+			tst_res(TFAIL | TERRNO, "msync() failed");
+			goto clean;
+		}
+		dirty = get_dirty_bit(mmaped_area);
+		if (dirty)
+			tst_res(TFAIL, "msync() failed to write dirty page despite"
+					" succeeding");
+		else
+			tst_res(TPASS, "msync() working correctly");
+
 	}
-	dirty = get_dirty_bit(mmaped_area);
-	if (dirty)
-		tst_res(TFAIL, "msync() failed to write dirty page despite"
-				" succeeding");
-	else
-		tst_res(TPASS, "msync() working correctly");
 
 clean:
 	SAFE_MUNMAP(mmaped_area, pagesize);
Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Jan, Wei,

Quoted 4 lines

msync04 test is inherently racy and nothing guarantees that the page is not written out before get_dirty_page() manages to read the page state. Hence the test should be reworked to verify the page contents is on disk when it finds dirty bit isn't set anymore...

@Jan some time ago [1] you asked to:

mmap file, write to mmap, msync, abort fs, mount fs again, check the data is there.

Could you please have a look if this is enough? Or is it aborting fs and mounting again necessary?

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/[email protected]/

Quoted 58 lines
---
 testcases/kernel/syscalls/msync/msync04.c | 34 ++++++++++++++---------
 1 file changed, 21 insertions(+), 13 deletions(-)
testcases/kernel/syscalls/msync/msync04.c +21 -13
index 72ddc27a4..c296c8b20 100644
@@ -46,6 +46,7 @@ uint64_t get_dirty_bit(void *data)
 static void test_msync(void)
 {
 	uint64_t dirty;
+	char buffer[20];
 
 	test_fd = SAFE_OPEN("msync04/testfile", O_CREAT | O_TRUNC | O_RDWR,
 		0644);
@@ -56,20 +57,27 @@ static void test_msync(void)
 	mmaped_area[8] = 'B';
 	dirty = get_dirty_bit(mmaped_area);
 	if (!dirty) {
-		tst_res(TFAIL, "Expected dirty bit to be set after writing to"
-				" mmap()-ed area");
-		goto clean;
-	}
-	if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
-		tst_res(TFAIL | TERRNO, "msync() failed");
-		goto clean;
+		tst_res(TINFO, "Not see dirty bit so we check content of file instead");
+		test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY);
+		SAFE_READ(0, test_fd, buffer, 9);
+		if (buffer[8] == 'B')
+			tst_res(TCONF, "write file ok but msync couldn't be tested"
+				" because the storage was written to too quickly");
+		else
+			tst_res(TFAIL, "write file failed");
+	} else {
+		if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
+			tst_res(TFAIL | TERRNO, "msync() failed");
+			goto clean;
+		}
+		dirty = get_dirty_bit(mmaped_area);
+		if (dirty)
+			tst_res(TFAIL, "msync() failed to write dirty page despite"
+					" succeeding");
+		else
+			tst_res(TPASS, "msync() working correctly");
+
 	}
-	dirty = get_dirty_bit(mmaped_area);
-	if (dirty)
-		tst_res(TFAIL, "msync() failed to write dirty page despite"
-				" succeeding");
-	else
-		tst_res(TPASS, "msync() working correctly");
 
 clean:
 	SAFE_MUNMAP(mmaped_area, pagesize);
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi Petr!

Quoted 5 lines · Petr Vorel
Quoted 4 lines

msync04 test is inherently racy and nothing guarantees that the page is not written out before get_dirty_page() manages to read the page state. Hence the test should be reworked to verify the page contents is on disk when it finds dirty bit isn't set anymore...

Quoted 7 lines

@Jan some time ago [1] you asked to:

mmap file, write to mmap, msync, abort fs, mount fs again, check the data is there.

Could you please have a look if this is enough? Or is it aborting fs and mounting again necessary?

Well, it depends on what functionality exactly you want to test... See below.

Quoted 23 lines
Quoted 23 lines
testcases/kernel/syscalls/msync/msync04.c +1 -7
index 72ddc27a4..c296c8b20 100644
@@ -46,6 +46,7 @@ uint64_t get_dirty_bit(void *data)
 static void test_msync(void)
 {
 	uint64_t dirty;
+	char buffer[20];
 
 	test_fd = SAFE_OPEN("msync04/testfile", O_CREAT | O_TRUNC | O_RDWR,
 		0644);
@@ -56,20 +57,27 @@ static void test_msync(void)
 	mmaped_area[8] = 'B';
 	dirty = get_dirty_bit(mmaped_area);
 	if (!dirty) {
-		tst_res(TFAIL, "Expected dirty bit to be set after writing to"
-				" mmap()-ed area");
-		goto clean;
-	}
-	if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
-		tst_res(TFAIL | TERRNO, "msync() failed");
-		goto clean;
Quoted 8 lines

testcases/kernel/syscalls/msync/msync04.c static void test_msync(void) · line 67

Quoted 8 lines
testcases/kernel/syscalls/msync/msync04.c +8 -0
+		tst_res(TINFO, "Not see dirty bit so we check content of file instead");
+		test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY);
+		SAFE_READ(0, test_fd, buffer, 9);
+		if (buffer[8] == 'B')
+			tst_res(TCONF, "write file ok but msync couldn't be tested"
+				" because the storage was written to too quickly");
+		else
+			tst_res(TFAIL, "write file failed");

So this will read the very same page in the page cache that mmap(2) has been using for write. As such it isn't very interesting verification. I'd at least use direct IO to verify the data is stored on disk now.

Quoted 11 lines
Quoted 11 lines
testcases/kernel/syscalls/msync/msync04.c +11 -0
+	} else {
+		if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
+			tst_res(TFAIL | TERRNO, "msync() failed");
+			goto clean;
+		}
+		dirty = get_dirty_bit(mmaped_area);
+		if (dirty)
+			tst_res(TFAIL, "msync() failed to write dirty page despite"
+					" succeeding");
+		else
+			tst_res(TPASS, "msync() working correctly");
Quoted 8 lines

testcases/kernel/syscalls/msync/msync04.c static void test_msync(void) · line 72

Quoted 8 lines
testcases/kernel/syscalls/msync/msync04.c +1 -6
+
 	}
-	dirty = get_dirty_bit(mmaped_area);
-	if (dirty)
-		tst_res(TFAIL, "msync() failed to write dirty page despite"
-				" succeeding");
-	else
-		tst_res(TPASS, "msync() working correctly");

Overall this looks correct. But what this test really does is that it verifies msync(2) is clearing dirty page bits. That is not very useful verification from userspace point of view (as IMO it leans too much on internal kernel implementation details). msync(2) is really a data integrity operation so ideally its tests would verify data integrity guarantees are met after a power failure - that is what userspace is interested in. But probably these test are more within the realm of fstests (where we do similar tests) as LTP lacks necessary infrastructure to do this low-level filesystem manipulation so I guess what you have is fine.

Honza

Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 46 lines

Hi Petr!

Quoted 5 lines · Petr Vorel
Quoted 4 lines

msync04 test is inherently racy and nothing guarantees that the page is not written out before get_dirty_page() manages to read the page state. Hence the test should be reworked to verify the page contents is on disk when it finds dirty bit isn't set anymore...

Quoted 7 lines

@Jan some time ago [1] you asked to:

mmap file, write to mmap, msync, abort fs, mount fs again, check the data is there.

Could you please have a look if this is enough? Or is it aborting fs and mounting again necessary?

Well, it depends on what functionality exactly you want to test... See below.

Quoted 27 lines
Quoted 27 lines
testcases/kernel/syscalls/msync/msync04.c +5 -7
index 72ddc27a4..c296c8b20 100644
@@ -46,6 +46,7 @@ uint64_t get_dirty_bit(void *data)
 static void test_msync(void)
 {
 	uint64_t dirty;
+	char buffer[20];
 
 	test_fd = SAFE_OPEN("msync04/testfile", O_CREAT | O_TRUNC | O_RDWR,
 		0644);
@@ -56,20 +57,27 @@ static void test_msync(void)
 	mmaped_area[8] = 'B';
 	dirty = get_dirty_bit(mmaped_area);
 	if (!dirty) {
-		tst_res(TFAIL, "Expected dirty bit to be set after writing to"
-				" mmap()-ed area");
-		goto clean;
-	}
-	if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
-		tst_res(TFAIL | TERRNO, "msync() failed");
-		goto clean;
+		tst_res(TINFO, "Not see dirty bit so we check content of file instead");
+		test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY);
+		SAFE_READ(0, test_fd, buffer, 9);
+		if (buffer[8] == 'B')
Quoted 8 lines
Quoted 4 lines

testcases/kernel/syscalls/msync/msync04.c static void test_msync(void) · line 67

Quoted 4 lines
testcases/kernel/syscalls/msync/msync04.c +4 -0
+			tst_res(TCONF, "write file ok but msync couldn't be tested"
+				" because the storage was written to too quickly");
+		else
+			tst_res(TFAIL, "write file failed");

So this will read the very same page in the page cache that mmap(2) has been using for write. As such it isn't very interesting verification. I'd at least use direct IO to verify the data is stored on disk now.

+1

Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
testcases/kernel/syscalls/msync/msync04.c +11 -0
+	} else {
+		if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
+			tst_res(TFAIL | TERRNO, "msync() failed");
+			goto clean;
+		}
+		dirty = get_dirty_bit(mmaped_area);
+		if (dirty)
+			tst_res(TFAIL, "msync() failed to write dirty page despite"
+					" succeeding");
+		else
+			tst_res(TPASS, "msync() working correctly");
Quoted 8 lines

testcases/kernel/syscalls/msync/msync04.c static void test_msync(void) · line 72

Quoted 8 lines
testcases/kernel/syscalls/msync/msync04.c +1 -6
+
 	}
-	dirty = get_dirty_bit(mmaped_area);
-	if (dirty)
-		tst_res(TFAIL, "msync() failed to write dirty page despite"
-				" succeeding");
-	else
-		tst_res(TPASS, "msync() working correctly");

Overall this looks correct. But what this test really does is that it

Quoted 8 lines

verifies msync(2) is clearing dirty page bits. That is not very useful verification from userspace point of view (as IMO it leans too much on internal kernel implementation details). msync(2) is really a data integrity operation so ideally its tests would verify data integrity guarantees are met after a power failure - that is what userspace is interested in. But probably these test are more within the realm of fstests (where we do similar tests) as LTP lacks necessary infrastructure to do this low-level filesystem manipulation so I guess what you have is fine.

Thanks for having a deeper look and explanation! I guess we take it and we can think how hard would be to implement the hard way (abort fs), as Cyril wanted we do it. Or we at least add direct IO. (update that in the ticket:
https://github.com/linux-test-project/ltp/issues/1157).
And sure, if anybody adds msync() support to fstests, that would be the best.

But for now I merged it with your RBT (I'm sorry if I was too inventive to add it) to get some improvement (hope that without direct IO we did not actually turned it into false negative).

@Wei: feel free to further improve it.

Kind regards,
Petr

Quoted 1 line

Honza

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 18 lines · Petr Vorel
Quoted 11 lines

Hi Petr!

Overall this looks correct. But what this test really does is that it verifies msync(2) is clearing dirty page bits. That is not very useful verification from userspace point of view (as IMO it leans too much on internal kernel implementation details). msync(2) is really a data integrity operation so ideally its tests would verify data integrity guarantees are met after a power failure - that is what userspace is interested in. But probably these test are more within the realm of fstests (where we do similar tests) as LTP lacks necessary infrastructure to do this low-level filesystem manipulation so I guess what you have is fine.

Thanks for having a deeper look and explanation! I guess we take it and we can think how hard would be to implement the hard way (abort fs), as Cyril wanted we do it. Or we at least add direct IO. (update that in the ticket:
https://github.com/linux-test-project/ltp/issues/1157).
And sure, if anybody adds msync() support to fstests, that would be the best.

Quoted 5 lines

But for now I merged it with your RBT (I'm sorry if I was too inventive to add it) to get some improvement (hope that without direct IO we did not actually turned it into false negative).

@Wei: feel free to further improve it.

Thanks Petr and Jan's feedback, i will continue do improve on this case such as add direct IO firstly.

Quoted 5 lines

Kind regards,
Petr

Quoted 1 line

Honza

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
In reply to Wei Gao via ltp

Hi Wei,

Quoted 4 lines

msync04 test is inherently racy and nothing guarantees that the page is not written out before get_dirty_page() manages to read the page state. Hence the test should be reworked to verify the page contents is on disk when it finds dirty bit isn't set anymore...

It's nice habit to add Jan's credit :)

He did it [1], thus one would add:

+ Add a ticket:
Implements: https://github.com/linux-test-project/ltp/issues/1157

[1] https://bugzilla.suse.com/show_bug.cgi?id=1224201#c13
[2] https://lore.kernel.org/ltp/[email protected]/

testcases/kernel/syscalls/msync/msync04.c uint64_t get_dirty_bit(void *data) · line 48

Quoted 13 lines
---
 testcases/kernel/syscalls/msync/msync04.c | 34 ++++++++++++++---------
 1 file changed, 21 insertions(+), 13 deletions(-)
testcases/kernel/syscalls/msync/msync04.c +0 -0
index 72ddc27a4..c296c8b20 100644
@@ -46,6 +46,7 @@ uint64_t get_dirty_bit(void *data)
 static void test_msync(void)
 {
 	uint64_t dirty;

While at it, could you please remove dirty variable and use get_dirty_bit(...) directly?

testcases/kernel/syscalls/msync/msync04.c static void test_msync(void) · line 59

Quoted 8 lines
testcases/kernel/syscalls/msync/msync04.c +1 -0
+	char buffer[20];
 
 	test_fd = SAFE_OPEN("msync04/testfile", O_CREAT | O_TRUNC | O_RDWR,
 		0644);
@@ -56,20 +57,27 @@ static void test_msync(void)
 	mmaped_area[8] = 'B';
 	dirty = get_dirty_bit(mmaped_area);
 	if (!dirty) {

if (!get_dirty_bit(mmaped_area)) {

Quoted 8 lines
testcases/kernel/syscalls/msync/msync04.c +1 -7
-		tst_res(TFAIL, "Expected dirty bit to be set after writing to"
-				" mmap()-ed area");
-		goto clean;
-	}
-	if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
-		tst_res(TFAIL | TERRNO, "msync() failed");
-		goto clean;
+		tst_res(TINFO, "Not see dirty bit so we check content of file instead");

testcases/kernel/syscalls/msync/msync04.c static void test_msync(void) · line 70

Quoted 10 lines
testcases/kernel/syscalls/msync/msync04.c +10 -0
+		test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY);
+		SAFE_READ(0, test_fd, buffer, 9);
+		if (buffer[8] == 'B')
+			tst_res(TCONF, "write file ok but msync couldn't be tested"
+				" because the storage was written to too quickly");
+		else
+			tst_res(TFAIL, "write file failed");
+	} else {
+		if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
+			tst_res(TFAIL | TERRNO, "msync() failed");

I know you copy old code, but it would deserve to fix: -1 is failure, anything different than 0 or -1 is invalid value. And you get this for free if you use TST_EXP_PASS_SILENT().

testcases/kernel/syscalls/msync/msync04.c static void test_msync(void) · line 71

Quoted 1 line
testcases/kernel/syscalls/msync/msync04.c +1 -0
+			goto clean;

nit: Having if and else part in it's own functions (verify_mmaped(), verify_dirty() would allow to get rid of goto and produced slightly nicer code (less indentation => less split lines):

	if (!get_dirty_bit(mmaped_area))
		verify_mmaped();
	else
		verify_dirty();

	SAFE_MUNMAP(mmaped_area, pagesize);
	mmaped_area = NULL;

testcases/kernel/syscalls/msync/msync04.c static void test_msync(void) · line 74

Quoted 3 lines
testcases/kernel/syscalls/msync/msync04.c +3 -0
+		}
+		dirty = get_dirty_bit(mmaped_area);
+		if (dirty)
		if (get_dirty_bit(mmaped_area)) {

testcases/kernel/syscalls/msync/msync04.c static void test_msync(void) · line 76

Quoted 2 lines
testcases/kernel/syscalls/msync/msync04.c +2 -0
+			tst_res(TFAIL, "msync() failed to write dirty page despite"
+					" succeeding");

I would keep this string separate

Quoted 14 lines
testcases/kernel/syscalls/msync/msync04.c +3 -6
+		else
+			tst_res(TPASS, "msync() working correctly");
+
 
 	}
-	dirty = get_dirty_bit(mmaped_area);
-	if (dirty)
-		tst_res(TFAIL, "msync() failed to write dirty page despite"
-				" succeeding");
-	else
-		tst_res(TPASS, "msync() working correctly");
 
 clean:

SAFE_MUNMAP(mmaped_area, pagesize);

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp