From patchwork Wed Mar 8 22:19:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13166509 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 1194AC6FD19 for ; Wed, 8 Mar 2023 22:19:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94FA36B0071; Wed, 8 Mar 2023 17:19:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FD746B0072; Wed, 8 Mar 2023 17:19:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C5AA6B0075; Wed, 8 Mar 2023 17:19:45 -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 6B7C56B0071 for ; Wed, 8 Mar 2023 17:19:45 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3E670C0448 for ; Wed, 8 Mar 2023 22:19:45 +0000 (UTC) X-FDA: 80547149130.07.4DEA20C Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf06.hostedemail.com (Postfix) with ESMTP id 6600B180021 for ; Wed, 8 Mar 2023 22:19:43 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="lbzztN2/"; spf=pass (imf06.hostedemail.com: domain of 3_gkJZA0KCF05S9GM5NHPNN9IBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3_gkJZA0KCF05S9GM5NHPNN9IBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--axelrasmussen.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=1678313983; 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=5txiN0T0pyYIZ4jIjYUYC6xswaf7imtQj/PyicFDO6w=; b=ceF7UfSgTH7urzSGRCr3FQhmhCgGPrqyCJqeXC/ydXKMxn0c+J4XB45EF5DpVs+9pOLa97 cHZQbOvDs+fJQj+KmFg42atvkJk6PjDkPvg2tB9Ks+PRLsYPLy+Di+EZ6yGrmCeviFW3pc 4wJlGiRDcgHzG6mG9ricvT7I0+cqyfc= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="lbzztN2/"; spf=pass (imf06.hostedemail.com: domain of 3_gkJZA0KCF05S9GM5NHPNN9IBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3_gkJZA0KCF05S9GM5NHPNN9IBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678313983; a=rsa-sha256; cv=none; b=nj/cmI/PYLGYb68vsQuswsUoJBwDk7kT9iHQXhNWEHoTkW588vzIL9+qY31hCiBU7f9KIr Df4qvJOF3puE9AqslUGS+vGHYS1u5Orbytx1GWaWLdfbZKL2le5PHOvMQHDNH5oYKRs8E/ 42XP7/qzm/zeb0ALHnO4rfbn/P7FJxk= Received: by mail-yb1-f202.google.com with SMTP id e195-20020a25e7cc000000b00a1e59ba7ed9so133743ybh.11 for ; Wed, 08 Mar 2023 14:19:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678313982; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5txiN0T0pyYIZ4jIjYUYC6xswaf7imtQj/PyicFDO6w=; b=lbzztN2/q5dDGMHVRVSf807ec5rCEfvkCwizvtWnZzZ/Z4PrrdIz6Gv86YKVDkihJ/ 7npmw4xBdNwGsN/R9KaIXYutexUdxJbdKyAhPW2NpSmeTAWUA/eNYaslmBnlbKvUrraD zLW5NpuDV3wvvZzwFEzA+X2E32pzb8hXRJN41gMuVmp/brZFJY/tS5B4zfeoPCnhkpVj UBbietgCHSUx8yEip+LCESU6x06FcO74L69wHRicx0BCU0omCy6TEELrdMdc1JKTdR/3 0BwHDZBrcASUQTApgPtWm9Sgp4ny2lBTeEhu3RsjG822JWKRPbgb6bQolYovnkppq7Yw Lshw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678313982; 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=5txiN0T0pyYIZ4jIjYUYC6xswaf7imtQj/PyicFDO6w=; b=UXnsvMMgv6WjUMz+qQtp5hUoctZlYny6Gm2WUVXmitvBb1X3CBKHekkTx4v+5s5e9T iKcXdGA6U83HTO8NgmUYLMs84N7khu3virmCPXiruDowpjF4gBC5gSm+Gl15ZM687Iib 1gP0s1cTmuj4t17g5lq4+mmu03fowhp2h/LUVM7j5t4dWgxQ6312/ifh1SBWcAqKOwSV 6cSVuGUnUkMylbslzlIiwrKmFYCB3gcdUZsJWoVR7Isxqw3ALIf8w04NjTeiEiwD+eRa 3vZFCv3380k2DqvHJAbCXnlsie4pLrJXVm3rMfMiLdDuPKuZoQDTcHmbEtfnJgC25Kcl wjsg== X-Gm-Message-State: AO0yUKVs1Yxe6H1k1wrP5lBHBl110bz027h6b8P4YD6vSQBm+Dwf7bvB xIxZuh19HL5O3JG48xcwRyFzmY8xxV6GxAWHEphg X-Google-Smtp-Source: AK7set9U7RePYRpvr5M0NfAv/z+rVkS3Bu5OE4ujNbqr5wxWZuaDAbPmVcOQ61tYdUZusmoyznesUpm5/KvdmIkv4NtW X-Received: from axel.svl.corp.google.com ([2620:15c:2d4:203:96cb:1c04:7322:78a4]) (user=axelrasmussen job=sendgmr) by 2002:a81:4e8a:0:b0:52f:b69:1f7e with SMTP id c132-20020a814e8a000000b0052f0b691f7emr7ywb.26.1678313982097; Wed, 08 Mar 2023 14:19:42 -0800 (PST) Date: Wed, 8 Mar 2023 14:19:29 -0800 In-Reply-To: <20230308221932.1548827-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230308221932.1548827-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230308221932.1548827-2-axelrasmussen@google.com> Subject: [PATCH v4 1/4] mm: userfaultfd: rename functions for clarity + consistency From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Hugh Dickins , Jan Kara , "Liam R. Howlett" , Matthew Wilcox , Mike Kravetz , Mike Rapoport , Muchun Song , Nadav Amit , Peter Xu , Shuah Khan Cc: James Houghton , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6600B180021 X-Stat-Signature: bsksyiecjtbzmu61i1icmpog9grb7fka X-Rspam-User: X-HE-Tag: 1678313983-427598 X-HE-Meta: U2FsdGVkX18um/lc3gh6WAMCyg2cIPomHyR5+82wvpPKST+SJ0EbN9GFzVgCJo8kRTbKHfae5Sh8r8qYb4q4y2WP807mJa939GWbr3a72wcehQbE8KQLa1J7ZKvcW92Jn5/Pfxnw+eLhQQ/c/sx/eMHsZ3Oa3waWWVBXeCGYyvO7asmDn43IXbf/2vSk9Ti3Hg11yXjXd/Nf1MN9AUJ5pQKIRwqfytmXZwAwnoV1e0bokFN0bvt5vwEYKM0RWWx5Laj/y4g2tJ8fuVKK9OMshkki8dYH5uGcQWqqe16HpRaoSQmgXV/mM4v3s0qJlCf2DWApFtdTQ8+VQ05kqKSYSjCU87aRJA0ZW7U322O3DQx9bd1wUhcEDKU8Crypjxpzpbs2cmQOjlpTJ6muvpWL8GS3KrXjc5mJyReF3+6McxcGmgxvfHxcqgDHKGQz1uS0lE1TUp9CP/ttwcnh/WxTQRzkt6SGyP6KpAmcvK23mQMS3DfrURQGDkyAkfflc50RJe7ZgvnU/GiFpidMB5pBMremJvyouvFfuQLHcKfd1UnJPqos02Si/Enn9zhiupyzXyO4/nwhQZw4cozr3A5jtBgv9rUK9/q8+y2Y3IFdk334d1MhVhE3cWv2PeHy7A+R6YYKPS9vsfsa+ES98TYz/q6Jb2NiYo5hgQE8x7Cwetv2vdvrAeiBxKzxIgXF3rjBLMAVZYC8yeYXyRxeTN7Pg8wKsEacpfOqzEvH+PD6IkOsAwfzO257asSWEMxx0/CgIxYwr/IekN54tduE3jIN4RcqbR09/2vXtvX2t52PCPVhIS78j1XA2i2aeYMTTN9LmypBSBqZrXArhCYtcMt5M0FrGq8mm4j/8n5tSMaCRKhqXMnDipCkf7+S7EXBFJNMi7WQmCWBLqn+EIXPOvo8jqGC/auMKlKXWoRnOV7pS8GE16Fw4LxLY5Iawk9zBG6Sv2RuzMxe7c9s29dFmrE 8hDrCZZh xg6snEyi8v7FBy+/ycSzJCV3BNazqTrYQ9/ShqF2p3ZKzfbLP/i6I7QLDmYswRa2iI2U+ElJ2azrDRhJwt4zcElbihq6VE7FKfYQ8lssxOR+fuho2V0Xbvx/xBioVMOjOvPpw6CmR4gqycttQmakGap/p4ba5ezFgrh5Lf4FlVdJiSztZCuqXpgM8ZwX56GGh5tZh00rnu+2oCF13S8fdU97Lg43AEYVP8H/cC9lkwLhE6iYnGgCxTZb5own4ZfvYOLke8SS1vxHjkd+VnjckytJX2/myABNpg/xvLF2hYTGNVUPQVF6kV1+tv5OMx5F5ff1GqqdwnDMnRaGxeym8PsfCUsbtWa8ysm6WScksxblg8uB2A3yMdUNl5uppFQGt2kPqc8t39VjigGpRxpMaQ2tp4h75K1gu1oI25quJ4bv97+SLo9Y3w36bj7FSmwQfKTMVEN04HGZh6X51pUt6SPDHxQta9rDtAXx2EC9DbaJ757JU3/0KBQedUhryw9d2SgavbPBbcyXd/Ge6/OqwHaFXMgPQ8EZ8W2ANeQvUvpM7c/Oo1QIUZ7D2e+oFYrA+N1bIOxmr3+4m5CweWIG09TNIYHSK0BLexdQDEMQS9n/j725PZPk+nvp5WZencJ4Y/+z6owUeUNi48/e8O6M5EqDlJ0cLKWx08OlrVGZgJON5RuButmrH13LmhSI3mTm7POEdBFIhIQw3h6Y= 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: The basic problem is, over time we've added new userfaultfd ioctls, and we've refactored the code so functions which used to handle only one case are now re-used to deal with several cases. While this happened, we didn't bother to rename the functions. Similarly, as we added new functions, we cargo-culted pieces of the now-inconsistent naming scheme, so those functions too ended up with names that don't make a lot of sense. A key point here is, "copy" in most userfaultfd code refers specifically to UFFDIO_COPY, where we allocate a new page and copy its contents from userspace. There are many functions with "copy" in the name that don't actually do this (at least in some cases). So, rename things into a consistent scheme. The high level idea is that the call stack for userfaultfd ioctls becomes: userfaultfd_ioctl -> userfaultfd_(particular ioctl) -> mfill_atomic_(particular kind of fill operation) -> mfill_atomic /* loops over pages in range */ -> mfill_atomic_pte /* deals with single pages */ -> mfill_atomic_pte_(particular kind of fill operation) -> mfill_atomic_install_pte There are of course some special cases (shmem, hugetlb), but this is the general structure which all function names now adhere to. Acked-by: Peter Xu Signed-off-by: Axel Rasmussen Acked-by: Mike Rapoport (IBM) --- fs/userfaultfd.c | 18 +++---- include/linux/hugetlb.h | 30 +++++------ include/linux/userfaultfd_k.h | 18 +++---- mm/hugetlb.c | 20 +++---- mm/userfaultfd.c | 98 +++++++++++++++++------------------ 5 files changed, 92 insertions(+), 92 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 44d1ee429eb0..365bf00dd8dd 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1741,9 +1741,9 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx, if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP)) goto out; if (mmget_not_zero(ctx->mm)) { - ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src, - uffdio_copy.len, &ctx->mmap_changing, - uffdio_copy.mode); + ret = mfill_atomic_copy(ctx->mm, uffdio_copy.dst, uffdio_copy.src, + uffdio_copy.len, &ctx->mmap_changing, + uffdio_copy.mode); mmput(ctx->mm); } else { return -ESRCH; @@ -1793,9 +1793,9 @@ static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx, goto out; if (mmget_not_zero(ctx->mm)) { - ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start, - uffdio_zeropage.range.len, - &ctx->mmap_changing); + ret = mfill_atomic_zeropage(ctx->mm, uffdio_zeropage.range.start, + uffdio_zeropage.range.len, + &ctx->mmap_changing); mmput(ctx->mm); } else { return -ESRCH; @@ -1903,9 +1903,9 @@ static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg) goto out; if (mmget_not_zero(ctx->mm)) { - ret = mcopy_continue(ctx->mm, uffdio_continue.range.start, - uffdio_continue.range.len, - &ctx->mmap_changing); + ret = mfill_atomic_continue(ctx->mm, uffdio_continue.range.start, + uffdio_continue.range.len, + &ctx->mmap_changing); mmput(ctx->mm); } else { return -ESRCH; diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 7c977d234aba..8f0467bf1cbd 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -158,13 +158,13 @@ unsigned long hugetlb_total_pages(void); vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags); #ifdef CONFIG_USERFAULTFD -int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, pte_t *dst_pte, - struct vm_area_struct *dst_vma, - unsigned long dst_addr, - unsigned long src_addr, - enum mcopy_atomic_mode mode, - struct page **pagep, - bool wp_copy); +int hugetlb_mfill_atomic_pte(struct mm_struct *dst_mm, pte_t *dst_pte, + struct vm_area_struct *dst_vma, + unsigned long dst_addr, + unsigned long src_addr, + enum mcopy_atomic_mode mode, + struct page **pagep, + bool wp_copy); #endif /* CONFIG_USERFAULTFD */ bool hugetlb_reserve_pages(struct inode *inode, long from, long to, struct vm_area_struct *vma, @@ -393,14 +393,14 @@ static inline void hugetlb_free_pgd_range(struct mmu_gather *tlb, } #ifdef CONFIG_USERFAULTFD -static inline int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, - pte_t *dst_pte, - struct vm_area_struct *dst_vma, - unsigned long dst_addr, - unsigned long src_addr, - enum mcopy_atomic_mode mode, - struct page **pagep, - bool wp_copy) +static inline int hugetlb_mfill_atomic_pte(struct mm_struct *dst_mm, + pte_t *dst_pte, + struct vm_area_struct *dst_vma, + unsigned long dst_addr, + unsigned long src_addr, + enum mcopy_atomic_mode mode, + struct page **pagep, + bool wp_copy) { BUG(); return 0; diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 3767f18114ef..468080125612 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -61,15 +61,15 @@ extern int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, unsigned long dst_addr, struct page *page, bool newly_allocated, bool wp_copy); -extern ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start, - unsigned long src_start, unsigned long len, - atomic_t *mmap_changing, __u64 mode); -extern ssize_t mfill_zeropage(struct mm_struct *dst_mm, - unsigned long dst_start, - unsigned long len, - atomic_t *mmap_changing); -extern ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long dst_start, - unsigned long len, atomic_t *mmap_changing); +extern ssize_t mfill_atomic_copy(struct mm_struct *dst_mm, unsigned long dst_start, + unsigned long src_start, unsigned long len, + atomic_t *mmap_changing, __u64 mode); +extern ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, + unsigned long dst_start, + unsigned long len, + atomic_t *mmap_changing); +extern ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long dst_start, + unsigned long len, atomic_t *mmap_changing); extern int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, unsigned long len, bool enable_wp, atomic_t *mmap_changing); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 07abcb6eb203..4c9276549394 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6154,17 +6154,17 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, #ifdef CONFIG_USERFAULTFD /* - * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with - * modifications for huge pages. + * Used by userfaultfd UFFDIO_* ioctls. Based on userfaultfd's mfill_atomic_pte + * with modifications for hugetlb pages. */ -int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, - pte_t *dst_pte, - struct vm_area_struct *dst_vma, - unsigned long dst_addr, - unsigned long src_addr, - enum mcopy_atomic_mode mode, - struct page **pagep, - bool wp_copy) +int hugetlb_mfill_atomic_pte(struct mm_struct *dst_mm, + pte_t *dst_pte, + struct vm_area_struct *dst_vma, + unsigned long dst_addr, + unsigned long src_addr, + enum mcopy_atomic_mode mode, + struct page **pagep, + bool wp_copy) { bool is_continue = (mode == MCOPY_ATOMIC_CONTINUE); struct hstate *h = hstate_vma(dst_vma); diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 53c3d916ff66..84db5b2fad3a 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -127,13 +127,13 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, return ret; } -static int mcopy_atomic_pte(struct mm_struct *dst_mm, - pmd_t *dst_pmd, - struct vm_area_struct *dst_vma, - unsigned long dst_addr, - unsigned long src_addr, - struct page **pagep, - bool wp_copy) +static int mfill_atomic_pte_copy(struct mm_struct *dst_mm, + pmd_t *dst_pmd, + struct vm_area_struct *dst_vma, + unsigned long dst_addr, + unsigned long src_addr, + struct page **pagep, + bool wp_copy) { void *page_kaddr; int ret; @@ -204,10 +204,10 @@ static int mcopy_atomic_pte(struct mm_struct *dst_mm, goto out; } -static int mfill_zeropage_pte(struct mm_struct *dst_mm, - pmd_t *dst_pmd, - struct vm_area_struct *dst_vma, - unsigned long dst_addr) +static int mfill_atomic_pte_zeropage(struct mm_struct *dst_mm, + pmd_t *dst_pmd, + struct vm_area_struct *dst_vma, + unsigned long dst_addr) { pte_t _dst_pte, *dst_pte; spinlock_t *ptl; @@ -240,11 +240,11 @@ static int mfill_zeropage_pte(struct mm_struct *dst_mm, } /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */ -static int mcontinue_atomic_pte(struct mm_struct *dst_mm, - pmd_t *dst_pmd, - struct vm_area_struct *dst_vma, - unsigned long dst_addr, - bool wp_copy) +static int mfill_atomic_pte_continue(struct mm_struct *dst_mm, + pmd_t *dst_pmd, + struct vm_area_struct *dst_vma, + unsigned long dst_addr, + bool wp_copy) { struct inode *inode = file_inode(dst_vma->vm_file); pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); @@ -307,10 +307,10 @@ static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address) #ifdef CONFIG_HUGETLB_PAGE /* - * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is + * mfill_atomic processing for HUGETLB vmas. Note that this routine is * called with mmap_lock held, it will release mmap_lock before returning. */ -static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, +static __always_inline ssize_t mfill_atomic_hugetlb(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, unsigned long dst_start, unsigned long src_start, @@ -411,7 +411,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, goto out_unlock; } - err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma, + err = hugetlb_mfill_atomic_pte(dst_mm, dst_pte, dst_vma, dst_addr, src_addr, mode, &page, wp_copy); @@ -463,7 +463,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, } #else /* !CONFIG_HUGETLB_PAGE */ /* fail at build time if gcc attempts to use this */ -extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, +extern ssize_t mfill_atomic_hugetlb(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, unsigned long dst_start, unsigned long src_start, @@ -484,8 +484,8 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, ssize_t err; if (mode == MCOPY_ATOMIC_CONTINUE) { - return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, - wp_copy); + return mfill_atomic_pte_continue(dst_mm, dst_pmd, dst_vma, + dst_addr, wp_copy); } /* @@ -500,11 +500,11 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, */ if (!(dst_vma->vm_flags & VM_SHARED)) { if (mode == MCOPY_ATOMIC_NORMAL) - err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma, - dst_addr, src_addr, page, - wp_copy); + err = mfill_atomic_pte_copy(dst_mm, dst_pmd, dst_vma, + dst_addr, src_addr, page, + wp_copy); else - err = mfill_zeropage_pte(dst_mm, dst_pmd, + err = mfill_atomic_pte_zeropage(dst_mm, dst_pmd, dst_vma, dst_addr); } else { err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, @@ -516,13 +516,13 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, return err; } -static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, - unsigned long dst_start, - unsigned long src_start, - unsigned long len, - enum mcopy_atomic_mode mcopy_mode, - atomic_t *mmap_changing, - __u64 mode) +static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, + unsigned long dst_start, + unsigned long src_start, + unsigned long len, + enum mcopy_atomic_mode mcopy_mode, + atomic_t *mmap_changing, + __u64 mode) { struct vm_area_struct *dst_vma; ssize_t err; @@ -588,9 +588,9 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, * If this is a HUGETLB vma, pass off to appropriate routine */ if (is_vm_hugetlb_page(dst_vma)) - return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start, - src_start, len, mcopy_mode, - wp_copy); + return mfill_atomic_hugetlb(dst_mm, dst_vma, dst_start, + src_start, len, mcopy_mode, + wp_copy); if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) goto out_unlock; @@ -688,26 +688,26 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, return copied ? copied : err; } -ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start, - unsigned long src_start, unsigned long len, - atomic_t *mmap_changing, __u64 mode) +ssize_t mfill_atomic_copy(struct mm_struct *dst_mm, unsigned long dst_start, + unsigned long src_start, unsigned long len, + atomic_t *mmap_changing, __u64 mode) { - return __mcopy_atomic(dst_mm, dst_start, src_start, len, - MCOPY_ATOMIC_NORMAL, mmap_changing, mode); + return mfill_atomic(dst_mm, dst_start, src_start, len, + MCOPY_ATOMIC_NORMAL, mmap_changing, mode); } -ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start, - unsigned long len, atomic_t *mmap_changing) +ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, unsigned long start, + unsigned long len, atomic_t *mmap_changing) { - return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE, - mmap_changing, 0); + return mfill_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE, + mmap_changing, 0); } -ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start, - unsigned long len, atomic_t *mmap_changing) +ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long start, + unsigned long len, atomic_t *mmap_changing) { - return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE, - mmap_changing, 0); + return mfill_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE, + mmap_changing, 0); } long uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, From patchwork Wed Mar 8 22:19:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13166510 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 6D674C742A7 for ; Wed, 8 Mar 2023 22:19:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EF1D0280002; Wed, 8 Mar 2023 17:19:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EA1A56B0075; Wed, 8 Mar 2023 17:19:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D730F280002; Wed, 8 Mar 2023 17:19:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C307E6B0072 for ; Wed, 8 Mar 2023 17:19:47 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 88A671A1071 for ; Wed, 8 Mar 2023 22:19:47 +0000 (UTC) X-FDA: 80547149214.18.DA80FC0 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf27.hostedemail.com (Postfix) with ESMTP id A409340004 for ; Wed, 8 Mar 2023 22:19:45 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="k5wkO/Kb"; spf=pass (imf27.hostedemail.com: domain of 3AAoJZA0KCF87UBIO7PJRPPBKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3AAoJZA0KCF87UBIO7PJRPPBKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--axelrasmussen.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=1678313985; 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=3qfduLMYpbX/eaIxmSwqctkndy7m9wVy8PCWJ7GfFGI=; b=nTKy8JGyMuFtxzczzFBnQEJd3NQfLSKcyLuFURc3GButOIyuL2+I0VDXMSPAn//O/fpSdo /TYXVFpUZv5VAKpFkjpTOW9Vtr3xxKGFtDw2/msGJbNsrZlioesZqfXk2A5eVS99YnYafe ZlWKCb+MRp29LCkKu6jsga4VgG8KKFg= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="k5wkO/Kb"; spf=pass (imf27.hostedemail.com: domain of 3AAoJZA0KCF87UBIO7PJRPPBKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3AAoJZA0KCF87UBIO7PJRPPBKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678313985; a=rsa-sha256; cv=none; b=LS+CxPk6QCVMSQD896vBoHafrH1hRiW8kfJglQ5Da5V8r1KQSfd4GG7pcNWURZfBovMoWA qqBIRKIKMNlMcxkiNht7S6GG6XIJadYUntgn1ZJgii+wOheK+0DEWR0KFRW+knDlAamm8g ZuwLYqDwoxfGey8uROq87fg6cZdL+dE= Received: by mail-yb1-f201.google.com with SMTP id a137-20020a25ca8f000000b0091b90b20cd9so151946ybg.6 for ; Wed, 08 Mar 2023 14:19:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678313984; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3qfduLMYpbX/eaIxmSwqctkndy7m9wVy8PCWJ7GfFGI=; b=k5wkO/KbrcGkUo25W5QN4LLfIuQXvS4nk4V7ivGZu4a+ARwzHF8LYZN+MXIvZZfzvD 8PpS7Y3UtSlgTY2kOJHGU7UZPPk2UR+HnRZ3GucaS2hJ1DTwe6fAggGkwIijDIbuZwjR UoXgdH6BBQXDLVoMNI6Qi0NoatB27T1PEu6JuBYHJNzuDe0cC2DW+c0VsLc3XQkM3WKV G/AydUC2vCvEiAl9rGj5NAm3aPG8UO/Oc1kY94iN3o+2dI1lufYZBKbPsAQX0YMUS62I PHZgAgCwW1+rze6bkP1nCv1GL4GiifUQogx/i21ma41dJbc6Tr8UbQne3+GHUcF+F3/X jd6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678313984; 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=3qfduLMYpbX/eaIxmSwqctkndy7m9wVy8PCWJ7GfFGI=; b=7ucCOQWfubyuZ/+VyVBHETHFLXsfYAiOPNS5fRXNZdmFX9x00q2XnZkv9xHqlCIV2C LHFu/eFN9+hupmTOV/ICE8E5fNsz8sMbru84oLa4KnNiRCARlq4zTOm57uTnroeibXMQ 9IgBr6Iy/Zqx33ZWEhxvzVX3jeP/uG6FpBGrDdsqaLFcFL8yjjr+iNFezI3PVKGgDLpf MFbMGm5RNZdXFvTbcuMNA9TDKLWloPerOJXc8FtcW1Ju3zlnfiHEohlkVgbFIzu5GxWz oED9BaUeL1MxRCzw3ghyxOQFKBxASwV/PuZ6ZRcWbNuY1OlJni8j0yw7nxuCsZLWr+4Q VwEA== X-Gm-Message-State: AO0yUKVGeImQSQABdIQFIuv04ij7g8H1oNbaMq43eduxKSFAJ0xb7pWK ZSuGyBa5oJAr7sXkQeakzkq3rihZzmB7DOoDix5x X-Google-Smtp-Source: AK7set8ENvk9aqnhQf9+1ATRq7bfy/EuaPhVEUgvUxOZRsyPIYfoGwwgoRtTIEhiUpIMpBy1g+alYPUzkKhDDmmxgvaF X-Received: from axel.svl.corp.google.com ([2620:15c:2d4:203:96cb:1c04:7322:78a4]) (user=axelrasmussen job=sendgmr) by 2002:a81:a1ca:0:b0:527:9d23:f6d7 with SMTP id y193-20020a81a1ca000000b005279d23f6d7mr9ywg.48.1678313984535; Wed, 08 Mar 2023 14:19:44 -0800 (PST) Date: Wed, 8 Mar 2023 14:19:30 -0800 In-Reply-To: <20230308221932.1548827-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230308221932.1548827-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230308221932.1548827-3-axelrasmussen@google.com> Subject: [PATCH v4 2/4] mm: userfaultfd: don't pass around both mm and vma From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Hugh Dickins , Jan Kara , "Liam R. Howlett" , Matthew Wilcox , Mike Kravetz , Mike Rapoport , Muchun Song , Nadav Amit , Peter Xu , Shuah Khan Cc: James Houghton , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A409340004 X-Rspam-User: X-Stat-Signature: iroso6ii7gw6edouz84fnuhbtm7q6bt4 X-HE-Tag: 1678313985-12976 X-HE-Meta: U2FsdGVkX1+VfI6L+tvPsgjuUGzma30ppJ2+rmryNoQxoY6dlOBQ5/VbnEYmHrlPl+ne2GQ5yNozLc7+O2vKacNMlnKaK3eJLQND70Uuq7Rdb1+O4cUPis1N58bbq/wjrWyvm+oIuQw5QbzsaAH9wfRS5QawENmllj60AwdLu8M2j3of0Co/LHrFzwIhJgAj1vN4tz38HERIjtKQ8pjAWetEFMO8vR18oSJ/8j0fAVP6qc7uFE3V5kElGJ35zZ+wy4aGPJSWCk9V0xEa0kU3AP4MPQmbn4bjF0tiB8Tz6FlNTXpuxVRCjvoaOhlXzclkM55J4nxbGPijdeej0Mew5dgnwx1zjBCsIheOvJzionpp7r0YSyBNXC0jZauSGNquW40/4QpqXypo5eWk8FmDBJrA7jDPERDWyUkyRQQ0pVXYqBa4M/m4ca4uJxZQW/NU9Sxaffa/X27MBI7sgYxwRwz1V6OuBtA/edi58g7bxoXASyLSc5qpxOKOKxcIrnycybMpF8G6zm5+PLPVuYzIMWND9i3JfSX4ynXh/ea+SGTjYtCndBgJssYaHFvpk2yNkJl6zU1qID0Y1TGTDjHX3DnUusZBlDq0+jpGN9wIRTViHN0zPlvbw+v0GM4yeYNYeaxGeFpYtvTcyEDO+zDcOVY8Uvf1SomPFUjf3vMY6fz/Bn9uiPlowsBM7yVSvrAyMsvEdSXrqYDvPoAac4Kt2THkjeOAxVeLgS+ik9dfhWud8vWzsOMqx+sFzDpcVWS53lyP6fMvLNOKC6CBeZQgPZochKqUO4DR2iZXajNMtMqi5qT+O5jmqaVfc3ablPT+ET4Dp3pgPUgyX68VbFptO5Ekat1GfZrM/kvRJklDshGdzTOiHvPqW47HrY2fqa4D0JiOIjBRrk1Z8fALn7Nlx6afC3r3W8ChuX+L7vxnqdE0mHSqryQFe9thZitiTTxHMew14sX5lQK5CsYM8zm s2g4feWW IpaC/pEoR8RMBmGso2Ofp7UJIlNIH2i/6QhvLDO6uHLN4dqBkGTeBOA7WHFx6t/ZRsQtkbbBeDq8fv6lcaLK8gPFtO7beC1tSbcP+lBI4I4y8Y1ATJQTlHDhOOzuSxXz2OXq1TirWby/2/Q1IroTWQU/zz2yioKnFFJH9AXqOhVjsMIR6TR+tnCsXRM1IVMVqYs4qXpg9Bqr0hPRxi0TpiCGvuZ2Ixzm6jHqSsF3a3vc7/QU3BITgSxCRp6QcfdGDoIFALM+htODHBU+d6PqbLL+/A0lC6UWhTl7avXKkjc8gec7EB2wHhdGXpim+//+pP63vWEUG6q1J69mfSViB1Lyf/RT1L8ppHLnMIivPPQ796NPHrlM7Kb9gI0kgIXZy94RIJ8Vx+Y7SqHmgV3BKwicDYymnrlmHMAZm7U1uto+N2IOCHnCl9GeURLpFec7qRn1S/rx90SbDmmaN+ejUW7B0rZQf+Ean4qrAjvjjW4wVDiVnwLbYWQ+GmCukaGnQtEMyy7ddfRIG6CMAY0JhWCBr6fKti2mDKhp9IA081hemdHT0g0qwUyV71k6/QWwLsFgrPb8IvecMTxMv1fbNRqLxKYVI5mWs0rosW7hr290qW2tRJZQe+2sNLKeIphXWl99klcGxZleVB6Q4yCo9X62GyhMeDDp/fB18q/C9rk2TXRISYgZTPsqPsURgG8iLIy9I/TTrP9N5xCI= 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: Quite a few userfaultfd functions took both mm and vma pointers as arguments. Since the mm is trivially accessible via vma->vm_mm, there's no reason to pass both; it just needlessly extends the already long argument list. Get rid of the mm pointer, where possible, to shorten the argument list. Acked-by: Peter Xu Signed-off-by: Axel Rasmussen Acked-by: Mike Rapoport (IBM) --- fs/userfaultfd.c | 2 +- include/linux/hugetlb.h | 5 ++- include/linux/shmem_fs.h | 4 +-- include/linux/userfaultfd_k.h | 4 +-- mm/hugetlb.c | 4 +-- mm/shmem.c | 7 ++-- mm/userfaultfd.c | 61 +++++++++++++++++------------------ 7 files changed, 41 insertions(+), 46 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 365bf00dd8dd..84d5d402214a 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1629,7 +1629,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, /* Reset ptes for the whole vma range if wr-protected */ if (userfaultfd_wp(vma)) - uffd_wp_range(mm, vma, start, vma_end - start, false); + uffd_wp_range(vma, start, vma_end - start, false); new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS; prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags, diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 8f0467bf1cbd..8b9325f77ac3 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -158,7 +158,7 @@ unsigned long hugetlb_total_pages(void); vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags); #ifdef CONFIG_USERFAULTFD -int hugetlb_mfill_atomic_pte(struct mm_struct *dst_mm, pte_t *dst_pte, +int hugetlb_mfill_atomic_pte(pte_t *dst_pte, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, @@ -393,8 +393,7 @@ static inline void hugetlb_free_pgd_range(struct mmu_gather *tlb, } #ifdef CONFIG_USERFAULTFD -static inline int hugetlb_mfill_atomic_pte(struct mm_struct *dst_mm, - pte_t *dst_pte, +static inline int hugetlb_mfill_atomic_pte(pte_t *dst_pte, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 103d1000a5a2..b82916c25e61 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -151,14 +151,14 @@ extern void shmem_uncharge(struct inode *inode, long pages); #ifdef CONFIG_USERFAULTFD #ifdef CONFIG_SHMEM -extern int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, +extern int shmem_mfill_atomic_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, bool zeropage, bool wp_copy, struct page **pagep); #else /* !CONFIG_SHMEM */ -#define shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, \ +#define shmem_mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, \ src_addr, zeropage, wp_copy, pagep) ({ BUG(); 0; }) #endif /* CONFIG_SHMEM */ #endif /* CONFIG_USERFAULTFD */ diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 468080125612..ba79e296fcc7 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -56,7 +56,7 @@ enum mcopy_atomic_mode { MCOPY_ATOMIC_CONTINUE, }; -extern int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, +extern int mfill_atomic_install_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, struct page *page, bool newly_allocated, bool wp_copy); @@ -73,7 +73,7 @@ extern ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long dst extern int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, unsigned long len, bool enable_wp, atomic_t *mmap_changing); -extern long uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *vma, +extern long uffd_wp_range(struct vm_area_struct *vma, unsigned long start, unsigned long len, bool enable_wp); /* mm helpers */ diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 4c9276549394..fe043034ab46 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6157,8 +6157,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, * Used by userfaultfd UFFDIO_* ioctls. Based on userfaultfd's mfill_atomic_pte * with modifications for hugetlb pages. */ -int hugetlb_mfill_atomic_pte(struct mm_struct *dst_mm, - pte_t *dst_pte, +int hugetlb_mfill_atomic_pte(pte_t *dst_pte, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, @@ -6166,6 +6165,7 @@ int hugetlb_mfill_atomic_pte(struct mm_struct *dst_mm, struct page **pagep, bool wp_copy) { + struct mm_struct *dst_mm = dst_vma->vm_mm; bool is_continue = (mode == MCOPY_ATOMIC_CONTINUE); struct hstate *h = hstate_vma(dst_vma); struct address_space *mapping = dst_vma->vm_file->f_mapping; diff --git a/mm/shmem.c b/mm/shmem.c index 448f393d8ab2..1d751b6cf1ac 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2415,8 +2415,7 @@ static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block } #ifdef CONFIG_USERFAULTFD -int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, - pmd_t *dst_pmd, +int shmem_mfill_atomic_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, @@ -2506,11 +2505,11 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, goto out_release; ret = shmem_add_to_page_cache(folio, mapping, pgoff, NULL, - gfp & GFP_RECLAIM_MASK, dst_mm); + gfp & GFP_RECLAIM_MASK, dst_vma->vm_mm); if (ret) goto out_release; - ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, + ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, &folio->page, true, wp_copy); if (ret) goto out_delete_from_cache; diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 84db5b2fad3a..4fc373476739 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -55,12 +55,13 @@ struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem * and anon, and for both shared and private VMAs. */ -int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, +int mfill_atomic_install_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, struct page *page, bool newly_allocated, bool wp_copy) { int ret; + struct mm_struct *dst_mm = dst_vma->vm_mm; pte_t _dst_pte, *dst_pte; bool writable = dst_vma->vm_flags & VM_WRITE; bool vm_shared = dst_vma->vm_flags & VM_SHARED; @@ -127,8 +128,7 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, return ret; } -static int mfill_atomic_pte_copy(struct mm_struct *dst_mm, - pmd_t *dst_pmd, +static int mfill_atomic_pte_copy(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, @@ -190,10 +190,10 @@ static int mfill_atomic_pte_copy(struct mm_struct *dst_mm, __SetPageUptodate(page); ret = -ENOMEM; - if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL)) + if (mem_cgroup_charge(page_folio(page), dst_vma->vm_mm, GFP_KERNEL)) goto out_release; - ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, + ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, page, true, wp_copy); if (ret) goto out_release; @@ -204,8 +204,7 @@ static int mfill_atomic_pte_copy(struct mm_struct *dst_mm, goto out; } -static int mfill_atomic_pte_zeropage(struct mm_struct *dst_mm, - pmd_t *dst_pmd, +static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr) { @@ -217,7 +216,7 @@ static int mfill_atomic_pte_zeropage(struct mm_struct *dst_mm, _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr), dst_vma->vm_page_prot)); - dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); + dst_pte = pte_offset_map_lock(dst_vma->vm_mm, dst_pmd, dst_addr, &ptl); if (dst_vma->vm_file) { /* the shmem MAP_PRIVATE case requires checking the i_size */ inode = dst_vma->vm_file->f_inode; @@ -230,7 +229,7 @@ static int mfill_atomic_pte_zeropage(struct mm_struct *dst_mm, ret = -EEXIST; if (!pte_none(*dst_pte)) goto out_unlock; - set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); + set_pte_at(dst_vma->vm_mm, dst_addr, dst_pte, _dst_pte); /* No need to invalidate - it was non-present before */ update_mmu_cache(dst_vma, dst_addr, dst_pte); ret = 0; @@ -240,8 +239,7 @@ static int mfill_atomic_pte_zeropage(struct mm_struct *dst_mm, } /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */ -static int mfill_atomic_pte_continue(struct mm_struct *dst_mm, - pmd_t *dst_pmd, +static int mfill_atomic_pte_continue(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, bool wp_copy) @@ -269,7 +267,7 @@ static int mfill_atomic_pte_continue(struct mm_struct *dst_mm, goto out_release; } - ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, + ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, page, false, wp_copy); if (ret) goto out_release; @@ -310,7 +308,7 @@ static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address) * mfill_atomic processing for HUGETLB vmas. Note that this routine is * called with mmap_lock held, it will release mmap_lock before returning. */ -static __always_inline ssize_t mfill_atomic_hugetlb(struct mm_struct *dst_mm, +static __always_inline ssize_t mfill_atomic_hugetlb( struct vm_area_struct *dst_vma, unsigned long dst_start, unsigned long src_start, @@ -318,6 +316,7 @@ static __always_inline ssize_t mfill_atomic_hugetlb(struct mm_struct *dst_mm, enum mcopy_atomic_mode mode, bool wp_copy) { + struct mm_struct *dst_mm = dst_vma->vm_mm; int vm_shared = dst_vma->vm_flags & VM_SHARED; ssize_t err; pte_t *dst_pte; @@ -411,7 +410,7 @@ static __always_inline ssize_t mfill_atomic_hugetlb(struct mm_struct *dst_mm, goto out_unlock; } - err = hugetlb_mfill_atomic_pte(dst_mm, dst_pte, dst_vma, + err = hugetlb_mfill_atomic_pte(dst_pte, dst_vma, dst_addr, src_addr, mode, &page, wp_copy); @@ -463,17 +462,15 @@ static __always_inline ssize_t mfill_atomic_hugetlb(struct mm_struct *dst_mm, } #else /* !CONFIG_HUGETLB_PAGE */ /* fail at build time if gcc attempts to use this */ -extern ssize_t mfill_atomic_hugetlb(struct mm_struct *dst_mm, - struct vm_area_struct *dst_vma, - unsigned long dst_start, - unsigned long src_start, - unsigned long len, - enum mcopy_atomic_mode mode, - bool wp_copy); +extern ssize_t mfill_atomic_hugetlb(struct vm_area_struct *dst_vma, + unsigned long dst_start, + unsigned long src_start, + unsigned long len, + enum mcopy_atomic_mode mode, + bool wp_copy); #endif /* CONFIG_HUGETLB_PAGE */ -static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, - pmd_t *dst_pmd, +static __always_inline ssize_t mfill_atomic_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, @@ -484,7 +481,7 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, ssize_t err; if (mode == MCOPY_ATOMIC_CONTINUE) { - return mfill_atomic_pte_continue(dst_mm, dst_pmd, dst_vma, + return mfill_atomic_pte_continue(dst_pmd, dst_vma, dst_addr, wp_copy); } @@ -500,14 +497,14 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, */ if (!(dst_vma->vm_flags & VM_SHARED)) { if (mode == MCOPY_ATOMIC_NORMAL) - err = mfill_atomic_pte_copy(dst_mm, dst_pmd, dst_vma, + err = mfill_atomic_pte_copy(dst_pmd, dst_vma, dst_addr, src_addr, page, wp_copy); else - err = mfill_atomic_pte_zeropage(dst_mm, dst_pmd, + err = mfill_atomic_pte_zeropage(dst_pmd, dst_vma, dst_addr); } else { - err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, + err = shmem_mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, src_addr, mode != MCOPY_ATOMIC_NORMAL, wp_copy, page); @@ -588,7 +585,7 @@ static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, * If this is a HUGETLB vma, pass off to appropriate routine */ if (is_vm_hugetlb_page(dst_vma)) - return mfill_atomic_hugetlb(dst_mm, dst_vma, dst_start, + return mfill_atomic_hugetlb(dst_vma, dst_start, src_start, len, mcopy_mode, wp_copy); @@ -641,7 +638,7 @@ static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, BUG_ON(pmd_none(*dst_pmd)); BUG_ON(pmd_trans_huge(*dst_pmd)); - err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, + err = mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, src_addr, &page, mcopy_mode, wp_copy); cond_resched(); @@ -710,7 +707,7 @@ ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long start, mmap_changing, 0); } -long uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, +long uffd_wp_range(struct vm_area_struct *dst_vma, unsigned long start, unsigned long len, bool enable_wp) { unsigned int mm_cp_flags; @@ -730,7 +727,7 @@ long uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, */ if (!enable_wp && vma_wants_manual_pte_write_upgrade(dst_vma)) mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE; - tlb_gather_mmu(&tlb, dst_mm); + tlb_gather_mmu(&tlb, dst_vma->vm_mm); ret = change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags); tlb_finish_mmu(&tlb); @@ -782,7 +779,7 @@ int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, goto out_unlock; } - err = uffd_wp_range(dst_mm, dst_vma, start, len, enable_wp); + err = uffd_wp_range(dst_vma, start, len, enable_wp); /* Return 0 on success, <0 on failures */ if (err > 0) From patchwork Wed Mar 8 22:19:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13166511 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 411A4C64EC4 for ; Wed, 8 Mar 2023 22:19:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D1EB2280003; Wed, 8 Mar 2023 17:19:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CA5DD6B0075; Wed, 8 Mar 2023 17:19:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAC30280003; Wed, 8 Mar 2023 17:19:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 962016B0072 for ; Wed, 8 Mar 2023 17:19:49 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6B3AFC0F84 for ; Wed, 8 Mar 2023 22:19:49 +0000 (UTC) X-FDA: 80547149298.01.F710F2B Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf29.hostedemail.com (Postfix) with ESMTP id 924CC120019 for ; Wed, 8 Mar 2023 22:19:47 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Yz9Wf7aQ; spf=pass (imf29.hostedemail.com: domain of 3AgoJZA0KCGE9WDKQ9RLTRRDMFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3AgoJZA0KCGE9WDKQ9RLTRRDMFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--axelrasmussen.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=1678313987; 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=LvHQfBSgJpk2leJg2lbv23wkIb9ch0JlJ29o+UkLqLA=; b=GXBPsaIXSFq7eVp1nyL3OGEGIJzkI1bduJ2VUjuNtmPJTfffzSvPI6jvFE0128gVi3vtsA F2qPQEEway7wks3b5IoMvTd5Oo9NgE9pqdxnkHAC1GN7XEvE0/q5hi5ZrMWtr2p/2xEJIP C7UZUg66/AJiti4q8RKCeG5qyrwTv00= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Yz9Wf7aQ; spf=pass (imf29.hostedemail.com: domain of 3AgoJZA0KCGE9WDKQ9RLTRRDMFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3AgoJZA0KCGE9WDKQ9RLTRRDMFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678313987; a=rsa-sha256; cv=none; b=XArjqjPWCUtgD68G/rPXk//KKhzuyLnpRAFCOmoOifguaienDHqyeaHr0zkE9CFR8EISjo pBOcnlqPmuvYpWU3eZEIPKKZ0AXB8whe2rff8psUMt9X3ZCjbUgY2t6WTuf0Xs/07QTdvq odaQ5XhysAX+Wf08PuXRYDubF6kGdAo= Received: by mail-yb1-f202.google.com with SMTP id 204-20020a2514d5000000b00a3637aea9e1so113924ybu.17 for ; Wed, 08 Mar 2023 14:19:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678313987; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LvHQfBSgJpk2leJg2lbv23wkIb9ch0JlJ29o+UkLqLA=; b=Yz9Wf7aQWqLQu1CN+0EwInCRKNr1DZvHqklRMi8tSv9Sr1rghhBdpEihEd63RbUoa0 2NRJujBJLTC3SpjGMRlifUR1fwSqU6aR7EVWjIhNnC7xz6dv0ZVz/cEGUem3moSNsQBs XfRbhbkuDe372oOYOP/U6vAvu4Wup3jUKLucRHtNKiunVDgWUp7QfzcB8SW7WoL7L0um Lp9LZwsGK/jZR42Tx3qTYyWARChru2UYNB2Hhq1LY8YGVuN2DUuPvNBD5GhwEN8+BBJr cl6f7pex42DgsZtrrBikRfz7crFb0JCd4BK26FQYBBq+XArctEwmIDjuZ9GLW2KyzIRr A0vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678313987; 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=LvHQfBSgJpk2leJg2lbv23wkIb9ch0JlJ29o+UkLqLA=; b=aOVR4j56LPgL7mTD62idyESbFcYbUsDgM4SYecM5MgGClOBvt5WF8rMQbBQBxyXPa6 vxROJ67n9xhtMvr2KnxpMJ9WoesS769L+udG+WjgcbSx26pWJHrQVBBtEgTceqdIloqG JqgncMc0W9KMcrTV1KHnCQuUe2t+/wCgRRLgDnSL8TlHJ8UMEfkRM6VmchcN+VxW+aU7 jjsjRGtBp9vaY/ALhLnKa8EIUu4IpsCR1yy6hvqNOOkdjd/rlaxzK1PPWomy/OFBvy2s 4VIY3MM4/hynNGEcrweNmSonS3p1SD9e/MglrKno7es5GnJdhoNWGTV6N8cUao5FsuC+ juRA== X-Gm-Message-State: AO0yUKVo+cXkzEnDqoBXktw5NFgcIjYFZ0pxj5NB8s2EvoHDTULK7AlW v3+okfeXj3du37hT4ET5a5HAH6TIVIA9H1nhAiy6 X-Google-Smtp-Source: AK7set9UW1FIPyZ8r+wZxfqJFfSITGVvxyKDazvFuPddI3cchLABBfsGdj4cnjKMOvoWWBNNOwQLCA920hU23FYdRvgk X-Received: from axel.svl.corp.google.com ([2620:15c:2d4:203:96cb:1c04:7322:78a4]) (user=axelrasmussen job=sendgmr) by 2002:a05:6902:145:b0:ac2:a7a7:23c3 with SMTP id p5-20020a056902014500b00ac2a7a723c3mr7748521ybh.12.1678313986816; Wed, 08 Mar 2023 14:19:46 -0800 (PST) Date: Wed, 8 Mar 2023 14:19:31 -0800 In-Reply-To: <20230308221932.1548827-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230308221932.1548827-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230308221932.1548827-4-axelrasmussen@google.com> Subject: [PATCH v4 3/4] mm: userfaultfd: combine 'mode' and 'wp_copy' arguments From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Hugh Dickins , Jan Kara , "Liam R. Howlett" , Matthew Wilcox , Mike Kravetz , Mike Rapoport , Muchun Song , Nadav Amit , Peter Xu , Shuah Khan Cc: James Houghton , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 924CC120019 X-Stat-Signature: o4q7e6itf77snogoggu8acoqwbqtm4za X-HE-Tag: 1678313987-962940 X-HE-Meta: U2FsdGVkX1+AVjeFmHkUKldtpkwaF6K/hxNTUgwFzimTw3Xx+Jh2Ht+mgOY+YAmOXYH4CfGquc9DJaNIlTwyAAeZ5MmFrayNpyeFiea6IhYtNWfe0fuAtsvcb5/DNWHO8jfZ3wcVSOxxDNriOQxdttbueeSu/tVXvF3n6ZCYcgvmqctE5JwPQNhkzTNgRmHguycBhjUb4vGfBHJAp1g18zNGvn6TVmp+1XPP/aHW/PtURxviS+QvhOcRorTvCsPiVDFCZ/SsM7IZKS4bvYIAa0SAxdGMJ9uZMXz4pYutVV8bL6e3q8aTC3vkD9MNCuEc/bLJKk3rmQ9Wgt8s66L8KuGgVRqNWYWtjETPN2zSpHCfO+uJMSdRNhawCrHVOIbGdZoUOBEn80+dvoqCxpx0bKFR+z69Q2AOr/9ViGRBareaiimhfoifSTHYM7JmDa9KkNPLtpnBmDZi39HulshViT0hdRNuNNFdGqIzKJGIhYLyIu7OkHUbbylRhWODu1bbPJM1iIjpFbcutLwlZDen6dRRtmm/uJpmHWsQH/n3pXL00C+50kRUqsHd0x+woCG2lHRxgluiN4c8c9jT0tjAgXnR4KAV0JtX73laDWqywJQKN5aZ8IqFg8uRTKFgFVL6plaujwBWdTJ8hRBQQPXFsS2ozpC4VLqpaiEPdCrJBptBNtVUT/N54d4v9FWXyrGkejUo1Rr5Pvh3jt8kai4+Vwr+dzVU4VRGXPjDhspqvpPBEAx28Bp89/eZiiERB8r5LxPbLCieJSKKmvFzmXOj/F/7taMp3yOTjW1U2E0Mtuw3ekN2JfFpNARKXA1a0xuuuOxO9zkb9ypWpJAUUMNuWdUz2oRA+jI0Z6UaChOgFyVNjVbSd+LMDuXybDQS8GqxKRsc5BVg40GuAL+oPIJWprihr6VGndhvg/kPeaiCa6riO0B4vqlq8n4CMtumhwD/ip8YDmDZH+LSH34qPcU g3oCpxKp 7FKTiEufQgC5/9cqVVRxt8O3PuDXYwVtzh/ytNuGte6/tvlO5JprWd02077yECp8aRtUn7kgMtFP6CR7OWbZ5MujInNUlxFFYuwTz/AOJpcoSoCqdsEdJQmzTuYsZbICnK7xbxy/Rhqpo3UJBIMvdJAIxstM3VZX4jJGJSmmIszyL0zBhuI9VQY4J8KGepjk29FCOcn0EftjXtopRmf6xrRghouJIczo3dgqRucSTvGbi8CdEQsOPTCxGAXDsuyMmV+nyH+8FzAih1NeV2qAJvoVuCi1Wtm5KmGMPisWe19nN5/4jIu2fFTt+xJro/4YJmVj2+Fhivlhzghu3OFVL6UX5R6S+Mgu5G06UyNpK8kIuncVN1pVcRA8Igwos/41dtH1YPaHXEiea3CuPlf45dsLZvcX74VY+cpUaOh15XDLEYC5EPcDKKgxp2/PtdjIRnt/myGRstc4iPJo9hlxZOOk5wIaPsmGTmMC7XjMKGK0PWfglQkDwyibx5P6zbpWBe2rbfLjyj2KFrYAYyWmpoMh9Ry/w1/WJ0FylhUPI6Wq/C8fmspolaA7B71xP9iE3jE1E+tsx4XFx/o88ujeypFZkeDyziMIzIO4k4JFLzo1wKRsLNZNHy7EgDS8fu43o29n6IKevcWCSDOChgBGV2m+GEnoAJTI4KpLKYH/wF/QDMMB9xuxGCOoQe9JOeMHINEBg9686KHtOWOxy07rnZrgY6DvwBZXmhCxh4zM1E8O74ZPMH9ZGxREbOcQV4W5ryqoJcGw4hMdug3tw5LIjpuQ6CplFpSBBsNTJezlu27yEppGTg4C7pzQESQ== 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: Many userfaultfd ioctl functions take both a 'mode' and a 'wp_copy' argument. In future commits we plan to plumb the flags through to more places, so we'd be proliferating the very long argument list even further. Let's take the time to simplify the argument list. Combine the two arguments into one - and generalize, so when we add more flags in the future, it doesn't imply more function arguments. Since the modes (copy, zeropage, continue) are mutually exclusive, store them as an integer value (0, 1, 2) in the low bits. Place combine-able flag bits in the high bits. This is quite similar to an earlier patch proposed by Nadav Amit ("userfaultfd: introduce uffd_flags" [1]). The main difference is that patch only handled flags, whereas this patch *also* combines the "mode" argument into the same type to shorten the argument list. [1]: https://lore.kernel.org/all/20220619233449.181323-2-namit@vmware.com/ Acked-by: James Houghton Signed-off-by: Axel Rasmussen Acked-by: Peter Xu Acked-by: Mike Rapoport (IBM) --- fs/userfaultfd.c | 5 ++- include/linux/hugetlb.h | 10 ++--- include/linux/shmem_fs.h | 5 ++- include/linux/userfaultfd_k.h | 45 +++++++++++++-------- mm/hugetlb.c | 12 +++--- mm/shmem.c | 7 ++-- mm/userfaultfd.c | 76 ++++++++++++++++------------------- 7 files changed, 83 insertions(+), 77 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 84d5d402214a..56e54e50414e 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1714,6 +1714,7 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx, struct uffdio_copy uffdio_copy; struct uffdio_copy __user *user_uffdio_copy; struct userfaultfd_wake_range range; + uffd_flags_t flags = 0; user_uffdio_copy = (struct uffdio_copy __user *) arg; @@ -1740,10 +1741,12 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx, goto out; if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP)) goto out; + if (uffdio_copy.mode & UFFDIO_COPY_MODE_WP) + flags |= MFILL_ATOMIC_WP; if (mmget_not_zero(ctx->mm)) { ret = mfill_atomic_copy(ctx->mm, uffdio_copy.dst, uffdio_copy.src, uffdio_copy.len, &ctx->mmap_changing, - uffdio_copy.mode); + flags); mmput(ctx->mm); } else { return -ESRCH; diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 8b9325f77ac3..6270a4786584 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -162,9 +162,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - enum mcopy_atomic_mode mode, - struct page **pagep, - bool wp_copy); + uffd_flags_t flags, + struct page **pagep); #endif /* CONFIG_USERFAULTFD */ bool hugetlb_reserve_pages(struct inode *inode, long from, long to, struct vm_area_struct *vma, @@ -397,9 +396,8 @@ static inline int hugetlb_mfill_atomic_pte(pte_t *dst_pte, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - enum mcopy_atomic_mode mode, - struct page **pagep, - bool wp_copy) + uffd_flags_t flags, + struct page **pagep) { BUG(); return 0; diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index b82916c25e61..b7048bd88a8d 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -9,6 +9,7 @@ #include #include #include +#include /* inode in-kernel data */ @@ -155,11 +156,11 @@ extern int shmem_mfill_atomic_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - bool zeropage, bool wp_copy, + uffd_flags_t flags, struct page **pagep); #else /* !CONFIG_SHMEM */ #define shmem_mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, \ - src_addr, zeropage, wp_copy, pagep) ({ BUG(); 0; }) + src_addr, flags, pagep) ({ BUG(); 0; }) #endif /* CONFIG_SHMEM */ #endif /* CONFIG_USERFAULTFD */ diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index ba79e296fcc7..4d7425684171 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -40,30 +40,43 @@ extern int sysctl_unprivileged_userfaultfd; extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason); -/* - * The mode of operation for __mcopy_atomic and its helpers. - * - * This is almost an implementation detail (mcopy_atomic below doesn't take this - * as a parameter), but it's exposed here because memory-kind-specific - * implementations (e.g. hugetlbfs) need to know the mode of operation. - */ -enum mcopy_atomic_mode { - /* A normal copy_from_user into the destination range. */ - MCOPY_ATOMIC_NORMAL, - /* Don't copy; map the destination range to the zero page. */ - MCOPY_ATOMIC_ZEROPAGE, - /* Just install pte(s) with the existing page(s) in the page cache. */ - MCOPY_ATOMIC_CONTINUE, +/* A combined operation mode + behavior flags. */ +typedef unsigned int __bitwise uffd_flags_t; + +/* Mutually exclusive modes of operation. */ +enum mfill_atomic_mode { + MFILL_ATOMIC_COPY, + MFILL_ATOMIC_ZEROPAGE, + MFILL_ATOMIC_CONTINUE, + NR_MFILL_ATOMIC_MODES, }; +#define MFILL_ATOMIC_MODE_BITS (const_ilog2(NR_MFILL_ATOMIC_MODES - 1) + 1) +#define MFILL_ATOMIC_BIT(nr) BIT(MFILL_ATOMIC_MODE_BITS + (nr)) +#define MFILL_ATOMIC_FLAG(nr) ((__force uffd_flags_t) MFILL_ATOMIC_BIT(nr)) +#define MFILL_ATOMIC_MODE_MASK ((__force uffd_flags_t) (MFILL_ATOMIC_BIT(0) - 1)) + +static inline bool uffd_flags_has_mode(uffd_flags_t flags, enum mfill_atomic_mode expected) +{ + return (flags & MFILL_ATOMIC_MODE_MASK) == ((__force uffd_flags_t) expected); +} + +static inline uffd_flags_t uffd_flags_set_mode(uffd_flags_t flags, enum mfill_atomic_mode mode) +{ + return flags | ((__force uffd_flags_t) mode); +} + +/* Flags controlling behavior. */ +#define MFILL_ATOMIC_WP MFILL_ATOMIC_FLAG(0) + extern int mfill_atomic_install_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, struct page *page, - bool newly_allocated, bool wp_copy); + bool newly_allocated, uffd_flags_t flags); extern ssize_t mfill_atomic_copy(struct mm_struct *dst_mm, unsigned long dst_start, unsigned long src_start, unsigned long len, - atomic_t *mmap_changing, __u64 mode); + atomic_t *mmap_changing, uffd_flags_t flags); extern ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, unsigned long dst_start, unsigned long len, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index fe043034ab46..493406a2d61e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6161,12 +6161,12 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - enum mcopy_atomic_mode mode, - struct page **pagep, - bool wp_copy) + uffd_flags_t flags, + struct page **pagep) { struct mm_struct *dst_mm = dst_vma->vm_mm; - bool is_continue = (mode == MCOPY_ATOMIC_CONTINUE); + bool is_continue = uffd_flags_has_mode(flags, MFILL_ATOMIC_CONTINUE); + bool wp_enabled = (flags & MFILL_ATOMIC_WP); struct hstate *h = hstate_vma(dst_vma); struct address_space *mapping = dst_vma->vm_file->f_mapping; pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr); @@ -6301,7 +6301,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY * with wp flag set, don't set pte write bit. */ - if (wp_copy || (is_continue && !vm_shared)) + if (wp_enabled || (is_continue && !vm_shared)) writable = 0; else writable = dst_vma->vm_flags & VM_WRITE; @@ -6316,7 +6316,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, _dst_pte = huge_pte_mkdirty(_dst_pte); _dst_pte = pte_mkyoung(_dst_pte); - if (wp_copy) + if (wp_enabled) _dst_pte = huge_pte_mkuffd_wp(_dst_pte); set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); diff --git a/mm/shmem.c b/mm/shmem.c index 1d751b6cf1ac..7d688afb5e31 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -76,7 +76,6 @@ static struct vfsmount *shm_mnt; #include #include #include -#include #include #include @@ -2419,7 +2418,7 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - bool zeropage, bool wp_copy, + uffd_flags_t flags, struct page **pagep) { struct inode *inode = file_inode(dst_vma->vm_file); @@ -2451,7 +2450,7 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, if (!folio) goto out_unacct_blocks; - if (!zeropage) { /* COPY */ + if (uffd_flags_has_mode(flags, MFILL_ATOMIC_COPY)) { page_kaddr = kmap_local_folio(folio, 0); /* * The read mmap_lock is held here. Despite the @@ -2510,7 +2509,7 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, goto out_release; ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, - &folio->page, true, wp_copy); + &folio->page, true, flags); if (ret) goto out_delete_from_cache; diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 4fc373476739..dd807924446f 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -58,7 +58,7 @@ struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, int mfill_atomic_install_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, struct page *page, - bool newly_allocated, bool wp_copy) + bool newly_allocated, uffd_flags_t flags) { int ret; struct mm_struct *dst_mm = dst_vma->vm_mm; @@ -77,7 +77,7 @@ int mfill_atomic_install_pte(pmd_t *dst_pmd, writable = false; if (writable) _dst_pte = pte_mkwrite(_dst_pte); - if (wp_copy) + if (flags & MFILL_ATOMIC_WP) _dst_pte = pte_mkuffd_wp(_dst_pte); dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); @@ -132,8 +132,8 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - struct page **pagep, - bool wp_copy) + uffd_flags_t flags, + struct page **pagep) { void *page_kaddr; int ret; @@ -194,7 +194,7 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, goto out_release; ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, - page, true, wp_copy); + page, true, flags); if (ret) goto out_release; out: @@ -242,7 +242,7 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, static int mfill_atomic_pte_continue(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, - bool wp_copy) + uffd_flags_t flags) { struct inode *inode = file_inode(dst_vma->vm_file); pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); @@ -268,7 +268,7 @@ static int mfill_atomic_pte_continue(pmd_t *dst_pmd, } ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, - page, false, wp_copy); + page, false, flags); if (ret) goto out_release; @@ -313,8 +313,7 @@ static __always_inline ssize_t mfill_atomic_hugetlb( unsigned long dst_start, unsigned long src_start, unsigned long len, - enum mcopy_atomic_mode mode, - bool wp_copy) + uffd_flags_t flags) { struct mm_struct *dst_mm = dst_vma->vm_mm; int vm_shared = dst_vma->vm_flags & VM_SHARED; @@ -334,7 +333,7 @@ static __always_inline ssize_t mfill_atomic_hugetlb( * by THP. Since we can not reliably insert a zero page, this * feature is not supported. */ - if (mode == MCOPY_ATOMIC_ZEROPAGE) { + if (uffd_flags_has_mode(flags, MFILL_ATOMIC_ZEROPAGE)) { mmap_read_unlock(dst_mm); return -EINVAL; } @@ -402,7 +401,7 @@ static __always_inline ssize_t mfill_atomic_hugetlb( goto out_unlock; } - if (mode != MCOPY_ATOMIC_CONTINUE && + if (!uffd_flags_has_mode(flags, MFILL_ATOMIC_CONTINUE) && !huge_pte_none_mostly(huge_ptep_get(dst_pte))) { err = -EEXIST; hugetlb_vma_unlock_read(dst_vma); @@ -410,9 +409,8 @@ static __always_inline ssize_t mfill_atomic_hugetlb( goto out_unlock; } - err = hugetlb_mfill_atomic_pte(dst_pte, dst_vma, - dst_addr, src_addr, mode, &page, - wp_copy); + err = hugetlb_mfill_atomic_pte(dst_pte, dst_vma, dst_addr, + src_addr, flags, &page); hugetlb_vma_unlock_read(dst_vma); mutex_unlock(&hugetlb_fault_mutex_table[hash]); @@ -466,23 +464,21 @@ extern ssize_t mfill_atomic_hugetlb(struct vm_area_struct *dst_vma, unsigned long dst_start, unsigned long src_start, unsigned long len, - enum mcopy_atomic_mode mode, - bool wp_copy); + uffd_flags_t flags); #endif /* CONFIG_HUGETLB_PAGE */ static __always_inline ssize_t mfill_atomic_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - struct page **page, - enum mcopy_atomic_mode mode, - bool wp_copy) + uffd_flags_t flags, + struct page **pagep) { ssize_t err; - if (mode == MCOPY_ATOMIC_CONTINUE) { + if (uffd_flags_has_mode(flags, MFILL_ATOMIC_CONTINUE)) { return mfill_atomic_pte_continue(dst_pmd, dst_vma, - dst_addr, wp_copy); + dst_addr, flags); } /* @@ -496,18 +492,17 @@ static __always_inline ssize_t mfill_atomic_pte(pmd_t *dst_pmd, * and not in the radix tree. */ if (!(dst_vma->vm_flags & VM_SHARED)) { - if (mode == MCOPY_ATOMIC_NORMAL) + if (uffd_flags_has_mode(flags, MFILL_ATOMIC_COPY)) err = mfill_atomic_pte_copy(dst_pmd, dst_vma, - dst_addr, src_addr, page, - wp_copy); + dst_addr, src_addr, + flags, pagep); else err = mfill_atomic_pte_zeropage(dst_pmd, dst_vma, dst_addr); } else { err = shmem_mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, src_addr, - mode != MCOPY_ATOMIC_NORMAL, - wp_copy, page); + flags, pagep); } return err; @@ -517,9 +512,8 @@ static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, unsigned long dst_start, unsigned long src_start, unsigned long len, - enum mcopy_atomic_mode mcopy_mode, atomic_t *mmap_changing, - __u64 mode) + uffd_flags_t flags) { struct vm_area_struct *dst_vma; ssize_t err; @@ -527,7 +521,6 @@ static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, unsigned long src_addr, dst_addr; long copied; struct page *page; - bool wp_copy; /* * Sanitize the command parameters: @@ -577,8 +570,7 @@ static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, * validate 'mode' now that we know the dst_vma: don't allow * a wrprotect copy if the userfaultfd didn't register as WP. */ - wp_copy = mode & UFFDIO_COPY_MODE_WP; - if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP)) + if ((flags & MFILL_ATOMIC_WP) && !(dst_vma->vm_flags & VM_UFFD_WP)) goto out_unlock; /* @@ -586,12 +578,12 @@ static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, */ if (is_vm_hugetlb_page(dst_vma)) return mfill_atomic_hugetlb(dst_vma, dst_start, - src_start, len, mcopy_mode, - wp_copy); + src_start, len, flags); if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) goto out_unlock; - if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE) + if (!vma_is_shmem(dst_vma) && + uffd_flags_has_mode(flags, MFILL_ATOMIC_CONTINUE)) goto out_unlock; /* @@ -639,7 +631,7 @@ static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, BUG_ON(pmd_trans_huge(*dst_pmd)); err = mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, - src_addr, &page, mcopy_mode, wp_copy); + src_addr, flags, &page); cond_resched(); if (unlikely(err == -ENOENT)) { @@ -687,24 +679,24 @@ static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, ssize_t mfill_atomic_copy(struct mm_struct *dst_mm, unsigned long dst_start, unsigned long src_start, unsigned long len, - atomic_t *mmap_changing, __u64 mode) + atomic_t *mmap_changing, uffd_flags_t flags) { - return mfill_atomic(dst_mm, dst_start, src_start, len, - MCOPY_ATOMIC_NORMAL, mmap_changing, mode); + return mfill_atomic(dst_mm, dst_start, src_start, len, mmap_changing, + uffd_flags_set_mode(flags, MFILL_ATOMIC_COPY)); } ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, unsigned long start, unsigned long len, atomic_t *mmap_changing) { - return mfill_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE, - mmap_changing, 0); + return mfill_atomic(dst_mm, start, 0, len, mmap_changing, + uffd_flags_set_mode(0, MFILL_ATOMIC_ZEROPAGE)); } ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long start, unsigned long len, atomic_t *mmap_changing) { - return mfill_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE, - mmap_changing, 0); + return mfill_atomic(dst_mm, start, 0, len, mmap_changing, + uffd_flags_set_mode(0, MFILL_ATOMIC_CONTINUE)); } long uffd_wp_range(struct vm_area_struct *dst_vma, From patchwork Wed Mar 8 22:19:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13166512 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 38A70C742A7 for ; Wed, 8 Mar 2023 22:19:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA7F2280004; Wed, 8 Mar 2023 17:19:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C58B86B0075; Wed, 8 Mar 2023 17:19:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF8D1280004; Wed, 8 Mar 2023 17:19:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 988716B0072 for ; Wed, 8 Mar 2023 17:19:51 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6B32D41083 for ; Wed, 8 Mar 2023 22:19:51 +0000 (UTC) X-FDA: 80547149382.12.10FDDFD Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf07.hostedemail.com (Postfix) with ESMTP id 9884740016 for ; Wed, 8 Mar 2023 22:19:49 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=eDQwE03s; spf=pass (imf07.hostedemail.com: domain of 3BAoJZA0KCGMBYFMSBTNVTTFOHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3BAoJZA0KCGMBYFMSBTNVTTFOHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--axelrasmussen.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=1678313989; 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=YSBFx52RvXhjtM1Ms4suwV92HKajMqVhV7t5r8e9JsE=; b=ey99mM9Ojs6ITcn8zFJVv1U/bA8JR1PkM/zKuCalCghZS95dGtgF2YSmgqYbnULIo90a8v Q4vUBYf6DwPtYzfAXaEtMaRjc7MtB+LQhvl+pEjJ9NbSOCBR90KWAJ+knP5Yh+FFPUsJ0B 6AK2J7LSpUz9qR6pA1EuIghaIrix9yM= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=eDQwE03s; spf=pass (imf07.hostedemail.com: domain of 3BAoJZA0KCGMBYFMSBTNVTTFOHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3BAoJZA0KCGMBYFMSBTNVTTFOHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678313989; a=rsa-sha256; cv=none; b=xeh/jc0R4oe0XnHpmo3F/UpXNiCC9TcT80WDz5Z0Jfr1mTdiG6UkQZvFk1H+3lslK7E7Fx jN8evAI5XVpi4rDpaI44s7we+5whS/poC6m8Ew+VcxWz1Hi9RIx8P8MfKKCXgU4nX24Xq1 y1Oe8fHXZaEv4Ww5Sn49vIC4Pj9+3JU= Received: by mail-yb1-f201.google.com with SMTP id l24-20020a25b318000000b007eba3f8e3baso158512ybj.4 for ; Wed, 08 Mar 2023 14:19:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678313989; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YSBFx52RvXhjtM1Ms4suwV92HKajMqVhV7t5r8e9JsE=; b=eDQwE03ssVefKuCHB5TW/9cRNCO3i1U9pzf4Ycjg91bvG1agIDtj0QTDE2eFmvjKVx fxVClPeOZeCKq1qpEHM10j3+vzTxm44vqdZdGCiDVrHAvZXHmxYM4g8g7ee6/gIHO4RV 03sVypQgd6aKkvssDe9MhchkPxSpvN6scQQTzDvldztsTNuwXJ25oes+JdaaWvEgW/vh Ks2aJ38BOoW1g1SD7qNi3QXAOQ0/ZHcfW8uPrpQnxFE88qQ6reh6KQ7yHElvT2u7uvjc Kakf4FsYCP6sbS07URRagbKBCEzMwfQV1R7J01ZCBx0pl/NWDzpaG7SW0ucedJET5wuC 9P6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678313989; 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=YSBFx52RvXhjtM1Ms4suwV92HKajMqVhV7t5r8e9JsE=; b=K+DzSRUQ0C8pW/h3gLxIN1556mGG3v5b8diEjH9DOSUseC46chiUi3xlFJbZW3420P G+JVKKJzqvKPO2FVssnylZifwX10XRaXXooJkCXGhv/v8HYPTKfkHbLQp31lbzqjJTFE 8ZISxVFGDAxHybbT/e5dXH6booeeCRcAhAJRXnIdGjZTI4yZN4sPy01EdN2w0JaS8MEU Kmth4Vld8I5cnCAyNJvZ2dc3SivfpjrS2x/DA8hNWYXkYM64xy+TU/1qoLepuQocMqNb H7xviiwF1hboz48skwcbpflLZeXlsH2fI2QCUoQnBsTHDWy6VfHNprrWD7NuKpsuHOQ0 IqPg== X-Gm-Message-State: AO0yUKUfnXz3woRfKuCWquAFd/yg62ay43Rafe8On0tBdc/icOiSU1xC jKO3cHdSgy3RWqBgON1qLE0XFd5niocEmJL8xBI9 X-Google-Smtp-Source: AK7set/Y8V4CBFECi0ndhD/iIYsa8Ih564aUc0de91ZFzdsRCTCZqdn0UCRIo/zFP9QU38jh/ZHPKYk13T3u2uxCOIk3 X-Received: from axel.svl.corp.google.com ([2620:15c:2d4:203:96cb:1c04:7322:78a4]) (user=axelrasmussen job=sendgmr) by 2002:a81:ad5a:0:b0:536:4d58:54b2 with SMTP id l26-20020a81ad5a000000b005364d5854b2mr13032103ywk.4.1678313988783; Wed, 08 Mar 2023 14:19:48 -0800 (PST) Date: Wed, 8 Mar 2023 14:19:32 -0800 In-Reply-To: <20230308221932.1548827-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230308221932.1548827-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230308221932.1548827-5-axelrasmussen@google.com> Subject: [PATCH v4 4/4] mm: userfaultfd: add UFFDIO_CONTINUE_MODE_WP to install WP PTEs From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Hugh Dickins , Jan Kara , "Liam R. Howlett" , Matthew Wilcox , Mike Kravetz , Mike Rapoport , Muchun Song , Nadav Amit , Peter Xu , Shuah Khan Cc: James Houghton , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9884740016 X-Stat-Signature: 3n1cguczcgjce4dfbu6fw3on7gujce8r X-HE-Tag: 1678313989-694588 X-HE-Meta: U2FsdGVkX19jQVaCY+wCAMYKw2GV0xqR/qPWxFPke4AhSZ6RkACQnqLJfufPA4x9iqPmUYhovWockswfVuljzhMOncsew3eS583gkug9P3EtXlHin1rdKML41aMAO5VwpSfQ+o36WI6Ox13/UlS40wHk9r0kDCzxQ5flBkrrZNlegJJGwsqF1NY14mxeHtBeN1+AgLqpzmLkQZ/tgakR0na7A1RoX+90cIpv2iHCGu3/MYi8Q7Hbb20b7vuY+OnQgMO/fN79gj07L8FGuZZBFH4OzAyQkKxjMfgvUuBtrdG66qW/H7+MlxjKVTu2N6yASOmWL0IBaBetiW7IjSG8zWdHpTrBvg8NYUd+4PNfH9A/dXHvR0DgrLD4eXAf11chbg7Oo9VtOEjjLo5FRMr5IUmVoKw38+FyDS79Jap/4NHsZPcMdtYVsk0fxB/2ZhmfJCVdhtX5saaGcsi/Jzjf7dOq9n7PhCW9xkCmDUJ2WWTcDuymZiYjN26jUIuoQ7J8E40Q77zBu9asbis11mA6aXnJ8rO6yCxb+m5Adh8E4h7OJjlW0xb89nHml7EPJK7uZHbuFKpiaHkhzM0yI9lbD7kOfjxAdfxkmg7w4oD8/uyniiLO2OwrGLGd1KnDvdjLiLnGJI/SaUNVlzdLCRnKC4dmxgvNw9aOXZJtRA3eNVVLUW0/SBw318Dme/OmSYGewNp4mUcfqdLBGofEgDZpE9V35MCPHuLgZNxZwrAKN6/wVV9CJoc24GvmqNvCVClkPsn5PzLDa9wxMNdhV6LaeaqHrO9TPKY6Rm1BZy+TTL14rcyv4IFphpGl5VGceHCZf22AHFzncEaAQgmJO0YNsXQJ4yAwFbVWDgNwVSmSyE/uaXsV2EyUsGMVy4o3iUsj6lL6T778J49kGPHDHQUgvuWB75dDp8ebUpqy7XiE9uGMGNO9TjGrkRTzKsF6SoEIENJoRTx0FPKSCs3b7Lp o7F6o+4J hWziNrLgvU/9S10fT6WiujEWMhRr8mQzdRDre3HFru5bc8+TzQqkANS5eaP6ua9lTvvPuco505gsuZpRNB1zfRR+gvQVHpndaGo3WO0s/uH57q5DWXdhKHjtD1l5wk5wqixqN07cHSgnKcGFI91m7pj+MlF85MQLa6ihC57cfT/z22TyuyBokALMpIhM3iJsNeNZKbG6nHXhlNf0UmqOu4MELTJmqfVpoF7zE5Fs/jfXXAUxQaL/Z0tjfF9EFKNwJfBB389WvkpLs07A1psaVrZYSOK0RaK15YajU2yvCU5ny8wRj9li96YuUer9JrQzvuywwOKuSwBzj07ZZ6RduzQCV8jTw0T5Z0cvLlZi7iOTwEQhvPw0GhM763WdGvf5I3tPigyKVKzrPuI7O3ItFj+1ybPYf5e1jVCo0soY32vF1Kj0Pq5zSGhbZpTS/ZENZUWMQ/7sXv9wFFUwCq2FnDHfmHzMpft1ZWkRAxw3XLG9B3WI4zVtol9PoNBpzeFatKAWo0tX/lFhHT89Y5MedeOjrN6DNAzZwxkp2Imxbni3mJDH2Yyn2h+HVHMfQGX/f/vj4FlKPZJOZgBVN8H58nwH3xgty+nmKkL1XJX/g6mw8l7lf83J5RZBFZUTrnpJaUwZqXNYuTRXTuazIMPqO2hubD/t4vUFtQ9N9raEOq4tDybbT1GCZ0ACNiqNRWZJ44cZTUL2b1kg9MHFO028k2hTiReah5N70sR6fM/BoWsmjfgRCk+8glzow0UKAyuPTIkRB3E4usbgkbfCoEyvUNsIjUzd7OBxKLqbYtrzcYhmozkrJH4B+LgV7vw== 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: UFFDIO_COPY already has UFFDIO_COPY_MODE_WP, so when installing a new PTE to resolve a missing fault, one can install a write-protected one. This is useful when using UFFDIO_REGISTER_MODE_{MISSING,WP} in combination. This was motivated by testing HugeTLB HGM [1], and in particular its interaction with userfaultfd features. Existing userfaultfd code supports using WP and MINOR modes together (i.e. you can register an area with both enabled), but without this CONTINUE flag the combination is in practice unusable. So, add an analogous UFFDIO_CONTINUE_MODE_WP, which does the same thing as UFFDIO_COPY_MODE_WP, but for *minor* faults. Update the selftest to do some very basic exercising of the new flag. [1]: https://patchwork.kernel.org/project/linux-mm/cover/20230218002819.1486479-1-jthoughton@google.com/ Acked-by: Peter Xu Signed-off-by: Axel Rasmussen Acked-by: Mike Rapoport (IBM) --- fs/userfaultfd.c | 8 ++++++-- include/linux/userfaultfd_k.h | 3 ++- include/uapi/linux/userfaultfd.h | 7 +++++++ mm/userfaultfd.c | 5 +++-- tools/testing/selftests/mm/userfaultfd.c | 4 ++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 56e54e50414e..664019381e04 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1878,6 +1878,7 @@ static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg) struct uffdio_continue uffdio_continue; struct uffdio_continue __user *user_uffdio_continue; struct userfaultfd_wake_range range; + uffd_flags_t flags = 0; user_uffdio_continue = (struct uffdio_continue __user *)arg; @@ -1902,13 +1903,16 @@ static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg) uffdio_continue.range.start) { goto out; } - if (uffdio_continue.mode & ~UFFDIO_CONTINUE_MODE_DONTWAKE) + if (uffdio_continue.mode & ~(UFFDIO_CONTINUE_MODE_DONTWAKE | + UFFDIO_CONTINUE_MODE_WP)) goto out; + if (uffdio_continue.mode & UFFDIO_CONTINUE_MODE_WP) + flags |= MFILL_ATOMIC_WP; if (mmget_not_zero(ctx->mm)) { ret = mfill_atomic_continue(ctx->mm, uffdio_continue.range.start, uffdio_continue.range.len, - &ctx->mmap_changing); + &ctx->mmap_changing, flags); mmput(ctx->mm); } else { return -ESRCH; diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 4d7425684171..9499cfcf83fa 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -82,7 +82,8 @@ extern ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, unsigned long len, atomic_t *mmap_changing); extern ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long dst_start, - unsigned long len, atomic_t *mmap_changing); + unsigned long len, atomic_t *mmap_changing, + uffd_flags_t flags); extern int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, unsigned long len, bool enable_wp, atomic_t *mmap_changing); diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h index 005e5e306266..14059a0861bf 100644 --- a/include/uapi/linux/userfaultfd.h +++ b/include/uapi/linux/userfaultfd.h @@ -297,6 +297,13 @@ struct uffdio_writeprotect { struct uffdio_continue { struct uffdio_range range; #define UFFDIO_CONTINUE_MODE_DONTWAKE ((__u64)1<<0) + /* + * UFFDIO_CONTINUE_MODE_WP will map the page write protected on + * the fly. UFFDIO_CONTINUE_MODE_WP is available only if the + * write protected ioctl is implemented for the range + * according to the uffdio_register.ioctls. + */ +#define UFFDIO_CONTINUE_MODE_WP ((__u64)1<<1) __u64 mode; /* diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index dd807924446f..2f64e0a9b234 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -693,10 +693,11 @@ ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, unsigned long start, } ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long start, - unsigned long len, atomic_t *mmap_changing) + unsigned long len, atomic_t *mmap_changing, + uffd_flags_t flags) { return mfill_atomic(dst_mm, start, 0, len, mmap_changing, - uffd_flags_set_mode(0, MFILL_ATOMIC_CONTINUE)); + uffd_flags_set_mode(flags, MFILL_ATOMIC_CONTINUE)); } long uffd_wp_range(struct vm_area_struct *dst_vma, diff --git a/tools/testing/selftests/mm/userfaultfd.c b/tools/testing/selftests/mm/userfaultfd.c index 7f22844ed704..41c1f9abc481 100644 --- a/tools/testing/selftests/mm/userfaultfd.c +++ b/tools/testing/selftests/mm/userfaultfd.c @@ -585,6 +585,8 @@ static void continue_range(int ufd, __u64 start, __u64 len) req.range.start = start; req.range.len = len; req.mode = 0; + if (test_uffdio_wp) + req.mode |= UFFDIO_CONTINUE_MODE_WP; if (ioctl(ufd, UFFDIO_CONTINUE, &req)) err("UFFDIO_CONTINUE failed for address 0x%" PRIx64, @@ -1332,6 +1334,8 @@ static int userfaultfd_minor_test(void) uffdio_register.range.start = (unsigned long)area_dst_alias; uffdio_register.range.len = nr_pages * page_size; uffdio_register.mode = UFFDIO_REGISTER_MODE_MINOR; + if (test_uffdio_wp) + uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP; if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) err("register failure");