vm_util.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <sys/mman.h>
  5. #include <err.h>
  6. #include <stdarg.h>
  7. #include <strings.h> /* ffsl() */
  8. #include <unistd.h> /* _SC_PAGESIZE */
  9. #include "kselftest.h"
  10. #include <linux/fs.h>
  11. #define BIT_ULL(nr) (1ULL << (nr))
  12. #define PM_SOFT_DIRTY BIT_ULL(55)
  13. #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
  14. #define PM_UFFD_WP BIT_ULL(57)
  15. #define PM_GUARD_REGION BIT_ULL(58)
  16. #define PM_FILE BIT_ULL(61)
  17. #define PM_SWAP BIT_ULL(62)
  18. #define PM_PRESENT BIT_ULL(63)
  19. #define KPF_COMPOUND_HEAD BIT_ULL(15)
  20. #define KPF_COMPOUND_TAIL BIT_ULL(16)
  21. #define KPF_HWPOISON BIT_ULL(19)
  22. #define KPF_THP BIT_ULL(22)
  23. /*
  24. * Ignore the checkpatch warning, we must read from x but don't want to do
  25. * anything with it in order to trigger a read page fault. We therefore must use
  26. * volatile to stop the compiler from optimising this away.
  27. */
  28. #define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))
  29. extern unsigned int __page_size;
  30. extern unsigned int __page_shift;
  31. /*
  32. * Represents an open fd and PROCMAP_QUERY state for binary (via ioctl)
  33. * /proc/$pid/[s]maps lookup.
  34. */
  35. struct procmap_fd {
  36. int fd;
  37. struct procmap_query query;
  38. };
  39. static inline unsigned int psize(void)
  40. {
  41. if (!__page_size)
  42. __page_size = sysconf(_SC_PAGESIZE);
  43. return __page_size;
  44. }
  45. static inline unsigned int pshift(void)
  46. {
  47. if (!__page_shift)
  48. __page_shift = (ffsl(psize()) - 1);
  49. return __page_shift;
  50. }
  51. static inline void force_read_pages(char *addr, unsigned int nr_pages,
  52. size_t pagesize)
  53. {
  54. for (unsigned int i = 0; i < nr_pages; i++)
  55. FORCE_READ(addr[i * pagesize]);
  56. }
  57. bool detect_huge_zeropage(void);
  58. /*
  59. * Plan 9 FS has bugs (at least on QEMU) where certain operations fail with
  60. * ENOENT on unlinked files. See
  61. * https://gitlab.com/qemu-project/qemu/-/issues/103 for some info about such
  62. * bugs. There are rumours of NFS implementations with similar bugs.
  63. *
  64. * Ideally, tests should just detect filesystems known to have such issues and
  65. * bail early. But 9pfs has the additional "feature" that it causes fstatfs to
  66. * pass through the f_type field from the host filesystem. To avoid having to
  67. * scrape /proc/mounts or some other hackery, tests can call this function when
  68. * it seems such a bug might have been encountered.
  69. */
  70. static inline void skip_test_dodgy_fs(const char *op_name)
  71. {
  72. ksft_test_result_skip("%s failed with ENOENT. Filesystem might be buggy (9pfs?)\n", op_name);
  73. }
  74. uint64_t pagemap_get_entry(int fd, char *start);
  75. bool pagemap_is_softdirty(int fd, char *start);
  76. bool pagemap_is_swapped(int fd, char *start);
  77. bool pagemap_is_populated(int fd, char *start);
  78. unsigned long pagemap_get_pfn(int fd, char *start);
  79. void clear_softdirty(void);
  80. bool check_for_pattern(FILE *fp, const char *pattern, char *buf, size_t len);
  81. uint64_t read_pmd_pagesize(void);
  82. unsigned long rss_anon(void);
  83. bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);
  84. bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size);
  85. bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
  86. int64_t allocate_transhuge(void *ptr, int pagemap_fd);
  87. unsigned long default_huge_page_size(void);
  88. int detect_hugetlb_page_sizes(size_t sizes[], int max);
  89. int pageflags_get(unsigned long pfn, int kpageflags_fd, uint64_t *flags);
  90. int uffd_register(int uffd, void *addr, uint64_t len,
  91. bool miss, bool wp, bool minor);
  92. int uffd_unregister(int uffd, void *addr, uint64_t len);
  93. int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
  94. bool miss, bool wp, bool minor, uint64_t *ioctls);
  95. unsigned long get_free_hugepages(void);
  96. bool check_vmflag_io(void *addr);
  97. bool check_vmflag_pfnmap(void *addr);
  98. bool check_vmflag_guard(void *addr);
  99. int open_procmap(pid_t pid, struct procmap_fd *procmap_out);
  100. int query_procmap(struct procmap_fd *procmap);
  101. bool find_vma_procmap(struct procmap_fd *procmap, void *address);
  102. int close_procmap(struct procmap_fd *procmap);
  103. int write_sysfs(const char *file_path, unsigned long val);
  104. int read_sysfs(const char *file_path, unsigned long *val);
  105. bool softdirty_supported(void);
  106. static inline int open_self_procmap(struct procmap_fd *procmap_out)
  107. {
  108. pid_t pid = getpid();
  109. return open_procmap(pid, procmap_out);
  110. }
  111. /* These helpers need to be inline to match the kselftest.h idiom. */
  112. static char test_name[1024];
  113. static inline void log_test_start(const char *name, ...)
  114. {
  115. va_list args;
  116. va_start(args, name);
  117. vsnprintf(test_name, sizeof(test_name), name, args);
  118. ksft_print_msg("[RUN] %s\n", test_name);
  119. va_end(args);
  120. }
  121. static inline void log_test_result(int result)
  122. {
  123. ksft_test_result_report(result, "%s\n", test_name);
  124. }
  125. static inline int sz2ord(size_t size, size_t pagesize)
  126. {
  127. return __builtin_ctzll(size / pagesize);
  128. }
  129. void *sys_mremap(void *old_address, unsigned long old_size,
  130. unsigned long new_size, int flags, void *new_address);
  131. long ksm_get_self_zero_pages(void);
  132. long ksm_get_self_merging_pages(void);
  133. long ksm_get_full_scans(void);
  134. int ksm_use_zero_pages(void);
  135. int ksm_start(void);
  136. int ksm_stop(void);
  137. int get_hardware_corrupted_size(unsigned long *val);
  138. int unpoison_memory(unsigned long pfn);
  139. /*
  140. * On ppc64 this will only work with radix 2M hugepage size
  141. */
  142. #define HPAGE_SHIFT 21
  143. #define HPAGE_SIZE (1 << HPAGE_SHIFT)
  144. #define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0)
  145. #define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1))