1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783 |
- #define pr_fmt(fmt) "%s: " fmt, __func__
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/errno.h>
- #include <linux/time.h>
- #include <linux/aio_abi.h>
- #include <linux/export.h>
- #include <linux/syscalls.h>
- #include <linux/backing-dev.h>
- #include <linux/uio.h>
- #include <linux/sched.h>
- #include <linux/fs.h>
- #include <linux/file.h>
- #include <linux/mm.h>
- #include <linux/mman.h>
- #include <linux/mmu_context.h>
- #include <linux/percpu.h>
- #include <linux/slab.h>
- #include <linux/timer.h>
- #include <linux/aio.h>
- #include <linux/highmem.h>
- #include <linux/workqueue.h>
- #include <linux/security.h>
- #include <linux/eventfd.h>
- #include <linux/blkdev.h>
- #include <linux/compat.h>
- #include <linux/migrate.h>
- #include <linux/ramfs.h>
- #include <linux/percpu-refcount.h>
- #include <linux/mount.h>
- #include <linux/nospec.h>
- #include <asm/kmap_types.h>
- #include <asm/uaccess.h>
- #include "internal.h"
- #define AIO_RING_MAGIC 0xa10a10a1
- #define AIO_RING_COMPAT_FEATURES 1
- #define AIO_RING_INCOMPAT_FEATURES 0
- struct aio_ring {
- unsigned id;
- unsigned nr;
- unsigned head;
- unsigned tail;
- unsigned magic;
- unsigned compat_features;
- unsigned incompat_features;
- unsigned header_length;
- struct io_event io_events[0];
- };
- #define AIO_RING_PAGES 8
- struct kioctx_table {
- struct rcu_head rcu;
- unsigned nr;
- struct kioctx __rcu *table[];
- };
- struct kioctx_cpu {
- unsigned reqs_available;
- };
- struct ctx_rq_wait {
- struct completion comp;
- atomic_t count;
- };
- struct kioctx {
- struct percpu_ref users;
- atomic_t dead;
- struct percpu_ref reqs;
- unsigned long user_id;
- struct __percpu kioctx_cpu *cpu;
-
- unsigned req_batch;
-
- unsigned max_reqs;
-
- unsigned nr_events;
- unsigned long mmap_base;
- unsigned long mmap_size;
- struct page **ring_pages;
- long nr_pages;
- struct rcu_head free_rcu;
- struct work_struct free_work;
-
- struct ctx_rq_wait *rq_wait;
- struct {
-
- atomic_t reqs_available;
- } ____cacheline_aligned_in_smp;
- struct {
- spinlock_t ctx_lock;
- struct list_head active_reqs;
- } ____cacheline_aligned_in_smp;
- struct {
- struct mutex ring_lock;
- wait_queue_head_t wait;
- } ____cacheline_aligned_in_smp;
- struct {
- unsigned tail;
- unsigned completed_events;
- spinlock_t completion_lock;
- } ____cacheline_aligned_in_smp;
- struct page *internal_pages[AIO_RING_PAGES];
- struct file *aio_ring_file;
- unsigned id;
- };
- #define KIOCB_CANCELLED ((void *) (~0ULL))
- struct aio_kiocb {
- struct kiocb common;
- struct kioctx *ki_ctx;
- kiocb_cancel_fn *ki_cancel;
- struct iocb __user *ki_user_iocb;
- __u64 ki_user_data;
- struct list_head ki_list;
-
- struct eventfd_ctx *ki_eventfd;
- };
- static DEFINE_SPINLOCK(aio_nr_lock);
- unsigned long aio_nr;
- unsigned long aio_max_nr = 0x10000;
- static struct kmem_cache *kiocb_cachep;
- static struct kmem_cache *kioctx_cachep;
- static struct vfsmount *aio_mnt;
- static const struct file_operations aio_ring_fops;
- static const struct address_space_operations aio_ctx_aops;
- static struct file *aio_private_file(struct kioctx *ctx, loff_t nr_pages)
- {
- struct qstr this = QSTR_INIT("[aio]", 5);
- struct file *file;
- struct path path;
- struct inode *inode = alloc_anon_inode(aio_mnt->mnt_sb);
- if (IS_ERR(inode))
- return ERR_CAST(inode);
- inode->i_mapping->a_ops = &aio_ctx_aops;
- inode->i_mapping->private_data = ctx;
- inode->i_size = PAGE_SIZE * nr_pages;
- path.dentry = d_alloc_pseudo(aio_mnt->mnt_sb, &this);
- if (!path.dentry) {
- iput(inode);
- return ERR_PTR(-ENOMEM);
- }
- path.mnt = mntget(aio_mnt);
- d_instantiate(path.dentry, inode);
- file = alloc_file(&path, FMODE_READ | FMODE_WRITE, &aio_ring_fops);
- if (IS_ERR(file)) {
- path_put(&path);
- return file;
- }
- file->f_flags = O_RDWR;
- return file;
- }
- static struct dentry *aio_mount(struct file_system_type *fs_type,
- int flags, const char *dev_name, void *data)
- {
- static const struct dentry_operations ops = {
- .d_dname = simple_dname,
- };
- struct dentry *root = mount_pseudo(fs_type, "aio:", NULL, &ops,
- AIO_RING_MAGIC);
- if (!IS_ERR(root))
- root->d_sb->s_iflags |= SB_I_NOEXEC;
- return root;
- }
- static int __init aio_setup(void)
- {
- static struct file_system_type aio_fs = {
- .name = "aio",
- .mount = aio_mount,
- .kill_sb = kill_anon_super,
- };
- aio_mnt = kern_mount(&aio_fs);
- if (IS_ERR(aio_mnt))
- panic("Failed to create aio fs mount.");
- kiocb_cachep = KMEM_CACHE(aio_kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
- kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
- pr_debug("sizeof(struct page) = %zu\n", sizeof(struct page));
- return 0;
- }
- __initcall(aio_setup);
- static void put_aio_ring_file(struct kioctx *ctx)
- {
- struct file *aio_ring_file = ctx->aio_ring_file;
- struct address_space *i_mapping;
- if (aio_ring_file) {
- truncate_setsize(aio_ring_file->f_inode, 0);
-
- i_mapping = aio_ring_file->f_inode->i_mapping;
- spin_lock(&i_mapping->private_lock);
- i_mapping->private_data = NULL;
- ctx->aio_ring_file = NULL;
- spin_unlock(&i_mapping->private_lock);
- fput(aio_ring_file);
- }
- }
- static void aio_free_ring(struct kioctx *ctx)
- {
- int i;
-
- put_aio_ring_file(ctx);
- for (i = 0; i < ctx->nr_pages; i++) {
- struct page *page;
- pr_debug("pid(%d) [%d] page->count=%d\n", current->pid, i,
- page_count(ctx->ring_pages[i]));
- page = ctx->ring_pages[i];
- if (!page)
- continue;
- ctx->ring_pages[i] = NULL;
- put_page(page);
- }
- if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages) {
- kfree(ctx->ring_pages);
- ctx->ring_pages = NULL;
- }
- }
- static int aio_ring_mremap(struct vm_area_struct *vma)
- {
- struct file *file = vma->vm_file;
- struct mm_struct *mm = vma->vm_mm;
- struct kioctx_table *table;
- int i, res = -EINVAL;
- spin_lock(&mm->ioctx_lock);
- rcu_read_lock();
- table = rcu_dereference(mm->ioctx_table);
- for (i = 0; i < table->nr; i++) {
- struct kioctx *ctx;
- ctx = rcu_dereference(table->table[i]);
- if (ctx && ctx->aio_ring_file == file) {
- if (!atomic_read(&ctx->dead)) {
- ctx->user_id = ctx->mmap_base = vma->vm_start;
- res = 0;
- }
- break;
- }
- }
- rcu_read_unlock();
- spin_unlock(&mm->ioctx_lock);
- return res;
- }
- static const struct vm_operations_struct aio_ring_vm_ops = {
- .mremap = aio_ring_mremap,
- #if IS_ENABLED(CONFIG_MMU)
- .fault = filemap_fault,
- .map_pages = filemap_map_pages,
- .page_mkwrite = filemap_page_mkwrite,
- #endif
- };
- static int aio_ring_mmap(struct file *file, struct vm_area_struct *vma)
- {
- vma->vm_flags |= VM_DONTEXPAND;
- vma->vm_ops = &aio_ring_vm_ops;
- return 0;
- }
- static const struct file_operations aio_ring_fops = {
- .mmap = aio_ring_mmap,
- };
- #if IS_ENABLED(CONFIG_MIGRATION)
- static int aio_migratepage(struct address_space *mapping, struct page *new,
- struct page *old, enum migrate_mode mode)
- {
- struct kioctx *ctx;
- unsigned long flags;
- pgoff_t idx;
- int rc;
- rc = 0;
-
- spin_lock(&mapping->private_lock);
- ctx = mapping->private_data;
- if (!ctx) {
- rc = -EINVAL;
- goto out;
- }
-
- if (!mutex_trylock(&ctx->ring_lock)) {
- rc = -EAGAIN;
- goto out;
- }
- idx = old->index;
- if (idx < (pgoff_t)ctx->nr_pages) {
-
- if (ctx->ring_pages[idx] != old)
- rc = -EAGAIN;
- } else
- rc = -EINVAL;
- if (rc != 0)
- goto out_unlock;
-
- BUG_ON(PageWriteback(old));
- get_page(new);
- rc = migrate_page_move_mapping(mapping, new, old, NULL, mode, 1);
- if (rc != MIGRATEPAGE_SUCCESS) {
- put_page(new);
- goto out_unlock;
- }
-
- spin_lock_irqsave(&ctx->completion_lock, flags);
- migrate_page_copy(new, old);
- BUG_ON(ctx->ring_pages[idx] != old);
- ctx->ring_pages[idx] = new;
- spin_unlock_irqrestore(&ctx->completion_lock, flags);
-
- put_page(old);
- out_unlock:
- mutex_unlock(&ctx->ring_lock);
- out:
- spin_unlock(&mapping->private_lock);
- return rc;
- }
- #endif
- static const struct address_space_operations aio_ctx_aops = {
- .set_page_dirty = __set_page_dirty_no_writeback,
- #if IS_ENABLED(CONFIG_MIGRATION)
- .migratepage = aio_migratepage,
- #endif
- };
- static int aio_setup_ring(struct kioctx *ctx)
- {
- struct aio_ring *ring;
- unsigned nr_events = ctx->max_reqs;
- struct mm_struct *mm = current->mm;
- unsigned long size, unused;
- int nr_pages;
- int i;
- struct file *file;
-
- nr_events += 2;
- size = sizeof(struct aio_ring);
- size += sizeof(struct io_event) * nr_events;
- nr_pages = PFN_UP(size);
- if (nr_pages < 0)
- return -EINVAL;
- file = aio_private_file(ctx, nr_pages);
- if (IS_ERR(file)) {
- ctx->aio_ring_file = NULL;
- return -ENOMEM;
- }
- ctx->aio_ring_file = file;
- nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring))
- / sizeof(struct io_event);
- ctx->ring_pages = ctx->internal_pages;
- if (nr_pages > AIO_RING_PAGES) {
- ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
- GFP_KERNEL);
- if (!ctx->ring_pages) {
- put_aio_ring_file(ctx);
- return -ENOMEM;
- }
- }
- for (i = 0; i < nr_pages; i++) {
- struct page *page;
- page = find_or_create_page(file->f_inode->i_mapping,
- i, GFP_HIGHUSER | __GFP_ZERO);
- if (!page)
- break;
- pr_debug("pid(%d) page[%d]->count=%d\n",
- current->pid, i, page_count(page));
- SetPageUptodate(page);
- unlock_page(page);
- ctx->ring_pages[i] = page;
- }
- ctx->nr_pages = i;
- if (unlikely(i != nr_pages)) {
- aio_free_ring(ctx);
- return -ENOMEM;
- }
- ctx->mmap_size = nr_pages * PAGE_SIZE;
- pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
- if (down_write_killable(&mm->mmap_sem)) {
- ctx->mmap_size = 0;
- aio_free_ring(ctx);
- return -EINTR;
- }
- ctx->mmap_base = do_mmap_pgoff(ctx->aio_ring_file, 0, ctx->mmap_size,
- PROT_READ | PROT_WRITE,
- MAP_SHARED, 0, &unused);
- up_write(&mm->mmap_sem);
- if (IS_ERR((void *)ctx->mmap_base)) {
- ctx->mmap_size = 0;
- aio_free_ring(ctx);
- return -ENOMEM;
- }
- pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
- ctx->user_id = ctx->mmap_base;
- ctx->nr_events = nr_events;
- ring = kmap_atomic(ctx->ring_pages[0]);
- ring->nr = nr_events;
- ring->id = ~0U;
- ring->head = ring->tail = 0;
- ring->magic = AIO_RING_MAGIC;
- ring->compat_features = AIO_RING_COMPAT_FEATURES;
- ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
- ring->header_length = sizeof(struct aio_ring);
- kunmap_atomic(ring);
- flush_dcache_page(ctx->ring_pages[0]);
- return 0;
- }
- #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
- #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
- #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
- void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
- {
- struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, common);
- struct kioctx *ctx = req->ki_ctx;
- unsigned long flags;
- spin_lock_irqsave(&ctx->ctx_lock, flags);
- if (!req->ki_list.next)
- list_add(&req->ki_list, &ctx->active_reqs);
- req->ki_cancel = cancel;
- spin_unlock_irqrestore(&ctx->ctx_lock, flags);
- }
- EXPORT_SYMBOL(kiocb_set_cancel_fn);
- static int kiocb_cancel(struct aio_kiocb *kiocb)
- {
- kiocb_cancel_fn *old, *cancel;
-
- cancel = ACCESS_ONCE(kiocb->ki_cancel);
- do {
- if (!cancel || cancel == KIOCB_CANCELLED)
- return -EINVAL;
- old = cancel;
- cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
- } while (cancel != old);
- return cancel(&kiocb->common);
- }
- static void free_ioctx(struct work_struct *work)
- {
- struct kioctx *ctx = container_of(work, struct kioctx, free_work);
- pr_debug("freeing %p\n", ctx);
- aio_free_ring(ctx);
- free_percpu(ctx->cpu);
- percpu_ref_exit(&ctx->reqs);
- percpu_ref_exit(&ctx->users);
- kmem_cache_free(kioctx_cachep, ctx);
- }
- static void free_ioctx_rcufn(struct rcu_head *head)
- {
- struct kioctx *ctx = container_of(head, struct kioctx, free_rcu);
- INIT_WORK(&ctx->free_work, free_ioctx);
- schedule_work(&ctx->free_work);
- }
- static void free_ioctx_reqs(struct percpu_ref *ref)
- {
- struct kioctx *ctx = container_of(ref, struct kioctx, reqs);
-
- if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
- complete(&ctx->rq_wait->comp);
-
- call_rcu(&ctx->free_rcu, free_ioctx_rcufn);
- }
- static void free_ioctx_users(struct percpu_ref *ref)
- {
- struct kioctx *ctx = container_of(ref, struct kioctx, users);
- struct aio_kiocb *req;
- spin_lock_irq(&ctx->ctx_lock);
- while (!list_empty(&ctx->active_reqs)) {
- req = list_first_entry(&ctx->active_reqs,
- struct aio_kiocb, ki_list);
- kiocb_cancel(req);
- list_del_init(&req->ki_list);
- }
- spin_unlock_irq(&ctx->ctx_lock);
- percpu_ref_kill(&ctx->reqs);
- percpu_ref_put(&ctx->reqs);
- }
- static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
- {
- unsigned i, new_nr;
- struct kioctx_table *table, *old;
- struct aio_ring *ring;
- spin_lock(&mm->ioctx_lock);
- table = rcu_dereference_raw(mm->ioctx_table);
- while (1) {
- if (table)
- for (i = 0; i < table->nr; i++)
- if (!rcu_access_pointer(table->table[i])) {
- ctx->id = i;
- rcu_assign_pointer(table->table[i], ctx);
- spin_unlock(&mm->ioctx_lock);
-
- ring = kmap_atomic(ctx->ring_pages[0]);
- ring->id = ctx->id;
- kunmap_atomic(ring);
- return 0;
- }
- new_nr = (table ? table->nr : 1) * 4;
- spin_unlock(&mm->ioctx_lock);
- table = kzalloc(sizeof(*table) + sizeof(struct kioctx *) *
- new_nr, GFP_KERNEL);
- if (!table)
- return -ENOMEM;
- table->nr = new_nr;
- spin_lock(&mm->ioctx_lock);
- old = rcu_dereference_raw(mm->ioctx_table);
- if (!old) {
- rcu_assign_pointer(mm->ioctx_table, table);
- } else if (table->nr > old->nr) {
- memcpy(table->table, old->table,
- old->nr * sizeof(struct kioctx *));
- rcu_assign_pointer(mm->ioctx_table, table);
- kfree_rcu(old, rcu);
- } else {
- kfree(table);
- table = old;
- }
- }
- }
- static void aio_nr_sub(unsigned nr)
- {
- spin_lock(&aio_nr_lock);
- if (WARN_ON(aio_nr - nr > aio_nr))
- aio_nr = 0;
- else
- aio_nr -= nr;
- spin_unlock(&aio_nr_lock);
- }
- static struct kioctx *ioctx_alloc(unsigned nr_events)
- {
- struct mm_struct *mm = current->mm;
- struct kioctx *ctx;
- int err = -ENOMEM;
-
- nr_events = max(nr_events, num_possible_cpus() * 4);
- nr_events *= 2;
-
- if (nr_events > (0x10000000U / sizeof(struct io_event))) {
- pr_debug("ENOMEM: nr_events too high\n");
- return ERR_PTR(-EINVAL);
- }
- if (!nr_events || (unsigned long)nr_events > (aio_max_nr * 2UL))
- return ERR_PTR(-EAGAIN);
- ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
- if (!ctx)
- return ERR_PTR(-ENOMEM);
- ctx->max_reqs = nr_events;
- spin_lock_init(&ctx->ctx_lock);
- spin_lock_init(&ctx->completion_lock);
- mutex_init(&ctx->ring_lock);
-
- mutex_lock(&ctx->ring_lock);
- init_waitqueue_head(&ctx->wait);
- INIT_LIST_HEAD(&ctx->active_reqs);
- if (percpu_ref_init(&ctx->users, free_ioctx_users, 0, GFP_KERNEL))
- goto err;
- if (percpu_ref_init(&ctx->reqs, free_ioctx_reqs, 0, GFP_KERNEL))
- goto err;
- ctx->cpu = alloc_percpu(struct kioctx_cpu);
- if (!ctx->cpu)
- goto err;
- err = aio_setup_ring(ctx);
- if (err < 0)
- goto err;
- atomic_set(&ctx->reqs_available, ctx->nr_events - 1);
- ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4);
- if (ctx->req_batch < 1)
- ctx->req_batch = 1;
-
- spin_lock(&aio_nr_lock);
- if (aio_nr + nr_events > (aio_max_nr * 2UL) ||
- aio_nr + nr_events < aio_nr) {
- spin_unlock(&aio_nr_lock);
- err = -EAGAIN;
- goto err_ctx;
- }
- aio_nr += ctx->max_reqs;
- spin_unlock(&aio_nr_lock);
- percpu_ref_get(&ctx->users);
- percpu_ref_get(&ctx->reqs);
- err = ioctx_add_table(ctx, mm);
- if (err)
- goto err_cleanup;
-
- mutex_unlock(&ctx->ring_lock);
- pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
- ctx, ctx->user_id, mm, ctx->nr_events);
- return ctx;
- err_cleanup:
- aio_nr_sub(ctx->max_reqs);
- err_ctx:
- atomic_set(&ctx->dead, 1);
- if (ctx->mmap_size)
- vm_munmap(ctx->mmap_base, ctx->mmap_size);
- aio_free_ring(ctx);
- err:
- mutex_unlock(&ctx->ring_lock);
- free_percpu(ctx->cpu);
- percpu_ref_exit(&ctx->reqs);
- percpu_ref_exit(&ctx->users);
- kmem_cache_free(kioctx_cachep, ctx);
- pr_debug("error allocating ioctx %d\n", err);
- return ERR_PTR(err);
- }
- static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx,
- struct ctx_rq_wait *wait)
- {
- struct kioctx_table *table;
- spin_lock(&mm->ioctx_lock);
- if (atomic_xchg(&ctx->dead, 1)) {
- spin_unlock(&mm->ioctx_lock);
- return -EINVAL;
- }
- table = rcu_dereference_raw(mm->ioctx_table);
- WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id]));
- RCU_INIT_POINTER(table->table[ctx->id], NULL);
- spin_unlock(&mm->ioctx_lock);
-
- wake_up_all(&ctx->wait);
-
- aio_nr_sub(ctx->max_reqs);
- if (ctx->mmap_size)
- vm_munmap(ctx->mmap_base, ctx->mmap_size);
- ctx->rq_wait = wait;
- percpu_ref_kill(&ctx->users);
- return 0;
- }
- void exit_aio(struct mm_struct *mm)
- {
- struct kioctx_table *table = rcu_dereference_raw(mm->ioctx_table);
- struct ctx_rq_wait wait;
- int i, skipped;
- if (!table)
- return;
- atomic_set(&wait.count, table->nr);
- init_completion(&wait.comp);
- skipped = 0;
- for (i = 0; i < table->nr; ++i) {
- struct kioctx *ctx =
- rcu_dereference_protected(table->table[i], true);
- if (!ctx) {
- skipped++;
- continue;
- }
-
- ctx->mmap_size = 0;
- kill_ioctx(mm, ctx, &wait);
- }
- if (!atomic_sub_and_test(skipped, &wait.count)) {
-
- wait_for_completion(&wait.comp);
- }
- RCU_INIT_POINTER(mm->ioctx_table, NULL);
- kfree(table);
- }
- static void put_reqs_available(struct kioctx *ctx, unsigned nr)
- {
- struct kioctx_cpu *kcpu;
- unsigned long flags;
- local_irq_save(flags);
- kcpu = this_cpu_ptr(ctx->cpu);
- kcpu->reqs_available += nr;
- while (kcpu->reqs_available >= ctx->req_batch * 2) {
- kcpu->reqs_available -= ctx->req_batch;
- atomic_add(ctx->req_batch, &ctx->reqs_available);
- }
- local_irq_restore(flags);
- }
- static bool get_reqs_available(struct kioctx *ctx)
- {
- struct kioctx_cpu *kcpu;
- bool ret = false;
- unsigned long flags;
- local_irq_save(flags);
- kcpu = this_cpu_ptr(ctx->cpu);
- if (!kcpu->reqs_available) {
- int old, avail = atomic_read(&ctx->reqs_available);
- do {
- if (avail < ctx->req_batch)
- goto out;
- old = avail;
- avail = atomic_cmpxchg(&ctx->reqs_available,
- avail, avail - ctx->req_batch);
- } while (avail != old);
- kcpu->reqs_available += ctx->req_batch;
- }
- ret = true;
- kcpu->reqs_available--;
- out:
- local_irq_restore(flags);
- return ret;
- }
- static void refill_reqs_available(struct kioctx *ctx, unsigned head,
- unsigned tail)
- {
- unsigned events_in_ring, completed;
-
- head %= ctx->nr_events;
- if (head <= tail)
- events_in_ring = tail - head;
- else
- events_in_ring = ctx->nr_events - (head - tail);
- completed = ctx->completed_events;
- if (events_in_ring < completed)
- completed -= events_in_ring;
- else
- completed = 0;
- if (!completed)
- return;
- ctx->completed_events -= completed;
- put_reqs_available(ctx, completed);
- }
- static void user_refill_reqs_available(struct kioctx *ctx)
- {
- spin_lock_irq(&ctx->completion_lock);
- if (ctx->completed_events) {
- struct aio_ring *ring;
- unsigned head;
-
- ring = kmap_atomic(ctx->ring_pages[0]);
- head = ring->head;
- kunmap_atomic(ring);
- refill_reqs_available(ctx, head, ctx->tail);
- }
- spin_unlock_irq(&ctx->completion_lock);
- }
- static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
- {
- struct aio_kiocb *req;
- if (!get_reqs_available(ctx)) {
- user_refill_reqs_available(ctx);
- if (!get_reqs_available(ctx))
- return NULL;
- }
- req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
- if (unlikely(!req))
- goto out_put;
- percpu_ref_get(&ctx->reqs);
- req->ki_ctx = ctx;
- return req;
- out_put:
- put_reqs_available(ctx, 1);
- return NULL;
- }
- static void kiocb_free(struct aio_kiocb *req)
- {
- if (req->common.ki_filp)
- fput(req->common.ki_filp);
- if (req->ki_eventfd != NULL)
- eventfd_ctx_put(req->ki_eventfd);
- kmem_cache_free(kiocb_cachep, req);
- }
- static struct kioctx *lookup_ioctx(unsigned long ctx_id)
- {
- struct aio_ring __user *ring = (void __user *)ctx_id;
- struct mm_struct *mm = current->mm;
- struct kioctx *ctx, *ret = NULL;
- struct kioctx_table *table;
- unsigned id;
- if (get_user(id, &ring->id))
- return NULL;
- rcu_read_lock();
- table = rcu_dereference(mm->ioctx_table);
- if (!table || id >= table->nr)
- goto out;
- id = array_index_nospec(id, table->nr);
- ctx = rcu_dereference(table->table[id]);
- if (ctx && ctx->user_id == ctx_id) {
- if (percpu_ref_tryget_live(&ctx->users))
- ret = ctx;
- }
- out:
- rcu_read_unlock();
- return ret;
- }
- static void aio_complete(struct kiocb *kiocb, long res, long res2)
- {
- struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, common);
- struct kioctx *ctx = iocb->ki_ctx;
- struct aio_ring *ring;
- struct io_event *ev_page, *event;
- unsigned tail, pos, head;
- unsigned long flags;
- if (kiocb->ki_flags & IOCB_WRITE) {
- struct file *file = kiocb->ki_filp;
-
- if (S_ISREG(file_inode(file)->i_mode))
- __sb_writers_acquired(file_inode(file)->i_sb, SB_FREEZE_WRITE);
- file_end_write(file);
- }
-
- BUG_ON(is_sync_kiocb(kiocb));
- if (iocb->ki_list.next) {
- unsigned long flags;
- spin_lock_irqsave(&ctx->ctx_lock, flags);
- list_del(&iocb->ki_list);
- spin_unlock_irqrestore(&ctx->ctx_lock, flags);
- }
-
- spin_lock_irqsave(&ctx->completion_lock, flags);
- tail = ctx->tail;
- pos = tail + AIO_EVENTS_OFFSET;
- if (++tail >= ctx->nr_events)
- tail = 0;
- ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
- event = ev_page + pos % AIO_EVENTS_PER_PAGE;
- event->obj = (u64)(unsigned long)iocb->ki_user_iocb;
- event->data = iocb->ki_user_data;
- event->res = res;
- event->res2 = res2;
- kunmap_atomic(ev_page);
- flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
- pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
- ctx, tail, iocb, iocb->ki_user_iocb, iocb->ki_user_data,
- res, res2);
-
- smp_wmb();
- ctx->tail = tail;
- ring = kmap_atomic(ctx->ring_pages[0]);
- head = ring->head;
- ring->tail = tail;
- kunmap_atomic(ring);
- flush_dcache_page(ctx->ring_pages[0]);
- ctx->completed_events++;
- if (ctx->completed_events > 1)
- refill_reqs_available(ctx, head, tail);
- spin_unlock_irqrestore(&ctx->completion_lock, flags);
- pr_debug("added to ring %p at [%u]\n", iocb, tail);
-
- if (iocb->ki_eventfd != NULL)
- eventfd_signal(iocb->ki_eventfd, 1);
-
- kiocb_free(iocb);
-
- smp_mb();
- if (waitqueue_active(&ctx->wait))
- wake_up(&ctx->wait);
- percpu_ref_put(&ctx->reqs);
- }
- static long aio_read_events_ring(struct kioctx *ctx,
- struct io_event __user *event, long nr)
- {
- struct aio_ring *ring;
- unsigned head, tail, pos;
- long ret = 0;
- int copy_ret;
-
- sched_annotate_sleep();
- mutex_lock(&ctx->ring_lock);
-
- ring = kmap_atomic(ctx->ring_pages[0]);
- head = ring->head;
- tail = ring->tail;
- kunmap_atomic(ring);
-
- smp_rmb();
- pr_debug("h%u t%u m%u\n", head, tail, ctx->nr_events);
- if (head == tail)
- goto out;
- head %= ctx->nr_events;
- tail %= ctx->nr_events;
- while (ret < nr) {
- long avail;
- struct io_event *ev;
- struct page *page;
- avail = (head <= tail ? tail : ctx->nr_events) - head;
- if (head == tail)
- break;
- avail = min(avail, nr - ret);
- avail = min_t(long, avail, AIO_EVENTS_PER_PAGE -
- ((head + AIO_EVENTS_OFFSET) % AIO_EVENTS_PER_PAGE));
- pos = head + AIO_EVENTS_OFFSET;
- page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
- pos %= AIO_EVENTS_PER_PAGE;
- ev = kmap(page);
- copy_ret = copy_to_user(event + ret, ev + pos,
- sizeof(*ev) * avail);
- kunmap(page);
- if (unlikely(copy_ret)) {
- ret = -EFAULT;
- goto out;
- }
- ret += avail;
- head += avail;
- head %= ctx->nr_events;
- }
- ring = kmap_atomic(ctx->ring_pages[0]);
- ring->head = head;
- kunmap_atomic(ring);
- flush_dcache_page(ctx->ring_pages[0]);
- pr_debug("%li h%u t%u\n", ret, head, tail);
- out:
- mutex_unlock(&ctx->ring_lock);
- return ret;
- }
- static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
- struct io_event __user *event, long *i)
- {
- long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
- if (ret > 0)
- *i += ret;
- if (unlikely(atomic_read(&ctx->dead)))
- ret = -EINVAL;
- if (!*i)
- *i = ret;
- return ret < 0 || *i >= min_nr;
- }
- static long read_events(struct kioctx *ctx, long min_nr, long nr,
- struct io_event __user *event,
- struct timespec __user *timeout)
- {
- ktime_t until = { .tv64 = KTIME_MAX };
- long ret = 0;
- if (timeout) {
- struct timespec ts;
- if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
- return -EFAULT;
- until = timespec_to_ktime(ts);
- }
-
- if (until.tv64 == 0)
- aio_read_events(ctx, min_nr, nr, event, &ret);
- else
- wait_event_interruptible_hrtimeout(ctx->wait,
- aio_read_events(ctx, min_nr, nr, event, &ret),
- until);
- if (!ret && signal_pending(current))
- ret = -EINTR;
- return ret;
- }
- SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
- {
- struct kioctx *ioctx = NULL;
- unsigned long ctx;
- long ret;
- ret = get_user(ctx, ctxp);
- if (unlikely(ret))
- goto out;
- ret = -EINVAL;
- if (unlikely(ctx || nr_events == 0)) {
- pr_debug("EINVAL: ctx %lu nr_events %u\n",
- ctx, nr_events);
- goto out;
- }
- ioctx = ioctx_alloc(nr_events);
- ret = PTR_ERR(ioctx);
- if (!IS_ERR(ioctx)) {
- ret = put_user(ioctx->user_id, ctxp);
- if (ret)
- kill_ioctx(current->mm, ioctx, NULL);
- percpu_ref_put(&ioctx->users);
- }
- out:
- return ret;
- }
- SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
- {
- struct kioctx *ioctx = lookup_ioctx(ctx);
- if (likely(NULL != ioctx)) {
- struct ctx_rq_wait wait;
- int ret;
- init_completion(&wait.comp);
- atomic_set(&wait.count, 1);
-
- ret = kill_ioctx(current->mm, ioctx, &wait);
- percpu_ref_put(&ioctx->users);
-
- if (!ret)
- wait_for_completion(&wait.comp);
- return ret;
- }
- pr_debug("EINVAL: invalid context id\n");
- return -EINVAL;
- }
- static int aio_setup_rw(int rw, struct iocb *iocb, struct iovec **iovec,
- bool vectored, bool compat, struct iov_iter *iter)
- {
- void __user *buf = (void __user *)(uintptr_t)iocb->aio_buf;
- size_t len = iocb->aio_nbytes;
- if (!vectored) {
- ssize_t ret = import_single_range(rw, buf, len, *iovec, iter);
- *iovec = NULL;
- return ret;
- }
- #ifdef CONFIG_COMPAT
- if (compat)
- return compat_import_iovec(rw, buf, len, UIO_FASTIOV, iovec,
- iter);
- #endif
- return import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter);
- }
- static inline ssize_t aio_ret(struct kiocb *req, ssize_t ret)
- {
- switch (ret) {
- case -EIOCBQUEUED:
- return ret;
- case -ERESTARTSYS:
- case -ERESTARTNOINTR:
- case -ERESTARTNOHAND:
- case -ERESTART_RESTARTBLOCK:
-
- ret = -EINTR;
-
- default:
- aio_complete(req, ret, 0);
- return 0;
- }
- }
- static ssize_t aio_read(struct kiocb *req, struct iocb *iocb, bool vectored,
- bool compat)
- {
- struct file *file = req->ki_filp;
- struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
- struct iov_iter iter;
- ssize_t ret;
- if (unlikely(!(file->f_mode & FMODE_READ)))
- return -EBADF;
- if (unlikely(!file->f_op->read_iter))
- return -EINVAL;
- ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
- if (ret)
- return ret;
- ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
- if (!ret)
- ret = aio_ret(req, file->f_op->read_iter(req, &iter));
- kfree(iovec);
- return ret;
- }
- static ssize_t aio_write(struct kiocb *req, struct iocb *iocb, bool vectored,
- bool compat)
- {
- struct file *file = req->ki_filp;
- struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
- struct iov_iter iter;
- ssize_t ret;
- if (unlikely(!(file->f_mode & FMODE_WRITE)))
- return -EBADF;
- if (unlikely(!file->f_op->write_iter))
- return -EINVAL;
- ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
- if (ret)
- return ret;
- ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter));
- if (!ret) {
- req->ki_flags |= IOCB_WRITE;
- file_start_write(file);
- ret = aio_ret(req, file->f_op->write_iter(req, &iter));
-
- if (S_ISREG(file_inode(file)->i_mode))
- __sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
- }
- kfree(iovec);
- return ret;
- }
- static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
- struct iocb *iocb, bool compat)
- {
- struct aio_kiocb *req;
- struct file *file;
- ssize_t ret;
-
- if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
- pr_debug("EINVAL: reserve field set\n");
- return -EINVAL;
- }
-
- if (unlikely(
- (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
- (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
- ((ssize_t)iocb->aio_nbytes < 0)
- )) {
- pr_debug("EINVAL: overflow check\n");
- return -EINVAL;
- }
- req = aio_get_req(ctx);
- if (unlikely(!req))
- return -EAGAIN;
- req->common.ki_filp = file = fget(iocb->aio_fildes);
- if (unlikely(!req->common.ki_filp)) {
- ret = -EBADF;
- goto out_put_req;
- }
- req->common.ki_pos = iocb->aio_offset;
- req->common.ki_complete = aio_complete;
- req->common.ki_flags = iocb_flags(req->common.ki_filp);
- if (iocb->aio_flags & IOCB_FLAG_RESFD) {
-
- req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
- if (IS_ERR(req->ki_eventfd)) {
- ret = PTR_ERR(req->ki_eventfd);
- req->ki_eventfd = NULL;
- goto out_put_req;
- }
- req->common.ki_flags |= IOCB_EVENTFD;
- }
- ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
- if (unlikely(ret)) {
- pr_debug("EFAULT: aio_key\n");
- goto out_put_req;
- }
- req->ki_user_iocb = user_iocb;
- req->ki_user_data = iocb->aio_data;
- get_file(file);
- switch (iocb->aio_lio_opcode) {
- case IOCB_CMD_PREAD:
- ret = aio_read(&req->common, iocb, false, compat);
- break;
- case IOCB_CMD_PWRITE:
- ret = aio_write(&req->common, iocb, false, compat);
- break;
- case IOCB_CMD_PREADV:
- ret = aio_read(&req->common, iocb, true, compat);
- break;
- case IOCB_CMD_PWRITEV:
- ret = aio_write(&req->common, iocb, true, compat);
- break;
- default:
- pr_debug("invalid aio operation %d\n", iocb->aio_lio_opcode);
- ret = -EINVAL;
- break;
- }
- fput(file);
- if (ret && ret != -EIOCBQUEUED)
- goto out_put_req;
- return 0;
- out_put_req:
- put_reqs_available(ctx, 1);
- percpu_ref_put(&ctx->reqs);
- kiocb_free(req);
- return ret;
- }
- long do_io_submit(aio_context_t ctx_id, long nr,
- struct iocb __user *__user *iocbpp, bool compat)
- {
- struct kioctx *ctx;
- long ret = 0;
- int i = 0;
- struct blk_plug plug;
- if (unlikely(nr < 0))
- return -EINVAL;
- if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
- nr = LONG_MAX/sizeof(*iocbpp);
- if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
- return -EFAULT;
- ctx = lookup_ioctx(ctx_id);
- if (unlikely(!ctx)) {
- pr_debug("EINVAL: invalid context id\n");
- return -EINVAL;
- }
- blk_start_plug(&plug);
-
- for (i=0; i<nr; i++) {
- struct iocb __user *user_iocb;
- struct iocb tmp;
- if (unlikely(__get_user(user_iocb, iocbpp + i))) {
- ret = -EFAULT;
- break;
- }
- if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
- ret = -EFAULT;
- break;
- }
- ret = io_submit_one(ctx, user_iocb, &tmp, compat);
- if (ret)
- break;
- }
- blk_finish_plug(&plug);
- percpu_ref_put(&ctx->users);
- return i ? i : ret;
- }
- SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
- struct iocb __user * __user *, iocbpp)
- {
- return do_io_submit(ctx_id, nr, iocbpp, 0);
- }
- static struct aio_kiocb *
- lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb, u32 key)
- {
- struct aio_kiocb *kiocb;
- assert_spin_locked(&ctx->ctx_lock);
- if (key != KIOCB_KEY)
- return NULL;
-
- list_for_each_entry(kiocb, &ctx->active_reqs, ki_list) {
- if (kiocb->ki_user_iocb == iocb)
- return kiocb;
- }
- return NULL;
- }
- SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
- struct io_event __user *, result)
- {
- struct kioctx *ctx;
- struct aio_kiocb *kiocb;
- u32 key;
- int ret;
- ret = get_user(key, &iocb->aio_key);
- if (unlikely(ret))
- return -EFAULT;
- ctx = lookup_ioctx(ctx_id);
- if (unlikely(!ctx))
- return -EINVAL;
- spin_lock_irq(&ctx->ctx_lock);
- kiocb = lookup_kiocb(ctx, iocb, key);
- if (kiocb)
- ret = kiocb_cancel(kiocb);
- else
- ret = -EINVAL;
- spin_unlock_irq(&ctx->ctx_lock);
- if (!ret) {
-
- ret = -EINPROGRESS;
- }
- percpu_ref_put(&ctx->users);
- return ret;
- }
- SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
- long, min_nr,
- long, nr,
- struct io_event __user *, events,
- struct timespec __user *, timeout)
- {
- struct kioctx *ioctx = lookup_ioctx(ctx_id);
- long ret = -EINVAL;
- if (likely(ioctx)) {
- if (likely(min_nr <= nr && min_nr >= 0))
- ret = read_events(ioctx, min_nr, nr, events, timeout);
- percpu_ref_put(&ioctx->users);
- }
- return ret;
- }
|