aio.c 37 KB

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