From patchwork Thu Jul 6 22:50:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13304271 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 7D74CC001DF for ; Thu, 6 Jul 2023 22:50:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E11ED6B0072; Thu, 6 Jul 2023 18:50:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9B0E8D0002; Thu, 6 Jul 2023 18:50:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BECB46B0075; Thu, 6 Jul 2023 18:50:54 -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 AE8596B0072 for ; Thu, 6 Jul 2023 18:50:54 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6C0ECB077A for ; Thu, 6 Jul 2023 22:50:54 +0000 (UTC) X-FDA: 80982683628.01.C9143DC Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf21.hostedemail.com (Postfix) with ESMTP id 779821C0002 for ; Thu, 6 Jul 2023 22:50:52 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=yiBN+5aT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3S0WnZA0KCJ48VCJP8QKSQQCLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3S0WnZA0KCJ48VCJP8QKSQQCLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688683852; 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=axERsIn3EwMGffTL4fWJaIzLcp6XXXIYiFKDUzVzBqY=; b=Yftqs97Dmq9lbJ9P1Kh9yWjuPF5rYYUrsfnJDeVfgSOYLzkNYVmyQbGdClDsCJBxrdu/EK x1fdCr2YpfBKotO1Hki+Nl+tbfah3G4qPP0SOwmmSS3viRTE4BtT734JeolXLGaJi6EdD7 Gt+XlfxB4TA3rE4gXvk37kNWckSTrAQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=yiBN+5aT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3S0WnZA0KCJ48VCJP8QKSQQCLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3S0WnZA0KCJ48VCJP8QKSQQCLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688683852; a=rsa-sha256; cv=none; b=Ry4Zi+YznBcdO5xg/ydG4PfXRG8Q5xdXu1XUNYGHlM9eN6L5AEhP+JQEjhe0UhPBCI0C02 QGBJVQwI8vsZos95rpccOMIdpbuwEBNWYuSlE35ayhCHCEJcqIJ+llGqjU2V2TUCb5WjfV 8bbYhLFDzUuHZwT/O0hbqtfMyUGH9Nk= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-c4cda9d3823so1264625276.1 for ; Thu, 06 Jul 2023 15:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683851; x=1691275851; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=axERsIn3EwMGffTL4fWJaIzLcp6XXXIYiFKDUzVzBqY=; b=yiBN+5aT9dRdKtxMwsd3u/C1Xvh33zuWSJWtyc3b2oqUAezQBMDYehbTlZSQ43q9rc GjxiS4tCHuYh72lUDHDWnhv1RNBmlVhS7uMGqqF6xgkO8xPXMZwt06lYIEgO4HT0GePH vDCilEspj/9gvKK6E/Xlu0bf+76WPjOT9jJ/vO5uQtiepbi44/uGZ+K0VB7otdT5xt0v Qw3dpYo216D1CFQLlt2k1rfsnwsPsRhpOt1hZF/IcAy4lMynJc8FNYjbTCbEFXlclhiW saVTIojFJ1zVagbeXVsgf0uniofnqL9UTOGwJEomvRE02Enf5Gry/gezLCRdZ5MsxGyZ mU/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683851; x=1691275851; 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=axERsIn3EwMGffTL4fWJaIzLcp6XXXIYiFKDUzVzBqY=; b=SHqL0bMTC6zv5LTLp4XCnWIsmy2Y1OuSi0ppFR7f4E2nXjUiAGtOPiOevhTvbPvtuN C5oz6Tx5q/5M0zPX3vl5HQuVhLDedJyJUbMPsnAWIl5iIXj7iluF1ITUKg3R7IU/ZNXn 88qhM5K/F1/8EZ0pnmb4mC4b5uKnafoO3R0mr7lQd+meBbEDI9c2TIcwVdeib9stNB8J u65INhCCaewhGyc7iJz7ak3Y8Q2/f8wp2KJOsS5/hVOMCFAts60GXF8mQ147zePQU2s7 ohxOIst+SfvyV3wP/sUBB/d6pwk13xX7oxCNHzAdgYJRpUllg1plnC/iAqdLY6G6m3aj C7iQ== X-Gm-Message-State: ABy/qLafO8zmiifIDY47Y6Xr2qZRtFRqZZccbI5/sqdF6kTObCIDqExT Yq8hpV3ZOMaXlLClf0DNDoornf6Gs6W1Cnzr5Y9x X-Google-Smtp-Source: APBJJlEW6RQNFcBDV4u0eUIIqgKV8wvUZrxJlNc8y+4BgE1qPhkLJw0lh7sQLrM0PWWyfIF59ugMgar+7EWrCClJ8tE/ X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a05:6902:30b:b0:c67:ebc5:de5d with SMTP id b11-20020a056902030b00b00c67ebc5de5dmr18087ybs.4.1688683851353; Thu, 06 Jul 2023 15:50:51 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:29 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-2-axelrasmussen@google.com> Subject: [PATCH v3 1/8] mm: make PTE_MARKER_SWAPIN_ERROR more general From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , 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: 779821C0002 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: mz1x7tag7aokmhmkwribmfq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pgAf0jjAIvHJDO0Y4NGGbUJZLlFgO3iHBhVVsxCHpRpAUIRnG1PAhcR6mTdVDqhGuGKli3SdU6RbajHx0gO1FIlZxqzAoO7dA+egluyq2Dp/m9U1VD7a/EAOvzipTp4/H9R2Sv7qIKAw3W+JZW6pU77JhX7W/0x5hCXzuWI39+LBngfk6kt5oNJ0NZTw12vKX36NJEJaGi36Bq3AJQfoW1EaWjo7nAlVzw4nwvWT7ZBFW2kWOkPCNUSc7k29GSstjtvEFNOZ7ZWC8tpsw9dOfkNIfSKlASBRxqhrTUJ8xIun/PdsVXObxYtql42RRe8sXLtlfy2IGQYwIlt3qLBKQV8vzbYe4hKwm8LA0qBECQKAfLd03vzzoQeGnZPqhPNNkct/LlYGy7CXShJNiNcf2pVkn40qRuQv9BsWrUPBfNLmUQ70z0MAeSjyoqeGfhc+VN3/hN3rH5zZOpFARDfc3E/Q9jYCXPjsyGyhu2ETsZJqyevcwMF2X43Wtm1R5mQts1dlYKQceW8ZFYoZPJGHO1OKtR335c9+JOSMtmOxXrlX4Y/IkAeGkX6vAk0rbwog4cjFjw92aJ7AGGKnGQNBf6DCNo9PQ8cxpfe6qhuAJEuNjy13Gu4L5DRkGRgGdr3wgnd09wiFoYPSocF6HSpw5qKvfOJ93Y4bw== 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: Future patches will re-use PTE_MARKER_SWAPIN_ERROR to implement UFFDIO_POISON, so make some various preparations for that: First, rename it to just PTE_MARKER_ERROR. The "SWAPIN" can be confusing since we're going to re-use it for something not really related to swap. This can be particularly confusing for things like hugetlbfs, which doesn't support swap whatsoever. Also rename some various helper functions. Next, fix pte marker copying for hugetlbfs. Previously, it would WARN on seeing a PTE_MARKER_SWAPIN_ERROR, since hugetlbfs doesn't support swap. But, since we're going to re-use it, we want it to go ahead and copy it just like non-hugetlbfs memory does today. Since the code to do this is more complicated now, pull it out into a helper which can be re-used in both places. While we're at it, also make it slightly more explicit in its handling of e.g. uffd wp markers. For non-hugetlbfs page faults, instead of returning VM_FAULT_SIGBUS for an error entry, return VM_FAULT_HWPOISON. For most cases this change doesn't matter, e.g. a userspace program would receive a SIGBUS either way. But for UFFDIO_POISON, this change will let KVM guests get an MCE out of the box, instead of giving a SIGBUS to the hypervisor and requiring it to somehow inject an MCE. Finally, for hugetlbfs faults, handle PTE_MARKER_ERROR, and return VM_FAULT_HWPOISON_LARGE in such cases. Note that this can't happen today because the lack of swap support means we'll never end up with such a PTE anyway, but this behavior will be needed once such entries *can* show up via UFFDIO_POISON. Signed-off-by: Axel Rasmussen --- include/linux/mm_inline.h | 19 +++++++++++++++++++ include/linux/swapops.h | 10 +++++----- mm/hugetlb.c | 32 +++++++++++++++++++++----------- mm/madvise.c | 2 +- mm/memory.c | 15 +++++++++------ mm/mprotect.c | 4 ++-- mm/shmem.c | 4 ++-- mm/swapfile.c | 2 +- 8 files changed, 60 insertions(+), 28 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 21d6c72bcc71..329bd9370b49 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -523,6 +523,25 @@ static inline bool mm_tlb_flush_nested(struct mm_struct *mm) return atomic_read(&mm->tlb_flush_pending) > 1; } +/* + * Computes the pte marker to copy from the given source entry into dst_vma. + * If no marker should be copied, returns 0. + * The caller should insert a new pte created with make_pte_marker(). + */ +static inline pte_marker copy_pte_marker( + swp_entry_t entry, struct vm_area_struct *dst_vma) +{ + pte_marker srcm = pte_marker_get(entry); + /* Always copy error entries. */ + pte_marker dstm = srcm & PTE_MARKER_ERROR; + + /* Only copy PTE markers if UFFD register matches. */ + if ((srcm & PTE_MARKER_UFFD_WP) && userfaultfd_wp(dst_vma)) + dstm |= PTE_MARKER_UFFD_WP; + + return dstm; +} + /* * If this pte is wr-protected by uffd-wp in any form, arm the special pte to * replace a none pte. NOTE! This should only be called when *pte is already diff --git a/include/linux/swapops.h b/include/linux/swapops.h index 4c932cb45e0b..5f1818d48dd6 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -393,7 +393,7 @@ static inline bool is_migration_entry_dirty(swp_entry_t entry) typedef unsigned long pte_marker; #define PTE_MARKER_UFFD_WP BIT(0) -#define PTE_MARKER_SWAPIN_ERROR BIT(1) +#define PTE_MARKER_ERROR BIT(1) #define PTE_MARKER_MASK (BIT(2) - 1) static inline swp_entry_t make_pte_marker_entry(pte_marker marker) @@ -421,15 +421,15 @@ static inline pte_t make_pte_marker(pte_marker marker) return swp_entry_to_pte(make_pte_marker_entry(marker)); } -static inline swp_entry_t make_swapin_error_entry(void) +static inline swp_entry_t make_error_swp_entry(void) { - return make_pte_marker_entry(PTE_MARKER_SWAPIN_ERROR); + return make_pte_marker_entry(PTE_MARKER_ERROR); } -static inline int is_swapin_error_entry(swp_entry_t entry) +static inline int is_error_swp_entry(swp_entry_t entry) { return is_pte_marker_entry(entry) && - (pte_marker_get(entry) & PTE_MARKER_SWAPIN_ERROR); + (pte_marker_get(entry) & PTE_MARKER_ERROR); } /* diff --git a/mm/hugetlb.c b/mm/hugetlb.c index bce28cca73a1..934e129d9939 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -5101,15 +5102,12 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, entry = huge_pte_clear_uffd_wp(entry); set_huge_pte_at(dst, addr, dst_pte, entry); } else if (unlikely(is_pte_marker(entry))) { - /* No swap on hugetlb */ - WARN_ON_ONCE( - is_swapin_error_entry(pte_to_swp_entry(entry))); - /* - * We copy the pte marker only if the dst vma has - * uffd-wp enabled. - */ - if (userfaultfd_wp(dst_vma)) - set_huge_pte_at(dst, addr, dst_pte, entry); + pte_marker marker = copy_pte_marker( + pte_to_swp_entry(entry), dst_vma); + + if (marker) + set_huge_pte_at(dst, addr, dst_pte, + make_pte_marker(marker)); } else { entry = huge_ptep_get(src_pte); pte_folio = page_folio(pte_page(entry)); @@ -6090,14 +6088,26 @@ 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)) { + pte_marker marker = + pte_marker_get(pte_to_swp_entry(entry)); + + if (marker & PTE_MARKER_ERROR) { + 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; diff --git a/mm/madvise.c b/mm/madvise.c index 886f06066622..59e954586e2a 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -660,7 +660,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, free_swap_and_cache(entry); pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); } else if (is_hwpoison_entry(entry) || - is_swapin_error_entry(entry)) { + is_error_swp_entry(entry)) { pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); } continue; diff --git a/mm/memory.c b/mm/memory.c index 0ae594703021..c8b6de99d14c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -860,8 +860,11 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, return -EBUSY; return -ENOENT; } else if (is_pte_marker_entry(entry)) { - if (is_swapin_error_entry(entry) || userfaultfd_wp(dst_vma)) - set_pte_at(dst_mm, addr, dst_pte, pte); + pte_marker marker = copy_pte_marker(entry, dst_vma); + + if (marker) + set_pte_at(dst_mm, addr, dst_pte, + make_pte_marker(marker)); return 0; } if (!userfaultfd_wp(dst_vma)) @@ -1500,7 +1503,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, !zap_drop_file_uffd_wp(details)) continue; } else if (is_hwpoison_entry(entry) || - is_swapin_error_entry(entry)) { + is_error_swp_entry(entry)) { if (!should_zap_cows(details)) continue; } else { @@ -3647,7 +3650,7 @@ static vm_fault_t pte_marker_clear(struct vm_fault *vmf) * none pte. Otherwise it means the pte could have changed, so retry. * * This should also cover the case where e.g. the pte changed - * quickly from a PTE_MARKER_UFFD_WP into PTE_MARKER_SWAPIN_ERROR. + * quickly from a PTE_MARKER_UFFD_WP into PTE_MARKER_ERROR. * So is_pte_marker() check is not enough to safely drop the pte. */ if (pte_same(vmf->orig_pte, ptep_get(vmf->pte))) @@ -3693,8 +3696,8 @@ static vm_fault_t handle_pte_marker(struct vm_fault *vmf) return VM_FAULT_SIGBUS; /* Higher priority than uffd-wp when data corrupted */ - if (marker & PTE_MARKER_SWAPIN_ERROR) - return VM_FAULT_SIGBUS; + if (marker & PTE_MARKER_ERROR) + return VM_FAULT_HWPOISON; if (pte_marker_entry_uffd_wp(entry)) return pte_marker_handle_uffd_wp(vmf); diff --git a/mm/mprotect.c b/mm/mprotect.c index 6f658d483704..47d255c8c2f2 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -230,10 +230,10 @@ static long change_pte_range(struct mmu_gather *tlb, newpte = pte_swp_mkuffd_wp(newpte); } else if (is_pte_marker_entry(entry)) { /* - * Ignore swapin errors unconditionally, + * Ignore error swap entries unconditionally, * because any access should sigbus anyway. */ - if (is_swapin_error_entry(entry)) + if (is_error_swp_entry(entry)) continue; /* * If this is uffd-wp pte marker and we'd like diff --git a/mm/shmem.c b/mm/shmem.c index 2f2e0e618072..c0f408c2c020 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1707,7 +1707,7 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, swp_entry_t swapin_error; void *old; - swapin_error = make_swapin_error_entry(); + swapin_error = make_error_swp_entry(); old = xa_cmpxchg_irq(&mapping->i_pages, index, swp_to_radix_entry(swap), swp_to_radix_entry(swapin_error), 0); @@ -1752,7 +1752,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, swap = radix_to_swp_entry(*foliop); *foliop = NULL; - if (is_swapin_error_entry(swap)) + if (is_error_swp_entry(swap)) return -EIO; si = get_swap_device(swap); diff --git a/mm/swapfile.c b/mm/swapfile.c index 8e6dde68b389..72e110387e67 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1773,7 +1773,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, swp_entry = make_hwpoison_entry(swapcache); page = swapcache; } else { - swp_entry = make_swapin_error_entry(); + swp_entry = make_error_swp_entry(); } new_pte = swp_entry_to_pte(swp_entry); ret = 0; From patchwork Thu Jul 6 22:50:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13304272 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 E0729EB64D9 for ; Thu, 6 Jul 2023 22:50:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6979B8D0003; Thu, 6 Jul 2023 18:50:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5FCB08D0002; Thu, 6 Jul 2023 18:50:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4250C8D0003; Thu, 6 Jul 2023 18:50:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2DA9D8D0002 for ; Thu, 6 Jul 2023 18:50:56 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id EEDE2C0146 for ; Thu, 6 Jul 2023 22:50:55 +0000 (UTC) X-FDA: 80982683670.21.10901A6 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf18.hostedemail.com (Postfix) with ESMTP id 296C51C0005 for ; Thu, 6 Jul 2023 22:50:53 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=oG6cUbXq; spf=pass (imf18.hostedemail.com: domain of 3TUWnZA0KCKAAXELRASMUSSENGOOGLE.COMLINUX-MMKVACK.ORG@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3TUWnZA0KCKAAXELRASMUSSENGOOGLE.COMLINUX-MMKVACK.ORG@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=1688683854; 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=dr4h9VGDfsn/VMT+xHDPiOp9dXBZSKUD38K7YaReMD4=; b=sQ06Bg4e30OarG+GI8RW3WHXNkTMLUcR5SgP3d3Mr359LqnddddKT1co+Ry7Ug04lxcMVs el4ZQHzNjnjY/G6gM0seUrXCzpkDsKLhXch78PZ3Y23cZQpwFB4zRGV3iIifAmSEgm3U+7 DqzLp+0pgL8YIeb1+4YsKCJ0I9iBV1Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688683854; a=rsa-sha256; cv=none; b=mMIMYEsJKuQhl50SVceK/X1JcQLqWnvnRTbuTyUUpyxlakI2v4fkGI7yV2G2//Of390LLQ N2/oAqS6gExDxdTLpLfjZzANPA5hYgQweFWc//1Cl11XjKlqiNr9j5eDZRbwqfIqYi+/a4 l17KFScCrW+Gw5dvrKPQxtMIoT3VKiw= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=oG6cUbXq; spf=pass (imf18.hostedemail.com: domain of 3TUWnZA0KCKAAXELRASMUSSENGOOGLE.COMLINUX-MMKVACK.ORG@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3TUWnZA0KCKAAXELRASMUSSENGOOGLE.COMLINUX-MMKVACK.ORG@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-c60ba0bd413so1225275276.2 for ; Thu, 06 Jul 2023 15:50:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683853; x=1691275853; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dr4h9VGDfsn/VMT+xHDPiOp9dXBZSKUD38K7YaReMD4=; b=oG6cUbXqueLNKCE01QfHG46TYxjrnSczrRs1z4fAd0XehnlwpZ6QzxEJt4W2Ue3oTg jmIlcVMPOO3wBc5Jmpx0+P7PdoVG9zHOG4l9JNotEr1VVRKn8UBqflwPIUBZiM7amwz3 tHjgfEOpZHvakwUYyo/kFliQBAwyxEYaH9p9Krdz4uCEX03XtWL0k4huwPf4JMBUWiuJ vH0FF8WK3+d9dnq2JbKxfTWxmC9bR4z1x++0EIg8VN51Y6zcrk9sFkWFs+n0Ee8WaTrI T/JjfeQ68QMFZsZ0wuD8e82BwZ+qeWxNN+55lMptu2IruYWfXF6lh4uEunTtWoQohPkt /enw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683853; x=1691275853; 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=dr4h9VGDfsn/VMT+xHDPiOp9dXBZSKUD38K7YaReMD4=; b=QKSuuDFxrMeY0fXCOOjtWjlMyhv6dytYISuqRmAULYqQa1UWNOIg0KSE2IcmR5OVit Zr3ydsbnOlu1z3Dzp0b/QmQAbYfzuN4NxxNNERmG/U+2met17EqkZKzZ4bZMKIX64yrO a6XUFoxEZSRyTDrmIZnTwhiCCZ6nRHheX9PFNMAD1FuPzG3pnhTIioh5dkIbkEXgxv0i pBfg3qpKGNuLx/LgmmcgrdlMBmjQh9JmjZGDLIzN6+lkEQ74VsJQtuncSMeEBP8fx5TH j8PSKA82lLlVBmVclWXqarU2wyH/VhDSdm+C6wfJ0XBu60K4ESImSnrmehUkHlAAFM8T 6UuA== X-Gm-Message-State: ABy/qLZ6GADmzaeVll8IdPbpI3dCu29bhTIsXAj+VtNOVjf63ENdox92 L6KhZN0VYAFY0yoNXxNvvmK4L7p9l/gfkwU1NS+j X-Google-Smtp-Source: APBJJlEl5yfZInV23tQIGQwzZqjUhDMQjVJrb/6PeQU7rd1hzOOT/WdC6F9gqXpUHWmfYN6lE3AUVfAUoi1up8aCXiJW X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a25:c70e:0:b0:c67:e177:100a with SMTP id w14-20020a25c70e000000b00c67e177100amr19507ybe.4.1688683853333; Thu, 06 Jul 2023 15:50:53 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:30 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-3-axelrasmussen@google.com> Subject: [PATCH v3 2/8] mm: userfaultfd: check for start + len overflow in validate_range From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , 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: 296C51C0005 X-Rspam-User: X-Stat-Signature: ap5brkstuo5tyeg3c88tphfpnwrrykgu X-Rspamd-Server: rspam03 X-HE-Tag: 1688683853-800388 X-HE-Meta: U2FsdGVkX1/AMJQfsXBwWvz6YbVM2S4hensfTgalq1AQztBFeUAA627j64aHE3qJ4d5pq/nxW/ACdF3Hh8DIZuA4EzgO4p84jU5Jf9RFk+WkQzT/pGS3jOuHvmoMxUADzQMIdGQ/p68zoLOHl0FUa2NWG6pUoWYLA/VkAYPkOJ38QW+Jr3NXiaHzVQBW/GxLXMVkRHaS6XNTG8Kym4AgTLoEOTgiTVcjkg2EeIxkNLlcKep5YDCQ/PsaY9IKudH60AxLhoVLmlO4PBFo2GXGYt24IEJqNduevRD1RA5VHSX/8KQXgTN/UlnQEoXGsBlDrXOoIaqTnkeJ6keFzaFxjLJPT9T5G6wvC3o4TY6uSuMA5gEjUC5xZEC/QAJsn6uFT39OtCMwp/iRLdZ/VIxftQCwIhhDCmns7+94ypDwW/g/QggWyTFBoM+KjE4hBVKy/DLA4DyeZPmzdwnvB6pjCiOykt5em1Z8E8j5rSWXQKgj2ElK2SS+dRHdlF1bWt/viRfTjsW+VKPkCWDUrIBQmhsufywB3OcyEivPhR4tuDFU+FB/N4SSxrk48hH9LoiP/Zxs9AZeY/yzRILWmxr9Q6VIHHbJ5O2TCFessvG/pUhhGI1xflXXAQqyqQdMMzN8KKeyw+irEc/pcibGc3wr/LGpkpkEuXivAd+MHhCjkeBScjoZ5FdY4+eLWrDnRi4pp9MwsrVAKnSj4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op: owner-majordomo@kvack.org List-ID: Most userfaultfd ioctls take a `start + len` range as an argument. We have the validate_range helper to check that such ranges are valid. However, some (but not all!) ioctls *also* check that `start + len` doesn't wrap around (overflow). Just check for this in validate_range. This saves some repetitive code, and adds the check to some ioctls which weren't bothering to check for it before. Signed-off-by: Axel Rasmussen Reviewed-by: Peter Xu --- fs/userfaultfd.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 7cecd49e078b..2e84684c46f0 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1306,6 +1306,8 @@ static __always_inline int validate_range(struct mm_struct *mm, return -EINVAL; if (len > task_size - start) return -EINVAL; + if (start + len <= start) + return -EINVAL; return 0; } @@ -1760,14 +1762,8 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx, ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len); if (ret) goto out; - /* - * double check for wraparound just in case. copy_from_user() - * will later check uffdio_copy.src + uffdio_copy.len to fit - * in the userland range. - */ + ret = -EINVAL; - if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src) - goto out; if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP)) goto out; if (uffdio_copy.mode & UFFDIO_COPY_MODE_WP) @@ -1927,11 +1923,6 @@ static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg) goto out; ret = -EINVAL; - /* double check for wraparound just in case. */ - if (uffdio_continue.range.start + uffdio_continue.range.len <= - uffdio_continue.range.start) { - goto out; - } if (uffdio_continue.mode & ~(UFFDIO_CONTINUE_MODE_DONTWAKE | UFFDIO_CONTINUE_MODE_WP)) goto out; From patchwork Thu Jul 6 22:50:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13304273 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 85A84C0015E for ; Thu, 6 Jul 2023 22:50:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B6658D0005; Thu, 6 Jul 2023 18:50:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 117D78D0002; Thu, 6 Jul 2023 18:50:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EAD938D0005; Thu, 6 Jul 2023 18:50:58 -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 D6A658D0002 for ; Thu, 6 Jul 2023 18:50:58 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 584DAA0CB9 for ; Thu, 6 Jul 2023 22:50:58 +0000 (UTC) X-FDA: 80982683796.03.3846E3C Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf13.hostedemail.com (Postfix) with ESMTP id 7D48E20018 for ; Thu, 6 Jul 2023 22:50:56 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=HaacAumE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3T0WnZA0KCKICZGNTCUOWUUGPIQQING.EQONKPWZ-OOMXCEM.QTI@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3T0WnZA0KCKICZGNTCUOWUUGPIQQING.EQONKPWZ-OOMXCEM.QTI@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688683856; 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=zDsVvAgbqAnX6PMRHG2ZwgnBlt3+9ofsZz4Jdu5BLSk=; b=S99CrMxnHJygDRS9H2G+aftHCcAoixfEdt4FM6YYKyHLuYJfmSCJbeUOD1WLRX0GuO3xXk OVEAGzPDHJInJUcVqL0B9gFqMzpAI4hHv+rWOISMOn446NEkTwVlDY3hngPJRFHsjEuhzu RdIBj2o869S6A039AWIhYFlBowXxRP8= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=HaacAumE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3T0WnZA0KCKICZGNTCUOWUUGPIQQING.EQONKPWZ-OOMXCEM.QTI@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3T0WnZA0KCKICZGNTCUOWUUGPIQQING.EQONKPWZ-OOMXCEM.QTI@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688683856; a=rsa-sha256; cv=none; b=LGsmtspgeX1QPvDQ+tilT5yjeUSwGpR1pWfKnxyhjsrwGDGcoc64QzgY77xajFGqn0WNZs 2yV+HqFvHJ8O0n3sg38EGRJpOZRQcx0nW0XHiKgAqBCNMfjBtSKr4iFnynqDh9LfuxU7XX aSIygAc1hIRwuXy+hsbUK7HmYpVYjCM= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-c0f35579901so1352098276.0 for ; Thu, 06 Jul 2023 15:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683855; x=1691275855; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zDsVvAgbqAnX6PMRHG2ZwgnBlt3+9ofsZz4Jdu5BLSk=; b=HaacAumEvn1eewgFiWzFDvsikzZVzPppCLT9rnA4TuMVOYLLy/vYJp6o8eS4v2uERC eDEIsx+BKjHd/+jFBv97VKbIdryCcYHF7rTvthDzDOT6vjTdMik5bmYGYq9YywI8gk/E IGbbrW20IF+yQTBxXBW0WV2O4wlEDTsHUqaG4YRBMSJGdJJ7YunZJhzePLuWnx9Wd57A 27l/ThhbyJNl1BV6XUx7oUBXPVXukMu7rVGxP8OS6VqC0GqDxzyj1IOddqpGqcyY2e9m uEIi8hNXSJHFLEllQyYR0l9vuzQePw7paEHgOJxnlvQ+OYByrCTbg4bjwmtqYY5COLUP d0Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683855; x=1691275855; 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=zDsVvAgbqAnX6PMRHG2ZwgnBlt3+9ofsZz4Jdu5BLSk=; b=CIaMHd6VdQQ3n4H0xq3nnnt7p5WmbnIF2Wd/VKXOENeLCMi/x8hyCZpqIcH4Tg1Kkm nida6D2bFMreMCziwGQwqHJQqfHC532OJDdFIVLiFMaPx3LVzgIMSB2Zvt0u2A73THA6 ZUdtoLE/7FXgbQVCCBfDcPTHL6YJNRUPpGrVIePUw8au280jABu7VMm5pgo/ZH7qq5NH nlYVZKwgmXKrIbg6jlEgEeGBsKSzGpKguWHJSnpAQxd7GsvDxsg767VPdjQXPTFpLNXD OSjWG3N3j8FS5LalrN2eQ1jQYthC8eo0pKCSVQiLiHdK6TSpMo7MXhFuwqR0SZBLaiUw f4PQ== X-Gm-Message-State: ABy/qLacNyzWfWEi63gZ9CE/IxqmIky2gquSjN5PeD0GklBdMW/itL46 /G9zHnzq93aoJovY9KJGf7v83crNiN2yhtakMLCo X-Google-Smtp-Source: APBJJlEyx3c2b3n+HetuTYESkQKsyNdyuLP/Hi0we79ObPjhaTnQQ2CLDQEif0ahTjNKD3svWuq1bxNKKEwVrZS6kxor X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a25:53c2:0:b0:be4:7214:7aef with SMTP id h185-20020a2553c2000000b00be472147aefmr17503ybb.10.1688683855407; Thu, 06 Jul 2023 15:50:55 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:31 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-4-axelrasmussen@google.com> Subject: [PATCH v3 3/8] mm: userfaultfd: extract file size check out into a helper From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , 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: 7D48E20018 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: u9jypqfbhbzp88xx3g3i7osyjoc4inqm X-HE-Tag: 1688683856-823135 X-HE-Meta: U2FsdGVkX19xjPcBphDNVposR4/sUGhQh6zTrJYtAz31NX5Tty4HYRLycLaA+qAdL9ic7WTaK43c0KEEidXCq5ISmGUQH4gl2nwb1yKIp+wbTh5HT/hmPeOa/QwA3AIhNFHo4iROqjUn0Xbn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XDZWjFumRY7lqLUwCkGftiCKzhTCPzBe2axGwTbwkAKhVjjvEblQRUFMv6aaV3eZjmAVewAr2SN234r4iqx8E6p4Rdjrx5Zpfg0OHvwW6El9uZnt72Ei1zTZiuvmtQiZtc6VB4LeIqJaJ5lqBm/rycEX+4/30elJ1oTne501okjybo4aOhOHYoN9O2yLa+uY50XrqcYYto/+dlyNJxcb9B/mqI8ScJSFpDErNhul8EyJd54reOJ+4I3kL3shc1+ZQ2/EdWAlT1V9lZlHd7IO7D0uq6Ep/FKTn0TwoF8HfZmWbagePR0xjbAXnVymV3qjVbJYd66e6JctWiegiQsmW7BqOQKK1fjNpL5ojP6ddSeiuRiUqGfEm0buCf/wt4mIVt17pUwvdcNWXSCu+05RQLhpkSvsP9rJ5RmTwjQJLDjSkOvz/oikZ15 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: This code is already duplicated twice, and UFFDIO_POISON will do the same check a third time. So, it's worth extracting into a helper to save repetitive lines of code. Signed-off-by: Axel Rasmussen Reviewed-by: Peter Xu --- mm/userfaultfd.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index a2bf37ee276d..4244ca7ee903 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -45,6 +45,22 @@ struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, return dst_vma; } +/* Check if dst_addr is outside of file's size. Must be called with ptl held. */ +static bool mfill_file_over_size(struct vm_area_struct *dst_vma, + unsigned long dst_addr) +{ + struct inode *inode; + pgoff_t offset, max_off; + + if (!dst_vma->vm_file) + return false; + + 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); + return offset >= max_off; +} + /* * Install PTEs, to map dst_addr (within dst_vma) to page. * @@ -64,8 +80,6 @@ int mfill_atomic_install_pte(pmd_t *dst_pmd, bool page_in_cache = page_mapping(page); spinlock_t *ptl; struct folio *folio; - struct inode *inode; - pgoff_t offset, max_off; _dst_pte = mk_pte(page, dst_vma->vm_page_prot); _dst_pte = pte_mkdirty(_dst_pte); @@ -81,14 +95,9 @@ int mfill_atomic_install_pte(pmd_t *dst_pmd, if (!dst_pte) goto out; - if (vma_is_shmem(dst_vma)) { - /* 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); + if (mfill_file_over_size(dst_vma, dst_addr)) { ret = -EFAULT; - if (unlikely(offset >= max_off)) - goto out_unlock; + goto out_unlock; } ret = -EEXIST; @@ -211,8 +220,6 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, pte_t _dst_pte, *dst_pte; spinlock_t *ptl; int ret; - pgoff_t offset, max_off; - struct inode *inode; _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr), dst_vma->vm_page_prot)); @@ -220,14 +227,9 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, dst_pte = pte_offset_map_lock(dst_vma->vm_mm, dst_pmd, dst_addr, &ptl); if (!dst_pte) goto out; - if (dst_vma->vm_file) { - /* the shmem MAP_PRIVATE case requires checking the i_size */ - 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); + if (mfill_file_over_size(dst_vma, dst_addr)) { ret = -EFAULT; - if (unlikely(offset >= max_off)) - goto out_unlock; + goto out_unlock; } ret = -EEXIST; if (!pte_none(ptep_get(dst_pte))) From patchwork Thu Jul 6 22:50:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13304274 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 D9AF9EB64DC for ; Thu, 6 Jul 2023 22:51:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 474538D0006; Thu, 6 Jul 2023 18:51:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4266E8D0002; Thu, 6 Jul 2023 18:51:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DB5A8D0006; Thu, 6 Jul 2023 18:51:01 -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 0BE558D0002 for ; Thu, 6 Jul 2023 18:51:01 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C6226A0CBC for ; Thu, 6 Jul 2023 22:51:00 +0000 (UTC) X-FDA: 80982683880.11.1C191FD Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf23.hostedemail.com (Postfix) with ESMTP id EE49F14000F for ; Thu, 6 Jul 2023 22:50:58 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=D2k9xCsf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3UUWnZA0KCKQEbIPVEWQYWWIRKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3UUWnZA0KCKQEbIPVEWQYWWIRKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688683859; a=rsa-sha256; cv=none; b=oCVJtcjsTsaT4JHHr2ygHcCJ2+FaygbUNTMUXfKj4/7cYALn/nN5MLngS6o4u4uSa71os5 QeAYI5sMtRZwYCAq05CnHM7+10OPrySbSsmrgctstyi4/F5qfKwyuzJlQbC9aBMW/ypyUh hLMIoljJGymHB/EIRieWsAD17Y4zlgY= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=D2k9xCsf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3UUWnZA0KCKQEbIPVEWQYWWIRKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3UUWnZA0KCKQEbIPVEWQYWWIRKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688683859; 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=PJqf0m2qw+Xjs8Quv7ufLANzkWFzCscIfWrhHyj74r4=; b=6N7ftu79KN9Q4VwdhbT4nihlAW/5kXNTI7q/LlpT9hzrNhT1WEq57aNAyQLy2GtRcEcJhf 2gAIcH1TC45hstJsz/qZJSLuvmF+TL/GZt09emCRZ2HYFuHAsIrr7kpP9qoTGXDTVS/WFx Jn4mwtVbfdRqJWFKS49E3qGvTU45uqk= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-57003dac4a8so34056827b3.1 for ; Thu, 06 Jul 2023 15:50:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683858; x=1691275858; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PJqf0m2qw+Xjs8Quv7ufLANzkWFzCscIfWrhHyj74r4=; b=D2k9xCsfeg61xPSIpw+bsBr3jPwLNm9nVEHyGhqfkG8lUTP35cd6IFL212tdgreqNz 0YlkyIjNjF30q+hjO/oW6G1cNPIRff4f/oHqF7Wy+jVNAq1/WuXr6jz15FTr0ttknNyr 0KjASHAlCDktsRxguFNoBBTItvv/YFICr/K4mQRPcS2hhFcyPon/giYdNfLAM48U0Jx0 eXvwik0EB73EKMqASVZMZZsmmHmToMBPVsvzhfYgIrpjZiLkZdUoFdyj4hnakuxSym+M ulEcJTqFS3AoXIeAsnUQhvNIC+QtVFc1duLHDu/kIEV70/F62oZksyCAFiDKBKPrHFko dgvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683858; x=1691275858; 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=PJqf0m2qw+Xjs8Quv7ufLANzkWFzCscIfWrhHyj74r4=; b=cBd77nACOsyjC5H7lUKB9GKx/eRbvN86xD7ykpvTW7wbh1tJz7Zq9omZjVRhIQLEqE ZmBj4z5hrWcCJPengIiQKdFRK0oWmANlf8eQ8Asrpiv8KDZzFq7tY8un3JZ2lgBd3HSj R40fHvOZJsU6x/l9n52l0Jz2YfuhD7BkZci4msvWcPNKU05lJMqT99ikpB3FjYr9PiGW 8TuwXWzV6LTpE+akeyT4V5m1JU0sAKmqeAQnZyp04piv7ACubvuZf27vw371BDYQzQKt 2aXx5uGUlTTtB5pPAuxeWHJkIihf4bhNbYWypfK/W9zl4bMvjxlOxEJPLsRTuWlNJV61 u6AQ== X-Gm-Message-State: ABy/qLY3FRSuohMbUfkQeNlB5tsJStJSR8lORVzlCvUfxX5fX2nFIRdy dskkDoO3SRJURmzWkXTjn+wQOv+dzwPcbH+oHdzx X-Google-Smtp-Source: APBJJlE+eG+2pcJk+y2idAnX2/Q8CxWYDm8cs4gjQZ7N5JmAkaEanN8FC+O33rAco+pnBzwZaUMX3uj6yPdrSXWcsU/9 X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a25:d17:0:b0:bd5:dc2d:9d7f with SMTP id 23-20020a250d17000000b00bd5dc2d9d7fmr78836ybn.4.1688683857962; Thu, 06 Jul 2023 15:50:57 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:32 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-5-axelrasmussen@google.com> Subject: [PATCH v3 4/8] mm: userfaultfd: add new UFFDIO_POISON ioctl From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , 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: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: EE49F14000F X-Stat-Signature: eee9uyp95httfknkg8f453fs1bkrhpmn X-HE-Tag: 1688683858-769062 X-HE-Meta: U2FsdGVkX18uSNSrvXy6kyGUYcovbXgu5p66M8vkPyFD9yWpUzO09UZpKyyTbMJo2xkKnyES3BsUqeHcnfqpeWdGB8woWqvbns0rmUlLRMJp9ixzwEQJiH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8HpXGjz8L1zDqfe61oMs067XRtayTKeOtRrJ/MQgQvxgBkO32SwBMYJ+BOS4QgWe+VKuHjnqErQ04VArBhbFFLJmVWNneHeWj8/r899OXsB17UjQUt0HEXpJ9Gsg+CPrDN2xmvZomrfU5wPUWHmhFgovRe1z50EW5flFe3IuL09b+qI4ZlUh3NjGVOJGNyz+FumcA0w+4ouAk5/9bzSZ6feukPR4rKZISA+nF9gYaACVjOCKfx8fijL7hEnrAtEZl59/oVDhsW3lPrkstb326FScIrxTY43MrqOlsq5P12KArNenD27UgASJqzDyKCJJFJyB1KyCPaebhuX59sXj9Ay/6HuQ59EcFra9zbzJm3t0E9o2SahLIIH/EqIhaCW+gQUjAr631hu4ITZs1VpHmC1EPs2k/CrHxyosaHtU7kaGc9w2dDtLzGqUSLYWzBa/VYPZgJ8UT3V6uxZRKGGriPIJMfbQW5ahJYi1L7xU7zoGjDBSMNIAeAsJDwbw== 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. Similarly, UFFDIO_POISON won't replace existing PTE markers. This might be okay to do, but it seems to be safer to just refuse to overwrite any existing entry (like a UFFD_WP PTE marker). Signed-off-by: Axel Rasmussen Acked-by: Peter Xu --- fs/userfaultfd.c | 58 ++++++++++++++++++++++++++++++++ include/linux/userfaultfd_k.h | 4 +++ include/uapi/linux/userfaultfd.h | 16 +++++++++ mm/userfaultfd.c | 48 +++++++++++++++++++++++++- 4 files changed, 125 insertions(+), 1 deletion(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 2e84684c46f0..53a7220c4679 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1956,6 +1956,61 @@ 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; + 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) { /* @@ -2057,6 +2112,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/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..b5f07eacc697 100644 --- a/include/uapi/linux/userfaultfd.h +++ b/include/uapi/linux/userfaultfd.h @@ -71,6 +71,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 +92,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 +228,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 +325,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/userfaultfd.c b/mm/userfaultfd.c index 4244ca7ee903..899aa621d7c1 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -288,6 +288,40 @@ 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_ERROR); + dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); + + if (mfill_file_over_size(dst_vma, dst_addr)) { + ret = -EFAULT; + goto out_unlock; + } + + ret = -EEXIST; + /* Refuse to overwrite any PTE, even a PTE marker (e.g. UFFD WP). */ + if (!pte_none(*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; @@ -339,7 +373,8 @@ static __always_inline ssize_t mfill_atomic_hugetlb( * by THP. Since we can not reliably insert a zero page, this * feature is not supported. */ - if (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; } @@ -483,6 +518,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); } /* @@ -704,6 +742,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 Jul 6 22:50:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13304275 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 EFB8AEB64D9 for ; Thu, 6 Jul 2023 22:51:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 893AA8D0007; Thu, 6 Jul 2023 18:51:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 81E008D0002; Thu, 6 Jul 2023 18:51:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FAB48D0007; Thu, 6 Jul 2023 18:51:03 -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 5018C8D0002 for ; Thu, 6 Jul 2023 18:51:03 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2577EB07B5 for ; Thu, 6 Jul 2023 22:51:03 +0000 (UTC) X-FDA: 80982684006.28.8AC5251 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf23.hostedemail.com (Postfix) with ESMTP id 5BFE3140007 for ; Thu, 6 Jul 2023 22:51:01 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Y2yizmMp; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3VEWnZA0KCKcHeLSYHZTbZZLUNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VEWnZA0KCKcHeLSYHZTbZZLUNVVNSL.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=1688683861; 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=Qwjh0UAlAYXVze8TCXBt1h/rZKLWFE0vCD0k3jVLMVk=; b=0mrJa0+X4XhE9P1JqEh8AWMTbdtX0w+/nz18hQEQ1MjjOefHYFxB/0hef4hvWdXqbOcAYn dZaT2p/F3eA7yS/sQZ/bzvXtbWQCX5Oi7TcltZaLauoHz3az4tWqXWTajKxrR/VuB6LTbH sRo08EZQRyD2EtoXwSy18oxEmRVThMw= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Y2yizmMp; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3VEWnZA0KCKcHeLSYHZTbZZLUNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VEWnZA0KCKcHeLSYHZTbZZLUNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688683861; a=rsa-sha256; cv=none; b=CauUBlzsmVhhD8Qhscc4IY93lSLj1YQ/7spuBX20+8apdZScd+/2ZGEJSxevm/v8oThB1b K3lm44vxpd5ankfApIj3Lod9S8IGb6rknAc8grGjEvIQ1V+vFUdagw/rsvG02gnr1Uf/NN rEeOiIGEQ/6cTXATVNiuYxAajV1LANU= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5704995f964so14134017b3.2 for ; Thu, 06 Jul 2023 15:51:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683860; x=1691275860; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Qwjh0UAlAYXVze8TCXBt1h/rZKLWFE0vCD0k3jVLMVk=; b=Y2yizmMpjzL0EwPNfj1fzhhPy4fB1Fio/333NC+Pl3+CabD5WvQiBObIAnbDvfdIz0 55qqIyoV/B43yRJQMgc5PRzcnEO7oZ13pzUFcnMsRpYGZDSVSZeMWMJRv9WA6h3i6GfT TXaYPaz84vks82iZ0vbDvAGXP/Ef7g9EhKOVR1CvAJFhj45Vp0/ta+dk/HgbDOBioOwa 2BKF9RsPqJx9l5jS/r3knSsrH/fNyDrHo/fZURqQr/YXnDI5GwojI2TLVLkJxGr1ous4 fBuKAKMZ/SIafScZO/gr72YFQKtZJ6oKMnuQ1yiJQjaFtVwKCY9iv6k7Pl0tvv06AM6a 3A8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683860; x=1691275860; 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=Qwjh0UAlAYXVze8TCXBt1h/rZKLWFE0vCD0k3jVLMVk=; b=Dn+ZPuyDWC1DQAAAq2yKxUET/rTyvkGz7KuAPeVHa14tiFhe6e9Ovy6kmGKLKw9nYt VYc+tg+1mGuVKGoDLc+uaPGq6mJmv+mvzpj2j8Vqpxq5QtMKrdem9inrs7yJcQA8hKo1 ANv1xLTCdlZfi0frJN7eVojccgPcU+Fe3ghjSXoA/F9IAIJvzp1YLcLdUTQ1vGBssjpW HEcs//my4x2eu/9z3c1So7k5BDLQwoLVjwj45TBKOblWZy289CrPAqFtbxo97ltW4H8t NVJg0H/2xNDWjjeE1LZd0b1LNOPrxsknES1IpB8l2uJWNMh1qitSqTwoHulMlj2+owU8 2oOQ== X-Gm-Message-State: ABy/qLZEDllUi65pSR8K6B3WPY6VP8xhaX4OabZvy4BHOLn4W4ZswJQ+ LaN6YY+WfaRJ5xtxGe2rFbWfjfyYllAPHff4enHF X-Google-Smtp-Source: APBJJlEpiTBdlhXFZSaqCdtDMzVQ5W4G5GGFJKBbdNzbEaJCnt1GA18H5a/L9p5DQaoHYiuhi3h9J1K3iDtJg/4EMWJg X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a81:c642:0:b0:576:d9ea:1331 with SMTP id q2-20020a81c642000000b00576d9ea1331mr27910ywj.4.1688683860327; Thu, 06 Jul 2023 15:51:00 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:33 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-6-axelrasmussen@google.com> Subject: [PATCH v3 5/8] mm: userfaultfd: support UFFDIO_POISON for hugetlbfs From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , 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: 5BFE3140007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 9n95my3546yxwunzz8afn6pwof6paqfq X-HE-Tag: 1688683861-203151 X-HE-Meta: U2FsdGVkX1+5x8okTckQp5N2aF6evhmIQOFrPqqHS51uQwSKqJrwZZyScizzqtiAIR+C3VWpbMnkLL0S/MDzcPikyR3FqqXH6tw2IsQYPP79ppHcZwHHS8Qswxu0KIwRnroL1fJbrmpr0BcxsjU1qOIosPfy9lyiNoZLcGVaIXoUKB8i5ljduHUw7zFstlir34+Bx3ScA2AG6kSbL27Qyz6SLNXeGcsAmca1pHKkULav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FSP5NpnqVLgn06o95Ta1vq1U3uaHgR4vNTRMsP6y+SHp5EUQq4MuHOp9xl2KpxUOayIlhWHyN83fhs/5jj1c7ZX5UEB1ZEhOWCRiFtHco/33mm1G2N+F9o7s7UzGlgtuE4AF81h9ePJss5rMDCn3JZ50cfpQZV4C0gtupnNT6bVI/6zy+SZGHNHpTgKUEzGVI9wiKEYuFCWTt5do+Yeryqv5hTZLLSniNNamIroYdHK2nRkJcQtnkVB9LgSwFSdpJt8fKjOOYBYh7hn+Mot0P2Q9O3q8JpJPZQba2OCqtnYEZ5cum/R3B2n8KUfdqAp73jfpZIqSUxKF/bK3JNuTf1B0GYeVnnYbW+xOGgWeOTTWBRoDXYO9wP402hOC+/UA9oFfRFkJRgUU8kbALNLsG2S5oET4tUz3p/g== 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 Acked-by: Peter Xu --- mm/hugetlb.c | 19 +++++++++++++++++++ mm/userfaultfd.c | 3 +-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 934e129d9939..20c5f6a5420a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6263,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_ERROR); + 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 899aa621d7c1..9ce129fdd596 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -373,8 +373,7 @@ static __always_inline ssize_t mfill_atomic_hugetlb( * by THP. Since we can not reliably insert a zero page, this * feature is not supported. */ - if (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 Jul 6 22:50:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13304276 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 7FBA8EB64DD for ; Thu, 6 Jul 2023 22:51:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0567A8D0002; Thu, 6 Jul 2023 18:51:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F23068E0001; Thu, 6 Jul 2023 18:51:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D9AF98D0008; Thu, 6 Jul 2023 18:51:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C6F018D0002 for ; Thu, 6 Jul 2023 18:51:05 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 1BF9C1405FE for ; Thu, 6 Jul 2023 22:51:05 +0000 (UTC) X-FDA: 80982684090.13.CEC82DB Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf05.hostedemail.com (Postfix) with ESMTP id 45936100013 for ; Thu, 6 Jul 2023 22:51:03 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=MUCpPcJ5; spf=pass (imf05.hostedemail.com: domain of 3VkWnZA0KCKkJgNUaJbVdbbNWPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VkWnZA0KCKkJgNUaJbVdbbNWPXXPUN.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=1688683863; 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=7OMVl3YxFVN2A+Ry/UxbO1YmCHHRZcxw7VDxaJg9ek4=; b=D1ywCopONE8ALByeJKPqWKOlRRrWM4ZwQakak/GTSPTlUwu9xKDWo1+GICdlJJxK5tevb/ 015SrflOUDG6LVg9owbodUtdYEowo5E+8dfkIZtx6XvX+Zel4hLaVC+JWLlAkzZgA6yMtI hgmLCB9JWxQdp3pJ+1ablbU2/Y983xo= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=MUCpPcJ5; spf=pass (imf05.hostedemail.com: domain of 3VkWnZA0KCKkJgNUaJbVdbbNWPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VkWnZA0KCKkJgNUaJbVdbbNWPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688683863; a=rsa-sha256; cv=none; b=s6EDO68cumqKYp9V/uQRDmNeN5jOGwBWV7PyyvtdkgWqwCYVXBmkl45ldnGYmycA9R7X7o jWbBZStYtTKIGtcPEkznopuHYQtdppXKDUYg06Pibp1XyFn2PF2FPuGixVEGPtwkQ0D9QV 36Ap9FoiQ2XpO41ZWtuHscoNKafcS2Y= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5704970148dso13017257b3.3 for ; Thu, 06 Jul 2023 15:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683862; x=1691275862; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7OMVl3YxFVN2A+Ry/UxbO1YmCHHRZcxw7VDxaJg9ek4=; b=MUCpPcJ5Cgcqj6eW6sHdBBG776f5QGOT4kgvGYw/zAIra7r+ur1/6MP7Dq6MkoSZmq 4xRE3PYyIXD4h8qZWQWCXhWmcWP1AwGUjGAf2ElqV4yAkcG0gjhOddOoakQQadmYz+QD Is/7A9d1hSTithrMC6POOEAOeFEch61LmagZDJM30S0vYIcKNDDe5/OQ3z3oRxh6zHQl utpA3lGtPFljt8j9Ntkp9LhkEyWfCZ3raeDW8NKQTH9gVvZpq5Wizvy5vkyeCh1Pwwja CGEW2G1lqZ2p9p/Ga+J136aVMeRPFS8hQNLl5QI5DDjAJCWHkUOphl3NTJbnSXMCQUoE o8sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683862; x=1691275862; 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=7OMVl3YxFVN2A+Ry/UxbO1YmCHHRZcxw7VDxaJg9ek4=; b=Z52rXm5iEFLSUCsSVUd+E809ndMf/I3yVWb1rC5GtGBf/Iks5UEtnzQ5llpdkdvCu9 Irdo+JUNQPMt1uEBVHubbyU+dH3L0TYCqXtkegLDxtTFSYIyRh4CkhjppSSsYyxj5aHw 0mqt3prNPNPvQP+fp7V1QVBz9p0vfAU99kiNFIL39HJYq6tjWkDt91s+YD18w/7UsL6D ATOrrPMi2jaEE8t8LPpU0StZQFAbVNPIH6s1OR/i0XHY384TK5hVgPQpfJ9G0Ulh9JpI Z7mYqMKAI3Sj2vx5/2yPMGxFO/8XwF2HN6kJX6fLo3FHriAr/Qh8un/FmRxVtJAKRvps OEpA== X-Gm-Message-State: ABy/qLYZYNpRgiqOiQLBber6gt81ywvdjBeC5Rq+PNVKLpvB6x7vp9ki 9hNMU4FZmbeJpv3SVkFsde+en9f/1CfUH2Z6YAGL X-Google-Smtp-Source: APBJJlG1dkyvwdYp0IICzgcbHzfcY4HpHckyy/miTsDbBSll9Umrn8xTYfU/5fbclpora7Bohc9XblNeG3IvM415E6Ta X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a81:af07:0:b0:565:a33a:a49f with SMTP id n7-20020a81af07000000b00565a33aa49fmr25333ywh.6.1688683862421; Thu, 06 Jul 2023 15:51:02 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:34 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-7-axelrasmussen@google.com> Subject: [PATCH v3 6/8] mm: userfaultfd: document and enable new UFFDIO_POISON feature From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , 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: 45936100013 X-Rspam-User: X-Stat-Signature: ep8ursnzcpegeprcwgghofincmzhte5g X-Rspamd-Server: rspam01 X-HE-Tag: 1688683863-137044 X-HE-Meta: U2FsdGVkX1+z5f/lGE54gtjeRdJNYjnxG+0kFkyv2GPOiF2YGZvsQGfqDTxFt7ri7oNK03psK80Lg20+un5caMNVeL7eDxBCzQhcQc8Tbe1jth+26gQzrlda4lhi5L0X1A7um5MNzIVXSCZp0Y8szOhKxcNwx+fVq48JCXUQMamNE5TweXMYXUe02mbjRB6ZaWo9zg/kmt8oO7ZpsmXuS7RKT2Uks/XLlN26sLI2OTmkZyI0/QbbUza/mj/oaJRSxwo1lUerJVIPdZuu6MSQiZvLcxJskjJ0bSYpT7tMzxuFLxpFjap+M8PCoaYVNSSQqKb6DqmdiT+PlZwlIZtCvDFEsk8bd2o0SIEqwOsr+ud7mswQk1pL1vNs7AqlqU1wUYYhMdSk/4/zlLoeJgbTe7B/hO/rdogyvfbI6A58csx0yX91ezMPU+Tzy3zZbYQ3gqy/AQLodaqtXtSL6u5jfwo2EngFrWS9frm1aWbs4aByjZR1XTxPkB2U99Xk0Orb8lH68HCsBNyUwsPo17QapFK88/wSQjUK6V1XlcUrS7op+55UkLa3oYKFUj1QK1ZbacLDziOTC8ED2Lw7RnE9IZYj6dQtFWWvuXxU5HH4MRe9PmwqHLyf1M3xMuYzylUCqVHpR9yaGgysBhY8Dg8gHUQmcYXUfw7O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isFzYvE= 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: Update the userfaultfd API to advertise this feature as part of feature flags and supported ioctls (returned upon registration). Add basic documentation describing the new feature. Acked-by: Peter Xu Signed-off-by: Axel Rasmussen --- Documentation/admin-guide/mm/userfaultfd.rst | 15 +++++++++++++++ include/uapi/linux/userfaultfd.h | 9 ++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Documentation/admin-guide/mm/userfaultfd.rst b/Documentation/admin-guide/mm/userfaultfd.rst index 7c304e432205..4349a8c2b978 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 ostensibly +doesn't have a memory error in the exact same spot. + QEMU/KVM ======== diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h index b5f07eacc697..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 From patchwork Thu Jul 6 22: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: 13304277 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 97B27C0015E for ; Thu, 6 Jul 2023 22:51:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A19E48E0002; Thu, 6 Jul 2023 18:51:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 97BF68E0001; Thu, 6 Jul 2023 18:51:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7592C8E0002; Thu, 6 Jul 2023 18:51:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 64BA18E0001 for ; Thu, 6 Jul 2023 18:51:07 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 19E78806CE for ; Thu, 6 Jul 2023 22:51:07 +0000 (UTC) X-FDA: 80982684174.25.78459A3 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf19.hostedemail.com (Postfix) with ESMTP id 438371A001D for ; Thu, 6 Jul 2023 22:51:05 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=zVK6nDx2; spf=pass (imf19.hostedemail.com: domain of 3WEWnZA0KCKsLiPWcLdXfddPYRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WEWnZA0KCKsLiPWcLdXfddPYRZZRWP.NZXWTYfi-XXVgLNV.ZcR@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=1688683865; 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=eAe+9W4QFAtfUXcQRRwl94udp4bagyjbxgyezGm+pEc3MUQsZM04UlUFqkmO1kLwUbNp/7 LPC8mSCM/5/PtSv2uKJptBoBFZl7vl1ujMHcQJpZ0ewf4G5mas0soS77bRIEcHShOsW3Ll QG3OVedJnY7q6bB5of/JCDEBnm9B414= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688683865; a=rsa-sha256; cv=none; b=2ciC5NHxRBY9z9i8fQV8ghKXheILcBMNLMCkRaJnhfEgKG4FsWzQnR60uTRRjxIwGPO3kb Xxv3uLTIVRsT2cU4KUEujZ0svJJ1j6bpx7HAnrhkMKmy97nnWNJBldYgvd/s2bz45qeqb+ XNo05RNdBGeF6NZoPp783dz1aC1nT7k= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=zVK6nDx2; spf=pass (imf19.hostedemail.com: domain of 3WEWnZA0KCKsLiPWcLdXfddPYRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WEWnZA0KCKsLiPWcLdXfddPYRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-c5a17bfb38bso1257102276.3 for ; Thu, 06 Jul 2023 15:51:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683864; x=1691275864; 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=zVK6nDx2/vIL1dc7h7hFZDfr/rEOBcNDC5lzAQ8fMfaY2kDXm1d90zN+sAdZBRkAVy eS0e/cYLAI5GlN5hCyqzAZsI3npJTnOPGdGRNCf5Z9XQRS26+3V3iKb68AeK1pqfrrK/ xI0bVwy1i7i/os5XjQVIXRLYlXw2zGr2RbOxqfH7TjC+dya4aBmpaEQwquOV67eYDYli EHdK6eJUGxQqPuxQuTMsQewM+3qR50sGQFaisJznV2893CLPnbjYkC1qV0z6IJvBHP2Q Qm/1xZWrhlds7/VAWCEpvN4M1cI6E8ouf/4BdK8WRTe5GRh7RJaWO+df/EurZS1IsV5C ESHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683864; x=1691275864; 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=Gsr5zCkNj4kBKfND6JSdZuuSR/E0Jz1JL1FDf+AA/91liB5ppGh8aTtkW80dWP/pMJ 7qiBPBCjLx5r99hZWQrXlwKAyHOdufJeEzdf7JBa5PW/W5uFEodo2fj87rBeA1L3NbEq 484v6cMTLQhHevnzLwp5gkCJYwqQgmAwJjQSi1M91DNVM2kDEVLUShrcjoPzDuNNZiVJ cwNFYpNiZkLhEXVDATv8xPHrV+yIsEs5NHyeVDh23H4n/bvPiBSLn76qZdwUd/gctlBj LkGKa2RKFXeMA1V9LICIMP1YPLKItpf8UM2i7Gsg0bF43PEQ+3bXePhKCZhEke961NFT ZHQw== X-Gm-Message-State: ABy/qLbj7Jefu23Sh7cwXjUH8cVdUG4e7K5GqdzlUV/3GjZOI+kOlpOq QNUVo+xU+G4QuVY5ov82TvkTOV5BJkfZcPnKR4fi X-Google-Smtp-Source: APBJJlGqdtbTPga9QJm8ffVaUw6WvKONQskr2t33RDHlmoUv4ugqzz1h75IhlcTDYghh9LVvfKAy5QaL8bfvdcxVM2r/ X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a25:b193:0:b0:c01:e1c0:3b8f with SMTP id h19-20020a25b193000000b00c01e1c03b8fmr16283ybj.6.1688683864381; Thu, 06 Jul 2023 15:51:04 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:35 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-8-axelrasmussen@google.com> Subject: [PATCH v3 7/8] selftests/mm: refactor uffd_poll_thread to allow custom fault handlers From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , 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: n5o7hxegm4do564ej8o1sfmwdo4759hn X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 438371A001D X-Rspam-User: X-HE-Tag: 1688683865-28292 X-HE-Meta: U2FsdGVkX1+zx4DHXTUKpAqsB0ajdYAFLpeW7Q+72o8ym8mL3VKrEGalxrkjHDsOnpqyDSOz6a5TUGZ2Z7bsowWhnFacYBHEJlKtiO7eqEC/H2qTlgRBAxSRHHzn2B2Bm4slBwbIESHKmfyLSeH2hCbmJZFpcBbCVwrLtg84TjWsr1eqIPfzUbUPNP8Wda4DrMWNfkaIWdNxnEGdXCVPcuJ77Q/2j68TljHhMOfUpqCuprSDjhL9yqNmmmE3lIVG3cHCZ59heT+b1g8iyCtWJUUzXOt23g4+gexHmwZfnDUEsiZcvqTZscDY9en0Z5+rRG3GDVirF+Oc9vnraJMFu675f8fFuvpo2udcHvN/0JfCS4+VzTINmGcEEcywRO6jxTFvvquMFrQ5ouOvQqwR47PrFspTA8y+WgG3GQywCFTpDWAzse6fyMmH1OmBHLtt6CJbrMZem4q1L8TQlHAsH2ZGxHSZN9jBbw/xeQtxFFdRS7jgGf9gX6o3oRG96G9zSaXrqs261nHSmvu+E3W2N/e3Zo85+GUBlP0Ba+trJFXf9/z+T0r+FIPPpxnD0UIzbrC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1AJJEdiEiZCYE4Rfx6aYjOLAp0xh5mCWLtiTN6EDaNdnDRA== 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 Jul 6 22: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: 13304278 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 F3837EB64D9 for ; Thu, 6 Jul 2023 22:51:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A51F8E0003; Thu, 6 Jul 2023 18:51:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 92FA28E0001; Thu, 6 Jul 2023 18:51:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7822E8E0003; Thu, 6 Jul 2023 18:51:09 -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 630BD8E0001 for ; Thu, 6 Jul 2023 18:51:09 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 37B401C80A9 for ; Thu, 6 Jul 2023 22:51:09 +0000 (UTC) X-FDA: 80982684258.10.3393B39 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf21.hostedemail.com (Postfix) with ESMTP id 6EDF51C0018 for ; Thu, 6 Jul 2023 22:51:07 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=EK01mLyp; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3WkWnZA0KCK0NkRYeNfZhffRaTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WkWnZA0KCK0NkRYeNfZhffRaTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688683867; 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=vNyM+7c0est1wcVkLGeYyWS2zFzaKr32nANrKlDiGpI=; b=yjjsEN+P2YsB5lMgZhmzFCWUcboJOASh0yl/YOhhCUptxbKADZyRkrkGXcu8ddqJiI8y9o iN7YXENPvjFO7rUi+dCrLTPqw09m3kyCH3Hj9W77a+o9KPa3jlIGLIPnV5LoD/2/x5S4AO NF2yhAkpXNtXTBphrVLEOjPITULzvCc= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=EK01mLyp; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3WkWnZA0KCK0NkRYeNfZhffRaTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WkWnZA0KCK0NkRYeNfZhffRaTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688683867; a=rsa-sha256; cv=none; b=yRZRY/ho+C8ecPa/TLZo4teZTHMZraNtZIAHJmVxjOiIYK0orKKDdV7G+4sRsM/gFs5Ayz 9bXTZx4zvdmc5aEnsutKEMkUABZbD1v3L1CFnXm+7iKKQfJxPyzG8B21Xj9E479R0A6j9U /zeGA0i6d3qahIX3yDS/KcKtnjRgx/M= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-c6c616faa39so541305276.1 for ; Thu, 06 Jul 2023 15:51:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683866; x=1691275866; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vNyM+7c0est1wcVkLGeYyWS2zFzaKr32nANrKlDiGpI=; b=EK01mLypW26xwbB17tl+WVi77pgJ0OApJdOy0ZdOVv84/lYvfVuW7WowZ4rEK8f7WX g4rTBFdn/gfopVFptl2AY7oR1MbOxcuKJ5NLlgJS4KZnwJQ/O9DYar58Cp6+LJtem2dz 7aV6c0xPB8rtx/DVLBBYyqqPk4Y5aP7bx24BzHZVQh7fjXNIuWh3WsjiPncdyuOWOy0f szlc4nj/Zd3/Khbkib9ISdsge3HmKG+4el8u83yJEUvEUWR2sl14PZi/zm9NTrqicFDS XKFqXwSRFH+ICd8i9r7ldw2FSna2YUHpUhgptiJGArRniVNPuiWl7n0jMNBuoRiJ6+di 6/Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683866; x=1691275866; 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=vNyM+7c0est1wcVkLGeYyWS2zFzaKr32nANrKlDiGpI=; b=Xm7/cGu/MHCEXa+jNc2LKgtk+VTt0qjuvdHZTpZ8fI733icvBTs+WbtAycv4f7CMoW XY8K1g9AiwpWA1iuompg4OpzUWPn/B9XAQIaiKb9xNV5oJ23Pwi+J7NCN+moeqXGVBT+ Bezr7rsZOfTXF9yeqAiLSE4J2sP0ph56ph9EYkQGaBHrTCelqO5eVn4GTOwDboPe3QJN uIqQRnXm1SHouLi5Ze/zP/Vl/5HZK3U3bbWT+Hu82C3GbiWNwBE3qe8qJU/QkqVil+rW L0cQ07TXV4ePX6RHfLbzv/yVlt41Ff6wz/UygoY71KWlTgMix5NPMhVVAQJ3m1IRHZli S5oA== X-Gm-Message-State: ABy/qLYkgFK1Pq7mvP7KZWgD7zljZt6SJJVXlJs5YPYangt++HwKI0m0 Y1WkoMFhuA1+OPND64WiD39aUbRIJR9/zTPxOQlS X-Google-Smtp-Source: APBJJlHgoq6fX5m1+Roj1PD+nUlgNcdctSDPa98kXCzajWjYbfEt4nefdrt/24V9AOah7Hkvx+a3A+kIbPjrz61F6Iqg X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a25:ab90:0:b0:bc4:a660:528f with SMTP id v16-20020a25ab90000000b00bc4a660528fmr39975ybi.5.1688683866544; Thu, 06 Jul 2023 15:51:06 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:36 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-9-axelrasmussen@google.com> Subject: [PATCH v3 8/8] selftests/mm: add uffd unit test for UFFDIO_POISON From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , 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: 6EDF51C0018 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ojkrhwswub1qy4ncqdurrbx8ijddo8py X-HE-Tag: 1688683867-121304 X-HE-Meta: U2FsdGVkX19e9trafK3sSmrxnKntE9d+RZ/ndcphClkzdT8pOr/Lx+SDCp8Parw9RZNGsbLTf61blyic0Neq+bRoDevNYom9r9/ZDTY8GfcihF01xUk05XEy+SIECDQEejFguIG2vETJmbXqz5xgC791ibZiEG42A9NSk/xpYE8WVB0JxJxYaQa/0Nt9Mit8ur7vDq/Z5LhHbnxUB6F7KjX1p+Lo3DuOPG4TAVqNW6iuoHnkJ0ygCZ7ltoPYcRLYXe5rR/otK+GF7YO2py2oTu5+Hf7b2rRhx8ECv1F3RShjCVFv8c7M5cXiSA0JEwQNmW7/HV+WeH5GTmX+RNJR1kdAPp1ruNuWlky+vnWfwTEyKwcIp9C7YefGvp8qDvFCl3BL55PPueRqX7BiMK/JssA7m7pTIIKTZQVUci/mPR8zX+sojZ1wBqk/fz3C2/JiF+e0L1WqtCsOcVmIKEbmpZNQcQPOsyrk1jcOJIVuRXMo/Xs+wXNz4bkML9nHPjle1V0BMjB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rlHyzpXV681g886iWhadOuRGdfN0g6tUYhOFuS8nxPU6iAFJdqjLnX3tiWx6R9HRUzKXM7B9L2crgMGv1Ph/W7XRREzF5t2f7LxeWd7LIx2gowY/ZJdIoe4MoT0RegJg4a4S0xg= 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. Acked-by: Peter Xu Signed-off-by: Axel Rasmussen --- 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)