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 C89B2C7EE2C for ; Sat, 27 May 2023 20:13:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32280900003; Sat, 27 May 2023 16:13:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D1C1900002; Sat, 27 May 2023 16:13:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 199AF900003; Sat, 27 May 2023 16:13:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 0AD07900002 for ; Sat, 27 May 2023 16:13:47 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CEB44C02C2 for ; Sat, 27 May 2023 20:13:46 +0000 (UTC) X-FDA: 80837135652.18.90CBAC4 Received: from mail-il1-f179.google.com (mail-il1-f179.google.com [209.85.166.179]) by imf02.hostedemail.com (Postfix) with ESMTP id 0D6638000F for ; Sat, 27 May 2023 20:13:44 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=QKnbpAkf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of yuzhao@google.com designates 209.85.166.179 as permitted sender) smtp.mailfrom=yuzhao@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685218425; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hK0j7POD8nRAsgGVVyJyo0UAU2zdW6aeJVN1+UjYElg=; b=JJs+iKsn3nZxrJSeTp4lgU3mjdy//AoxLEAq+VZfJLfOVLceVWWLj8W7O832In3jmnyTgu LWoINm/ZXZbzrrzcZdKHnqUUefv6OzzKvi4jBtc4fEIpXF8g/STUQxJlEUaZZXpk5iDGzz rkI5CbwoKc1A9CSN3YD2942FJWSrOjM= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=QKnbpAkf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of yuzhao@google.com designates 209.85.166.179 as permitted sender) smtp.mailfrom=yuzhao@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685218425; a=rsa-sha256; cv=none; b=8EcJc4EVt42d4WK+WMlyPskoat2SVW4m/4zuVkN4DhOjLFnegBvWnxP00Dk31ktU0ZsEv/ UavVD/dt2Al4BobesY4uTiL9vlKl9+bAZ35twdjVB2bwDUqXVAYciGfJ7c+wEzmMqZ0CbY 5LKzO1rD5C19Wn8EuNzfR7UL5yYfEjg= Received: by mail-il1-f179.google.com with SMTP id e9e14a558f8ab-33b0848c04aso88125ab.1 for ; Sat, 27 May 2023 13:13:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685218424; x=1687810424; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=hK0j7POD8nRAsgGVVyJyo0UAU2zdW6aeJVN1+UjYElg=; b=QKnbpAkfLda1TvWrqaHzwSET9fcih+s5ib+TdEQpynJiDGgVyvcDWnq3f1jN8iSFFh i4wFHU4TObni6/73zZFM2D/pTSqgWk3+PPUYZMhIR48y37fhDEuDC9+d1+t2fRyrNgkr ayuCVtu0XYwZQdosz7e+wfHDnmUd/KZvqBq519Uy5lfqbbTbuuKK9L+bNXaHlLVwpoUG cBLrrDoSDWDEZ8OZLuLbQmUVgsGX8ts8KOPplFja+x5y6f4LRsCZW4nMJ6qgcp4bf8BG SEOtEkefIHI1t2fsfFozpr5n9BYz9LMkdnQOauJRbx9hSkM/0nH1fRNwjjqiDsKIVZLy cqTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685218424; x=1687810424; h=content-transfer-encoding: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=hK0j7POD8nRAsgGVVyJyo0UAU2zdW6aeJVN1+UjYElg=; b=ARsMEi0SpIsCRVgwdF+oGn1edhIJS/L4U7sCUa9R5jRrmZS6xeMapkb34z+lzN4mU1 E1P6NlcY5ZdGCgjP/IHYdsbMkTsrM4wGwAOD8vM7mUtD8wy7FogwLzTrgU/b8TfpdZ2+ Ji3pn31MpvxMeKTON30/y+Cs7oDMENFAdjJE3E6ZZL8dGpbiXt8mItikd2zYxCUsiyHc deMQ9Nk0+0u5wc0+fOdcKIswQic/Z+ZaKRZHegfODcpUttaC9oprZgdYydkVER7EedR2 eAMfPOpwhzHPYslwV6OIRoN5RkpqQBt63IubD3yy5B1FpKkVCmF27qKE4+e7B7AJtfT7 E64w== X-Gm-Message-State: AC+VfDzPS6gFjyhX9D7J3yl4Bj+rVbAQCBcxRkvhgGIJkH+sv8Aw2EYh EhqLvHifu0dubVxxxURUy/FCCLL/lLBcAWEWuxkwsw== X-Google-Smtp-Source: ACHHUZ5RLdNfCuhTMMzeLnK0AkjXh46hEGbQ5DPXaiuA4QGSm27lLMiJSKz6HarBK0wHhmVvVqcZAJpytCQ/s6idEUA= X-Received: by 2002:a05:6e02:2185:b0:337:c9ec:4ca with SMTP id j5-20020a056e02218500b00337c9ec04camr138001ila.2.1685218423829; Sat, 27 May 2023 13:13:43 -0700 (PDT) MIME-Version: 1.0 References: <20230526234435.662652-1-yuzhao@google.com> <20230526234435.662652-5-yuzhao@google.com> In-Reply-To: From: Yu Zhao Date: Sat, 27 May 2023 14:13:07 -0600 Message-ID: Subject: Re: [PATCH mm-unstable v2 04/10] kvm/arm64: make stage2 page tables RCU safe To: Oliver Upton Cc: Andrew Morton , Paolo Bonzini , Alistair Popple , Anup Patel , Ben Gardon , Borislav Petkov , Catalin Marinas , Chao Peng , Christophe Leroy , Dave Hansen , Fabiano Rosas , Gaosheng Cui , Gavin Shan , "H. Peter Anvin" , Ingo Molnar , James Morse , "Jason A. Donenfeld" , Jason Gunthorpe , Jonathan Corbet , Marc Zyngier , Masami Hiramatsu , Michael Ellerman , Michael Larabel , Mike Rapoport , Nicholas Piggin , Paul Mackerras , Peter Xu , Sean Christopherson , Steven Rostedt , Suzuki K Poulose , Thomas Gleixner , Thomas Huth , Will Deacon , Zenghui Yu , kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, linux-mm@google.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 0D6638000F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: foy1rdf5hcciam68sk9g5om786sk6j7d X-HE-Tag: 1685218424-183979 X-HE-Meta: U2FsdGVkX1908n/hbfS7btW41H0S9Q9bzKyHvihTym89R/nNncmQKGqu8+KA4fHL1+SNDUqSKk+r+vJpQT5SF3bvTB3V1P9vwoZPdaDqr9zeuWE086EN8Nt28iV093bfYhp6/5qKZx9SXll8t8hO0H+7s0q4nIFKrlFpcbRBxGA1Q69+Hotcxrv2Sex2OE6JDV5kjK49bhyER39Y/LyFRGb0LL6IqN+LXcPVlJxfGj7yAkbU6LQyteUuS3xQzISMm0IeEmMktZFbvzhRuZNfQKXu4tihSb5LN1GOPalWziw+9XExWKcHqYjNjqOxXZY/qga0/yVuwOfL1bqUwk0b+/p6ihpkdtspEjySfjYAZPt379pVZGYc9hLl0qa5Tko+nb8rIfezb803Vx5qMV344p6TAjLhZ4vSz3jFBt/AZ7mOuI+pSlMihfaO9iA8G00RqUYwbqrMWdC1dJ0SpawCfp1yMQP3BzeOaoTCSbXmSkyvlR6ACoLnHnDa4ktdxJQ2zIUMTShn6M8O78lO+2A3wCMLpUh6Y+JhfiNG8inVWM49XBEG144Shpm5w6egBk1+Y85h/EO+yZN6jAuT0ZER3t3ZLdBuvPkVc8Xi+MgOXhaqtxZSxLn10AB3TK4HsjGA7mPBvAEl7NNGFps7jieV+me/GJCcbAQDfWb5G53T38tXtaYdsSeDn00sd+mUcyUMY7U/O4RM34GKjX7kdf8jZXcFC2aihI+vimYmtp1cVPMh8EZRMtxpV+0dySbI9chcDk4qbchkG3pMThrXsTkeFLji3BYJ8QsSLDLxgLjca+O4rgvcdRo6TxKxvNIJS/A7IhiXUYHs8fvA2aK4v0fqtWfxsHIgyTxskyAOv9b8Ut1NkPk+Yr2EvTGTwiM9NUlLmC/iCa5FaUOfUUQMEFLK8Gkl0/VtjvfMAfjReV9bv7Errpigoda/SGdGcZWppos3fJhs2n2S3c6HIl6gTrR d7UHVGjd XhYHTem6XaT2eS27G8xbHTOmz/qes9UP98B3ohluvik+2VGLe+8GHHFSF++b2JnRMmiSsK3X4V//OI5xvpUo4Gu9rUAyyMuOT0Xe4PQSxx8I3Prz5ZxJHy6fQf5d4B14pP3T4TTvQVeX9HBC3yVNEE4dYfdd8laspMBgenYcxRwcvAc2ArRAUUJw8/B+ctOWtpTVzVLJA9UdAXT/Thq30ZtdD8Rd1dluGb2Di0Usx1RU6HIaIbDpBpsEJYGIh+8xHgrqqjkKltXhDjE4kDIZyfdLBu/O21gsIAbxV X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Sat, May 27, 2023 at 12:08=E2=80=AFPM Oliver Upton wrote: > > Yu, > > On Fri, May 26, 2023 at 05:44:29PM -0600, Yu Zhao wrote: > > Stage2 page tables are currently not RCU safe against unmapping or VM > > destruction. The previous mmu_notifier_ops members rely on > > kvm->mmu_lock to synchronize with those operations. > > > > However, the new mmu_notifier_ops member test_clear_young() provides > > a fast path that does not take kvm->mmu_lock. To implement > > kvm_arch_test_clear_young() for that path, unmapped page tables need > > to be freed by RCU and kvm_free_stage2_pgd() needs to be after > > mmu_notifier_unregister(). > > > > Remapping, specifically stage2_free_removed_table(), is already RCU > > safe. > > > > Signed-off-by: Yu Zhao > > --- > > arch/arm64/include/asm/kvm_pgtable.h | 2 ++ > > arch/arm64/kvm/arm.c | 1 + > > arch/arm64/kvm/hyp/pgtable.c | 8 ++++++-- > > arch/arm64/kvm/mmu.c | 17 ++++++++++++++++- > > 4 files changed, 25 insertions(+), 3 deletions(-) > > > > diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/= asm/kvm_pgtable.h > > index ff520598b62c..5cab52e3a35f 100644 > > --- a/arch/arm64/include/asm/kvm_pgtable.h > > +++ b/arch/arm64/include/asm/kvm_pgtable.h > > @@ -153,6 +153,7 @@ static inline bool kvm_level_supports_block_mapping= (u32 level) > > * @put_page: Decrement the refcount on a page.= When the > > * refcount reaches 0 the page is automatica= lly > > * freed. > > + * @put_page_rcu: RCU variant of the above. > > You don't need to add yet another hook to implement this. I was working > on lock-free walks in a separate context and arrived at the following: > > commit f82d264a37745e07ee28e116c336f139f681fd7f > Author: Oliver Upton > Date: Mon May 1 08:53:37 2023 +0000 > > KVM: arm64: Consistently use free_removed_table() for stage-2 > > free_removed_table() is essential to the RCU-protected parallel walki= ng > scheme, as behind the scenes the cleanup is deferred until an RCU gra= ce > period. Nonetheless, the stage-2 unmap path calls put_page() directly= , > which leads to table memory being freed inline with the table walk. > > This is safe for the time being, as the stage-2 unmap walker is calle= d > while holding the write lock. A future change to KVM will further rel= ax > the locking mechanics around the stage-2 page tables to allow lock-fr= ee > walkers protected only by RCU. As such, switch to the RCU-safe mechan= ism > for freeing table memory. > > Signed-off-by: Oliver Upton > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index 3d61bd3e591d..bfbebdcb4ef0 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -1019,7 +1019,7 @@ static int stage2_unmap_walker(const struct kvm_pgt= able_visit_ctx *ctx, > kvm_granule_size(ctx->leve= l)); > > if (childp) > - mm_ops->put_page(childp); > + mm_ops->free_removed_table(childp, ctx->level); Thanks, Oliver. A couple of things I haven't had the chance to verify -- I'm hoping you could help clarify: 1. For unmapping, with free_removed_table(), wouldn't we have to look into the table we know it's empty unnecessarily? 2. For remapping and unmapping, how does free_removed_table() put the final refcnt on the table passed in? (Previously we had put_page(childp) in stage2_map_walk_table_post(). So I'm assuming we'd have to do something equivalent with free_removed_table().)