From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49BEC606A31E for ; Mon, 23 Nov 2026 17:51:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=HLczT8Oh; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nSRmqnb+ DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=BmdihYOxirTm+1eTZLfQO693ehUdXJrj7zcBiNWtsTU=; b=HLczT8OhzVO9Ceyn+L8QO6Tfz1 OsnVc6tbQ0vYBIfImPj4DvAz6Qmyy60ezA+Yi1CfOJGxoqCUaJasX/OQ/tDnUfyzwgU2KW8tIFQh9 4ysaMY/7kC29nvPZ0AFIyZwOBmz4hfYffC7eH7dJriRscLHotPGB+4pZ7LPWO9S2zVgOTabjGZHG7 2x+K1qSt39kCe1kiXWIG9oW53OWatSFUeXUUjh34SfjUThZwK7B7SP8NFYxITZ7TICpYHyTxuWD97 f5pC+v0Hj8bmsBCOyjmZ/XTVrgioontgGnclRiIE4sber721BMLzoTZdkOGfRcx+4PQhLmkr2JVpt 7gF0qyAg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xVYBr-00000004Qbr-3QAE; Mon, 23 Nov 2026 17:50:55 +0000 Received: from sea.source.kernel.org ([172.234.252.31]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xVYBr-00000004QbV-17wd for linux-arm-kernel@lists.infradead.org; Mon, 23 Nov 2026 17:50:55 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 0CDC6430A6; Mon, 23 Nov 2026 17:50:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 654B31F000E1; Mon, 23 Nov 2026 17:50:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795456254; bh=BmdihYOxirTm+1eTZLfQO693ehUdXJrj7zcBiNWtsTU=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=nSRmqnb+fUbrWxvbElJY6njf1lvjAVkusLXLFpjaFxEgQ+kYXgZgf4ZMN68xKQ+YZ ZDuOeDeOvEWuXxAdz4qv+/1uDMRZHkpDyX1hQ1mnfoWDZcWUddJmIEI6qTPdBUTbDg awoNG1kWDag7/uPxC53z6rUsK6vHnGFypQu7hk2+cVKf5+sypF3nTrEcss5aYpXMw+ xoDcJp5Jg6FpDbcSSK1DeP6iO6/SlQUJfg6gEnYBS9hlbKJYlzvCxsOFMRDYGV3Chi 5TT1OusQZKg4Q+PiFJOYRjcC7vso4DnCTC4vreS0mgIU0gaaEptbrENdnfFZRF2lxm Et+USmiUgrwyw== Date: Mon, 23 Nov 2026 17:50:46 +0000 From: "Lorenzo Stoakes (ARM)" To: Marc Zyngier Cc: Jia He , Oliver Upton , Fuad Tabba , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mark Rutland , Paolo Bonzini , Sean Christopherson , Shuah Khan , Sascha Bischoff , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH 1/3] KVM: arm64: Don't load hardware vCPU state before first run Message-ID: References: <20261123150917.2679600-1-justin.he@arm.com> <20261123150917.2679600-2-justin.he@arm.com> <86eccbzcqh.wl-maz@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <86eccbzcqh.wl-maz@kernel.org> X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-5.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Mon, Nov 23, 2026 at 05:20:06PM +0000, Marc Zyngier wrote: > [+ Lorenzo] > > Hey Jia, > > On Mon, 23 Nov 2026 15:09:15 +0000, > Jia He wrote: > > > > KVM_PRE_FAULT_MEMORY calls vcpu_load() as soon as KVM_ARM_VCPU_INIT has > > completed. On arm64, however, kvm->arch.sysreg_masks, the trap > > configuration, vncr_tlb and nested vGIC state are only set up on the > > first KVM_RUN in kvm_arch_vcpu_run_pid_change(). > > I think I'm going to queue a patch disabling prefaulting immediately, > and then we can fix this crap. Ack, will prioritise looking through Jia's series. > > > > > Running the full vcpu_load() path before then reads unsanitised guest > > sysregs and touches uninitialised nested state: get_s2_mmu_nested() > > populates nested_mmus[] from raw VTTBR_EL2/VTCR_EL2/HCR_EL2 values, FGT > > and HCRX_EL2 are written before kvm_calculate_traps() has run, and > > kvm_vgic_load() can enter vgic_v3_load_nested() early. In particular, if > > userspace creates an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac cleared to 0 > > (so vncr_tlb is never allocated), sets HCR_EL2.NV=1 and an EL1 PSTATE, > > and calls KVM_PRE_FAULT_MEMORY, kvm_vcpu_load_hw_mmu() queues > > KVM_REQ_MAP_L1_VNCR_EL2. The next KVM_RUN then dereferences a NULL > > vncr_tlb in kvm_map_l1_vncr() with preemption disabled and kvm->mmu_lock > > held for read: > > > > [ 73.636014] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000078 > > [ 73.801972] pc : kvm_map_l1_vncr+0x48/0x2d0 > > [ 73.884820] Call trace: > > [ 73.887253] kvm_map_l1_vncr+0x48/0x2d0 (P) > > [ 73.891422] check_nested_vcpu_requests+0xb8/0x168 > > [ 73.896200] check_vcpu_requests+0x2a0/0x428 > > [ 73.900458] kvm_arch_vcpu_ioctl_run+0x2d8/0x6d8 > > [ 73.905062] kvm_vcpu_ioctl+0x388/0xaf0 > > > > KVM_PRE_FAULT_MEMORY still needs generic vcpu_load() to set > > kvm_running_vcpu for dirty-ring tracking in mark_page_dirty_in_slot(), > > but kvm_arch_vcpu_pre_fault_memory() only walks and populates > > &kvm->arch.mmu and does not use any hardware vCPU state. Skip the arch > > vcpu_load()/vcpu_put() callbacks until vcpu_has_run_once() is true. In > > KVM_RUN, vcpu->pid is set under vcpu->mutex right after > > kvm_arch_vcpu_run_pid_change() succeeds and before > > kvm_arch_vcpu_ioctl_run() calls vcpu_load(). > > > > Since kvm_running_vcpu can now be set without VHE state on the CPU, use > > host_ctxt->__hyp_running_vcpu in VHE enter_vmid_context() (matching > > nVHE) > > Can you clarify under which circumstances we end-up on the TLB > invalidation path? Simply faulting things in should not result in > invalidation. Unless we end-up with MMU notifiers firing? Yeah I think the issue is that an enter_vmid_context() / exit_vmid_context() pair flushing some other MMU could end up writing the vCPU's (canonical) hw_mmu into VTTBR_EL2/VTCR_EL2 on exit, even though it was never loaded in the first place. But that would just get overwritten when next loaded anyway? So I think it's probably the right change but makes no difference in practice ATM? > > > and check vcpu_has_run_once() in vgic_maintenance_handler(). > > I have similar questions here. It isn't obvious how you end-up getting > a VGIC MI if you don't run and don't load the state on the CPU. Yeah I don't think there is a way? Maybe this is just defensive? > > Thanks, > > M. > > -- > Without deviation from the norm, progress is not possible. -- Cheers, Lorenzo