Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1368714pxb; Thu, 28 Jan 2021 14:55:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJwcDo6Fme0KklFKrZ+8Dk6xT6OiBSczW2ezlUWGS5hICQAXTNwn4DlNxRz5NMNISafCkF27 X-Received: by 2002:a17:906:7f98:: with SMTP id f24mr1710374ejr.75.1611874532535; Thu, 28 Jan 2021 14:55:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611874532; cv=none; d=google.com; s=arc-20160816; b=JpMr4vPSi0TU6u6vkFWX+aF2SRbdAxv7e6tuih5vE5nh4UWfPh2wGBNzLAleLc8slE TezJ5K/lY+2vjqEZPXFrsEXDC+8xOZINIY0R5qNdRblwcFXqzWEjUSeg8CVpLL2l4g9/ bx4yVMRnFAWtQ0+VGsBroRFA2bz9iix6MHxrMPS8grHXo9kdN+GhlqI89e4HBcZmkeC3 n5p/lT8oYNp7R/8G3z+kCY2b1UrUCaUJQbx2CPxEhY3xiO0vEZBa39RucjKXNWf/7Svj s9EbCpqwTFZe5/42PNr2kB7TD8d5Q6EBh85U0uFL1cUmbL7MA7HiEunnSj+8NUEzQwoe A9Yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=1emyqdFZVCXAT6rTF3qK5V6G85egQi2sq8wmP9IwMVM=; b=bpRL1T1iZphys00QdV9B/afRGhqEEJ7H2x9DIXOWVpB+Nq995YC1BAK4BpkCH3Li6O s9vgiEmScN16V1rmw7PAIRFoIoj3iTisihio0dglWJ5iIjhjGDEPmzq6WZ1l8zZqg+x0 uqx5F/UNH0o2/BTiIvxCvYvX9KvJ390TjW8D30OXrb77Tlv8lWIxQXS77bjZ0kfkRcQE 1OnP+xe6ifGmz7sKSlZa9KXLnDZmmZEklFg5o73Je/P6V7lSJE/8SWis69TgSAYdnOSw ZUUEyiJbv2VUaFX7QLiG+rd5/1DJIQqiHd8WU2lQtqtAFSyw7VdWpI9ykTbC/CbJ+Dzb 9Kkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=MXK6OLFr; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v19si3601827eji.75.2021.01.28.14.55.08; Thu, 28 Jan 2021 14:55:32 -0800 (PST) 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=@google.com header.s=20161025 header.b=MXK6OLFr; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231633AbhA1Ww0 (ORCPT + 99 others); Thu, 28 Jan 2021 17:52:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231700AbhA1Wuq (ORCPT ); Thu, 28 Jan 2021 17:50:46 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3ABD5C06178C for ; Thu, 28 Jan 2021 14:48:34 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id y34so4876046pgk.21 for ; Thu, 28 Jan 2021 14:48:34 -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=1emyqdFZVCXAT6rTF3qK5V6G85egQi2sq8wmP9IwMVM=; b=MXK6OLFrEwQXO2YXIDCKrYrqQU0p23E8NbH13hMb1MKTaIDOmtNbiJXDynuZXIWU+J CZz0tqM1e1C0hVXGiVA16Y2U3CW7cDG8ob9xyuQi6o2VYUXDFRaZZ+Y0NbagMmtBo1Yg UbqJm7XQqA6X98024gvHfReNBimEqhXmvKE9z4pxe+5o1/z3P+GiYOOJbojW1fd+DQmp zQG+GtIlAQWMFXhFx1Lrune47/qQkHdKo8goSTH44aR2uPoCtDNUKSMRCrXcG8JdKy04 IQ4rFBC3uNNzpmC1Ax2QVykwy6PKTdrqeABIiLdfL71vDtgSeAdvuJpuO0CwbGP/NWKy SC1A== 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=1emyqdFZVCXAT6rTF3qK5V6G85egQi2sq8wmP9IwMVM=; b=Q08Jk46KXvy1OnjZJksPYDbiD7cRM0c5lnRY+0+KCV5rzp7ZmDMp9cFM+3CLMaypIR +XcgIojlu4k/1gCyxh9EU8eupc0CfPGg6XaiMnDePBDErHGyF06HOXh/6PjZn2oMX4k7 ftZNRFC6jHf/XvcqBj8L4JMhlO6cs4AVlwZ78MB8QEGASH4vUx9X5DNeAJiv9it2H58W oAsK0vUPvdZrWlWeIrATdhW/YpHpj+c4ibyYa+R/PNJnp7/ZvBeDL8rxZJBkclh4kNCG BHhFxFPH21lhB01Gch/znIc/O1xv0C9kHFs4pC+1UVxWy+cSZRTGKJlFh8GqEl5+Xa5V h+Bw== X-Gm-Message-State: AOAM533e0ulRNYRqQ8KbrlFNecNVwl0qYXPQY7Ic/WcgPwWEGOGyjWwm yPIqk4za4RNnfF0gMqMyZwvR9OtVFDCZJPIRSd0M Sender: "axelrasmussen via sendgmr" X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:f693:9fff:feef:c8f8]) (user=axelrasmussen job=sendgmr) by 2002:a63:fd10:: with SMTP id d16mr1520502pgh.333.1611874113566; Thu, 28 Jan 2021 14:48:33 -0800 (PST) Date: Thu, 28 Jan 2021 14:48:14 -0800 In-Reply-To: <20210128224819.2651899-1-axelrasmussen@google.com> Message-Id: <20210128224819.2651899-5-axelrasmussen@google.com> Mime-Version: 1.0 References: <20210128224819.2651899-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH v3 4/9] 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=20Koutn=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 , Oliver Upton Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 45 ++++++++++++++++++++++++++++++++++++ include/linux/mmu_notifier.h | 1 + 2 files changed, 46 insertions(+) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 894cc28142e7..2c6706ac2504 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -1190,6 +1191,47 @@ 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; + + /* + * 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) { @@ -1448,6 +1490,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) -- 2.30.0.365.g02bc693789-goog