From patchwork Fri Mar 4 05:16:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12768459 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 07C00C433EF for ; Fri, 4 Mar 2022 05:17:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F3FA8D0007; Fri, 4 Mar 2022 00:17:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A34F8D0001; Fri, 4 Mar 2022 00:17:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86BB78D0007; Fri, 4 Mar 2022 00:17:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0160.hostedemail.com [216.40.44.160]) by kanga.kvack.org (Postfix) with ESMTP id 796558D0001 for ; Fri, 4 Mar 2022 00:17:58 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 30E3D8249980 for ; Fri, 4 Mar 2022 05:17:58 +0000 (UTC) X-FDA: 79205547036.27.48D1014 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf04.hostedemail.com (Postfix) with ESMTP id A430E4000A for ; Fri, 4 Mar 2022 05:17:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646371077; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YEH//nu79wWwuy8WPoBNLdXG3sMM2ihkgiTBywoRJ9c=; b=FyWNvuRSHvyu8C7bcNKENLpqPCLBNCcRzuH1HEJOQwWEpKVADyrVArblByHb8679njJoyV UBao93ehEhQoJ55x/6yAo19Os1dCUb+C440AV9B47p1gZUz+xvK1u15tiLsyrUPD0tpNu+ faP0brv2KdxSuwhwhoKZocPIEWDpSE0= Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-633-LXf7z2OHNEeIf91hDCfVDA-1; Fri, 04 Mar 2022 00:17:56 -0500 X-MC-Unique: LXf7z2OHNEeIf91hDCfVDA-1 Received: by mail-pg1-f199.google.com with SMTP id u74-20020a63794d000000b00373efe2ac5aso3905851pgc.14 for ; Thu, 03 Mar 2022 21:17:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YEH//nu79wWwuy8WPoBNLdXG3sMM2ihkgiTBywoRJ9c=; b=EV3A7+O4C12Jlh6cZhoNmeD2XkmY/u8oY/pzj1+fmImfXYsSGevd5UUEtH9Sj/Mwz8 ExgC3kxGyGrZpPv3JCIHcUSDM8wQENeHmE3RMD8Uc74htCwPntBqZeDvhX73Ug5kPkbW XWe8xKjMVkEpzrdcis1tP95xEX8szgLDEbF2mmObi9ET6atJ1p834nfpoAXQNDWLtGyw XVhDnZrfnZCpd8/sH+vFlolmvXvdqizC3nprjUSMJHaYE0IH2HVRE7w4PIbcL9Q/tEQp JsYRcW6ZjmBwo48d8MCUNy+3+Ss4A4zAaxzQVNyYl6zZTjowrMf1FhsrKtoK4UP2M2c0 HhdQ== X-Gm-Message-State: AOAM531P3icKZ6sNa8kAxm9ImuPLXxWBo4zRToDgoOmAQNadsdryC+IN /JhLBOmGpERfEkvh3+M2qqyZ9Tt7YFepVaejVmO/zh4B9ZXjDGUaAKfwRxAY18yDL5HdVBnuEkK UMZhoBuD/wF0xGNUosl6Ib7b4xe4XH4TNBnZrxdg1z6f05Gzhk1GIQ9wX7hDI X-Received: by 2002:a17:90a:5d93:b0:1bc:4f9c:8eed with SMTP id t19-20020a17090a5d9300b001bc4f9c8eedmr8907327pji.180.1646371074867; Thu, 03 Mar 2022 21:17:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJwXiEiEBTk5qw5SPMWSMqnxEw34BDQjWY2sS9WIU+2vrSP3R9/ofOCk8lF82RBcX9U1hDyWIw== X-Received: by 2002:a17:90a:5d93:b0:1bc:4f9c:8eed with SMTP id t19-20020a17090a5d9300b001bc4f9c8eedmr8907294pji.180.1646371074503; Thu, 03 Mar 2022 21:17:54 -0800 (PST) Received: from localhost.localdomain ([94.177.118.59]) by smtp.gmail.com with ESMTPSA id p16-20020a056a000b5000b004f669806cd9sm4323865pfo.87.2022.03.03.21.17.47 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 03 Mar 2022 21:17:54 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Nadav Amit , Hugh Dickins , David Hildenbrand , Axel Rasmussen , Matthew Wilcox , Alistair Popple , Mike Rapoport , Andrew Morton , Jerome Glisse , Mike Kravetz , "Kirill A . Shutemov" , Andrea Arcangeli Subject: [PATCH v7 04/23] mm/uffd: PTE_MARKER_UFFD_WP Date: Fri, 4 Mar 2022 13:16:49 +0800 Message-Id: <20220304051708.86193-5-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220304051708.86193-1-peterx@redhat.com> References: <20220304051708.86193-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A430E4000A X-Stat-Signature: y7bjczmqe5jwg377gsdifrf7kmgeftju Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FyWNvuRS; spf=none (imf04.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-HE-Tag: 1646371077-907670 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 patch introduces the 1st user of pte marker: the uffd-wp marker. When the pte marker is installed with the uffd-wp bit set, it means this pte was wr-protected by uffd. We will use this special pte to arm the ptes that got either unmapped or swapped out for a file-backed region that was previously wr-protected. This special pte could trigger a page fault just like swap entries. This idea is greatly inspired by Hugh and Andrea in the discussion, which is referenced in the links below. Some helpers are introduced to detect whether a swap pte is uffd wr-protected. After the pte marker introduced, one swap pte can be wr-protected in two forms: either it is a normal swap pte and it has _PAGE_SWP_UFFD_WP set, or it's a pte marker that has PTE_MARKER_UFFD_WP set. Link: https://lore.kernel.org/lkml/20201126222359.8120-1-peterx@redhat.com/ Link: https://lore.kernel.org/lkml/20201130230603.46187-1-peterx@redhat.com/ Suggested-by: Andrea Arcangeli Suggested-by: Hugh Dickins Signed-off-by: Peter Xu --- include/linux/swapops.h | 3 ++- include/linux/userfaultfd_k.h | 43 +++++++++++++++++++++++++++++++++++ mm/Kconfig | 9 ++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/include/linux/swapops.h b/include/linux/swapops.h index 5103d2a4ae38..2cec3ef355a7 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -249,7 +249,8 @@ static inline int is_writable_migration_entry(swp_entry_t entry) typedef unsigned long pte_marker; -#define PTE_MARKER_MASK (0) +#define PTE_MARKER_UFFD_WP BIT(0) +#define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP) #ifdef CONFIG_PTE_MARKER diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 33cea484d1ad..bd09c3c89b59 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -15,6 +15,8 @@ #include #include +#include +#include #include /* The set of all possible UFFD-related VM flags. */ @@ -236,4 +238,45 @@ static inline void userfaultfd_unmap_complete(struct mm_struct *mm, #endif /* CONFIG_USERFAULTFD */ +static inline bool pte_marker_entry_uffd_wp(swp_entry_t entry) +{ + return is_pte_marker_entry(entry) && + (pte_marker_get(entry) & PTE_MARKER_UFFD_WP); +} + +static inline bool pte_marker_uffd_wp(pte_t pte) +{ +#ifdef CONFIG_PTE_MARKER_UFFD_WP + swp_entry_t entry; + + if (!is_swap_pte(pte)) + return false; + + entry = pte_to_swp_entry(pte); + + return pte_marker_entry_uffd_wp(entry); +#else + return false; +#endif +} + +/* + * Returns true if this is a swap pte and was uffd-wp wr-protected in either + * forms (pte marker or a normal swap pte), false otherwise. + */ +static inline bool pte_swp_uffd_wp_any(pte_t pte) +{ +#ifdef CONFIG_PTE_MARKER_UFFD_WP + if (!is_swap_pte(pte)) + return false; + + if (pte_swp_uffd_wp(pte)) + return true; + + if (pte_marker_uffd_wp(pte)) + return true; +#endif + return false; +} + #endif /* _LINUX_USERFAULTFD_K_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 25bcbb89f8e5..a80ea8721885 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -907,6 +907,15 @@ config PTE_MARKER help Allows to create marker PTEs for file-backed memory. +config PTE_MARKER_UFFD_WP + bool "Marker PTEs support for userfaultfd write protection" + depends on PTE_MARKER && HAVE_ARCH_USERFAULTFD_WP + + help + Allows to create marker PTEs for userfaultfd write protection + purposes. It is required to enable userfaultfd write protection on + file-backed memory types like shmem and hugetlbfs. + source "mm/damon/Kconfig" endmenu