1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2004 4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2013-2016 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci * This is a test case for madvise(2) system call. 9f08c3bdfSopenharmony_ci * It tests madvise(2) with combinations of advice values. 10f08c3bdfSopenharmony_ci * No error should be returned. 11f08c3bdfSopenharmony_ci */ 12f08c3bdfSopenharmony_ci 13f08c3bdfSopenharmony_ci#include <sys/types.h> 14f08c3bdfSopenharmony_ci#include <sys/stat.h> 15f08c3bdfSopenharmony_ci#include <sys/mount.h> 16f08c3bdfSopenharmony_ci#include <errno.h> 17f08c3bdfSopenharmony_ci#include <fcntl.h> 18f08c3bdfSopenharmony_ci#include <stdio.h> 19f08c3bdfSopenharmony_ci#include <string.h> 20f08c3bdfSopenharmony_ci#include <unistd.h> 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#include "tst_test.h" 23f08c3bdfSopenharmony_ci#include "lapi/mmap.h" 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#define TMP_DIR "tmp_madvise" 26f08c3bdfSopenharmony_ci#define TEST_FILE TMP_DIR"/testfile" 27f08c3bdfSopenharmony_ci#define KSM_SYS_DIR "/sys/kernel/mm/ksm" 28f08c3bdfSopenharmony_ci#define STR "abcdefghijklmnopqrstuvwxyz12345\n" 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_cistatic char *sfile; 31f08c3bdfSopenharmony_cistatic char *amem; 32f08c3bdfSopenharmony_cistatic struct stat st; 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_cistatic struct tcase { 35f08c3bdfSopenharmony_ci int advice; 36f08c3bdfSopenharmony_ci char *name; 37f08c3bdfSopenharmony_ci char **addr; 38f08c3bdfSopenharmony_ci} tcases[] = { 39f08c3bdfSopenharmony_ci {MADV_NORMAL, "MADV_NORMAL", &sfile}, 40f08c3bdfSopenharmony_ci {MADV_RANDOM, "MADV_RANDOM", &sfile}, 41f08c3bdfSopenharmony_ci {MADV_SEQUENTIAL, "MADV_SEQUENTIAL", &sfile}, 42f08c3bdfSopenharmony_ci {MADV_WILLNEED, "MADV_WILLNEED", &sfile}, 43f08c3bdfSopenharmony_ci {MADV_DONTNEED, "MADV_DONTNEED", &sfile}, 44f08c3bdfSopenharmony_ci {MADV_REMOVE, "MADV_REMOVE", &sfile}, /* since Linux 2.6.16 */ 45f08c3bdfSopenharmony_ci {MADV_DONTFORK, "MADV_DONTFORK", &sfile}, /* since Linux 2.6.16 */ 46f08c3bdfSopenharmony_ci {MADV_DOFORK, "MADV_DOFORK", &sfile}, /* since Linux 2.6.16 */ 47f08c3bdfSopenharmony_ci {MADV_HWPOISON, "MADV_HWPOISON", &sfile}, /* since Linux 2.6.32 */ 48f08c3bdfSopenharmony_ci {MADV_MERGEABLE, "MADV_MERGEABLE", &sfile}, /* since Linux 2.6.32 */ 49f08c3bdfSopenharmony_ci {MADV_UNMERGEABLE, "MADV_UNMERGEABLE", &sfile}, /* since Linux 2.6.32 */ 50f08c3bdfSopenharmony_ci {MADV_HUGEPAGE, "MADV_HUGEPAGE", &amem}, /* since Linux 2.6.38 */ 51f08c3bdfSopenharmony_ci {MADV_NOHUGEPAGE, "MADV_NOHUGEPAGE", &amem}, /* since Linux 2.6.38 */ 52f08c3bdfSopenharmony_ci {MADV_DONTDUMP, "MADV_DONTDUMP", &sfile}, /* since Linux 3.4 */ 53f08c3bdfSopenharmony_ci {MADV_DODUMP, "MADV_DODUMP", &sfile}, /* since Linux 3.4 */ 54f08c3bdfSopenharmony_ci {MADV_FREE, "MADV_FREE", &amem}, /* since Linux 4.5 */ 55f08c3bdfSopenharmony_ci {MADV_WIPEONFORK, "MADV_WIPEONFORK", &amem}, /* since Linux 4.14 */ 56f08c3bdfSopenharmony_ci {MADV_KEEPONFORK, "MADV_KEEPONFORK", &amem}, /* since Linux 4.14 */ 57f08c3bdfSopenharmony_ci {MADV_COLD, "MADV_COLD", &amem}, /* since Linux 5.4 */ 58f08c3bdfSopenharmony_ci {MADV_PAGEOUT, "MADV_PAGEOUT", &amem}, /* since Linux 5.4 */ 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci}; 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_cistatic void setup(void) 63f08c3bdfSopenharmony_ci{ 64f08c3bdfSopenharmony_ci unsigned int i; 65f08c3bdfSopenharmony_ci int fd; 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci SAFE_MKDIR(TMP_DIR, 0664); 68f08c3bdfSopenharmony_ci SAFE_MOUNT(TMP_DIR, TMP_DIR, "tmpfs", 0, NULL); 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664); 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci /* Writing 40 KB of random data into this file [32 * 1280 = 40960] */ 73f08c3bdfSopenharmony_ci for (i = 0; i < 1280; i++) 74f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd, STR, strlen(STR)); 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci SAFE_FSTAT(fd, &st); 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci /* Map the input file into shared memory */ 79f08c3bdfSopenharmony_ci sfile = SAFE_MMAP(NULL, st.st_size, 80f08c3bdfSopenharmony_ci PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci /* Map the input file into private memory. MADV_HUGEPAGE only works 83f08c3bdfSopenharmony_ci * with private anonymous pages */ 84f08c3bdfSopenharmony_ci amem = SAFE_MMAP(NULL, st.st_size, 85f08c3bdfSopenharmony_ci PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 88f08c3bdfSopenharmony_ci} 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_cistatic void cleanup(void) 91f08c3bdfSopenharmony_ci{ 92f08c3bdfSopenharmony_ci SAFE_MUNMAP(sfile, st.st_size); 93f08c3bdfSopenharmony_ci SAFE_MUNMAP(amem, st.st_size); 94f08c3bdfSopenharmony_ci SAFE_UMOUNT(TMP_DIR); 95f08c3bdfSopenharmony_ci} 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_cistatic void verify_madvise(unsigned int i) 98f08c3bdfSopenharmony_ci{ 99f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[i]; 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci TEST(madvise(*(tc->addr), st.st_size, tc->advice)); 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_ci if (TST_RET == -1) { 104f08c3bdfSopenharmony_ci if (TST_ERR == EINVAL) { 105f08c3bdfSopenharmony_ci tst_res(TCONF, "%s is not supported", tc->name); 106f08c3bdfSopenharmony_ci } else { 107f08c3bdfSopenharmony_ci tst_res(TFAIL, "madvise test for %s failed with " 108f08c3bdfSopenharmony_ci "return = %ld, errno = %d : %s", 109f08c3bdfSopenharmony_ci tc->name, TST_RET, TST_ERR, 110f08c3bdfSopenharmony_ci tst_strerrno(TFAIL | TTERRNO)); 111f08c3bdfSopenharmony_ci } 112f08c3bdfSopenharmony_ci } else { 113f08c3bdfSopenharmony_ci tst_res(TPASS, "madvise test for %s PASSED", tc->name); 114f08c3bdfSopenharmony_ci } 115f08c3bdfSopenharmony_ci} 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_cistatic struct tst_test test = { 118f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 119f08c3bdfSopenharmony_ci .test = verify_madvise, 120f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 121f08c3bdfSopenharmony_ci .needs_root = 1, 122f08c3bdfSopenharmony_ci .setup = setup, 123f08c3bdfSopenharmony_ci .cleanup = cleanup, 124f08c3bdfSopenharmony_ci}; 125