Skip to contentSkip to conversation

msync04.c: Use direct IO to verify the data is stored on disk

PATCH Superseded Wei Gao via ltp 4 participants 8 messages May 30, 2024 – Jun 17, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 msync04.c: Use direct IO to verify the data is stored on disk

---
 testcases/kernel/syscalls/msync/msync04.c | 56 +++++++++++++----------
 1 file changed, 33 insertions(+), 23 deletions(-)
testcases/kernel/syscalls/msync/msync04.c +33 -23
index 1718bd7dc..c0580d1b0 100644
@@ -11,6 +11,7 @@
  * is no longer dirty after msync() call.
  */
 
+#define _GNU_SOURCE
 #include <errno.h>
 #include "tst_test.h"
 
@@ -43,10 +44,35 @@ static uint64_t get_dirty_bit(void *data)
 	return pageflag_entry & (1ULL << 4);
 }
 
-static void test_msync(void)
+static void verify_mmaped(void)
+{
+	void *buffer = SAFE_MEMALIGN(getpagesize(), getpagesize());
+
+	tst_res(TINFO, "Not see dirty bit so we check content of file instead");
+	test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY | O_DIRECT);
+	SAFE_READ(0, test_fd, buffer, getpagesize());
+
+	char *char_buffer = (char *)buffer;
+
+	if (char_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");
+}
+
+static void verify_dirty(void)
 {
-	char buffer[20];
+	TST_EXP_PASS_SILENT(msync(mmaped_area, pagesize, MS_SYNC));
+
+	if (TST_RET == 0 && !get_dirty_bit(mmaped_area))
+		tst_res(TPASS, "msync() verify dirty page ok");
+	else
+		tst_res(TFAIL, "msync() verify dirty page failed");
+}
 
+static void test_msync(void)
+{
 	test_fd = SAFE_OPEN("msync04/testfile", O_CREAT | O_TRUNC | O_RDWR,
 		0644);
 	SAFE_WRITE(SAFE_WRITE_ANY, test_fd, STRING_TO_WRITE, sizeof(STRING_TO_WRITE) - 1);
@@ -55,27 +81,11 @@ static void test_msync(void)
 	SAFE_CLOSE(test_fd);
 	mmaped_area[8] = 'B';
 
-	if (!get_dirty_bit(mmaped_area)) {
-		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;
-		}
-		if (get_dirty_bit(mmaped_area))
-			tst_res(TFAIL, "msync() failed to write dirty page despite succeeding");
-		else
-			tst_res(TPASS, "msync() working correctly");
-	}
-
-clean:
+	if (!get_dirty_bit(mmaped_area))
+		verify_mmaped();
+	else
+		verify_dirty();
+
 	SAFE_MUNMAP(mmaped_area, pagesize);
 	mmaped_area = NULL;
 }
Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Wei Gao

Looks good to me! Feel free to add:

Honza

Quoted 90 lines
---
 testcases/kernel/syscalls/msync/msync04.c | 56 +++++++++++++----------
 1 file changed, 33 insertions(+), 23 deletions(-)
testcases/kernel/syscalls/msync/msync04.c +33 -23
index 1718bd7dc..c0580d1b0 100644
@@ -11,6 +11,7 @@
  * is no longer dirty after msync() call.
  */
 
+#define _GNU_SOURCE
 #include <errno.h>
 #include "tst_test.h"
 
@@ -43,10 +44,35 @@ static uint64_t get_dirty_bit(void *data)
 	return pageflag_entry & (1ULL << 4);
 }
 
-static void test_msync(void)
+static void verify_mmaped(void)
+{
+	void *buffer = SAFE_MEMALIGN(getpagesize(), getpagesize());
+
+	tst_res(TINFO, "Not see dirty bit so we check content of file instead");
+	test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY | O_DIRECT);
+	SAFE_READ(0, test_fd, buffer, getpagesize());
+
+	char *char_buffer = (char *)buffer;
+
+	if (char_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");
+}
+
+static void verify_dirty(void)
 {
-	char buffer[20];
+	TST_EXP_PASS_SILENT(msync(mmaped_area, pagesize, MS_SYNC));
+
+	if (TST_RET == 0 && !get_dirty_bit(mmaped_area))
+		tst_res(TPASS, "msync() verify dirty page ok");
+	else
+		tst_res(TFAIL, "msync() verify dirty page failed");
+}
 
+static void test_msync(void)
+{
 	test_fd = SAFE_OPEN("msync04/testfile", O_CREAT | O_TRUNC | O_RDWR,
 		0644);
 	SAFE_WRITE(SAFE_WRITE_ANY, test_fd, STRING_TO_WRITE, sizeof(STRING_TO_WRITE) - 1);
@@ -55,27 +81,11 @@ static void test_msync(void)
 	SAFE_CLOSE(test_fd);
 	mmaped_area[8] = 'B';
 
-	if (!get_dirty_bit(mmaped_area)) {
-		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;
-		}
-		if (get_dirty_bit(mmaped_area))
-			tst_res(TFAIL, "msync() failed to write dirty page despite succeeding");
-		else
-			tst_res(TPASS, "msync() working correctly");
-	}
-
-clean:
+	if (!get_dirty_bit(mmaped_area))
+		verify_mmaped();
+	else
+		verify_dirty();
+
 	SAFE_MUNMAP(mmaped_area, pagesize);
 	mmaped_area = NULL;
 }
Signature · 1 line
2.35.3
Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Wei Gao via ltp

Hi!

Quoted 19 lines
---
 testcases/kernel/syscalls/msync/msync04.c | 56 +++++++++++++----------
 1 file changed, 33 insertions(+), 23 deletions(-)
testcases/kernel/syscalls/msync/msync04.c +1 -0
index 1718bd7dc..c0580d1b0 100644
@@ -11,6 +11,7 @@
  * is no longer dirty after msync() call.
  */
 
+#define _GNU_SOURCE
 #include <errno.h>
 #include "tst_test.h"
 

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

Quoted 9 lines
testcases/kernel/syscalls/msync/msync04.c +5 -1
@@ -43,10 +44,35 @@ static uint64_t get_dirty_bit(void *data)
 	return pageflag_entry & (1ULL << 4);
 }
 
-static void test_msync(void)
+static void verify_mmaped(void)
+{
+	void *buffer = SAFE_MEMALIGN(getpagesize(), getpagesize());
+
+	tst_res(TINFO, "Not see dirty bit so we check content of file instead");

^
Haven't seen dirty...

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

Quoted 4 lines
testcases/kernel/syscalls/msync/msync04.c +4 -0
+	test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY | O_DIRECT);
+	SAFE_READ(0, test_fd, buffer, getpagesize());
+
+	char *char_buffer = (char *)buffer;

You can declare the buffer directly as char * instead, there is no need for this indirection.

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

Quoted 3 lines
testcases/kernel/syscalls/msync/msync04.c +3 -0
+	if (char_buffer[8] == 'B')
+		tst_res(TCONF, "write file ok but msync couldn't be tested"
+				" because the storage was written to too quickly");

This could be shorter and stil to the point:

"Write was too fast, couldn't test msync()"

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

Quoted 2 lines
testcases/kernel/syscalls/msync/msync04.c +2 -0
+	else
+		tst_res(TFAIL, "write file failed");

We should free() allocated the memory here.

Other than these minor issues the rest looks good to me.

You can add my Reviewed-by: once these issues are fixed.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH v2 1/1 msync04.c: Use direct IO to verify the data is stored on disk

In reply to Wei Gao via ltp
---
 testcases/kernel/syscalls/msync/msync04.c | 55 +++++++++++++----------
 1 file changed, 32 insertions(+), 23 deletions(-)
testcases/kernel/syscalls/msync/msync04.c +32 -23
index 1718bd7dc..e3478f690 100644
@@ -11,6 +11,7 @@
  * is no longer dirty after msync() call.
  */
 
+#define _GNU_SOURCE
 #include <errno.h>
 #include "tst_test.h"
 
@@ -43,10 +44,34 @@ static uint64_t get_dirty_bit(void *data)
 	return pageflag_entry & (1ULL << 4);
 }
 
-static void test_msync(void)
+static void verify_mmaped(void)
+{
+	char *buffer = SAFE_MEMALIGN(getpagesize(), getpagesize());
+
+	tst_res(TINFO, "Haven't seen dirty bit so we check content of file instead");
+	test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY | O_DIRECT);
+	SAFE_READ(0, test_fd, buffer, getpagesize());
+
+	if (buffer[8] == 'B')
+		tst_res(TCONF, "Write was too fast, couldn't test msync()");
+	else
+		tst_res(TFAIL, "write file failed");
+
+	free(buffer);
+}
+
+static void verify_dirty(void)
 {
-	char buffer[20];
+	TST_EXP_PASS_SILENT(msync(mmaped_area, pagesize, MS_SYNC));
+
+	if (TST_RET == 0 && !get_dirty_bit(mmaped_area))
+		tst_res(TPASS, "msync() verify dirty page ok");
+	else
+		tst_res(TFAIL, "msync() verify dirty page failed");
+}
 
+static void test_msync(void)
+{
 	test_fd = SAFE_OPEN("msync04/testfile", O_CREAT | O_TRUNC | O_RDWR,
 		0644);
 	SAFE_WRITE(SAFE_WRITE_ANY, test_fd, STRING_TO_WRITE, sizeof(STRING_TO_WRITE) - 1);
@@ -55,27 +80,11 @@ static void test_msync(void)
 	SAFE_CLOSE(test_fd);
 	mmaped_area[8] = 'B';
 
-	if (!get_dirty_bit(mmaped_area)) {
-		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;
-		}
-		if (get_dirty_bit(mmaped_area))
-			tst_res(TFAIL, "msync() failed to write dirty page despite succeeding");
-		else
-			tst_res(TPASS, "msync() working correctly");
-	}
-
-clean:
+	if (!get_dirty_bit(mmaped_area))
+		verify_mmaped();
+	else
+		verify_dirty();
+
 	SAFE_MUNMAP(mmaped_area, pagesize);
 	mmaped_area = NULL;
 }
Signature · 5 lines
2.35.3


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

Hi Wei,

...

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

Quoted 6 lines
testcases/kernel/syscalls/msync/msync04.c +6 -0
+static void verify_mmaped(void)
+{
+	char *buffer = SAFE_MEMALIGN(getpagesize(), getpagesize());
+
+	tst_res(TINFO, "Haven't seen dirty bit so we check content of file instead");
+	test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY | O_DIRECT);

If this fails we have memory leak, because free() is not run.

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

Quoted 1 line
testcases/kernel/syscalls/msync/msync04.c +1 -0
+	SAFE_READ(0, test_fd, buffer, getpagesize());

Also here. Maybe create buf as static and have conditional free() also at the cleanup?

Kind regards,
Petr

Quoted 9 lines
testcases/kernel/syscalls/msync/msync04.c +9 -0
+
+	if (buffer[8] == 'B')
+		tst_res(TCONF, "Write was too fast, couldn't test msync()");
+	else
+		tst_res(TFAIL, "write file failed");
+
+	free(buffer);
+}
+
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Hi!

Quoted 11 lines

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

Quoted 6 lines
testcases/kernel/syscalls/msync/msync04.c +6 -0
+static void verify_mmaped(void)
+{
+	char *buffer = SAFE_MEMALIGN(getpagesize(), getpagesize());
+
+	tst_res(TINFO, "Haven't seen dirty bit so we check content of file instead");
+	test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY | O_DIRECT);

If this fails we have memory leak, because free() is not run.

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

Quoted 1 line
testcases/kernel/syscalls/msync/msync04.c +1 -0
+	SAFE_READ(0, test_fd, buffer, getpagesize());

Also here. Maybe create buf as static and have conditional free() also at the cleanup?

If these fail we exit the test, which frees memory anyways...

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Hi!

Quoted 7 lines

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

Quoted 6 lines
testcases/kernel/syscalls/msync/msync04.c +6 -0
+static void verify_mmaped(void)
+{
+	char *buffer = SAFE_MEMALIGN(getpagesize(), getpagesize());
+
+	tst_res(TINFO, "Haven't seen dirty bit so we check content of file instead");
+	test_fd = SAFE_OPEN("msync04/testfile", O_RDONLY | O_DIRECT);

If this fails we have memory leak, because free() is not run.

Quoted 5 lines
Quoted 3 lines

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

Quoted 1 line
testcases/kernel/syscalls/msync/msync04.c +1 -0
+	SAFE_READ(0, test_fd, buffer, getpagesize());

Also here. Maybe create buf as static and have conditional free() also at the cleanup?

If these fail we exit the test, which frees memory anyways...

Thanks for correcting me. I always forget the basis that exit() frees the memory (unlike return 0 and unless overwritten). And we call exit() in tst_brk().

Kind regards,
Petr

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