Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp861924pxj; Thu, 27 May 2021 13:25:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwy6BNFP4OLwJkYDr9J7cTqJLUtsRrf7ssJBoO09Z8HWvNf50/zvlGigCk74cRoD6ZxHDzE X-Received: by 2002:a02:6d6c:: with SMTP id e44mr5108823jaf.81.1622147126385; Thu, 27 May 2021 13:25:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622147126; cv=none; d=google.com; s=arc-20160816; b=OskpUsRh4yICCGGMut4RFinJ0g4ymRIzTLrgxSG3OcPVU6t+GOYTWYRkWcTlPeS2tp ghAiVKKsJWr/quJZUhym1nw3cYEkCxr4j54u4LBkhdEZLju2v3OBbqsVUUHUFMOmAOEP puFmoYmS069J00ZCS5FKlwuP/cNu1LpovspiCna6Xub1OVOjb3GI1IWQc7bZGpSBnbSR tOuhlONhqHhkOHdoLayCncIsxoP3j7JB41nURZ2ZKj2l97TFYRNDNkw1mlJjpf56tFJg CiC/C0MgJAYR5CY727YcotdzlVhLz8u44nzZuEDUp64k4KlAHOUTBmIRGvUc/skwemnD I8JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WbZt0eoEWLox3U/frFubDdAmo5YNm3Rwmy7KTYyiQYM=; b=djTRc1742lzx8ZfpekZAhOjV3c1mF5RCncTl2lehLJg5nz8Zl9Yxc7B8K4lrmWU6D3 rkEScDn/xIn2ZGz+TsDk79rEjR71A0ZJWgEugiMMs8NYseMDosy2iD6LLGOpZTRu4JUD uDZp4ieHaB+KsY1SjQAXuPSmEuL77pwiGtRjrj2Hl+m4zTKbvRz5Nhr9qP2k1bRgU3QO kznz0NbrlXISv1QD99E3lfMDtRRP4iAHtdPsqRCraQmr9VmMzM6ihnjArQwgxytiQu0z CD0knn/YLa/6EaBE22sf+QI1e5BahLz1ZNNgE9sL6qG9h7SC8smDkQ9/XIMTBwUPvVl8 Z2Yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fEBllCdj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z6si3237823ilq.89.2021.05.27.13.25.02; Thu, 27 May 2021 13:25:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fEBllCdj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236670AbhE0UZO (ORCPT + 99 others); Thu, 27 May 2021 16:25:14 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:41562 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236591AbhE0UZB (ORCPT ); Thu, 27 May 2021 16:25:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622147007; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WbZt0eoEWLox3U/frFubDdAmo5YNm3Rwmy7KTYyiQYM=; b=fEBllCdj4KipenFJAPDb+gEbTODiR5kqXLC3MM+Fte6rpUuHfppgySssPtakZJHKoVuadm eJ1610h0zXSbonu62SFe4zAnIV5aIdb7PIWjQ7dUe1n/AaAmES2Y93CfA49/FerklcK7r+ eOgoLt3BIgy9nCEdqbSJ0cSGnqR//u4= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-202-ej7WcUOTPuOSBZtfhASgLg-1; Thu, 27 May 2021 16:23:25 -0400 X-MC-Unique: ej7WcUOTPuOSBZtfhASgLg-1 Received: by mail-qv1-f69.google.com with SMTP id j14-20020a0cf30e0000b02902142ba51ef2so1022298qvl.10 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=UNTV+QdAjprJakYAx/KdUD1+HxI8qRvbWe27KUagd37C8tJfv6VrWVse+oDyLnN6po V2+91qsweLsUCyzO316MMX3RekxFg0K6eO6v94VZEAMDJrX14j+FFR50aXM73Y2/erjl UtRdXRwd/2pEbisyEWr/+pwRji5VnqA+7lzxYPm/44DTWNalyCHwNY2R8g3OIBnkTjdU oBGlTM7540V8NBXxL6gKdB8RM5hcAp7034HNSPWzWKiJ/Tc9LAdCQXvf18spftnej035 k1zeUpiAdHJcETjMD6uytOwXorKoShAyONYGFdC4lmUAhrQzDeijxk6mc2iHLw0JUUuv BROw== X-Gm-Message-State: AOAM532FzYZmT2R9Qxe8jb6w2pmWV4LcnIG1zvD7UE5m+Z/kD4HXbOdq GTyusFSEauOoO3vAzuyHMYeQf9/hK+IM39E582EOCZvDZUFjI+jtHNHDoYOnQWPMfHs9TbytAgX QlfLuXJbdpS/6tY0fKpQqyOWElYpBTxl+vkDMPVOR0vjZbLVL3QBeW64raxCGTYz7K/iaOgjY2w == X-Received: by 2002:a05:620a:8d6:: with SMTP id z22mr289802qkz.407.1622147004669; Thu, 27 May 2021 13:23:24 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This starts from passing cp_flags into hugetlb_change_protection() so hugetlb 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, unsigned 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); 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 *oldpage, 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, struct vm_area_struct *vma, } 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 = vma->vm_mm; unsigned long start = address; @@ -5454,6 +5455,8 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, unsigned long pages = 0; bool shared_pmd = false; struct mmu_notifier_range range; + bool uffd_wp = cp_flags & MM_CP_UFFD_WP; + bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; /* * 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 = make_readable_migration_entry( swp_offset(entry)); newpte = swp_entry_to_pte(entry); + if (uffd_wp) + newpte = pte_swp_mkuffd_wp(newpte); + 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)); pages++; @@ -5509,6 +5516,10 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, old_pte = huge_ptep_modify_prot_start(vma, address, ptep); pte = pte_mkhuge(huge_pte_modify(old_pte, newprot)); pte = arch_make_huge_pte(pte, shift, vma->vm_flags); + if (uffd_wp) + pte = huge_pte_mkuffd_wp(huge_pte_wrprotect(pte)); + else if (uffd_wp_resolve) + pte = 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) == MM_CP_UFFD_WP_ALL); if (is_vm_hugetlb_page(vma)) - pages = hugetlb_change_protection(vma, start, end, newprot); + pages = hugetlb_change_protection(vma, start, end, newprot, + cp_flags); else pages = 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, unsigned 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; @@ -779,6 +780,13 @@ int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, if (!vma_is_anonymous(dst_vma)) goto out_unlock; + if (is_vm_hugetlb_page(dst_vma)) { + err = -EINVAL; + page_mask = vma_kernel_pagesize(dst_vma) - 1; + if ((start & page_mask) || (len & page_mask)) + goto out_unlock; + } + if (enable_wp) newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE)); else -- 2.31.1