param_test.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. // SPDX-License-Identifier: LGPL-2.1
  2. #define _GNU_SOURCE
  3. #include <assert.h>
  4. #include <linux/membarrier.h>
  5. #include <pthread.h>
  6. #include <sched.h>
  7. #include <stdatomic.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <syscall.h>
  13. #include <unistd.h>
  14. #include <poll.h>
  15. #include <sys/types.h>
  16. #include <signal.h>
  17. #include <errno.h>
  18. #include <stddef.h>
  19. #include <stdbool.h>
  20. static inline pid_t rseq_gettid(void)
  21. {
  22. return syscall(__NR_gettid);
  23. }
  24. #define NR_INJECT 9
  25. static int loop_cnt[NR_INJECT + 1];
  26. static int loop_cnt_1 asm("asm_loop_cnt_1") __attribute__((used));
  27. static int loop_cnt_2 asm("asm_loop_cnt_2") __attribute__((used));
  28. static int loop_cnt_3 asm("asm_loop_cnt_3") __attribute__((used));
  29. static int loop_cnt_4 asm("asm_loop_cnt_4") __attribute__((used));
  30. static int loop_cnt_5 asm("asm_loop_cnt_5") __attribute__((used));
  31. static int loop_cnt_6 asm("asm_loop_cnt_6") __attribute__((used));
  32. static int opt_modulo, verbose;
  33. static int opt_yield, opt_signal, opt_sleep,
  34. opt_disable_rseq, opt_threads = 200,
  35. opt_disable_mod = 0, opt_test = 's';
  36. static long long opt_reps = 5000;
  37. static __thread __attribute__((tls_model("initial-exec")))
  38. unsigned int signals_delivered;
  39. #ifndef BENCHMARK
  40. static __thread __attribute__((tls_model("initial-exec"), unused))
  41. unsigned int yield_mod_cnt, nr_abort;
  42. #define printf_verbose(fmt, ...) \
  43. do { \
  44. if (verbose) \
  45. printf(fmt, ## __VA_ARGS__); \
  46. } while (0)
  47. #ifdef __i386__
  48. #define INJECT_ASM_REG "eax"
  49. #define RSEQ_INJECT_CLOBBER \
  50. , INJECT_ASM_REG
  51. #define RSEQ_INJECT_ASM(n) \
  52. "mov asm_loop_cnt_" #n ", %%" INJECT_ASM_REG "\n\t" \
  53. "test %%" INJECT_ASM_REG ",%%" INJECT_ASM_REG "\n\t" \
  54. "jz 333f\n\t" \
  55. "222:\n\t" \
  56. "dec %%" INJECT_ASM_REG "\n\t" \
  57. "jnz 222b\n\t" \
  58. "333:\n\t"
  59. #elif defined(__x86_64__)
  60. #define INJECT_ASM_REG_P "rax"
  61. #define INJECT_ASM_REG "eax"
  62. #define RSEQ_INJECT_CLOBBER \
  63. , INJECT_ASM_REG_P \
  64. , INJECT_ASM_REG
  65. #define RSEQ_INJECT_ASM(n) \
  66. "lea asm_loop_cnt_" #n "(%%rip), %%" INJECT_ASM_REG_P "\n\t" \
  67. "mov (%%" INJECT_ASM_REG_P "), %%" INJECT_ASM_REG "\n\t" \
  68. "test %%" INJECT_ASM_REG ",%%" INJECT_ASM_REG "\n\t" \
  69. "jz 333f\n\t" \
  70. "222:\n\t" \
  71. "dec %%" INJECT_ASM_REG "\n\t" \
  72. "jnz 222b\n\t" \
  73. "333:\n\t"
  74. #elif defined(__s390__)
  75. #define RSEQ_INJECT_INPUT \
  76. , [loop_cnt_1]"m"(loop_cnt[1]) \
  77. , [loop_cnt_2]"m"(loop_cnt[2]) \
  78. , [loop_cnt_3]"m"(loop_cnt[3]) \
  79. , [loop_cnt_4]"m"(loop_cnt[4]) \
  80. , [loop_cnt_5]"m"(loop_cnt[5]) \
  81. , [loop_cnt_6]"m"(loop_cnt[6])
  82. #define INJECT_ASM_REG "r12"
  83. #define RSEQ_INJECT_CLOBBER \
  84. , INJECT_ASM_REG
  85. #define RSEQ_INJECT_ASM(n) \
  86. "l %%" INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
  87. "ltr %%" INJECT_ASM_REG ", %%" INJECT_ASM_REG "\n\t" \
  88. "je 333f\n\t" \
  89. "222:\n\t" \
  90. "ahi %%" INJECT_ASM_REG ", -1\n\t" \
  91. "jnz 222b\n\t" \
  92. "333:\n\t"
  93. #elif defined(__ARMEL__)
  94. #define RSEQ_INJECT_INPUT \
  95. , [loop_cnt_1]"m"(loop_cnt[1]) \
  96. , [loop_cnt_2]"m"(loop_cnt[2]) \
  97. , [loop_cnt_3]"m"(loop_cnt[3]) \
  98. , [loop_cnt_4]"m"(loop_cnt[4]) \
  99. , [loop_cnt_5]"m"(loop_cnt[5]) \
  100. , [loop_cnt_6]"m"(loop_cnt[6])
  101. #define INJECT_ASM_REG "r4"
  102. #define RSEQ_INJECT_CLOBBER \
  103. , INJECT_ASM_REG
  104. #define RSEQ_INJECT_ASM(n) \
  105. "ldr " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
  106. "cmp " INJECT_ASM_REG ", #0\n\t" \
  107. "beq 333f\n\t" \
  108. "222:\n\t" \
  109. "subs " INJECT_ASM_REG ", #1\n\t" \
  110. "bne 222b\n\t" \
  111. "333:\n\t"
  112. #elif defined(__AARCH64EL__)
  113. #define RSEQ_INJECT_INPUT \
  114. , [loop_cnt_1] "Qo" (loop_cnt[1]) \
  115. , [loop_cnt_2] "Qo" (loop_cnt[2]) \
  116. , [loop_cnt_3] "Qo" (loop_cnt[3]) \
  117. , [loop_cnt_4] "Qo" (loop_cnt[4]) \
  118. , [loop_cnt_5] "Qo" (loop_cnt[5]) \
  119. , [loop_cnt_6] "Qo" (loop_cnt[6])
  120. #define INJECT_ASM_REG RSEQ_ASM_TMP_REG32
  121. #define RSEQ_INJECT_ASM(n) \
  122. " ldr " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n" \
  123. " cbz " INJECT_ASM_REG ", 333f\n" \
  124. "222:\n" \
  125. " sub " INJECT_ASM_REG ", " INJECT_ASM_REG ", #1\n" \
  126. " cbnz " INJECT_ASM_REG ", 222b\n" \
  127. "333:\n"
  128. #elif defined(__PPC__)
  129. #define RSEQ_INJECT_INPUT \
  130. , [loop_cnt_1]"m"(loop_cnt[1]) \
  131. , [loop_cnt_2]"m"(loop_cnt[2]) \
  132. , [loop_cnt_3]"m"(loop_cnt[3]) \
  133. , [loop_cnt_4]"m"(loop_cnt[4]) \
  134. , [loop_cnt_5]"m"(loop_cnt[5]) \
  135. , [loop_cnt_6]"m"(loop_cnt[6])
  136. #define INJECT_ASM_REG "r18"
  137. #define RSEQ_INJECT_CLOBBER \
  138. , INJECT_ASM_REG
  139. #define RSEQ_INJECT_ASM(n) \
  140. "lwz %%" INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
  141. "cmpwi %%" INJECT_ASM_REG ", 0\n\t" \
  142. "beq 333f\n\t" \
  143. "222:\n\t" \
  144. "subic. %%" INJECT_ASM_REG ", %%" INJECT_ASM_REG ", 1\n\t" \
  145. "bne 222b\n\t" \
  146. "333:\n\t"
  147. #elif defined(__mips__)
  148. #define RSEQ_INJECT_INPUT \
  149. , [loop_cnt_1]"m"(loop_cnt[1]) \
  150. , [loop_cnt_2]"m"(loop_cnt[2]) \
  151. , [loop_cnt_3]"m"(loop_cnt[3]) \
  152. , [loop_cnt_4]"m"(loop_cnt[4]) \
  153. , [loop_cnt_5]"m"(loop_cnt[5]) \
  154. , [loop_cnt_6]"m"(loop_cnt[6])
  155. #define INJECT_ASM_REG "$5"
  156. #define RSEQ_INJECT_CLOBBER \
  157. , INJECT_ASM_REG
  158. #define RSEQ_INJECT_ASM(n) \
  159. "lw " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
  160. "beqz " INJECT_ASM_REG ", 333f\n\t" \
  161. "222:\n\t" \
  162. "addiu " INJECT_ASM_REG ", -1\n\t" \
  163. "bnez " INJECT_ASM_REG ", 222b\n\t" \
  164. "333:\n\t"
  165. #elif defined(__riscv)
  166. #define RSEQ_INJECT_INPUT \
  167. , [loop_cnt_1]"m"(loop_cnt[1]) \
  168. , [loop_cnt_2]"m"(loop_cnt[2]) \
  169. , [loop_cnt_3]"m"(loop_cnt[3]) \
  170. , [loop_cnt_4]"m"(loop_cnt[4]) \
  171. , [loop_cnt_5]"m"(loop_cnt[5]) \
  172. , [loop_cnt_6]"m"(loop_cnt[6])
  173. #define INJECT_ASM_REG "t1"
  174. #define RSEQ_INJECT_CLOBBER \
  175. , INJECT_ASM_REG
  176. #define RSEQ_INJECT_ASM(n) \
  177. "lw " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
  178. "beqz " INJECT_ASM_REG ", 333f\n\t" \
  179. "222:\n\t" \
  180. "addi " INJECT_ASM_REG "," INJECT_ASM_REG ", -1\n\t" \
  181. "bnez " INJECT_ASM_REG ", 222b\n\t" \
  182. "333:\n\t"
  183. #else
  184. #error unsupported target
  185. #endif
  186. #define RSEQ_INJECT_FAILED \
  187. nr_abort++;
  188. #define RSEQ_INJECT_C(n) \
  189. { \
  190. int loc_i, loc_nr_loops = loop_cnt[n]; \
  191. \
  192. for (loc_i = 0; loc_i < loc_nr_loops; loc_i++) { \
  193. rseq_barrier(); \
  194. } \
  195. if (loc_nr_loops == -1 && opt_modulo) { \
  196. if (yield_mod_cnt == opt_modulo - 1) { \
  197. if (opt_sleep > 0) \
  198. poll(NULL, 0, opt_sleep); \
  199. if (opt_yield) \
  200. sched_yield(); \
  201. if (opt_signal) \
  202. raise(SIGUSR1); \
  203. yield_mod_cnt = 0; \
  204. } else { \
  205. yield_mod_cnt++; \
  206. } \
  207. } \
  208. }
  209. #else
  210. #define printf_verbose(fmt, ...)
  211. #endif /* BENCHMARK */
  212. #include "rseq.h"
  213. static enum rseq_mo opt_mo = RSEQ_MO_RELAXED;
  214. #ifdef RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
  215. #define TEST_MEMBARRIER
  216. static int sys_membarrier(int cmd, int flags, int cpu_id)
  217. {
  218. return syscall(__NR_membarrier, cmd, flags, cpu_id);
  219. }
  220. #endif
  221. #ifdef BUILDOPT_RSEQ_PERCPU_MM_CID
  222. # define RSEQ_PERCPU RSEQ_PERCPU_MM_CID
  223. static
  224. int get_current_cpu_id(void)
  225. {
  226. return rseq_current_mm_cid();
  227. }
  228. static
  229. bool rseq_validate_cpu_id(void)
  230. {
  231. return rseq_mm_cid_available();
  232. }
  233. static
  234. bool rseq_use_cpu_index(void)
  235. {
  236. return false; /* Use mm_cid */
  237. }
  238. # ifdef TEST_MEMBARRIER
  239. /*
  240. * Membarrier does not currently support targeting a mm_cid, so
  241. * issue the barrier on all cpus.
  242. */
  243. static
  244. int rseq_membarrier_expedited(int cpu)
  245. {
  246. return sys_membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ,
  247. 0, 0);
  248. }
  249. # endif /* TEST_MEMBARRIER */
  250. #else
  251. # define RSEQ_PERCPU RSEQ_PERCPU_CPU_ID
  252. static
  253. int get_current_cpu_id(void)
  254. {
  255. return rseq_cpu_start();
  256. }
  257. static
  258. bool rseq_validate_cpu_id(void)
  259. {
  260. return rseq_current_cpu_raw() >= 0;
  261. }
  262. static
  263. bool rseq_use_cpu_index(void)
  264. {
  265. return true; /* Use cpu_id as index. */
  266. }
  267. # ifdef TEST_MEMBARRIER
  268. static
  269. int rseq_membarrier_expedited(int cpu)
  270. {
  271. return sys_membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ,
  272. MEMBARRIER_CMD_FLAG_CPU, cpu);
  273. }
  274. # endif /* TEST_MEMBARRIER */
  275. #endif
  276. struct percpu_lock_entry {
  277. intptr_t v;
  278. } __attribute__((aligned(128)));
  279. struct percpu_lock {
  280. struct percpu_lock_entry c[CPU_SETSIZE];
  281. };
  282. struct test_data_entry {
  283. intptr_t count;
  284. } __attribute__((aligned(128)));
  285. struct spinlock_test_data {
  286. struct percpu_lock lock;
  287. struct test_data_entry c[CPU_SETSIZE];
  288. };
  289. struct spinlock_thread_test_data {
  290. struct spinlock_test_data *data;
  291. long long reps;
  292. int reg;
  293. };
  294. struct inc_test_data {
  295. struct test_data_entry c[CPU_SETSIZE];
  296. };
  297. struct inc_thread_test_data {
  298. struct inc_test_data *data;
  299. long long reps;
  300. int reg;
  301. };
  302. struct percpu_list_node {
  303. intptr_t data;
  304. struct percpu_list_node *next;
  305. };
  306. struct percpu_list_entry {
  307. struct percpu_list_node *head;
  308. } __attribute__((aligned(128)));
  309. struct percpu_list {
  310. struct percpu_list_entry c[CPU_SETSIZE];
  311. };
  312. #define BUFFER_ITEM_PER_CPU 100
  313. struct percpu_buffer_node {
  314. intptr_t data;
  315. };
  316. struct percpu_buffer_entry {
  317. intptr_t offset;
  318. intptr_t buflen;
  319. struct percpu_buffer_node **array;
  320. } __attribute__((aligned(128)));
  321. struct percpu_buffer {
  322. struct percpu_buffer_entry c[CPU_SETSIZE];
  323. };
  324. #define MEMCPY_BUFFER_ITEM_PER_CPU 100
  325. struct percpu_memcpy_buffer_node {
  326. intptr_t data1;
  327. uint64_t data2;
  328. };
  329. struct percpu_memcpy_buffer_entry {
  330. intptr_t offset;
  331. intptr_t buflen;
  332. struct percpu_memcpy_buffer_node *array;
  333. } __attribute__((aligned(128)));
  334. struct percpu_memcpy_buffer {
  335. struct percpu_memcpy_buffer_entry c[CPU_SETSIZE];
  336. };
  337. /* A simple percpu spinlock. Grabs lock on current cpu. */
  338. static int rseq_this_cpu_lock(struct percpu_lock *lock)
  339. {
  340. int cpu;
  341. for (;;) {
  342. int ret;
  343. cpu = get_current_cpu_id();
  344. if (cpu < 0) {
  345. fprintf(stderr, "pid: %d: tid: %d, cpu: %d: cid: %d\n",
  346. getpid(), (int) rseq_gettid(), rseq_current_cpu_raw(), cpu);
  347. abort();
  348. }
  349. ret = rseq_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
  350. &lock->c[cpu].v,
  351. 0, 1, cpu);
  352. if (rseq_likely(!ret))
  353. break;
  354. /* Retry if comparison fails or rseq aborts. */
  355. }
  356. /*
  357. * Acquire semantic when taking lock after control dependency.
  358. * Matches rseq_smp_store_release().
  359. */
  360. rseq_smp_acquire__after_ctrl_dep();
  361. return cpu;
  362. }
  363. static void rseq_percpu_unlock(struct percpu_lock *lock, int cpu)
  364. {
  365. assert(lock->c[cpu].v == 1);
  366. /*
  367. * Release lock, with release semantic. Matches
  368. * rseq_smp_acquire__after_ctrl_dep().
  369. */
  370. rseq_smp_store_release(&lock->c[cpu].v, 0);
  371. }
  372. void *test_percpu_spinlock_thread(void *arg)
  373. {
  374. struct spinlock_thread_test_data *thread_data = arg;
  375. struct spinlock_test_data *data = thread_data->data;
  376. long long i, reps;
  377. if (!opt_disable_rseq && thread_data->reg &&
  378. rseq_register_current_thread())
  379. abort();
  380. reps = thread_data->reps;
  381. for (i = 0; i < reps; i++) {
  382. int cpu = rseq_this_cpu_lock(&data->lock);
  383. data->c[cpu].count++;
  384. rseq_percpu_unlock(&data->lock, cpu);
  385. #ifndef BENCHMARK
  386. if (i != 0 && !(i % (reps / 10)))
  387. printf_verbose("tid %d: count %lld\n",
  388. (int) rseq_gettid(), i);
  389. #endif
  390. }
  391. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  392. (int) rseq_gettid(), nr_abort, signals_delivered);
  393. if (!opt_disable_rseq && thread_data->reg &&
  394. rseq_unregister_current_thread())
  395. abort();
  396. return NULL;
  397. }
  398. /*
  399. * A simple test which implements a sharded counter using a per-cpu
  400. * lock. Obviously real applications might prefer to simply use a
  401. * per-cpu increment; however, this is reasonable for a test and the
  402. * lock can be extended to synchronize more complicated operations.
  403. */
  404. void test_percpu_spinlock(void)
  405. {
  406. const int num_threads = opt_threads;
  407. int i, ret;
  408. uint64_t sum;
  409. pthread_t test_threads[num_threads];
  410. struct spinlock_test_data data;
  411. struct spinlock_thread_test_data thread_data[num_threads];
  412. memset(&data, 0, sizeof(data));
  413. for (i = 0; i < num_threads; i++) {
  414. thread_data[i].reps = opt_reps;
  415. if (opt_disable_mod <= 0 || (i % opt_disable_mod))
  416. thread_data[i].reg = 1;
  417. else
  418. thread_data[i].reg = 0;
  419. thread_data[i].data = &data;
  420. ret = pthread_create(&test_threads[i], NULL,
  421. test_percpu_spinlock_thread,
  422. &thread_data[i]);
  423. if (ret) {
  424. errno = ret;
  425. perror("pthread_create");
  426. abort();
  427. }
  428. }
  429. for (i = 0; i < num_threads; i++) {
  430. ret = pthread_join(test_threads[i], NULL);
  431. if (ret) {
  432. errno = ret;
  433. perror("pthread_join");
  434. abort();
  435. }
  436. }
  437. sum = 0;
  438. for (i = 0; i < CPU_SETSIZE; i++)
  439. sum += data.c[i].count;
  440. assert(sum == (uint64_t)opt_reps * num_threads);
  441. }
  442. void *test_percpu_inc_thread(void *arg)
  443. {
  444. struct inc_thread_test_data *thread_data = arg;
  445. struct inc_test_data *data = thread_data->data;
  446. long long i, reps;
  447. if (!opt_disable_rseq && thread_data->reg &&
  448. rseq_register_current_thread())
  449. abort();
  450. reps = thread_data->reps;
  451. for (i = 0; i < reps; i++) {
  452. int ret;
  453. do {
  454. int cpu;
  455. cpu = get_current_cpu_id();
  456. ret = rseq_addv(RSEQ_MO_RELAXED, RSEQ_PERCPU,
  457. &data->c[cpu].count, 1, cpu);
  458. } while (rseq_unlikely(ret));
  459. #ifndef BENCHMARK
  460. if (i != 0 && !(i % (reps / 10)))
  461. printf_verbose("tid %d: count %lld\n",
  462. (int) rseq_gettid(), i);
  463. #endif
  464. }
  465. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  466. (int) rseq_gettid(), nr_abort, signals_delivered);
  467. if (!opt_disable_rseq && thread_data->reg &&
  468. rseq_unregister_current_thread())
  469. abort();
  470. return NULL;
  471. }
  472. void test_percpu_inc(void)
  473. {
  474. const int num_threads = opt_threads;
  475. int i, ret;
  476. uint64_t sum;
  477. pthread_t test_threads[num_threads];
  478. struct inc_test_data data;
  479. struct inc_thread_test_data thread_data[num_threads];
  480. memset(&data, 0, sizeof(data));
  481. for (i = 0; i < num_threads; i++) {
  482. thread_data[i].reps = opt_reps;
  483. if (opt_disable_mod <= 0 || (i % opt_disable_mod))
  484. thread_data[i].reg = 1;
  485. else
  486. thread_data[i].reg = 0;
  487. thread_data[i].data = &data;
  488. ret = pthread_create(&test_threads[i], NULL,
  489. test_percpu_inc_thread,
  490. &thread_data[i]);
  491. if (ret) {
  492. errno = ret;
  493. perror("pthread_create");
  494. abort();
  495. }
  496. }
  497. for (i = 0; i < num_threads; i++) {
  498. ret = pthread_join(test_threads[i], NULL);
  499. if (ret) {
  500. errno = ret;
  501. perror("pthread_join");
  502. abort();
  503. }
  504. }
  505. sum = 0;
  506. for (i = 0; i < CPU_SETSIZE; i++)
  507. sum += data.c[i].count;
  508. assert(sum == (uint64_t)opt_reps * num_threads);
  509. }
  510. void this_cpu_list_push(struct percpu_list *list,
  511. struct percpu_list_node *node,
  512. int *_cpu)
  513. {
  514. int cpu;
  515. for (;;) {
  516. intptr_t *targetptr, newval, expect;
  517. int ret;
  518. cpu = get_current_cpu_id();
  519. /* Load list->c[cpu].head with single-copy atomicity. */
  520. expect = (intptr_t)RSEQ_READ_ONCE(list->c[cpu].head);
  521. newval = (intptr_t)node;
  522. targetptr = (intptr_t *)&list->c[cpu].head;
  523. node->next = (struct percpu_list_node *)expect;
  524. ret = rseq_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
  525. targetptr, expect, newval, cpu);
  526. if (rseq_likely(!ret))
  527. break;
  528. /* Retry if comparison fails or rseq aborts. */
  529. }
  530. if (_cpu)
  531. *_cpu = cpu;
  532. }
  533. /*
  534. * Unlike a traditional lock-less linked list; the availability of a
  535. * rseq primitive allows us to implement pop without concerns over
  536. * ABA-type races.
  537. */
  538. struct percpu_list_node *this_cpu_list_pop(struct percpu_list *list,
  539. int *_cpu)
  540. {
  541. struct percpu_list_node *node = NULL;
  542. int cpu;
  543. for (;;) {
  544. struct percpu_list_node *head;
  545. intptr_t *targetptr, expectnot, *load;
  546. long offset;
  547. int ret;
  548. cpu = get_current_cpu_id();
  549. targetptr = (intptr_t *)&list->c[cpu].head;
  550. expectnot = (intptr_t)NULL;
  551. offset = offsetof(struct percpu_list_node, next);
  552. load = (intptr_t *)&head;
  553. ret = rseq_cmpnev_storeoffp_load(RSEQ_MO_RELAXED, RSEQ_PERCPU,
  554. targetptr, expectnot,
  555. offset, load, cpu);
  556. if (rseq_likely(!ret)) {
  557. node = head;
  558. break;
  559. }
  560. if (ret > 0)
  561. break;
  562. /* Retry if rseq aborts. */
  563. }
  564. if (_cpu)
  565. *_cpu = cpu;
  566. return node;
  567. }
  568. /*
  569. * __percpu_list_pop is not safe against concurrent accesses. Should
  570. * only be used on lists that are not concurrently modified.
  571. */
  572. struct percpu_list_node *__percpu_list_pop(struct percpu_list *list, int cpu)
  573. {
  574. struct percpu_list_node *node;
  575. node = list->c[cpu].head;
  576. if (!node)
  577. return NULL;
  578. list->c[cpu].head = node->next;
  579. return node;
  580. }
  581. void *test_percpu_list_thread(void *arg)
  582. {
  583. long long i, reps;
  584. struct percpu_list *list = (struct percpu_list *)arg;
  585. if (!opt_disable_rseq && rseq_register_current_thread())
  586. abort();
  587. reps = opt_reps;
  588. for (i = 0; i < reps; i++) {
  589. struct percpu_list_node *node;
  590. node = this_cpu_list_pop(list, NULL);
  591. if (opt_yield)
  592. sched_yield(); /* encourage shuffling */
  593. if (node)
  594. this_cpu_list_push(list, node, NULL);
  595. }
  596. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  597. (int) rseq_gettid(), nr_abort, signals_delivered);
  598. if (!opt_disable_rseq && rseq_unregister_current_thread())
  599. abort();
  600. return NULL;
  601. }
  602. /* Simultaneous modification to a per-cpu linked list from many threads. */
  603. void test_percpu_list(void)
  604. {
  605. const int num_threads = opt_threads;
  606. int i, j, ret;
  607. uint64_t sum = 0, expected_sum = 0;
  608. struct percpu_list list;
  609. pthread_t test_threads[num_threads];
  610. cpu_set_t allowed_cpus;
  611. memset(&list, 0, sizeof(list));
  612. /* Generate list entries for every usable cpu. */
  613. sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
  614. for (i = 0; i < CPU_SETSIZE; i++) {
  615. if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus))
  616. continue;
  617. for (j = 1; j <= 100; j++) {
  618. struct percpu_list_node *node;
  619. expected_sum += j;
  620. node = malloc(sizeof(*node));
  621. assert(node);
  622. node->data = j;
  623. node->next = list.c[i].head;
  624. list.c[i].head = node;
  625. }
  626. }
  627. for (i = 0; i < num_threads; i++) {
  628. ret = pthread_create(&test_threads[i], NULL,
  629. test_percpu_list_thread, &list);
  630. if (ret) {
  631. errno = ret;
  632. perror("pthread_create");
  633. abort();
  634. }
  635. }
  636. for (i = 0; i < num_threads; i++) {
  637. ret = pthread_join(test_threads[i], NULL);
  638. if (ret) {
  639. errno = ret;
  640. perror("pthread_join");
  641. abort();
  642. }
  643. }
  644. for (i = 0; i < CPU_SETSIZE; i++) {
  645. struct percpu_list_node *node;
  646. if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus))
  647. continue;
  648. while ((node = __percpu_list_pop(&list, i))) {
  649. sum += node->data;
  650. free(node);
  651. }
  652. }
  653. /*
  654. * All entries should now be accounted for (unless some external
  655. * actor is interfering with our allowed affinity while this
  656. * test is running).
  657. */
  658. assert(sum == expected_sum);
  659. }
  660. bool this_cpu_buffer_push(struct percpu_buffer *buffer,
  661. struct percpu_buffer_node *node,
  662. int *_cpu)
  663. {
  664. bool result = false;
  665. int cpu;
  666. for (;;) {
  667. intptr_t *targetptr_spec, newval_spec;
  668. intptr_t *targetptr_final, newval_final;
  669. intptr_t offset;
  670. int ret;
  671. cpu = get_current_cpu_id();
  672. offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
  673. if (offset == buffer->c[cpu].buflen)
  674. break;
  675. newval_spec = (intptr_t)node;
  676. targetptr_spec = (intptr_t *)&buffer->c[cpu].array[offset];
  677. newval_final = offset + 1;
  678. targetptr_final = &buffer->c[cpu].offset;
  679. ret = rseq_cmpeqv_trystorev_storev(opt_mo, RSEQ_PERCPU,
  680. targetptr_final, offset, targetptr_spec,
  681. newval_spec, newval_final, cpu);
  682. if (rseq_likely(!ret)) {
  683. result = true;
  684. break;
  685. }
  686. /* Retry if comparison fails or rseq aborts. */
  687. }
  688. if (_cpu)
  689. *_cpu = cpu;
  690. return result;
  691. }
  692. struct percpu_buffer_node *this_cpu_buffer_pop(struct percpu_buffer *buffer,
  693. int *_cpu)
  694. {
  695. struct percpu_buffer_node *head;
  696. int cpu;
  697. for (;;) {
  698. intptr_t *targetptr, newval;
  699. intptr_t offset;
  700. int ret;
  701. cpu = get_current_cpu_id();
  702. /* Load offset with single-copy atomicity. */
  703. offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
  704. if (offset == 0) {
  705. head = NULL;
  706. break;
  707. }
  708. head = RSEQ_READ_ONCE(buffer->c[cpu].array[offset - 1]);
  709. newval = offset - 1;
  710. targetptr = (intptr_t *)&buffer->c[cpu].offset;
  711. ret = rseq_cmpeqv_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
  712. targetptr, offset,
  713. (intptr_t *)&buffer->c[cpu].array[offset - 1],
  714. (intptr_t)head, newval, cpu);
  715. if (rseq_likely(!ret))
  716. break;
  717. /* Retry if comparison fails or rseq aborts. */
  718. }
  719. if (_cpu)
  720. *_cpu = cpu;
  721. return head;
  722. }
  723. /*
  724. * __percpu_buffer_pop is not safe against concurrent accesses. Should
  725. * only be used on buffers that are not concurrently modified.
  726. */
  727. struct percpu_buffer_node *__percpu_buffer_pop(struct percpu_buffer *buffer,
  728. int cpu)
  729. {
  730. struct percpu_buffer_node *head;
  731. intptr_t offset;
  732. offset = buffer->c[cpu].offset;
  733. if (offset == 0)
  734. return NULL;
  735. head = buffer->c[cpu].array[offset - 1];
  736. buffer->c[cpu].offset = offset - 1;
  737. return head;
  738. }
  739. void *test_percpu_buffer_thread(void *arg)
  740. {
  741. long long i, reps;
  742. struct percpu_buffer *buffer = (struct percpu_buffer *)arg;
  743. if (!opt_disable_rseq && rseq_register_current_thread())
  744. abort();
  745. reps = opt_reps;
  746. for (i = 0; i < reps; i++) {
  747. struct percpu_buffer_node *node;
  748. node = this_cpu_buffer_pop(buffer, NULL);
  749. if (opt_yield)
  750. sched_yield(); /* encourage shuffling */
  751. if (node) {
  752. if (!this_cpu_buffer_push(buffer, node, NULL)) {
  753. /* Should increase buffer size. */
  754. abort();
  755. }
  756. }
  757. }
  758. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  759. (int) rseq_gettid(), nr_abort, signals_delivered);
  760. if (!opt_disable_rseq && rseq_unregister_current_thread())
  761. abort();
  762. return NULL;
  763. }
  764. /* Simultaneous modification to a per-cpu buffer from many threads. */
  765. void test_percpu_buffer(void)
  766. {
  767. const int num_threads = opt_threads;
  768. int i, j, ret;
  769. uint64_t sum = 0, expected_sum = 0;
  770. struct percpu_buffer buffer;
  771. pthread_t test_threads[num_threads];
  772. cpu_set_t allowed_cpus;
  773. memset(&buffer, 0, sizeof(buffer));
  774. /* Generate list entries for every usable cpu. */
  775. sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
  776. for (i = 0; i < CPU_SETSIZE; i++) {
  777. if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus))
  778. continue;
  779. /* Worse-case is every item in same CPU. */
  780. buffer.c[i].array =
  781. malloc(sizeof(*buffer.c[i].array) * CPU_SETSIZE *
  782. BUFFER_ITEM_PER_CPU);
  783. assert(buffer.c[i].array);
  784. buffer.c[i].buflen = CPU_SETSIZE * BUFFER_ITEM_PER_CPU;
  785. for (j = 1; j <= BUFFER_ITEM_PER_CPU; j++) {
  786. struct percpu_buffer_node *node;
  787. expected_sum += j;
  788. /*
  789. * We could theoretically put the word-sized
  790. * "data" directly in the buffer. However, we
  791. * want to model objects that would not fit
  792. * within a single word, so allocate an object
  793. * for each node.
  794. */
  795. node = malloc(sizeof(*node));
  796. assert(node);
  797. node->data = j;
  798. buffer.c[i].array[j - 1] = node;
  799. buffer.c[i].offset++;
  800. }
  801. }
  802. for (i = 0; i < num_threads; i++) {
  803. ret = pthread_create(&test_threads[i], NULL,
  804. test_percpu_buffer_thread, &buffer);
  805. if (ret) {
  806. errno = ret;
  807. perror("pthread_create");
  808. abort();
  809. }
  810. }
  811. for (i = 0; i < num_threads; i++) {
  812. ret = pthread_join(test_threads[i], NULL);
  813. if (ret) {
  814. errno = ret;
  815. perror("pthread_join");
  816. abort();
  817. }
  818. }
  819. for (i = 0; i < CPU_SETSIZE; i++) {
  820. struct percpu_buffer_node *node;
  821. if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus))
  822. continue;
  823. while ((node = __percpu_buffer_pop(&buffer, i))) {
  824. sum += node->data;
  825. free(node);
  826. }
  827. free(buffer.c[i].array);
  828. }
  829. /*
  830. * All entries should now be accounted for (unless some external
  831. * actor is interfering with our allowed affinity while this
  832. * test is running).
  833. */
  834. assert(sum == expected_sum);
  835. }
  836. bool this_cpu_memcpy_buffer_push(struct percpu_memcpy_buffer *buffer,
  837. struct percpu_memcpy_buffer_node item,
  838. int *_cpu)
  839. {
  840. bool result = false;
  841. int cpu;
  842. for (;;) {
  843. intptr_t *targetptr_final, newval_final, offset;
  844. char *destptr, *srcptr;
  845. size_t copylen;
  846. int ret;
  847. cpu = get_current_cpu_id();
  848. /* Load offset with single-copy atomicity. */
  849. offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
  850. if (offset == buffer->c[cpu].buflen)
  851. break;
  852. destptr = (char *)&buffer->c[cpu].array[offset];
  853. srcptr = (char *)&item;
  854. /* copylen must be <= 4kB. */
  855. copylen = sizeof(item);
  856. newval_final = offset + 1;
  857. targetptr_final = &buffer->c[cpu].offset;
  858. ret = rseq_cmpeqv_trymemcpy_storev(
  859. opt_mo, RSEQ_PERCPU,
  860. targetptr_final, offset,
  861. destptr, srcptr, copylen,
  862. newval_final, cpu);
  863. if (rseq_likely(!ret)) {
  864. result = true;
  865. break;
  866. }
  867. /* Retry if comparison fails or rseq aborts. */
  868. }
  869. if (_cpu)
  870. *_cpu = cpu;
  871. return result;
  872. }
  873. bool this_cpu_memcpy_buffer_pop(struct percpu_memcpy_buffer *buffer,
  874. struct percpu_memcpy_buffer_node *item,
  875. int *_cpu)
  876. {
  877. bool result = false;
  878. int cpu;
  879. for (;;) {
  880. intptr_t *targetptr_final, newval_final, offset;
  881. char *destptr, *srcptr;
  882. size_t copylen;
  883. int ret;
  884. cpu = get_current_cpu_id();
  885. /* Load offset with single-copy atomicity. */
  886. offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
  887. if (offset == 0)
  888. break;
  889. destptr = (char *)item;
  890. srcptr = (char *)&buffer->c[cpu].array[offset - 1];
  891. /* copylen must be <= 4kB. */
  892. copylen = sizeof(*item);
  893. newval_final = offset - 1;
  894. targetptr_final = &buffer->c[cpu].offset;
  895. ret = rseq_cmpeqv_trymemcpy_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
  896. targetptr_final, offset, destptr, srcptr, copylen,
  897. newval_final, cpu);
  898. if (rseq_likely(!ret)) {
  899. result = true;
  900. break;
  901. }
  902. /* Retry if comparison fails or rseq aborts. */
  903. }
  904. if (_cpu)
  905. *_cpu = cpu;
  906. return result;
  907. }
  908. /*
  909. * __percpu_memcpy_buffer_pop is not safe against concurrent accesses. Should
  910. * only be used on buffers that are not concurrently modified.
  911. */
  912. bool __percpu_memcpy_buffer_pop(struct percpu_memcpy_buffer *buffer,
  913. struct percpu_memcpy_buffer_node *item,
  914. int cpu)
  915. {
  916. intptr_t offset;
  917. offset = buffer->c[cpu].offset;
  918. if (offset == 0)
  919. return false;
  920. memcpy(item, &buffer->c[cpu].array[offset - 1], sizeof(*item));
  921. buffer->c[cpu].offset = offset - 1;
  922. return true;
  923. }
  924. void *test_percpu_memcpy_buffer_thread(void *arg)
  925. {
  926. long long i, reps;
  927. struct percpu_memcpy_buffer *buffer = (struct percpu_memcpy_buffer *)arg;
  928. if (!opt_disable_rseq && rseq_register_current_thread())
  929. abort();
  930. reps = opt_reps;
  931. for (i = 0; i < reps; i++) {
  932. struct percpu_memcpy_buffer_node item;
  933. bool result;
  934. result = this_cpu_memcpy_buffer_pop(buffer, &item, NULL);
  935. if (opt_yield)
  936. sched_yield(); /* encourage shuffling */
  937. if (result) {
  938. if (!this_cpu_memcpy_buffer_push(buffer, item, NULL)) {
  939. /* Should increase buffer size. */
  940. abort();
  941. }
  942. }
  943. }
  944. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  945. (int) rseq_gettid(), nr_abort, signals_delivered);
  946. if (!opt_disable_rseq && rseq_unregister_current_thread())
  947. abort();
  948. return NULL;
  949. }
  950. /* Simultaneous modification to a per-cpu buffer from many threads. */
  951. void test_percpu_memcpy_buffer(void)
  952. {
  953. const int num_threads = opt_threads;
  954. int i, j, ret;
  955. uint64_t sum = 0, expected_sum = 0;
  956. struct percpu_memcpy_buffer buffer;
  957. pthread_t test_threads[num_threads];
  958. cpu_set_t allowed_cpus;
  959. memset(&buffer, 0, sizeof(buffer));
  960. /* Generate list entries for every usable cpu. */
  961. sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
  962. for (i = 0; i < CPU_SETSIZE; i++) {
  963. if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus))
  964. continue;
  965. /* Worse-case is every item in same CPU. */
  966. buffer.c[i].array =
  967. malloc(sizeof(*buffer.c[i].array) * CPU_SETSIZE *
  968. MEMCPY_BUFFER_ITEM_PER_CPU);
  969. assert(buffer.c[i].array);
  970. buffer.c[i].buflen = CPU_SETSIZE * MEMCPY_BUFFER_ITEM_PER_CPU;
  971. for (j = 1; j <= MEMCPY_BUFFER_ITEM_PER_CPU; j++) {
  972. expected_sum += 2 * j + 1;
  973. /*
  974. * We could theoretically put the word-sized
  975. * "data" directly in the buffer. However, we
  976. * want to model objects that would not fit
  977. * within a single word, so allocate an object
  978. * for each node.
  979. */
  980. buffer.c[i].array[j - 1].data1 = j;
  981. buffer.c[i].array[j - 1].data2 = j + 1;
  982. buffer.c[i].offset++;
  983. }
  984. }
  985. for (i = 0; i < num_threads; i++) {
  986. ret = pthread_create(&test_threads[i], NULL,
  987. test_percpu_memcpy_buffer_thread,
  988. &buffer);
  989. if (ret) {
  990. errno = ret;
  991. perror("pthread_create");
  992. abort();
  993. }
  994. }
  995. for (i = 0; i < num_threads; i++) {
  996. ret = pthread_join(test_threads[i], NULL);
  997. if (ret) {
  998. errno = ret;
  999. perror("pthread_join");
  1000. abort();
  1001. }
  1002. }
  1003. for (i = 0; i < CPU_SETSIZE; i++) {
  1004. struct percpu_memcpy_buffer_node item;
  1005. if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus))
  1006. continue;
  1007. while (__percpu_memcpy_buffer_pop(&buffer, &item, i)) {
  1008. sum += item.data1;
  1009. sum += item.data2;
  1010. }
  1011. free(buffer.c[i].array);
  1012. }
  1013. /*
  1014. * All entries should now be accounted for (unless some external
  1015. * actor is interfering with our allowed affinity while this
  1016. * test is running).
  1017. */
  1018. assert(sum == expected_sum);
  1019. }
  1020. static void test_signal_interrupt_handler(int signo)
  1021. {
  1022. signals_delivered++;
  1023. }
  1024. static int set_signal_handler(void)
  1025. {
  1026. int ret = 0;
  1027. struct sigaction sa;
  1028. sigset_t sigset;
  1029. ret = sigemptyset(&sigset);
  1030. if (ret < 0) {
  1031. perror("sigemptyset");
  1032. return ret;
  1033. }
  1034. sa.sa_handler = test_signal_interrupt_handler;
  1035. sa.sa_mask = sigset;
  1036. sa.sa_flags = 0;
  1037. ret = sigaction(SIGUSR1, &sa, NULL);
  1038. if (ret < 0) {
  1039. perror("sigaction");
  1040. return ret;
  1041. }
  1042. printf_verbose("Signal handler set for SIGUSR1\n");
  1043. return ret;
  1044. }
  1045. /* Test MEMBARRIER_CMD_PRIVATE_RESTART_RSEQ_ON_CPU membarrier command. */
  1046. #ifdef TEST_MEMBARRIER
  1047. struct test_membarrier_thread_args {
  1048. int stop;
  1049. intptr_t percpu_list_ptr;
  1050. };
  1051. /* Worker threads modify data in their "active" percpu lists. */
  1052. void *test_membarrier_worker_thread(void *arg)
  1053. {
  1054. struct test_membarrier_thread_args *args =
  1055. (struct test_membarrier_thread_args *)arg;
  1056. const int iters = opt_reps;
  1057. int i;
  1058. if (rseq_register_current_thread()) {
  1059. fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
  1060. errno, strerror(errno));
  1061. abort();
  1062. }
  1063. /* Wait for initialization. */
  1064. while (!__atomic_load_n(&args->percpu_list_ptr, __ATOMIC_ACQUIRE)) {}
  1065. for (i = 0; i < iters; ++i) {
  1066. int ret;
  1067. do {
  1068. int cpu = get_current_cpu_id();
  1069. ret = rseq_offset_deref_addv(RSEQ_MO_RELAXED, RSEQ_PERCPU,
  1070. &args->percpu_list_ptr,
  1071. sizeof(struct percpu_list_entry) * cpu, 1, cpu);
  1072. } while (rseq_unlikely(ret));
  1073. }
  1074. if (rseq_unregister_current_thread()) {
  1075. fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
  1076. errno, strerror(errno));
  1077. abort();
  1078. }
  1079. return NULL;
  1080. }
  1081. void test_membarrier_init_percpu_list(struct percpu_list *list)
  1082. {
  1083. int i;
  1084. memset(list, 0, sizeof(*list));
  1085. for (i = 0; i < CPU_SETSIZE; i++) {
  1086. struct percpu_list_node *node;
  1087. node = malloc(sizeof(*node));
  1088. assert(node);
  1089. node->data = 0;
  1090. node->next = NULL;
  1091. list->c[i].head = node;
  1092. }
  1093. }
  1094. void test_membarrier_free_percpu_list(struct percpu_list *list)
  1095. {
  1096. int i;
  1097. for (i = 0; i < CPU_SETSIZE; i++)
  1098. free(list->c[i].head);
  1099. }
  1100. /*
  1101. * The manager thread swaps per-cpu lists that worker threads see,
  1102. * and validates that there are no unexpected modifications.
  1103. */
  1104. void *test_membarrier_manager_thread(void *arg)
  1105. {
  1106. struct test_membarrier_thread_args *args =
  1107. (struct test_membarrier_thread_args *)arg;
  1108. struct percpu_list list_a, list_b;
  1109. intptr_t expect_a = 0, expect_b = 0;
  1110. int cpu_a = 0, cpu_b = 0;
  1111. if (rseq_register_current_thread()) {
  1112. fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
  1113. errno, strerror(errno));
  1114. abort();
  1115. }
  1116. /* Init lists. */
  1117. test_membarrier_init_percpu_list(&list_a);
  1118. test_membarrier_init_percpu_list(&list_b);
  1119. __atomic_store_n(&args->percpu_list_ptr, (intptr_t)&list_a, __ATOMIC_RELEASE);
  1120. while (!__atomic_load_n(&args->stop, __ATOMIC_ACQUIRE)) {
  1121. /* list_a is "active". */
  1122. cpu_a = rand() % CPU_SETSIZE;
  1123. /*
  1124. * As list_b is "inactive", we should never see changes
  1125. * to list_b.
  1126. */
  1127. if (expect_b != __atomic_load_n(&list_b.c[cpu_b].head->data, __ATOMIC_ACQUIRE)) {
  1128. fprintf(stderr, "Membarrier test failed\n");
  1129. abort();
  1130. }
  1131. /* Make list_b "active". */
  1132. __atomic_store_n(&args->percpu_list_ptr, (intptr_t)&list_b, __ATOMIC_RELEASE);
  1133. if (rseq_membarrier_expedited(cpu_a) &&
  1134. errno != ENXIO /* missing CPU */) {
  1135. perror("sys_membarrier");
  1136. abort();
  1137. }
  1138. /*
  1139. * Cpu A should now only modify list_b, so the values
  1140. * in list_a should be stable.
  1141. */
  1142. expect_a = __atomic_load_n(&list_a.c[cpu_a].head->data, __ATOMIC_ACQUIRE);
  1143. cpu_b = rand() % CPU_SETSIZE;
  1144. /*
  1145. * As list_a is "inactive", we should never see changes
  1146. * to list_a.
  1147. */
  1148. if (expect_a != __atomic_load_n(&list_a.c[cpu_a].head->data, __ATOMIC_ACQUIRE)) {
  1149. fprintf(stderr, "Membarrier test failed\n");
  1150. abort();
  1151. }
  1152. /* Make list_a "active". */
  1153. __atomic_store_n(&args->percpu_list_ptr, (intptr_t)&list_a, __ATOMIC_RELEASE);
  1154. if (rseq_membarrier_expedited(cpu_b) &&
  1155. errno != ENXIO /* missing CPU*/) {
  1156. perror("sys_membarrier");
  1157. abort();
  1158. }
  1159. /* Remember a value from list_b. */
  1160. expect_b = __atomic_load_n(&list_b.c[cpu_b].head->data, __ATOMIC_ACQUIRE);
  1161. }
  1162. test_membarrier_free_percpu_list(&list_a);
  1163. test_membarrier_free_percpu_list(&list_b);
  1164. if (rseq_unregister_current_thread()) {
  1165. fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
  1166. errno, strerror(errno));
  1167. abort();
  1168. }
  1169. return NULL;
  1170. }
  1171. void test_membarrier(void)
  1172. {
  1173. const int num_threads = opt_threads;
  1174. struct test_membarrier_thread_args thread_args;
  1175. pthread_t worker_threads[num_threads];
  1176. pthread_t manager_thread;
  1177. int i, ret;
  1178. if (sys_membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ, 0, 0)) {
  1179. perror("sys_membarrier");
  1180. abort();
  1181. }
  1182. thread_args.stop = 0;
  1183. thread_args.percpu_list_ptr = 0;
  1184. ret = pthread_create(&manager_thread, NULL,
  1185. test_membarrier_manager_thread, &thread_args);
  1186. if (ret) {
  1187. errno = ret;
  1188. perror("pthread_create");
  1189. abort();
  1190. }
  1191. for (i = 0; i < num_threads; i++) {
  1192. ret = pthread_create(&worker_threads[i], NULL,
  1193. test_membarrier_worker_thread, &thread_args);
  1194. if (ret) {
  1195. errno = ret;
  1196. perror("pthread_create");
  1197. abort();
  1198. }
  1199. }
  1200. for (i = 0; i < num_threads; i++) {
  1201. ret = pthread_join(worker_threads[i], NULL);
  1202. if (ret) {
  1203. errno = ret;
  1204. perror("pthread_join");
  1205. abort();
  1206. }
  1207. }
  1208. __atomic_store_n(&thread_args.stop, 1, __ATOMIC_RELEASE);
  1209. ret = pthread_join(manager_thread, NULL);
  1210. if (ret) {
  1211. errno = ret;
  1212. perror("pthread_join");
  1213. abort();
  1214. }
  1215. }
  1216. #else /* TEST_MEMBARRIER */
  1217. void test_membarrier(void)
  1218. {
  1219. fprintf(stderr, "rseq_offset_deref_addv is not implemented on this architecture. "
  1220. "Skipping membarrier test.\n");
  1221. }
  1222. #endif
  1223. static void show_usage(int argc, char **argv)
  1224. {
  1225. printf("Usage : %s <OPTIONS>\n",
  1226. argv[0]);
  1227. printf("OPTIONS:\n");
  1228. printf(" [-1 loops] Number of loops for delay injection 1\n");
  1229. printf(" [-2 loops] Number of loops for delay injection 2\n");
  1230. printf(" [-3 loops] Number of loops for delay injection 3\n");
  1231. printf(" [-4 loops] Number of loops for delay injection 4\n");
  1232. printf(" [-5 loops] Number of loops for delay injection 5\n");
  1233. printf(" [-6 loops] Number of loops for delay injection 6\n");
  1234. printf(" [-7 loops] Number of loops for delay injection 7 (-1 to enable -m)\n");
  1235. printf(" [-8 loops] Number of loops for delay injection 8 (-1 to enable -m)\n");
  1236. printf(" [-9 loops] Number of loops for delay injection 9 (-1 to enable -m)\n");
  1237. printf(" [-m N] Yield/sleep/kill every modulo N (default 0: disabled) (>= 0)\n");
  1238. printf(" [-y] Yield\n");
  1239. printf(" [-k] Kill thread with signal\n");
  1240. printf(" [-s S] S: =0: disabled (default), >0: sleep time (ms)\n");
  1241. printf(" [-t N] Number of threads (default 200)\n");
  1242. printf(" [-r N] Number of repetitions per thread (default 5000)\n");
  1243. printf(" [-d] Disable rseq system call (no initialization)\n");
  1244. printf(" [-D M] Disable rseq for each M threads\n");
  1245. printf(" [-T test] Choose test: (s)pinlock, (l)ist, (b)uffer, (m)emcpy, (i)ncrement, membarrie(r)\n");
  1246. printf(" [-M] Push into buffer and memcpy buffer with memory barriers.\n");
  1247. printf(" [-v] Verbose output.\n");
  1248. printf(" [-h] Show this help.\n");
  1249. printf("\n");
  1250. }
  1251. int main(int argc, char **argv)
  1252. {
  1253. int i;
  1254. for (i = 1; i < argc; i++) {
  1255. if (argv[i][0] != '-')
  1256. continue;
  1257. switch (argv[i][1]) {
  1258. case '1':
  1259. case '2':
  1260. case '3':
  1261. case '4':
  1262. case '5':
  1263. case '6':
  1264. case '7':
  1265. case '8':
  1266. case '9':
  1267. if (argc < i + 2) {
  1268. show_usage(argc, argv);
  1269. goto error;
  1270. }
  1271. loop_cnt[argv[i][1] - '0'] = atol(argv[i + 1]);
  1272. i++;
  1273. break;
  1274. case 'm':
  1275. if (argc < i + 2) {
  1276. show_usage(argc, argv);
  1277. goto error;
  1278. }
  1279. opt_modulo = atol(argv[i + 1]);
  1280. if (opt_modulo < 0) {
  1281. show_usage(argc, argv);
  1282. goto error;
  1283. }
  1284. i++;
  1285. break;
  1286. case 's':
  1287. if (argc < i + 2) {
  1288. show_usage(argc, argv);
  1289. goto error;
  1290. }
  1291. opt_sleep = atol(argv[i + 1]);
  1292. if (opt_sleep < 0) {
  1293. show_usage(argc, argv);
  1294. goto error;
  1295. }
  1296. i++;
  1297. break;
  1298. case 'y':
  1299. opt_yield = 1;
  1300. break;
  1301. case 'k':
  1302. opt_signal = 1;
  1303. break;
  1304. case 'd':
  1305. opt_disable_rseq = 1;
  1306. break;
  1307. case 'D':
  1308. if (argc < i + 2) {
  1309. show_usage(argc, argv);
  1310. goto error;
  1311. }
  1312. opt_disable_mod = atol(argv[i + 1]);
  1313. if (opt_disable_mod < 0) {
  1314. show_usage(argc, argv);
  1315. goto error;
  1316. }
  1317. i++;
  1318. break;
  1319. case 't':
  1320. if (argc < i + 2) {
  1321. show_usage(argc, argv);
  1322. goto error;
  1323. }
  1324. opt_threads = atol(argv[i + 1]);
  1325. if (opt_threads < 0) {
  1326. show_usage(argc, argv);
  1327. goto error;
  1328. }
  1329. i++;
  1330. break;
  1331. case 'r':
  1332. if (argc < i + 2) {
  1333. show_usage(argc, argv);
  1334. goto error;
  1335. }
  1336. opt_reps = atoll(argv[i + 1]);
  1337. if (opt_reps < 0) {
  1338. show_usage(argc, argv);
  1339. goto error;
  1340. }
  1341. i++;
  1342. break;
  1343. case 'h':
  1344. show_usage(argc, argv);
  1345. goto end;
  1346. case 'T':
  1347. if (argc < i + 2) {
  1348. show_usage(argc, argv);
  1349. goto error;
  1350. }
  1351. opt_test = *argv[i + 1];
  1352. switch (opt_test) {
  1353. case 's':
  1354. case 'l':
  1355. case 'i':
  1356. case 'b':
  1357. case 'm':
  1358. case 'r':
  1359. break;
  1360. default:
  1361. show_usage(argc, argv);
  1362. goto error;
  1363. }
  1364. i++;
  1365. break;
  1366. case 'v':
  1367. verbose = 1;
  1368. break;
  1369. case 'M':
  1370. opt_mo = RSEQ_MO_RELEASE;
  1371. break;
  1372. default:
  1373. show_usage(argc, argv);
  1374. goto error;
  1375. }
  1376. }
  1377. loop_cnt_1 = loop_cnt[1];
  1378. loop_cnt_2 = loop_cnt[2];
  1379. loop_cnt_3 = loop_cnt[3];
  1380. loop_cnt_4 = loop_cnt[4];
  1381. loop_cnt_5 = loop_cnt[5];
  1382. loop_cnt_6 = loop_cnt[6];
  1383. if (set_signal_handler())
  1384. goto error;
  1385. if (!opt_disable_rseq && rseq_register_current_thread())
  1386. goto error;
  1387. if (!opt_disable_rseq && !rseq_validate_cpu_id()) {
  1388. fprintf(stderr, "Error: cpu id getter unavailable\n");
  1389. goto error;
  1390. }
  1391. switch (opt_test) {
  1392. case 's':
  1393. printf_verbose("spinlock\n");
  1394. test_percpu_spinlock();
  1395. break;
  1396. case 'l':
  1397. printf_verbose("linked list\n");
  1398. test_percpu_list();
  1399. break;
  1400. case 'b':
  1401. printf_verbose("buffer\n");
  1402. test_percpu_buffer();
  1403. break;
  1404. case 'm':
  1405. printf_verbose("memcpy buffer\n");
  1406. test_percpu_memcpy_buffer();
  1407. break;
  1408. case 'i':
  1409. printf_verbose("counter increment\n");
  1410. test_percpu_inc();
  1411. break;
  1412. case 'r':
  1413. printf_verbose("membarrier\n");
  1414. test_membarrier();
  1415. break;
  1416. }
  1417. if (!opt_disable_rseq && rseq_unregister_current_thread())
  1418. abort();
  1419. end:
  1420. return 0;
  1421. error:
  1422. return -1;
  1423. }