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 05519C433C1 for ; Tue, 23 Mar 2021 00:50:01 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9F87B619A0 for ; Tue, 23 Mar 2021 00:50:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9F87B619A0 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 4BA9A6B0134; Mon, 22 Mar 2021 20:49:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 442686B0136; Mon, 22 Mar 2021 20:49:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 295E66B0137; Mon, 22 Mar 2021 20:49:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0163.hostedemail.com [216.40.44.163]) by kanga.kvack.org (Postfix) with ESMTP id 055156B0134 for ; Mon, 22 Mar 2021 20:49:48 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id C5926180AD82F for ; Tue, 23 Mar 2021 00:49:47 +0000 (UTC) X-FDA: 77949306414.36.DFD31F3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf03.hostedemail.com (Postfix) with ESMTP id A5225C0007C4 for ; Tue, 23 Mar 2021 00:49:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616460586; 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=UhBZPg5Z3WMoKN7PnLVoaXXwSSsImCDdZ5uN4rmdysI=; b=HTDEx8z2gH6CJOGQUaQoiDsAc3Q3GN3//49gDTbTtVN6b+GlIa0BwLzAykKd/lDRyyKVHi kj8TM5T89QeC+3bHaaaYHnFhvRfjf6JVCu18IX7A1hNPYXGDdbbPNc2/S0Gx1jnFxzeDgk iOcPf6+11PYyNroOxyIZ7P3A23Sr5Ak= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-295-qEOzYZonNoWHhVT4F2HrCA-1; Mon, 22 Mar 2021 20:49:45 -0400 X-MC-Unique: qEOzYZonNoWHhVT4F2HrCA-1 Received: by mail-qt1-f197.google.com with SMTP id c20so404408qtw.9 for ; Mon, 22 Mar 2021 17:49:45 -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=UhBZPg5Z3WMoKN7PnLVoaXXwSSsImCDdZ5uN4rmdysI=; b=eCTPtDEBIDN13y2EXOiBAMgx2iHAex8mdJnv01wQcf1X+2Z3ohOYb8H3tmXxFTocET n0P11inyIw+CKbHnJrQ7pV9mKAGsjcviNvwJXwE+jt2zspJXrnjHS2XCE1+P3LqqXlmT QUDf9y7NdPVsgbgzKEDdvUbXB0bWzSDV2UPN9aI2720QC3bGq4FjXUcSAj8Xj1TP0B9n p9oVP8imv1a9d0liyMjVEeOrTr4y/VOrUHMXzbexTZuxfCIUBjYRJXr/mAi/g19/RMJj 27P7ifD0qMJE4P+P/5bVluoW9fhdjN1qfNUPrk3JrdKz7b2V0HhE0DuhhxYFHY0fcKsf e6BQ== X-Gm-Message-State: AOAM530yzyW/sA8heXNcILc/nGHLWewQxK9ItpqHkgrawz4nGu0un53A D37LFAXbSr3HsS8g8gr06ucKHBtXnLzXEiznVRStxJj2ZfT3OMeGAqGovxfzWzT2YK87F0dupZn GaqvpS+lMzSw= X-Received: by 2002:ae9:e513:: with SMTP id w19mr3120251qkf.231.1616460584702; Mon, 22 Mar 2021 17:49:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCH+SBGMXuDXb1jmvEX2TeCWc+Nab5QwAa3TRvreNK9N+mjxOgDxPA3LiapLQzrImvnQdEXA== X-Received: by 2002:ae9:e513:: with SMTP id w19mr3120225qkf.231.1616460584478; Mon, 22 Mar 2021 17:49:44 -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 n6sm5031793qtx.22.2021.03.22.17.49.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 17:49:44 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: "Kirill A . Shutemov" , Jerome Glisse , Mike Kravetz , Matthew Wilcox , Andrew Morton , Axel Rasmussen , Hugh Dickins , peterx@redhat.com, Nadav Amit , Andrea Arcangeli , Mike Rapoport Subject: [PATCH 17/23] hugetlb/userfaultfd: Handle UFFDIO_WRITEPROTECT Date: Mon, 22 Mar 2021 20:49:06 -0400 Message-Id: <20210323004912.35132-18-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 Content-Type: text/plain; charset="US-ASCII" X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A5225C0007C4 X-Stat-Signature: xzqqpf7zfo8tewarih8gd3twayydggup Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf03; 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: 1616460586-73992 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: This starts from passing cp_flags into hugetlb_change_protection() so hug= etlb will be able to handle MM_CP_UFFD_WP[_RESOLVE] requests. huge_pte_clear_uffd_wp() is introduced to handle the case where the UFFDIO_WRITEPROTECT is requested upon migrating huge page entries. Signed-off-by: Peter Xu --- include/asm-generic/hugetlb.h | 5 +++++ include/linux/hugetlb.h | 6 ++++-- mm/hugetlb.c | 13 ++++++++++++- mm/mprotect.c | 3 ++- mm/userfaultfd.c | 8 ++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/include/asm-generic/hugetlb.h b/include/asm-generic/hugetlb.= h index 548212eccbd6..181cdc3297e7 100644 --- a/include/asm-generic/hugetlb.h +++ b/include/asm-generic/hugetlb.h @@ -32,6 +32,11 @@ static inline pte_t huge_pte_mkuffd_wp(pte_t pte) return pte_mkuffd_wp(pte); } =20 +static inline pte_t huge_pte_clear_uffd_wp(pte_t pte) +{ + return pte_clear_uffd_wp(pte); +} + static inline pte_t huge_pte_modify(pte_t pte, pgprot_t newprot) { return pte_modify(pte, newprot); diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index ef8d2b8427b1..92710600596e 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -190,7 +190,8 @@ struct page *follow_huge_pgd(struct mm_struct *mm, un= signed long address, int pmd_huge(pmd_t pmd); int pud_huge(pud_t pud); unsigned long hugetlb_change_protection(struct vm_area_struct *vma, - unsigned long address, unsigned long end, pgprot_t newprot); + unsigned long address, unsigned long end, pgprot_t newprot, + unsigned long cp_flags); =20 bool is_hugetlb_entry_migration(pte_t pte); void hugetlb_unshare_all_pmds(struct vm_area_struct *vma); @@ -352,7 +353,8 @@ static inline void move_hugetlb_state(struct page *ol= dpage, =20 static inline unsigned long hugetlb_change_protection( struct vm_area_struct *vma, unsigned long address, - unsigned long end, pgprot_t newprot) + unsigned long end, pgprot_t newprot, + unsigned long cp_flags) { return 0; } diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f0e55b341ebd..fd3e87517e10 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5063,7 +5063,8 @@ long follow_hugetlb_page(struct mm_struct *mm, stru= ct vm_area_struct *vma, } =20 unsigned long hugetlb_change_protection(struct vm_area_struct *vma, - unsigned long address, unsigned long end, pgprot_t newprot) + unsigned long address, unsigned long end, + pgprot_t newprot, unsigned long cp_flags) { struct mm_struct *mm =3D vma->vm_mm; unsigned long start =3D address; @@ -5073,6 +5074,8 @@ unsigned long hugetlb_change_protection(struct vm_a= rea_struct *vma, unsigned long pages =3D 0; bool shared_pmd =3D false; struct mmu_notifier_range range; + bool uffd_wp =3D cp_flags & MM_CP_UFFD_WP; + bool uffd_wp_resolve =3D cp_flags & MM_CP_UFFD_WP_RESOLVE; =20 /* * In the case of shared PMDs, the area to flush could be beyond @@ -5113,6 +5116,10 @@ unsigned long hugetlb_change_protection(struct vm_= area_struct *vma, =20 make_migration_entry_read(&entry); newpte =3D swp_entry_to_pte(entry); + if (uffd_wp) + newpte =3D pte_swp_mkuffd_wp(newpte); + 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)); pages++; @@ -5126,6 +5133,10 @@ unsigned long hugetlb_change_protection(struct vm_= area_struct *vma, old_pte =3D huge_ptep_modify_prot_start(vma, address, ptep); pte =3D pte_mkhuge(huge_pte_modify(old_pte, newprot)); pte =3D arch_make_huge_pte(pte, vma, NULL, 0); + if (uffd_wp) + pte =3D huge_pte_mkuffd_wp(huge_pte_wrprotect(pte)); + else if (uffd_wp_resolve) + pte =3D huge_pte_clear_uffd_wp(pte); huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte); pages++; } diff --git a/mm/mprotect.c b/mm/mprotect.c index 51c954afa406..fe5a5b96a61f 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -416,7 +416,8 @@ unsigned long change_protection(struct vm_area_struct= *vma, unsigned long start, BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) =3D=3D MM_CP_UFFD_WP_ALL); =20 if (is_vm_hugetlb_page(vma)) - pages =3D hugetlb_change_protection(vma, start, end, newprot); + pages =3D hugetlb_change_protection(vma, start, end, newprot, + cp_flags); else pages =3D change_protection_range(vma, start, end, newprot, cp_flags); diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 78471ae3d25c..01170197a3d7 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -654,6 +654,7 @@ int mwriteprotect_range(struct mm_struct *dst_mm, uns= igned long start, unsigned long len, bool enable_wp, bool *mmap_changing) { struct vm_area_struct *dst_vma; + unsigned long page_mask; pgprot_t newprot; int err; =20 @@ -690,6 +691,13 @@ int mwriteprotect_range(struct mm_struct *dst_mm, un= signed long start, if (!vma_is_anonymous(dst_vma)) goto out_unlock; =20 + if (is_vm_hugetlb_page(dst_vma)) { + err =3D -EINVAL; + page_mask =3D vma_kernel_pagesize(dst_vma) - 1; + if ((start & page_mask) || (len & page_mask)) + goto out_unlock; + } + if (enable_wp) newprot =3D vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE)); else --=20 2.26.2