1/* 2 * Copyright (c) International Business Machines Corp., 2004 3 * Copyright (c) Linux Test Project, 2004-2017 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 * the GNU General Public License for more details. 14 */ 15 16/* 17 * DESCRIPTION 18 * hugeshmat02 - check for EINVAL and EACCES errors with hugetlb 19 * 20 * ALGORITHM 21 * loop if that option was specified 22 * call shmat() using three invalid test cases 23 * check the errno value 24 * issue a PASS message if we get EINVAL or EACCES 25 * otherwise, the tests fails 26 * issue a FAIL message 27 * call cleanup 28 * 29 * HISTORY 30 * 03/2001 - Written by Wayne Boyer 31 * 04/2004 - Updated By Robbie Williamson 32 * 33 * RESTRICTIONS 34 * Must be ran as root 35 */ 36 37#include <pwd.h> 38#include <limits.h> 39#include "hugetlb.h" 40 41#if __WORDSIZE == 64 42#define NADDR 0x10000000eef /* a 64bit non alligned address value */ 43#else 44#define NADDR 0x60000eef /* a non alligned address value */ 45#endif 46 47static size_t shm_size; 48static int shm_id_1 = -1; 49static int shm_id_2 = -1; 50static void *addr; 51 52static long hugepages = 128; 53 54static struct tst_option options[] = { 55 {"s:", &nr_opt, "-s num Set the number of the been allocated hugepages"}, 56 {NULL, NULL, NULL} 57}; 58 59struct tcase { 60 int *shmid; 61 void *addr; 62 int error; 63} tcases[] = { 64 /* EINVAL - the shared memory ID is not valid */ 65 {&shm_id_1, NULL, EINVAL}, 66 /* EINVAL - the address is not page aligned and SHM_RND is not given */ 67 {&shm_id_2, (void *)NADDR, EINVAL} 68}; 69 70static void verify_hugeshmat(unsigned int i) 71{ 72 struct tcase *tc = &tcases[i]; 73 74 addr = shmat(*(tc->shmid), tc->addr, 0); 75 if (addr != (void *)-1) { 76 tst_res(TFAIL, "shmat suceeded unexpectedly"); 77 return; 78 } 79 80 if (errno == tc->error) { 81 tst_res(TPASS | TERRNO, "shmat failed as " 82 "expected"); 83 } else { 84 tst_res(TFAIL | TERRNO, "shmat failed " 85 "unexpectedly - expect errno=%d, " 86 "got", tc->error); 87 } 88} 89 90void setup(void) 91{ 92 long hpage_size; 93 94 save_nr_hugepages(); 95 if (nr_opt) 96 hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX); 97 98 set_sys_tune("nr_hugepages", hugepages, 1); 99 hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024; 100 101 shm_size = hpage_size * hugepages / 2; 102 update_shm_size(&shm_size); 103 shmkey = getipckey(); 104 105 /* create a shared memory resource with read and write permissions */ 106 /* also post increment the shmkey for the next shmget call */ 107 shm_id_2 = shmget(shmkey++, shm_size, 108 SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL); 109 if (shm_id_2 == -1) 110 tst_brk(TBROK | TERRNO, "shmget"); 111} 112 113void cleanup(void) 114{ 115 rm_shm(shm_id_2); 116 restore_nr_hugepages(); 117} 118 119static struct tst_test test = { 120 .needs_root = 1, 121 .needs_tmpdir = 1, 122 .options = options, 123 .tcnt = ARRAY_SIZE(tcases), 124 .test = verify_hugeshmat, 125 .setup = setup, 126 .cleanup = cleanup, 127}; 128