aio.c 38 KB

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