1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: LGPL-2.1-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (C) 2005-2006 IBM Corporation. 4f08c3bdfSopenharmony_ci * Author: David Gibson & Adam Litke 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This Test perform Direct Write/Read from/To hugetlbfs file 11f08c3bdfSopenharmony_ci * which is mapped to process address space. The test is checking if it 12f08c3bdfSopenharmony_ci * succeeds and data written or read is not corrupted. 13f08c3bdfSopenharmony_ci */ 14f08c3bdfSopenharmony_ci 15f08c3bdfSopenharmony_ci#define _GNU_SOURCE 16f08c3bdfSopenharmony_ci#include <stdio.h> 17f08c3bdfSopenharmony_ci#include <sys/mount.h> 18f08c3bdfSopenharmony_ci#include <limits.h> 19f08c3bdfSopenharmony_ci#include <sys/param.h> 20f08c3bdfSopenharmony_ci#include <sys/types.h> 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#include "hugetlb.h" 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci#define P0 "ffffffff" 25f08c3bdfSopenharmony_ci#define IOSZ 4096 26f08c3bdfSopenharmony_ci#define NORMAL_PATH "" 27f08c3bdfSopenharmony_ci#define MNTPOINT "hugetlbfs/" 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_cistatic long hpage_size; 30f08c3bdfSopenharmony_cistatic int fd = -1, nfd = -1; 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_cistatic void run_test(void) 33f08c3bdfSopenharmony_ci{ 34f08c3bdfSopenharmony_ci void *p; 35f08c3bdfSopenharmony_ci char buf[IOSZ] __attribute__((aligned(IOSZ))); 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ci fd = tst_creat_unlinked(MNTPOINT, 0); 38f08c3bdfSopenharmony_ci nfd = tst_creat_unlinked(NORMAL_PATH, O_DIRECT); 39f08c3bdfSopenharmony_ci p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); 40f08c3bdfSopenharmony_ci memcpy(p, P0, 8); 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci SAFE_WRITE(1, nfd, p, IOSZ); 43f08c3bdfSopenharmony_ci SAFE_LSEEK(nfd, 0, SEEK_SET); 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci SAFE_READ(1, nfd, buf, IOSZ); 46f08c3bdfSopenharmony_ci if (memcmp(P0, buf, 8)) { 47f08c3bdfSopenharmony_ci tst_res(TFAIL, "Memory mismatch after Direct-IO write"); 48f08c3bdfSopenharmony_ci goto cleanup; 49f08c3bdfSopenharmony_ci } 50f08c3bdfSopenharmony_ci SAFE_LSEEK(nfd, 0, SEEK_SET); 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci memset(p, 0, IOSZ); 53f08c3bdfSopenharmony_ci SAFE_READ(1, nfd, p, IOSZ); 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_ci if (memcmp(p, P0, 8)) 56f08c3bdfSopenharmony_ci tst_res(TFAIL, "Memory mismatch after Direct-IO read"); 57f08c3bdfSopenharmony_ci else 58f08c3bdfSopenharmony_ci tst_res(TPASS, "Direct-IO read/write to/from hugepages is successful"); 59f08c3bdfSopenharmony_cicleanup: 60f08c3bdfSopenharmony_ci SAFE_MUNMAP(p, hpage_size); 61f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 62f08c3bdfSopenharmony_ci SAFE_CLOSE(nfd); 63f08c3bdfSopenharmony_ci} 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_cistatic void setup(void) 66f08c3bdfSopenharmony_ci{ 67f08c3bdfSopenharmony_ci hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024; 68f08c3bdfSopenharmony_ci} 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_cistatic void cleanup(void) 71f08c3bdfSopenharmony_ci{ 72f08c3bdfSopenharmony_ci if (fd > 0) 73f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 74f08c3bdfSopenharmony_ci if (nfd > 0) 75f08c3bdfSopenharmony_ci SAFE_CLOSE(nfd); 76f08c3bdfSopenharmony_ci} 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_cistatic struct tst_test test = { 79f08c3bdfSopenharmony_ci .needs_root = 1, 80f08c3bdfSopenharmony_ci .mntpoint = MNTPOINT, 81f08c3bdfSopenharmony_ci .needs_hugetlbfs = 1, 82f08c3bdfSopenharmony_ci .setup = setup, 83f08c3bdfSopenharmony_ci .cleanup = cleanup, 84f08c3bdfSopenharmony_ci .test_all = run_test, 85f08c3bdfSopenharmony_ci .hugepages = {1, TST_NEEDS}, 86f08c3bdfSopenharmony_ci}; 87