aio.c 48 KB

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