aio.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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. #define pr_fmt(fmt) "%s: " fmt, __func__
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/time.h>
  16. #include <linux/aio_abi.h>
  17. #include <linux/export.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/backing-dev.h>
  20. #include <linux/uio.h>
  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. #define AIO_RING_MAGIC 0xa10a10a1
  39. #define AIO_RING_COMPAT_FEATURES 1
  40. #define AIO_RING_INCOMPAT_FEATURES 0
  41. struct aio_ring {
  42. unsigned id; /* kernel internal index number */
  43. unsigned nr; /* number of io_events */
  44. unsigned head;
  45. unsigned tail;
  46. unsigned magic;
  47. unsigned compat_features;
  48. unsigned incompat_features;
  49. unsigned header_length; /* size of aio_ring */
  50. struct io_event io_events[0];
  51. }; /* 128 bytes + ring size */
  52. #define AIO_RING_PAGES 8
  53. struct aio_ring_info {
  54. unsigned long mmap_base;
  55. unsigned long mmap_size;
  56. struct page **ring_pages;
  57. struct mutex ring_lock;
  58. long nr_pages;
  59. unsigned nr, tail;
  60. struct page *internal_pages[AIO_RING_PAGES];
  61. };
  62. static inline unsigned aio_ring_avail(struct aio_ring_info *info,
  63. struct aio_ring *ring)
  64. {
  65. return (ring->head + info->nr - 1 - ring->tail) % info->nr;
  66. }
  67. struct kioctx {
  68. atomic_t users;
  69. atomic_t dead;
  70. /* This needs improving */
  71. unsigned long user_id;
  72. struct hlist_node list;
  73. wait_queue_head_t wait;
  74. spinlock_t ctx_lock;
  75. atomic_t reqs_active;
  76. struct list_head active_reqs; /* used for cancellation */
  77. /* sys_io_setup currently limits this to an unsigned int */
  78. unsigned max_reqs;
  79. struct aio_ring_info ring_info;
  80. spinlock_t completion_lock;
  81. struct rcu_head rcu_head;
  82. struct work_struct rcu_work;
  83. };
  84. /*------ sysctl variables----*/
  85. static DEFINE_SPINLOCK(aio_nr_lock);
  86. unsigned long aio_nr; /* current system wide number of aio requests */
  87. unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
  88. /*----end sysctl variables---*/
  89. static struct kmem_cache *kiocb_cachep;
  90. static struct kmem_cache *kioctx_cachep;
  91. /* aio_setup
  92. * Creates the slab caches used by the aio routines, panic on
  93. * failure as this is done early during the boot sequence.
  94. */
  95. static int __init aio_setup(void)
  96. {
  97. kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  98. kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  99. pr_debug("sizeof(struct page) = %zu\n", sizeof(struct page));
  100. return 0;
  101. }
  102. __initcall(aio_setup);
  103. static void aio_free_ring(struct kioctx *ctx)
  104. {
  105. struct aio_ring_info *info = &ctx->ring_info;
  106. long i;
  107. for (i=0; i<info->nr_pages; i++)
  108. put_page(info->ring_pages[i]);
  109. if (info->mmap_size) {
  110. vm_munmap(info->mmap_base, info->mmap_size);
  111. }
  112. if (info->ring_pages && info->ring_pages != info->internal_pages)
  113. kfree(info->ring_pages);
  114. info->ring_pages = NULL;
  115. info->nr = 0;
  116. }
  117. static int aio_setup_ring(struct kioctx *ctx)
  118. {
  119. struct aio_ring *ring;
  120. struct aio_ring_info *info = &ctx->ring_info;
  121. unsigned nr_events = ctx->max_reqs;
  122. struct mm_struct *mm = current->mm;
  123. unsigned long size, populate;
  124. int nr_pages;
  125. /* Compensate for the ring buffer's head/tail overlap entry */
  126. nr_events += 2; /* 1 is required, 2 for good luck */
  127. size = sizeof(struct aio_ring);
  128. size += sizeof(struct io_event) * nr_events;
  129. nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
  130. if (nr_pages < 0)
  131. return -EINVAL;
  132. nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
  133. info->nr = 0;
  134. info->ring_pages = info->internal_pages;
  135. if (nr_pages > AIO_RING_PAGES) {
  136. info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  137. if (!info->ring_pages)
  138. return -ENOMEM;
  139. }
  140. info->mmap_size = nr_pages * PAGE_SIZE;
  141. pr_debug("attempting mmap of %lu bytes\n", info->mmap_size);
  142. down_write(&mm->mmap_sem);
  143. info->mmap_base = do_mmap_pgoff(NULL, 0, info->mmap_size,
  144. PROT_READ|PROT_WRITE,
  145. MAP_ANONYMOUS|MAP_PRIVATE, 0,
  146. &populate);
  147. if (IS_ERR((void *)info->mmap_base)) {
  148. up_write(&mm->mmap_sem);
  149. info->mmap_size = 0;
  150. aio_free_ring(ctx);
  151. return -EAGAIN;
  152. }
  153. pr_debug("mmap address: 0x%08lx\n", info->mmap_base);
  154. info->nr_pages = get_user_pages(current, mm, info->mmap_base, nr_pages,
  155. 1, 0, info->ring_pages, NULL);
  156. up_write(&mm->mmap_sem);
  157. if (unlikely(info->nr_pages != nr_pages)) {
  158. aio_free_ring(ctx);
  159. return -EAGAIN;
  160. }
  161. if (populate)
  162. mm_populate(info->mmap_base, populate);
  163. ctx->user_id = info->mmap_base;
  164. info->nr = nr_events; /* trusted copy */
  165. ring = kmap_atomic(info->ring_pages[0]);
  166. ring->nr = nr_events; /* user copy */
  167. ring->id = ctx->user_id;
  168. ring->head = ring->tail = 0;
  169. ring->magic = AIO_RING_MAGIC;
  170. ring->compat_features = AIO_RING_COMPAT_FEATURES;
  171. ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
  172. ring->header_length = sizeof(struct aio_ring);
  173. kunmap_atomic(ring);
  174. flush_dcache_page(info->ring_pages[0]);
  175. return 0;
  176. }
  177. #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
  178. #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
  179. #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
  180. void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel)
  181. {
  182. struct kioctx *ctx = req->ki_ctx;
  183. unsigned long flags;
  184. spin_lock_irqsave(&ctx->ctx_lock, flags);
  185. if (!req->ki_list.next)
  186. list_add(&req->ki_list, &ctx->active_reqs);
  187. req->ki_cancel = cancel;
  188. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  189. }
  190. EXPORT_SYMBOL(kiocb_set_cancel_fn);
  191. static int kiocb_cancel(struct kioctx *ctx, struct kiocb *kiocb,
  192. struct io_event *res)
  193. {
  194. kiocb_cancel_fn *old, *cancel;
  195. int ret = -EINVAL;
  196. /*
  197. * Don't want to set kiocb->ki_cancel = KIOCB_CANCELLED unless it
  198. * actually has a cancel function, hence the cmpxchg()
  199. */
  200. cancel = ACCESS_ONCE(kiocb->ki_cancel);
  201. do {
  202. if (!cancel || cancel == KIOCB_CANCELLED)
  203. return ret;
  204. old = cancel;
  205. cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
  206. } while (cancel != old);
  207. atomic_inc(&kiocb->ki_users);
  208. spin_unlock_irq(&ctx->ctx_lock);
  209. memset(res, 0, sizeof(*res));
  210. res->obj = (u64)(unsigned long)kiocb->ki_obj.user;
  211. res->data = kiocb->ki_user_data;
  212. ret = cancel(kiocb, res);
  213. spin_lock_irq(&ctx->ctx_lock);
  214. return ret;
  215. }
  216. static void free_ioctx_rcu(struct rcu_head *head)
  217. {
  218. struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
  219. kmem_cache_free(kioctx_cachep, ctx);
  220. }
  221. /*
  222. * When this function runs, the kioctx has been removed from the "hash table"
  223. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  224. * now it's safe to cancel any that need to be.
  225. */
  226. static void free_ioctx(struct kioctx *ctx)
  227. {
  228. struct io_event res;
  229. struct kiocb *req;
  230. spin_lock_irq(&ctx->ctx_lock);
  231. while (!list_empty(&ctx->active_reqs)) {
  232. req = list_first_entry(&ctx->active_reqs,
  233. struct kiocb, ki_list);
  234. list_del_init(&req->ki_list);
  235. kiocb_cancel(ctx, req, &res);
  236. }
  237. spin_unlock_irq(&ctx->ctx_lock);
  238. wait_event(ctx->wait, !atomic_read(&ctx->reqs_active));
  239. aio_free_ring(ctx);
  240. spin_lock(&aio_nr_lock);
  241. BUG_ON(aio_nr - ctx->max_reqs > aio_nr);
  242. aio_nr -= ctx->max_reqs;
  243. spin_unlock(&aio_nr_lock);
  244. pr_debug("freeing %p\n", ctx);
  245. /*
  246. * Here the call_rcu() is between the wait_event() for reqs_active to
  247. * hit 0, and freeing the ioctx.
  248. *
  249. * aio_complete() decrements reqs_active, but it has to touch the ioctx
  250. * after to issue a wakeup so we use rcu.
  251. */
  252. call_rcu(&ctx->rcu_head, free_ioctx_rcu);
  253. }
  254. static void put_ioctx(struct kioctx *ctx)
  255. {
  256. if (unlikely(atomic_dec_and_test(&ctx->users)))
  257. free_ioctx(ctx);
  258. }
  259. /* ioctx_alloc
  260. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  261. */
  262. static struct kioctx *ioctx_alloc(unsigned nr_events)
  263. {
  264. struct mm_struct *mm = current->mm;
  265. struct kioctx *ctx;
  266. int err = -ENOMEM;
  267. /* Prevent overflows */
  268. if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
  269. (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
  270. pr_debug("ENOMEM: nr_events too high\n");
  271. return ERR_PTR(-EINVAL);
  272. }
  273. if (!nr_events || (unsigned long)nr_events > aio_max_nr)
  274. return ERR_PTR(-EAGAIN);
  275. ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
  276. if (!ctx)
  277. return ERR_PTR(-ENOMEM);
  278. ctx->max_reqs = nr_events;
  279. atomic_set(&ctx->users, 2);
  280. atomic_set(&ctx->dead, 0);
  281. spin_lock_init(&ctx->ctx_lock);
  282. spin_lock_init(&ctx->completion_lock);
  283. mutex_init(&ctx->ring_info.ring_lock);
  284. init_waitqueue_head(&ctx->wait);
  285. INIT_LIST_HEAD(&ctx->active_reqs);
  286. if (aio_setup_ring(ctx) < 0)
  287. goto out_freectx;
  288. /* limit the number of system wide aios */
  289. spin_lock(&aio_nr_lock);
  290. if (aio_nr + nr_events > aio_max_nr ||
  291. aio_nr + nr_events < aio_nr) {
  292. spin_unlock(&aio_nr_lock);
  293. goto out_cleanup;
  294. }
  295. aio_nr += ctx->max_reqs;
  296. spin_unlock(&aio_nr_lock);
  297. /* now link into global list. */
  298. spin_lock(&mm->ioctx_lock);
  299. hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
  300. spin_unlock(&mm->ioctx_lock);
  301. pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  302. ctx, ctx->user_id, mm, ctx->ring_info.nr);
  303. return ctx;
  304. out_cleanup:
  305. err = -EAGAIN;
  306. aio_free_ring(ctx);
  307. out_freectx:
  308. kmem_cache_free(kioctx_cachep, ctx);
  309. pr_debug("error allocating ioctx %d\n", err);
  310. return ERR_PTR(err);
  311. }
  312. static void kill_ioctx_work(struct work_struct *work)
  313. {
  314. struct kioctx *ctx = container_of(work, struct kioctx, rcu_work);
  315. wake_up_all(&ctx->wait);
  316. put_ioctx(ctx);
  317. }
  318. static void kill_ioctx_rcu(struct rcu_head *head)
  319. {
  320. struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
  321. INIT_WORK(&ctx->rcu_work, kill_ioctx_work);
  322. schedule_work(&ctx->rcu_work);
  323. }
  324. /* kill_ioctx
  325. * Cancels all outstanding aio requests on an aio context. Used
  326. * when the processes owning a context have all exited to encourage
  327. * the rapid destruction of the kioctx.
  328. */
  329. static void kill_ioctx(struct kioctx *ctx)
  330. {
  331. if (!atomic_xchg(&ctx->dead, 1)) {
  332. hlist_del_rcu(&ctx->list);
  333. /* Between hlist_del_rcu() and dropping the initial ref */
  334. synchronize_rcu();
  335. /*
  336. * We can't punt to workqueue here because put_ioctx() ->
  337. * free_ioctx() will unmap the ringbuffer, and that has to be
  338. * done in the original process's context. kill_ioctx_rcu/work()
  339. * exist for exit_aio(), as in that path free_ioctx() won't do
  340. * the unmap.
  341. */
  342. kill_ioctx_work(&ctx->rcu_work);
  343. }
  344. }
  345. /* wait_on_sync_kiocb:
  346. * Waits on the given sync kiocb to complete.
  347. */
  348. ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
  349. {
  350. while (atomic_read(&iocb->ki_users)) {
  351. set_current_state(TASK_UNINTERRUPTIBLE);
  352. if (!atomic_read(&iocb->ki_users))
  353. break;
  354. io_schedule();
  355. }
  356. __set_current_state(TASK_RUNNING);
  357. return iocb->ki_user_data;
  358. }
  359. EXPORT_SYMBOL(wait_on_sync_kiocb);
  360. /*
  361. * exit_aio: called when the last user of mm goes away. At this point, there is
  362. * no way for any new requests to be submited or any of the io_* syscalls to be
  363. * called on the context.
  364. *
  365. * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
  366. * them.
  367. */
  368. void exit_aio(struct mm_struct *mm)
  369. {
  370. struct kioctx *ctx;
  371. struct hlist_node *n;
  372. hlist_for_each_entry_safe(ctx, n, &mm->ioctx_list, list) {
  373. if (1 != atomic_read(&ctx->users))
  374. printk(KERN_DEBUG
  375. "exit_aio:ioctx still alive: %d %d %d\n",
  376. atomic_read(&ctx->users),
  377. atomic_read(&ctx->dead),
  378. atomic_read(&ctx->reqs_active));
  379. /*
  380. * We don't need to bother with munmap() here -
  381. * exit_mmap(mm) is coming and it'll unmap everything.
  382. * Since aio_free_ring() uses non-zero ->mmap_size
  383. * as indicator that it needs to unmap the area,
  384. * just set it to 0; aio_free_ring() is the only
  385. * place that uses ->mmap_size, so it's safe.
  386. */
  387. ctx->ring_info.mmap_size = 0;
  388. if (!atomic_xchg(&ctx->dead, 1)) {
  389. hlist_del_rcu(&ctx->list);
  390. call_rcu(&ctx->rcu_head, kill_ioctx_rcu);
  391. }
  392. }
  393. }
  394. /* aio_get_req
  395. * Allocate a slot for an aio request. Increments the ki_users count
  396. * of the kioctx so that the kioctx stays around until all requests are
  397. * complete. Returns NULL if no requests are free.
  398. *
  399. * Returns with kiocb->ki_users set to 2. The io submit code path holds
  400. * an extra reference while submitting the i/o.
  401. * This prevents races between the aio code path referencing the
  402. * req (after submitting it) and aio_complete() freeing the req.
  403. */
  404. static struct kiocb *__aio_get_req(struct kioctx *ctx)
  405. {
  406. struct kiocb *req = NULL;
  407. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
  408. if (unlikely(!req))
  409. return NULL;
  410. atomic_set(&req->ki_users, 2);
  411. req->ki_ctx = ctx;
  412. return req;
  413. }
  414. /*
  415. * struct kiocb's are allocated in batches to reduce the number of
  416. * times the ctx lock is acquired and released.
  417. */
  418. #define KIOCB_BATCH_SIZE 32L
  419. struct kiocb_batch {
  420. struct list_head head;
  421. long count; /* number of requests left to allocate */
  422. };
  423. static void kiocb_batch_init(struct kiocb_batch *batch, long total)
  424. {
  425. INIT_LIST_HEAD(&batch->head);
  426. batch->count = total;
  427. }
  428. static void kiocb_batch_free(struct kioctx *ctx, struct kiocb_batch *batch)
  429. {
  430. struct kiocb *req, *n;
  431. if (list_empty(&batch->head))
  432. return;
  433. spin_lock_irq(&ctx->ctx_lock);
  434. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  435. list_del(&req->ki_batch);
  436. kmem_cache_free(kiocb_cachep, req);
  437. atomic_dec(&ctx->reqs_active);
  438. }
  439. spin_unlock_irq(&ctx->ctx_lock);
  440. }
  441. /*
  442. * Allocate a batch of kiocbs. This avoids taking and dropping the
  443. * context lock a lot during setup.
  444. */
  445. static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
  446. {
  447. unsigned short allocated, to_alloc;
  448. long avail;
  449. struct kiocb *req, *n;
  450. struct aio_ring *ring;
  451. to_alloc = min(batch->count, KIOCB_BATCH_SIZE);
  452. for (allocated = 0; allocated < to_alloc; allocated++) {
  453. req = __aio_get_req(ctx);
  454. if (!req)
  455. /* allocation failed, go with what we've got */
  456. break;
  457. list_add(&req->ki_batch, &batch->head);
  458. }
  459. if (allocated == 0)
  460. goto out;
  461. spin_lock_irq(&ctx->ctx_lock);
  462. ring = kmap_atomic(ctx->ring_info.ring_pages[0]);
  463. avail = aio_ring_avail(&ctx->ring_info, ring) -
  464. atomic_read(&ctx->reqs_active);
  465. BUG_ON(avail < 0);
  466. if (avail < allocated) {
  467. /* Trim back the number of requests. */
  468. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  469. list_del(&req->ki_batch);
  470. kmem_cache_free(kiocb_cachep, req);
  471. if (--allocated <= avail)
  472. break;
  473. }
  474. }
  475. batch->count -= allocated;
  476. atomic_add(allocated, &ctx->reqs_active);
  477. kunmap_atomic(ring);
  478. spin_unlock_irq(&ctx->ctx_lock);
  479. out:
  480. return allocated;
  481. }
  482. static inline struct kiocb *aio_get_req(struct kioctx *ctx,
  483. struct kiocb_batch *batch)
  484. {
  485. struct kiocb *req;
  486. if (list_empty(&batch->head))
  487. if (kiocb_batch_refill(ctx, batch) == 0)
  488. return NULL;
  489. req = list_first_entry(&batch->head, struct kiocb, ki_batch);
  490. list_del(&req->ki_batch);
  491. return req;
  492. }
  493. static void kiocb_free(struct kiocb *req)
  494. {
  495. if (req->ki_filp)
  496. fput(req->ki_filp);
  497. if (req->ki_eventfd != NULL)
  498. eventfd_ctx_put(req->ki_eventfd);
  499. if (req->ki_dtor)
  500. req->ki_dtor(req);
  501. if (req->ki_iovec != &req->ki_inline_vec)
  502. kfree(req->ki_iovec);
  503. kmem_cache_free(kiocb_cachep, req);
  504. }
  505. void aio_put_req(struct kiocb *req)
  506. {
  507. if (atomic_dec_and_test(&req->ki_users))
  508. kiocb_free(req);
  509. }
  510. EXPORT_SYMBOL(aio_put_req);
  511. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  512. {
  513. struct mm_struct *mm = current->mm;
  514. struct kioctx *ctx, *ret = NULL;
  515. rcu_read_lock();
  516. hlist_for_each_entry_rcu(ctx, &mm->ioctx_list, list) {
  517. if (ctx->user_id == ctx_id) {
  518. atomic_inc(&ctx->users);
  519. ret = ctx;
  520. break;
  521. }
  522. }
  523. rcu_read_unlock();
  524. return ret;
  525. }
  526. /* aio_complete
  527. * Called when the io request on the given iocb is complete.
  528. */
  529. void aio_complete(struct kiocb *iocb, long res, long res2)
  530. {
  531. struct kioctx *ctx = iocb->ki_ctx;
  532. struct aio_ring_info *info;
  533. struct aio_ring *ring;
  534. struct io_event *ev_page, *event;
  535. unsigned long flags;
  536. unsigned tail, pos;
  537. /*
  538. * Special case handling for sync iocbs:
  539. * - events go directly into the iocb for fast handling
  540. * - the sync task with the iocb in its stack holds the single iocb
  541. * ref, no other paths have a way to get another ref
  542. * - the sync task helpfully left a reference to itself in the iocb
  543. */
  544. if (is_sync_kiocb(iocb)) {
  545. BUG_ON(atomic_read(&iocb->ki_users) != 1);
  546. iocb->ki_user_data = res;
  547. atomic_set(&iocb->ki_users, 0);
  548. wake_up_process(iocb->ki_obj.tsk);
  549. return;
  550. }
  551. info = &ctx->ring_info;
  552. /*
  553. * Take rcu_read_lock() in case the kioctx is being destroyed, as we
  554. * need to issue a wakeup after decrementing reqs_active.
  555. */
  556. rcu_read_lock();
  557. if (iocb->ki_list.next) {
  558. unsigned long flags;
  559. spin_lock_irqsave(&ctx->ctx_lock, flags);
  560. list_del(&iocb->ki_list);
  561. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  562. }
  563. /*
  564. * cancelled requests don't get events, userland was given one
  565. * when the event got cancelled.
  566. */
  567. if (unlikely(xchg(&iocb->ki_cancel,
  568. KIOCB_CANCELLED) == KIOCB_CANCELLED))
  569. goto put_rq;
  570. /*
  571. * Add a completion event to the ring buffer. Must be done holding
  572. * ctx->ctx_lock to prevent other code from messing with the tail
  573. * pointer since we might be called from irq context.
  574. */
  575. spin_lock_irqsave(&ctx->completion_lock, flags);
  576. tail = info->tail;
  577. pos = tail + AIO_EVENTS_OFFSET;
  578. if (++tail >= info->nr)
  579. tail = 0;
  580. ev_page = kmap_atomic(info->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  581. event = ev_page + pos % AIO_EVENTS_PER_PAGE;
  582. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  583. event->data = iocb->ki_user_data;
  584. event->res = res;
  585. event->res2 = res2;
  586. kunmap_atomic(ev_page);
  587. flush_dcache_page(info->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  588. pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
  589. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  590. res, res2);
  591. /* after flagging the request as done, we
  592. * must never even look at it again
  593. */
  594. smp_wmb(); /* make event visible before updating tail */
  595. info->tail = tail;
  596. ring = kmap_atomic(info->ring_pages[0]);
  597. ring->tail = tail;
  598. kunmap_atomic(ring);
  599. flush_dcache_page(info->ring_pages[0]);
  600. spin_unlock_irqrestore(&ctx->completion_lock, flags);
  601. pr_debug("added to ring %p at [%u]\n", iocb, tail);
  602. /*
  603. * Check if the user asked us to deliver the result through an
  604. * eventfd. The eventfd_signal() function is safe to be called
  605. * from IRQ context.
  606. */
  607. if (iocb->ki_eventfd != NULL)
  608. eventfd_signal(iocb->ki_eventfd, 1);
  609. put_rq:
  610. /* everything turned out well, dispose of the aiocb. */
  611. aio_put_req(iocb);
  612. atomic_dec(&ctx->reqs_active);
  613. /*
  614. * We have to order our ring_info tail store above and test
  615. * of the wait list below outside the wait lock. This is
  616. * like in wake_up_bit() where clearing a bit has to be
  617. * ordered with the unlocked test.
  618. */
  619. smp_mb();
  620. if (waitqueue_active(&ctx->wait))
  621. wake_up(&ctx->wait);
  622. rcu_read_unlock();
  623. }
  624. EXPORT_SYMBOL(aio_complete);
  625. /* aio_read_events
  626. * Pull an event off of the ioctx's event ring. Returns the number of
  627. * events fetched
  628. */
  629. static long aio_read_events_ring(struct kioctx *ctx,
  630. struct io_event __user *event, long nr)
  631. {
  632. struct aio_ring_info *info = &ctx->ring_info;
  633. struct aio_ring *ring;
  634. unsigned head, pos;
  635. long ret = 0;
  636. int copy_ret;
  637. mutex_lock(&info->ring_lock);
  638. ring = kmap_atomic(info->ring_pages[0]);
  639. head = ring->head;
  640. kunmap_atomic(ring);
  641. pr_debug("h%u t%u m%u\n", head, info->tail, info->nr);
  642. if (head == info->tail)
  643. goto out;
  644. while (ret < nr) {
  645. long avail;
  646. struct io_event *ev;
  647. struct page *page;
  648. avail = (head <= info->tail ? info->tail : info->nr) - head;
  649. if (head == info->tail)
  650. break;
  651. avail = min(avail, nr - ret);
  652. avail = min_t(long, avail, AIO_EVENTS_PER_PAGE -
  653. ((head + AIO_EVENTS_OFFSET) % AIO_EVENTS_PER_PAGE));
  654. pos = head + AIO_EVENTS_OFFSET;
  655. page = info->ring_pages[pos / AIO_EVENTS_PER_PAGE];
  656. pos %= AIO_EVENTS_PER_PAGE;
  657. ev = kmap(page);
  658. copy_ret = copy_to_user(event + ret, ev + pos,
  659. sizeof(*ev) * avail);
  660. kunmap(page);
  661. if (unlikely(copy_ret)) {
  662. ret = -EFAULT;
  663. goto out;
  664. }
  665. ret += avail;
  666. head += avail;
  667. head %= info->nr;
  668. }
  669. ring = kmap_atomic(info->ring_pages[0]);
  670. ring->head = head;
  671. kunmap_atomic(ring);
  672. flush_dcache_page(info->ring_pages[0]);
  673. pr_debug("%li h%u t%u\n", ret, head, info->tail);
  674. out:
  675. mutex_unlock(&info->ring_lock);
  676. return ret;
  677. }
  678. static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
  679. struct io_event __user *event, long *i)
  680. {
  681. long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
  682. if (ret > 0)
  683. *i += ret;
  684. if (unlikely(atomic_read(&ctx->dead)))
  685. ret = -EINVAL;
  686. if (!*i)
  687. *i = ret;
  688. return ret < 0 || *i >= min_nr;
  689. }
  690. static long read_events(struct kioctx *ctx, long min_nr, long nr,
  691. struct io_event __user *event,
  692. struct timespec __user *timeout)
  693. {
  694. ktime_t until = { .tv64 = KTIME_MAX };
  695. long ret = 0;
  696. if (timeout) {
  697. struct timespec ts;
  698. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  699. return -EFAULT;
  700. until = timespec_to_ktime(ts);
  701. }
  702. /*
  703. * Note that aio_read_events() is being called as the conditional - i.e.
  704. * we're calling it after prepare_to_wait() has set task state to
  705. * TASK_INTERRUPTIBLE.
  706. *
  707. * But aio_read_events() can block, and if it blocks it's going to flip
  708. * the task state back to TASK_RUNNING.
  709. *
  710. * This should be ok, provided it doesn't flip the state back to
  711. * TASK_RUNNING and return 0 too much - that causes us to spin. That
  712. * will only happen if the mutex_lock() call blocks, and we then find
  713. * the ringbuffer empty. So in practice we should be ok, but it's
  714. * something to be aware of when touching this code.
  715. */
  716. wait_event_interruptible_hrtimeout(ctx->wait,
  717. aio_read_events(ctx, min_nr, nr, event, &ret), until);
  718. if (!ret && signal_pending(current))
  719. ret = -EINTR;
  720. return ret;
  721. }
  722. /* sys_io_setup:
  723. * Create an aio_context capable of receiving at least nr_events.
  724. * ctxp must not point to an aio_context that already exists, and
  725. * must be initialized to 0 prior to the call. On successful
  726. * creation of the aio_context, *ctxp is filled in with the resulting
  727. * handle. May fail with -EINVAL if *ctxp is not initialized,
  728. * if the specified nr_events exceeds internal limits. May fail
  729. * with -EAGAIN if the specified nr_events exceeds the user's limit
  730. * of available events. May fail with -ENOMEM if insufficient kernel
  731. * resources are available. May fail with -EFAULT if an invalid
  732. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  733. * implemented.
  734. */
  735. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  736. {
  737. struct kioctx *ioctx = NULL;
  738. unsigned long ctx;
  739. long ret;
  740. ret = get_user(ctx, ctxp);
  741. if (unlikely(ret))
  742. goto out;
  743. ret = -EINVAL;
  744. if (unlikely(ctx || nr_events == 0)) {
  745. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  746. ctx, nr_events);
  747. goto out;
  748. }
  749. ioctx = ioctx_alloc(nr_events);
  750. ret = PTR_ERR(ioctx);
  751. if (!IS_ERR(ioctx)) {
  752. ret = put_user(ioctx->user_id, ctxp);
  753. if (ret)
  754. kill_ioctx(ioctx);
  755. put_ioctx(ioctx);
  756. }
  757. out:
  758. return ret;
  759. }
  760. /* sys_io_destroy:
  761. * Destroy the aio_context specified. May cancel any outstanding
  762. * AIOs and block on completion. Will fail with -ENOSYS if not
  763. * implemented. May fail with -EINVAL if the context pointed to
  764. * is invalid.
  765. */
  766. SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
  767. {
  768. struct kioctx *ioctx = lookup_ioctx(ctx);
  769. if (likely(NULL != ioctx)) {
  770. kill_ioctx(ioctx);
  771. put_ioctx(ioctx);
  772. return 0;
  773. }
  774. pr_debug("EINVAL: io_destroy: invalid context id\n");
  775. return -EINVAL;
  776. }
  777. static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
  778. {
  779. struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
  780. BUG_ON(ret <= 0);
  781. while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
  782. ssize_t this = min((ssize_t)iov->iov_len, ret);
  783. iov->iov_base += this;
  784. iov->iov_len -= this;
  785. iocb->ki_left -= this;
  786. ret -= this;
  787. if (iov->iov_len == 0) {
  788. iocb->ki_cur_seg++;
  789. iov++;
  790. }
  791. }
  792. /* the caller should not have done more io than what fit in
  793. * the remaining iovecs */
  794. BUG_ON(ret > 0 && iocb->ki_left == 0);
  795. }
  796. static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
  797. {
  798. struct file *file = iocb->ki_filp;
  799. struct address_space *mapping = file->f_mapping;
  800. struct inode *inode = mapping->host;
  801. ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
  802. unsigned long, loff_t);
  803. ssize_t ret = 0;
  804. unsigned short opcode;
  805. if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
  806. (iocb->ki_opcode == IOCB_CMD_PREAD)) {
  807. rw_op = file->f_op->aio_read;
  808. opcode = IOCB_CMD_PREADV;
  809. } else {
  810. rw_op = file->f_op->aio_write;
  811. opcode = IOCB_CMD_PWRITEV;
  812. }
  813. /* This matches the pread()/pwrite() logic */
  814. if (iocb->ki_pos < 0)
  815. return -EINVAL;
  816. if (opcode == IOCB_CMD_PWRITEV)
  817. file_start_write(file);
  818. do {
  819. ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
  820. iocb->ki_nr_segs - iocb->ki_cur_seg,
  821. iocb->ki_pos);
  822. if (ret > 0)
  823. aio_advance_iovec(iocb, ret);
  824. /* retry all partial writes. retry partial reads as long as its a
  825. * regular file. */
  826. } while (ret > 0 && iocb->ki_left > 0 &&
  827. (opcode == IOCB_CMD_PWRITEV ||
  828. (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
  829. if (opcode == IOCB_CMD_PWRITEV)
  830. file_end_write(file);
  831. /* This means we must have transferred all that we could */
  832. /* No need to retry anymore */
  833. if ((ret == 0) || (iocb->ki_left == 0))
  834. ret = iocb->ki_nbytes - iocb->ki_left;
  835. /* If we managed to write some out we return that, rather than
  836. * the eventual error. */
  837. if (opcode == IOCB_CMD_PWRITEV
  838. && ret < 0 && ret != -EIOCBQUEUED
  839. && iocb->ki_nbytes - iocb->ki_left)
  840. ret = iocb->ki_nbytes - iocb->ki_left;
  841. return ret;
  842. }
  843. static ssize_t aio_fdsync(struct kiocb *iocb)
  844. {
  845. struct file *file = iocb->ki_filp;
  846. ssize_t ret = -EINVAL;
  847. if (file->f_op->aio_fsync)
  848. ret = file->f_op->aio_fsync(iocb, 1);
  849. return ret;
  850. }
  851. static ssize_t aio_fsync(struct kiocb *iocb)
  852. {
  853. struct file *file = iocb->ki_filp;
  854. ssize_t ret = -EINVAL;
  855. if (file->f_op->aio_fsync)
  856. ret = file->f_op->aio_fsync(iocb, 0);
  857. return ret;
  858. }
  859. static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
  860. {
  861. ssize_t ret;
  862. #ifdef CONFIG_COMPAT
  863. if (compat)
  864. ret = compat_rw_copy_check_uvector(type,
  865. (struct compat_iovec __user *)kiocb->ki_buf,
  866. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  867. &kiocb->ki_iovec);
  868. else
  869. #endif
  870. ret = rw_copy_check_uvector(type,
  871. (struct iovec __user *)kiocb->ki_buf,
  872. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  873. &kiocb->ki_iovec);
  874. if (ret < 0)
  875. goto out;
  876. ret = rw_verify_area(type, kiocb->ki_filp, &kiocb->ki_pos, ret);
  877. if (ret < 0)
  878. goto out;
  879. kiocb->ki_nr_segs = kiocb->ki_nbytes;
  880. kiocb->ki_cur_seg = 0;
  881. /* ki_nbytes/left now reflect bytes instead of segs */
  882. kiocb->ki_nbytes = ret;
  883. kiocb->ki_left = ret;
  884. ret = 0;
  885. out:
  886. return ret;
  887. }
  888. static ssize_t aio_setup_single_vector(int type, struct file * file, struct kiocb *kiocb)
  889. {
  890. int bytes;
  891. bytes = rw_verify_area(type, file, &kiocb->ki_pos, kiocb->ki_left);
  892. if (bytes < 0)
  893. return bytes;
  894. kiocb->ki_iovec = &kiocb->ki_inline_vec;
  895. kiocb->ki_iovec->iov_base = kiocb->ki_buf;
  896. kiocb->ki_iovec->iov_len = bytes;
  897. kiocb->ki_nr_segs = 1;
  898. kiocb->ki_cur_seg = 0;
  899. return 0;
  900. }
  901. /*
  902. * aio_setup_iocb:
  903. * Performs the initial checks and aio retry method
  904. * setup for the kiocb at the time of io submission.
  905. */
  906. static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
  907. {
  908. struct file *file = kiocb->ki_filp;
  909. ssize_t ret = 0;
  910. switch (kiocb->ki_opcode) {
  911. case IOCB_CMD_PREAD:
  912. ret = -EBADF;
  913. if (unlikely(!(file->f_mode & FMODE_READ)))
  914. break;
  915. ret = -EFAULT;
  916. if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
  917. kiocb->ki_left)))
  918. break;
  919. ret = aio_setup_single_vector(READ, file, kiocb);
  920. if (ret)
  921. break;
  922. ret = -EINVAL;
  923. if (file->f_op->aio_read)
  924. kiocb->ki_retry = aio_rw_vect_retry;
  925. break;
  926. case IOCB_CMD_PWRITE:
  927. ret = -EBADF;
  928. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  929. break;
  930. ret = -EFAULT;
  931. if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
  932. kiocb->ki_left)))
  933. break;
  934. ret = aio_setup_single_vector(WRITE, file, kiocb);
  935. if (ret)
  936. break;
  937. ret = -EINVAL;
  938. if (file->f_op->aio_write)
  939. kiocb->ki_retry = aio_rw_vect_retry;
  940. break;
  941. case IOCB_CMD_PREADV:
  942. ret = -EBADF;
  943. if (unlikely(!(file->f_mode & FMODE_READ)))
  944. break;
  945. ret = aio_setup_vectored_rw(READ, kiocb, compat);
  946. if (ret)
  947. break;
  948. ret = -EINVAL;
  949. if (file->f_op->aio_read)
  950. kiocb->ki_retry = aio_rw_vect_retry;
  951. break;
  952. case IOCB_CMD_PWRITEV:
  953. ret = -EBADF;
  954. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  955. break;
  956. ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
  957. if (ret)
  958. break;
  959. ret = -EINVAL;
  960. if (file->f_op->aio_write)
  961. kiocb->ki_retry = aio_rw_vect_retry;
  962. break;
  963. case IOCB_CMD_FDSYNC:
  964. ret = -EINVAL;
  965. if (file->f_op->aio_fsync)
  966. kiocb->ki_retry = aio_fdsync;
  967. break;
  968. case IOCB_CMD_FSYNC:
  969. ret = -EINVAL;
  970. if (file->f_op->aio_fsync)
  971. kiocb->ki_retry = aio_fsync;
  972. break;
  973. default:
  974. pr_debug("EINVAL: no operation provided\n");
  975. ret = -EINVAL;
  976. }
  977. if (!kiocb->ki_retry)
  978. return ret;
  979. return 0;
  980. }
  981. static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  982. struct iocb *iocb, struct kiocb_batch *batch,
  983. bool compat)
  984. {
  985. struct kiocb *req;
  986. ssize_t ret;
  987. /* enforce forwards compatibility on users */
  988. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
  989. pr_debug("EINVAL: reserve field set\n");
  990. return -EINVAL;
  991. }
  992. /* prevent overflows */
  993. if (unlikely(
  994. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  995. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  996. ((ssize_t)iocb->aio_nbytes < 0)
  997. )) {
  998. pr_debug("EINVAL: io_submit: overflow check\n");
  999. return -EINVAL;
  1000. }
  1001. req = aio_get_req(ctx, batch); /* returns with 2 references to req */
  1002. if (unlikely(!req))
  1003. return -EAGAIN;
  1004. req->ki_filp = fget(iocb->aio_fildes);
  1005. if (unlikely(!req->ki_filp)) {
  1006. ret = -EBADF;
  1007. goto out_put_req;
  1008. }
  1009. if (iocb->aio_flags & IOCB_FLAG_RESFD) {
  1010. /*
  1011. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  1012. * instance of the file* now. The file descriptor must be
  1013. * an eventfd() fd, and will be signaled for each completed
  1014. * event using the eventfd_signal() function.
  1015. */
  1016. req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
  1017. if (IS_ERR(req->ki_eventfd)) {
  1018. ret = PTR_ERR(req->ki_eventfd);
  1019. req->ki_eventfd = NULL;
  1020. goto out_put_req;
  1021. }
  1022. }
  1023. ret = put_user(req->ki_key, &user_iocb->aio_key);
  1024. if (unlikely(ret)) {
  1025. pr_debug("EFAULT: aio_key\n");
  1026. goto out_put_req;
  1027. }
  1028. req->ki_obj.user = user_iocb;
  1029. req->ki_user_data = iocb->aio_data;
  1030. req->ki_pos = iocb->aio_offset;
  1031. req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
  1032. req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
  1033. req->ki_opcode = iocb->aio_lio_opcode;
  1034. ret = aio_setup_iocb(req, compat);
  1035. if (ret)
  1036. goto out_put_req;
  1037. ret = req->ki_retry(req);
  1038. if (ret != -EIOCBQUEUED) {
  1039. /*
  1040. * There's no easy way to restart the syscall since other AIO's
  1041. * may be already running. Just fail this IO with EINTR.
  1042. */
  1043. if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
  1044. ret == -ERESTARTNOHAND ||
  1045. ret == -ERESTART_RESTARTBLOCK))
  1046. ret = -EINTR;
  1047. aio_complete(req, ret, 0);
  1048. }
  1049. aio_put_req(req); /* drop extra ref to req */
  1050. return 0;
  1051. out_put_req:
  1052. atomic_dec(&ctx->reqs_active);
  1053. aio_put_req(req); /* drop extra ref to req */
  1054. aio_put_req(req); /* drop i/o ref to req */
  1055. return ret;
  1056. }
  1057. long do_io_submit(aio_context_t ctx_id, long nr,
  1058. struct iocb __user *__user *iocbpp, bool compat)
  1059. {
  1060. struct kioctx *ctx;
  1061. long ret = 0;
  1062. int i = 0;
  1063. struct blk_plug plug;
  1064. struct kiocb_batch batch;
  1065. if (unlikely(nr < 0))
  1066. return -EINVAL;
  1067. if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
  1068. nr = LONG_MAX/sizeof(*iocbpp);
  1069. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1070. return -EFAULT;
  1071. ctx = lookup_ioctx(ctx_id);
  1072. if (unlikely(!ctx)) {
  1073. pr_debug("EINVAL: invalid context id\n");
  1074. return -EINVAL;
  1075. }
  1076. kiocb_batch_init(&batch, nr);
  1077. blk_start_plug(&plug);
  1078. /*
  1079. * AKPM: should this return a partial result if some of the IOs were
  1080. * successfully submitted?
  1081. */
  1082. for (i=0; i<nr; i++) {
  1083. struct iocb __user *user_iocb;
  1084. struct iocb tmp;
  1085. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1086. ret = -EFAULT;
  1087. break;
  1088. }
  1089. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1090. ret = -EFAULT;
  1091. break;
  1092. }
  1093. ret = io_submit_one(ctx, user_iocb, &tmp, &batch, compat);
  1094. if (ret)
  1095. break;
  1096. }
  1097. blk_finish_plug(&plug);
  1098. kiocb_batch_free(ctx, &batch);
  1099. put_ioctx(ctx);
  1100. return i ? i : ret;
  1101. }
  1102. /* sys_io_submit:
  1103. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1104. * the number of iocbs queued. May return -EINVAL if the aio_context
  1105. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1106. * *iocbpp[0] is not properly initialized, if the operation specified
  1107. * is invalid for the file descriptor in the iocb. May fail with
  1108. * -EFAULT if any of the data structures point to invalid data. May
  1109. * fail with -EBADF if the file descriptor specified in the first
  1110. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1111. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1112. * fail with -ENOSYS if not implemented.
  1113. */
  1114. SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
  1115. struct iocb __user * __user *, iocbpp)
  1116. {
  1117. return do_io_submit(ctx_id, nr, iocbpp, 0);
  1118. }
  1119. /* lookup_kiocb
  1120. * Finds a given iocb for cancellation.
  1121. */
  1122. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1123. u32 key)
  1124. {
  1125. struct list_head *pos;
  1126. assert_spin_locked(&ctx->ctx_lock);
  1127. /* TODO: use a hash or array, this sucks. */
  1128. list_for_each(pos, &ctx->active_reqs) {
  1129. struct kiocb *kiocb = list_kiocb(pos);
  1130. if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
  1131. return kiocb;
  1132. }
  1133. return NULL;
  1134. }
  1135. /* sys_io_cancel:
  1136. * Attempts to cancel an iocb previously passed to io_submit. If
  1137. * the operation is successfully cancelled, the resulting event is
  1138. * copied into the memory pointed to by result without being placed
  1139. * into the completion queue and 0 is returned. May fail with
  1140. * -EFAULT if any of the data structures pointed to are invalid.
  1141. * May fail with -EINVAL if aio_context specified by ctx_id is
  1142. * invalid. May fail with -EAGAIN if the iocb specified was not
  1143. * cancelled. Will fail with -ENOSYS if not implemented.
  1144. */
  1145. SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
  1146. struct io_event __user *, result)
  1147. {
  1148. struct io_event res;
  1149. struct kioctx *ctx;
  1150. struct kiocb *kiocb;
  1151. u32 key;
  1152. int ret;
  1153. ret = get_user(key, &iocb->aio_key);
  1154. if (unlikely(ret))
  1155. return -EFAULT;
  1156. ctx = lookup_ioctx(ctx_id);
  1157. if (unlikely(!ctx))
  1158. return -EINVAL;
  1159. spin_lock_irq(&ctx->ctx_lock);
  1160. kiocb = lookup_kiocb(ctx, iocb, key);
  1161. if (kiocb)
  1162. ret = kiocb_cancel(ctx, kiocb, &res);
  1163. else
  1164. ret = -EINVAL;
  1165. spin_unlock_irq(&ctx->ctx_lock);
  1166. if (!ret) {
  1167. /* Cancellation succeeded -- copy the result
  1168. * into the user's buffer.
  1169. */
  1170. if (copy_to_user(result, &res, sizeof(res)))
  1171. ret = -EFAULT;
  1172. }
  1173. put_ioctx(ctx);
  1174. return ret;
  1175. }
  1176. /* io_getevents:
  1177. * Attempts to read at least min_nr events and up to nr events from
  1178. * the completion queue for the aio_context specified by ctx_id. If
  1179. * it succeeds, the number of read events is returned. May fail with
  1180. * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
  1181. * out of range, if timeout is out of range. May fail with -EFAULT
  1182. * if any of the memory specified is invalid. May return 0 or
  1183. * < min_nr if the timeout specified by timeout has elapsed
  1184. * before sufficient events are available, where timeout == NULL
  1185. * specifies an infinite timeout. Note that the timeout pointed to by
  1186. * timeout is relative and will be updated if not NULL and the
  1187. * operation blocks. Will fail with -ENOSYS if not implemented.
  1188. */
  1189. SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
  1190. long, min_nr,
  1191. long, nr,
  1192. struct io_event __user *, events,
  1193. struct timespec __user *, timeout)
  1194. {
  1195. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1196. long ret = -EINVAL;
  1197. if (likely(ioctx)) {
  1198. if (likely(min_nr <= nr && min_nr >= 0))
  1199. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1200. put_ioctx(ioctx);
  1201. }
  1202. return ret;
  1203. }