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 C555AC77B7E for ; Sat, 27 May 2023 18:08:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21DA56B0071; Sat, 27 May 2023 14:08:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CD23900003; Sat, 27 May 2023 14:08:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09621900002; Sat, 27 May 2023 14:08:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id EF5B66B0071 for ; Sat, 27 May 2023 14:08:51 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C662B160292 for ; Sat, 27 May 2023 18:08:51 +0000 (UTC) X-FDA: 80836820862.16.EC56279 Received: from out-24.mta0.migadu.com (out-24.mta0.migadu.com [91.218.175.24]) by imf01.hostedemail.com (Postfix) with ESMTP id E477A40015 for ; Sat, 27 May 2023 18:08:48 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=llE6IRkx; spf=pass (imf01.hostedemail.com: domain of oliver.upton@linux.dev designates 91.218.175.24 as permitted sender) smtp.mailfrom=oliver.upton@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685210929; 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=qro1ly8D0IN85bz01iVra27g5btF8pv3UlKO41a9+sM=; b=TE95knBqllQGsulJEyCpOk37rajnUGMnJxqxPAPX083qC1Y2SZPVVL6fFQ1JYZTaXThLiB T6sLKODpaOFm2lB5/tov0sArehrZJQ5ZTAnrjBlrr/OcoNbvW68naJxD6sA9QZ3ZgwlzOs BaGiKRh7/GxcG5EquvXAi8OctZxcwVY= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=llE6IRkx; spf=pass (imf01.hostedemail.com: domain of oliver.upton@linux.dev designates 91.218.175.24 as permitted sender) smtp.mailfrom=oliver.upton@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685210929; a=rsa-sha256; cv=none; b=D2Niq/y1kIei99C88eokh7QeoRGTcjlhy4VhsYLqrYbPnkNTormtK3XSTAkVYH1J+VDBhq +qWG9WdsDqBZ83rrDXmKc8efL2CsjKceCgvRviN15AGFsfx5HGlpR49SU4TgebtLDGmoyh RqfhZixRspXcgCWyT32MVIJToI5eP8Y= Date: Sat, 27 May 2023 18:08:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1685210926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=qro1ly8D0IN85bz01iVra27g5btF8pv3UlKO41a9+sM=; b=llE6IRkx/n91QHSJfUuz8Lvf36Zsdvbsda8s4+YDAphela0hHMrM+udZgnGOhdOMmdcMem 6eMFZGQIhSrfmCXeAFxKmM8bvTDiACcfidzZol+st9/hDl3h5IknZGYlF6wWBlLroxmus0 h5pa83O4YLhVJG62l7K1d5yuFK61noU= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Yu Zhao 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 Subject: Re: [PATCH mm-unstable v2 04/10] kvm/arm64: make stage2 page tables RCU safe Message-ID: References: <20230526234435.662652-1-yuzhao@google.com> <20230526234435.662652-5-yuzhao@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230526234435.662652-5-yuzhao@google.com> X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: E477A40015 X-Rspam-User: X-Stat-Signature: kaftse6ecs1pdkrnnewqr7i9gt9srot5 X-Rspamd-Server: rspam01 X-HE-Tag: 1685210928-390007 X-HE-Meta: U2FsdGVkX1/9dhLjuEJfZHcdGCnCC622HryK3iTF02A419E+BQDaJO5OsbhCooRVz+RQMx/+gdUItCm6Q+b52qebmFVouVNR8rrAojKo4lSb8PiFxfZ1VpBXlzMaWf40IpJYecBoChaCEjx871gQg72fzHPOKMzfNLY8EngRm/I5MqCZGojMd6K1nak/EUOmSA9EYVSEQeYnRga6U2qvNR55iZskkPp1aYYFF7ZiGL6CqTqu5gTeMX5LJ2V9C/4CDdyceL8ymwArg27ER+7KP+yVwXeZj4trwRigN34dSGanNE+3/XERtz0B9mSxgmMG5OhfWqCm80NAZMRoaLUjfSrQF12P9yuej9QrQf0eJ4rAQZpvuK2kqbSwfkXqFt90jEGUK88bA3/Q/j/wSw7Ron9BDQ6g8bN97XYGgIgBc5jlESrMUBpqVvJq8KE/3v+LSpuax9/1EIJVW3aakD0OJYRpCjpMPZNzz1dbSHdEcqyrkM4NfOtxnCnPNMXox+8OTE+f+IEvfwggfTVCnoTBXACE9tV9f6maU3n2rzuAWVQrHa4lz1EspqSSHasEbkbVxorUZ0H3HXUhX3Yh2upXVfmWjYSh+u/ftK03F9awrDs0OxoqBRol7UYUkyGvfazIKKHrpCvZXqoN1ZvW++bDGD8iNWKWOBDYLPmfEp7h/BMq1a7picGigyvMP4ufAD7E9hOQdsRCvoM1Srpptqy1XKgh0ytOspYYyRRjg7ExuVEG0GH6ysEZB4IMTUvH8Pjz2soF6BrWvmx0vIsSK4WVEW84l0uNpIiE3afunWA1r6WwfLHlKHnnYdefwnPU7ezpa8klvOIEePdJwLUXBncNC5fXvUIp0LiuzTFBsGqAZVg8n2oHOxh8Z04zh6Lk+yDxxpGzN6b2tNNIQDHUUVKZRewdoKwS/1nc8o+WdZBHCrqTS6p4HzvP2ydlqwTqOgH260yIdz0e5Cm711vAPWO 94lT8Q3y MImj2SvIlTGgEPia2s5VYBYX15+V3cMhxFY2EvpBdNe4NMW1zOOaASQWYKrJCg2sOWtyUIVjQiC0FAztzCZliLCwv843KxHGzmnNvpfkgBakOL9ZS7uf2xwsNrw== 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: 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 automatically > * 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 walking scheme, as behind the scenes the cleanup is deferred until an RCU grace 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 called while holding the write lock. A future change to KVM will further relax the locking mechanics around the stage-2 page tables to allow lock-free walkers protected only by RCU. As such, switch to the RCU-safe mechanism 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_pgtable_visit_ctx *ctx, kvm_granule_size(ctx->level)); if (childp) - mm_ops->put_page(childp); + mm_ops->free_removed_table(childp, ctx->level); return 0; } -- Thanks, Oliver