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.5 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 A48BFC4707F for ; Thu, 27 May 2021 20:23:31 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 465CB613CC for ; Thu, 27 May 2021 20:23:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 465CB613CC 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 DAE5D6B0075; Thu, 27 May 2021 16:23:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D86E36B0081; Thu, 27 May 2021 16:23:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD7AF6B0082; Thu, 27 May 2021 16:23:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0081.hostedemail.com [216.40.44.81]) by kanga.kvack.org (Postfix) with ESMTP id 8A49A6B0081 for ; Thu, 27 May 2021 16:23:30 -0400 (EDT) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 19698181AF5CA for ; Thu, 27 May 2021 20:23:30 +0000 (UTC) X-FDA: 78188136180.07.FD88CC2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 2423CE005F2A for ; Thu, 27 May 2021 20:23:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622147009; 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=WbZt0eoEWLox3U/frFubDdAmo5YNm3Rwmy7KTYyiQYM=; b=Ab5r7yGfLjsI3nuqjJGO6eRRgJGeQGVSIpnaB2Kc1xjPkhXIzjCurKDzg6SkaGzR9TWUKM ZcoPA+P4bvZDzleUI76xyWzJfFKKkWhOgcNUT9oz2a6dFAlhfUjXFG2bq+avhqqY0tyRLI qdizSD0ZAyaUFcEz3l1GqEz9MXuNKtw= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-163-rWw2ytkyOu-AjYshqr0N5w-1; Thu, 27 May 2021 16:23:25 -0400 X-MC-Unique: rWw2ytkyOu-AjYshqr0N5w-1 Received: by mail-qv1-f70.google.com with SMTP id fi6-20020a0562141a46b02901f064172b74so1049514qvb.3 for ; Thu, 27 May 2021 13:23:25 -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=WbZt0eoEWLox3U/frFubDdAmo5YNm3Rwmy7KTYyiQYM=; b=Q496BME/waUmHDqLJSUUuNnfwoiG7JvTol0Qqr+UDB5R4cSaAfxC9HZAyH0Xhc+rei wXSsJi74No+ur8QPkFchAri6n8hQU9qNiOFUw5EyFVQ6bJK/UsItCa7y622/E972VfM3 49/5tBSRGs5U5ODLl03vxAYnY+5jlYMG2KrWGM8TmEdivHKQNUF/pp2dwjVLSFjq7izu KcECXtSHzspRstW/D+1h2GdCa0QkbrHQaKszTnRAzyB0HtlDOwsS7A36ZwIzZ5EvH4bN 7CgTPhPxCwM70Mki+fs3YxxPGc5qAVVVAHXtLU0DUMLXfbehgHGysB1v3VkDXJ9Fcga7 IQPQ== X-Gm-Message-State: AOAM532ZyKK9wuQjsEOwf6xHTOZLK+L2fGocqHN6WHp8bU9b9XezCcCv 0EGj0XUNYVBBkNKyx1+oOS2Xu1SZVtRh4SSpvGFmpgVXbZGhzE/OLPotl6opA319zh12oXqHZa/ +MC9TRNOf+3U= X-Received: by 2002:a05:620a:8d6:: with SMTP id z22mr289787qkz.407.1622147004607; Thu, 27 May 2021 13:23:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7EDf1VO1anNf/T5xNjyyn6/xaNm6K5kKhKB1cqb49ErCH+/+zpWA+AqfvzIh476npMMVMZA== X-Received: by 2002:a05:620a:8d6:: with SMTP id z22mr289770qkz.407.1622147004349; Thu, 27 May 2021 13:23:24 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id t25sm2011860qkt.62.2021.05.27.13.23.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 May 2021 13:23:23 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Rapoport , peterx@redhat.com, Andrew Morton , Mike Kravetz , Jerome Glisse , Miaohe Lin , Nadav Amit , Hugh Dickins , Matthew Wilcox , Jason Gunthorpe , "Kirill A . Shutemov" , Andrea Arcangeli , Axel Rasmussen Subject: [PATCH v3 20/27] hugetlb/userfaultfd: Handle UFFDIO_WRITEPROTECT Date: Thu, 27 May 2021 16:23:20 -0400 Message-Id: <20210527202320.32048-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210527201927.29586-1-peterx@redhat.com> References: <20210527201927.29586-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: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ab5r7yGf; spf=none (imf21.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: rspam01 X-Rspamd-Queue-Id: 2423CE005F2A X-Stat-Signature: zd8twup4kj5o748be6pz8h8sqg8muyjk X-HE-Tag: 1622146999-655364 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 d238a69bcbb3..3e4c5c64d867 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -204,7 +204,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); @@ -368,7 +369,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 9bdcc208f5d9..b101c3af3ab5 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5444,7 +5444,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; @@ -5454,6 +5455,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 @@ -5495,6 +5498,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++; @@ -5509,6 +5516,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 424d0adc3f80..82c235f555b8 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -743,6 +743,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 @@ -779,6 +780,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