Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp6912290rwb; Wed, 18 Jan 2023 10:55:37 -0800 (PST) X-Google-Smtp-Source: AMrXdXvZQXhaBemW/iJC95LTnUmuqzf/xQL32ASR7v+Ce1E+EXX42zD1CmVoB4SVS24r4JlscFwU X-Received: by 2002:a17:906:15d2:b0:84c:cf42:e16 with SMTP id l18-20020a17090615d200b0084ccf420e16mr10293015ejd.1.1674068136796; Wed, 18 Jan 2023 10:55:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674068136; cv=none; d=google.com; s=arc-20160816; b=iFpDDkQ6yDteR0+uy0Car3S3nJW4mmhZN2VvfTp2tMp00UE0B0p0KBKdxvj0SrRwBn HL2wmVnwSAqlgdEiq7xrLc4l+kMsuzeAAxALiVmNGGmCVz5p8GhPKhC+LsB88KPtJcvS jxFY88Qc0bWOwdtvxg64QhziOim+nHreaNahIsGXiD6nXYn5z6HYdFZyVNHRmfCutHcc LEbGpSZE6F0Ncmf0cPF4VFKvZ5gkyHbwU6uWmwhGTji7Oixo5WFBSGkeEHUb7mupZh42 Ty8zil2B3ApharIan8o+eyP7hNoDUHcs6DaaqawlQJRgtOy5b6ow1QJZughqokXAuAHe AP5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=1ggn1Gn4xsGfWkGoz3fdeEx12ls41kiX+Bcgea51Xy0=; b=xwzesyRxwrVhSuUeDwJkV+zmr3TYGc59ncHMR1x48zJ6dxZNFim2+g8GnGJzmL2HBJ ivy0f+wM1yKVhhzkb+BllDf9mj3yN+ksB2/eSJB3zgfcQzF0wpjUrmEifXlWYsWA1eJI oJTYddsN3El83HOGng9pu9tDA2Gl2NJaBV1k8ZVi5w6EjNTBrh8KA5g3Qc70I7xJjiHH 0/18tEGRpj9e+I24WA7GuaqYiMQnMWvVUxfne/H3sk2HN1cYWYDPwKUtTu5YWVAZ5kNp VHeLd0b7k9yZnFghUT4+ai8UI10bTxWt6/LUBD9lBjXJJ3W5FFofSgJRRb0QJV4B68/B GSAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Ea4CJI0T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i3-20020a1709064fc300b00867779a2fddsi23677921ejw.188.2023.01.18.10.55.25; Wed, 18 Jan 2023 10:55:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Ea4CJI0T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231268AbjARRn2 (ORCPT + 45 others); Wed, 18 Jan 2023 12:43:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231319AbjARRma (ORCPT ); Wed, 18 Jan 2023 12:42:30 -0500 Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0EF439291 for ; Wed, 18 Jan 2023 09:41:52 -0800 (PST) Received: by mail-yb1-xb34.google.com with SMTP id p188so39063710yba.5 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=jAgK4IwUqR5BenqV0EN7YhOM2BxhTtu8igsge1hh13a6KKHahhyCi0jdmOrR4e8uSX SgwqiUyCScwEUh4vAn8LLNpt7bFOK3k+aOkb2/ixH58FLGVYunBv5LLY4ZykuPhap7cb UOMLEe952GSCtfi32p+7YbxiyxyjVV0xI2effIXxOff826eVNung8zYOX+9CeH3fdDgd S0RjQRRNjmyXekG/LsTpG1uRb78UY4HGDWrr8UMZXjtezn4UIx0VA4BLQEwwcmUnk/lr 8mqMujaFBra0gHwG94B3ZtNMrAPJ7NSJ7Eo4YwGji/E5TuQ+dRuUtNIXJ3I7SQ9O8oR6 xF8g== X-Gm-Message-State: AFqh2krD+d4U0osWKOy+fIgPizmWrTj5GMnF023rql5KXd902X2+NaFw 9m7anX5/pzHCFCbm/Fla7wmN+a/tzN0q0yq9DfFGLw== 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-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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