Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp21296571rwd; Thu, 29 Jun 2023 14:01:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7syzqbo2xcV+LtU5GYvtScIvrP1HfOn8+e7REEaogZCqqNSOpjuE+vkmcMgoYUe5REuu7M X-Received: by 2002:a05:6a20:3d8e:b0:127:1d60:31d with SMTP id s14-20020a056a203d8e00b001271d60031dmr1050956pzi.20.1688072480353; Thu, 29 Jun 2023 14:01:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688072480; cv=none; d=google.com; s=arc-20160816; b=dPGKusrQap/HuUdJ7eErPkS9DM0mo2r8TYG4sJUZnEBBmXBKOKXVQqDwPF9CmAgjtW 94Lzr9W4wfNunX0hIhS8BCTn5a8LQpliW82tJ9NwcZrhZ7Hfpt/M6uWHvo22JtxnG7mn 0+QJqmHp2raVEs7zn23jsZo+vwVE34vmvRkmTuVVQonQmqWPDxV3wgl34pmLlWO5odEy p/aPGpE4X6KLPkmTqoTsJ8joIWVdcGS/HSxOuXT+dz7gPSWzlS1JF8RML3T0z7i8gWm0 V3ICKKRjhLHIjpHruahrLpUDJ7Ndgg5E2E7IfxmfOZlZ85SjpEToCPXOVUdjlyrRytzZ Tk2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=oot+BFijmfKZ0KO/nJIrIU5HcDe1IwfMU2NYWHhWcUA=; fh=eRaYFbc68TLHCmej1r/Q9kziindMZl6i1kh9xMHvl2k=; b=tyY209OoXWTCd2XI0PHz1la3Uc0E/n23t0fOwxw5PP9TDoFfhPxscC5DJCqmfvkG2P 8L+OBL345vK8ogpqgjOvhk6uUP4vF4cmQHrj35p+6Jz/VImUwC04wrETez/JRbEiXLgt DcGMmKj7NaVu40Y2PEqYBML3aw4X3Gg++5C4070Uw7M5K0O8/ra/CId4jb6fSqmkw9VO vXDkpHw3DqOHc3Sv08Ho5+drXWvtoL8YOEyC13yP0i+gWhWUOiekD9u/KEyI/15F6buI lDmH6/Q1Xap8vEU+dXD+u+N8ewMTIOYWBGoweTCtfc3TtfC2e4uE1Agp4E6i00zv0cmg hNFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=dv8waobW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bk13-20020a056a02028d00b0053fbde21369si11303275pgb.503.2023.06.29.14.01.04; Thu, 29 Jun 2023 14:01:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=dv8waobW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232047AbjF2UvD (ORCPT + 99 others); Thu, 29 Jun 2023 16:51:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231766AbjF2Uuz (ORCPT ); Thu, 29 Jun 2023 16:50:55 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1C553595 for ; Thu, 29 Jun 2023 13:50:52 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c01e1c0648aso879394276.1 for ; Thu, 29 Jun 2023 13:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688071852; x=1690663852; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=oot+BFijmfKZ0KO/nJIrIU5HcDe1IwfMU2NYWHhWcUA=; b=dv8waobWVaNSUsOT47GOsDx8vV/iRkGODf8zThZKK4JKq0ZSnTrMWy7R3IAiX1fM4t tegdL5nlYEw74WxJbU4ciZTSnZwOVQYCywDP/6JAHDEF3YVNRNQoSLNsxO+X0jE8+WLZ 4ib0DDeM9eP9xJoybwRbj9HMMqb5rE/qTj6br4mOJWTUvJsbEgikpTHZ4BkIpODJP7uS FeOaZbb/LsG8QdVorLcYBayDjEa0cgjzLqqSuGqP5etuMwIPhztE5czIvy2CmabhCZfJ vnsHoy7ITFctcEgQKTEC1tg/xVWWYMTZvhXULy5xYz0ZzCxTiOdKZ00s3BGxSv+HUqqN HmAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688071852; x=1690663852; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=oot+BFijmfKZ0KO/nJIrIU5HcDe1IwfMU2NYWHhWcUA=; b=gmvXkJJcdkiNMYjIs4DSXTkOWPOEwBxaQxZMp5luhWtYR2Ou555i9xIc1scqt66MB0 iyH+OZM7GjRtSAHTDgg+o1B12tfsUnQQs8d9CXF0X4kTLLrXoyYDBNrjNgUr56uAv0fr dMWIC+Q0hEUWvMG+bK2O8Rku2RLLWPNfHRtPIYXRYrsmcavjoThUZ//23738QDnFnglY FieFa91JqNy3mwbe+49HG7J2QnPuTYi0tIWckiGoqAE2uRE1JcDT+JdsaJY0kwXMpghb 3bXrJ2Il2yGtf2ETB1Kw+ifH9S54E7MnvrDaaE75x++n8MwTmXq3t1ImYE2WRNX64ldh PXhQ== X-Gm-Message-State: ABy/qLYP43xkFcKA+xtjJm8bdHguB89agurh3g1dJgZmPPFX36YOG9cJ TZTTOfsmHMtEHQ1b8A3518ZYZawGYg7VCSAFJcZQ X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:e20f:5917:3efa:d4bb]) (user=axelrasmussen job=sendgmr) by 2002:a25:f621:0:b0:bc7:f6af:8cff with SMTP id t33-20020a25f621000000b00bc7f6af8cffmr5650ybd.2.1688071852014; Thu, 29 Jun 2023 13:50:52 -0700 (PDT) Date: Thu, 29 Jun 2023 13:50:37 -0700 In-Reply-To: <20230629205040.665834-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230629205040.665834-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230629205040.665834-3-axelrasmussen@google.com> Subject: [PATCH v2 3/6] mm: userfaultfd: support UFFDIO_POISON for hugetlbfs From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Christian Brauner , David Hildenbrand , Huang Ying , Hugh Dickins , James Houghton , Jiaqi Yan , Jonathan Corbet , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Shuah Khan , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; } -- 2.41.0.255.g8b1d071c50-goog