From patchwork Tue Mar 14 22:12:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13175060 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 B0231C7618B for ; Tue, 14 Mar 2023 22:13:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 507766B0075; Tue, 14 Mar 2023 18:13:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4683F6B0078; Tue, 14 Mar 2023 18:13:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 30A1B6B007B; Tue, 14 Mar 2023 18:13:10 -0400 (EDT) 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 224106B0075 for ; Tue, 14 Mar 2023 18:13:10 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id EB54440DA2 for ; Tue, 14 Mar 2023 22:13:09 +0000 (UTC) X-FDA: 80568905298.08.66DFF58 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf24.hostedemail.com (Postfix) with ESMTP id 2A5A918001B for ; Tue, 14 Mar 2023 22:13:07 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=MwYaay51; spf=pass (imf24.hostedemail.com: domain of 3c_EQZA0KCMAg3krxgys0yyktmuumrk.iusrot03-ssq1giq.uxm@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3c_EQZA0KCMAg3krxgys0yyktmuumrk.iusrot03-ssq1giq.uxm@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=1678831988; 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=kFDuYHkaOlvK+enkas00sVrIsC76CTfX34FshAQHzlg=; b=GLcEtXSOwA87E+L7rwkqTFCq3DlXl8AWQFWkeKyG13hFXoD/AN+oUNEzZL+ptpyBbFxSFL 9T3Clf6pbxRIhBUqosRR0kMIVI0gurtN3xrAs9vthBeUKCYdXXBH12ls5uHvlzxJVJGy5y 5v60Rt2T6g8PYQIfbMG70KNie30Vhc0= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=MwYaay51; spf=pass (imf24.hostedemail.com: domain of 3c_EQZA0KCMAg3krxgys0yyktmuumrk.iusrot03-ssq1giq.uxm@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3c_EQZA0KCMAg3krxgys0yyktmuumrk.iusrot03-ssq1giq.uxm@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678831988; a=rsa-sha256; cv=none; b=XxQKDSthXFngytOQr6FLqhjQJBUhPRMM9+zBQO61LRIWtkzD37RH9+Wkmm9EkeZWY/cIX7 x9zuP9chI9HoIiCqxCXO3Y4Mq6iUrBHhh39Dw9l1hOE25K0q7FeDwws/TNOHzXp5NM1GSa 5NcuuIdgBqa0/j4X75P2qn5Iize5Ilw= Received: by mail-yb1-f201.google.com with SMTP id j125-20020a25d283000000b008f257b16d71so18382176ybg.15 for ; Tue, 14 Mar 2023 15:13:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678831987; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kFDuYHkaOlvK+enkas00sVrIsC76CTfX34FshAQHzlg=; b=MwYaay51HRjaVNMna4SxO6mPWibtfJSnbxlCmTcqWdj/aMcNYci/2o/HXRev/PP/a/ N79JfEvLkW5mLC9XkXesBhQysJMJ4srDBJ4vL87QskYQK3mKuwwv/4taYZfGVJ7Jq80N 2/i6yWkt51L55RV3O0tGRGFKfDQ5Z+s2EYC7Mmdglk6XEyQvyunhJxyQ4SRwg2x4jTeD p181/GaDdx+Kz19kWgJLQFsvZvk2Cfi2ZVnytsLPoCoLnA0U1Z637qH2Bmo+80TX4w8A F5bhUn5Rg97TAfnCl4dd+4pVLbS4vYI1Bjj6ZE69VDbu70C/3HfVUy6wtL4eViJ7fnW8 i2Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678831987; 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=kFDuYHkaOlvK+enkas00sVrIsC76CTfX34FshAQHzlg=; b=tT7Xblm4b87TT1E8j9+lB+tVDqISZP5aDZ/szepC9NNSm6XQkJczt45vgQoVSyzbF1 V+x4lVfhxNKFgUlue5zTcMpPlmFfq5Fwf3p99lPoFubM3yeXu8GxQT4eNfbDW650y1o5 fSS6wYKfj46AbFbUHFQebZSJM9vffbZfFCtB95TbtWcCyZZ3hLX29D+pmZa7CVAiyKAh FKTzRd3H95LQr+WnlDAZEuOUWENZLD2r6/FLLk3YMcSOLaTvWWDdYs94aK80UY0L3dwO smn8OOVplQhS9lu4IJN0R2s9E0WvnqLpgNE/KGfOVWZRoaackibQIIbHN/d+ZAV3KUjm kBxg== X-Gm-Message-State: AO0yUKVzkcgsLVkkiHKw2rZF1kVB9gMkr6SzLjSU4kNUX4l3nKzWMaYA vx0eplqzG0dVM1dhJBGrRtkDmyXbNWVlG9lSbKQt X-Google-Smtp-Source: AK7set/wGv0xruYIZBBoEGIzpt2s96oHOq53uLqNN1Mq5bzIulMhX3WvmMWGiE43w4x90Uj10QrUMaAjjTrWV2ivLCbz X-Received: from axel.svl.corp.google.com ([2620:15c:2d4:203:21ce:bab3:17ec:2276]) (user=axelrasmussen job=sendgmr) by 2002:a81:4305:0:b0:544:6455:e023 with SMTP id q5-20020a814305000000b005446455e023mr2198836ywa.10.1678831987198; Tue, 14 Mar 2023 15:13:07 -0700 (PDT) Date: Tue, 14 Mar 2023 15:12:47 -0700 In-Reply-To: <20230314221250.682452-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230314221250.682452-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230314221250.682452-2-axelrasmussen@google.com> Subject: [PATCH v5 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2A5A918001B X-Stat-Signature: e9xxcxjkenb7rq7kcyzw16cwhdkpfegg X-HE-Tag: 1678831987-438246 X-HE-Meta: U2FsdGVkX19DmHJSZy/MmL4hiExoaZGkhCGpG77cZfi7Ar0tS3ZDKzPaA7DE7Ak/dWoizHaHEm5xRH4eAgOwj+zVMgi6d/q/xDi9LBb7OBK2uxM8JlA9SgwjDOK/S4dze1WSGWCEUm2F6O6cv3MPXxLw9PZk4bwxUqDY5V2I7BmubGhuIXc9UsLCNB/ZMijPvWDO1QuImNocET/iO5R8LUdLmA4xZAgA8rt7c7t2sTVfD31zeDVpSpR6qeTkknkwkG2UPirhtiBYvmJZ5c9qiwLvIDR+cT0c19qZ38+Rs2GasMAW51vBlKaLUxxDRoZIxRQEVQB9Y7Xf4RgqWShb3nsvFASGv2jtxj1Nb9sX36ZFxz/Bfj9FNfDeDZrRuwtaLq1IL4RpEjQY85rycxgWUrxua8AREohjyky3zrcojABsohj1Tns4GGgXdPGRWOg9pa2GdbvTwVh4r21jC07NvCKKGSo+YQ2wePiGjE8e1PwW0WqQ1VVoHD0DcfcQv7Mmqmhg320YhZL7dOMnl7HUWRorE9/3BPtzE2ruNZl8ChSa7FAxH+3o4OK4jjUClNDCuxD90DOm9UznfNSoPxcmury1/qATtGv7gScxTnTlENTWzn77vKPSxkDLaGEV42SoT4Lr1ZH5La8M26y1tCdQpBuQt4Fu+WDhE5l/EZ4iqdm1Ed+uRSBP1ckedvTkjvUm9feZO+1vCALUKO8u2w9sz5IKHKkmUR0Y8Jqdphch3AFg/uB4JYCrpixsUJhbWw5IO2S8TfgOHP/b8qSE/4nL0SnMnt6e7tOgGtWx6Adc9eBoh7zBltPHgXKkbhihP9XxjaZuORrgKneOdFEIvl4TrH5992VauuR0CCpP05C3fhkuuLYmIYIFDHmp2JOD83OcecSHx0k8JccGFSFT8tJk9CpOt0wCDTCxRWDBq5z14Y5RiM8gRJ1d150pfadby4dkgOBVnq9yslQL9+q8+gp 6ltHAAej 2W4gLcmhOs7GKEovKyNMyxC3btQ5kv1BGdnjnHyYSK9JgIVO2xx3Xvx1JCiSbaMQu9xcoioInXfxlIUREyks4gsurQnVjrZIo0diwC7jJVCTtSycyQgMrt8UUIwHIIthKjtRCXrKNkKo6s4NpWV836JOdWY6sUp1PAkb/t4aE93g3bC2xbCrOwXeWL4biT1puoTpDazbJ2QBz8ZjNiDMQ9/jyH4ZyYK88DU5R9jM+cheP/UdhP5M0/vsParbJHOC25ed1/noLjhdh5u3ApdGu4NvsKW8WvhbXCnmKTET3LQAeuNsqJnfoDnzRTAzuq0O5Zdm+qFH0kL0nR/LCgoVfTHjk/UCVwvLogPLy6wJMYOLRXENTOgmEM/PHPNcDlxGaW6dTEXsLGl+V0500Zp7FbcTZbPv98OvdgQkQL0SoZFmqT4M5GX8wFzuaAQO1yiybTFVWX99lkvBY95RrYAyMImidzV2tjZFqZ5Bncd+g0ubbdZ602zgqJ5SzPVNu8JAGqadqlid8jQvhstRz23d16roXU7B3v1mnvvB4X6quWtNflCIO2Gn6pmTJLWkajyyjaQ1C/MKifO+NbPslgcpPqmC1Q9c8J/uhjtCjU5l/OgV/reDaRldshKHYoodUfT97cW+oScg4q1L9jOBvFMG+NwTCmoKOUsUspExZGIGUaCwAs89m9q+cbpFpgjt1skB7gsvC2b7HWB6x7B2Nv3Z3w6IZXBs0cJCu9CLr8LrO1eIRxEcG1rDKfXmkTf0i+68omWjOIMWvusyQ1EY= 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 Acked-by: Mike Rapoport (IBM) Signed-off-by: Axel Rasmussen --- 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 Tue Mar 14 22:12:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13175061 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 806E7C6FD1F for ; Tue, 14 Mar 2023 22:13:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 247AA6B0078; Tue, 14 Mar 2023 18:13:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D0D66B007B; Tue, 14 Mar 2023 18:13:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 098FB6B007D; Tue, 14 Mar 2023 18:13:12 -0400 (EDT) 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 EFCA46B0078 for ; Tue, 14 Mar 2023 18:13:11 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id BBC371C5D03 for ; Tue, 14 Mar 2023 22:13:11 +0000 (UTC) X-FDA: 80568905382.24.E6B2B15 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf17.hostedemail.com (Postfix) with ESMTP id ECC9540005 for ; Tue, 14 Mar 2023 22:13:09 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=MI0DIqw5; spf=pass (imf17.hostedemail.com: domain of 3dfEQZA0KCMIi5mtzi0u200mvowwotm.kwutqv25-uus3iks.wzo@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3dfEQZA0KCMIi5mtzi0u200mvowwotm.kwutqv25-uus3iks.wzo@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=1678831990; 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=tLH8n4YXArkUQffQbzzj3eK/Q2WLoaxfMp0XyWUCAx0=; b=mCtT7sy8VXgZGZmj8ZY1KH0+ums9tjYnTbKFKh84Laq3ElcPMLNba5O6oIOThoUBa6oNZt sqd8QlejleMwOHIapXwYt6qTHmV3BDRrHFMr4QA8kE4RSswjncIYGevdg1BfIXpaJvluYF w0V7FXSzMZ75gHIAemGKpqBRc4zsxZM= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=MI0DIqw5; spf=pass (imf17.hostedemail.com: domain of 3dfEQZA0KCMIi5mtzi0u200mvowwotm.kwutqv25-uus3iks.wzo@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3dfEQZA0KCMIi5mtzi0u200mvowwotm.kwutqv25-uus3iks.wzo@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678831990; a=rsa-sha256; cv=none; b=LdEURd2teDWSeNFM4JyjF8MkJONIYCUr3Ib8xgYiFdKCyu1XaaCwx/dx6IQV1/+9+nE448 0MU5hKGSw9hiY2utw2Z6VzokZafzLcDh1b1s0Du24xegQi09CmSvePrQ4yz11US5LFvv2d SX9r1cbje/ACZnHfLYL84OyzrOPJcjo= Received: by mail-yb1-f202.google.com with SMTP id w5-20020a253005000000b00aedd4305ff2so18569758ybw.13 for ; Tue, 14 Mar 2023 15:13:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678831989; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tLH8n4YXArkUQffQbzzj3eK/Q2WLoaxfMp0XyWUCAx0=; b=MI0DIqw5o1kZ1LidUuNaCJMA1DxP0NtutTXWrUU8amV1STMNMqNO2OPPS9//RZuv3A zzB0FueU6Jqwgkt3s73/teOAXyeLiYzeAhcfl72HJAz9vcXG4WiP4Cl6pwvUeooTLu8X qLDXXeOJ5LFaOKHGQ4BrfEnF6FOPwA1/isprSDTO7hbDxWSQ8EueIhnqKeM5j7T/C9Ht w3RMJcxALYuduZmuu/mzmTD0DbkgnvmNEvul75x9RGDTSACOgQ54D+7BARCdmMU8K6oe 7MiOfSGSiV9f+yhGhCKUbtBNj06VNFw1ccDp2m/3PInTQvkDFM7rN0LFb/AlNiFMhgvu 5b9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678831989; 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=tLH8n4YXArkUQffQbzzj3eK/Q2WLoaxfMp0XyWUCAx0=; b=dkS9gssIgsDffjCDdZtnLoFsPV4Ll4iUqm5IosbL4gEjsa6j5uLmXgQt/v76pkDXhn ZwhkxrssK1CXSvyPmF7v1br2l50xKEm1w3DqdCI/2ZAuBG2fL/2c9QQPxXalz5XgsVTZ 3mZslOvH/Y0+heA7Zw5SOKqREb+LjVRzlnu287oAg+60oCjTkewfvyxj0G1LTc6KQ/ea gX7mj/Yp4W/IkWHUqAih0nRuEH1uQ5CRN0Xbj+Qk9GTQiIPc4zWKcTdeikOTWh9EE/en n+XUUOIdE5mJZ7GFMvoHbsoNhP7iKDWFra1ZlR9SD+8eIUbaqv3ngfBzR6QoL9G9eeBT pZvg== X-Gm-Message-State: AO0yUKXHiy63fSwBsBbQwVff7/9hS2yTUdWvF9O5e7U2Ek2tol18YSpZ x9PY3WQwi1Ykmlli/pJbrm/qc278brALE/CueANC X-Google-Smtp-Source: AK7set8X8Ot/dtqhk3gU8wbWHExODdLNpBgnH2rdmHFOGldWEBXjs/mUM1rQq6qsmvaLIXxgbT15ZCWXxNsrjbSSl02K X-Received: from axel.svl.corp.google.com ([2620:15c:2d4:203:21ce:bab3:17ec:2276]) (user=axelrasmussen job=sendgmr) by 2002:a81:ad66:0:b0:52e:e8b1:d51e with SMTP id l38-20020a81ad66000000b0052ee8b1d51emr11451580ywk.1.1678831989108; Tue, 14 Mar 2023 15:13:09 -0700 (PDT) Date: Tue, 14 Mar 2023 15:12:48 -0700 In-Reply-To: <20230314221250.682452-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230314221250.682452-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230314221250.682452-3-axelrasmussen@google.com> Subject: [PATCH v5 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: ECC9540005 X-Stat-Signature: amdzi6uzg7sbo1yro5ewzokpan5s39yi X-HE-Tag: 1678831989-167881 X-HE-Meta: U2FsdGVkX1+8jCc+ALIGuFHsj7k3p8w7k4ldPjnCW8tuvlGnU3VImlkfIgMRzGexO+fQH5Vv/0RcUb26KWFX+vfhJcXIwNybhCfpW5Tqo+O1AyxEo4B6/ht+zRUn/b6BCRVhsz78GksTYHhLperZZYPuP23Oi4GgGBfaNU2FhUZP/vVfH8/ofwVFY9lC5PXAzqgT+YKo0K/bpo0RRzXmFBVQVYciCkavKbZiJHhF8TPBnqyWuL77jDZVpr1h74VzLZZQwXAjoeMfy8bTFFBv4KEK4qLvhljTblSwXJR8p4NQmbozzU3Onq4WAlfbAKFZ9OIHNJYgk9FLXquzcpoukVFuyi3LsEsU8aMENr6RwqB0ZlSN+HDRZxrnfxcgWKctBLWWawGFyIx9R9g+v8SyytO9HQmOKed1ndh8U1s1SwDkm9pGgIvnefzCzTaIZog9w74rH8D7uqh/HLhrImEWzE8/mxYLO+xqdBo6TxiBWQegv+MUjFgghFXC0hbiC465eYldzbEokbmuoXUy0V6vAKcYqH5HfTm8LK2u2O8AWa1u/ihrrD665QGzGV/teDgndko24xoe83Eh9fHck3rHD+D7q/eDgL11vQezKZsyG8gdGQ8U0vVtvNyqduXjVb3XXqJWtou2CufQ4Lismdx+RfvAw0DKTpVubglXEMUBMZhgNusNrXQmutKLWgZ61eHCWK0mWCVru0A4W8t6C/eQl8FJsH2/QG+VduFW31d2c3w9890o5oKcd+UXfGivTMObrL70gT9gdw828cgORDT30PzqgodV8hlLb21WQ4iGk4CEJ4KNVtYcreRdM73oESHOxnsiX+/6e6QoUiLa4h1k/AD/au9k+hIyeP/lDK3I1e1En/YwrpX4eELUqWBFHQ4xSS8zZzCQ6+f2PbZ91q/MTgqdBSakfvsN3o9lLI4h07crSzCxk4j9UwYk8Tg+FjsPW9jUNjengkHkZ7r0A28 jtNjrmtn GaKWcecJ6zYGBu0xTjnymcUxRDTR08RnXb5CxPwh0MP9yazTDpRfisokJEY6JS9ZLm7I9ECLy/MYApUMeorIODcV6K2ANy5r5ePDpvo9zSg3strfCP6LF3+YHL0VND0WkK3+EbfNSjGm26HBLJJk+BV9wnSZ5VUKIsiLwMZaaMAiyg9JBrPYRB+fSo9ADiB3mqfLRzSR+TcNvKO1jLIltgxOD+4pJnLj78swFOZsFEgCBZDuxNHdHDdm/uIm1a5+mwDGuj8or1z3o1AsZBEKRrpI9Rt6b7M1Kom0sJmfvMZt0cCqmFkt/O1F2zoI/L7WHZ+jyTw6Tl5vh9v/sThDGUEjpkbRppIuHbd07E8fDWb4PU8wLHeHC79pdPOniRoNKlevS0k0PHj33Wt80s95Y8obtxzy8WFUVh1DGX1cFNsoM+OZONezFkpuymuNB0LGHWAQtAyYma+Aw5kiij6Pg3XkPtNZxnc0HcWN35nNw4dabtQKcQvqi/Bk6TSJQKRY9WEJHm9gTry6iqyLqqvScO5IPTLIIUvFv5YHIgxjwJ4HEFfyUUnF/GgFTL4Sj52ggs96JukWSo0kPJ80gVSHA2Ca0eRLGm0dEZ19M1qtBMprXlt2F/ze67E7FUBbLcxubfTKB/K9uO6ahXvPu4DyC0PrggmAWvRFOrQJnMuT9Rp3Piei+F5hAJwjPWvSYWILI1QK2bExfKGnwsZQ3ufFY9rtYlGhMWyUrVjWbUj87JX0mPuulzfiwzYFPPzUaHGLErBJgo4HhrTfsW7k= 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 Acked-by: Mike Rapoport (IBM) Signed-off-by: Axel Rasmussen --- 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 Tue Mar 14 22:12:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13175062 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 70E50C7618D for ; Tue, 14 Mar 2023 22:13:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 137EB6B007B; Tue, 14 Mar 2023 18:13:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 09AC46B007D; Tue, 14 Mar 2023 18:13:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E54908E0001; Tue, 14 Mar 2023 18:13:13 -0400 (EDT) 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 D5F1D6B007B for ; Tue, 14 Mar 2023 18:13:13 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A609F160E51 for ; Tue, 14 Mar 2023 22:13:13 +0000 (UTC) X-FDA: 80568905466.14.CCA3E27 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf12.hostedemail.com (Postfix) with ESMTP id C7DCD40019 for ; Tue, 14 Mar 2023 22:13:11 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=PnWY2eR6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3d_EQZA0KCMQk7ov1k2w422oxqyyqvo.mywvsx47-wwu5kmu.y1q@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3d_EQZA0KCMQk7ov1k2w422oxqyyqvo.mywvsx47-wwu5kmu.y1q@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678831991; 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=ImHbAtkizfKLPz3JC3iAXxy0w6LaD0vWlTlvRyA16l0=; b=Bc2QLZ1gvPvOwHX5eiCQdkqo5d1j50wZRo6kvshtQt8LAcPNB6gqQn+si7Lvpe4836OHOx oG90bAZn3KMf+kaWZSWknKqDoQIredrB9TQsXgeSXUx/VvYOEiBLX1iQYLJWNEgnV4qo53 OkJ/dy/u55SQ/EAyaC9pX/9DH0e8B6I= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=PnWY2eR6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3d_EQZA0KCMQk7ov1k2w422oxqyyqvo.mywvsx47-wwu5kmu.y1q@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3d_EQZA0KCMQk7ov1k2w422oxqyyqvo.mywvsx47-wwu5kmu.y1q@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678831991; a=rsa-sha256; cv=none; b=0xNX8mZVv/am9PuAeVjO1aAhzBuwAJ7GlVcHd61JB4I+2aZvvk2yhzgNXyDYr/Pc/jYNuT 2+x+MC8SQkzOFd8zcivIXimR6ie+cGWI39zQPxXOkAFZ3b6mZlLf1J/3vfWbhHfsyxphK4 B+8e6/auXmzblYs0efXOtUT5gJAarVo= Received: by mail-yb1-f202.google.com with SMTP id g5-20020a25a485000000b009419f64f6afso18407881ybi.2 for ; Tue, 14 Mar 2023 15:13:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678831991; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ImHbAtkizfKLPz3JC3iAXxy0w6LaD0vWlTlvRyA16l0=; b=PnWY2eR6h2SQx8G87DvZRgumoEUR1L2NCiN/WyCrYqnZdaHvIJm18y/csfqIi60SqB PC7Dg3N4UMmGy+/ACT4WlZP+IitiLo2PlngzdokCfWCm25yRhGV9j6f8xDjwGUReTsoG 1Efcgv/oJyLEfp+XFlZ3Zao4UR5up5QEOzaSaJktNyFRRSVQx2zU3jwkjpYWLj/zILWs o1rEP+EbBdRcbkOo7F5aLesK2bku01vqEW0r+N9OF9gJKcIoLbftjHOoCwc12EnXpKFn mpWo43llTD3U/KZ0OOLJCA51HOCZIEP2zdwl1Q7jAWnVLssejjLOtSIyt/McxjUWvsJ2 ge1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678831991; 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=ImHbAtkizfKLPz3JC3iAXxy0w6LaD0vWlTlvRyA16l0=; b=AQ9xCdVu4LfkWdERoyRWhDXf0eazoMhWudTHr2C0JmocOWb5DExxDpoFzirapYnmwd PpyifveGi1fRZTZigByequJ/NXu5DlzZnT0L6mG9eACIW0JQHesH4JAKmnGfio5Wrul3 fuODDfL3blKgCnGK4GvpcWmRQwZbNsLyITKTE/lxXPo/okxkfRQLjlhZ2HAYDqbs/iXx gyVZy68gDDyXWNCV+L46PbAwnfuSbFqlv++3psZQUECpQOmVjBEw8wb675+0HdjcSAML WXEo6yDWa64uyywocLmpTGTbjcl9bb4dSe8k+gR+D7hfs8Wf1g458FqmjsRhgFSMTHWd pOLQ== X-Gm-Message-State: AO0yUKUdm67GJqCOhts7pmhtF+w8/5PsZuBGgkszOZ28Lrk3gflRGjJl 6AjI96OEcsvy7WTKxDEsIbinOV38K29B4KL7Y6Cz X-Google-Smtp-Source: AK7set85aKajdWP33/Zpu/BlOEyevwLQnaj2hBtMBbeN+LJGVm1iyRMj3+sEWIhBTMqX32ZFfPuzrRM8Cs1Vi4yHJYSj X-Received: from axel.svl.corp.google.com ([2620:15c:2d4:203:21ce:bab3:17ec:2276]) (user=axelrasmussen job=sendgmr) by 2002:a5b:c51:0:b0:91d:98cd:bfe4 with SMTP id d17-20020a5b0c51000000b0091d98cdbfe4mr24901375ybr.10.1678831991032; Tue, 14 Mar 2023 15:13:11 -0700 (PDT) Date: Tue, 14 Mar 2023 15:12:49 -0700 In-Reply-To: <20230314221250.682452-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230314221250.682452-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230314221250.682452-4-axelrasmussen@google.com> Subject: [PATCH v5 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: rspam02 X-Rspamd-Queue-Id: C7DCD40019 X-Stat-Signature: 891mnbcttqn3kz7nckx3o83np3qgerc1 X-HE-Tag: 1678831991-985074 X-HE-Meta: U2FsdGVkX1+ff7+Zlh7My9t5JE9KBztwT5BsoSZa1r2PnjfMqtNXN2rq9MCBZOGPR57SvRLKBwU7maXJVLUwLhM4KWtt4M3TCto7n9ZSDJeQpmnhgGXtR+M03r+UiMtvgvlp90gYtL4lOZKb7x0+zNAeYElP34AtAAMCbE9jxWP4RPPtznAauzteD7cWwhiHCVKA0xOW2BUK33wxncutQmOrQO18a9TwLeV0YsO7zqaT/G0ozcC8tYst3isw9QH3gJDQBj9qMWSVnPzXBR5F1i3W0cl6SKNc2rNWOypbLnl3cyGRHOIl8rB7kDbybRyWRxgJFbvbUdAJ8bn83pGbtH/3qRWqTEcwGSg6Wnhpj8Y+SYovi4S447mpDO8pfwzcmLRlSWoTGYD0d0ceX45SPOnkFTdtb9ljEAiwyL8uL/EIExHzsXec6kPVprCY6mpNpMmjIQeRFMlX23/J+I5FWFeVCemCZKRN7oqhdJRUYtgysYcj6x08FH8ZLuzuJGRHtXWLfIrzgPCckrnvJX7VErkVmUEtto01Oircx/OQwZhXdMdSqG2yBu5E5V7xfJS4+D1t8I9y/8WEAO/vK9gqcQqpQyVcRa1taboYacXdxB/qdNL0uayKcnZ2koWfz2arWK9BLpYDQNYZHkhLQ/KSNj73/8ZZ5ZvssUkUnG4aMlj3GZm9Bsur6j65F1VGB8/0d2zxaNnPqfelAgMJTyxEd0lfOlyLIN/UJprpVMb+lIRlMxlRPYbKhUeeBmP+InDnBzjmPfUbRJ3D32RanD7665UhlUkXOzyRIjsyCv8PbAkfrfeScbKLYu08N3WENBbyyqxxqJWlhe0+YPO57RVSqEg5fD4zj4sSP3CUM0Lieth1y4xikbf7wk/Dz+lCOviUQ2IglS362EEm4lg39nl3d5vIKjDb/f/sa1WbIOfzQVkTA+LII9JA7Qwz5bD7eox3W7V1AiGnbeEnDWkqRXK QRdztDDC Jzcw9eq25V/KzL0FR8MoHb3u2Qyg3I0jPEuyDmdR4si3WQOBGdzCtX3+cfYNmmmAgqVur5g0ORdSAWOucrAyfZa2RZBN00jBbbYmnPFmAIXR2A5ssyIoXnmb0OpHkw6CpEolPnbvZG8YCVmkFhlg6krpd0gvplhxbmy9hfItzoBSIRK+UdR9iZ7l3ssMdJSL6FmG3ShhU9KOdlgDZA28fT9KJ1eY4hA7JUuGNGNp4qwsk3oTdA9evu5MO0e47urZeQdxdBmzXXLhrBoX2Sicu9uuv3ErCiMXU6BVD9l7VMceThnxIgaVF7ooareaDIsmOoWFdmw1eeu45635fSZyPzPfj5llwDt9r2ykKEqGaufOcioB8xP/gDrt0Ze1Y44/CLdIT9z7EXmiYr7i/KSnm08KrVG8dYG6vDZeoqj4ATrCpaP6L/vyIiiC6XV5+pDPZWsO2bWStBvfXM45U++Iva3VMEDrRHbdnBZS2BOzGbQTL/xy31ijF124oApsMMY2QBl6MFBvz3A2ieUI0wSK8stFPk61gv05nvhTn3YCcSbeQjpFJYEjlzk5vG5yz35Nhn65krpCnEFWJADqe0tzM4H/tQU7Rmx2b1q5rlQ4CAlmE57tnDSCT7l4fLhgiCYFH/t2shDTMeXnEQJ96k4Vh8P2G/TTgN0pFsa8sYa8S6Pojh7rWFJFlvv06zi+EMbOjk5A25+hkP1QEeX82G82h4PbbMcY8maRiCs3fADHFddDPUATjSvQcldEG0lQaCsDkbBikv/XTG//1DiuSo5eBygqIovn7DOjxX6hjJx7HKVbU3U3HcUmFcegApHUGaJvBXcXYJ7l9j1nqVLM= 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 Acked-by: Peter Xu Acked-by: Mike Rapoport (IBM) Signed-off-by: Axel Rasmussen --- fs/userfaultfd.c | 5 ++- include/linux/hugetlb.h | 10 ++--- include/linux/shmem_fs.h | 5 ++- include/linux/userfaultfd_k.h | 46 +++++++++++++-------- mm/hugetlb.c | 12 +++--- mm/shmem.c | 7 ++-- mm/userfaultfd.c | 76 ++++++++++++++++------------------- 7 files changed, 84 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..a948d92154f5 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -40,30 +40,44 @@ 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_mode_is(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) +{ + flags &= ~MFILL_ATOMIC_MODE_MASK; + return flags | ((__force uffd_flags_t) mode); +} + +/* Flags controlling behavior. These behavior changes are mode-independent. */ +#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..63fdea37ee3b 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_mode_is(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..73c456109d5b 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_mode_is(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..9202c1fc79ba 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_mode_is(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_mode_is(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_mode_is(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_mode_is(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_mode_is(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 Tue Mar 14 22:12:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13175063 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 5EFC4C6FD1F for ; Tue, 14 Mar 2023 22:13:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F40756B007D; Tue, 14 Mar 2023 18:13:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECA008E0001; Tue, 14 Mar 2023 18:13:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D439A6B0080; Tue, 14 Mar 2023 18:13:15 -0400 (EDT) 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 C18016B007D for ; Tue, 14 Mar 2023 18:13:15 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 82D111C3981 for ; Tue, 14 Mar 2023 22:13:15 +0000 (UTC) X-FDA: 80568905550.29.FAD59FF Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf17.hostedemail.com (Postfix) with ESMTP id C20734000A for ; Tue, 14 Mar 2023 22:13:13 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="R5pck/04"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3ePEQZA0KCMUl8pw2l3x533pyrzzrwp.nzxwty58-xxv6lnv.z2r@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3ePEQZA0KCMUl8pw2l3x533pyrzzrwp.nzxwty58-xxv6lnv.z2r@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678831993; 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=4pTQknU0yyvhsrgkZGBBdxk3ve6H2jxjb5KB6cR7GsY=; b=s0LZ0QMnsmxJBPdH4Z2nGz4lPVnFkzADpDsaYORY7rErzb117VCZm2pwZvKC3tT25cQPSt SNHvcsLaXq4Q9iPt4PR5Tp1oQCuckFiAbW6+zR3BKV71i+RhFK8dl1IHl9JsUo2KfwKbAd 7X2SPdBVacZAEaS93nGlHBnbuM2BKQM= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="R5pck/04"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3ePEQZA0KCMUl8pw2l3x533pyrzzrwp.nzxwty58-xxv6lnv.z2r@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3ePEQZA0KCMUl8pw2l3x533pyrzzrwp.nzxwty58-xxv6lnv.z2r@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678831993; a=rsa-sha256; cv=none; b=QF3u1sVNOD/ZVLb0oZEU3N74H3skrAsv7vSiXj4Z/NnnHGzTVc+oX/jvRfBRjsdtj7Ah8L rN3yJyf40ikkuP5WXR1ieTWvY7NAI0jgajL9pqMSrZH6XxPXbQJX3NypH1MgFx/zQ6caJk LlEuKN76MpwhjZsqO2cIfto4LUlpAkk= Received: by mail-yb1-f201.google.com with SMTP id z31-20020a25a122000000b00b38d2b9a2e9so10121358ybh.3 for ; Tue, 14 Mar 2023 15:13:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678831993; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4pTQknU0yyvhsrgkZGBBdxk3ve6H2jxjb5KB6cR7GsY=; b=R5pck/04mfxPmIVBB9o4uaUAo15/ZfMIMU4zBBeiy60dN3PkzfG+pL2Jk8ShQx/wnT tu1lgw8s51UeJbNP7Jq1JSaDKFmENaAF8z1goiVAAzBIHtPHaXkByRIiol+yJE5/YgnQ hW5qdRTIt9H9xpPdA02kwFo7Rk+Ynt1+wYVf34T5VMzeBtBNZvPPA5O3aC3D91D0RNtm mSJ+v1/gDC92H9jo4qw/x0rtVNP40bPesp90xC2s/8DWp1y16hh7VG0pXJdfZGwPx2Bb 3g6PFtXcDzEpd66sHf07DdrXtn5FXRMWyPg2Jsy3ULZKlqOp9KCEKjeIRXmZa8Bhn/E3 nxaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678831993; 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=4pTQknU0yyvhsrgkZGBBdxk3ve6H2jxjb5KB6cR7GsY=; b=3yF4M8TnQAaZqHt7wtI8i5kl0SVx6miRwAkPUI4SbhYVvfSrfoau6S2K8fjxabnRDt h9Shtye9c7xjEaG+xyZLsRvCalPlQRsBCIGvHTHOnScgptg94VT5WFE1s/Nvp0FTxZ1i IM1vOCIVKGdv7SL41JTwuKfzyVscTsiTlKEB7u5Q6HXMzYjMJ7gjAiCIBoEhb88gUdeS UcQW5/FSijQ7ScFrXXYVmwcT5nPRBRh8y9m9eoI18JlIrnElJs1Pv1//G8Jjwnlk91tR n1r5XaGPJD23YxePiFvXlu3bEh886oUwPfm3wqVQQmYqZ/BfznrnMG7ZR/GN0DPPYbh0 VuxA== X-Gm-Message-State: AO0yUKX3HRfiRsghAgGKAI9JeQ4tHDOFa62lzt8WaURG8xPrUphAvT7v ihfurIYYey9Iv3RQdsOC/gkARjpWhAjIhBGlbAx4 X-Google-Smtp-Source: AK7set+CfovE4o2SVuha7KE903+E9XdVfmvAi2JH8R4Sr+s1e8IgI0cxifkuT5NnCB1X9zPcFkujhBDANx40mexrV28r X-Received: from axel.svl.corp.google.com ([2620:15c:2d4:203:21ce:bab3:17ec:2276]) (user=axelrasmussen job=sendgmr) by 2002:a81:ac52:0:b0:541:9b2b:8240 with SMTP id z18-20020a81ac52000000b005419b2b8240mr6114708ywj.6.1678831992903; Tue, 14 Mar 2023 15:13:12 -0700 (PDT) Date: Tue, 14 Mar 2023 15:12:50 -0700 In-Reply-To: <20230314221250.682452-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230314221250.682452-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230314221250.682452-5-axelrasmussen@google.com> Subject: [PATCH v5 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: rspam02 X-Rspamd-Queue-Id: C20734000A X-Stat-Signature: w31uwaw34e3o4ownuae3piass66jrpse X-HE-Tag: 1678831993-196752 X-HE-Meta: U2FsdGVkX18wsi7+FjICUInkmyqqUdu38X+tVZmVGHO7Du4CByJZw+RCPRfx23/2zx/XGRoxzcR8TfQQjMjELPIb6asm5fBPkjrqau0/hJ9RS0eJY9FasXsEgHuN1ZVj32gIvRKRymQr2qjRZD37BG6YFkJZ2ijRlQRiQ9VmQZcD6HGMl0NG8tzW3EeoFqdk91gFrO+7aYz44BYAyZ2olGr5uJGCAYQ9p2x1r7LtbFMolWofH/IKKxuRjPD6ymnnfvUvRTTrJhsgLZmg2+kIUq9EK+RpvRG/81HYkcKHJLHE/SGxMyHZNy2X5NKHoP1G8ohLfqbQ63TxyESnydx1zYqjkf0qQ7LLcoPUGAwVwdMfR5az9lMdc0rBmmMbmrLRtHgIdnjfcXHrVTxLhu5aUHwQUW5fZ4lrMHXJZqmV3PFRU7xduVVb+7j6Xj8wUsCQyiVDr+eBTzfbRfZIk58d6Ia4hdJdos692IlqvmzA9OcNUsPg3fE/kmfu3EYZpq7Lxibd1DJcvt6sDjRSRyBTPwytcq/LxlRL2dSFxUReurjDbGowPE/J+8vWYCV1xLsl+e1gf3d0u5JRjlKb/Jtjr3S7HO3dC/xoYLGnmxhLwfS5rWQNhWAkSta6abSLw5tQL1UowQ5IENUSMQq0gx8YdHGIlXZW74K8PHLoZ4GqdRXqluVuvigiHm9A2QNCskIpi8m4KAa3hwEJBiUQxfbjJdEVsto7fjxsD6TWryT+R7soKvJKtynYPyR59vRbPuTf1WlJQurhA85mScsVRp+b/QaexrZIgG5E6K/czw7XeNAmw5BnReUaCPW1vn22DNUtJSAQ26z/xGUCeYAb4ACDBWfEFXiNIJzGfdS1QFsp2OJ/B9jR1kDUfX6NZfaJgzmjQeaASDWX7Gdf6A8u4EmDvb97CzW8epXhB0zZe8Q9SKe8WhMEePgXibbcfWNGd46fZi3DM9Wla9MHW2ANebT uGJFYRkD TB8mdig/L98QCs2S5Rf/p9odDHzojH0ZMfY61uvnIc8Dne7WxgB67JrpRzNd28f0Kl2oszD+4q0nyok8YqNVt4xW7pImlVYfL6HjSSDaKZf+am4ZkpVw1gE8DhNnZyTlN5zfQmR0/3IPpP94ZI5Qk7MGLX/wz55V0d6KdfIl4DAjc0BOVGVegKTF/wk4qBaY7ZSKF7gAzTtfKlO0Ks49EwQOWheUgjTJfrb221ULwsQWrrDKzVJvzUKjcBv9ypJ/7sbJ74mT6gy8bIV9gBV/7E3q5g7OP60dKOuH/dJVKfUbSASj9Y0Pr0CqgMx9E5wEwxkCDtNBmIrTXty2kRJC25vijsYfcKCVNNVVNlhj+J5FIJ9EPZ9MR0KBVUqobql20onfc7+DsAzDNv4yn2tJ69mlOeTL5kX3o2iLv2GG0/X4KCdaYSZVvBkl6zQZgm678AdXPCHTaFH2EDtzx6bKpdUFgKTbNi+m6VTb3pRzt/8pXy+xDG+xhYFlz09NeOgtW9ui1Oc2rVFT0Udnj7k81f/e/lRGHqKozUyTE79c/2cjld+400KVxudeyEwRcysB2MtIsAD8uPCbiIY37ip/fUoTi+mvtmoGBf4d9cS+4dY0bJ9w+0AmIN/m+tSx88SVkwHhwrFDfEmMYSwbhq3VE7eAvlp0sxBRrZuCMfh8nI05gSxDlBIZQlSkqIGfPlsXBFPauPAjSUKAqghh5sTFm7jBzAo4PVxrcJdKJlj06hkySQsHcqJ3eMVnm599W0G9z4p1LApAe53+Hg6ApVNBfz8UNLl6y2ZckCzKD327ua/+TE5Lj3pSFnq3tgw== 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. Update Documentation/ to describe how these flags are used (neither the COPY nor the new CONTINUE versions of this mode flag were described there before). [1]: https://patchwork.kernel.org/project/linux-mm/cover/20230218002819.1486479-1-jthoughton@google.com/ Acked-by: Peter Xu Acked-by: Mike Rapoport (IBM) Signed-off-by: Axel Rasmussen --- Documentation/admin-guide/mm/userfaultfd.rst | 8 ++++++++ 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 ++++ 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/mm/userfaultfd.rst b/Documentation/admin-guide/mm/userfaultfd.rst index 7dc823b56ca4..0ce400f8da93 100644 --- a/Documentation/admin-guide/mm/userfaultfd.rst +++ b/Documentation/admin-guide/mm/userfaultfd.rst @@ -219,6 +219,14 @@ former will have ``UFFD_PAGEFAULT_FLAG_WP`` set, the latter you still need to supply a page when ``UFFDIO_REGISTER_MODE_MISSING`` was used. +When using ``UFFDIO_REGISTER_MODE_WP`` in combination with either +``UFFDIO_REGISTER_MODE_MISSING`` or ``UFFDIO_REGISTER_MODE_MINOR``, when +resolving missing / minor faults with ``UFFDIO_COPY`` or ``UFFDIO_CONTINUE`` +respectively, it may be desirable for the new page / mapping to be +write-protected (so future writes will also result in a WP fault). These ioctls +support a mode flag (``UFFDIO_COPY_MODE_WP`` or ``UFFDIO_CONTINUE_MODE_WP`` +respectively) to configure the mapping this way. + QEMU/KVM ======== 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 a948d92154f5..fd6d7d80b6ea 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -83,7 +83,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 9202c1fc79ba..048beb5d0edd 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");