From mboxrd@z Thu Jan 1 00:00:00 1970 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 7BB59C433EF for ; Tue, 5 Apr 2022 01:49:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8ABD46B0075; Mon, 4 Apr 2022 21:48:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 859956B0078; Mon, 4 Apr 2022 21:48:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FA996B007B; Mon, 4 Apr 2022 21:48:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0019.hostedemail.com [216.40.44.19]) by kanga.kvack.org (Postfix) with ESMTP id 62D506B0075 for ; Mon, 4 Apr 2022 21:48:54 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 232A98249980 for ; Tue, 5 Apr 2022 01:48:44 +0000 (UTC) X-FDA: 79321141326.20.DD264D8 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 98BED40009 for ; Tue, 5 Apr 2022 01:48:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649123323; 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=U8n+4P5JjRaDUHKZYY8oH8Iy0i6vYl4G2VE7hAdGm3Q=; b=gNTy7bOb/elG58f1Is8sENvDQEyyaX3Q8vxBYP+bHo/SKecV3NLYcnVba6fNhrGiaABgcV 4+yvhhD98OEwfZm10d0RraQGzJ3wpn38xwRmVClC6AAUE4AkDAsEjRqA5quVWze/AAlCe3 0S8A0Qrz62jioAmk3YW6AuguDlL5DJc= Received: from mail-io1-f69.google.com (mail-io1-f69.google.com [209.85.166.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-423-FVSJfvcZNrajefsPreim0Q-1; Mon, 04 Apr 2022 21:48:42 -0400 X-MC-Unique: FVSJfvcZNrajefsPreim0Q-1 Received: by mail-io1-f69.google.com with SMTP id a9-20020a5d89c9000000b0064cb68a9ba6so7441102iot.11 for ; Mon, 04 Apr 2022 18:48:42 -0700 (PDT) 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=U8n+4P5JjRaDUHKZYY8oH8Iy0i6vYl4G2VE7hAdGm3Q=; b=wEe7/dXN4UeMk1rBgDiCqKrbwnbQfiu6jc92yMMEzzCPCQR+SAnlx+6o1c8skLHwrS CwtosCODlwZNh5agNELkoiuSWOD/65iQuU7yS86+FLCHfdaV69GJ0qsWoic7yyDpwfXQ 3KX26B65ADZz/km69iWX7b1pbRBO267K1qECVp4GvI6AeImMd61EwCz6/V7QY7X+iB4Q Y4dZ4WnysTliM0ywFKYnZRaqrgaFJ4J1Q2ACM9wLJ9Sglw5kFU+5PitfWqXuO5TQPUB3 3oEMFlF6n5k+/ItmS3PF3cmtuu+4dgztXLu2Kye4CSLS9qB0k+80sU8YHmx3vJvTIRf+ xNoA== X-Gm-Message-State: AOAM530CUef32OgDn9XMOOVRCa4AgHbUlXERV+LPHJTHjMUqGlSyQXYm ciyl05FOHP2+jYSChXgl70NG+lAm+tjA9a2nyDQ/rep1ED4Q9sSCCGjelKTZg8oYp3R6Sad2ohV YO5FlYEB0VWQ= X-Received: by 2002:a05:6602:490:b0:638:c8ed:1e38 with SMTP id y16-20020a056602049000b00638c8ed1e38mr589228iov.202.1649123321664; Mon, 04 Apr 2022 18:48:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBgvwBVvGE1Vqojm5+pnVWt3lmiOe9Zr6Q3Q9kFVvDL2PkJIvmVaEpRNWmsAlmIdguH9c/+A== X-Received: by 2002:a05:6602:490:b0:638:c8ed:1e38 with SMTP id y16-20020a056602049000b00638c8ed1e38mr589217iov.202.1649123321422; Mon, 04 Apr 2022 18:48:41 -0700 (PDT) Received: from localhost.localdomain (cpec09435e3e0ee-cmc09435e3e0ec.cpe.net.cable.rogers.com. [99.241.198.116]) by smtp.gmail.com with ESMTPSA id s5-20020a056602168500b0064c82210ce4sm7650607iow.13.2022.04.04.18.48.40 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 04 Apr 2022 18:48:41 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Nadav Amit , Matthew Wilcox , Mike Rapoport , David Hildenbrand , Hugh Dickins , Jerome Glisse , "Kirill A . Shutemov" , Andrea Arcangeli , Andrew Morton , Axel Rasmussen , Alistair Popple , peterx@redhat.com Subject: [PATCH v8 04/23] mm/uffd: PTE_MARKER_UFFD_WP Date: Mon, 4 Apr 2022 21:48:38 -0400 Message-Id: <20220405014838.14131-1-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220405014646.13522-1-peterx@redhat.com> References: <20220405014646.13522-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="US-ASCII"; x-default=true Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gNTy7bOb; spf=none (imf11.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-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 98BED40009 X-Stat-Signature: rg3gy73ureq5u5zj7siotxga3opnjd46 X-HE-Tag: 1649123323-764163 Content-Transfer-Encoding: quoted-printable 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. T= his 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-prote= cted. 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.co= m/ Link: https://lore.kernel.org/lkml/20201130230603.46187-1-peterx@redhat.c= om/ 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 7a00627845f0..fffbba0036f6 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -276,7 +276,8 @@ static inline int is_readable_migration_entry(swp_ent= ry_t entry) =20 typedef unsigned long pte_marker; =20 -#define PTE_MARKER_MASK (0) +#define PTE_MARKER_UFFD_WP BIT(0) +#define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP) =20 #ifdef CONFIG_PTE_MARKER =20 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 @@ =20 #include #include +#include +#include #include =20 /* The set of all possible UFFD-related VM flags. */ @@ -236,4 +238,45 @@ static inline void userfaultfd_unmap_complete(struct= mm_struct *mm, =20 #endif /* CONFIG_USERFAULTFD */ =20 +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 =3D 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 ei= ther + * 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 a1688b9314b2..6e7c2d59fa96 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -915,6 +915,15 @@ config PTE_MARKER help Allows to create marker PTEs for file-backed memory. =20 +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" =20 endmenu --=20 2.32.0