1// SPDX-License-Identifier: LGPL-2.1-or-later 2/* 3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation. 4 * Author: David Gibson & Adam Litke 5 */ 6 7/*\ 8 * [Description] 9 * 10 * The test checks that mlocking hugetlb areas works with all combinations 11 * of MAP_PRIVATE and MAP_SHARED with and without MAP_LOCKED specified. 12 */ 13 14#include "hugetlb.h" 15 16#define MNTPOINT "hugetlbfs/" 17 18static int fd = -1; 19static unsigned long hpage_size; 20 21static struct tcase { 22 int flags; 23 char *flags_str; 24} tcases[] = { 25 {MAP_PRIVATE, "MAP_PRIVATE"}, 26 {MAP_SHARED, "MAP_SHARED"}, 27 {MAP_PRIVATE | MAP_LOCKED, "MAP_PRIVATE | MAP_LOCKED"}, 28 {MAP_SHARED | MAP_LOCKED, "MAP_SHARED | MAP_LOCKED"}, 29}; 30 31static void run_test(unsigned int i) 32{ 33 int ret; 34 void *p; 35 struct tcase *tc = &tcases[i]; 36 37 fd = tst_creat_unlinked(MNTPOINT, 0); 38 p = SAFE_MMAP(0, hpage_size, PROT_READ|PROT_WRITE, tc->flags, fd, 0); 39 40 ret = mlock(p, hpage_size); 41 if (ret) { 42 tst_res(TFAIL|TERRNO, "mlock() failed (flags %s)", tc->flags_str); 43 goto cleanup; 44 } 45 46 ret = munlock(p, hpage_size); 47 if (ret) 48 tst_res(TFAIL|TERRNO, "munlock() failed (flags %s)", tc->flags_str); 49 else 50 tst_res(TPASS, "mlock/munlock with %s hugetlb mmap", tc->flags_str); 51 52cleanup: 53 SAFE_MUNMAP(p, hpage_size); 54 SAFE_CLOSE(fd); 55} 56 57static void setup(void) 58{ 59 struct rlimit limit_info; 60 61 hpage_size = tst_get_hugepage_size(); 62 63 SAFE_GETRLIMIT(RLIMIT_MEMLOCK, &limit_info); 64 if (limit_info.rlim_cur < hpage_size) { 65 limit_info.rlim_max = hpage_size; 66 limit_info.rlim_cur = hpage_size; 67 SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &limit_info); 68 } 69} 70 71static void cleanup(void) 72{ 73 if (fd >= 0) 74 SAFE_CLOSE(fd); 75} 76 77static struct tst_test test = { 78 .tcnt = ARRAY_SIZE(tcases), 79 .needs_root = 1, 80 .mntpoint = MNTPOINT, 81 .needs_hugetlbfs = 1, 82 .needs_tmpdir = 1, 83 .setup = setup, 84 .cleanup = cleanup, 85 .test = run_test, 86 .hugepages = {1, TST_NEEDS}, 87}; 88