diff options
author | Mike Rapoport <rppt@linux.vnet.ibm.com> | 2017-02-24 17:58:22 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2017-02-24 20:46:55 -0500 |
commit | 897ab3e0c49e24b62e2d54d165c7afec6bbca65b (patch) | |
tree | 5fa7e09864d6c959cef33849f6cb10ed04e459e4 /fs/userfaultfd.c | |
parent | 846b1a0f1db065a8479159dd8fecddb1ebf30547 (diff) |
userfaultfd: non-cooperative: add event for memory unmaps
When a non-cooperative userfaultfd monitor copies pages in the
background, it may encounter regions that were already unmapped.
Addition of UFFD_EVENT_UNMAP allows the uffd monitor to track precisely
changes in the virtual memory layout.
Since there might be different uffd contexts for the affected VMAs, we
first should create a temporary representation for the unmap event for
each uffd context and then notify them one by one to the appropriate
userfault file descriptors.
The event notification occurs after the mmap_sem has been released.
[arnd@arndb.de: fix nommu build]
Link: http://lkml.kernel.org/r/20170203165141.3665284-1-arnd@arndb.de
[mhocko@suse.com: fix nommu build]
Link: http://lkml.kernel.org/r/20170202091503.GA22823@dhcp22.suse.cz
Link: http://lkml.kernel.org/r/1485542673-24387-3-git-send-email-rppt@linux.vnet.ibm.com
Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
Signed-off-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Acked-by: Hillf Danton <hillf.zj@alibaba-inc.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Pavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs/userfaultfd.c')
-rw-r--r-- | fs/userfaultfd.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 8fe601b4875e..4c78458ea78d 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c | |||
@@ -71,6 +71,13 @@ struct userfaultfd_fork_ctx { | |||
71 | struct list_head list; | 71 | struct list_head list; |
72 | }; | 72 | }; |
73 | 73 | ||
74 | struct userfaultfd_unmap_ctx { | ||
75 | struct userfaultfd_ctx *ctx; | ||
76 | unsigned long start; | ||
77 | unsigned long end; | ||
78 | struct list_head list; | ||
79 | }; | ||
80 | |||
74 | struct userfaultfd_wait_queue { | 81 | struct userfaultfd_wait_queue { |
75 | struct uffd_msg msg; | 82 | struct uffd_msg msg; |
76 | wait_queue_t wq; | 83 | wait_queue_t wq; |
@@ -709,6 +716,64 @@ void userfaultfd_remove(struct vm_area_struct *vma, | |||
709 | down_read(&mm->mmap_sem); | 716 | down_read(&mm->mmap_sem); |
710 | } | 717 | } |
711 | 718 | ||
719 | static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps, | ||
720 | unsigned long start, unsigned long end) | ||
721 | { | ||
722 | struct userfaultfd_unmap_ctx *unmap_ctx; | ||
723 | |||
724 | list_for_each_entry(unmap_ctx, unmaps, list) | ||
725 | if (unmap_ctx->ctx == ctx && unmap_ctx->start == start && | ||
726 | unmap_ctx->end == end) | ||
727 | return true; | ||
728 | |||
729 | return false; | ||
730 | } | ||
731 | |||
732 | int userfaultfd_unmap_prep(struct vm_area_struct *vma, | ||
733 | unsigned long start, unsigned long end, | ||
734 | struct list_head *unmaps) | ||
735 | { | ||
736 | for ( ; vma && vma->vm_start < end; vma = vma->vm_next) { | ||
737 | struct userfaultfd_unmap_ctx *unmap_ctx; | ||
738 | struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx; | ||
739 | |||
740 | if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) || | ||
741 | has_unmap_ctx(ctx, unmaps, start, end)) | ||
742 | continue; | ||
743 | |||
744 | unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL); | ||
745 | if (!unmap_ctx) | ||
746 | return -ENOMEM; | ||
747 | |||
748 | userfaultfd_ctx_get(ctx); | ||
749 | unmap_ctx->ctx = ctx; | ||
750 | unmap_ctx->start = start; | ||
751 | unmap_ctx->end = end; | ||
752 | list_add_tail(&unmap_ctx->list, unmaps); | ||
753 | } | ||
754 | |||
755 | return 0; | ||
756 | } | ||
757 | |||
758 | void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf) | ||
759 | { | ||
760 | struct userfaultfd_unmap_ctx *ctx, *n; | ||
761 | struct userfaultfd_wait_queue ewq; | ||
762 | |||
763 | list_for_each_entry_safe(ctx, n, uf, list) { | ||
764 | msg_init(&ewq.msg); | ||
765 | |||
766 | ewq.msg.event = UFFD_EVENT_UNMAP; | ||
767 | ewq.msg.arg.remove.start = ctx->start; | ||
768 | ewq.msg.arg.remove.end = ctx->end; | ||
769 | |||
770 | userfaultfd_event_wait_completion(ctx->ctx, &ewq); | ||
771 | |||
772 | list_del(&ctx->list); | ||
773 | kfree(ctx); | ||
774 | } | ||
775 | } | ||
776 | |||
712 | static int userfaultfd_release(struct inode *inode, struct file *file) | 777 | static int userfaultfd_release(struct inode *inode, struct file *file) |
713 | { | 778 | { |
714 | struct userfaultfd_ctx *ctx = file->private_data; | 779 | struct userfaultfd_ctx *ctx = file->private_data; |