Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp7290860rwn; Tue, 13 Sep 2022 17:54:19 -0700 (PDT) X-Google-Smtp-Source: AA6agR72NcOBBT326MXRoSLf3Eyadh5NYzSeptX3kcNXxvWya7RgnHA+VVoIIX7GIAEYi6fxd3lf X-Received: by 2002:a63:164e:0:b0:434:9014:7627 with SMTP id 14-20020a63164e000000b0043490147627mr28657084pgw.504.1663116858793; Tue, 13 Sep 2022 17:54:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663116858; cv=none; d=google.com; s=arc-20160816; b=zAxQwwNI/ov7MZFRXE945OtDYnFirQmiqCoI8VO0eujVT+0Rd+OPMVnb6T/gDn4WeH bbA67/PG5styHT88rt8SZKuDjGyENo0qGgOkjfwRO3iXTr/HbP0GmHyBHVjro2X4A9Wh f/MpdMnCPAISZPiQ9hd9ULUYbQ9yGfcCy3sgL0C6iTKBLaISpbszXP21abAxC7Kmgf4/ AupT3h1zgvtt4MUwUmoA7T6z0Db5weeygg4s1U+5o1YMo7Ay3QBaLvBqmTZWL5ExIxt9 lQXfOTLDaOzBJtltH6Na5sreVZFMWNwfAcrMkimlkETNXHlZpe9V/10mPyuoKdKubPYQ vrbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=VC9mI68no/Lhw0HfZpwkORWcc1RC3UZIYHwVkfGB/r4=; b=tq36U1DDI8KrwtsAL1pUP6AgT98FxHT3Ax+yK08fVPyXQSJ/px9/Zi2H7NHu4M+5GZ 1kychevfkUp5vSNqBfxaSMjqKHlPoXKXXTPUzCM/yj3Uo5RMbyUJbOm5047xVxa65nkT 4fhV5oiGuI2Df5HM+iAowLRg6OAcfrKBed02/M8rCPv/SqNZqY+wrEr8RHrAmWEGwz3y 5Dxe/M0/vZVgYsuDgVcuAwY4aGdAhLe9T1TtCHI+3YpE5fjufPp2WteLaLw2qfYrJnei 0XOEAmNGOyzLAK8AR98QSb1N6qmlHZt6U8gTEH3YRO49OS7N7SSZLMTNeIMUJc9Q25KH ZYhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hrIqf5MD; 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 m10-20020a170902db0a00b00172cde9be63si11891170plx.127.2022.09.13.17.54.06; Tue, 13 Sep 2022 17:54:18 -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=20210112 header.b=hrIqf5MD; 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 S229456AbiINAth (ORCPT + 99 others); Tue, 13 Sep 2022 20:49:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbiINAtd (ORCPT ); Tue, 13 Sep 2022 20:49:33 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6BF9DEA for ; Tue, 13 Sep 2022 17:49:31 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id q3so12940632pjg.3 for ; Tue, 13 Sep 2022 17:49:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=VC9mI68no/Lhw0HfZpwkORWcc1RC3UZIYHwVkfGB/r4=; b=hrIqf5MD/xB/9myyBeSm33TcaOokPoX7OBfouhtK+QYu3xN2uBJxyO2hwvyYtuh2W0 RdwXYSsNmn+DsH/Gpdq5vM3R8ZfCoKhKciL2HGbxoduQKF7c8ktRoTQjLfMESLPFpZyL Ns0E2DVLJos2dtMmG5R41fTtTe3J/4Tlo1NyAQwzTGqI70CMfMYO11+ICelzhKCetwtW SkPeKYcul80lg+mpqpXs6SCD0MpG3NKQFFzJeeWkjLD0aS7zcXg0vSm1kwe5vEoNUwPa CPS85UG7CF4R83h+gydjSaaK82yV2BkpbSMULGBFAaCTe6LmRRsym1vdrmpcBBksyYr9 H8nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=VC9mI68no/Lhw0HfZpwkORWcc1RC3UZIYHwVkfGB/r4=; b=Qa4sKRciI2mKGj9ahZZ5X4zmX73n7EJJ+jOcgtXfqNMjGIeJppnhG8j891Xzv0myUp MF1fBcEWbmwZcqI1xVRO5+h1v+KWqLMTkJj8pg4PTrS4Bxi/qw7GHpGhug9WltzqrY1O YrN8K73xcXXwjPmE2/q0n2Zewxq1S/dmRNXf0bfM+8ySrYEiJB29GYqXdPhQ0OGXm50Y FI9jqmBe1nddFkSCL1JgjM5wv9V2z4eN7Eh1VplJ8sl2h70qt4BDnk0dFAteDqY3jrF2 JZUyqQdSTF1u4x5wgw2s2aAF9pNheh0H3qyvt/zJ8aFxUrRi6s2g8o1Mkcb0PXpu0Ruc dW0g== X-Gm-Message-State: ACrzQf3HAfaJo52XowaP60KGARF5D3vlJVLDrAkCwk23AeqfVhm/FaxB N8e7Yjba4ejfDpCSbTP9fTQ39A== X-Received: by 2002:a17:90a:7006:b0:200:aabc:891 with SMTP id f6-20020a17090a700600b00200aabc0891mr1948603pjk.67.1663116571104; Tue, 13 Sep 2022 17:49:31 -0700 (PDT) Received: from google.com (220.181.82.34.bc.googleusercontent.com. [34.82.181.220]) by smtp.gmail.com with ESMTPSA id y186-20020a6232c3000000b00535e46171c1sm8503863pfy.117.2022.09.13.17.49.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Sep 2022 17:49:30 -0700 (PDT) Date: Tue, 13 Sep 2022 17:49:27 -0700 From: Ricardo Koller To: Oliver Upton Cc: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon , Quentin Perret , Reiji Watanabe , David Matlack , Ben Gardon , Paolo Bonzini , Gavin Shan , Peter Xu , Sean Christopherson , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 09/14] KVM: arm64: Free removed stage-2 tables in RCU callback Message-ID: References: <20220830194132.962932-1-oliver.upton@linux.dev> <20220830194132.962932-10-oliver.upton@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220830194132.962932-10-oliver.upton@linux.dev> 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=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 Hi Oliver, On Tue, Aug 30, 2022 at 07:41:27PM +0000, Oliver Upton wrote: > There is no real urgency to free a stage-2 subtree that was pruned. > Nonetheless, KVM does the tear down in the stage-2 fault path while > holding the MMU lock. > > Free removed stage-2 subtrees after an RCU grace period. To guarantee > all stage-2 table pages are freed before killing a VM, add an > rcu_barrier() to the flush path. > > Signed-off-by: Oliver Upton > --- > arch/arm64/kvm/mmu.c | 35 ++++++++++++++++++++++++++++++++++- > 1 file changed, 34 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index 91521f4aab97..265951c05879 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -97,6 +97,38 @@ static void *stage2_memcache_zalloc_page(void *arg) > return kvm_mmu_memory_cache_alloc(mc); > } > > +#define STAGE2_PAGE_PRIVATE_LEVEL_MASK GENMASK_ULL(2, 0) > + > +static inline unsigned long stage2_page_private(u32 level, void *arg) > +{ > + unsigned long pvt = (unsigned long)arg; > + > + BUILD_BUG_ON(KVM_PGTABLE_MAX_LEVELS > STAGE2_PAGE_PRIVATE_LEVEL_MASK); > + WARN_ON_ONCE(pvt & STAGE2_PAGE_PRIVATE_LEVEL_MASK); If the pgt pointer (arg) is not aligned for some reason, I think it might be better to BUG_ON(). Alternatively, why not trying to pass a new struct (with level and arg) that's freed by the rcu callback. > + > + return pvt | level; > +} > + > +static void stage2_free_removed_table_rcu_cb(struct rcu_head *head) > +{ > + struct page *page = container_of(head, struct page, rcu_head); > + unsigned long pvt = page_private(page); > + void *arg = (void *)(pvt & ~STAGE2_PAGE_PRIVATE_LEVEL_MASK); > + u32 level = (u32)(pvt & STAGE2_PAGE_PRIVATE_LEVEL_MASK); > + void *pgtable = page_to_virt(page); > + > + kvm_pgtable_stage2_free_removed(pgtable, level, arg); > +} > + > +static void stage2_free_removed_table(void *pgtable, u32 level, void *arg) > +{ > + unsigned long pvt = stage2_page_private(level, arg); > + struct page *page = virt_to_page(pgtable); > + > + set_page_private(page, (unsigned long)pvt); > + call_rcu(&page->rcu_head, stage2_free_removed_table_rcu_cb); > +} > + > static void *kvm_host_zalloc_pages_exact(size_t size) > { > return alloc_pages_exact(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO); > @@ -627,7 +659,7 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { > .zalloc_page = stage2_memcache_zalloc_page, > .zalloc_pages_exact = kvm_host_zalloc_pages_exact, > .free_pages_exact = free_pages_exact, > - .free_removed_table = kvm_pgtable_stage2_free_removed, > + .free_removed_table = stage2_free_removed_table, > .get_page = kvm_host_get_page, > .put_page = kvm_host_put_page, > .page_count = kvm_host_page_count, > @@ -770,6 +802,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) > if (pgt) { > kvm_pgtable_stage2_destroy(pgt); > kfree(pgt); > + rcu_barrier(); > } > } > > -- > 2.37.2.672.g94769d06f0-goog >