aio.c 36 KB

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