mlock-random-test.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * It tests the mlock/mlock2() when they are invoked
  4. * on randomly memory region.
  5. */
  6. #include <unistd.h>
  7. #include <sys/resource.h>
  8. #include <sys/capability.h>
  9. #include <sys/mman.h>
  10. #include <linux/mman.h>
  11. #include <fcntl.h>
  12. #include <string.h>
  13. #include <sys/ipc.h>
  14. #include <sys/shm.h>
  15. #include <time.h>
  16. #include "kselftest.h"
  17. #include "mlock2.h"
  18. #define CHUNK_UNIT (128 * 1024)
  19. #define MLOCK_RLIMIT_SIZE (CHUNK_UNIT * 2)
  20. #define MLOCK_WITHIN_LIMIT_SIZE CHUNK_UNIT
  21. #define MLOCK_OUTOF_LIMIT_SIZE (CHUNK_UNIT * 3)
  22. #define TEST_LOOP 100
  23. #define PAGE_ALIGN(size, ps) (((size) + ((ps) - 1)) & ~((ps) - 1))
  24. int set_cap_limits(rlim_t max)
  25. {
  26. struct rlimit new;
  27. cap_t cap = cap_init();
  28. new.rlim_cur = max;
  29. new.rlim_max = max;
  30. if (setrlimit(RLIMIT_MEMLOCK, &new)) {
  31. ksft_perror("setrlimit() returns error\n");
  32. return -1;
  33. }
  34. /* drop capabilities including CAP_IPC_LOCK */
  35. if (cap_set_proc(cap)) {
  36. ksft_perror("cap_set_proc() returns error\n");
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. int get_proc_locked_vm_size(void)
  42. {
  43. FILE *f;
  44. int ret = -1;
  45. char line[1024] = {0};
  46. unsigned long lock_size = 0;
  47. f = fopen("/proc/self/status", "r");
  48. if (!f)
  49. ksft_exit_fail_msg("fopen: %s\n", strerror(errno));
  50. while (fgets(line, 1024, f)) {
  51. if (strstr(line, "VmLck")) {
  52. ret = sscanf(line, "VmLck:\t%8lu kB", &lock_size);
  53. if (ret <= 0) {
  54. fclose(f);
  55. ksft_exit_fail_msg("sscanf() on VmLck error: %s: %d\n",
  56. line, ret);
  57. }
  58. fclose(f);
  59. return (int)(lock_size << 10);
  60. }
  61. }
  62. fclose(f);
  63. ksft_exit_fail_msg("cannot parse VmLck in /proc/self/status: %s\n", strerror(errno));
  64. return -1;
  65. }
  66. /*
  67. * Get the MMUPageSize of the memory region including input
  68. * address from proc file.
  69. *
  70. * return value: on error case, 0 will be returned.
  71. * Otherwise the page size(in bytes) is returned.
  72. */
  73. int get_proc_page_size(unsigned long addr)
  74. {
  75. FILE *smaps;
  76. char *line;
  77. unsigned long mmupage_size = 0;
  78. size_t size;
  79. smaps = seek_to_smaps_entry(addr);
  80. if (!smaps)
  81. ksft_exit_fail_msg("Unable to parse /proc/self/smaps\n");
  82. while (getline(&line, &size, smaps) > 0) {
  83. if (!strstr(line, "MMUPageSize")) {
  84. free(line);
  85. line = NULL;
  86. size = 0;
  87. continue;
  88. }
  89. /* found the MMUPageSize of this section */
  90. if (sscanf(line, "MMUPageSize: %8lu kB", &mmupage_size) < 1)
  91. ksft_exit_fail_msg("Unable to parse smaps entry for Size:%s\n",
  92. line);
  93. }
  94. free(line);
  95. if (smaps)
  96. fclose(smaps);
  97. return mmupage_size << 10;
  98. }
  99. /*
  100. * Test mlock/mlock2() on provided memory chunk.
  101. * It expects the mlock/mlock2() to be successful (within rlimit)
  102. *
  103. * With allocated memory chunk [p, p + alloc_size), this
  104. * test will choose start/len randomly to perform mlock/mlock2
  105. * [start, start + len] memory range. The range is within range
  106. * of the allocated chunk.
  107. *
  108. * The memory region size alloc_size is within the rlimit.
  109. * So we always expect a success of mlock/mlock2.
  110. *
  111. * VmLck is assumed to be 0 before this test.
  112. *
  113. * return value: 0 - success
  114. * else: failure
  115. */
  116. static void test_mlock_within_limit(char *p, int alloc_size)
  117. {
  118. int i;
  119. int ret = 0;
  120. int locked_vm_size = 0;
  121. struct rlimit cur;
  122. int page_size = 0;
  123. getrlimit(RLIMIT_MEMLOCK, &cur);
  124. if (cur.rlim_cur < alloc_size)
  125. ksft_exit_fail_msg("alloc_size[%d] < %u rlimit,lead to mlock failure\n",
  126. alloc_size, (unsigned int)cur.rlim_cur);
  127. srand(time(NULL));
  128. for (i = 0; i < TEST_LOOP; i++) {
  129. /*
  130. * - choose mlock/mlock2 randomly
  131. * - choose lock_size randomly but lock_size < alloc_size
  132. * - choose start_offset randomly but p+start_offset+lock_size
  133. * < p+alloc_size
  134. */
  135. int is_mlock = !!(rand() % 2);
  136. int lock_size = rand() % alloc_size;
  137. int start_offset = rand() % (alloc_size - lock_size);
  138. if (is_mlock)
  139. ret = mlock(p + start_offset, lock_size);
  140. else
  141. ret = mlock2_(p + start_offset, lock_size,
  142. MLOCK_ONFAULT);
  143. if (ret)
  144. ksft_exit_fail_msg("%s() failure (%s) at |%p(%d)| mlock:|%p(%d)|\n",
  145. is_mlock ? "mlock" : "mlock2",
  146. strerror(errno), p, alloc_size,
  147. p + start_offset, lock_size);
  148. }
  149. /*
  150. * Check VmLck left by the tests.
  151. */
  152. locked_vm_size = get_proc_locked_vm_size();
  153. page_size = get_proc_page_size((unsigned long)p);
  154. if (locked_vm_size > PAGE_ALIGN(alloc_size, page_size) + page_size)
  155. ksft_exit_fail_msg("%s left VmLck:%d on %d chunk\n",
  156. __func__, locked_vm_size, alloc_size);
  157. ksft_test_result_pass("%s\n", __func__);
  158. }
  159. /*
  160. * We expect the mlock/mlock2() to be fail (outof limitation)
  161. *
  162. * With allocated memory chunk [p, p + alloc_size), this
  163. * test will randomly choose start/len and perform mlock/mlock2
  164. * on [start, start+len] range.
  165. *
  166. * The memory region size alloc_size is above the rlimit.
  167. * And the len to be locked is higher than rlimit.
  168. * So we always expect a failure of mlock/mlock2.
  169. * No locked page number should be increased as a side effect.
  170. *
  171. * return value: 0 - success
  172. * else: failure
  173. */
  174. static void test_mlock_outof_limit(char *p, int alloc_size)
  175. {
  176. int i;
  177. int ret = 0;
  178. int locked_vm_size = 0, old_locked_vm_size = 0;
  179. struct rlimit cur;
  180. getrlimit(RLIMIT_MEMLOCK, &cur);
  181. if (cur.rlim_cur >= alloc_size)
  182. ksft_exit_fail_msg("alloc_size[%d] >%u rlimit, violates test condition\n",
  183. alloc_size, (unsigned int)cur.rlim_cur);
  184. old_locked_vm_size = get_proc_locked_vm_size();
  185. srand(time(NULL));
  186. for (i = 0; i < TEST_LOOP; i++) {
  187. int is_mlock = !!(rand() % 2);
  188. int lock_size = (rand() % (alloc_size - cur.rlim_cur))
  189. + cur.rlim_cur;
  190. int start_offset = rand() % (alloc_size - lock_size);
  191. if (is_mlock)
  192. ret = mlock(p + start_offset, lock_size);
  193. else
  194. ret = mlock2_(p + start_offset, lock_size,
  195. MLOCK_ONFAULT);
  196. if (ret == 0)
  197. ksft_exit_fail_msg("%s() succeeds? on %p(%d) mlock%p(%d)\n",
  198. is_mlock ? "mlock" : "mlock2",
  199. p, alloc_size, p + start_offset, lock_size);
  200. }
  201. locked_vm_size = get_proc_locked_vm_size();
  202. if (locked_vm_size != old_locked_vm_size)
  203. ksft_exit_fail_msg("tests leads to new mlocked page: old[%d], new[%d]\n",
  204. old_locked_vm_size,
  205. locked_vm_size);
  206. ksft_test_result_pass("%s\n", __func__);
  207. }
  208. int main(int argc, char **argv)
  209. {
  210. char *p = NULL;
  211. ksft_print_header();
  212. if (set_cap_limits(MLOCK_RLIMIT_SIZE))
  213. ksft_finished();
  214. ksft_set_plan(2);
  215. p = malloc(MLOCK_WITHIN_LIMIT_SIZE);
  216. if (p == NULL)
  217. ksft_exit_fail_msg("malloc() failure: %s\n", strerror(errno));
  218. test_mlock_within_limit(p, MLOCK_WITHIN_LIMIT_SIZE);
  219. munlock(p, MLOCK_WITHIN_LIMIT_SIZE);
  220. free(p);
  221. p = malloc(MLOCK_OUTOF_LIMIT_SIZE);
  222. if (p == NULL)
  223. ksft_exit_fail_msg("malloc() failure: %s\n", strerror(errno));
  224. test_mlock_outof_limit(p, MLOCK_OUTOF_LIMIT_SIZE);
  225. munlock(p, MLOCK_OUTOF_LIMIT_SIZE);
  226. free(p);
  227. ksft_finished();
  228. }