aio.c 45 KB

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