aio.c 47 KB

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