From patchwork Wed Feb 10 21:21:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12081793 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_ADSP_CUSTOM_MED,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 BEC14C433E6 for ; Wed, 10 Feb 2021 21:22:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5E6FE64ED0 for ; Wed, 10 Feb 2021 21:22:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5E6FE64ED0 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6CF296B0073; Wed, 10 Feb 2021 16:22:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 65BB26B0074; Wed, 10 Feb 2021 16:22:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 523AE8D0001; Wed, 10 Feb 2021 16:22:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0212.hostedemail.com [216.40.44.212]) by kanga.kvack.org (Postfix) with ESMTP id 34D3B6B0073 for ; Wed, 10 Feb 2021 16:22:16 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E8771F02C for ; Wed, 10 Feb 2021 21:22:15 +0000 (UTC) X-FDA: 77803631430.27.fact12_2e0585b27613 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin27.hostedemail.com (Postfix) with ESMTP id C86063D663 for ; Wed, 10 Feb 2021 21:22:15 +0000 (UTC) X-HE-Tag: fact12_2e0585b27613 X-Filterd-Recvd-Size: 7565 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) by imf08.hostedemail.com (Postfix) with ESMTP for ; Wed, 10 Feb 2021 21:22:15 +0000 (UTC) Received: by mail-qk1-f201.google.com with SMTP id h185so2747187qkd.4 for ; Wed, 10 Feb 2021 13:22:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=GZiDGzBZc6CdQco6utSqJxvMDhSmOX0vGMrGoDVipFA=; b=CSNZplzMVyec7GHEIAFJ1eRCsQNhOq/fj6tcy3OeZYMxVQ0KVyYmb0dTEK7zKHFTGS yebuUuuasp9KvEVUdZL5opsPRGziEruE1yuW80GOuHJd8OTK/AQMO0URaaXVNWOY9yE4 XwdwtnodywYIMXJ5E9ykKtwlxvJbCfYQpihPabgZwv1gJm/69Elh2UqDmzr/MRnIhC89 RrBQfdi3VBH3neG0sO+qFV+9OgWaH109CixY6+ca6R1+eIxQFqRkHSIbkvO6G12QDnrZ EclOhkjUfZdpL44LIIw/9EUTZdEtY13TGdzOUzevn0BlDx3rM1E1JbTX41JR1mf0SBsE zYoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=GZiDGzBZc6CdQco6utSqJxvMDhSmOX0vGMrGoDVipFA=; b=S11U+8MhGLflQwAwVgU2LWc7/y6hthcC7Bvit0IM3fnI0hOZGx55cp8X3NCOcRYrkM p6UFRAhv+AnmNSnXDC3LGq6mR8l1+vm9YA+wDT5tAp38CXRCvyrksTjkP5laJ6fobkPq LOxdJzp/jt5m3r7GKaBSLNf0HLd83QhbnZZHxhvbYESyE+0EIwEvcCJ0wtLEmzkOBXu+ EInidckzPEUHCnYOEQx5rcoaiWuPPolaCNgjOMHmuS7uVm1Xw3VTfJm4VKn0YFkC1lKp 5gySdyZEGpYAJKK3QDysQHcfFeQN/AdWyaZjjdOaPJyCETYJn4ACki2nS2aAkTXgZPl+ taIA== X-Gm-Message-State: AOAM530vBAEJKnieJnFx5LKQAbptFVK+vOVoWxDrfInuxn0LjPRtWu9t WLDZgpMIXvn7xTQ8+4e/73Et45x8qGbCDQhbdeI7 X-Google-Smtp-Source: ABdhPJyeUvHSePNxBk4zXDwQilv7B/ls1YQ+N4EzdaCrrLILHIqkJGtRKVUfnXXG71oYRXMNeelVp+MO89+DHWJBPIcN X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:94ee:de01:168:9f20]) (user=axelrasmussen job=sendgmr) by 2002:a0c:fec3:: with SMTP id z3mr4869971qvs.52.1612992134367; Wed, 10 Feb 2021 13:22:14 -0800 (PST) Date: Wed, 10 Feb 2021 13:21:54 -0800 In-Reply-To: <20210210212200.1097784-1-axelrasmussen@google.com> Message-Id: <20210210212200.1097784-5-axelrasmussen@google.com> Mime-Version: 1.0 References: <20210210212200.1097784-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH v5 04/10] hugetlb/userfaultfd: Unshare all pmds for hugetlbfs when register wp From: Axel Rasmussen To: Alexander Viro , Alexey Dobriyan , Andrea Arcangeli , Andrew Morton , Anshuman Khandual , Catalin Marinas , Chinwen Chang , Huang Ying , Ingo Molnar , Jann Horn , Jerome Glisse , Lokesh Gidra , "Matthew Wilcox (Oracle)" , Michael Ellerman , " =?utf-8?q?Michal_Koutn=C3=BD?= " , Michel Lespinasse , Mike Kravetz , Mike Rapoport , Nicholas Piggin , Peter Xu , Shaohua Li , Shawn Anastasio , Steven Rostedt , Steven Price , Vlastimil Babka Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Adam Ruprecht , Axel Rasmussen , Cannon Matthews , "Dr . David Alan Gilbert" , David Rientjes , Mina Almasry , Oliver Upton 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: From: Peter Xu Huge pmd sharing for hugetlbfs is racy with userfaultfd-wp because userfaultfd-wp is always based on pgtable entries, so they cannot be shared. Walk the hugetlb range and unshare all such mappings if there is, right before UFFDIO_REGISTER will succeed and return to userspace. This will pair with want_pmd_share() in hugetlb code so that huge pmd sharing is completely disabled for userfaultfd-wp registered range. Signed-off-by: Peter Xu Signed-off-by: Axel Rasmussen --- fs/userfaultfd.c | 48 ++++++++++++++++++++++++++++++++++++ include/linux/mmu_notifier.h | 1 + 2 files changed, 49 insertions(+) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 0be8cdd4425a..1f4a34b1a1e7 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -1191,6 +1192,50 @@ static ssize_t userfaultfd_read(struct file *file, char __user *buf, } } +/* + * This function will unconditionally remove all the shared pmd pgtable entries + * within the specific vma for a hugetlbfs memory range. + */ +static void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) +{ +#ifdef CONFIG_HUGETLB_PAGE + struct hstate *h = hstate_vma(vma); + unsigned long sz = huge_page_size(h); + struct mm_struct *mm = vma->vm_mm; + struct mmu_notifier_range range; + unsigned long address; + spinlock_t *ptl; + pte_t *ptep; + + if (!(vma->vm_flags & VM_MAYSHARE)) + 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_HUGETLB_UNSHARE, + 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 = vma->vm_start; address < vma->vm_end; address += sz) { + ptep = huge_pte_offset(mm, address, sz); + if (!ptep) + continue; + ptl = huge_pte_lock(h, mm, ptep); + huge_pmd_unshare(mm, vma, &address, 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 +} + static void __wake_userfault(struct userfaultfd_ctx *ctx, struct userfaultfd_wake_range *range) { @@ -1449,6 +1494,9 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, vma->vm_flags = new_flags; vma->vm_userfaultfd_ctx.ctx = ctx; + if (is_vm_hugetlb_page(vma) && uffd_disable_huge_pmd_share(vma)) + hugetlb_unshare_all_pmds(vma); + skip: prev = vma; start = vma->vm_end; diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h index b8200782dede..ff50c8528113 100644 --- a/include/linux/mmu_notifier.h +++ b/include/linux/mmu_notifier.h @@ -51,6 +51,7 @@ enum mmu_notifier_event { MMU_NOTIFY_SOFT_DIRTY, MMU_NOTIFY_RELEASE, MMU_NOTIFY_MIGRATE, + MMU_NOTIFY_HUGETLB_UNSHARE, }; #define MMU_NOTIFIER_RANGE_BLOCKABLE (1 << 0)