From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3BA3C47DB3 for ; Mon, 29 Jan 2024 19:35:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 86E366B0093; Mon, 29 Jan 2024 14:35:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CF046B0098; Mon, 29 Jan 2024 14:35:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 649296B0099; Mon, 29 Jan 2024 14:35:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4C0446B0093 for ; Mon, 29 Jan 2024 14:35:39 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id EDFB9160131 for ; Mon, 29 Jan 2024 19:35:38 +0000 (UTC) X-FDA: 81733353156.21.D35E056 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf04.hostedemail.com (Postfix) with ESMTP id 3521D40015 for ; Mon, 29 Jan 2024 19:35:37 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=eBoheyW1; spf=pass (imf04.hostedemail.com: domain of 3CP63ZQsKCBkAD93H6572Gz5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--lokeshgidra.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3CP63ZQsKCBkAD93H6572Gz5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--lokeshgidra.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706556937; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/rTTdTkZrF1wexi3aiNZ7/0XWdKeqDeZCcom0yepX1o=; b=hHlqZgDlBOZEpClowVHUNVi/FvJv+eEsk6DBuEPDen7WQPCdCrkM/NYKAVujtZa+NeJTn1 YlUk2U+rRWpknLvGcchaepStdl1Sm8QukUuUZBfbv+6j39vjf16G00hLrts7Q0m/YZ7OCm ZxucsF++UFH23JbXWcXBTl5foyK1V2M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706556937; a=rsa-sha256; cv=none; b=4x9cXWFGHH3QaDT7acLjk2Mi+b2a78DhInoa9fq1/jegkdoqRJdhcac2IZmL4jKxc7y3Lz s6H/rCgkVULZhMB5aix5Zf52p7CNJcuPsRzWgEgABN1HVVnDzhRaqCWqi5ElrCUIsgHunY O6JIIuihpEY1tlELti0Gu/ziF1+nVhI= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=eBoheyW1; spf=pass (imf04.hostedemail.com: domain of 3CP63ZQsKCBkAD93H6572Gz5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--lokeshgidra.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3CP63ZQsKCBkAD93H6572Gz5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--lokeshgidra.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc22f3426aeso5840156276.1 for ; Mon, 29 Jan 2024 11:35:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706556936; x=1707161736; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/rTTdTkZrF1wexi3aiNZ7/0XWdKeqDeZCcom0yepX1o=; b=eBoheyW14/+yF6Q99tPelVwiirREl7+mCwXUTArASOH28qKrWrgxsUmRleVC6bNtIm b0/y0gbbQHfqczHVL3F80Y3cftaCm7voM8oej7XhGVd8Kdopa7UCJDHv3Su+AmME26xK cCvTjYApid14a83lWMAZfj8kMemSL0oxOFByMp1A7HmLxtSuFrcFzMGThxKjz2yU4YyS KmSrcnRHF9TWZGJz60B9RFlqceIScDURup5NlqoOM2nGek8urhunQCffdywvjX/4Rarf EAnX6URIcFMS6sqIZo8WLICusxDCD0P2TBm6ksQLJ/rzSJ0Quhr1bG7YCVsUygHMGfK+ Fiow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706556936; x=1707161736; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/rTTdTkZrF1wexi3aiNZ7/0XWdKeqDeZCcom0yepX1o=; b=gAxQa0sIZlIje9k73NVo3rHXlB+zmaLPoGL9XuNCDc6ewUC5NUVll637OKrqwHeQLT ZzLN7usBeTNvkSbmUrXSwsuknJ5U1TeTeGnWZgGi0BEQinnJHC/yM0wDfqoS3q3te8qO dzOq9tVQhWWOqCsMUlapQVpSS3fzxDX9rtXY1+LI1c4w7s8ESJZfl8KuiokBRFFsDNkl YGxWguPC1jaQFEACB0SD9WnHNXfIraHMvRs03W8e14dScHtLQyXfnk6CijXRkCT5up9R H461GyoPkEmekJbrKu/WtT6uFtjAAny9RpDTdxzO++9RNTSpWRi4gdrkAmpJy9tK6m54 NHaw== X-Gm-Message-State: AOJu0YxY6IcKc5b+tJ2rExtSEHJcznzweFvorcmI4rUeSliG+35MMXDh FkaOP8EJPXjwuOAojMkUd1ZdPillzlEA+u/+Uw70N4IQM3WSWoprw75qz5WpqpF/gfuW7UcvNDa 82HI5w5nTul79+TgaFa/82Q== X-Google-Smtp-Source: AGHT+IHg5bBsWt55Rz1Rugqar6MZ+VAZSQCpoEjW8I0EHN+ywy05kgFpt3/w0bymXcr1SFJEZK7hul4bbbOZB2g6+g== X-Received: from lg.mtv.corp.google.com ([2620:15c:211:202:9b1d:f1ee:f750:93f1]) (user=lokeshgidra job=sendgmr) by 2002:a05:6902:250b:b0:dbe:d0a9:2be3 with SMTP id dt11-20020a056902250b00b00dbed0a92be3mr2244340ybb.3.1706556936414; Mon, 29 Jan 2024 11:35:36 -0800 (PST) Date: Mon, 29 Jan 2024 11:35:12 -0800 In-Reply-To: <20240129193512.123145-1-lokeshgidra@google.com> Mime-Version: 1.0 References: <20240129193512.123145-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240129193512.123145-4-lokeshgidra@google.com> Subject: [PATCH v2 3/3] userfaultfd: use per-vma locks in userfaultfd operations From: Lokesh Gidra To: akpm@linux-foundation.org Cc: lokeshgidra@google.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, selinux@vger.kernel.org, surenb@google.com, kernel-team@android.com, aarcange@redhat.com, peterx@redhat.com, david@redhat.com, axelrasmussen@google.com, bgeffon@google.com, willy@infradead.org, jannh@google.com, kaleshsingh@google.com, ngeoffray@google.com, timmurray@google.com, rppt@kernel.org Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: s8f1xbi6a4szypn3pnk13uofm5uxnps6 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 3521D40015 X-Rspam-User: X-HE-Tag: 1706556937-417791 X-HE-Meta: U2FsdGVkX1/JKRGXTea/eRJqicpa5uhZjsT8XD3o9jscTKWZgvzlhkH2/TFRTKl/1dHV9njjTg5HcKUirPfM84qQ+2ErKQpGx2j93dCxq+7o7noqqPPL11iJaM72gWe5i+BBDhJL9JQAc8tc0c32LF7Vp8J1Keqg9wL5RAOm+GRD+n3ScMBdbHZt87+Qb3LomMYIq6tb0HYvPQLxiYz8JID/tIMcaLgKsxjh8iE3zO6YD0JHPUID/8KHWF08XlrnJs/6Xfu9ov1VEt1RPL3R7AZztNre2rafmIYkokP8VuBPcGu3ybHTENw0A7ZaJh8bYbQkhXzbIVuWLqjuZfdFFYrU7v2yUM8M9FE3BxuniKWmLV4CV2HOAetXjyZ36808Ej6MqQ13P3HhoXfEEqJJS5RDhHj1XQIZsfcjvCG14CohstOSnd9ICvdhHCoBH+JvujGeFuZIgE1sT0AVmwAh5fhXVuC6fIVI3cXLW/J0TtzyMsMuGhBQL/robfxr7KTEbkH4Eet6djs6wohvruLuPV+M2oEUxgSSh2hAEFzeEmpGs05Utx9OsnKRH+c0JbARMS0sPCHC/H2EopAVxXF4J6HOtrC9ASE8MOWAeCBpCCL7UAdjR9DQBCu86zJgOchq5Gx4sUJi2dZu6FgTTbJenJJIaXzkS5Z89JBd7BbJkWddXa4L8CyEE0eqpf/x2nvNPd3wVpqwN9hCL/qfLzMu1TmVtRHghEGo0Q9lz1qH/GbfZDRegnN6OekSTmI/4aAUYOJaU8q2enMxLiN0wGuwd+HbZ0UI9L9M6j6rSpVX8Q6Wi9A3seVdQJgZlcX81vj/NRkxhlJvg2r1vPHiZf6WTVW7ReXdsHgckyRgKbLJhkuitvbsXix3ECRI9GYD++LZMSGm5GWfo9QahUFI7i62blzQC0uClCayhIHBza2qieto1GkU0zTm07c1j80m1sKf6oJ6hVsiT6TOuqwK2kl IaBlae8x QTNE7jgZhu65m8pHPrWOL6YPJ2/5IajXQYIe4P3eNGfp+vtgbUlH2mzkpkm6ovb1MoAnvimVgHOcdBL46pwJvzhpJ882Jh2rmeUv+Njr50X3bOsl+9RCaTvBdOqh7Bbdb8jVTYe+9xlCME+B/vd5GU17LinxCx4jcajUu/VG1Vv6sp1nw2cFUACrei/X2mbRcewP/EAXCzw/qZFdtIhBnlTp+DhEiwU4IEGdFNfarvAatzRPUjS7oAASUc03sdcvFeJBUVoUsY9/dIoc/td3rQEys3nXTj02JY8hN2OuUoDs0/oB8ZhQviJmuy245BCZ2lWK3ax3O3l1zbnINGPUNDovXtoYs75Wp38pcnCtVlioOvazEgu2m1a0rMf/VDD8jbaj59J1IkbjkSRWg0Y9BQW7AsEMThAhi2JkwRwJy8LQ6FkTPl7FMpiWAe/nA0bXC8mMIZdqkErJ7UyQuNHTgRosnDaKwwu4VrISsiVS3K9pe7nj1s3rwNyQQrFakDoS5inrA+n95jfOiSifheYie+X4L2jwjkgtE5lDtEiUvaKeeTDQAHleLkGgfrqkVkns8ixOSTV/iM8gUzLhDuZl7YlxZgUY0KHXf3PTfikJjlhCexg0DWMQ9NIZzwtWJnOstQDFKvvpL0+GTbaGAYiPQOna9Jg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: All userfaultfd operations, except write-protect, opportunistically use per-vma locks to lock vmas. If we fail then fall back to locking mmap-lock in read-mode. Write-protect operation requires mmap_lock as it iterates over multiple vmas. Signed-off-by: Lokesh Gidra --- fs/userfaultfd.c | 13 +-- include/linux/userfaultfd_k.h | 5 +- mm/userfaultfd.c | 175 +++++++++++++++++++++++----------- 3 files changed, 122 insertions(+), 71 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index c00a021bcce4..60dcfafdc11a 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -2005,17 +2005,8 @@ static int userfaultfd_move(struct userfaultfd_ctx *ctx, return -EINVAL; if (mmget_not_zero(mm)) { - mmap_read_lock(mm); - - /* Re-check after taking map_changing_lock */ - down_read(&ctx->map_changing_lock); - if (likely(!atomic_read(&ctx->mmap_changing))) - ret = move_pages(ctx, mm, uffdio_move.dst, uffdio_move.src, - uffdio_move.len, uffdio_move.mode); - else - ret = -EAGAIN; - up_read(&ctx->map_changing_lock); - mmap_read_unlock(mm); + ret = move_pages(ctx, uffdio_move.dst, uffdio_move.src, + uffdio_move.len, uffdio_move.mode); mmput(mm); } else { return -ESRCH; diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 3210c3552976..05d59f74fc88 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -138,9 +138,8 @@ extern long uffd_wp_range(struct vm_area_struct *vma, /* move_pages */ void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2); void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2); -ssize_t move_pages(struct userfaultfd_ctx *ctx, struct mm_struct *mm, - unsigned long dst_start, unsigned long src_start, - unsigned long len, __u64 flags); +ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start, + unsigned long src_start, unsigned long len, __u64 flags); int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval, struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 6e2ca04ab04d..d55bf18b80db 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -19,20 +19,39 @@ #include #include "internal.h" -static __always_inline -struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, - unsigned long dst_start, - unsigned long len) +void unpin_vma(struct mm_struct *mm, struct vm_area_struct *vma, bool *mmap_locked) +{ + BUG_ON(!vma && !*mmap_locked); + + if (*mmap_locked) { + mmap_read_unlock(mm); + *mmap_locked = false; + } else + vma_end_read(vma); +} + +/* + * Search for VMA and make sure it is stable either by locking it or taking + * mmap_lock. + */ +struct vm_area_struct *find_and_pin_dst_vma(struct mm_struct *dst_mm, + unsigned long dst_start, + unsigned long len, + bool *mmap_locked) { + struct vm_area_struct *dst_vma = lock_vma_under_rcu(dst_mm, dst_start); + if (!dst_vma) { + mmap_read_lock(dst_mm); + *mmap_locked = true; + dst_vma = find_vma(dst_mm, dst_start); + } + /* * Make sure that the dst range is both valid and fully within a * single existing vma. */ - struct vm_area_struct *dst_vma; - - dst_vma = find_vma(dst_mm, dst_start); if (!range_in_vma(dst_vma, dst_start, dst_start + len)) - return NULL; + goto unpin; /* * Check the vma is registered in uffd, this is required to @@ -40,9 +59,13 @@ struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, * time. */ if (!dst_vma->vm_userfaultfd_ctx.ctx) - return NULL; + goto unpin; return dst_vma; + +unpin: + unpin_vma(dst_mm, dst_vma, mmap_locked); + return NULL; } /* Check if dst_addr is outside of file's size. Must be called with ptl held. */ @@ -350,7 +373,8 @@ static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address) #ifdef CONFIG_HUGETLB_PAGE /* * mfill_atomic processing for HUGETLB vmas. Note that this routine is - * called with mmap_lock held, it will release mmap_lock before returning. + * called with either vma-lock or mmap_lock held, it will release the lock + * before returning. */ static __always_inline ssize_t mfill_atomic_hugetlb( struct userfaultfd_ctx *ctx, @@ -358,7 +382,8 @@ static __always_inline ssize_t mfill_atomic_hugetlb( unsigned long dst_start, unsigned long src_start, unsigned long len, - uffd_flags_t flags) + uffd_flags_t flags, + bool *mmap_locked) { struct mm_struct *dst_mm = dst_vma->vm_mm; int vm_shared = dst_vma->vm_flags & VM_SHARED; @@ -380,7 +405,7 @@ static __always_inline ssize_t mfill_atomic_hugetlb( */ if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE)) { up_read(&ctx->map_changing_lock); - mmap_read_unlock(dst_mm); + unpin_vma(dst_mm, dst_vma, mmap_locked); return -EINVAL; } @@ -404,12 +429,25 @@ static __always_inline ssize_t mfill_atomic_hugetlb( */ if (!dst_vma) { err = -ENOENT; - dst_vma = find_dst_vma(dst_mm, dst_start, len); - if (!dst_vma || !is_vm_hugetlb_page(dst_vma)) - goto out_unlock; + dst_vma = find_and_pin_dst_vma(dst_mm, dst_start, + len, mmap_locked); + if (!dst_vma) + goto out; + if (!is_vm_hugetlb_page(dst_vma)) + goto out_unlock_vma; err = -EINVAL; if (vma_hpagesize != vma_kernel_pagesize(dst_vma)) + goto out_unlock_vma; + + /* + * If memory mappings are changing because of non-cooperative + * operation (e.g. mremap) running in parallel, bail out and + * request the user to retry later + */ + down_read(&ctx->map_changing_lock); + err = -EAGAIN; + if (atomic_read(&ctx->mmap_changing)) goto out_unlock; vm_shared = dst_vma->vm_flags & VM_SHARED; @@ -465,7 +503,7 @@ static __always_inline ssize_t mfill_atomic_hugetlb( if (unlikely(err == -ENOENT)) { up_read(&ctx->map_changing_lock); - mmap_read_unlock(dst_mm); + unpin_vma(dst_mm, dst_vma, mmap_locked); BUG_ON(!folio); err = copy_folio_from_user(folio, @@ -474,17 +512,6 @@ static __always_inline ssize_t mfill_atomic_hugetlb( err = -EFAULT; goto out; } - mmap_read_lock(dst_mm); - down_read(&ctx->map_changing_lock); - /* - * If memory mappings are changing because of non-cooperative - * operation (e.g. mremap) running in parallel, bail out and - * request the user to retry later - */ - if (atomic_read(ctx->mmap_changing)) { - err = -EAGAIN; - break; - } dst_vma = NULL; goto retry; @@ -505,7 +532,8 @@ static __always_inline ssize_t mfill_atomic_hugetlb( out_unlock: up_read(&ctx->map_changing_lock); - mmap_read_unlock(dst_mm); +out_unlock_vma: + unpin_vma(dst_mm, dst_vma, mmap_locked); out: if (folio) folio_put(folio); @@ -521,7 +549,8 @@ extern ssize_t mfill_atomic_hugetlb(struct userfaultfd_ctx *ctx, unsigned long dst_start, unsigned long src_start, unsigned long len, - uffd_flags_t flags); + uffd_flags_t flags, + bool *mmap_locked); #endif /* CONFIG_HUGETLB_PAGE */ static __always_inline ssize_t mfill_atomic_pte(pmd_t *dst_pmd, @@ -581,6 +610,7 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx, unsigned long src_addr, dst_addr; long copied; struct folio *folio; + bool mmap_locked = false; /* * Sanitize the command parameters: @@ -597,7 +627,14 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx, copied = 0; folio = NULL; retry: - mmap_read_lock(dst_mm); + /* + * Make sure the vma is not shared, that the dst range is + * both valid and fully within a single existing vma. + */ + err = -ENOENT; + dst_vma = find_and_pin_dst_vma(dst_mm, dst_start, len, &mmap_locked); + if (!dst_vma) + goto out; /* * If memory mappings are changing because of non-cooperative @@ -609,15 +646,6 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx, if (atomic_read(&ctx->mmap_changing)) goto out_unlock; - /* - * Make sure the vma is not shared, that the dst range is - * both valid and fully within a single existing vma. - */ - err = -ENOENT; - dst_vma = find_dst_vma(dst_mm, dst_start, len); - if (!dst_vma) - goto out_unlock; - err = -EINVAL; /* * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but @@ -638,8 +666,8 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx, * If this is a HUGETLB vma, pass off to appropriate routine */ if (is_vm_hugetlb_page(dst_vma)) - return mfill_atomic_hugetlb(ctx, dst_vma, dst_start, - src_start, len, flags); + return mfill_atomic_hugetlb(ctx, dst_vma, dst_start, src_start + len, flags, &mmap_locked); if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) goto out_unlock; @@ -699,7 +727,8 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx, void *kaddr; up_read(&ctx->map_changing_lock); - mmap_read_unlock(dst_mm); + unpin_vma(dst_mm, dst_vma, &mmap_locked); + BUG_ON(!folio); kaddr = kmap_local_folio(folio, 0); @@ -730,7 +759,7 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx, out_unlock: up_read(&ctx->map_changing_lock); - mmap_read_unlock(dst_mm); + unpin_vma(dst_mm, dst_vma, &mmap_locked); out: if (folio) folio_put(folio); @@ -1285,8 +1314,6 @@ static int validate_move_areas(struct userfaultfd_ctx *ctx, * @len: length of the virtual memory range * @mode: flags from uffdio_move.mode * - * Must be called with mmap_lock held for read. - * * move_pages() remaps arbitrary anonymous pages atomically in zero * copy. It only works on non shared anonymous pages because those can * be relocated without generating non linear anon_vmas in the rmap @@ -1353,15 +1380,16 @@ static int validate_move_areas(struct userfaultfd_ctx *ctx, * could be obtained. This is the only additional complexity added to * the rmap code to provide this anonymous page remapping functionality. */ -ssize_t move_pages(struct userfaultfd_ctx *ctx, struct mm_struct *mm, - unsigned long dst_start, unsigned long src_start, - unsigned long len, __u64 mode) +ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start, + unsigned long src_start, unsigned long len, __u64 mode) { + struct mm_struct *mm = ctx->mm; struct vm_area_struct *src_vma, *dst_vma; unsigned long src_addr, dst_addr; pmd_t *src_pmd, *dst_pmd; long err = -EINVAL; ssize_t moved = 0; + bool mmap_locked = false; /* Sanitize the command parameters. */ if (WARN_ON_ONCE(src_start & ~PAGE_MASK) || @@ -1374,28 +1402,52 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, struct mm_struct *mm, WARN_ON_ONCE(dst_start + len <= dst_start)) goto out; + dst_vma = NULL; + src_vma = lock_vma_under_rcu(mm, src_start); + if (src_vma) { + dst_vma = lock_vma_under_rcu(mm, dst_start); + if (!dst_vma) + vma_end_read(src_vma); + } + + /* If we failed to lock both VMAs, fall back to mmap_lock */ + if (!dst_vma) { + mmap_read_lock(mm); + mmap_locked = true; + src_vma = find_vma(mm, src_start); + if (!src_vma) + goto out_unlock_mmap; + dst_vma = find_vma(mm, dst_start); + if (!dst_vma) + goto out_unlock_mmap; + } + + /* Re-check after taking map_changing_lock */ + down_read(&ctx->map_changing_lock); + if (likely(atomic_read(&ctx->mmap_changing))) { + err = -EAGAIN; + goto out_unlock; + } /* * Make sure the vma is not shared, that the src and dst remap * ranges are both valid and fully within a single existing * vma. */ - src_vma = find_vma(mm, src_start); - if (!src_vma || (src_vma->vm_flags & VM_SHARED)) - goto out; + if (src_vma->vm_flags & VM_SHARED) + goto out_unlock; if (src_start < src_vma->vm_start || src_start + len > src_vma->vm_end) - goto out; + goto out_unlock; - dst_vma = find_vma(mm, dst_start); - if (!dst_vma || (dst_vma->vm_flags & VM_SHARED)) - goto out; + if (dst_vma->vm_flags & VM_SHARED) + goto out_unlock; if (dst_start < dst_vma->vm_start || dst_start + len > dst_vma->vm_end) - goto out; + goto out_unlock; err = validate_move_areas(ctx, src_vma, dst_vma); if (err) - goto out; + goto out_unlock; for (src_addr = src_start, dst_addr = dst_start; src_addr < src_start + len;) { @@ -1512,6 +1564,15 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, struct mm_struct *mm, moved += step_size; } +out_unlock: + up_read(&ctx->map_changing_lock); +out_unlock_mmap: + if (mmap_locked) + mmap_read_unlock(mm); + else { + vma_end_read(dst_vma); + vma_end_read(src_vma); + } out: VM_WARN_ON(moved < 0); VM_WARN_ON(err > 0); -- 2.43.0.429.g432eaa2c6b-goog