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 9679FC54E76 for ; Tue, 17 Jan 2023 21:05:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F81E6B0073; Tue, 17 Jan 2023 16:05:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A8616B0074; Tue, 17 Jan 2023 16:05:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 171546B0075; Tue, 17 Jan 2023 16:05:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 084E56B0073 for ; Tue, 17 Jan 2023 16:05:30 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D6C761403D2 for ; Tue, 17 Jan 2023 21:05:29 +0000 (UTC) X-FDA: 80365521978.04.D1704DB Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) by imf09.hostedemail.com (Postfix) with ESMTP id 54BB014001A for ; Tue, 17 Jan 2023 21:05:28 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Dz70IjXM; spf=pass (imf09.hostedemail.com: domain of surenb@google.com designates 209.85.219.177 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=1673989528; 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=dwj8xbw0bnEL25Qq4gmjxS00IOeAjG6GNQxAdbnWO2A=; b=dk1ppAX+7FvaH6CtT0bbXDhKtwKGXlDC29Gcim6ftwDntrbPfGZAPidOYJNWD4up+nbK9z 4kbAb4el62DnDm8kRfogEuxlXLPkXs7obBX6Oxia82xygP0ZCL9vIhWBGIl9rQR+wIz+a2 EZ8SoZzioDfBojE7xxmHJ8BLo8jYR+s= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Dz70IjXM; spf=pass (imf09.hostedemail.com: domain of surenb@google.com designates 209.85.219.177 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=1673989528; a=rsa-sha256; cv=none; b=CbFw3MAdOufRUQpSJuhtMOCuLCQjZ7hO/ZV3PB5fTF0lbw8RH//OJcRiHFXRyztS8RQm9Y C6KtRNIJmoB3YG7rP6bz1YxFxIujnwBhS+xlQmlCMKaBHkP13VCzaGjwpfKMKR2JYZLyyo gRMS5I4pKP7u/UUH++iXobsblDThyEA= Received: by mail-yb1-f177.google.com with SMTP id e202so12335469ybh.11 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=WtTBCIQ5G1xUR8wBUi9AbuLAT0tMty7Vs1MjCCcH0Sj4qxHy5vhhqx8CyfgcrfgwZu cd63slGtiVl3v31642P12ukW7rmNS6f/9kyvCOJVe+hKwphdHnEdf2NREnCDUKYVCAwU 7Ld6YCqyWyle/ZBGSZ0nB4pwyntQQAXrJHIp+84L6f9SR34tbmtX69hD4iET6XttVfXf x9AmYaY3C600DcFMM2KYWoI93junTBgwQFFJPL64QnnNr4lN+uTQlKFdQ745QFVXkEVF zEDvZSjwsNe0l7tXOUKk3oyFNwGB3FC0M5MVvRc9tDaJZzmWUb8vIo/3P6H+mcTjIima pcvQ== X-Gm-Message-State: AFqh2kpLmi37vruOzivOq4pokeW9n5RnRKv1kETTxbyjRJWKcjvBKUSC dGGullakkAsOPoOLDVY+2GIQqavNowfELNGH/z5fpg== X-Google-Smtp-Source: AMrXdXv6CeeFzN1+4R4tdHg/IKJ5C4l7PaOinzVK4PGVJM9c7GIhFphMdEPkr6ezjD9k28ifY1AtMqJwzv7bPIGABGE= 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 54BB014001A X-Rspam-User: X-Stat-Signature: xag5waozembrzuka9cxdu7qcdjse3one X-HE-Tag: 1673989528-961697 X-HE-Meta: U2FsdGVkX1+978YWdF/dFCq9KuqJVQZc+foOKUB4TnvO7ACYIX8dk8OsjqGuSx9ntN3Hy+8+P1LpYsfWqum9GaBK80rdwYAtN/V9qTBmWI8R1kpspxEn4QAw4+BeeI7NuhXqyFxUxGrn4bnq4j9EgYCnJePCxQ8ufhcsJBPE53imL08utAJDntQl2JIyNUmZO8GH6sVkfOT3wYgwrUalJKgbtfyDz6XGhZYF+3sk/wgznC4kI6RgDQgivbd6bUCsnqSC8NtDRILyL5lJI2OY8FI1s+EZMXhPygjGg1qv6wtHs5fxFLLwEiGoAv5jQsuw8fNO53cRCgT2Bf8AAUz2/QPJdRFLk1SktXttPyFvfn3LFjRZJb9hUNOQnQFK5cW7wqs1XkgYPQBHYoFcQXzOucbkiQZl8/8vkw5UvRY90SCdnwtQNDkk1dqD/i7pLirFL9YMopWtL/qakw2ugDMeTMUVlLzP19g3HOPCXEdvV1WFkUaW677NCO6VsGjJxdLUFxTi/BY+Vq9mXRclX+VWVIeUZjrO+CS1pY6Nefu9I6Ity/IyG2OV4dTIMp6ka9AnPBPnb3qEZXWyJnt4cjiFyoZa/qNXLxhsrQgYyV0zDs5+ERXROEZhOGY8L6hoefJ1QGKnhr/OpAftXJBxQzRRA8LAhT6erG1AzSm4nX/45CIcoLaKBuQcQXzPezpl9oSmCatoWunZuG4MNAkJo8DliQ5OamBuXIpn6lpAC8bw64eSbpQFjeMWmXrxYxp1LU4sOe7qHcnJIJ7X7Lwuv3vzhW7f2FGnsmrL8yO/DeRMtCYsoTMuAzceYZpD1YFIiRAPFTQs9NOC9wYdHPqDGQRLxLfgXnYTI30ST6toOSi09OR+fdIXSXOPfxrIzwIxdqjPSsSWo/PLsQ8BgbVC/v3SMITJsz6j8povTq6szWg6A1njor1qJS/6q/c/NIchOFVLZTursy6AGIUWqgc0qSj Kk/PsQ7a pri15H64laFTNDBE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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.