aio.c 47 KB

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