aio.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/errno.h>
  14. #include <linux/time.h>
  15. #include <linux/aio_abi.h>
  16. #include <linux/export.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/backing-dev.h>
  19. #include <linux/uio.h>
  20. #define DEBUG 0
  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. #if DEBUG > 1
  39. #define dprintk printk
  40. #else
  41. #define dprintk(x...) do { ; } while (0)
  42. #endif
  43. /*------ sysctl variables----*/
  44. static DEFINE_SPINLOCK(aio_nr_lock);
  45. unsigned long aio_nr; /* current system wide number of aio requests */
  46. unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
  47. /*----end sysctl variables---*/
  48. static struct kmem_cache *kiocb_cachep;
  49. static struct kmem_cache *kioctx_cachep;
  50. static struct workqueue_struct *aio_wq;
  51. /* Used for rare fput completion. */
  52. static void aio_fput_routine(struct work_struct *);
  53. static DECLARE_WORK(fput_work, aio_fput_routine);
  54. static DEFINE_SPINLOCK(fput_lock);
  55. static LIST_HEAD(fput_head);
  56. static void aio_kick_handler(struct work_struct *);
  57. static void aio_queue_work(struct kioctx *);
  58. /* aio_setup
  59. * Creates the slab caches used by the aio routines, panic on
  60. * failure as this is done early during the boot sequence.
  61. */
  62. static int __init aio_setup(void)
  63. {
  64. kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  65. kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  66. aio_wq = alloc_workqueue("aio", 0, 1); /* used to limit concurrency */
  67. BUG_ON(!aio_wq);
  68. pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
  69. return 0;
  70. }
  71. __initcall(aio_setup);
  72. static void aio_free_ring(struct kioctx *ctx)
  73. {
  74. struct aio_ring_info *info = &ctx->ring_info;
  75. long i;
  76. for (i=0; i<info->nr_pages; i++)
  77. put_page(info->ring_pages[i]);
  78. if (info->mmap_size) {
  79. down_write(&ctx->mm->mmap_sem);
  80. do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
  81. up_write(&ctx->mm->mmap_sem);
  82. }
  83. if (info->ring_pages && info->ring_pages != info->internal_pages)
  84. kfree(info->ring_pages);
  85. info->ring_pages = NULL;
  86. info->nr = 0;
  87. }
  88. static int aio_setup_ring(struct kioctx *ctx)
  89. {
  90. struct aio_ring *ring;
  91. struct aio_ring_info *info = &ctx->ring_info;
  92. unsigned nr_events = ctx->max_reqs;
  93. unsigned long size;
  94. int nr_pages;
  95. /* Compensate for the ring buffer's head/tail overlap entry */
  96. nr_events += 2; /* 1 is required, 2 for good luck */
  97. size = sizeof(struct aio_ring);
  98. size += sizeof(struct io_event) * nr_events;
  99. nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
  100. if (nr_pages < 0)
  101. return -EINVAL;
  102. nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
  103. info->nr = 0;
  104. info->ring_pages = info->internal_pages;
  105. if (nr_pages > AIO_RING_PAGES) {
  106. info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  107. if (!info->ring_pages)
  108. return -ENOMEM;
  109. }
  110. info->mmap_size = nr_pages * PAGE_SIZE;
  111. dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
  112. down_write(&ctx->mm->mmap_sem);
  113. info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
  114. PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
  115. 0);
  116. if (IS_ERR((void *)info->mmap_base)) {
  117. up_write(&ctx->mm->mmap_sem);
  118. info->mmap_size = 0;
  119. aio_free_ring(ctx);
  120. return -EAGAIN;
  121. }
  122. dprintk("mmap address: 0x%08lx\n", info->mmap_base);
  123. info->nr_pages = get_user_pages(current, ctx->mm,
  124. info->mmap_base, nr_pages,
  125. 1, 0, info->ring_pages, NULL);
  126. up_write(&ctx->mm->mmap_sem);
  127. if (unlikely(info->nr_pages != nr_pages)) {
  128. aio_free_ring(ctx);
  129. return -EAGAIN;
  130. }
  131. ctx->user_id = info->mmap_base;
  132. info->nr = nr_events; /* trusted copy */
  133. ring = kmap_atomic(info->ring_pages[0]);
  134. ring->nr = nr_events; /* user copy */
  135. ring->id = ctx->user_id;
  136. ring->head = ring->tail = 0;
  137. ring->magic = AIO_RING_MAGIC;
  138. ring->compat_features = AIO_RING_COMPAT_FEATURES;
  139. ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
  140. ring->header_length = sizeof(struct aio_ring);
  141. kunmap_atomic(ring);
  142. return 0;
  143. }
  144. /* aio_ring_event: returns a pointer to the event at the given index from
  145. * kmap_atomic(). Release the pointer with put_aio_ring_event();
  146. */
  147. #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
  148. #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
  149. #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
  150. #define aio_ring_event(info, nr) ({ \
  151. unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
  152. struct io_event *__event; \
  153. __event = kmap_atomic( \
  154. (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE]); \
  155. __event += pos % AIO_EVENTS_PER_PAGE; \
  156. __event; \
  157. })
  158. #define put_aio_ring_event(event) do { \
  159. struct io_event *__event = (event); \
  160. (void)__event; \
  161. kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK)); \
  162. } while(0)
  163. static void ctx_rcu_free(struct rcu_head *head)
  164. {
  165. struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
  166. kmem_cache_free(kioctx_cachep, ctx);
  167. }
  168. /* __put_ioctx
  169. * Called when the last user of an aio context has gone away,
  170. * and the struct needs to be freed.
  171. */
  172. static void __put_ioctx(struct kioctx *ctx)
  173. {
  174. unsigned nr_events = ctx->max_reqs;
  175. BUG_ON(ctx->reqs_active);
  176. cancel_delayed_work_sync(&ctx->wq);
  177. aio_free_ring(ctx);
  178. mmdrop(ctx->mm);
  179. ctx->mm = NULL;
  180. if (nr_events) {
  181. spin_lock(&aio_nr_lock);
  182. BUG_ON(aio_nr - nr_events > aio_nr);
  183. aio_nr -= nr_events;
  184. spin_unlock(&aio_nr_lock);
  185. }
  186. pr_debug("__put_ioctx: freeing %p\n", ctx);
  187. call_rcu(&ctx->rcu_head, ctx_rcu_free);
  188. }
  189. static inline int try_get_ioctx(struct kioctx *kioctx)
  190. {
  191. return atomic_inc_not_zero(&kioctx->users);
  192. }
  193. static inline void put_ioctx(struct kioctx *kioctx)
  194. {
  195. BUG_ON(atomic_read(&kioctx->users) <= 0);
  196. if (unlikely(atomic_dec_and_test(&kioctx->users)))
  197. __put_ioctx(kioctx);
  198. }
  199. /* ioctx_alloc
  200. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  201. */
  202. static struct kioctx *ioctx_alloc(unsigned nr_events)
  203. {
  204. struct mm_struct *mm;
  205. struct kioctx *ctx;
  206. int err = -ENOMEM;
  207. /* Prevent overflows */
  208. if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
  209. (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
  210. pr_debug("ENOMEM: nr_events too high\n");
  211. return ERR_PTR(-EINVAL);
  212. }
  213. if (!nr_events || (unsigned long)nr_events > aio_max_nr)
  214. return ERR_PTR(-EAGAIN);
  215. ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
  216. if (!ctx)
  217. return ERR_PTR(-ENOMEM);
  218. ctx->max_reqs = nr_events;
  219. mm = ctx->mm = current->mm;
  220. atomic_inc(&mm->mm_count);
  221. atomic_set(&ctx->users, 2);
  222. spin_lock_init(&ctx->ctx_lock);
  223. spin_lock_init(&ctx->ring_info.ring_lock);
  224. init_waitqueue_head(&ctx->wait);
  225. INIT_LIST_HEAD(&ctx->active_reqs);
  226. INIT_LIST_HEAD(&ctx->run_list);
  227. INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
  228. if (aio_setup_ring(ctx) < 0)
  229. goto out_freectx;
  230. /* limit the number of system wide aios */
  231. spin_lock(&aio_nr_lock);
  232. if (aio_nr + nr_events > aio_max_nr ||
  233. aio_nr + nr_events < aio_nr) {
  234. spin_unlock(&aio_nr_lock);
  235. goto out_cleanup;
  236. }
  237. aio_nr += ctx->max_reqs;
  238. spin_unlock(&aio_nr_lock);
  239. /* now link into global list. */
  240. spin_lock(&mm->ioctx_lock);
  241. hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
  242. spin_unlock(&mm->ioctx_lock);
  243. dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  244. ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
  245. return ctx;
  246. out_cleanup:
  247. err = -EAGAIN;
  248. aio_free_ring(ctx);
  249. out_freectx:
  250. mmdrop(mm);
  251. kmem_cache_free(kioctx_cachep, ctx);
  252. dprintk("aio: error allocating ioctx %d\n", err);
  253. return ERR_PTR(err);
  254. }
  255. /* kill_ctx
  256. * Cancels all outstanding aio requests on an aio context. Used
  257. * when the processes owning a context have all exited to encourage
  258. * the rapid destruction of the kioctx.
  259. */
  260. static void kill_ctx(struct kioctx *ctx)
  261. {
  262. int (*cancel)(struct kiocb *, struct io_event *);
  263. struct task_struct *tsk = current;
  264. DECLARE_WAITQUEUE(wait, tsk);
  265. struct io_event res;
  266. spin_lock_irq(&ctx->ctx_lock);
  267. ctx->dead = 1;
  268. while (!list_empty(&ctx->active_reqs)) {
  269. struct list_head *pos = ctx->active_reqs.next;
  270. struct kiocb *iocb = list_kiocb(pos);
  271. list_del_init(&iocb->ki_list);
  272. cancel = iocb->ki_cancel;
  273. kiocbSetCancelled(iocb);
  274. if (cancel) {
  275. iocb->ki_users++;
  276. spin_unlock_irq(&ctx->ctx_lock);
  277. cancel(iocb, &res);
  278. spin_lock_irq(&ctx->ctx_lock);
  279. }
  280. }
  281. if (!ctx->reqs_active)
  282. goto out;
  283. add_wait_queue(&ctx->wait, &wait);
  284. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  285. while (ctx->reqs_active) {
  286. spin_unlock_irq(&ctx->ctx_lock);
  287. io_schedule();
  288. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  289. spin_lock_irq(&ctx->ctx_lock);
  290. }
  291. __set_task_state(tsk, TASK_RUNNING);
  292. remove_wait_queue(&ctx->wait, &wait);
  293. out:
  294. spin_unlock_irq(&ctx->ctx_lock);
  295. }
  296. /* wait_on_sync_kiocb:
  297. * Waits on the given sync kiocb to complete.
  298. */
  299. ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
  300. {
  301. while (iocb->ki_users) {
  302. set_current_state(TASK_UNINTERRUPTIBLE);
  303. if (!iocb->ki_users)
  304. break;
  305. io_schedule();
  306. }
  307. __set_current_state(TASK_RUNNING);
  308. return iocb->ki_user_data;
  309. }
  310. EXPORT_SYMBOL(wait_on_sync_kiocb);
  311. /* exit_aio: called when the last user of mm goes away. At this point,
  312. * there is no way for any new requests to be submited or any of the
  313. * io_* syscalls to be called on the context. However, there may be
  314. * outstanding requests which hold references to the context; as they
  315. * go away, they will call put_ioctx and release any pinned memory
  316. * associated with the request (held via struct page * references).
  317. */
  318. void exit_aio(struct mm_struct *mm)
  319. {
  320. struct kioctx *ctx;
  321. while (!hlist_empty(&mm->ioctx_list)) {
  322. ctx = hlist_entry(mm->ioctx_list.first, struct kioctx, list);
  323. hlist_del_rcu(&ctx->list);
  324. kill_ctx(ctx);
  325. if (1 != atomic_read(&ctx->users))
  326. printk(KERN_DEBUG
  327. "exit_aio:ioctx still alive: %d %d %d\n",
  328. atomic_read(&ctx->users), ctx->dead,
  329. ctx->reqs_active);
  330. put_ioctx(ctx);
  331. }
  332. }
  333. /* aio_get_req
  334. * Allocate a slot for an aio request. Increments the users count
  335. * of the kioctx so that the kioctx stays around until all requests are
  336. * complete. Returns NULL if no requests are free.
  337. *
  338. * Returns with kiocb->users set to 2. The io submit code path holds
  339. * an extra reference while submitting the i/o.
  340. * This prevents races between the aio code path referencing the
  341. * req (after submitting it) and aio_complete() freeing the req.
  342. */
  343. static struct kiocb *__aio_get_req(struct kioctx *ctx)
  344. {
  345. struct kiocb *req = NULL;
  346. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
  347. if (unlikely(!req))
  348. return NULL;
  349. req->ki_flags = 0;
  350. req->ki_users = 2;
  351. req->ki_key = 0;
  352. req->ki_ctx = ctx;
  353. req->ki_cancel = NULL;
  354. req->ki_retry = NULL;
  355. req->ki_dtor = NULL;
  356. req->private = NULL;
  357. req->ki_iovec = NULL;
  358. INIT_LIST_HEAD(&req->ki_run_list);
  359. req->ki_eventfd = NULL;
  360. return req;
  361. }
  362. /*
  363. * struct kiocb's are allocated in batches to reduce the number of
  364. * times the ctx lock is acquired and released.
  365. */
  366. #define KIOCB_BATCH_SIZE 32L
  367. struct kiocb_batch {
  368. struct list_head head;
  369. long count; /* number of requests left to allocate */
  370. };
  371. static void kiocb_batch_init(struct kiocb_batch *batch, long total)
  372. {
  373. INIT_LIST_HEAD(&batch->head);
  374. batch->count = total;
  375. }
  376. static void kiocb_batch_free(struct kioctx *ctx, struct kiocb_batch *batch)
  377. {
  378. struct kiocb *req, *n;
  379. if (list_empty(&batch->head))
  380. return;
  381. spin_lock_irq(&ctx->ctx_lock);
  382. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  383. list_del(&req->ki_batch);
  384. list_del(&req->ki_list);
  385. kmem_cache_free(kiocb_cachep, req);
  386. ctx->reqs_active--;
  387. }
  388. if (unlikely(!ctx->reqs_active && ctx->dead))
  389. wake_up_all(&ctx->wait);
  390. spin_unlock_irq(&ctx->ctx_lock);
  391. }
  392. /*
  393. * Allocate a batch of kiocbs. This avoids taking and dropping the
  394. * context lock a lot during setup.
  395. */
  396. static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
  397. {
  398. unsigned short allocated, to_alloc;
  399. long avail;
  400. bool called_fput = false;
  401. struct kiocb *req, *n;
  402. struct aio_ring *ring;
  403. to_alloc = min(batch->count, KIOCB_BATCH_SIZE);
  404. for (allocated = 0; allocated < to_alloc; allocated++) {
  405. req = __aio_get_req(ctx);
  406. if (!req)
  407. /* allocation failed, go with what we've got */
  408. break;
  409. list_add(&req->ki_batch, &batch->head);
  410. }
  411. if (allocated == 0)
  412. goto out;
  413. retry:
  414. spin_lock_irq(&ctx->ctx_lock);
  415. ring = kmap_atomic(ctx->ring_info.ring_pages[0]);
  416. avail = aio_ring_avail(&ctx->ring_info, ring) - ctx->reqs_active;
  417. BUG_ON(avail < 0);
  418. if (avail == 0 && !called_fput) {
  419. /*
  420. * Handle a potential starvation case. It is possible that
  421. * we hold the last reference on a struct file, causing us
  422. * to delay the final fput to non-irq context. In this case,
  423. * ctx->reqs_active is artificially high. Calling the fput
  424. * routine here may free up a slot in the event completion
  425. * ring, allowing this allocation to succeed.
  426. */
  427. kunmap_atomic(ring);
  428. spin_unlock_irq(&ctx->ctx_lock);
  429. aio_fput_routine(NULL);
  430. called_fput = true;
  431. goto retry;
  432. }
  433. if (avail < allocated) {
  434. /* Trim back the number of requests. */
  435. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  436. list_del(&req->ki_batch);
  437. kmem_cache_free(kiocb_cachep, req);
  438. if (--allocated <= avail)
  439. break;
  440. }
  441. }
  442. batch->count -= allocated;
  443. list_for_each_entry(req, &batch->head, ki_batch) {
  444. list_add(&req->ki_list, &ctx->active_reqs);
  445. ctx->reqs_active++;
  446. }
  447. kunmap_atomic(ring);
  448. spin_unlock_irq(&ctx->ctx_lock);
  449. out:
  450. return allocated;
  451. }
  452. static inline struct kiocb *aio_get_req(struct kioctx *ctx,
  453. struct kiocb_batch *batch)
  454. {
  455. struct kiocb *req;
  456. if (list_empty(&batch->head))
  457. if (kiocb_batch_refill(ctx, batch) == 0)
  458. return NULL;
  459. req = list_first_entry(&batch->head, struct kiocb, ki_batch);
  460. list_del(&req->ki_batch);
  461. return req;
  462. }
  463. static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
  464. {
  465. assert_spin_locked(&ctx->ctx_lock);
  466. if (req->ki_eventfd != NULL)
  467. eventfd_ctx_put(req->ki_eventfd);
  468. if (req->ki_dtor)
  469. req->ki_dtor(req);
  470. if (req->ki_iovec != &req->ki_inline_vec)
  471. kfree(req->ki_iovec);
  472. kmem_cache_free(kiocb_cachep, req);
  473. ctx->reqs_active--;
  474. if (unlikely(!ctx->reqs_active && ctx->dead))
  475. wake_up_all(&ctx->wait);
  476. }
  477. static void aio_fput_routine(struct work_struct *data)
  478. {
  479. spin_lock_irq(&fput_lock);
  480. while (likely(!list_empty(&fput_head))) {
  481. struct kiocb *req = list_kiocb(fput_head.next);
  482. struct kioctx *ctx = req->ki_ctx;
  483. list_del(&req->ki_list);
  484. spin_unlock_irq(&fput_lock);
  485. /* Complete the fput(s) */
  486. if (req->ki_filp != NULL)
  487. fput(req->ki_filp);
  488. /* Link the iocb into the context's free list */
  489. rcu_read_lock();
  490. spin_lock_irq(&ctx->ctx_lock);
  491. really_put_req(ctx, req);
  492. /*
  493. * at that point ctx might've been killed, but actual
  494. * freeing is RCU'd
  495. */
  496. spin_unlock_irq(&ctx->ctx_lock);
  497. rcu_read_unlock();
  498. spin_lock_irq(&fput_lock);
  499. }
  500. spin_unlock_irq(&fput_lock);
  501. }
  502. /* __aio_put_req
  503. * Returns true if this put was the last user of the request.
  504. */
  505. static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
  506. {
  507. dprintk(KERN_DEBUG "aio_put(%p): f_count=%ld\n",
  508. req, atomic_long_read(&req->ki_filp->f_count));
  509. assert_spin_locked(&ctx->ctx_lock);
  510. req->ki_users--;
  511. BUG_ON(req->ki_users < 0);
  512. if (likely(req->ki_users))
  513. return 0;
  514. list_del(&req->ki_list); /* remove from active_reqs */
  515. req->ki_cancel = NULL;
  516. req->ki_retry = NULL;
  517. /*
  518. * Try to optimize the aio and eventfd file* puts, by avoiding to
  519. * schedule work in case it is not final fput() time. In normal cases,
  520. * we would not be holding the last reference to the file*, so
  521. * this function will be executed w/out any aio kthread wakeup.
  522. */
  523. if (unlikely(!fput_atomic(req->ki_filp))) {
  524. spin_lock(&fput_lock);
  525. list_add(&req->ki_list, &fput_head);
  526. spin_unlock(&fput_lock);
  527. schedule_work(&fput_work);
  528. } else {
  529. req->ki_filp = NULL;
  530. really_put_req(ctx, req);
  531. }
  532. return 1;
  533. }
  534. /* aio_put_req
  535. * Returns true if this put was the last user of the kiocb,
  536. * false if the request is still in use.
  537. */
  538. int aio_put_req(struct kiocb *req)
  539. {
  540. struct kioctx *ctx = req->ki_ctx;
  541. int ret;
  542. spin_lock_irq(&ctx->ctx_lock);
  543. ret = __aio_put_req(ctx, req);
  544. spin_unlock_irq(&ctx->ctx_lock);
  545. return ret;
  546. }
  547. EXPORT_SYMBOL(aio_put_req);
  548. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  549. {
  550. struct mm_struct *mm = current->mm;
  551. struct kioctx *ctx, *ret = NULL;
  552. struct hlist_node *n;
  553. rcu_read_lock();
  554. hlist_for_each_entry_rcu(ctx, n, &mm->ioctx_list, list) {
  555. /*
  556. * RCU protects us against accessing freed memory but
  557. * we have to be careful not to get a reference when the
  558. * reference count already dropped to 0 (ctx->dead test
  559. * is unreliable because of races).
  560. */
  561. if (ctx->user_id == ctx_id && !ctx->dead && try_get_ioctx(ctx)){
  562. ret = ctx;
  563. break;
  564. }
  565. }
  566. rcu_read_unlock();
  567. return ret;
  568. }
  569. /*
  570. * Queue up a kiocb to be retried. Assumes that the kiocb
  571. * has already been marked as kicked, and places it on
  572. * the retry run list for the corresponding ioctx, if it
  573. * isn't already queued. Returns 1 if it actually queued
  574. * the kiocb (to tell the caller to activate the work
  575. * queue to process it), or 0, if it found that it was
  576. * already queued.
  577. */
  578. static inline int __queue_kicked_iocb(struct kiocb *iocb)
  579. {
  580. struct kioctx *ctx = iocb->ki_ctx;
  581. assert_spin_locked(&ctx->ctx_lock);
  582. if (list_empty(&iocb->ki_run_list)) {
  583. list_add_tail(&iocb->ki_run_list,
  584. &ctx->run_list);
  585. return 1;
  586. }
  587. return 0;
  588. }
  589. /* aio_run_iocb
  590. * This is the core aio execution routine. It is
  591. * invoked both for initial i/o submission and
  592. * subsequent retries via the aio_kick_handler.
  593. * Expects to be invoked with iocb->ki_ctx->lock
  594. * already held. The lock is released and reacquired
  595. * as needed during processing.
  596. *
  597. * Calls the iocb retry method (already setup for the
  598. * iocb on initial submission) for operation specific
  599. * handling, but takes care of most of common retry
  600. * execution details for a given iocb. The retry method
  601. * needs to be non-blocking as far as possible, to avoid
  602. * holding up other iocbs waiting to be serviced by the
  603. * retry kernel thread.
  604. *
  605. * The trickier parts in this code have to do with
  606. * ensuring that only one retry instance is in progress
  607. * for a given iocb at any time. Providing that guarantee
  608. * simplifies the coding of individual aio operations as
  609. * it avoids various potential races.
  610. */
  611. static ssize_t aio_run_iocb(struct kiocb *iocb)
  612. {
  613. struct kioctx *ctx = iocb->ki_ctx;
  614. ssize_t (*retry)(struct kiocb *);
  615. ssize_t ret;
  616. if (!(retry = iocb->ki_retry)) {
  617. printk("aio_run_iocb: iocb->ki_retry = NULL\n");
  618. return 0;
  619. }
  620. /*
  621. * We don't want the next retry iteration for this
  622. * operation to start until this one has returned and
  623. * updated the iocb state. However, wait_queue functions
  624. * can trigger a kick_iocb from interrupt context in the
  625. * meantime, indicating that data is available for the next
  626. * iteration. We want to remember that and enable the
  627. * next retry iteration _after_ we are through with
  628. * this one.
  629. *
  630. * So, in order to be able to register a "kick", but
  631. * prevent it from being queued now, we clear the kick
  632. * flag, but make the kick code *think* that the iocb is
  633. * still on the run list until we are actually done.
  634. * When we are done with this iteration, we check if
  635. * the iocb was kicked in the meantime and if so, queue
  636. * it up afresh.
  637. */
  638. kiocbClearKicked(iocb);
  639. /*
  640. * This is so that aio_complete knows it doesn't need to
  641. * pull the iocb off the run list (We can't just call
  642. * INIT_LIST_HEAD because we don't want a kick_iocb to
  643. * queue this on the run list yet)
  644. */
  645. iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
  646. spin_unlock_irq(&ctx->ctx_lock);
  647. /* Quit retrying if the i/o has been cancelled */
  648. if (kiocbIsCancelled(iocb)) {
  649. ret = -EINTR;
  650. aio_complete(iocb, ret, 0);
  651. /* must not access the iocb after this */
  652. goto out;
  653. }
  654. /*
  655. * Now we are all set to call the retry method in async
  656. * context.
  657. */
  658. ret = retry(iocb);
  659. if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
  660. /*
  661. * There's no easy way to restart the syscall since other AIO's
  662. * may be already running. Just fail this IO with EINTR.
  663. */
  664. if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
  665. ret == -ERESTARTNOHAND || ret == -ERESTART_RESTARTBLOCK))
  666. ret = -EINTR;
  667. aio_complete(iocb, ret, 0);
  668. }
  669. out:
  670. spin_lock_irq(&ctx->ctx_lock);
  671. if (-EIOCBRETRY == ret) {
  672. /*
  673. * OK, now that we are done with this iteration
  674. * and know that there is more left to go,
  675. * this is where we let go so that a subsequent
  676. * "kick" can start the next iteration
  677. */
  678. /* will make __queue_kicked_iocb succeed from here on */
  679. INIT_LIST_HEAD(&iocb->ki_run_list);
  680. /* we must queue the next iteration ourselves, if it
  681. * has already been kicked */
  682. if (kiocbIsKicked(iocb)) {
  683. __queue_kicked_iocb(iocb);
  684. /*
  685. * __queue_kicked_iocb will always return 1 here, because
  686. * iocb->ki_run_list is empty at this point so it should
  687. * be safe to unconditionally queue the context into the
  688. * work queue.
  689. */
  690. aio_queue_work(ctx);
  691. }
  692. }
  693. return ret;
  694. }
  695. /*
  696. * __aio_run_iocbs:
  697. * Process all pending retries queued on the ioctx
  698. * run list.
  699. * Assumes it is operating within the aio issuer's mm
  700. * context.
  701. */
  702. static int __aio_run_iocbs(struct kioctx *ctx)
  703. {
  704. struct kiocb *iocb;
  705. struct list_head run_list;
  706. assert_spin_locked(&ctx->ctx_lock);
  707. list_replace_init(&ctx->run_list, &run_list);
  708. while (!list_empty(&run_list)) {
  709. iocb = list_entry(run_list.next, struct kiocb,
  710. ki_run_list);
  711. list_del(&iocb->ki_run_list);
  712. /*
  713. * Hold an extra reference while retrying i/o.
  714. */
  715. iocb->ki_users++; /* grab extra reference */
  716. aio_run_iocb(iocb);
  717. __aio_put_req(ctx, iocb);
  718. }
  719. if (!list_empty(&ctx->run_list))
  720. return 1;
  721. return 0;
  722. }
  723. static void aio_queue_work(struct kioctx * ctx)
  724. {
  725. unsigned long timeout;
  726. /*
  727. * if someone is waiting, get the work started right
  728. * away, otherwise, use a longer delay
  729. */
  730. smp_mb();
  731. if (waitqueue_active(&ctx->wait))
  732. timeout = 1;
  733. else
  734. timeout = HZ/10;
  735. queue_delayed_work(aio_wq, &ctx->wq, timeout);
  736. }
  737. /*
  738. * aio_run_all_iocbs:
  739. * Process all pending retries queued on the ioctx
  740. * run list, and keep running them until the list
  741. * stays empty.
  742. * Assumes it is operating within the aio issuer's mm context.
  743. */
  744. static inline void aio_run_all_iocbs(struct kioctx *ctx)
  745. {
  746. spin_lock_irq(&ctx->ctx_lock);
  747. while (__aio_run_iocbs(ctx))
  748. ;
  749. spin_unlock_irq(&ctx->ctx_lock);
  750. }
  751. /*
  752. * aio_kick_handler:
  753. * Work queue handler triggered to process pending
  754. * retries on an ioctx. Takes on the aio issuer's
  755. * mm context before running the iocbs, so that
  756. * copy_xxx_user operates on the issuer's address
  757. * space.
  758. * Run on aiod's context.
  759. */
  760. static void aio_kick_handler(struct work_struct *work)
  761. {
  762. struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
  763. mm_segment_t oldfs = get_fs();
  764. struct mm_struct *mm;
  765. int requeue;
  766. set_fs(USER_DS);
  767. use_mm(ctx->mm);
  768. spin_lock_irq(&ctx->ctx_lock);
  769. requeue =__aio_run_iocbs(ctx);
  770. mm = ctx->mm;
  771. spin_unlock_irq(&ctx->ctx_lock);
  772. unuse_mm(mm);
  773. set_fs(oldfs);
  774. /*
  775. * we're in a worker thread already; no point using non-zero delay
  776. */
  777. if (requeue)
  778. queue_delayed_work(aio_wq, &ctx->wq, 0);
  779. }
  780. /*
  781. * Called by kick_iocb to queue the kiocb for retry
  782. * and if required activate the aio work queue to process
  783. * it
  784. */
  785. static void try_queue_kicked_iocb(struct kiocb *iocb)
  786. {
  787. struct kioctx *ctx = iocb->ki_ctx;
  788. unsigned long flags;
  789. int run = 0;
  790. spin_lock_irqsave(&ctx->ctx_lock, flags);
  791. /* set this inside the lock so that we can't race with aio_run_iocb()
  792. * testing it and putting the iocb on the run list under the lock */
  793. if (!kiocbTryKick(iocb))
  794. run = __queue_kicked_iocb(iocb);
  795. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  796. if (run)
  797. aio_queue_work(ctx);
  798. }
  799. /*
  800. * kick_iocb:
  801. * Called typically from a wait queue callback context
  802. * to trigger a retry of the iocb.
  803. * The retry is usually executed by aio workqueue
  804. * threads (See aio_kick_handler).
  805. */
  806. void kick_iocb(struct kiocb *iocb)
  807. {
  808. /* sync iocbs are easy: they can only ever be executing from a
  809. * single context. */
  810. if (is_sync_kiocb(iocb)) {
  811. kiocbSetKicked(iocb);
  812. wake_up_process(iocb->ki_obj.tsk);
  813. return;
  814. }
  815. try_queue_kicked_iocb(iocb);
  816. }
  817. EXPORT_SYMBOL(kick_iocb);
  818. /* aio_complete
  819. * Called when the io request on the given iocb is complete.
  820. * Returns true if this is the last user of the request. The
  821. * only other user of the request can be the cancellation code.
  822. */
  823. int aio_complete(struct kiocb *iocb, long res, long res2)
  824. {
  825. struct kioctx *ctx = iocb->ki_ctx;
  826. struct aio_ring_info *info;
  827. struct aio_ring *ring;
  828. struct io_event *event;
  829. unsigned long flags;
  830. unsigned long tail;
  831. int ret;
  832. /*
  833. * Special case handling for sync iocbs:
  834. * - events go directly into the iocb for fast handling
  835. * - the sync task with the iocb in its stack holds the single iocb
  836. * ref, no other paths have a way to get another ref
  837. * - the sync task helpfully left a reference to itself in the iocb
  838. */
  839. if (is_sync_kiocb(iocb)) {
  840. BUG_ON(iocb->ki_users != 1);
  841. iocb->ki_user_data = res;
  842. iocb->ki_users = 0;
  843. wake_up_process(iocb->ki_obj.tsk);
  844. return 1;
  845. }
  846. info = &ctx->ring_info;
  847. /* add a completion event to the ring buffer.
  848. * must be done holding ctx->ctx_lock to prevent
  849. * other code from messing with the tail
  850. * pointer since we might be called from irq
  851. * context.
  852. */
  853. spin_lock_irqsave(&ctx->ctx_lock, flags);
  854. if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
  855. list_del_init(&iocb->ki_run_list);
  856. /*
  857. * cancelled requests don't get events, userland was given one
  858. * when the event got cancelled.
  859. */
  860. if (kiocbIsCancelled(iocb))
  861. goto put_rq;
  862. ring = kmap_atomic(info->ring_pages[0]);
  863. tail = info->tail;
  864. event = aio_ring_event(info, tail);
  865. if (++tail >= info->nr)
  866. tail = 0;
  867. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  868. event->data = iocb->ki_user_data;
  869. event->res = res;
  870. event->res2 = res2;
  871. dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
  872. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  873. res, res2);
  874. /* after flagging the request as done, we
  875. * must never even look at it again
  876. */
  877. smp_wmb(); /* make event visible before updating tail */
  878. info->tail = tail;
  879. ring->tail = tail;
  880. put_aio_ring_event(event);
  881. kunmap_atomic(ring);
  882. pr_debug("added to ring %p at [%lu]\n", iocb, tail);
  883. /*
  884. * Check if the user asked us to deliver the result through an
  885. * eventfd. The eventfd_signal() function is safe to be called
  886. * from IRQ context.
  887. */
  888. if (iocb->ki_eventfd != NULL)
  889. eventfd_signal(iocb->ki_eventfd, 1);
  890. put_rq:
  891. /* everything turned out well, dispose of the aiocb. */
  892. ret = __aio_put_req(ctx, iocb);
  893. /*
  894. * We have to order our ring_info tail store above and test
  895. * of the wait list below outside the wait lock. This is
  896. * like in wake_up_bit() where clearing a bit has to be
  897. * ordered with the unlocked test.
  898. */
  899. smp_mb();
  900. if (waitqueue_active(&ctx->wait))
  901. wake_up(&ctx->wait);
  902. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  903. return ret;
  904. }
  905. EXPORT_SYMBOL(aio_complete);
  906. /* aio_read_evt
  907. * Pull an event off of the ioctx's event ring. Returns the number of
  908. * events fetched (0 or 1 ;-)
  909. * FIXME: make this use cmpxchg.
  910. * TODO: make the ringbuffer user mmap()able (requires FIXME).
  911. */
  912. static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
  913. {
  914. struct aio_ring_info *info = &ioctx->ring_info;
  915. struct aio_ring *ring;
  916. unsigned long head;
  917. int ret = 0;
  918. ring = kmap_atomic(info->ring_pages[0]);
  919. dprintk("in aio_read_evt h%lu t%lu m%lu\n",
  920. (unsigned long)ring->head, (unsigned long)ring->tail,
  921. (unsigned long)ring->nr);
  922. if (ring->head == ring->tail)
  923. goto out;
  924. spin_lock(&info->ring_lock);
  925. head = ring->head % info->nr;
  926. if (head != ring->tail) {
  927. struct io_event *evp = aio_ring_event(info, head);
  928. *ent = *evp;
  929. head = (head + 1) % info->nr;
  930. smp_mb(); /* finish reading the event before updatng the head */
  931. ring->head = head;
  932. ret = 1;
  933. put_aio_ring_event(evp);
  934. }
  935. spin_unlock(&info->ring_lock);
  936. out:
  937. kunmap_atomic(ring);
  938. dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
  939. (unsigned long)ring->head, (unsigned long)ring->tail);
  940. return ret;
  941. }
  942. struct aio_timeout {
  943. struct timer_list timer;
  944. int timed_out;
  945. struct task_struct *p;
  946. };
  947. static void timeout_func(unsigned long data)
  948. {
  949. struct aio_timeout *to = (struct aio_timeout *)data;
  950. to->timed_out = 1;
  951. wake_up_process(to->p);
  952. }
  953. static inline void init_timeout(struct aio_timeout *to)
  954. {
  955. setup_timer_on_stack(&to->timer, timeout_func, (unsigned long) to);
  956. to->timed_out = 0;
  957. to->p = current;
  958. }
  959. static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
  960. const struct timespec *ts)
  961. {
  962. to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
  963. if (time_after(to->timer.expires, jiffies))
  964. add_timer(&to->timer);
  965. else
  966. to->timed_out = 1;
  967. }
  968. static inline void clear_timeout(struct aio_timeout *to)
  969. {
  970. del_singleshot_timer_sync(&to->timer);
  971. }
  972. static int read_events(struct kioctx *ctx,
  973. long min_nr, long nr,
  974. struct io_event __user *event,
  975. struct timespec __user *timeout)
  976. {
  977. long start_jiffies = jiffies;
  978. struct task_struct *tsk = current;
  979. DECLARE_WAITQUEUE(wait, tsk);
  980. int ret;
  981. int i = 0;
  982. struct io_event ent;
  983. struct aio_timeout to;
  984. int retry = 0;
  985. /* needed to zero any padding within an entry (there shouldn't be
  986. * any, but C is fun!
  987. */
  988. memset(&ent, 0, sizeof(ent));
  989. retry:
  990. ret = 0;
  991. while (likely(i < nr)) {
  992. ret = aio_read_evt(ctx, &ent);
  993. if (unlikely(ret <= 0))
  994. break;
  995. dprintk("read event: %Lx %Lx %Lx %Lx\n",
  996. ent.data, ent.obj, ent.res, ent.res2);
  997. /* Could we split the check in two? */
  998. ret = -EFAULT;
  999. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  1000. dprintk("aio: lost an event due to EFAULT.\n");
  1001. break;
  1002. }
  1003. ret = 0;
  1004. /* Good, event copied to userland, update counts. */
  1005. event ++;
  1006. i ++;
  1007. }
  1008. if (min_nr <= i)
  1009. return i;
  1010. if (ret)
  1011. return ret;
  1012. /* End fast path */
  1013. /* racey check, but it gets redone */
  1014. if (!retry && unlikely(!list_empty(&ctx->run_list))) {
  1015. retry = 1;
  1016. aio_run_all_iocbs(ctx);
  1017. goto retry;
  1018. }
  1019. init_timeout(&to);
  1020. if (timeout) {
  1021. struct timespec ts;
  1022. ret = -EFAULT;
  1023. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  1024. goto out;
  1025. set_timeout(start_jiffies, &to, &ts);
  1026. }
  1027. while (likely(i < nr)) {
  1028. add_wait_queue_exclusive(&ctx->wait, &wait);
  1029. do {
  1030. set_task_state(tsk, TASK_INTERRUPTIBLE);
  1031. ret = aio_read_evt(ctx, &ent);
  1032. if (ret)
  1033. break;
  1034. if (min_nr <= i)
  1035. break;
  1036. if (unlikely(ctx->dead)) {
  1037. ret = -EINVAL;
  1038. break;
  1039. }
  1040. if (to.timed_out) /* Only check after read evt */
  1041. break;
  1042. /* Try to only show up in io wait if there are ops
  1043. * in flight */
  1044. if (ctx->reqs_active)
  1045. io_schedule();
  1046. else
  1047. schedule();
  1048. if (signal_pending(tsk)) {
  1049. ret = -EINTR;
  1050. break;
  1051. }
  1052. /*ret = aio_read_evt(ctx, &ent);*/
  1053. } while (1) ;
  1054. set_task_state(tsk, TASK_RUNNING);
  1055. remove_wait_queue(&ctx->wait, &wait);
  1056. if (unlikely(ret <= 0))
  1057. break;
  1058. ret = -EFAULT;
  1059. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  1060. dprintk("aio: lost an event due to EFAULT.\n");
  1061. break;
  1062. }
  1063. /* Good, event copied to userland, update counts. */
  1064. event ++;
  1065. i ++;
  1066. }
  1067. if (timeout)
  1068. clear_timeout(&to);
  1069. out:
  1070. destroy_timer_on_stack(&to.timer);
  1071. return i ? i : ret;
  1072. }
  1073. /* Take an ioctx and remove it from the list of ioctx's. Protects
  1074. * against races with itself via ->dead.
  1075. */
  1076. static void io_destroy(struct kioctx *ioctx)
  1077. {
  1078. struct mm_struct *mm = current->mm;
  1079. int was_dead;
  1080. /* delete the entry from the list is someone else hasn't already */
  1081. spin_lock(&mm->ioctx_lock);
  1082. was_dead = ioctx->dead;
  1083. ioctx->dead = 1;
  1084. hlist_del_rcu(&ioctx->list);
  1085. spin_unlock(&mm->ioctx_lock);
  1086. dprintk("aio_release(%p)\n", ioctx);
  1087. if (likely(!was_dead))
  1088. put_ioctx(ioctx); /* twice for the list */
  1089. kill_ctx(ioctx);
  1090. /*
  1091. * Wake up any waiters. The setting of ctx->dead must be seen
  1092. * by other CPUs at this point. Right now, we rely on the
  1093. * locking done by the above calls to ensure this consistency.
  1094. */
  1095. wake_up_all(&ioctx->wait);
  1096. }
  1097. /* sys_io_setup:
  1098. * Create an aio_context capable of receiving at least nr_events.
  1099. * ctxp must not point to an aio_context that already exists, and
  1100. * must be initialized to 0 prior to the call. On successful
  1101. * creation of the aio_context, *ctxp is filled in with the resulting
  1102. * handle. May fail with -EINVAL if *ctxp is not initialized,
  1103. * if the specified nr_events exceeds internal limits. May fail
  1104. * with -EAGAIN if the specified nr_events exceeds the user's limit
  1105. * of available events. May fail with -ENOMEM if insufficient kernel
  1106. * resources are available. May fail with -EFAULT if an invalid
  1107. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  1108. * implemented.
  1109. */
  1110. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  1111. {
  1112. struct kioctx *ioctx = NULL;
  1113. unsigned long ctx;
  1114. long ret;
  1115. ret = get_user(ctx, ctxp);
  1116. if (unlikely(ret))
  1117. goto out;
  1118. ret = -EINVAL;
  1119. if (unlikely(ctx || nr_events == 0)) {
  1120. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  1121. ctx, nr_events);
  1122. goto out;
  1123. }
  1124. ioctx = ioctx_alloc(nr_events);
  1125. ret = PTR_ERR(ioctx);
  1126. if (!IS_ERR(ioctx)) {
  1127. ret = put_user(ioctx->user_id, ctxp);
  1128. if (ret)
  1129. io_destroy(ioctx);
  1130. put_ioctx(ioctx);
  1131. }
  1132. out:
  1133. return ret;
  1134. }
  1135. /* sys_io_destroy:
  1136. * Destroy the aio_context specified. May cancel any outstanding
  1137. * AIOs and block on completion. Will fail with -ENOSYS if not
  1138. * implemented. May fail with -EINVAL if the context pointed to
  1139. * is invalid.
  1140. */
  1141. SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
  1142. {
  1143. struct kioctx *ioctx = lookup_ioctx(ctx);
  1144. if (likely(NULL != ioctx)) {
  1145. io_destroy(ioctx);
  1146. put_ioctx(ioctx);
  1147. return 0;
  1148. }
  1149. pr_debug("EINVAL: io_destroy: invalid context id\n");
  1150. return -EINVAL;
  1151. }
  1152. static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
  1153. {
  1154. struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
  1155. BUG_ON(ret <= 0);
  1156. while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
  1157. ssize_t this = min((ssize_t)iov->iov_len, ret);
  1158. iov->iov_base += this;
  1159. iov->iov_len -= this;
  1160. iocb->ki_left -= this;
  1161. ret -= this;
  1162. if (iov->iov_len == 0) {
  1163. iocb->ki_cur_seg++;
  1164. iov++;
  1165. }
  1166. }
  1167. /* the caller should not have done more io than what fit in
  1168. * the remaining iovecs */
  1169. BUG_ON(ret > 0 && iocb->ki_left == 0);
  1170. }
  1171. static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
  1172. {
  1173. struct file *file = iocb->ki_filp;
  1174. struct address_space *mapping = file->f_mapping;
  1175. struct inode *inode = mapping->host;
  1176. ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
  1177. unsigned long, loff_t);
  1178. ssize_t ret = 0;
  1179. unsigned short opcode;
  1180. if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
  1181. (iocb->ki_opcode == IOCB_CMD_PREAD)) {
  1182. rw_op = file->f_op->aio_read;
  1183. opcode = IOCB_CMD_PREADV;
  1184. } else {
  1185. rw_op = file->f_op->aio_write;
  1186. opcode = IOCB_CMD_PWRITEV;
  1187. }
  1188. /* This matches the pread()/pwrite() logic */
  1189. if (iocb->ki_pos < 0)
  1190. return -EINVAL;
  1191. do {
  1192. ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
  1193. iocb->ki_nr_segs - iocb->ki_cur_seg,
  1194. iocb->ki_pos);
  1195. if (ret > 0)
  1196. aio_advance_iovec(iocb, ret);
  1197. /* retry all partial writes. retry partial reads as long as its a
  1198. * regular file. */
  1199. } while (ret > 0 && iocb->ki_left > 0 &&
  1200. (opcode == IOCB_CMD_PWRITEV ||
  1201. (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
  1202. /* This means we must have transferred all that we could */
  1203. /* No need to retry anymore */
  1204. if ((ret == 0) || (iocb->ki_left == 0))
  1205. ret = iocb->ki_nbytes - iocb->ki_left;
  1206. /* If we managed to write some out we return that, rather than
  1207. * the eventual error. */
  1208. if (opcode == IOCB_CMD_PWRITEV
  1209. && ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY
  1210. && iocb->ki_nbytes - iocb->ki_left)
  1211. ret = iocb->ki_nbytes - iocb->ki_left;
  1212. return ret;
  1213. }
  1214. static ssize_t aio_fdsync(struct kiocb *iocb)
  1215. {
  1216. struct file *file = iocb->ki_filp;
  1217. ssize_t ret = -EINVAL;
  1218. if (file->f_op->aio_fsync)
  1219. ret = file->f_op->aio_fsync(iocb, 1);
  1220. return ret;
  1221. }
  1222. static ssize_t aio_fsync(struct kiocb *iocb)
  1223. {
  1224. struct file *file = iocb->ki_filp;
  1225. ssize_t ret = -EINVAL;
  1226. if (file->f_op->aio_fsync)
  1227. ret = file->f_op->aio_fsync(iocb, 0);
  1228. return ret;
  1229. }
  1230. static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
  1231. {
  1232. ssize_t ret;
  1233. #ifdef CONFIG_COMPAT
  1234. if (compat)
  1235. ret = compat_rw_copy_check_uvector(type,
  1236. (struct compat_iovec __user *)kiocb->ki_buf,
  1237. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  1238. &kiocb->ki_iovec, 1);
  1239. else
  1240. #endif
  1241. ret = rw_copy_check_uvector(type,
  1242. (struct iovec __user *)kiocb->ki_buf,
  1243. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  1244. &kiocb->ki_iovec, 1);
  1245. if (ret < 0)
  1246. goto out;
  1247. kiocb->ki_nr_segs = kiocb->ki_nbytes;
  1248. kiocb->ki_cur_seg = 0;
  1249. /* ki_nbytes/left now reflect bytes instead of segs */
  1250. kiocb->ki_nbytes = ret;
  1251. kiocb->ki_left = ret;
  1252. ret = 0;
  1253. out:
  1254. return ret;
  1255. }
  1256. static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
  1257. {
  1258. kiocb->ki_iovec = &kiocb->ki_inline_vec;
  1259. kiocb->ki_iovec->iov_base = kiocb->ki_buf;
  1260. kiocb->ki_iovec->iov_len = kiocb->ki_left;
  1261. kiocb->ki_nr_segs = 1;
  1262. kiocb->ki_cur_seg = 0;
  1263. return 0;
  1264. }
  1265. /*
  1266. * aio_setup_iocb:
  1267. * Performs the initial checks and aio retry method
  1268. * setup for the kiocb at the time of io submission.
  1269. */
  1270. static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
  1271. {
  1272. struct file *file = kiocb->ki_filp;
  1273. ssize_t ret = 0;
  1274. switch (kiocb->ki_opcode) {
  1275. case IOCB_CMD_PREAD:
  1276. ret = -EBADF;
  1277. if (unlikely(!(file->f_mode & FMODE_READ)))
  1278. break;
  1279. ret = -EFAULT;
  1280. if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
  1281. kiocb->ki_left)))
  1282. break;
  1283. ret = security_file_permission(file, MAY_READ);
  1284. if (unlikely(ret))
  1285. break;
  1286. ret = aio_setup_single_vector(kiocb);
  1287. if (ret)
  1288. break;
  1289. ret = -EINVAL;
  1290. if (file->f_op->aio_read)
  1291. kiocb->ki_retry = aio_rw_vect_retry;
  1292. break;
  1293. case IOCB_CMD_PWRITE:
  1294. ret = -EBADF;
  1295. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1296. break;
  1297. ret = -EFAULT;
  1298. if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
  1299. kiocb->ki_left)))
  1300. break;
  1301. ret = security_file_permission(file, MAY_WRITE);
  1302. if (unlikely(ret))
  1303. break;
  1304. ret = aio_setup_single_vector(kiocb);
  1305. if (ret)
  1306. break;
  1307. ret = -EINVAL;
  1308. if (file->f_op->aio_write)
  1309. kiocb->ki_retry = aio_rw_vect_retry;
  1310. break;
  1311. case IOCB_CMD_PREADV:
  1312. ret = -EBADF;
  1313. if (unlikely(!(file->f_mode & FMODE_READ)))
  1314. break;
  1315. ret = security_file_permission(file, MAY_READ);
  1316. if (unlikely(ret))
  1317. break;
  1318. ret = aio_setup_vectored_rw(READ, kiocb, compat);
  1319. if (ret)
  1320. break;
  1321. ret = -EINVAL;
  1322. if (file->f_op->aio_read)
  1323. kiocb->ki_retry = aio_rw_vect_retry;
  1324. break;
  1325. case IOCB_CMD_PWRITEV:
  1326. ret = -EBADF;
  1327. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1328. break;
  1329. ret = security_file_permission(file, MAY_WRITE);
  1330. if (unlikely(ret))
  1331. break;
  1332. ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
  1333. if (ret)
  1334. break;
  1335. ret = -EINVAL;
  1336. if (file->f_op->aio_write)
  1337. kiocb->ki_retry = aio_rw_vect_retry;
  1338. break;
  1339. case IOCB_CMD_FDSYNC:
  1340. ret = -EINVAL;
  1341. if (file->f_op->aio_fsync)
  1342. kiocb->ki_retry = aio_fdsync;
  1343. break;
  1344. case IOCB_CMD_FSYNC:
  1345. ret = -EINVAL;
  1346. if (file->f_op->aio_fsync)
  1347. kiocb->ki_retry = aio_fsync;
  1348. break;
  1349. default:
  1350. dprintk("EINVAL: io_submit: no operation provided\n");
  1351. ret = -EINVAL;
  1352. }
  1353. if (!kiocb->ki_retry)
  1354. return ret;
  1355. return 0;
  1356. }
  1357. static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  1358. struct iocb *iocb, struct kiocb_batch *batch,
  1359. bool compat)
  1360. {
  1361. struct kiocb *req;
  1362. struct file *file;
  1363. ssize_t ret;
  1364. /* enforce forwards compatibility on users */
  1365. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
  1366. pr_debug("EINVAL: io_submit: reserve field set\n");
  1367. return -EINVAL;
  1368. }
  1369. /* prevent overflows */
  1370. if (unlikely(
  1371. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  1372. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  1373. ((ssize_t)iocb->aio_nbytes < 0)
  1374. )) {
  1375. pr_debug("EINVAL: io_submit: overflow check\n");
  1376. return -EINVAL;
  1377. }
  1378. file = fget(iocb->aio_fildes);
  1379. if (unlikely(!file))
  1380. return -EBADF;
  1381. req = aio_get_req(ctx, batch); /* returns with 2 references to req */
  1382. if (unlikely(!req)) {
  1383. fput(file);
  1384. return -EAGAIN;
  1385. }
  1386. req->ki_filp = file;
  1387. if (iocb->aio_flags & IOCB_FLAG_RESFD) {
  1388. /*
  1389. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  1390. * instance of the file* now. The file descriptor must be
  1391. * an eventfd() fd, and will be signaled for each completed
  1392. * event using the eventfd_signal() function.
  1393. */
  1394. req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
  1395. if (IS_ERR(req->ki_eventfd)) {
  1396. ret = PTR_ERR(req->ki_eventfd);
  1397. req->ki_eventfd = NULL;
  1398. goto out_put_req;
  1399. }
  1400. }
  1401. ret = put_user(req->ki_key, &user_iocb->aio_key);
  1402. if (unlikely(ret)) {
  1403. dprintk("EFAULT: aio_key\n");
  1404. goto out_put_req;
  1405. }
  1406. req->ki_obj.user = user_iocb;
  1407. req->ki_user_data = iocb->aio_data;
  1408. req->ki_pos = iocb->aio_offset;
  1409. req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
  1410. req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
  1411. req->ki_opcode = iocb->aio_lio_opcode;
  1412. ret = aio_setup_iocb(req, compat);
  1413. if (ret)
  1414. goto out_put_req;
  1415. spin_lock_irq(&ctx->ctx_lock);
  1416. /*
  1417. * We could have raced with io_destroy() and are currently holding a
  1418. * reference to ctx which should be destroyed. We cannot submit IO
  1419. * since ctx gets freed as soon as io_submit() puts its reference. The
  1420. * check here is reliable: io_destroy() sets ctx->dead before waiting
  1421. * for outstanding IO and the barrier between these two is realized by
  1422. * unlock of mm->ioctx_lock and lock of ctx->ctx_lock. Analogously we
  1423. * increment ctx->reqs_active before checking for ctx->dead and the
  1424. * barrier is realized by unlock and lock of ctx->ctx_lock. Thus if we
  1425. * don't see ctx->dead set here, io_destroy() waits for our IO to
  1426. * finish.
  1427. */
  1428. if (ctx->dead) {
  1429. spin_unlock_irq(&ctx->ctx_lock);
  1430. ret = -EINVAL;
  1431. goto out_put_req;
  1432. }
  1433. aio_run_iocb(req);
  1434. if (!list_empty(&ctx->run_list)) {
  1435. /* drain the run list */
  1436. while (__aio_run_iocbs(ctx))
  1437. ;
  1438. }
  1439. spin_unlock_irq(&ctx->ctx_lock);
  1440. aio_put_req(req); /* drop extra ref to req */
  1441. return 0;
  1442. out_put_req:
  1443. aio_put_req(req); /* drop extra ref to req */
  1444. aio_put_req(req); /* drop i/o ref to req */
  1445. return ret;
  1446. }
  1447. long do_io_submit(aio_context_t ctx_id, long nr,
  1448. struct iocb __user *__user *iocbpp, bool compat)
  1449. {
  1450. struct kioctx *ctx;
  1451. long ret = 0;
  1452. int i = 0;
  1453. struct blk_plug plug;
  1454. struct kiocb_batch batch;
  1455. if (unlikely(nr < 0))
  1456. return -EINVAL;
  1457. if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
  1458. nr = LONG_MAX/sizeof(*iocbpp);
  1459. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1460. return -EFAULT;
  1461. ctx = lookup_ioctx(ctx_id);
  1462. if (unlikely(!ctx)) {
  1463. pr_debug("EINVAL: io_submit: invalid context id\n");
  1464. return -EINVAL;
  1465. }
  1466. kiocb_batch_init(&batch, nr);
  1467. blk_start_plug(&plug);
  1468. /*
  1469. * AKPM: should this return a partial result if some of the IOs were
  1470. * successfully submitted?
  1471. */
  1472. for (i=0; i<nr; i++) {
  1473. struct iocb __user *user_iocb;
  1474. struct iocb tmp;
  1475. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1476. ret = -EFAULT;
  1477. break;
  1478. }
  1479. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1480. ret = -EFAULT;
  1481. break;
  1482. }
  1483. ret = io_submit_one(ctx, user_iocb, &tmp, &batch, compat);
  1484. if (ret)
  1485. break;
  1486. }
  1487. blk_finish_plug(&plug);
  1488. kiocb_batch_free(ctx, &batch);
  1489. put_ioctx(ctx);
  1490. return i ? i : ret;
  1491. }
  1492. /* sys_io_submit:
  1493. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1494. * the number of iocbs queued. May return -EINVAL if the aio_context
  1495. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1496. * *iocbpp[0] is not properly initialized, if the operation specified
  1497. * is invalid for the file descriptor in the iocb. May fail with
  1498. * -EFAULT if any of the data structures point to invalid data. May
  1499. * fail with -EBADF if the file descriptor specified in the first
  1500. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1501. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1502. * fail with -ENOSYS if not implemented.
  1503. */
  1504. SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
  1505. struct iocb __user * __user *, iocbpp)
  1506. {
  1507. return do_io_submit(ctx_id, nr, iocbpp, 0);
  1508. }
  1509. /* lookup_kiocb
  1510. * Finds a given iocb for cancellation.
  1511. */
  1512. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1513. u32 key)
  1514. {
  1515. struct list_head *pos;
  1516. assert_spin_locked(&ctx->ctx_lock);
  1517. /* TODO: use a hash or array, this sucks. */
  1518. list_for_each(pos, &ctx->active_reqs) {
  1519. struct kiocb *kiocb = list_kiocb(pos);
  1520. if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
  1521. return kiocb;
  1522. }
  1523. return NULL;
  1524. }
  1525. /* sys_io_cancel:
  1526. * Attempts to cancel an iocb previously passed to io_submit. If
  1527. * the operation is successfully cancelled, the resulting event is
  1528. * copied into the memory pointed to by result without being placed
  1529. * into the completion queue and 0 is returned. May fail with
  1530. * -EFAULT if any of the data structures pointed to are invalid.
  1531. * May fail with -EINVAL if aio_context specified by ctx_id is
  1532. * invalid. May fail with -EAGAIN if the iocb specified was not
  1533. * cancelled. Will fail with -ENOSYS if not implemented.
  1534. */
  1535. SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
  1536. struct io_event __user *, result)
  1537. {
  1538. int (*cancel)(struct kiocb *iocb, struct io_event *res);
  1539. struct kioctx *ctx;
  1540. struct kiocb *kiocb;
  1541. u32 key;
  1542. int ret;
  1543. ret = get_user(key, &iocb->aio_key);
  1544. if (unlikely(ret))
  1545. return -EFAULT;
  1546. ctx = lookup_ioctx(ctx_id);
  1547. if (unlikely(!ctx))
  1548. return -EINVAL;
  1549. spin_lock_irq(&ctx->ctx_lock);
  1550. ret = -EAGAIN;
  1551. kiocb = lookup_kiocb(ctx, iocb, key);
  1552. if (kiocb && kiocb->ki_cancel) {
  1553. cancel = kiocb->ki_cancel;
  1554. kiocb->ki_users ++;
  1555. kiocbSetCancelled(kiocb);
  1556. } else
  1557. cancel = NULL;
  1558. spin_unlock_irq(&ctx->ctx_lock);
  1559. if (NULL != cancel) {
  1560. struct io_event tmp;
  1561. pr_debug("calling cancel\n");
  1562. memset(&tmp, 0, sizeof(tmp));
  1563. tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
  1564. tmp.data = kiocb->ki_user_data;
  1565. ret = cancel(kiocb, &tmp);
  1566. if (!ret) {
  1567. /* Cancellation succeeded -- copy the result
  1568. * into the user's buffer.
  1569. */
  1570. if (copy_to_user(result, &tmp, sizeof(tmp)))
  1571. ret = -EFAULT;
  1572. }
  1573. } else
  1574. ret = -EINVAL;
  1575. put_ioctx(ctx);
  1576. return ret;
  1577. }
  1578. /* io_getevents:
  1579. * Attempts to read at least min_nr events and up to nr events from
  1580. * the completion queue for the aio_context specified by ctx_id. If
  1581. * it succeeds, the number of read events is returned. May fail with
  1582. * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
  1583. * out of range, if timeout is out of range. May fail with -EFAULT
  1584. * if any of the memory specified is invalid. May return 0 or
  1585. * < min_nr if the timeout specified by timeout has elapsed
  1586. * before sufficient events are available, where timeout == NULL
  1587. * specifies an infinite timeout. Note that the timeout pointed to by
  1588. * timeout is relative and will be updated if not NULL and the
  1589. * operation blocks. Will fail with -ENOSYS if not implemented.
  1590. */
  1591. SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
  1592. long, min_nr,
  1593. long, nr,
  1594. struct io_event __user *, events,
  1595. struct timespec __user *, timeout)
  1596. {
  1597. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1598. long ret = -EINVAL;
  1599. if (likely(ioctx)) {
  1600. if (likely(min_nr <= nr && min_nr >= 0))
  1601. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1602. put_ioctx(ioctx);
  1603. }
  1604. asmlinkage_protect(5, ret, ctx_id, min_nr, nr, events, timeout);
  1605. return ret;
  1606. }