From patchwork Thu Jun 29 20:50:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 114446 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9916051vqr; Thu, 29 Jun 2023 14:11:50 -0700 (PDT) X-Google-Smtp-Source: APBJJlEGH9pEawKxYfkVTqD2zKfooAtaoskQNu2Vacwup/8Wt6o9dycG/HcoQs98fU7TgXpzkuqL X-Received: by 2002:a17:90a:8042:b0:262:e045:a6af with SMTP id e2-20020a17090a804200b00262e045a6afmr493843pjw.18.1688073109708; Thu, 29 Jun 2023 14:11:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688073109; cv=none; d=google.com; s=arc-20160816; b=fmSc57x9vVz4P15ADeNAPCCPX6gmGhJTF9z7Ja3O2eSQtOXPIe+HoU7oD4mWtsQY/A r/hibCfATnLOd4+nHVdewny3cISyEbxSvQSx873od3zvjz1086b8191jJG8QGG2mq1cR cF+YYCwOWvQhdu9Uo295vJK+daj4U1xgpwR62qaA7/c6zql2wHO8+luHoPV3uyNZkkgw mG7TWNHOu4obWIYkdCg6x6+oEGfjoOciEG9Gv8HSpSQFk532UWF376POBe+6hPRff5hh vlOWK+2crK3rrtwSRcR6MOQcHj7Po8equQjhWcC1sjZLYz87tE0QheTiT3i0PDVO7EHA 6oPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=n31snoBoqTjmW3LOSDrR7Vzlxun4GSqbUT1PJH9sHYA=; fh=eRaYFbc68TLHCmej1r/Q9kziindMZl6i1kh9xMHvl2k=; b=iewiGYMShT+jrn9EbyDoXHKGktDK3IF8bAdqBsBfhCrJLu2Nm6NYKJSalzFBwNJ5ue hKpMb82ivF+ucjmWPj0z6IJHD7MALabdPuNLtlRTb79ZQHBxCTbhE1GbgKjNk1hos2UC 01NclOML2ASWMgdu/S11d5KQr4qYJbPRa0qJ4H2ctwgki3lTK8BV4ngmYyIi0ThiQ4FS EmUqleU1rtBz1UpL+mnAY9yT++LbwjBVChpO8NwD7O8Kr3FTij0WgMcQlu5dMEXm83GG k5lC3hJwQ/j6+AryMj3dPAF7vFPbrAvKaURyeU3CQ1CgvRORc2jkQ4c+0CGyJaZy+ubC MO8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Ut9VLeaC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s61-20020a17090a2f4300b0025c02c13dfbsi13563431pjd.100.2023.06.29.14.11.34; Thu, 29 Jun 2023 14:11:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Ut9VLeaC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232240AbjF2Uu4 (ORCPT + 99 others); Thu, 29 Jun 2023 16:50:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231950AbjF2Uuv (ORCPT ); Thu, 29 Jun 2023 16:50:51 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2743B2D4A for ; Thu, 29 Jun 2023 13:50:49 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bb2a7308f21so939915276.2 for ; Thu, 29 Jun 2023 13:50:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688071848; x=1690663848; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=n31snoBoqTjmW3LOSDrR7Vzlxun4GSqbUT1PJH9sHYA=; b=Ut9VLeaCRmmMAVyM2bCZtqXgqEOBvF2UAZ9ooQLERry2XywKsFOp6Vs+Mt3/y1Q9md swkNVxtOZc2GH7cv2qiPiSGkrSmTkhI4kqORgRlzoVBbgg7cQIVc/rcmIA/x8cYpvSCl uTbxQzPvE5rHK4Nd113c2aEWWMRS9LJbucjC3HVmnRDp2IQmlYsdGAI8PPNILNjdHyU8 3F00ri/hxhXpJj8E6lioDYe867VXsmh4zUq2dF/V1vNd7PwNp8LMgoLdNF3V05iO5BBb WB3sHkODIx05aSDHExdUWM1Q9OAEkiP0/E5mcJm640ZTcWo4vgmV50jP04IPPVy80wY6 v5Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688071848; x=1690663848; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=n31snoBoqTjmW3LOSDrR7Vzlxun4GSqbUT1PJH9sHYA=; b=UHn6p1/DlU3BuTEIm+NbI+qWlkVmg/+A6GokE99kmNF0opCO9Ed7yqV+i2zD6FSvi/ 4ma67FmOrvq7zYv9asFX+VQdF4NMMn6OCOVqwR2LzGMBimQ3+j7QzlW5/U68Q8XP158r zcF3YdxzyxzRWpU0x85/Pnca8GDzVDQXGw9GztFnDsZWhCJEYIdc11CRHHQHgxKQT7Hg 05v7sZ6KMZ4Wbjv73jhyjgOiL+g8Jx/T1S/+SgxDGtKrX0Yh2ZsUlh9MQpN9mVh6i5HC tAZc03wmR+CvehhAPyzsN7UCPynqZz8VomyV/VqnNTZQi/nghem4Y0CMJjEyZgm6Itng Gf5Q== X-Gm-Message-State: ABy/qLYiBhCA/q0fAWXCZX7H5N3uhiq9Tz8LuWyrZEJABwn2gi+mZ2dG 0OQVa9HtpggtFf12SEzaTdJphzwc+HdMrpcp37ah X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:e20f:5917:3efa:d4bb]) (user=axelrasmussen job=sendgmr) by 2002:a25:aa61:0:b0:c1d:4fce:452 with SMTP id s88-20020a25aa61000000b00c1d4fce0452mr5988ybi.1.1688071848320; Thu, 29 Jun 2023 13:50:48 -0700 (PDT) Date: Thu, 29 Jun 2023 13:50:35 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230629205040.665834-1-axelrasmussen@google.com> Subject: [PATCH v2 1/6] mm: userfaultfd: add new UFFDIO_POISON ioctl From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Christian Brauner , David Hildenbrand , Huang Ying , Hugh Dickins , James Houghton , Jiaqi Yan , Jonathan Corbet , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Shuah Khan , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770072949327101926?= X-GMAIL-MSGID: =?utf-8?q?1770072949327101926?= The basic idea here is to "simulate" memory poisoning for VMs. A VM running on some host might encounter a memory error, after which some page(s) are poisoned (i.e., future accesses SIGBUS). They expect that once poisoned, pages can never become "un-poisoned". So, when we live migrate the VM, we need to preserve the poisoned status of these pages. When live migrating, we try to get the guest running on its new host as quickly as possible. So, we start it running before all memory has been copied, and before we're certain which pages should be poisoned or not. So the basic way to use this new feature is: - On the new host, the guest's memory is registered with userfaultfd, in either MISSING or MINOR mode (doesn't really matter for this purpose). - On any first access, we get a userfaultfd event. At this point we can communicate with the old host to find out if the page was poisoned. - If so, we can respond with a UFFDIO_POISON - this places a swap marker so any future accesses will SIGBUS. Because the pte is now "present", future accesses won't generate more userfaultfd events, they'll just SIGBUS directly. UFFDIO_POISON does not handle unmapping previously-present PTEs. This isn't needed, because during live migration we want to intercept all accesses with userfaultfd (not just writes, so WP mode isn't useful for this). So whether minor or missing mode is being used (or both), the PTE won't be present in any case, so handling that case isn't needed. Why return VM_FAULT_HWPOISON instead of VM_FAULT_SIGBUS when one of these markers is encountered? For "normal" userspace programs there isn't a big difference, both yield a SIGBUS. The difference for KVM is key though: VM_FAULT_HWPOISON will result in an MCE being injected into the guest (which is the behavior we want). With VM_FAULT_SIGBUS, the hypervisor would need to catch the SIGBUS and deal with the MCE injection itself. Signed-off-by: Axel Rasmussen --- fs/userfaultfd.c | 63 ++++++++++++++++++++++++++++++++ include/linux/swapops.h | 3 +- include/linux/userfaultfd_k.h | 4 ++ include/uapi/linux/userfaultfd.h | 25 +++++++++++-- mm/memory.c | 4 ++ mm/userfaultfd.c | 62 ++++++++++++++++++++++++++++++- 6 files changed, 156 insertions(+), 5 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 7cecd49e078b..c26a883399c9 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1965,6 +1965,66 @@ static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg) return ret; } +static inline int userfaultfd_poison(struct userfaultfd_ctx *ctx, unsigned long arg) +{ + __s64 ret; + struct uffdio_poison uffdio_poison; + struct uffdio_poison __user *user_uffdio_poison; + struct userfaultfd_wake_range range; + + user_uffdio_poison = (struct uffdio_poison __user *)arg; + + ret = -EAGAIN; + if (atomic_read(&ctx->mmap_changing)) + goto out; + + ret = -EFAULT; + if (copy_from_user(&uffdio_poison, user_uffdio_poison, + /* don't copy the output fields */ + sizeof(uffdio_poison) - (sizeof(__s64)))) + goto out; + + ret = validate_range(ctx->mm, uffdio_poison.range.start, + uffdio_poison.range.len); + if (ret) + goto out; + + ret = -EINVAL; + /* double check for wraparound just in case. */ + if (uffdio_poison.range.start + uffdio_poison.range.len <= + uffdio_poison.range.start) { + goto out; + } + if (uffdio_poison.mode & ~UFFDIO_POISON_MODE_DONTWAKE) + goto out; + + if (mmget_not_zero(ctx->mm)) { + ret = mfill_atomic_poison(ctx->mm, uffdio_poison.range.start, + uffdio_poison.range.len, + &ctx->mmap_changing, 0); + mmput(ctx->mm); + } else { + return -ESRCH; + } + + if (unlikely(put_user(ret, &user_uffdio_poison->updated))) + return -EFAULT; + if (ret < 0) + goto out; + + /* len == 0 would wake all */ + BUG_ON(!ret); + range.len = ret; + if (!(uffdio_poison.mode & UFFDIO_POISON_MODE_DONTWAKE)) { + range.start = uffdio_poison.range.start; + wake_userfault(ctx, &range); + } + ret = range.len == uffdio_poison.range.len ? 0 : -EAGAIN; + +out: + return ret; +} + static inline unsigned int uffd_ctx_features(__u64 user_features) { /* @@ -2066,6 +2126,9 @@ static long userfaultfd_ioctl(struct file *file, unsigned cmd, case UFFDIO_CONTINUE: ret = userfaultfd_continue(ctx, arg); break; + case UFFDIO_POISON: + ret = userfaultfd_poison(ctx, arg); + break; } return ret; } diff --git a/include/linux/swapops.h b/include/linux/swapops.h index 4c932cb45e0b..8259fee32421 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -394,7 +394,8 @@ typedef unsigned long pte_marker; #define PTE_MARKER_UFFD_WP BIT(0) #define PTE_MARKER_SWAPIN_ERROR BIT(1) -#define PTE_MARKER_MASK (BIT(2) - 1) +#define PTE_MARKER_UFFD_POISON BIT(2) +#define PTE_MARKER_MASK (BIT(3) - 1) static inline swp_entry_t make_pte_marker_entry(pte_marker marker) { diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index ac7b0c96d351..ac8c6854097c 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -46,6 +46,7 @@ enum mfill_atomic_mode { MFILL_ATOMIC_COPY, MFILL_ATOMIC_ZEROPAGE, MFILL_ATOMIC_CONTINUE, + MFILL_ATOMIC_POISON, NR_MFILL_ATOMIC_MODES, }; @@ -83,6 +84,9 @@ extern ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, extern ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long dst_start, unsigned long len, atomic_t *mmap_changing, uffd_flags_t flags); +extern ssize_t mfill_atomic_poison(struct mm_struct *dst_mm, unsigned long start, + 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 66dd4cd277bd..62151706c5a3 100644 --- a/include/uapi/linux/userfaultfd.h +++ b/include/uapi/linux/userfaultfd.h @@ -39,7 +39,8 @@ UFFD_FEATURE_MINOR_SHMEM | \ UFFD_FEATURE_EXACT_ADDRESS | \ UFFD_FEATURE_WP_HUGETLBFS_SHMEM | \ - UFFD_FEATURE_WP_UNPOPULATED) + UFFD_FEATURE_WP_UNPOPULATED | \ + UFFD_FEATURE_POISON) #define UFFD_API_IOCTLS \ ((__u64)1 << _UFFDIO_REGISTER | \ (__u64)1 << _UFFDIO_UNREGISTER | \ @@ -49,12 +50,14 @@ (__u64)1 << _UFFDIO_COPY | \ (__u64)1 << _UFFDIO_ZEROPAGE | \ (__u64)1 << _UFFDIO_WRITEPROTECT | \ - (__u64)1 << _UFFDIO_CONTINUE) + (__u64)1 << _UFFDIO_CONTINUE | \ + (__u64)1 << _UFFDIO_POISON) #define UFFD_API_RANGE_IOCTLS_BASIC \ ((__u64)1 << _UFFDIO_WAKE | \ (__u64)1 << _UFFDIO_COPY | \ + (__u64)1 << _UFFDIO_WRITEPROTECT | \ (__u64)1 << _UFFDIO_CONTINUE | \ - (__u64)1 << _UFFDIO_WRITEPROTECT) + (__u64)1 << _UFFDIO_POISON) /* * Valid ioctl command number range with this API is from 0x00 to @@ -71,6 +74,7 @@ #define _UFFDIO_ZEROPAGE (0x04) #define _UFFDIO_WRITEPROTECT (0x06) #define _UFFDIO_CONTINUE (0x07) +#define _UFFDIO_POISON (0x08) #define _UFFDIO_API (0x3F) /* userfaultfd ioctl ids */ @@ -91,6 +95,8 @@ struct uffdio_writeprotect) #define UFFDIO_CONTINUE _IOWR(UFFDIO, _UFFDIO_CONTINUE, \ struct uffdio_continue) +#define UFFDIO_POISON _IOWR(UFFDIO, _UFFDIO_POISON, \ + struct uffdio_poison) /* read() structure */ struct uffd_msg { @@ -225,6 +231,7 @@ struct uffdio_api { #define UFFD_FEATURE_EXACT_ADDRESS (1<<11) #define UFFD_FEATURE_WP_HUGETLBFS_SHMEM (1<<12) #define UFFD_FEATURE_WP_UNPOPULATED (1<<13) +#define UFFD_FEATURE_POISON (1<<14) __u64 features; __u64 ioctls; @@ -321,6 +328,18 @@ struct uffdio_continue { __s64 mapped; }; +struct uffdio_poison { + struct uffdio_range range; +#define UFFDIO_POISON_MODE_DONTWAKE ((__u64)1<<0) + __u64 mode; + + /* + * Fields below here are written by the ioctl and must be at the end: + * the copy_from_user will not read past here. + */ + __s64 updated; +}; + /* * Flags for the userfaultfd(2) system call itself. */ diff --git a/mm/memory.c b/mm/memory.c index d8a9a770b1f1..7fbda39e060d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3692,6 +3692,10 @@ static vm_fault_t handle_pte_marker(struct vm_fault *vmf) if (WARN_ON_ONCE(!marker)) return VM_FAULT_SIGBUS; + /* Poison emulation explicitly requested for this PTE. */ + if (marker & PTE_MARKER_UFFD_POISON) + return VM_FAULT_HWPOISON; + /* Higher priority than uffd-wp when data corrupted */ if (marker & PTE_MARKER_SWAPIN_ERROR) return VM_FAULT_SIGBUS; diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index a2bf37ee276d..87b62ca1e09e 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -286,6 +286,51 @@ static int mfill_atomic_pte_continue(pmd_t *dst_pmd, goto out; } +/* Handles UFFDIO_POISON for all non-hugetlb VMAs. */ +static int mfill_atomic_pte_poison(pmd_t *dst_pmd, + struct vm_area_struct *dst_vma, + unsigned long dst_addr, + uffd_flags_t flags) +{ + int ret; + struct mm_struct *dst_mm = dst_vma->vm_mm; + pte_t _dst_pte, *dst_pte; + spinlock_t *ptl; + + _dst_pte = make_pte_marker(PTE_MARKER_UFFD_POISON); + dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); + + if (vma_is_shmem(dst_vma)) { + struct inode *inode; + pgoff_t offset, max_off; + + /* serialize against truncate with the page table lock */ + inode = dst_vma->vm_file->f_inode; + offset = linear_page_index(dst_vma, dst_addr); + max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); + ret = -EFAULT; + if (unlikely(offset >= max_off)) + goto out_unlock; + } + + ret = -EEXIST; + /* + * For now, we don't handle unmapping pages, so only support filling in + * none PTEs, or replacing PTE markers. + */ + if (!pte_none_mostly(*dst_pte)) + goto out_unlock; + + set_pte_at(dst_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; +out_unlock: + pte_unmap_unlock(dst_pte, ptl); + return ret; +} + static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address) { pgd_t *pgd; @@ -336,8 +381,12 @@ static __always_inline ssize_t mfill_atomic_hugetlb( * supported by hugetlb. A PMD_SIZE huge pages may exist as used * by THP. Since we can not reliably insert a zero page, this * feature is not supported. + * + * PTE marker handling for hugetlb is a bit special, so for now + * UFFDIO_POISON is not supported. */ - if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE)) { + if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE) || + uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) { mmap_read_unlock(dst_mm); return -EINVAL; } @@ -481,6 +530,9 @@ static __always_inline ssize_t mfill_atomic_pte(pmd_t *dst_pmd, if (uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) { return mfill_atomic_pte_continue(dst_pmd, dst_vma, dst_addr, flags); + } else if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) { + return mfill_atomic_pte_poison(dst_pmd, dst_vma, + dst_addr, flags); } /* @@ -702,6 +754,14 @@ ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long start, uffd_flags_set_mode(flags, MFILL_ATOMIC_CONTINUE)); } +ssize_t mfill_atomic_poison(struct mm_struct *dst_mm, unsigned long start, + 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(flags, MFILL_ATOMIC_POISON)); +} + long uffd_wp_range(struct vm_area_struct *dst_vma, unsigned long start, unsigned long len, bool enable_wp) { From patchwork Thu Jun 29 20:50:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 114443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9910300vqr; Thu, 29 Jun 2023 14:02:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6/xE+C1QyW2pPLix8H/5UYGuVAIQloElpm1GPS9qP8Tp7Degj0ePI4QIzbJbD92cvSqHKT X-Received: by 2002:a05:6a20:394a:b0:119:4141:6989 with SMTP id r10-20020a056a20394a00b0011941416989mr6217093pzg.13.1688072548783; Thu, 29 Jun 2023 14:02:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688072548; cv=none; d=google.com; s=arc-20160816; b=SeNm1+Fanw+ReleOtI/eRvGQtt7ZSrCKtpNdirPxkPzJc7RGCZSc5iG44KALkV2aYF ylGdNLESS4dKMxDscWla+P2ABAMzPKdKmvWHyMEniiVpaXWIqqQNHNDvzfivDcXBQQMX RYwV+SQsyjvdxSCEgDH1Wq0oLjAPNfpnLyTeUAQxl8QwSZxXuk7W7ke68460tZ+XPIWr XBvbSFh3NqHH+IRwECzpU5pW18pt/Kl7OhOq60woJ9Czodh9QY7sYVE1RnHQKK1y1SQQ 1d08vKca1FunpLrjg689YSeOo2e9kWuGbCHvty7IEfY8L6XFKD8VB/9SOLhSSfW6OD8i FW8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=R8jkAmCGbYNygAZgSop/zKP0BAnGYhZIuFEhOa1in5M=; fh=eRaYFbc68TLHCmej1r/Q9kziindMZl6i1kh9xMHvl2k=; b=dkAx1BGXRd4TTiIeWuoGN4QIu5ROvcp3TY02RqrXtAnCHz5izTlm7uiJCrW6+zVB4+ L4lBS3sQ7/M/c0Ptm0SjGZmsLJkpGu6GiPQZEX2tSsYF2GlK2aPiKdlOAgf/I7pXKdcz fDXjkzc0krTsSzyGOmnKGrKeLr0yS63xgXLpc+GHB2oQ6hlGzNC/h6kveplKiS1cuSUx 1XXP1ULJi2MboNWt1xS51niMWeGj3gjd76QxzUNC4t5fVWV5BG7tBUEyUTHa0nK7dp5G RpUDSzjC1s4l+ny6RRKgcWvwOaWnpsQ0YuDto9tXE5fOI1VU9zd4CetWxoVn2t8c2u0x CZ1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=a8O0wagy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j30-20020a63231e000000b005429248576asi11522853pgj.128.2023.06.29.14.02.13; Thu, 29 Jun 2023 14:02:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=a8O0wagy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232301AbjF2Uu7 (ORCPT + 99 others); Thu, 29 Jun 2023 16:50:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232017AbjF2Uuy (ORCPT ); Thu, 29 Jun 2023 16:50:54 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD5533582 for ; Thu, 29 Jun 2023 13:50:50 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bff1f2780ebso923590276.1 for ; Thu, 29 Jun 2023 13:50:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688071850; x=1690663850; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R8jkAmCGbYNygAZgSop/zKP0BAnGYhZIuFEhOa1in5M=; b=a8O0wagyUtUHav4dJ+0FTU4BfULv1hqXducKGWGQFuo+G+tIPjHy7DS1ADLv5ngmlB RSngMc0CnR4cfT4Dl1/3ekbmm7f4jkeC6RGHf37xUtQNDImjBtBI9DhIP8Z9AdXeugxu EEIqVXFJE09pcCYmkIu4KbepOYvezPBB0mUQ8g4biI54D+MfCLg+3QhsHFv6yxYBACnS hW5iDv/qv08V0YHMgwph0e9b+5pGUlXVr84spJ6yslQMHWase0Q3RfrmBefxTbxnzdJF i1oPLS3gMTFjIwMQZ2twKVJdYWAw+m3Lbi649+iUQ0hPFKbJ4IuR/hqznd3O1UEd0bUk 66bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688071850; x=1690663850; 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=R8jkAmCGbYNygAZgSop/zKP0BAnGYhZIuFEhOa1in5M=; b=UU0j/A20Nf5Eae7TIgpnsVC5CZlOtlXPaK4NjsSnzenRO4v9yJsVkScXJ4wVT6G42R 3Euv/CNsJA9RWg+3FdE8/2wURE4VBqKdXXzfT8op5yz+ujyUbHoVuspaAtQK0tu2Za8O MnjL/+T5cwfEUppNYynBTU2y2u3gSqjkl5ljjPTRgyseFnHeornChNvMgpo/KQQBRdQ9 X1rg5JC66T9mWhn5SWeN7PjYM5ShJDedbLRnomTwBsPWfNNF16DbmrHEDVxl4P8RDTBg QbAS5yusYDAcUDxYsTlFi2vKQLsrMExmuQM+KWIdf+QYkCAkixxHLHJHCBG9ELG/9L7f USNQ== X-Gm-Message-State: ABy/qLYgauaQS3mNXj2oAX2Hk6nebqer1MItGDDh0XFtUn2rTRPbqy7K Bq0YaNgvV4NuLGmCQ6EaowixIPW1G9J9zJW6uLl2 X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:e20f:5917:3efa:d4bb]) (user=axelrasmussen job=sendgmr) by 2002:a25:738e:0:b0:bcc:285c:66ea with SMTP id o136-20020a25738e000000b00bcc285c66eamr5716ybc.3.1688071850044; Thu, 29 Jun 2023 13:50:50 -0700 (PDT) Date: Thu, 29 Jun 2023 13:50:36 -0700 In-Reply-To: <20230629205040.665834-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230629205040.665834-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230629205040.665834-2-axelrasmussen@google.com> Subject: [PATCH v2 2/6] mm: userfaultfd: refactor hugetlb folio allocation / lookup code From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Christian Brauner , David Hildenbrand , Huang Ying , Hugh Dickins , James Houghton , Jiaqi Yan , Jonathan Corbet , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Shuah Khan , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770072360513766721?= X-GMAIL-MSGID: =?utf-8?q?1770072360513766721?= At the top of `hugetlb_mfill_atomic_pte`, we need to get the folio we're going to be mapping. There are three basic cases we're dealing with here: 1. We're doing a UFFDIO_CONTINUE, in which case we lookup an existing folio in the pagecache, instead of allocating a new one. 2. We need to allocate a new folio. 3. We previously failed while populating our new folio, so we "returned" a temporary folio using `foliop` and had our caller retry. In a future commit I'm going to add a fourth case for UFFDIO_POISON, where we aren't going to map a folio at all (newly allocated or otherwise). This end state will be simpler, and we can re-use a bit more code, if we stop using `if (...)` to distinguish the cases. So, refactor the cases so they share most of the same code, and instead switch to `goto` to skip some parts depending on the case at hand. Signed-off-by: Axel Rasmussen --- mm/hugetlb.c | 53 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index bce28cca73a1..38711d49e4db 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6259,22 +6259,32 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, if (IS_ERR(folio)) goto out; folio_in_pagecache = true; - } else if (!*foliop) { - /* If a folio already exists, then it's UFFDIO_COPY for - * a non-missing case. Return -EEXIST. - */ - if (vm_shared && - hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { - ret = -EEXIST; - goto out; + goto ready; + } + + /* If a folio already exists, then it's UFFDIO_COPY for + * a non-missing case. Return -EEXIST. + */ + if (vm_shared && hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { + ret = -EEXIST; + if (*foliop) { + folio_put(*foliop); + *foliop = NULL; } + goto out; + } - folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0); - if (IS_ERR(folio)) { - ret = -ENOMEM; - goto out; + folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0); + if (IS_ERR(folio)) { + ret = -ENOMEM; + if (*foliop) { + folio_put(*foliop); + *foliop = NULL; } + goto out; + } + if (!*foliop) { ret = copy_folio_from_user(folio, (const void __user *) src_addr, false); @@ -6302,22 +6312,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, */ goto out; } - } else { - if (vm_shared && - hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { - folio_put(*foliop); - ret = -EEXIST; - *foliop = NULL; - goto out; - } - - folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0); - if (IS_ERR(folio)) { - folio_put(*foliop); - ret = -ENOMEM; - *foliop = NULL; - goto out; - } + } else { /* Caller retried because we set *foliop previously */ ret = copy_user_large_folio(folio, *foliop, dst_addr, dst_vma); folio_put(*foliop); *foliop = NULL; @@ -6327,6 +6322,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, } } +ready: /* `folio` ready to map (non-NULL, populated) */ + /* * The memory barrier inside __folio_mark_uptodate makes sure that * preceding stores to the page contents become visible before From patchwork Thu Jun 29 20:50:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 114439 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9905687vqr; Thu, 29 Jun 2023 13:52:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlHDdCMDxbIzx9bi12ZUIx1mB31lEqhyLyhY0kp7NzfUE0x7NK3lrgxPFk1zq2dMOKS5hUFF X-Received: by 2002:a17:902:f54c:b0:1b7:f061:fa6d with SMTP id h12-20020a170902f54c00b001b7f061fa6dmr298772plf.31.1688071977377; Thu, 29 Jun 2023 13:52:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688071977; cv=none; d=google.com; s=arc-20160816; b=YUi2uEvG8Hngn/BqVXB6P1H3UaiNcLKQEJollKJOOdIPYMwTZ6RHbJm1BEN9Xn/0JH m+IeLcqGVp8thBraKyknQx+LoghfVel02aiCcb2b0+WbuB3rRuQnZ3OlR/LRvzw3JNn2 ZDgHgcqkNMhYbhqncPe0vgLFX9FAGSOkR2AU9dvI1yUtBrQ40DTtErGzbeqq009BV9Vn boljOqVCyrz5lB2WbGlnUht61z9/lnm6KIdIl3Vgj8bWYUgyNZwz2RZsASoZAaJX5OZG L0Omy5tb0kJWEUYvCgUDxCn/EjUY8CcRY/l4LYXC6racaB5HVoBSRFhx7vwv6CNTlDQM 1h8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=oot+BFijmfKZ0KO/nJIrIU5HcDe1IwfMU2NYWHhWcUA=; fh=eRaYFbc68TLHCmej1r/Q9kziindMZl6i1kh9xMHvl2k=; b=tyY209OoXWTCd2XI0PHz1la3Uc0E/n23t0fOwxw5PP9TDoFfhPxscC5DJCqmfvkG2P 8L+OBL345vK8ogpqgjOvhk6uUP4vF4cmQHrj35p+6Jz/VImUwC04wrETez/JRbEiXLgt DcGMmKj7NaVu40Y2PEqYBML3aw4X3Gg++5C4070Uw7M5K0O8/ra/CId4jb6fSqmkw9VO vXDkpHw3DqOHc3Sv08Ho5+drXWvtoL8YOEyC13yP0i+gWhWUOiekD9u/KEyI/15F6buI lDmH6/Q1Xap8vEU+dXD+u+N8ewMTIOYWBGoweTCtfc3TtfC2e4uE1Agp4E6i00zv0cmg hNFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=dv8waobW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bg9-20020a1709028e8900b001b3d6d125f9si10395482plb.524.2023.06.29.13.52.39; Thu, 29 Jun 2023 13:52:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=dv8waobW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231787AbjF2UvD (ORCPT + 99 others); Thu, 29 Jun 2023 16:51:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231766AbjF2Uuz (ORCPT ); Thu, 29 Jun 2023 16:50:55 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1C553595 for ; Thu, 29 Jun 2023 13:50:52 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c01e1c0648aso879394276.1 for ; Thu, 29 Jun 2023 13:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688071852; x=1690663852; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=oot+BFijmfKZ0KO/nJIrIU5HcDe1IwfMU2NYWHhWcUA=; b=dv8waobWVaNSUsOT47GOsDx8vV/iRkGODf8zThZKK4JKq0ZSnTrMWy7R3IAiX1fM4t tegdL5nlYEw74WxJbU4ciZTSnZwOVQYCywDP/6JAHDEF3YVNRNQoSLNsxO+X0jE8+WLZ 4ib0DDeM9eP9xJoybwRbj9HMMqb5rE/qTj6br4mOJWTUvJsbEgikpTHZ4BkIpODJP7uS FeOaZbb/LsG8QdVorLcYBayDjEa0cgjzLqqSuGqP5etuMwIPhztE5czIvy2CmabhCZfJ vnsHoy7ITFctcEgQKTEC1tg/xVWWYMTZvhXULy5xYz0ZzCxTiOdKZ00s3BGxSv+HUqqN HmAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688071852; x=1690663852; 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=oot+BFijmfKZ0KO/nJIrIU5HcDe1IwfMU2NYWHhWcUA=; b=gmvXkJJcdkiNMYjIs4DSXTkOWPOEwBxaQxZMp5luhWtYR2Ou555i9xIc1scqt66MB0 iyH+OZM7GjRtSAHTDgg+o1B12tfsUnQQs8d9CXF0X4kTLLrXoyYDBNrjNgUr56uAv0fr dMWIC+Q0hEUWvMG+bK2O8Rku2RLLWPNfHRtPIYXRYrsmcavjoThUZ//23738QDnFnglY FieFa91JqNy3mwbe+49HG7J2QnPuTYi0tIWckiGoqAE2uRE1JcDT+JdsaJY0kwXMpghb 3bXrJ2Il2yGtf2ETB1Kw+ifH9S54E7MnvrDaaE75x++n8MwTmXq3t1ImYE2WRNX64ldh PXhQ== X-Gm-Message-State: ABy/qLYP43xkFcKA+xtjJm8bdHguB89agurh3g1dJgZmPPFX36YOG9cJ TZTTOfsmHMtEHQ1b8A3518ZYZawGYg7VCSAFJcZQ X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:e20f:5917:3efa:d4bb]) (user=axelrasmussen job=sendgmr) by 2002:a25:f621:0:b0:bc7:f6af:8cff with SMTP id t33-20020a25f621000000b00bc7f6af8cffmr5650ybd.2.1688071852014; Thu, 29 Jun 2023 13:50:52 -0700 (PDT) Date: Thu, 29 Jun 2023 13:50:37 -0700 In-Reply-To: <20230629205040.665834-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230629205040.665834-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230629205040.665834-3-axelrasmussen@google.com> Subject: [PATCH v2 3/6] mm: userfaultfd: support UFFDIO_POISON for hugetlbfs From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Christian Brauner , David Hildenbrand , Huang Ying , Hugh Dickins , James Houghton , Jiaqi Yan , Jonathan Corbet , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Shuah Khan , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770071761508889704?= X-GMAIL-MSGID: =?utf-8?q?1770071761508889704?= The behavior here is the same as it is for anon/shmem. This is done separately because hugetlb pte marker handling is a bit different. Signed-off-by: Axel Rasmussen --- mm/hugetlb.c | 33 +++++++++++++++++++++++++++++++-- mm/userfaultfd.c | 6 +----- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 38711d49e4db..05abe88986b6 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6090,14 +6090,24 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, } entry = huge_ptep_get(ptep); - /* PTE markers should be handled the same way as none pte */ - if (huge_pte_none_mostly(entry)) + if (huge_pte_none_mostly(entry)) { + if (is_pte_marker(entry)) { + unsigned long marker = pte_marker_get(pte_to_swp_entry(entry)); + + if (marker & PTE_MARKER_UFFD_POISON) { + ret = VM_FAULT_HWPOISON_LARGE; + goto out_mutex; + } + } /* + * Other PTE markers should be handled the same way as none PTE. + * * hugetlb_no_page will drop vma lock and hugetlb fault * mutex internally, which make us return immediately. */ return hugetlb_no_page(mm, vma, mapping, idx, address, ptep, entry, flags); + } ret = 0; @@ -6253,6 +6263,25 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, int writable; bool folio_in_pagecache = false; + if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) { + ptl = huge_pte_lock(h, dst_mm, dst_pte); + + /* Don't overwrite any existing PTEs (even markers) */ + if (!huge_pte_none(huge_ptep_get(dst_pte))) { + spin_unlock(ptl); + return -EEXIST; + } + + _dst_pte = make_pte_marker(PTE_MARKER_UFFD_POISON); + set_huge_pte_at(dst_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); + + spin_unlock(ptl); + return 0; + } + if (is_continue) { ret = -EFAULT; folio = filemap_lock_folio(mapping, idx); diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 87b62ca1e09e..4436cae1c7a8 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -381,12 +381,8 @@ static __always_inline ssize_t mfill_atomic_hugetlb( * supported by hugetlb. A PMD_SIZE huge pages may exist as used * by THP. Since we can not reliably insert a zero page, this * feature is not supported. - * - * PTE marker handling for hugetlb is a bit special, so for now - * UFFDIO_POISON is not supported. */ - if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE) || - uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) { + if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE)) { mmap_read_unlock(dst_mm); return -EINVAL; } From patchwork Thu Jun 29 20:50:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 114444 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9910449vqr; Thu, 29 Jun 2023 14:02:44 -0700 (PDT) X-Google-Smtp-Source: APBJJlFuK6nPNO+mP3U2j59VWg9FIVD4XKfm1c7xtcqEypqffIrLifG7QRIaYKBFwufWaQ1xMhx/ X-Received: by 2002:a17:90a:dc0c:b0:262:d4cf:9f71 with SMTP id i12-20020a17090adc0c00b00262d4cf9f71mr436324pjv.39.1688072563832; Thu, 29 Jun 2023 14:02:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688072563; cv=none; d=google.com; s=arc-20160816; b=xJRk4+65NXUv3HQpDpi3qRHPpidviX1qNZnQ3xO6/dITfK2/9APJC3epHGXUH9GvWE Sv0eQsBL3DU6PziZaytAMKnOK/emnibK+DBB+DeDwyTlizuRAMgjbPYgCU6/jEhCp5mF AXhtJ7u2GkhhoVFiPLL8aEidCyEm+gu4+bUwvwk8ktgg7KVn8oZJks/K+BOXUCme1CcD dxzAal0BvkwEq1DYVb0oD0giWswu8iihZIOcBC95ccgLBs2KjtlREJoGbM+YGmEGCl31 d/38xnxMUGsKOzDuUaD4xJI/s64xcSHTVjThUQSVT+mes13b8Auk9rksUpouwbqLqCo3 Ve/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=0eR1m0YpsGhZ6GlqZ9XuYhhV42VymJr50nJOc8nZiNo=; fh=eRaYFbc68TLHCmej1r/Q9kziindMZl6i1kh9xMHvl2k=; b=A1RKAF0O3GdE1VfqUXXqaNlaBbIiFl3CQpBQhJX3QOLeta155Tl0yj+IEeUJdTphtH gidHowvf7G50u2AwHIQgtadpfnB4dmmATE0qexdEs+RdOQi/y21ciBRXWE1pmwUfLspV mng25heQc659nq5ltNUarvu2YFEJ+lygfLeJvAU+RMy7ZOn3Lze5qMWp0A9mr0CYLr8+ dtF6aY2xVAZNQ5/WbZ4bBCIuxDRtnUjV8iGn2icOgcv8YEu7zgBID1bh6D721kUFGkPz RYQ1d01vN0ybVhlLpmwgE5gk+Q7H9qPjWlGrrLVjh6toILD9K+S/mZ+jMKkgPrnBojpX /PwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=3cQsv9mP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mh9-20020a17090b4ac900b00259a11f3a69si12523175pjb.145.2023.06.29.14.02.28; Thu, 29 Jun 2023 14:02:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=3cQsv9mP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232097AbjF2UvH (ORCPT + 99 others); Thu, 29 Jun 2023 16:51:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232238AbjF2Uu4 (ORCPT ); Thu, 29 Jun 2023 16:50:56 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F66935A5 for ; Thu, 29 Jun 2023 13:50:54 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-569e7aec37bso9293437b3.2 for ; Thu, 29 Jun 2023 13:50:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688071853; x=1690663853; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=0eR1m0YpsGhZ6GlqZ9XuYhhV42VymJr50nJOc8nZiNo=; b=3cQsv9mPUr/J47bNoSUJhw0yv5GwYe9VZP9mYSYio932WB5S45QeiLp3sUUgqKWWVH dSxCe5jkZQeEy91/Za56iq/BaKt7D2eO5CKC5tFRQYXavX5XUCleRaiiLN/LYOpTc3Q5 TlyJDS6v9N9nUKx4AeHRICAHwHnZXdJB2EprVzPDgrV+x6B+GNNBpU5V0gfMamqVpbki nvs6vizS27HjxtsanHmKiVM1v+306yk8JhmYhHT5HisNsno4bY2z/AyQxRY5mcHLj0/S 73GQMk4SEbiPfcHxLnui5ipf505zkiJKpuRvNi5XJnjd8ujS1kh0z5BvTGrPJHuMpVHk xgHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688071853; x=1690663853; 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=0eR1m0YpsGhZ6GlqZ9XuYhhV42VymJr50nJOc8nZiNo=; b=IUwsO4EYXcaRfIoj5C/zhY8dV0n1vDhtbpcCj3HmFnXy5r5nYhthc65rNDZY1h52gs JUOLNsFcDxGzPU524VABQpzG6VpU58GgpFK8GRgrYa16oE1Mxo0043m4VnUGQcnCmF2Q 8T+XhwJWZV472eta4wHi969Lw/71gzajE3LgcGKZPAzvnv3e1hBQu9WULs1uIPfwXdmF Rj6SXaXqffo+J7bTeORM03hpVrOZ06bbljQUzn6ycfBUEchoL4yJkqYNphe+V7t4n2XC DIrr/AM14B1/GmG4Bjb8bAbDesNeVbcU5LGvQGlO/+EbdznWJ/aimbbc/haaotWqkem/ eCMw== X-Gm-Message-State: ABy/qLb2CUFE9WMIWxtEZu/S3FV+ZSu4LMBGTyh4ndhS2uPRDt3Cb4gZ 9FdCyULUQjPPGfQpupUp8g+6Z/25wml5L+Q6PX2s X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:e20f:5917:3efa:d4bb]) (user=axelrasmussen job=sendgmr) by 2002:a0d:dfd0:0:b0:56c:e2da:f440 with SMTP id i199-20020a0ddfd0000000b0056ce2daf440mr4758ywe.0.1688071853655; Thu, 29 Jun 2023 13:50:53 -0700 (PDT) Date: Thu, 29 Jun 2023 13:50:38 -0700 In-Reply-To: <20230629205040.665834-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230629205040.665834-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230629205040.665834-4-axelrasmussen@google.com> Subject: [PATCH v2 4/6] selftests/mm: refactor uffd_poll_thread to allow custom fault handlers From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Christian Brauner , David Hildenbrand , Huang Ying , Hugh Dickins , James Houghton , Jiaqi Yan , Jonathan Corbet , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Shuah Khan , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770072376873444261?= X-GMAIL-MSGID: =?utf-8?q?1770072376873444261?= Previously, we had "one fault handler to rule them all", which used several branches to deal with all of the scenarios required by all of the various tests. In upcoming patches, I plan to add a new test, which has its own slightly different fault handling logic. Instead of continuing to add cruft to the existing fault handler, let's allow tests to define custom ones, separate from other tests. Signed-off-by: Axel Rasmussen --- tools/testing/selftests/mm/uffd-common.c | 5 ++++- tools/testing/selftests/mm/uffd-common.h | 3 +++ tools/testing/selftests/mm/uffd-stress.c | 12 +++++++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c index ba20d7504022..02b89860e193 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -499,6 +499,9 @@ void *uffd_poll_thread(void *arg) int ret; char tmp_chr; + if (!args->handle_fault) + args->handle_fault = uffd_handle_page_fault; + pollfd[0].fd = uffd; pollfd[0].events = POLLIN; pollfd[1].fd = pipefd[cpu*2]; @@ -527,7 +530,7 @@ void *uffd_poll_thread(void *arg) err("unexpected msg event %u\n", msg.event); break; case UFFD_EVENT_PAGEFAULT: - uffd_handle_page_fault(&msg, args); + args->handle_fault(&msg, args); break; case UFFD_EVENT_FORK: close(uffd); diff --git a/tools/testing/selftests/mm/uffd-common.h b/tools/testing/selftests/mm/uffd-common.h index 197f5262fe0d..7c4fa964c3b0 100644 --- a/tools/testing/selftests/mm/uffd-common.h +++ b/tools/testing/selftests/mm/uffd-common.h @@ -77,6 +77,9 @@ struct uffd_args { unsigned long missing_faults; unsigned long wp_faults; unsigned long minor_faults; + + /* A custom fault handler; defaults to uffd_handle_page_fault. */ + void (*handle_fault)(struct uffd_msg *msg, struct uffd_args *args); }; struct uffd_test_ops { diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c index 995ff13e74c7..50b1224d72c7 100644 --- a/tools/testing/selftests/mm/uffd-stress.c +++ b/tools/testing/selftests/mm/uffd-stress.c @@ -189,10 +189,8 @@ static int stress(struct uffd_args *args) locking_thread, (void *)cpu)) return 1; if (bounces & BOUNCE_POLL) { - if (pthread_create(&uffd_threads[cpu], &attr, - uffd_poll_thread, - (void *)&args[cpu])) - return 1; + if (pthread_create(&uffd_threads[cpu], &attr, uffd_poll_thread, &args[cpu])) + err("uffd_poll_thread create"); } else { if (pthread_create(&uffd_threads[cpu], &attr, uffd_read_thread, @@ -247,9 +245,13 @@ static int userfaultfd_stress(void) { void *area; unsigned long nr; - struct uffd_args args[nr_cpus]; + struct uffd_args *args; uint64_t mem_size = nr_pages * page_size; + args = calloc(nr_cpus, sizeof(struct uffd_args)); + if (!args) + err("allocating args array failed"); + if (uffd_test_ctx_init(UFFD_FEATURE_WP_UNPOPULATED, NULL)) err("context init failed"); From patchwork Thu Jun 29 20:50:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 114447 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9920283vqr; Thu, 29 Jun 2023 14:20:40 -0700 (PDT) X-Google-Smtp-Source: APBJJlFvFvUgG4dsELP/xUvjAmO0E8UeJiUJCeaItKWx9Uy08g6OpcIDSzIPTa6RGXXCQIOBEN8C X-Received: by 2002:aa7:88cb:0:b0:666:ae6b:c484 with SMTP id k11-20020aa788cb000000b00666ae6bc484mr1517405pff.13.1688073640575; Thu, 29 Jun 2023 14:20:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688073640; cv=none; d=google.com; s=arc-20160816; b=zArJSq3UuyAFaLtdkeVHQ6vwFRSWO/GTL06quNIeSyCwgRmTtVyZul36OfypSaQnlj /2XYIRWV0aRU8uLRI+F4km4D88x8G+7UGAzh7wttMFQ3P1mSPb+dzMp5cr8DamXol64w fSXL94g5NgyeG0Q1LI6KBTkv5Uav1vLBWqz9+YPRw1Dcs7e1AAJ09S1yNUmLPNKXlS63 FOEOwYwZOpLAqbTibSeTba6wOM5YQXmgXmAQY7hAK+yum0iHzG3KpReH3gBZUztLMd6Y Ui+Rz3i3Cpzxaj8PxmZtZKnxbVCzgKcZd8suj/QlzTDFc4Hec/X8ACsZ88Zt0L4yOmQs vhPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=FDMn97NE4k9ts/MbSvQk7TIpBtwkvtlY8wmg3pzMxc0=; fh=eRaYFbc68TLHCmej1r/Q9kziindMZl6i1kh9xMHvl2k=; b=S5zPPfzNbgFxvLeSywlWs3AecUxNbukVeFY765Sh0GZLmwQdmQnyXl2yaZqrKdCXnQ UEAyA3DJ3iG50Wc31v7lfdRbd/LFsMEhZqx2F+R4EYwzqiRkH9zpDeWrQl4UmpNabNtv YYm7r0NhDryEuIOuUy2os6PGfVwaYZZiPwxO33WUuK87H5gz70Tg/zhiJi0R3VTMusgi i6anEqRxQWfh1txD/q6Ad1oXVpcADdPHWMytBapLwb0HvQ7tfKyTMPAge7BmIB3azypC PLPSytNB0tqCWa0X2WFIO0BR31IOKzsVf2Rsh8SkR6mp8ZxwxTSAOABVjOtT1ywoaQVN 16uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=b9ic7m0u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o21-20020a635d55000000b00557807d78e2si10826505pgm.555.2023.06.29.14.20.23; Thu, 29 Jun 2023 14:20:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=b9ic7m0u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230233AbjF2Uvw (ORCPT + 99 others); Thu, 29 Jun 2023 16:51:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231539AbjF2Uu6 (ORCPT ); Thu, 29 Jun 2023 16:50:58 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E32E35AC for ; Thu, 29 Jun 2023 13:50:56 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-57320c10635so9637497b3.3 for ; Thu, 29 Jun 2023 13:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688071855; x=1690663855; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FDMn97NE4k9ts/MbSvQk7TIpBtwkvtlY8wmg3pzMxc0=; b=b9ic7m0uviwwst/a/b6ej/KmTI1fbXLL05tIs7ZZktU/1u1vjioSSlgudRjzPPssiy tp8Df963g7Gw9okHBP74jobr3AMGXd284bnbTndsz/MzbhN8YGVA8qDN4fDKB/ndeZVg QJI4l1QU+fC4jjOAj9fa1B2eKR2xgGeEAW2jObEdLCiYk4pjpamRXhIA/AvCrm3AxS1A S57uq/giJWwpOzsio0l1XJxDbLPkIQcot63aSrBC/XjE/hIXayLN7QhrNIf6De7X0ojG MHaKy6jVInPl550XNyYzoiSALiiydqGt38OXbmQvLutW7JTQFw252iMVADLjBUBo1bab iXag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688071855; x=1690663855; 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=FDMn97NE4k9ts/MbSvQk7TIpBtwkvtlY8wmg3pzMxc0=; b=gbNK+ZXOjZIHkbKUi68Ps0uIZ4mKmtys0gZr7RAJ5zom9gvR9d431RcKw9LybjuTkj QI+QvqK+/HiOXJWB9uiOHrxUXL2/0KPF+8GcuOTHQCH95azjzXwZ6CxWRTrSy4zZxnQP NeMXoKBlIROfwH+wTnZKM2kRzlxrOGNgN1HER7gcAK5O3M6/iTB6VPfT/RiP3lU1HGMf r7iL4QDg9W22fTHQh9UneWq4W48ZvHiH/RQEbFZg550RPA56A7ZNMI546taG5Pt1/Im3 plNXFPQ/+uLAJsqQPIFwUR7vfQ7uBM59SJcmjgJdNIYVEUx7A3KVArPKdfz/fWsX8K9X tldg== X-Gm-Message-State: ABy/qLYHeLtUvk7JlR0uaXn1STVmqJoc6aCys1BuqHB9QFv1kk6eG58E 4ivrKrSTjlJk56x/Z5OXH00ym0Alk7ytinh83qJT X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:e20f:5917:3efa:d4bb]) (user=axelrasmussen job=sendgmr) by 2002:a25:d243:0:b0:c38:b4b5:13d5 with SMTP id j64-20020a25d243000000b00c38b4b513d5mr6655ybg.3.1688071855661; Thu, 29 Jun 2023 13:50:55 -0700 (PDT) Date: Thu, 29 Jun 2023 13:50:39 -0700 In-Reply-To: <20230629205040.665834-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230629205040.665834-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230629205040.665834-5-axelrasmussen@google.com> Subject: [PATCH v2 5/6] selftests/mm: add uffd unit test for UFFDIO_POISON From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Christian Brauner , David Hildenbrand , Huang Ying , Hugh Dickins , James Houghton , Jiaqi Yan , Jonathan Corbet , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Shuah Khan , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770073505857543897?= X-GMAIL-MSGID: =?utf-8?q?1770073505857543897?= The test is pretty basic, and exercises UFFDIO_POISON straightforwardly. We register a region with userfaultfd, in missing fault mode. For each fault, we either UFFDIO_COPY a zeroed page (odd pages) or UFFDIO_POISON (even pages). We do this mix to test "something like a real use case", where guest memory would be some mix of poisoned and non-poisoned pages. We read each page in the region, and assert that the odd pages are zeroed as expected, and the even pages yield a SIGBUS as expected. Why UFFDIO_COPY instead of UFFDIO_ZEROPAGE? Because hugetlb doesn't support UFFDIO_ZEROPAGE, and we don't want to have special case code. Signed-off-by: Axel Rasmussen Acked-by: Peter Xu --- tools/testing/selftests/mm/uffd-unit-tests.c | 117 +++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c index 04d91f144d1c..2709a34a39c5 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -951,6 +951,117 @@ static void uffd_zeropage_test(uffd_test_args_t *args) uffd_test_pass(); } +static void uffd_register_poison(int uffd, void *addr, uint64_t len) +{ + uint64_t ioctls = 0; + uint64_t expected = (1 << _UFFDIO_COPY) | (1 << _UFFDIO_POISON); + + if (uffd_register_with_ioctls(uffd, addr, len, true, + false, false, &ioctls)) + err("poison register fail"); + + if ((ioctls & expected) != expected) + err("registered area doesn't support COPY and POISON ioctls"); +} + +static void do_uffdio_poison(int uffd, unsigned long offset) +{ + struct uffdio_poison uffdio_poison = { 0 }; + int ret; + __s64 res; + + uffdio_poison.range.start = (unsigned long) area_dst + offset; + uffdio_poison.range.len = page_size; + uffdio_poison.mode = 0; + ret = ioctl(uffd, UFFDIO_POISON, &uffdio_poison); + res = uffdio_poison.updated; + + if (ret) + err("UFFDIO_POISON error: %"PRId64, (int64_t)res); + else if (res != page_size) + err("UFFDIO_POISON unexpected size: %"PRId64, (int64_t)res); +} + +static void uffd_poison_handle_fault( + struct uffd_msg *msg, struct uffd_args *args) +{ + unsigned long offset; + + if (msg->event != UFFD_EVENT_PAGEFAULT) + err("unexpected msg event %u", msg->event); + + if (msg->arg.pagefault.flags & + (UFFD_PAGEFAULT_FLAG_WP | UFFD_PAGEFAULT_FLAG_MINOR)) + err("unexpected fault type %llu", msg->arg.pagefault.flags); + + offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst; + offset &= ~(page_size-1); + + /* Odd pages -> copy zeroed page; even pages -> poison. */ + if (offset & page_size) + copy_page(uffd, offset, false); + else + do_uffdio_poison(uffd, offset); +} + +static void uffd_poison_test(uffd_test_args_t *targs) +{ + pthread_t uffd_mon; + char c; + struct uffd_args args = { 0 }; + struct sigaction act = { 0 }; + unsigned long nr_sigbus = 0; + unsigned long nr; + + fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK); + + uffd_register_poison(uffd, area_dst, nr_pages * page_size); + memset(area_src, 0, nr_pages * page_size); + + args.handle_fault = uffd_poison_handle_fault; + if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args)) + err("uffd_poll_thread create"); + + sigbuf = &jbuf; + act.sa_sigaction = sighndl; + act.sa_flags = SA_SIGINFO; + if (sigaction(SIGBUS, &act, 0)) + err("sigaction"); + + for (nr = 0; nr < nr_pages; ++nr) { + unsigned long offset = nr * page_size; + const char *bytes = (const char *) area_dst + offset; + const char *i; + + if (sigsetjmp(*sigbuf, 1)) { + /* + * Access below triggered a SIGBUS, which was caught by + * sighndl, which then jumped here. Count this SIGBUS, + * and move on to next page. + */ + ++nr_sigbus; + continue; + } + + for (i = bytes; i < bytes + page_size; ++i) { + if (*i) + err("nonzero byte in area_dst (%p) at %p: %u", + area_dst, i, *i); + } + } + + if (write(pipefd[1], &c, sizeof(c)) != sizeof(c)) + err("pipe write"); + if (pthread_join(uffd_mon, NULL)) + err("pthread_join()"); + + if (nr_sigbus != nr_pages / 2) + err("expected to receive %lu SIGBUS, actually received %lu", + nr_pages / 2, nr_sigbus); + + uffd_test_pass(); +} + /* * Test the returned uffdio_register.ioctls with different register modes. * Note that _UFFDIO_ZEROPAGE is tested separately in the zeropage test. @@ -1126,6 +1237,12 @@ uffd_test_case_t uffd_tests[] = { UFFD_FEATURE_PAGEFAULT_FLAG_WP | UFFD_FEATURE_WP_HUGETLBFS_SHMEM, }, + { + .name = "poison", + .uffd_fn = uffd_poison_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = UFFD_FEATURE_POISON, + }, }; static void usage(const char *prog) From patchwork Thu Jun 29 20:50:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 114441 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9905826vqr; Thu, 29 Jun 2023 13:53:15 -0700 (PDT) X-Google-Smtp-Source: APBJJlGWuiqBlUUFg7SO3wCnvYFsBgpk+XHTaBGuWOm6FtCk96uYnx83gTXx4S1rbxabiID4B03I X-Received: by 2002:a05:6870:aa0f:b0:19f:45a1:b5a2 with SMTP id gv15-20020a056870aa0f00b0019f45a1b5a2mr1064710oab.49.1688071995494; Thu, 29 Jun 2023 13:53:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688071995; cv=none; d=google.com; s=arc-20160816; b=K3dRLf/1K89D7SaTyDBLiTuVSi4Yow34WoSjUMIrrj+NxvGfs+Yky7ZFOjgP7+yiUY br2HYO+9FMztGhxGyyz4sj30jQkQebrV/hBquWAMlPPi2QYoyJ5rXjOkNYBHlYzH1gf3 RxdJL6Wv/U6RumPNGwoC63x4HN2K56iTAa75zR75oI2h4SUhRVzzv8NcIVnVwNBNG4Xx K1V8hsTLo9yP6GicTcgUFo7xJXDgKKYFdsayYkOBikE0fkdCCQJG4PtxDQMRe+tf1mK4 tOK6BZRGxyLCu3A7PKBQx7aCzHYLwmOm0tqwSGleYmNn39SOI76/fowBqWFnH3YTfRSN PghQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=yLzMk6njG0VUm6oo8ECi7NNqjOvlkj+YcLB9LCE7+MA=; fh=eRaYFbc68TLHCmej1r/Q9kziindMZl6i1kh9xMHvl2k=; b=gHro5TVw3ClxWmdZQxTMvan28rZzlUTSpKx/YKmaSjoNXNSgFs4p++jE7yk7mXXg3p 4fuU/HnSB+e3P2OAycChoVHFbP8DVFjVghPSWWlCX5VAdjVXjCwTlBJk6rUEBhq+qV1z BMlqs3hKtTUVt2PuAC4A+rGhPkrsuSN89PoFZk+2ggFmKy9owT+PxUNwTumladqOxCU8 sEiNchIWzgW63fbLRkROF2RLjaSjuYeWZDycUwEjHHb71rvKmQ/2Eyh9H3Ri71JT/82+ 6HnXTjAjVG7+YXXd8ZR6A5pFQks1RDqmpvY2Q/WS6Ac1DwL1cPApL9hfliT0sTf1DssZ 4AMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=vd6FO9RK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 8-20020a17090a19c800b0025e90db660esi10950463pjj.100.2023.06.29.13.52.56; Thu, 29 Jun 2023 13:53:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=vd6FO9RK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232314AbjF2UwG (ORCPT + 99 others); Thu, 29 Jun 2023 16:52:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232316AbjF2Uu7 (ORCPT ); Thu, 29 Jun 2023 16:50:59 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25FFF359E for ; Thu, 29 Jun 2023 13:50:58 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c118efd0c36so1740762276.0 for ; Thu, 29 Jun 2023 13:50:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688071857; x=1690663857; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yLzMk6njG0VUm6oo8ECi7NNqjOvlkj+YcLB9LCE7+MA=; b=vd6FO9RKLDqV//QD3xfOnLM9ABYCd6dUPJXpRHT4NX5noS8/sUBwSo898iH3nVkB0i DtNAyV+Pi6bgq0S9jGXWkBhQ9/YdQKbSPEohpFeN8NdUWPbX25v+l+vYnpTa7r6jcPht dbVhBKQwNHFgV1WouNWGszv4XwOpqInDK4ouCSz6Ak/7s9gGDRqjeI3/ZhamBO61VWCa Z6gPSDN+iIv39tWkyOgXo6urQM1zLyzmqO53tLfzCPB+LOgT197q6GD2uuEhk9CDZuub vqBRd9kZ1XKyBS+VrCDyZJ59YXT8sSu0AY+uPtiN0Du5fb03DdmO0Rm9zeWUxhbXnaCv j0ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688071857; x=1690663857; 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=yLzMk6njG0VUm6oo8ECi7NNqjOvlkj+YcLB9LCE7+MA=; b=gflIpOzKfp124eXcDxj+Qb1p8N6gtQsPfY/5kocwFbUpC+WinNKEWVj9JT9Q1gWnvq DaLtelj5/l7tbKi4kDhJzc2XFDJFmYTVPQGGrZtZ+i5mTiuReKVOyWmVhOuOJkn9VOx3 tXDrWuyS/s796p3M9zco1Hazdz/Ce9c5wzTJFMVpfjHFtkiwAwvZbx2KuEHfEIgVJfRT mui5qpNu4gMXNlX+Zle4ii/Mqb8GZ8p8NDKKdHhhyGC563TgkklJfq5kzx1Rp9+mNbDY A2sA5znLTdVLmzFnWCXGTF+d0uCjfuqLlP5/TpRF0orIkrEwGzqAa4wjMzLIczoIlCaD rt1w== X-Gm-Message-State: ABy/qLYnS4d+CKR/sxAn7JyD81Hz0rZhq6R3TdUpMNwa2J9Bf/zRqdO6 X7VUTMQbyfaP+Pg1quy/ndGnXp00hoP8/IevEh71 X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:e20f:5917:3efa:d4bb]) (user=axelrasmussen job=sendgmr) by 2002:a05:6902:1105:b0:bc3:cdb7:4ec8 with SMTP id o5-20020a056902110500b00bc3cdb74ec8mr74829ybu.6.1688071857157; Thu, 29 Jun 2023 13:50:57 -0700 (PDT) Date: Thu, 29 Jun 2023 13:50:40 -0700 In-Reply-To: <20230629205040.665834-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230629205040.665834-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230629205040.665834-6-axelrasmussen@google.com> Subject: [PATCH v2 6/6] mm: userfaultfd: add basic documentation for UFFDIO_POISON From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Christian Brauner , David Hildenbrand , Huang Ying , Hugh Dickins , James Houghton , Jiaqi Yan , Jonathan Corbet , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Shuah Khan , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770071780762731908?= X-GMAIL-MSGID: =?utf-8?q?1770071780762731908?= Just describe the feature at a really basic level. Signed-off-by: Axel Rasmussen Acked-by: Peter Xu --- Documentation/admin-guide/mm/userfaultfd.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Documentation/admin-guide/mm/userfaultfd.rst b/Documentation/admin-guide/mm/userfaultfd.rst index 7c304e432205..b19053436369 100644 --- a/Documentation/admin-guide/mm/userfaultfd.rst +++ b/Documentation/admin-guide/mm/userfaultfd.rst @@ -244,6 +244,21 @@ 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. +Memory Poisioning Emulation +--------------------------- + +In response to a fault (either missing or minor), an action userspace can +take to "resolve" it is to issue a ``UFFDIO_POISON``. This will cause any +future faulters to either get a SIGBUS, or in KVM's case the guest will +receive an MCE as if there were hardware memory poisoning. + +This is used to emulate hardware memory poisoning. Imagine a VM running on a +machine which experiences a real hardware memory error. Later, we live migrate +the VM to another physical machine. Since we want the migration to be +transparent to the guest, we want that same address range to act as if it was +still poisoned, even though it's on a new physical host which ostentisbly +doesn't have a memory error in the exact same spot. + QEMU/KVM ========