aio.c 50 KB

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