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 59F04C433E0 for ; Wed, 17 Feb 2021 20:46:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 02BFB64E76 for ; Wed, 17 Feb 2021 20:46:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 02BFB64E76 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 A7BF28D0001; Wed, 17 Feb 2021 15:46:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 969246B0071; Wed, 17 Feb 2021 15:46:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82E2B8D0001; Wed, 17 Feb 2021 15:46:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0204.hostedemail.com [216.40.44.204]) by kanga.kvack.org (Postfix) with ESMTP id 6DDD16B0070 for ; Wed, 17 Feb 2021 15:46:28 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 3711380ACD83 for ; Wed, 17 Feb 2021 20:46:28 +0000 (UTC) X-FDA: 77828942856.19.5A8FD5E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf18.hostedemail.com (Postfix) with ESMTP id F25662000D89 for ; Wed, 17 Feb 2021 20:46:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613594787; 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=9gY06+Wkmcfht3KanCaWtohAIi6SQMiBnqeZFMOnly8=; b=IN2ElJ9cL3cqba1j7Fco5GJVYQf1K2TPSUTrkqkqOzXz0mVNiJbdYeZdlQ1wmpfwhY3Wss o3IKypfp4Ke/JfNhko8ujSVHCea54wn+Afl9jSh6z1P8eqayY4MzZM0A9Vr8gxd1HpZkN9 vqc/r7Ilsp4AKz3t7vH9xqBHjFSjyuI= 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-175-9Qoyw68jMH2Xi6decUZoVg-1; Wed, 17 Feb 2021 15:46:25 -0500 X-MC-Unique: 9Qoyw68jMH2Xi6decUZoVg-1 Received: by mail-qv1-f70.google.com with SMTP id dk3so9623377qvb.1 for ; Wed, 17 Feb 2021 12:46:25 -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=9gY06+Wkmcfht3KanCaWtohAIi6SQMiBnqeZFMOnly8=; b=a0YyFnqMB1yRPmLJMJl002fA2k5bQT9a5JHdg+jda6Y+F+27kFYJ7yhlm8Heyy6GYx eZObkmkIi6ZZPzm5FbEA14LPOEUFTGdcGrWzeyAzRHw9Ls8khTKUpNCA35oHgjpS7lBb opk0gtfvGmVmuvMdYAww9x+WGPd8+zXCBkxX3wPZpAgWUckwNymFkW/WqAGIEswwV3Wc fJO2B7GL5UvP3zL5Kxm4nKdfgtRksLwYA+pd/5BsIxCBPTd0LOoe8wFmESA8SwPb4ydL lkT0A8v1fzUzFQ0Pwb/aInEOeqqe8w6I6zXhGgtEGpH88+DTXFBbBpazZDK+5sV30E35 e6vg== X-Gm-Message-State: AOAM532HDaB4I5Qq8+vIYri8P3YFyqyR1JYzSWkUQcV4cinlGgTqpE6A UY3ZsPTuPIbBYSVFJZuErYizlUsa46u9BNrPold+yWswnlyHFXKRmE/JL/9g9LBuTKfUAeoz9ao 2VeJ3uV7ZRAe+ESfWhUBIx3cESxJo7OJ2voy6OrCYoV6zfVpOmPJ3XgCW+/aP X-Received: by 2002:aed:3366:: with SMTP id u93mr1036115qtd.255.1613594785038; Wed, 17 Feb 2021 12:46:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJxMbRLQXtnhXCNIMHW/s0UkO3hOcL2E4K5NIeBOPs0XHxCUYd5LV5uO57+v1rN/IHYI76Nexg== X-Received: by 2002:aed:3366:: with SMTP id u93mr1036087qtd.255.1613594784778; Wed, 17 Feb 2021 12:46:24 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id p25sm2354901qkh.79.2021.02.17.12.46.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Feb 2021 12:46:24 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Mike Kravetz , peterx@redhat.com, Mike Rapoport , Andrea Arcangeli , Axel Rasmussen , Matthew Wilcox , "Kirill A . Shutemov" , Andrew Morton Subject: [PATCH v2 4/4] hugetlb/userfaultfd: Unshare all pmds for hugetlbfs when register wp Date: Wed, 17 Feb 2021 15:46:19 -0500 Message-Id: <20210217204619.54761-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210217204619.54761-1-peterx@redhat.com> References: <20210217204418.54259-1-peterx@redhat.com> <20210217204619.54761-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" X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: F25662000D89 X-Stat-Signature: 4rsdx5n586cdh3dh1b8e4pdkoxmoui4q Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf18; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1613594787-271985 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: Huge pmd sharing for hugetlbfs is racy with userfaultfd-wp because userfaultfd-wp is always based on pgtable entries, so they cannot be shar= ed. Walk the hugetlb range and unshare all such mappings if there is, right b= efore UFFDIO_REGISTER will succeed and return to userspace. This will pair with want_pmd_share() in hugetlb code so that huge pmd sha= ring is completely disabled for userfaultfd-wp registered range. Signed-off-by: Peter Xu --- fs/userfaultfd.c | 4 ++++ include/linux/hugetlb.h | 1 + mm/hugetlb.c | 51 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 894cc28142e7..e259318fcae1 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -1448,6 +1449,9 @@ static int userfaultfd_register(struct userfaultfd_= ctx *ctx, vma->vm_flags =3D new_flags; vma->vm_userfaultfd_ctx.ctx =3D ctx; =20 + if (is_vm_hugetlb_page(vma) && uffd_disable_huge_pmd_share(vma)) + hugetlb_unshare_all_pmds(vma); + skip: prev =3D vma; start =3D vma->vm_end; diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 3b4104021dd3..97ecfd4c20b2 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -188,6 +188,7 @@ unsigned long hugetlb_change_protection(struct vm_are= a_struct *vma, unsigned long address, unsigned long end, pgprot_t newprot); =20 bool is_hugetlb_entry_migration(pte_t pte); +void hugetlb_unshare_all_pmds(struct vm_area_struct *vma); =20 #else /* !CONFIG_HUGETLB_PAGE */ =20 diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f53a0b852ed8..83c006ea3ff9 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5723,4 +5723,55 @@ void __init hugetlb_cma_check(void) pr_warn("hugetlb_cma: the option isn't supported by current arch\n"); } =20 +/* + * This function will unconditionally remove all the shared pmd pgtable = entries + * within the specific vma for a hugetlbfs memory range. + */ +void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) +{ + struct hstate *h =3D hstate_vma(vma); + unsigned long sz =3D huge_page_size(h); + struct mm_struct *mm =3D vma->vm_mm; + struct mmu_notifier_range range; + unsigned long address, start, end; + spinlock_t *ptl; + pte_t *ptep; + + if (!(vma->vm_flags & VM_MAYSHARE)) + return; + + start =3D ALIGN(vma->vm_start, PUD_SIZE); + end =3D ALIGN_DOWN(vma->vm_end, PUD_SIZE); + + if (start >=3D end) + return; + + /* + * No need to call adjust_range_if_pmd_sharing_possible(), because + * we're going to operate on the whole vma + */ + mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, + vma->vm_start, vma->vm_end); + mmu_notifier_invalidate_range_start(&range); + i_mmap_lock_write(vma->vm_file->f_mapping); + for (address =3D start; address < end; address +=3D PUD_SIZE) { + unsigned long tmp =3D address; + + ptep =3D huge_pte_offset(mm, address, sz); + if (!ptep) + continue; + ptl =3D huge_pte_lock(h, mm, ptep); + /* We don't want 'address' to be changed */ + huge_pmd_unshare(mm, vma, &tmp, ptep); + spin_unlock(ptl); + } + flush_hugetlb_tlb_range(vma, vma->vm_start, vma->vm_end); + i_mmap_unlock_write(vma->vm_file->f_mapping); + /* + * No need to call mmu_notifier_invalidate_range(), see + * Documentation/vm/mmu_notifier.rst. + */ + mmu_notifier_invalidate_range_end(&range); +} + #endif /* CONFIG_CMA */ --=20 2.26.2