From patchwork Sun Oct 30 21:29:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13025213 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85CC5ECAAA1 for ; Sun, 30 Oct 2022 21:29:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D0D36B0071; Sun, 30 Oct 2022 17:29:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 980B46B0073; Sun, 30 Oct 2022 17:29:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8489E8E0001; Sun, 30 Oct 2022 17:29:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 770446B0071 for ; Sun, 30 Oct 2022 17:29:36 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 222FF140A8B for ; Sun, 30 Oct 2022 21:29:36 +0000 (UTC) X-FDA: 80078907552.30.F269F93 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 89D682000A for ; Sun, 30 Oct 2022 21:29:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667165374; 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; bh=yGU8owWMZGuxGXrsN8bO4qgKTaN6U0bELfBGUQoRFws=; b=Z3EGMtK33SF95064dC6MHa6wX25eCLXfsZA9O/F7aVugs3lEETrGc7kPbJQn+byux1Obau TXd1SCJSPyIg4K5qQbgNP22lcy/pUnykLT789CHttuvQa6qA1nFBjEpwvcVKzdyqYpaDz1 ABZ7AMFwaX5gIosXN8HxtGXnbpdTXfA= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-13-AOXqWekjNxujA_T8fRMsUA-1; Sun, 30 Oct 2022 17:29:33 -0400 X-MC-Unique: AOXqWekjNxujA_T8fRMsUA-1 Received: by mail-qt1-f200.google.com with SMTP id u31-20020a05622a199f00b003a51fa90654so1130099qtc.19 for ; Sun, 30 Oct 2022 14:29:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yGU8owWMZGuxGXrsN8bO4qgKTaN6U0bELfBGUQoRFws=; b=RSWZuSUQ5JlnKGc3HuLPwTxFW6salvPR+x62OPDPI/jxoZfbmNmmLyLqL3GSnkTQ52 NJE+B68ShR2Q2tnyKKIBqQOU7CEyhNl1lMMBunkGPIWCC2wgO5HjOtCP6/XIQG3sqskK cPTSslpxvWUfpungPXEHj1q+qwxF0KvbKVHEBKfLAePQi0MBuBomcsYGR63uRpUIlaRh +Cdqx4EMEtWYbz2C27IkgEXUQjY+K4dyIabwfdnR8GIKVO9yYZA3ENMB06Ezre2JQBxj nVjbq5OyxbjroTctt6Qc2cKYDg/osxOB4jeq/axUbJU2qwieezOwSf6uFt20XoXOK92k Q/7Q== X-Gm-Message-State: ACrzQf3tS77FsPIcS5NjCh1zF2wZti9HozRKWZqdW1WABDNr9xpXzh9p yQEuC8eul7I92Kc/T9AwjHXmmC0SlzMuoLPC//RD0rPYxMEYmTwiTp5bxIA2oVPAW7i0fe6VYMJ K7u90GMlrN/Y= X-Received: by 2002:a05:620a:28d3:b0:6ee:cffb:21ef with SMTP id l19-20020a05620a28d300b006eecffb21efmr7071685qkp.123.1667165373005; Sun, 30 Oct 2022 14:29:33 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6VpOzrWwDdbUTzdAgTMoettQDq3IlJ9e+osrfrA0AlcEDzYeUQ/IFWJudGkNNmAaIjI3fHGA== X-Received: by 2002:a05:620a:28d3:b0:6ee:cffb:21ef with SMTP id l19-20020a05620a28d300b006eecffb21efmr7071670qkp.123.1667165372751; Sun, 30 Oct 2022 14:29:32 -0700 (PDT) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id a1-20020ac81081000000b003a4b88b886esm2654781qtj.96.2022.10.30.14.29.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Oct 2022 14:29:31 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Andrew Morton , James Houghton , Miaohe Lin , David Hildenbrand , Muchun Song , Andrea Arcangeli , Nadav Amit , Mike Kravetz , peterx@redhat.com, Rik van Riel Subject: [PATCH RFC 00/10] mm/hugetlb: Make huge_pte_offset() thread-safe for pmd unshare Date: Sun, 30 Oct 2022 17:29:19 -0400 Message-Id: <20221030212929.335473-1-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Z3EGMtK3; spf=pass (imf03.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667165375; a=rsa-sha256; cv=none; b=z4owyQlV1RWrn6UN39PuCqHdNa2OiVjqxlgcmK62sQSq01sOsUR/tEbz9QDJGLiHHyXjTr mrgnrO9JElxX+iSN6IvZyru44Re+5rKaalPUXfYBb1W0T8ssNe4wUUI2+dBfUkYxGgw/Hp D/9RpR2/skrWdBYVl3KFY/vwmR5pE8A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667165375; h=from:from:sender: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: references:dkim-signature; bh=yGU8owWMZGuxGXrsN8bO4qgKTaN6U0bELfBGUQoRFws=; b=MSdSorr8vIq0nlSZKzLcmK8c2uaNwFu5tnxzY2cHAevg0+Uu1kwQWqLK9oVdKYPeoJaBvW cGVbBQkdsareVMZA5CxN2C1Ljr9k3DWPlZm419zXa0y58QemyB6fxY0SeHnrIh/g0P6D2z 50EHJiJ/76XGuHIzPidRAAnUOLPi8/g= X-Rspam-User: X-Rspamd-Queue-Id: 89D682000A Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Z3EGMtK3; spf=pass (imf03.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: h4ofury76dos1xu8oqa119x8h8suscso X-Rspamd-Server: rspam10 X-HE-Tag: 1667165375-372253 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 can be seen as a follow-up series to Mike's recent hugetlb vma lock series for pmd unsharing. So this series also depends on that one. But there're some huge_pte_offset() paths that seem to be still racy on pmd unsharing (as they don't take vma lock), more below. Hopefully this series can make it a more complete resolution for pmd unsharing. Problem ======= huge_pte_offset() is a major helper used by hugetlb code paths to walk a hugetlb pgtable. It's used mostly everywhere since that's needed even before taking the pgtable lock. huge_pte_offset() is always called with mmap lock held with either read or write. For normal memory types that's far enough, since any pgtable removal requires mmap write lock (e.g. munmap or mm destructions). However hugetlb has the pmd unshare feature, it means not only the pgtable page can be gone from under us when we're doing a walking, but also the pgtable page we're walking (even after unshared, in this case it can only be the huge PUD page which contains 512 huge pmd entries, with the vma VM_SHARED mapped). It's possible because even though freeing the pgtable page requires mmap write lock, it doesn't help us from when we're walking on another mm's pgtable, so it's still on risk even if we're with the current->mm's mmap lock. The recent work from Mike on vma lock can resolve most of this already. It's achieved by forbidden pmd unsharing during the lock being taken, so no further risk of the pgtable page being freed. But it means it'll work only if we take the vma lock for all the places around huge_pte_offset(). There're already a bunch of them that we did as per the latest mm-unstable, but also a lot that we didn't for various reasons. E.g. it may not be applicable for not-allow-to-sleep contexts like FOLL_NOWAIT. I have totally no report showing that I can trigger such a race, but from code wise I never see anything that stops the race from happening. This series is trying to resolve that problem. Resolution ========== What this patch proposed is, besides using the vma lock, we can also use RCU to protect the pgtable page from being freed from under us when huge_pte_offset() is used. The idea is kind of similar to RCU fast-gup. Note that fast-gup is very safe regarding pmd unsharing even before vma lock, because fast-gup relies on RCU to protect walking any pgtable page, including another mm's. To apply the same idea to huge_pte_offset(), it means with proper RCU protection the pte_t* pointer returned from huge_pte_offset() can also be always safe to access and de-reference, along with the pgtable lock that was bound to the pgtable page. Patch Layout ============ Patch 1 is a trivial cleanup that I noticed when working on this. Please shoot if anyone think I should just post it separately, or hopefully I can still just carry it over. Patch 2 is the gut of the patchset, describing how we should use the helper huge_pte_offset() correctly. Only a comment patch but should be the most important one, as the follow up patches are just trying to follow the rule it setup here. The rest patches resolve all the call sites of huge_pte_offset() to make sure either it's with the vma lock (which is perfectly good enough for safety in this case; the last patch commented on all those callers to make sure we won't miss a single case, and why they're safe). Besides, each of the patch will add rcu protection to one caller of huge_pte_offset(). Tests ===== Only lightly tested on hugetlb kselftests including uffd, no more errors triggered than current mm-unstable (hugetlb-madvise fails before/after here, with error "Unexpected number of free huge pages line 207"; haven't really got time to look into it). Since this is so far only discussed with Mike quickly in the other thread, marking this as RFC for now as I could have missed something. Comments welcomed, thanks. Peter Xu (10): mm/hugetlb: Let vma_offset_start() to return start mm/hugetlb: Comment huge_pte_offset() for its locking requirements mm/hugetlb: Make hugetlb_vma_maps_page() RCU-safe mm/hugetlb: Make userfaultfd_huge_must_wait() RCU-safe mm/hugetlb: Make walk_hugetlb_range() RCU-safe mm/hugetlb: Make page_vma_mapped_walk() RCU-safe mm/hugetlb: Make hugetlb_follow_page_mask() RCU-safe mm/hugetlb: Make follow_hugetlb_page RCU-safe mm/hugetlb: Make hugetlb_fault() RCU-safe mm/hugetlb: Comment at rest huge_pte_offset() places arch/arm64/mm/hugetlbpage.c | 32 ++++++++++++++++++++++++++ fs/hugetlbfs/inode.c | 39 ++++++++++++++++++-------------- fs/userfaultfd.c | 4 ++++ include/linux/rmap.h | 3 +++ mm/hugetlb.c | 45 +++++++++++++++++++++++++++++++++++-- mm/page_vma_mapped.c | 7 +++++- mm/pagewalk.c | 5 +++++ 7 files changed, 115 insertions(+), 20 deletions(-)