From patchwork Mon Nov 15 08:02:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12618873 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7870C433EF for ; Mon, 15 Nov 2021 08:02:18 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 94A5761B97 for ; Mon, 15 Nov 2021 08:02:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 94A5761B97 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 3EFA26B007D; Mon, 15 Nov 2021 03:02:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 39F736B0093; Mon, 15 Nov 2021 03:02:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 267516B0095; Mon, 15 Nov 2021 03:02:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0069.hostedemail.com [216.40.44.69]) by kanga.kvack.org (Postfix) with ESMTP id 185456B007D for ; Mon, 15 Nov 2021 03:02:18 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id CE2B382D90 for ; Mon, 15 Nov 2021 08:02:17 +0000 (UTC) X-FDA: 78810421956.14.8136F44 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 66E01B000093 for ; Mon, 15 Nov 2021 08:02:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1636963336; 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=/Iv5CypmvIVE6oMVapm0QBWFP5IxCJXl/Bg1UzUVmko=; b=GqIdtyVnWnmYfMPtmeaD6NqUEoFAxXF46atsus5grGBG5zA1Fi/ccq92+BXw1QuYDMFs0a J6d6rcSPS0lr+EPKigDWkT+tCwpYzgXF/EGCdG7FSjHrQ/QLxWcAayYtKKrRt9DoQ+0F1+ o+t/KM7TL6sHjT+Gt8KxaJ6tzPmw44I= Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-601-SAseUZbsNwur529P4IiJ4Q-1; Mon, 15 Nov 2021 03:02:15 -0500 X-MC-Unique: SAseUZbsNwur529P4IiJ4Q-1 Received: by mail-pf1-f197.google.com with SMTP id y124-20020a623282000000b0047a09271e49so9557926pfy.16 for ; Mon, 15 Nov 2021 00:02:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/Iv5CypmvIVE6oMVapm0QBWFP5IxCJXl/Bg1UzUVmko=; b=zU8/KA+6SxeWRsJmJAqC8EJeB4+ncME1nFymd/I1NcAW3cMu1Ft6T9dwmsbmFSMNDm XOJ9/QSENms1NNWjeK4cFLqHn+Lou9jf/5OxrNuVCTtFju3S8RL+Y7rWVxnhmsbNwL/q kgBHHNKefl91BgfnnT+ttDJ+GG/KLgSKQet2o6J62t2iP6TO2plOqf/0PuWncJ4IbZJ1 03nXJWEVB1NWaJbW03G4M4w6tvBloAWigt0JmZFMaKxoVJ8+K9SEmEgIolbzDukf4XNq CkEdwl0GX91BB1nqQ8CJ6GmkeRxOYLyvpfkA92Z3BolM2UJCUJ6NkReOUIaqd+kCi+Pp ezbQ== X-Gm-Message-State: AOAM533oWrRKMYQNeTMS5djP6SkftoDuljs7EOLqnbmmd3F9NRGUKt/U 7MSERK8vXzYPzb83vZeMgJML2xDLy8fW9EQJScld/P3wYU1+aw2lAIFhCYMHSTjHtE3RApNM+TI M8JO941eTwCFIRvJl85Rmuo1D7vMMKnPZ5Jl2E6qUx8dykNSF+ZajiTZrhX/t X-Received: by 2002:a17:90b:38c5:: with SMTP id nn5mr61485332pjb.220.1636963334144; Mon, 15 Nov 2021 00:02:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJyNSkZ/vww+jPhvco2wNnlsy70sP9i0ZGZit2163wehM7xneRAKVX66NU/eBtol2Mp8mRmPcQ== X-Received: by 2002:a17:90b:38c5:: with SMTP id nn5mr61485283pjb.220.1636963333787; Mon, 15 Nov 2021 00:02:13 -0800 (PST) Received: from localhost.localdomain ([94.177.118.89]) by smtp.gmail.com with ESMTPSA id h25sm10459878pgm.33.2021.11.15.00.02.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Nov 2021 00:02:13 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , peterx@redhat.com, Alistair Popple , Andrew Morton , Mike Kravetz , Mike Rapoport , Matthew Wilcox , Jerome Glisse , Axel Rasmussen , "Kirill A . Shutemov" , David Hildenbrand , Andrea Arcangeli , Hugh Dickins Subject: [PATCH v6 14/23] mm/hugetlb: Handle UFFDIO_WRITEPROTECT Date: Mon, 15 Nov 2021 16:02:00 +0800 Message-Id: <20211115080200.74866-1-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211115075522.73795-1-peterx@redhat.com> References: <20211115075522.73795-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 66E01B000093 X-Stat-Signature: yswo9emrwc1h67quqgftoti9con3u5t4 Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GqIdtyVn; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf19.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com X-HE-Tag: 1636963327-196486 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 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 4da0c4b4159a..a46011510e49 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -210,7 +210,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); @@ -391,7 +392,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 8146240eefc6..7fc213c0ebf8 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6127,7 +6127,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; @@ -6137,6 +6138,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 @@ -6178,6 +6181,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++; @@ -6192,6 +6199,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 0d4bf755cee8..1cc4a6d1886b 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -441,7 +441,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 6174a212c72f..037f82719e64 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -690,6 +690,7 @@ int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, atomic_t *mmap_changing) { struct vm_area_struct *dst_vma; + unsigned long page_mask; pgprot_t newprot; int err; @@ -726,6 +727,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