aio.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158
  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. * Copyright 2018 Christoph Hellwig.
  9. *
  10. * See ../COPYING for licensing terms.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/time.h>
  17. #include <linux/aio_abi.h>
  18. #include <linux/export.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/backing-dev.h>
  21. #include <linux/uio.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/fs.h>
  24. #include <linux/file.h>
  25. #include <linux/mm.h>
  26. #include <linux/mman.h>
  27. #include <linux/mmu_context.h>
  28. #include <linux/percpu.h>
  29. #include <linux/slab.h>
  30. #include <linux/timer.h>
  31. #include <linux/aio.h>
  32. #include <linux/highmem.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/security.h>
  35. #include <linux/eventfd.h>
  36. #include <linux/blkdev.h>
  37. #include <linux/compat.h>
  38. #include <linux/migrate.h>
  39. #include <linux/ramfs.h>
  40. #include <linux/percpu-refcount.h>
  41. #include <linux/mount.h>
  42. #include <asm/kmap_types.h>
  43. #include <linux/uaccess.h>
  44. #include "internal.h"
  45. #define KIOCB_KEY 0
  46. #define AIO_RING_MAGIC 0xa10a10a1
  47. #define AIO_RING_COMPAT_FEATURES 1
  48. #define AIO_RING_INCOMPAT_FEATURES 0
  49. struct aio_ring {
  50. unsigned id; /* kernel internal index number */
  51. unsigned nr; /* number of io_events */
  52. unsigned head; /* Written to by userland or under ring_lock
  53. * mutex by aio_read_events_ring(). */
  54. unsigned tail;
  55. unsigned magic;
  56. unsigned compat_features;
  57. unsigned incompat_features;
  58. unsigned header_length; /* size of aio_ring */
  59. struct io_event io_events[0];
  60. }; /* 128 bytes + ring size */
  61. #define AIO_RING_PAGES 8
  62. struct kioctx_table {
  63. struct rcu_head rcu;
  64. unsigned nr;
  65. struct kioctx __rcu *table[];
  66. };
  67. struct kioctx_cpu {
  68. unsigned reqs_available;
  69. };
  70. struct ctx_rq_wait {
  71. struct completion comp;
  72. atomic_t count;
  73. };
  74. struct kioctx {
  75. struct percpu_ref users;
  76. atomic_t dead;
  77. struct percpu_ref reqs;
  78. unsigned long user_id;
  79. struct __percpu kioctx_cpu *cpu;
  80. /*
  81. * For percpu reqs_available, number of slots we move to/from global
  82. * counter at a time:
  83. */
  84. unsigned req_batch;
  85. /*
  86. * This is what userspace passed to io_setup(), it's not used for
  87. * anything but counting against the global max_reqs quota.
  88. *
  89. * The real limit is nr_events - 1, which will be larger (see
  90. * aio_setup_ring())
  91. */
  92. unsigned max_reqs;
  93. /* Size of ringbuffer, in units of struct io_event */
  94. unsigned nr_events;
  95. unsigned long mmap_base;
  96. unsigned long mmap_size;
  97. struct page **ring_pages;
  98. long nr_pages;
  99. struct rcu_work free_rwork; /* see free_ioctx() */
  100. /*
  101. * signals when all in-flight requests are done
  102. */
  103. struct ctx_rq_wait *rq_wait;
  104. struct {
  105. /*
  106. * This counts the number of available slots in the ringbuffer,
  107. * so we avoid overflowing it: it's decremented (if positive)
  108. * when allocating a kiocb and incremented when the resulting
  109. * io_event is pulled off the ringbuffer.
  110. *
  111. * We batch accesses to it with a percpu version.
  112. */
  113. atomic_t reqs_available;
  114. } ____cacheline_aligned_in_smp;
  115. struct {
  116. spinlock_t ctx_lock;
  117. struct list_head active_reqs; /* used for cancellation */
  118. } ____cacheline_aligned_in_smp;
  119. struct {
  120. struct mutex ring_lock;
  121. wait_queue_head_t wait;
  122. } ____cacheline_aligned_in_smp;
  123. struct {
  124. unsigned tail;
  125. unsigned completed_events;
  126. spinlock_t completion_lock;
  127. } ____cacheline_aligned_in_smp;
  128. struct page *internal_pages[AIO_RING_PAGES];
  129. struct file *aio_ring_file;
  130. unsigned id;
  131. };
  132. struct fsync_iocb {
  133. struct work_struct work;
  134. struct file *file;
  135. bool datasync;
  136. };
  137. struct poll_iocb {
  138. struct file *file;
  139. __poll_t events;
  140. struct wait_queue_head *head;
  141. union {
  142. struct wait_queue_entry wait;
  143. struct work_struct work;
  144. };
  145. };
  146. struct aio_kiocb {
  147. union {
  148. struct kiocb rw;
  149. struct fsync_iocb fsync;
  150. struct poll_iocb poll;
  151. };
  152. struct kioctx *ki_ctx;
  153. kiocb_cancel_fn *ki_cancel;
  154. struct iocb __user *ki_user_iocb; /* user's aiocb */
  155. __u64 ki_user_data; /* user's data for completion */
  156. struct list_head ki_list; /* the aio core uses this
  157. * for cancellation */
  158. /*
  159. * If the aio_resfd field of the userspace iocb is not zero,
  160. * this is the underlying eventfd context to deliver events to.
  161. */
  162. struct eventfd_ctx *ki_eventfd;
  163. };
  164. /*------ sysctl variables----*/
  165. static DEFINE_SPINLOCK(aio_nr_lock);
  166. unsigned long aio_nr; /* current system wide number of aio requests */
  167. unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
  168. /*----end sysctl variables---*/
  169. static struct kmem_cache *kiocb_cachep;
  170. static struct kmem_cache *kioctx_cachep;
  171. static struct vfsmount *aio_mnt;
  172. static const struct file_operations aio_ring_fops;
  173. static const struct address_space_operations aio_ctx_aops;
  174. static struct file *aio_private_file(struct kioctx *ctx, loff_t nr_pages)
  175. {
  176. struct qstr this = QSTR_INIT("[aio]", 5);
  177. struct file *file;
  178. struct path path;
  179. struct inode *inode = alloc_anon_inode(aio_mnt->mnt_sb);
  180. if (IS_ERR(inode))
  181. return ERR_CAST(inode);
  182. inode->i_mapping->a_ops = &aio_ctx_aops;
  183. inode->i_mapping->private_data = ctx;
  184. inode->i_size = PAGE_SIZE * nr_pages;
  185. path.dentry = d_alloc_pseudo(aio_mnt->mnt_sb, &this);
  186. if (!path.dentry) {
  187. iput(inode);
  188. return ERR_PTR(-ENOMEM);
  189. }
  190. path.mnt = mntget(aio_mnt);
  191. d_instantiate(path.dentry, inode);
  192. file = alloc_file(&path, FMODE_READ | FMODE_WRITE, &aio_ring_fops);
  193. if (IS_ERR(file)) {
  194. path_put(&path);
  195. return file;
  196. }
  197. file->f_flags = O_RDWR;
  198. return file;
  199. }
  200. static struct dentry *aio_mount(struct file_system_type *fs_type,
  201. int flags, const char *dev_name, void *data)
  202. {
  203. static const struct dentry_operations ops = {
  204. .d_dname = simple_dname,
  205. };
  206. struct dentry *root = mount_pseudo(fs_type, "aio:", NULL, &ops,
  207. AIO_RING_MAGIC);
  208. if (!IS_ERR(root))
  209. root->d_sb->s_iflags |= SB_I_NOEXEC;
  210. return root;
  211. }
  212. /* aio_setup
  213. * Creates the slab caches used by the aio routines, panic on
  214. * failure as this is done early during the boot sequence.
  215. */
  216. static int __init aio_setup(void)
  217. {
  218. static struct file_system_type aio_fs = {
  219. .name = "aio",
  220. .mount = aio_mount,
  221. .kill_sb = kill_anon_super,
  222. };
  223. aio_mnt = kern_mount(&aio_fs);
  224. if (IS_ERR(aio_mnt))
  225. panic("Failed to create aio fs mount.");
  226. kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  227. kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  228. return 0;
  229. }
  230. __initcall(aio_setup);
  231. static void put_aio_ring_file(struct kioctx *ctx)
  232. {
  233. struct file *aio_ring_file = ctx->aio_ring_file;
  234. struct address_space *i_mapping;
  235. if (aio_ring_file) {
  236. truncate_setsize(file_inode(aio_ring_file), 0);
  237. /* Prevent further access to the kioctx from migratepages */
  238. i_mapping = aio_ring_file->f_mapping;
  239. spin_lock(&i_mapping->private_lock);
  240. i_mapping->private_data = NULL;
  241. ctx->aio_ring_file = NULL;
  242. spin_unlock(&i_mapping->private_lock);
  243. fput(aio_ring_file);
  244. }
  245. }
  246. static void aio_free_ring(struct kioctx *ctx)
  247. {
  248. int i;
  249. /* Disconnect the kiotx from the ring file. This prevents future
  250. * accesses to the kioctx from page migration.
  251. */
  252. put_aio_ring_file(ctx);
  253. for (i = 0; i < ctx->nr_pages; i++) {
  254. struct page *page;
  255. pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i,
  256. page_count(ctx->ring_pages[i]));
  257. page = ctx->ring_pages[i];
  258. if (!page)
  259. continue;
  260. ctx->ring_pages[i] = NULL;
  261. put_page(page);
  262. }
  263. if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages) {
  264. kfree(ctx->ring_pages);
  265. ctx->ring_pages = NULL;
  266. }
  267. }
  268. static int aio_ring_mremap(struct vm_area_struct *vma)
  269. {
  270. struct file *file = vma->vm_file;
  271. struct mm_struct *mm = vma->vm_mm;
  272. struct kioctx_table *table;
  273. int i, res = -EINVAL;
  274. spin_lock(&mm->ioctx_lock);
  275. rcu_read_lock();
  276. table = rcu_dereference(mm->ioctx_table);
  277. for (i = 0; i < table->nr; i++) {
  278. struct kioctx *ctx;
  279. ctx = rcu_dereference(table->table[i]);
  280. if (ctx && ctx->aio_ring_file == file) {
  281. if (!atomic_read(&ctx->dead)) {
  282. ctx->user_id = ctx->mmap_base = vma->vm_start;
  283. res = 0;
  284. }
  285. break;
  286. }
  287. }
  288. rcu_read_unlock();
  289. spin_unlock(&mm->ioctx_lock);
  290. return res;
  291. }
  292. static const struct vm_operations_struct aio_ring_vm_ops = {
  293. .mremap = aio_ring_mremap,
  294. #if IS_ENABLED(CONFIG_MMU)
  295. .fault = filemap_fault,
  296. .map_pages = filemap_map_pages,
  297. .page_mkwrite = filemap_page_mkwrite,
  298. #endif
  299. };
  300. static int aio_ring_mmap(struct file *file, struct vm_area_struct *vma)
  301. {
  302. vma->vm_flags |= VM_DONTEXPAND;
  303. vma->vm_ops = &aio_ring_vm_ops;
  304. return 0;
  305. }
  306. static const struct file_operations aio_ring_fops = {
  307. .mmap = aio_ring_mmap,
  308. };
  309. #if IS_ENABLED(CONFIG_MIGRATION)
  310. static int aio_migratepage(struct address_space *mapping, struct page *new,
  311. struct page *old, enum migrate_mode mode)
  312. {
  313. struct kioctx *ctx;
  314. unsigned long flags;
  315. pgoff_t idx;
  316. int rc;
  317. /*
  318. * We cannot support the _NO_COPY case here, because copy needs to
  319. * happen under the ctx->completion_lock. That does not work with the
  320. * migration workflow of MIGRATE_SYNC_NO_COPY.
  321. */
  322. if (mode == MIGRATE_SYNC_NO_COPY)
  323. return -EINVAL;
  324. rc = 0;
  325. /* mapping->private_lock here protects against the kioctx teardown. */
  326. spin_lock(&mapping->private_lock);
  327. ctx = mapping->private_data;
  328. if (!ctx) {
  329. rc = -EINVAL;
  330. goto out;
  331. }
  332. /* The ring_lock mutex. The prevents aio_read_events() from writing
  333. * to the ring's head, and prevents page migration from mucking in
  334. * a partially initialized kiotx.
  335. */
  336. if (!mutex_trylock(&ctx->ring_lock)) {
  337. rc = -EAGAIN;
  338. goto out;
  339. }
  340. idx = old->index;
  341. if (idx < (pgoff_t)ctx->nr_pages) {
  342. /* Make sure the old page hasn't already been changed */
  343. if (ctx->ring_pages[idx] != old)
  344. rc = -EAGAIN;
  345. } else
  346. rc = -EINVAL;
  347. if (rc != 0)
  348. goto out_unlock;
  349. /* Writeback must be complete */
  350. BUG_ON(PageWriteback(old));
  351. get_page(new);
  352. rc = migrate_page_move_mapping(mapping, new, old, NULL, mode, 1);
  353. if (rc != MIGRATEPAGE_SUCCESS) {
  354. put_page(new);
  355. goto out_unlock;
  356. }
  357. /* Take completion_lock to prevent other writes to the ring buffer
  358. * while the old page is copied to the new. This prevents new
  359. * events from being lost.
  360. */
  361. spin_lock_irqsave(&ctx->completion_lock, flags);
  362. migrate_page_copy(new, old);
  363. BUG_ON(ctx->ring_pages[idx] != old);
  364. ctx->ring_pages[idx] = new;
  365. spin_unlock_irqrestore(&ctx->completion_lock, flags);
  366. /* The old page is no longer accessible. */
  367. put_page(old);
  368. out_unlock:
  369. mutex_unlock(&ctx->ring_lock);
  370. out:
  371. spin_unlock(&mapping->private_lock);
  372. return rc;
  373. }
  374. #endif
  375. static const struct address_space_operations aio_ctx_aops = {
  376. .set_page_dirty = __set_page_dirty_no_writeback,
  377. #if IS_ENABLED(CONFIG_MIGRATION)
  378. .migratepage = aio_migratepage,
  379. #endif
  380. };
  381. static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
  382. {
  383. struct aio_ring *ring;
  384. struct mm_struct *mm = current->mm;
  385. unsigned long size, unused;
  386. int nr_pages;
  387. int i;
  388. struct file *file;
  389. /* Compensate for the ring buffer's head/tail overlap entry */
  390. nr_events += 2; /* 1 is required, 2 for good luck */
  391. size = sizeof(struct aio_ring);
  392. size += sizeof(struct io_event) * nr_events;
  393. nr_pages = PFN_UP(size);
  394. if (nr_pages < 0)
  395. return -EINVAL;
  396. file = aio_private_file(ctx, nr_pages);
  397. if (IS_ERR(file)) {
  398. ctx->aio_ring_file = NULL;
  399. return -ENOMEM;
  400. }
  401. ctx->aio_ring_file = file;
  402. nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring))
  403. / sizeof(struct io_event);
  404. ctx->ring_pages = ctx->internal_pages;
  405. if (nr_pages > AIO_RING_PAGES) {
  406. ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
  407. GFP_KERNEL);
  408. if (!ctx->ring_pages) {
  409. put_aio_ring_file(ctx);
  410. return -ENOMEM;
  411. }
  412. }
  413. for (i = 0; i < nr_pages; i++) {
  414. struct page *page;
  415. page = find_or_create_page(file->f_mapping,
  416. i, GFP_HIGHUSER | __GFP_ZERO);
  417. if (!page)
  418. break;
  419. pr_debug("pid(%d) page[%d]->count=%d\n",
  420. current->pid, i, page_count(page));
  421. SetPageUptodate(page);
  422. unlock_page(page);
  423. ctx->ring_pages[i] = page;
  424. }
  425. ctx->nr_pages = i;
  426. if (unlikely(i != nr_pages)) {
  427. aio_free_ring(ctx);
  428. return -ENOMEM;
  429. }
  430. ctx->mmap_size = nr_pages * PAGE_SIZE;
  431. pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
  432. if (down_write_killable(&mm->mmap_sem)) {
  433. ctx->mmap_size = 0;
  434. aio_free_ring(ctx);
  435. return -EINTR;
  436. }
  437. ctx->mmap_base = do_mmap_pgoff(ctx->aio_ring_file, 0, ctx->mmap_size,
  438. PROT_READ | PROT_WRITE,
  439. MAP_SHARED, 0, &unused, NULL);
  440. up_write(&mm->mmap_sem);
  441. if (IS_ERR((void *)ctx->mmap_base)) {
  442. ctx->mmap_size = 0;
  443. aio_free_ring(ctx);
  444. return -ENOMEM;
  445. }
  446. pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
  447. ctx->user_id = ctx->mmap_base;
  448. ctx->nr_events = nr_events; /* trusted copy */
  449. ring = kmap_atomic(ctx->ring_pages[0]);
  450. ring->nr = nr_events; /* user copy */
  451. ring->id = ~0U;
  452. ring->head = ring->tail = 0;
  453. ring->magic = AIO_RING_MAGIC;
  454. ring->compat_features = AIO_RING_COMPAT_FEATURES;
  455. ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
  456. ring->header_length = sizeof(struct aio_ring);
  457. kunmap_atomic(ring);
  458. flush_dcache_page(ctx->ring_pages[0]);
  459. return 0;
  460. }
  461. #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
  462. #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
  463. #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
  464. void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
  465. {
  466. struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
  467. struct kioctx *ctx = req->ki_ctx;
  468. unsigned long flags;
  469. if (WARN_ON_ONCE(!list_empty(&req->ki_list)))
  470. return;
  471. spin_lock_irqsave(&ctx->ctx_lock, flags);
  472. list_add_tail(&req->ki_list, &ctx->active_reqs);
  473. req->ki_cancel = cancel;
  474. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  475. }
  476. EXPORT_SYMBOL(kiocb_set_cancel_fn);
  477. /*
  478. * free_ioctx() should be RCU delayed to synchronize against the RCU
  479. * protected lookup_ioctx() and also needs process context to call
  480. * aio_free_ring(). Use rcu_work.
  481. */
  482. static void free_ioctx(struct work_struct *work)
  483. {
  484. struct kioctx *ctx = container_of(to_rcu_work(work), struct kioctx,
  485. free_rwork);
  486. pr_debug("freeing %p\n", ctx);
  487. aio_free_ring(ctx);
  488. free_percpu(ctx->cpu);
  489. percpu_ref_exit(&ctx->reqs);
  490. percpu_ref_exit(&ctx->users);
  491. kmem_cache_free(kioctx_cachep, ctx);
  492. }
  493. static void free_ioctx_reqs(struct percpu_ref *ref)
  494. {
  495. struct kioctx *ctx = container_of(ref, struct kioctx, reqs);
  496. /* At this point we know that there are no any in-flight requests */
  497. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  498. complete(&ctx->rq_wait->comp);
  499. /* Synchronize against RCU protected table->table[] dereferences */
  500. INIT_RCU_WORK(&ctx->free_rwork, free_ioctx);
  501. queue_rcu_work(system_wq, &ctx->free_rwork);
  502. }
  503. /*
  504. * When this function runs, the kioctx has been removed from the "hash table"
  505. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  506. * now it's safe to cancel any that need to be.
  507. */
  508. static void free_ioctx_users(struct percpu_ref *ref)
  509. {
  510. struct kioctx *ctx = container_of(ref, struct kioctx, users);
  511. struct aio_kiocb *req;
  512. spin_lock_irq(&ctx->ctx_lock);
  513. while (!list_empty(&ctx->active_reqs)) {
  514. req = list_first_entry(&ctx->active_reqs,
  515. struct aio_kiocb, ki_list);
  516. req->ki_cancel(&req->rw);
  517. list_del_init(&req->ki_list);
  518. }
  519. spin_unlock_irq(&ctx->ctx_lock);
  520. percpu_ref_kill(&ctx->reqs);
  521. percpu_ref_put(&ctx->reqs);
  522. }
  523. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  524. {
  525. unsigned i, new_nr;
  526. struct kioctx_table *table, *old;
  527. struct aio_ring *ring;
  528. spin_lock(&mm->ioctx_lock);
  529. table = rcu_dereference_raw(mm->ioctx_table);
  530. while (1) {
  531. if (table)
  532. for (i = 0; i < table->nr; i++)
  533. if (!rcu_access_pointer(table->table[i])) {
  534. ctx->id = i;
  535. rcu_assign_pointer(table->table[i], ctx);
  536. spin_unlock(&mm->ioctx_lock);
  537. /* While kioctx setup is in progress,
  538. * we are protected from page migration
  539. * changes ring_pages by ->ring_lock.
  540. */
  541. ring = kmap_atomic(ctx->ring_pages[0]);
  542. ring->id = ctx->id;
  543. kunmap_atomic(ring);
  544. return 0;
  545. }
  546. new_nr = (table ? table->nr : 1) * 4;
  547. spin_unlock(&mm->ioctx_lock);
  548. table = kzalloc(sizeof(*table) + sizeof(struct kioctx *) *
  549. new_nr, GFP_KERNEL);
  550. if (!table)
  551. return -ENOMEM;
  552. table->nr = new_nr;
  553. spin_lock(&mm->ioctx_lock);
  554. old = rcu_dereference_raw(mm->ioctx_table);
  555. if (!old) {
  556. rcu_assign_pointer(mm->ioctx_table, table);
  557. } else if (table->nr > old->nr) {
  558. memcpy(table->table, old->table,
  559. old->nr * sizeof(struct kioctx *));
  560. rcu_assign_pointer(mm->ioctx_table, table);
  561. kfree_rcu(old, rcu);
  562. } else {
  563. kfree(table);
  564. table = old;
  565. }
  566. }
  567. }
  568. static void aio_nr_sub(unsigned nr)
  569. {
  570. spin_lock(&aio_nr_lock);
  571. if (WARN_ON(aio_nr - nr > aio_nr))
  572. aio_nr = 0;
  573. else
  574. aio_nr -= nr;
  575. spin_unlock(&aio_nr_lock);
  576. }
  577. /* ioctx_alloc
  578. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  579. */
  580. static struct kioctx *ioctx_alloc(unsigned nr_events)
  581. {
  582. struct mm_struct *mm = current->mm;
  583. struct kioctx *ctx;
  584. int err = -ENOMEM;
  585. /*
  586. * Store the original nr_events -- what userspace passed to io_setup(),
  587. * for counting against the global limit -- before it changes.
  588. */
  589. unsigned int max_reqs = nr_events;
  590. /*
  591. * We keep track of the number of available ringbuffer slots, to prevent
  592. * overflow (reqs_available), and we also use percpu counters for this.
  593. *
  594. * So since up to half the slots might be on other cpu's percpu counters
  595. * and unavailable, double nr_events so userspace sees what they
  596. * expected: additionally, we move req_batch slots to/from percpu
  597. * counters at a time, so make sure that isn't 0:
  598. */
  599. nr_events = max(nr_events, num_possible_cpus() * 4);
  600. nr_events *= 2;
  601. /* Prevent overflows */
  602. if (nr_events > (0x10000000U / sizeof(struct io_event))) {
  603. pr_debug("ENOMEM: nr_events too high\n");
  604. return ERR_PTR(-EINVAL);
  605. }
  606. if (!nr_events || (unsigned long)max_reqs > aio_max_nr)
  607. return ERR_PTR(-EAGAIN);
  608. ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
  609. if (!ctx)
  610. return ERR_PTR(-ENOMEM);
  611. ctx->max_reqs = max_reqs;
  612. spin_lock_init(&ctx->ctx_lock);
  613. spin_lock_init(&ctx->completion_lock);
  614. mutex_init(&ctx->ring_lock);
  615. /* Protect against page migration throughout kiotx setup by keeping
  616. * the ring_lock mutex held until setup is complete. */
  617. mutex_lock(&ctx->ring_lock);
  618. init_waitqueue_head(&ctx->wait);
  619. INIT_LIST_HEAD(&ctx->active_reqs);
  620. if (percpu_ref_init(&ctx->users, free_ioctx_users, 0, GFP_KERNEL))
  621. goto err;
  622. if (percpu_ref_init(&ctx->reqs, free_ioctx_reqs, 0, GFP_KERNEL))
  623. goto err;
  624. ctx->cpu = alloc_percpu(struct kioctx_cpu);
  625. if (!ctx->cpu)
  626. goto err;
  627. err = aio_setup_ring(ctx, nr_events);
  628. if (err < 0)
  629. goto err;
  630. atomic_set(&ctx->reqs_available, ctx->nr_events - 1);
  631. ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4);
  632. if (ctx->req_batch < 1)
  633. ctx->req_batch = 1;
  634. /* limit the number of system wide aios */
  635. spin_lock(&aio_nr_lock);
  636. if (aio_nr + ctx->max_reqs > aio_max_nr ||
  637. aio_nr + ctx->max_reqs < aio_nr) {
  638. spin_unlock(&aio_nr_lock);
  639. err = -EAGAIN;
  640. goto err_ctx;
  641. }
  642. aio_nr += ctx->max_reqs;
  643. spin_unlock(&aio_nr_lock);
  644. percpu_ref_get(&ctx->users); /* io_setup() will drop this ref */
  645. percpu_ref_get(&ctx->reqs); /* free_ioctx_users() will drop this */
  646. err = ioctx_add_table(ctx, mm);
  647. if (err)
  648. goto err_cleanup;
  649. /* Release the ring_lock mutex now that all setup is complete. */
  650. mutex_unlock(&ctx->ring_lock);
  651. pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  652. ctx, ctx->user_id, mm, ctx->nr_events);
  653. return ctx;
  654. err_cleanup:
  655. aio_nr_sub(ctx->max_reqs);
  656. err_ctx:
  657. atomic_set(&ctx->dead, 1);
  658. if (ctx->mmap_size)
  659. vm_munmap(ctx->mmap_base, ctx->mmap_size);
  660. aio_free_ring(ctx);
  661. err:
  662. mutex_unlock(&ctx->ring_lock);
  663. free_percpu(ctx->cpu);
  664. percpu_ref_exit(&ctx->reqs);
  665. percpu_ref_exit(&ctx->users);
  666. kmem_cache_free(kioctx_cachep, ctx);
  667. pr_debug("error allocating ioctx %d\n", err);
  668. return ERR_PTR(err);
  669. }
  670. /* kill_ioctx
  671. * Cancels all outstanding aio requests on an aio context. Used
  672. * when the processes owning a context have all exited to encourage
  673. * the rapid destruction of the kioctx.
  674. */
  675. static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx,
  676. struct ctx_rq_wait *wait)
  677. {
  678. struct kioctx_table *table;
  679. spin_lock(&mm->ioctx_lock);
  680. if (atomic_xchg(&ctx->dead, 1)) {
  681. spin_unlock(&mm->ioctx_lock);
  682. return -EINVAL;
  683. }
  684. table = rcu_dereference_raw(mm->ioctx_table);
  685. WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id]));
  686. RCU_INIT_POINTER(table->table[ctx->id], NULL);
  687. spin_unlock(&mm->ioctx_lock);
  688. /* free_ioctx_reqs() will do the necessary RCU synchronization */
  689. wake_up_all(&ctx->wait);
  690. /*
  691. * It'd be more correct to do this in free_ioctx(), after all
  692. * the outstanding kiocbs have finished - but by then io_destroy
  693. * has already returned, so io_setup() could potentially return
  694. * -EAGAIN with no ioctxs actually in use (as far as userspace
  695. * could tell).
  696. */
  697. aio_nr_sub(ctx->max_reqs);
  698. if (ctx->mmap_size)
  699. vm_munmap(ctx->mmap_base, ctx->mmap_size);
  700. ctx->rq_wait = wait;
  701. percpu_ref_kill(&ctx->users);
  702. return 0;
  703. }
  704. /*
  705. * exit_aio: called when the last user of mm goes away. At this point, there is
  706. * no way for any new requests to be submited or any of the io_* syscalls to be
  707. * called on the context.
  708. *
  709. * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
  710. * them.
  711. */
  712. void exit_aio(struct mm_struct *mm)
  713. {
  714. struct kioctx_table *table = rcu_dereference_raw(mm->ioctx_table);
  715. struct ctx_rq_wait wait;
  716. int i, skipped;
  717. if (!table)
  718. return;
  719. atomic_set(&wait.count, table->nr);
  720. init_completion(&wait.comp);
  721. skipped = 0;
  722. for (i = 0; i < table->nr; ++i) {
  723. struct kioctx *ctx =
  724. rcu_dereference_protected(table->table[i], true);
  725. if (!ctx) {
  726. skipped++;
  727. continue;
  728. }
  729. /*
  730. * We don't need to bother with munmap() here - exit_mmap(mm)
  731. * is coming and it'll unmap everything. And we simply can't,
  732. * this is not necessarily our ->mm.
  733. * Since kill_ioctx() uses non-zero ->mmap_size as indicator
  734. * that it needs to unmap the area, just set it to 0.
  735. */
  736. ctx->mmap_size = 0;
  737. kill_ioctx(mm, ctx, &wait);
  738. }
  739. if (!atomic_sub_and_test(skipped, &wait.count)) {
  740. /* Wait until all IO for the context are done. */
  741. wait_for_completion(&wait.comp);
  742. }
  743. RCU_INIT_POINTER(mm->ioctx_table, NULL);
  744. kfree(table);
  745. }
  746. static void put_reqs_available(struct kioctx *ctx, unsigned nr)
  747. {
  748. struct kioctx_cpu *kcpu;
  749. unsigned long flags;
  750. local_irq_save(flags);
  751. kcpu = this_cpu_ptr(ctx->cpu);
  752. kcpu->reqs_available += nr;
  753. while (kcpu->reqs_available >= ctx->req_batch * 2) {
  754. kcpu->reqs_available -= ctx->req_batch;
  755. atomic_add(ctx->req_batch, &ctx->reqs_available);
  756. }
  757. local_irq_restore(flags);
  758. }
  759. static bool get_reqs_available(struct kioctx *ctx)
  760. {
  761. struct kioctx_cpu *kcpu;
  762. bool ret = false;
  763. unsigned long flags;
  764. local_irq_save(flags);
  765. kcpu = this_cpu_ptr(ctx->cpu);
  766. if (!kcpu->reqs_available) {
  767. int old, avail = atomic_read(&ctx->reqs_available);
  768. do {
  769. if (avail < ctx->req_batch)
  770. goto out;
  771. old = avail;
  772. avail = atomic_cmpxchg(&ctx->reqs_available,
  773. avail, avail - ctx->req_batch);
  774. } while (avail != old);
  775. kcpu->reqs_available += ctx->req_batch;
  776. }
  777. ret = true;
  778. kcpu->reqs_available--;
  779. out:
  780. local_irq_restore(flags);
  781. return ret;
  782. }
  783. /* refill_reqs_available
  784. * Updates the reqs_available reference counts used for tracking the
  785. * number of free slots in the completion ring. This can be called
  786. * from aio_complete() (to optimistically update reqs_available) or
  787. * from aio_get_req() (the we're out of events case). It must be
  788. * called holding ctx->completion_lock.
  789. */
  790. static void refill_reqs_available(struct kioctx *ctx, unsigned head,
  791. unsigned tail)
  792. {
  793. unsigned events_in_ring, completed;
  794. /* Clamp head since userland can write to it. */
  795. head %= ctx->nr_events;
  796. if (head <= tail)
  797. events_in_ring = tail - head;
  798. else
  799. events_in_ring = ctx->nr_events - (head - tail);
  800. completed = ctx->completed_events;
  801. if (events_in_ring < completed)
  802. completed -= events_in_ring;
  803. else
  804. completed = 0;
  805. if (!completed)
  806. return;
  807. ctx->completed_events -= completed;
  808. put_reqs_available(ctx, completed);
  809. }
  810. /* user_refill_reqs_available
  811. * Called to refill reqs_available when aio_get_req() encounters an
  812. * out of space in the completion ring.
  813. */
  814. static void user_refill_reqs_available(struct kioctx *ctx)
  815. {
  816. spin_lock_irq(&ctx->completion_lock);
  817. if (ctx->completed_events) {
  818. struct aio_ring *ring;
  819. unsigned head;
  820. /* Access of ring->head may race with aio_read_events_ring()
  821. * here, but that's okay since whether we read the old version
  822. * or the new version, and either will be valid. The important
  823. * part is that head cannot pass tail since we prevent
  824. * aio_complete() from updating tail by holding
  825. * ctx->completion_lock. Even if head is invalid, the check
  826. * against ctx->completed_events below will make sure we do the
  827. * safe/right thing.
  828. */
  829. ring = kmap_atomic(ctx->ring_pages[0]);
  830. head = ring->head;
  831. kunmap_atomic(ring);
  832. refill_reqs_available(ctx, head, ctx->tail);
  833. }
  834. spin_unlock_irq(&ctx->completion_lock);
  835. }
  836. /* aio_get_req
  837. * Allocate a slot for an aio request.
  838. * Returns NULL if no requests are free.
  839. */
  840. static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
  841. {
  842. struct aio_kiocb *req;
  843. if (!get_reqs_available(ctx)) {
  844. user_refill_reqs_available(ctx);
  845. if (!get_reqs_available(ctx))
  846. return NULL;
  847. }
  848. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
  849. if (unlikely(!req))
  850. goto out_put;
  851. percpu_ref_get(&ctx->reqs);
  852. INIT_LIST_HEAD(&req->ki_list);
  853. req->ki_ctx = ctx;
  854. return req;
  855. out_put:
  856. put_reqs_available(ctx, 1);
  857. return NULL;
  858. }
  859. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  860. {
  861. struct aio_ring __user *ring = (void __user *)ctx_id;
  862. struct mm_struct *mm = current->mm;
  863. struct kioctx *ctx, *ret = NULL;
  864. struct kioctx_table *table;
  865. unsigned id;
  866. if (get_user(id, &ring->id))
  867. return NULL;
  868. rcu_read_lock();
  869. table = rcu_dereference(mm->ioctx_table);
  870. if (!table || id >= table->nr)
  871. goto out;
  872. ctx = rcu_dereference(table->table[id]);
  873. if (ctx && ctx->user_id == ctx_id) {
  874. if (percpu_ref_tryget_live(&ctx->users))
  875. ret = ctx;
  876. }
  877. out:
  878. rcu_read_unlock();
  879. return ret;
  880. }
  881. /* aio_complete
  882. * Called when the io request on the given iocb is complete.
  883. */
  884. static void aio_complete(struct aio_kiocb *iocb, long res, long res2)
  885. {
  886. struct kioctx *ctx = iocb->ki_ctx;
  887. struct aio_ring *ring;
  888. struct io_event *ev_page, *event;
  889. unsigned tail, pos, head;
  890. unsigned long flags;
  891. /*
  892. * Add a completion event to the ring buffer. Must be done holding
  893. * ctx->completion_lock to prevent other code from messing with the tail
  894. * pointer since we might be called from irq context.
  895. */
  896. spin_lock_irqsave(&ctx->completion_lock, flags);
  897. tail = ctx->tail;
  898. pos = tail + AIO_EVENTS_OFFSET;
  899. if (++tail >= ctx->nr_events)
  900. tail = 0;
  901. ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  902. event = ev_page + pos % AIO_EVENTS_PER_PAGE;
  903. event->obj = (u64)(unsigned long)iocb->ki_user_iocb;
  904. event->data = iocb->ki_user_data;
  905. event->res = res;
  906. event->res2 = res2;
  907. kunmap_atomic(ev_page);
  908. flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  909. pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
  910. ctx, tail, iocb, iocb->ki_user_iocb, iocb->ki_user_data,
  911. res, res2);
  912. /* after flagging the request as done, we
  913. * must never even look at it again
  914. */
  915. smp_wmb(); /* make event visible before updating tail */
  916. ctx->tail = tail;
  917. ring = kmap_atomic(ctx->ring_pages[0]);
  918. head = ring->head;
  919. ring->tail = tail;
  920. kunmap_atomic(ring);
  921. flush_dcache_page(ctx->ring_pages[0]);
  922. ctx->completed_events++;
  923. if (ctx->completed_events > 1)
  924. refill_reqs_available(ctx, head, tail);
  925. spin_unlock_irqrestore(&ctx->completion_lock, flags);
  926. pr_debug("added to ring %p at [%u]\n", iocb, tail);
  927. /*
  928. * Check if the user asked us to deliver the result through an
  929. * eventfd. The eventfd_signal() function is safe to be called
  930. * from IRQ context.
  931. */
  932. if (iocb->ki_eventfd) {
  933. eventfd_signal(iocb->ki_eventfd, 1);
  934. eventfd_ctx_put(iocb->ki_eventfd);
  935. }
  936. kmem_cache_free(kiocb_cachep, iocb);
  937. /*
  938. * We have to order our ring_info tail store above and test
  939. * of the wait list below outside the wait lock. This is
  940. * like in wake_up_bit() where clearing a bit has to be
  941. * ordered with the unlocked test.
  942. */
  943. smp_mb();
  944. if (waitqueue_active(&ctx->wait))
  945. wake_up(&ctx->wait);
  946. percpu_ref_put(&ctx->reqs);
  947. }
  948. /* aio_read_events_ring
  949. * Pull an event off of the ioctx's event ring. Returns the number of
  950. * events fetched
  951. */
  952. static long aio_read_events_ring(struct kioctx *ctx,
  953. struct io_event __user *event, long nr)
  954. {
  955. struct aio_ring *ring;
  956. unsigned head, tail, pos;
  957. long ret = 0;
  958. int copy_ret;
  959. /*
  960. * The mutex can block and wake us up and that will cause
  961. * wait_event_interruptible_hrtimeout() to schedule without sleeping
  962. * and repeat. This should be rare enough that it doesn't cause
  963. * peformance issues. See the comment in read_events() for more detail.
  964. */
  965. sched_annotate_sleep();
  966. mutex_lock(&ctx->ring_lock);
  967. /* Access to ->ring_pages here is protected by ctx->ring_lock. */
  968. ring = kmap_atomic(ctx->ring_pages[0]);
  969. head = ring->head;
  970. tail = ring->tail;
  971. kunmap_atomic(ring);
  972. /*
  973. * Ensure that once we've read the current tail pointer, that
  974. * we also see the events that were stored up to the tail.
  975. */
  976. smp_rmb();
  977. pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
  978. if (head == tail)
  979. goto out;
  980. head %= ctx->nr_events;
  981. tail %= ctx->nr_events;
  982. while (ret < nr) {
  983. long avail;
  984. struct io_event *ev;
  985. struct page *page;
  986. avail = (head <= tail ? tail : ctx->nr_events) - head;
  987. if (head == tail)
  988. break;
  989. pos = head + AIO_EVENTS_OFFSET;
  990. page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
  991. pos %= AIO_EVENTS_PER_PAGE;
  992. avail = min(avail, nr - ret);
  993. avail = min_t(long, avail, AIO_EVENTS_PER_PAGE - pos);
  994. ev = kmap(page);
  995. copy_ret = copy_to_user(event + ret, ev + pos,
  996. sizeof(*ev) * avail);
  997. kunmap(page);
  998. if (unlikely(copy_ret)) {
  999. ret = -EFAULT;
  1000. goto out;
  1001. }
  1002. ret += avail;
  1003. head += avail;
  1004. head %= ctx->nr_events;
  1005. }
  1006. ring = kmap_atomic(ctx->ring_pages[0]);
  1007. ring->head = head;
  1008. kunmap_atomic(ring);
  1009. flush_dcache_page(ctx->ring_pages[0]);
  1010. pr_debug("%li h%u t%u\n", ret, head, tail);
  1011. out:
  1012. mutex_unlock(&ctx->ring_lock);
  1013. return ret;
  1014. }
  1015. static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
  1016. struct io_event __user *event, long *i)
  1017. {
  1018. long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
  1019. if (ret > 0)
  1020. *i += ret;
  1021. if (unlikely(atomic_read(&ctx->dead)))
  1022. ret = -EINVAL;
  1023. if (!*i)
  1024. *i = ret;
  1025. return ret < 0 || *i >= min_nr;
  1026. }
  1027. static long read_events(struct kioctx *ctx, long min_nr, long nr,
  1028. struct io_event __user *event,
  1029. ktime_t until)
  1030. {
  1031. long ret = 0;
  1032. /*
  1033. * Note that aio_read_events() is being called as the conditional - i.e.
  1034. * we're calling it after prepare_to_wait() has set task state to
  1035. * TASK_INTERRUPTIBLE.
  1036. *
  1037. * But aio_read_events() can block, and if it blocks it's going to flip
  1038. * the task state back to TASK_RUNNING.
  1039. *
  1040. * This should be ok, provided it doesn't flip the state back to
  1041. * TASK_RUNNING and return 0 too much - that causes us to spin. That
  1042. * will only happen if the mutex_lock() call blocks, and we then find
  1043. * the ringbuffer empty. So in practice we should be ok, but it's
  1044. * something to be aware of when touching this code.
  1045. */
  1046. if (until == 0)
  1047. aio_read_events(ctx, min_nr, nr, event, &ret);
  1048. else
  1049. wait_event_interruptible_hrtimeout(ctx->wait,
  1050. aio_read_events(ctx, min_nr, nr, event, &ret),
  1051. until);
  1052. return ret;
  1053. }
  1054. /* sys_io_setup:
  1055. * Create an aio_context capable of receiving at least nr_events.
  1056. * ctxp must not point to an aio_context that already exists, and
  1057. * must be initialized to 0 prior to the call. On successful
  1058. * creation of the aio_context, *ctxp is filled in with the resulting
  1059. * handle. May fail with -EINVAL if *ctxp is not initialized,
  1060. * if the specified nr_events exceeds internal limits. May fail
  1061. * with -EAGAIN if the specified nr_events exceeds the user's limit
  1062. * of available events. May fail with -ENOMEM if insufficient kernel
  1063. * resources are available. May fail with -EFAULT if an invalid
  1064. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  1065. * implemented.
  1066. */
  1067. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  1068. {
  1069. struct kioctx *ioctx = NULL;
  1070. unsigned long ctx;
  1071. long ret;
  1072. ret = get_user(ctx, ctxp);
  1073. if (unlikely(ret))
  1074. goto out;
  1075. ret = -EINVAL;
  1076. if (unlikely(ctx || nr_events == 0)) {
  1077. pr_debug("EINVAL: ctx %lu nr_events %u\n",
  1078. ctx, nr_events);
  1079. goto out;
  1080. }
  1081. ioctx = ioctx_alloc(nr_events);
  1082. ret = PTR_ERR(ioctx);
  1083. if (!IS_ERR(ioctx)) {
  1084. ret = put_user(ioctx->user_id, ctxp);
  1085. if (ret)
  1086. kill_ioctx(current->mm, ioctx, NULL);
  1087. percpu_ref_put(&ioctx->users);
  1088. }
  1089. out:
  1090. return ret;
  1091. }
  1092. #ifdef CONFIG_COMPAT
  1093. COMPAT_SYSCALL_DEFINE2(io_setup, unsigned, nr_events, u32 __user *, ctx32p)
  1094. {
  1095. struct kioctx *ioctx = NULL;
  1096. unsigned long ctx;
  1097. long ret;
  1098. ret = get_user(ctx, ctx32p);
  1099. if (unlikely(ret))
  1100. goto out;
  1101. ret = -EINVAL;
  1102. if (unlikely(ctx || nr_events == 0)) {
  1103. pr_debug("EINVAL: ctx %lu nr_events %u\n",
  1104. ctx, nr_events);
  1105. goto out;
  1106. }
  1107. ioctx = ioctx_alloc(nr_events);
  1108. ret = PTR_ERR(ioctx);
  1109. if (!IS_ERR(ioctx)) {
  1110. /* truncating is ok because it's a user address */
  1111. ret = put_user((u32)ioctx->user_id, ctx32p);
  1112. if (ret)
  1113. kill_ioctx(current->mm, ioctx, NULL);
  1114. percpu_ref_put(&ioctx->users);
  1115. }
  1116. out:
  1117. return ret;
  1118. }
  1119. #endif
  1120. /* sys_io_destroy:
  1121. * Destroy the aio_context specified. May cancel any outstanding
  1122. * AIOs and block on completion. Will fail with -ENOSYS if not
  1123. * implemented. May fail with -EINVAL if the context pointed to
  1124. * is invalid.
  1125. */
  1126. SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
  1127. {
  1128. struct kioctx *ioctx = lookup_ioctx(ctx);
  1129. if (likely(NULL != ioctx)) {
  1130. struct ctx_rq_wait wait;
  1131. int ret;
  1132. init_completion(&wait.comp);
  1133. atomic_set(&wait.count, 1);
  1134. /* Pass requests_done to kill_ioctx() where it can be set
  1135. * in a thread-safe way. If we try to set it here then we have
  1136. * a race condition if two io_destroy() called simultaneously.
  1137. */
  1138. ret = kill_ioctx(current->mm, ioctx, &wait);
  1139. percpu_ref_put(&ioctx->users);
  1140. /* Wait until all IO for the context are done. Otherwise kernel
  1141. * keep using user-space buffers even if user thinks the context
  1142. * is destroyed.
  1143. */
  1144. if (!ret)
  1145. wait_for_completion(&wait.comp);
  1146. return ret;
  1147. }
  1148. pr_debug("EINVAL: invalid context id\n");
  1149. return -EINVAL;
  1150. }
  1151. static void aio_remove_iocb(struct aio_kiocb *iocb)
  1152. {
  1153. struct kioctx *ctx = iocb->ki_ctx;
  1154. unsigned long flags;
  1155. spin_lock_irqsave(&ctx->ctx_lock, flags);
  1156. list_del(&iocb->ki_list);
  1157. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  1158. }
  1159. static void aio_complete_rw(struct kiocb *kiocb, long res, long res2)
  1160. {
  1161. struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw);
  1162. if (!list_empty_careful(&iocb->ki_list))
  1163. aio_remove_iocb(iocb);
  1164. if (kiocb->ki_flags & IOCB_WRITE) {
  1165. struct inode *inode = file_inode(kiocb->ki_filp);
  1166. /*
  1167. * Tell lockdep we inherited freeze protection from submission
  1168. * thread.
  1169. */
  1170. if (S_ISREG(inode->i_mode))
  1171. __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
  1172. file_end_write(kiocb->ki_filp);
  1173. }
  1174. fput(kiocb->ki_filp);
  1175. aio_complete(iocb, res, res2);
  1176. }
  1177. static int aio_prep_rw(struct kiocb *req, struct iocb *iocb)
  1178. {
  1179. int ret;
  1180. req->ki_filp = fget(iocb->aio_fildes);
  1181. if (unlikely(!req->ki_filp))
  1182. return -EBADF;
  1183. req->ki_complete = aio_complete_rw;
  1184. req->ki_pos = iocb->aio_offset;
  1185. req->ki_flags = iocb_flags(req->ki_filp);
  1186. if (iocb->aio_flags & IOCB_FLAG_RESFD)
  1187. req->ki_flags |= IOCB_EVENTFD;
  1188. req->ki_hint = ki_hint_validate(file_write_hint(req->ki_filp));
  1189. if (iocb->aio_flags & IOCB_FLAG_IOPRIO) {
  1190. /*
  1191. * If the IOCB_FLAG_IOPRIO flag of aio_flags is set, then
  1192. * aio_reqprio is interpreted as an I/O scheduling
  1193. * class and priority.
  1194. */
  1195. ret = ioprio_check_cap(iocb->aio_reqprio);
  1196. if (ret) {
  1197. pr_debug("aio ioprio check cap error: %d\n", ret);
  1198. return ret;
  1199. }
  1200. req->ki_ioprio = iocb->aio_reqprio;
  1201. } else
  1202. req->ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
  1203. ret = kiocb_set_rw_flags(req, iocb->aio_rw_flags);
  1204. if (unlikely(ret))
  1205. fput(req->ki_filp);
  1206. return ret;
  1207. }
  1208. static int aio_setup_rw(int rw, struct iocb *iocb, struct iovec **iovec,
  1209. bool vectored, bool compat, struct iov_iter *iter)
  1210. {
  1211. void __user *buf = (void __user *)(uintptr_t)iocb->aio_buf;
  1212. size_t len = iocb->aio_nbytes;
  1213. if (!vectored) {
  1214. ssize_t ret = import_single_range(rw, buf, len, *iovec, iter);
  1215. *iovec = NULL;
  1216. return ret;
  1217. }
  1218. #ifdef CONFIG_COMPAT
  1219. if (compat)
  1220. return compat_import_iovec(rw, buf, len, UIO_FASTIOV, iovec,
  1221. iter);
  1222. #endif
  1223. return import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter);
  1224. }
  1225. static inline void aio_rw_done(struct kiocb *req, ssize_t ret)
  1226. {
  1227. switch (ret) {
  1228. case -EIOCBQUEUED:
  1229. break;
  1230. case -ERESTARTSYS:
  1231. case -ERESTARTNOINTR:
  1232. case -ERESTARTNOHAND:
  1233. case -ERESTART_RESTARTBLOCK:
  1234. /*
  1235. * There's no easy way to restart the syscall since other AIO's
  1236. * may be already running. Just fail this IO with EINTR.
  1237. */
  1238. ret = -EINTR;
  1239. /*FALLTHRU*/
  1240. default:
  1241. aio_complete_rw(req, ret, 0);
  1242. }
  1243. }
  1244. static ssize_t aio_read(struct kiocb *req, struct iocb *iocb, bool vectored,
  1245. bool compat)
  1246. {
  1247. struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
  1248. struct iov_iter iter;
  1249. struct file *file;
  1250. ssize_t ret;
  1251. ret = aio_prep_rw(req, iocb);
  1252. if (ret)
  1253. return ret;
  1254. file = req->ki_filp;
  1255. ret = -EBADF;
  1256. if (unlikely(!(file->f_mode & FMODE_READ)))
  1257. goto out_fput;
  1258. ret = -EINVAL;
  1259. if (unlikely(!file->f_op->read_iter))
  1260. goto out_fput;
  1261. ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
  1262. if (ret)
  1263. goto out_fput;
  1264. ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
  1265. if (!ret)
  1266. aio_rw_done(req, call_read_iter(file, req, &iter));
  1267. kfree(iovec);
  1268. out_fput:
  1269. if (unlikely(ret))
  1270. fput(file);
  1271. return ret;
  1272. }
  1273. static ssize_t aio_write(struct kiocb *req, struct iocb *iocb, bool vectored,
  1274. bool compat)
  1275. {
  1276. struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
  1277. struct iov_iter iter;
  1278. struct file *file;
  1279. ssize_t ret;
  1280. ret = aio_prep_rw(req, iocb);
  1281. if (ret)
  1282. return ret;
  1283. file = req->ki_filp;
  1284. ret = -EBADF;
  1285. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1286. goto out_fput;
  1287. ret = -EINVAL;
  1288. if (unlikely(!file->f_op->write_iter))
  1289. goto out_fput;
  1290. ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
  1291. if (ret)
  1292. goto out_fput;
  1293. ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter));
  1294. if (!ret) {
  1295. /*
  1296. * Open-code file_start_write here to grab freeze protection,
  1297. * which will be released by another thread in
  1298. * aio_complete_rw(). Fool lockdep by telling it the lock got
  1299. * released so that it doesn't complain about the held lock when
  1300. * we return to userspace.
  1301. */
  1302. if (S_ISREG(file_inode(file)->i_mode)) {
  1303. __sb_start_write(file_inode(file)->i_sb, SB_FREEZE_WRITE, true);
  1304. __sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
  1305. }
  1306. req->ki_flags |= IOCB_WRITE;
  1307. aio_rw_done(req, call_write_iter(file, req, &iter));
  1308. }
  1309. kfree(iovec);
  1310. out_fput:
  1311. if (unlikely(ret))
  1312. fput(file);
  1313. return ret;
  1314. }
  1315. static void aio_fsync_work(struct work_struct *work)
  1316. {
  1317. struct fsync_iocb *req = container_of(work, struct fsync_iocb, work);
  1318. int ret;
  1319. ret = vfs_fsync(req->file, req->datasync);
  1320. fput(req->file);
  1321. aio_complete(container_of(req, struct aio_kiocb, fsync), ret, 0);
  1322. }
  1323. static int aio_fsync(struct fsync_iocb *req, struct iocb *iocb, bool datasync)
  1324. {
  1325. if (unlikely(iocb->aio_buf || iocb->aio_offset || iocb->aio_nbytes ||
  1326. iocb->aio_rw_flags))
  1327. return -EINVAL;
  1328. req->file = fget(iocb->aio_fildes);
  1329. if (unlikely(!req->file))
  1330. return -EBADF;
  1331. if (unlikely(!req->file->f_op->fsync)) {
  1332. fput(req->file);
  1333. return -EINVAL;
  1334. }
  1335. req->datasync = datasync;
  1336. INIT_WORK(&req->work, aio_fsync_work);
  1337. schedule_work(&req->work);
  1338. return 0;
  1339. }
  1340. /* need to use list_del_init so we can check if item was present */
  1341. static inline bool __aio_poll_remove(struct poll_iocb *req)
  1342. {
  1343. if (list_empty(&req->wait.entry))
  1344. return false;
  1345. list_del_init(&req->wait.entry);
  1346. return true;
  1347. }
  1348. static inline void __aio_poll_complete(struct aio_kiocb *iocb, __poll_t mask)
  1349. {
  1350. fput(iocb->poll.file);
  1351. aio_complete(iocb, mangle_poll(mask), 0);
  1352. }
  1353. static void aio_poll_work(struct work_struct *work)
  1354. {
  1355. struct aio_kiocb *iocb = container_of(work, struct aio_kiocb, poll.work);
  1356. if (!list_empty_careful(&iocb->ki_list))
  1357. aio_remove_iocb(iocb);
  1358. __aio_poll_complete(iocb, iocb->poll.events);
  1359. }
  1360. static int aio_poll_cancel(struct kiocb *iocb)
  1361. {
  1362. struct aio_kiocb *aiocb = container_of(iocb, struct aio_kiocb, rw);
  1363. struct poll_iocb *req = &aiocb->poll;
  1364. struct wait_queue_head *head = req->head;
  1365. bool found = false;
  1366. spin_lock(&head->lock);
  1367. found = __aio_poll_remove(req);
  1368. spin_unlock(&head->lock);
  1369. if (found) {
  1370. req->events = 0;
  1371. INIT_WORK(&req->work, aio_poll_work);
  1372. schedule_work(&req->work);
  1373. }
  1374. return 0;
  1375. }
  1376. static int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
  1377. void *key)
  1378. {
  1379. struct poll_iocb *req = container_of(wait, struct poll_iocb, wait);
  1380. struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll);
  1381. struct file *file = req->file;
  1382. __poll_t mask = key_to_poll(key);
  1383. assert_spin_locked(&req->head->lock);
  1384. /* for instances that support it check for an event match first: */
  1385. if (mask && !(mask & req->events))
  1386. return 0;
  1387. mask = file->f_op->poll_mask(file, req->events) & req->events;
  1388. if (!mask)
  1389. return 0;
  1390. __aio_poll_remove(req);
  1391. /*
  1392. * Try completing without a context switch if we can acquire ctx_lock
  1393. * without spinning. Otherwise we need to defer to a workqueue to
  1394. * avoid a deadlock due to the lock order.
  1395. */
  1396. if (spin_trylock(&iocb->ki_ctx->ctx_lock)) {
  1397. list_del_init(&iocb->ki_list);
  1398. spin_unlock(&iocb->ki_ctx->ctx_lock);
  1399. __aio_poll_complete(iocb, mask);
  1400. } else {
  1401. req->events = mask;
  1402. INIT_WORK(&req->work, aio_poll_work);
  1403. schedule_work(&req->work);
  1404. }
  1405. return 1;
  1406. }
  1407. static ssize_t aio_poll(struct aio_kiocb *aiocb, struct iocb *iocb)
  1408. {
  1409. struct kioctx *ctx = aiocb->ki_ctx;
  1410. struct poll_iocb *req = &aiocb->poll;
  1411. __poll_t mask;
  1412. /* reject any unknown events outside the normal event mask. */
  1413. if ((u16)iocb->aio_buf != iocb->aio_buf)
  1414. return -EINVAL;
  1415. /* reject fields that are not defined for poll */
  1416. if (iocb->aio_offset || iocb->aio_nbytes || iocb->aio_rw_flags)
  1417. return -EINVAL;
  1418. req->events = demangle_poll(iocb->aio_buf) | EPOLLERR | EPOLLHUP;
  1419. req->file = fget(iocb->aio_fildes);
  1420. if (unlikely(!req->file))
  1421. return -EBADF;
  1422. if (!file_has_poll_mask(req->file))
  1423. goto out_fail;
  1424. req->head = req->file->f_op->get_poll_head(req->file, req->events);
  1425. if (!req->head)
  1426. goto out_fail;
  1427. if (IS_ERR(req->head)) {
  1428. mask = EPOLLERR;
  1429. goto done;
  1430. }
  1431. init_waitqueue_func_entry(&req->wait, aio_poll_wake);
  1432. aiocb->ki_cancel = aio_poll_cancel;
  1433. spin_lock_irq(&ctx->ctx_lock);
  1434. spin_lock(&req->head->lock);
  1435. mask = req->file->f_op->poll_mask(req->file, req->events) & req->events;
  1436. if (!mask) {
  1437. __add_wait_queue(req->head, &req->wait);
  1438. list_add_tail(&aiocb->ki_list, &ctx->active_reqs);
  1439. }
  1440. spin_unlock(&req->head->lock);
  1441. spin_unlock_irq(&ctx->ctx_lock);
  1442. done:
  1443. if (mask)
  1444. __aio_poll_complete(aiocb, mask);
  1445. return 0;
  1446. out_fail:
  1447. fput(req->file);
  1448. return -EINVAL; /* same as no support for IOCB_CMD_POLL */
  1449. }
  1450. static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  1451. bool compat)
  1452. {
  1453. struct aio_kiocb *req;
  1454. struct iocb iocb;
  1455. ssize_t ret;
  1456. if (unlikely(copy_from_user(&iocb, user_iocb, sizeof(iocb))))
  1457. return -EFAULT;
  1458. /* enforce forwards compatibility on users */
  1459. if (unlikely(iocb.aio_reserved2)) {
  1460. pr_debug("EINVAL: reserve field set\n");
  1461. return -EINVAL;
  1462. }
  1463. /* prevent overflows */
  1464. if (unlikely(
  1465. (iocb.aio_buf != (unsigned long)iocb.aio_buf) ||
  1466. (iocb.aio_nbytes != (size_t)iocb.aio_nbytes) ||
  1467. ((ssize_t)iocb.aio_nbytes < 0)
  1468. )) {
  1469. pr_debug("EINVAL: overflow check\n");
  1470. return -EINVAL;
  1471. }
  1472. req = aio_get_req(ctx);
  1473. if (unlikely(!req))
  1474. return -EAGAIN;
  1475. if (iocb.aio_flags & IOCB_FLAG_RESFD) {
  1476. /*
  1477. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  1478. * instance of the file* now. The file descriptor must be
  1479. * an eventfd() fd, and will be signaled for each completed
  1480. * event using the eventfd_signal() function.
  1481. */
  1482. req->ki_eventfd = eventfd_ctx_fdget((int) iocb.aio_resfd);
  1483. if (IS_ERR(req->ki_eventfd)) {
  1484. ret = PTR_ERR(req->ki_eventfd);
  1485. req->ki_eventfd = NULL;
  1486. goto out_put_req;
  1487. }
  1488. }
  1489. ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
  1490. if (unlikely(ret)) {
  1491. pr_debug("EFAULT: aio_key\n");
  1492. goto out_put_req;
  1493. }
  1494. req->ki_user_iocb = user_iocb;
  1495. req->ki_user_data = iocb.aio_data;
  1496. switch (iocb.aio_lio_opcode) {
  1497. case IOCB_CMD_PREAD:
  1498. ret = aio_read(&req->rw, &iocb, false, compat);
  1499. break;
  1500. case IOCB_CMD_PWRITE:
  1501. ret = aio_write(&req->rw, &iocb, false, compat);
  1502. break;
  1503. case IOCB_CMD_PREADV:
  1504. ret = aio_read(&req->rw, &iocb, true, compat);
  1505. break;
  1506. case IOCB_CMD_PWRITEV:
  1507. ret = aio_write(&req->rw, &iocb, true, compat);
  1508. break;
  1509. case IOCB_CMD_FSYNC:
  1510. ret = aio_fsync(&req->fsync, &iocb, false);
  1511. break;
  1512. case IOCB_CMD_FDSYNC:
  1513. ret = aio_fsync(&req->fsync, &iocb, true);
  1514. break;
  1515. case IOCB_CMD_POLL:
  1516. ret = aio_poll(req, &iocb);
  1517. break;
  1518. default:
  1519. pr_debug("invalid aio operation %d\n", iocb.aio_lio_opcode);
  1520. ret = -EINVAL;
  1521. break;
  1522. }
  1523. /*
  1524. * If ret is 0, we'd either done aio_complete() ourselves or have
  1525. * arranged for that to be done asynchronously. Anything non-zero
  1526. * means that we need to destroy req ourselves.
  1527. */
  1528. if (ret)
  1529. goto out_put_req;
  1530. return 0;
  1531. out_put_req:
  1532. put_reqs_available(ctx, 1);
  1533. percpu_ref_put(&ctx->reqs);
  1534. if (req->ki_eventfd)
  1535. eventfd_ctx_put(req->ki_eventfd);
  1536. kmem_cache_free(kiocb_cachep, req);
  1537. return ret;
  1538. }
  1539. /* sys_io_submit:
  1540. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1541. * the number of iocbs queued. May return -EINVAL if the aio_context
  1542. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1543. * *iocbpp[0] is not properly initialized, if the operation specified
  1544. * is invalid for the file descriptor in the iocb. May fail with
  1545. * -EFAULT if any of the data structures point to invalid data. May
  1546. * fail with -EBADF if the file descriptor specified in the first
  1547. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1548. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1549. * fail with -ENOSYS if not implemented.
  1550. */
  1551. SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
  1552. struct iocb __user * __user *, iocbpp)
  1553. {
  1554. struct kioctx *ctx;
  1555. long ret = 0;
  1556. int i = 0;
  1557. struct blk_plug plug;
  1558. if (unlikely(nr < 0))
  1559. return -EINVAL;
  1560. ctx = lookup_ioctx(ctx_id);
  1561. if (unlikely(!ctx)) {
  1562. pr_debug("EINVAL: invalid context id\n");
  1563. return -EINVAL;
  1564. }
  1565. if (nr > ctx->nr_events)
  1566. nr = ctx->nr_events;
  1567. blk_start_plug(&plug);
  1568. for (i = 0; i < nr; i++) {
  1569. struct iocb __user *user_iocb;
  1570. if (unlikely(get_user(user_iocb, iocbpp + i))) {
  1571. ret = -EFAULT;
  1572. break;
  1573. }
  1574. ret = io_submit_one(ctx, user_iocb, false);
  1575. if (ret)
  1576. break;
  1577. }
  1578. blk_finish_plug(&plug);
  1579. percpu_ref_put(&ctx->users);
  1580. return i ? i : ret;
  1581. }
  1582. #ifdef CONFIG_COMPAT
  1583. COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id,
  1584. int, nr, compat_uptr_t __user *, iocbpp)
  1585. {
  1586. struct kioctx *ctx;
  1587. long ret = 0;
  1588. int i = 0;
  1589. struct blk_plug plug;
  1590. if (unlikely(nr < 0))
  1591. return -EINVAL;
  1592. ctx = lookup_ioctx(ctx_id);
  1593. if (unlikely(!ctx)) {
  1594. pr_debug("EINVAL: invalid context id\n");
  1595. return -EINVAL;
  1596. }
  1597. if (nr > ctx->nr_events)
  1598. nr = ctx->nr_events;
  1599. blk_start_plug(&plug);
  1600. for (i = 0; i < nr; i++) {
  1601. compat_uptr_t user_iocb;
  1602. if (unlikely(get_user(user_iocb, iocbpp + i))) {
  1603. ret = -EFAULT;
  1604. break;
  1605. }
  1606. ret = io_submit_one(ctx, compat_ptr(user_iocb), true);
  1607. if (ret)
  1608. break;
  1609. }
  1610. blk_finish_plug(&plug);
  1611. percpu_ref_put(&ctx->users);
  1612. return i ? i : ret;
  1613. }
  1614. #endif
  1615. /* lookup_kiocb
  1616. * Finds a given iocb for cancellation.
  1617. */
  1618. static struct aio_kiocb *
  1619. lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb)
  1620. {
  1621. struct aio_kiocb *kiocb;
  1622. assert_spin_locked(&ctx->ctx_lock);
  1623. /* TODO: use a hash or array, this sucks. */
  1624. list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) {
  1625. if (kiocb->ki_user_iocb == iocb)
  1626. return kiocb;
  1627. }
  1628. return NULL;
  1629. }
  1630. /* sys_io_cancel:
  1631. * Attempts to cancel an iocb previously passed to io_submit. If
  1632. * the operation is successfully cancelled, the resulting event is
  1633. * copied into the memory pointed to by result without being placed
  1634. * into the completion queue and 0 is returned. May fail with
  1635. * -EFAULT if any of the data structures pointed to are invalid.
  1636. * May fail with -EINVAL if aio_context specified by ctx_id is
  1637. * invalid. May fail with -EAGAIN if the iocb specified was not
  1638. * cancelled. Will fail with -ENOSYS if not implemented.
  1639. */
  1640. SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
  1641. struct io_event __user *, result)
  1642. {
  1643. struct kioctx *ctx;
  1644. struct aio_kiocb *kiocb;
  1645. int ret = -EINVAL;
  1646. u32 key;
  1647. if (unlikely(get_user(key, &iocb->aio_key)))
  1648. return -EFAULT;
  1649. if (unlikely(key != KIOCB_KEY))
  1650. return -EINVAL;
  1651. ctx = lookup_ioctx(ctx_id);
  1652. if (unlikely(!ctx))
  1653. return -EINVAL;
  1654. spin_lock_irq(&ctx->ctx_lock);
  1655. kiocb = lookup_kiocb(ctx, iocb);
  1656. if (kiocb) {
  1657. ret = kiocb->ki_cancel(&kiocb->rw);
  1658. list_del_init(&kiocb->ki_list);
  1659. }
  1660. spin_unlock_irq(&ctx->ctx_lock);
  1661. if (!ret) {
  1662. /*
  1663. * The result argument is no longer used - the io_event is
  1664. * always delivered via the ring buffer. -EINPROGRESS indicates
  1665. * cancellation is progress:
  1666. */
  1667. ret = -EINPROGRESS;
  1668. }
  1669. percpu_ref_put(&ctx->users);
  1670. return ret;
  1671. }
  1672. static long do_io_getevents(aio_context_t ctx_id,
  1673. long min_nr,
  1674. long nr,
  1675. struct io_event __user *events,
  1676. struct timespec64 *ts)
  1677. {
  1678. ktime_t until = ts ? timespec64_to_ktime(*ts) : KTIME_MAX;
  1679. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1680. long ret = -EINVAL;
  1681. if (likely(ioctx)) {
  1682. if (likely(min_nr <= nr && min_nr >= 0))
  1683. ret = read_events(ioctx, min_nr, nr, events, until);
  1684. percpu_ref_put(&ioctx->users);
  1685. }
  1686. return ret;
  1687. }
  1688. /* io_getevents:
  1689. * Attempts to read at least min_nr events and up to nr events from
  1690. * the completion queue for the aio_context specified by ctx_id. If
  1691. * it succeeds, the number of read events is returned. May fail with
  1692. * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
  1693. * out of range, if timeout is out of range. May fail with -EFAULT
  1694. * if any of the memory specified is invalid. May return 0 or
  1695. * < min_nr if the timeout specified by timeout has elapsed
  1696. * before sufficient events are available, where timeout == NULL
  1697. * specifies an infinite timeout. Note that the timeout pointed to by
  1698. * timeout is relative. Will fail with -ENOSYS if not implemented.
  1699. */
  1700. SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
  1701. long, min_nr,
  1702. long, nr,
  1703. struct io_event __user *, events,
  1704. struct timespec __user *, timeout)
  1705. {
  1706. struct timespec64 ts;
  1707. int ret;
  1708. if (timeout && unlikely(get_timespec64(&ts, timeout)))
  1709. return -EFAULT;
  1710. ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
  1711. if (!ret && signal_pending(current))
  1712. ret = -EINTR;
  1713. return ret;
  1714. }
  1715. SYSCALL_DEFINE6(io_pgetevents,
  1716. aio_context_t, ctx_id,
  1717. long, min_nr,
  1718. long, nr,
  1719. struct io_event __user *, events,
  1720. struct timespec __user *, timeout,
  1721. const struct __aio_sigset __user *, usig)
  1722. {
  1723. struct __aio_sigset ksig = { NULL, };
  1724. sigset_t ksigmask, sigsaved;
  1725. struct timespec64 ts;
  1726. int ret;
  1727. if (timeout && unlikely(get_timespec64(&ts, timeout)))
  1728. return -EFAULT;
  1729. if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
  1730. return -EFAULT;
  1731. if (ksig.sigmask) {
  1732. if (ksig.sigsetsize != sizeof(sigset_t))
  1733. return -EINVAL;
  1734. if (copy_from_user(&ksigmask, ksig.sigmask, sizeof(ksigmask)))
  1735. return -EFAULT;
  1736. sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  1737. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1738. }
  1739. ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
  1740. if (signal_pending(current)) {
  1741. if (ksig.sigmask) {
  1742. current->saved_sigmask = sigsaved;
  1743. set_restore_sigmask();
  1744. }
  1745. if (!ret)
  1746. ret = -ERESTARTNOHAND;
  1747. } else {
  1748. if (ksig.sigmask)
  1749. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1750. }
  1751. return ret;
  1752. }
  1753. #ifdef CONFIG_COMPAT
  1754. COMPAT_SYSCALL_DEFINE5(io_getevents, compat_aio_context_t, ctx_id,
  1755. compat_long_t, min_nr,
  1756. compat_long_t, nr,
  1757. struct io_event __user *, events,
  1758. struct compat_timespec __user *, timeout)
  1759. {
  1760. struct timespec64 t;
  1761. int ret;
  1762. if (timeout && compat_get_timespec64(&t, timeout))
  1763. return -EFAULT;
  1764. ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
  1765. if (!ret && signal_pending(current))
  1766. ret = -EINTR;
  1767. return ret;
  1768. }
  1769. struct __compat_aio_sigset {
  1770. compat_sigset_t __user *sigmask;
  1771. compat_size_t sigsetsize;
  1772. };
  1773. COMPAT_SYSCALL_DEFINE6(io_pgetevents,
  1774. compat_aio_context_t, ctx_id,
  1775. compat_long_t, min_nr,
  1776. compat_long_t, nr,
  1777. struct io_event __user *, events,
  1778. struct compat_timespec __user *, timeout,
  1779. const struct __compat_aio_sigset __user *, usig)
  1780. {
  1781. struct __compat_aio_sigset ksig = { NULL, };
  1782. sigset_t ksigmask, sigsaved;
  1783. struct timespec64 t;
  1784. int ret;
  1785. if (timeout && compat_get_timespec64(&t, timeout))
  1786. return -EFAULT;
  1787. if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
  1788. return -EFAULT;
  1789. if (ksig.sigmask) {
  1790. if (ksig.sigsetsize != sizeof(compat_sigset_t))
  1791. return -EINVAL;
  1792. if (get_compat_sigset(&ksigmask, ksig.sigmask))
  1793. return -EFAULT;
  1794. sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  1795. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1796. }
  1797. ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
  1798. if (signal_pending(current)) {
  1799. if (ksig.sigmask) {
  1800. current->saved_sigmask = sigsaved;
  1801. set_restore_sigmask();
  1802. }
  1803. if (!ret)
  1804. ret = -ERESTARTNOHAND;
  1805. } else {
  1806. if (ksig.sigmask)
  1807. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1808. }
  1809. return ret;
  1810. }
  1811. #endif