* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown [not found] ` <20230512233127.804012-2-seanjc@google.com> @ 2023-12-09 7:26 ` Gowans, James 2023-12-10 4:53 ` Eric W. Biederman 2023-12-11 17:34 ` Sean Christopherson 0 siblings, 2 replies; 11+ messages in thread From: Gowans, James @ 2023-12-09 7:26 UTC (permalink / raw) To: pbonzini@redhat.com, Graf (AWS), Alexander, seanjc@google.com, Schönherr, Jan H., ebiederm@xmission.com Cc: yuzenghui@huawei.com, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, chenhuacai@kernel.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, maz@kernel.org, kvm@vger.kernel.org, aleksandar.qemu.devel@gmail.com, anup@brainfault.org, kexec@lists.infradead.org Hi Sean, Blast from the past but I've just been bitten by this patch when rebasing across v6.4. On Fri, 2023-05-12 at 16:31 -0700, Sean Christopherson wrote: > Use syscore_ops.shutdown to disable hardware virtualization during a > reboot instead of using the dedicated reboot_notifier so that KVM disables > virtualization _after_ system_state has been updated. This will allow > fixing a race in KVM's handling of a forced reboot where KVM can end up > enabling hardware virtualization between kernel_restart_prepare() and > machine_restart(). The issue is that, AFAICT, the syscore_ops.shutdown are not called when doing a kexec. Reboot notifiers are called across kexec via: kernel_kexec kernel_restart_prepare blocking_notifier_call_chain kvm_reboot So after this patch, KVM is not shutdown during kexec; if hardware virt mode is enabled then the kexec hangs in exactly the same manner as you describe with the reboot. Some specific shutdown callbacks, for example IOMMU, HPET, IRQ, etc are called in native_machine_shutdown, but KVM is not one of these. Thoughts on possible ways to fix this: a) go back to reboot notifiers b) get kexec to call syscore_shutdown() to invoke all of these callbacks c) Add a KVM-specific callback to native_machine_shutdown(); we only need this for Intel x86, right? My slight preference is towards adding syscore_shutdown() to kexec, but I'm not sure that's feasible. Adding kexec maintainers for input. JG _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-09 7:26 ` [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown Gowans, James @ 2023-12-10 4:53 ` Eric W. Biederman 2023-12-11 7:54 ` Gowans, James 2023-12-11 17:34 ` Sean Christopherson 1 sibling, 1 reply; 11+ messages in thread From: Eric W. Biederman @ 2023-12-10 4:53 UTC (permalink / raw) To: Gowans, James Cc: pbonzini@redhat.com, Graf (AWS), Alexander, seanjc@google.com, Schönherr, Jan H., yuzenghui@huawei.com, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, chenhuacai@kernel.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, maz@kernel.org, kvm@vger.kernel.org, aleksandar.qemu.devel@gmail.com, anup@brainfault.org, kexec@lists.infradead.org "Gowans, James" <jgowans@amazon.com> writes: > Hi Sean, > > Blast from the past but I've just been bitten by this patch when > rebasing across v6.4. > > On Fri, 2023-05-12 at 16:31 -0700, Sean Christopherson wrote: >> Use syscore_ops.shutdown to disable hardware virtualization during a >> reboot instead of using the dedicated reboot_notifier so that KVM disables >> virtualization _after_ system_state has been updated. This will allow >> fixing a race in KVM's handling of a forced reboot where KVM can end up >> enabling hardware virtualization between kernel_restart_prepare() and >> machine_restart(). > > The issue is that, AFAICT, the syscore_ops.shutdown are not called when > doing a kexec. Reboot notifiers are called across kexec via: > > kernel_kexec > kernel_restart_prepare > blocking_notifier_call_chain > kvm_reboot > > So after this patch, KVM is not shutdown during kexec; if hardware virt > mode is enabled then the kexec hangs in exactly the same manner as you > describe with the reboot. kernel_restart_prepare calls device_shutdown. Which should call all of the shutdown operations. This has been the way the code has been structured since December 2005. > Some specific shutdown callbacks, for example IOMMU, HPET, IRQ, etc are > called in native_machine_shutdown, but KVM is not one of these. > > Thoughts on possible ways to fix this: > a) go back to reboot notifiers > b) get kexec to call syscore_shutdown() to invoke all of these callbacks > c) Add a KVM-specific callback to native_machine_shutdown(); we only > need this for Intel x86, right? > > My slight preference is towards adding syscore_shutdown() to kexec, but > I'm not sure that's feasible. Adding kexec maintainers for input. Why isn't device_suthdown calling syscore_shutdown? What problem are you running into with your rebase that worked with reboot notifiers that is not working with syscore_shutdown? Eric _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-10 4:53 ` Eric W. Biederman @ 2023-12-11 7:54 ` Gowans, James 2023-12-11 10:27 ` Gowans, James 0 siblings, 1 reply; 11+ messages in thread From: Gowans, James @ 2023-12-11 7:54 UTC (permalink / raw) To: Graf (AWS), Alexander, seanjc@google.com, ebiederm@xmission.com, Schönherr, Jan H. Cc: yuzenghui@huawei.com, kvm-riscv@lists.infradead.org, kexec@lists.infradead.org, james.morse@arm.com, oliver.upton@linux.dev, suzuki.poulose@arm.com, chenhuacai@kernel.org, atishp@atishpatra.org, linux-kernel@vger.kernel.org, maz@kernel.org, pbonzini@redhat.com, kvm@vger.kernel.org, kvmarm@lists.linux.dev, anup@brainfault.org, aleksandar.qemu.devel@gmail.com On Sat, 2023-12-09 at 22:53 -0600, Eric W. Biederman wrote: > > On Fri, 2023-05-12 at 16:31 -0700, Sean Christopherson wrote: > > > Use syscore_ops.shutdown to disable hardware virtualization during a > > > reboot instead of using the dedicated reboot_notifier so that KVM disables > > > virtualization _after_ system_state has been updated. This will allow > > > fixing a race in KVM's handling of a forced reboot where KVM can end up > > > enabling hardware virtualization between kernel_restart_prepare() and > > > machine_restart(). > > > > The issue is that, AFAICT, the syscore_ops.shutdown are not called when > > doing a kexec. Reboot notifiers are called across kexec via: > > > > kernel_kexec > > kernel_restart_prepare > > blocking_notifier_call_chain > > kvm_reboot > > > > So after this patch, KVM is not shutdown during kexec; if hardware virt > > mode is enabled then the kexec hangs in exactly the same manner as you > > describe with the reboot. > > kernel_restart_prepare calls device_shutdown. Which should call all > of the shutdown operations. This has been the way the code has been > structured since December 2005. Yes, kernel_reset_prepare calls device_shutdown which calls dev->driver->shutdown for each dev which has a driver. KVM, however, is not a dev with a driver, hence doesn't have a dev->driver->shutdown callback. So KVM is no-op'ed in device_shutdown. KVM adds its shutdown callback to syscore_ops and expects to be shut down that way. > > > Some specific shutdown callbacks, for example IOMMU, HPET, IRQ, etc are > > called in native_machine_shutdown, but KVM is not one of these. > > > > Thoughts on possible ways to fix this: > > a) go back to reboot notifiers > > b) get kexec to call syscore_shutdown() to invoke all of these callbacks > > c) Add a KVM-specific callback to native_machine_shutdown(); we only > > need this for Intel x86, right? > > > > My slight preference is towards adding syscore_shutdown() to kexec, but > > I'm not sure that's feasible. Adding kexec maintainers for input. > > Why isn't device_suthdown calling syscore_shutdown? I'm not sure - that would indeed be one way to fix; adding a call to syscore_shutdown() inside device_shutdown. We may need to clean up other callers to not do their own syscore_shutdown and rather depend on device_shutdown to do it all. Would you support adding syscore_shutdown() to device_shutdown()? > > What problem are you running into with your rebase that worked with > reboot notifiers that is not working with syscore_shutdown? Prior to this commit [1] which changed KVM from reboot notifiers to syscore_ops, KVM's reboot notifier shutdown callback was invoked on kexec via kernel_restart_prepare. After this commit, KVM is not being shut down because currently the kexec flow does not call syscore_shutdown. JG [1] https://github.com/torvalds/linux/commit/6735150b6997 _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-11 7:54 ` Gowans, James @ 2023-12-11 10:27 ` Gowans, James 2023-12-11 23:50 ` Eric W. Biederman 0 siblings, 1 reply; 11+ messages in thread From: Gowans, James @ 2023-12-11 10:27 UTC (permalink / raw) To: Graf (AWS), Alexander, seanjc@google.com, ebiederm@xmission.com, Schönherr, Jan H. Cc: yuzenghui@huawei.com, kvm-riscv@lists.infradead.org, kexec@lists.infradead.org, james.morse@arm.com, oliver.upton@linux.dev, suzuki.poulose@arm.com, chenhuacai@kernel.org, atishp@atishpatra.org, linux-kernel@vger.kernel.org, maz@kernel.org, pbonzini@redhat.com, kvm@vger.kernel.org, kvmarm@lists.linux.dev, anup@brainfault.org, aleksandar.qemu.devel@gmail.com On Mon, 2023-12-11 at 09:54 +0200, James Gowans wrote: > > > > What problem are you running into with your rebase that worked with > > reboot notifiers that is not working with syscore_shutdown? > > Prior to this commit [1] which changed KVM from reboot notifiers to > syscore_ops, KVM's reboot notifier shutdown callback was invoked on > kexec via kernel_restart_prepare. > > After this commit, KVM is not being shut down because currently the > kexec flow does not call syscore_shutdown. I think I missed what you're asking here; you're asking for a reproducer for the specific failure? 1. Launch a QEMU VM with -enable-kvm flag 2. Do an immediate (-f flag) kexec: kexec -f --reuse-cmdline ./bzImage Somewhere after doing the RET to new kernel in the relocate_kernel asm function the new kernel starts triple faulting; I can't exactly figure out where but I think it has to do with the new kernel trying to modify CR3 while the VMXE bit is still set in CR4 causing the triple fault. If KVM has been shut down via the shutdown callback, or alternatively if the QEMU process has actually been killed first (by not doing a -f exec) then the VMXE bit is clear and the kexec goes smoothly. So, TL;DR: kexec -f use to work with a KVM VM active, now it goes into a triple fault crash. JG _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-11 10:27 ` Gowans, James @ 2023-12-11 23:50 ` Eric W. Biederman 2023-12-12 8:50 ` Gowans, James 0 siblings, 1 reply; 11+ messages in thread From: Eric W. Biederman @ 2023-12-11 23:50 UTC (permalink / raw) To: Gowans, James Cc: Graf (AWS), Alexander, seanjc@google.com, Schönherr, Jan H., yuzenghui@huawei.com, kvm-riscv@lists.infradead.org, kexec@lists.infradead.org, james.morse@arm.com, oliver.upton@linux.dev, suzuki.poulose@arm.com, chenhuacai@kernel.org, atishp@atishpatra.org, linux-kernel@vger.kernel.org, maz@kernel.org, pbonzini@redhat.com, kvm@vger.kernel.org, kvmarm@lists.linux.dev, anup@brainfault.org, aleksandar.qemu.devel@gmail.com "Gowans, James" <jgowans@amazon.com> writes: > On Mon, 2023-12-11 at 09:54 +0200, James Gowans wrote: >> > >> > What problem are you running into with your rebase that worked with >> > reboot notifiers that is not working with syscore_shutdown? >> >> Prior to this commit [1] which changed KVM from reboot notifiers to >> syscore_ops, KVM's reboot notifier shutdown callback was invoked on >> kexec via kernel_restart_prepare. >> >> After this commit, KVM is not being shut down because currently the >> kexec flow does not call syscore_shutdown. > > I think I missed what you're asking here; you're asking for a reproducer > for the specific failure? > > 1. Launch a QEMU VM with -enable-kvm flag > > 2. Do an immediate (-f flag) kexec: > kexec -f --reuse-cmdline ./bzImage > > Somewhere after doing the RET to new kernel in the relocate_kernel asm > function the new kernel starts triple faulting; I can't exactly figure > out where but I think it has to do with the new kernel trying to modify > CR3 while the VMXE bit is still set in CR4 causing the triple fault. > > If KVM has been shut down via the shutdown callback, or alternatively if > the QEMU process has actually been killed first (by not doing a -f exec) > then the VMXE bit is clear and the kexec goes smoothly. > > So, TL;DR: kexec -f use to work with a KVM VM active, now it goes into a > triple fault crash. You mentioned I rebase so I thought your were backporting kernel patches. By rebase do you mean you porting your userspace to a newer kernel? In any event I believe the bug with respect to kexec was introduced in commit 6f389a8f1dd2 ("PM / reboot: call syscore_shutdown() after disable_nonboot_cpus()"). That is where syscore_shutdown was removed from kernel_restart_prepare(). At this point it looks like someone just needs to add the missing syscore_shutdown call into kernel_kexec() right after migrate_to_reboot_cpu() is called. That said I am not seeing the reboot notifiers being called on the kexec path either so your issue with kvm might be deeper. Eric _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-11 23:50 ` Eric W. Biederman @ 2023-12-12 8:50 ` Gowans, James 2023-12-12 13:38 ` Paolo Bonzini 2023-12-13 6:47 ` Gowans, James 0 siblings, 2 replies; 11+ messages in thread From: Gowans, James @ 2023-12-12 8:50 UTC (permalink / raw) To: Graf (AWS), Alexander, seanjc@google.com, ebiederm@xmission.com, Schönherr, Jan H. Cc: yuzenghui@huawei.com, kexec@lists.infradead.org, kvm-riscv@lists.infradead.org, james.morse@arm.com, oliver.upton@linux.dev, suzuki.poulose@arm.com, chenhuacai@kernel.org, atishp@atishpatra.org, linux-kernel@vger.kernel.org, maz@kernel.org, pbonzini@redhat.com, kvm@vger.kernel.org, kvmarm@lists.linux.dev, aleksandar.qemu.devel@gmail.com, anup@brainfault.org On Mon, 2023-12-11 at 17:50 -0600, Eric W. Biederman wrote: > "Gowans, James" <jgowans@amazon.com> writes: > > > On Mon, 2023-12-11 at 09:54 +0200, James Gowans wrote: > > > > > > > > What problem are you running into with your rebase that worked with > > > > reboot notifiers that is not working with syscore_shutdown? > > > > > > Prior to this commit [1] which changed KVM from reboot notifiers to > > > syscore_ops, KVM's reboot notifier shutdown callback was invoked on > > > kexec via kernel_restart_prepare. > > > > > > After this commit, KVM is not being shut down because currently the > > > kexec flow does not call syscore_shutdown. > > > > I think I missed what you're asking here; you're asking for a reproducer > > for the specific failure? > > > > 1. Launch a QEMU VM with -enable-kvm flag > > > > 2. Do an immediate (-f flag) kexec: > > kexec -f --reuse-cmdline ./bzImage > > > > Somewhere after doing the RET to new kernel in the relocate_kernel asm > > function the new kernel starts triple faulting; I can't exactly figure > > out where but I think it has to do with the new kernel trying to modify > > CR3 while the VMXE bit is still set in CR4 causing the triple fault. > > > > If KVM has been shut down via the shutdown callback, or alternatively if > > the QEMU process has actually been killed first (by not doing a -f exec) > > then the VMXE bit is clear and the kexec goes smoothly. > > > > So, TL;DR: kexec -f use to work with a KVM VM active, now it goes into a > > triple fault crash. > > You mentioned I rebase so I thought your were backporting kernel patches. > By rebase do you mean you porting your userspace to a newer kernel? I've been working on some patches and when I rebased my work-in-progress patches to latest master then kexec stopped working when KVM VMs exist. Originally the WIP patches were based on an older stable version. > > In any event I believe the bug with respect to kexec was introduced in > commit 6f389a8f1dd2 ("PM / reboot: call syscore_shutdown() after > disable_nonboot_cpus()"). That is where syscore_shutdown was removed > from kernel_restart_prepare(). > > At this point it looks like someone just needs to add the missing > syscore_shutdown call into kernel_kexec() right after > migrate_to_reboot_cpu() is called. Seems good and I'm happy to do that; one thing we need to check first: are all CPUs online at that point? The commit message for 6f389a8f1dd2 ("PM / reboot: call syscore_shutdown() after disable_nonboot_cpus()") speaks about: "one CPU on-line and interrupts disabled" when syscore_shutdown is called. KVM's syscore shutdown hook does: on_each_cpu(hardware_disable_nolock, NULL, 1); ... so that smells to me like it wants all the CPUs to be online at kvm_shutdown point. It's not clear to me: 1. Does hardware_disable_nolock actually need to be done on *every* CPU or would the offlined ones be fine to ignore because they will be reset and the VMXE bit will be cleared that way? With cooperative CPU handover we probably do indeed want to do this on every CPU and not depend on resetting. 2. Are CPUs actually offline at this point? When that commit was authored there used to be a call to hardware_disable_nolock() but that's not there anymore. > > That said I am not seeing the reboot notifiers being called on the kexec > path either so your issue with kvm might be deeper. Previously it was called via: kernel_kexec kernel_restart_prepare blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); kvm_shutdown JG _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-12 8:50 ` Gowans, James @ 2023-12-12 13:38 ` Paolo Bonzini 2023-12-13 6:47 ` Gowans, James 1 sibling, 0 replies; 11+ messages in thread From: Paolo Bonzini @ 2023-12-12 13:38 UTC (permalink / raw) To: Gowans, James Cc: Graf (AWS), Alexander, seanjc@google.com, ebiederm@xmission.com, Schönherr, Jan H., yuzenghui@huawei.com, kexec@lists.infradead.org, kvm-riscv@lists.infradead.org, james.morse@arm.com, oliver.upton@linux.dev, suzuki.poulose@arm.com, chenhuacai@kernel.org, atishp@atishpatra.org, linux-kernel@vger.kernel.org, maz@kernel.org, kvm@vger.kernel.org, kvmarm@lists.linux.dev, aleksandar.qemu.devel@gmail.com, anup@brainfault.org On Tue, Dec 12, 2023 at 9:51 AM Gowans, James <jgowans@amazon.com> wrote: > 1. Does hardware_disable_nolock actually need to be done on *every* CPU > or would the offlined ones be fine to ignore because they will be reset > and the VMXE bit will be cleared that way? With cooperative CPU handover > we probably do indeed want to do this on every CPU and not depend on > resetting. Offlined and onlined CPUs are handled via the CPU hotplug state machine, which calls into kvm_online_cpu and kvm_offline_cpu. Paolo _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-12 8:50 ` Gowans, James 2023-12-12 13:38 ` Paolo Bonzini @ 2023-12-13 6:47 ` Gowans, James 1 sibling, 0 replies; 11+ messages in thread From: Gowans, James @ 2023-12-13 6:47 UTC (permalink / raw) To: Graf (AWS), Alexander, seanjc@google.com, ebiederm@xmission.com, Schönherr, Jan H. Cc: yuzenghui@huawei.com, kexec@lists.infradead.org, kvm-riscv@lists.infradead.org, james.morse@arm.com, oliver.upton@linux.dev, suzuki.poulose@arm.com, chenhuacai@kernel.org, atishp@atishpatra.org, linux-kernel@vger.kernel.org, maz@kernel.org, pbonzini@redhat.com, kvm@vger.kernel.org, kvmarm@lists.linux.dev, aleksandar.qemu.devel@gmail.com, anup@brainfault.org On Tue, 2023-12-12 at 10:50 +0200, James Gowans wrote: > > > > In any event I believe the bug with respect to kexec was introduced in > > commit 6f389a8f1dd2 ("PM / reboot: call syscore_shutdown() after > > disable_nonboot_cpus()"). That is where syscore_shutdown was removed > > from kernel_restart_prepare(). > > > > At this point it looks like someone just needs to add the missing > > syscore_shutdown call into kernel_kexec() right after > > migrate_to_reboot_cpu() is called. > > Seems good and I'm happy to do that; one thing we need to check first: > are all CPUs online at that point? The commit message for > 6f389a8f1dd2 ("PM / reboot: call syscore_shutdown() after disable_nonboot_cpus()") > speaks about: "one CPU on-line and interrupts disabled" when > syscore_shutdown is called. KVM's syscore shutdown hook does: > > on_each_cpu(hardware_disable_nolock, NULL, 1); > > ... so that smells to me like it wants all the CPUs to be online at > kvm_shutdown point. > > It's not clear to me: > > 1. Does hardware_disable_nolock actually need to be done on *every* CPU > or would the offlined ones be fine to ignore because they will be reset > and the VMXE bit will be cleared that way? With cooperative CPU handover > we probably do indeed want to do this on every CPU and not depend on > resetting. > > 2. Are CPUs actually offline at this point? When that commit was > authored there used to be a call to hardware_disable_nolock() but that's > not there anymore. I've sent out a patch: https://lore.kernel.org/kexec/20231213064004.2419447-1-jgowans@amazon.com/T/#u Let's continue the discussion there. JG _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-09 7:26 ` [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown Gowans, James 2023-12-10 4:53 ` Eric W. Biederman @ 2023-12-11 17:34 ` Sean Christopherson 2023-12-11 17:51 ` Sean Christopherson 2023-12-11 18:47 ` Gowans, James 1 sibling, 2 replies; 11+ messages in thread From: Sean Christopherson @ 2023-12-11 17:34 UTC (permalink / raw) To: James Gowans Cc: pbonzini@redhat.com, Alexander Graf, Jan Schönherr, ebiederm@xmission.com, yuzenghui@huawei.com, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, chenhuacai@kernel.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, maz@kernel.org, kvm@vger.kernel.org, aleksandar.qemu.devel@gmail.com, anup@brainfault.org, kexec@lists.infradead.org On Sat, Dec 09, 2023, James Gowans wrote: > Hi Sean, > > Blast from the past but I've just been bitten by this patch when > rebasing across v6.4. > > On Fri, 2023-05-12 at 16:31 -0700, Sean Christopherson wrote: > > Use syscore_ops.shutdown to disable hardware virtualization during a > > reboot instead of using the dedicated reboot_notifier so that KVM disables > > virtualization _after_ system_state has been updated. This will allow > > fixing a race in KVM's handling of a forced reboot where KVM can end up > > enabling hardware virtualization between kernel_restart_prepare() and > > machine_restart(). > > The issue is that, AFAICT, the syscore_ops.shutdown are not called when > doing a kexec. Reboot notifiers are called across kexec via: > > kernel_kexec > kernel_restart_prepare > blocking_notifier_call_chain > kvm_reboot > > So after this patch, KVM is not shutdown during kexec; if hardware virt > mode is enabled then the kexec hangs in exactly the same manner as you > describe with the reboot. > > Some specific shutdown callbacks, for example IOMMU, HPET, IRQ, etc are > called in native_machine_shutdown, but KVM is not one of these. > > Thoughts on possible ways to fix this: > a) go back to reboot notifiers > b) get kexec to call syscore_shutdown() to invoke all of these callbacks > c) Add a KVM-specific callback to native_machine_shutdown(); we only > need this for Intel x86, right? I don't like (c). VMX is the most sensitive/problematic, e.g. the whole blocking of INIT thing, but SVM can also run afoul of EFER.SVME being cleared, and KVM really should leave virtualization enabled across kexec(), even if leaving virtualization enabled is relatively benign on other architectures. One more option would be: d) Add another sycore hook, e.g. syscore_kexec() specifically for this path. > My slight preference is towards adding syscore_shutdown() to kexec, but > I'm not sure that's feasible. Adding kexec maintainers for input. In a vacuum, that'd be my preference too. It's the obvious choice IMO, e.g. the kexec_image->preserve_context path does syscore_suspend() (and then resume(), so it's not completely uncharted territory. However, there's a rather big wrinkle in that not all of the existing .shutdown() implementations are obviously ok to call during kexec. Luckily, AFAICT there are very few users of the syscore .shutdown hook, so it's at least feasible to go that route. x86's mce_syscore_shutdown() should be ok, and arguably is correct, e.g. I don't see how leaving #MC reporting enabled across kexec can work. ledtrig_cpu_syscore_shutdown() is also likely ok and arguably correct. The interrupt controllers though? x86 disables IRQs at the very beginning of machine_kexec(), so it's likely fine. But every other architecture? No clue. E.g. PPC's default_machine_kexec() sends IPIs to shutdown other CPUs, though I have no idea if that can run afoul of any of the paths below. arch/powerpc/platforms/cell/spu_base.c .shutdown = spu_shutdown, arch/x86/kernel/cpu/mce/core.c .shutdown = mce_syscore_shutdown, arch/x86/kernel/i8259.c .shutdown = i8259A_shutdown, drivers/irqchip/irq-i8259.c .shutdown = i8259A_shutdown, drivers/irqchip/irq-sun6i-r.c .shutdown = sun6i_r_intc_shutdown, drivers/leds/trigger/ledtrig-cpu.c .shutdown = ledtrig_cpu_syscore_shutdown, drivers/power/reset/sc27xx-poweroff.c .shutdown = sc27xx_poweroff_shutdown, kernel/irq/generic-chip.c .shutdown = irq_gc_shutdown, virt/kvm/kvm_main.c .shutdown = kvm_shutdown, The whole thing is a bit of a mess. E.g. x86 treats machine_shutdown() from kexec pretty much the same as shutdown for reboot, but other architectures have what appear to be unique paths for handling kexec. FWIW, if we want to go with option (b), syscore_shutdown() hooks could use kexec_in_progress to differentiate between "regular" shutdown/reboot and kexec. _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-11 17:34 ` Sean Christopherson @ 2023-12-11 17:51 ` Sean Christopherson 2023-12-11 18:47 ` Gowans, James 1 sibling, 0 replies; 11+ messages in thread From: Sean Christopherson @ 2023-12-11 17:51 UTC (permalink / raw) To: James Gowans Cc: pbonzini@redhat.com, Alexander Graf, Jan Schönherr, ebiederm@xmission.com, yuzenghui@huawei.com, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, chenhuacai@kernel.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, maz@kernel.org, kvm@vger.kernel.org, aleksandar.qemu.devel@gmail.com, anup@brainfault.org, kexec@lists.infradead.org On Mon, Dec 11, 2023, Sean Christopherson wrote: > On Sat, Dec 09, 2023, James Gowans wrote: > > Hi Sean, > > > > Blast from the past but I've just been bitten by this patch when > > rebasing across v6.4. > > > > On Fri, 2023-05-12 at 16:31 -0700, Sean Christopherson wrote: > > > Use syscore_ops.shutdown to disable hardware virtualization during a > > > reboot instead of using the dedicated reboot_notifier so that KVM disables > > > virtualization _after_ system_state has been updated. This will allow > > > fixing a race in KVM's handling of a forced reboot where KVM can end up > > > enabling hardware virtualization between kernel_restart_prepare() and > > > machine_restart(). > > > > The issue is that, AFAICT, the syscore_ops.shutdown are not called when > > doing a kexec. Reboot notifiers are called across kexec via: > > > > kernel_kexec > > kernel_restart_prepare > > blocking_notifier_call_chain > > kvm_reboot > > > > So after this patch, KVM is not shutdown during kexec; if hardware virt > > mode is enabled then the kexec hangs in exactly the same manner as you > > describe with the reboot. > > > > Some specific shutdown callbacks, for example IOMMU, HPET, IRQ, etc are > > called in native_machine_shutdown, but KVM is not one of these. > > > > Thoughts on possible ways to fix this: > > a) go back to reboot notifiers > > b) get kexec to call syscore_shutdown() to invoke all of these callbacks > > c) Add a KVM-specific callback to native_machine_shutdown(); we only > > need this for Intel x86, right? > > I don't like (c). VMX is the most sensitive/problematic, e.g. the whole blocking > of INIT thing, but SVM can also run afoul of EFER.SVME being cleared, and KVM really > should leave virtualization enabled across kexec(), even if leaving virtualization *shouldn't* > enabled is relatively benign on other architectures. > > One more option would be: > > d) Add another sycore hook, e.g. syscore_kexec() specifically for this path. > > > My slight preference is towards adding syscore_shutdown() to kexec, but > > I'm not sure that's feasible. Adding kexec maintainers for input. > > In a vacuum, that'd be my preference too. It's the obvious choice IMO, e.g. the > kexec_image->preserve_context path does syscore_suspend() (and then resume(), so > it's not completely uncharted territory. > > However, there's a rather big wrinkle in that not all of the existing .shutdown() > implementations are obviously ok to call during kexec. Luckily, AFAICT there are > very few users of the syscore .shutdown hook, so it's at least feasible to go that > route. > > x86's mce_syscore_shutdown() should be ok, and arguably is correct, e.g. I don't > see how leaving #MC reporting enabled across kexec can work. > > ledtrig_cpu_syscore_shutdown() is also likely ok and arguably correct. > > The interrupt controllers though? x86 disables IRQs at the very beginning of > machine_kexec(), so it's likely fine. But every other architecture? No clue. > E.g. PPC's default_machine_kexec() sends IPIs to shutdown other CPUs, though I > have no idea if that can run afoul of any of the paths below. > > arch/powerpc/platforms/cell/spu_base.c .shutdown = spu_shutdown, > arch/x86/kernel/cpu/mce/core.c .shutdown = mce_syscore_shutdown, > arch/x86/kernel/i8259.c .shutdown = i8259A_shutdown, > drivers/irqchip/irq-i8259.c .shutdown = i8259A_shutdown, > drivers/irqchip/irq-sun6i-r.c .shutdown = sun6i_r_intc_shutdown, > drivers/leds/trigger/ledtrig-cpu.c .shutdown = ledtrig_cpu_syscore_shutdown, > drivers/power/reset/sc27xx-poweroff.c .shutdown = sc27xx_poweroff_shutdown, > kernel/irq/generic-chip.c .shutdown = irq_gc_shutdown, > virt/kvm/kvm_main.c .shutdown = kvm_shutdown, > > The whole thing is a bit of a mess. E.g. x86 treats machine_shutdown() from > kexec pretty much the same as shutdown for reboot, but other architectures have > what appear to be unique paths for handling kexec. > > FWIW, if we want to go with option (b), syscore_shutdown() hooks could use > kexec_in_progress to differentiate between "regular" shutdown/reboot and kexec. _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown 2023-12-11 17:34 ` Sean Christopherson 2023-12-11 17:51 ` Sean Christopherson @ 2023-12-11 18:47 ` Gowans, James 1 sibling, 0 replies; 11+ messages in thread From: Gowans, James @ 2023-12-11 18:47 UTC (permalink / raw) To: pbonzini@redhat.com, Graf (AWS), Alexander, seanjc@google.com, Schönherr, Jan H., ebiederm@xmission.com Cc: yuzenghui@huawei.com, kvm-riscv@lists.infradead.org, kexec@lists.infradead.org, james.morse@arm.com, oliver.upton@linux.dev, suzuki.poulose@arm.com, chenhuacai@kernel.org, atishp@atishpatra.org, linux-kernel@vger.kernel.org, maz@kernel.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, aleksandar.qemu.devel@gmail.com, anup@brainfault.org On Mon, 2023-12-11 at 09:34 -0800, Sean Christopherson wrote: > On Sat, Dec 09, 2023, James Gowans wrote: > > Thoughts on possible ways to fix this: > > a) go back to reboot notifiers > > b) get kexec to call syscore_shutdown() to invoke all of these callbacks > > c) Add a KVM-specific callback to native_machine_shutdown(); we only > > need this for Intel x86, right? > > I don't like (c). VMX is the most sensitive/problematic, e.g. the whole blocking > of INIT thing, but SVM can also run afoul of EFER.SVME being cleared, and KVM really > should leave virtualization enabled across kexec(), even if leaving virtualization > enabled is relatively benign on other architectures. Good to know. Agreed that clean shutdown in all cases is best and we discard (c). > > One more option would be: > > d) Add another sycore hook, e.g. syscore_kexec() specifically for this path. > > > My slight preference is towards adding syscore_shutdown() to kexec, but > > I'm not sure that's feasible. Adding kexec maintainers for input. > > In a vacuum, that'd be my preference too. It's the obvious choice IMO, e.g. the > kexec_image->preserve_context path does syscore_suspend() (and then resume(), so > it's not completely uncharted territory. > > However, there's a rather big wrinkle in that not all of the existing .shutdown() > implementations are obviously ok to call during kexec. Luckily, AFAICT there are > very few users of the syscore .shutdown hook, so it's at least feasible to go that > route. > > x86's mce_syscore_shutdown() should be ok, and arguably is correct, e.g. I don't > see how leaving #MC reporting enabled across kexec can work. > > ledtrig_cpu_syscore_shutdown() is also likely ok and arguably correct. I like your observation here that we probably have other misses like MCE which should be shut down too - that's a hint that adding syscore_shutdown() to kexec is the way to go. > > The interrupt controllers though? x86 disables IRQs at the very beginning of > machine_kexec(), so it's likely fine. But every other architecture? No clue. > E.g. PPC's default_machine_kexec() sends IPIs to shutdown other CPUs, though I > have no idea if that can run afoul of any of the paths below. > > arch/powerpc/platforms/cell/spu_base.c .shutdown = spu_shutdown, > arch/x86/kernel/cpu/mce/core.c .shutdown = mce_syscore_shutdown, > arch/x86/kernel/i8259.c .shutdown = i8259A_shutdown, > drivers/irqchip/irq-i8259.c .shutdown = i8259A_shutdown, > drivers/irqchip/irq-sun6i-r.c .shutdown = sun6i_r_intc_shutdown, > drivers/leds/trigger/ledtrig-cpu.c .shutdown = ledtrig_cpu_syscore_shutdown, > drivers/power/reset/sc27xx-poweroff.c .shutdown = sc27xx_poweroff_shutdown, > kernel/irq/generic-chip.c .shutdown = irq_gc_shutdown, > virt/kvm/kvm_main.c .shutdown = kvm_shutdown, > > The whole thing is a bit of a mess. E.g. x86 treats machine_shutdown() from > kexec pretty much the same as shutdown for reboot, but other architectures have > what appear to be unique paths for handling kexec. > > FWIW, if we want to go with option (b), syscore_shutdown() hooks could use > kexec_in_progress to differentiate between "regular" shutdown/reboot and kexec. Yeah, perhaps that's the best: add syscore_shutdown to kexec and get the callers to handle both cases if necessary. We could get maintainers for all of these drivers to sign off on the change and say whether they need to differentiate between kexec and reboot. Eric, what are your thoughts on this approach? I can try to whip up a patch for this and add the maintainers for all of the drivers. JG _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2023-12-13 6:48 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20230512233127.804012-1-seanjc@google.com>
[not found] ` <20230512233127.804012-2-seanjc@google.com>
2023-12-09 7:26 ` [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown Gowans, James
2023-12-10 4:53 ` Eric W. Biederman
2023-12-11 7:54 ` Gowans, James
2023-12-11 10:27 ` Gowans, James
2023-12-11 23:50 ` Eric W. Biederman
2023-12-12 8:50 ` Gowans, James
2023-12-12 13:38 ` Paolo Bonzini
2023-12-13 6:47 ` Gowans, James
2023-12-11 17:34 ` Sean Christopherson
2023-12-11 17:51 ` Sean Christopherson
2023-12-11 18:47 ` Gowans, James
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox