| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _GNU_SOURCE |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <sys/mman.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <sys/types.h> |
| 29 |
|
| 30 |
#include <hugetlbfs.h> |
| 31 |
|
| 32 |
#include "hugetests.h" |
| 33 |
|
| 34 |
#define P0 "ffffffff" |
| 35 |
#define IOSZ 4096 |
| 36 |
char buf[IOSZ] __attribute__ ((aligned (IOSZ))); |
| 37 |
#define TMPFILE "/tmp/direct" |
| 38 |
|
| 39 |
int main(int argc, char *argv[]) |
| 40 |
{ |
| 41 |
long hpage_size; |
| 42 |
int fd, dfd; |
| 43 |
void *p; |
| 44 |
size_t ret; |
| 45 |
|
| 46 |
test_init(argc, argv); |
| 47 |
|
| 48 |
hpage_size = check_hugepagesize(); |
| 49 |
|
| 50 |
fd = hugetlbfs_unlinked_fd(); |
| 51 |
if (fd < 0) |
| 52 |
FAIL("hugetlbfs_unlinked_fd()"); |
| 53 |
|
| 54 |
dfd = open(TMPFILE, O_CREAT|O_EXCL|O_DIRECT|O_RDWR, 0600); |
| 55 |
if (dfd < 0) |
| 56 |
CONFIG("Failed to open direct-IO file: %s", strerror(errno)); |
| 57 |
unlink(TMPFILE); |
| 58 |
|
| 59 |
p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 60 |
if (p == MAP_FAILED) |
| 61 |
FAIL("mmap hugetlbfs file: %s", strerror(errno)); |
| 62 |
|
| 63 |
memcpy(p, P0, 8); |
| 64 |
|
| 65 |
|
| 66 |
ret = write(dfd, p, IOSZ); |
| 67 |
if (ret == -1) |
| 68 |
FAIL("Direct-IO write from huge page: %s", strerror(errno)); |
| 69 |
if (ret != IOSZ) |
| 70 |
FAIL("Short direct-IO write from huge page"); |
| 71 |
if (lseek(dfd, 0, SEEK_SET) == -1) |
| 72 |
FAIL("lseek: %s", strerror(errno)); |
| 73 |
|
| 74 |
|
| 75 |
ret = read(dfd, buf, IOSZ); |
| 76 |
if (ret == -1) |
| 77 |
FAIL("Direct-IO read to normal memory: %s", strerror(errno)); |
| 78 |
if (ret != IOSZ) |
| 79 |
FAIL("Short direct-IO read to normal memory"); |
| 80 |
if (memcmp(P0, buf, 8)) |
| 81 |
FAIL("Memory mismatch after Direct-IO write"); |
| 82 |
if (lseek(dfd, 0, SEEK_SET) == -1) |
| 83 |
FAIL("lseek: %s", strerror(errno)); |
| 84 |
|
| 85 |
|
| 86 |
memset(p, 0, IOSZ); |
| 87 |
ret = read(dfd, p, IOSZ); |
| 88 |
if (ret == -1) |
| 89 |
FAIL("Direct-IO read to huge page: %s\n", strerror(errno)); |
| 90 |
if (ret != IOSZ) |
| 91 |
FAIL("Short direct-IO read to huge page"); |
| 92 |
|
| 93 |
|
| 94 |
if (memcmp(p, P0, 8)) |
| 95 |
FAIL("Memory mismatch after Direct-IO read"); |
| 96 |
|
| 97 |
close(dfd); |
| 98 |
unlink(TMPFILE); |
| 99 |
|
| 100 |
PASS(); |
| 101 |
} |