aio.c 46 KB

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