aio.c 46 KB

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