From patchwork Tue Mar 23 00:50:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12156515 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 6A388C433E0 for ; Tue, 23 Mar 2021 00:50:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F2D446196C for ; Tue, 23 Mar 2021 00:50:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F2D446196C 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 863356B013C; Mon, 22 Mar 2021 20:50:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 839F26B013E; Mon, 22 Mar 2021 20:50:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 63F766B013F; Mon, 22 Mar 2021 20:50:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0140.hostedemail.com [216.40.44.140]) by kanga.kvack.org (Postfix) with ESMTP id 429726B013C for ; Mon, 22 Mar 2021 20:50:58 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 06A5E8249980 for ; Tue, 23 Mar 2021 00:50:58 +0000 (UTC) X-FDA: 77949309396.03.4C58F1D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf08.hostedemail.com (Postfix) with ESMTP id CCA6E80192D5 for ; Tue, 23 Mar 2021 00:50:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616460657; 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=QYSwV1DdjoGbYhLD7l7jd/ThoyIb5S4HvnYDjWOs2EQ=; b=atv6ChkpYXCADsCIO4jC6My370XExgsucXq8yq9iwoKxw1BGxrqUOQtNRRJ/OjCVWxFvju CcJLl7k0BTZDNsunMbTi4giC4Qp8gZL+YEDPdjJLfIlnKOYcB534lDsmrBboi0HNtMUbqN gjtOu/1qFVAt+FIcL1BUD/jinGgpnD0= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-172--Fx1KWIONYCYcZ0VPFrkUQ-1; Mon, 22 Mar 2021 20:50:55 -0400 X-MC-Unique: -Fx1KWIONYCYcZ0VPFrkUQ-1 Received: by mail-qk1-f200.google.com with SMTP id b127so792171qkf.19 for ; Mon, 22 Mar 2021 17:50:55 -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=QYSwV1DdjoGbYhLD7l7jd/ThoyIb5S4HvnYDjWOs2EQ=; b=MPsrQAk9pPqu5FALAic+doeqGNR9hsvQMbyZTvBB4AB3MWqty79GT0yGzQ9aP1PeVX LLv0AV8ZzoFBWsDrdc3d4GulBqOwO9QoeYROK+ZZGZJ66AoWjKKT2BcY6uAaSjKgIRDe /DOirQSA07k/Em/jbVICqBJxxQyKkeNnAV3GtH1rVwJoMCcOIe8gptRx3f46PJ3UQUZx qv0FVMyqocQyV04nd44jx/xtuqkuqTt+hDObiT5TWb7uz05SZYXpgDyOmu+N+g89zPP9 H8l3lzooQSD6LVCZvfO0SGgd0uG6re78xpJji3j5TbD4Q9x78KpbhbStYIy0aJaSeS1m mLAA== X-Gm-Message-State: AOAM531NId4T2fcDsQM6RMpJbKlvg7MccPvq61B+QDij8qU7cI3Ve0BR 7GIsGXeBUqqEF5qegiXYEb+YebjoANZbVl/eMTQ2C3bjmnA+wq78NpvK0gaCXqf27VYa9JNCa0q 0cVn9Cjh954sloXwzjug4s/u4m1DG+Hsc+oVZGOurQrYDL9BFmt7nhGGdMwUj X-Received: by 2002:ad4:4cc8:: with SMTP id i8mr2460908qvz.56.1616460654793; Mon, 22 Mar 2021 17:50:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/wYGS6MPDtkctKR6iln/9KAo2nZVmqKkBxLuPXgL5Im3S3NO0Z0sGaNw2LONKOI8LyEdDOw== X-Received: by 2002:ad4:4cc8:: with SMTP id i8mr2460882qvz.56.1616460654532; Mon, 22 Mar 2021 17:50:54 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id 8sm12051921qkc.32.2021.03.22.17.50.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 17:50:54 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Mike Rapoport , Nadav Amit , Jerome Glisse , Hugh Dickins , Andrea Arcangeli , Andrew Morton , "Kirill A . Shutemov" , Axel Rasmussen , peterx@redhat.com, Mike Kravetz , Matthew Wilcox Subject: [PATCH 20/23] hugetlb/userfaultfd: Allow wr-protect none ptes Date: Mon, 22 Mar 2021 20:50:52 -0400 Message-Id: <20210323005052.35916-1-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210323004912.35132-1-peterx@redhat.com> References: <20210323004912.35132-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 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: CCA6E80192D5 X-Stat-Signature: 6dn9gsy8s68cbqagadkcs9g67wwh8q4r Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf08; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=170.10.133.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616460657-793427 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. Signed-off-by: Peter Xu Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 448ef745d5ee..d4acf9d9d087 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5110,7 +5110,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; @@ -5130,13 +5130,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; @@ -5160,12 +5166,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; @@ -5178,6 +5193,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); }