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 1E40EC433E0 for ; Fri, 15 Jan 2021 17:10:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BFE9A238EE for ; Fri, 15 Jan 2021 17:10:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BFE9A238EE 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 694268D01B4; Fri, 15 Jan 2021 12:10:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 645248D01B2; Fri, 15 Jan 2021 12:10:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 497208D01B4; Fri, 15 Jan 2021 12:10:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0201.hostedemail.com [216.40.44.201]) by kanga.kvack.org (Postfix) with ESMTP id 2937B8D01B2 for ; Fri, 15 Jan 2021 12:10:08 -0500 (EST) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id BDA94180AD5F6 for ; Fri, 15 Jan 2021 17:10:07 +0000 (UTC) X-FDA: 77708647254.02.kite05_120a17427531 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin02.hostedemail.com (Postfix) with ESMTP id 97BF9100DAA0A for ; Fri, 15 Jan 2021 17:10:07 +0000 (UTC) X-HE-Tag: kite05_120a17427531 X-Filterd-Recvd-Size: 6772 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf33.hostedemail.com (Postfix) with ESMTP for ; Fri, 15 Jan 2021 17:10:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610730606; 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=yrCU019uaMiIX4Y0l4Cvu/CTWpK4vi96Wr+NfUKCM+Y=; b=hD4vKDgTCZ+isd99xziIvVevTlZ/dxc/aT9L4yBHCOftN4acyXwF9Lljx3K4ySgEt/lpxl xlP0J1hXDQvcUVgBIJOm4YnJrialO4LKmkjEtLGgmtTX3fcI3RgKIt+xGTDMBh2q4KVAr9 ccwlsTQ+mzA/PfyiyAqYYNG7klpwrLo= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-388-vqs9zEOwN2CiT5kG31YSYg-1; Fri, 15 Jan 2021 12:10:03 -0500 X-MC-Unique: vqs9zEOwN2CiT5kG31YSYg-1 Received: by mail-qt1-f199.google.com with SMTP id h7so7860520qtn.21 for ; Fri, 15 Jan 2021 09:10:03 -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=yrCU019uaMiIX4Y0l4Cvu/CTWpK4vi96Wr+NfUKCM+Y=; b=stOIr3q01jfxndNxYkuLEzHBihzDqbVoisnnPrnxBVIUHkvEFR1nBOK1U+MHxWN/Pp 4WYdmgr4K7uXiWJWsRLRTQglaLQHPBIozWDulvRFyQwIQcTeDUSt1hOiAWduUrZ4ZAo/ cxjNjDbBa0vxiQH98mCQHwGfbQL6ym7ILdaDpplXszwy8DFlQCmxHfreHN1wcGkaC91m nyt7zNkX2SVQvoUbgU7AHuX9YeVrZfaPVAKW3Gx6vVyG0XRF5x/niYmCkbXe1VmoRIye WeCeaNwjWAZ2g4Eq/vnGTXyCw+K7/TJTveP6bF+esJEKRdteJsKWrWGL/Gogv7Fz5Fq1 nrrA== X-Gm-Message-State: AOAM533TnrDoPwbm3BqMXjixeMqE4kk2mo163eC9JIdr0d3vI/MojGQR yDxCmlX+5zqb4/TGKeTEGh94iuDPM+ess/J2TW9ZMulLVQe/sh9bstABAl/h0RRs3BWeB4c5zjQ eMXcvGObjvdQ= X-Received: by 2002:a05:6214:4e2:: with SMTP id cl2mr12899053qvb.27.1610730602575; Fri, 15 Jan 2021 09:10:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJwDGxBmzq+M9a/zQLYdo9stwTA7YMDDZpLptv18Gfz7OBlvn0/azI9YdYGAEij/PUHl4Ymv6w== X-Received: by 2002:a05:6214:4e2:: with SMTP id cl2mr12899037qvb.27.1610730602378; Fri, 15 Jan 2021 09:10:02 -0800 (PST) Received: from localhost.localdomain ([142.126.83.202]) by smtp.gmail.com with ESMTPSA id d123sm5187840qke.95.2021.01.15.09.10.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jan 2021 09:10:01 -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 27/30] hugetlb/userfaultfd: Allow wr-protect none ptes Date: Fri, 15 Jan 2021 12:09:04 -0500 Message-Id: <20210115170907.24498-28-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: 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 u= ffd-wp marker pte and remove it for uffd_wp_resolve. Since at it, introduce a variable "psize" to replace all references to th= e huge page size fetcher. 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 9b9f71ec30e1..7959fb4b1633 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4978,7 +4978,7 @@ unsigned long hugetlb_change_protection(struct vm_a= rea_struct *vma, pte_t *ptep; pte_t pte; struct hstate *h =3D hstate_vma(vma); - unsigned long pages =3D 0; + unsigned long pages =3D 0, psize =3D huge_page_size(h); bool shared_pmd =3D false; struct mmu_notifier_range range; bool uffd_wp =3D cp_flags & MM_CP_UFFD_WP; @@ -4998,13 +4998,19 @@ unsigned long hugetlb_change_protection(struct vm= _area_struct *vma, =20 mmu_notifier_invalidate_range_start(&range); i_mmap_lock_write(vma->vm_file->f_mapping); - for (; address < end; address +=3D huge_page_size(h)) { + for (; address < end; address +=3D psize) { spinlock_t *ptl; - ptep =3D huge_pte_offset(mm, address, huge_page_size(h)); + ptep =3D huge_pte_offset(mm, address, psize); if (!ptep) continue; ptl =3D 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 =3D true; @@ -5028,12 +5034,21 @@ unsigned long hugetlb_change_protection(struct vm= _area_struct *vma, else if (uffd_wp_resolve) newpte =3D 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; =20 @@ -5046,6 +5061,12 @@ unsigned long hugetlb_change_protection(struct vm_= area_struct *vma, pte =3D 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); } --=20 2.26.2