mlock2-tests.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <sys/mman.h>
  4. #include <stdint.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/time.h>
  8. #include <sys/resource.h>
  9. #include <stdbool.h>
  10. #include "mlock2.h"
  11. #include "../kselftest.h"
  12. struct vm_boundaries {
  13. unsigned long start;
  14. unsigned long end;
  15. };
  16. static int get_vm_area(unsigned long addr, struct vm_boundaries *area)
  17. {
  18. FILE *file;
  19. int ret = 1;
  20. char line[1024] = {0};
  21. char *end_addr;
  22. char *stop;
  23. unsigned long start;
  24. unsigned long end;
  25. if (!area)
  26. return ret;
  27. file = fopen("/proc/self/maps", "r");
  28. if (!file) {
  29. perror("fopen");
  30. return ret;
  31. }
  32. memset(area, 0, sizeof(struct vm_boundaries));
  33. while(fgets(line, 1024, file)) {
  34. end_addr = strchr(line, '-');
  35. if (!end_addr) {
  36. printf("cannot parse /proc/self/maps\n");
  37. goto out;
  38. }
  39. *end_addr = '\0';
  40. end_addr++;
  41. stop = strchr(end_addr, ' ');
  42. if (!stop) {
  43. printf("cannot parse /proc/self/maps\n");
  44. goto out;
  45. }
  46. stop = '\0';
  47. sscanf(line, "%lx", &start);
  48. sscanf(end_addr, "%lx", &end);
  49. if (start <= addr && end > addr) {
  50. area->start = start;
  51. area->end = end;
  52. ret = 0;
  53. goto out;
  54. }
  55. }
  56. out:
  57. fclose(file);
  58. return ret;
  59. }
  60. #define VMFLAGS "VmFlags:"
  61. static bool is_vmflag_set(unsigned long addr, const char *vmflag)
  62. {
  63. char *line = NULL;
  64. char *flags;
  65. size_t size = 0;
  66. bool ret = false;
  67. FILE *smaps;
  68. smaps = seek_to_smaps_entry(addr);
  69. if (!smaps) {
  70. printf("Unable to parse /proc/self/smaps\n");
  71. goto out;
  72. }
  73. while (getline(&line, &size, smaps) > 0) {
  74. if (!strstr(line, VMFLAGS)) {
  75. free(line);
  76. line = NULL;
  77. size = 0;
  78. continue;
  79. }
  80. flags = line + strlen(VMFLAGS);
  81. ret = (strstr(flags, vmflag) != NULL);
  82. goto out;
  83. }
  84. out:
  85. free(line);
  86. fclose(smaps);
  87. return ret;
  88. }
  89. #define SIZE "Size:"
  90. #define RSS "Rss:"
  91. #define LOCKED "lo"
  92. static unsigned long get_value_for_name(unsigned long addr, const char *name)
  93. {
  94. char *line = NULL;
  95. size_t size = 0;
  96. char *value_ptr;
  97. FILE *smaps = NULL;
  98. unsigned long value = -1UL;
  99. smaps = seek_to_smaps_entry(addr);
  100. if (!smaps) {
  101. printf("Unable to parse /proc/self/smaps\n");
  102. goto out;
  103. }
  104. while (getline(&line, &size, smaps) > 0) {
  105. if (!strstr(line, name)) {
  106. free(line);
  107. line = NULL;
  108. size = 0;
  109. continue;
  110. }
  111. value_ptr = line + strlen(name);
  112. if (sscanf(value_ptr, "%lu kB", &value) < 1) {
  113. printf("Unable to parse smaps entry for Size\n");
  114. goto out;
  115. }
  116. break;
  117. }
  118. out:
  119. if (smaps)
  120. fclose(smaps);
  121. free(line);
  122. return value;
  123. }
  124. static bool is_vma_lock_on_fault(unsigned long addr)
  125. {
  126. bool locked;
  127. unsigned long vma_size, vma_rss;
  128. locked = is_vmflag_set(addr, LOCKED);
  129. if (!locked)
  130. return false;
  131. vma_size = get_value_for_name(addr, SIZE);
  132. vma_rss = get_value_for_name(addr, RSS);
  133. /* only one page is faulted in */
  134. return (vma_rss < vma_size);
  135. }
  136. #define PRESENT_BIT 0x8000000000000000ULL
  137. #define PFN_MASK 0x007FFFFFFFFFFFFFULL
  138. #define UNEVICTABLE_BIT (1UL << 18)
  139. static int lock_check(unsigned long addr)
  140. {
  141. bool locked;
  142. unsigned long vma_size, vma_rss;
  143. locked = is_vmflag_set(addr, LOCKED);
  144. if (!locked)
  145. return false;
  146. vma_size = get_value_for_name(addr, SIZE);
  147. vma_rss = get_value_for_name(addr, RSS);
  148. return (vma_rss == vma_size);
  149. }
  150. static int unlock_lock_check(char *map)
  151. {
  152. if (is_vmflag_set((unsigned long)map, LOCKED)) {
  153. printf("VMA flag %s is present on page 1 after unlock\n", LOCKED);
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. static int test_mlock_lock()
  159. {
  160. char *map;
  161. int ret = 1;
  162. unsigned long page_size = getpagesize();
  163. map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
  164. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  165. if (map == MAP_FAILED) {
  166. perror("test_mlock_locked mmap");
  167. goto out;
  168. }
  169. if (mlock2_(map, 2 * page_size, 0)) {
  170. if (errno == ENOSYS) {
  171. printf("Cannot call new mlock family, skipping test\n");
  172. _exit(KSFT_SKIP);
  173. }
  174. perror("mlock2(0)");
  175. goto unmap;
  176. }
  177. if (!lock_check((unsigned long)map))
  178. goto unmap;
  179. /* Now unlock and recheck attributes */
  180. if (munlock(map, 2 * page_size)) {
  181. perror("munlock()");
  182. goto unmap;
  183. }
  184. ret = unlock_lock_check(map);
  185. unmap:
  186. munmap(map, 2 * page_size);
  187. out:
  188. return ret;
  189. }
  190. static int onfault_check(char *map)
  191. {
  192. *map = 'a';
  193. if (!is_vma_lock_on_fault((unsigned long)map)) {
  194. printf("VMA is not marked for lock on fault\n");
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. static int unlock_onfault_check(char *map)
  200. {
  201. unsigned long page_size = getpagesize();
  202. if (is_vma_lock_on_fault((unsigned long)map) ||
  203. is_vma_lock_on_fault((unsigned long)map + page_size)) {
  204. printf("VMA is still lock on fault after unlock\n");
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. static int test_mlock_onfault()
  210. {
  211. char *map;
  212. int ret = 1;
  213. unsigned long page_size = getpagesize();
  214. map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
  215. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  216. if (map == MAP_FAILED) {
  217. perror("test_mlock_locked mmap");
  218. goto out;
  219. }
  220. if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
  221. if (errno == ENOSYS) {
  222. printf("Cannot call new mlock family, skipping test\n");
  223. _exit(KSFT_SKIP);
  224. }
  225. perror("mlock2(MLOCK_ONFAULT)");
  226. goto unmap;
  227. }
  228. if (onfault_check(map))
  229. goto unmap;
  230. /* Now unlock and recheck attributes */
  231. if (munlock(map, 2 * page_size)) {
  232. if (errno == ENOSYS) {
  233. printf("Cannot call new mlock family, skipping test\n");
  234. _exit(KSFT_SKIP);
  235. }
  236. perror("munlock()");
  237. goto unmap;
  238. }
  239. ret = unlock_onfault_check(map);
  240. unmap:
  241. munmap(map, 2 * page_size);
  242. out:
  243. return ret;
  244. }
  245. static int test_lock_onfault_of_present()
  246. {
  247. char *map;
  248. int ret = 1;
  249. unsigned long page_size = getpagesize();
  250. map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
  251. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  252. if (map == MAP_FAILED) {
  253. perror("test_mlock_locked mmap");
  254. goto out;
  255. }
  256. *map = 'a';
  257. if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
  258. if (errno == ENOSYS) {
  259. printf("Cannot call new mlock family, skipping test\n");
  260. _exit(KSFT_SKIP);
  261. }
  262. perror("mlock2(MLOCK_ONFAULT)");
  263. goto unmap;
  264. }
  265. if (!is_vma_lock_on_fault((unsigned long)map) ||
  266. !is_vma_lock_on_fault((unsigned long)map + page_size)) {
  267. printf("VMA with present pages is not marked lock on fault\n");
  268. goto unmap;
  269. }
  270. ret = 0;
  271. unmap:
  272. munmap(map, 2 * page_size);
  273. out:
  274. return ret;
  275. }
  276. static int test_munlockall()
  277. {
  278. char *map;
  279. int ret = 1;
  280. unsigned long page_size = getpagesize();
  281. map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
  282. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  283. if (map == MAP_FAILED) {
  284. perror("test_munlockall mmap");
  285. goto out;
  286. }
  287. if (mlockall(MCL_CURRENT)) {
  288. perror("mlockall(MCL_CURRENT)");
  289. goto out;
  290. }
  291. if (!lock_check((unsigned long)map))
  292. goto unmap;
  293. if (munlockall()) {
  294. perror("munlockall()");
  295. goto unmap;
  296. }
  297. if (unlock_lock_check(map))
  298. goto unmap;
  299. munmap(map, 2 * page_size);
  300. map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
  301. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  302. if (map == MAP_FAILED) {
  303. perror("test_munlockall second mmap");
  304. goto out;
  305. }
  306. if (mlockall(MCL_CURRENT | MCL_ONFAULT)) {
  307. perror("mlockall(MCL_CURRENT | MCL_ONFAULT)");
  308. goto unmap;
  309. }
  310. if (onfault_check(map))
  311. goto unmap;
  312. if (munlockall()) {
  313. perror("munlockall()");
  314. goto unmap;
  315. }
  316. if (unlock_onfault_check(map))
  317. goto unmap;
  318. if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
  319. perror("mlockall(MCL_CURRENT | MCL_FUTURE)");
  320. goto out;
  321. }
  322. if (!lock_check((unsigned long)map))
  323. goto unmap;
  324. if (munlockall()) {
  325. perror("munlockall()");
  326. goto unmap;
  327. }
  328. ret = unlock_lock_check(map);
  329. unmap:
  330. munmap(map, 2 * page_size);
  331. out:
  332. munlockall();
  333. return ret;
  334. }
  335. static int test_vma_management(bool call_mlock)
  336. {
  337. int ret = 1;
  338. void *map;
  339. unsigned long page_size = getpagesize();
  340. struct vm_boundaries page1;
  341. struct vm_boundaries page2;
  342. struct vm_boundaries page3;
  343. map = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
  344. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  345. if (map == MAP_FAILED) {
  346. perror("mmap()");
  347. return ret;
  348. }
  349. if (call_mlock && mlock2_(map, 3 * page_size, MLOCK_ONFAULT)) {
  350. if (errno == ENOSYS) {
  351. printf("Cannot call new mlock family, skipping test\n");
  352. _exit(KSFT_SKIP);
  353. }
  354. perror("mlock(ONFAULT)\n");
  355. goto out;
  356. }
  357. if (get_vm_area((unsigned long)map, &page1) ||
  358. get_vm_area((unsigned long)map + page_size, &page2) ||
  359. get_vm_area((unsigned long)map + page_size * 2, &page3)) {
  360. printf("couldn't find mapping in /proc/self/maps\n");
  361. goto out;
  362. }
  363. /*
  364. * Before we unlock a portion, we need to that all three pages are in
  365. * the same VMA. If they are not we abort this test (Note that this is
  366. * not a failure)
  367. */
  368. if (page1.start != page2.start || page2.start != page3.start) {
  369. printf("VMAs are not merged to start, aborting test\n");
  370. ret = 0;
  371. goto out;
  372. }
  373. if (munlock(map + page_size, page_size)) {
  374. perror("munlock()");
  375. goto out;
  376. }
  377. if (get_vm_area((unsigned long)map, &page1) ||
  378. get_vm_area((unsigned long)map + page_size, &page2) ||
  379. get_vm_area((unsigned long)map + page_size * 2, &page3)) {
  380. printf("couldn't find mapping in /proc/self/maps\n");
  381. goto out;
  382. }
  383. /* All three VMAs should be different */
  384. if (page1.start == page2.start || page2.start == page3.start) {
  385. printf("failed to split VMA for munlock\n");
  386. goto out;
  387. }
  388. /* Now unlock the first and third page and check the VMAs again */
  389. if (munlock(map, page_size * 3)) {
  390. perror("munlock()");
  391. goto out;
  392. }
  393. if (get_vm_area((unsigned long)map, &page1) ||
  394. get_vm_area((unsigned long)map + page_size, &page2) ||
  395. get_vm_area((unsigned long)map + page_size * 2, &page3)) {
  396. printf("couldn't find mapping in /proc/self/maps\n");
  397. goto out;
  398. }
  399. /* Now all three VMAs should be the same */
  400. if (page1.start != page2.start || page2.start != page3.start) {
  401. printf("failed to merge VMAs after munlock\n");
  402. goto out;
  403. }
  404. ret = 0;
  405. out:
  406. munmap(map, 3 * page_size);
  407. return ret;
  408. }
  409. static int test_mlockall(int (test_function)(bool call_mlock))
  410. {
  411. int ret = 1;
  412. if (mlockall(MCL_CURRENT | MCL_ONFAULT | MCL_FUTURE)) {
  413. perror("mlockall");
  414. return ret;
  415. }
  416. ret = test_function(false);
  417. munlockall();
  418. return ret;
  419. }
  420. int main(int argc, char **argv)
  421. {
  422. int ret = 0;
  423. ret += test_mlock_lock();
  424. ret += test_mlock_onfault();
  425. ret += test_munlockall();
  426. ret += test_lock_onfault_of_present();
  427. ret += test_vma_management(true);
  428. ret += test_mlockall(test_vma_management);
  429. return ret;
  430. }