From patchwork Fri Mar 4 05:17:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12768471 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 BECEDC433EF for ; Fri, 4 Mar 2022 05:19:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D4398D0009; Fri, 4 Mar 2022 00:19:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 583938D0001; Fri, 4 Mar 2022 00:19:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 473B18D0009; Fri, 4 Mar 2022 00:19:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0146.hostedemail.com [216.40.44.146]) by kanga.kvack.org (Postfix) with ESMTP id 3ADF98D0001 for ; Fri, 4 Mar 2022 00:19:35 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id F15589790B for ; Fri, 4 Mar 2022 05:19:34 +0000 (UTC) X-FDA: 79205551068.19.9F89CC4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 752ED80014 for ; Fri, 4 Mar 2022 05:19:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646371174; 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=8c/lNa0oM80rkM4kbMMwF9soU2NQfSaZ5E7MuZnV//g=; b=BpIU8IX4aZq2mQ4OpxNSOS1347l7NI45mKR2ht5ruCz0hg/adnD3vpucH86bbSyF/1idad LLZIwcHmAcfRvqgM9Aqx185q6Xvoq3/xMLViJIRy4eTuihEtPcjEf5Kkl4qo052u9utZts 4MgxwpSc2FphMGYe37J/f2aWzjMEdBo= Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-102-gL3lvmDbOqmwmze5tyvTmg-1; Fri, 04 Mar 2022 00:19:32 -0500 X-MC-Unique: gL3lvmDbOqmwmze5tyvTmg-1 Received: by mail-pj1-f71.google.com with SMTP id y1-20020a17090a644100b001bc901aba0dso3369632pjm.8 for ; Thu, 03 Mar 2022 21:19:32 -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=8c/lNa0oM80rkM4kbMMwF9soU2NQfSaZ5E7MuZnV//g=; b=PFJGUHy7t1/A6X6eWKFFsWqE3GgwytepafXWP3fURs2t27iAhC6yB03EDjAOcpHVu+ DTBpnEMXcKa6veleBHpmINUynnhAF4NNeUTTAENelKHggiUt+kDodKHgeIFf5dYm2QQ7 IxRbBDfWagxlUt/ggo564MLIVcdc9BEasyu0gZ/8dAU+fD+MNjVGKmf3PHgRYvZJI44Z oVq5Gk8RKuxhnnPZGxYvxgHUG0zDyySrwt0Z7g3cB1JyqzilfJR3iXKrLqANlOdyebk/ L8k4+AFiS1kis3d4cbolTfiJh5wzP1cTKVktBAnmMRR1kPUZVbUuyYa+i6OvyGO/4w4R 2ofw== X-Gm-Message-State: AOAM531hM+m+DNWH33d+sv5MCpwFKVsFgFLYNVQj0kpPsN80koJL4NUY 45DzfWMjmUG7M61t5FTvwSJwWZmh7og89VK0YCyaONeWigtb6IFQpuDRIl3sDkvmK+9NnCeNqRU ZBB5/guJMcaMKDPrxNjlH/U9WF//Er1ofksv2ULZqn9ZZtL41EhiKGDHgv9Wa X-Received: by 2002:a17:902:f145:b0:151:a441:433a with SMTP id d5-20020a170902f14500b00151a441433amr8804590plb.44.1646371171422; Thu, 03 Mar 2022 21:19:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFfQF8+il2cx3Q+YaVVz5krns+0z+Q5hZNGFwZcBniVedYFkNfGyU++c+42HLp3JPbzL24yQ== X-Received: by 2002:a17:902:f145:b0:151:a441:433a with SMTP id d5-20020a170902f14500b00151a441433amr8804562plb.44.1646371171051; Thu, 03 Mar 2022 21:19:31 -0800 (PST) Received: from localhost.localdomain ([94.177.118.59]) by smtp.gmail.com with ESMTPSA id p16-20020a056a000b5000b004f669806cd9sm4323865pfo.87.2022.03.03.21.19.23 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 03 Mar 2022 21:19:30 -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 16/23] mm/hugetlb: Allow uffd wr-protect none ptes Date: Fri, 4 Mar 2022 13:17:01 +0800 Message-Id: <20220304051708.86193-17-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: rspam05 X-Rspamd-Queue-Id: 752ED80014 X-Rspam-User: Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BpIU8IX4; spf=none (imf02.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: qmw6pq758j6eqqtahudz97zcnndixqer X-HE-Tag: 1646371174-52984 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 afd3d93cfe9a..1a20be29ac3a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6169,7 +6169,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; @@ -6189,13 +6189,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; @@ -6220,12 +6226,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(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)); @@ -6239,6 +6253,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); }