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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25B3FC32793 for ; Wed, 18 Jan 2023 17:41:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BB166B007B; Wed, 18 Jan 2023 12:41:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 96BB16B0082; Wed, 18 Jan 2023 12:41:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80CDD6B0083; Wed, 18 Jan 2023 12:41:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 6EF0D6B007B for ; Wed, 18 Jan 2023 12:41:54 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 50649408C6 for ; Wed, 18 Jan 2023 17:41:54 +0000 (UTC) X-FDA: 80368637748.11.D2A31C5 Received: from mail-yb1-f182.google.com (mail-yb1-f182.google.com [209.85.219.182]) by imf03.hostedemail.com (Postfix) with ESMTP id B359320016 for ; Wed, 18 Jan 2023 17:41:52 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ea4CJI0T; spf=pass (imf03.hostedemail.com: domain of surenb@google.com designates 209.85.219.182 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674063712; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1ggn1Gn4xsGfWkGoz3fdeEx12ls41kiX+Bcgea51Xy0=; b=JpMeiI4NFkdDJ328O942ADj8PJbj5xBmfMrm9hAcNfod+2TZ1N2RAT633p8O/D9n1KsTuM a1b7wwP/kFd/Dnlp4q89aHPswyz1svya/2W7DRgnqnSybHh5gEuGZJuGIMorIUjs7Z9Ulr Qrhc97/UiUpz21LYRusSyKfqbMPr5Uw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ea4CJI0T; spf=pass (imf03.hostedemail.com: domain of surenb@google.com designates 209.85.219.182 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674063712; a=rsa-sha256; cv=none; b=ewK6tsLWGae72pBkbjt15RGOx0PV2iHiPoc/efNwadmlJ/BOhMwXOQT2hitGCDWL185V85 o99WeSZf7fG6IzAtuI8Wt04IwJO4AaLGYfeHAmy2IBFRNq1URv/aeAYGzJO+Tc1a7o97LX JKJnOg7jueFUPw8In12ciBnD89yL9J8= Received: by mail-yb1-f182.google.com with SMTP id 203so39025403yby.10 for ; Wed, 18 Jan 2023 09:41:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=1ggn1Gn4xsGfWkGoz3fdeEx12ls41kiX+Bcgea51Xy0=; b=Ea4CJI0TSp0VpR3KYCvzeCipTBJjBVuqbyf4rfd+Jv1neOPo9p/t/BduqEOInn0y3C ofgrWDbDQgEP2g8l1MvXXGn/QX4fbJhNvQffDX/t+trziL1O5MKSR5kfDMjyLWGQG3EA vJQ1epz0AZ7pySAVPAR4XXXKslbRKDY9rkCVemC3yTtudRIBFHB5aYgnxkMTfX+a315y +MCEhYZQkWH78CcxuVrSGlmVx3JBTtYjrsCR/65yYqhEc8iYg6n/Mrymr9pNuAmPcW8Y 0VNTZ6B+ZwgCycamWGYsxLS5X/qCdfxOOX3rFQTg4BAZq6XeZYwYYTJPt8RKmXpMgxru ZHYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1ggn1Gn4xsGfWkGoz3fdeEx12ls41kiX+Bcgea51Xy0=; b=b0/bZwKGzBPW1TEFCnAAQtMNQrWPbB2V1eDxi0kMBB8fiLM61eagiCjaVgchUKBRmW eUUaUBNx958neEAaWrrgaw3e4IKF5vzWnSlOyslmsf1SWhW69JMNsySfTkFCHEQB9Qq9 mFLTrGC6YqS5L1IWBVKYoBGeK5BO8yVj6vPf3rTbc0JoSHrIwud/ZX0vcYiF3RJhdxEh AVWjgsmXuVou0MqUof3RXNKQjjJPOHFeTtFwfciKGohP8J1Gu791CbtJXN1qFcHXLI8v w7cxURuR38UhdTIeG8QhrIefZL54ruV8jrK6WmwIijNY7CvJcPNPl0fGb0Y2rY8+FfoU iSpg== X-Gm-Message-State: AFqh2koz9w8u527IV6e4a09UF+UwN5b1BVp5BpShAdlpWT47RfTu8wT8 Pad4pVKhQ4Ggrwu3yuelM+zAiprMSZlnpiNRzAveTw== X-Google-Smtp-Source: AMrXdXumY8p2sU8BkGgo7Lctu1jBI5wsBXEgb6BSMszQ1CFMqwRFSLoQ/TGhweY0s84T2kXVvFhuWQ3VSTGzwjugGTw= X-Received: by 2002:a25:9801:0:b0:7d5:b884:3617 with SMTP id a1-20020a259801000000b007d5b8843617mr745969ybo.380.1674063711547; Wed, 18 Jan 2023 09:41:51 -0800 (PST) MIME-Version: 1.0 References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-19-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Wed, 18 Jan 2023 09:41:38 -0800 Message-ID: Subject: Re: [PATCH 18/41] mm/khugepaged: write-lock VMA while collapsing a huge page To: Michal Hocko Cc: Jann Horn , akpm@linux-foundation.org, michel@lespinasse.org, jglisse@google.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, peterz@infradead.org, ldufour@linux.ibm.com, laurent.dufour@fr.ibm.com, paulmck@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, hughlynch@google.com, leewalsh@google.com, posk@google.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: B359320016 X-Stat-Signature: 776ht4rsmyjbo6mgk94un5hnphc9ju5t X-Rspam-User: X-HE-Tag: 1674063712-29222 X-HE-Meta: U2FsdGVkX1+FwfdtWRFyE3bIxW63SdXadSpv1cC+SMKiG+PRp4GuMzpdi2+d6n3x2145XZfjjZHgwkudEnh3fKScdpgFcIDLsK38cPv/ok773rnQoV2By7fm2kANAKyTJ3OmSUS/0oVhQHb+cuD8LPcdrbso/IPbx3oeEwtvbH4UaQXeqbFS9beNKs1bx3MSP0JBDevH8M+wMYYlnPRp8VBqQZSsKcXRe00ccTG6YHoNVW/nFDCip8yHednQQHd1i9coJZHu0my4/KSNdyxhx2pJ6iDyQUlH7tr2Kc9KzSLjZFQsBxHdvZ08oWSkOszSzUa3QGoXLh1Hzvd16mpgVQZykH4npUBL6Xy7yOsll7niWLVx/UQHxWDqGy0gUi4PrKUjQrVmHTHYoBnyr4KncljAQVrEikxasxd1pyyPMd/kVxO2q7ckQg8xDxa41QLAEaF0fxyIQiX/R3k7+Zg6XwXnZThAY/btXIpjTfoMeswpJ+qC1OMFFKbfNgRiobN5UWzEX99KWCZUV6aoFTEvZuA82d2KXE3AIJlwPVgvk83GmeF7z+RGIZXMqPAV2fwYwuXEaF4DXw4FGS/EbQUCowC3HzzwM5O9kdwlYZJim86S8HYjN7848EiCjAcccdUMGgU/S8aEhT3G5l/EvLlHYBuxMt1V1HO1mwTEvBlE7I8C4mnannmvolx0AUzO88+70j4tr7oKpY3+qnXhWiRHkBSVTPlNWP1QXJ8B/NZpuzf6B01Pu6H63E+GHsYRJxAMSfixU6LE1XN170fvfhY4ytrNJgZU25UytqOEOK1TlnltuxyvyVHoLi7NZ+RabFLXZi5M06LgGryK3rolj5Iq6OaJY46DxC80hCQh49Q7ODz928fBj3hMIKW+F1RteuNJtGr5uMFfdsfh8NhNBa4h1/2Yh8Zw/c84vFYDQwMAKcndHpPoN3tevHzpPrFjfEx3OKS9ziu3ls0i1IjbQmA IFwkEw/a x7Lx97iohXJKNRfE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000009, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Jan 18, 2023 at 1:40 AM Michal Hocko wrote: > > On Tue 17-01-23 21:28:06, Jann Horn wrote: > > On Tue, Jan 17, 2023 at 4:25 PM Michal Hocko wrote: > > > On Mon 09-01-23 12:53:13, Suren Baghdasaryan wrote: > > > > Protect VMA from concurrent page fault handler while collapsing a huge > > > > page. Page fault handler needs a stable PMD to use PTL and relies on > > > > per-VMA lock to prevent concurrent PMD changes. pmdp_collapse_flush(), > > > > set_huge_pmd() and collapse_and_free_pmd() can modify a PMD, which will > > > > not be detected by a page fault handler without proper locking. > > > > > > I am struggling with this changelog. Maybe because my recollection of > > > the THP collapsing subtleties is weak. But aren't you just trying to say > > > that the current #PF handling and THP collapsing need to be mutually > > > exclusive currently so in order to keep that assumption you have mark > > > the vma write locked? > > > > > > Also it is not really clear to me how that handles other vmas which can > > > share the same thp? > > > > It's not about the hugepage itself, it's about how the THP collapse > > operation frees page tables. > > > > Before this series, page tables can be walked under any one of the > > mmap lock, the mapping lock, and the anon_vma lock; so when khugepaged > > unlinks and frees page tables, it must ensure that all of those either > > are locked or don't exist. This series adds a fourth lock under which > > page tables can be traversed, and so khugepaged must also lock out that one. > > > > There is a codepath in khugepaged that iterates through all mappings > > of a file to zap page tables (retract_page_tables()), which locks each > > visited mm with mmap_write_trylock() and now also does > > vma_write_lock(). > > OK, I see. This would be a great addendum to the changelog. I'll add Jann's description in the changelog. Thanks Jann! > > > I think one aspect of this patch that might cause trouble later on, if > > support for non-anonymous VMAs is added, is that retract_page_tables() > > now does vma_write_lock() while holding the mapping lock; the page > > fault handling path would probably take the locks the other way > > around, leading to a deadlock? So the vma_write_lock() in > > retract_page_tables() might have to become a trylock later on. > > This, right? > #PF retract_page_tables > vma_read_lock > i_mmap_lock_write > i_mmap_lock_read > vma_write_lock > > > I might be missing something but I have only found huge_pmd_share to be > called from the #PF path. That one should be safe as it cannot be a > target for THP. Not that it would matter much because such a dependency > chain would be really subtle. > -- > Michal Hocko > SUSE Labs