param_test.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. // SPDX-License-Identifier: LGPL-2.1
  2. #define _GNU_SOURCE
  3. #include <assert.h>
  4. #include <pthread.h>
  5. #include <sched.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <syscall.h>
  11. #include <unistd.h>
  12. #include <poll.h>
  13. #include <sys/types.h>
  14. #include <signal.h>
  15. #include <errno.h>
  16. #include <stddef.h>
  17. static inline pid_t gettid(void)
  18. {
  19. return syscall(__NR_gettid);
  20. }
  21. #define NR_INJECT 9
  22. static int loop_cnt[NR_INJECT + 1];
  23. static int loop_cnt_1 asm("asm_loop_cnt_1") __attribute__((used));
  24. static int loop_cnt_2 asm("asm_loop_cnt_2") __attribute__((used));
  25. static int loop_cnt_3 asm("asm_loop_cnt_3") __attribute__((used));
  26. static int loop_cnt_4 asm("asm_loop_cnt_4") __attribute__((used));
  27. static int loop_cnt_5 asm("asm_loop_cnt_5") __attribute__((used));
  28. static int loop_cnt_6 asm("asm_loop_cnt_6") __attribute__((used));
  29. static int opt_modulo, verbose;
  30. static int opt_yield, opt_signal, opt_sleep,
  31. opt_disable_rseq, opt_threads = 200,
  32. opt_disable_mod = 0, opt_test = 's', opt_mb = 0;
  33. #ifndef RSEQ_SKIP_FASTPATH
  34. static long long opt_reps = 5000;
  35. #else
  36. static long long opt_reps = 100;
  37. #endif
  38. static __thread __attribute__((tls_model("initial-exec")))
  39. unsigned int signals_delivered;
  40. #ifndef BENCHMARK
  41. static __thread __attribute__((tls_model("initial-exec"), unused))
  42. unsigned int yield_mod_cnt, nr_abort;
  43. #define printf_verbose(fmt, ...) \
  44. do { \
  45. if (verbose) \
  46. printf(fmt, ## __VA_ARGS__); \
  47. } while (0)
  48. #if defined(__x86_64__) || defined(__i386__)
  49. #define INJECT_ASM_REG "eax"
  50. #define RSEQ_INJECT_CLOBBER \
  51. , INJECT_ASM_REG
  52. #ifdef __i386__
  53. #define RSEQ_INJECT_ASM(n) \
  54. "mov asm_loop_cnt_" #n ", %%" INJECT_ASM_REG "\n\t" \
  55. "test %%" INJECT_ASM_REG ",%%" INJECT_ASM_REG "\n\t" \
  56. "jz 333f\n\t" \
  57. "222:\n\t" \
  58. "dec %%" INJECT_ASM_REG "\n\t" \
  59. "jnz 222b\n\t" \
  60. "333:\n\t"
  61. #elif defined(__x86_64__)
  62. #define RSEQ_INJECT_ASM(n) \
  63. "lea asm_loop_cnt_" #n "(%%rip), %%" INJECT_ASM_REG "\n\t" \
  64. "mov (%%" INJECT_ASM_REG "), %%" INJECT_ASM_REG "\n\t" \
  65. "test %%" INJECT_ASM_REG ",%%" INJECT_ASM_REG "\n\t" \
  66. "jz 333f\n\t" \
  67. "222:\n\t" \
  68. "dec %%" INJECT_ASM_REG "\n\t" \
  69. "jnz 222b\n\t" \
  70. "333:\n\t"
  71. #else
  72. #error "Unsupported architecture"
  73. #endif
  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 __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. #else
  166. #error unsupported target
  167. #endif
  168. #define RSEQ_INJECT_FAILED \
  169. nr_abort++;
  170. #define RSEQ_INJECT_C(n) \
  171. { \
  172. int loc_i, loc_nr_loops = loop_cnt[n]; \
  173. \
  174. for (loc_i = 0; loc_i < loc_nr_loops; loc_i++) { \
  175. rseq_barrier(); \
  176. } \
  177. if (loc_nr_loops == -1 && opt_modulo) { \
  178. if (yield_mod_cnt == opt_modulo - 1) { \
  179. if (opt_sleep > 0) \
  180. poll(NULL, 0, opt_sleep); \
  181. if (opt_yield) \
  182. sched_yield(); \
  183. if (opt_signal) \
  184. raise(SIGUSR1); \
  185. yield_mod_cnt = 0; \
  186. } else { \
  187. yield_mod_cnt++; \
  188. } \
  189. } \
  190. }
  191. #else
  192. #define printf_verbose(fmt, ...)
  193. #endif /* BENCHMARK */
  194. #include "rseq.h"
  195. struct percpu_lock_entry {
  196. intptr_t v;
  197. } __attribute__((aligned(128)));
  198. struct percpu_lock {
  199. struct percpu_lock_entry c[CPU_SETSIZE];
  200. };
  201. struct test_data_entry {
  202. intptr_t count;
  203. } __attribute__((aligned(128)));
  204. struct spinlock_test_data {
  205. struct percpu_lock lock;
  206. struct test_data_entry c[CPU_SETSIZE];
  207. };
  208. struct spinlock_thread_test_data {
  209. struct spinlock_test_data *data;
  210. long long reps;
  211. int reg;
  212. };
  213. struct inc_test_data {
  214. struct test_data_entry c[CPU_SETSIZE];
  215. };
  216. struct inc_thread_test_data {
  217. struct inc_test_data *data;
  218. long long reps;
  219. int reg;
  220. };
  221. struct percpu_list_node {
  222. intptr_t data;
  223. struct percpu_list_node *next;
  224. };
  225. struct percpu_list_entry {
  226. struct percpu_list_node *head;
  227. } __attribute__((aligned(128)));
  228. struct percpu_list {
  229. struct percpu_list_entry c[CPU_SETSIZE];
  230. };
  231. #define BUFFER_ITEM_PER_CPU 100
  232. struct percpu_buffer_node {
  233. intptr_t data;
  234. };
  235. struct percpu_buffer_entry {
  236. intptr_t offset;
  237. intptr_t buflen;
  238. struct percpu_buffer_node **array;
  239. } __attribute__((aligned(128)));
  240. struct percpu_buffer {
  241. struct percpu_buffer_entry c[CPU_SETSIZE];
  242. };
  243. #define MEMCPY_BUFFER_ITEM_PER_CPU 100
  244. struct percpu_memcpy_buffer_node {
  245. intptr_t data1;
  246. uint64_t data2;
  247. };
  248. struct percpu_memcpy_buffer_entry {
  249. intptr_t offset;
  250. intptr_t buflen;
  251. struct percpu_memcpy_buffer_node *array;
  252. } __attribute__((aligned(128)));
  253. struct percpu_memcpy_buffer {
  254. struct percpu_memcpy_buffer_entry c[CPU_SETSIZE];
  255. };
  256. /* A simple percpu spinlock. Grabs lock on current cpu. */
  257. static int rseq_this_cpu_lock(struct percpu_lock *lock)
  258. {
  259. int cpu;
  260. for (;;) {
  261. int ret;
  262. cpu = rseq_cpu_start();
  263. ret = rseq_cmpeqv_storev(&lock->c[cpu].v,
  264. 0, 1, cpu);
  265. if (rseq_likely(!ret))
  266. break;
  267. /* Retry if comparison fails or rseq aborts. */
  268. }
  269. /*
  270. * Acquire semantic when taking lock after control dependency.
  271. * Matches rseq_smp_store_release().
  272. */
  273. rseq_smp_acquire__after_ctrl_dep();
  274. return cpu;
  275. }
  276. static void rseq_percpu_unlock(struct percpu_lock *lock, int cpu)
  277. {
  278. assert(lock->c[cpu].v == 1);
  279. /*
  280. * Release lock, with release semantic. Matches
  281. * rseq_smp_acquire__after_ctrl_dep().
  282. */
  283. rseq_smp_store_release(&lock->c[cpu].v, 0);
  284. }
  285. void *test_percpu_spinlock_thread(void *arg)
  286. {
  287. struct spinlock_thread_test_data *thread_data = arg;
  288. struct spinlock_test_data *data = thread_data->data;
  289. long long i, reps;
  290. if (!opt_disable_rseq && thread_data->reg &&
  291. rseq_register_current_thread())
  292. abort();
  293. reps = thread_data->reps;
  294. for (i = 0; i < reps; i++) {
  295. int cpu = rseq_cpu_start();
  296. cpu = rseq_this_cpu_lock(&data->lock);
  297. data->c[cpu].count++;
  298. rseq_percpu_unlock(&data->lock, cpu);
  299. #ifndef BENCHMARK
  300. if (i != 0 && !(i % (reps / 10)))
  301. printf_verbose("tid %d: count %lld\n", (int) gettid(), i);
  302. #endif
  303. }
  304. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  305. (int) gettid(), nr_abort, signals_delivered);
  306. if (!opt_disable_rseq && thread_data->reg &&
  307. rseq_unregister_current_thread())
  308. abort();
  309. return NULL;
  310. }
  311. /*
  312. * A simple test which implements a sharded counter using a per-cpu
  313. * lock. Obviously real applications might prefer to simply use a
  314. * per-cpu increment; however, this is reasonable for a test and the
  315. * lock can be extended to synchronize more complicated operations.
  316. */
  317. void test_percpu_spinlock(void)
  318. {
  319. const int num_threads = opt_threads;
  320. int i, ret;
  321. uint64_t sum;
  322. pthread_t test_threads[num_threads];
  323. struct spinlock_test_data data;
  324. struct spinlock_thread_test_data thread_data[num_threads];
  325. memset(&data, 0, sizeof(data));
  326. for (i = 0; i < num_threads; i++) {
  327. thread_data[i].reps = opt_reps;
  328. if (opt_disable_mod <= 0 || (i % opt_disable_mod))
  329. thread_data[i].reg = 1;
  330. else
  331. thread_data[i].reg = 0;
  332. thread_data[i].data = &data;
  333. ret = pthread_create(&test_threads[i], NULL,
  334. test_percpu_spinlock_thread,
  335. &thread_data[i]);
  336. if (ret) {
  337. errno = ret;
  338. perror("pthread_create");
  339. abort();
  340. }
  341. }
  342. for (i = 0; i < num_threads; i++) {
  343. ret = pthread_join(test_threads[i], NULL);
  344. if (ret) {
  345. errno = ret;
  346. perror("pthread_join");
  347. abort();
  348. }
  349. }
  350. sum = 0;
  351. for (i = 0; i < CPU_SETSIZE; i++)
  352. sum += data.c[i].count;
  353. assert(sum == (uint64_t)opt_reps * num_threads);
  354. }
  355. void *test_percpu_inc_thread(void *arg)
  356. {
  357. struct inc_thread_test_data *thread_data = arg;
  358. struct inc_test_data *data = thread_data->data;
  359. long long i, reps;
  360. if (!opt_disable_rseq && thread_data->reg &&
  361. rseq_register_current_thread())
  362. abort();
  363. reps = thread_data->reps;
  364. for (i = 0; i < reps; i++) {
  365. int ret;
  366. do {
  367. int cpu;
  368. cpu = rseq_cpu_start();
  369. ret = rseq_addv(&data->c[cpu].count, 1, cpu);
  370. } while (rseq_unlikely(ret));
  371. #ifndef BENCHMARK
  372. if (i != 0 && !(i % (reps / 10)))
  373. printf_verbose("tid %d: count %lld\n", (int) gettid(), i);
  374. #endif
  375. }
  376. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  377. (int) gettid(), nr_abort, signals_delivered);
  378. if (!opt_disable_rseq && thread_data->reg &&
  379. rseq_unregister_current_thread())
  380. abort();
  381. return NULL;
  382. }
  383. void test_percpu_inc(void)
  384. {
  385. const int num_threads = opt_threads;
  386. int i, ret;
  387. uint64_t sum;
  388. pthread_t test_threads[num_threads];
  389. struct inc_test_data data;
  390. struct inc_thread_test_data thread_data[num_threads];
  391. memset(&data, 0, sizeof(data));
  392. for (i = 0; i < num_threads; i++) {
  393. thread_data[i].reps = opt_reps;
  394. if (opt_disable_mod <= 0 || (i % opt_disable_mod))
  395. thread_data[i].reg = 1;
  396. else
  397. thread_data[i].reg = 0;
  398. thread_data[i].data = &data;
  399. ret = pthread_create(&test_threads[i], NULL,
  400. test_percpu_inc_thread,
  401. &thread_data[i]);
  402. if (ret) {
  403. errno = ret;
  404. perror("pthread_create");
  405. abort();
  406. }
  407. }
  408. for (i = 0; i < num_threads; i++) {
  409. ret = pthread_join(test_threads[i], NULL);
  410. if (ret) {
  411. errno = ret;
  412. perror("pthread_join");
  413. abort();
  414. }
  415. }
  416. sum = 0;
  417. for (i = 0; i < CPU_SETSIZE; i++)
  418. sum += data.c[i].count;
  419. assert(sum == (uint64_t)opt_reps * num_threads);
  420. }
  421. void this_cpu_list_push(struct percpu_list *list,
  422. struct percpu_list_node *node,
  423. int *_cpu)
  424. {
  425. int cpu;
  426. for (;;) {
  427. intptr_t *targetptr, newval, expect;
  428. int ret;
  429. cpu = rseq_cpu_start();
  430. /* Load list->c[cpu].head with single-copy atomicity. */
  431. expect = (intptr_t)RSEQ_READ_ONCE(list->c[cpu].head);
  432. newval = (intptr_t)node;
  433. targetptr = (intptr_t *)&list->c[cpu].head;
  434. node->next = (struct percpu_list_node *)expect;
  435. ret = rseq_cmpeqv_storev(targetptr, expect, newval, cpu);
  436. if (rseq_likely(!ret))
  437. break;
  438. /* Retry if comparison fails or rseq aborts. */
  439. }
  440. if (_cpu)
  441. *_cpu = cpu;
  442. }
  443. /*
  444. * Unlike a traditional lock-less linked list; the availability of a
  445. * rseq primitive allows us to implement pop without concerns over
  446. * ABA-type races.
  447. */
  448. struct percpu_list_node *this_cpu_list_pop(struct percpu_list *list,
  449. int *_cpu)
  450. {
  451. struct percpu_list_node *node = NULL;
  452. int cpu;
  453. for (;;) {
  454. struct percpu_list_node *head;
  455. intptr_t *targetptr, expectnot, *load;
  456. off_t offset;
  457. int ret;
  458. cpu = rseq_cpu_start();
  459. targetptr = (intptr_t *)&list->c[cpu].head;
  460. expectnot = (intptr_t)NULL;
  461. offset = offsetof(struct percpu_list_node, next);
  462. load = (intptr_t *)&head;
  463. ret = rseq_cmpnev_storeoffp_load(targetptr, expectnot,
  464. offset, load, cpu);
  465. if (rseq_likely(!ret)) {
  466. node = head;
  467. break;
  468. }
  469. if (ret > 0)
  470. break;
  471. /* Retry if rseq aborts. */
  472. }
  473. if (_cpu)
  474. *_cpu = cpu;
  475. return node;
  476. }
  477. /*
  478. * __percpu_list_pop is not safe against concurrent accesses. Should
  479. * only be used on lists that are not concurrently modified.
  480. */
  481. struct percpu_list_node *__percpu_list_pop(struct percpu_list *list, int cpu)
  482. {
  483. struct percpu_list_node *node;
  484. node = list->c[cpu].head;
  485. if (!node)
  486. return NULL;
  487. list->c[cpu].head = node->next;
  488. return node;
  489. }
  490. void *test_percpu_list_thread(void *arg)
  491. {
  492. long long i, reps;
  493. struct percpu_list *list = (struct percpu_list *)arg;
  494. if (!opt_disable_rseq && rseq_register_current_thread())
  495. abort();
  496. reps = opt_reps;
  497. for (i = 0; i < reps; i++) {
  498. struct percpu_list_node *node;
  499. node = this_cpu_list_pop(list, NULL);
  500. if (opt_yield)
  501. sched_yield(); /* encourage shuffling */
  502. if (node)
  503. this_cpu_list_push(list, node, NULL);
  504. }
  505. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  506. (int) gettid(), nr_abort, signals_delivered);
  507. if (!opt_disable_rseq && rseq_unregister_current_thread())
  508. abort();
  509. return NULL;
  510. }
  511. /* Simultaneous modification to a per-cpu linked list from many threads. */
  512. void test_percpu_list(void)
  513. {
  514. const int num_threads = opt_threads;
  515. int i, j, ret;
  516. uint64_t sum = 0, expected_sum = 0;
  517. struct percpu_list list;
  518. pthread_t test_threads[num_threads];
  519. cpu_set_t allowed_cpus;
  520. memset(&list, 0, sizeof(list));
  521. /* Generate list entries for every usable cpu. */
  522. sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
  523. for (i = 0; i < CPU_SETSIZE; i++) {
  524. if (!CPU_ISSET(i, &allowed_cpus))
  525. continue;
  526. for (j = 1; j <= 100; j++) {
  527. struct percpu_list_node *node;
  528. expected_sum += j;
  529. node = malloc(sizeof(*node));
  530. assert(node);
  531. node->data = j;
  532. node->next = list.c[i].head;
  533. list.c[i].head = node;
  534. }
  535. }
  536. for (i = 0; i < num_threads; i++) {
  537. ret = pthread_create(&test_threads[i], NULL,
  538. test_percpu_list_thread, &list);
  539. if (ret) {
  540. errno = ret;
  541. perror("pthread_create");
  542. abort();
  543. }
  544. }
  545. for (i = 0; i < num_threads; i++) {
  546. ret = pthread_join(test_threads[i], NULL);
  547. if (ret) {
  548. errno = ret;
  549. perror("pthread_join");
  550. abort();
  551. }
  552. }
  553. for (i = 0; i < CPU_SETSIZE; i++) {
  554. struct percpu_list_node *node;
  555. if (!CPU_ISSET(i, &allowed_cpus))
  556. continue;
  557. while ((node = __percpu_list_pop(&list, i))) {
  558. sum += node->data;
  559. free(node);
  560. }
  561. }
  562. /*
  563. * All entries should now be accounted for (unless some external
  564. * actor is interfering with our allowed affinity while this
  565. * test is running).
  566. */
  567. assert(sum == expected_sum);
  568. }
  569. bool this_cpu_buffer_push(struct percpu_buffer *buffer,
  570. struct percpu_buffer_node *node,
  571. int *_cpu)
  572. {
  573. bool result = false;
  574. int cpu;
  575. for (;;) {
  576. intptr_t *targetptr_spec, newval_spec;
  577. intptr_t *targetptr_final, newval_final;
  578. intptr_t offset;
  579. int ret;
  580. cpu = rseq_cpu_start();
  581. offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
  582. if (offset == buffer->c[cpu].buflen)
  583. break;
  584. newval_spec = (intptr_t)node;
  585. targetptr_spec = (intptr_t *)&buffer->c[cpu].array[offset];
  586. newval_final = offset + 1;
  587. targetptr_final = &buffer->c[cpu].offset;
  588. if (opt_mb)
  589. ret = rseq_cmpeqv_trystorev_storev_release(
  590. targetptr_final, offset, targetptr_spec,
  591. newval_spec, newval_final, cpu);
  592. else
  593. ret = rseq_cmpeqv_trystorev_storev(targetptr_final,
  594. offset, targetptr_spec, newval_spec,
  595. newval_final, cpu);
  596. if (rseq_likely(!ret)) {
  597. result = true;
  598. break;
  599. }
  600. /* Retry if comparison fails or rseq aborts. */
  601. }
  602. if (_cpu)
  603. *_cpu = cpu;
  604. return result;
  605. }
  606. struct percpu_buffer_node *this_cpu_buffer_pop(struct percpu_buffer *buffer,
  607. int *_cpu)
  608. {
  609. struct percpu_buffer_node *head;
  610. int cpu;
  611. for (;;) {
  612. intptr_t *targetptr, newval;
  613. intptr_t offset;
  614. int ret;
  615. cpu = rseq_cpu_start();
  616. /* Load offset with single-copy atomicity. */
  617. offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
  618. if (offset == 0) {
  619. head = NULL;
  620. break;
  621. }
  622. head = RSEQ_READ_ONCE(buffer->c[cpu].array[offset - 1]);
  623. newval = offset - 1;
  624. targetptr = (intptr_t *)&buffer->c[cpu].offset;
  625. ret = rseq_cmpeqv_cmpeqv_storev(targetptr, offset,
  626. (intptr_t *)&buffer->c[cpu].array[offset - 1],
  627. (intptr_t)head, newval, cpu);
  628. if (rseq_likely(!ret))
  629. break;
  630. /* Retry if comparison fails or rseq aborts. */
  631. }
  632. if (_cpu)
  633. *_cpu = cpu;
  634. return head;
  635. }
  636. /*
  637. * __percpu_buffer_pop is not safe against concurrent accesses. Should
  638. * only be used on buffers that are not concurrently modified.
  639. */
  640. struct percpu_buffer_node *__percpu_buffer_pop(struct percpu_buffer *buffer,
  641. int cpu)
  642. {
  643. struct percpu_buffer_node *head;
  644. intptr_t offset;
  645. offset = buffer->c[cpu].offset;
  646. if (offset == 0)
  647. return NULL;
  648. head = buffer->c[cpu].array[offset - 1];
  649. buffer->c[cpu].offset = offset - 1;
  650. return head;
  651. }
  652. void *test_percpu_buffer_thread(void *arg)
  653. {
  654. long long i, reps;
  655. struct percpu_buffer *buffer = (struct percpu_buffer *)arg;
  656. if (!opt_disable_rseq && rseq_register_current_thread())
  657. abort();
  658. reps = opt_reps;
  659. for (i = 0; i < reps; i++) {
  660. struct percpu_buffer_node *node;
  661. node = this_cpu_buffer_pop(buffer, NULL);
  662. if (opt_yield)
  663. sched_yield(); /* encourage shuffling */
  664. if (node) {
  665. if (!this_cpu_buffer_push(buffer, node, NULL)) {
  666. /* Should increase buffer size. */
  667. abort();
  668. }
  669. }
  670. }
  671. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  672. (int) gettid(), nr_abort, signals_delivered);
  673. if (!opt_disable_rseq && rseq_unregister_current_thread())
  674. abort();
  675. return NULL;
  676. }
  677. /* Simultaneous modification to a per-cpu buffer from many threads. */
  678. void test_percpu_buffer(void)
  679. {
  680. const int num_threads = opt_threads;
  681. int i, j, ret;
  682. uint64_t sum = 0, expected_sum = 0;
  683. struct percpu_buffer buffer;
  684. pthread_t test_threads[num_threads];
  685. cpu_set_t allowed_cpus;
  686. memset(&buffer, 0, sizeof(buffer));
  687. /* Generate list entries for every usable cpu. */
  688. sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
  689. for (i = 0; i < CPU_SETSIZE; i++) {
  690. if (!CPU_ISSET(i, &allowed_cpus))
  691. continue;
  692. /* Worse-case is every item in same CPU. */
  693. buffer.c[i].array =
  694. malloc(sizeof(*buffer.c[i].array) * CPU_SETSIZE *
  695. BUFFER_ITEM_PER_CPU);
  696. assert(buffer.c[i].array);
  697. buffer.c[i].buflen = CPU_SETSIZE * BUFFER_ITEM_PER_CPU;
  698. for (j = 1; j <= BUFFER_ITEM_PER_CPU; j++) {
  699. struct percpu_buffer_node *node;
  700. expected_sum += j;
  701. /*
  702. * We could theoretically put the word-sized
  703. * "data" directly in the buffer. However, we
  704. * want to model objects that would not fit
  705. * within a single word, so allocate an object
  706. * for each node.
  707. */
  708. node = malloc(sizeof(*node));
  709. assert(node);
  710. node->data = j;
  711. buffer.c[i].array[j - 1] = node;
  712. buffer.c[i].offset++;
  713. }
  714. }
  715. for (i = 0; i < num_threads; i++) {
  716. ret = pthread_create(&test_threads[i], NULL,
  717. test_percpu_buffer_thread, &buffer);
  718. if (ret) {
  719. errno = ret;
  720. perror("pthread_create");
  721. abort();
  722. }
  723. }
  724. for (i = 0; i < num_threads; i++) {
  725. ret = pthread_join(test_threads[i], NULL);
  726. if (ret) {
  727. errno = ret;
  728. perror("pthread_join");
  729. abort();
  730. }
  731. }
  732. for (i = 0; i < CPU_SETSIZE; i++) {
  733. struct percpu_buffer_node *node;
  734. if (!CPU_ISSET(i, &allowed_cpus))
  735. continue;
  736. while ((node = __percpu_buffer_pop(&buffer, i))) {
  737. sum += node->data;
  738. free(node);
  739. }
  740. free(buffer.c[i].array);
  741. }
  742. /*
  743. * All entries should now be accounted for (unless some external
  744. * actor is interfering with our allowed affinity while this
  745. * test is running).
  746. */
  747. assert(sum == expected_sum);
  748. }
  749. bool this_cpu_memcpy_buffer_push(struct percpu_memcpy_buffer *buffer,
  750. struct percpu_memcpy_buffer_node item,
  751. int *_cpu)
  752. {
  753. bool result = false;
  754. int cpu;
  755. for (;;) {
  756. intptr_t *targetptr_final, newval_final, offset;
  757. char *destptr, *srcptr;
  758. size_t copylen;
  759. int ret;
  760. cpu = rseq_cpu_start();
  761. /* Load offset with single-copy atomicity. */
  762. offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
  763. if (offset == buffer->c[cpu].buflen)
  764. break;
  765. destptr = (char *)&buffer->c[cpu].array[offset];
  766. srcptr = (char *)&item;
  767. /* copylen must be <= 4kB. */
  768. copylen = sizeof(item);
  769. newval_final = offset + 1;
  770. targetptr_final = &buffer->c[cpu].offset;
  771. if (opt_mb)
  772. ret = rseq_cmpeqv_trymemcpy_storev_release(
  773. targetptr_final, offset,
  774. destptr, srcptr, copylen,
  775. newval_final, cpu);
  776. else
  777. ret = rseq_cmpeqv_trymemcpy_storev(targetptr_final,
  778. offset, destptr, srcptr, copylen,
  779. newval_final, cpu);
  780. if (rseq_likely(!ret)) {
  781. result = true;
  782. break;
  783. }
  784. /* Retry if comparison fails or rseq aborts. */
  785. }
  786. if (_cpu)
  787. *_cpu = cpu;
  788. return result;
  789. }
  790. bool this_cpu_memcpy_buffer_pop(struct percpu_memcpy_buffer *buffer,
  791. struct percpu_memcpy_buffer_node *item,
  792. int *_cpu)
  793. {
  794. bool result = false;
  795. int cpu;
  796. for (;;) {
  797. intptr_t *targetptr_final, newval_final, offset;
  798. char *destptr, *srcptr;
  799. size_t copylen;
  800. int ret;
  801. cpu = rseq_cpu_start();
  802. /* Load offset with single-copy atomicity. */
  803. offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
  804. if (offset == 0)
  805. break;
  806. destptr = (char *)item;
  807. srcptr = (char *)&buffer->c[cpu].array[offset - 1];
  808. /* copylen must be <= 4kB. */
  809. copylen = sizeof(*item);
  810. newval_final = offset - 1;
  811. targetptr_final = &buffer->c[cpu].offset;
  812. ret = rseq_cmpeqv_trymemcpy_storev(targetptr_final,
  813. offset, destptr, srcptr, copylen,
  814. newval_final, cpu);
  815. if (rseq_likely(!ret)) {
  816. result = true;
  817. break;
  818. }
  819. /* Retry if comparison fails or rseq aborts. */
  820. }
  821. if (_cpu)
  822. *_cpu = cpu;
  823. return result;
  824. }
  825. /*
  826. * __percpu_memcpy_buffer_pop is not safe against concurrent accesses. Should
  827. * only be used on buffers that are not concurrently modified.
  828. */
  829. bool __percpu_memcpy_buffer_pop(struct percpu_memcpy_buffer *buffer,
  830. struct percpu_memcpy_buffer_node *item,
  831. int cpu)
  832. {
  833. intptr_t offset;
  834. offset = buffer->c[cpu].offset;
  835. if (offset == 0)
  836. return false;
  837. memcpy(item, &buffer->c[cpu].array[offset - 1], sizeof(*item));
  838. buffer->c[cpu].offset = offset - 1;
  839. return true;
  840. }
  841. void *test_percpu_memcpy_buffer_thread(void *arg)
  842. {
  843. long long i, reps;
  844. struct percpu_memcpy_buffer *buffer = (struct percpu_memcpy_buffer *)arg;
  845. if (!opt_disable_rseq && rseq_register_current_thread())
  846. abort();
  847. reps = opt_reps;
  848. for (i = 0; i < reps; i++) {
  849. struct percpu_memcpy_buffer_node item;
  850. bool result;
  851. result = this_cpu_memcpy_buffer_pop(buffer, &item, NULL);
  852. if (opt_yield)
  853. sched_yield(); /* encourage shuffling */
  854. if (result) {
  855. if (!this_cpu_memcpy_buffer_push(buffer, item, NULL)) {
  856. /* Should increase buffer size. */
  857. abort();
  858. }
  859. }
  860. }
  861. printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
  862. (int) gettid(), nr_abort, signals_delivered);
  863. if (!opt_disable_rseq && rseq_unregister_current_thread())
  864. abort();
  865. return NULL;
  866. }
  867. /* Simultaneous modification to a per-cpu buffer from many threads. */
  868. void test_percpu_memcpy_buffer(void)
  869. {
  870. const int num_threads = opt_threads;
  871. int i, j, ret;
  872. uint64_t sum = 0, expected_sum = 0;
  873. struct percpu_memcpy_buffer buffer;
  874. pthread_t test_threads[num_threads];
  875. cpu_set_t allowed_cpus;
  876. memset(&buffer, 0, sizeof(buffer));
  877. /* Generate list entries for every usable cpu. */
  878. sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
  879. for (i = 0; i < CPU_SETSIZE; i++) {
  880. if (!CPU_ISSET(i, &allowed_cpus))
  881. continue;
  882. /* Worse-case is every item in same CPU. */
  883. buffer.c[i].array =
  884. malloc(sizeof(*buffer.c[i].array) * CPU_SETSIZE *
  885. MEMCPY_BUFFER_ITEM_PER_CPU);
  886. assert(buffer.c[i].array);
  887. buffer.c[i].buflen = CPU_SETSIZE * MEMCPY_BUFFER_ITEM_PER_CPU;
  888. for (j = 1; j <= MEMCPY_BUFFER_ITEM_PER_CPU; j++) {
  889. expected_sum += 2 * j + 1;
  890. /*
  891. * We could theoretically put the word-sized
  892. * "data" directly in the buffer. However, we
  893. * want to model objects that would not fit
  894. * within a single word, so allocate an object
  895. * for each node.
  896. */
  897. buffer.c[i].array[j - 1].data1 = j;
  898. buffer.c[i].array[j - 1].data2 = j + 1;
  899. buffer.c[i].offset++;
  900. }
  901. }
  902. for (i = 0; i < num_threads; i++) {
  903. ret = pthread_create(&test_threads[i], NULL,
  904. test_percpu_memcpy_buffer_thread,
  905. &buffer);
  906. if (ret) {
  907. errno = ret;
  908. perror("pthread_create");
  909. abort();
  910. }
  911. }
  912. for (i = 0; i < num_threads; i++) {
  913. ret = pthread_join(test_threads[i], NULL);
  914. if (ret) {
  915. errno = ret;
  916. perror("pthread_join");
  917. abort();
  918. }
  919. }
  920. for (i = 0; i < CPU_SETSIZE; i++) {
  921. struct percpu_memcpy_buffer_node item;
  922. if (!CPU_ISSET(i, &allowed_cpus))
  923. continue;
  924. while (__percpu_memcpy_buffer_pop(&buffer, &item, i)) {
  925. sum += item.data1;
  926. sum += item.data2;
  927. }
  928. free(buffer.c[i].array);
  929. }
  930. /*
  931. * All entries should now be accounted for (unless some external
  932. * actor is interfering with our allowed affinity while this
  933. * test is running).
  934. */
  935. assert(sum == expected_sum);
  936. }
  937. static void test_signal_interrupt_handler(int signo)
  938. {
  939. signals_delivered++;
  940. }
  941. static int set_signal_handler(void)
  942. {
  943. int ret = 0;
  944. struct sigaction sa;
  945. sigset_t sigset;
  946. ret = sigemptyset(&sigset);
  947. if (ret < 0) {
  948. perror("sigemptyset");
  949. return ret;
  950. }
  951. sa.sa_handler = test_signal_interrupt_handler;
  952. sa.sa_mask = sigset;
  953. sa.sa_flags = 0;
  954. ret = sigaction(SIGUSR1, &sa, NULL);
  955. if (ret < 0) {
  956. perror("sigaction");
  957. return ret;
  958. }
  959. printf_verbose("Signal handler set for SIGUSR1\n");
  960. return ret;
  961. }
  962. static void show_usage(int argc, char **argv)
  963. {
  964. printf("Usage : %s <OPTIONS>\n",
  965. argv[0]);
  966. printf("OPTIONS:\n");
  967. printf(" [-1 loops] Number of loops for delay injection 1\n");
  968. printf(" [-2 loops] Number of loops for delay injection 2\n");
  969. printf(" [-3 loops] Number of loops for delay injection 3\n");
  970. printf(" [-4 loops] Number of loops for delay injection 4\n");
  971. printf(" [-5 loops] Number of loops for delay injection 5\n");
  972. printf(" [-6 loops] Number of loops for delay injection 6\n");
  973. printf(" [-7 loops] Number of loops for delay injection 7 (-1 to enable -m)\n");
  974. printf(" [-8 loops] Number of loops for delay injection 8 (-1 to enable -m)\n");
  975. printf(" [-9 loops] Number of loops for delay injection 9 (-1 to enable -m)\n");
  976. printf(" [-m N] Yield/sleep/kill every modulo N (default 0: disabled) (>= 0)\n");
  977. printf(" [-y] Yield\n");
  978. printf(" [-k] Kill thread with signal\n");
  979. printf(" [-s S] S: =0: disabled (default), >0: sleep time (ms)\n");
  980. printf(" [-t N] Number of threads (default 200)\n");
  981. printf(" [-r N] Number of repetitions per thread (default 5000)\n");
  982. printf(" [-d] Disable rseq system call (no initialization)\n");
  983. printf(" [-D M] Disable rseq for each M threads\n");
  984. printf(" [-T test] Choose test: (s)pinlock, (l)ist, (b)uffer, (m)emcpy, (i)ncrement\n");
  985. printf(" [-M] Push into buffer and memcpy buffer with memory barriers.\n");
  986. printf(" [-v] Verbose output.\n");
  987. printf(" [-h] Show this help.\n");
  988. printf("\n");
  989. }
  990. int main(int argc, char **argv)
  991. {
  992. int i;
  993. for (i = 1; i < argc; i++) {
  994. if (argv[i][0] != '-')
  995. continue;
  996. switch (argv[i][1]) {
  997. case '1':
  998. case '2':
  999. case '3':
  1000. case '4':
  1001. case '5':
  1002. case '6':
  1003. case '7':
  1004. case '8':
  1005. case '9':
  1006. if (argc < i + 2) {
  1007. show_usage(argc, argv);
  1008. goto error;
  1009. }
  1010. loop_cnt[argv[i][1] - '0'] = atol(argv[i + 1]);
  1011. i++;
  1012. break;
  1013. case 'm':
  1014. if (argc < i + 2) {
  1015. show_usage(argc, argv);
  1016. goto error;
  1017. }
  1018. opt_modulo = atol(argv[i + 1]);
  1019. if (opt_modulo < 0) {
  1020. show_usage(argc, argv);
  1021. goto error;
  1022. }
  1023. i++;
  1024. break;
  1025. case 's':
  1026. if (argc < i + 2) {
  1027. show_usage(argc, argv);
  1028. goto error;
  1029. }
  1030. opt_sleep = atol(argv[i + 1]);
  1031. if (opt_sleep < 0) {
  1032. show_usage(argc, argv);
  1033. goto error;
  1034. }
  1035. i++;
  1036. break;
  1037. case 'y':
  1038. opt_yield = 1;
  1039. break;
  1040. case 'k':
  1041. opt_signal = 1;
  1042. break;
  1043. case 'd':
  1044. opt_disable_rseq = 1;
  1045. break;
  1046. case 'D':
  1047. if (argc < i + 2) {
  1048. show_usage(argc, argv);
  1049. goto error;
  1050. }
  1051. opt_disable_mod = atol(argv[i + 1]);
  1052. if (opt_disable_mod < 0) {
  1053. show_usage(argc, argv);
  1054. goto error;
  1055. }
  1056. i++;
  1057. break;
  1058. case 't':
  1059. if (argc < i + 2) {
  1060. show_usage(argc, argv);
  1061. goto error;
  1062. }
  1063. opt_threads = atol(argv[i + 1]);
  1064. if (opt_threads < 0) {
  1065. show_usage(argc, argv);
  1066. goto error;
  1067. }
  1068. i++;
  1069. break;
  1070. case 'r':
  1071. if (argc < i + 2) {
  1072. show_usage(argc, argv);
  1073. goto error;
  1074. }
  1075. opt_reps = atoll(argv[i + 1]);
  1076. if (opt_reps < 0) {
  1077. show_usage(argc, argv);
  1078. goto error;
  1079. }
  1080. i++;
  1081. break;
  1082. case 'h':
  1083. show_usage(argc, argv);
  1084. goto end;
  1085. case 'T':
  1086. if (argc < i + 2) {
  1087. show_usage(argc, argv);
  1088. goto error;
  1089. }
  1090. opt_test = *argv[i + 1];
  1091. switch (opt_test) {
  1092. case 's':
  1093. case 'l':
  1094. case 'i':
  1095. case 'b':
  1096. case 'm':
  1097. break;
  1098. default:
  1099. show_usage(argc, argv);
  1100. goto error;
  1101. }
  1102. i++;
  1103. break;
  1104. case 'v':
  1105. verbose = 1;
  1106. break;
  1107. case 'M':
  1108. opt_mb = 1;
  1109. break;
  1110. default:
  1111. show_usage(argc, argv);
  1112. goto error;
  1113. }
  1114. }
  1115. loop_cnt_1 = loop_cnt[1];
  1116. loop_cnt_2 = loop_cnt[2];
  1117. loop_cnt_3 = loop_cnt[3];
  1118. loop_cnt_4 = loop_cnt[4];
  1119. loop_cnt_5 = loop_cnt[5];
  1120. loop_cnt_6 = loop_cnt[6];
  1121. if (set_signal_handler())
  1122. goto error;
  1123. if (!opt_disable_rseq && rseq_register_current_thread())
  1124. goto error;
  1125. switch (opt_test) {
  1126. case 's':
  1127. printf_verbose("spinlock\n");
  1128. test_percpu_spinlock();
  1129. break;
  1130. case 'l':
  1131. printf_verbose("linked list\n");
  1132. test_percpu_list();
  1133. break;
  1134. case 'b':
  1135. printf_verbose("buffer\n");
  1136. test_percpu_buffer();
  1137. break;
  1138. case 'm':
  1139. printf_verbose("memcpy buffer\n");
  1140. test_percpu_memcpy_buffer();
  1141. break;
  1142. case 'i':
  1143. printf_verbose("counter increment\n");
  1144. test_percpu_inc();
  1145. break;
  1146. }
  1147. if (!opt_disable_rseq && rseq_unregister_current_thread())
  1148. abort();
  1149. end:
  1150. return 0;
  1151. error:
  1152. return -1;
  1153. }