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 X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A058DC433E0 for ; Fri, 15 Jan 2021 17:09:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4235F2333E for ; Fri, 15 Jan 2021 17:09:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4235F2333E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4490E8D01A6; Fri, 15 Jan 2021 12:09:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3FB808D01A4; Fri, 15 Jan 2021 12:09:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 272EA8D01A6; Fri, 15 Jan 2021 12:09:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0038.hostedemail.com [216.40.44.38]) by kanga.kvack.org (Postfix) with ESMTP id 12DF28D01A4 for ; Fri, 15 Jan 2021 12:09:41 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id CE3F5381E for ; Fri, 15 Jan 2021 17:09:40 +0000 (UTC) X-FDA: 77708646120.03.night79_611332927531 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id A96D328A4EA for ; Fri, 15 Jan 2021 17:09:40 +0000 (UTC) X-HE-Tag: night79_611332927531 X-Filterd-Recvd-Size: 12906 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf25.hostedemail.com (Postfix) with ESMTP for ; Fri, 15 Jan 2021 17:09:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610730579; 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=a5KLb2lATkQ/XNiv9F7KPoJriimXjMJy8UfLukuBk5Y=; b=MjrziuEcfTD5+mwPzX9AvSQeyVvuFntoKzPSHqsbf1BpCYGChOQmeskHKBys62MJFR65nZ eC/OxS85sYZxlHK5Ddf9e5TzKCmtBKJ33mY54YdNmjrEHW9O4A2EjiCU0szfSrWkFhp7Rv yabUMlmx5AktBUqY1WVZ5Njc9CcRL7Q= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-579-S8FE7ntOMpiS6sjb3Od3Hw-1; Fri, 15 Jan 2021 12:09:36 -0500 X-MC-Unique: S8FE7ntOMpiS6sjb3Od3Hw-1 Received: by mail-qv1-f72.google.com with SMTP id j24so8265757qvg.8 for ; Fri, 15 Jan 2021 09:09:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a5KLb2lATkQ/XNiv9F7KPoJriimXjMJy8UfLukuBk5Y=; b=tQIK9+KoJ0EwrfzYrD3qeaahM6Hh2UXJYTbqzIIkHoN/W6Pg7mS7qayTzYSvclvAES 5gNE76EuLivsPOP5d0MKHKS+ycikHsKuNF+zwOyVFxZI0LJt6t099koi+dDXfJZnAacr NbBu8MHnHxZ01fiAoWKyw1deXXw8W4UeesvZgW4KmqStq0UEA01wOIl7IR64Ia43y5Zm 2Iv3AglY/pQEkqUU6ThDQgakqWXK/aA1cAmSqI5dfE9ZQd8qcNFQOIeSJibXvRyzTcfx a/e3F3cs1C2bacXTH0Lrb0DRgFszVuAFlTM0MO2A5qNtpded9tYMlMZbLOyCiFkkGSnq pxyA== X-Gm-Message-State: AOAM531l3ZSEDfxIi5sA64fUYtpeAGkUhp32Y97ByFVkCmaEhE4yGp/A Mh81fhxt0xyvk/8nxy7nIcXql2tHUz6iodxkmmufacr+V/dET8qhKJxDVQZBq1nUNA5k+vVXR1+ ubi5HZUvtme0= X-Received: by 2002:ac8:396d:: with SMTP id t42mr12482420qtb.273.1610730575865; Fri, 15 Jan 2021 09:09:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJwx+fHE05WbMDp5unvCgzYCvNrqmnR+hBQDHIuSFam5gLX1Ken6kJ60Sbq11jUg42je7VpxZw== X-Received: by 2002:ac8:396d:: with SMTP id t42mr12482366qtb.273.1610730575395; Fri, 15 Jan 2021 09:09:35 -0800 (PST) Received: from localhost.localdomain ([142.126.83.202]) by smtp.gmail.com with ESMTPSA id d123sm5187840qke.95.2021.01.15.09.09.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jan 2021 09:09:34 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Rapoport , Mike Kravetz , peterx@redhat.com, Jerome Glisse , "Kirill A . Shutemov" , Hugh Dickins , Axel Rasmussen , Matthew Wilcox , Andrew Morton , Andrea Arcangeli , Nadav Amit Subject: [PATCH RFC 13/30] shmem/userfaultfd: Persist uffd-wp bit across zapping for file-backed Date: Fri, 15 Jan 2021 12:08:50 -0500 Message-Id: <20210115170907.24498-14-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210115170907.24498-1-peterx@redhat.com> References: <20210115170907.24498-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="US-ASCII" 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: File-backed memory is prone to being unmapped at any time. It means all information in the pte will be dropped, including the uffd-wp flag. Since the uffd-wp info cannot be stored in page cache or swap cache, pers= ist this wr-protect information by installing the special uffd-wp marker pte = when we're going to unmap a uffd wr-protected pte. When the pte is accessed a= gain, we will know it's previously wr-protected by recognizing the special pte. Meanwhile add a new flag ZAP_FLAG_DROP_FILE_UFFD_WP when we don't want to persist such an information. For example, when destroying the whole vma,= or punching a hole in a shmem file. For the latter, we can only drop the uf= fd-wp bit when holding the page lock. It means the unmap_mapping_range() in shmem_fallocate() still reuqires to zap without ZAP_FLAG_DROP_FILE_UFFD_W= P because that's still racy with the page faults. Signed-off-by: Peter Xu --- include/linux/mm.h | 11 ++++++++++ include/linux/mm_inline.h | 43 +++++++++++++++++++++++++++++++++++++++ mm/memory.c | 42 +++++++++++++++++++++++++++++++++++++- mm/rmap.c | 8 ++++++++ mm/truncate.c | 8 +++++++- 5 files changed, 110 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 57bb3d680844..e4aba745be62 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1634,6 +1634,8 @@ extern void user_shm_unlock(size_t, struct user_str= uct *); #define ZAP_FLAG_CHECK_MAPPING BIT(0) /* Whether to skip zapping swap entries */ #define ZAP_FLAG_SKIP_SWAP BIT(1) +/* Whether to completely drop uffd-wp entries for file-backed memory */ +#define ZAP_FLAG_DROP_FILE_UFFD_WP BIT(2) =20 /* * Parameter block passed down to zap_pte_range in exceptional cases. @@ -1666,6 +1668,15 @@ zap_skip_swap(struct zap_details *details) return details->zap_flags & ZAP_FLAG_SKIP_SWAP; } =20 +static inline bool +zap_drop_file_uffd_wp(struct zap_details *details) +{ + if (!details) + return false; + + return details->zap_flags & ZAP_FLAG_DROP_FILE_UFFD_WP; +} + struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long ad= dr, pte_t pte); struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned lon= g addr, diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 8fc71e9d7bb0..da4c710859e6 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -4,6 +4,8 @@ =20 #include #include +#include +#include =20 /** * page_is_file_lru - should the page be on a file LRU or anon LRU? @@ -125,4 +127,45 @@ static __always_inline enum lru_list page_lru(struct= page *page) } return lru; } + +/* + * If this pte is wr-protected by uffd-wp in any form, arm the special p= te to + * replace a none pte. NOTE! This should only be called when *pte is a= lready + * cleared so we will never accidentally replace something valuable. Me= anwhile + * none pte also means we are not demoting the pte so if tlb flushed the= n we + * don't need to do it again; otherwise if tlb flush is postponed then i= t's + * even better. + * + * Must be called with pgtable lock held. + */ +static inline void +pte_install_uffd_wp_if_needed(struct vm_area_struct *vma, unsigned long = addr, + pte_t *pte, pte_t pteval) +{ +#ifdef CONFIG_USERFAULTFD + bool arm_uffd_pte =3D false; + + /* The current status of the pte should be "cleared" before calling */ + WARN_ON_ONCE(!pte_none(*pte)); + + if (vma_is_anonymous(vma)) + return; + + /* A uffd-wp wr-protected normal pte */ + if (unlikely(pte_present(pteval) && pte_uffd_wp(pteval))) + arm_uffd_pte =3D true; + + /* + * A uffd-wp wr-protected swap pte. Note: this should even work for + * pte_swp_uffd_wp_special() too. + */ + if (unlikely(is_swap_pte(pteval) && pte_swp_uffd_wp(pteval))) + arm_uffd_pte =3D true; + + if (unlikely(arm_uffd_pte)) + set_pte_at(vma->vm_mm, addr, pte, + pte_swp_mkuffd_wp_special(vma)); +#endif +} + #endif diff --git a/mm/memory.c b/mm/memory.c index afe09fccdee1..f87b5a8a098e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -73,6 +73,7 @@ #include #include #include +#include =20 #include =20 @@ -1194,6 +1195,21 @@ copy_page_range(struct vm_area_struct *dst_vma, st= ruct vm_area_struct *src_vma) return ret; } =20 +/* + * This function makes sure that we'll replace the none pte with an uffd= -wp + * swap special pte marker when necessary. Must be with the pgtable lock= held. + */ +static inline void +zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, + unsigned long addr, pte_t *pte, + struct zap_details *details, pte_t pteval) +{ + if (zap_drop_file_uffd_wp(details)) + return; + + pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end, @@ -1231,6 +1247,8 @@ static unsigned long zap_pte_range(struct mmu_gathe= r *tlb, ptent =3D ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); tlb_remove_tlb_entry(tlb, pte, addr); + zap_install_uffd_wp_if_needed(vma, addr, pte, details, + ptent); if (unlikely(!page)) continue; =20 @@ -1255,6 +1273,22 @@ static unsigned long zap_pte_range(struct mmu_gath= er *tlb, continue; } =20 + /* + * If this is a special uffd-wp marker pte... Drop it only if + * enforced to do so. + */ + if (unlikely(is_swap_special_pte(ptent))) { + WARN_ON_ONCE(!pte_swp_uffd_wp_special(ptent)); + /* + * If this is a common unmap of ptes, keep this as is. + * Drop it only if this is a whole-vma destruction. + */ + if (zap_drop_file_uffd_wp(details)) + ptep_get_and_clear_full(mm, addr, pte, + tlb->fullmm); + continue; + } + entry =3D pte_to_swp_entry(ptent); if (is_device_private_entry(entry)) { struct page *page =3D device_private_entry_to_page(entry); @@ -1265,6 +1299,8 @@ static unsigned long zap_pte_range(struct mmu_gathe= r *tlb, rss[mm_counter(page)]--; page_remove_rmap(page, false); put_page(page); + zap_install_uffd_wp_if_needed(vma, addr, pte, details, + ptent); continue; } =20 @@ -1282,6 +1318,7 @@ static unsigned long zap_pte_range(struct mmu_gathe= r *tlb, if (unlikely(!free_swap_and_cache(entry))) print_bad_pte(vma, addr, ptent, NULL); pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); + zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); } while (pte++, addr +=3D PAGE_SIZE, addr !=3D end); =20 add_mm_rss_vec(mm, rss); @@ -1481,12 +1518,15 @@ void unmap_vmas(struct mmu_gather *tlb, unsigned long end_addr) { struct mmu_notifier_range range; + struct zap_details details =3D { + .zap_flags =3D ZAP_FLAG_DROP_FILE_UFFD_WP, + }; =20 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm, start_addr, end_addr); mmu_notifier_invalidate_range_start(&range); for ( ; vma && vma->vm_start < end_addr; vma =3D vma->vm_next) - unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); + unmap_single_vma(tlb, vma, start_addr, end_addr, &details); mmu_notifier_invalidate_range_end(&range); } =20 diff --git a/mm/rmap.c b/mm/rmap.c index 31b29321adfe..f6cc0b9b1963 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -72,6 +72,7 @@ #include #include #include +#include =20 #include =20 @@ -1560,6 +1561,13 @@ static bool try_to_unmap_one(struct page *page, st= ruct vm_area_struct *vma, pteval =3D ptep_clear_flush(vma, address, pvmw.pte); } =20 + /* + * Now the pte is cleared. If this is uffd-wp armed pte, we + * may want to replace a none pte with a marker pte if it's + * file-backed, so we don't lose the tracking information. + */ + pte_install_uffd_wp_if_needed(vma, address, pvmw.pte, pteval); + /* Move the dirty bit to the page. Now the pte is gone. */ if (pte_dirty(pteval)) set_page_dirty(page); diff --git a/mm/truncate.c b/mm/truncate.c index dac66749e400..35df3b1d301e 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -179,7 +179,13 @@ truncate_cleanup_page(struct address_space *mapping,= struct page *page) if (page_mapped(page)) { unsigned int nr =3D thp_nr_pages(page); unmap_mapping_pages(mapping, page->index, nr, - ZAP_FLAG_CHECK_MAPPING); + ZAP_FLAG_CHECK_MAPPING | + /* + * Now it's safe to drop uffd-wp because + * we're with page lock, and the page is + * being truncated. + */ + ZAP_FLAG_DROP_FILE_UFFD_WP); } =20 if (page_has_private(page)) --=20 2.26.2