aio.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. /*
  2. * An async IO implementation for Linux
  3. * Written by Benjamin LaHaise <bcrl@kvack.org>
  4. *
  5. * Implements an efficient asynchronous io interface.
  6. *
  7. * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved.
  8. *
  9. * See ../COPYING for licensing terms.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/errno.h>
  14. #include <linux/time.h>
  15. #include <linux/aio_abi.h>
  16. #include <linux/module.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/backing-dev.h>
  19. #include <linux/uio.h>
  20. #define DEBUG 0
  21. #include <linux/sched.h>
  22. #include <linux/fs.h>
  23. #include <linux/file.h>
  24. #include <linux/mm.h>
  25. #include <linux/mman.h>
  26. #include <linux/mmu_context.h>
  27. #include <linux/slab.h>
  28. #include <linux/timer.h>
  29. #include <linux/aio.h>
  30. #include <linux/highmem.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/security.h>
  33. #include <linux/eventfd.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/mempool.h>
  36. #include <linux/hash.h>
  37. #include <asm/kmap_types.h>
  38. #include <asm/uaccess.h>
  39. #if DEBUG > 1
  40. #define dprintk printk
  41. #else
  42. #define dprintk(x...) do { ; } while (0)
  43. #endif
  44. /*------ sysctl variables----*/
  45. static DEFINE_SPINLOCK(aio_nr_lock);
  46. unsigned long aio_nr; /* current system wide number of aio requests */
  47. unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
  48. /*----end sysctl variables---*/
  49. static struct kmem_cache *kiocb_cachep;
  50. static struct kmem_cache *kioctx_cachep;
  51. static struct workqueue_struct *aio_wq;
  52. /* Used for rare fput completion. */
  53. static void aio_fput_routine(struct work_struct *);
  54. static DECLARE_WORK(fput_work, aio_fput_routine);
  55. static DEFINE_SPINLOCK(fput_lock);
  56. static LIST_HEAD(fput_head);
  57. #define AIO_BATCH_HASH_BITS 3 /* allocated on-stack, so don't go crazy */
  58. #define AIO_BATCH_HASH_SIZE (1 << AIO_BATCH_HASH_BITS)
  59. struct aio_batch_entry {
  60. struct hlist_node list;
  61. struct address_space *mapping;
  62. };
  63. mempool_t *abe_pool;
  64. static void aio_kick_handler(struct work_struct *);
  65. static void aio_queue_work(struct kioctx *);
  66. /* aio_setup
  67. * Creates the slab caches used by the aio routines, panic on
  68. * failure as this is done early during the boot sequence.
  69. */
  70. static int __init aio_setup(void)
  71. {
  72. kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  73. kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  74. aio_wq = create_workqueue("aio");
  75. abe_pool = mempool_create_kmalloc_pool(1, sizeof(struct aio_batch_entry));
  76. BUG_ON(!abe_pool);
  77. pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
  78. return 0;
  79. }
  80. __initcall(aio_setup);
  81. static void aio_free_ring(struct kioctx *ctx)
  82. {
  83. struct aio_ring_info *info = &ctx->ring_info;
  84. long i;
  85. for (i=0; i<info->nr_pages; i++)
  86. put_page(info->ring_pages[i]);
  87. if (info->mmap_size) {
  88. down_write(&ctx->mm->mmap_sem);
  89. do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
  90. up_write(&ctx->mm->mmap_sem);
  91. }
  92. if (info->ring_pages && info->ring_pages != info->internal_pages)
  93. kfree(info->ring_pages);
  94. info->ring_pages = NULL;
  95. info->nr = 0;
  96. }
  97. static int aio_setup_ring(struct kioctx *ctx)
  98. {
  99. struct aio_ring *ring;
  100. struct aio_ring_info *info = &ctx->ring_info;
  101. unsigned nr_events = ctx->max_reqs;
  102. unsigned long size;
  103. int nr_pages;
  104. /* Compensate for the ring buffer's head/tail overlap entry */
  105. nr_events += 2; /* 1 is required, 2 for good luck */
  106. size = sizeof(struct aio_ring);
  107. size += sizeof(struct io_event) * nr_events;
  108. nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
  109. if (nr_pages < 0)
  110. return -EINVAL;
  111. nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
  112. info->nr = 0;
  113. info->ring_pages = info->internal_pages;
  114. if (nr_pages > AIO_RING_PAGES) {
  115. info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  116. if (!info->ring_pages)
  117. return -ENOMEM;
  118. }
  119. info->mmap_size = nr_pages * PAGE_SIZE;
  120. dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
  121. down_write(&ctx->mm->mmap_sem);
  122. info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
  123. PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
  124. 0);
  125. if (IS_ERR((void *)info->mmap_base)) {
  126. up_write(&ctx->mm->mmap_sem);
  127. info->mmap_size = 0;
  128. aio_free_ring(ctx);
  129. return -EAGAIN;
  130. }
  131. dprintk("mmap address: 0x%08lx\n", info->mmap_base);
  132. info->nr_pages = get_user_pages(current, ctx->mm,
  133. info->mmap_base, nr_pages,
  134. 1, 0, info->ring_pages, NULL);
  135. up_write(&ctx->mm->mmap_sem);
  136. if (unlikely(info->nr_pages != nr_pages)) {
  137. aio_free_ring(ctx);
  138. return -EAGAIN;
  139. }
  140. ctx->user_id = info->mmap_base;
  141. info->nr = nr_events; /* trusted copy */
  142. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  143. ring->nr = nr_events; /* user copy */
  144. ring->id = ctx->user_id;
  145. ring->head = ring->tail = 0;
  146. ring->magic = AIO_RING_MAGIC;
  147. ring->compat_features = AIO_RING_COMPAT_FEATURES;
  148. ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
  149. ring->header_length = sizeof(struct aio_ring);
  150. kunmap_atomic(ring, KM_USER0);
  151. return 0;
  152. }
  153. /* aio_ring_event: returns a pointer to the event at the given index from
  154. * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
  155. */
  156. #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
  157. #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
  158. #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
  159. #define aio_ring_event(info, nr, km) ({ \
  160. unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
  161. struct io_event *__event; \
  162. __event = kmap_atomic( \
  163. (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
  164. __event += pos % AIO_EVENTS_PER_PAGE; \
  165. __event; \
  166. })
  167. #define put_aio_ring_event(event, km) do { \
  168. struct io_event *__event = (event); \
  169. (void)__event; \
  170. kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
  171. } while(0)
  172. static void ctx_rcu_free(struct rcu_head *head)
  173. {
  174. struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
  175. unsigned nr_events = ctx->max_reqs;
  176. kmem_cache_free(kioctx_cachep, ctx);
  177. if (nr_events) {
  178. spin_lock(&aio_nr_lock);
  179. BUG_ON(aio_nr - nr_events > aio_nr);
  180. aio_nr -= nr_events;
  181. spin_unlock(&aio_nr_lock);
  182. }
  183. }
  184. /* __put_ioctx
  185. * Called when the last user of an aio context has gone away,
  186. * and the struct needs to be freed.
  187. */
  188. static void __put_ioctx(struct kioctx *ctx)
  189. {
  190. BUG_ON(ctx->reqs_active);
  191. cancel_delayed_work(&ctx->wq);
  192. cancel_work_sync(&ctx->wq.work);
  193. aio_free_ring(ctx);
  194. mmdrop(ctx->mm);
  195. ctx->mm = NULL;
  196. pr_debug("__put_ioctx: freeing %p\n", ctx);
  197. call_rcu(&ctx->rcu_head, ctx_rcu_free);
  198. }
  199. #define get_ioctx(kioctx) do { \
  200. BUG_ON(atomic_read(&(kioctx)->users) <= 0); \
  201. atomic_inc(&(kioctx)->users); \
  202. } while (0)
  203. #define put_ioctx(kioctx) do { \
  204. BUG_ON(atomic_read(&(kioctx)->users) <= 0); \
  205. if (unlikely(atomic_dec_and_test(&(kioctx)->users))) \
  206. __put_ioctx(kioctx); \
  207. } while (0)
  208. /* ioctx_alloc
  209. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  210. */
  211. static struct kioctx *ioctx_alloc(unsigned nr_events)
  212. {
  213. struct mm_struct *mm;
  214. struct kioctx *ctx;
  215. int did_sync = 0;
  216. /* Prevent overflows */
  217. if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
  218. (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
  219. pr_debug("ENOMEM: nr_events too high\n");
  220. return ERR_PTR(-EINVAL);
  221. }
  222. if ((unsigned long)nr_events > aio_max_nr)
  223. return ERR_PTR(-EAGAIN);
  224. ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
  225. if (!ctx)
  226. return ERR_PTR(-ENOMEM);
  227. ctx->max_reqs = nr_events;
  228. mm = ctx->mm = current->mm;
  229. atomic_inc(&mm->mm_count);
  230. atomic_set(&ctx->users, 1);
  231. spin_lock_init(&ctx->ctx_lock);
  232. spin_lock_init(&ctx->ring_info.ring_lock);
  233. init_waitqueue_head(&ctx->wait);
  234. INIT_LIST_HEAD(&ctx->active_reqs);
  235. INIT_LIST_HEAD(&ctx->run_list);
  236. INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
  237. if (aio_setup_ring(ctx) < 0)
  238. goto out_freectx;
  239. /* limit the number of system wide aios */
  240. do {
  241. spin_lock_bh(&aio_nr_lock);
  242. if (aio_nr + nr_events > aio_max_nr ||
  243. aio_nr + nr_events < aio_nr)
  244. ctx->max_reqs = 0;
  245. else
  246. aio_nr += ctx->max_reqs;
  247. spin_unlock_bh(&aio_nr_lock);
  248. if (ctx->max_reqs || did_sync)
  249. break;
  250. /* wait for rcu callbacks to have completed before giving up */
  251. synchronize_rcu();
  252. did_sync = 1;
  253. ctx->max_reqs = nr_events;
  254. } while (1);
  255. if (ctx->max_reqs == 0)
  256. goto out_cleanup;
  257. /* now link into global list. */
  258. spin_lock(&mm->ioctx_lock);
  259. hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
  260. spin_unlock(&mm->ioctx_lock);
  261. dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  262. ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
  263. return ctx;
  264. out_cleanup:
  265. __put_ioctx(ctx);
  266. return ERR_PTR(-EAGAIN);
  267. out_freectx:
  268. mmdrop(mm);
  269. kmem_cache_free(kioctx_cachep, ctx);
  270. ctx = ERR_PTR(-ENOMEM);
  271. dprintk("aio: error allocating ioctx %p\n", ctx);
  272. return ctx;
  273. }
  274. /* aio_cancel_all
  275. * Cancels all outstanding aio requests on an aio context. Used
  276. * when the processes owning a context have all exited to encourage
  277. * the rapid destruction of the kioctx.
  278. */
  279. static void aio_cancel_all(struct kioctx *ctx)
  280. {
  281. int (*cancel)(struct kiocb *, struct io_event *);
  282. struct io_event res;
  283. spin_lock_irq(&ctx->ctx_lock);
  284. ctx->dead = 1;
  285. while (!list_empty(&ctx->active_reqs)) {
  286. struct list_head *pos = ctx->active_reqs.next;
  287. struct kiocb *iocb = list_kiocb(pos);
  288. list_del_init(&iocb->ki_list);
  289. cancel = iocb->ki_cancel;
  290. kiocbSetCancelled(iocb);
  291. if (cancel) {
  292. iocb->ki_users++;
  293. spin_unlock_irq(&ctx->ctx_lock);
  294. cancel(iocb, &res);
  295. spin_lock_irq(&ctx->ctx_lock);
  296. }
  297. }
  298. spin_unlock_irq(&ctx->ctx_lock);
  299. }
  300. static void wait_for_all_aios(struct kioctx *ctx)
  301. {
  302. struct task_struct *tsk = current;
  303. DECLARE_WAITQUEUE(wait, tsk);
  304. spin_lock_irq(&ctx->ctx_lock);
  305. if (!ctx->reqs_active)
  306. goto out;
  307. add_wait_queue(&ctx->wait, &wait);
  308. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  309. while (ctx->reqs_active) {
  310. spin_unlock_irq(&ctx->ctx_lock);
  311. io_schedule();
  312. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  313. spin_lock_irq(&ctx->ctx_lock);
  314. }
  315. __set_task_state(tsk, TASK_RUNNING);
  316. remove_wait_queue(&ctx->wait, &wait);
  317. out:
  318. spin_unlock_irq(&ctx->ctx_lock);
  319. }
  320. /* wait_on_sync_kiocb:
  321. * Waits on the given sync kiocb to complete.
  322. */
  323. ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
  324. {
  325. while (iocb->ki_users) {
  326. set_current_state(TASK_UNINTERRUPTIBLE);
  327. if (!iocb->ki_users)
  328. break;
  329. io_schedule();
  330. }
  331. __set_current_state(TASK_RUNNING);
  332. return iocb->ki_user_data;
  333. }
  334. EXPORT_SYMBOL(wait_on_sync_kiocb);
  335. /* exit_aio: called when the last user of mm goes away. At this point,
  336. * there is no way for any new requests to be submited or any of the
  337. * io_* syscalls to be called on the context. However, there may be
  338. * outstanding requests which hold references to the context; as they
  339. * go away, they will call put_ioctx and release any pinned memory
  340. * associated with the request (held via struct page * references).
  341. */
  342. void exit_aio(struct mm_struct *mm)
  343. {
  344. struct kioctx *ctx;
  345. while (!hlist_empty(&mm->ioctx_list)) {
  346. ctx = hlist_entry(mm->ioctx_list.first, struct kioctx, list);
  347. hlist_del_rcu(&ctx->list);
  348. aio_cancel_all(ctx);
  349. wait_for_all_aios(ctx);
  350. /*
  351. * Ensure we don't leave the ctx on the aio_wq
  352. */
  353. cancel_work_sync(&ctx->wq.work);
  354. if (1 != atomic_read(&ctx->users))
  355. printk(KERN_DEBUG
  356. "exit_aio:ioctx still alive: %d %d %d\n",
  357. atomic_read(&ctx->users), ctx->dead,
  358. ctx->reqs_active);
  359. put_ioctx(ctx);
  360. }
  361. }
  362. /* aio_get_req
  363. * Allocate a slot for an aio request. Increments the users count
  364. * of the kioctx so that the kioctx stays around until all requests are
  365. * complete. Returns NULL if no requests are free.
  366. *
  367. * Returns with kiocb->users set to 2. The io submit code path holds
  368. * an extra reference while submitting the i/o.
  369. * This prevents races between the aio code path referencing the
  370. * req (after submitting it) and aio_complete() freeing the req.
  371. */
  372. static struct kiocb *__aio_get_req(struct kioctx *ctx)
  373. {
  374. struct kiocb *req = NULL;
  375. struct aio_ring *ring;
  376. int okay = 0;
  377. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
  378. if (unlikely(!req))
  379. return NULL;
  380. req->ki_flags = 0;
  381. req->ki_users = 2;
  382. req->ki_key = 0;
  383. req->ki_ctx = ctx;
  384. req->ki_cancel = NULL;
  385. req->ki_retry = NULL;
  386. req->ki_dtor = NULL;
  387. req->private = NULL;
  388. req->ki_iovec = NULL;
  389. INIT_LIST_HEAD(&req->ki_run_list);
  390. req->ki_eventfd = NULL;
  391. /* Check if the completion queue has enough free space to
  392. * accept an event from this io.
  393. */
  394. spin_lock_irq(&ctx->ctx_lock);
  395. ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
  396. if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
  397. list_add(&req->ki_list, &ctx->active_reqs);
  398. ctx->reqs_active++;
  399. okay = 1;
  400. }
  401. kunmap_atomic(ring, KM_USER0);
  402. spin_unlock_irq(&ctx->ctx_lock);
  403. if (!okay) {
  404. kmem_cache_free(kiocb_cachep, req);
  405. req = NULL;
  406. }
  407. return req;
  408. }
  409. static inline struct kiocb *aio_get_req(struct kioctx *ctx)
  410. {
  411. struct kiocb *req;
  412. /* Handle a potential starvation case -- should be exceedingly rare as
  413. * requests will be stuck on fput_head only if the aio_fput_routine is
  414. * delayed and the requests were the last user of the struct file.
  415. */
  416. req = __aio_get_req(ctx);
  417. if (unlikely(NULL == req)) {
  418. aio_fput_routine(NULL);
  419. req = __aio_get_req(ctx);
  420. }
  421. return req;
  422. }
  423. static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
  424. {
  425. assert_spin_locked(&ctx->ctx_lock);
  426. if (req->ki_eventfd != NULL)
  427. eventfd_ctx_put(req->ki_eventfd);
  428. if (req->ki_dtor)
  429. req->ki_dtor(req);
  430. if (req->ki_iovec != &req->ki_inline_vec)
  431. kfree(req->ki_iovec);
  432. kmem_cache_free(kiocb_cachep, req);
  433. ctx->reqs_active--;
  434. if (unlikely(!ctx->reqs_active && ctx->dead))
  435. wake_up(&ctx->wait);
  436. }
  437. static void aio_fput_routine(struct work_struct *data)
  438. {
  439. spin_lock_irq(&fput_lock);
  440. while (likely(!list_empty(&fput_head))) {
  441. struct kiocb *req = list_kiocb(fput_head.next);
  442. struct kioctx *ctx = req->ki_ctx;
  443. list_del(&req->ki_list);
  444. spin_unlock_irq(&fput_lock);
  445. /* Complete the fput(s) */
  446. if (req->ki_filp != NULL)
  447. __fput(req->ki_filp);
  448. /* Link the iocb into the context's free list */
  449. spin_lock_irq(&ctx->ctx_lock);
  450. really_put_req(ctx, req);
  451. spin_unlock_irq(&ctx->ctx_lock);
  452. put_ioctx(ctx);
  453. spin_lock_irq(&fput_lock);
  454. }
  455. spin_unlock_irq(&fput_lock);
  456. }
  457. /* __aio_put_req
  458. * Returns true if this put was the last user of the request.
  459. */
  460. static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
  461. {
  462. dprintk(KERN_DEBUG "aio_put(%p): f_count=%ld\n",
  463. req, atomic_long_read(&req->ki_filp->f_count));
  464. assert_spin_locked(&ctx->ctx_lock);
  465. req->ki_users--;
  466. BUG_ON(req->ki_users < 0);
  467. if (likely(req->ki_users))
  468. return 0;
  469. list_del(&req->ki_list); /* remove from active_reqs */
  470. req->ki_cancel = NULL;
  471. req->ki_retry = NULL;
  472. /*
  473. * Try to optimize the aio and eventfd file* puts, by avoiding to
  474. * schedule work in case it is not __fput() time. In normal cases,
  475. * we would not be holding the last reference to the file*, so
  476. * this function will be executed w/out any aio kthread wakeup.
  477. */
  478. if (unlikely(atomic_long_dec_and_test(&req->ki_filp->f_count))) {
  479. get_ioctx(ctx);
  480. spin_lock(&fput_lock);
  481. list_add(&req->ki_list, &fput_head);
  482. spin_unlock(&fput_lock);
  483. queue_work(aio_wq, &fput_work);
  484. } else {
  485. req->ki_filp = NULL;
  486. really_put_req(ctx, req);
  487. }
  488. return 1;
  489. }
  490. /* aio_put_req
  491. * Returns true if this put was the last user of the kiocb,
  492. * false if the request is still in use.
  493. */
  494. int aio_put_req(struct kiocb *req)
  495. {
  496. struct kioctx *ctx = req->ki_ctx;
  497. int ret;
  498. spin_lock_irq(&ctx->ctx_lock);
  499. ret = __aio_put_req(ctx, req);
  500. spin_unlock_irq(&ctx->ctx_lock);
  501. return ret;
  502. }
  503. EXPORT_SYMBOL(aio_put_req);
  504. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  505. {
  506. struct mm_struct *mm = current->mm;
  507. struct kioctx *ctx, *ret = NULL;
  508. struct hlist_node *n;
  509. rcu_read_lock();
  510. hlist_for_each_entry_rcu(ctx, n, &mm->ioctx_list, list) {
  511. if (ctx->user_id == ctx_id && !ctx->dead) {
  512. get_ioctx(ctx);
  513. ret = ctx;
  514. break;
  515. }
  516. }
  517. rcu_read_unlock();
  518. return ret;
  519. }
  520. /*
  521. * Queue up a kiocb to be retried. Assumes that the kiocb
  522. * has already been marked as kicked, and places it on
  523. * the retry run list for the corresponding ioctx, if it
  524. * isn't already queued. Returns 1 if it actually queued
  525. * the kiocb (to tell the caller to activate the work
  526. * queue to process it), or 0, if it found that it was
  527. * already queued.
  528. */
  529. static inline int __queue_kicked_iocb(struct kiocb *iocb)
  530. {
  531. struct kioctx *ctx = iocb->ki_ctx;
  532. assert_spin_locked(&ctx->ctx_lock);
  533. if (list_empty(&iocb->ki_run_list)) {
  534. list_add_tail(&iocb->ki_run_list,
  535. &ctx->run_list);
  536. return 1;
  537. }
  538. return 0;
  539. }
  540. /* aio_run_iocb
  541. * This is the core aio execution routine. It is
  542. * invoked both for initial i/o submission and
  543. * subsequent retries via the aio_kick_handler.
  544. * Expects to be invoked with iocb->ki_ctx->lock
  545. * already held. The lock is released and reacquired
  546. * as needed during processing.
  547. *
  548. * Calls the iocb retry method (already setup for the
  549. * iocb on initial submission) for operation specific
  550. * handling, but takes care of most of common retry
  551. * execution details for a given iocb. The retry method
  552. * needs to be non-blocking as far as possible, to avoid
  553. * holding up other iocbs waiting to be serviced by the
  554. * retry kernel thread.
  555. *
  556. * The trickier parts in this code have to do with
  557. * ensuring that only one retry instance is in progress
  558. * for a given iocb at any time. Providing that guarantee
  559. * simplifies the coding of individual aio operations as
  560. * it avoids various potential races.
  561. */
  562. static ssize_t aio_run_iocb(struct kiocb *iocb)
  563. {
  564. struct kioctx *ctx = iocb->ki_ctx;
  565. ssize_t (*retry)(struct kiocb *);
  566. ssize_t ret;
  567. if (!(retry = iocb->ki_retry)) {
  568. printk("aio_run_iocb: iocb->ki_retry = NULL\n");
  569. return 0;
  570. }
  571. /*
  572. * We don't want the next retry iteration for this
  573. * operation to start until this one has returned and
  574. * updated the iocb state. However, wait_queue functions
  575. * can trigger a kick_iocb from interrupt context in the
  576. * meantime, indicating that data is available for the next
  577. * iteration. We want to remember that and enable the
  578. * next retry iteration _after_ we are through with
  579. * this one.
  580. *
  581. * So, in order to be able to register a "kick", but
  582. * prevent it from being queued now, we clear the kick
  583. * flag, but make the kick code *think* that the iocb is
  584. * still on the run list until we are actually done.
  585. * When we are done with this iteration, we check if
  586. * the iocb was kicked in the meantime and if so, queue
  587. * it up afresh.
  588. */
  589. kiocbClearKicked(iocb);
  590. /*
  591. * This is so that aio_complete knows it doesn't need to
  592. * pull the iocb off the run list (We can't just call
  593. * INIT_LIST_HEAD because we don't want a kick_iocb to
  594. * queue this on the run list yet)
  595. */
  596. iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
  597. spin_unlock_irq(&ctx->ctx_lock);
  598. /* Quit retrying if the i/o has been cancelled */
  599. if (kiocbIsCancelled(iocb)) {
  600. ret = -EINTR;
  601. aio_complete(iocb, ret, 0);
  602. /* must not access the iocb after this */
  603. goto out;
  604. }
  605. /*
  606. * Now we are all set to call the retry method in async
  607. * context.
  608. */
  609. ret = retry(iocb);
  610. if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
  611. BUG_ON(!list_empty(&iocb->ki_wait.task_list));
  612. aio_complete(iocb, ret, 0);
  613. }
  614. out:
  615. spin_lock_irq(&ctx->ctx_lock);
  616. if (-EIOCBRETRY == ret) {
  617. /*
  618. * OK, now that we are done with this iteration
  619. * and know that there is more left to go,
  620. * this is where we let go so that a subsequent
  621. * "kick" can start the next iteration
  622. */
  623. /* will make __queue_kicked_iocb succeed from here on */
  624. INIT_LIST_HEAD(&iocb->ki_run_list);
  625. /* we must queue the next iteration ourselves, if it
  626. * has already been kicked */
  627. if (kiocbIsKicked(iocb)) {
  628. __queue_kicked_iocb(iocb);
  629. /*
  630. * __queue_kicked_iocb will always return 1 here, because
  631. * iocb->ki_run_list is empty at this point so it should
  632. * be safe to unconditionally queue the context into the
  633. * work queue.
  634. */
  635. aio_queue_work(ctx);
  636. }
  637. }
  638. return ret;
  639. }
  640. /*
  641. * __aio_run_iocbs:
  642. * Process all pending retries queued on the ioctx
  643. * run list.
  644. * Assumes it is operating within the aio issuer's mm
  645. * context.
  646. */
  647. static int __aio_run_iocbs(struct kioctx *ctx)
  648. {
  649. struct kiocb *iocb;
  650. struct list_head run_list;
  651. assert_spin_locked(&ctx->ctx_lock);
  652. list_replace_init(&ctx->run_list, &run_list);
  653. while (!list_empty(&run_list)) {
  654. iocb = list_entry(run_list.next, struct kiocb,
  655. ki_run_list);
  656. list_del(&iocb->ki_run_list);
  657. /*
  658. * Hold an extra reference while retrying i/o.
  659. */
  660. iocb->ki_users++; /* grab extra reference */
  661. aio_run_iocb(iocb);
  662. __aio_put_req(ctx, iocb);
  663. }
  664. if (!list_empty(&ctx->run_list))
  665. return 1;
  666. return 0;
  667. }
  668. static void aio_queue_work(struct kioctx * ctx)
  669. {
  670. unsigned long timeout;
  671. /*
  672. * if someone is waiting, get the work started right
  673. * away, otherwise, use a longer delay
  674. */
  675. smp_mb();
  676. if (waitqueue_active(&ctx->wait))
  677. timeout = 1;
  678. else
  679. timeout = HZ/10;
  680. queue_delayed_work(aio_wq, &ctx->wq, timeout);
  681. }
  682. /*
  683. * aio_run_iocbs:
  684. * Process all pending retries queued on the ioctx
  685. * run list.
  686. * Assumes it is operating within the aio issuer's mm
  687. * context.
  688. */
  689. static inline void aio_run_iocbs(struct kioctx *ctx)
  690. {
  691. int requeue;
  692. spin_lock_irq(&ctx->ctx_lock);
  693. requeue = __aio_run_iocbs(ctx);
  694. spin_unlock_irq(&ctx->ctx_lock);
  695. if (requeue)
  696. aio_queue_work(ctx);
  697. }
  698. /*
  699. * just like aio_run_iocbs, but keeps running them until
  700. * the list stays empty
  701. */
  702. static inline void aio_run_all_iocbs(struct kioctx *ctx)
  703. {
  704. spin_lock_irq(&ctx->ctx_lock);
  705. while (__aio_run_iocbs(ctx))
  706. ;
  707. spin_unlock_irq(&ctx->ctx_lock);
  708. }
  709. /*
  710. * aio_kick_handler:
  711. * Work queue handler triggered to process pending
  712. * retries on an ioctx. Takes on the aio issuer's
  713. * mm context before running the iocbs, so that
  714. * copy_xxx_user operates on the issuer's address
  715. * space.
  716. * Run on aiod's context.
  717. */
  718. static void aio_kick_handler(struct work_struct *work)
  719. {
  720. struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
  721. mm_segment_t oldfs = get_fs();
  722. struct mm_struct *mm;
  723. int requeue;
  724. set_fs(USER_DS);
  725. use_mm(ctx->mm);
  726. spin_lock_irq(&ctx->ctx_lock);
  727. requeue =__aio_run_iocbs(ctx);
  728. mm = ctx->mm;
  729. spin_unlock_irq(&ctx->ctx_lock);
  730. unuse_mm(mm);
  731. set_fs(oldfs);
  732. /*
  733. * we're in a worker thread already, don't use queue_delayed_work,
  734. */
  735. if (requeue)
  736. queue_delayed_work(aio_wq, &ctx->wq, 0);
  737. }
  738. /*
  739. * Called by kick_iocb to queue the kiocb for retry
  740. * and if required activate the aio work queue to process
  741. * it
  742. */
  743. static void try_queue_kicked_iocb(struct kiocb *iocb)
  744. {
  745. struct kioctx *ctx = iocb->ki_ctx;
  746. unsigned long flags;
  747. int run = 0;
  748. /* We're supposed to be the only path putting the iocb back on the run
  749. * list. If we find that the iocb is *back* on a wait queue already
  750. * than retry has happened before we could queue the iocb. This also
  751. * means that the retry could have completed and freed our iocb, no
  752. * good. */
  753. BUG_ON((!list_empty(&iocb->ki_wait.task_list)));
  754. spin_lock_irqsave(&ctx->ctx_lock, flags);
  755. /* set this inside the lock so that we can't race with aio_run_iocb()
  756. * testing it and putting the iocb on the run list under the lock */
  757. if (!kiocbTryKick(iocb))
  758. run = __queue_kicked_iocb(iocb);
  759. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  760. if (run)
  761. aio_queue_work(ctx);
  762. }
  763. /*
  764. * kick_iocb:
  765. * Called typically from a wait queue callback context
  766. * (aio_wake_function) to trigger a retry of the iocb.
  767. * The retry is usually executed by aio workqueue
  768. * threads (See aio_kick_handler).
  769. */
  770. void kick_iocb(struct kiocb *iocb)
  771. {
  772. /* sync iocbs are easy: they can only ever be executing from a
  773. * single context. */
  774. if (is_sync_kiocb(iocb)) {
  775. kiocbSetKicked(iocb);
  776. wake_up_process(iocb->ki_obj.tsk);
  777. return;
  778. }
  779. try_queue_kicked_iocb(iocb);
  780. }
  781. EXPORT_SYMBOL(kick_iocb);
  782. /* aio_complete
  783. * Called when the io request on the given iocb is complete.
  784. * Returns true if this is the last user of the request. The
  785. * only other user of the request can be the cancellation code.
  786. */
  787. int aio_complete(struct kiocb *iocb, long res, long res2)
  788. {
  789. struct kioctx *ctx = iocb->ki_ctx;
  790. struct aio_ring_info *info;
  791. struct aio_ring *ring;
  792. struct io_event *event;
  793. unsigned long flags;
  794. unsigned long tail;
  795. int ret;
  796. /*
  797. * Special case handling for sync iocbs:
  798. * - events go directly into the iocb for fast handling
  799. * - the sync task with the iocb in its stack holds the single iocb
  800. * ref, no other paths have a way to get another ref
  801. * - the sync task helpfully left a reference to itself in the iocb
  802. */
  803. if (is_sync_kiocb(iocb)) {
  804. BUG_ON(iocb->ki_users != 1);
  805. iocb->ki_user_data = res;
  806. iocb->ki_users = 0;
  807. wake_up_process(iocb->ki_obj.tsk);
  808. return 1;
  809. }
  810. info = &ctx->ring_info;
  811. /* add a completion event to the ring buffer.
  812. * must be done holding ctx->ctx_lock to prevent
  813. * other code from messing with the tail
  814. * pointer since we might be called from irq
  815. * context.
  816. */
  817. spin_lock_irqsave(&ctx->ctx_lock, flags);
  818. if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
  819. list_del_init(&iocb->ki_run_list);
  820. /*
  821. * cancelled requests don't get events, userland was given one
  822. * when the event got cancelled.
  823. */
  824. if (kiocbIsCancelled(iocb))
  825. goto put_rq;
  826. ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
  827. tail = info->tail;
  828. event = aio_ring_event(info, tail, KM_IRQ0);
  829. if (++tail >= info->nr)
  830. tail = 0;
  831. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  832. event->data = iocb->ki_user_data;
  833. event->res = res;
  834. event->res2 = res2;
  835. dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
  836. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  837. res, res2);
  838. /* after flagging the request as done, we
  839. * must never even look at it again
  840. */
  841. smp_wmb(); /* make event visible before updating tail */
  842. info->tail = tail;
  843. ring->tail = tail;
  844. put_aio_ring_event(event, KM_IRQ0);
  845. kunmap_atomic(ring, KM_IRQ1);
  846. pr_debug("added to ring %p at [%lu]\n", iocb, tail);
  847. /*
  848. * Check if the user asked us to deliver the result through an
  849. * eventfd. The eventfd_signal() function is safe to be called
  850. * from IRQ context.
  851. */
  852. if (iocb->ki_eventfd != NULL)
  853. eventfd_signal(iocb->ki_eventfd, 1);
  854. put_rq:
  855. /* everything turned out well, dispose of the aiocb. */
  856. ret = __aio_put_req(ctx, iocb);
  857. /*
  858. * We have to order our ring_info tail store above and test
  859. * of the wait list below outside the wait lock. This is
  860. * like in wake_up_bit() where clearing a bit has to be
  861. * ordered with the unlocked test.
  862. */
  863. smp_mb();
  864. if (waitqueue_active(&ctx->wait))
  865. wake_up(&ctx->wait);
  866. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  867. return ret;
  868. }
  869. EXPORT_SYMBOL(aio_complete);
  870. /* aio_read_evt
  871. * Pull an event off of the ioctx's event ring. Returns the number of
  872. * events fetched (0 or 1 ;-)
  873. * FIXME: make this use cmpxchg.
  874. * TODO: make the ringbuffer user mmap()able (requires FIXME).
  875. */
  876. static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
  877. {
  878. struct aio_ring_info *info = &ioctx->ring_info;
  879. struct aio_ring *ring;
  880. unsigned long head;
  881. int ret = 0;
  882. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  883. dprintk("in aio_read_evt h%lu t%lu m%lu\n",
  884. (unsigned long)ring->head, (unsigned long)ring->tail,
  885. (unsigned long)ring->nr);
  886. if (ring->head == ring->tail)
  887. goto out;
  888. spin_lock(&info->ring_lock);
  889. head = ring->head % info->nr;
  890. if (head != ring->tail) {
  891. struct io_event *evp = aio_ring_event(info, head, KM_USER1);
  892. *ent = *evp;
  893. head = (head + 1) % info->nr;
  894. smp_mb(); /* finish reading the event before updatng the head */
  895. ring->head = head;
  896. ret = 1;
  897. put_aio_ring_event(evp, KM_USER1);
  898. }
  899. spin_unlock(&info->ring_lock);
  900. out:
  901. kunmap_atomic(ring, KM_USER0);
  902. dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
  903. (unsigned long)ring->head, (unsigned long)ring->tail);
  904. return ret;
  905. }
  906. struct aio_timeout {
  907. struct timer_list timer;
  908. int timed_out;
  909. struct task_struct *p;
  910. };
  911. static void timeout_func(unsigned long data)
  912. {
  913. struct aio_timeout *to = (struct aio_timeout *)data;
  914. to->timed_out = 1;
  915. wake_up_process(to->p);
  916. }
  917. static inline void init_timeout(struct aio_timeout *to)
  918. {
  919. setup_timer_on_stack(&to->timer, timeout_func, (unsigned long) to);
  920. to->timed_out = 0;
  921. to->p = current;
  922. }
  923. static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
  924. const struct timespec *ts)
  925. {
  926. to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
  927. if (time_after(to->timer.expires, jiffies))
  928. add_timer(&to->timer);
  929. else
  930. to->timed_out = 1;
  931. }
  932. static inline void clear_timeout(struct aio_timeout *to)
  933. {
  934. del_singleshot_timer_sync(&to->timer);
  935. }
  936. static int read_events(struct kioctx *ctx,
  937. long min_nr, long nr,
  938. struct io_event __user *event,
  939. struct timespec __user *timeout)
  940. {
  941. long start_jiffies = jiffies;
  942. struct task_struct *tsk = current;
  943. DECLARE_WAITQUEUE(wait, tsk);
  944. int ret;
  945. int i = 0;
  946. struct io_event ent;
  947. struct aio_timeout to;
  948. int retry = 0;
  949. /* needed to zero any padding within an entry (there shouldn't be
  950. * any, but C is fun!
  951. */
  952. memset(&ent, 0, sizeof(ent));
  953. retry:
  954. ret = 0;
  955. while (likely(i < nr)) {
  956. ret = aio_read_evt(ctx, &ent);
  957. if (unlikely(ret <= 0))
  958. break;
  959. dprintk("read event: %Lx %Lx %Lx %Lx\n",
  960. ent.data, ent.obj, ent.res, ent.res2);
  961. /* Could we split the check in two? */
  962. ret = -EFAULT;
  963. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  964. dprintk("aio: lost an event due to EFAULT.\n");
  965. break;
  966. }
  967. ret = 0;
  968. /* Good, event copied to userland, update counts. */
  969. event ++;
  970. i ++;
  971. }
  972. if (min_nr <= i)
  973. return i;
  974. if (ret)
  975. return ret;
  976. /* End fast path */
  977. /* racey check, but it gets redone */
  978. if (!retry && unlikely(!list_empty(&ctx->run_list))) {
  979. retry = 1;
  980. aio_run_all_iocbs(ctx);
  981. goto retry;
  982. }
  983. init_timeout(&to);
  984. if (timeout) {
  985. struct timespec ts;
  986. ret = -EFAULT;
  987. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  988. goto out;
  989. set_timeout(start_jiffies, &to, &ts);
  990. }
  991. while (likely(i < nr)) {
  992. add_wait_queue_exclusive(&ctx->wait, &wait);
  993. do {
  994. set_task_state(tsk, TASK_INTERRUPTIBLE);
  995. ret = aio_read_evt(ctx, &ent);
  996. if (ret)
  997. break;
  998. if (min_nr <= i)
  999. break;
  1000. if (unlikely(ctx->dead)) {
  1001. ret = -EINVAL;
  1002. break;
  1003. }
  1004. if (to.timed_out) /* Only check after read evt */
  1005. break;
  1006. /* Try to only show up in io wait if there are ops
  1007. * in flight */
  1008. if (ctx->reqs_active)
  1009. io_schedule();
  1010. else
  1011. schedule();
  1012. if (signal_pending(tsk)) {
  1013. ret = -EINTR;
  1014. break;
  1015. }
  1016. /*ret = aio_read_evt(ctx, &ent);*/
  1017. } while (1) ;
  1018. set_task_state(tsk, TASK_RUNNING);
  1019. remove_wait_queue(&ctx->wait, &wait);
  1020. if (unlikely(ret <= 0))
  1021. break;
  1022. ret = -EFAULT;
  1023. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  1024. dprintk("aio: lost an event due to EFAULT.\n");
  1025. break;
  1026. }
  1027. /* Good, event copied to userland, update counts. */
  1028. event ++;
  1029. i ++;
  1030. }
  1031. if (timeout)
  1032. clear_timeout(&to);
  1033. out:
  1034. destroy_timer_on_stack(&to.timer);
  1035. return i ? i : ret;
  1036. }
  1037. /* Take an ioctx and remove it from the list of ioctx's. Protects
  1038. * against races with itself via ->dead.
  1039. */
  1040. static void io_destroy(struct kioctx *ioctx)
  1041. {
  1042. struct mm_struct *mm = current->mm;
  1043. int was_dead;
  1044. /* delete the entry from the list is someone else hasn't already */
  1045. spin_lock(&mm->ioctx_lock);
  1046. was_dead = ioctx->dead;
  1047. ioctx->dead = 1;
  1048. hlist_del_rcu(&ioctx->list);
  1049. spin_unlock(&mm->ioctx_lock);
  1050. dprintk("aio_release(%p)\n", ioctx);
  1051. if (likely(!was_dead))
  1052. put_ioctx(ioctx); /* twice for the list */
  1053. aio_cancel_all(ioctx);
  1054. wait_for_all_aios(ioctx);
  1055. /*
  1056. * Wake up any waiters. The setting of ctx->dead must be seen
  1057. * by other CPUs at this point. Right now, we rely on the
  1058. * locking done by the above calls to ensure this consistency.
  1059. */
  1060. wake_up(&ioctx->wait);
  1061. put_ioctx(ioctx); /* once for the lookup */
  1062. }
  1063. /* sys_io_setup:
  1064. * Create an aio_context capable of receiving at least nr_events.
  1065. * ctxp must not point to an aio_context that already exists, and
  1066. * must be initialized to 0 prior to the call. On successful
  1067. * creation of the aio_context, *ctxp is filled in with the resulting
  1068. * handle. May fail with -EINVAL if *ctxp is not initialized,
  1069. * if the specified nr_events exceeds internal limits. May fail
  1070. * with -EAGAIN if the specified nr_events exceeds the user's limit
  1071. * of available events. May fail with -ENOMEM if insufficient kernel
  1072. * resources are available. May fail with -EFAULT if an invalid
  1073. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  1074. * implemented.
  1075. */
  1076. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  1077. {
  1078. struct kioctx *ioctx = NULL;
  1079. unsigned long ctx;
  1080. long ret;
  1081. ret = get_user(ctx, ctxp);
  1082. if (unlikely(ret))
  1083. goto out;
  1084. ret = -EINVAL;
  1085. if (unlikely(ctx || nr_events == 0)) {
  1086. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  1087. ctx, nr_events);
  1088. goto out;
  1089. }
  1090. ioctx = ioctx_alloc(nr_events);
  1091. ret = PTR_ERR(ioctx);
  1092. if (!IS_ERR(ioctx)) {
  1093. ret = put_user(ioctx->user_id, ctxp);
  1094. if (!ret)
  1095. return 0;
  1096. get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
  1097. io_destroy(ioctx);
  1098. }
  1099. out:
  1100. return ret;
  1101. }
  1102. /* sys_io_destroy:
  1103. * Destroy the aio_context specified. May cancel any outstanding
  1104. * AIOs and block on completion. Will fail with -ENOSYS if not
  1105. * implemented. May fail with -EFAULT if the context pointed to
  1106. * is invalid.
  1107. */
  1108. SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
  1109. {
  1110. struct kioctx *ioctx = lookup_ioctx(ctx);
  1111. if (likely(NULL != ioctx)) {
  1112. io_destroy(ioctx);
  1113. return 0;
  1114. }
  1115. pr_debug("EINVAL: io_destroy: invalid context id\n");
  1116. return -EINVAL;
  1117. }
  1118. static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
  1119. {
  1120. struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
  1121. BUG_ON(ret <= 0);
  1122. while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
  1123. ssize_t this = min((ssize_t)iov->iov_len, ret);
  1124. iov->iov_base += this;
  1125. iov->iov_len -= this;
  1126. iocb->ki_left -= this;
  1127. ret -= this;
  1128. if (iov->iov_len == 0) {
  1129. iocb->ki_cur_seg++;
  1130. iov++;
  1131. }
  1132. }
  1133. /* the caller should not have done more io than what fit in
  1134. * the remaining iovecs */
  1135. BUG_ON(ret > 0 && iocb->ki_left == 0);
  1136. }
  1137. static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
  1138. {
  1139. struct file *file = iocb->ki_filp;
  1140. struct address_space *mapping = file->f_mapping;
  1141. struct inode *inode = mapping->host;
  1142. ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
  1143. unsigned long, loff_t);
  1144. ssize_t ret = 0;
  1145. unsigned short opcode;
  1146. if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
  1147. (iocb->ki_opcode == IOCB_CMD_PREAD)) {
  1148. rw_op = file->f_op->aio_read;
  1149. opcode = IOCB_CMD_PREADV;
  1150. } else {
  1151. rw_op = file->f_op->aio_write;
  1152. opcode = IOCB_CMD_PWRITEV;
  1153. }
  1154. /* This matches the pread()/pwrite() logic */
  1155. if (iocb->ki_pos < 0)
  1156. return -EINVAL;
  1157. do {
  1158. ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
  1159. iocb->ki_nr_segs - iocb->ki_cur_seg,
  1160. iocb->ki_pos);
  1161. if (ret > 0)
  1162. aio_advance_iovec(iocb, ret);
  1163. /* retry all partial writes. retry partial reads as long as its a
  1164. * regular file. */
  1165. } while (ret > 0 && iocb->ki_left > 0 &&
  1166. (opcode == IOCB_CMD_PWRITEV ||
  1167. (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
  1168. /* This means we must have transferred all that we could */
  1169. /* No need to retry anymore */
  1170. if ((ret == 0) || (iocb->ki_left == 0))
  1171. ret = iocb->ki_nbytes - iocb->ki_left;
  1172. /* If we managed to write some out we return that, rather than
  1173. * the eventual error. */
  1174. if (opcode == IOCB_CMD_PWRITEV
  1175. && ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY
  1176. && iocb->ki_nbytes - iocb->ki_left)
  1177. ret = iocb->ki_nbytes - iocb->ki_left;
  1178. return ret;
  1179. }
  1180. static ssize_t aio_fdsync(struct kiocb *iocb)
  1181. {
  1182. struct file *file = iocb->ki_filp;
  1183. ssize_t ret = -EINVAL;
  1184. if (file->f_op->aio_fsync)
  1185. ret = file->f_op->aio_fsync(iocb, 1);
  1186. return ret;
  1187. }
  1188. static ssize_t aio_fsync(struct kiocb *iocb)
  1189. {
  1190. struct file *file = iocb->ki_filp;
  1191. ssize_t ret = -EINVAL;
  1192. if (file->f_op->aio_fsync)
  1193. ret = file->f_op->aio_fsync(iocb, 0);
  1194. return ret;
  1195. }
  1196. static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb)
  1197. {
  1198. ssize_t ret;
  1199. ret = rw_copy_check_uvector(type, (struct iovec __user *)kiocb->ki_buf,
  1200. kiocb->ki_nbytes, 1,
  1201. &kiocb->ki_inline_vec, &kiocb->ki_iovec);
  1202. if (ret < 0)
  1203. goto out;
  1204. kiocb->ki_nr_segs = kiocb->ki_nbytes;
  1205. kiocb->ki_cur_seg = 0;
  1206. /* ki_nbytes/left now reflect bytes instead of segs */
  1207. kiocb->ki_nbytes = ret;
  1208. kiocb->ki_left = ret;
  1209. ret = 0;
  1210. out:
  1211. return ret;
  1212. }
  1213. static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
  1214. {
  1215. kiocb->ki_iovec = &kiocb->ki_inline_vec;
  1216. kiocb->ki_iovec->iov_base = kiocb->ki_buf;
  1217. kiocb->ki_iovec->iov_len = kiocb->ki_left;
  1218. kiocb->ki_nr_segs = 1;
  1219. kiocb->ki_cur_seg = 0;
  1220. return 0;
  1221. }
  1222. /*
  1223. * aio_setup_iocb:
  1224. * Performs the initial checks and aio retry method
  1225. * setup for the kiocb at the time of io submission.
  1226. */
  1227. static ssize_t aio_setup_iocb(struct kiocb *kiocb)
  1228. {
  1229. struct file *file = kiocb->ki_filp;
  1230. ssize_t ret = 0;
  1231. switch (kiocb->ki_opcode) {
  1232. case IOCB_CMD_PREAD:
  1233. ret = -EBADF;
  1234. if (unlikely(!(file->f_mode & FMODE_READ)))
  1235. break;
  1236. ret = -EFAULT;
  1237. if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
  1238. kiocb->ki_left)))
  1239. break;
  1240. ret = security_file_permission(file, MAY_READ);
  1241. if (unlikely(ret))
  1242. break;
  1243. ret = aio_setup_single_vector(kiocb);
  1244. if (ret)
  1245. break;
  1246. ret = -EINVAL;
  1247. if (file->f_op->aio_read)
  1248. kiocb->ki_retry = aio_rw_vect_retry;
  1249. break;
  1250. case IOCB_CMD_PWRITE:
  1251. ret = -EBADF;
  1252. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1253. break;
  1254. ret = -EFAULT;
  1255. if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
  1256. kiocb->ki_left)))
  1257. break;
  1258. ret = security_file_permission(file, MAY_WRITE);
  1259. if (unlikely(ret))
  1260. break;
  1261. ret = aio_setup_single_vector(kiocb);
  1262. if (ret)
  1263. break;
  1264. ret = -EINVAL;
  1265. if (file->f_op->aio_write)
  1266. kiocb->ki_retry = aio_rw_vect_retry;
  1267. break;
  1268. case IOCB_CMD_PREADV:
  1269. ret = -EBADF;
  1270. if (unlikely(!(file->f_mode & FMODE_READ)))
  1271. break;
  1272. ret = security_file_permission(file, MAY_READ);
  1273. if (unlikely(ret))
  1274. break;
  1275. ret = aio_setup_vectored_rw(READ, kiocb);
  1276. if (ret)
  1277. break;
  1278. ret = -EINVAL;
  1279. if (file->f_op->aio_read)
  1280. kiocb->ki_retry = aio_rw_vect_retry;
  1281. break;
  1282. case IOCB_CMD_PWRITEV:
  1283. ret = -EBADF;
  1284. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1285. break;
  1286. ret = security_file_permission(file, MAY_WRITE);
  1287. if (unlikely(ret))
  1288. break;
  1289. ret = aio_setup_vectored_rw(WRITE, kiocb);
  1290. if (ret)
  1291. break;
  1292. ret = -EINVAL;
  1293. if (file->f_op->aio_write)
  1294. kiocb->ki_retry = aio_rw_vect_retry;
  1295. break;
  1296. case IOCB_CMD_FDSYNC:
  1297. ret = -EINVAL;
  1298. if (file->f_op->aio_fsync)
  1299. kiocb->ki_retry = aio_fdsync;
  1300. break;
  1301. case IOCB_CMD_FSYNC:
  1302. ret = -EINVAL;
  1303. if (file->f_op->aio_fsync)
  1304. kiocb->ki_retry = aio_fsync;
  1305. break;
  1306. default:
  1307. dprintk("EINVAL: io_submit: no operation provided\n");
  1308. ret = -EINVAL;
  1309. }
  1310. if (!kiocb->ki_retry)
  1311. return ret;
  1312. return 0;
  1313. }
  1314. /*
  1315. * aio_wake_function:
  1316. * wait queue callback function for aio notification,
  1317. * Simply triggers a retry of the operation via kick_iocb.
  1318. *
  1319. * This callback is specified in the wait queue entry in
  1320. * a kiocb.
  1321. *
  1322. * Note:
  1323. * This routine is executed with the wait queue lock held.
  1324. * Since kick_iocb acquires iocb->ctx->ctx_lock, it nests
  1325. * the ioctx lock inside the wait queue lock. This is safe
  1326. * because this callback isn't used for wait queues which
  1327. * are nested inside ioctx lock (i.e. ctx->wait)
  1328. */
  1329. static int aio_wake_function(wait_queue_t *wait, unsigned mode,
  1330. int sync, void *key)
  1331. {
  1332. struct kiocb *iocb = container_of(wait, struct kiocb, ki_wait);
  1333. list_del_init(&wait->task_list);
  1334. kick_iocb(iocb);
  1335. return 1;
  1336. }
  1337. static void aio_batch_add(struct address_space *mapping,
  1338. struct hlist_head *batch_hash)
  1339. {
  1340. struct aio_batch_entry *abe;
  1341. struct hlist_node *pos;
  1342. unsigned bucket;
  1343. bucket = hash_ptr(mapping, AIO_BATCH_HASH_BITS);
  1344. hlist_for_each_entry(abe, pos, &batch_hash[bucket], list) {
  1345. if (abe->mapping == mapping)
  1346. return;
  1347. }
  1348. abe = mempool_alloc(abe_pool, GFP_KERNEL);
  1349. BUG_ON(!igrab(mapping->host));
  1350. abe->mapping = mapping;
  1351. hlist_add_head(&abe->list, &batch_hash[bucket]);
  1352. return;
  1353. }
  1354. static void aio_batch_free(struct hlist_head *batch_hash)
  1355. {
  1356. struct aio_batch_entry *abe;
  1357. struct hlist_node *pos, *n;
  1358. int i;
  1359. for (i = 0; i < AIO_BATCH_HASH_SIZE; i++) {
  1360. hlist_for_each_entry_safe(abe, pos, n, &batch_hash[i], list) {
  1361. blk_run_address_space(abe->mapping);
  1362. iput(abe->mapping->host);
  1363. hlist_del(&abe->list);
  1364. mempool_free(abe, abe_pool);
  1365. }
  1366. }
  1367. }
  1368. static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  1369. struct iocb *iocb, struct hlist_head *batch_hash)
  1370. {
  1371. struct kiocb *req;
  1372. struct file *file;
  1373. ssize_t ret;
  1374. /* enforce forwards compatibility on users */
  1375. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
  1376. pr_debug("EINVAL: io_submit: reserve field set\n");
  1377. return -EINVAL;
  1378. }
  1379. /* prevent overflows */
  1380. if (unlikely(
  1381. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  1382. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  1383. ((ssize_t)iocb->aio_nbytes < 0)
  1384. )) {
  1385. pr_debug("EINVAL: io_submit: overflow check\n");
  1386. return -EINVAL;
  1387. }
  1388. file = fget(iocb->aio_fildes);
  1389. if (unlikely(!file))
  1390. return -EBADF;
  1391. req = aio_get_req(ctx); /* returns with 2 references to req */
  1392. if (unlikely(!req)) {
  1393. fput(file);
  1394. return -EAGAIN;
  1395. }
  1396. req->ki_filp = file;
  1397. if (iocb->aio_flags & IOCB_FLAG_RESFD) {
  1398. /*
  1399. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  1400. * instance of the file* now. The file descriptor must be
  1401. * an eventfd() fd, and will be signaled for each completed
  1402. * event using the eventfd_signal() function.
  1403. */
  1404. req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
  1405. if (IS_ERR(req->ki_eventfd)) {
  1406. ret = PTR_ERR(req->ki_eventfd);
  1407. req->ki_eventfd = NULL;
  1408. goto out_put_req;
  1409. }
  1410. }
  1411. ret = put_user(req->ki_key, &user_iocb->aio_key);
  1412. if (unlikely(ret)) {
  1413. dprintk("EFAULT: aio_key\n");
  1414. goto out_put_req;
  1415. }
  1416. req->ki_obj.user = user_iocb;
  1417. req->ki_user_data = iocb->aio_data;
  1418. req->ki_pos = iocb->aio_offset;
  1419. req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
  1420. req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
  1421. req->ki_opcode = iocb->aio_lio_opcode;
  1422. init_waitqueue_func_entry(&req->ki_wait, aio_wake_function);
  1423. INIT_LIST_HEAD(&req->ki_wait.task_list);
  1424. ret = aio_setup_iocb(req);
  1425. if (ret)
  1426. goto out_put_req;
  1427. spin_lock_irq(&ctx->ctx_lock);
  1428. aio_run_iocb(req);
  1429. if (!list_empty(&ctx->run_list)) {
  1430. /* drain the run list */
  1431. while (__aio_run_iocbs(ctx))
  1432. ;
  1433. }
  1434. spin_unlock_irq(&ctx->ctx_lock);
  1435. if (req->ki_opcode == IOCB_CMD_PREAD ||
  1436. req->ki_opcode == IOCB_CMD_PREADV ||
  1437. req->ki_opcode == IOCB_CMD_PWRITE ||
  1438. req->ki_opcode == IOCB_CMD_PWRITEV)
  1439. aio_batch_add(file->f_mapping, batch_hash);
  1440. aio_put_req(req); /* drop extra ref to req */
  1441. return 0;
  1442. out_put_req:
  1443. aio_put_req(req); /* drop extra ref to req */
  1444. aio_put_req(req); /* drop i/o ref to req */
  1445. return ret;
  1446. }
  1447. /* sys_io_submit:
  1448. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1449. * the number of iocbs queued. May return -EINVAL if the aio_context
  1450. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1451. * *iocbpp[0] is not properly initialized, if the operation specified
  1452. * is invalid for the file descriptor in the iocb. May fail with
  1453. * -EFAULT if any of the data structures point to invalid data. May
  1454. * fail with -EBADF if the file descriptor specified in the first
  1455. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1456. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1457. * fail with -ENOSYS if not implemented.
  1458. */
  1459. SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
  1460. struct iocb __user * __user *, iocbpp)
  1461. {
  1462. struct kioctx *ctx;
  1463. long ret = 0;
  1464. int i;
  1465. struct hlist_head batch_hash[AIO_BATCH_HASH_SIZE] = { { 0, }, };
  1466. if (unlikely(nr < 0))
  1467. return -EINVAL;
  1468. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1469. return -EFAULT;
  1470. ctx = lookup_ioctx(ctx_id);
  1471. if (unlikely(!ctx)) {
  1472. pr_debug("EINVAL: io_submit: invalid context id\n");
  1473. return -EINVAL;
  1474. }
  1475. /*
  1476. * AKPM: should this return a partial result if some of the IOs were
  1477. * successfully submitted?
  1478. */
  1479. for (i=0; i<nr; i++) {
  1480. struct iocb __user *user_iocb;
  1481. struct iocb tmp;
  1482. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1483. ret = -EFAULT;
  1484. break;
  1485. }
  1486. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1487. ret = -EFAULT;
  1488. break;
  1489. }
  1490. ret = io_submit_one(ctx, user_iocb, &tmp, batch_hash);
  1491. if (ret)
  1492. break;
  1493. }
  1494. aio_batch_free(batch_hash);
  1495. put_ioctx(ctx);
  1496. return i ? i : ret;
  1497. }
  1498. /* lookup_kiocb
  1499. * Finds a given iocb for cancellation.
  1500. */
  1501. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1502. u32 key)
  1503. {
  1504. struct list_head *pos;
  1505. assert_spin_locked(&ctx->ctx_lock);
  1506. /* TODO: use a hash or array, this sucks. */
  1507. list_for_each(pos, &ctx->active_reqs) {
  1508. struct kiocb *kiocb = list_kiocb(pos);
  1509. if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
  1510. return kiocb;
  1511. }
  1512. return NULL;
  1513. }
  1514. /* sys_io_cancel:
  1515. * Attempts to cancel an iocb previously passed to io_submit. If
  1516. * the operation is successfully cancelled, the resulting event is
  1517. * copied into the memory pointed to by result without being placed
  1518. * into the completion queue and 0 is returned. May fail with
  1519. * -EFAULT if any of the data structures pointed to are invalid.
  1520. * May fail with -EINVAL if aio_context specified by ctx_id is
  1521. * invalid. May fail with -EAGAIN if the iocb specified was not
  1522. * cancelled. Will fail with -ENOSYS if not implemented.
  1523. */
  1524. SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
  1525. struct io_event __user *, result)
  1526. {
  1527. int (*cancel)(struct kiocb *iocb, struct io_event *res);
  1528. struct kioctx *ctx;
  1529. struct kiocb *kiocb;
  1530. u32 key;
  1531. int ret;
  1532. ret = get_user(key, &iocb->aio_key);
  1533. if (unlikely(ret))
  1534. return -EFAULT;
  1535. ctx = lookup_ioctx(ctx_id);
  1536. if (unlikely(!ctx))
  1537. return -EINVAL;
  1538. spin_lock_irq(&ctx->ctx_lock);
  1539. ret = -EAGAIN;
  1540. kiocb = lookup_kiocb(ctx, iocb, key);
  1541. if (kiocb && kiocb->ki_cancel) {
  1542. cancel = kiocb->ki_cancel;
  1543. kiocb->ki_users ++;
  1544. kiocbSetCancelled(kiocb);
  1545. } else
  1546. cancel = NULL;
  1547. spin_unlock_irq(&ctx->ctx_lock);
  1548. if (NULL != cancel) {
  1549. struct io_event tmp;
  1550. pr_debug("calling cancel\n");
  1551. memset(&tmp, 0, sizeof(tmp));
  1552. tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
  1553. tmp.data = kiocb->ki_user_data;
  1554. ret = cancel(kiocb, &tmp);
  1555. if (!ret) {
  1556. /* Cancellation succeeded -- copy the result
  1557. * into the user's buffer.
  1558. */
  1559. if (copy_to_user(result, &tmp, sizeof(tmp)))
  1560. ret = -EFAULT;
  1561. }
  1562. } else
  1563. ret = -EINVAL;
  1564. put_ioctx(ctx);
  1565. return ret;
  1566. }
  1567. /* io_getevents:
  1568. * Attempts to read at least min_nr events and up to nr events from
  1569. * the completion queue for the aio_context specified by ctx_id. May
  1570. * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
  1571. * if nr is out of range, if when is out of range. May fail with
  1572. * -EFAULT if any of the memory specified to is invalid. May return
  1573. * 0 or < min_nr if no events are available and the timeout specified
  1574. * by when has elapsed, where when == NULL specifies an infinite
  1575. * timeout. Note that the timeout pointed to by when is relative and
  1576. * will be updated if not NULL and the operation blocks. Will fail
  1577. * with -ENOSYS if not implemented.
  1578. */
  1579. SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
  1580. long, min_nr,
  1581. long, nr,
  1582. struct io_event __user *, events,
  1583. struct timespec __user *, timeout)
  1584. {
  1585. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1586. long ret = -EINVAL;
  1587. if (likely(ioctx)) {
  1588. if (likely(min_nr <= nr && min_nr >= 0 && nr >= 0))
  1589. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1590. put_ioctx(ioctx);
  1591. }
  1592. asmlinkage_protect(5, ret, ctx_id, min_nr, nr, events, timeout);
  1593. return ret;
  1594. }