aio.c 46 KB

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