From patchwork Mon Nov 15 08:02:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12618877 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5606FC433EF for ; Mon, 15 Nov 2021 08:02:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 078E561B97 for ; Mon, 15 Nov 2021 08:02:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 078E561B97 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 992F86B0093; Mon, 15 Nov 2021 03:02:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 943A36B0095; Mon, 15 Nov 2021 03:02:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80BC06B0096; Mon, 15 Nov 2021 03:02:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0190.hostedemail.com [216.40.44.190]) by kanga.kvack.org (Postfix) with ESMTP id 6F8EF6B0093 for ; Mon, 15 Nov 2021 03:02:46 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 318F082D90 for ; Mon, 15 Nov 2021 08:02:46 +0000 (UTC) X-FDA: 78810423132.04.D68CF34 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 4FC0210512E0 for ; Mon, 15 Nov 2021 08:02:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1636963365; 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=qkupakg3emmkCuPdLA0U47j3HM8dJoa5zeaf2nr8YrE=; b=Zet983ylOvpWbj+2+1Z4z7P6KzkVddp9+cGJDN6IDlhIP2J8XZLc/MpdPnz7l7mkwcIo0B RBBgDIKloVFknIQSiJyWl9kCEu0Z1TY5E2mrk8/9Ib8kLN/+FBhik62hPzbEQXRpwCy+8e AT3GB1qMDjSQR9ijD2308tM+nbxzdio= Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-580-HNKxxPbAP_SSiqUZTq6f5g-1; Mon, 15 Nov 2021 03:02:44 -0500 X-MC-Unique: HNKxxPbAP_SSiqUZTq6f5g-1 Received: by mail-pl1-f197.google.com with SMTP id l3-20020a170902f68300b00142892d0a86so5807273plg.13 for ; Mon, 15 Nov 2021 00:02:44 -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=qkupakg3emmkCuPdLA0U47j3HM8dJoa5zeaf2nr8YrE=; b=Q7FXW4b0OU4SBpqA9DlyfUp8wPzbq/bsciDt8mOstTNqqhUEsOsIBzMMvQK8cEwuAZ aFtS0dP7s/JQispzV8vmr4/YP/2aGK6Xd3Nps/IxEoJSS9ZC0aKPpnW2nNo+g50YWAe1 115alMOrRbfZM+CdfJFibE+55M8uKsa24efrhkIz7loJqqhzwhpCZFrD8xfEYnAgE8ko az6nZpwwQ7umr2u9xF19Qz/dL7YqsAStije/2aGFCUo9Fp/ueB6dIwSH9e2X23DQAJU2 BTFwPPVwHNMDgWgA0nbdlBjh1x+LyglWAHdeKhUQ8lO6JL8JWZvgg+h6WMp/WaTDb63L lywQ== X-Gm-Message-State: AOAM532ci1J733Ihwz9wqP1UJYpDVFM3y2G+MgSI2ahlktMk61UUcvTb 4Ky3D+nb6vn+JrNsZR9JToiy/Va5HzPOX7sG661Ru3Qrc6ZdagozHZ+mYvmwdhvlq0r/23ZvvS8 f4LiD6Xdk3MonP2mWE/RvswLeCOB5kzS1A303bQPstqoNyxohBXt/esp8Qifq X-Received: by 2002:aa7:8019:0:b0:44d:d761:6f79 with SMTP id j25-20020aa78019000000b0044dd7616f79mr31742121pfi.3.1636963362660; Mon, 15 Nov 2021 00:02:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJyv5uP4O5V4xtiTKpwFZqqVXpd4X2FyO5vCLmFS1r+O+rAVPgEZZOgFH+PNonWxXoKguHRPXQ== X-Received: by 2002:aa7:8019:0:b0:44d:d761:6f79 with SMTP id j25-20020aa78019000000b0044dd7616f79mr31742074pfi.3.1636963362297; Mon, 15 Nov 2021 00:02:42 -0800 (PST) Received: from localhost.localdomain ([94.177.118.89]) by smtp.gmail.com with ESMTPSA id 95sm11508978pjo.2.2021.11.15.00.02.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Nov 2021 00:02:41 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , peterx@redhat.com, Alistair Popple , Andrew Morton , Mike Kravetz , Mike Rapoport , Matthew Wilcox , Jerome Glisse , Axel Rasmussen , "Kirill A . Shutemov" , David Hildenbrand , Andrea Arcangeli , Hugh Dickins Subject: [PATCH v6 16/23] mm/hugetlb: Allow uffd wr-protect none ptes Date: Mon, 15 Nov 2021 16:02:28 +0800 Message-Id: <20211115080228.74982-1-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211115075522.73795-1-peterx@redhat.com> References: <20211115075522.73795-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 4FC0210512E0 X-Stat-Signature: uhqdabi57iuqwnhgf1coyouy6saw5j5n Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Zet983yl; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf13.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com X-HE-Tag: 1636963353-125835 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: Teach hugetlbfs code to wr-protect none ptes just in case the page cache existed for that pte. Meanwhile we also need to be able to recognize a uffd-wp marker pte and remove it for uffd_wp_resolve. Since at it, introduce a variable "psize" to replace all references to the huge page size fetcher. Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu --- mm/hugetlb.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index e8d01277af0f..bba2ede5f6dc 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6145,7 +6145,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, pte_t *ptep; pte_t pte; struct hstate *h = hstate_vma(vma); - unsigned long pages = 0; + unsigned long pages = 0, psize = huge_page_size(h); bool shared_pmd = false; struct mmu_notifier_range range; bool uffd_wp = cp_flags & MM_CP_UFFD_WP; @@ -6165,13 +6165,19 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, mmu_notifier_invalidate_range_start(&range); i_mmap_lock_write(vma->vm_file->f_mapping); - for (; address < end; address += huge_page_size(h)) { + for (; address < end; address += psize) { spinlock_t *ptl; - ptep = huge_pte_offset(mm, address, huge_page_size(h)); + ptep = huge_pte_offset(mm, address, psize); if (!ptep) continue; ptl = huge_pte_lock(h, mm, ptep); if (huge_pmd_unshare(mm, vma, &address, ptep)) { + /* + * When uffd-wp is enabled on the vma, unshare + * shouldn't happen at all. Warn about it if it + * happened due to some reason. + */ + WARN_ON_ONCE(uffd_wp || uffd_wp_resolve); pages++; spin_unlock(ptl); shared_pmd = true; @@ -6196,12 +6202,20 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, else if (uffd_wp_resolve) newpte = pte_swp_clear_uffd_wp(newpte); set_huge_swap_pte_at(mm, address, ptep, - newpte, huge_page_size(h)); + newpte, psize); pages++; } spin_unlock(ptl); continue; } + if (unlikely(is_pte_marker_uffd_wp(pte))) { + /* + * This is changing a non-present pte into a none pte, + * no need for huge_ptep_modify_prot_start/commit(). + */ + if (uffd_wp_resolve) + huge_pte_clear(mm, address, ptep, psize); + } if (!huge_pte_none(pte)) { pte_t old_pte; unsigned int shift = huge_page_shift(hstate_vma(vma)); @@ -6215,6 +6229,12 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, pte = huge_pte_clear_uffd_wp(pte); huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte); pages++; + } else { + /* None pte */ + if (unlikely(uffd_wp)) + /* Safe to modify directly (none->non-present). */ + set_huge_pte_at(mm, address, ptep, + make_pte_marker(PTE_MARKER_UFFD_WP)); } spin_unlock(ptl); }