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: 13297311 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 C6B37EB64DD for ; Thu, 29 Jun 2023 20:50:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4DC98D0002; Thu, 29 Jun 2023 16:50:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD26E8D0001; Thu, 29 Jun 2023 16:50:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4C1B8D0002; Thu, 29 Jun 2023 16:50:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9DA898D0001 for ; Thu, 29 Jun 2023 16:50:52 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6141EB017B for ; Thu, 29 Jun 2023 20:50:52 +0000 (UTC) X-FDA: 80956979544.10.1157F37 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf03.hostedemail.com (Postfix) with ESMTP id 94D0820009 for ; Thu, 29 Jun 2023 20:50:49 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Ut9VLeaC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3qO6dZA0KCCkFcJQWFXRZXXJSLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3qO6dZA0KCCkFcJQWFXRZXXJSLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688071849; 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: references:dkim-signature; bh=n31snoBoqTjmW3LOSDrR7Vzlxun4GSqbUT1PJH9sHYA=; b=KSHPWKInP6z3Wf/6SJ9I5D4sorTzmVJtfiD16mJ71Hk7FMPw2I7px02hcCm1IS4dba+zmV c1kvILlR5h+bZJCYj/W0Uk9XPwatIGCy0TsjtFFBx/zG1WSep8uU6qOKsnezM6AISbM69B pzeLoMx5wBsavSn0BECqYd7+CXuizUg= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Ut9VLeaC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3qO6dZA0KCCkFcJQWFXRZXXJSLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3qO6dZA0KCCkFcJQWFXRZXXJSLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688071849; a=rsa-sha256; cv=none; b=WkhgWHXhe5+rCO0hY3gkCoMzC80+DIm2xoPe38/aVe1ae67FiLd8PQvRRGoovII1NC5nr2 cR22YwOkTwYEJOsIg1XD8YA2j0CoSAIaaEuT4Lm9sjlR4VYXIPQ7W6kekJQGDEsLSBpbbY FxW6YdkrkQFtGZ9PYgnhDjWPXTB1AaY= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-bacd408046cso940034276.3 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=O7wsZqcgzx299h7vSVERndX4vuU3bQ3GQ47z+vT5DsLt1LxjRSM8noo18JlsGoNAFl 3MK+MuBjLMaKuOQfS19Eg+DxP7xYuVjjv0N+WcCAvKHNuj3Az/d38TIwawG8BlR2kC3f yxil9B/unZVfRy8xHFcHfOjvA5EfQEYcVxMMDRFGsT2O6SVRVq8G5KW0Vgiro6aJyTcL WeiV6xvJzJUZ3BFLJKRopOsSq4t5WH2WfKOg7B9Q/OQFJb1MKJ7Y05M824DbIhcCRfRA v0qfUEwQwCAlxWVVaJNa2m/Q8bT23aubsLbD8wQi3ofZd8QGy55u009QcJyuw6j/eg4m R6Vw== X-Gm-Message-State: ABy/qLbizxheui9fV3BsU2Uq+njHIlynmNtfRDrMf22A9HAPpQr6IbLD Z+iqbLmYND+B14pupnUbA+10yQbJs1FLWDIqalCy X-Google-Smtp-Source: APBJJlH4u/TteIZMM5o6+B+C6/pVPx63puKun/LsxSpecFO4G35qd0IIggv8TwfKmYBT/GWrBU4MPZ6uuL+/00FR93UM 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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 94D0820009 X-Stat-Signature: ditcq6tx6esudhzhdsqic67dnj6h1dc1 X-Rspam-User: X-HE-Tag: 1688071849-922908 X-HE-Meta: U2FsdGVkX19tW8MWJEgVUmEvfEV4i5cGYTQ/dwOITpXd2V0p7u1f9kpFHf2s9ufxF3k1lZlDfWKnoQUW3+YapSt+ucfGVojsB0t2hHESd5AKg2XpTkm6bjgGaJMqZZFYtQJtLHM0YcXbqOjuzBPXDLO9leui6UR5+9qvMrtPRtGpTHzu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1kfN/zcy2dtHWbqa25aSsgWCO1w+JZ7WJJ//nL4yJH0eYOYhZWPCHJHPErvJnlWAjpl9AR3vesC4erg1eKBZZBeRtN7L+cEunc/RfqX3vUsdW/wGkKau14hPHlzAkXcjhJuZa/hOfym86dnS4UPl3eg749wwZ//cEU26uKlNoU2lD/snCXsnita867/jslqY8arDd4oiC2Q2tIN2Us3S7Uie9fmfisf7otRJEyyqBmfTilZEf5DPVioUwF7u1typJGq6iCETkdiu4siae9jk7J98TA0132MS/5d83xXCY5hnWl6itmw63WbIpJ8FNdgETKeflvUnwXm4xDMvTytvNr6j/zNm7w0ASBUris6wxt2MlTBVKXO5cTMJOWXI4M8qWWHosdX0UnMjdjUUHZ/wHJGpFiH68Fn313VBVS6+5XIr/Ay6GuFwvsAGQS2k+Og== 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 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: 13297312 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 DA4A9C0015E for ; Thu, 29 Jun 2023 20:50:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 384FD8D0003; Thu, 29 Jun 2023 16:50:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 33B728D0001; Thu, 29 Jun 2023 16:50:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2060C8D0001; Thu, 29 Jun 2023 16:50:53 -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 0DC648D0001 for ; Thu, 29 Jun 2023 16:50:53 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D6EEAB017B for ; Thu, 29 Jun 2023 20:50:52 +0000 (UTC) X-FDA: 80956979544.09.494F755 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf05.hostedemail.com (Postfix) with ESMTP id E811F100016 for ; Thu, 29 Jun 2023 20:50:50 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=a8O0wagy; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3qu6dZA0KCCsHeLSYHZTbZZLUNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3qu6dZA0KCCsHeLSYHZTbZZLUNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688071851; 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=R8jkAmCGbYNygAZgSop/zKP0BAnGYhZIuFEhOa1in5M=; b=SRAzgUmKk3WHDYFlZmDH2lNMmcCgpot7Eb1Itkd+sOzALCg16eMn4NNynJkmpT0jY64P08 8OnrOcl1TZC+SRiA8xAlhLbvYdGMUnjl7a3o1wsodmv6hEX5fSS4WFcLvSdlYlQKuHES33 kTaHpt270Cf0+QRs6LKw0wsUjGLiYrs= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=a8O0wagy; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3qu6dZA0KCCsHeLSYHZTbZZLUNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3qu6dZA0KCCsHeLSYHZTbZZLUNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688071851; a=rsa-sha256; cv=none; b=X5Bk4EDSgtfEGFUhdZalQBaEXLJxNGPAq91abJsafuV71LPcW335ZD7rpHpooWkcKOEHao 6wDak/Gp5nvS3237HZ74pLdGH+ni+KthZ/NxAv6cZHidYG1knhFYQ2oYo2toIqn9v7baqr fJmeoiwxZZS2Pm+Co5b6CttMK2rnhnI= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-bfee66a6398so919142276.3 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=jhD4TAbBAdaBOW/tZMSAHVIEz3KnNLxys7QENaAI3bqGSylAPQMpHrZrRzxUQqpWVI UAv89XVwdiGO8HoIK9d/bac04QPguJq5lgbbn8YCow+WzsiIsCCfMQtiehJMG+P7ay2m kdH1fDafmNPXN81Y/OrFOmpmMOycw2hg58mO+qwxFs+HaWVK4YJ/WvSeix3vGye65tQj 2I+WXrTJ5LkA8oW067UyV5lyk+H/Xw5BbY/5bZk/0Krm1vjwHhUG0MxoHUCs2Mjd97+1 RG7AMRh+f1SEg24ir5srKHSQY/q/c1Icpsgn/BJglpDYnWGzqWfjuIfkT76kE1sGkLbv 0Ffw== X-Gm-Message-State: ABy/qLbsyxQvfL84wZDcvwxpgE42X1UUn2yWzV62Tm551sOvFMaKeE5Z 1jthNW9TTUqOQEyK+HYbSWH1EHzoHcVqVvnrDMHu X-Google-Smtp-Source: APBJJlEqqtwXeQ2F+CB5P4eAYTWY2GLrYOnygDGt3hdnYMvy8/GPfEVbxlgghXg+IFwNtZpThqff4X0A++LKH4Bo56yy 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-Rspam-User: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BcAXU 9NuagtUR RUDNn1nriOgIEPv8Lu7RnGzYsWVleXhO7Y8hZAzSD8bLaEGVVFv+3SIyvdAhK1gIarJ940jkS7VrNd6xGS6dIZ4a+5w6l0UvWfwuqSHFUvOTaZFjJFsDOTI4smLlDLOC2QYhAQtX9jVsdVoIgiVQzqm3o7OsKh1AE75D3e4NDrbYb7ttfMD1GHJsgy3/TUM2r6T/Fva4sw+Hup+ZPoSTYMcRWsFreRrOYCoJy6y9Uhe5BS7xRpS64Xy80IjN4wUlscs5S2gmdGgjQM1DFjW6mjYF8/ynIFI0optcLZFhZZ9xkaQfZFYa+xPsDmpme/ktrmeTjyFpwvnJPinrl9U2Yz9HrhNMFTl/NoPV1WL6INk1bIULFVW60ytTuOe9bjXIx85LVbupkrfBsPYO+Zv8feeRuU3XLkgk0exCe6S9Onzz3hU26VRB5Hb/KV10ONGCIgPFd+6EbpmDjQ1JKxJXYD5oX98EuaPCns7mfFVQHZngQb54IyATblwJYzNIhz8neluH6ZE/QHNwI9kJ2SR5wr460/WE+6eHpfVQnKfHzOePduwCd9IorrG1/AklVnU2fYUqymIEyC+cn/T42I0dfO4MwpR0dThXGR7NSAxZOVQoSrwjrCA408+hPHTNaBGWnelVuMevWiOBCuQz112uLeSSsmIp5DKtm6rpW 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: 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: 13297313 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 C9285C001B3 for ; Thu, 29 Jun 2023 20:50:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5351D8D0005; Thu, 29 Jun 2023 16:50:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 46F278D0001; Thu, 29 Jun 2023 16:50:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 18BC28D0005; Thu, 29 Jun 2023 16:50:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 033AF8D0001 for ; Thu, 29 Jun 2023 16:50:55 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CA3A91A0C6F for ; Thu, 29 Jun 2023 20:50:54 +0000 (UTC) X-FDA: 80956979628.19.AC43F99 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf02.hostedemail.com (Postfix) with ESMTP id E6BC08000F for ; Thu, 29 Jun 2023 20:50:52 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=dv8waobW; spf=pass (imf02.hostedemail.com: domain of 3rO6dZA0KCC0JgNUaJbVdbbNWPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3rO6dZA0KCC0JgNUaJbVdbbNWPXXPUN.LXVURWdg-VVTeJLT.XaP@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=1688071852; 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=oot+BFijmfKZ0KO/nJIrIU5HcDe1IwfMU2NYWHhWcUA=; b=1WEucTaxYzs15T4r8FFmDu4z9ytESswBmGz7R3V31Qf1Tl0awBOk5p8D3FaoSEjajaP5Hd zPQLBbmoM/bkW5feP9L7L2ip3RVKj5iI9mYTCJzuMrj4ypyyckYH5AW7MKPHDQ6Y9SZOD2 j5/uBae5i2lOdNif6hXVIboQMkLjuAk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688071853; a=rsa-sha256; cv=none; b=iR9dCgN6O1p2tPk3ZDkbGETS6w3m3/urw/+XBwxx+PywjKDvTkbDh6qKMuDhaZ7BCwi3aq zexVXCkVlM713ip6ABTYLwSWEbeYeLZIMKyMP8BlVlBTBfgdQp5/IzI15Cwc69DfWNkiSa 7fohqnWKt8H0QalJ/uG39wabs8LSlo0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=dv8waobW; spf=pass (imf02.hostedemail.com: domain of 3rO6dZA0KCC0JgNUaJbVdbbNWPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3rO6dZA0KCC0JgNUaJbVdbbNWPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-bd69ee0edacso874389276.3 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=X7mLpVXGoxxOyZTSVWE1S7v+HOyiltDl0CRtbZ8aRRNL+a53L6u7dGcOwAHtA0k+sv 9aWhrIoP/4EzqVADOAxJ4RB+v5OnprVBxjAwDSxwTNcIS3EK+c+tml3W0sIPWJKqo9lA f1zV+ixuXiD/BymUnjabSxood3iFkGU3SsllRpuPb0iY0TG9FyH/ONQwgwcbumRcZqrD 6ECQRjhG58mst9hExZqM4Xek3Ro8RKY4Xwyp7NpnTyQVzBwNuToxZzxIo9KFBiu4BGhm zZwQOZZw+HJADcFJpSdHGULzgpE/Vpa86/Lyd7waC1zn0sPxg3TK6yk+2vptUFdTnjyD d3/Q== X-Gm-Message-State: ABy/qLZKiNFRB3ZFKCBhH/H1+kixCjUT5wfSjcujftkK3ikjXpb2jrZg vcz8IKHiFGQPlt7elRyWsqNcHVN0uxtPBtvCdgF3 X-Google-Smtp-Source: APBJJlG01gugR5pBeLSzgr9QaRAnN1lelWD28Wisa41iTTVqrwvQyIgcQU0WMKN/qYg/ivXFlgsOpx2irKkAZegi36c2 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-Rspamd-Queue-Id: E6BC08000F X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: a78ccbg9gxxo9zkcxzfeujzmfefqhgsz X-HE-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BCgizSZeSh0qCd7OvMzCM5x/J8PABmY0vzUjYRVxki2suXq/KKfGtM10erQquMaQdhM79HT9LvJDrusuc5Il2Ur0JZKZwb0cgBtkYM3n+H0E3a6uuADhWQYGublNQgB8RPBoasqHAiedQo2Y6e4iPgQJwhI7C9fYKcjWChXt/VFflDInwlvrSq+wlBtcCXPl858qZ/rtvZMkwEQ6KPbU0XrnOgG1S1CfiMFy+//pt5DE39qWw2lQ98+kb9F4b3M8h5B+B5eGX6bBlVvZTiauemncD+JtkVer4JeSw6IugN7TkLp/0CG6aM+AOjQK6bLjYMynco6I/HdneWupvGff+ILIWymvY/kNJAAuf5qarc+g8liDK9tLEz5meIq9XS+NOBPYilnrclZ2qiW6Gu8HC2X7ACc3o6zsZAtMIdBfRuFeK50/Fu2LHPs3RdRmmqzvrF8cJokqveBuDinUUSrNT9tuines7O9c3/wfpr+LmkEGz7jmrFMMcxBWJGyDwtpB/bIksdEXEkfc5IZSBJx8Dy5apFrC1IlzN+wx2Bi27Mk2kJ3PKBhqSQqW4GfXKL3VwWTkZpPiw4G4/Ar5E9dwi0Rv6h57CkhOgQVr9iwM+2opD4dwzZwGH9qVI/1QK5ITZ3D0fQMG/XM= 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 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: 13297314 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 9BDECC001E0 for ; Thu, 29 Jun 2023 20:50:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C8C18D0006; Thu, 29 Jun 2023 16:50:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F1E1C8D0001; Thu, 29 Jun 2023 16:50:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C87948D0006; Thu, 29 Jun 2023 16:50:56 -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 B82BF8D0001 for ; Thu, 29 Jun 2023 16:50:56 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 910271A0CEA for ; Thu, 29 Jun 2023 20:50:56 +0000 (UTC) X-FDA: 80956979712.02.552EB32 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf24.hostedemail.com (Postfix) with ESMTP id AD2B1180007 for ; Thu, 29 Jun 2023 20:50:54 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=3cQsv9mP; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3re6dZA0KCC4KhOVbKcWeccOXQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3re6dZA0KCC4KhOVbKcWeccOXQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688071854; 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=0eR1m0YpsGhZ6GlqZ9XuYhhV42VymJr50nJOc8nZiNo=; b=IlpEpOp35astXfaGf5gGt02iidIACX9u5ucZv1vgjUU44KDKTeIO9cUKDTQx7maJtRCdgT ScRSWuaIA4jvmV2VAl7fCjvnB0Hgbfa9bDkTQxhIQtxFs1/oBksYX7wa91VI3K6Wp1z+1Y kT0fexUzk1mCMggezT6Wyk4te6nzGTk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=3cQsv9mP; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3re6dZA0KCC4KhOVbKcWeccOXQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3re6dZA0KCC4KhOVbKcWeccOXQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688071854; a=rsa-sha256; cv=none; b=i1/oK1oYjIbf41p9Jub8NCc59stsbeKELxTHpSCZzhGNLDjkC+paVn7Z/aS/8TTOEH6U/l b/wp4RRsKM6sq5Zz+IwM6f0iz+iwWzdsCoqXST+lRkW8OdP039Q8IK9JTOskmoIxnCHlCP DHFhvTtgFx6MgjhUJ8MRjTjI5CIt+5Q= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-56ff7b4feefso9446787b3.0 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=eXaD+5q9vdnv4fedrilU4YJta/sGpbAJAAx9ktHjCpPg/Jt3xujiqQH9k3IzDZR+Te DbWRB/AwWgppwBq/SmKdeNyZyc6BuLr0Dhj0u0mM3+iZo32gEWSLtEf04RB1BLpybGd0 8/zP3Q0hAVh8cT5bt1piYAogRhHgeNtkT+u4xQtDsnN9DNgpCWsBkHHw72vAj3nfF9ZF 7w3uvGvVrN3A3BRG6yoBC++mEha/6GHkxQ2oOVLVflM3FkdwzwZEgddM5rHMsUC/h+NE crDKEA8iAW3drp1I880fdi8yofs64L5b3zR7WYsMutkiBppgf4Wb2mvs7YquJcZ5k43a 04+A== X-Gm-Message-State: ABy/qLZ5qISNMMnrCyGXPlTSi2HDE1ql79OBhu4qPZxAmH9Tw+y5zNJ2 SjGFn9ZgkSXDeyp0hqDZ/hJNLjuCz2Ni1/rkX7Wn X-Google-Smtp-Source: APBJJlGMR6Hew5dlxFm7tULAhbAWqjtH0IRMMwzkK9vijNpv3CfU43JDb01xtUzDErorDPisxqOLpILC+1eZPblYAKLk 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-Rspamd-Queue-Id: AD2B1180007 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: rm1yw1dn1sgi63it6un5s66u3rwsxbi5 X-HE-Tag: 1688071854-329984 X-HE-Meta: U2FsdGVkX19txrONgssApzbsWxQog10plWfxccPgH4mTWpjH0ehDtf0jYzPiAeKwjvMoeRkxkgsy95gGBBO96YkUWbEk3v0jfHtzqH72ykUOHz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pv+hWxcMJfGFp12et0IQRWr4VhP8MSuWc9DYMdv+6aNVj+Lco8BF9WexKxsymz39VToDCqNENtyLkbe7nP79BFXN2QZ5jBKO9+9dtHIqgfOgKNcx8AnzDOYDrtA4WtoCz3Y3c8hlKWVvER4v61Rip6baqdrIeb9rVe8zL0EBYnPMTKcfCiNQxt+q9E/gD40707HVXy4UrG4/WL5tvpbXJpI51U41h1gEbkeUAicUO4sjr3rtN+3neRoMJyrt/5MgxVbNi1QeugW2mcyvHcgLa1NKm3muKa28p0AP6a7BwraH2VhMIvcRmkPxqGTItlbwoPNQQmXEbLHy2C9bCJIRsPcj2mBCQc7km03DfNyQc9DIugw+9KN2UBZ+vc6Tgg0WPCK74fNeCbMLBsP4+VC3XU0Ia9H9zI61rk692VhtxwIGdRvRtCC1O6zmjKjosl/IQrpaiXkXiKqDxjP2oG/u+JhmWnHHF7VVK5rRkyMT6psfzPJ5rWchtupElAaGvTnx0ZkQmG5QPaUdFEA== 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: 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: 13297315 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 97BB5C001DE for ; Thu, 29 Jun 2023 20:51:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBF258D0007; Thu, 29 Jun 2023 16:50:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D235C8D0001; Thu, 29 Jun 2023 16:50:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B74A38D0007; Thu, 29 Jun 2023 16:50:58 -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 A4EAE8D0001 for ; Thu, 29 Jun 2023 16:50:58 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 59854B0783 for ; Thu, 29 Jun 2023 20:50:58 +0000 (UTC) X-FDA: 80956979796.22.2DB9239 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf01.hostedemail.com (Postfix) with ESMTP id 7A7E040013 for ; Thu, 29 Jun 2023 20:50:56 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=b9ic7m0u; spf=pass (imf01.hostedemail.com: domain of 3r-6dZA0KCDAMjQXdMeYgeeQZSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3r-6dZA0KCDAMjQXdMeYgeeQZSaaSXQ.OaYXUZgj-YYWhMOW.adS@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=1688071856; 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=FDMn97NE4k9ts/MbSvQk7TIpBtwkvtlY8wmg3pzMxc0=; b=8eo8OzAeHBKXxbG8uSqiOIRwwCkk9FqEvCvwNPuKB3+TFdAVyOS1dRq3ckZLNlIWgSiqTv jfav0ybrrSjDUGKDusMsWr+TSZlz234Ghqjg1lS4wjvBghaV5/Df4FD02XHeCmmDLt6QfL 3E5gEOhDnlGYL7ggHvVarF3Mk47qIK4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688071856; a=rsa-sha256; cv=none; b=p4tP0ZDTeObhvUKb/y/zywitjB0z0hdd3X3HOEG7ZHaYMSQ8bnSEIsOLBmHPq2OcgJZ7Vs K0H+vuRxyqweekpFUowvNpSKeL9CEsjSwB/2i1pFFSTcwPEhJer3VdLZH5A10OQ59LROpF 06jP+hlJJbO6HE3wLjbGSbQPTAj1f9E= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=b9ic7m0u; spf=pass (imf01.hostedemail.com: domain of 3r-6dZA0KCDAMjQXdMeYgeeQZSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3r-6dZA0KCDAMjQXdMeYgeeQZSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-573d70da2dcso9724617b3.1 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=E94e83U7Uc7jKEkQb3k8GQuTmxyZRVFRDGiJv9Csx+YfXOFgRKXQmBa7eInY98PwvQ O4Oe0HW+BDL2KrfTCnvzx1PJQuhVwjTRmk7rKRYH5cFW9tvG7K9QiOdts+llY4nPkZom gM8biIMj27axiVPgrsiYrJ2nHeC3Gu/Y/Bkbu7lgVuD8/MiZFZszxvQpnsE2aNfOxN16 ww8ZePSxpPHauhAlEVOhUL1h1SNF83ct6eJUABChXjwMbrjdl6uGiUpjv7+h1MLi+szs WRHzVrr2ho12ufd6pI6EEOqh7c8QZ5Jlhzmb0W419QLc3xXVn2ssdpU/PV/UkcuEUvkc Ok+A== X-Gm-Message-State: ABy/qLbMFQrXLldkzxSAeIVuhKiFEMVCQ7x8pBCDlpp8tX4NvKDFKAcj un7rmTHS/gV7ZmkthZqW0D6dsmb2XARKjP0/OLgK X-Google-Smtp-Source: APBJJlFEeXNU5wkKYldS4vdZYkexQZAWshF/qAuKQS3UJdZOiHvOTMlytCrLVWGNw78bI6vNEGHYZeLbYDtkVMX51oUg 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-Stat-Signature: 5im7y8tpj8b7cg63f139eo6ugtiyrhdb X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 7A7E040013 X-Rspam-User: X-HE-Tag: 1688071856-972192 X-HE-Meta: U2FsdGVkX1/PwZEitkLWDMVn6tMqtYOS1C7m+1aPMPu72gnmCNa17xflbF2oyDeK81xrTFpf0M+Ehwty1RNneSUfn0lZx6AuuZg9IHU1g2GJaPRUacs9JbsUxquX6skLK0l66uVJvWT38UlM8JHBIbghZpzimGw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f2zoQ4c3H94s8LC0FvfZpVmZTl1uHy3x7r3wOpx5h6aa87LP5RBM6GZEAizLsFki4YR/+RUm84CR22wv7ygivfQ7Yej+ZSjeU+WVAVWa8vdwLqHXpUvjzvL8A9N/rsiSYzRcnH2rxUlX+I/x06JJK1w01zY0fg9wrHuLFW+ZJbpvhXE9TOYlw+SDadIrrr3DK0sGPaDbUWf3W9XgR675uyUPwZC/QQIsb9w/oh82eWc9EM6m7GoaXvlcj92wNxMV8qx23g72uL7qBGvKNtIlsK4SLFFwR7DA9MYAPUavo9jvNQjImOy9KyjbmSvI1T6I/Xl1WwLvWQjN/bk4Gq+KhPR4U3WqCud3JrP4VOgSRwqj3YTKvHsJ3HxcOUu2fxOvNGBbLVehDjscOfpQFbkb6T0YjKMjcUTuMc5FNoG+fm457j4ejTU7qVHgwQwxHiB1QBqZqA4IGohTUeGbQSqGIkMO0tssK+9T1Nw== 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 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: 13297316 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 74885EB64D9 for ; Thu, 29 Jun 2023 20:51:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3AE838D0008; Thu, 29 Jun 2023 16:51:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C2B18D0001; Thu, 29 Jun 2023 16:51:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 118008D0008; Thu, 29 Jun 2023 16:51:00 -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 E9EFD8D0001 for ; Thu, 29 Jun 2023 16:50:59 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id BEE571A0CEA for ; Thu, 29 Jun 2023 20:50:59 +0000 (UTC) X-FDA: 80956979838.03.32716C3 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf19.hostedemail.com (Postfix) with ESMTP id 027F81A000B for ; Thu, 29 Jun 2023 20:50:57 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=vd6FO9RK; spf=pass (imf19.hostedemail.com: domain of 3se6dZA0KCDIOlSZfOgaiggSbUccUZS.QcaZWbil-aaYjOQY.cfU@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3se6dZA0KCDIOlSZfOgaiggSbUccUZS.QcaZWbil-aaYjOQY.cfU@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=1688071858; 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=yLzMk6njG0VUm6oo8ECi7NNqjOvlkj+YcLB9LCE7+MA=; b=ndym8xE+fxU/QKtHjCxcMfyEi8ZKTQpvoxiWYuBHf1ifP/pnWLvZzPG8XWWPY/4l+/lvMq EO6GlTWigCSUsQEe5XDSPrIz8KWlH6yzWw+BQf54K78w61z6/osFjDlY7r7qZvn5g0YN8Z hP2TuXjc917xU0ITBjid50W+dne1G+k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688071858; a=rsa-sha256; cv=none; b=YplYXxN4y54D/oK8sEJrjYXY/CN/jxnzWUSw1jdfHLjSw7C+aJCH2se0tuNBsmGLba72Kz g0DOxwNw0au9iFfiorM/YOZ6vIeG0ReU//Od31ibr71oMO+15zTyEFnAbAXB9Adz7XK8XG vThPMSf10I45nCzRmuZPcpmnTZQv5ng= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=vd6FO9RK; spf=pass (imf19.hostedemail.com: domain of 3se6dZA0KCDIOlSZfOgaiggSbUccUZS.QcaZWbil-aaYjOQY.cfU@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3se6dZA0KCDIOlSZfOgaiggSbUccUZS.QcaZWbil-aaYjOQY.cfU@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-c361777c7f7so1737595276.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=kc77S9KgrdLC2LM1WZNQfVY+Wk4/DUEWWoRzMMfoyn+bxNeIL6JaPDD24mvqN380wS 03mqretG3Tdu5byXi3RR8LY3HnXExHZz+rD0c/86PnEITD2UDy22JsckM61/3yxPWoYB /6+hG18XTURbJ99CiFwOew5d7H+ZOpzCtFsXfRwNv3SdKEpGnoPN3gzqH5gb4N5vqoux cLY15QLGSNJ9uvJlOJfIb6trC3yKe6ZAvaKBZHGNfSvuRR6tXLA0fz0vU3tf+1QZah6g s1iowz5mVK0FaaNqlrSumfJBUO5gCFaznAoKhDMpNNRgrJ5wr2jvhTk7WneXuyxpSARF g0Wg== X-Gm-Message-State: ABy/qLabWQVqjMq4pjPGthyQEN3XbKJtJEX9RrbllrKz2vsUDiSvW67M OYdvnbP1ScQIS+pc+WxGDcRXf2rPhTNbklU/vxyC X-Google-Smtp-Source: APBJJlFxXqx6n/8cIVb44SOqXuMVe8nfJc8KbVSjzubcNqQNTjaVwvxr+Cfitw1Wlj4qp7VAwfdaVDGWjSlIa4+NsAGc 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-Rspamd-Queue-Id: 027F81A000B X-Rspam-User: X-Stat-Signature: 51qqkqa76qdd7fgk3rqo96yyd8nohxid X-Rspamd-Server: rspam03 X-HE-Tag: 1688071857-34783 X-HE-Meta: U2FsdGVkX1+D1AItIxGNu8KfQCoQwO3XZNr0mYj0QzI2xVc8xwoXrCJiqcWrF/mBQUSgHiezSPeS7Rg98k+moA0VyShaEUNqK4qhNx2pECCFD6EPJ9/3f30OQneTeUpP1IcvMdeTrw+argzjMZFo8cC3qIiNTbfFQ+Xek9xtw5h+OlBP9B19QDp7dVeEj9duKS1+ekELRkx8BeU5H/vAekZ9dh+5GkPENCrsvgHaXyiUTlPYhitBFo0iLZxCN6wYejg7nmfWYII1Y7yhpOAtR/UhdkB4lXkqUWY+nMbOURenm7e/6dNuTc7PEMAk98R0lTmNWTldqoyhgFFP4dhwQLBlOS18jekxayuUtnC2DIc76evDgtmEzB94PB6bUX8+O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wmY+wr5p/+BYAEvVcSrqrnFqZjbxA8fm8J/nEJv87RPFst2GPgmHIk4vQg+XY0k/no50D45K9APGLyqeIEjHSTslzbrdXkrD8/EbCofoEJYEr9x2o8uFA8AEwqBhzaTMTO2Z2BNgmKI/3YodO5hoia+NQhZzUgnwiKabiTnnEQsdRhzf7MIU/Eudn3v78Y26WYO/3I0hp04E+Wq6siuJBie7RmEU3oTAC2wkulpRIbiqsXfvr5pD9BmYs= 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: 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 ========