Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1220442rwd; Sat, 27 May 2023 13:28:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5FMERlIWswqDRW+68Ns2BXn+p7c9TwZ/QFeKwAR+XN5tukZm6g4UFFxfBrlinmfHnh7X/l X-Received: by 2002:a17:902:d508:b0:1ae:2e0d:b38c with SMTP id b8-20020a170902d50800b001ae2e0db38cmr9082605plg.12.1685219306587; Sat, 27 May 2023 13:28:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685219306; cv=none; d=google.com; s=arc-20160816; b=mC9S93+nehX6SfNn0EUi48Efll1Obyf5bj8ePxdxYisZ78r0pwQQV+5wDa+3iBHC+U OVRRggveKo2mmTQYVd9lhkbw8mVAbSBvTUZhil/PrCFGIpBkQ7d5IY78t560FAN8HHwv YMRB9qzm4aMLdt3FKhY8oHwoqL++Rilz5ZLt5boK6K/kJZ5g51yo2HJ8rvrWCnw10Gtj SjdjLYziOgimMpAfBuc35Wrxvq+JCsbk8U+u6PLQdBzXX+mu5HF3milLg+nvcy9vHqqE 0PnjLQBcmHgMm/tpMDwg03BzgLVWlEqDEwoA6ld/ZJULTGFQ3AXiRrTwifKoGUYRAsn4 nSiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=hK0j7POD8nRAsgGVVyJyo0UAU2zdW6aeJVN1+UjYElg=; b=eDrqS4qX9PBU1Tg4ZfPo5HGcz1tqFbAVl1FxKfMcDWT/eTYHjOi8xWp6/fFgQbc+jz nQDKZxpHXobTo+iWFq2Eln7Au51uFxnLgPmpDRoQXcW6vVca1lyXrR6FqcPDOe5FhJkO jUZgwXslFMQn2vfT5TCnr3Q4S74mJljh2vjY2Xxku4nyE9AkfsUz/4eRY2vLSLmh4RTN u/aa+00IEsrF1h3KpU/q9oTTmD+LP/WhZlx9vzJSVsRdDCcHeYELLbIvFoKluTJ8rT3c lHLFvDQz3UJLoHmnXH5eORhJdSL6jyDnwmPX3rkxfF8KlKF65XfvP7wi/CrVBDZjvFtA fIFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=QKnbpAkf; 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 s3-20020a170902a50300b001ae5eb838easi6163482plq.292.2023.05.27.13.28.11; Sat, 27 May 2023 13:28:26 -0700 (PDT) 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=20221208 header.b=QKnbpAkf; 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 S229647AbjE0UNs (ORCPT + 99 others); Sat, 27 May 2023 16:13:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229501AbjE0UNr (ORCPT ); Sat, 27 May 2023 16:13:47 -0400 Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA3A0DE for ; Sat, 27 May 2023 13:13:44 -0700 (PDT) Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-33b0848c04aso88015ab.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=azU57B0PgNhq/7OAG6DwxMLlNoxam15xfRii93WwU36xdMDS5LbsUCTkxYJLfrdmRt BdRN8dPS00+CxjRD2HiLRloUIBTxP29xgAzX7Be3suma8wRjGLm3eflYbykUAQf07vze FATGFLpoQoyGwsXNRg/SpRsVeIS+Sl/VbdRuMNDH8SluzgAQFnZJZTswOi7NWliPs47j JpR4uCjGvqI2VaO/iX1UQ3K4vKfXgUFXnu4KKbkRI2vWyfsFlxJA/d9y0O0H7evbeJ2W sqrNQiK5rXAgVgnskoynxSnfKk0FSplQY4hWnOdZ/xJ94lEqaMq081A6r/5aJTBugtry hQ4A== X-Gm-Message-State: AC+VfDya5NJ9boRi0BR9aZ5RiwrutNhRLoIAJiCDYtRD1QHkHWFsa3zc Xnvqz16JG70zOXkAYZdNztJsrUpB7I4hWmFThjtw9Q== 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-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, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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 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().)