aio.c 33 KB

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