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 AD16DC636CA for ; Thu, 15 Jul 2021 20:16:38 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5D1C0613D2 for ; Thu, 15 Jul 2021 20:16:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5D1C0613D2 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 BD8B58D0102; Thu, 15 Jul 2021 16:16:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BAE7A8D00EC; Thu, 15 Jul 2021 16:16:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A50EA8D0102; Thu, 15 Jul 2021 16:16:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0145.hostedemail.com [216.40.44.145]) by kanga.kvack.org (Postfix) with ESMTP id 7D9688D00EC for ; Thu, 15 Jul 2021 16:16:38 -0400 (EDT) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5CD9D1859AA0B for ; Thu, 15 Jul 2021 20:16:37 +0000 (UTC) X-FDA: 78365930034.31.5FC49AC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf05.hostedemail.com (Postfix) with ESMTP id ED9685010187 for ; Thu, 15 Jul 2021 20:16:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626380196; 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=TaAa2f5HhiDdTwzlDRlFEqwUvqKvM7+1vDjPPjycPYg=; b=VKUJQrkC0f7sKc9nnNUhaBZK7fxjKComlrepLIajcorASZVBFg3Dv/wg4q0zEaHiMS9ySy 6qwfUyVlRE2T43Xve+9hSryVXCvThEVHgREFcIPR0QZL15q2PHk4K8ESE1DLzX2l4lOhT8 czT8kXc/Zv0bDaZ2d+uzdsAppenxNp4= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-135-lIf8qRdoNyWy0RdLwOMZZg-1; Thu, 15 Jul 2021 16:16:35 -0400 X-MC-Unique: lIf8qRdoNyWy0RdLwOMZZg-1 Received: by mail-qk1-f199.google.com with SMTP id 81-20020a370b540000b02903b854c43335so4655585qkl.21 for ; Thu, 15 Jul 2021 13:16:35 -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=TaAa2f5HhiDdTwzlDRlFEqwUvqKvM7+1vDjPPjycPYg=; b=INdSW9gxpxGlzbGnesebC9JP7c2QNXBCPHmtNEKdrcmP06NZKaZ01Ezf8sk55eJU5t /G2b3DFMjxhXpyMqAQcjYqRW7pb4SKcK3BHzCgI/nSkVDsM6Oz5pAWqe4gL6AkG1nfjz HcACZVOnanvF3RbgKTNqFypjd7XlZZlX7VwQX7kmuRgPwVmf/iBwnE1rLRn3J29pyrgk 03I32bIuk9cD3KJumZmRsbbfn1ujJgtjJgbHJQ+50pi6DayCwyhrY73IZaO6sTzwCwWW pJXMRs7XLRdZGHFYGRgihaR538Vx7RUFwVA6w3kaYOiMc2coItI7xJmQVNmPy23j4D3P WAOQ== X-Gm-Message-State: AOAM532Z6VYH7E/WJRj1LYXDIKeK6QDOdaZ0XFlLQOrrNjt/uBru5m0M FZEWp+kV1VdehUmA5ws1sRN3ylC56BuS0uC6DrUDO0vGiJV5dtrAGB6emrsSUr18SLCEJuDFPQB KWrfbBi4HNmA= X-Received: by 2002:a05:622a:554:: with SMTP id m20mr5586529qtx.179.1626380194673; Thu, 15 Jul 2021 13:16:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCYFCYWTao+/GmLyupOP7Orbtxc30SWwQIjFWpns0q/lqsZyOZZQdbkKy3HTSsxYXRi6ymAA== X-Received: by 2002:a05:622a:554:: with SMTP id m20mr5586498qtx.179.1626380194418; Thu, 15 Jul 2021 13:16:34 -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 k66sm3157896qke.28.2021.07.15.13.16.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jul 2021 13:16:33 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Nadav Amit , Jerome Glisse , "Kirill A . Shutemov" , Jason Gunthorpe , Alistair Popple , Andrew Morton , David Hildenbrand , peterx@redhat.com, Andrea Arcangeli , Matthew Wilcox , Mike Kravetz , Tiberiu Georgescu , Hugh Dickins , Miaohe Lin , Mike Rapoport Subject: [PATCH v5 19/26] hugetlb/userfaultfd: Handle UFFDIO_WRITEPROTECT Date: Thu, 15 Jul 2021 16:16:30 -0400 Message-Id: <20210715201630.211865-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210715201422.211004-1-peterx@redhat.com> References: <20210715201422.211004-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="US-ASCII" Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VKUJQrkC; spf=none (imf05.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-Rspamd-Server: rspam02 X-Stat-Signature: 3gasfqf3rc5uupwcjpuu1mu31jntyzdr X-Rspamd-Queue-Id: ED9685010187 X-HE-Tag: 1626380196-319388 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. Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu --- include/linux/hugetlb.h | 6 ++++-- mm/hugetlb.c | 13 ++++++++++++- mm/mprotect.c | 3 ++- mm/userfaultfd.c | 8 ++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index fcdbf9f46d85..e19ca363803d 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -205,7 +205,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); @@ -372,7 +373,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 880cb2137d04..4edb3ee885ea 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5519,7 +5519,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; @@ -5529,6 +5530,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 @@ -5570,6 +5573,10 @@ unsigned long hugetlb_change_protection(struct vm_= area_struct *vma, entry =3D make_readable_migration_entry( swp_offset(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++; @@ -5584,6 +5591,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, shift, vma->vm_flags); + 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 3fcb87b59696..96f4df023439 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -426,7 +426,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 501d6b9f7a5a..7ba721aca1c5 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -695,6 +695,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 @@ -731,6 +732,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.31.1