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