From patchwork Tue Apr 27 16:13:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12226907 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 52DB1C433ED for ; Tue, 27 Apr 2021 16:14:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E084D61158 for ; Tue, 27 Apr 2021 16:14:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E084D61158 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 F40276B009F; Tue, 27 Apr 2021 12:14:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC6EB6B00A0; Tue, 27 Apr 2021 12:14:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C7DAA6B00A1; Tue, 27 Apr 2021 12:14:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0108.hostedemail.com [216.40.44.108]) by kanga.kvack.org (Postfix) with ESMTP id A92F16B009F for ; Tue, 27 Apr 2021 12:14:02 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 6C27A180AD822 for ; Tue, 27 Apr 2021 16:14:02 +0000 (UTC) X-FDA: 78078643524.12.10B8C58 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 9C0F9E00012A for ; Tue, 27 Apr 2021 16:13:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619540041; 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=l1z1o5mbCVa78Hzla2EzzKhxkMDgZgiPiKG4mm1uLIY=; b=S8dwxmVXkEaUSf5V8eG6+HuRXktYgqn+7lyn8ak7QoGAP2Vpnn95aD0CJ9i5SelMl6WpMM r3sgAA3gM414gKMiKSl6O206vpx8YHaOib3CyKSboF4xZJWmu9EKseFJc06/8cRXdoSPWO 0gAWfIcp4k3oXBebfjp8qyZw8Vp9KZ4= 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-41-YmWaluwFMyyHOfBdGFb3Gw-1; Tue, 27 Apr 2021 12:13:57 -0400 X-MC-Unique: YmWaluwFMyyHOfBdGFb3Gw-1 Received: by mail-qt1-f199.google.com with SMTP id x13-20020ac84d4d0000b02901a95d7c4bb5so23539247qtv.14 for ; Tue, 27 Apr 2021 09:13:57 -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=l1z1o5mbCVa78Hzla2EzzKhxkMDgZgiPiKG4mm1uLIY=; b=G1K8EngY+tJU0hmsj6DXh3w3B7Wlu4lyzNf9p6bnr0TzKcAj26JJ2OxVC7wFcUgws+ iD2E592GAgZ4NvGbBPsIRnUYot33PMvLsBBTgqq6vB3ewOtBXhObBSjjO0CEeDYuMxfF BzNWPeZWfKcW7/EBXMwzhID4mdd9Wa3i/aB3Ay+ptXthUK2Y1/hDqbufxM1eWcsMDqA1 AfDgeikgp3n6wV3QePSPpK/+laaYu1ezib4tYP1qKpxoc41o6FH4LztyD3wYHJgS2Lo1 b1ZFI1V9GBcWxpSrpPMGgT1sWoUtmlzn9XHgF0UKO6TzwG0AmmOUISz6Nn4R+GtuiLKF S5lA== X-Gm-Message-State: AOAM5317ub3AIzW6rkUSk/qGAgtyUQ0/Mn6ABI9yO+ApRYDwPaagoJDi p6AsAaP2Xjl6j6aUvoCPWSmC3RLFHN6zbyQz+N47uvn2HPV0t+tfrMFXMF8QyjU61qgV9mEWF7B 6xaw+Qg3JcZkMQwnluuJn3hjo2vh75pcxSEEQg82HosT9myox++g1R6GSAYbu X-Received: by 2002:a05:620a:133c:: with SMTP id p28mr11069472qkj.3.1619540036890; Tue, 27 Apr 2021 09:13:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBwc48GEDC8J2KNhfxo5GVkwB09tGhSlMBTREmsPdAdSwfXDt4d9f/uX9TJoG1RIhmgJavrQ== X-Received: by 2002:a05:620a:133c:: with SMTP id p28mr11069428qkj.3.1619540036590; Tue, 27 Apr 2021 09:13:56 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-77-184-145-104-227.dsl.bell.ca. [184.145.104.227]) by smtp.gmail.com with ESMTPSA id v66sm3103621qkd.113.2021.04.27.09.13.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 09:13:55 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , Miaohe Lin , Mike Rapoport , Andrea Arcangeli , Hugh Dickins , peterx@redhat.com, Jerome Glisse , Mike Kravetz , Jason Gunthorpe , Matthew Wilcox , Andrew Morton , Axel Rasmussen , "Kirill A . Shutemov" Subject: [PATCH v2 21/24] hugetlb/userfaultfd: Allow wr-protect none ptes Date: Tue, 27 Apr 2021 12:13:14 -0400 Message-Id: <20210427161317.50682-22-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210427161317.50682-1-peterx@redhat.com> References: <20210427161317.50682-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-Queue-Id: 9C0F9E00012A X-Stat-Signature: jpprhehsso1mq5tgc7iy6gaerwraehr1 X-Rspamd-Server: rspam02 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf30; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1619540022-642108 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 d9ff7db14175d..fa9af9c893512 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5264,7 +5264,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; @@ -5284,13 +5284,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; @@ -5314,12 +5320,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; @@ -5332,6 +5347,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); }