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 91C40C433DB for ; Wed, 17 Feb 2021 16:31:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2BDBB64DFF for ; Wed, 17 Feb 2021 16:31:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2BDBB64DFF 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 A320D6B006E; Wed, 17 Feb 2021 11:31:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 943AC6B0070; Wed, 17 Feb 2021 11:31:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6AE086B0071; Wed, 17 Feb 2021 11:31:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0076.hostedemail.com [216.40.44.76]) by kanga.kvack.org (Postfix) with ESMTP id 542C46B006E for ; Wed, 17 Feb 2021 11:31:22 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 16DF79893 for ; Wed, 17 Feb 2021 16:31:22 +0000 (UTC) X-FDA: 77828300004.20.hot99_02139c52764d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 2B5FD180C1476 for ; Wed, 17 Feb 2021 16:31:15 +0000 (UTC) X-HE-Tag: hot99_02139c52764d X-Filterd-Recvd-Size: 6897 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf21.hostedemail.com (Postfix) with ESMTP for ; Wed, 17 Feb 2021 16:31:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613579474; 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=ftGXzlNwaFnDz8kDiuMs+kiPJ3W722AFkGJD2WRAqcY=; b=hv9Y4w0PMfmGGm2uTCpDbHHfA0P13USHjiu9Gi/YrOzledvCAseMCVFb67Um2vXLshhJkQ dKhSSYDjrRrMxn9N73mQlVrXpDeHYDlsHsKSeMtLoO5qIGwIOAlU+uUOsC6aC2xefDV5PI 9kE/4IGz1NhW6aS11+sYyBB/TJZbfwQ= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-515-TJSJ_JYYPu-SX7BmpWaztQ-1; Wed, 17 Feb 2021 11:31:10 -0500 X-MC-Unique: TJSJ_JYYPu-SX7BmpWaztQ-1 Received: by mail-qk1-f199.google.com with SMTP id k185so9861825qkb.17 for ; Wed, 17 Feb 2021 08:31:10 -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=ftGXzlNwaFnDz8kDiuMs+kiPJ3W722AFkGJD2WRAqcY=; b=U6nHIqV6k4FbrBrN5BzHRJnIQu2u3p3qj94WQZCLdR1kstE7kmBMN2oNfTBZ0M13j3 iUFe7y4UBuS05PXlFAJZexvuZBaxWHL7oHaYwRuL5jiChgqgScGitDvhpOaFNNZa88H+ 1vpSTrJRWC1H3ESmMsi7vCkVNFWHe/EAXcfqqIxSwtpuFFy2oV/RiMYQSpX0j6Db2tCJ gQWPJySZLIzci64CAzFOLoTwVjGORyZE5tOMBUsIP3FYfPnKo7rllu3I+ob5iAlt7dkR BiOT4lBh+2YSchbuTHQHmb/mbe85CRzpZdc1dFbbe/BkZfENgL5FwjUg1vFxYCXWC4w/ V1Gg== X-Gm-Message-State: AOAM531JZ+U66RBZTeLERczCyjoXFRxF4LMTaSuST8QN1nRDT8Az283D 70je5vj+0OOxRXQQYBP3rJqnQgE5CRdEQBf/I6aAiMX9S/CeC4jZ7i2EBOXocO0ViKXtkNcQdhk Whxn9XR0Scqw= X-Received: by 2002:a05:622a:54e:: with SMTP id m14mr101661qtx.128.1613579469950; Wed, 17 Feb 2021 08:31:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJztC/d/cZ0fIo7SdGnxYzSnhkW+w3i5etEZbDCMiMqGgbvRAOf3wv7D+bDWPxCKQ3NiCG4Z/w== X-Received: by 2002:a05:622a:54e:: with SMTP id m14mr101606qtx.128.1613579469720; Wed, 17 Feb 2021 08:31:09 -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 z37sm399902qth.87.2021.02.17.08.31.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Feb 2021 08:31:09 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: peterx@redhat.com, Axel Rasmussen , Andrew Morton , Matthew Wilcox , Mike Rapoport , "Kirill A . Shutemov" , Andrea Arcangeli , Mike Kravetz Subject: [PATCH 4/4] hugetlb/userfaultfd: Unshare all pmds for hugetlbfs when register wp Date: Wed, 17 Feb 2021 11:31:02 -0500 Message-Id: <20210217163102.13436-5-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210217163102.13436-1-peterx@redhat.com> References: <20210217163102.13436-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" 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 | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 894cc28142e7..3fbdacc25ff4 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -1190,6 +1191,59 @@ static ssize_t userfaultfd_read(struct file *file,= char __user *buf, } } =20 +/* + * 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 =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 +} + static void __wake_userfault(struct userfaultfd_ctx *ctx, struct userfaultfd_wake_range *range) { @@ -1448,6 +1502,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; --=20 2.26.2