aio.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. {
  298. kiocb_cancel_fn *old, *cancel;
  299. /*
  300. * Don't want to set kiocb->ki_cancel = KIOCB_CANCELLED unless it
  301. * actually has a cancel function, hence the cmpxchg()
  302. */
  303. cancel = ACCESS_ONCE(kiocb->ki_cancel);
  304. do {
  305. if (!cancel || cancel == KIOCB_CANCELLED)
  306. return -EINVAL;
  307. old = cancel;
  308. cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
  309. } while (cancel != old);
  310. return cancel(kiocb);
  311. }
  312. static void free_ioctx_rcu(struct rcu_head *head)
  313. {
  314. struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
  315. free_percpu(ctx->cpu);
  316. kmem_cache_free(kioctx_cachep, ctx);
  317. }
  318. /*
  319. * When this function runs, the kioctx has been removed from the "hash table"
  320. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  321. * now it's safe to cancel any that need to be.
  322. */
  323. static void free_ioctx(struct work_struct *work)
  324. {
  325. struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  326. struct aio_ring *ring;
  327. struct kiocb *req;
  328. unsigned cpu, avail;
  329. DEFINE_WAIT(wait);
  330. spin_lock_irq(&ctx->ctx_lock);
  331. while (!list_empty(&ctx->active_reqs)) {
  332. req = list_first_entry(&ctx->active_reqs,
  333. struct kiocb, ki_list);
  334. list_del_init(&req->ki_list);
  335. kiocb_cancel(ctx, req);
  336. }
  337. spin_unlock_irq(&ctx->ctx_lock);
  338. for_each_possible_cpu(cpu) {
  339. struct kioctx_cpu *kcpu = per_cpu_ptr(ctx->cpu, cpu);
  340. atomic_add(kcpu->reqs_available, &ctx->reqs_available);
  341. kcpu->reqs_available = 0;
  342. }
  343. while (1) {
  344. prepare_to_wait(&ctx->wait, &wait, TASK_UNINTERRUPTIBLE);
  345. ring = kmap_atomic(ctx->ring_pages[0]);
  346. avail = (ring->head <= ring->tail)
  347. ? ring->tail - ring->head
  348. : ctx->nr_events - ring->head + ring->tail;
  349. atomic_add(avail, &ctx->reqs_available);
  350. ring->head = ring->tail;
  351. kunmap_atomic(ring);
  352. if (atomic_read(&ctx->reqs_available) >= ctx->nr_events - 1)
  353. break;
  354. schedule();
  355. }
  356. finish_wait(&ctx->wait, &wait);
  357. WARN_ON(atomic_read(&ctx->reqs_available) > ctx->nr_events - 1);
  358. aio_free_ring(ctx);
  359. pr_debug("freeing %p\n", ctx);
  360. /*
  361. * Here the call_rcu() is between the wait_event() for reqs_active to
  362. * hit 0, and freeing the ioctx.
  363. *
  364. * aio_complete() decrements reqs_active, but it has to touch the ioctx
  365. * after to issue a wakeup so we use rcu.
  366. */
  367. call_rcu(&ctx->rcu_head, free_ioctx_rcu);
  368. }
  369. static void free_ioctx_ref(struct percpu_ref *ref)
  370. {
  371. struct kioctx *ctx = container_of(ref, struct kioctx, users);
  372. INIT_WORK(&ctx->free_work, free_ioctx);
  373. schedule_work(&ctx->free_work);
  374. }
  375. /* ioctx_alloc
  376. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  377. */
  378. static struct kioctx *ioctx_alloc(unsigned nr_events)
  379. {
  380. struct mm_struct *mm = current->mm;
  381. struct kioctx *ctx;
  382. int err = -ENOMEM;
  383. /*
  384. * We keep track of the number of available ringbuffer slots, to prevent
  385. * overflow (reqs_available), and we also use percpu counters for this.
  386. *
  387. * So since up to half the slots might be on other cpu's percpu counters
  388. * and unavailable, double nr_events so userspace sees what they
  389. * expected: additionally, we move req_batch slots to/from percpu
  390. * counters at a time, so make sure that isn't 0:
  391. */
  392. nr_events = max(nr_events, num_possible_cpus() * 4);
  393. nr_events *= 2;
  394. /* Prevent overflows */
  395. if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
  396. (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
  397. pr_debug("ENOMEM: nr_events too high\n");
  398. return ERR_PTR(-EINVAL);
  399. }
  400. if (!nr_events || (unsigned long)nr_events > aio_max_nr)
  401. return ERR_PTR(-EAGAIN);
  402. ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
  403. if (!ctx)
  404. return ERR_PTR(-ENOMEM);
  405. ctx->max_reqs = nr_events;
  406. if (percpu_ref_init(&ctx->users, free_ioctx_ref))
  407. goto out_freectx;
  408. spin_lock_init(&ctx->ctx_lock);
  409. spin_lock_init(&ctx->completion_lock);
  410. mutex_init(&ctx->ring_lock);
  411. init_waitqueue_head(&ctx->wait);
  412. INIT_LIST_HEAD(&ctx->active_reqs);
  413. ctx->cpu = alloc_percpu(struct kioctx_cpu);
  414. if (!ctx->cpu)
  415. goto out_freeref;
  416. if (aio_setup_ring(ctx) < 0)
  417. goto out_freepcpu;
  418. atomic_set(&ctx->reqs_available, ctx->nr_events - 1);
  419. ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4);
  420. BUG_ON(!ctx->req_batch);
  421. /* limit the number of system wide aios */
  422. spin_lock(&aio_nr_lock);
  423. if (aio_nr + nr_events > aio_max_nr ||
  424. aio_nr + nr_events < aio_nr) {
  425. spin_unlock(&aio_nr_lock);
  426. goto out_cleanup;
  427. }
  428. aio_nr += ctx->max_reqs;
  429. spin_unlock(&aio_nr_lock);
  430. percpu_ref_get(&ctx->users); /* io_setup() will drop this ref */
  431. /* now link into global list. */
  432. spin_lock(&mm->ioctx_lock);
  433. hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
  434. spin_unlock(&mm->ioctx_lock);
  435. pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  436. ctx, ctx->user_id, mm, ctx->nr_events);
  437. return ctx;
  438. out_cleanup:
  439. err = -EAGAIN;
  440. aio_free_ring(ctx);
  441. out_freepcpu:
  442. free_percpu(ctx->cpu);
  443. out_freeref:
  444. free_percpu(ctx->users.pcpu_count);
  445. out_freectx:
  446. if (ctx->aio_ring_file)
  447. fput(ctx->aio_ring_file);
  448. kmem_cache_free(kioctx_cachep, ctx);
  449. pr_debug("error allocating ioctx %d\n", err);
  450. return ERR_PTR(err);
  451. }
  452. /* kill_ioctx
  453. * Cancels all outstanding aio requests on an aio context. Used
  454. * when the processes owning a context have all exited to encourage
  455. * the rapid destruction of the kioctx.
  456. */
  457. static void kill_ioctx(struct kioctx *ctx)
  458. {
  459. if (!atomic_xchg(&ctx->dead, 1)) {
  460. hlist_del_rcu(&ctx->list);
  461. /* percpu_ref_kill() will do the necessary call_rcu() */
  462. wake_up_all(&ctx->wait);
  463. /*
  464. * It'd be more correct to do this in free_ioctx(), after all
  465. * the outstanding kiocbs have finished - but by then io_destroy
  466. * has already returned, so io_setup() could potentially return
  467. * -EAGAIN with no ioctxs actually in use (as far as userspace
  468. * could tell).
  469. */
  470. spin_lock(&aio_nr_lock);
  471. BUG_ON(aio_nr - ctx->max_reqs > aio_nr);
  472. aio_nr -= ctx->max_reqs;
  473. spin_unlock(&aio_nr_lock);
  474. if (ctx->mmap_size)
  475. vm_munmap(ctx->mmap_base, ctx->mmap_size);
  476. percpu_ref_kill(&ctx->users);
  477. }
  478. }
  479. /* wait_on_sync_kiocb:
  480. * Waits on the given sync kiocb to complete.
  481. */
  482. ssize_t wait_on_sync_kiocb(struct kiocb *req)
  483. {
  484. while (!req->ki_ctx) {
  485. set_current_state(TASK_UNINTERRUPTIBLE);
  486. if (req->ki_ctx)
  487. break;
  488. io_schedule();
  489. }
  490. __set_current_state(TASK_RUNNING);
  491. return req->ki_user_data;
  492. }
  493. EXPORT_SYMBOL(wait_on_sync_kiocb);
  494. /*
  495. * exit_aio: called when the last user of mm goes away. At this point, there is
  496. * no way for any new requests to be submited or any of the io_* syscalls to be
  497. * called on the context.
  498. *
  499. * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
  500. * them.
  501. */
  502. void exit_aio(struct mm_struct *mm)
  503. {
  504. struct kioctx *ctx;
  505. struct hlist_node *n;
  506. hlist_for_each_entry_safe(ctx, n, &mm->ioctx_list, list) {
  507. /*
  508. * We don't need to bother with munmap() here -
  509. * exit_mmap(mm) is coming and it'll unmap everything.
  510. * Since aio_free_ring() uses non-zero ->mmap_size
  511. * as indicator that it needs to unmap the area,
  512. * just set it to 0; aio_free_ring() is the only
  513. * place that uses ->mmap_size, so it's safe.
  514. */
  515. ctx->mmap_size = 0;
  516. kill_ioctx(ctx);
  517. }
  518. }
  519. static void put_reqs_available(struct kioctx *ctx, unsigned nr)
  520. {
  521. struct kioctx_cpu *kcpu;
  522. preempt_disable();
  523. kcpu = this_cpu_ptr(ctx->cpu);
  524. kcpu->reqs_available += nr;
  525. while (kcpu->reqs_available >= ctx->req_batch * 2) {
  526. kcpu->reqs_available -= ctx->req_batch;
  527. atomic_add(ctx->req_batch, &ctx->reqs_available);
  528. }
  529. preempt_enable();
  530. }
  531. static bool get_reqs_available(struct kioctx *ctx)
  532. {
  533. struct kioctx_cpu *kcpu;
  534. bool ret = false;
  535. preempt_disable();
  536. kcpu = this_cpu_ptr(ctx->cpu);
  537. if (!kcpu->reqs_available) {
  538. int old, avail = atomic_read(&ctx->reqs_available);
  539. do {
  540. if (avail < ctx->req_batch)
  541. goto out;
  542. old = avail;
  543. avail = atomic_cmpxchg(&ctx->reqs_available,
  544. avail, avail - ctx->req_batch);
  545. } while (avail != old);
  546. kcpu->reqs_available += ctx->req_batch;
  547. }
  548. ret = true;
  549. kcpu->reqs_available--;
  550. out:
  551. preempt_enable();
  552. return ret;
  553. }
  554. /* aio_get_req
  555. * Allocate a slot for an aio request.
  556. * Returns NULL if no requests are free.
  557. */
  558. static inline struct kiocb *aio_get_req(struct kioctx *ctx)
  559. {
  560. struct kiocb *req;
  561. if (!get_reqs_available(ctx))
  562. return NULL;
  563. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
  564. if (unlikely(!req))
  565. goto out_put;
  566. req->ki_ctx = ctx;
  567. return req;
  568. out_put:
  569. put_reqs_available(ctx, 1);
  570. return NULL;
  571. }
  572. static void kiocb_free(struct kiocb *req)
  573. {
  574. if (req->ki_filp)
  575. fput(req->ki_filp);
  576. if (req->ki_eventfd != NULL)
  577. eventfd_ctx_put(req->ki_eventfd);
  578. if (req->ki_dtor)
  579. req->ki_dtor(req);
  580. kmem_cache_free(kiocb_cachep, req);
  581. }
  582. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  583. {
  584. struct mm_struct *mm = current->mm;
  585. struct kioctx *ctx, *ret = NULL;
  586. rcu_read_lock();
  587. hlist_for_each_entry_rcu(ctx, &mm->ioctx_list, list) {
  588. if (ctx->user_id == ctx_id) {
  589. percpu_ref_get(&ctx->users);
  590. ret = ctx;
  591. break;
  592. }
  593. }
  594. rcu_read_unlock();
  595. return ret;
  596. }
  597. /* aio_complete
  598. * Called when the io request on the given iocb is complete.
  599. */
  600. void aio_complete(struct kiocb *iocb, long res, long res2)
  601. {
  602. struct kioctx *ctx = iocb->ki_ctx;
  603. struct aio_ring *ring;
  604. struct io_event *ev_page, *event;
  605. unsigned long flags;
  606. unsigned tail, pos;
  607. /*
  608. * Special case handling for sync iocbs:
  609. * - events go directly into the iocb for fast handling
  610. * - the sync task with the iocb in its stack holds the single iocb
  611. * ref, no other paths have a way to get another ref
  612. * - the sync task helpfully left a reference to itself in the iocb
  613. */
  614. if (is_sync_kiocb(iocb)) {
  615. iocb->ki_user_data = res;
  616. smp_wmb();
  617. iocb->ki_ctx = ERR_PTR(-EXDEV);
  618. wake_up_process(iocb->ki_obj.tsk);
  619. return;
  620. }
  621. /*
  622. * Take rcu_read_lock() in case the kioctx is being destroyed, as we
  623. * need to issue a wakeup after incrementing reqs_available.
  624. */
  625. rcu_read_lock();
  626. if (iocb->ki_list.next) {
  627. unsigned long flags;
  628. spin_lock_irqsave(&ctx->ctx_lock, flags);
  629. list_del(&iocb->ki_list);
  630. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  631. }
  632. /*
  633. * Add a completion event to the ring buffer. Must be done holding
  634. * ctx->completion_lock to prevent other code from messing with the tail
  635. * pointer since we might be called from irq context.
  636. */
  637. spin_lock_irqsave(&ctx->completion_lock, flags);
  638. tail = ctx->tail;
  639. pos = tail + AIO_EVENTS_OFFSET;
  640. if (++tail >= ctx->nr_events)
  641. tail = 0;
  642. ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  643. event = ev_page + pos % AIO_EVENTS_PER_PAGE;
  644. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  645. event->data = iocb->ki_user_data;
  646. event->res = res;
  647. event->res2 = res2;
  648. kunmap_atomic(ev_page);
  649. flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  650. pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
  651. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  652. res, res2);
  653. /* after flagging the request as done, we
  654. * must never even look at it again
  655. */
  656. smp_wmb(); /* make event visible before updating tail */
  657. ctx->tail = tail;
  658. ring = kmap_atomic(ctx->ring_pages[0]);
  659. ring->tail = tail;
  660. kunmap_atomic(ring);
  661. flush_dcache_page(ctx->ring_pages[0]);
  662. spin_unlock_irqrestore(&ctx->completion_lock, flags);
  663. pr_debug("added to ring %p at [%u]\n", iocb, tail);
  664. /*
  665. * Check if the user asked us to deliver the result through an
  666. * eventfd. The eventfd_signal() function is safe to be called
  667. * from IRQ context.
  668. */
  669. if (iocb->ki_eventfd != NULL)
  670. eventfd_signal(iocb->ki_eventfd, 1);
  671. /* everything turned out well, dispose of the aiocb. */
  672. kiocb_free(iocb);
  673. /*
  674. * We have to order our ring_info tail store above and test
  675. * of the wait list below outside the wait lock. This is
  676. * like in wake_up_bit() where clearing a bit has to be
  677. * ordered with the unlocked test.
  678. */
  679. smp_mb();
  680. if (waitqueue_active(&ctx->wait))
  681. wake_up(&ctx->wait);
  682. rcu_read_unlock();
  683. }
  684. EXPORT_SYMBOL(aio_complete);
  685. /* aio_read_events
  686. * Pull an event off of the ioctx's event ring. Returns the number of
  687. * events fetched
  688. */
  689. static long aio_read_events_ring(struct kioctx *ctx,
  690. struct io_event __user *event, long nr)
  691. {
  692. struct aio_ring *ring;
  693. unsigned head, tail, pos;
  694. long ret = 0;
  695. int copy_ret;
  696. mutex_lock(&ctx->ring_lock);
  697. ring = kmap_atomic(ctx->ring_pages[0]);
  698. head = ring->head;
  699. tail = ring->tail;
  700. kunmap_atomic(ring);
  701. pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
  702. if (head == tail)
  703. goto out;
  704. while (ret < nr) {
  705. long avail;
  706. struct io_event *ev;
  707. struct page *page;
  708. avail = (head <= tail ? tail : ctx->nr_events) - head;
  709. if (head == tail)
  710. break;
  711. avail = min(avail, nr - ret);
  712. avail = min_t(long, avail, AIO_EVENTS_PER_PAGE -
  713. ((head + AIO_EVENTS_OFFSET) % AIO_EVENTS_PER_PAGE));
  714. pos = head + AIO_EVENTS_OFFSET;
  715. page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
  716. pos %= AIO_EVENTS_PER_PAGE;
  717. ev = kmap(page);
  718. copy_ret = copy_to_user(event + ret, ev + pos,
  719. sizeof(*ev) * avail);
  720. kunmap(page);
  721. if (unlikely(copy_ret)) {
  722. ret = -EFAULT;
  723. goto out;
  724. }
  725. ret += avail;
  726. head += avail;
  727. head %= ctx->nr_events;
  728. }
  729. ring = kmap_atomic(ctx->ring_pages[0]);
  730. ring->head = head;
  731. kunmap_atomic(ring);
  732. flush_dcache_page(ctx->ring_pages[0]);
  733. pr_debug("%li h%u t%u\n", ret, head, tail);
  734. put_reqs_available(ctx, ret);
  735. out:
  736. mutex_unlock(&ctx->ring_lock);
  737. return ret;
  738. }
  739. static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
  740. struct io_event __user *event, long *i)
  741. {
  742. long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
  743. if (ret > 0)
  744. *i += ret;
  745. if (unlikely(atomic_read(&ctx->dead)))
  746. ret = -EINVAL;
  747. if (!*i)
  748. *i = ret;
  749. return ret < 0 || *i >= min_nr;
  750. }
  751. static long read_events(struct kioctx *ctx, long min_nr, long nr,
  752. struct io_event __user *event,
  753. struct timespec __user *timeout)
  754. {
  755. ktime_t until = { .tv64 = KTIME_MAX };
  756. long ret = 0;
  757. if (timeout) {
  758. struct timespec ts;
  759. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  760. return -EFAULT;
  761. until = timespec_to_ktime(ts);
  762. }
  763. /*
  764. * Note that aio_read_events() is being called as the conditional - i.e.
  765. * we're calling it after prepare_to_wait() has set task state to
  766. * TASK_INTERRUPTIBLE.
  767. *
  768. * But aio_read_events() can block, and if it blocks it's going to flip
  769. * the task state back to TASK_RUNNING.
  770. *
  771. * This should be ok, provided it doesn't flip the state back to
  772. * TASK_RUNNING and return 0 too much - that causes us to spin. That
  773. * will only happen if the mutex_lock() call blocks, and we then find
  774. * the ringbuffer empty. So in practice we should be ok, but it's
  775. * something to be aware of when touching this code.
  776. */
  777. wait_event_interruptible_hrtimeout(ctx->wait,
  778. aio_read_events(ctx, min_nr, nr, event, &ret), until);
  779. if (!ret && signal_pending(current))
  780. ret = -EINTR;
  781. return ret;
  782. }
  783. /* sys_io_setup:
  784. * Create an aio_context capable of receiving at least nr_events.
  785. * ctxp must not point to an aio_context that already exists, and
  786. * must be initialized to 0 prior to the call. On successful
  787. * creation of the aio_context, *ctxp is filled in with the resulting
  788. * handle. May fail with -EINVAL if *ctxp is not initialized,
  789. * if the specified nr_events exceeds internal limits. May fail
  790. * with -EAGAIN if the specified nr_events exceeds the user's limit
  791. * of available events. May fail with -ENOMEM if insufficient kernel
  792. * resources are available. May fail with -EFAULT if an invalid
  793. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  794. * implemented.
  795. */
  796. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  797. {
  798. struct kioctx *ioctx = NULL;
  799. unsigned long ctx;
  800. long ret;
  801. ret = get_user(ctx, ctxp);
  802. if (unlikely(ret))
  803. goto out;
  804. ret = -EINVAL;
  805. if (unlikely(ctx || nr_events == 0)) {
  806. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  807. ctx, nr_events);
  808. goto out;
  809. }
  810. ioctx = ioctx_alloc(nr_events);
  811. ret = PTR_ERR(ioctx);
  812. if (!IS_ERR(ioctx)) {
  813. ret = put_user(ioctx->user_id, ctxp);
  814. if (ret)
  815. kill_ioctx(ioctx);
  816. percpu_ref_put(&ioctx->users);
  817. }
  818. out:
  819. return ret;
  820. }
  821. /* sys_io_destroy:
  822. * Destroy the aio_context specified. May cancel any outstanding
  823. * AIOs and block on completion. Will fail with -ENOSYS if not
  824. * implemented. May fail with -EINVAL if the context pointed to
  825. * is invalid.
  826. */
  827. SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
  828. {
  829. struct kioctx *ioctx = lookup_ioctx(ctx);
  830. if (likely(NULL != ioctx)) {
  831. kill_ioctx(ioctx);
  832. percpu_ref_put(&ioctx->users);
  833. return 0;
  834. }
  835. pr_debug("EINVAL: io_destroy: invalid context id\n");
  836. return -EINVAL;
  837. }
  838. typedef ssize_t (aio_rw_op)(struct kiocb *, const struct iovec *,
  839. unsigned long, loff_t);
  840. static ssize_t aio_setup_vectored_rw(struct kiocb *kiocb,
  841. int rw, char __user *buf,
  842. unsigned long *nr_segs,
  843. struct iovec **iovec,
  844. bool compat)
  845. {
  846. ssize_t ret;
  847. *nr_segs = kiocb->ki_nbytes;
  848. #ifdef CONFIG_COMPAT
  849. if (compat)
  850. ret = compat_rw_copy_check_uvector(rw,
  851. (struct compat_iovec __user *)buf,
  852. *nr_segs, 1, *iovec, iovec);
  853. else
  854. #endif
  855. ret = rw_copy_check_uvector(rw,
  856. (struct iovec __user *)buf,
  857. *nr_segs, 1, *iovec, iovec);
  858. if (ret < 0)
  859. return ret;
  860. /* ki_nbytes now reflect bytes instead of segs */
  861. kiocb->ki_nbytes = ret;
  862. return 0;
  863. }
  864. static ssize_t aio_setup_single_vector(struct kiocb *kiocb,
  865. int rw, char __user *buf,
  866. unsigned long *nr_segs,
  867. struct iovec *iovec)
  868. {
  869. if (unlikely(!access_ok(!rw, buf, kiocb->ki_nbytes)))
  870. return -EFAULT;
  871. iovec->iov_base = buf;
  872. iovec->iov_len = kiocb->ki_nbytes;
  873. *nr_segs = 1;
  874. return 0;
  875. }
  876. /*
  877. * aio_setup_iocb:
  878. * Performs the initial checks and aio retry method
  879. * setup for the kiocb at the time of io submission.
  880. */
  881. static ssize_t aio_run_iocb(struct kiocb *req, unsigned opcode,
  882. char __user *buf, bool compat)
  883. {
  884. struct file *file = req->ki_filp;
  885. ssize_t ret;
  886. unsigned long nr_segs;
  887. int rw;
  888. fmode_t mode;
  889. aio_rw_op *rw_op;
  890. struct iovec inline_vec, *iovec = &inline_vec;
  891. switch (opcode) {
  892. case IOCB_CMD_PREAD:
  893. case IOCB_CMD_PREADV:
  894. mode = FMODE_READ;
  895. rw = READ;
  896. rw_op = file->f_op->aio_read;
  897. goto rw_common;
  898. case IOCB_CMD_PWRITE:
  899. case IOCB_CMD_PWRITEV:
  900. mode = FMODE_WRITE;
  901. rw = WRITE;
  902. rw_op = file->f_op->aio_write;
  903. goto rw_common;
  904. rw_common:
  905. if (unlikely(!(file->f_mode & mode)))
  906. return -EBADF;
  907. if (!rw_op)
  908. return -EINVAL;
  909. ret = (opcode == IOCB_CMD_PREADV ||
  910. opcode == IOCB_CMD_PWRITEV)
  911. ? aio_setup_vectored_rw(req, rw, buf, &nr_segs,
  912. &iovec, compat)
  913. : aio_setup_single_vector(req, rw, buf, &nr_segs,
  914. iovec);
  915. if (ret)
  916. return ret;
  917. ret = rw_verify_area(rw, file, &req->ki_pos, req->ki_nbytes);
  918. if (ret < 0) {
  919. if (iovec != &inline_vec)
  920. kfree(iovec);
  921. return ret;
  922. }
  923. req->ki_nbytes = ret;
  924. /* XXX: move/kill - rw_verify_area()? */
  925. /* This matches the pread()/pwrite() logic */
  926. if (req->ki_pos < 0) {
  927. ret = -EINVAL;
  928. break;
  929. }
  930. if (rw == WRITE)
  931. file_start_write(file);
  932. ret = rw_op(req, iovec, nr_segs, req->ki_pos);
  933. if (rw == WRITE)
  934. file_end_write(file);
  935. break;
  936. case IOCB_CMD_FDSYNC:
  937. if (!file->f_op->aio_fsync)
  938. return -EINVAL;
  939. ret = file->f_op->aio_fsync(req, 1);
  940. break;
  941. case IOCB_CMD_FSYNC:
  942. if (!file->f_op->aio_fsync)
  943. return -EINVAL;
  944. ret = file->f_op->aio_fsync(req, 0);
  945. break;
  946. default:
  947. pr_debug("EINVAL: no operation provided\n");
  948. return -EINVAL;
  949. }
  950. if (iovec != &inline_vec)
  951. kfree(iovec);
  952. if (ret != -EIOCBQUEUED) {
  953. /*
  954. * There's no easy way to restart the syscall since other AIO's
  955. * may be already running. Just fail this IO with EINTR.
  956. */
  957. if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
  958. ret == -ERESTARTNOHAND ||
  959. ret == -ERESTART_RESTARTBLOCK))
  960. ret = -EINTR;
  961. aio_complete(req, ret, 0);
  962. }
  963. return 0;
  964. }
  965. static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  966. struct iocb *iocb, bool compat)
  967. {
  968. struct kiocb *req;
  969. ssize_t ret;
  970. /* enforce forwards compatibility on users */
  971. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
  972. pr_debug("EINVAL: reserve field set\n");
  973. return -EINVAL;
  974. }
  975. /* prevent overflows */
  976. if (unlikely(
  977. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  978. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  979. ((ssize_t)iocb->aio_nbytes < 0)
  980. )) {
  981. pr_debug("EINVAL: io_submit: overflow check\n");
  982. return -EINVAL;
  983. }
  984. req = aio_get_req(ctx);
  985. if (unlikely(!req))
  986. return -EAGAIN;
  987. req->ki_filp = fget(iocb->aio_fildes);
  988. if (unlikely(!req->ki_filp)) {
  989. ret = -EBADF;
  990. goto out_put_req;
  991. }
  992. if (iocb->aio_flags & IOCB_FLAG_RESFD) {
  993. /*
  994. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  995. * instance of the file* now. The file descriptor must be
  996. * an eventfd() fd, and will be signaled for each completed
  997. * event using the eventfd_signal() function.
  998. */
  999. req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
  1000. if (IS_ERR(req->ki_eventfd)) {
  1001. ret = PTR_ERR(req->ki_eventfd);
  1002. req->ki_eventfd = NULL;
  1003. goto out_put_req;
  1004. }
  1005. }
  1006. ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
  1007. if (unlikely(ret)) {
  1008. pr_debug("EFAULT: aio_key\n");
  1009. goto out_put_req;
  1010. }
  1011. req->ki_obj.user = user_iocb;
  1012. req->ki_user_data = iocb->aio_data;
  1013. req->ki_pos = iocb->aio_offset;
  1014. req->ki_nbytes = iocb->aio_nbytes;
  1015. ret = aio_run_iocb(req, iocb->aio_lio_opcode,
  1016. (char __user *)(unsigned long)iocb->aio_buf,
  1017. compat);
  1018. if (ret)
  1019. goto out_put_req;
  1020. return 0;
  1021. out_put_req:
  1022. put_reqs_available(ctx, 1);
  1023. kiocb_free(req);
  1024. return ret;
  1025. }
  1026. long do_io_submit(aio_context_t ctx_id, long nr,
  1027. struct iocb __user *__user *iocbpp, bool compat)
  1028. {
  1029. struct kioctx *ctx;
  1030. long ret = 0;
  1031. int i = 0;
  1032. struct blk_plug plug;
  1033. if (unlikely(nr < 0))
  1034. return -EINVAL;
  1035. if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
  1036. nr = LONG_MAX/sizeof(*iocbpp);
  1037. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1038. return -EFAULT;
  1039. ctx = lookup_ioctx(ctx_id);
  1040. if (unlikely(!ctx)) {
  1041. pr_debug("EINVAL: invalid context id\n");
  1042. return -EINVAL;
  1043. }
  1044. blk_start_plug(&plug);
  1045. /*
  1046. * AKPM: should this return a partial result if some of the IOs were
  1047. * successfully submitted?
  1048. */
  1049. for (i=0; i<nr; i++) {
  1050. struct iocb __user *user_iocb;
  1051. struct iocb tmp;
  1052. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1053. ret = -EFAULT;
  1054. break;
  1055. }
  1056. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1057. ret = -EFAULT;
  1058. break;
  1059. }
  1060. ret = io_submit_one(ctx, user_iocb, &tmp, compat);
  1061. if (ret)
  1062. break;
  1063. }
  1064. blk_finish_plug(&plug);
  1065. percpu_ref_put(&ctx->users);
  1066. return i ? i : ret;
  1067. }
  1068. /* sys_io_submit:
  1069. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1070. * the number of iocbs queued. May return -EINVAL if the aio_context
  1071. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1072. * *iocbpp[0] is not properly initialized, if the operation specified
  1073. * is invalid for the file descriptor in the iocb. May fail with
  1074. * -EFAULT if any of the data structures point to invalid data. May
  1075. * fail with -EBADF if the file descriptor specified in the first
  1076. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1077. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1078. * fail with -ENOSYS if not implemented.
  1079. */
  1080. SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
  1081. struct iocb __user * __user *, iocbpp)
  1082. {
  1083. return do_io_submit(ctx_id, nr, iocbpp, 0);
  1084. }
  1085. /* lookup_kiocb
  1086. * Finds a given iocb for cancellation.
  1087. */
  1088. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1089. u32 key)
  1090. {
  1091. struct list_head *pos;
  1092. assert_spin_locked(&ctx->ctx_lock);
  1093. if (key != KIOCB_KEY)
  1094. return NULL;
  1095. /* TODO: use a hash or array, this sucks. */
  1096. list_for_each(pos, &ctx->active_reqs) {
  1097. struct kiocb *kiocb = list_kiocb(pos);
  1098. if (kiocb->ki_obj.user == iocb)
  1099. return kiocb;
  1100. }
  1101. return NULL;
  1102. }
  1103. /* sys_io_cancel:
  1104. * Attempts to cancel an iocb previously passed to io_submit. If
  1105. * the operation is successfully cancelled, the resulting event is
  1106. * copied into the memory pointed to by result without being placed
  1107. * into the completion queue and 0 is returned. May fail with
  1108. * -EFAULT if any of the data structures pointed to are invalid.
  1109. * May fail with -EINVAL if aio_context specified by ctx_id is
  1110. * invalid. May fail with -EAGAIN if the iocb specified was not
  1111. * cancelled. Will fail with -ENOSYS if not implemented.
  1112. */
  1113. SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
  1114. struct io_event __user *, result)
  1115. {
  1116. struct kioctx *ctx;
  1117. struct kiocb *kiocb;
  1118. u32 key;
  1119. int ret;
  1120. ret = get_user(key, &iocb->aio_key);
  1121. if (unlikely(ret))
  1122. return -EFAULT;
  1123. ctx = lookup_ioctx(ctx_id);
  1124. if (unlikely(!ctx))
  1125. return -EINVAL;
  1126. spin_lock_irq(&ctx->ctx_lock);
  1127. kiocb = lookup_kiocb(ctx, iocb, key);
  1128. if (kiocb)
  1129. ret = kiocb_cancel(ctx, kiocb);
  1130. else
  1131. ret = -EINVAL;
  1132. spin_unlock_irq(&ctx->ctx_lock);
  1133. if (!ret) {
  1134. /*
  1135. * The result argument is no longer used - the io_event is
  1136. * always delivered via the ring buffer. -EINPROGRESS indicates
  1137. * cancellation is progress:
  1138. */
  1139. ret = -EINPROGRESS;
  1140. }
  1141. percpu_ref_put(&ctx->users);
  1142. return ret;
  1143. }
  1144. /* io_getevents:
  1145. * Attempts to read at least min_nr events and up to nr events from
  1146. * the completion queue for the aio_context specified by ctx_id. If
  1147. * it succeeds, the number of read events is returned. May fail with
  1148. * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
  1149. * out of range, if timeout is out of range. May fail with -EFAULT
  1150. * if any of the memory specified is invalid. May return 0 or
  1151. * < min_nr if the timeout specified by timeout has elapsed
  1152. * before sufficient events are available, where timeout == NULL
  1153. * specifies an infinite timeout. Note that the timeout pointed to by
  1154. * timeout is relative. Will fail with -ENOSYS if not implemented.
  1155. */
  1156. SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
  1157. long, min_nr,
  1158. long, nr,
  1159. struct io_event __user *, events,
  1160. struct timespec __user *, timeout)
  1161. {
  1162. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1163. long ret = -EINVAL;
  1164. if (likely(ioctx)) {
  1165. if (likely(min_nr <= nr && min_nr >= 0))
  1166. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1167. percpu_ref_put(&ioctx->users);
  1168. }
  1169. return ret;
  1170. }