diff options
| author | Pavel Emelyanov <xemul@parallels.com> | 2013-04-30 18:27:06 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-04-30 20:04:01 -0400 |
| commit | 58c7be84fec87b3a96964d65129b36c0e8c59a19 (patch) | |
| tree | 8a4c9fe5a1b23bd481542e371410b62ae3393e4e /tools | |
| parent | 835f2f51608fd80e1aef5a8955dabcc36ea528a4 (diff) | |
selftest: add simple test for soft-dirty bit
It creates a mapping of 3 pages and checks that reads, writes and
clear-refs result in present and soft-dirt bits reported from pagemap2
set as expected.
[akpm@linux-foundation.org: alphasort the Makefile TARGETS to reduce rejects]
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/testing/selftests/Makefile | 7 | ||||
| -rw-r--r-- | tools/testing/selftests/soft-dirty/Makefile | 10 | ||||
| -rw-r--r-- | tools/testing/selftests/soft-dirty/soft-dirty.c | 114 |
3 files changed, 128 insertions, 3 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 3cc0ad7ae863..5eff5f785b35 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile | |||
| @@ -1,10 +1,11 @@ | |||
| 1 | TARGETS = breakpoints | 1 | TARGETS = breakpoints |
| 2 | TARGETS += cpu-hotplug | ||
| 3 | TARGETS += efivarfs | ||
| 2 | TARGETS += kcmp | 4 | TARGETS += kcmp |
| 5 | TARGETS += memory-hotplug | ||
| 3 | TARGETS += mqueue | 6 | TARGETS += mqueue |
| 7 | TARGETS += soft-dirty | ||
| 4 | TARGETS += vm | 8 | TARGETS += vm |
| 5 | TARGETS += cpu-hotplug | ||
| 6 | TARGETS += memory-hotplug | ||
| 7 | TARGETS += efivarfs | ||
| 8 | 9 | ||
| 9 | all: | 10 | all: |
| 10 | for TARGET in $(TARGETS); do \ | 11 | for TARGET in $(TARGETS); do \ |
diff --git a/tools/testing/selftests/soft-dirty/Makefile b/tools/testing/selftests/soft-dirty/Makefile new file mode 100644 index 000000000000..a9cdc823d6e0 --- /dev/null +++ b/tools/testing/selftests/soft-dirty/Makefile | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | CFLAGS += -iquote../../../../include/uapi -Wall | ||
| 2 | soft-dirty: soft-dirty.c | ||
| 3 | |||
| 4 | all: soft-dirty | ||
| 5 | |||
| 6 | clean: | ||
| 7 | rm -f soft-dirty | ||
| 8 | |||
| 9 | run_tests: all | ||
| 10 | @./soft-dirty || echo "soft-dirty selftests: [FAIL]" | ||
diff --git a/tools/testing/selftests/soft-dirty/soft-dirty.c b/tools/testing/selftests/soft-dirty/soft-dirty.c new file mode 100644 index 000000000000..aba4f87f87f0 --- /dev/null +++ b/tools/testing/selftests/soft-dirty/soft-dirty.c | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <sys/mman.h> | ||
| 4 | #include <unistd.h> | ||
| 5 | #include <fcntl.h> | ||
| 6 | #include <sys/types.h> | ||
| 7 | |||
| 8 | typedef unsigned long long u64; | ||
| 9 | |||
| 10 | #define PME_PRESENT (1ULL << 63) | ||
| 11 | #define PME_SOFT_DIRTY (1Ull << 55) | ||
| 12 | |||
| 13 | #define PAGES_TO_TEST 3 | ||
| 14 | #ifndef PAGE_SIZE | ||
| 15 | #define PAGE_SIZE 4096 | ||
| 16 | #endif | ||
| 17 | |||
| 18 | static void get_pagemap2(char *mem, u64 *map) | ||
| 19 | { | ||
| 20 | int fd; | ||
| 21 | |||
| 22 | fd = open("/proc/self/pagemap2", O_RDONLY); | ||
| 23 | if (fd < 0) { | ||
| 24 | perror("Can't open pagemap2"); | ||
| 25 | exit(1); | ||
| 26 | } | ||
| 27 | |||
| 28 | lseek(fd, (unsigned long)mem / PAGE_SIZE * sizeof(u64), SEEK_SET); | ||
| 29 | read(fd, map, sizeof(u64) * PAGES_TO_TEST); | ||
| 30 | close(fd); | ||
| 31 | } | ||
| 32 | |||
| 33 | static inline char map_p(u64 map) | ||
| 34 | { | ||
| 35 | return map & PME_PRESENT ? 'p' : '-'; | ||
| 36 | } | ||
| 37 | |||
| 38 | static inline char map_sd(u64 map) | ||
| 39 | { | ||
| 40 | return map & PME_SOFT_DIRTY ? 'd' : '-'; | ||
| 41 | } | ||
| 42 | |||
| 43 | static int check_pte(int step, int page, u64 *map, u64 want) | ||
| 44 | { | ||
| 45 | if ((map[page] & want) != want) { | ||
| 46 | printf("Step %d Page %d has %c%c, want %c%c\n", | ||
| 47 | step, page, | ||
| 48 | map_p(map[page]), map_sd(map[page]), | ||
| 49 | map_p(want), map_sd(want)); | ||
| 50 | return 1; | ||
| 51 | } | ||
| 52 | |||
| 53 | return 0; | ||
| 54 | } | ||
| 55 | |||
| 56 | static void clear_refs(void) | ||
| 57 | { | ||
| 58 | int fd; | ||
| 59 | char *v = "4"; | ||
| 60 | |||
| 61 | fd = open("/proc/self/clear_refs", O_WRONLY); | ||
| 62 | if (write(fd, v, 3) < 3) { | ||
| 63 | perror("Can't clear soft-dirty bit"); | ||
| 64 | exit(1); | ||
| 65 | } | ||
| 66 | close(fd); | ||
| 67 | } | ||
| 68 | |||
| 69 | int main(void) | ||
| 70 | { | ||
| 71 | char *mem, x; | ||
| 72 | u64 map[PAGES_TO_TEST]; | ||
| 73 | |||
| 74 | mem = mmap(NULL, PAGES_TO_TEST * PAGE_SIZE, | ||
| 75 | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, 0, 0); | ||
| 76 | |||
| 77 | x = mem[0]; | ||
| 78 | mem[2 * PAGE_SIZE] = 'c'; | ||
| 79 | get_pagemap2(mem, map); | ||
| 80 | |||
| 81 | if (check_pte(1, 0, map, PME_PRESENT)) | ||
| 82 | return 1; | ||
| 83 | if (check_pte(1, 1, map, 0)) | ||
| 84 | return 1; | ||
| 85 | if (check_pte(1, 2, map, PME_PRESENT | PME_SOFT_DIRTY)) | ||
| 86 | return 1; | ||
| 87 | |||
| 88 | clear_refs(); | ||
| 89 | get_pagemap2(mem, map); | ||
| 90 | |||
| 91 | if (check_pte(2, 0, map, PME_PRESENT)) | ||
| 92 | return 1; | ||
| 93 | if (check_pte(2, 1, map, 0)) | ||
| 94 | return 1; | ||
| 95 | if (check_pte(2, 2, map, PME_PRESENT)) | ||
| 96 | return 1; | ||
| 97 | |||
| 98 | mem[0] = 'a'; | ||
| 99 | mem[PAGE_SIZE] = 'b'; | ||
| 100 | x = mem[2 * PAGE_SIZE]; | ||
| 101 | get_pagemap2(mem, map); | ||
| 102 | |||
| 103 | if (check_pte(3, 0, map, PME_PRESENT | PME_SOFT_DIRTY)) | ||
| 104 | return 1; | ||
| 105 | if (check_pte(3, 1, map, PME_PRESENT | PME_SOFT_DIRTY)) | ||
| 106 | return 1; | ||
| 107 | if (check_pte(3, 2, map, PME_PRESENT)) | ||
| 108 | return 1; | ||
| 109 | |||
| 110 | (void)x; /* gcc warn */ | ||
| 111 | |||
| 112 | printf("PASS\n"); | ||
| 113 | return 0; | ||
| 114 | } | ||
