1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: LGPL-2.1-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (C) 2005-2006 IBM Corporation. 4f08c3bdfSopenharmony_ci * Author: Eric B Munson and Mel Gorman 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * madvise() on some kernels can cause the reservation counter to get 11f08c3bdfSopenharmony_ci * corrupted. The problem is that the patches are allocated 12f08c3bdfSopenharmony_ci * for the reservation but not faulted in at the time of allocation. The 13f08c3bdfSopenharmony_ci * counters do not get updated and effectively "leak". This test 14f08c3bdfSopenharmony_ci * identifies whether the kernel is vulnerable to the problem or not. 15f08c3bdfSopenharmony_ci * It is fixed in kernel by commit f2deae9d4e70 16f08c3bdfSopenharmony_ci */ 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#define _GNU_SOURCE 19f08c3bdfSopenharmony_ci#include <stdio.h> 20f08c3bdfSopenharmony_ci#include <sys/mount.h> 21f08c3bdfSopenharmony_ci#include <limits.h> 22f08c3bdfSopenharmony_ci#include <sys/param.h> 23f08c3bdfSopenharmony_ci#include <sys/types.h> 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#include "hugetlb.h" 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_ci#define MNTPOINT "hugetlbfs/" 28f08c3bdfSopenharmony_cistatic int fd = -1; 29f08c3bdfSopenharmony_cistatic long hpage_size; 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_cistatic void run_test(void) 32f08c3bdfSopenharmony_ci{ 33f08c3bdfSopenharmony_ci void *p; 34f08c3bdfSopenharmony_ci unsigned long initial_rsvd, map_rsvd, madvise_rsvd, end_rsvd; 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci fd = tst_creat_unlinked(MNTPOINT, 0); 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci initial_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD); 39f08c3bdfSopenharmony_ci tst_res(TINFO, "Reserve count before map: %lu", initial_rsvd); 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_ci p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, 42f08c3bdfSopenharmony_ci fd, 0); 43f08c3bdfSopenharmony_ci map_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD); 44f08c3bdfSopenharmony_ci tst_res(TINFO, "Reserve count after map: %lu", map_rsvd); 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_ci if (madvise(p, hpage_size, MADV_WILLNEED) == -1) 47f08c3bdfSopenharmony_ci tst_brk(TBROK|TERRNO, "madvise()"); 48f08c3bdfSopenharmony_ci madvise_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD); 49f08c3bdfSopenharmony_ci tst_res(TINFO, "Reserve count after madvise: %lu", madvise_rsvd); 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci SAFE_MUNMAP(p, hpage_size); 52f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 53f08c3bdfSopenharmony_ci end_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD); 54f08c3bdfSopenharmony_ci tst_res(TINFO, "Reserve count after close(): %lu", end_rsvd); 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci TST_EXP_EQ_LU(end_rsvd, initial_rsvd); 57f08c3bdfSopenharmony_ci} 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_cistatic void setup(void) 60f08c3bdfSopenharmony_ci{ 61f08c3bdfSopenharmony_ci hpage_size = SAFE_READ_MEMINFO("Hugepagesize:")*1024; 62f08c3bdfSopenharmony_ci} 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_cistatic void cleanup(void) 65f08c3bdfSopenharmony_ci{ 66f08c3bdfSopenharmony_ci if (fd >= 0) 67f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 68f08c3bdfSopenharmony_ci} 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_cistatic struct tst_test test = { 71f08c3bdfSopenharmony_ci .tags = (struct tst_tag[]) { 72f08c3bdfSopenharmony_ci {"linux-git", "f2deae9d4e70"}, 73f08c3bdfSopenharmony_ci {} 74f08c3bdfSopenharmony_ci }, 75f08c3bdfSopenharmony_ci .needs_root = 1, 76f08c3bdfSopenharmony_ci .mntpoint = MNTPOINT, 77f08c3bdfSopenharmony_ci .needs_hugetlbfs = 1, 78f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 79f08c3bdfSopenharmony_ci .setup = setup, 80f08c3bdfSopenharmony_ci .cleanup = cleanup, 81f08c3bdfSopenharmony_ci .test_all = run_test, 82f08c3bdfSopenharmony_ci .hugepages = {1, TST_NEEDS}, 83f08c3bdfSopenharmony_ci}; 84