timeout.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various timeout tests
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <sys/time.h>
  14. #include <sys/wait.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include "helpers.h"
  18. #include "liburing.h"
  19. #include "../src/syscall.h"
  20. #define TIMEOUT_MSEC 200
  21. static int not_supported;
  22. static int no_modify;
  23. static int no_multishot;
  24. static void msec_to_ts(struct __kernel_timespec *ts, unsigned int msec)
  25. {
  26. ts->tv_sec = msec / 1000;
  27. ts->tv_nsec = (msec % 1000) * 1000000;
  28. }
  29. static unsigned long long mtime_since(const struct timeval *s,
  30. const struct timeval *e)
  31. {
  32. long long sec, usec;
  33. sec = e->tv_sec - s->tv_sec;
  34. usec = (e->tv_usec - s->tv_usec);
  35. if (sec > 0 && usec < 0) {
  36. sec--;
  37. usec += 1000000;
  38. }
  39. sec *= 1000;
  40. usec /= 1000;
  41. return sec + usec;
  42. }
  43. static unsigned long long mtime_since_now(struct timeval *tv)
  44. {
  45. struct timeval end;
  46. gettimeofday(&end, NULL);
  47. return mtime_since(tv, &end);
  48. }
  49. /*
  50. * Test that we return to userspace if a timeout triggers, even if we
  51. * don't satisfy the number of events asked for.
  52. */
  53. static int test_single_timeout_many(struct io_uring *ring)
  54. {
  55. struct io_uring_cqe *cqe;
  56. struct io_uring_sqe *sqe;
  57. unsigned long long exp;
  58. struct __kernel_timespec ts;
  59. struct timeval tv;
  60. int ret;
  61. sqe = io_uring_get_sqe(ring);
  62. if (!sqe) {
  63. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  64. goto err;
  65. }
  66. msec_to_ts(&ts, TIMEOUT_MSEC);
  67. io_uring_prep_timeout(sqe, &ts, 0, 0);
  68. ret = io_uring_submit(ring);
  69. if (ret <= 0) {
  70. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  71. goto err;
  72. }
  73. gettimeofday(&tv, NULL);
  74. ret = __sys_io_uring_enter(ring->ring_fd, 0, 4, IORING_ENTER_GETEVENTS,
  75. NULL);
  76. if (ret < 0) {
  77. fprintf(stderr, "%s: io_uring_enter %d\n", __FUNCTION__, ret);
  78. goto err;
  79. }
  80. ret = io_uring_wait_cqe(ring, &cqe);
  81. if (ret < 0) {
  82. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  83. goto err;
  84. }
  85. ret = cqe->res;
  86. io_uring_cqe_seen(ring, cqe);
  87. if (ret == -EINVAL) {
  88. fprintf(stdout, "Timeout not supported, ignored\n");
  89. not_supported = 1;
  90. return 0;
  91. } else if (ret != -ETIME) {
  92. fprintf(stderr, "Timeout: %s\n", strerror(-ret));
  93. goto err;
  94. }
  95. exp = mtime_since_now(&tv);
  96. if (exp >= TIMEOUT_MSEC / 2 && exp <= (TIMEOUT_MSEC * 3) / 2)
  97. return 0;
  98. fprintf(stderr, "%s: Timeout seems wonky (got %llu)\n", __FUNCTION__, exp);
  99. err:
  100. return 1;
  101. }
  102. /*
  103. * Test numbered trigger of timeout
  104. */
  105. static int test_single_timeout_nr(struct io_uring *ring, int nr)
  106. {
  107. struct io_uring_cqe *cqe;
  108. struct io_uring_sqe *sqe;
  109. struct __kernel_timespec ts;
  110. int i, ret;
  111. sqe = io_uring_get_sqe(ring);
  112. if (!sqe) {
  113. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  114. goto err;
  115. }
  116. msec_to_ts(&ts, TIMEOUT_MSEC);
  117. io_uring_prep_timeout(sqe, &ts, nr, 0);
  118. sqe = io_uring_get_sqe(ring);
  119. io_uring_prep_nop(sqe);
  120. io_uring_sqe_set_data(sqe, (void *) 1);
  121. sqe = io_uring_get_sqe(ring);
  122. io_uring_prep_nop(sqe);
  123. io_uring_sqe_set_data(sqe, (void *) 1);
  124. ret = io_uring_submit_and_wait(ring, 3);
  125. if (ret <= 0) {
  126. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  127. goto err;
  128. }
  129. i = 0;
  130. while (i < 3) {
  131. ret = io_uring_wait_cqe(ring, &cqe);
  132. if (ret < 0) {
  133. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  134. goto err;
  135. }
  136. ret = cqe->res;
  137. /*
  138. * NOP commands have user_data as 1. Check that we get the
  139. * at least 'nr' NOPs first, then the successfully removed timeout.
  140. */
  141. if (io_uring_cqe_get_data(cqe) == NULL) {
  142. if (i < nr) {
  143. fprintf(stderr, "%s: timeout received too early\n", __FUNCTION__);
  144. goto err;
  145. }
  146. if (ret) {
  147. fprintf(stderr, "%s: timeout triggered by passage of"
  148. " time, not by events completed\n", __FUNCTION__);
  149. goto err;
  150. }
  151. }
  152. io_uring_cqe_seen(ring, cqe);
  153. if (ret) {
  154. fprintf(stderr, "res: %d\n", ret);
  155. goto err;
  156. }
  157. i++;
  158. }
  159. return 0;
  160. err:
  161. return 1;
  162. }
  163. static int test_single_timeout_wait(struct io_uring *ring,
  164. struct io_uring_params *p)
  165. {
  166. struct io_uring_cqe *cqe;
  167. struct io_uring_sqe *sqe;
  168. struct __kernel_timespec ts;
  169. int i, ret;
  170. sqe = io_uring_get_sqe(ring);
  171. io_uring_prep_nop(sqe);
  172. io_uring_sqe_set_data(sqe, (void *) 1);
  173. sqe = io_uring_get_sqe(ring);
  174. io_uring_prep_nop(sqe);
  175. io_uring_sqe_set_data(sqe, (void *) 1);
  176. /* no implied submit for newer kernels */
  177. if (p->features & IORING_FEAT_EXT_ARG) {
  178. ret = io_uring_submit(ring);
  179. if (ret != 2) {
  180. fprintf(stderr, "%s: submit %d\n", __FUNCTION__, ret);
  181. return 1;
  182. }
  183. }
  184. msec_to_ts(&ts, 1000);
  185. i = 0;
  186. do {
  187. ret = io_uring_wait_cqes(ring, &cqe, 2, &ts, NULL);
  188. if (ret == -ETIME)
  189. break;
  190. if (ret < 0) {
  191. fprintf(stderr, "%s: wait timeout failed: %d\n", __FUNCTION__, ret);
  192. goto err;
  193. }
  194. ret = cqe->res;
  195. io_uring_cqe_seen(ring, cqe);
  196. if (ret < 0) {
  197. fprintf(stderr, "res: %d\n", ret);
  198. goto err;
  199. }
  200. i++;
  201. } while (1);
  202. if (i != 2) {
  203. fprintf(stderr, "got %d completions\n", i);
  204. goto err;
  205. }
  206. return 0;
  207. err:
  208. return 1;
  209. }
  210. /*
  211. * Test single timeout waking us up
  212. */
  213. static int test_single_timeout(struct io_uring *ring)
  214. {
  215. struct io_uring_cqe *cqe;
  216. struct io_uring_sqe *sqe;
  217. unsigned long long exp;
  218. struct __kernel_timespec ts;
  219. struct timeval tv;
  220. int ret;
  221. sqe = io_uring_get_sqe(ring);
  222. if (!sqe) {
  223. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  224. goto err;
  225. }
  226. msec_to_ts(&ts, TIMEOUT_MSEC);
  227. io_uring_prep_timeout(sqe, &ts, 0, 0);
  228. ret = io_uring_submit(ring);
  229. if (ret <= 0) {
  230. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  231. goto err;
  232. }
  233. gettimeofday(&tv, NULL);
  234. ret = io_uring_wait_cqe(ring, &cqe);
  235. if (ret < 0) {
  236. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  237. goto err;
  238. }
  239. ret = cqe->res;
  240. io_uring_cqe_seen(ring, cqe);
  241. if (ret == -EINVAL) {
  242. fprintf(stdout, "%s: Timeout not supported, ignored\n", __FUNCTION__);
  243. not_supported = 1;
  244. return 0;
  245. } else if (ret != -ETIME) {
  246. fprintf(stderr, "%s: Timeout: %s\n", __FUNCTION__, strerror(-ret));
  247. goto err;
  248. }
  249. exp = mtime_since_now(&tv);
  250. if (exp >= TIMEOUT_MSEC / 2 && exp <= (TIMEOUT_MSEC * 3) / 2)
  251. return 0;
  252. fprintf(stderr, "%s: Timeout seems wonky (got %llu)\n", __FUNCTION__, exp);
  253. err:
  254. return 1;
  255. }
  256. static int test_single_timeout_remove_notfound(struct io_uring *ring)
  257. {
  258. struct io_uring_cqe *cqe;
  259. struct io_uring_sqe *sqe;
  260. struct __kernel_timespec ts;
  261. int ret, i;
  262. if (no_modify)
  263. return 0;
  264. sqe = io_uring_get_sqe(ring);
  265. if (!sqe) {
  266. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  267. goto err;
  268. }
  269. msec_to_ts(&ts, TIMEOUT_MSEC);
  270. io_uring_prep_timeout(sqe, &ts, 2, 0);
  271. sqe->user_data = 1;
  272. ret = io_uring_submit(ring);
  273. if (ret <= 0) {
  274. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  275. goto err;
  276. }
  277. sqe = io_uring_get_sqe(ring);
  278. if (!sqe) {
  279. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  280. goto err;
  281. }
  282. io_uring_prep_timeout_remove(sqe, 2, 0);
  283. sqe->user_data = 2;
  284. ret = io_uring_submit(ring);
  285. if (ret <= 0) {
  286. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  287. goto err;
  288. }
  289. /*
  290. * We should get two completions. One is our modify request, which should
  291. * complete with -ENOENT. The other is the timeout that will trigger after
  292. * TIMEOUT_MSEC.
  293. */
  294. for (i = 0; i < 2; i++) {
  295. ret = io_uring_wait_cqe(ring, &cqe);
  296. if (ret < 0) {
  297. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  298. goto err;
  299. }
  300. if (cqe->user_data == 2) {
  301. if (cqe->res != -ENOENT) {
  302. fprintf(stderr, "%s: modify ret %d, wanted ENOENT\n", __FUNCTION__, cqe->res);
  303. break;
  304. }
  305. } else if (cqe->user_data == 1) {
  306. if (cqe->res != -ETIME) {
  307. fprintf(stderr, "%s: timeout ret %d, wanted -ETIME\n", __FUNCTION__, cqe->res);
  308. break;
  309. }
  310. }
  311. io_uring_cqe_seen(ring, cqe);
  312. }
  313. return 0;
  314. err:
  315. return 1;
  316. }
  317. static int test_single_timeout_remove(struct io_uring *ring)
  318. {
  319. struct io_uring_cqe *cqe;
  320. struct io_uring_sqe *sqe;
  321. struct __kernel_timespec ts;
  322. int ret, i;
  323. sqe = io_uring_get_sqe(ring);
  324. if (!sqe) {
  325. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  326. goto err;
  327. }
  328. msec_to_ts(&ts, TIMEOUT_MSEC);
  329. io_uring_prep_timeout(sqe, &ts, 0, 0);
  330. sqe->user_data = 1;
  331. ret = io_uring_submit(ring);
  332. if (ret <= 0) {
  333. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  334. goto err;
  335. }
  336. sqe = io_uring_get_sqe(ring);
  337. if (!sqe) {
  338. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  339. goto err;
  340. }
  341. io_uring_prep_timeout_remove(sqe, 1, 0);
  342. sqe->user_data = 2;
  343. ret = io_uring_submit(ring);
  344. if (ret <= 0) {
  345. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  346. goto err;
  347. }
  348. /*
  349. * We should have two completions ready. One is for the original timeout
  350. * request, user_data == 1, that should have a ret of -ECANCELED. The other
  351. * is for our modify request, user_data == 2, that should have a ret of 0.
  352. */
  353. for (i = 0; i < 2; i++) {
  354. ret = io_uring_wait_cqe(ring, &cqe);
  355. if (ret < 0) {
  356. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  357. goto err;
  358. }
  359. if (no_modify)
  360. goto seen;
  361. if (cqe->res == -EINVAL && cqe->user_data == 2) {
  362. fprintf(stdout, "Timeout modify not supported, ignoring\n");
  363. no_modify = 1;
  364. goto seen;
  365. }
  366. if (cqe->user_data == 1) {
  367. if (cqe->res != -ECANCELED) {
  368. fprintf(stderr, "%s: timeout ret %d, wanted canceled\n", __FUNCTION__, cqe->res);
  369. break;
  370. }
  371. } else if (cqe->user_data == 2) {
  372. if (cqe->res) {
  373. fprintf(stderr, "%s: modify ret %d, wanted 0\n", __FUNCTION__, cqe->res);
  374. break;
  375. }
  376. }
  377. seen:
  378. io_uring_cqe_seen(ring, cqe);
  379. }
  380. return 0;
  381. err:
  382. return 1;
  383. }
  384. /*
  385. * Test single absolute timeout waking us up
  386. */
  387. static int test_single_timeout_abs(struct io_uring *ring)
  388. {
  389. struct io_uring_cqe *cqe;
  390. struct io_uring_sqe *sqe;
  391. unsigned long long exp;
  392. struct __kernel_timespec ts;
  393. struct timespec abs_ts;
  394. struct timeval tv;
  395. int ret;
  396. sqe = io_uring_get_sqe(ring);
  397. if (!sqe) {
  398. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  399. goto err;
  400. }
  401. clock_gettime(CLOCK_MONOTONIC, &abs_ts);
  402. ts.tv_sec = abs_ts.tv_sec + 1;
  403. ts.tv_nsec = abs_ts.tv_nsec;
  404. io_uring_prep_timeout(sqe, &ts, 0, IORING_TIMEOUT_ABS);
  405. ret = io_uring_submit(ring);
  406. if (ret <= 0) {
  407. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  408. goto err;
  409. }
  410. gettimeofday(&tv, NULL);
  411. ret = io_uring_wait_cqe(ring, &cqe);
  412. if (ret < 0) {
  413. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  414. goto err;
  415. }
  416. ret = cqe->res;
  417. io_uring_cqe_seen(ring, cqe);
  418. if (ret == -EINVAL) {
  419. fprintf(stdout, "Absolute timeouts not supported, ignored\n");
  420. return 0;
  421. } else if (ret != -ETIME) {
  422. fprintf(stderr, "Timeout: %s\n", strerror(-ret));
  423. goto err;
  424. }
  425. exp = mtime_since_now(&tv);
  426. if (exp >= 1000 / 2 && exp <= (1000 * 3) / 2)
  427. return 0;
  428. fprintf(stderr, "%s: Timeout seems wonky (got %llu)\n", __FUNCTION__, exp);
  429. err:
  430. return 1;
  431. }
  432. /*
  433. * Test that timeout is canceled on exit
  434. */
  435. static int test_single_timeout_exit(struct io_uring *ring)
  436. {
  437. struct io_uring_sqe *sqe;
  438. struct __kernel_timespec ts;
  439. int ret;
  440. sqe = io_uring_get_sqe(ring);
  441. if (!sqe) {
  442. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  443. goto err;
  444. }
  445. msec_to_ts(&ts, 30000);
  446. io_uring_prep_timeout(sqe, &ts, 0, 0);
  447. ret = io_uring_submit(ring);
  448. if (ret <= 0) {
  449. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  450. goto err;
  451. }
  452. io_uring_queue_exit(ring);
  453. return 0;
  454. err:
  455. io_uring_queue_exit(ring);
  456. return 1;
  457. }
  458. /*
  459. * Test multi timeouts waking us up
  460. */
  461. static int test_multi_timeout(struct io_uring *ring)
  462. {
  463. struct io_uring_sqe *sqe;
  464. struct io_uring_cqe *cqe;
  465. struct __kernel_timespec ts[2];
  466. unsigned int timeout[2];
  467. unsigned long long exp;
  468. struct timeval tv;
  469. int ret, i;
  470. /* req_1: timeout req, count = 1, time = (TIMEOUT_MSEC * 2) */
  471. timeout[0] = TIMEOUT_MSEC * 2;
  472. msec_to_ts(&ts[0], timeout[0]);
  473. sqe = io_uring_get_sqe(ring);
  474. if (!sqe) {
  475. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  476. goto err;
  477. }
  478. io_uring_prep_timeout(sqe, &ts[0], 1, 0);
  479. sqe->user_data = 1;
  480. /* req_2: timeout req, count = 1, time = TIMEOUT_MSEC */
  481. timeout[1] = TIMEOUT_MSEC;
  482. msec_to_ts(&ts[1], timeout[1]);
  483. sqe = io_uring_get_sqe(ring);
  484. if (!sqe) {
  485. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  486. goto err;
  487. }
  488. io_uring_prep_timeout(sqe, &ts[1], 1, 0);
  489. sqe->user_data = 2;
  490. ret = io_uring_submit(ring);
  491. if (ret <= 0) {
  492. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  493. goto err;
  494. }
  495. gettimeofday(&tv, NULL);
  496. for (i = 0; i < 2; i++) {
  497. unsigned int time = 0;
  498. __u64 user_data = 0;
  499. ret = io_uring_wait_cqe(ring, &cqe);
  500. if (ret < 0) {
  501. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  502. goto err;
  503. }
  504. /*
  505. * Both of these two reqs should timeout, but req_2 should
  506. * return before req_1.
  507. */
  508. switch (i) {
  509. case 0:
  510. user_data = 2;
  511. time = timeout[1];
  512. break;
  513. case 1:
  514. user_data = 1;
  515. time = timeout[0];
  516. break;
  517. }
  518. if (cqe->user_data != user_data) {
  519. fprintf(stderr, "%s: unexpected timeout req %d sequence\n",
  520. __FUNCTION__, i+1);
  521. goto err;
  522. }
  523. if (cqe->res != -ETIME) {
  524. fprintf(stderr, "%s: Req %d timeout: %s\n",
  525. __FUNCTION__, i+1, strerror(cqe->res));
  526. goto err;
  527. }
  528. exp = mtime_since_now(&tv);
  529. if (exp < time / 2 || exp > (time * 3) / 2) {
  530. fprintf(stderr, "%s: Req %d timeout seems wonky (got %llu)\n",
  531. __FUNCTION__, i+1, exp);
  532. goto err;
  533. }
  534. io_uring_cqe_seen(ring, cqe);
  535. }
  536. return 0;
  537. err:
  538. return 1;
  539. }
  540. /*
  541. * Test multi timeout req with different count
  542. */
  543. static int test_multi_timeout_nr(struct io_uring *ring)
  544. {
  545. struct io_uring_sqe *sqe;
  546. struct io_uring_cqe *cqe;
  547. struct __kernel_timespec ts;
  548. int ret, i;
  549. msec_to_ts(&ts, TIMEOUT_MSEC);
  550. /* req_1: timeout req, count = 2 */
  551. sqe = io_uring_get_sqe(ring);
  552. if (!sqe) {
  553. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  554. goto err;
  555. }
  556. io_uring_prep_timeout(sqe, &ts, 2, 0);
  557. sqe->user_data = 1;
  558. /* req_2: timeout req, count = 1 */
  559. sqe = io_uring_get_sqe(ring);
  560. if (!sqe) {
  561. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  562. goto err;
  563. }
  564. io_uring_prep_timeout(sqe, &ts, 1, 0);
  565. sqe->user_data = 2;
  566. /* req_3: nop req */
  567. sqe = io_uring_get_sqe(ring);
  568. if (!sqe) {
  569. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  570. goto err;
  571. }
  572. io_uring_prep_nop(sqe);
  573. io_uring_sqe_set_data(sqe, (void *) 1);
  574. ret = io_uring_submit(ring);
  575. if (ret <= 0) {
  576. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  577. goto err;
  578. }
  579. /*
  580. * req_2 (count=1) should return without error and req_1 (count=2)
  581. * should timeout.
  582. */
  583. for (i = 0; i < 3; i++) {
  584. ret = io_uring_wait_cqe(ring, &cqe);
  585. if (ret < 0) {
  586. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  587. goto err;
  588. }
  589. switch (i) {
  590. case 0:
  591. /* Should be nop req */
  592. if (io_uring_cqe_get_data(cqe) != (void *) 1) {
  593. fprintf(stderr, "%s: nop not seen as 1 or 2\n", __FUNCTION__);
  594. goto err;
  595. }
  596. break;
  597. case 1:
  598. /* Should be timeout req_2 */
  599. if (cqe->user_data != 2) {
  600. fprintf(stderr, "%s: unexpected timeout req %d sequence\n",
  601. __FUNCTION__, i+1);
  602. goto err;
  603. }
  604. if (cqe->res < 0) {
  605. fprintf(stderr, "%s: Req %d res %d\n",
  606. __FUNCTION__, i+1, cqe->res);
  607. goto err;
  608. }
  609. break;
  610. case 2:
  611. /* Should be timeout req_1 */
  612. if (cqe->user_data != 1) {
  613. fprintf(stderr, "%s: unexpected timeout req %d sequence\n",
  614. __FUNCTION__, i+1);
  615. goto err;
  616. }
  617. if (cqe->res != -ETIME) {
  618. fprintf(stderr, "%s: Req %d timeout: %s\n",
  619. __FUNCTION__, i+1, strerror(cqe->res));
  620. goto err;
  621. }
  622. break;
  623. }
  624. io_uring_cqe_seen(ring, cqe);
  625. }
  626. return 0;
  627. err:
  628. return 1;
  629. }
  630. /*
  631. * Test timeout <link> timeout <drain> timeout
  632. */
  633. static int test_timeout_flags1(struct io_uring *ring)
  634. {
  635. struct io_uring_sqe *sqe;
  636. struct io_uring_cqe *cqe;
  637. struct __kernel_timespec ts;
  638. int ret, i;
  639. msec_to_ts(&ts, TIMEOUT_MSEC);
  640. sqe = io_uring_get_sqe(ring);
  641. if (!sqe) {
  642. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  643. goto err;
  644. }
  645. io_uring_prep_timeout(sqe, &ts, 0, 0);
  646. sqe->user_data = 1;
  647. sqe->flags |= IOSQE_IO_LINK;
  648. sqe = io_uring_get_sqe(ring);
  649. if (!sqe) {
  650. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  651. goto err;
  652. }
  653. io_uring_prep_timeout(sqe, &ts, 0, 0);
  654. sqe->user_data = 2;
  655. sqe->flags |= IOSQE_IO_DRAIN;
  656. sqe = io_uring_get_sqe(ring);
  657. if (!sqe) {
  658. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  659. goto err;
  660. }
  661. io_uring_prep_timeout(sqe, &ts, 0, 0);
  662. sqe->user_data = 3;
  663. ret = io_uring_submit(ring);
  664. if (ret <= 0) {
  665. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  666. goto err;
  667. }
  668. for (i = 0; i < 3; i++) {
  669. ret = io_uring_wait_cqe(ring, &cqe);
  670. if (ret < 0) {
  671. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  672. goto err;
  673. }
  674. if (cqe->res == -EINVAL) {
  675. if (!i)
  676. fprintf(stdout, "%s: timeout flags not supported\n",
  677. __FUNCTION__);
  678. io_uring_cqe_seen(ring, cqe);
  679. continue;
  680. }
  681. switch (cqe->user_data) {
  682. case 1:
  683. if (cqe->res != -ETIME) {
  684. fprintf(stderr, "%s: got %d, wanted %d\n",
  685. __FUNCTION__, cqe->res, -ETIME);
  686. goto err;
  687. }
  688. break;
  689. case 2:
  690. if (cqe->res != -ECANCELED) {
  691. fprintf(stderr, "%s: got %d, wanted %d\n",
  692. __FUNCTION__, cqe->res,
  693. -ECANCELED);
  694. goto err;
  695. }
  696. break;
  697. case 3:
  698. if (cqe->res != -ETIME) {
  699. fprintf(stderr, "%s: got %d, wanted %d\n",
  700. __FUNCTION__, cqe->res, -ETIME);
  701. goto err;
  702. }
  703. break;
  704. }
  705. io_uring_cqe_seen(ring, cqe);
  706. }
  707. return 0;
  708. err:
  709. return 1;
  710. }
  711. /*
  712. * Test timeout <link> timeout <link> timeout
  713. */
  714. static int test_timeout_flags2(struct io_uring *ring)
  715. {
  716. struct io_uring_sqe *sqe;
  717. struct io_uring_cqe *cqe;
  718. struct __kernel_timespec ts;
  719. int ret, i;
  720. msec_to_ts(&ts, TIMEOUT_MSEC);
  721. sqe = io_uring_get_sqe(ring);
  722. if (!sqe) {
  723. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  724. goto err;
  725. }
  726. io_uring_prep_timeout(sqe, &ts, 0, 0);
  727. sqe->user_data = 1;
  728. sqe->flags |= IOSQE_IO_LINK;
  729. sqe = io_uring_get_sqe(ring);
  730. if (!sqe) {
  731. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  732. goto err;
  733. }
  734. io_uring_prep_timeout(sqe, &ts, 0, 0);
  735. sqe->user_data = 2;
  736. sqe->flags |= IOSQE_IO_LINK;
  737. sqe = io_uring_get_sqe(ring);
  738. if (!sqe) {
  739. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  740. goto err;
  741. }
  742. io_uring_prep_timeout(sqe, &ts, 0, 0);
  743. sqe->user_data = 3;
  744. ret = io_uring_submit(ring);
  745. if (ret <= 0) {
  746. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  747. goto err;
  748. }
  749. for (i = 0; i < 3; i++) {
  750. ret = io_uring_wait_cqe(ring, &cqe);
  751. if (ret < 0) {
  752. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  753. goto err;
  754. }
  755. if (cqe->res == -EINVAL) {
  756. if (!i)
  757. fprintf(stdout, "%s: timeout flags not supported\n",
  758. __FUNCTION__);
  759. io_uring_cqe_seen(ring, cqe);
  760. continue;
  761. }
  762. switch (cqe->user_data) {
  763. case 1:
  764. if (cqe->res != -ETIME) {
  765. fprintf(stderr, "%s: got %d, wanted %d\n",
  766. __FUNCTION__, cqe->res, -ETIME);
  767. goto err;
  768. }
  769. break;
  770. case 2:
  771. case 3:
  772. if (cqe->res != -ECANCELED) {
  773. fprintf(stderr, "%s: got %d, wanted %d\n",
  774. __FUNCTION__, cqe->res,
  775. -ECANCELED);
  776. goto err;
  777. }
  778. break;
  779. }
  780. io_uring_cqe_seen(ring, cqe);
  781. }
  782. return 0;
  783. err:
  784. return 1;
  785. }
  786. /*
  787. * Test timeout <drain> timeout <link> timeout
  788. */
  789. static int test_timeout_flags3(struct io_uring *ring)
  790. {
  791. struct io_uring_sqe *sqe;
  792. struct io_uring_cqe *cqe;
  793. struct __kernel_timespec ts;
  794. int ret, i;
  795. msec_to_ts(&ts, TIMEOUT_MSEC);
  796. sqe = io_uring_get_sqe(ring);
  797. if (!sqe) {
  798. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  799. goto err;
  800. }
  801. io_uring_prep_timeout(sqe, &ts, 0, 0);
  802. sqe->user_data = 1;
  803. sqe->flags |= IOSQE_IO_DRAIN;
  804. sqe = io_uring_get_sqe(ring);
  805. if (!sqe) {
  806. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  807. goto err;
  808. }
  809. io_uring_prep_timeout(sqe, &ts, 0, 0);
  810. sqe->user_data = 2;
  811. sqe->flags |= IOSQE_IO_LINK;
  812. sqe = io_uring_get_sqe(ring);
  813. if (!sqe) {
  814. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  815. goto err;
  816. }
  817. io_uring_prep_timeout(sqe, &ts, 0, 0);
  818. sqe->user_data = 3;
  819. ret = io_uring_submit(ring);
  820. if (ret <= 0) {
  821. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  822. goto err;
  823. }
  824. for (i = 0; i < 3; i++) {
  825. ret = io_uring_wait_cqe(ring, &cqe);
  826. if (ret < 0) {
  827. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  828. goto err;
  829. }
  830. if (cqe->res == -EINVAL) {
  831. if (!i)
  832. fprintf(stdout, "%s: timeout flags not supported\n",
  833. __FUNCTION__);
  834. io_uring_cqe_seen(ring, cqe);
  835. continue;
  836. }
  837. switch (cqe->user_data) {
  838. case 1:
  839. case 2:
  840. if (cqe->res != -ETIME) {
  841. fprintf(stderr, "%s: got %d, wanted %d\n",
  842. __FUNCTION__, cqe->res, -ETIME);
  843. goto err;
  844. }
  845. break;
  846. case 3:
  847. if (cqe->res != -ECANCELED) {
  848. fprintf(stderr, "%s: got %d, wanted %d\n",
  849. __FUNCTION__, cqe->res,
  850. -ECANCELED);
  851. goto err;
  852. }
  853. break;
  854. }
  855. io_uring_cqe_seen(ring, cqe);
  856. }
  857. return 0;
  858. err:
  859. return 1;
  860. }
  861. static int test_update_timeout(struct io_uring *ring, unsigned long ms,
  862. bool abs, bool async, bool linked)
  863. {
  864. struct io_uring_sqe *sqe;
  865. struct io_uring_cqe *cqe;
  866. struct __kernel_timespec ts, ts_upd;
  867. unsigned long long exp_ms, base_ms = 10000;
  868. struct timeval tv;
  869. int ret, i, nr = 2;
  870. __u32 mode = abs ? IORING_TIMEOUT_ABS : 0;
  871. msec_to_ts(&ts_upd, ms);
  872. gettimeofday(&tv, NULL);
  873. sqe = io_uring_get_sqe(ring);
  874. if (!sqe) {
  875. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  876. goto err;
  877. }
  878. msec_to_ts(&ts, base_ms);
  879. io_uring_prep_timeout(sqe, &ts, 0, 0);
  880. sqe->user_data = 1;
  881. if (linked) {
  882. sqe = io_uring_get_sqe(ring);
  883. if (!sqe) {
  884. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  885. goto err;
  886. }
  887. io_uring_prep_nop(sqe);
  888. sqe->user_data = 3;
  889. sqe->flags = IOSQE_IO_LINK;
  890. if (async)
  891. sqe->flags |= IOSQE_ASYNC;
  892. nr++;
  893. }
  894. sqe = io_uring_get_sqe(ring);
  895. if (!sqe) {
  896. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  897. goto err;
  898. }
  899. io_uring_prep_timeout_update(sqe, &ts_upd, 1, mode);
  900. sqe->user_data = 2;
  901. if (async)
  902. sqe->flags |= IOSQE_ASYNC;
  903. ret = io_uring_submit(ring);
  904. if (ret != nr) {
  905. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  906. goto err;
  907. }
  908. for (i = 0; i < nr; i++) {
  909. ret = io_uring_wait_cqe(ring, &cqe);
  910. if (ret < 0) {
  911. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  912. goto err;
  913. }
  914. switch (cqe->user_data) {
  915. case 1:
  916. if (cqe->res != -ETIME) {
  917. fprintf(stderr, "%s: got %d, wanted %d\n",
  918. __FUNCTION__, cqe->res, -ETIME);
  919. goto err;
  920. }
  921. break;
  922. case 2:
  923. if (cqe->res != 0) {
  924. fprintf(stderr, "%s: got %d, wanted %d\n",
  925. __FUNCTION__, cqe->res,
  926. 0);
  927. goto err;
  928. }
  929. break;
  930. case 3:
  931. if (cqe->res != 0) {
  932. fprintf(stderr, "nop failed\n");
  933. goto err;
  934. }
  935. break;
  936. default:
  937. goto err;
  938. }
  939. io_uring_cqe_seen(ring, cqe);
  940. }
  941. exp_ms = mtime_since_now(&tv);
  942. if (exp_ms >= base_ms / 2) {
  943. fprintf(stderr, "too long, timeout wasn't updated\n");
  944. goto err;
  945. }
  946. if (ms >= 1000 && !abs && exp_ms < ms / 2) {
  947. fprintf(stderr, "fired too early, potentially updated to 0 ms"
  948. "instead of %lu\n", ms);
  949. goto err;
  950. }
  951. return 0;
  952. err:
  953. return 1;
  954. }
  955. static int test_update_nonexistent_timeout(struct io_uring *ring)
  956. {
  957. struct io_uring_sqe *sqe;
  958. struct io_uring_cqe *cqe;
  959. struct __kernel_timespec ts;
  960. int ret;
  961. sqe = io_uring_get_sqe(ring);
  962. if (!sqe) {
  963. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  964. goto err;
  965. }
  966. msec_to_ts(&ts, 0);
  967. io_uring_prep_timeout_update(sqe, &ts, 42, 0);
  968. ret = io_uring_submit(ring);
  969. if (ret != 1) {
  970. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  971. goto err;
  972. }
  973. ret = io_uring_wait_cqe(ring, &cqe);
  974. if (ret < 0) {
  975. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  976. goto err;
  977. }
  978. ret = cqe->res;
  979. if (ret == -ENOENT)
  980. ret = 0;
  981. io_uring_cqe_seen(ring, cqe);
  982. return ret;
  983. err:
  984. return 1;
  985. }
  986. static int test_update_invalid_flags(struct io_uring *ring)
  987. {
  988. struct io_uring_sqe *sqe;
  989. struct io_uring_cqe *cqe;
  990. struct __kernel_timespec ts;
  991. int ret;
  992. sqe = io_uring_get_sqe(ring);
  993. if (!sqe) {
  994. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  995. goto err;
  996. }
  997. io_uring_prep_timeout_remove(sqe, 0, IORING_TIMEOUT_ABS);
  998. ret = io_uring_submit(ring);
  999. if (ret != 1) {
  1000. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  1001. goto err;
  1002. }
  1003. ret = io_uring_wait_cqe(ring, &cqe);
  1004. if (ret < 0) {
  1005. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1006. goto err;
  1007. }
  1008. if (cqe->res != -EINVAL) {
  1009. fprintf(stderr, "%s: got %d, wanted %d\n",
  1010. __FUNCTION__, cqe->res, -EINVAL);
  1011. goto err;
  1012. }
  1013. io_uring_cqe_seen(ring, cqe);
  1014. sqe = io_uring_get_sqe(ring);
  1015. if (!sqe) {
  1016. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  1017. goto err;
  1018. }
  1019. msec_to_ts(&ts, 0);
  1020. io_uring_prep_timeout_update(sqe, &ts, 0, -1);
  1021. ret = io_uring_submit(ring);
  1022. if (ret != 1) {
  1023. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  1024. goto err;
  1025. }
  1026. ret = io_uring_wait_cqe(ring, &cqe);
  1027. if (ret < 0) {
  1028. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1029. goto err;
  1030. }
  1031. if (cqe->res != -EINVAL) {
  1032. fprintf(stderr, "%s: got %d, wanted %d\n",
  1033. __FUNCTION__, cqe->res, -EINVAL);
  1034. goto err;
  1035. }
  1036. io_uring_cqe_seen(ring, cqe);
  1037. return 0;
  1038. err:
  1039. return 1;
  1040. }
  1041. static int fill_exec_target(char *dst, char *path)
  1042. {
  1043. struct stat sb;
  1044. /*
  1045. * Should either be ./exec-target.t or test/exec-target.t
  1046. */
  1047. sprintf(dst, "%s", path);
  1048. return stat(dst, &sb);
  1049. }
  1050. static int test_timeout_link_cancel(void)
  1051. {
  1052. struct io_uring ring;
  1053. struct io_uring_cqe *cqe;
  1054. char prog_path[PATH_MAX];
  1055. pid_t p;
  1056. int ret, i, wstatus;
  1057. if (fill_exec_target(prog_path, "./exec-target.t") &&
  1058. fill_exec_target(prog_path, "test/exec-target.t")) {
  1059. fprintf(stdout, "Can't find exec-target, skipping\n");
  1060. return 0;
  1061. }
  1062. ret = io_uring_queue_init(8, &ring, 0);
  1063. if (ret) {
  1064. fprintf(stderr, "ring create failed: %d\n", ret);
  1065. return 1;
  1066. }
  1067. p = fork();
  1068. if (p == -1) {
  1069. fprintf(stderr, "fork() failed\n");
  1070. return 1;
  1071. }
  1072. if (p == 0) {
  1073. struct io_uring_sqe *sqe;
  1074. struct __kernel_timespec ts;
  1075. msec_to_ts(&ts, 10000);
  1076. sqe = io_uring_get_sqe(&ring);
  1077. io_uring_prep_timeout(sqe, &ts, 0, 0);
  1078. sqe->flags |= IOSQE_IO_LINK;
  1079. sqe->user_data = 0;
  1080. sqe = io_uring_get_sqe(&ring);
  1081. io_uring_prep_nop(sqe);
  1082. sqe->user_data = 1;
  1083. ret = io_uring_submit(&ring);
  1084. if (ret != 2) {
  1085. fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
  1086. exit(1);
  1087. }
  1088. /* trigger full cancellation */
  1089. ret = execl(prog_path, prog_path, NULL);
  1090. if (ret) {
  1091. fprintf(stderr, "exec failed %i\n", errno);
  1092. exit(1);
  1093. }
  1094. exit(0);
  1095. }
  1096. if (waitpid(p, &wstatus, 0) == (pid_t)-1) {
  1097. perror("waitpid()");
  1098. return 1;
  1099. }
  1100. if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus)) {
  1101. fprintf(stderr, "child failed %i\n", WEXITSTATUS(wstatus));
  1102. return 1;
  1103. }
  1104. for (i = 0; i < 2; ++i) {
  1105. ret = io_uring_wait_cqe(&ring, &cqe);
  1106. if (ret) {
  1107. fprintf(stderr, "wait_cqe=%d\n", ret);
  1108. return 1;
  1109. }
  1110. if (cqe->res != -ECANCELED) {
  1111. fprintf(stderr, "invalid result, user_data: %i res: %i\n",
  1112. (int)cqe->user_data, cqe->res);
  1113. return 1;
  1114. }
  1115. io_uring_cqe_seen(&ring, cqe);
  1116. }
  1117. io_uring_queue_exit(&ring);
  1118. return 0;
  1119. }
  1120. static int test_not_failing_links(void)
  1121. {
  1122. struct io_uring ring;
  1123. struct io_uring_sqe *sqe;
  1124. struct io_uring_cqe *cqe;
  1125. struct __kernel_timespec ts;
  1126. int ret;
  1127. ret = io_uring_queue_init(8, &ring, 0);
  1128. if (ret) {
  1129. fprintf(stderr, "ring create failed: %d\n", ret);
  1130. return 1;
  1131. }
  1132. msec_to_ts(&ts, 1);
  1133. sqe = io_uring_get_sqe(&ring);
  1134. io_uring_prep_timeout(sqe, &ts, 0, IORING_TIMEOUT_ETIME_SUCCESS);
  1135. sqe->user_data = 1;
  1136. sqe->flags |= IOSQE_IO_LINK;
  1137. sqe = io_uring_get_sqe(&ring);
  1138. io_uring_prep_nop(sqe);
  1139. sqe->user_data = 2;
  1140. ret = io_uring_submit(&ring);
  1141. if (ret != 2) {
  1142. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  1143. return 1;
  1144. }
  1145. ret = io_uring_wait_cqe(&ring, &cqe);
  1146. if (ret < 0) {
  1147. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1148. return 1;
  1149. } else if (cqe->user_data == 1 && cqe->res == -EINVAL) {
  1150. goto done;
  1151. } else if (cqe->res != -ETIME || cqe->user_data != 1) {
  1152. fprintf(stderr, "timeout failed %i %i\n", cqe->res,
  1153. (int)cqe->user_data);
  1154. return 1;
  1155. }
  1156. io_uring_cqe_seen(&ring, cqe);
  1157. ret = io_uring_wait_cqe(&ring, &cqe);
  1158. if (ret < 0) {
  1159. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1160. return 1;
  1161. } else if (cqe->res || cqe->user_data != 2) {
  1162. fprintf(stderr, "nop failed %i %i\n", cqe->res,
  1163. (int)cqe->user_data);
  1164. return 1;
  1165. }
  1166. done:
  1167. io_uring_cqe_seen(&ring, cqe);
  1168. io_uring_queue_exit(&ring);
  1169. return 0;
  1170. }
  1171. static int test_timeout_multishot(struct io_uring *ring)
  1172. {
  1173. struct io_uring_cqe *cqe;
  1174. struct io_uring_sqe *sqe;
  1175. struct __kernel_timespec ts;
  1176. int ret;
  1177. sqe = io_uring_get_sqe(ring);
  1178. if (!sqe) {
  1179. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  1180. goto err;
  1181. }
  1182. msec_to_ts(&ts, TIMEOUT_MSEC);
  1183. io_uring_prep_timeout(sqe, &ts, 0, IORING_TIMEOUT_MULTISHOT);
  1184. io_uring_sqe_set_data(sqe, (void *) 1);
  1185. ret = io_uring_submit(ring);
  1186. if (ret <= 0) {
  1187. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  1188. goto err;
  1189. }
  1190. for (int i = 0; i < 2; i++) {
  1191. ret = io_uring_wait_cqe(ring, &cqe);
  1192. if (ret < 0) {
  1193. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1194. goto err;
  1195. }
  1196. ret = cqe->res;
  1197. if (ret == -EINVAL) {
  1198. no_multishot = 1;
  1199. return T_EXIT_SKIP;
  1200. }
  1201. if (!(cqe->flags & IORING_CQE_F_MORE)) {
  1202. fprintf(stderr, "%s: flag not set in cqe\n", __FUNCTION__);
  1203. goto err;
  1204. }
  1205. if (ret != -ETIME) {
  1206. fprintf(stderr, "%s: Timeout: %s\n", __FUNCTION__, strerror(-ret));
  1207. goto err;
  1208. }
  1209. io_uring_cqe_seen(ring, cqe);
  1210. }
  1211. sqe = io_uring_get_sqe(ring);
  1212. if (!sqe) {
  1213. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  1214. goto err;
  1215. }
  1216. io_uring_prep_timeout_remove(sqe, 1, 0);
  1217. io_uring_sqe_set_data(sqe, (void *) 2);
  1218. ret = io_uring_submit(ring);
  1219. if (ret <= 0) {
  1220. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  1221. goto err;
  1222. }
  1223. ret = io_uring_wait_cqe(ring, &cqe);
  1224. if (ret < 0) {
  1225. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1226. goto err;
  1227. }
  1228. ret = cqe->res;
  1229. if (ret < 0) {
  1230. fprintf(stderr, "%s: remove failed: %s\n", __FUNCTION__, strerror(-ret));
  1231. goto err;
  1232. }
  1233. io_uring_cqe_seen(ring, cqe);
  1234. ret = io_uring_wait_cqe(ring, &cqe);
  1235. if (ret < 0) {
  1236. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1237. goto err;
  1238. }
  1239. ret = cqe->res;
  1240. if (ret != -ECANCELED) {
  1241. fprintf(stderr, "%s: timeout canceled: %s %llu\n", __FUNCTION__, strerror(-ret), cqe->user_data);
  1242. goto err;
  1243. }
  1244. io_uring_cqe_seen(ring, cqe);
  1245. return 0;
  1246. err:
  1247. return 1;
  1248. }
  1249. static int test_timeout_multishot_nr(struct io_uring *ring)
  1250. {
  1251. struct io_uring_cqe *cqe;
  1252. struct io_uring_sqe *sqe;
  1253. struct __kernel_timespec ts;
  1254. int ret;
  1255. if (no_multishot)
  1256. return T_EXIT_SKIP;
  1257. sqe = io_uring_get_sqe(ring);
  1258. if (!sqe) {
  1259. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  1260. goto err;
  1261. }
  1262. msec_to_ts(&ts, TIMEOUT_MSEC);
  1263. io_uring_prep_timeout(sqe, &ts, 3, IORING_TIMEOUT_MULTISHOT);
  1264. io_uring_sqe_set_data(sqe, (void *) 1);
  1265. ret = io_uring_submit(ring);
  1266. if (ret <= 0) {
  1267. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  1268. goto err;
  1269. }
  1270. for (int i = 0; i < 3; i++) {
  1271. ret = io_uring_wait_cqe(ring, &cqe);
  1272. if (ret < 0) {
  1273. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1274. goto err;
  1275. }
  1276. if (i < 2 && !(cqe->flags & IORING_CQE_F_MORE)) {
  1277. fprintf(stderr, "%s: flag not set in cqe\n", __FUNCTION__);
  1278. goto err;
  1279. }
  1280. if (i == 3 && (cqe->flags & IORING_CQE_F_MORE)) {
  1281. fprintf(stderr, "%s: flag set in cqe\n", __FUNCTION__);
  1282. goto err;
  1283. }
  1284. ret = cqe->res;
  1285. if (ret != -ETIME) {
  1286. fprintf(stderr, "%s: Timeout: %s\n", __FUNCTION__, strerror(-ret));
  1287. goto err;
  1288. }
  1289. io_uring_cqe_seen(ring, cqe);
  1290. }
  1291. msec_to_ts(&ts, 2 * TIMEOUT_MSEC);
  1292. ret = io_uring_wait_cqe_timeout(ring, &cqe, &ts);
  1293. if (ret != -ETIME) {
  1294. fprintf(stderr, "%s: wait completion timeout %s\n", __FUNCTION__, strerror(-ret));
  1295. goto err;
  1296. }
  1297. return 0;
  1298. err:
  1299. return 1;
  1300. }
  1301. static int test_timeout_multishot_overflow(struct io_uring *ring)
  1302. {
  1303. struct io_uring_cqe *cqe;
  1304. struct io_uring_sqe *sqe;
  1305. struct __kernel_timespec ts;
  1306. int ret;
  1307. if (no_multishot)
  1308. return T_EXIT_SKIP;
  1309. sqe = io_uring_get_sqe(ring);
  1310. if (!sqe) {
  1311. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  1312. goto err;
  1313. }
  1314. msec_to_ts(&ts, 10);
  1315. io_uring_prep_timeout(sqe, &ts, 0, IORING_TIMEOUT_MULTISHOT);
  1316. io_uring_sqe_set_data(sqe, (void *) 1);
  1317. ret = io_uring_submit(ring);
  1318. if (ret <= 0) {
  1319. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  1320. goto err;
  1321. }
  1322. ret = io_uring_wait_cqe(ring, &cqe);
  1323. if (ret < 0) {
  1324. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1325. goto err;
  1326. }
  1327. ret = cqe->res;
  1328. if (ret != -ETIME) {
  1329. fprintf(stderr, "%s: Timeout: %s\n", __FUNCTION__, strerror(-ret));
  1330. goto err;
  1331. }
  1332. io_uring_cqe_seen(ring, cqe);
  1333. sleep(1);
  1334. if (!((*ring->sq.kflags) & IORING_SQ_CQ_OVERFLOW)) {
  1335. goto err;
  1336. }
  1337. /* multishot timer should be gone */
  1338. sqe = io_uring_get_sqe(ring);
  1339. if (!sqe) {
  1340. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  1341. goto err;
  1342. }
  1343. io_uring_prep_timeout_remove(sqe, 1, 0);
  1344. ret = io_uring_submit(ring);
  1345. if (ret <= 0) {
  1346. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  1347. goto err;
  1348. }
  1349. ret = io_uring_wait_cqe(ring, &cqe);
  1350. if (ret < 0) {
  1351. fprintf(stderr, "%s: wait completion %d\n", __FUNCTION__, ret);
  1352. goto err;
  1353. }
  1354. ret = cqe->res;
  1355. io_uring_cqe_seen(ring, cqe);
  1356. if (ret != -ETIME) {
  1357. fprintf(stderr, "%s: remove failed: %d %s\n", __FUNCTION__, ret, strerror(-ret));
  1358. goto err;
  1359. }
  1360. return 0;
  1361. err:
  1362. return 1;
  1363. }
  1364. int main(int argc, char *argv[])
  1365. {
  1366. struct io_uring ring, sqpoll_ring;
  1367. bool has_timeout_update, sqpoll;
  1368. struct io_uring_params p = { };
  1369. int ret;
  1370. if (argc > 1)
  1371. return 0;
  1372. ret = io_uring_queue_init_params(8, &ring, &p);
  1373. if (ret) {
  1374. fprintf(stderr, "ring setup failed\n");
  1375. return 1;
  1376. }
  1377. ret = io_uring_queue_init(8, &sqpoll_ring, IORING_SETUP_SQPOLL);
  1378. sqpoll = !ret;
  1379. ret = test_single_timeout(&ring);
  1380. if (ret) {
  1381. fprintf(stderr, "test_single_timeout failed\n");
  1382. return ret;
  1383. }
  1384. if (not_supported)
  1385. return 0;
  1386. ret = test_multi_timeout(&ring);
  1387. if (ret) {
  1388. fprintf(stderr, "test_multi_timeout failed\n");
  1389. return ret;
  1390. }
  1391. ret = test_single_timeout_abs(&ring);
  1392. if (ret) {
  1393. fprintf(stderr, "test_single_timeout_abs failed\n");
  1394. return ret;
  1395. }
  1396. ret = test_single_timeout_remove(&ring);
  1397. if (ret) {
  1398. fprintf(stderr, "test_single_timeout_remove failed\n");
  1399. return ret;
  1400. }
  1401. ret = test_single_timeout_remove_notfound(&ring);
  1402. if (ret) {
  1403. fprintf(stderr, "test_single_timeout_remove_notfound failed\n");
  1404. return ret;
  1405. }
  1406. ret = test_single_timeout_many(&ring);
  1407. if (ret) {
  1408. fprintf(stderr, "test_single_timeout_many failed\n");
  1409. return ret;
  1410. }
  1411. ret = test_single_timeout_nr(&ring, 1);
  1412. if (ret) {
  1413. fprintf(stderr, "test_single_timeout_nr(1) failed\n");
  1414. return ret;
  1415. }
  1416. ret = test_single_timeout_nr(&ring, 2);
  1417. if (ret) {
  1418. fprintf(stderr, "test_single_timeout_nr(2) failed\n");
  1419. return ret;
  1420. }
  1421. ret = test_multi_timeout_nr(&ring);
  1422. if (ret) {
  1423. fprintf(stderr, "test_multi_timeout_nr failed\n");
  1424. return ret;
  1425. }
  1426. ret = test_timeout_flags1(&ring);
  1427. if (ret) {
  1428. fprintf(stderr, "test_timeout_flags1 failed\n");
  1429. return ret;
  1430. }
  1431. ret = test_timeout_flags2(&ring);
  1432. if (ret) {
  1433. fprintf(stderr, "test_timeout_flags2 failed\n");
  1434. return ret;
  1435. }
  1436. ret = test_timeout_flags3(&ring);
  1437. if (ret) {
  1438. fprintf(stderr, "test_timeout_flags3 failed\n");
  1439. return ret;
  1440. }
  1441. ret = test_timeout_multishot(&ring);
  1442. if (ret && ret != T_EXIT_SKIP) {
  1443. fprintf(stderr, "test_timeout_multishot failed\n");
  1444. return ret;
  1445. }
  1446. ret = test_timeout_multishot_nr(&ring);
  1447. if (ret && ret != T_EXIT_SKIP) {
  1448. fprintf(stderr, "test_timeout_multishot_nr failed\n");
  1449. return ret;
  1450. }
  1451. /* io_uring_wait_cqe_timeout() may have left a timeout, reinit ring */
  1452. io_uring_queue_exit(&ring);
  1453. ret = io_uring_queue_init(8, &ring, 0);
  1454. if (ret) {
  1455. fprintf(stderr, "ring setup failed\n");
  1456. return 1;
  1457. }
  1458. ret = test_timeout_multishot_overflow(&ring);
  1459. if (ret && ret != T_EXIT_SKIP) {
  1460. fprintf(stderr, "test_timeout_multishot_overflow failed\n");
  1461. return ret;
  1462. }
  1463. /* io_uring_wait_cqe_timeout() may have left a timeout, reinit ring */
  1464. io_uring_queue_exit(&ring);
  1465. ret = io_uring_queue_init(8, &ring, 0);
  1466. if (ret) {
  1467. fprintf(stderr, "ring setup failed\n");
  1468. return 1;
  1469. }
  1470. ret = test_single_timeout_wait(&ring, &p);
  1471. if (ret) {
  1472. fprintf(stderr, "test_single_timeout_wait failed\n");
  1473. return ret;
  1474. }
  1475. /* io_uring_wait_cqes() may have left a timeout, reinit ring */
  1476. io_uring_queue_exit(&ring);
  1477. ret = io_uring_queue_init(8, &ring, 0);
  1478. if (ret) {
  1479. fprintf(stderr, "ring setup failed\n");
  1480. return 1;
  1481. }
  1482. ret = test_update_nonexistent_timeout(&ring);
  1483. has_timeout_update = (ret != -EINVAL);
  1484. if (has_timeout_update) {
  1485. if (ret) {
  1486. fprintf(stderr, "test_update_nonexistent_timeout failed\n");
  1487. return ret;
  1488. }
  1489. ret = test_update_invalid_flags(&ring);
  1490. if (ret) {
  1491. fprintf(stderr, "test_update_invalid_flags failed\n");
  1492. return ret;
  1493. }
  1494. ret = test_update_timeout(&ring, 0, false, false, false);
  1495. if (ret) {
  1496. fprintf(stderr, "test_update_timeout failed\n");
  1497. return ret;
  1498. }
  1499. ret = test_update_timeout(&ring, 1, false, false, false);
  1500. if (ret) {
  1501. fprintf(stderr, "test_update_timeout 1ms failed\n");
  1502. return ret;
  1503. }
  1504. ret = test_update_timeout(&ring, 1000, false, false, false);
  1505. if (ret) {
  1506. fprintf(stderr, "test_update_timeout 1s failed\n");
  1507. return ret;
  1508. }
  1509. ret = test_update_timeout(&ring, 0, true, true, false);
  1510. if (ret) {
  1511. fprintf(stderr, "test_update_timeout abs failed\n");
  1512. return ret;
  1513. }
  1514. ret = test_update_timeout(&ring, 0, false, true, false);
  1515. if (ret) {
  1516. fprintf(stderr, "test_update_timeout async failed\n");
  1517. return ret;
  1518. }
  1519. ret = test_update_timeout(&ring, 0, false, false, true);
  1520. if (ret) {
  1521. fprintf(stderr, "test_update_timeout linked failed\n");
  1522. return ret;
  1523. }
  1524. if (sqpoll) {
  1525. ret = test_update_timeout(&sqpoll_ring, 0, false, false,
  1526. false);
  1527. if (ret) {
  1528. fprintf(stderr, "test_update_timeout sqpoll"
  1529. "failed\n");
  1530. return ret;
  1531. }
  1532. }
  1533. }
  1534. /*
  1535. * this test must go last, it kills the ring
  1536. */
  1537. ret = test_single_timeout_exit(&ring);
  1538. if (ret) {
  1539. fprintf(stderr, "test_single_timeout_exit failed\n");
  1540. return ret;
  1541. }
  1542. ret = test_timeout_link_cancel();
  1543. if (ret) {
  1544. fprintf(stderr, "test_timeout_link_cancel failed\n");
  1545. return ret;
  1546. }
  1547. ret = test_not_failing_links();
  1548. if (ret) {
  1549. fprintf(stderr, "test_not_failing_links failed\n");
  1550. return ret;
  1551. }
  1552. if (sqpoll)
  1553. io_uring_queue_exit(&sqpoll_ring);
  1554. return 0;
  1555. }