xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * Copyright (C) 2005-2006 IBM Corporation.
4  * Author: David Gibson & Adam Litke
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Tests copy-on-write semantics of large pages where a number of threads
11  * map the same file with the MAP_PRIVATE flag. The threads then write
12  * into their copy of the mapping and recheck the contents to ensure they
13  * were not corrupted by the other threads.
14  */
15 
16 #include "hugetlb.h"
17 
18 #define THREADS 5
19 #define NR_HUGEPAGES 6
20 #define MNTPOINT "hugetlbfs/"
21 
22 static int fd = -1;
23 static long hpage_size;
24 
do_work(int thread,size_t size,int fd)25 static void do_work(int thread, size_t size, int fd)
26 {
27 	char *addr;
28 	size_t i;
29 	char pattern = thread+65;
30 
31 	addr = SAFE_MMAP(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
32 
33 	tst_res(TINFO, "Thread %d (pid %d): Mapped at address %p",
34 	       thread, getpid(), addr);
35 
36 	for (i = 0; i < size; i++)
37 		memcpy((char *)addr+i, &pattern, 1);
38 
39 	SAFE_MSYNC(addr, size, MS_SYNC);
40 
41 	for (i = 0; i < size; i++) {
42 		if (addr[i] != pattern) {
43 			tst_res(TFAIL, "thread %d (pid: %d): Corruption at %p; "
44 				   "Got %c, Expected %c", thread, getpid(),
45 				   &addr[i], addr[i], pattern);
46 			goto cleanup;
47 		}
48 	}
49 
50 	tst_res(TINFO, "Thread %d (pid %d): Pattern verified",
51 			thread, getpid());
52 
53 cleanup:
54 	SAFE_MUNMAP(addr, size);
55 	exit(0);
56 }
57 
run_test(void)58 static void run_test(void)
59 {
60 	int i, pid;
61 	char *addr;
62 	size_t size, itr;
63 	pid_t *wait_list;
64 
65 	wait_list = SAFE_MALLOC(THREADS * sizeof(pid_t));
66 	size = (NR_HUGEPAGES / (THREADS+1)) * hpage_size;
67 
68 	/*
69 	 * First, mmap the file with MAP_SHARED and fill with data
70 	 * If this is not done, then the fault handler will not be
71 	 * called in the kernel since private mappings will be
72 	 * created for the children at prefault time.
73 	 */
74 	addr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
75 
76 	for (itr = 0; itr < size; itr += 8)
77 		memcpy(addr+itr, "deadbeef", 8);
78 
79 	for (i = 0; i < THREADS; i++) {
80 		pid = SAFE_FORK();
81 
82 		if (pid == 0)
83 			do_work(i, size, fd);
84 
85 		wait_list[i] = pid;
86 	}
87 	tst_reap_children();
88 
89 	SAFE_MUNMAP(addr, size);
90 	free(wait_list);
91 	tst_res(TPASS, "mmap COW working as expected.");
92 }
93 
setup(void)94 static void setup(void)
95 {
96 	hpage_size = tst_get_hugepage_size();
97 	fd = tst_creat_unlinked(MNTPOINT, 0);
98 }
99 
cleanup(void)100 static void cleanup(void)
101 {
102 	if (fd >= 1)
103 		SAFE_CLOSE(fd);
104 }
105 
106 static struct tst_test test = {
107 	.needs_root = 1,
108 	.mntpoint = MNTPOINT,
109 	.needs_hugetlbfs = 1,
110 	.needs_tmpdir = 1,
111 	.forks_child = 1,
112 	.setup = setup,
113 	.cleanup = cleanup,
114 	.test_all = run_test,
115 	.hugepages = {NR_HUGEPAGES, TST_NEEDS},
116 };
117