From patchwork Fri May 26 23:44:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13257481 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 C2E9CC77B7C for ; Fri, 26 May 2023 23:50:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B301900003; Fri, 26 May 2023 19:50:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 561636B0072; Fri, 26 May 2023 19:50:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 429D6900003; Fri, 26 May 2023 19:50:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2FAE76B0071 for ; Fri, 26 May 2023 19:50:47 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D9E5C160FC5 for ; Fri, 26 May 2023 23:50:46 +0000 (UTC) X-FDA: 80834053692.15.E727C00 Received: from mail-vk1-f202.google.com (mail-vk1-f202.google.com [209.85.221.202]) by imf15.hostedemail.com (Postfix) with ESMTP id E8620A000E for ; Fri, 26 May 2023 23:50:44 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=tM0X4+0+; spf=pass (imf15.hostedemail.com: domain of 3bkRxZAYKCOUfbgOHVNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--yuzhao.bounces.google.com designates 209.85.221.202 as permitted sender) smtp.mailfrom=3bkRxZAYKCOUfbgOHVNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--yuzhao.bounces.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=1685145044; 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=9t4P2GvyPz5aecPxpWswvlll2je3r362Snycxj82bkU=; b=kxF7Kg+IU8pTKAumyznfCwUjioGCq1rjuHmi3VvgHNcbwkoqCl3yJBcY3CJvU4FT343pfj 3r5Dayt6PnQpaSRG5SpBgVf5vRck0MkT+nCaoV87bc1dfogvhCKBVw9iX/qq2tlXQ0rHv6 5F1sq0HD0Ax06IRfTK2IQBUIZz7OU2k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685145045; a=rsa-sha256; cv=none; b=ZdJ/gc7NEYZrx9D9wvpACT6/+YAm9NwxwzHsM10PbU1ynFu0UbbWaQ1586OzdPxhHkJr2n V6+OZ/pWNkW/s2z5qnNY12ur+K6/Hi9fiENhqENHbbRZow3/K/wdLV/hcYVUdIawft/eGN iwpsv1u507eZWIz1c4d+BUAeEmhcMIE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=tM0X4+0+; spf=pass (imf15.hostedemail.com: domain of 3bkRxZAYKCOUfbgOHVNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--yuzhao.bounces.google.com designates 209.85.221.202 as permitted sender) smtp.mailfrom=3bkRxZAYKCOUfbgOHVNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-vk1-f202.google.com with SMTP id 71dfb90a1353d-456f19ee641so1163307e0c.0 for ; Fri, 26 May 2023 16:50:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685145044; x=1687737044; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9t4P2GvyPz5aecPxpWswvlll2je3r362Snycxj82bkU=; b=tM0X4+0+KNJG57eNQDBDXkefF1mnzRqNrsk5Lpmj4vnB41bmjx5s/E/aQiCko2nR2H dc3/aDoEiGxOM/755MExbY4j+S9GCT2IB7fJzmZDNsjcj/a/lwsOBIRWjUXkEOGKXfxZ qHMEHhgAFNNtWFUavJ2kDc95CHOzOWbPGWKVA2lnykxkVX95BXBAGQoxzGvIKIa+vXLL 9fniYEWvL3B7m0gVvrngoh/GiWh4KRDGcuy1MNvBDsUl+EY6G/HAstXJhUsQ/wP/gzvx OmJGbc+fOFofTDeZFlpMX6+spIxmCmgjkvRld21l9RPsFXfr0Knu6VwHGYHYd0biT+0I UiAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685145044; x=1687737044; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9t4P2GvyPz5aecPxpWswvlll2je3r362Snycxj82bkU=; b=PK6hYNttwNGaRYpV9vqKbphQ1ELDtCwF3sjSigeG8tj+2YCmuvoVc2zBDOtg7NRpQ2 2qbb7rOl8yd3iG5MNhVT2ma8knIwIeKeLeSC9H8DpedtOjm9xRXMZwss6zt6ltrdfgkU ZjJzmXe75jHe5VXPmJRSihxm3ILp8Mmr0nqLlE5/JW1cydDQqaM5vWgcLY+4qgz4Ez2h NjXgqTpo+kOwiLlWeujhGv4MjFRHMxzoAcssbR4BC2Q/mlwt/LstTyy4bi+Ntk06BGI5 oH14jN5eJaD3vfeUmEcq9AvEssD11AzNHgXQix1q4ZuCv0Dd+FyGVS2pSn6N+2O9z4jz CQWQ== X-Gm-Message-State: AC+VfDw++XYNU+1YnCVPr3ODBOar2zBPEAn1yRPFmgKmXxjZpRyVcS7I acjq1kgkxzxwwnk1I+Empa9c80xpDRE= X-Google-Smtp-Source: ACHHUZ671mv9b1WNngTqma0ZpDsFyu7jCdBVYj6cC/Q6bUpKz7y4SPqrDazly1Z4TuFOLliyXqQloqgktcM= X-Received: from yuzhao.bld.corp.google.com ([2620:15c:183:200:910f:8a15:592b:2087]) (user=yuzhao job=sendgmr) by 2002:a81:b627:0:b0:559:d859:d749 with SMTP id u39-20020a81b627000000b00559d859d749mr437593ywh.5.1685144686225; Fri, 26 May 2023 16:44:46 -0700 (PDT) Date: Fri, 26 May 2023 17:44:29 -0600 In-Reply-To: <20230526234435.662652-1-yuzhao@google.com> Message-Id: <20230526234435.662652-5-yuzhao@google.com> Mime-Version: 1.0 References: <20230526234435.662652-1-yuzhao@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Subject: [PATCH mm-unstable v2 04/10] kvm/arm64: make stage2 page tables RCU safe From: Yu Zhao To: Andrew Morton , Paolo Bonzini Cc: 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 , Oliver Upton , 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, Yu Zhao X-Stat-Signature: wrjydh43aas6a9a6sdscuozcp75wyy6e X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: E8620A000E X-Rspam-User: X-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R78EZhZoRU6M0Lsiuqw3tzH5tkfN2ivpjre4pDRzkzu3m6ESxA/lIefYCXO1kDO3G72hQ2XonNeR860nQYTvWcIMMPZ/IK4WGGffXhlfnn1KRwtzZ9yGlovmat1lwIO5bwSZ2yNfOvMsPKIc7H/n+U0HpsfEz4jTovJm2qj6dL7lbxgAV0JoZtj1z1TiiWN8Qp1WO6xY/LicoBL9IV8pFvd//eklzYWIAPL3MqWYpiTJEMJ4T5ARkwbxFTFz8z9DYskYHfL+9BEdpAkkeweLPjALkIalC9guTMNCVe30Z97IIXSSzuuUirG90kVnwI3ag5Kn8/4nENluVO3mXH7uHAn8H/iDU1b/jNZwS28AD3aN61VacQ8VYvewni1OMu78GLMpJWP2NLfDEAuBhAGGg2rlpCFYCy/0rXR31FYToL0rYF7NFdDvm5CV6WCZhcQQsIhbQaDwlqTyMETebIwqBVuxO0DHiUTzvYpvkQLiXxG9GEQDTnDEnfKObctemv2wtUf1ce5ktDhnYNDEQarCFHvoqirI0YdXId7Vv6NeV5UrggU0M0clgH0Ovy4GDwZzd0= 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: 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. * @page_count: Return the refcount of a page. * @phys_to_virt: Convert a physical address into a virtual * address mapped in the current context. @@ -170,6 +171,7 @@ struct kvm_pgtable_mm_ops { void (*free_removed_table)(void *addr, u32 level); void (*get_page)(void *addr); void (*put_page)(void *addr); + void (*put_page_rcu)(void *addr); int (*page_count)(void *addr); void* (*phys_to_virt)(phys_addr_t phys); phys_addr_t (*virt_to_phys)(void *addr); diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 14391826241c..ee93271035d9 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -191,6 +191,7 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) */ void kvm_arch_destroy_vm(struct kvm *kvm) { + kvm_free_stage2_pgd(&kvm->arch.mmu); bitmap_free(kvm->arch.pmu_filter); free_cpumask_var(kvm->arch.supported_cpus); diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 24678ccba76a..dbace4c6a841 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -988,8 +988,12 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx, mm_ops->dcache_clean_inval_poc(kvm_pte_follow(ctx->old, mm_ops), kvm_granule_size(ctx->level)); - if (childp) - mm_ops->put_page(childp); + if (childp) { + if (mm_ops->put_page_rcu) + mm_ops->put_page_rcu(childp); + else + mm_ops->put_page(childp); + } return 0; } diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 3b9d4d24c361..c3b3e2afe26f 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -172,6 +172,21 @@ static int kvm_host_page_count(void *addr) return page_count(virt_to_page(addr)); } +static void kvm_s2_rcu_put_page(struct rcu_head *head) +{ + put_page(container_of(head, struct page, rcu_head)); +} + +static void kvm_s2_put_page_rcu(void *addr) +{ + struct page *page = virt_to_page(addr); + + if (kvm_host_page_count(addr) == 1) + kvm_account_pgtable_pages(addr, -1); + + call_rcu(&page->rcu_head, kvm_s2_rcu_put_page); +} + static phys_addr_t kvm_host_pa(void *addr) { return __pa(addr); @@ -704,6 +719,7 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { .free_removed_table = stage2_free_removed_table, .get_page = kvm_host_get_page, .put_page = kvm_s2_put_page, + .put_page_rcu = kvm_s2_put_page_rcu, .page_count = kvm_host_page_count, .phys_to_virt = kvm_host_va, .virt_to_phys = kvm_host_pa, @@ -1877,7 +1893,6 @@ void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) void kvm_arch_flush_shadow_all(struct kvm *kvm) { - kvm_free_stage2_pgd(&kvm->arch.mmu); } void kvm_arch_flush_shadow_memslot(struct kvm *kvm,