1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: LGPL-2.1-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (C) 2013 LG Electronics. 4f08c3bdfSopenharmony_ci * Author: Joonsoo Kim 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * Test to preserve a reserved page against no-reserved mapping. If all 11f08c3bdfSopenharmony_ci * hugepages are reserved, access to no-reserved shared mapping cause a 12f08c3bdfSopenharmony_ci * process die, instead of stealing a hugepage which is reserved for other 13f08c3bdfSopenharmony_ci * process. 14f08c3bdfSopenharmony_ci */ 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include <setjmp.h> 17f08c3bdfSopenharmony_ci#include "hugetlb.h" 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci#define MNTPOINT "hugetlbfs/" 20f08c3bdfSopenharmony_cistatic long hpage_size; 21f08c3bdfSopenharmony_cistatic int fd1 = -1, fd2 = -1; 22f08c3bdfSopenharmony_cistatic sigjmp_buf sig_escape; 23f08c3bdfSopenharmony_cistatic void *sig_expected = MAP_FAILED; 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_cistatic void sig_handler(int signum, siginfo_t *si, void *uc) 26f08c3bdfSopenharmony_ci{ 27f08c3bdfSopenharmony_ci (void)uc; 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_ci if (signum == SIGBUS) { 30f08c3bdfSopenharmony_ci if (si->si_addr == sig_expected) 31f08c3bdfSopenharmony_ci siglongjmp(sig_escape, 1); 32f08c3bdfSopenharmony_ci tst_res(TFAIL, "SIGBUS somewhere unexpected: %p (expected: %p)", 33f08c3bdfSopenharmony_ci si->si_addr, sig_expected); 34f08c3bdfSopenharmony_ci } else { 35f08c3bdfSopenharmony_ci tst_res(TFAIL, "Unexpected signal %s", strsignal(signum)); 36f08c3bdfSopenharmony_ci } 37f08c3bdfSopenharmony_ci} 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_cistatic int test_write(void *p) 40f08c3bdfSopenharmony_ci{ 41f08c3bdfSopenharmony_ci volatile char *pl = p; 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_ci if (sigsetjmp(sig_escape, 1)) { 44f08c3bdfSopenharmony_ci /* We got a SIGBUS */ 45f08c3bdfSopenharmony_ci return 1; 46f08c3bdfSopenharmony_ci } 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_ci sig_expected = p; 49f08c3bdfSopenharmony_ci barrier(); 50f08c3bdfSopenharmony_ci *pl = 's'; 51f08c3bdfSopenharmony_ci return 0; 52f08c3bdfSopenharmony_ci} 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_cistatic void run_test(void) 55f08c3bdfSopenharmony_ci{ 56f08c3bdfSopenharmony_ci int nr_hugepages; 57f08c3bdfSopenharmony_ci int surp_hugepages; 58f08c3bdfSopenharmony_ci int ret; 59f08c3bdfSopenharmony_ci char *p, *q; 60f08c3bdfSopenharmony_ci struct sigaction sa = { 61f08c3bdfSopenharmony_ci .sa_sigaction = sig_handler, 62f08c3bdfSopenharmony_ci .sa_flags = SA_SIGINFO, 63f08c3bdfSopenharmony_ci }; 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci nr_hugepages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE); 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci SAFE_SIGACTION(SIGBUS, &sa, NULL); 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci p = SAFE_MMAP(NULL, hpage_size * nr_hugepages, 70f08c3bdfSopenharmony_ci PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0); 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci tst_res(TINFO, "Reserve all hugepages %d", nr_hugepages); 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci q = SAFE_MMAP(NULL, hpage_size, 75f08c3bdfSopenharmony_ci PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, fd2, 0); 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci tst_res(TINFO, "Write to %p to steal reserved page", q); 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_ci surp_hugepages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SURP); 80f08c3bdfSopenharmony_ci ret = test_write(q); 81f08c3bdfSopenharmony_ci if (ret == 1) { 82f08c3bdfSopenharmony_ci tst_res(TPASS, "Successful with SIGSEGV received"); 83f08c3bdfSopenharmony_ci goto cleanup; 84f08c3bdfSopenharmony_ci } 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci /* Provisioning succeeded because of overcommit */ 87f08c3bdfSopenharmony_ci if (SAFE_READ_MEMINFO(MEMINFO_HPAGE_SURP) == 88f08c3bdfSopenharmony_ci surp_hugepages + 1) { 89f08c3bdfSopenharmony_ci tst_res(TPASS, "Successful because of surplus pages"); 90f08c3bdfSopenharmony_ci goto cleanup; 91f08c3bdfSopenharmony_ci } 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_ci tst_res(TFAIL, "Steal reserved page"); 94f08c3bdfSopenharmony_cicleanup: 95f08c3bdfSopenharmony_ci SAFE_MUNMAP(p, hpage_size * nr_hugepages); 96f08c3bdfSopenharmony_ci SAFE_MUNMAP(q, hpage_size); 97f08c3bdfSopenharmony_ci} 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_cistatic void setup(void) 100f08c3bdfSopenharmony_ci{ 101f08c3bdfSopenharmony_ci hpage_size = tst_get_hugepage_size(); 102f08c3bdfSopenharmony_ci fd1 = tst_creat_unlinked(MNTPOINT, 0); 103f08c3bdfSopenharmony_ci fd2 = tst_creat_unlinked(MNTPOINT, 0); 104f08c3bdfSopenharmony_ci} 105f08c3bdfSopenharmony_ci 106f08c3bdfSopenharmony_cistatic void cleanup(void) 107f08c3bdfSopenharmony_ci{ 108f08c3bdfSopenharmony_ci if (fd1 >= 0) 109f08c3bdfSopenharmony_ci SAFE_CLOSE(fd1); 110f08c3bdfSopenharmony_ci if (fd2 >= 0) 111f08c3bdfSopenharmony_ci SAFE_CLOSE(fd2); 112f08c3bdfSopenharmony_ci} 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_cistatic struct tst_test test = { 115f08c3bdfSopenharmony_ci .needs_root = 1, 116f08c3bdfSopenharmony_ci .mntpoint = MNTPOINT, 117f08c3bdfSopenharmony_ci .needs_hugetlbfs = 1, 118f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 119f08c3bdfSopenharmony_ci .setup = setup, 120f08c3bdfSopenharmony_ci .cleanup = cleanup, 121f08c3bdfSopenharmony_ci .test_all = run_test, 122f08c3bdfSopenharmony_ci .hugepages = {2, TST_NEEDS}, 123f08c3bdfSopenharmony_ci}; 124