aio.c 47 KB

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