From patchwork Wed Jul 14 22:25:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12377983 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,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 88F79C12002 for ; Wed, 14 Jul 2021 22:25:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3EDA9600D1 for ; Wed, 14 Jul 2021 22:25:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3EDA9600D1 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 903526B00D4; Wed, 14 Jul 2021 18:25:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B2606B00D5; Wed, 14 Jul 2021 18:25:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B7F48D0003; Wed, 14 Jul 2021 18:25:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0022.hostedemail.com [216.40.44.22]) by kanga.kvack.org (Postfix) with ESMTP id 49DE06B00D4 for ; Wed, 14 Jul 2021 18:25:27 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 3F3F518484CAE for ; Wed, 14 Jul 2021 22:25:26 +0000 (UTC) X-FDA: 78362625852.09.ECDAF47 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf11.hostedemail.com (Postfix) with ESMTP id D7090F0000A8 for ; Wed, 14 Jul 2021 22:25:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626301525; 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=4EzkVzkNiBeZzaTPMlb/0FnWgb1POP033DBuy6Id530=; b=b1JSRGqlZjPlcgLPS72gkEvg75CXizZBM8DzP+k8xlOCmGEl+2aQn2kYRnXEjxhkA3t1i1 rUtPMml9Nmv/UVjTNpqQJLFpn6sIOCdJj77DgxqPmKtClCUvzMfs39wGC5P0GIMhclut+g RWBjDUYrhmN4YrenyrzG3wrzfyimeKo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626301525; 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=4EzkVzkNiBeZzaTPMlb/0FnWgb1POP033DBuy6Id530=; b=b1JSRGqlZjPlcgLPS72gkEvg75CXizZBM8DzP+k8xlOCmGEl+2aQn2kYRnXEjxhkA3t1i1 rUtPMml9Nmv/UVjTNpqQJLFpn6sIOCdJj77DgxqPmKtClCUvzMfs39wGC5P0GIMhclut+g RWBjDUYrhmN4YrenyrzG3wrzfyimeKo= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-425-ILzpduGLPq6A89uxmHtRVQ-1; Wed, 14 Jul 2021 18:25:24 -0400 X-MC-Unique: ILzpduGLPq6A89uxmHtRVQ-1 Received: by mail-qt1-f198.google.com with SMTP id h17-20020ac858510000b0290251fc608f73so2826241qth.10 for ; Wed, 14 Jul 2021 15:25:24 -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=4EzkVzkNiBeZzaTPMlb/0FnWgb1POP033DBuy6Id530=; b=hdkrzhavLHJzf/4e8RgboTlwKaAUVyGnHiFn0mT1B5Nn51O+Yj0ClFR8nU/2PnVfNP 9zYqaUB+OcDVBeo1o7qzeWIPnIAtDl94yLAx+OQY9QJOs/p1P7bEVe60OvhVmWlwoTnT LTXRxCM6sfey5L6GmYtPtdolG2roFTc7WvbB9jVh6sGuzgGbji0D/Ska1MAEmPooH3uA n7jrp8HGzs3iMd8RXiDau+v2iasMhQT1VTyXtHRv/sst1rSAYaMvSVVZ6dVYsOnCixe9 OosUaX8DMmq96HTAp982lq3s9yOPQknUOQDbB7+iZ+BXfbG1BrRclnp+J4YHN1OALMg2 pS3Q== X-Gm-Message-State: AOAM531Lfm6hmuMUxyXqHxrz1lhwuEjwRAmxI2oe2ll1XqcranhLMpcU l1Jww6vzguqBsPs8L1qyu818wQyfzS/6nKiWBzU66/qe/p6C0oYaffsS4TJNCeBz6sBDaYRhn92 qLCHRw3ExH8tZqNVTry7sAt1cr1U2BiMeLEkQatD2SsLMWjj+EkFDv7hzErCo X-Received: by 2002:ac8:7d90:: with SMTP id c16mr305203qtd.337.1626301523592; Wed, 14 Jul 2021 15:25:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhP5fg1wkNrQQCAKpXp/mImlAGeXr4rNaibuFCbezexu1TgG1nGIouuRpG9MYniJZSSS7zdQ== X-Received: by 2002:ac8:7d90:: with SMTP id c16mr305170qtd.337.1626301523315; Wed, 14 Jul 2021 15:25:23 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-65-184-144-111-238.dsl.bell.ca. [184.144.111.238]) by smtp.gmail.com with ESMTPSA id b3sm1299441qto.49.2021.07.14.15.25.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 15:25:22 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Mike Kravetz , Axel Rasmussen , Miaohe Lin , "Kirill A . Shutemov" , Hugh Dickins , Jason Gunthorpe , Alistair Popple , Matthew Wilcox , peterx@redhat.com, Jerome Glisse , Andrea Arcangeli , Mike Rapoport , Nadav Amit , David Hildenbrand Subject: [PATCH v4 22/26] hugetlb/userfaultfd: Allow wr-protect none ptes Date: Wed, 14 Jul 2021 18:25:19 -0400 Message-Id: <20210714222519.49194-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714222117.47648-1-peterx@redhat.com> References: <20210714222117.47648-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=b1JSRGql; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=b1JSRGql; spf=none (imf11.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-Stat-Signature: exjoytcaaac9tnz13zrgsn3u4bqgpred X-Rspamd-Queue-Id: D7090F0000A8 X-Rspamd-Server: rspam01 X-HE-Tag: 1626301525-479020 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 15e5de480cf0..6ae911185554 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5561,7 +5561,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; @@ -5581,13 +5581,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; @@ -5612,12 +5618,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)); @@ -5631,6 +5646,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); }