Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp5578372rwb; Tue, 17 Jan 2023 16:02:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXuzIUkEKZLFH/hwDFZlJZm+2lB5pgQ0PiNX3pTc9gxQMu9G4Tx8lJ4APosEhT70Q/12539+ X-Received: by 2002:a17:907:2395:b0:871:629:227c with SMTP id vf21-20020a170907239500b008710629227cmr4089924ejb.66.1674000152580; Tue, 17 Jan 2023 16:02:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674000152; cv=none; d=google.com; s=arc-20160816; b=xWDERaNZLsHc7PM3ZlkKwNTznrv/NqHNGB0K2fcxvhAdsDaQRKQxlRJtv9xh68D4Fe E/9YYNknYHoOnjI+hPljXpdLM8sqj+HCYSVgt9LpVLSyZj9BgqZVZJXT89fMt2/r7Rqx 237g2IaT2Wn4TxVkbvqg/9aWAKnSwahudALYG5hsM5Koh34oFLA8Aywv1Hkuw+MrdoVT uXxIkJ6/v44KP6C0z90u+pm0j98qJMvQb56Pc+BzwY7Y6Y8PB80NV1mM0lPoYJfb1ib3 YaXdRXtLZ5bYvFmh0dE3uXxaD9ys+AVT+D3ESHtZUCbrF54VkQydqLTU844vjRP17Vst zzqA== 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=dwj8xbw0bnEL25Qq4gmjxS00IOeAjG6GNQxAdbnWO2A=; b=a7mLEAF/H2VeCMFlmSEhi+ejsk9Us8TGD8hUPjHNpXJ8EzdLyy/J3lyOQbbcCR69dm mNmwowC+MAoKT025ScHZizybz/lsAyxVI1P0Pu/arqO9Tltyc4PmhcGVJeHKTaYjzvBf RHa32S48RP8DoImuYU3FhK0qdYFm0MQF1Q3Ij6YJGGcFOlLnVQak2wXV2+6S9ht+r3Ql YRHdrFk2gaaVm7AMJTk4kvP0D1XrJ+vGXlQdK+vcevwZ5/8kFJoSrUfL3h0YxGzcJml+ 22CSNi9D6Kh2YLrAdXC2PXyHOvPCC4G/R56vdhV0spm1+oABqAYNcF8SLTR6zS6L/ZMI 9Chw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Dz70IjXM; 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 wu4-20020a170906eec400b008269916aa9dsi35428159ejb.160.2023.01.17.16.02.21; Tue, 17 Jan 2023 16:02:32 -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=Dz70IjXM; 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 S229819AbjAQXSg (ORCPT + 46 others); Tue, 17 Jan 2023 18:18:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229837AbjAQXSB (ORCPT ); Tue, 17 Jan 2023 18:18:01 -0500 Received: from mail-yb1-xb30.google.com (mail-yb1-xb30.google.com [IPv6:2607:f8b0:4864:20::b30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35C1153565 for ; Tue, 17 Jan 2023 13:05:28 -0800 (PST) Received: by mail-yb1-xb30.google.com with SMTP id 203so35692992yby.10 for ; Tue, 17 Jan 2023 13:05:28 -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=dwj8xbw0bnEL25Qq4gmjxS00IOeAjG6GNQxAdbnWO2A=; b=Dz70IjXMbGEDAEUfGYkAjARcQ1aaCB4C/yV4J6ap8RocWxiLCyWOuVOLCBBQakbozM 9ThYUJ2KetdMGb1OtZwAPbQdTeKDKGOTXmZPv7NRZfHpQ/EtRgE8Ld6EbmyV5wGLlFfD AmFXyZvQdUOwQq4pgbxz9auvXTWcfQa87IMGGXpePOrO4iwYcAZ75BGEQVE7OIxHdtIF iD+cVHzpbMidwELSER4I/Iw3wY397BVq3STlFx+qXjWhSLJBTofB3gwY/gj9toQTJM4R jBMlUAVfbUhJueWyXvCfLluIrDwiqZ2eF2H8pnWKIRVh1BJHTVXCcWs41PWRMqbf3FUo viIw== 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=dwj8xbw0bnEL25Qq4gmjxS00IOeAjG6GNQxAdbnWO2A=; b=oDb6gYKNvfzTxEuB5E19U5b/KsYxmV2WNh50Il56rb4GW+jhGAQC4FKi08O9EIwcbR MUrdyQgvOG7A0HVBGP+MI8JY/qIHX110rRxXFbc/P33Piq+kO4X3HKKgdkHjimR90wWb gdIdO5mCLi6zJCs1EaFxjK1DbDJ+lzgl/LU8pz6xGV7GeqfE8kGKw6W1vRB3IXB9lEa4 uEluQychN6Zu/2Mi3ncRrt+p8sRHzWeInnFGArMqDaRHa45xocEYOrCQIE3ZBRyEmTkN 3eJX1ll/CH0nW+BChY1lSuLVLMeEDa3sXY6F3Nv5KJc4h7RjbYC4dnZaHXyIhTrDPH5o z2qA== X-Gm-Message-State: AFqh2kpZSAxOI/9AsRLF+6eN9ufewVVkXCEiMjooI1+k0cVvCC1JJnLV +W/RLdJOmn6cx0uvdQm9+YA9YrcV0ROIU/3iW5R2/Q== X-Received: by 2002:a25:9801:0:b0:7d5:b884:3617 with SMTP id a1-20020a259801000000b007d5b8843617mr457605ybo.380.1673989527114; Tue, 17 Jan 2023 13:05:27 -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: Tue, 17 Jan 2023 13:05:15 -0800 Message-ID: Subject: Re: [PATCH 18/41] mm/khugepaged: write-lock VMA while collapsing a huge page To: Jann Horn Cc: Michal Hocko , 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 Tue, Jan 17, 2023 at 12:28 PM 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(). > > > 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. > > Related: Please add the new VMA lock to the big lock ordering comments > at the top of mm/rmap.c. (And maybe later mm/filemap.c, if/when you > add file VMA support.) Thanks for the clarifications and the warning. I'll add appropriate comments and will take this deadlocking scenario into account when later implementing support for file-backed page faults.