subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Vijay Kumar B. <vijaykumar@bravegnu.org> |
| 3 | * |
| 4 | * Based on testcases/kernel/syscalls/waitpid/waitpid01.c |
| 5 | * Original copyright message: |
| 6 | * |
| 7 | * Copyright (c) International Business Machines Corp., 2001 |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * NAME |
| 26 | * move_pages11.c |
| 27 | * |
| 28 | * DESCRIPTION |
| 29 | * Failure when trying move shared pages. |
| 30 | * |
| 31 | * ALGORITHM |
| 32 | * 1. Allocate a shared memory in NUMA node A. |
| 33 | * 2. Fork another process. |
| 34 | * 3. Use move_pages() to move the pages to NUMA node B, with the |
| 35 | * MPOL_MF_MOVE_ALL. |
| 36 | * 4. Check if errno is set to EPERM. |
| 37 | * |
| 38 | * USAGE: <for command-line> |
| 39 | * move_pages11 [-c n] [-i n] [-I x] [-P x] [-t] |
| 40 | * where, -c n : Run n copies concurrently. |
| 41 | * -i n : Execute test n times. |
| 42 | * -I x : Execute test for x seconds. |
| 43 | * -P x : Pause for x seconds between iterations. |
| 44 | * -t : Turn on syscall timing. |
| 45 | * |
| 46 | * History |
| 47 | * 05/2008 Vijay Kumar |
| 48 | * Initial Version. |
| 49 | * |
| 50 | * Restrictions |
| 51 | * None |
| 52 | */ |
| 53 | |
| 54 | #include <sys/types.h> |
| 55 | #include <sys/wait.h> |
| 56 | #include <unistd.h> |
| 57 | #include <signal.h> |
| 58 | #include <semaphore.h> |
| 59 | #include <errno.h> |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 60 | #include <pwd.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 61 | #include "test.h" |
| 62 | #include "usctest.h" |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 63 | #include "move_pages_support.h" |
| 64 | |
| 65 | #define TEST_PAGES 2 |
| 66 | #define TEST_NODES 2 |
| 67 | |
| 68 | enum { |
| 69 | SEM_CHILD_SETUP, |
| 70 | SEM_PARENT_TEST, |
| 71 | |
| 72 | MAX_SEMS |
| 73 | }; |
| 74 | |
| 75 | void setup(void); |
| 76 | void cleanup(void); |
| 77 | |
| 78 | char *TCID = "move_pages11"; |
| 79 | int TST_TOTAL = 1; |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * child() - touches shared pages, and waits for signal from parent. |
| 83 | * @pages: shared pages allocated in parent |
| 84 | * @sem: semaphore to sync with parent |
| 85 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | void child(void **pages, sem_t * sem) |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 87 | { |
| 88 | int i; |
| 89 | |
| 90 | for (i = 0; i < TEST_PAGES; i++) { |
| 91 | char *page; |
| 92 | |
| 93 | page = pages[i]; |
| 94 | page[0] = 0xAA; |
| 95 | } |
| 96 | |
| 97 | /* Setup complete. Ask parent to continue. */ |
| 98 | if (sem_post(&sem[SEM_CHILD_SETUP]) == -1) |
| 99 | tst_resm(TWARN, "error post semaphore: %s", strerror(errno)); |
| 100 | |
| 101 | /* Wait for testcase in parent to complete. */ |
| 102 | if (sem_wait(&sem[SEM_PARENT_TEST]) == -1) |
| 103 | tst_resm(TWARN, "error wait semaphore: %s", strerror(errno)); |
| 104 | |
| 105 | exit(0); |
| 106 | } |
| 107 | |
| 108 | int main(int argc, char **argv) |
| 109 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 110 | char *msg; |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 111 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 112 | msg = parse_opts(argc, argv, NULL, NULL); |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 113 | if (msg != NULL) { |
| 114 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 115 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | setup(); |
| 120 | |
yaberauneya | ef77253 | 2009-10-09 17:55:43 +0000 | [diff] [blame] | 121 | #if HAVE_NUMA_MOVE_PAGES |
| 122 | unsigned int i; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 123 | int lc; |
Jan Stancek | 134bea0 | 2012-06-28 11:03:18 +0200 | [diff] [blame] | 124 | unsigned int from_node; |
| 125 | unsigned int to_node; |
| 126 | int ret; |
| 127 | |
Jan Stancek | d534a44 | 2012-08-09 14:15:38 +0800 | [diff] [blame] | 128 | ret = get_allowed_nodes(NH_MEMS, 2, &from_node, &to_node); |
| 129 | if (ret < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret); |
yaberauneya | ef77253 | 2009-10-09 17:55:43 +0000 | [diff] [blame] | 131 | |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 132 | /* check for looping state if -i option is given */ |
| 133 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 134 | void *pages[TEST_PAGES] = { 0 }; |
| 135 | int nodes[TEST_PAGES]; |
| 136 | int status[TEST_PAGES]; |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 137 | pid_t cpid; |
| 138 | sem_t *sem; |
| 139 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame^] | 140 | /* reset tst_count in case we are looping */ |
| 141 | tst_count = 0; |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 142 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | ret = alloc_shared_pages_on_node(pages, TEST_PAGES, from_node); |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 144 | if (ret == -1) |
| 145 | continue; |
| 146 | |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 147 | for (i = 0; i < TEST_PAGES; i++) { |
| 148 | nodes[i] = to_node; |
| 149 | } |
| 150 | |
| 151 | sem = alloc_sem(MAX_SEMS); |
| 152 | if (sem == NULL) { |
| 153 | goto err_free_pages; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Fork a child process so that the shared pages are |
| 158 | * now really shared between two processes. |
| 159 | */ |
| 160 | cpid = fork(); |
| 161 | if (cpid == -1) { |
| 162 | tst_resm(TBROK, "forking child failed: %s", |
| 163 | strerror(errno)); |
| 164 | goto err_free_sem; |
| 165 | } else if (cpid == 0) { |
| 166 | child(pages, sem); |
| 167 | } |
| 168 | |
| 169 | /* Wait for child to setup and signal. */ |
| 170 | if (sem_wait(&sem[SEM_CHILD_SETUP]) == -1) |
| 171 | tst_resm(TWARN, "error wait semaphore: %s", |
| 172 | strerror(errno)); |
| 173 | |
| 174 | ret = numa_move_pages(0, TEST_PAGES, pages, nodes, |
| 175 | status, MPOL_MF_MOVE_ALL); |
| 176 | TEST_ERRNO = errno; |
| 177 | if (ret == -1 && errno == EPERM) |
| 178 | tst_resm(TPASS, "move_pages failed with " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | "EPERM as expected"); |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 180 | else |
| 181 | tst_resm(TFAIL, "move_pages did not fail " |
| 182 | "with EPERM"); |
| 183 | |
| 184 | /* Test done. Ask child to terminate. */ |
| 185 | if (sem_post(&sem[SEM_PARENT_TEST]) == -1) |
| 186 | tst_resm(TWARN, "error post semaphore: %s", |
| 187 | strerror(errno)); |
| 188 | /* Read the status, no zombies! */ |
| 189 | wait(NULL); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 190 | err_free_sem: |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 191 | free_sem(sem, MAX_SEMS); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 192 | err_free_pages: |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 193 | free_shared_pages(pages, TEST_PAGES); |
| 194 | } |
yaberauneya | ef77253 | 2009-10-09 17:55:43 +0000 | [diff] [blame] | 195 | #else |
| 196 | tst_resm(TCONF, "move_pages support not found."); |
| 197 | #endif |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 198 | |
| 199 | cleanup(); |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 200 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 201 | tst_exit(); |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * setup() - performs all ONE TIME setup for this test |
| 206 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | void setup(void) |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 208 | { |
| 209 | struct passwd *ltpuser; |
| 210 | |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 211 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 212 | |
| 213 | check_config(TEST_NODES); |
| 214 | |
| 215 | if (geteuid() != 0) { |
| 216 | tst_resm(TBROK, "test must be run as root"); |
| 217 | tst_exit(); |
| 218 | } |
| 219 | |
| 220 | if ((ltpuser = getpwnam("nobody")) == NULL) { |
| 221 | tst_resm(TBROK, "'nobody' user not present"); |
| 222 | tst_exit(); |
| 223 | } |
| 224 | |
| 225 | if (seteuid(ltpuser->pw_uid) == -1) { |
| 226 | tst_resm(TBROK, "setting uid to %d failed", ltpuser->pw_uid); |
| 227 | tst_exit(); |
| 228 | } |
| 229 | |
| 230 | /* Pause if that option was specified |
| 231 | * TEST_PAUSE contains the code to fork the test with the -c option. |
| 232 | */ |
| 233 | TEST_PAUSE; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * cleanup() - performs all ONE TIME cleanup for this test at completion |
| 238 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | void cleanup(void) |
subrata_modak | 8dfa1b3 | 2008-07-26 04:15:36 +0000 | [diff] [blame] | 240 | { |
| 241 | /* |
| 242 | * print timing stats if that option was specified. |
| 243 | * print errno log if that option was specified. |
| 244 | */ |
| 245 | TEST_CLEANUP; |
| 246 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 247 | } |