^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * fs/userfaultfd.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2008-2009 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2015 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Some part derived from fs/eventfd.c (anon inode setup) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * mm/ksm.c (mm hashing).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/hashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mmu_notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/anon_inodes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/userfaultfd_k.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/mempolicy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/hugetlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int sysctl_unprivileged_userfaultfd __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * Start with fault_pending_wqh and fault_wqh so they're more likely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * to be in the same cacheline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Locking order:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * fd_wqh.lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * fault_pending_wqh.lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * fault_wqh.lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * event_wqh.lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * To avoid deadlocks, IRQs must be disabled when taking any of the above locks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * since fd_wqh.lock is taken by aio_poll() while it's holding a lock that's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * also taken in IRQ context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct userfaultfd_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* waitqueue head for the pending (i.e. not read) userfaults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) wait_queue_head_t fault_pending_wqh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* waitqueue head for the userfaults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) wait_queue_head_t fault_wqh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* waitqueue head for the pseudo fd to wakeup poll/read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) wait_queue_head_t fd_wqh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* waitqueue head for events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) wait_queue_head_t event_wqh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* a refile sequence protected by fault_pending_wqh lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) seqcount_spinlock_t refile_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* pseudo fd refcounting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) refcount_t refcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* userfaultfd syscall flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* features requested from the userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned int features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* released */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) bool released;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* memory mappings are changing because of non-cooperative event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) bool mmap_changing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* mm with one ore more vmas attached to this userfaultfd_ctx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct mm_struct *mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct userfaultfd_fork_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct userfaultfd_ctx *orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct userfaultfd_ctx *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct userfaultfd_unmap_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct userfaultfd_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct userfaultfd_wait_queue {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct uffd_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) wait_queue_entry_t wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct userfaultfd_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) bool waken;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct userfaultfd_wake_range {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) unsigned long len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* internal indication that UFFD_API ioctl was successfully executed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define UFFD_FEATURE_INITIALIZED (1u << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static bool userfaultfd_is_initialized(struct userfaultfd_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return ctx->features & UFFD_FEATURE_INITIALIZED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int wake_flags, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct userfaultfd_wake_range *range = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct userfaultfd_wait_queue *uwq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned long start, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* len == 0 means wake all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) start = range->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) len = range->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (len && (start > uwq->msg.arg.pagefault.address ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) start + len <= uwq->msg.arg.pagefault.address))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) WRITE_ONCE(uwq->waken, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * The Program-Order guarantees provided by the scheduler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * ensure uwq->waken is visible before the task is woken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ret = wake_up_state(wq->private, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Wake only once, autoremove behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * After the effect of list_del_init is visible to the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * CPUs, the waitqueue may disappear from under us, see the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * !list_empty_careful() in handle_userfault().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * try_to_wake_up() has an implicit smp_mb(), and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * wq->private is read before calling the extern function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * "wake_up_state" (which in turns calls try_to_wake_up).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) list_del_init(&wq->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * @ctx: [in] Pointer to the userfaultfd context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) refcount_inc(&ctx->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @ctx: [in] Pointer to userfaultfd context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * The userfaultfd context reference must have been previously acquired either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (refcount_dec_and_test(&ctx->refcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) mmdrop(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) kmem_cache_free(userfaultfd_ctx_cachep, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static inline void msg_init(struct uffd_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * Must use memset to zero out the paddings or kernel data is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * leaked to userland.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) memset(msg, 0, sizeof(struct uffd_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static inline struct uffd_msg userfault_msg(unsigned long address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) unsigned long reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) unsigned int features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct uffd_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) msg_init(&msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) msg.event = UFFD_EVENT_PAGEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) msg.arg.pagefault.address = address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * These flags indicate why the userfault occurred:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * - UFFD_PAGEFAULT_FLAG_WP indicates a write protect fault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * - UFFD_PAGEFAULT_FLAG_MINOR indicates a minor fault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * - Neither of these flags being set indicates a MISSING fault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * Separately, UFFD_PAGEFAULT_FLAG_WRITE indicates it was a write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * fault. Otherwise, it was a read fault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (flags & FAULT_FLAG_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (reason & VM_UFFD_WP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (reason & VM_UFFD_MINOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (features & UFFD_FEATURE_THREAD_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) msg.arg.pagefault.feat.ptid = task_pid_vnr(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) #ifdef CONFIG_HUGETLB_PAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * Same functionality as userfaultfd_must_wait below with modifications for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * hugepmd ranges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) unsigned long address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) unsigned long flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) unsigned long reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct mm_struct *mm = ctx->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) pte_t *ptep, pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) bool ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) mmap_assert_locked(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ptep = huge_pte_offset(mm, address, vma_mmu_pagesize(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!ptep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) pte = huge_ptep_get(ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * Lockless access: we're in a wait_event so it's ok if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * changes under us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (huge_pte_none(pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (!huge_pte_write(pte) && (reason & VM_UFFD_WP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) unsigned long address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) unsigned long flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) unsigned long reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return false; /* should never get here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) #endif /* CONFIG_HUGETLB_PAGE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * Verify the pagetables are still not ok after having reigstered into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * userfault that has already been resolved, if userfaultfd_read and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * threads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) unsigned long address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) unsigned long flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) unsigned long reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct mm_struct *mm = ctx->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) pgd_t *pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) pmd_t *pmd, _pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) bool ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) mmap_assert_locked(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) pgd = pgd_offset(mm, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (!pgd_present(*pgd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) p4d = p4d_offset(pgd, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (!p4d_present(*p4d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) pud = pud_offset(p4d, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!pud_present(*pud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) pmd = pmd_offset(pud, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * READ_ONCE must function as a barrier with narrower scope
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * and it must be equivalent to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * _pmd = *pmd; barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * This is to deal with the instability (as in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * pmd_trans_unstable) of the pmd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) _pmd = READ_ONCE(*pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (pmd_none(_pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (!pmd_present(_pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (pmd_trans_huge(_pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (!pmd_write(_pmd) && (reason & VM_UFFD_WP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * and use the standard pte_offset_map() instead of parsing _pmd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) pte = pte_offset_map(pmd, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * Lockless access: we're in a wait_event so it's ok if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * changes under us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (pte_none(*pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (!pte_write(*pte) && (reason & VM_UFFD_WP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) pte_unmap(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static inline long userfaultfd_get_blocking_state(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (flags & FAULT_FLAG_INTERRUPTIBLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return TASK_INTERRUPTIBLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (flags & FAULT_FLAG_KILLABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return TASK_KILLABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return TASK_UNINTERRUPTIBLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * The locking rules involved in returning VM_FAULT_RETRY depending on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * recommendation in __lock_page_or_retry is not an understatement.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_lock must be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * set, VM_FAULT_RETRY can still be returned if and only if there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * fatal_signal_pending()s, and the mmap_lock must be released before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * returning it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct mm_struct *mm = vmf->vma->vm_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct userfaultfd_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct userfaultfd_wait_queue uwq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) vm_fault_t ret = VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) bool must_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) long blocking_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * We don't do userfault handling for the final child pid update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * We also don't do userfault handling during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * coredumping. hugetlbfs has the special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * follow_hugetlb_page() to skip missing pages in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * the no_page_table() helper in follow_page_mask(), but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * shmem_vm_ops->fault method is invoked even during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * coredumping without mmap_lock and it ends up here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (current->flags & (PF_EXITING|PF_DUMPCORE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * Coredumping runs without mmap_lock so we can only check that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * the mmap_lock is held, if PF_DUMPCORE was not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) mmap_assert_locked(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) BUG_ON(ctx->mm != mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /* Any unrecognized flag is a bug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) VM_BUG_ON(reason & ~__VM_UFFD_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /* 0 or > 1 flags set is a bug; we expect exactly 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) VM_BUG_ON(!reason || (reason & (reason - 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (ctx->features & UFFD_FEATURE_SIGBUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if ((vmf->flags & FAULT_FLAG_USER) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) ctx->flags & UFFD_USER_MODE_ONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) printk_once(KERN_WARNING "uffd: Set unprivileged_userfaultfd "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) "sysctl knob to 1 if kernel faults must be handled "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) "without obtaining CAP_SYS_PTRACE capability\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * If it's already released don't get it. This avoids to loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * in __get_user_pages if userfaultfd_release waits on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * caller of handle_userfault to release the mmap_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (unlikely(READ_ONCE(ctx->released))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * Don't return VM_FAULT_SIGBUS in this case, so a non
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * cooperative manager can close the uffd after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * last UFFDIO_COPY, without risking to trigger an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * involuntary SIGBUS if the process was starting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * userfaultfd while the userfaultfd was still armed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * (but after the last UFFDIO_COPY). If the uffd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * wasn't already closed when the userfault reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * this point, that would normally be solved by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * userfaultfd_must_wait returning 'false'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * If we were to return VM_FAULT_SIGBUS here, the non
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * cooperative manager would be instead forced to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * always call UFFDIO_UNREGISTER before it can safely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * close the uffd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) ret = VM_FAULT_NOPAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * Check that we can return VM_FAULT_RETRY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * NOTE: it should become possible to return VM_FAULT_RETRY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * even if FAULT_FLAG_TRIED is set without leading to gup()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * -EBUSY failures, if the userfaultfd is to be extended for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * VM_UFFD_WP tracking and we intend to arm the userfault
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * without first stopping userland access to the memory. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * VM_UFFD_MISSING userfaults this is enough for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * Validate the invariant that nowait must allow retry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * to be sure not to return SIGBUS erroneously on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * nowait invocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) #ifdef CONFIG_DEBUG_VM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (printk_ratelimit()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) "FAULT_FLAG_ALLOW_RETRY missing %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) vmf->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * Handle nowait, not much to do other than tell it to retry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * and wait.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ret = VM_FAULT_RETRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /* take the reference before dropping the mmap_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) userfaultfd_ctx_get(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) uwq.wq.private = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) uwq.msg = userfault_msg(vmf->address, vmf->flags, reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) ctx->features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) uwq.ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) uwq.waken = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) blocking_state = userfaultfd_get_blocking_state(vmf->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) spin_lock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * After the __add_wait_queue the uwq is visible to userland
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * through poll/read().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * The smp_mb() after __set_current_state prevents the reads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * following the spin_unlock to happen before the list_add in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * __add_wait_queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) set_current_state(blocking_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) spin_unlock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (!is_vm_hugetlb_page(vmf->vma))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) vmf->address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) vmf->flags, reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (likely(must_wait && !READ_ONCE(ctx->released))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) wake_up_poll(&ctx->fd_wqh, EPOLLIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * Here we race with the list_del; list_add in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) * userfaultfd_ctx_read(), however because we don't ever run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) * list_del_init() to refile across the two lists, the prev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) * and next pointers will never point to self. list_add also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) * would never let any of the two pointers to point to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) * self. So list_empty_careful won't risk to see both pointers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * pointing to self at any time during the list refile. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * only case where list_del_init() is called is the full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * removal in the wake function and there we don't re-list_add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * and it's fine not to block on the spinlock. The uwq on this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * kernel stack can be released after the list_del_init.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (!list_empty_careful(&uwq.wq.entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) spin_lock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * No need of list_del_init(), the uwq on the stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * will be freed shortly anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) list_del(&uwq.wq.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) spin_unlock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * ctx may go away after this if the userfault pseudo fd is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * already released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) userfaultfd_ctx_put(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) struct userfaultfd_wait_queue *ewq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) struct userfaultfd_ctx *release_new_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (WARN_ON_ONCE(current->flags & PF_EXITING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) ewq->ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) init_waitqueue_entry(&ewq->wq, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) release_new_ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) spin_lock_irq(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * After the __add_wait_queue the uwq is visible to userland
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * through poll/read().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) __add_wait_queue(&ctx->event_wqh, &ewq->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) set_current_state(TASK_KILLABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (ewq->msg.event == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (READ_ONCE(ctx->released) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) fatal_signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * &ewq->wq may be queued in fork_event, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * __remove_wait_queue ignores the head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * parameter. It would be a problem if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * didn't.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (ewq->msg.event == UFFD_EVENT_FORK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) struct userfaultfd_ctx *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) new = (struct userfaultfd_ctx *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) (unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) ewq->msg.arg.reserved.reserved1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) release_new_ctx = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) spin_unlock_irq(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) wake_up_poll(&ctx->fd_wqh, EPOLLIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) spin_lock_irq(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) spin_unlock_irq(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (release_new_ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) struct vm_area_struct *vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) struct mm_struct *mm = release_new_ctx->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /* the various vma->vm_userfaultfd_ctx still points to it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) mmap_write_lock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) for (vma = mm->mmap; vma; vma = vma->vm_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) vma->vm_flags &= ~__VM_UFFD_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) mmap_write_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) userfaultfd_ctx_put(release_new_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * ctx may go away after this if the userfault pseudo fd is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * already released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) WRITE_ONCE(ctx->mmap_changing, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) userfaultfd_ctx_put(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) struct userfaultfd_wait_queue *ewq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) ewq->msg.event = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) wake_up_locked(&ctx->event_wqh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) struct userfaultfd_ctx *ctx = NULL, *octx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) struct userfaultfd_fork_ctx *fctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) octx = vma->vm_userfaultfd_ctx.ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) vm_write_begin(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) WRITE_ONCE(vma->vm_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) vma->vm_flags & ~__VM_UFFD_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) vm_write_end(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) list_for_each_entry(fctx, fcs, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (fctx->orig == octx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) ctx = fctx->new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (!ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (!fctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (!ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) kfree(fctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) refcount_set(&ctx->refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) ctx->flags = octx->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) ctx->features = octx->features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) ctx->released = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) ctx->mmap_changing = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) ctx->mm = vma->vm_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) mmgrab(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) userfaultfd_ctx_get(octx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) WRITE_ONCE(octx->mmap_changing, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) fctx->orig = octx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) fctx->new = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) list_add_tail(&fctx->list, fcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) vma->vm_userfaultfd_ctx.ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) struct userfaultfd_ctx *ctx = fctx->orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) struct userfaultfd_wait_queue ewq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) msg_init(&ewq.msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) ewq.msg.event = UFFD_EVENT_FORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) userfaultfd_event_wait_completion(ctx, &ewq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) void dup_userfaultfd_complete(struct list_head *fcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) struct userfaultfd_fork_ctx *fctx, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) list_for_each_entry_safe(fctx, n, fcs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) dup_fctx(fctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) list_del(&fctx->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) kfree(fctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) void mremap_userfaultfd_prep(struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) struct vm_userfaultfd_ctx *vm_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct userfaultfd_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) ctx = vma->vm_userfaultfd_ctx.ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (ctx->features & UFFD_FEATURE_EVENT_REMAP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) vm_ctx->ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) userfaultfd_ctx_get(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) WRITE_ONCE(ctx->mmap_changing, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) /* Drop uffd context if remap feature not enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) vma->vm_flags &= ~__VM_UFFD_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) unsigned long from, unsigned long to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) struct userfaultfd_ctx *ctx = vm_ctx->ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) struct userfaultfd_wait_queue ewq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (to & ~PAGE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) userfaultfd_ctx_put(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) msg_init(&ewq.msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) ewq.msg.event = UFFD_EVENT_REMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) ewq.msg.arg.remap.from = from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) ewq.msg.arg.remap.to = to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) ewq.msg.arg.remap.len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) userfaultfd_event_wait_completion(ctx, &ewq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) bool userfaultfd_remove(struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) unsigned long start, unsigned long end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) struct mm_struct *mm = vma->vm_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) struct userfaultfd_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) struct userfaultfd_wait_queue ewq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) ctx = vma->vm_userfaultfd_ctx.ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) userfaultfd_ctx_get(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) WRITE_ONCE(ctx->mmap_changing, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) msg_init(&ewq.msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) ewq.msg.event = UFFD_EVENT_REMOVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) ewq.msg.arg.remove.start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) ewq.msg.arg.remove.end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) userfaultfd_event_wait_completion(ctx, &ewq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) unsigned long start, unsigned long end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) struct userfaultfd_unmap_ctx *unmap_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) list_for_each_entry(unmap_ctx, unmaps, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) unmap_ctx->end == end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) int userfaultfd_unmap_prep(struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) unsigned long start, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) struct list_head *unmaps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) struct userfaultfd_unmap_ctx *unmap_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) has_unmap_ctx(ctx, unmaps, start, end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (!unmap_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) userfaultfd_ctx_get(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) WRITE_ONCE(ctx->mmap_changing, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) unmap_ctx->ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) unmap_ctx->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) unmap_ctx->end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) list_add_tail(&unmap_ctx->list, unmaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) struct userfaultfd_unmap_ctx *ctx, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) struct userfaultfd_wait_queue ewq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) list_for_each_entry_safe(ctx, n, uf, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) msg_init(&ewq.msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) ewq.msg.event = UFFD_EVENT_UNMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) ewq.msg.arg.remove.start = ctx->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) ewq.msg.arg.remove.end = ctx->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) userfaultfd_event_wait_completion(ctx->ctx, &ewq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) list_del(&ctx->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) kfree(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) static int userfaultfd_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) struct userfaultfd_ctx *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) struct mm_struct *mm = ctx->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) struct vm_area_struct *vma, *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) /* len == 0 means wake all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct userfaultfd_wake_range range = { .len = 0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) unsigned long new_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) WRITE_ONCE(ctx->released, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (!mmget_not_zero(mm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) goto wakeup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) * Flush page faults out of all CPUs. NOTE: all page faults
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * must be retried without returning VM_FAULT_SIGBUS if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * changes while handle_userfault released the mmap_lock. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * it's critical that released is set to true (above), before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * taking the mmap_lock for writing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) mmap_write_lock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) for (vma = mm->mmap; vma; vma = vma->vm_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) !!(vma->vm_flags & __VM_UFFD_FLAGS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) if (vma->vm_userfaultfd_ctx.ctx != ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) prev = vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) new_flags, vma->anon_vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) vma->vm_file, vma->vm_pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) vma_policy(vma),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) NULL_VM_UFFD_CTX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) vma_get_anon_name(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) vma = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) prev = vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) vm_write_begin(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) WRITE_ONCE(vma->vm_flags, new_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) vm_write_end(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) mmap_write_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) mmput(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) wakeup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * After no new page faults can wait on this fault_*wqh, flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * the last page faults that may have been already waiting on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) * the fault_*wqh.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) spin_lock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) spin_unlock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) /* Flush pending events that may still wait on event_wqh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) wake_up_all(&ctx->event_wqh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) wake_up_poll(&ctx->fd_wqh, EPOLLHUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) userfaultfd_ctx_put(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) /* fault_pending_wqh.lock must be hold by the caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) static inline struct userfaultfd_wait_queue *find_userfault_in(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) wait_queue_head_t *wqh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) wait_queue_entry_t *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) struct userfaultfd_wait_queue *uwq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) lockdep_assert_held(&wqh->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) uwq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (!waitqueue_active(wqh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) /* walk in reverse to provide FIFO behavior to read userfaults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) wq = list_last_entry(&wqh->head, typeof(*wq), entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) return uwq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) static inline struct userfaultfd_wait_queue *find_userfault(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) struct userfaultfd_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) return find_userfault_in(&ctx->fault_pending_wqh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) static inline struct userfaultfd_wait_queue *find_userfault_evt(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) struct userfaultfd_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) return find_userfault_in(&ctx->event_wqh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) static __poll_t userfaultfd_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) struct userfaultfd_ctx *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) __poll_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) poll_wait(file, &ctx->fd_wqh, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (!userfaultfd_is_initialized(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) return EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * poll() never guarantees that read won't block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) * userfaults can be waken before they're read().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (unlikely(!(file->f_flags & O_NONBLOCK)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) return EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) * lockless access to see if there are pending faults
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) * __pollwait last action is the add_wait_queue but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) * the spin_unlock would allow the waitqueue_active to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) * pass above the actual list_add inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) * add_wait_queue critical section. So use a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * memory barrier to serialize the list_add write of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) * add_wait_queue() with the waitqueue_active read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) * below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (waitqueue_active(&ctx->fault_pending_wqh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) ret = EPOLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) else if (waitqueue_active(&ctx->event_wqh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) ret = EPOLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) static const struct file_operations userfaultfd_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) static int resolve_userfault_fork(struct userfaultfd_ctx *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) struct uffd_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS), inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) msg->arg.reserved.reserved1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) msg->arg.fork.ufd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) struct uffd_msg *msg, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) struct userfaultfd_wait_queue *uwq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) * Handling fork event requires sleeping operations, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) * we drop the event_wqh lock, then do these ops, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * lock it back and wake up the waiter. While the lock is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * dropped the ewq may go away so we keep track of it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * carefully.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) LIST_HEAD(fork_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) struct userfaultfd_ctx *fork_nctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) /* always take the fd_wqh lock before the fault_pending_wqh lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) spin_lock_irq(&ctx->fd_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) __add_wait_queue(&ctx->fd_wqh, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) spin_lock(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) uwq = find_userfault(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) if (uwq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * Use a seqcount to repeat the lockless check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) * in wake_userfault() to avoid missing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * wakeups because during the refile both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * waitqueue could become empty if this is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * only userfault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) write_seqcount_begin(&ctx->refile_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) * The fault_pending_wqh.lock prevents the uwq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) * to disappear from under us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) * Refile this userfault from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) * fault_pending_wqh to fault_wqh, it's not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) * pending anymore after we read it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) * Use list_del() by hand (as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) * userfaultfd_wake_function also uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) * list_del_init() by hand) to be sure nobody
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) * changes __remove_wait_queue() to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) * list_del_init() in turn breaking the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) * !list_empty_careful() check in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) * handle_userfault(). The uwq->wq.head list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) * must never be empty at any time during the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * refile, or the waitqueue could disappear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * from under us. The "wait_queue_head_t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) * parameter of __remove_wait_queue() is unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) * anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) list_del(&uwq->wq.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) add_wait_queue(&ctx->fault_wqh, &uwq->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) write_seqcount_end(&ctx->refile_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) /* careful to always initialize msg if ret == 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) *msg = uwq->msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) spin_unlock(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) spin_unlock(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) spin_lock(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) uwq = find_userfault_evt(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) if (uwq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) *msg = uwq->msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) if (uwq->msg.event == UFFD_EVENT_FORK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) fork_nctx = (struct userfaultfd_ctx *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) (unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) uwq->msg.arg.reserved.reserved1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) list_move(&uwq->wq.entry, &fork_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) * fork_nctx can be freed as soon as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) * we drop the lock, unless we take a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) * reference on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) userfaultfd_ctx_get(fork_nctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) spin_unlock(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) userfaultfd_event_complete(ctx, uwq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) spin_unlock(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) spin_unlock(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) if (no_wait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) spin_unlock_irq(&ctx->fd_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) spin_lock_irq(&ctx->fd_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) __remove_wait_queue(&ctx->fd_wqh, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) spin_unlock_irq(&ctx->fd_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) if (!ret && msg->event == UFFD_EVENT_FORK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) ret = resolve_userfault_fork(fork_nctx, inode, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) spin_lock_irq(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) if (!list_empty(&fork_event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) * The fork thread didn't abort, so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) * drop the temporary refcount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) userfaultfd_ctx_put(fork_nctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) uwq = list_first_entry(&fork_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) typeof(*uwq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) wq.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) * If fork_event list wasn't empty and in turn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) * the event wasn't already released by fork
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) * (the event is allocated on fork kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) * stack), put the event back to its place in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) * the event_wq. fork_event head will be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) * as soon as we return so the event cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) * stay queued there no matter the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) * "ret" value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) list_del(&uwq->wq.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) __add_wait_queue(&ctx->event_wqh, &uwq->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) * Leave the event in the waitqueue and report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) * error to userland if we failed to resolve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) * the userfault fork.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) if (likely(!ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) userfaultfd_event_complete(ctx, uwq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) * Here the fork thread aborted and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) * refcount from the fork thread on fork_nctx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) * has already been released. We still hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) * the reference we took before releasing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) * lock above. If resolve_userfault_fork
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) * failed we've to drop it because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) * fork_nctx has to be freed in such case. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) * it succeeded we'll hold it because the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) * uffd references it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) userfaultfd_ctx_put(fork_nctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) spin_unlock_irq(&ctx->event_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) static ssize_t userfaultfd_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) struct userfaultfd_ctx *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) ssize_t _ret, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) struct uffd_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) int no_wait = file->f_flags & O_NONBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) if (!userfaultfd_is_initialized(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) if (count < sizeof(msg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) return ret ? ret : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) _ret = userfaultfd_ctx_read(ctx, no_wait, &msg, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) if (_ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) return ret ? ret : _ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) return ret ? ret : -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) ret += sizeof(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) buf += sizeof(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) count -= sizeof(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) * Allow to read more than one fault at time but only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) * block if waiting for the very first one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) no_wait = O_NONBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) static void __wake_userfault(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct userfaultfd_wake_range *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) spin_lock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) /* wake all in the range and autoremove */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (waitqueue_active(&ctx->fault_pending_wqh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) if (waitqueue_active(&ctx->fault_wqh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) spin_unlock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) struct userfaultfd_wake_range *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) unsigned seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) bool need_wakeup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) * To be sure waitqueue_active() is not reordered by the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) * before the pagetable update, use an explicit SMP memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) * barrier here. PT lock release or mmap_read_unlock(mm) still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) * have release semantics that can allow the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) * waitqueue_active() to be reordered before the pte update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) * Use waitqueue_active because it's very frequent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) * change the address space atomically even if there are no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) * userfaults yet. So we take the spinlock only when we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) * sure we've userfaults to wake.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) seq = read_seqcount_begin(&ctx->refile_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) waitqueue_active(&ctx->fault_wqh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) } while (read_seqcount_retry(&ctx->refile_seq, seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) if (need_wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) __wake_userfault(ctx, range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) static __always_inline int validate_range(struct mm_struct *mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) __u64 start, __u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) __u64 task_size = mm->task_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) if (start & ~PAGE_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) if (len & ~PAGE_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) if (start < mmap_min_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) if (start >= task_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) if (len > task_size - start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) static inline bool vma_can_userfault(struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) unsigned long vm_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) /* FIXME: add WP support to hugetlbfs and shmem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) if (vm_flags & VM_UFFD_WP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (is_vm_hugetlb_page(vma) || vma_is_shmem(vma))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) if (vm_flags & VM_UFFD_MINOR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) if (!(is_vm_hugetlb_page(vma) || vma_is_shmem(vma)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) vma_is_shmem(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) static int userfaultfd_register(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) struct mm_struct *mm = ctx->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) struct vm_area_struct *vma, *prev, *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) struct uffdio_register uffdio_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) struct uffdio_register __user *user_uffdio_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) unsigned long vm_flags, new_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) bool basic_ioctls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) unsigned long start, end, vma_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) user_uffdio_register = (struct uffdio_register __user *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) if (copy_from_user(&uffdio_register, user_uffdio_register,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) sizeof(uffdio_register)-sizeof(__u64)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) if (!uffdio_register.mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) if (uffdio_register.mode & ~UFFD_API_REGISTER_MODES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) vm_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) vm_flags |= VM_UFFD_MISSING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) vm_flags |= VM_UFFD_WP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MINOR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) vm_flags |= VM_UFFD_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) ret = validate_range(mm, uffdio_register.range.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) uffdio_register.range.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) start = uffdio_register.range.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) end = start + uffdio_register.range.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) if (!mmget_not_zero(mm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) mmap_write_lock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) vma = find_vma_prev(mm, start, &prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) if (!vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) /* check that there's at least one vma in the range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) if (vma->vm_start >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) * If the first vma contains huge pages, make sure start address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) * is aligned to huge page size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) if (is_vm_hugetlb_page(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) if (start & (vma_hpagesize - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) * Search for not compatible vmas.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) basic_ioctls = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) !!(cur->vm_flags & __VM_UFFD_FLAGS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) /* check not compatible vmas */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) if (!vma_can_userfault(cur, vm_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) * UFFDIO_COPY will fill file holes even without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) * PROT_WRITE. This check enforces that if this is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) * MAP_SHARED, the process has write permission to the backing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) * file. If VM_MAYWRITE is set it also enforces that on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) * MAP_SHARED vma: there is no F_WRITE_SEAL and no further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) * F_WRITE_SEAL can be taken until the vma is destroyed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) * If this vma contains ending address, and huge pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) * check alignment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) end > cur->vm_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) if (end & (vma_hpagesize - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) if ((vm_flags & VM_UFFD_WP) && !(cur->vm_flags & VM_MAYWRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) * Check that this vma isn't already owned by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) * different userfaultfd. We can't allow more than one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) * userfaultfd to own a single vma simultaneously or we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) * wouldn't know which one to deliver the userfaults to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) if (cur->vm_userfaultfd_ctx.ctx &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) cur->vm_userfaultfd_ctx.ctx != ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) * Note vmas containing huge pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) if (is_vm_hugetlb_page(cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) basic_ioctls = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) BUG_ON(!found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) if (vma->vm_start < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) prev = vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) BUG_ON(!vma_can_userfault(vma, vm_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) vma->vm_userfaultfd_ctx.ctx != ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) * Nothing to do: this vma is already registered into this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) * userfaultfd and with the right tracking mode too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) if (vma->vm_userfaultfd_ctx.ctx == ctx &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) (vma->vm_flags & vm_flags) == vm_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) if (vma->vm_start > start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) start = vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) vma_end = min(end, vma->vm_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) new_flags = (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) prev = vma_merge(mm, prev, start, vma_end, new_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) vma->anon_vma, vma->vm_file, vma->vm_pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) vma_policy(vma),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) ((struct vm_userfaultfd_ctx){ ctx }),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) vma_get_anon_name(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) if (prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) vma = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) if (vma->vm_start < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) ret = split_vma(mm, vma, start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) if (vma->vm_end > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) ret = split_vma(mm, vma, end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) * In the vma_merge() successful mprotect-like case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) * the next vma was merged into the current one and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) * the current one has not been updated yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) vm_write_begin(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) WRITE_ONCE(vma->vm_flags, new_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) vma->vm_userfaultfd_ctx.ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) vm_write_end(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) if (is_vm_hugetlb_page(vma) && uffd_disable_huge_pmd_share(vma))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) hugetlb_unshare_all_pmds(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) prev = vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) start = vma->vm_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) vma = vma->vm_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) } while (vma && vma->vm_start < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) mmap_write_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) mmput(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) __u64 ioctls_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) ioctls_out = basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) UFFD_API_RANGE_IOCTLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) * Declare the WP ioctl only if the WP mode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) * specified and all checks passed with the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_WP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) ioctls_out &= ~((__u64)1 << _UFFDIO_WRITEPROTECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) /* CONTINUE ioctl is only supported for MINOR ranges. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) if (!(uffdio_register.mode & UFFDIO_REGISTER_MODE_MINOR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) ioctls_out &= ~((__u64)1 << _UFFDIO_CONTINUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) * Now that we scanned all vmas we can already tell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) * userland which ioctls methods are guaranteed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) * succeed on this range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) if (put_user(ioctls_out, &user_uffdio_register->ioctls))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) struct mm_struct *mm = ctx->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) struct vm_area_struct *vma, *prev, *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) struct uffdio_range uffdio_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) unsigned long new_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) unsigned long start, end, vma_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) const void __user *buf = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) ret = validate_range(mm, uffdio_unregister.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) uffdio_unregister.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) start = uffdio_unregister.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) end = start + uffdio_unregister.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) if (!mmget_not_zero(mm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) mmap_write_lock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) vma = find_vma_prev(mm, start, &prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) if (!vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) /* check that there's at least one vma in the range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) if (vma->vm_start >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) * If the first vma contains huge pages, make sure start address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) * is aligned to huge page size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) if (is_vm_hugetlb_page(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) if (start & (vma_hpagesize - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) * Search for not compatible vmas.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) !!(cur->vm_flags & __VM_UFFD_FLAGS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) * Check not compatible vmas, not strictly required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) * here as not compatible vmas cannot have an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) * userfaultfd_ctx registered on them, but this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) * provides for more strict behavior to notice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) * unregistration errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) if (!vma_can_userfault(cur, cur->vm_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) BUG_ON(!found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (vma->vm_start < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) prev = vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) BUG_ON(!vma_can_userfault(vma, vma->vm_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) * Nothing to do: this vma is already registered into this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) * userfaultfd and with the right tracking mode too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) if (!vma->vm_userfaultfd_ctx.ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) if (vma->vm_start > start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) start = vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) vma_end = min(end, vma->vm_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) if (userfaultfd_missing(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) * Wake any concurrent pending userfault while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) * we unregister, so they will not hang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) * permanently and it avoids userland to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) * UFFDIO_WAKE explicitly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) struct userfaultfd_wake_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) range.start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) range.len = vma_end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) prev = vma_merge(mm, prev, start, vma_end, new_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) vma->anon_vma, vma->vm_file, vma->vm_pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) vma_policy(vma),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) NULL_VM_UFFD_CTX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) vma_get_anon_name(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) if (prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) vma = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) if (vma->vm_start < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) ret = split_vma(mm, vma, start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) if (vma->vm_end > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) ret = split_vma(mm, vma, end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) * In the vma_merge() successful mprotect-like case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) * the next vma was merged into the current one and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) * the current one has not been updated yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) vm_write_begin(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) WRITE_ONCE(vma->vm_flags, new_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) vm_write_end(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) prev = vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) start = vma->vm_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) vma = vma->vm_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) } while (vma && vma->vm_start < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) mmap_write_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) mmput(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) * userfaultfd_wake may be used in combination with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) struct uffdio_range uffdio_wake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) struct userfaultfd_wake_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) const void __user *buf = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) range.start = uffdio_wake.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) range.len = uffdio_wake.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) * len == 0 means wake all and we don't want to wake all here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) * so check it again to be sure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) VM_BUG_ON(!range.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) wake_userfault(ctx, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) __s64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) struct uffdio_copy uffdio_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) struct uffdio_copy __user *user_uffdio_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) struct userfaultfd_wake_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) user_uffdio_copy = (struct uffdio_copy __user *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) if (READ_ONCE(ctx->mmap_changing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) if (copy_from_user(&uffdio_copy, user_uffdio_copy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) /* don't copy "copy" last field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) sizeof(uffdio_copy)-sizeof(__s64)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) * double check for wraparound just in case. copy_from_user()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) * will later check uffdio_copy.src + uffdio_copy.len to fit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) * in the userland range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) if (mmget_not_zero(ctx->mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) uffdio_copy.len, &ctx->mmap_changing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) uffdio_copy.mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) mmput(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) BUG_ON(!ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) /* len == 0 would wake all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) range.len = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) range.start = uffdio_copy.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) wake_userfault(ctx, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) __s64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) struct uffdio_zeropage uffdio_zeropage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) struct uffdio_zeropage __user *user_uffdio_zeropage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) struct userfaultfd_wake_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) if (READ_ONCE(ctx->mmap_changing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) /* don't copy "zeropage" last field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) sizeof(uffdio_zeropage)-sizeof(__s64)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) uffdio_zeropage.range.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) if (mmget_not_zero(ctx->mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) uffdio_zeropage.range.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) &ctx->mmap_changing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) mmput(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) /* len == 0 would wake all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) BUG_ON(!ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) range.len = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) range.start = uffdio_zeropage.range.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) wake_userfault(ctx, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) struct uffdio_writeprotect uffdio_wp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) struct uffdio_writeprotect __user *user_uffdio_wp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) struct userfaultfd_wake_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) bool mode_wp, mode_dontwake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) if (READ_ONCE(ctx->mmap_changing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) user_uffdio_wp = (struct uffdio_writeprotect __user *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) if (copy_from_user(&uffdio_wp, user_uffdio_wp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) sizeof(struct uffdio_writeprotect)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) ret = validate_range(ctx->mm, uffdio_wp.range.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) uffdio_wp.range.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) if (uffdio_wp.mode & ~(UFFDIO_WRITEPROTECT_MODE_DONTWAKE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) UFFDIO_WRITEPROTECT_MODE_WP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) mode_wp = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_WP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) mode_dontwake = uffdio_wp.mode & UFFDIO_WRITEPROTECT_MODE_DONTWAKE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) if (mode_wp && mode_dontwake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) if (mmget_not_zero(ctx->mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) uffdio_wp.range.len, mode_wp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) &ctx->mmap_changing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) mmput(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) if (!mode_wp && !mode_dontwake) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) range.start = uffdio_wp.range.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) range.len = uffdio_wp.range.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) wake_userfault(ctx, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) __s64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) struct uffdio_continue uffdio_continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) struct uffdio_continue __user *user_uffdio_continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) struct userfaultfd_wake_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) user_uffdio_continue = (struct uffdio_continue __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) if (READ_ONCE(ctx->mmap_changing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) if (copy_from_user(&uffdio_continue, user_uffdio_continue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) /* don't copy the output fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) sizeof(uffdio_continue) - (sizeof(__s64))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) ret = validate_range(ctx->mm, uffdio_continue.range.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) uffdio_continue.range.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) /* double check for wraparound just in case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) if (uffdio_continue.range.start + uffdio_continue.range.len <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) uffdio_continue.range.start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) if (uffdio_continue.mode & ~UFFDIO_CONTINUE_MODE_DONTWAKE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) if (mmget_not_zero(ctx->mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) ret = mcopy_continue(ctx->mm, uffdio_continue.range.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) uffdio_continue.range.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) &ctx->mmap_changing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) mmput(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) if (unlikely(put_user(ret, &user_uffdio_continue->mapped)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) /* len == 0 would wake all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) BUG_ON(!ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) range.len = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) if (!(uffdio_continue.mode & UFFDIO_CONTINUE_MODE_DONTWAKE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) range.start = uffdio_continue.range.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) wake_userfault(ctx, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) ret = range.len == uffdio_continue.range.len ? 0 : -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) static inline unsigned int uffd_ctx_features(__u64 user_features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) * For the current set of features the bits just coincide. Set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) * UFFD_FEATURE_INITIALIZED to mark the features as enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) return (unsigned int)user_features | UFFD_FEATURE_INITIALIZED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) * userland asks for a certain API version and we return which bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) * and ioctl commands are implemented in this kernel for such API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) * version or -EINVAL if unknown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) static int userfaultfd_api(struct userfaultfd_ctx *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) struct uffdio_api uffdio_api;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) void __user *buf = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) unsigned int ctx_features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) __u64 features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) features = uffdio_api.features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) /* report all available features and ioctls to userland */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) uffdio_api.features = UFFD_API_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) uffdio_api.features &=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) ~(UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) uffdio_api.ioctls = UFFD_API_IOCTLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) /* only enable the requested features for this uffd context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) ctx_features = uffd_ctx_features(features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) if (cmpxchg(&ctx->features, 0, ctx_features) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) memset(&uffdio_api, 0, sizeof(uffdio_api));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) static long userfaultfd_ioctl(struct file *file, unsigned cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) struct userfaultfd_ctx *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) if (cmd != UFFDIO_API && !userfaultfd_is_initialized(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) switch(cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) case UFFDIO_API:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) ret = userfaultfd_api(ctx, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) case UFFDIO_REGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) ret = userfaultfd_register(ctx, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) case UFFDIO_UNREGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) ret = userfaultfd_unregister(ctx, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) case UFFDIO_WAKE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) ret = userfaultfd_wake(ctx, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) case UFFDIO_COPY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) ret = userfaultfd_copy(ctx, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) case UFFDIO_ZEROPAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) ret = userfaultfd_zeropage(ctx, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) case UFFDIO_WRITEPROTECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) ret = userfaultfd_writeprotect(ctx, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) case UFFDIO_CONTINUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) ret = userfaultfd_continue(ctx, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) struct userfaultfd_ctx *ctx = f->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) wait_queue_entry_t *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) unsigned long pending = 0, total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) spin_lock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) pending++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) total++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) total++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) spin_unlock_irq(&ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) * If more protocols will be added, there will be all shown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) * separated by a space. Like this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) * protocols: aa:... bb:...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) pending, total, UFFD_API, ctx->features,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) static const struct file_operations userfaultfd_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) .show_fdinfo = userfaultfd_show_fdinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) .release = userfaultfd_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) .poll = userfaultfd_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) .read = userfaultfd_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) .unlocked_ioctl = userfaultfd_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) static void init_once_userfaultfd_ctx(void *mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) init_waitqueue_head(&ctx->fault_pending_wqh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) init_waitqueue_head(&ctx->fault_wqh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) init_waitqueue_head(&ctx->event_wqh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) init_waitqueue_head(&ctx->fd_wqh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) seqcount_spinlock_init(&ctx->refile_seq, &ctx->fault_pending_wqh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) SYSCALL_DEFINE1(userfaultfd, int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) struct userfaultfd_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) if (!sysctl_unprivileged_userfaultfd &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) (flags & UFFD_USER_MODE_ONLY) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) !capable(CAP_SYS_PTRACE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) printk_once(KERN_WARNING "uffd: Set unprivileged_userfaultfd "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) "sysctl knob to 1 if kernel faults must be handled "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) "without obtaining CAP_SYS_PTRACE capability\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) BUG_ON(!current->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) /* Check the UFFD_* constants for consistency. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) BUILD_BUG_ON(UFFD_USER_MODE_ONLY & UFFD_SHARED_FCNTL_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) if (flags & ~(UFFD_SHARED_FCNTL_FLAGS | UFFD_USER_MODE_ONLY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) refcount_set(&ctx->refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) ctx->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) ctx->features = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) ctx->released = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) ctx->mmap_changing = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) ctx->mm = current->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) /* prevent the mm struct to be freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) mmgrab(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) mmdrop(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) kmem_cache_free(userfaultfd_ctx_cachep, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) static int __init userfaultfd_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) sizeof(struct userfaultfd_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) SLAB_HWCACHE_ALIGN|SLAB_PANIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) init_once_userfaultfd_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) __initcall(userfaultfd_init);