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 87932C38159 for ; Wed, 18 Jan 2023 12:38:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF9086B0078; Wed, 18 Jan 2023 07:38:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DA9426B007B; Wed, 18 Jan 2023 07:38:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C97846B007D; Wed, 18 Jan 2023 07:38:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BB3086B0078 for ; Wed, 18 Jan 2023 07:38:56 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 77B2A1608DE for ; Wed, 18 Jan 2023 12:38:56 +0000 (UTC) X-FDA: 80367874272.13.7FC3262 Received: from mail-io1-f43.google.com (mail-io1-f43.google.com [209.85.166.43]) by imf17.hostedemail.com (Postfix) with ESMTP id F25A540005 for ; Wed, 18 Jan 2023 12:38:48 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rheJ1RUU; spf=pass (imf17.hostedemail.com: domain of jannh@google.com designates 209.85.166.43 as permitted sender) smtp.mailfrom=jannh@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=1674045529; 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=ne1ZZ8g0CKjtUM0flXLQwaKYgCBQx784VijV19d+6xc=; b=4FV4086uD2dD6f2WH5x2HbsSE/6lUj/bNhNOeKnIbcpttdDauWS43DHu1cUHNByiDUm3D8 gWYUYUJU4t30JWMOnje6D9IpoZbYltUbMbPNEsvoafBsbEw5zwfDrWkHiE2YrNJllDW7t+ DfO/iVvk+JyX8EIqfD4zXpEUtzvntT8= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rheJ1RUU; spf=pass (imf17.hostedemail.com: domain of jannh@google.com designates 209.85.166.43 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674045529; a=rsa-sha256; cv=none; b=pcUkJeytwP69UEaZNIcRihihSl+7fnej2tNgdYFF2XVI5JGJ/4SYYTOCLV9pSrufE5jfSZ hp9SO4Vifz4sttYe/RBpBFa/sASmWUp/dqmCIS8dkZNdMKC4FJrBozlbpNI0iY/jFN/H5l 1uQL/3wSJEL+e1X9OrDDYgnA4xZCJwQ= Received: by mail-io1-f43.google.com with SMTP id y69so1831461iof.3 for ; Wed, 18 Jan 2023 04:38:48 -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=ne1ZZ8g0CKjtUM0flXLQwaKYgCBQx784VijV19d+6xc=; b=rheJ1RUUcAkj+1owhrmAToGN1JKqVwS5uNfucsbLgdY6jiwLnv20ZvMr/zG5UmC3MR r6w4F2M4P+L39RFGiPrp7Pc4YsCSMo2eypVoYy7uAZH0skF0cb9KGqSqKmQtTROWTdZw Y4onbS8Bh9xurv/ecxH2l9+6u4n2k+Du7eEvZ+3bilOaoZOJlQlDDMN5a0ev1/1AkMIv 6hGk1h8D1872yYIqM9YnRK4SbQiqQUWcQ1xmAUvd1tSMlevfjfCKbIpNjzx92FXNNtTK eamrPPtqzoYNgbP3fcy329YWOqfU15+ST4GR/7gu/oa+wU+FfArYkk5BOsi4UvDxXYDv dylg== 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=ne1ZZ8g0CKjtUM0flXLQwaKYgCBQx784VijV19d+6xc=; b=5qubXG+TzgVuG5qdLbKJIV2idxnRvOdue/H7tjlQt4eoPVVjydI2MD01vKrHCVR8ZU X/U4jpjD0a23qN6B1KSyPSNM0OoRlj3DKkjxCnzZTOLNdheFq7okuEWHAr1uTE/k2/eZ BkVMq4xp/TUunm+8osY7HNk7uxzEs22c6H+Y7S4RUXyIQ1tpk+aIPvc2/V8o3jvPaltG BMeTcEeEizPp+AtvrGPX1Z+lege/4UMV6EHHD5cMu/JTO70t9hFy/RoeDTB7RidqnU3S 7ElXpdceNmsV5i0VHk5CnyvSHfhKYrMIEdRVyy0NOx8idnaK1xuyehogyc/26Qoe9OUg H1gQ== X-Gm-Message-State: AFqh2kpVJbAK+Yic3IybiFcst5FdbF1kPBTcAQPd2TCji4IOtApTBX+z PBUDEzFaPAUthnLkSmIG7tETyNrXr6N41AVw4FlI7w== X-Google-Smtp-Source: AMrXdXtdPpTPNrN7OiYOv16BmNfqKkmp2Nm/DssJpSsitfPp+gX8QsWWZQOd27RnCdytJ0z4l4VIPkrlODp7fq/3+OE= X-Received: by 2002:a02:cb45:0:b0:39e:6dd8:6c96 with SMTP id k5-20020a02cb45000000b0039e6dd86c96mr543739jap.246.1674045520432; Wed, 18 Jan 2023 04:38:40 -0800 (PST) MIME-Version: 1.0 References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-19-surenb@google.com> In-Reply-To: From: Jann Horn Date: Wed, 18 Jan 2023 13:38:03 +0100 Message-ID: Subject: Re: [PATCH 18/41] mm/khugepaged: write-lock VMA while collapsing a huge page To: Michal Hocko Cc: Suren Baghdasaryan , 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-Stat-Signature: yr443ruasbybkyqhqmbc6ryniskq6bt6 X-Rspam-User: X-Rspamd-Queue-Id: F25A540005 X-Rspamd-Server: rspam06 X-HE-Tag: 1674045528-761869 X-HE-Meta: U2FsdGVkX1+oAzHaQLFsRKdlc0MpO8bNJLvWTIgBO12VTNZZRmpnjL9e+aHcEcABWyU7jPC3UryCm5m9taFXiTq9d7DIzpM089j+wHjNwpyQehaGX4IrexhXN5lBNXCcbm5geN+0DE4QPsSjNnYtT3tRJvboMZB8EAy6XcCcWRDTu8Cw5Qn1WHgNfB9vQXgFD9fEwDoATh1ynbD4csuqLX7Zy0u6lzo8R2Iez/+itMAGy/wGCZcqyF7BF4DSUyf93pngkA5L9m1k8pIrBsvVF4kKB2V5mXkM4wcsvcMgZ46sgNZgkl17bqlU50W7p2yV+4OFcPbELORkx09h5m12+IfbMV09GrcRh+AjsIJcrNGuDnOU9zzgCnWX9yL3gC8r2y+oXpHZ/wRrp1rmivwN545uC+bNoRzFhVb9VUOck2PKGzSGbNMN/f4ZKH2K3IlFuCTklGMtHJfHMnMTKhlo3mI2n1lb0KeHZmvCZ0gfpb9wyAYWVwCP+eBh4MjblLxcIxbl3TkJHJp0JQKoQQRcArq6K9gxD3RYe9oq/iPiKvyIHVly8x1aZl6BDAFcFBU93vSL+JFxQmPhRHa0SA+0HCuCPPqUfTBJG0IRdiit4RjTSQ+evm7iKwJMGiIvI57NV7snbKjM2iI460j9gOV9GbhPVbakuOkiHA/jWOk1qUsLqz2ct92d/Ru3fGl0MdSEwPedQSMxhVmMlMxQMcAgm2HtNhtnaZB/N8JnvSer0tymIhiHvigk7OqGI48LkQeCo480IrwSugg1KS4QW6edjvwpc/xUhpIbOXf1PNtzdC07ymrRrWKLUiLLJMG+P8HC8XG3Z56ulxLrLMlRm+LFNUM4Wk/HSdcYyEKLts7oOjFR/eVkPDHxB+gOt8h5zDS9t/liKNpvbDTzPWR1qSyfLJIvbJJDfgQ+Kqwzy2v6vFRk0vfo1hBlv33tUcCTq1zlEbmD35+Rbzksv+wb6Il lbNOY3HJ gpyGFZGMv4Y5CSHA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.002825, 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 10: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 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. Oops, yeah. Now that I'm looking closer I also don't see a path from the #PF path to i_mmap_lock_read. Sorry for sending you on a wild goose chase.