1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: LGPL-2.1-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (C) 2005-2006 IBM Corporation. 4f08c3bdfSopenharmony_ci * Author: Mel Gorman 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * fadvise() on some kernels can cause the reservation counter to get 11f08c3bdfSopenharmony_ci * corrupted. The problem is that the patches are allocated for the 12f08c3bdfSopenharmony_ci * reservation but not faulted in at the time of allocation. The counters 13f08c3bdfSopenharmony_ci * do not get updated and effectively "leak". This test identifies whether 14f08c3bdfSopenharmony_ci * the kernel is vulnerable to the problem or not. It's fixed in kernel 15f08c3bdfSopenharmony_ci * by commit f2deae9d4e70793568ef9e85d227abb7bef5b622. 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 long hpage_size; 29f08c3bdfSopenharmony_cistatic int fd = -1; 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_cistatic void run_test(void) 32f08c3bdfSopenharmony_ci{ 33f08c3bdfSopenharmony_ci void *p; 34f08c3bdfSopenharmony_ci unsigned long initial_rsvd, map_rsvd, fadvise_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 SAFE_POSIX_FADVISE(fd, 0, hpage_size, POSIX_FADV_WILLNEED); 47f08c3bdfSopenharmony_ci fadvise_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD); 48f08c3bdfSopenharmony_ci tst_res(TINFO, "Reserve count after fadvise: %lu", fadvise_rsvd); 49f08c3bdfSopenharmony_ci 50f08c3bdfSopenharmony_ci memset(p, 1, hpage_size); 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci SAFE_MUNMAP(p, hpage_size); 53f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 54f08c3bdfSopenharmony_ci end_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD); 55f08c3bdfSopenharmony_ci tst_res(TINFO, "Reserve count after close: %lu", end_rsvd); 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci TST_EXP_EQ_LU(end_rsvd, initial_rsvd); 58f08c3bdfSopenharmony_ci} 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_cistatic void setup(void) 61f08c3bdfSopenharmony_ci{ 62f08c3bdfSopenharmony_ci hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024; 63f08c3bdfSopenharmony_ci} 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_cistatic void cleanup(void) 66f08c3bdfSopenharmony_ci{ 67f08c3bdfSopenharmony_ci if (fd > 0) 68f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 69f08c3bdfSopenharmony_ci} 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_cistatic struct tst_test test = { 72f08c3bdfSopenharmony_ci .tags = (struct tst_tag[]) { 73f08c3bdfSopenharmony_ci {"linux-git", "f2deae9d4e70"}, 74f08c3bdfSopenharmony_ci {} 75f08c3bdfSopenharmony_ci }, 76f08c3bdfSopenharmony_ci .needs_root = 1, 77f08c3bdfSopenharmony_ci .mntpoint = MNTPOINT, 78f08c3bdfSopenharmony_ci .needs_hugetlbfs = 1, 79f08c3bdfSopenharmony_ci .setup = setup, 80f08c3bdfSopenharmony_ci .cleanup = cleanup, 81f08c3bdfSopenharmony_ci .test_all = run_test, 82f08c3bdfSopenharmony_ci .hugepages = {1, TST_NEEDS}, 83f08c3bdfSopenharmony_ci}; 84