aio.c 35 KB

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