xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/msync/msync04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2017 Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Test description: Verify msync() after writing into mmap()-ed file works.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Write to mapped region and sync the memory back with file. Check the page
11*49cdfc7eSAndroid Build Coastguard Worker  * is no longer dirty after msync() call.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "pgsize_helpers.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static int test_fd;
19*49cdfc7eSAndroid Build Coastguard Worker static char *mmaped_area;
20*49cdfc7eSAndroid Build Coastguard Worker static long pagesize;
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #define STRING_TO_WRITE	"AAAAAAAAAA"
23*49cdfc7eSAndroid Build Coastguard Worker 
get_dirty_bit(void * data)24*49cdfc7eSAndroid Build Coastguard Worker uint64_t get_dirty_bit(void *data)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	int pagemap_fd, pageflags_fd;
27*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long addr;
28*49cdfc7eSAndroid Build Coastguard Worker 	uint64_t pagemap_entry, pageflag_entry, pfn, index;
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker 	addr = (unsigned long)data;
31*49cdfc7eSAndroid Build Coastguard Worker 	index = (addr / kernel_page_size()) * sizeof(uint64_t);
32*49cdfc7eSAndroid Build Coastguard Worker 	pagemap_fd = SAFE_OPEN("/proc/self/pagemap", O_RDONLY);
33*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(pagemap_fd, index, SEEK_SET);
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READ(1, pagemap_fd, &pagemap_entry, sizeof(pagemap_entry));
35*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pagemap_fd);
36*49cdfc7eSAndroid Build Coastguard Worker 	pfn = pagemap_entry & ((1ULL << 55) - 1);
37*49cdfc7eSAndroid Build Coastguard Worker 	if (!pfn)
38*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
39*49cdfc7eSAndroid Build Coastguard Worker 	pageflags_fd = SAFE_OPEN("/proc/kpageflags", O_RDONLY);
40*49cdfc7eSAndroid Build Coastguard Worker 	index = pfn * sizeof(uint64_t);
41*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(pageflags_fd, index, SEEK_SET);
42*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READ(1, pageflags_fd, &pageflag_entry, sizeof(pageflag_entry));
43*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pageflags_fd);
44*49cdfc7eSAndroid Build Coastguard Worker 	return pageflag_entry & (1ULL << 4);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
test_msync(void)47*49cdfc7eSAndroid Build Coastguard Worker static void test_msync(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	uint64_t dirty;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	test_fd = SAFE_OPEN("msync04/testfile", O_CREAT | O_TRUNC | O_RDWR,
52*49cdfc7eSAndroid Build Coastguard Worker 		0644);
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ANY, test_fd, STRING_TO_WRITE, sizeof(STRING_TO_WRITE) - 1);
54*49cdfc7eSAndroid Build Coastguard Worker 	mmaped_area = SAFE_MMAP(NULL, pagesize, PROT_READ | PROT_WRITE,
55*49cdfc7eSAndroid Build Coastguard Worker 			MAP_SHARED, test_fd, 0);
56*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(test_fd);
57*49cdfc7eSAndroid Build Coastguard Worker 	mmaped_area[8] = 'B';
58*49cdfc7eSAndroid Build Coastguard Worker 	dirty = get_dirty_bit(mmaped_area);
59*49cdfc7eSAndroid Build Coastguard Worker 	if (!dirty) {
60*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Expected dirty bit to be set after writing to"
61*49cdfc7eSAndroid Build Coastguard Worker 				" mmap()-ed area");
62*49cdfc7eSAndroid Build Coastguard Worker 		goto clean;
63*49cdfc7eSAndroid Build Coastguard Worker 	}
64*49cdfc7eSAndroid Build Coastguard Worker 	if (msync(mmaped_area, pagesize, MS_SYNC) < 0) {
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "msync() failed");
66*49cdfc7eSAndroid Build Coastguard Worker 		goto clean;
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker 	dirty = get_dirty_bit(mmaped_area);
69*49cdfc7eSAndroid Build Coastguard Worker 	if (dirty)
70*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "msync() failed to write dirty page despite"
71*49cdfc7eSAndroid Build Coastguard Worker 				" succeeding");
72*49cdfc7eSAndroid Build Coastguard Worker 	else
73*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "msync() working correctly");
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker clean:
76*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(mmaped_area, pagesize);
77*49cdfc7eSAndroid Build Coastguard Worker 	mmaped_area = NULL;
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)80*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	pagesize = (off_t)SAFE_SYSCONF(_SC_PAGESIZE);
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)85*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker 	if (mmaped_area)
88*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MUNMAP(mmaped_area, pagesize);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	if (test_fd > 0)
91*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(test_fd);
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
95*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = test_msync,
96*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
97*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
98*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
99*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = "msync04",
100*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
101*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
102*49cdfc7eSAndroid Build Coastguard Worker 	.skip_filesystems = (const char *[]) {
103*49cdfc7eSAndroid Build Coastguard Worker 		"tmpfs",
104*49cdfc7eSAndroid Build Coastguard Worker 		NULL
105*49cdfc7eSAndroid Build Coastguard Worker 	},
106*49cdfc7eSAndroid Build Coastguard Worker };
107