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 526A8C433E0 for ; Fri, 15 Jan 2021 17:10:08 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 020172333E for ; Fri, 15 Jan 2021 17:10:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 020172333E 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 A17068D01AA; Fri, 15 Jan 2021 12:09:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C64F8D01AE; Fri, 15 Jan 2021 12:09:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86A6D8D01AA; Fri, 15 Jan 2021 12:09:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0035.hostedemail.com [216.40.44.35]) by kanga.kvack.org (Postfix) with ESMTP id 6B64E8D01AE for ; Fri, 15 Jan 2021 12:09:52 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 26AAC1DFE for ; Fri, 15 Jan 2021 17:09:52 +0000 (UTC) X-FDA: 77708646624.18.rock89_580513327531 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin18.hostedemail.com (Postfix) with ESMTP id E884A100EDBCA for ; Fri, 15 Jan 2021 17:09:51 +0000 (UTC) X-HE-Tag: rock89_580513327531 X-Filterd-Recvd-Size: 9107 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 for ; Fri, 15 Jan 2021 17:09:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610730591; 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=ACj7dAXdw9MZrmiH0lb9ADzAHOunb4bShuydpXqJ1bw=; b=Wsma3Csp5LMGvUYiZPDZUVfGMFf+t9CJ6cDOgTrw0GwtqqPxQD0yz7KOGpccVh7ZJrGc3h zZABgtOYs4YQMeXt/wEjXJrOM3Fq2CKEhg99mxrheRvFh/KbzNjtZ55ab0zxn6Vaxc2D6G WpnRLK6BlDLKVzgEUTNwVODLhDMUp1Y= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-445-mC5fRZaCPxW6ncyQwX-vHw-1; Fri, 15 Jan 2021 12:09:49 -0500 X-MC-Unique: mC5fRZaCPxW6ncyQwX-vHw-1 Received: by mail-qv1-f72.google.com with SMTP id h1so8238970qvr.7 for ; Fri, 15 Jan 2021 09:09:49 -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=ACj7dAXdw9MZrmiH0lb9ADzAHOunb4bShuydpXqJ1bw=; b=pW3kx4DFeMBSjDEkVY/oETBunehfIesw4NsN93lD4fOOaDB1lTuJV5U/d2Ev1RaP0E w++Y6yi7ieI3PyxCbylko84/S/UAxFDmkfNOJfhlquKDcUKQ9XJcWjIlDmHVdqvFJTy5 xjBCHjfXCeIkbHf44wuJSeNi/mTJGPpPYjjuP2ZrTl/zyBALX5toMI7xrzJh/tW62b7w Of34RqG5EABHeJMpSvDuj0oCCOZKRC53iqUos216VrVMiGol4+P5qQ71+CxU9Z6P3ReA Bt6MUJzvwpsGu/Rd3AGPUW9DakO1LrwpRscvYWPo2eTJiMpdNb6EdWxJwAs0Jwm07TeS AmAA== X-Gm-Message-State: AOAM530+yAYfxliSncIk+/TQ6M5hPo5J3F+6Hgt/z++6sGmvxVnig7rz VtleVVGyCkkSNrrYbBGF6SUQ/hShp06/zNz26k8sGylyi1q1xCtQNJPlXn5bMObtcUQ//9wfAZy 9mBOT/qosHko= X-Received: by 2002:a05:620a:145a:: with SMTP id i26mr13319234qkl.31.1610730589030; Fri, 15 Jan 2021 09:09:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJym5MvHIxxRQ18MHwTBywIzvX4iFw2g80I1OMBrnkwSP/L9AX0DIkatK4Nke7iZJFu9Bek/gQ== X-Received: by 2002:a05:620a:145a:: with SMTP id i26mr13319205qkl.31.1610730588799; Fri, 15 Jan 2021 09:09:48 -0800 (PST) Received: from localhost.localdomain ([142.126.83.202]) by smtp.gmail.com with ESMTPSA id d123sm5187840qke.95.2021.01.15.09.09.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jan 2021 09:09:48 -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 20/30] hugetlb/userfaultfd: Handle UFFDIO_WRITEPROTECT Date: Fri, 15 Jan 2021 12:08:57 -0500 Message-Id: <20210115170907.24498-21-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: 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 bd061f7eedcb..fe1dde0afbaf 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -185,7 +185,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); =20 @@ -343,7 +344,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 563b8f70537f..18b236bac6cd 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4938,7 +4938,8 @@ long follow_hugetlb_page(struct mm_struct *mm, stru= ct vm_area_struct *vma, #endif =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; @@ -4948,6 +4949,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 @@ -4988,6 +4991,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++; @@ -5001,6 +5008,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 055871322007..fce87ac99117 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 b00e5e6b8b8b..480d91b783d4 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -644,6 +644,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 @@ -680,6 +681,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