From patchwork Thu May 27 20:23:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12285299 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.5 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,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 5F95FC4708D for ; Thu, 27 May 2021 20:23:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0CB23613D4 for ; Thu, 27 May 2021 20:23:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0CB23613D4 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 64AF26B0085; Thu, 27 May 2021 16:23:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 606FD6B0087; Thu, 27 May 2021 16:23:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0498A6B0088; Thu, 27 May 2021 16:23:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0098.hostedemail.com [216.40.44.98]) by kanga.kvack.org (Postfix) with ESMTP id AE2BF6B0085 for ; Thu, 27 May 2021 16:23:40 -0400 (EDT) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 5021218017376 for ; Thu, 27 May 2021 20:23:40 +0000 (UTC) X-FDA: 78188136600.31.1B0F7DF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 0E8E5C00775A for ; Thu, 27 May 2021 20:23:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622147019; 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=ibDzUgI+Vk9OcaOLv+5pNYdmMu7OVveyljm4h1/HCNE=; b=GOlZz/GtdNM/GGCwsRSH6iLgzJ5M5vZO5aC6yaSHBE6hu8W510uWtyniKgN8p8+IIX+k5h wGez7T6KOgBYc/kl5px7nzfJ9GhhM+giHUsHcy9Rl9TGQqNt3WoQ6gYmjlIhBH+zpqWn8f aLtDq6H/NxuW803KBx2PW2GiU9xtpy4= 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-281-tgNMt0XHOZyKwY4WELWkyQ-1; Thu, 27 May 2021 16:23:37 -0400 X-MC-Unique: tgNMt0XHOZyKwY4WELWkyQ-1 Received: by mail-qv1-f72.google.com with SMTP id w14-20020a056214012eb02901f3a4388530so997882qvs.17 for ; Thu, 27 May 2021 13:23:37 -0700 (PDT) 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=ibDzUgI+Vk9OcaOLv+5pNYdmMu7OVveyljm4h1/HCNE=; b=A0I4h3uskvzO4/NNnwiGMcxUDGx5Q5oNDA19NtX33KlgVhSWRSezaeAhUyldB8rKsd totcRtmkRWwx+P++RoeKhPFbR2seW0MKC+TBrz26oMeTcT1n0PIbdI4oOI7o7RN1/soA 6ktQO89H2RYyvSWVC0cBgXiVCX11d/jDmO+OFCcU6Z4+wrc4jPDAV7aZ1tS7CPU6RPGp 4V2TtjzyNWWFTFUJmhJYq4BPRyowSgeKZulEFAFAXjHz9DdT9Vi5TaOOUosbVnqQpxP7 aayHEd1Qw+HX2d//aZzZH7/cdQW6S7ioqTiZfLWfbU3d8+qnO30OTD/obSyQmUvwmMKb L7xA== X-Gm-Message-State: AOAM532Ct5TuF5+0s7CNDCNx+zGuhpYB0Y4tDAG7vVLzofdqeeSub7e1 P8sJ0HsoP1hz4y9i0z6I7lfKwsCTu2xEBJnQYzWJhbRVu+0X0EJuZztlrDX/c+D1YRbARA3nC0D fVrrKKOIyIds= X-Received: by 2002:ac8:4319:: with SMTP id z25mr368489qtm.262.1622147016869; Thu, 27 May 2021 13:23:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzRfnnHZwDBhN/ZaRlD0zP/AETW62hdvxMzoXmXTSL4dORe6PtNpJSYMnRHeAUI2t6h/U4u8Q== X-Received: by 2002:ac8:4319:: with SMTP id z25mr368460qtm.262.1622147016558; Thu, 27 May 2021 13:23:36 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id a27sm2057437qtn.97.2021.05.27.13.23.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 May 2021 13:23:35 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Rapoport , peterx@redhat.com, Andrew Morton , Mike Kravetz , Jerome Glisse , Miaohe Lin , Nadav Amit , Hugh Dickins , Matthew Wilcox , Jason Gunthorpe , "Kirill A . Shutemov" , Andrea Arcangeli , Axel Rasmussen Subject: [PATCH v3 23/27] hugetlb/userfaultfd: Allow wr-protect none ptes Date: Thu, 27 May 2021 16:23:32 -0400 Message-Id: <20210527202332.32205-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210527201927.29586-1-peterx@redhat.com> References: <20210527201927.29586-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="GOlZz/Gt"; spf=none (imf06.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 0E8E5C00775A X-Stat-Signature: eo6ta3st8sfm1h9mra76yqqw6mtqttsu X-HE-Tag: 1622147011-343226 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 | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index a17d894312c0..c4dd0c531bb5 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5486,7 +5486,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; @@ -5506,13 +5506,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; @@ -5537,12 +5543,21 @@ 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_swap_special_pte(pte))) { + WARN_ON_ONCE(!pte_swp_uffd_wp_special(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)); @@ -5556,6 +5571,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, + pte_swp_mkuffd_wp_special(vma)); } spin_unlock(ptl); }