stress_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <sched.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/socket.h>
  12. #include <sys/stat.h>
  13. #include <sys/syscall.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include <unistd.h>
  17. #include <linux/nsfs.h>
  18. #include "../kselftest_harness.h"
  19. #include "../filesystems/utils.h"
  20. #include "wrappers.h"
  21. /*
  22. * Stress tests for namespace active reference counting.
  23. *
  24. * These tests validate that the active reference counting system can handle
  25. * high load scenarios including rapid namespace creation/destruction, large
  26. * numbers of concurrent namespaces, and various edge cases under stress.
  27. */
  28. /*
  29. * Test rapid creation and destruction of user namespaces.
  30. * Create and destroy namespaces in quick succession to stress the
  31. * active reference tracking and ensure no leaks occur.
  32. */
  33. TEST(rapid_namespace_creation_destruction)
  34. {
  35. struct ns_id_req req = {
  36. .size = sizeof(req),
  37. .spare = 0,
  38. .ns_id = 0,
  39. .ns_type = CLONE_NEWUSER,
  40. .spare2 = 0,
  41. .user_ns_id = 0,
  42. };
  43. __u64 ns_ids_before[256], ns_ids_after[256];
  44. ssize_t ret_before, ret_after;
  45. int i;
  46. /* Get baseline count of active user namespaces */
  47. ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
  48. if (ret_before < 0) {
  49. if (errno == ENOSYS)
  50. SKIP(return, "listns() not supported");
  51. ASSERT_GE(ret_before, 0);
  52. }
  53. TH_LOG("Baseline: %zd active user namespaces", ret_before);
  54. /* Rapidly create and destroy 100 user namespaces */
  55. for (i = 0; i < 100; i++) {
  56. pid_t pid = fork();
  57. ASSERT_GE(pid, 0);
  58. if (pid == 0) {
  59. /* Child: create user namespace and immediately exit */
  60. if (setup_userns() < 0)
  61. exit(1);
  62. exit(0);
  63. }
  64. /* Parent: wait for child */
  65. int status;
  66. waitpid(pid, &status, 0);
  67. ASSERT_TRUE(WIFEXITED(status));
  68. ASSERT_EQ(WEXITSTATUS(status), 0);
  69. }
  70. /* Verify we're back to baseline (no leaked namespaces) */
  71. ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
  72. ASSERT_GE(ret_after, 0);
  73. TH_LOG("After 100 rapid create/destroy cycles: %zd active user namespaces", ret_after);
  74. ASSERT_EQ(ret_before, ret_after);
  75. }
  76. /*
  77. * Test creating many concurrent namespaces.
  78. * Verify that listns() correctly tracks all of them and that they all
  79. * become inactive after processes exit.
  80. */
  81. TEST(many_concurrent_namespaces)
  82. {
  83. struct ns_id_req req = {
  84. .size = sizeof(req),
  85. .spare = 0,
  86. .ns_id = 0,
  87. .ns_type = CLONE_NEWUSER,
  88. .spare2 = 0,
  89. .user_ns_id = 0,
  90. };
  91. __u64 ns_ids_before[512], ns_ids_during[512], ns_ids_after[512];
  92. ssize_t ret_before, ret_during, ret_after;
  93. pid_t pids[50];
  94. int num_children = 50;
  95. int i;
  96. int sv[2];
  97. /* Get baseline */
  98. ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
  99. if (ret_before < 0) {
  100. if (errno == ENOSYS)
  101. SKIP(return, "listns() not supported");
  102. ASSERT_GE(ret_before, 0);
  103. }
  104. TH_LOG("Baseline: %zd active user namespaces", ret_before);
  105. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
  106. /* Create many children, each with their own user namespace */
  107. for (i = 0; i < num_children; i++) {
  108. pids[i] = fork();
  109. ASSERT_GE(pids[i], 0);
  110. if (pids[i] == 0) {
  111. /* Child: create user namespace and wait for parent signal */
  112. char c;
  113. close(sv[0]);
  114. if (setup_userns() < 0) {
  115. close(sv[1]);
  116. exit(1);
  117. }
  118. /* Signal parent we're ready */
  119. if (write(sv[1], &c, 1) != 1) {
  120. close(sv[1]);
  121. exit(1);
  122. }
  123. /* Wait for parent signal to exit */
  124. if (read(sv[1], &c, 1) != 1) {
  125. close(sv[1]);
  126. exit(1);
  127. }
  128. close(sv[1]);
  129. exit(0);
  130. }
  131. }
  132. close(sv[1]);
  133. /* Wait for all children to signal ready */
  134. for (i = 0; i < num_children; i++) {
  135. char c;
  136. if (read(sv[0], &c, 1) != 1) {
  137. /* If we fail to read, kill all children and exit */
  138. close(sv[0]);
  139. for (int j = 0; j < num_children; j++)
  140. kill(pids[j], SIGKILL);
  141. for (int j = 0; j < num_children; j++)
  142. waitpid(pids[j], NULL, 0);
  143. ASSERT_TRUE(false);
  144. }
  145. }
  146. /* List namespaces while all children are running */
  147. ret_during = sys_listns(&req, ns_ids_during, ARRAY_SIZE(ns_ids_during), 0);
  148. ASSERT_GE(ret_during, 0);
  149. TH_LOG("With %d children running: %zd active user namespaces", num_children, ret_during);
  150. /* Should have at least num_children more namespaces than baseline */
  151. ASSERT_GE(ret_during, ret_before + num_children);
  152. /* Signal all children to exit */
  153. for (i = 0; i < num_children; i++) {
  154. char c = 'X';
  155. if (write(sv[0], &c, 1) != 1) {
  156. /* If we fail to write, kill remaining children */
  157. close(sv[0]);
  158. for (int j = i; j < num_children; j++)
  159. kill(pids[j], SIGKILL);
  160. for (int j = 0; j < num_children; j++)
  161. waitpid(pids[j], NULL, 0);
  162. ASSERT_TRUE(false);
  163. }
  164. }
  165. close(sv[0]);
  166. /* Wait for all children */
  167. for (i = 0; i < num_children; i++) {
  168. int status;
  169. waitpid(pids[i], &status, 0);
  170. ASSERT_TRUE(WIFEXITED(status));
  171. }
  172. /* Verify we're back to baseline */
  173. ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
  174. ASSERT_GE(ret_after, 0);
  175. TH_LOG("After all children exit: %zd active user namespaces", ret_after);
  176. ASSERT_EQ(ret_before, ret_after);
  177. }
  178. /*
  179. * Test rapid namespace creation with different namespace types.
  180. * Create multiple types of namespaces rapidly to stress the tracking system.
  181. */
  182. TEST(rapid_mixed_namespace_creation)
  183. {
  184. struct ns_id_req req = {
  185. .size = sizeof(req),
  186. .spare = 0,
  187. .ns_id = 0,
  188. .ns_type = 0, /* All types */
  189. .spare2 = 0,
  190. .user_ns_id = 0,
  191. };
  192. __u64 ns_ids_before[512], ns_ids_after[512];
  193. ssize_t ret_before, ret_after;
  194. int i;
  195. /* Get baseline count */
  196. ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
  197. if (ret_before < 0) {
  198. if (errno == ENOSYS)
  199. SKIP(return, "listns() not supported");
  200. ASSERT_GE(ret_before, 0);
  201. }
  202. TH_LOG("Baseline: %zd active namespaces (all types)", ret_before);
  203. /* Rapidly create and destroy namespaces with multiple types */
  204. for (i = 0; i < 50; i++) {
  205. pid_t pid = fork();
  206. ASSERT_GE(pid, 0);
  207. if (pid == 0) {
  208. /* Child: create multiple namespace types */
  209. if (setup_userns() < 0)
  210. exit(1);
  211. /* Create additional namespace types */
  212. if (unshare(CLONE_NEWNET) < 0)
  213. exit(1);
  214. if (unshare(CLONE_NEWUTS) < 0)
  215. exit(1);
  216. if (unshare(CLONE_NEWIPC) < 0)
  217. exit(1);
  218. exit(0);
  219. }
  220. /* Parent: wait for child */
  221. int status;
  222. waitpid(pid, &status, 0);
  223. ASSERT_TRUE(WIFEXITED(status));
  224. }
  225. /* Verify we're back to baseline */
  226. ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
  227. ASSERT_GE(ret_after, 0);
  228. TH_LOG("After 50 rapid mixed namespace cycles: %zd active namespaces", ret_after);
  229. ASSERT_EQ(ret_before, ret_after);
  230. }
  231. /*
  232. * Test nested namespace creation under stress.
  233. * Create deeply nested namespace hierarchies and verify proper cleanup.
  234. */
  235. TEST(nested_namespace_stress)
  236. {
  237. struct ns_id_req req = {
  238. .size = sizeof(req),
  239. .spare = 0,
  240. .ns_id = 0,
  241. .ns_type = CLONE_NEWUSER,
  242. .spare2 = 0,
  243. .user_ns_id = 0,
  244. };
  245. __u64 ns_ids_before[512], ns_ids_after[512];
  246. ssize_t ret_before, ret_after;
  247. int i;
  248. /* Get baseline */
  249. ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
  250. if (ret_before < 0) {
  251. if (errno == ENOSYS)
  252. SKIP(return, "listns() not supported");
  253. ASSERT_GE(ret_before, 0);
  254. }
  255. TH_LOG("Baseline: %zd active user namespaces", ret_before);
  256. /* Create 20 processes, each with nested user namespaces */
  257. for (i = 0; i < 20; i++) {
  258. pid_t pid = fork();
  259. ASSERT_GE(pid, 0);
  260. if (pid == 0) {
  261. int userns_fd;
  262. uid_t orig_uid = getuid();
  263. int depth;
  264. /* Create nested user namespaces (up to 5 levels) */
  265. for (depth = 0; depth < 5; depth++) {
  266. userns_fd = get_userns_fd(0, (depth == 0) ? orig_uid : 0, 1);
  267. if (userns_fd < 0)
  268. exit(1);
  269. if (setns(userns_fd, CLONE_NEWUSER) < 0) {
  270. close(userns_fd);
  271. exit(1);
  272. }
  273. close(userns_fd);
  274. }
  275. exit(0);
  276. }
  277. /* Parent: wait for child */
  278. int status;
  279. waitpid(pid, &status, 0);
  280. ASSERT_TRUE(WIFEXITED(status));
  281. }
  282. /* Verify we're back to baseline */
  283. ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
  284. ASSERT_GE(ret_after, 0);
  285. TH_LOG("After 20 nested namespace hierarchies: %zd active user namespaces", ret_after);
  286. ASSERT_EQ(ret_before, ret_after);
  287. }
  288. /*
  289. * Test listns() pagination under stress.
  290. * Create many namespaces and verify pagination works correctly.
  291. */
  292. TEST(listns_pagination_stress)
  293. {
  294. struct ns_id_req req = {
  295. .size = sizeof(req),
  296. .spare = 0,
  297. .ns_id = 0,
  298. .ns_type = CLONE_NEWUSER,
  299. .spare2 = 0,
  300. .user_ns_id = 0,
  301. };
  302. pid_t pids[30];
  303. int num_children = 30;
  304. int i;
  305. int sv[2];
  306. __u64 all_ns_ids[512];
  307. int total_found = 0;
  308. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
  309. /* Create many children with user namespaces */
  310. for (i = 0; i < num_children; i++) {
  311. pids[i] = fork();
  312. ASSERT_GE(pids[i], 0);
  313. if (pids[i] == 0) {
  314. char c;
  315. close(sv[0]);
  316. if (setup_userns() < 0) {
  317. close(sv[1]);
  318. exit(1);
  319. }
  320. /* Signal parent we're ready */
  321. if (write(sv[1], &c, 1) != 1) {
  322. close(sv[1]);
  323. exit(1);
  324. }
  325. /* Wait for parent signal to exit */
  326. if (read(sv[1], &c, 1) != 1) {
  327. close(sv[1]);
  328. exit(1);
  329. }
  330. close(sv[1]);
  331. exit(0);
  332. }
  333. }
  334. close(sv[1]);
  335. /* Wait for all children to signal ready */
  336. for (i = 0; i < num_children; i++) {
  337. char c;
  338. if (read(sv[0], &c, 1) != 1) {
  339. /* If we fail to read, kill all children and exit */
  340. close(sv[0]);
  341. for (int j = 0; j < num_children; j++)
  342. kill(pids[j], SIGKILL);
  343. for (int j = 0; j < num_children; j++)
  344. waitpid(pids[j], NULL, 0);
  345. ASSERT_TRUE(false);
  346. }
  347. }
  348. /* Paginate through all namespaces using small batch sizes */
  349. req.ns_id = 0;
  350. while (1) {
  351. __u64 batch[5]; /* Small batch size to force pagination */
  352. ssize_t ret;
  353. ret = sys_listns(&req, batch, ARRAY_SIZE(batch), 0);
  354. if (ret < 0) {
  355. if (errno == ENOSYS) {
  356. close(sv[0]);
  357. for (i = 0; i < num_children; i++)
  358. kill(pids[i], SIGKILL);
  359. for (i = 0; i < num_children; i++)
  360. waitpid(pids[i], NULL, 0);
  361. SKIP(return, "listns() not supported");
  362. }
  363. ASSERT_GE(ret, 0);
  364. }
  365. if (ret == 0)
  366. break;
  367. /* Store results */
  368. for (i = 0; i < ret && total_found < 512; i++) {
  369. all_ns_ids[total_found++] = batch[i];
  370. }
  371. /* Update cursor for next batch */
  372. if (ret == ARRAY_SIZE(batch))
  373. req.ns_id = batch[ret - 1];
  374. else
  375. break;
  376. }
  377. TH_LOG("Paginated through %d user namespaces", total_found);
  378. /* Verify no duplicates in pagination */
  379. for (i = 0; i < total_found; i++) {
  380. for (int j = i + 1; j < total_found; j++) {
  381. if (all_ns_ids[i] == all_ns_ids[j]) {
  382. TH_LOG("Found duplicate ns_id: %llu at positions %d and %d",
  383. (unsigned long long)all_ns_ids[i], i, j);
  384. ASSERT_TRUE(false);
  385. }
  386. }
  387. }
  388. /* Signal all children to exit */
  389. for (i = 0; i < num_children; i++) {
  390. char c = 'X';
  391. if (write(sv[0], &c, 1) != 1) {
  392. close(sv[0]);
  393. for (int j = i; j < num_children; j++)
  394. kill(pids[j], SIGKILL);
  395. for (int j = 0; j < num_children; j++)
  396. waitpid(pids[j], NULL, 0);
  397. ASSERT_TRUE(false);
  398. }
  399. }
  400. close(sv[0]);
  401. /* Wait for all children */
  402. for (i = 0; i < num_children; i++) {
  403. int status;
  404. waitpid(pids[i], &status, 0);
  405. }
  406. }
  407. /*
  408. * Test concurrent namespace operations.
  409. * Multiple processes creating, querying, and destroying namespaces concurrently.
  410. */
  411. TEST(concurrent_namespace_operations)
  412. {
  413. struct ns_id_req req = {
  414. .size = sizeof(req),
  415. .spare = 0,
  416. .ns_id = 0,
  417. .ns_type = 0,
  418. .spare2 = 0,
  419. .user_ns_id = 0,
  420. };
  421. __u64 ns_ids_before[512], ns_ids_after[512];
  422. ssize_t ret_before, ret_after;
  423. pid_t pids[20];
  424. int num_workers = 20;
  425. int i;
  426. /* Get baseline */
  427. ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
  428. if (ret_before < 0) {
  429. if (errno == ENOSYS)
  430. SKIP(return, "listns() not supported");
  431. ASSERT_GE(ret_before, 0);
  432. }
  433. TH_LOG("Baseline: %zd active namespaces", ret_before);
  434. /* Create worker processes that do concurrent operations */
  435. for (i = 0; i < num_workers; i++) {
  436. pids[i] = fork();
  437. ASSERT_GE(pids[i], 0);
  438. if (pids[i] == 0) {
  439. /* Each worker: create namespaces, list them, repeat */
  440. int iterations;
  441. for (iterations = 0; iterations < 10; iterations++) {
  442. int userns_fd;
  443. __u64 temp_ns_ids[100];
  444. ssize_t ret;
  445. /* Create a user namespace */
  446. userns_fd = get_userns_fd(0, getuid(), 1);
  447. if (userns_fd < 0)
  448. continue;
  449. /* List namespaces */
  450. ret = sys_listns(&req, temp_ns_ids, ARRAY_SIZE(temp_ns_ids), 0);
  451. (void)ret;
  452. close(userns_fd);
  453. /* Small delay */
  454. usleep(1000);
  455. }
  456. exit(0);
  457. }
  458. }
  459. /* Wait for all workers */
  460. for (i = 0; i < num_workers; i++) {
  461. int status;
  462. waitpid(pids[i], &status, 0);
  463. ASSERT_TRUE(WIFEXITED(status));
  464. ASSERT_EQ(WEXITSTATUS(status), 0);
  465. }
  466. /* Verify we're back to baseline */
  467. ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
  468. ASSERT_GE(ret_after, 0);
  469. TH_LOG("After concurrent operations: %zd active namespaces", ret_after);
  470. ASSERT_EQ(ret_before, ret_after);
  471. }
  472. /*
  473. * Test namespace churn - continuous creation and destruction.
  474. * Simulates high-churn scenarios like container orchestration.
  475. */
  476. TEST(namespace_churn)
  477. {
  478. struct ns_id_req req = {
  479. .size = sizeof(req),
  480. .spare = 0,
  481. .ns_id = 0,
  482. .ns_type = CLONE_NEWUSER | CLONE_NEWNET | CLONE_NEWUTS,
  483. .spare2 = 0,
  484. .user_ns_id = 0,
  485. };
  486. __u64 ns_ids_before[512], ns_ids_after[512];
  487. ssize_t ret_before, ret_after;
  488. int cycle;
  489. /* Get baseline */
  490. ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
  491. if (ret_before < 0) {
  492. if (errno == ENOSYS)
  493. SKIP(return, "listns() not supported");
  494. ASSERT_GE(ret_before, 0);
  495. }
  496. TH_LOG("Baseline: %zd active namespaces", ret_before);
  497. /* Simulate churn: batches of namespaces created and destroyed */
  498. for (cycle = 0; cycle < 10; cycle++) {
  499. pid_t batch_pids[10];
  500. int i;
  501. /* Create batch */
  502. for (i = 0; i < 10; i++) {
  503. batch_pids[i] = fork();
  504. ASSERT_GE(batch_pids[i], 0);
  505. if (batch_pids[i] == 0) {
  506. /* Create multiple namespace types */
  507. if (setup_userns() < 0)
  508. exit(1);
  509. if (unshare(CLONE_NEWNET) < 0)
  510. exit(1);
  511. if (unshare(CLONE_NEWUTS) < 0)
  512. exit(1);
  513. /* Keep namespaces alive briefly */
  514. usleep(10000);
  515. exit(0);
  516. }
  517. }
  518. /* Wait for batch to complete */
  519. for (i = 0; i < 10; i++) {
  520. int status;
  521. waitpid(batch_pids[i], &status, 0);
  522. }
  523. }
  524. /* Verify we're back to baseline */
  525. ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
  526. ASSERT_GE(ret_after, 0);
  527. TH_LOG("After 10 churn cycles (100 namespace sets): %zd active namespaces", ret_after);
  528. ASSERT_EQ(ret_before, ret_after);
  529. }
  530. TEST_HARNESS_MAIN