1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Copyright (c) 2008 Vijay Kumar B. <vijaykumar@bravegnu.org> 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * Based on testcases/kernel/syscalls/waitpid/waitpid01.c 5f08c3bdfSopenharmony_ci * Original copyright message: 6f08c3bdfSopenharmony_ci * 7f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 10f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 11f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 12f08c3bdfSopenharmony_ci * (at your option) any later version. 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 15f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 16f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 17f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 18f08c3bdfSopenharmony_ci * 19f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 20f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 21f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22f08c3bdfSopenharmony_ci */ 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci/* 25f08c3bdfSopenharmony_ci * NAME 26f08c3bdfSopenharmony_ci * move_pages05.c 27f08c3bdfSopenharmony_ci * 28f08c3bdfSopenharmony_ci * DESCRIPTION 29f08c3bdfSopenharmony_ci * Test movement of pages mapped by a process. 30f08c3bdfSopenharmony_ci * 31f08c3bdfSopenharmony_ci * ALGORITHM 32f08c3bdfSopenharmony_ci * 1. Start the test case program as root. 33f08c3bdfSopenharmony_ci * 2. Allocate a shared memory in NUMA node A. 34f08c3bdfSopenharmony_ci * 3. Fork another process. 35f08c3bdfSopenharmony_ci * 4. Use move_pages() to move the pages to NUMA node B, without 36f08c3bdfSopenharmony_ci * the MPOL_MF_MOVE_ALL. 37f08c3bdfSopenharmony_ci * 5. Check if the corresponding status is set to -EACCES. 38f08c3bdfSopenharmony_ci * 39f08c3bdfSopenharmony_ci * USAGE: <for command-line> 40f08c3bdfSopenharmony_ci * move_pages05 [-c n] [-i n] [-I x] [-P x] [-t] 41f08c3bdfSopenharmony_ci * where, -c n : Run n copies concurrently. 42f08c3bdfSopenharmony_ci * -i n : Execute test n times. 43f08c3bdfSopenharmony_ci * -I x : Execute test for x seconds. 44f08c3bdfSopenharmony_ci * -P x : Pause for x seconds between iterations. 45f08c3bdfSopenharmony_ci * -t : Turn on syscall timing. 46f08c3bdfSopenharmony_ci * 47f08c3bdfSopenharmony_ci * History 48f08c3bdfSopenharmony_ci * 05/2008 Vijay Kumar 49f08c3bdfSopenharmony_ci * Initial Version. 50f08c3bdfSopenharmony_ci * 51f08c3bdfSopenharmony_ci * Restrictions 52f08c3bdfSopenharmony_ci * None 53f08c3bdfSopenharmony_ci */ 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_ci#include <sys/types.h> 56f08c3bdfSopenharmony_ci#include <sys/wait.h> 57f08c3bdfSopenharmony_ci#include <unistd.h> 58f08c3bdfSopenharmony_ci#include <errno.h> 59f08c3bdfSopenharmony_ci#include "test.h" 60f08c3bdfSopenharmony_ci#include "move_pages_support.h" 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci#define SHARED_PAGE 0 63f08c3bdfSopenharmony_ci#define N_SHARED_PAGES 1 64f08c3bdfSopenharmony_ci#define UNSHARED_PAGE 1 65f08c3bdfSopenharmony_ci#define N_UNSHARED_PAGES 1 66f08c3bdfSopenharmony_ci#define N_TEST_PAGES (N_SHARED_PAGES + N_UNSHARED_PAGES) 67f08c3bdfSopenharmony_ci#define N_TEST_NODES 2 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_cienum { 70f08c3bdfSopenharmony_ci SEM_CHILD_SETUP, 71f08c3bdfSopenharmony_ci SEM_PARENT_TEST, 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci MAX_SEMS 74f08c3bdfSopenharmony_ci}; 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_civoid setup(void); 77f08c3bdfSopenharmony_civoid cleanup(void); 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_cichar *TCID = "move_pages05"; 80f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci/* 83f08c3bdfSopenharmony_ci * child() - touches pages, and waits for signal from parent. 84f08c3bdfSopenharmony_ci * @pages: shared pages allocated in parent 85f08c3bdfSopenharmony_ci */ 86f08c3bdfSopenharmony_civoid child(void **pages, sem_t * sem) 87f08c3bdfSopenharmony_ci{ 88f08c3bdfSopenharmony_ci int i; 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci for (i = 0; i < N_TEST_PAGES; i++) { 91f08c3bdfSopenharmony_ci char *page; 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_ci page = pages[i]; 94f08c3bdfSopenharmony_ci page[0] = 0xAA; 95f08c3bdfSopenharmony_ci } 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci /* Setup complete. Ask parent to continue. */ 98f08c3bdfSopenharmony_ci if (sem_post(&sem[SEM_CHILD_SETUP]) == -1) 99f08c3bdfSopenharmony_ci tst_resm(TWARN | TERRNO, "error post semaphore"); 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci /* Wait for testcase in parent to complete. */ 102f08c3bdfSopenharmony_ci if (sem_wait(&sem[SEM_PARENT_TEST]) == -1) 103f08c3bdfSopenharmony_ci tst_resm(TWARN | TERRNO, "error waiting for semaphore"); 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci exit(0); 106f08c3bdfSopenharmony_ci} 107f08c3bdfSopenharmony_ci 108f08c3bdfSopenharmony_ciint main(int argc, char **argv) 109f08c3bdfSopenharmony_ci{ 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci tst_parse_opts(argc, argv, NULL, NULL); 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci setup(); 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci#ifdef HAVE_NUMA_V2 116f08c3bdfSopenharmony_ci unsigned int i; 117f08c3bdfSopenharmony_ci int lc; 118f08c3bdfSopenharmony_ci unsigned int from_node; 119f08c3bdfSopenharmony_ci unsigned int to_node; 120f08c3bdfSopenharmony_ci int ret; 121f08c3bdfSopenharmony_ci 122f08c3bdfSopenharmony_ci ret = get_allowed_nodes(NH_MEMS, 2, &from_node, &to_node); 123f08c3bdfSopenharmony_ci if (ret < 0) 124f08c3bdfSopenharmony_ci tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret); 125f08c3bdfSopenharmony_ci 126f08c3bdfSopenharmony_ci /* check for looping state if -i option is given */ 127f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 128f08c3bdfSopenharmony_ci void *pages[N_TEST_PAGES] = { 0 }; 129f08c3bdfSopenharmony_ci int nodes[N_TEST_PAGES]; 130f08c3bdfSopenharmony_ci int status[N_TEST_PAGES]; 131f08c3bdfSopenharmony_ci pid_t cpid; 132f08c3bdfSopenharmony_ci sem_t *sem; 133f08c3bdfSopenharmony_ci 134f08c3bdfSopenharmony_ci /* reset tst_count in case we are looping */ 135f08c3bdfSopenharmony_ci tst_count = 0; 136f08c3bdfSopenharmony_ci 137f08c3bdfSopenharmony_ci ret = alloc_shared_pages_on_node(pages + SHARED_PAGE, 138f08c3bdfSopenharmony_ci N_SHARED_PAGES, from_node); 139f08c3bdfSopenharmony_ci if (ret == -1) 140f08c3bdfSopenharmony_ci continue; 141f08c3bdfSopenharmony_ci 142f08c3bdfSopenharmony_ci ret = alloc_pages_on_node(pages + UNSHARED_PAGE, 143f08c3bdfSopenharmony_ci N_UNSHARED_PAGES, from_node); 144f08c3bdfSopenharmony_ci if (ret == -1) 145f08c3bdfSopenharmony_ci goto err_free_shared; 146f08c3bdfSopenharmony_ci 147f08c3bdfSopenharmony_ci for (i = 0; i < N_TEST_PAGES; i++) { 148f08c3bdfSopenharmony_ci nodes[i] = to_node; 149f08c3bdfSopenharmony_ci } 150f08c3bdfSopenharmony_ci 151f08c3bdfSopenharmony_ci sem = alloc_sem(MAX_SEMS); 152f08c3bdfSopenharmony_ci if (sem == NULL) { 153f08c3bdfSopenharmony_ci goto err_free_unshared; 154f08c3bdfSopenharmony_ci } 155f08c3bdfSopenharmony_ci 156f08c3bdfSopenharmony_ci /* 157f08c3bdfSopenharmony_ci * Fork a child process so that the shared pages are 158f08c3bdfSopenharmony_ci * now really shared between two processes. 159f08c3bdfSopenharmony_ci */ 160f08c3bdfSopenharmony_ci cpid = fork(); 161f08c3bdfSopenharmony_ci if (cpid == -1) { 162f08c3bdfSopenharmony_ci tst_resm(TBROK, "forking child failed"); 163f08c3bdfSopenharmony_ci goto err_free_sem; 164f08c3bdfSopenharmony_ci } else if (cpid == 0) { 165f08c3bdfSopenharmony_ci child(pages, sem); 166f08c3bdfSopenharmony_ci } 167f08c3bdfSopenharmony_ci 168f08c3bdfSopenharmony_ci /* Wait for child to setup and signal. */ 169f08c3bdfSopenharmony_ci if (sem_wait(&sem[SEM_CHILD_SETUP]) == -1) 170f08c3bdfSopenharmony_ci tst_resm(TWARN | TERRNO, "error wait semaphore"); 171f08c3bdfSopenharmony_ci 172f08c3bdfSopenharmony_ci ret = numa_move_pages(0, N_TEST_PAGES, pages, nodes, 173f08c3bdfSopenharmony_ci status, MPOL_MF_MOVE); 174f08c3bdfSopenharmony_ci if (ret == -1) { 175f08c3bdfSopenharmony_ci tst_resm(TFAIL | TERRNO, 176f08c3bdfSopenharmony_ci "move_pages unexpectedly failed"); 177f08c3bdfSopenharmony_ci goto err_kill_child; 178f08c3bdfSopenharmony_ci } else if (ret > 0) { 179f08c3bdfSopenharmony_ci tst_resm(TINFO, "move_pages() returned %d", ret); 180f08c3bdfSopenharmony_ci } 181f08c3bdfSopenharmony_ci 182f08c3bdfSopenharmony_ci if (status[SHARED_PAGE] == -EACCES) 183f08c3bdfSopenharmony_ci tst_resm(TPASS, "status[%d] set to expected -EACCES", 184f08c3bdfSopenharmony_ci SHARED_PAGE); 185f08c3bdfSopenharmony_ci else 186f08c3bdfSopenharmony_ci tst_resm(TFAIL, "status[%d] is %d", 187f08c3bdfSopenharmony_ci SHARED_PAGE, status[SHARED_PAGE]); 188f08c3bdfSopenharmony_ci 189f08c3bdfSopenharmony_cierr_kill_child: 190f08c3bdfSopenharmony_ci /* Test done. Ask child to terminate. */ 191f08c3bdfSopenharmony_ci if (sem_post(&sem[SEM_PARENT_TEST]) == -1) 192f08c3bdfSopenharmony_ci tst_resm(TWARN | TERRNO, "error post semaphore"); 193f08c3bdfSopenharmony_ci /* Read the status, no zombies! */ 194f08c3bdfSopenharmony_ci wait(NULL); 195f08c3bdfSopenharmony_cierr_free_sem: 196f08c3bdfSopenharmony_ci free_sem(sem, MAX_SEMS); 197f08c3bdfSopenharmony_cierr_free_unshared: 198f08c3bdfSopenharmony_ci free_pages(pages + UNSHARED_PAGE, N_UNSHARED_PAGES); 199f08c3bdfSopenharmony_cierr_free_shared: 200f08c3bdfSopenharmony_ci free_shared_pages(pages + SHARED_PAGE, N_SHARED_PAGES); 201f08c3bdfSopenharmony_ci } 202f08c3bdfSopenharmony_ci#else 203f08c3bdfSopenharmony_ci tst_resm(TCONF, NUMA_ERROR_MSG); 204f08c3bdfSopenharmony_ci#endif 205f08c3bdfSopenharmony_ci 206f08c3bdfSopenharmony_ci cleanup(); 207f08c3bdfSopenharmony_ci 208f08c3bdfSopenharmony_ci tst_exit(); 209f08c3bdfSopenharmony_ci} 210f08c3bdfSopenharmony_ci 211f08c3bdfSopenharmony_ci/* 212f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test 213f08c3bdfSopenharmony_ci */ 214f08c3bdfSopenharmony_civoid setup(void) 215f08c3bdfSopenharmony_ci{ 216f08c3bdfSopenharmony_ci 217f08c3bdfSopenharmony_ci tst_sig(FORK, DEF_HANDLER, cleanup); 218f08c3bdfSopenharmony_ci 219f08c3bdfSopenharmony_ci check_config(N_TEST_NODES); 220f08c3bdfSopenharmony_ci 221f08c3bdfSopenharmony_ci /* Pause if that option was specified 222f08c3bdfSopenharmony_ci * TEST_PAUSE contains the code to fork the test with the -c option. 223f08c3bdfSopenharmony_ci */ 224f08c3bdfSopenharmony_ci TEST_PAUSE; 225f08c3bdfSopenharmony_ci} 226f08c3bdfSopenharmony_ci 227f08c3bdfSopenharmony_ci/* 228f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at completion 229f08c3bdfSopenharmony_ci */ 230f08c3bdfSopenharmony_civoid cleanup(void) 231f08c3bdfSopenharmony_ci{ 232f08c3bdfSopenharmony_ci 233f08c3bdfSopenharmony_ci} 234