aio.c 38 KB

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