aio.c 37 KB

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