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