Re: [PATCH tty v1 00/74] serial: wrappers for uart port lock

2023-09-15 Thread Maciej W. Rozycki
On Fri, 15 Sep 2023, Thomas Gleixner wrote:

> >> Patches 2-74 switch all uart port locking call sites to use the new
> >> wrappers. These patches were automatically generated using coccinelle.
> >
> >  Hmm, no need to do this for drivers/tty/serial/zs.c?
> 
> zs.c does not use port lock at all. It has like a couple of other
> drivers a local homebrewn spinlock.

 Ah, right, that's because there are registers shared between two ports 
within one SCC, so the spinlock has to be shared as well.

 This also indicates that dz.c is wrong and shouldn't be using a per-port 
spinlock as the DZ has a shared register set between all the four ports 
(it's a serial line multiplexer rather than a set discrete ports; up to 8 
lines are architecturally supported, though we don't have support in Linux 
for systems having those), e.g. there's only one 16-bit receiver buffer 
register for all the four ports, supplying the 8-bit character received 
along with the receive status and the number of the line this data arrived 
on, and similarly there's only one transmit data register, which supplies 
data to the next enabled line whose transmit buffer needs servicing, and 
the chip routes the data itself.  Therefore the driver ought to use a 
shared spinlock too.

 I guess it wasn't noticed so far because DZ devices aren't that common 
(and my usual test machine is currently broken too, pending an SRAM chip 
replacement, hopefully in the next few weeks) and then hardly ever more 
than one serial line has been used at a time with these devices.  It looks 
like the first issue for me to look into once the machine has been fixed.

 Maybe dz.c shouldn't be touched by this series then?  (Though obviously 
both drivers will have to be eventually adapted for the ultimate console 
rework.)

 Thanks for your input, as it turns out it has had an unexpected outcome.

  Maciej


[PATCH 26/26] KVM: Hide KVM internal data structures and values from kernel at-large

2023-09-15 Thread Sean Christopherson
Wrap all KVM internal APIs, data structures, values, etc. in public
headers with "#ifdef __KVM__" to effectively hide KVM's internal details
from other subsystems and the kernel at-large.  Hiding KVM details for
all architectures will, in the very distant future, allow loading a new
(or old) KVM module without needing to rebuild and reboot the entire
kernel, or to even allow loading and running multiple versions of KVM
simultaneously on a single host.

To allow different instances of KVM modules to freely modify KVM data
structures, enums, #defines, etc., e.g. the struct kvm_vcpu layout, there
must be exactly zero dereferences of KVM-defined structures/values in
non-KVM code (excepting code for architectures that don't support such
shenanigans).  Any such references could lead to latent bugs, e.g. as the
kernel would think a KVM structure has layout X, but in reality the
current incarnation of KVM uses layout Y.

In KVM x86, all remaining non-KVM references to KVM details have been now
eliminated.  To harden KVM against new references being introduced, hide
KVM's details to ensure that KVM doesn’t create a de facto ABI with the
rest of the kernel.

Use #ifdeffery to hide KVM details as doing so requires, by far, the least
amount of churn and impact on architectures that freely share select KVM
details with the rest of the kernel, e.g. s390, ARM64 and PPC, have
significant usage of KVM-defined APIs, structures, values, etc.  E.g.
attempting to extract the "private" chunks into dedicated KVM-internal
headers would require massive churn, even on x86, and it's not obvious the
end result would be a net positive (all attempts at moving code around
failed long before getting anywhere near compiling cleanly).

Another (bad) alternative that would be relative churn-free would be to
move the KVM headers to a dedicated KVM-specific path while maintaining
the generic layout, e.g. something like virt/kvm/include/linux.  That
would allow x86 to simply omit the KVM-specific include path.  But that
would require modifying the global include path, i.e. would make KVM a
really special snowflake and set the awful precedent that it's "ok" to
add subsystem specific directories to the global include path.

Grant exceptions to asm-offsets.c as needed, and to s390's VFIO AP driver.
Creating a KVM-specific asm-offsets, as was done for x86, adds no value
(x86 did so to avoid exposing vendor specific headers) and doesn't create
much of a "slippery slope" risk.  s390's VFIO AP driver on the other hand
is simply too intertwined with KVM to realistically treat it as anything
other than KVM code, despite it living in crypto drivers, e.g. the driver
has some rather crazy lock ordering rules between the device and KVM.

Add a static assert that __KVM__ is defined in trace/events/kvm.h, as that
is effectively a private KVM header that just happens to live in a public
path.

Shuffle a few PPC includes and an s390 declaration as needed, and
opportunistically include kvm_host.h in trace/events.kvm.h instead of
relying on the parent to provide the right includes.

Cc: Anish Ghulati 
Cc: Venkatesh Srinivas 
Cc: Andrew Thornton 
Signed-off-by: Sean Christopherson 
---
 arch/arm64/include/asm/kvm_emulate.h | 3 +++
 arch/arm64/include/asm/kvm_host.h| 2 ++
 arch/arm64/include/asm/kvm_mmu.h | 4 
 arch/arm64/kernel/asm-offsets.c  | 2 ++
 arch/arm64/kvm/hyp/Makefile  | 2 +-
 arch/arm64/kvm/hyp/nvhe/Makefile | 3 ++-
 arch/arm64/kvm/hyp/vhe/Makefile  | 2 +-
 arch/mips/include/asm/kvm_host.h | 2 ++
 arch/mips/kernel/asm-offsets.c   | 2 ++
 arch/powerpc/include/asm/kvm_book3s.h| 3 +++
 arch/powerpc/include/asm/kvm_book3s_64.h | 2 ++
 arch/powerpc/include/asm/kvm_booke.h | 4 
 arch/powerpc/include/asm/kvm_host.h  | 7 ---
 arch/powerpc/include/asm/kvm_ppc.h   | 2 ++
 arch/powerpc/kernel/asm-offsets.c| 1 +
 arch/riscv/include/asm/kvm_host.h| 3 +++
 arch/riscv/kernel/asm-offsets.c  | 1 +
 arch/s390/include/asm/kvm_host.h | 7 +--
 arch/s390/kernel/asm-offsets.c   | 1 +
 arch/x86/include/asm/kvm_host.h  | 3 +++
 drivers/s390/crypto/vfio_ap_drv.c| 1 +
 drivers/s390/crypto/vfio_ap_ops.c| 2 ++
 include/linux/kvm_host.h | 4 
 include/linux/kvm_types.h| 3 +++
 include/trace/events/kvm.h   | 5 +
 virt/kvm/Makefile.kvm| 2 +-
 26 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_emulate.h 
b/arch/arm64/include/asm/kvm_emulate.h
index 3d6725ff0bf6..5861fa00763b 100644
--- a/arch/arm64/include/asm/kvm_emulate.h
+++ b/arch/arm64/include/asm/kvm_emulate.h
@@ -21,6 +21,8 @@
 #include 
 #include 
 
+#ifdef __KVM__
+
 #define CURRENT_EL_SP_EL0_VECTOR   0x0
 #define CURRENT_EL_SP_ELx_VECTOR   0x200
 #define LOWER_EL_AArch64_VECTOR0x400
@@ -615,4 +617,5 @@ static __always_inline 

[PATCH 25/26] KVM: PPC: Rearrange code in kvm_ppc.h to isolate "public" information

2023-09-15 Thread Sean Christopherson
Move all declarations and definitions in kvm_ppc.h that are consumed by
things other than KVM to the top of the file.  This will allow wrapping
the parts of kvm_ppc.h that are intended only for KVM, i.e. are intended
to be "private" to KVM, with an #ifdef to hide KVM's internal details from
the kernel at-large.

Signed-off-by: Sean Christopherson 
---
 arch/powerpc/include/asm/kvm_ppc.h | 302 +++--
 1 file changed, 153 insertions(+), 149 deletions(-)

diff --git a/arch/powerpc/include/asm/kvm_ppc.h 
b/arch/powerpc/include/asm/kvm_ppc.h
index 72fee202d3ec..ead2ad892ebc 100644
--- a/arch/powerpc/include/asm/kvm_ppc.h
+++ b/arch/powerpc/include/asm/kvm_ppc.h
@@ -30,6 +30,159 @@
 #endif
 #include 
 
+struct openpic;
+
+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
+extern void kvm_cma_reserve(void) __init;
+static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
+{
+   paca_ptrs[cpu]->kvm_hstate.xics_phys = (void __iomem *)addr;
+}
+
+static inline void kvmppc_set_xive_tima(int cpu,
+   unsigned long phys_addr,
+   void __iomem *virt_addr)
+{
+   paca_ptrs[cpu]->kvm_hstate.xive_tima_phys = (void __iomem *)phys_addr;
+   paca_ptrs[cpu]->kvm_hstate.xive_tima_virt = virt_addr;
+}
+
+static inline u32 kvmppc_get_xics_latch(void)
+{
+   u32 xirr;
+
+   xirr = get_paca()->kvm_hstate.saved_xirr;
+   get_paca()->kvm_hstate.saved_xirr = 0;
+   return xirr;
+}
+
+/*
+ * To avoid the need to unnecessarily exit fully to the host kernel, an IPI to
+ * a CPU thread that's running/napping inside of a guest is by default regarded
+ * as a request to wake the CPU (if needed) and continue execution within the
+ * guest, potentially to process new state like externally-generated
+ * interrupts or IPIs sent from within the guest itself (e.g. H_PROD/H_IPI).
+ *
+ * To force an exit to the host kernel, kvmppc_set_host_ipi() must be called
+ * prior to issuing the IPI to set the corresponding 'host_ipi' flag in the
+ * target CPU's PACA. To avoid unnecessary exits to the host, this flag should
+ * be immediately cleared via kvmppc_clear_host_ipi() by the IPI handler on
+ * the receiving side prior to processing the IPI work.
+ *
+ * NOTE:
+ *
+ * We currently issue an smp_mb() at the beginning of kvmppc_set_host_ipi().
+ * This is to guard against sequences such as the following:
+ *
+ *  CPU
+ *X: smp_muxed_ipi_set_message():
+ *X:   smp_mb()
+ *X:   message[RESCHEDULE] = 1
+ *X: doorbell_global_ipi(42):
+ *X:   kvmppc_set_host_ipi(42)
+ *X:   ppc_msgsnd_sync()/smp_mb()
+ *X:   ppc_msgsnd() -> 42
+ *   42: doorbell_exception(): // from CPU X
+ *   42:   ppc_msgsync()
+ *  105: smp_muxed_ipi_set_message():
+ *  105:   smb_mb()
+ *   // STORE DEFERRED DUE TO RE-ORDERING
+ *--105:   message[CALL_FUNCTION] = 1
+ *| 105: doorbell_global_ipi(42):
+ *| 105:   kvmppc_set_host_ipi(42)
+ *|  42:   kvmppc_clear_host_ipi(42)
+ *|  42: smp_ipi_demux_relaxed()
+ *|  42: // returns to executing guest
+ *|  // RE-ORDERED STORE COMPLETES
+ *->105:   message[CALL_FUNCTION] = 1
+ *  105:   ppc_msgsnd_sync()/smp_mb()
+ *  105:   ppc_msgsnd() -> 42
+ *   42: local_paca->kvm_hstate.host_ipi == 0 // IPI ignored
+ *  105: // hangs waiting on 42 to process messages/call_single_queue
+ *
+ * We also issue an smp_mb() at the end of kvmppc_clear_host_ipi(). This is
+ * to guard against sequences such as the following (as well as to create
+ * a read-side pairing with the barrier in kvmppc_set_host_ipi()):
+ *
+ *  CPU
+ *X: smp_muxed_ipi_set_message():
+ *X:   smp_mb()
+ *X:   message[RESCHEDULE] = 1
+ *X: doorbell_global_ipi(42):
+ *X:   kvmppc_set_host_ipi(42)
+ *X:   ppc_msgsnd_sync()/smp_mb()
+ *X:   ppc_msgsnd() -> 42
+ *   42: doorbell_exception(): // from CPU X
+ *   42:   ppc_msgsync()
+ *   // STORE DEFERRED DUE TO RE-ORDERING
+ *-- 42:   kvmppc_clear_host_ipi(42)
+ *|  42: smp_ipi_demux_relaxed()
+ *| 105: smp_muxed_ipi_set_message():
+ *| 105:   smb_mb()
+ *| 105:   message[CALL_FUNCTION] = 1
+ *| 105: doorbell_global_ipi(42):
+ *| 105:   kvmppc_set_host_ipi(42)
+ *|  // RE-ORDERED STORE COMPLETES
+ *-> 42:   kvmppc_clear_host_ipi(42)
+ *   42: // returns to executing guest
+ *  105:   ppc_msgsnd_sync()/smp_mb()
+ *  105:   ppc_msgsnd() -> 42
+ *   42: local_paca->kvm_hstate.host_ipi == 0 // IPI ignored
+ *  105: // hangs waiting on 42 to process messages/call_single_queue
+ */
+static inline void kvmppc_set_host_ipi(int cpu)
+{
+   /*
+* order stores of IPI messages vs. setting of host_ipi flag
+*
+* pairs with the barrier in kvmppc_clear_host_ipi()
+*/
+   smp_mb();
+   

[PATCH 24/26] powerpc/xics: Move declaration of xics_wake_cpu() out of kvm_ppc.h

2023-09-15 Thread Sean Christopherson
xics_wake_cpu() is provided by core PPC code, not by KVM.  Move its
declaration out of kvm_ppc.h and into xics.h.

Signed-off-by: Sean Christopherson 
---
 arch/powerpc/include/asm/kvm_ppc.h | 2 --
 arch/powerpc/include/asm/xics.h| 3 +++
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/include/asm/kvm_ppc.h 
b/arch/powerpc/include/asm/kvm_ppc.h
index b4da8514af43..72fee202d3ec 100644
--- a/arch/powerpc/include/asm/kvm_ppc.h
+++ b/arch/powerpc/include/asm/kvm_ppc.h
@@ -1072,6 +1072,4 @@ static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu 
*vcpu, int ra, int rb)
return ea;
 }
 
-extern void xics_wake_cpu(int cpu);
-
 #endif /* __POWERPC_KVM_PPC_H__ */
diff --git a/arch/powerpc/include/asm/xics.h b/arch/powerpc/include/asm/xics.h
index 89090485bec1..17a45c022bd9 100644
--- a/arch/powerpc/include/asm/xics.h
+++ b/arch/powerpc/include/asm/xics.h
@@ -32,6 +32,9 @@
 extern int icp_native_init(void);
 extern void icp_native_flush_interrupt(void);
 extern void icp_native_cause_ipi_rm(int cpu);
+#ifdef CONFIG_SMP
+extern void xics_wake_cpu(int cpu);
+#endif
 #else
 static inline int icp_native_init(void) { return -ENODEV; }
 #endif
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 23/26] KVM: arm64: Move and consolidate "public" functions in asm/kvm_host.h

2023-09-15 Thread Sean Christopherson
Move a variety of "public" symbols in arm64's kvm_host.h, i.e. symbols and
functions that are used by non-KVM code, towards the top of the header.
This will allow hiding the "private" code in kvm_host.h, i.e. the stuff
that is intended to be KVM-internal, with a minimal amount of #ifdeffery.

Signed-off-by: Sean Christopherson 
---
 arch/arm64/include/asm/kvm_host.h | 48 ++-
 1 file changed, 22 insertions(+), 26 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_host.h 
b/arch/arm64/include/asm/kvm_host.h
index 90cfbf420545..89b40c34f0af 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -67,10 +67,32 @@ enum kvm_mode {
 };
 #ifdef CONFIG_KVM
 enum kvm_mode kvm_get_mode(void);
+
+extern phys_addr_t hyp_mem_base;
+extern phys_addr_t hyp_mem_size;
+void __init kvm_hyp_reserve(void);
+
+void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr);
+void kvm_clr_pmu_events(u32 clr);
+bool kvm_set_pmuserenr(u64 val);
 #else
 static inline enum kvm_mode kvm_get_mode(void) { return KVM_MODE_NONE; };
+
+static inline void kvm_hyp_reserve(void) { }
+
+static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
+static inline void kvm_clr_pmu_events(u32 clr) {}
+static inline bool kvm_set_pmuserenr(u64 val)
+{
+   return false;
+}
 #endif
 
+static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
+{
+   return (!has_vhe() && attr->exclude_host);
+}
+
 DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
 
 extern unsigned int __ro_after_init kvm_sve_max_vl;
@@ -1086,28 +1108,10 @@ void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu);
 void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu);
 
-static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
-{
-   return (!has_vhe() && attr->exclude_host);
-}
-
 /* Flags for host debug state */
 void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
 
-#ifdef CONFIG_KVM
-void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr);
-void kvm_clr_pmu_events(u32 clr);
-bool kvm_set_pmuserenr(u64 val);
-#else
-static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
-static inline void kvm_clr_pmu_events(u32 clr) {}
-static inline bool kvm_set_pmuserenr(u64 val)
-{
-   return false;
-}
-#endif
-
 void kvm_vcpu_load_sysregs_vhe(struct kvm_vcpu *vcpu);
 void kvm_vcpu_put_sysregs_vhe(struct kvm_vcpu *vcpu);
 
@@ -1142,14 +1146,6 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
(test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &(kvm)->arch.flags))
 
 int kvm_trng_call(struct kvm_vcpu *vcpu);
-#ifdef CONFIG_KVM
-extern phys_addr_t hyp_mem_base;
-extern phys_addr_t hyp_mem_size;
-void __init kvm_hyp_reserve(void);
-#else
-static inline void kvm_hyp_reserve(void) { }
-#endif
-
 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 22/26] entry/kvm: KVM: Move KVM details related to signal/-EINTR into KVM proper

2023-09-15 Thread Sean Christopherson
Move KVM's morphing of pending signals into exits to userspace into KVM
proper, and drop the @vcpu param from xfer_to_guest_mode_handle_work().
How KVM responds to -EINTR is a detail that really belongs in KVM itself,
and removing the non-KVM call to kvm_handle_signal_exit() will allow
hiding said API and the definition of "struct kvm_vcpu" from the kernel.

Alternatively, entry/kvm.c could be treated as part of KVM, i.e. be given
access to KVM internals, but that's not obviously better than having KVM
react to -EINTR (though it's not obviously worse either).

Signed-off-by: Sean Christopherson 
---
 arch/arm64/kvm/arm.c  |  3 +--
 arch/riscv/kvm/vcpu.c |  2 +-
 arch/x86/kvm/vmx/vmx.c|  1 -
 arch/x86/kvm/x86.c|  3 +--
 include/linux/entry-kvm.h |  3 +--
 include/linux/kvm_host.h  | 13 -
 kernel/entry/kvm.c| 11 ---
 7 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 6480628197b4..641df091e46b 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -6,7 +6,6 @@
 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -929,7 +928,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
/*
 * Check conditions before entering the guest
 */
-   ret = xfer_to_guest_mode_handle_work(vcpu);
+   ret = kvm_xfer_to_guest_mode_handle_work(vcpu);
if (!ret)
ret = 1;
 
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index 82229db1ce73..c313f4e90e70 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -667,7 +667,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
run->exit_reason = KVM_EXIT_UNKNOWN;
while (ret > 0) {
/* Check conditions before entering the guest */
-   ret = xfer_to_guest_mode_handle_work(vcpu);
+   ret = kvm_xfer_to_guest_mode_handle_work(vcpu);
if (ret)
continue;
ret = 1;
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index faf0071566ef..43b87ad5fde8 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -28,7 +28,6 @@
 #include 
 #include 
 #include 
-#include 
 
 #include 
 #include 
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 6c9c81e82e65..aab095f89d9e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -59,7 +59,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 
@@ -10987,7 +10986,7 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
 
if (__xfer_to_guest_mode_work_pending()) {
kvm_vcpu_srcu_read_unlock(vcpu);
-   r = xfer_to_guest_mode_handle_work(vcpu);
+   r = kvm_xfer_to_guest_mode_handle_work(vcpu);
kvm_vcpu_srcu_read_lock(vcpu);
if (r)
return r;
diff --git a/include/linux/entry-kvm.h b/include/linux/entry-kvm.h
index e7d90d06e566..e235a91d28fc 100644
--- a/include/linux/entry-kvm.h
+++ b/include/linux/entry-kvm.h
@@ -42,11 +42,10 @@ static inline int 
arch_xfer_to_guest_mode_handle_work(unsigned long ti_work)
 /**
  * xfer_to_guest_mode_handle_work - Check and handle pending work which needs
  * to be handled before going to guest mode
- * @vcpu:  Pointer to current's VCPU data
  *
  * Returns: 0 or an error code
  */
-int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu);
+int xfer_to_guest_mode_handle_work(void);
 
 /**
  * xfer_to_guest_mode_prepare - Perform last minute preparation work that
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index fb6c6109fdca..d520d6801070 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -2,7 +2,7 @@
 #ifndef __KVM_HOST_H
 #define __KVM_HOST_H
 
-
+#include 
 #include 
 #include 
 #include 
@@ -2293,6 +2293,17 @@ static inline void kvm_handle_signal_exit(struct 
kvm_vcpu *vcpu)
vcpu->run->exit_reason = KVM_EXIT_INTR;
vcpu->stat.signal_exits++;
 }
+
+static inline int kvm_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu)
+{
+   int r = xfer_to_guest_mode_handle_work();
+
+   if (r) {
+   WARN_ON_ONCE(r != -EINTR);
+   kvm_handle_signal_exit(vcpu);
+   }
+   return r;
+}
 #endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */
 
 /*
diff --git a/kernel/entry/kvm.c b/kernel/entry/kvm.c
index c2fc39824157..872617468b4a 100644
--- a/kernel/entry/kvm.c
+++ b/kernel/entry/kvm.c
@@ -1,17 +1,14 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include 
-#include 
 
-static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long 
ti_work)
+static int xfer_to_guest_mode_work(unsigned long ti_work)
 {
do {
int ret;
 
-   if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {
-   

[PATCH 21/26] entry/kvm: Drop @vcpu param from arch_xfer_to_guest_mode_handle_work()

2023-09-15 Thread Sean Christopherson
Drop the "struct kvm_vcpu" parameter from
arch_xfer_to_guest_mode_handle_work() as a preparatory step towards
removing the vCPU parameter from xfer_to_guest_mode_work() as well.

Signed-off-by: Sean Christopherson 
---
 include/linux/entry-kvm.h | 7 ++-
 kernel/entry/kvm.c| 2 +-
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/include/linux/entry-kvm.h b/include/linux/entry-kvm.h
index 6813171afccb..e7d90d06e566 100644
--- a/include/linux/entry-kvm.h
+++ b/include/linux/entry-kvm.h
@@ -25,18 +25,15 @@ struct kvm_vcpu;
 /**
  * arch_xfer_to_guest_mode_handle_work - Architecture specific xfer to guest
  *  mode work handling function.
- * @vcpu:  Pointer to current's VCPU data
  * @ti_work:   Cached TIF flags gathered in xfer_to_guest_mode_handle_work()
  *
  * Invoked from xfer_to_guest_mode_handle_work(). Defaults to NOOP. Can be
  * replaced by architecture specific code.
  */
-static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu,
- unsigned long ti_work);
+static inline int arch_xfer_to_guest_mode_handle_work(unsigned long ti_work);
 
 #ifndef arch_xfer_to_guest_mode_work
-static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu,
- unsigned long ti_work)
+static inline int arch_xfer_to_guest_mode_handle_work(unsigned long ti_work)
 {
return 0;
 }
diff --git a/kernel/entry/kvm.c b/kernel/entry/kvm.c
index 2e0f75bcb7fd..c2fc39824157 100644
--- a/kernel/entry/kvm.c
+++ b/kernel/entry/kvm.c
@@ -19,7 +19,7 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, 
unsigned long ti_work)
if (ti_work & _TIF_NOTIFY_RESUME)
resume_user_mode_work(NULL);
 
-   ret = arch_xfer_to_guest_mode_handle_work(vcpu, ti_work);
+   ret = arch_xfer_to_guest_mode_handle_work(ti_work);
if (ret)
return ret;
 
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 20/26] perf/x86: KVM: Have perf define a dedicated struct for getting guest PEBS data

2023-09-15 Thread Sean Christopherson
Have perf define a struct for getting guest PEBS data from KVM instead of
poking into the kvm_pmu structure.  Passing in an entire "struct kvm_pmu"
_as an opaque pointer_ to get at three fields is silly, especially since
one of the fields exists purely to convey information to perf, i.e. isn't
used by KVM.

Perf should also own its APIs, i.e. define what fields/data it needs, not
rely on KVM to throw fields into data structures that effectively hold
KVM-internal state.

Signed-off-by: Sean Christopherson 
---
 arch/x86/events/core.c|  5 +++--
 arch/x86/events/intel/core.c  | 18 +-
 arch/x86/events/perf_event.h  |  3 ++-
 arch/x86/include/asm/kvm_host.h   |  9 -
 arch/x86/include/asm/perf_event.h | 12 ++--
 arch/x86/kvm/vmx/pmu_intel.c  | 16 +---
 arch/x86/kvm/vmx/vmx.c| 10 ++
 arch/x86/kvm/vmx/vmx.h|  2 +-
 8 files changed, 44 insertions(+), 31 deletions(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 185f902e5f28..3b015cfcf41d 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -695,9 +695,10 @@ void x86_pmu_disable_all(void)
}
 }
 
-struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, void *data)
+struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr,
+ struct x86_guest_pebs 
*guest_pebs)
 {
-   return static_call(x86_pmu_guest_get_msrs)(nr, data);
+   return static_call(x86_pmu_guest_get_msrs)(nr, guest_pebs);
 }
 EXPORT_SYMBOL_GPL(perf_guest_get_msrs);
 
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index fa355d3658a6..9b848d7ebaaf 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -14,7 +14,6 @@
 #include 
 #include 
 #include 
-#include 
 
 #include 
 #include 
@@ -4053,11 +4052,11 @@ static int intel_pmu_hw_config(struct perf_event *event)
  * when it uses {RD,WR}MSR, which should be handled by the KVM context,
  * specifically in the intel_pmu_{get,set}_msr().
  */
-static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)
+static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr,
+ struct x86_guest_pebs 
*guest_pebs)
 {
struct cpu_hw_events *cpuc = this_cpu_ptr(_hw_events);
struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
-   struct kvm_pmu *kvm_pmu = (struct kvm_pmu *)data;
u64 intel_ctrl = hybrid(cpuc->pmu, intel_ctrl);
u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable;
int global_ctrl, pebs_enable;
@@ -4090,20 +4089,20 @@ static struct perf_guest_switch_msr 
*intel_guest_get_msrs(int *nr, void *data)
return arr;
}
 
-   if (!kvm_pmu || !x86_pmu.pebs_ept)
+   if (!guest_pebs || !x86_pmu.pebs_ept)
return arr;
 
arr[(*nr)++] = (struct perf_guest_switch_msr){
.msr = MSR_IA32_DS_AREA,
.host = (unsigned long)cpuc->ds,
-   .guest = kvm_pmu->ds_area,
+   .guest = guest_pebs->ds_area,
};
 
if (x86_pmu.intel_cap.pebs_baseline) {
arr[(*nr)++] = (struct perf_guest_switch_msr){
.msr = MSR_PEBS_DATA_CFG,
.host = cpuc->active_pebs_data_cfg,
-   .guest = kvm_pmu->pebs_data_cfg,
+   .guest = guest_pebs->data_cfg,
};
}
 
@@ -4119,8 +4118,8 @@ static struct perf_guest_switch_msr 
*intel_guest_get_msrs(int *nr, void *data)
arr[pebs_enable].guest = 0;
} else {
/* Disable guest PEBS thoroughly for cross-mapped PEBS 
counters. */
-   arr[pebs_enable].guest &= ~kvm_pmu->host_cross_mapped_mask;
-   arr[global_ctrl].guest &= ~kvm_pmu->host_cross_mapped_mask;
+   arr[pebs_enable].guest &= ~guest_pebs->cross_mapped_mask;
+   arr[global_ctrl].guest &= ~guest_pebs->cross_mapped_mask;
/* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for 
guest */
arr[global_ctrl].guest |= arr[pebs_enable].guest;
}
@@ -4128,7 +4127,8 @@ static struct perf_guest_switch_msr 
*intel_guest_get_msrs(int *nr, void *data)
return arr;
 }
 
-static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr, void *data)
+static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr,
+struct x86_guest_pebs 
*guest_pebs)
 {
struct cpu_hw_events *cpuc = this_cpu_ptr(_hw_events);
struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h
index c8ba2be7585d..d805c9007e35 100644
--- a/arch/x86/events/perf_event.h
+++ b/arch/x86/events/perf_event.h
@@ -920,7 +920,8 @@ struct x86_pmu {
/*
 * 

[PATCH 19/26] KVM: Standardize include paths across all architectures

2023-09-15 Thread Sean Christopherson
Standardize KVM's include paths across all architectures by declaring
the KVM-specific includes in the common Makefile.kvm.  Having common KVM
"own" the included paths reduces the temptation to unnecessarily add
virt/kvm to arch include paths, and conversely if allowing arch code to
grab headers from virt/kvm becomes desirable, virt/kvm can be added to
all architecture's include path with a single line update.

Having the common KVM makefile append to ccflags also provides a
convenient location to append other things, e.g. KVM-specific #defines.

Note, this changes the behavior of s390 and PPC, as s390 and PPC
previously overwrote ccflags-y instead of adding on.  There is no evidence
that overwriting ccflags-y was necessary or even deliberate, as both s390
and PPC switched to the overwrite behavior without so much as a passing
mention when EXTRA_CFLAGS was replaced with ccflags-y (commit c73028a02887
("s390: change to new flag variable") and commit 4108d9ba9091
("powerpc/Makefiles: Change to new flag variables")).

Signed-off-by: Sean Christopherson 
---
 arch/arm64/kvm/Makefile   | 2 --
 arch/mips/kvm/Makefile| 2 --
 arch/powerpc/kvm/Makefile | 2 --
 arch/riscv/kvm/Makefile   | 2 --
 arch/s390/kvm/Makefile| 2 --
 arch/x86/kvm/Makefile | 1 -
 virt/kvm/Makefile.kvm | 2 ++
 7 files changed, 2 insertions(+), 11 deletions(-)

diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile
index c0c050e53157..3996489baeef 100644
--- a/arch/arm64/kvm/Makefile
+++ b/arch/arm64/kvm/Makefile
@@ -3,8 +3,6 @@
 # Makefile for Kernel-based Virtual Machine module
 #
 
-ccflags-y += -I $(srctree)/$(src)
-
 include $(srctree)/virt/kvm/Makefile.kvm
 
 obj-$(CONFIG_KVM) += kvm.o
diff --git a/arch/mips/kvm/Makefile b/arch/mips/kvm/Makefile
index 96a7cd21b140..d198e1addea7 100644
--- a/arch/mips/kvm/Makefile
+++ b/arch/mips/kvm/Makefile
@@ -4,8 +4,6 @@
 
 include $(srctree)/virt/kvm/Makefile.kvm
 
-ccflags-y += -Iarch/mips/kvm
-
 kvm-$(CONFIG_CPU_HAS_MSA) += msa.o
 
 kvm-y +=mips.o emulate.o entry.o \
diff --git a/arch/powerpc/kvm/Makefile b/arch/powerpc/kvm/Makefile
index 08a0e53d58c7..d6c6678ddf65 100644
--- a/arch/powerpc/kvm/Makefile
+++ b/arch/powerpc/kvm/Makefile
@@ -3,8 +3,6 @@
 # Makefile for Kernel-based Virtual Machine module
 #
 
-ccflags-y := -Iarch/powerpc/kvm
-
 include $(srctree)/virt/kvm/Makefile.kvm
 
 common-objs-y += powerpc.o emulate_loadstore.o
diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile
index 4c2067fc59fc..ff7d5f67e229 100644
--- a/arch/riscv/kvm/Makefile
+++ b/arch/riscv/kvm/Makefile
@@ -3,8 +3,6 @@
 # Makefile for RISC-V KVM support
 #
 
-ccflags-y += -I $(srctree)/$(src)
-
 include $(srctree)/virt/kvm/Makefile.kvm
 
 obj-$(CONFIG_KVM) += kvm.o
diff --git a/arch/s390/kvm/Makefile b/arch/s390/kvm/Makefile
index f17249ab2a72..f8153189e003 100644
--- a/arch/s390/kvm/Makefile
+++ b/arch/s390/kvm/Makefile
@@ -5,8 +5,6 @@
 
 include $(srctree)/virt/kvm/Makefile.kvm
 
-ccflags-y := -Iarch/s390/kvm
-
 kvm-y += kvm-s390.o intercept.o interrupt.o priv.o sigp.o
 kvm-y += diag.o gaccess.o guestdbg.o vsie.o pv.o
 
diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
index 80e3fe184d17..d13f1a7b7b3d 100644
--- a/arch/x86/kvm/Makefile
+++ b/arch/x86/kvm/Makefile
@@ -1,6 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 
-ccflags-y += -I $(srctree)/arch/x86/kvm
 ccflags-$(CONFIG_KVM_WERROR) += -Werror
 
 ifeq ($(CONFIG_FRAME_POINTER),y)
diff --git a/virt/kvm/Makefile.kvm b/virt/kvm/Makefile.kvm
index 29373b59d89a..e85079ad245d 100644
--- a/virt/kvm/Makefile.kvm
+++ b/virt/kvm/Makefile.kvm
@@ -3,6 +3,8 @@
 # Makefile for Kernel-based Virtual Machine module
 #
 
+ccflags-y += -I$(srctree)/$(src)
+
 KVM ?= ../../../virt/kvm
 
 kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 18/26] KVM: s390: Stop adding virt/kvm to the arch include path

2023-09-15 Thread Sean Christopherson
Don't add virt/kvm to KVM s390's include path, the headers in virt/kvm are
intended to be used only by other code in virt/kvm, i.e. are "private" to
the core KVM code.  It's not clear that s390 *ever* included a header from
virt/kvm, i.e. odds are good the "-Ivirt/kvm" was copied from a x86's
Makefile when s390 support was first added.

The only headers in virt/kvm at the time were the x86 specific ioapic.h,
and iodev.h, neither of which shows up as an #include in the diff for the
commit range 37817f2982d0f..e976a2b997fc4.

Signed-off-by: Sean Christopherson 
---
 arch/s390/kvm/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/s390/kvm/Makefile b/arch/s390/kvm/Makefile
index 02217fb4ae10..f17249ab2a72 100644
--- a/arch/s390/kvm/Makefile
+++ b/arch/s390/kvm/Makefile
@@ -5,7 +5,7 @@
 
 include $(srctree)/virt/kvm/Makefile.kvm
 
-ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
+ccflags-y := -Iarch/s390/kvm
 
 kvm-y += kvm-s390.o intercept.o interrupt.o priv.o sigp.o
 kvm-y += diag.o gaccess.o guestdbg.o vsie.o pv.o
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 17/26] KVM: PPC: Stop adding virt/kvm to the arch include path

2023-09-15 Thread Sean Christopherson
Don't add virt/kvm to KVM PPC's include path, the headers in virt/kvm are
intended to be used only by other code in virt/kvm, i.e. are "private" to
the core KVM code.  It's not clear that PPC *ever* included a header from
virt/kvm, i.e. odds are good the "-Ivirt/kvm" was copied from a different
architecture's Makefile when PPC support was first added.

Signed-off-by: Sean Christopherson 
---
 arch/powerpc/kvm/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kvm/Makefile b/arch/powerpc/kvm/Makefile
index 5319d889b184..08a0e53d58c7 100644
--- a/arch/powerpc/kvm/Makefile
+++ b/arch/powerpc/kvm/Makefile
@@ -3,7 +3,7 @@
 # Makefile for Kernel-based Virtual Machine module
 #
 
-ccflags-y := -Ivirt/kvm -Iarch/powerpc/kvm
+ccflags-y := -Iarch/powerpc/kvm
 
 include $(srctree)/virt/kvm/Makefile.kvm
 
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 16/26] KVM: MIPS: Stop adding virt/kvm to the arch include path

2023-09-15 Thread Sean Christopherson
Don't add virt/kvm to KVM MIPS' include path, the headers in virt/kvm are
intended to be used only by other code in virt/kvm, i.e. are "private" to
the core KVM code.  It's not clear that MIPS *ever* included a header from
virt/kvm, i.e. odds are good the "-Ivirt/kvm" was copied from a different
architecture's Makefile when MIPS support was first added.

Signed-off-by: Sean Christopherson 
---
 arch/mips/kvm/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/mips/kvm/Makefile b/arch/mips/kvm/Makefile
index 805aeea2166e..96a7cd21b140 100644
--- a/arch/mips/kvm/Makefile
+++ b/arch/mips/kvm/Makefile
@@ -4,7 +4,7 @@
 
 include $(srctree)/virt/kvm/Makefile.kvm
 
-ccflags-y += -Ivirt/kvm -Iarch/mips/kvm
+ccflags-y += -Iarch/mips/kvm
 
 kvm-$(CONFIG_CPU_HAS_MSA) += msa.o
 
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 15/26] KVM: Move include/kvm/iodev.h to include/linux as kvm_iodev.h

2023-09-15 Thread Sean Christopherson
Move iodev.h, the last remaining holdout in include/kvm, to the standard
include/linux directory as kvm_iodev.h and delete include/kvm.

Signed-off-by: Sean Christopherson 
---
 MAINTAINERS| 1 -
 arch/arm64/include/asm/kvm_vgic.h  | 2 +-
 arch/arm64/kvm/vgic/vgic-mmio-v2.c | 2 +-
 arch/arm64/kvm/vgic/vgic-mmio-v3.c | 2 +-
 arch/arm64/kvm/vgic/vgic-mmio.c| 2 +-
 arch/mips/include/asm/kvm_host.h   | 3 +--
 arch/powerpc/kvm/mpic.c| 2 +-
 arch/riscv/kvm/aia_aplic.c | 2 +-
 arch/riscv/kvm/aia_imsic.c | 2 +-
 arch/x86/kvm/i8254.h   | 2 +-
 arch/x86/kvm/ioapic.h  | 2 +-
 arch/x86/kvm/irq.h | 2 +-
 arch/x86/kvm/lapic.h   | 2 +-
 include/{kvm/iodev.h => linux/kvm_iodev.h} | 0
 virt/kvm/coalesced_mmio.c  | 3 +--
 virt/kvm/eventfd.c | 2 +-
 virt/kvm/kvm_main.c| 3 +--
 17 files changed, 15 insertions(+), 19 deletions(-)
 rename include/{kvm/iodev.h => linux/kvm_iodev.h} (100%)

diff --git a/MAINTAINERS b/MAINTAINERS
index 90f13281d297..ddc8375d536c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11498,7 +11498,6 @@ W:  http://www.linux-kvm.org
 T: git git://git.kernel.org/pub/scm/virt/kvm/kvm.git
 F: Documentation/virt/kvm/
 F: include/asm-generic/kvm*
-F: include/kvm/iodev.h
 F: include/linux/kvm*
 F: include/trace/events/kvm.h
 F: include/uapi/asm-generic/kvm*
diff --git a/arch/arm64/include/asm/kvm_vgic.h 
b/arch/arm64/include/asm/kvm_vgic.h
index 5b27f94d4fad..2ca52888bc75 100644
--- a/arch/arm64/include/asm/kvm_vgic.h
+++ b/arch/arm64/include/asm/kvm_vgic.h
@@ -13,7 +13,7 @@
 #include 
 #include 
 #include 
-#include 
+#include 
 #include 
 #include 
 
diff --git a/arch/arm64/kvm/vgic/vgic-mmio-v2.c 
b/arch/arm64/kvm/vgic/vgic-mmio-v2.c
index bba0cfeefffe..646053ee892f 100644
--- a/arch/arm64/kvm/vgic/vgic-mmio-v2.c
+++ b/arch/arm64/kvm/vgic/vgic-mmio-v2.c
@@ -6,9 +6,9 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
-#include 
 #include 
 
 #include "vgic.h"
diff --git a/arch/arm64/kvm/vgic/vgic-mmio-v3.c 
b/arch/arm64/kvm/vgic/vgic-mmio-v3.c
index d54a90beef61..b79a2e860415 100644
--- a/arch/arm64/kvm/vgic/vgic-mmio-v3.c
+++ b/arch/arm64/kvm/vgic/vgic-mmio-v3.c
@@ -7,8 +7,8 @@
 #include 
 #include 
 #include 
+#include 
 #include 
-#include 
 
 #include 
 #include 
diff --git a/arch/arm64/kvm/vgic/vgic-mmio.c b/arch/arm64/kvm/vgic/vgic-mmio.c
index 68a3d8062473..4feca3b1d915 100644
--- a/arch/arm64/kvm/vgic/vgic-mmio.c
+++ b/arch/arm64/kvm/vgic/vgic-mmio.c
@@ -9,7 +9,7 @@
 #include 
 #include 
 #include 
-#include 
+#include 
 #include 
 #include 
 
diff --git a/arch/mips/include/asm/kvm_host.h b/arch/mips/include/asm/kvm_host.h
index 54a85f1d4f2c..f8f63d0aa399 100644
--- a/arch/mips/include/asm/kvm_host.h
+++ b/arch/mips/include/asm/kvm_host.h
@@ -16,6 +16,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -24,8 +25,6 @@
 #include 
 #include 
 
-#include 
-
 /* MIPS KVM register ids */
 #define MIPS_CP0_32(_R, _S)\
(KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
diff --git a/arch/powerpc/kvm/mpic.c b/arch/powerpc/kvm/mpic.c
index 23e9c2bd9f27..b25a03251544 100644
--- a/arch/powerpc/kvm/mpic.c
+++ b/arch/powerpc/kvm/mpic.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -33,7 +34,6 @@
 #include 
 #include 
 #include 
-#include 
 
 #define MAX_CPU 32
 #define MAX_SRC 256
diff --git a/arch/riscv/kvm/aia_aplic.c b/arch/riscv/kvm/aia_aplic.c
index 39e72aa016a4..b49e747f2bad 100644
--- a/arch/riscv/kvm/aia_aplic.c
+++ b/arch/riscv/kvm/aia_aplic.c
@@ -11,7 +11,7 @@
 #include 
 #include 
 #include 
-#include 
+#include 
 #include 
 
 struct aplic_irq {
diff --git a/arch/riscv/kvm/aia_imsic.c b/arch/riscv/kvm/aia_imsic.c
index 6cf23b8adb71..586e466a1c6d 100644
--- a/arch/riscv/kvm/aia_imsic.c
+++ b/arch/riscv/kvm/aia_imsic.c
@@ -10,10 +10,10 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 
diff --git a/arch/x86/kvm/i8254.h b/arch/x86/kvm/i8254.h
index a768212ba821..4de7a0b88e4f 100644
--- a/arch/x86/kvm/i8254.h
+++ b/arch/x86/kvm/i8254.h
@@ -4,7 +4,7 @@
 
 #include 
 
-#include 
+#include 
 
 struct kvm_kpit_channel_state {
u32 count; /* can be 65536 */
diff --git a/arch/x86/kvm/ioapic.h b/arch/x86/kvm/ioapic.h
index 539333ac4b38..2beec2daf1a3 100644
--- a/arch/x86/kvm/ioapic.h
+++ b/arch/x86/kvm/ioapic.h
@@ -3,7 +3,7 @@
 #define __KVM_IO_APIC_H
 
 #include 
-#include 
+#include 
 #include "irq.h"
 
 struct kvm;
diff --git a/arch/x86/kvm/irq.h b/arch/x86/kvm/irq.h
index c2d7cfe82d00..f9530e9a66f8 100644
--- a/arch/x86/kvm/irq.h
+++ b/arch/x86/kvm/irq.h
@@ -13,9 +13,9 @@
 #include 
 #include 

[PATCH 14/26] KVM: arm64: Move ARM specific headers in include/kvm to arch directory

2023-09-15 Thread Sean Christopherson
Move kvm/arm_{arch_timer,pmu,vgic}.h to arch/arm64/include/asm and drop
the "arm" prefix from all file names.  Now that KVM no longer supports
32-bit ARM, there is no reason to expose ARM specific headers to other
architectures beyond arm64.

Signed-off-by: Sean Christopherson 
---
 .../arm64/include/asm/kvm_arch_timer.h | 0
 arch/arm64/include/asm/kvm_host.h  | 7 +++
 include/kvm/arm_pmu.h => arch/arm64/include/asm/kvm_pmu.h  | 0
 .../kvm/arm_vgic.h => arch/arm64/include/asm/kvm_vgic.h| 0
 arch/arm64/kvm/arch_timer.c| 5 ++---
 arch/arm64/kvm/arm.c   | 3 +--
 arch/arm64/kvm/pmu-emul.c  | 6 +++---
 arch/arm64/kvm/reset.c | 3 +--
 arch/arm64/kvm/trace_arm.h | 2 +-
 arch/arm64/kvm/vgic/vgic-debug.c   | 2 +-
 arch/arm64/kvm/vgic/vgic-init.c| 2 +-
 arch/arm64/kvm/vgic/vgic-irqfd.c   | 2 +-
 arch/arm64/kvm/vgic/vgic-kvm-device.c  | 2 +-
 arch/arm64/kvm/vgic/vgic-mmio-v2.c | 2 +-
 arch/arm64/kvm/vgic/vgic-mmio-v3.c | 2 +-
 arch/arm64/kvm/vgic/vgic-mmio.c| 4 ++--
 arch/arm64/kvm/vgic/vgic-v2.c  | 2 +-
 arch/arm64/kvm/vgic/vgic-v3.c  | 2 +-
 18 files changed, 21 insertions(+), 25 deletions(-)
 rename include/kvm/arm_arch_timer.h => arch/arm64/include/asm/kvm_arch_timer.h 
(100%)
 rename include/kvm/arm_pmu.h => arch/arm64/include/asm/kvm_pmu.h (100%)
 rename include/kvm/arm_vgic.h => arch/arm64/include/asm/kvm_vgic.h (100%)

diff --git a/include/kvm/arm_arch_timer.h 
b/arch/arm64/include/asm/kvm_arch_timer.h
similarity index 100%
rename from include/kvm/arm_arch_timer.h
rename to arch/arm64/include/asm/kvm_arch_timer.h
diff --git a/arch/arm64/include/asm/kvm_host.h 
b/arch/arm64/include/asm/kvm_host.h
index af06ccb7ee34..90cfbf420545 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -26,16 +26,15 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
+#include 
 
 #define __KVM_HAVE_ARCH_INTC_INITIALIZED
 
 #define KVM_HALT_POLL_NS_DEFAULT 50
 
-#include 
-#include 
-#include 
-
 #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
 
 #define KVM_VCPU_MAX_FEATURES 7
diff --git a/include/kvm/arm_pmu.h b/arch/arm64/include/asm/kvm_pmu.h
similarity index 100%
rename from include/kvm/arm_pmu.h
rename to arch/arm64/include/asm/kvm_pmu.h
diff --git a/include/kvm/arm_vgic.h b/arch/arm64/include/asm/kvm_vgic.h
similarity index 100%
rename from include/kvm/arm_vgic.h
rename to arch/arm64/include/asm/kvm_vgic.h
diff --git a/arch/arm64/kvm/arch_timer.c b/arch/arm64/kvm/arch_timer.c
index 6dcdae4d38cb..5e40bea8f3f3 100644
--- a/arch/arm64/kvm/arch_timer.c
+++ b/arch/arm64/kvm/arch_timer.c
@@ -14,12 +14,11 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
-
-#include 
-#include 
+#include 
 
 #include "trace.h"
 
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 6de1d63b981b..6480628197b4 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -39,10 +39,9 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
-#include 
-
 #include "arm_hypercalls.h"
 #include "arm_psci.h"
 
diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index 6b066e04dc5d..b5d999adce56 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -11,10 +11,10 @@
 #include 
 #include 
 #include 
-#include 
-#include 
-#include 
 #include 
+#include 
+#include 
+#include 
 
 #define PERF_ATTR_CFG1_COUNTER_64BIT   BIT(0)
 
diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c
index 7a65a35ee4ac..ad5e239774f1 100644
--- a/arch/arm64/kvm/reset.c
+++ b/arch/arm64/kvm/reset.c
@@ -17,12 +17,11 @@
 #include 
 #include 
 
-#include 
-
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
diff --git a/arch/arm64/kvm/trace_arm.h b/arch/arm64/kvm/trace_arm.h
index 8ad53104934d..76ef72de6133 100644
--- a/arch/arm64/kvm/trace_arm.h
+++ b/arch/arm64/kvm/trace_arm.h
@@ -3,7 +3,7 @@
 #define _TRACE_ARM_ARM64_KVM_H
 
 #include 
-#include 
+#include 
 #include 
 
 #undef TRACE_SYSTEM
diff --git a/arch/arm64/kvm/vgic/vgic-debug.c b/arch/arm64/kvm/vgic/vgic-debug.c
index 07aa0437125a..2d542040008c 100644
--- a/arch/arm64/kvm/vgic/vgic-debug.c
+++ b/arch/arm64/kvm/vgic/vgic-debug.c
@@ -9,7 +9,7 @@
 #include 
 #include 
 #include 
-#include 
+#include 
 #include 
 #include "vgic.h"
 
diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c
index c8c3cb812783..9f548ffac90b 100644
--- a/arch/arm64/kvm/vgic/vgic-init.c
+++ b/arch/arm64/kvm/vgic/vgic-init.c
@@ -7,7 +7,7 @@
 #include 
 #include 
 #include 
-#include 
+#include 
 #include 
 #include 
 #include 

[PATCH 13/26] KVM: arm64: Include KVM headers to get forward declarations

2023-09-15 Thread Sean Christopherson
Include include/uapi/linux/kvm.h and include/linux/kvm_types.h in ARM's
public arm_arch_timer.h and arm_pmu.h headers to get forward declarations
of things like "struct kvm_vcpu" and "struct kvm_device_attr", which are
referenced but never declared (neither file includes *any* KVM headers).

The missing includes don't currently cause problems because of the order
of includes in parent files, but that order is largely arbitrary and is
subject to change, e.g. a future commit will move the ARM specific headers
to arch/arm64/include/asm and reorder parent includes to maintain
alphabetic ordering.

Reported-by: kernel test robot 
Signed-off-by: Sean Christopherson 
---
 include/kvm/arm_arch_timer.h | 2 ++
 include/kvm/arm_pmu.h| 2 ++
 2 files changed, 4 insertions(+)

diff --git a/include/kvm/arm_arch_timer.h b/include/kvm/arm_arch_timer.h
index bb3cb005873e..e22a15c66288 100644
--- a/include/kvm/arm_arch_timer.h
+++ b/include/kvm/arm_arch_timer.h
@@ -7,6 +7,8 @@
 #ifndef __ASM_ARM_KVM_ARCH_TIMER_H
 #define __ASM_ARM_KVM_ARCH_TIMER_H
 
+#include 
+#include 
 #include 
 #include 
 
diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h
index 31029f4f7be8..e80f1d090579 100644
--- a/include/kvm/arm_pmu.h
+++ b/include/kvm/arm_pmu.h
@@ -7,6 +7,8 @@
 #ifndef __ASM_ARM_KVM_PMU_H
 #define __ASM_ARM_KVM_PMU_H
 
+#include 
+#include 
 #include 
 #include 
 
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 12/26] KVM: arm64: Move arm_{psci,hypercalls}.h to an internal KVM path

2023-09-15 Thread Sean Christopherson
From: Anish Ghulati 

Move arm_hypercalls.h and arm_psci.h into arch/arm64/kvm now that KVM
no longer supports 32-bit ARM, i.e. now that there's no reason to make
the hypercall and PSCI APIs "public".

Signed-off-by: Anish Ghulati 
[sean: squash into one patch, write changelog]
Signed-off-by: Sean Christopherson 
---
 arch/arm64/kvm/arm.c | 5 +++--
 {include => arch/arm64}/kvm/arm_hypercalls.h | 0
 {include => arch/arm64}/kvm/arm_psci.h   | 0
 arch/arm64/kvm/guest.c   | 2 +-
 arch/arm64/kvm/handle_exit.c | 2 +-
 arch/arm64/kvm/hyp/Makefile  | 4 ++--
 arch/arm64/kvm/hyp/include/hyp/switch.h  | 4 ++--
 arch/arm64/kvm/hyp/nvhe/switch.c | 4 ++--
 arch/arm64/kvm/hyp/vhe/switch.c  | 4 ++--
 arch/arm64/kvm/hypercalls.c  | 4 ++--
 arch/arm64/kvm/psci.c| 4 ++--
 arch/arm64/kvm/pvtime.c  | 2 +-
 arch/arm64/kvm/trng.c| 2 +-
 13 files changed, 19 insertions(+), 18 deletions(-)
 rename {include => arch/arm64}/kvm/arm_hypercalls.h (100%)
 rename {include => arch/arm64}/kvm/arm_psci.h (100%)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 4866b3f7b4ea..6de1d63b981b 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -41,9 +41,10 @@
 #include 
 #include 
 
-#include 
 #include 
-#include 
+
+#include "arm_hypercalls.h"
+#include "arm_psci.h"
 
 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
 
diff --git a/include/kvm/arm_hypercalls.h b/arch/arm64/kvm/arm_hypercalls.h
similarity index 100%
rename from include/kvm/arm_hypercalls.h
rename to arch/arm64/kvm/arm_hypercalls.h
diff --git a/include/kvm/arm_psci.h b/arch/arm64/kvm/arm_psci.h
similarity index 100%
rename from include/kvm/arm_psci.h
rename to arch/arm64/kvm/arm_psci.h
diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index 95f6945c4432..32045ad51ec4 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -18,7 +18,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -27,6 +26,7 @@
 #include 
 #include 
 
+#include "arm_hypercalls.h"
 #include "trace.h"
 
 const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
index 617ae6dea5d5..f1d02d6f8683 100644
--- a/arch/arm64/kvm/handle_exit.c
+++ b/arch/arm64/kvm/handle_exit.c
@@ -21,7 +21,7 @@
 #include 
 #include 
 
-#include 
+#include "arm_hypercalls.h"
 
 #define CREATE_TRACE_POINTS
 #include "trace_handle_exit.h"
diff --git a/arch/arm64/kvm/hyp/Makefile b/arch/arm64/kvm/hyp/Makefile
index a38dea6186c9..4ce8c86967b5 100644
--- a/arch/arm64/kvm/hyp/Makefile
+++ b/arch/arm64/kvm/hyp/Makefile
@@ -4,7 +4,7 @@
 #
 
 incdir := $(srctree)/$(src)/include
-subdir-asflags-y := -I$(incdir)
-subdir-ccflags-y := -I$(incdir)
+subdir-asflags-y := -I$(incdir) -I$(srctree)/arch/arm64/kvm
+subdir-ccflags-y := -I$(incdir) -I$(srctree)/arch/arm64/kvm
 
 obj-$(CONFIG_KVM) += vhe/ nvhe/ pgtable.o
diff --git a/arch/arm64/kvm/hyp/include/hyp/switch.h 
b/arch/arm64/kvm/hyp/include/hyp/switch.h
index 9cfe6bd1dbe4..cd21cf71d7db 100644
--- a/arch/arm64/kvm/hyp/include/hyp/switch.h
+++ b/arch/arm64/kvm/hyp/include/hyp/switch.h
@@ -16,8 +16,6 @@
 #include 
 #include 
 
-#include 
-
 #include 
 #include 
 #include 
@@ -31,6 +29,8 @@
 #include 
 #include 
 
+#include "arm_psci.h"
+
 struct kvm_exception_table_entry {
int insn, fixup;
 };
diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c
index c353a06ee7e6..c0553ac36a46 100644
--- a/arch/arm64/kvm/hyp/nvhe/switch.c
+++ b/arch/arm64/kvm/hyp/nvhe/switch.c
@@ -13,8 +13,6 @@
 #include 
 #include 
 
-#include 
-
 #include 
 #include 
 #include 
@@ -29,6 +27,8 @@
 #include 
 #include 
 
+#include "arm_psci.h"
+
 /* Non-VHE specific context */
 DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data);
 DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c
index 6537f58b1a8c..f7423c890b5c 100644
--- a/arch/arm64/kvm/hyp/vhe/switch.c
+++ b/arch/arm64/kvm/hyp/vhe/switch.c
@@ -13,8 +13,6 @@
 #include 
 #include 
 
-#include 
-
 #include 
 #include 
 #include 
@@ -28,6 +26,8 @@
 #include 
 #include 
 
+#include "arm_psci.h"
+
 /* VHE specific context */
 DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data);
 DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c
index 7fb4df0456de..d34a31c9ace2 100644
--- a/arch/arm64/kvm/hypercalls.c
+++ b/arch/arm64/kvm/hypercalls.c
@@ -6,8 +6,8 @@
 
 #include 
 
-#include 
-#include 
+#include "arm_hypercalls.h"
+#include "arm_psci.h"
 
 #define KVM_ARM_SMCCC_STD_FEATURES \
GENMASK(KVM_REG_ARM_STD_BMAP_BIT_COUNT - 1, 0)
diff --git a/arch/arm64/kvm/psci.c b/arch/arm64/kvm/psci.c
index 1f69b667332b..c461e15026c9 100644
--- 

[PATCH 11/26] KVM: MIPS: Make HAVE_KVM a MIPS-only Kconfig

2023-09-15 Thread Sean Christopherson
Now that MIPS is the sole remaining user of HAVE_KVM, make HAVE_KVM a
MIPS-only Kconfig.  Because most architectures unconditionally support
virtualization, advertising to the kernel at-large that an arch has KVM
just led to a bunch of useless and misguided #ifdefs.

Signed-off-by: Sean Christopherson 
---
 arch/mips/kvm/Kconfig | 3 +++
 virt/kvm/Kconfig  | 3 ---
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/mips/kvm/Kconfig b/arch/mips/kvm/Kconfig
index a8cdba75f98d..c4508f49caae 100644
--- a/arch/mips/kvm/Kconfig
+++ b/arch/mips/kvm/Kconfig
@@ -4,6 +4,9 @@
 #
 source "virt/kvm/Kconfig"
 
+config HAVE_KVM
+   bool
+
 menuconfig VIRTUALIZATION
bool "Virtualization"
help
diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig
index f0be3b55cea6..f7bb6c59658e 100644
--- a/virt/kvm/Kconfig
+++ b/virt/kvm/Kconfig
@@ -1,9 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 # KVM common configuration items and defaults
 
-config HAVE_KVM
-   bool
-
 config HAVE_KVM_PFNCACHE
bool
 
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 10/26] KVM: s390: Stop selecting and depending on HAVE_KVM

2023-09-15 Thread Sean Christopherson
Now that nothing in s390 or architecture agnostic code consumes HAVE_KVM,
stop selecting it in s390.  This is one of several steps towards deleting
HAVE_KVM from the common KVM Kconfig.

Signed-off-by: Sean Christopherson 
---
 arch/s390/Kconfig | 1 -
 arch/s390/kvm/Kconfig | 1 -
 2 files changed, 2 deletions(-)

diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig
index ae29e4392664..8a5dca0036d9 100644
--- a/arch/s390/Kconfig
+++ b/arch/s390/Kconfig
@@ -193,7 +193,6 @@ config S390
select HAVE_KPROBES
select HAVE_KPROBES_ON_FTRACE
select HAVE_KRETPROBES
-   select HAVE_KVM
select HAVE_LIVEPATCH
select HAVE_MEMBLOCK_PHYS_MAP
select HAVE_MOD_ARCH_SPECIFIC
diff --git a/arch/s390/kvm/Kconfig b/arch/s390/kvm/Kconfig
index 459d536116a6..9056cee72945 100644
--- a/arch/s390/kvm/Kconfig
+++ b/arch/s390/kvm/Kconfig
@@ -19,7 +19,6 @@ if VIRTUALIZATION
 config KVM
def_tristate y
prompt "Kernel-based Virtual Machine (KVM) support"
-   depends on HAVE_KVM
select PREEMPT_NOTIFIERS
select HAVE_KVM_CPU_RELAX_INTERCEPT
select HAVE_KVM_VCPU_ASYNC_IOCTL
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 09/26] KVM: arm64: Stop selecting and depending on HAVE_KVM

2023-09-15 Thread Sean Christopherson
Now that nothing in arm64 or architecture agnostic code consumes HAVE_KVM,
stop selecting it in arm64.  This is one of several steps towards deleting
HAVE_KVM from the common KVM Kconfig.

Signed-off-by: Sean Christopherson 
---
 arch/arm64/Kconfig | 1 -
 arch/arm64/kvm/Kconfig | 1 -
 2 files changed, 2 deletions(-)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index b10515c0200b..7d99f132e57c 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -214,7 +214,6 @@ config ARM64
select HAVE_HW_BREAKPOINT if PERF_EVENTS
select HAVE_IOREMAP_PROT
select HAVE_IRQ_TIME_ACCOUNTING
-   select HAVE_KVM
select HAVE_MOD_ARCH_SPECIFIC
select HAVE_NMI
select HAVE_PERF_EVENTS
diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig
index 2b5c332f157d..c76af1973315 100644
--- a/arch/arm64/kvm/Kconfig
+++ b/arch/arm64/kvm/Kconfig
@@ -20,7 +20,6 @@ if VIRTUALIZATION
 
 menuconfig KVM
bool "Kernel-based Virtual Machine (KVM) support"
-   depends on HAVE_KVM
select KVM_GENERIC_HARDWARE_ENABLING
select MMU_NOTIFIER
select PREEMPT_NOTIFIERS
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 08/26] KVM: x86: Stop selecting and depending on HAVE_KVM

2023-09-15 Thread Sean Christopherson
Now that nothing in x86 or architecture agnostic code consumes HAVE_KVM,
stop selecting it in x86.  This is one of several steps towards deleting
HAVE_KVM from the common KVM Kconfig.

Signed-off-by: Sean Christopherson 
---
 arch/x86/Kconfig | 1 -
 arch/x86/kvm/Kconfig | 2 --
 2 files changed, 3 deletions(-)

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 982b777eadc7..32eb288a4e3a 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -240,7 +240,6 @@ config X86
select HAVE_FUNCTION_ERROR_INJECTION
select HAVE_KRETPROBES
select HAVE_RETHOOK
-   select HAVE_KVM
select HAVE_LIVEPATCH   if X86_64
select HAVE_MIXED_BREAKPOINTS_REGS
select HAVE_MOD_ARCH_SPECIFIC
diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig
index 0f01e5600b5f..8c5fb7f57b4c 100644
--- a/arch/x86/kvm/Kconfig
+++ b/arch/x86/kvm/Kconfig
@@ -7,7 +7,6 @@ source "virt/kvm/Kconfig"
 
 menuconfig VIRTUALIZATION
bool "Virtualization"
-   depends on HAVE_KVM || X86
default y
help
  Say Y here to get to see options for using your Linux host to run 
other
@@ -20,7 +19,6 @@ if VIRTUALIZATION
 
 config KVM
tristate "Kernel-based Virtual Machine (KVM) support"
-   depends on HAVE_KVM
depends on HIGH_RES_TIMERS
depends on X86_LOCAL_APIC
select PREEMPT_NOTIFIERS
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 07/26] x86/idt: Wrap KVM logic with CONFIG_KVM instead of CONFIG_HAVE_KVM

2023-09-15 Thread Sean Christopherson
Wrap all of the IDT-related code that deals with KVM in CONFIG_KVM instead
of CONFIG_HAVE_KVM.  x86 always "has" KVM, i.e. checking CONFIG_HAVE_KVM
is pointless.

Alternatively, the #ifdefs could simply be deleted, but the only potential
value is in printing the number of posted interrupts that end up being
handled by the host, everything else is completely dead code when KVM is
disabled.  And if KVM is disabled, there should not be any posted
interrupts, i.e. NOT wiring up the "dummy" handlers and treating IRQs on
those vectors as spurious is the right thing to do.

Signed-off-by: Sean Christopherson 
---
 arch/x86/include/asm/hardirq.h   | 2 +-
 arch/x86/include/asm/idtentry.h  | 2 +-
 arch/x86/include/asm/irq.h   | 2 +-
 arch/x86/include/asm/irq_vectors.h   | 2 +-
 arch/x86/kernel/idt.c| 2 +-
 arch/x86/kernel/irq.c| 4 ++--
 scripts/gdb/linux/constants.py.in| 2 +-
 scripts/gdb/linux/interrupts.py  | 2 +-
 tools/arch/x86/include/asm/irq_vectors.h | 2 +-
 9 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/arch/x86/include/asm/hardirq.h b/arch/x86/include/asm/hardirq.h
index 66837b8c67f1..fbc7722b87d1 100644
--- a/arch/x86/include/asm/hardirq.h
+++ b/arch/x86/include/asm/hardirq.h
@@ -15,7 +15,7 @@ typedef struct {
unsigned int irq_spurious_count;
unsigned int icr_read_retry_count;
 #endif
-#ifdef CONFIG_HAVE_KVM
+#if IS_ENABLED(CONFIG_KVM)
unsigned int kvm_posted_intr_ipis;
unsigned int kvm_posted_intr_wakeup_ipis;
unsigned int kvm_posted_intr_nested_ipis;
diff --git a/arch/x86/include/asm/idtentry.h b/arch/x86/include/asm/idtentry.h
index 05fd175cec7d..8eef16d0d72b 100644
--- a/arch/x86/include/asm/idtentry.h
+++ b/arch/x86/include/asm/idtentry.h
@@ -671,7 +671,7 @@ DECLARE_IDTENTRY_SYSVEC(IRQ_WORK_VECTOR,
sysvec_irq_work);
 # endif
 #endif
 
-#ifdef CONFIG_HAVE_KVM
+#if IS_ENABLED(CONFIG_KVM)
 DECLARE_IDTENTRY_SYSVEC(POSTED_INTR_VECTOR,
sysvec_kvm_posted_intr_ipi);
 DECLARE_IDTENTRY_SYSVEC(POSTED_INTR_WAKEUP_VECTOR, 
sysvec_kvm_posted_intr_wakeup_ipi);
 DECLARE_IDTENTRY_SYSVEC(POSTED_INTR_NESTED_VECTOR, 
sysvec_kvm_posted_intr_nested_ipi);
diff --git a/arch/x86/include/asm/irq.h b/arch/x86/include/asm/irq.h
index 836c170d3087..194dfff84cb1 100644
--- a/arch/x86/include/asm/irq.h
+++ b/arch/x86/include/asm/irq.h
@@ -29,7 +29,7 @@ struct irq_desc;
 
 extern void fixup_irqs(void);
 
-#ifdef CONFIG_HAVE_KVM
+#if IS_ENABLED(CONFIG_KVM)
 extern void kvm_set_posted_intr_wakeup_handler(void (*handler)(void));
 #endif
 
diff --git a/arch/x86/include/asm/irq_vectors.h 
b/arch/x86/include/asm/irq_vectors.h
index 3a19904c2db6..3f73ac3ed3a0 100644
--- a/arch/x86/include/asm/irq_vectors.h
+++ b/arch/x86/include/asm/irq_vectors.h
@@ -84,7 +84,7 @@
 #define HYPERVISOR_CALLBACK_VECTOR 0xf3
 
 /* Vector for KVM to deliver posted interrupt IPI */
-#ifdef CONFIG_HAVE_KVM
+#if IS_ENABLED(CONFIG_KVM)
 #define POSTED_INTR_VECTOR 0xf2
 #define POSTED_INTR_WAKEUP_VECTOR  0xf1
 #define POSTED_INTR_NESTED_VECTOR  0xf0
diff --git a/arch/x86/kernel/idt.c b/arch/x86/kernel/idt.c
index b786d48f5a0f..a9e6363d88c2 100644
--- a/arch/x86/kernel/idt.c
+++ b/arch/x86/kernel/idt.c
@@ -149,7 +149,7 @@ static const __initconst struct idt_data apic_idts[] = {
 #ifdef CONFIG_X86_LOCAL_APIC
INTG(LOCAL_TIMER_VECTOR,
asm_sysvec_apic_timer_interrupt),
INTG(X86_PLATFORM_IPI_VECTOR,   asm_sysvec_x86_platform_ipi),
-# ifdef CONFIG_HAVE_KVM
+#if IS_ENABLED(CONFIG_KVM)
INTG(POSTED_INTR_VECTOR,asm_sysvec_kvm_posted_intr_ipi),
INTG(POSTED_INTR_WAKEUP_VECTOR, 
asm_sysvec_kvm_posted_intr_wakeup_ipi),
INTG(POSTED_INTR_NESTED_VECTOR, 
asm_sysvec_kvm_posted_intr_nested_ipi),
diff --git a/arch/x86/kernel/irq.c b/arch/x86/kernel/irq.c
index 11761c124545..35fde0107901 100644
--- a/arch/x86/kernel/irq.c
+++ b/arch/x86/kernel/irq.c
@@ -164,7 +164,7 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 #if defined(CONFIG_X86_IO_APIC)
seq_printf(p, "%*s: %10u\n", prec, "MIS", atomic_read(_mis_count));
 #endif
-#ifdef CONFIG_HAVE_KVM
+#if IS_ENABLED(CONFIG_KVM)
seq_printf(p, "%*s: ", prec, "PIN");
for_each_online_cpu(j)
seq_printf(p, "%10u ", irq_stats(j)->kvm_posted_intr_ipis);
@@ -290,7 +290,7 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_x86_platform_ipi)
 }
 #endif
 
-#ifdef CONFIG_HAVE_KVM
+#if IS_ENABLED(CONFIG_KVM)
 static void dummy_handler(void) {}
 static void (*kvm_posted_intr_wakeup_handler)(void) = dummy_handler;
 
diff --git a/scripts/gdb/linux/constants.py.in 
b/scripts/gdb/linux/constants.py.in
index e3517d4ab8ec..054630f511e0 100644
--- a/scripts/gdb/linux/constants.py.in
+++ b/scripts/gdb/linux/constants.py.in
@@ -129,7 +129,7 @@ LX_CONFIG(CONFIG_X86_MCE_THRESHOLD)
 LX_CONFIG(CONFIG_X86_MCE_AMD)
 LX_CONFIG(CONFIG_X86_MCE)
 

[PATCH 06/26] KVM: Drop CONFIG_KVM_VFIO and just look at KVM+VFIO

2023-09-15 Thread Sean Christopherson
Drop KVM's KVM_VFIO Kconfig, and instead compile in VFIO support if
and only if VFIO itself is enabled.  Similar to the recent change to have
VFIO stop looking at HAVE_KVM, compiling in support for talking to VFIO
just because the architecture supports VFIO is nonsensical.

This fixes a bug where RISC-V doesn't select KVM_VFIO, i.e. would silently
fail to do connect KVM and VFIO, even though RISC-V supports VFIO.  The
bug is benign as the only driver in all of Linux that actually uses the
KVM reference provided by VFIO is KVM-GT, which is x86/Intel specific.

Signed-off-by: Sean Christopherson 
---
 arch/arm64/kvm/Kconfig   | 1 -
 arch/powerpc/kvm/Kconfig | 1 -
 arch/s390/kvm/Kconfig| 1 -
 arch/x86/kvm/Kconfig | 1 -
 virt/kvm/Kconfig | 3 ---
 virt/kvm/Makefile.kvm| 4 +++-
 virt/kvm/vfio.h  | 2 +-
 7 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig
index 83c1e09be42e..2b5c332f157d 100644
--- a/arch/arm64/kvm/Kconfig
+++ b/arch/arm64/kvm/Kconfig
@@ -28,7 +28,6 @@ menuconfig KVM
select KVM_MMIO
select KVM_GENERIC_DIRTYLOG_READ_PROTECT
select KVM_XFER_TO_GUEST_WORK
-   select KVM_VFIO
select HAVE_KVM_EVENTFD
select HAVE_KVM_IRQFD
select HAVE_KVM_DIRTY_RING_ACQ_REL
diff --git a/arch/powerpc/kvm/Kconfig b/arch/powerpc/kvm/Kconfig
index 902611954200..c4beb49c0eb2 100644
--- a/arch/powerpc/kvm/Kconfig
+++ b/arch/powerpc/kvm/Kconfig
@@ -22,7 +22,6 @@ config KVM
select PREEMPT_NOTIFIERS
select HAVE_KVM_EVENTFD
select HAVE_KVM_VCPU_ASYNC_IOCTL
-   select KVM_VFIO
select IRQ_BYPASS_MANAGER
select HAVE_KVM_IRQ_BYPASS
select INTERVAL_TREE
diff --git a/arch/s390/kvm/Kconfig b/arch/s390/kvm/Kconfig
index 45fdf2a9b2e3..459d536116a6 100644
--- a/arch/s390/kvm/Kconfig
+++ b/arch/s390/kvm/Kconfig
@@ -31,7 +31,6 @@ config KVM
select HAVE_KVM_IRQ_ROUTING
select HAVE_KVM_INVALID_WAKEUPS
select HAVE_KVM_NO_POLL
-   select KVM_VFIO
select INTERVAL_TREE
select MMU_NOTIFIER
help
diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig
index ed90f148140d..0f01e5600b5f 100644
--- a/arch/x86/kvm/Kconfig
+++ b/arch/x86/kvm/Kconfig
@@ -45,7 +45,6 @@ config KVM
select HAVE_KVM_NO_POLL
select KVM_XFER_TO_GUEST_WORK
select KVM_GENERIC_DIRTYLOG_READ_PROTECT
-   select KVM_VFIO
select INTERVAL_TREE
select HAVE_KVM_PM_NOTIFIER if PM
select KVM_GENERIC_HARDWARE_ENABLING
diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig
index 484d0873061c..f0be3b55cea6 100644
--- a/virt/kvm/Kconfig
+++ b/virt/kvm/Kconfig
@@ -59,9 +59,6 @@ config HAVE_KVM_MSI
 config HAVE_KVM_CPU_RELAX_INTERCEPT
bool
 
-config KVM_VFIO
-   bool
-
 config HAVE_KVM_INVALID_WAKEUPS
bool
 
diff --git a/virt/kvm/Makefile.kvm b/virt/kvm/Makefile.kvm
index 2c27d5d0c367..29373b59d89a 100644
--- a/virt/kvm/Makefile.kvm
+++ b/virt/kvm/Makefile.kvm
@@ -6,7 +6,9 @@
 KVM ?= ../../../virt/kvm
 
 kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o
-kvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o
+ifdef CONFIG_VFIO
+kvm-y += $(KVM)/vfio.o
+endif
 kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o
 kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
 kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o
diff --git a/virt/kvm/vfio.h b/virt/kvm/vfio.h
index e130a4a03530..af475a323965 100644
--- a/virt/kvm/vfio.h
+++ b/virt/kvm/vfio.h
@@ -2,7 +2,7 @@
 #ifndef __KVM_VFIO_H
 #define __KVM_VFIO_H
 
-#ifdef CONFIG_KVM_VFIO
+#if IS_ENABLED(CONFIG_KVM) && IS_ENABLED(CONFIG_VFIO)
 int kvm_vfio_ops_init(void);
 void kvm_vfio_ops_exit(void);
 #else
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 05/26] vfio: KVM: Pass get/put helpers from KVM to VFIO, don't do circular lookup

2023-09-15 Thread Sean Christopherson
Explicitly pass KVM's get/put helpers to VFIO when attaching a VM to
VFIO instead of having VFIO do a symbol lookup back into KVM.  Having both
KVM and VFIO do symbol lookups increases the overall complexity and places
an unnecessary dependency on KVM (from VFIO) without adding any value.

Signed-off-by: Sean Christopherson 
---
 drivers/vfio/vfio.h  |  2 ++
 drivers/vfio/vfio_main.c | 74 +++-
 include/linux/vfio.h |  4 ++-
 virt/kvm/vfio.c  |  9 +++--
 4 files changed, 47 insertions(+), 42 deletions(-)

diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
index a1f741365075..eec51c7ee822 100644
--- a/drivers/vfio/vfio.h
+++ b/drivers/vfio/vfio.h
@@ -19,6 +19,8 @@ struct vfio_container;
 
 struct vfio_kvm_reference {
struct kvm  *kvm;
+   bool(*get_kvm)(struct kvm *kvm);
+   void(*put_kvm)(struct kvm *kvm);
spinlock_t  lock;
 };
 
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index e77e8c6aae2f..1f58ab6dbcd2 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -16,7 +16,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -1306,38 +1305,22 @@ EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
 void vfio_device_get_kvm_safe(struct vfio_device *device,
  struct vfio_kvm_reference *ref)
 {
-   void (*pfn)(struct kvm *kvm);
-   bool (*fn)(struct kvm *kvm);
-   bool ret;
-
lockdep_assert_held(>dev_set->lock);
 
+   /*
+* Note!  The "kvm" and "put_kvm" pointers *must* be transferred to the
+* device so that the device can put its reference to KVM.  KVM can
+* invoke vfio_device_set_kvm() to detach from VFIO, i.e. nullify all
+* pointers in @ref, even if a device holds a reference to KVM!  That
+* also means that detaching KVM from VFIO only prevents "new" devices
+* from using KVM, it doesn't invalidate KVM references in existing
+* devices.
+*/
spin_lock(>lock);
-
-   if (!ref->kvm)
-   goto out;
-
-   pfn = symbol_get(kvm_put_kvm);
-   if (WARN_ON(!pfn))
-   goto out;
-
-   fn = symbol_get(kvm_get_kvm_safe);
-   if (WARN_ON(!fn)) {
-   symbol_put(kvm_put_kvm);
-   goto out;
+   if (ref->kvm && ref->get_kvm(ref->kvm)) {
+   device->kvm = ref->kvm;
+   device->put_kvm = ref->put_kvm;
}
-
-   ret = fn(ref->kvm);
-   symbol_put(kvm_get_kvm_safe);
-   if (!ret) {
-   symbol_put(kvm_put_kvm);
-   goto out;
-   }
-
-   device->put_kvm = pfn;
-   device->kvm = ref->kvm;
-
-out:
spin_unlock(>lock);
 }
 
@@ -1353,28 +1336,37 @@ void vfio_device_put_kvm(struct vfio_device *device)
 
device->put_kvm(device->kvm);
device->put_kvm = NULL;
-   symbol_put(kvm_put_kvm);
-
 clear:
device->kvm = NULL;
 }
 
 static void vfio_device_set_kvm(struct vfio_kvm_reference *ref,
-   struct kvm *kvm)
+   struct kvm *kvm,
+   bool (*get_kvm)(struct kvm *kvm),
+   void (*put_kvm)(struct kvm *kvm))
 {
+   if (WARN_ON_ONCE(kvm && (!get_kvm || !put_kvm)))
+   return;
+
spin_lock(>lock);
ref->kvm = kvm;
+   ref->get_kvm = get_kvm;
+   ref->put_kvm = put_kvm;
spin_unlock(>lock);
 }
 
-static void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
+static void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm,
+  bool (*get_kvm)(struct kvm *kvm),
+  void (*put_kvm)(struct kvm *kvm))
 {
 #if IS_ENABLED(CONFIG_VFIO_GROUP)
-   vfio_device_set_kvm(>kvm_ref, kvm);
+   vfio_device_set_kvm(>kvm_ref, kvm, get_kvm, put_kvm);
 #endif
 }
 
-static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
+static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm,
+bool (*get_kvm)(struct kvm *kvm),
+void (*put_kvm)(struct kvm *kvm))
 {
struct vfio_device_file *df = file->private_data;
 
@@ -1383,27 +1375,31 @@ static void vfio_device_file_set_kvm(struct file *file, 
struct kvm *kvm)
 * be propagated to vfio_device::kvm when the file is bound to
 * iommufd successfully in the vfio device cdev path.
 */
-   vfio_device_set_kvm(>kvm_ref, kvm);
+   vfio_device_set_kvm(>kvm_ref, kvm, get_kvm, put_kvm);
 }
 
 /**
  * vfio_file_set_kvm - Link a kvm with VFIO drivers
  * @file: VFIO group file or VFIO device file
  * @kvm: KVM to link
+ * @get_kvm: Callback to get a reference to @kvm
+ * @put_kvm: Callback to put a reference to @kvm
  *
  * When a 

[PATCH 04/26] vfio: Add struct to hold KVM assets and dedup group vs. iommufd code

2023-09-15 Thread Sean Christopherson
Add a struct to hold the KVM assets need to manage and pass along KVM
references to VFIO devices.  Providing a common struct deduplicates the
group vs. iommufd code, and will make it easier to rework the attachment
logic so that VFIO doesn't have to do a symbol lookup to retrieve the
get/put helpers from KVM.

Signed-off-by: Sean Christopherson 
---
 drivers/vfio/device_cdev.c |  9 +---
 drivers/vfio/group.c   | 18 ++--
 drivers/vfio/vfio.h| 22 +--
 drivers/vfio/vfio_main.c   | 43 +++---
 4 files changed, 45 insertions(+), 47 deletions(-)

diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c
index e75da0a70d1f..e484d6d6400a 100644
--- a/drivers/vfio/device_cdev.c
+++ b/drivers/vfio/device_cdev.c
@@ -46,13 +46,6 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct 
file *filep)
return ret;
 }
 
-static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
-{
-   spin_lock(>kvm_ref_lock);
-   vfio_device_get_kvm_safe(df->device, df->kvm);
-   spin_unlock(>kvm_ref_lock);
-}
-
 long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
struct vfio_device_bind_iommufd __user *arg)
 {
@@ -99,7 +92,7 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
 * a reference.  This reference is held until device closed.
 * Save the pointer in the device for use by drivers.
 */
-   vfio_df_get_kvm_safe(df);
+   vfio_device_get_kvm_safe(df->device, >kvm_ref);
 
ret = vfio_df_open(df);
if (ret)
diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c
index 610a429c6191..756e47ff4cf0 100644
--- a/drivers/vfio/group.c
+++ b/drivers/vfio/group.c
@@ -157,13 +157,6 @@ static int vfio_group_ioctl_set_container(struct 
vfio_group *group,
return ret;
 }
 
-static void vfio_device_group_get_kvm_safe(struct vfio_device *device)
-{
-   spin_lock(>group->kvm_ref_lock);
-   vfio_device_get_kvm_safe(device, device->group->kvm);
-   spin_unlock(>group->kvm_ref_lock);
-}
-
 static int vfio_df_group_open(struct vfio_device_file *df)
 {
struct vfio_device *device = df->device;
@@ -184,7 +177,7 @@ static int vfio_df_group_open(struct vfio_device_file *df)
 * the pointer in the device for use by drivers.
 */
if (device->open_count == 0)
-   vfio_device_group_get_kvm_safe(device);
+   vfio_device_get_kvm_safe(device, >group->kvm_ref);
 
df->iommufd = device->group->iommufd;
if (df->iommufd && vfio_device_is_noiommu(device) && device->open_count 
== 0) {
@@ -560,7 +553,7 @@ static struct vfio_group *vfio_group_alloc(struct 
iommu_group *iommu_group,
 
refcount_set(>drivers, 1);
mutex_init(>group_lock);
-   spin_lock_init(>kvm_ref_lock);
+   spin_lock_init(>kvm_ref.lock);
INIT_LIST_HEAD(>device_list);
mutex_init(>device_lock);
group->iommu_group = iommu_group;
@@ -884,13 +877,6 @@ bool vfio_group_enforced_coherent(struct vfio_group *group)
return ret;
 }
 
-void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
-{
-   spin_lock(>kvm_ref_lock);
-   group->kvm = kvm;
-   spin_unlock(>kvm_ref_lock);
-}
-
 /**
  * vfio_file_has_dev - True if the VFIO file is a handle for device
  * @file: VFIO file to check
diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
index c26d1ad68105..a1f741365075 100644
--- a/drivers/vfio/vfio.h
+++ b/drivers/vfio/vfio.h
@@ -12,18 +12,23 @@
 #include 
 #include 
 
+struct kvm;
 struct iommufd_ctx;
 struct iommu_group;
 struct vfio_container;
 
+struct vfio_kvm_reference {
+   struct kvm  *kvm;
+   spinlock_t  lock;
+};
+
 struct vfio_device_file {
struct vfio_device *device;
struct vfio_group *group;
 
u8 access_granted;
u32 devid; /* only valid when iommufd is valid */
-   spinlock_t kvm_ref_lock; /* protect kvm field */
-   struct kvm *kvm;
+   struct vfio_kvm_reference kvm_ref;
struct iommufd_ctx *iommufd; /* protected by struct 
vfio_device_set::lock */
 };
 
@@ -88,11 +93,10 @@ struct vfio_group {
 #endif
enum vfio_group_typetype;
struct mutexgroup_lock;
-   struct kvm  *kvm;
+   struct vfio_kvm_reference   kvm_ref;
struct file *opened_file;
struct blocking_notifier_head   notifier;
struct iommufd_ctx  *iommufd;
-   spinlock_t  kvm_ref_lock;
unsigned intcdev_device_open_cnt;
 };
 
@@ -108,7 +112,6 @@ void vfio_device_group_unuse_iommu(struct vfio_device 
*device);
 void vfio_df_group_close(struct vfio_device_file *df);
 struct vfio_group *vfio_group_from_file(struct file *file);
 bool vfio_group_enforced_coherent(struct vfio_group *group);
-void 

[PATCH 03/26] virt: Declare and define vfio_file_set_kvm() iff CONFIG_KVM is enabled

2023-09-15 Thread Sean Christopherson
Hide vfio_file_set_kvm() and its unique helpers if KVM is not enabled,
nothing else in the kernel (or out of the kernel) should be using a
KVM specific helper.

Signed-off-by: Sean Christopherson 
---
 drivers/vfio/vfio_main.c | 2 +-
 include/linux/vfio.h | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 6368eed7b7b2..124cc88966a7 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -1352,7 +1352,6 @@ void vfio_device_put_kvm(struct vfio_device *device)
 clear:
device->kvm = NULL;
 }
-#endif
 
 static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
 {
@@ -1388,6 +1387,7 @@ void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
vfio_device_file_set_kvm(file, kvm);
 }
 EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
+#endif
 
 /*
  * Sub-module support
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 454e9295970c..e80955de266c 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -311,7 +311,9 @@ static inline bool vfio_file_has_dev(struct file *file, 
struct vfio_device *devi
 #endif
 bool vfio_file_is_valid(struct file *file);
 bool vfio_file_enforced_coherent(struct file *file);
+#if IS_ENABLED(CONFIG_KVM)
 void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
+#endif
 
 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
 
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 02/26] vfio: Move KVM get/put helpers to colocate it with other KVM related code

2023-09-15 Thread Sean Christopherson
Move the definitions of vfio_device_get_kvm_safe() and vfio_device_put_kvm()
down in vfio_main.c to colocate them with other KVM-specific functions,
e.g. to allow wrapping them all with a single CONFIG_KVM check.

Signed-off-by: Sean Christopherson 
---
 drivers/vfio/vfio_main.c | 104 +++
 1 file changed, 52 insertions(+), 52 deletions(-)

diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 80e39f7a6d8f..6368eed7b7b2 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -381,58 +381,6 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 }
 EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 
-#if IS_ENABLED(CONFIG_KVM)
-void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm)
-{
-   void (*pfn)(struct kvm *kvm);
-   bool (*fn)(struct kvm *kvm);
-   bool ret;
-
-   lockdep_assert_held(>dev_set->lock);
-
-   if (!kvm)
-   return;
-
-   pfn = symbol_get(kvm_put_kvm);
-   if (WARN_ON(!pfn))
-   return;
-
-   fn = symbol_get(kvm_get_kvm_safe);
-   if (WARN_ON(!fn)) {
-   symbol_put(kvm_put_kvm);
-   return;
-   }
-
-   ret = fn(kvm);
-   symbol_put(kvm_get_kvm_safe);
-   if (!ret) {
-   symbol_put(kvm_put_kvm);
-   return;
-   }
-
-   device->put_kvm = pfn;
-   device->kvm = kvm;
-}
-
-void vfio_device_put_kvm(struct vfio_device *device)
-{
-   lockdep_assert_held(>dev_set->lock);
-
-   if (!device->kvm)
-   return;
-
-   if (WARN_ON(!device->put_kvm))
-   goto clear;
-
-   device->put_kvm(device->kvm);
-   device->put_kvm = NULL;
-   symbol_put(kvm_put_kvm);
-
-clear:
-   device->kvm = NULL;
-}
-#endif
-
 /* true if the vfio_device has open_device() called but not close_device() */
 static bool vfio_assert_device_open(struct vfio_device *device)
 {
@@ -1354,6 +1302,58 @@ bool vfio_file_enforced_coherent(struct file *file)
 }
 EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
 
+#if IS_ENABLED(CONFIG_KVM)
+void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm)
+{
+   void (*pfn)(struct kvm *kvm);
+   bool (*fn)(struct kvm *kvm);
+   bool ret;
+
+   lockdep_assert_held(>dev_set->lock);
+
+   if (!kvm)
+   return;
+
+   pfn = symbol_get(kvm_put_kvm);
+   if (WARN_ON(!pfn))
+   return;
+
+   fn = symbol_get(kvm_get_kvm_safe);
+   if (WARN_ON(!fn)) {
+   symbol_put(kvm_put_kvm);
+   return;
+   }
+
+   ret = fn(kvm);
+   symbol_put(kvm_get_kvm_safe);
+   if (!ret) {
+   symbol_put(kvm_put_kvm);
+   return;
+   }
+
+   device->put_kvm = pfn;
+   device->kvm = kvm;
+}
+
+void vfio_device_put_kvm(struct vfio_device *device)
+{
+   lockdep_assert_held(>dev_set->lock);
+
+   if (!device->kvm)
+   return;
+
+   if (WARN_ON(!device->put_kvm))
+   goto clear;
+
+   device->put_kvm(device->kvm);
+   device->put_kvm = NULL;
+   symbol_put(kvm_put_kvm);
+
+clear:
+   device->kvm = NULL;
+}
+#endif
+
 static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
 {
struct vfio_device_file *df = file->private_data;
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 01/26] vfio: Wrap KVM helpers with CONFIG_KVM instead of CONFIG_HAVE_KVM

2023-09-15 Thread Sean Christopherson
Wrap the helpers for getting references to KVM instances with a check on
CONFIG_KVM being enabled, not on CONFIG_HAVE_KVM being defined.  PPC does
NOT select HAVE_KVM, despite obviously supporting KVM, and guarding code
to get references to KVM based on whether or not the architecture supports
KVM is nonsensical.

Drop the guard around linux/kvm_host.h entirely, conditionally including a
generic headers is completely unnecessary.

Signed-off-by: Sean Christopherson 
---
 drivers/vfio/vfio.h  | 2 +-
 drivers/vfio/vfio_main.c | 4 +---
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
index 307e3f29b527..c26d1ad68105 100644
--- a/drivers/vfio/vfio.h
+++ b/drivers/vfio/vfio.h
@@ -434,7 +434,7 @@ static inline void vfio_virqfd_exit(void)
 }
 #endif
 
-#ifdef CONFIG_HAVE_KVM
+#if IS_ENABLED(CONFIG_KVM)
 void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm);
 void vfio_device_put_kvm(struct vfio_device *device);
 #else
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 40732e8ed4c6..80e39f7a6d8f 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -16,9 +16,7 @@
 #include 
 #include 
 #include 
-#ifdef CONFIG_HAVE_KVM
 #include 
-#endif
 #include 
 #include 
 #include 
@@ -383,7 +381,7 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 }
 EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 
-#ifdef CONFIG_HAVE_KVM
+#if IS_ENABLED(CONFIG_KVM)
 void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm)
 {
void (*pfn)(struct kvm *kvm);
-- 
2.42.0.459.ge4e396fd5e-goog



[PATCH 00/26] KVM: vfio: Hide KVM internals from others

2023-09-15 Thread Sean Christopherson
This is a borderline RFC series to hide KVM's internals from the rest of
the kernel, where "internals" means data structures, enums, #defines,
APIs, etc. that are intended to be KVM-only, but are exposed everywhere
due to kvm_host.h (and other headers) living in the global include paths.

The motiviation for hiding KVM's internals is to allow *safely* loading a
"new" KVM module without having to reboot the host.  Where "new" doesn't
have to be strictly newer, just a different incarnation of KVM.  Hiding
KVM's internals means those assets can change across KVM instances without
breaking things, e.g. would allow modifying the layout of struct kvm_vcpu
to introduce new fields related to a new feature or mitigation for hardware
bugs.

The end goal for all of this is to allow loading and running multiple
instances of KVM (the module) simultaneously on a single host, e.g. to
deploy fixes, mitigations, and/or new features without having to drain
all VMs from the host.

For now, the immediate goal is to get KVM to a state where KVM x86 doesn't
expose anything to the broader world that isn't intended for external
consumption, e.g. the page write-tracking APIs used by KVM-GT.

I say this is borderline RFC because I don't think I've "formally" proposed
the idea of hiding KVM internals before now.  I decided not to tag this RFC
because the changes ended up being not _that_ invasive, and everything
before the last six patches is worthwhile even if hiding internals is
ultimately rejected (IMO).

This would ideally be ~5 separate series, and I certainly have no objection
if that's how we want to get this stuff merged.  E.g. (1) VFIO cleanups,
(2) drop HAVE_KVM, (3) clean up makefiles, (4) x86 perf cleanup, and
(5) final push for hiding state.  The HAVE_KVM and virt/kvm include stuff
isn't strictly necessary, but I included them here because they're
relatively minor (in the grand scheme).

We've explore several alternatives to the #ifdef __KVM__ approach, and
they all sucked, hard.  What I really wanted (and still want) to do, is to
bury the bulk of kvm_host.h (and other KVM headers) in virt/kvm, but every
attempt to do that ended in flames.  Even with the __KVM__ guards in place,
each architecture's kvm_host.h is too intertwined with the common kvm_host.h,
and trying to extract small-ish pieces just doesn't work (each patch
inevitably snowballed into a gigantic beast).

The other idea we considered (which I thought of, and feel dirty for even
proposing it internally), is to move all headers under virt/kvm, add
virt/kvm/include to the global header path, and then have KVM x86 omit
virt/kvm/include when configured to hide KVM internals.  I hate this idea
because it sets a bad precedent, and requires a lot of file movement
without providing any benefit to other architectures.  E.g. I hope that
guarding KVM internals with #ifdef __KVM__ will allow us to slowly clean
things up so that some day KVM only exposes a handful of APIs to the rest
of the kernel (probably a pipe dream).

Anish Ghulati (1):
  KVM: arm64: Move arm_{psci,hypercalls}.h to an internal KVM path

Sean Christopherson (25):
  vfio: Wrap KVM helpers with CONFIG_KVM instead of CONFIG_HAVE_KVM
  vfio: Move KVM get/put helpers to colocate it with other KVM related
code
  virt: Declare and define vfio_file_set_kvm() iff CONFIG_KVM is enabled
  vfio: Add struct to hold KVM assets and dedup group vs. iommufd code
  vfio: KVM: Pass get/put helpers from KVM to VFIO, don't do circular
lookup
  KVM: Drop CONFIG_KVM_VFIO and just look at KVM+VFIO
  x86/idt: Wrap KVM logic with CONFIG_KVM instead of CONFIG_HAVE_KVM
  KVM: x86: Stop selecting and depending on HAVE_KVM
  KVM: arm64: Stop selecting and depending on HAVE_KVM
  KVM: s390: Stop selecting and depending on HAVE_KVM
  KVM: MIPS: Make HAVE_KVM a MIPS-only Kconfig
  KVM: arm64: Include KVM headers to get forward declarations
  KVM: arm64: Move ARM specific headers in include/kvm to arch directory
  KVM: Move include/kvm/iodev.h to include/linux as kvm_iodev.h
  KVM: MIPS: Stop adding virt/kvm to the arch include path
  KVM: PPC: Stop adding virt/kvm to the arch include path
  KVM: s390: Stop adding virt/kvm to the arch include path
  KVM: Standardize include paths across all architectures
  perf/x86: KVM: Have perf define a dedicated struct for getting guest
PEBS data
  entry/kvm: Drop @vcpu param from arch_xfer_to_guest_mode_handle_work()
  entry/kvm: KVM: Move KVM details related to signal/-EINTR into KVM
proper
  KVM: arm64: Move and consolidate "public" functions in asm/kvm_host.h
  powerpc/xics: Move declaration of xics_wake_cpu() out of kvm_ppc.h
  KVM: PPC: Rearrange code in kvm_ppc.h to isolate "public" information
  KVM: Hide KVM internal data structures and values from kernel at-large

 MAINTAINERS   |   1 -
 arch/arm64/Kconfig|   1 -
 .../arm64/include/asm/kvm_arch_timer.h|   2 +
 arch/arm64/include/asm/kvm_emulate.h   

Re: [PATCH][next] net: spider_net: Use size_add() in call to struct_size()

2023-09-15 Thread Geoff Levand
On 9/15/23 14:25, Gustavo A. R. Silva wrote:
> If, for any reason, the open-coded arithmetic causes a wraparound,
> the protection that `struct_size()` adds against potential integer
> overflows is defeated. Fix this by hardening call to `struct_size()`
> with `size_add()`.
> 
> Fixes: 3f1071ec39f7 ("net: spider_net: Use struct_size() helper")
> Signed-off-by: Gustavo A. R. Silva 
> ---
>  drivers/net/ethernet/toshiba/spider_net.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/toshiba/spider_net.c 
> b/drivers/net/ethernet/toshiba/spider_net.c
> index 50d7eacfec58..87e67121477c 100644
> --- a/drivers/net/ethernet/toshiba/spider_net.c
> +++ b/drivers/net/ethernet/toshiba/spider_net.c
> @@ -2332,7 +2332,7 @@ spider_net_alloc_card(void)
>   struct spider_net_card *card;
>  
>   netdev = alloc_etherdev(struct_size(card, darray,
> - tx_descriptors + rx_descriptors));
> + size_add(tx_descriptors, 
> rx_descriptors)));
>   if (!netdev)
>   return NULL;
>  

Looks good to me.  Thanks for your fix-up.

Signed-off-by: Geoff Levand 




Re: [RFC PATCH v12 01/33] KVM: Tweak kvm_hva_range and hva_handler_t to allow reusing for gfn ranges

2023-09-15 Thread Sean Christopherson
On Fri, Sep 15, 2023, Xiaoyao Li wrote:
> On 9/14/2023 9:54 AM, Sean Christopherson wrote:
> > Rework and rename "struct kvm_hva_range" into "kvm_mmu_notifier_range" so
> > that the structure can be used to handle notifications that operate on gfn
> > context, i.e. that aren't tied to a host virtual address.
> > 
> > Practically speaking, this is a nop for 64-bit kernels as the only
> > meaningful change is to store start+end as u64s instead of unsigned longs.
> > 
> > Reviewed-by: Paolo Bonzini 
> > Signed-off-by: Sean Christopherson 
> > ---
> >   virt/kvm/kvm_main.c | 34 +++---
> >   1 file changed, 19 insertions(+), 15 deletions(-)
> > 
> > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > index 486800a7024b..0524933856d4 100644
> > --- a/virt/kvm/kvm_main.c
> > +++ b/virt/kvm/kvm_main.c
> > @@ -541,18 +541,22 @@ static inline struct kvm *mmu_notifier_to_kvm(struct 
> > mmu_notifier *mn)
> > return container_of(mn, struct kvm, mmu_notifier);
> >   }
> > -typedef bool (*hva_handler_t)(struct kvm *kvm, struct kvm_gfn_range 
> > *range);
> > +typedef bool (*gfn_handler_t)(struct kvm *kvm, struct kvm_gfn_range 
> > *range);
> 
> Is it worth mentioning the rename of it as well in changelog?

Meh, I suppose.  At some point, we do have to assume a certain level of code
literacy though :-)


Re: [PATCH][next] net: spider_net: Use size_add() in call to struct_size()

2023-09-15 Thread Kees Cook
On Fri, Sep 15, 2023 at 01:25:36PM -0600, Gustavo A. R. Silva wrote:
> If, for any reason, the open-coded arithmetic causes a wraparound,
> the protection that `struct_size()` adds against potential integer
> overflows is defeated. Fix this by hardening call to `struct_size()`
> with `size_add()`.
> 
> Fixes: 3f1071ec39f7 ("net: spider_net: Use struct_size() helper")
> Signed-off-by: Gustavo A. R. Silva 

Reviewed-by: Kees Cook 

-- 
Kees Cook


[PATCH][next] net: spider_net: Use size_add() in call to struct_size()

2023-09-15 Thread Gustavo A. R. Silva
If, for any reason, the open-coded arithmetic causes a wraparound,
the protection that `struct_size()` adds against potential integer
overflows is defeated. Fix this by hardening call to `struct_size()`
with `size_add()`.

Fixes: 3f1071ec39f7 ("net: spider_net: Use struct_size() helper")
Signed-off-by: Gustavo A. R. Silva 
---
 drivers/net/ethernet/toshiba/spider_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/toshiba/spider_net.c 
b/drivers/net/ethernet/toshiba/spider_net.c
index 50d7eacfec58..87e67121477c 100644
--- a/drivers/net/ethernet/toshiba/spider_net.c
+++ b/drivers/net/ethernet/toshiba/spider_net.c
@@ -2332,7 +2332,7 @@ spider_net_alloc_card(void)
struct spider_net_card *card;
 
netdev = alloc_etherdev(struct_size(card, darray,
-   tx_descriptors + rx_descriptors));
+   size_add(tx_descriptors, 
rx_descriptors)));
if (!netdev)
return NULL;
 
-- 
2.34.1



[PATCH 3/3] PCI: layerscape: add suspend/resume for ls1043a

2023-09-15 Thread Frank Li
ls1043a add suspend/resume support.

Signed-off-by: Frank Li 
---
 drivers/pci/controller/dwc/pci-layerscape.c | 91 -
 1 file changed, 90 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/controller/dwc/pci-layerscape.c 
b/drivers/pci/controller/dwc/pci-layerscape.c
index bc5a8ff1a26ce..debabb9bb41f4 100644
--- a/drivers/pci/controller/dwc/pci-layerscape.c
+++ b/drivers/pci/controller/dwc/pci-layerscape.c
@@ -41,10 +41,20 @@
 #define SCFG_PEXSFTRSTCR   0x190
 #define PEXSR(idx) BIT(idx)
 
+/* LS1043A PEX PME control register */
+#define SCFG_PEXPMECR  0x144
+#define PEXPME(idx)BIT(31 - (idx) * 4)
+
+/* LS1043A PEX LUT debug register */
+#define LS_PCIE_LDBG   0x7fc
+#define LDBG_SRBIT(30)
+#define LDBG_WEBIT(31)
+
 #define PCIE_IATU_NUM  6
 
 struct ls_pcie_drvdata {
const u32 pf_off;
+   const u32 lut_off;
const struct dw_pcie_host_ops *ops;
void (*exit_from_l2)(struct dw_pcie_rp *pp);
bool pm_support;
@@ -54,6 +64,7 @@ struct ls_pcie {
struct dw_pcie *pci;
const struct ls_pcie_drvdata *drvdata;
void __iomem *pf_base;
+   void __iomem *lut_base;
struct regmap *scfg;
int index;
bool big_endian;
@@ -116,6 +127,23 @@ static void ls_pcie_pf_writel(struct ls_pcie *pcie, u32 
off, u32 val)
iowrite32(val, pcie->pf_base + off);
 }
 
+static u32 ls_pcie_lut_readl(struct ls_pcie *pcie, u32 off)
+{
+   if (pcie->big_endian)
+   return ioread32be(pcie->lut_base + off);
+
+   return ioread32(pcie->lut_base + off);
+}
+
+static void ls_pcie_lut_writel(struct ls_pcie *pcie, u32 off, u32 val)
+{
+   if (pcie->big_endian)
+   iowrite32be(val, pcie->lut_base + off);
+   else
+   iowrite32(val, pcie->lut_base + off);
+}
+
+
 static void ls_pcie_send_turnoff_msg(struct dw_pcie_rp *pp)
 {
struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
@@ -249,6 +277,54 @@ static int ls1021a_pcie_host_init(struct dw_pcie_rp *pp)
return ret;
 }
 
+static void ls1043a_pcie_send_turnoff_msg(struct dw_pcie_rp *pp)
+{
+   struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+   struct ls_pcie *pcie = to_ls_pcie(pci);
+   u32 val;
+
+   if (!pcie->scfg) {
+   dev_dbg(pcie->pci->dev, "SYSCFG is NULL\n");
+   return;
+   }
+
+   /* Send Turn_off message */
+   regmap_read(pcie->scfg, SCFG_PEXPMECR, );
+   val |= PEXPME(pcie->index);
+   regmap_write(pcie->scfg, SCFG_PEXPMECR, val);
+
+   /* There are not register to check ACK, so wait 
PCIE_PME_TO_L2_TIMEOUT_US */
+   mdelay(PCIE_PME_TO_L2_TIMEOUT_US/1000);
+
+   /* Clear Turn_off message */
+   regmap_read(pcie->scfg, SCFG_PEXPMECR, );
+   val &= ~PEXPME(pcie->index);
+   regmap_write(pcie->scfg, SCFG_PEXPMECR, val);
+}
+
+static void ls1043a_pcie_exit_from_l2(struct dw_pcie_rp *pp)
+{
+   struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+   struct ls_pcie *pcie = to_ls_pcie(pci);
+   u32 val;
+
+   val = ls_pcie_lut_readl(pcie, LS_PCIE_LDBG);
+   val |= LDBG_WE;
+   ls_pcie_lut_writel(pcie, LS_PCIE_LDBG, val);
+
+   val = ls_pcie_lut_readl(pcie, LS_PCIE_LDBG);
+   val |= LDBG_SR;
+   ls_pcie_lut_writel(pcie, LS_PCIE_LDBG, val);
+
+   val = ls_pcie_lut_readl(pcie, LS_PCIE_LDBG);
+   val &= ~LDBG_SR;
+   ls_pcie_lut_writel(pcie, LS_PCIE_LDBG, val);
+
+   val = ls_pcie_lut_readl(pcie, LS_PCIE_LDBG);
+   val &= ~LDBG_WE;
+   ls_pcie_lut_writel(pcie, LS_PCIE_LDBG, val);
+}
+
 static const struct dw_pcie_host_ops ls_pcie_host_ops = {
.host_init = ls_pcie_host_init,
.pme_turn_off = ls_pcie_send_turnoff_msg,
@@ -265,6 +341,18 @@ static const struct ls_pcie_drvdata ls1021a_drvdata = {
.exit_from_l2 = ls1021a_pcie_exit_from_l2,
 };
 
+static const struct dw_pcie_host_ops ls1043a_pcie_host_ops = {
+   .host_init = ls1021a_pcie_host_init, /* the same as ls1021 */
+   .pme_turn_off = ls1043a_pcie_send_turnoff_msg,
+};
+
+static const struct ls_pcie_drvdata ls1043a_drvdata = {
+   .lut_off = 0x1,
+   .pm_support = true,
+   .ops = _pcie_host_ops,
+   .exit_from_l2 = ls1043a_pcie_exit_from_l2,
+};
+
 static const struct ls_pcie_drvdata layerscape_drvdata = {
.pf_off = 0xc,
.pm_support = true,
@@ -275,7 +363,7 @@ static const struct of_device_id ls_pcie_of_match[] = {
{ .compatible = "fsl,ls1012a-pcie", .data = _drvdata },
{ .compatible = "fsl,ls1021a-pcie", .data = _drvdata },
{ .compatible = "fsl,ls1028a-pcie", .data = _drvdata },
-   { .compatible = "fsl,ls1043a-pcie", .data = _drvdata },
+   { .compatible = "fsl,ls1043a-pcie", .data = _drvdata },
{ .compatible = "fsl,ls1046a-pcie", .data = _drvdata },
{ .compatible = "fsl,ls2080a-pcie", .data = _drvdata },
{ .compatible = 

[PATCH 2/3] PCI: layerscape: add suspend/resume for ls1021a

2023-09-15 Thread Frank Li
ls1021a add suspend/resume support.

Signed-off-by: Frank Li 
---
 drivers/pci/controller/dwc/pci-layerscape.c | 88 -
 1 file changed, 87 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/controller/dwc/pci-layerscape.c 
b/drivers/pci/controller/dwc/pci-layerscape.c
index 20c48c06e2248..bc5a8ff1a26ce 100644
--- a/drivers/pci/controller/dwc/pci-layerscape.c
+++ b/drivers/pci/controller/dwc/pci-layerscape.c
@@ -35,6 +35,12 @@
 #define PF_MCR_PTOMR   BIT(0)
 #define PF_MCR_EXL2S   BIT(1)
 
+/* LS1021A PEXn PM Write Control Register */
+#define SCFG_PEXPMWRCR(idx)(0x5c + (idx) * 0x64)
+#define PMXMTTURNOFF   BIT(31)
+#define SCFG_PEXSFTRSTCR   0x190
+#define PEXSR(idx) BIT(idx)
+
 #define PCIE_IATU_NUM  6
 
 struct ls_pcie_drvdata {
@@ -48,6 +54,8 @@ struct ls_pcie {
struct dw_pcie *pci;
const struct ls_pcie_drvdata *drvdata;
void __iomem *pf_base;
+   struct regmap *scfg;
+   int index;
bool big_endian;
 };
 
@@ -170,13 +178,91 @@ static int ls_pcie_host_init(struct dw_pcie_rp *pp)
return 0;
 }
 
+static void ls1021a_pcie_send_turnoff_msg(struct dw_pcie_rp *pp)
+{
+   struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+   struct ls_pcie *pcie = to_ls_pcie(pci);
+   u32 val;
+
+   if (!pcie->scfg) {
+   dev_dbg(pcie->pci->dev, "SYSCFG is NULL\n");
+   return;
+   }
+
+   /* Send Turn_off message */
+   regmap_read(pcie->scfg, SCFG_PEXPMWRCR(pcie->index), );
+   val |= PMXMTTURNOFF;
+   regmap_write(pcie->scfg, SCFG_PEXPMWRCR(pcie->index), val);
+
+   /* There are not register to check ACK, so wait 
PCIE_PME_TO_L2_TIMEOUT_US */
+   mdelay(PCIE_PME_TO_L2_TIMEOUT_US/1000);
+
+   /* Clear Turn_off message */
+   regmap_read(pcie->scfg, SCFG_PEXPMWRCR(pcie->index), );
+   val &= ~PMXMTTURNOFF;
+   regmap_write(pcie->scfg, SCFG_PEXPMWRCR(pcie->index), val);
+}
+
+static void ls1021a_pcie_exit_from_l2(struct dw_pcie_rp *pp)
+{
+   struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+   struct ls_pcie *pcie = to_ls_pcie(pci);
+   u32 val;
+
+   regmap_read(pcie->scfg, SCFG_PEXSFTRSTCR, );
+   val |= PEXSR(pcie->index);
+   regmap_write(pcie->scfg, SCFG_PEXSFTRSTCR, val);
+
+   regmap_read(pcie->scfg, SCFG_PEXSFTRSTCR, );
+   val &= ~PEXSR(pcie->index);
+   regmap_write(pcie->scfg, SCFG_PEXSFTRSTCR, val);
+}
+
+static int ls1021a_pcie_host_init(struct dw_pcie_rp *pp)
+{
+   struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
+   struct ls_pcie *pcie = to_ls_pcie(pci);
+   struct device *dev = pcie->pci->dev;
+   u32 index[2];
+   int ret;
+
+   ret = ls_pcie_host_init(pp);
+   if (ret)
+   return ret;
+
+   pcie->scfg = syscon_regmap_lookup_by_phandle(dev->of_node, 
"fsl,pcie-scfg");
+   if (IS_ERR(pcie->scfg)) {
+   ret = PTR_ERR(pcie->scfg);
+   dev_err(dev, "No syscfg phandle specified\n");
+   pcie->scfg = NULL;
+   return ret;
+   }
+
+   ret = of_property_read_u32_array(dev->of_node, "fsl,pcie-scfg", index, 
2);
+   if (ret) {
+   pcie->scfg = NULL;
+   return ret;
+   }
+
+   pcie->index = index[1];
+
+   return ret;
+}
+
 static const struct dw_pcie_host_ops ls_pcie_host_ops = {
.host_init = ls_pcie_host_init,
.pme_turn_off = ls_pcie_send_turnoff_msg,
 };
 
+static const struct dw_pcie_host_ops ls1021a_pcie_host_ops = {
+   .host_init = ls1021a_pcie_host_init,
+   .pme_turn_off = ls1021a_pcie_send_turnoff_msg,
+};
+
 static const struct ls_pcie_drvdata ls1021a_drvdata = {
-   .pm_support = false,
+   .pm_support = true,
+   .ops = _pcie_host_ops,
+   .exit_from_l2 = ls1021a_pcie_exit_from_l2,
 };
 
 static const struct ls_pcie_drvdata layerscape_drvdata = {
-- 
2.34.1



[PATCH 1/3] PCI: layerscape: add function pointer for exit_from_l2()

2023-09-15 Thread Frank Li
Difference layerscape chip have not difference exit_from_l2() method.
Using function pointer for ls1028. It prepare for other layerscape
suspend/resume support.

Signed-off-by: Frank Li 
---
 drivers/pci/controller/dwc/pci-layerscape.c | 7 +--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/dwc/pci-layerscape.c 
b/drivers/pci/controller/dwc/pci-layerscape.c
index b931d597656f6..20c48c06e2248 100644
--- a/drivers/pci/controller/dwc/pci-layerscape.c
+++ b/drivers/pci/controller/dwc/pci-layerscape.c
@@ -39,6 +39,8 @@
 
 struct ls_pcie_drvdata {
const u32 pf_off;
+   const struct dw_pcie_host_ops *ops;
+   void (*exit_from_l2)(struct dw_pcie_rp *pp);
bool pm_support;
 };
 
@@ -180,6 +182,7 @@ static const struct ls_pcie_drvdata ls1021a_drvdata = {
 static const struct ls_pcie_drvdata layerscape_drvdata = {
.pf_off = 0xc,
.pm_support = true,
+   .exit_from_l2 = ls_pcie_exit_from_l2,
 };
 
 static const struct of_device_id ls_pcie_of_match[] = {
@@ -213,7 +216,7 @@ static int ls_pcie_probe(struct platform_device *pdev)
pcie->drvdata = of_device_get_match_data(dev);
 
pci->dev = dev;
-   pci->pp.ops = _pcie_host_ops;
+   pci->pp.ops = pcie->drvdata->ops ? pcie->drvdata->ops : 
_pcie_host_ops;
 
pcie->pci = pci;
 
@@ -251,7 +254,7 @@ static int ls_pcie_resume_noirq(struct device *dev)
if (!pcie->drvdata->pm_support)
return 0;
 
-   ls_pcie_exit_from_l2(>pci->pp);
+   pcie->drvdata->exit_from_l2(>pci->pp);
 
return dw_pcie_resume_noirq(pcie->pci);
 }
-- 
2.34.1



Re: [PATCH 1/3] core/device: Add function to return child node using name at substring "@"

2023-09-15 Thread Athira Rajeev



> On 15-Sep-2023, at 8:00 PM, Reza Arbab  wrote:
> 
> Hi Athira,
> 
> On Thu, Sep 14, 2023 at 10:02:04PM +0530, Athira Rajeev wrote: 
>> +struct dt_node *dt_find_by_name_before_addr(struct dt_node *root, const 
>> char *name)
>> +{
>> + struct dt_node *child, *match;
>> + char *child_node = NULL;
>> +
>> + list_for_each(>children, child, list) {
>> + child_node = strdup(child->name);
>> + if (!child_node)
>> + goto err;
>> + child_node = strtok(child_node, "@");
>> + if (!strcmp(child_node, name)) {
>> + free(child_node);
>> + return child;
>> + }
>> +
>> + match = dt_find_by_name_before_addr(child, name);
>> + if (match)
>> + return match;
> 
> When the function returns on this line, child_node is not freed.
> 
>> + }
>> +
>> + free(child_node);
>> +err:
>> + return NULL;
>> +}
> 
> I took at stab at moving free(child_node) inside the loop, and ended up with 
> this:
> 
> struct dt_node *dt_find_by_name_before_addr(struct dt_node *root, const char 
> *name)
> {
> struct dt_node *child, *match = NULL;
> char *child_name = NULL;
> 
> list_for_each(>children, child, list) {
> child_name = strdup(child->name);
> if (!child_name)
> return NULL;
> 
> child_name = strtok(child_name, "@");
> if (!strcmp(child_name, name))
> match = child;
> else
> match = dt_find_by_name_before_addr(child, name);
> 
> free(child_name);
> if (match)
> return match;
> }
> 
> return NULL;
> }
> 
> Does this seem okay to you? If you agree, no need to send another revision, I 
> can just fixup during commit. Let me know.

Hi Reza,

Sure, Change looks good. Thanks for the change and fixup.

Thanks
Athira
> 
>> diff --git a/core/test/run-device.c b/core/test/run-device.c
>> index 4a12382bb..fb7a7d2c0 100644
>> --- a/core/test/run-device.c
>> +++ b/core/test/run-device.c
>> @@ -466,6 +466,20 @@ int main(void)
>> new_prop_ph = dt_prop_get_u32(ut2, "something");
>> assert(!(new_prop_ph == ev1_ph));
>> dt_free(subtree);
>> +
>> + /* Test dt_find_by_name_before_addr */
>> + root = dt_new_root("");
>> + addr1 = dt_new_addr(root, "node", 0x1);
>> + addr2 = dt_new_addr(root, "node0_1", 0x2);
>> + assert(dt_find_by_name(root, "node@1") == addr1);
>> + assert(dt_find_by_name(root, "node0_1@2") == addr2);
>> + assert(dt_find_by_name_before_addr(root, "node") == addr1);
>> + assert(dt_find_by_name_before_addr(root, "node0_") == NULL);
> 
> This line appears twice. As above, can fix during commit, so no need for a 
> new patch.
> 
>> + assert(dt_find_by_name_before_addr(root, "node0_1") == addr2);
>> + assert(dt_find_by_name_before_addr(root, "node0") == NULL);
>> + assert(dt_find_by_name_before_addr(root, "node0_") == NULL);
>> + dt_free(root);
>> +
>> return 0;
>> }
>> 
> 
> -- 
> Reza Arbab



Re: [PATCH] ASoC: imx-audmix: Fix return error with devm_clk_get()

2023-09-15 Thread Mark Brown
On Fri, 15 Sep 2023 14:02:11 +0800, Shengjiu Wang wrote:
> The devm_clk_get() can return -EPROBE_DEFER error,
> modify the error code to be -EINVAL is not correct, which
> cause the -EPROBE_DEFER error is not correctly handled.
> 
> This patch is to fix the return error code.
> 
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

Thanks!

[1/1] ASoC: imx-audmix: Fix return error with devm_clk_get()
  commit: b19a5733de255cabba5feecabf6e900638b582d1

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark



Re: [PATCH tty v1 72/74] serial: ucc_uart: Use port lock wrappers

2023-09-15 Thread Timur Tabi
On Thu, Sep 14, 2023 at 1:39 PM John Ogness  wrote:

> Converted with coccinelle. No functional change.
>
> Signed-off-by: Thomas Gleixner 
> ---
>  drivers/tty/serial/ucc_uart.c | 4 ++--

Acked-by: Timur Tabi 


Re: [PATCH 1/3] core/device: Add function to return child node using name at substring "@"

2023-09-15 Thread Reza Arbab

Hi Athira,

On Thu, Sep 14, 2023 at 10:02:04PM +0530, Athira Rajeev wrote: 

+struct dt_node *dt_find_by_name_before_addr(struct dt_node *root, const char 
*name)
+{
+   struct dt_node *child, *match;
+   char *child_node = NULL;
+
+   list_for_each(>children, child, list) {
+   child_node = strdup(child->name);
+   if (!child_node)
+   goto err;
+   child_node = strtok(child_node, "@");
+   if (!strcmp(child_node, name)) {
+   free(child_node);
+   return child;
+   }
+
+   match = dt_find_by_name_before_addr(child, name);
+   if (match)
+   return match;


When the function returns on this line, child_node is not freed.


+   }
+
+   free(child_node);
+err:
+   return NULL;
+}


I took at stab at moving free(child_node) inside the loop, and ended up 
with this:


struct dt_node *dt_find_by_name_before_addr(struct dt_node *root, const char 
*name)
{
struct dt_node *child, *match = NULL;
char *child_name = NULL;

list_for_each(>children, child, list) {
child_name = strdup(child->name);
if (!child_name)
return NULL;

child_name = strtok(child_name, "@");
if (!strcmp(child_name, name))
match = child;
else
match = dt_find_by_name_before_addr(child, name);

free(child_name);
if (match)
return match;
}

return NULL;
}

Does this seem okay to you? If you agree, no need to send another 
revision, I can just fixup during commit. Let me know.



diff --git a/core/test/run-device.c b/core/test/run-device.c
index 4a12382bb..fb7a7d2c0 100644
--- a/core/test/run-device.c
+++ b/core/test/run-device.c
@@ -466,6 +466,20 @@ int main(void)
new_prop_ph = dt_prop_get_u32(ut2, "something");
assert(!(new_prop_ph == ev1_ph));
dt_free(subtree);
+
+   /* Test dt_find_by_name_before_addr */
+   root = dt_new_root("");
+   addr1 = dt_new_addr(root, "node", 0x1);
+   addr2 = dt_new_addr(root, "node0_1", 0x2);
+   assert(dt_find_by_name(root, "node@1") == addr1);
+   assert(dt_find_by_name(root, "node0_1@2") == addr2);
+   assert(dt_find_by_name_before_addr(root, "node") == addr1);
+   assert(dt_find_by_name_before_addr(root, "node0_") == NULL);


This line appears twice. As above, can fix during commit, so no need for 
a new patch.



+   assert(dt_find_by_name_before_addr(root, "node0_1") == addr2);
+   assert(dt_find_by_name_before_addr(root, "node0") == NULL);
+   assert(dt_find_by_name_before_addr(root, "node0_") == NULL);
+   dt_free(root);
+
return 0;
}



--
Reza Arbab


Re: [PATCH v2] crypto: vmx: Improved AES/XTS performance of 6-way unrolling for ppc.

2023-09-15 Thread Danny Tsen

Still waiting for the CCLA to send to Openssl.

Thanks.

-Danny

On 9/15/23 8:29 AM, Michael Ellerman wrote:

Danny Tsen  writes:

Improve AES/XTS performance of 6-way unrolling for PowerPC up
to 17% with tcrypt.  This is done by using one instruction,
vpermxor, to replace xor and vsldoi.

The same changes were applied to OpenSSL code and a pull request was
submitted.

https://github.com/openssl/openssl/pull/21812

Still unmerged as of today.

cheers


[powerpc:topic/ppc-kvm] BUILD SUCCESS 476652297f94a2e5e5ef29e734b0da37ade94110

2023-09-15 Thread kernel test robot
tree/branch: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git 
topic/ppc-kvm
branch HEAD: 476652297f94a2e5e5ef29e734b0da37ade94110  docs: powerpc: Document 
nested KVM on POWER

elapsed time: 727m

configs tested: 141
configs skipped: 2

The following configs have been built successfully.
More configs may be tested in the coming days.

tested configs:
alpha allnoconfig   gcc  
alphaallyesconfig   gcc  
alpha   defconfig   gcc  
arc  allmodconfig   gcc  
arc   allnoconfig   gcc  
arc  allyesconfig   gcc  
arc defconfig   gcc  
arc   randconfig-001-20230915   gcc  
arm  allmodconfig   gcc  
arm   allnoconfig   gcc  
arm  allyesconfig   gcc  
arm defconfig   gcc  
arm  pxa3xx_defconfig   gcc  
arm   randconfig-001-20230915   gcc  
arm64allmodconfig   gcc  
arm64 allnoconfig   gcc  
arm64allyesconfig   gcc  
arm64   defconfig   gcc  
csky allmodconfig   gcc  
csky  allnoconfig   gcc  
csky allyesconfig   gcc  
cskydefconfig   gcc  
i386 allmodconfig   gcc  
i386  allnoconfig   gcc  
i386 allyesconfig   gcc  
i386 buildonly-randconfig-001-20230915   gcc  
i386 buildonly-randconfig-002-20230915   gcc  
i386 buildonly-randconfig-003-20230915   gcc  
i386 buildonly-randconfig-004-20230915   gcc  
i386 buildonly-randconfig-005-20230915   gcc  
i386 buildonly-randconfig-006-20230915   gcc  
i386  debian-10.3   gcc  
i386defconfig   gcc  
i386  randconfig-001-20230915   gcc  
i386  randconfig-002-20230915   gcc  
i386  randconfig-003-20230915   gcc  
i386  randconfig-004-20230915   gcc  
i386  randconfig-005-20230915   gcc  
i386  randconfig-006-20230915   gcc  
i386  randconfig-011-20230915   gcc  
i386  randconfig-012-20230915   gcc  
i386  randconfig-013-20230915   gcc  
i386  randconfig-014-20230915   gcc  
i386  randconfig-015-20230915   gcc  
i386  randconfig-016-20230915   gcc  
loongarchallmodconfig   gcc  
loongarch allnoconfig   gcc  
loongarchallyesconfig   gcc  
loongarch   defconfig   gcc  
loongarch randconfig-001-20230915   gcc  
m68k allmodconfig   gcc  
m68k  allnoconfig   gcc  
m68k allyesconfig   gcc  
m68k   bvme6000_defconfig   gcc  
m68kdefconfig   gcc  
microblaze   allmodconfig   gcc  
microblazeallnoconfig   gcc  
microblaze   allyesconfig   gcc  
microblaze  defconfig   gcc  
mips allmodconfig   gcc  
mips  allnoconfig   gcc  
mips allyesconfig   gcc  
mips  loongson3_defconfig   gcc  
mips   xway_defconfig   gcc  
nios2allmodconfig   gcc  
nios2 allnoconfig   gcc  
nios2allyesconfig   gcc  
nios2   defconfig   gcc  
openrisc allmodconfig   gcc  
openrisc  allnoconfig   gcc  
openrisc allyesconfig   gcc  
openriscdefconfig   gcc  
parisc   allmodconfig   gcc  
pariscallnoconfig   gcc  
parisc   allyesconfig   gcc  
parisc  defconfig   gcc  
parisc64defconfig   gcc  
powerpc  allmodconfig   gcc  
powerpc   allnoconfig   gcc  
powerpc  allyesconfig   gcc  
riscvallmodconfig   gcc  
riscv allnoconfig   gcc  
riscvallyesconfig   gcc  
riscv   defconfig   gcc  
riscv randconfig-001-20230915   gcc  
riscv

Re: [RFC PATCH v12 18/33] KVM: x86/mmu: Handle page fault for private memory

2023-09-15 Thread Sean Christopherson
On Fri, Sep 15, 2023, Yan Zhao wrote:
> On Wed, Sep 13, 2023 at 06:55:16PM -0700, Sean Christopherson wrote:
> 
> > +static void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
> > + struct kvm_page_fault *fault)
> > +{
> > +   kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT,
> > + PAGE_SIZE, fault->write, fault->exec,
> > + fault->is_private);
> > +}
> > +
> > +static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu,
> > +  struct kvm_page_fault *fault)
> > +{
> > +   int max_order, r;
> > +
> > +   if (!kvm_slot_can_be_private(fault->slot)) {
> > +   kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
> > +   return -EFAULT;
> > +   }
> > +
> > +   r = kvm_gmem_get_pfn(vcpu->kvm, fault->slot, fault->gfn, >pfn,
> > +_order);
> > +   if (r) {
> > +   kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
> > +   return r;
> > +   }
> > +
> > +   fault->max_level = min(kvm_max_level_for_order(max_order),
> > +  fault->max_level);
> > +   fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY);
> > +
> > +   return RET_PF_CONTINUE;
> > +}
> > +
> >  static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault 
> > *fault)
> >  {
> > struct kvm_memory_slot *slot = fault->slot;
> > @@ -4293,6 +4356,14 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, 
> > struct kvm_page_fault *fault
> > return RET_PF_EMULATE;
> > }
> >  
> > +   if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) {
> In patch 21,
> fault->is_private is set as:
>   ".is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT)",
> then, the inequality here means memory attribute has been updated after
> last check.
> So, why an exit to user space for converting is required instead of a mere 
> retry?
> 
> Or, is it because how .is_private is assigned in patch 21 is subjected to 
> change
> in future? 

This.  Retrying on SNP or TDX would hang the guest.  I suppose we could special
case VMs where .is_private is derived from the memory attributes, but the
SW_PROTECTED_VM type is primary a development vehicle at this point.  I'd like 
to
have it mimic SNP/TDX as much as possible; performance is a secondary concern.

E.g. userspace needs to be prepared for "spurious" exits due to races on SNP and
TDX, which this can theoretically exercise.  Though the window is quite small so
I doubt that'll actually happen in practice; which of course also makes it less
important to retry instead of exiting.


Re: [PATCH v2] crypto: vmx: Improved AES/XTS performance of 6-way unrolling for ppc.

2023-09-15 Thread Michael Ellerman
Danny Tsen  writes:
> Improve AES/XTS performance of 6-way unrolling for PowerPC up
> to 17% with tcrypt.  This is done by using one instruction,
> vpermxor, to replace xor and vsldoi.
>
> The same changes were applied to OpenSSL code and a pull request was
> submitted.

https://github.com/openssl/openssl/pull/21812

Still unmerged as of today.

cheers


Re: [PATCH tty v1 00/74] serial: wrappers for uart port lock

2023-09-15 Thread Thomas Gleixner
On Thu, Sep 14 2023 at 20:01, Maciej W. Rozycki wrote:

> On Thu, 14 Sep 2023, John Ogness wrote:
>
>> Patches 2-74 switch all uart port locking call sites to use the new
>> wrappers. These patches were automatically generated using coccinelle.
>
>  Hmm, no need to do this for drivers/tty/serial/zs.c?

zs.c does not use port lock at all. It has like a couple of other
drivers a local homebrewn spinlock.

Thanks,

tglx


Re: [PATCH v2] crypto: vmx: Improved AES/XTS performance of 6-way unrolling for ppc.

2023-09-15 Thread Danny Tsen

Thanks Herbert.

-Danny

On 9/15/23 5:41 AM, Herbert Xu wrote:

On Wed, Aug 30, 2023 at 09:49:11AM -0400, Danny Tsen wrote:

Improve AES/XTS performance of 6-way unrolling for PowerPC up
to 17% with tcrypt.  This is done by using one instruction,
vpermxor, to replace xor and vsldoi.

The same changes were applied to OpenSSL code and a pull request was
submitted.

This patch has been tested with the kernel crypto module tcrypt.ko and
has passed the selftest.  The patch is also tested with
CONFIG_CRYPTO_MANAGER_EXTRA_TESTS enabled.

Signed-off-by: Danny Tsen 
---
  drivers/crypto/vmx/aesp8-ppc.pl | 141 +---
  1 file changed, 92 insertions(+), 49 deletions(-)

Patch applied.  Thanks.


Re: [PATCH v2] crypto: vmx: Improved AES/XTS performance of 6-way unrolling for ppc.

2023-09-15 Thread Herbert Xu
On Wed, Aug 30, 2023 at 09:49:11AM -0400, Danny Tsen wrote:
> Improve AES/XTS performance of 6-way unrolling for PowerPC up
> to 17% with tcrypt.  This is done by using one instruction,
> vpermxor, to replace xor and vsldoi.
> 
> The same changes were applied to OpenSSL code and a pull request was
> submitted.
> 
> This patch has been tested with the kernel crypto module tcrypt.ko and
> has passed the selftest.  The patch is also tested with
> CONFIG_CRYPTO_MANAGER_EXTRA_TESTS enabled.
> 
> Signed-off-by: Danny Tsen 
> ---
>  drivers/crypto/vmx/aesp8-ppc.pl | 141 +---
>  1 file changed, 92 insertions(+), 49 deletions(-)

Patch applied.  Thanks.
-- 
Email: Herbert Xu 
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt


Re: [PATCH tty v1 00/74] serial: wrappers for uart port lock

2023-09-15 Thread Ilpo Järvinen
On Thu, 14 Sep 2023, John Ogness wrote:

> When a serial port is used for kernel console output, then all
> modifications to the UART registers which are done from other contexts,
> e.g. getty, termios, are interference points for the kernel console.
> 
> So far this has been ignored and the printk output is based on the
> principle of hope. The rework of the console infrastructure which aims to
> support threaded and atomic consoles, requires to mark sections which
> modify the UART registers as unsafe. This allows the atomic write function
> to make informed decisions and eventually to restore operational state. It
> also allows to prevent the regular UART code from modifying UART registers
> while printk output is in progress.

Hi John,

Would this also be useful to enable printing to console while under port's 
lock (by postponing the output until the lock is released)?

E.g., 8250_dw.c has had this commented out since the dawn on time:
/*
 * FIXME: this deadlocks if port->lock is already held
 * dev_err(p->dev, "Couldn't set LCR to %d\n", value);
 */

-- 
 i.


> All modifications of UART registers are guarded by the UART port lock,
> which provides an obvious synchronization point with the console
> infrastructure.
> 
> Provide and use wrapper functions for spin_[un]lock*(port->lock)
> invocations so that the console mechanics can be applied later on at a
> single place and does not require to copy the same logic all over the
> drivers.
> 
> Patch 1 adds the wrapper functions.
> 
> Patches 2-74 switch all uart port locking call sites to use the new
> wrappers. These patches were automatically generated using coccinelle.
> The 2 used coccinelle scripts are included below and executed as
> follows:
> 
> $ spatch --sp-file uartlock-1.cocci $FILE
> $ spatch --sp-file uartlock-2.cocci --recursive-includes $FILE
> 
> This series brings no functional change.
> 
> Patches 2-74 contain identical commit message bodies. Feel free to
> fold them into a single commit if that seems more reasonable.
> 
> Thomas Gleixner (74):
>   serial: core: Provide port lock wrappers
>   serial: core: Use lock wrappers
>   serial: 21285: Use port lock wrappers
>   serial: 8250_aspeed_vuart: Use port lock wrappers
>   serial: 8250_bcm7271: Use port lock wrappers
>   serial: 8250: Use port lock wrappers
>   serial: 8250_dma: Use port lock wrappers
>   serial: 8250_dw: Use port lock wrappers
>   serial: 8250_exar: Use port lock wrappers
>   serial: 8250_fsl: Use port lock wrappers
>   serial: 8250_mtk: Use port lock wrappers
>   serial: 8250_omap: Use port lock wrappers
>   serial: 8250_pci1: Use port lock wrappers
>   serial: altera_jtaguart: Use port lock wrappers
>   serial: altera_uart: Use port lock wrappers
>   serial: amba-pl010: Use port lock wrappers
>   serial: amba-pl011: Use port lock wrappers
>   serial: apb: Use port lock wrappers
>   serial: ar933x: Use port lock wrappers
>   serial: arc_uart: Use port lock wrappers
>   serial: atmel: Use port lock wrappers
>   serial: bcm63xx-uart: Use port lock wrappers
>   serial: cpm_uart: Use port lock wrappers
>   serial: digicolor: Use port lock wrappers
>   serial: dz: Use port lock wrappers
>   serial: linflexuart: Use port lock wrappers
>   serial: fsl_lpuart: Use port lock wrappers
>   serial: icom: Use port lock wrappers
>   serial: imx: Use port lock wrappers
>   serial: ip22zilog: Use port lock wrappers
>   serial: jsm: Use port lock wrappers
>   serial: liteuart: Use port lock wrappers
>   serial: lpc32xx_hs: Use port lock wrappers
>   serial: ma35d1: Use port lock wrappers
>   serial: mcf: Use port lock wrappers
>   serial: men_z135_uart: Use port lock wrappers
>   serial: meson: Use port lock wrappers
>   serial: milbeaut_usio: Use port lock wrappers
>   serial: mpc52xx: Use port lock wrappers
>   serial: mps2-uart: Use port lock wrappers
>   serial: msm: Use port lock wrappers
>   serial: mvebu-uart: Use port lock wrappers
>   serial: omap: Use port lock wrappers
>   serial: owl: Use port lock wrappers
>   serial: pch: Use port lock wrappers
>   serial: pic32: Use port lock wrappers
>   serial: pmac_zilog: Use port lock wrappers
>   serial: pxa: Use port lock wrappers
>   serial: qcom-geni: Use port lock wrappers
>   serial: rda: Use port lock wrappers
>   serial: rp2: Use port lock wrappers
>   serial: sa1100: Use port lock wrappers
>   serial: samsung_tty: Use port lock wrappers
>   serial: sb1250-duart: Use port lock wrappers
>   serial: sc16is7xx: Use port lock wrappers
>   serial: tegra: Use port lock wrappers
>   serial: core: Use port lock wrappers
>   serial: mctrl_gpio: Use port lock wrappers
>   serial: txx9: Use port lock wrappers
>   serial: sh-sci: Use port lock wrappers
>   serial: sifive: Use port lock wrappers
>   serial: sprd: Use port lock wrappers
>   serial: st-asc: Use port lock wrappers
>   serial: stm32: Use port lock wrappers
>   serial: sunhv: Use port lock wrappers
>   serial: sunplus-uart: Use port lock 

Re: [PATCH v2] arch: Reserve map_shadow_stack() syscall number for all architectures

2023-09-15 Thread Michael Ellerman
Sohil Mehta  writes:
> commit c35559f94ebc ("x86/shstk: Introduce map_shadow_stack syscall")
> recently added support for map_shadow_stack() but it is limited to x86
> only for now. There is a possibility that other architectures (namely,
> arm64 and RISC-V), that are implementing equivalent support for shadow
> stacks, might need to add support for it.
>
> Independent of that, reserving arch-specific syscall numbers in the
> syscall tables of all architectures is good practice and would help
> avoid future conflicts. map_shadow_stack() is marked as a conditional
> syscall in sys_ni.c. Adding it to the syscall tables of other
> architectures is harmless and would return ENOSYS when exercised.
>
> Note, map_shadow_stack() was assigned #453 during the merge process
> since #452 was taken by fchmodat2().
>
> For Powerpc, map it to sys_ni_syscall() as is the norm for Powerpc
> syscall tables.

Mapping it to sys_map_shadow_stack() would work fine, but I'm happy with
sys_ni_syscall as I don't see powerpc implementing map_shadow_stack()
any time soon.

Acked-by: Michael Ellerman  (powerpc)

cheers


Re: [PATCH] ASoC: imx-audmix: Fix return error with devm_clk_get()

2023-09-15 Thread Daniel Baluta
On Fri, Sep 15, 2023 at 9:38 AM Shengjiu Wang  wrote:
>
> The devm_clk_get() can return -EPROBE_DEFER error,
> modify the error code to be -EINVAL is not correct, which
> cause the -EPROBE_DEFER error is not correctly handled.
>
> This patch is to fix the return error code.
>
> Fixes: b86ef5367761 ("ASoC: fsl: Add Audio Mixer machine driver")
> Signed-off-by: Shengjiu Wang 

Reviewed-by: Daniel Baluta 


Re: [PATCH] powerpc/82xx: Select FSL_SOC

2023-09-15 Thread Michael Ellerman
Christophe Leroy  writes:
> Le 15/09/2023 à 02:43, Michael Ellerman a écrit :
>> Christophe Leroy  writes:
>>> It used to be impossible to select CONFIG_CPM2 without selecting
>>> CONFIG_FSL_SOC at the same time because CONFIG_CPM2 was dependent
>>> on CONFIG_8260 and CONFIG_8260 was selecting CONFIG_FSL_SOC.
>>>
>>> But after commit eb5aa2137275 ("powerpc/82xx: Remove CONFIG_8260
>>> and CONFIG_8272") CONFIG_CPM2 depends on CONFIG_MPC82xx instead
>> ^
>> CONFIG_PPC_82xx
>> 
>> All the references to CONFIG_MPC82xx should be CONFIG_PPC_82xx right?
>> I can update when applying.
>
> Ah right, I mixed things up. This is CONFIG_PPC_82xx, CONFIG_PPC_8xx, 
> CONFIG_PPC_83xx and CONFIG_PPC_MPC512x

Thanks.

cheers


Re: [RFC PATCH v12 11/33] KVM: Introduce per-page memory attributes

2023-09-15 Thread Yan Zhao
On Wed, Sep 13, 2023 at 06:55:09PM -0700, Sean Christopherson wrote:
> From: Chao Peng 
> 
> In confidential computing usages, whether a page is private or shared is
> necessary information for KVM to perform operations like page fault
> handling, page zapping etc. There are other potential use cases for
> per-page memory attributes, e.g. to make memory read-only (or no-exec,
> or exec-only, etc.) without having to modify memslots.
> 
...
>> +bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t 
>> end,
> +  unsigned long attrs)
> +{
> + XA_STATE(xas, >mem_attr_array, start);
> + unsigned long index;
> + bool has_attrs;
> + void *entry;
> +
> + rcu_read_lock();
> +
> + if (!attrs) {
> + has_attrs = !xas_find(, end);
> + goto out;
> + }
> +
> + has_attrs = true;
> + for (index = start; index < end; index++) {
> + do {
> + entry = xas_next();
> + } while (xas_retry(, entry));
> +
> + if (xas.xa_index != index || xa_to_value(entry) != attrs) {
Should "xa_to_value(entry) != attrs" be "!(xa_to_value(entry) & attrs)" ?

> + has_attrs = false;
> + break;
> + }
> + }
> +
> +out:
> + rcu_read_unlock();
> + return has_attrs;
> +}
> +
...
> +/* Set @attributes for the gfn range [@start, @end). */
> +static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
> +  unsigned long attributes)
> +{
> + struct kvm_mmu_notifier_range pre_set_range = {
> + .start = start,
> + .end = end,
> + .handler = kvm_arch_pre_set_memory_attributes,
> + .on_lock = kvm_mmu_invalidate_begin,
> + .flush_on_ret = true,
> + .may_block = true,
> + };
> + struct kvm_mmu_notifier_range post_set_range = {
> + .start = start,
> + .end = end,
> + .arg.attributes = attributes,
> + .handler = kvm_arch_post_set_memory_attributes,
> + .on_lock = kvm_mmu_invalidate_end,
> + .may_block = true,
> + };
> + unsigned long i;
> + void *entry;
> + int r = 0;
> +
> + entry = attributes ? xa_mk_value(attributes) : NULL;
Also here, do we need to get existing attributes of a GFN first ?

> + mutex_lock(>slots_lock);
> +
> + /* Nothing to do if the entire range as the desired attributes. */
> + if (kvm_range_has_memory_attributes(kvm, start, end, attributes))
> + goto out_unlock;
> +
> + /*
> +  * Reserve memory ahead of time to avoid having to deal with failures
> +  * partway through setting the new attributes.
> +  */
> + for (i = start; i < end; i++) {
> + r = xa_reserve(>mem_attr_array, i, GFP_KERNEL_ACCOUNT);
> + if (r)
> + goto out_unlock;
> + }
> +
> + kvm_handle_gfn_range(kvm, _set_range);
> +
> + for (i = start; i < end; i++) {
> + r = xa_err(xa_store(>mem_attr_array, i, entry,
> + GFP_KERNEL_ACCOUNT));
> + KVM_BUG_ON(r, kvm);
> + }
> +
> + kvm_handle_gfn_range(kvm, _set_range);
> +
> +out_unlock:
> + mutex_unlock(>slots_lock);
> +
> + return r;
> +}
 


Re: [RFC PATCH v12 06/33] KVM: Introduce KVM_SET_USER_MEMORY_REGION2

2023-09-15 Thread Xiaoyao Li

On 9/14/2023 9:55 AM, Sean Christopherson wrote:

Introduce a "version 2" of KVM_SET_USER_MEMORY_REGION so that additional
information can be supplied without setting userspace up to fail.  The
padding in the new kvm_userspace_memory_region2 structure will be used to
pass a file descriptor in addition to the userspace_addr, i.e. allow
userspace to point at a file descriptor and map memory into a guest that
is NOT mapped into host userspace.

Alternatively, KVM could simply add "struct kvm_userspace_memory_region2"
without a new ioctl(), but as Paolo pointed out, adding a new ioctl()
makes detection of bad flags a bit more robust, e.g. if the new fd field
is guarded only by a flag and not a new ioctl(), then a userspace bug
(setting a "bad" flag) would generate out-of-bounds access instead of an
-EINVAL error.

Cc: Jarkko Sakkinen  > Reviewed-by: Paolo Bonzini 



Reviewed-by: Xiaoyao Li 


Signed-off-by: Sean Christopherson 
---
  arch/x86/kvm/x86.c   |  2 +-
  include/linux/kvm_host.h |  4 ++--
  include/uapi/linux/kvm.h | 13 +
  virt/kvm/kvm_main.c  | 38 ++
  4 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 6c9c81e82e65..8356907079e1 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -12447,7 +12447,7 @@ void __user * __x86_set_memory_region(struct kvm *kvm, 
int id, gpa_t gpa,
}
  
  	for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {

-   struct kvm_userspace_memory_region m;
+   struct kvm_userspace_memory_region2 m;
  
  		m.slot = id | (i << 16);

m.flags = 0;
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 5faba69403ac..4e741ff27af3 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -1146,9 +1146,9 @@ enum kvm_mr_change {
  };
  
  int kvm_set_memory_region(struct kvm *kvm,

- const struct kvm_userspace_memory_region *mem);
+ const struct kvm_userspace_memory_region2 *mem);
  int __kvm_set_memory_region(struct kvm *kvm,
-   const struct kvm_userspace_memory_region *mem);
+   const struct kvm_userspace_memory_region2 *mem);
  void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot);
  void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen);
  int kvm_arch_prepare_memory_region(struct kvm *kvm,
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 13065dd96132..bd1abe067f28 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -95,6 +95,16 @@ struct kvm_userspace_memory_region {
__u64 userspace_addr; /* start of the userspace allocated memory */
  };
  
+/* for KVM_SET_USER_MEMORY_REGION2 */

+struct kvm_userspace_memory_region2 {
+   __u32 slot;
+   __u32 flags;
+   __u64 guest_phys_addr;
+   __u64 memory_size;
+   __u64 userspace_addr;
+   __u64 pad[16];
+};
+
  /*
   * The bit 0 ~ bit 15 of kvm_userspace_memory_region::flags are visible for
   * userspace, other bits are reserved for kvm internal use which are defined
@@ -1192,6 +1202,7 @@ struct kvm_ppc_resize_hpt {
  #define KVM_CAP_COUNTER_OFFSET 227
  #define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228
  #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229
+#define KVM_CAP_USER_MEMORY2 230
  
  #ifdef KVM_CAP_IRQ_ROUTING
  
@@ -1473,6 +1484,8 @@ struct kvm_vfio_spapr_tce {

struct kvm_userspace_memory_region)
  #define KVM_SET_TSS_ADDR  _IO(KVMIO,   0x47)
  #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO,  0x48, __u64)
+#define KVM_SET_USER_MEMORY_REGION2 _IOW(KVMIO, 0x49, \
+struct kvm_userspace_memory_region2)
  
  /* enable ucontrol for s390 */

  struct kvm_s390_ucas_mapping {
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 8d21757cd5e9..7c0e38752526 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1571,7 +1571,7 @@ static void kvm_replace_memslot(struct kvm *kvm,
}
  }
  
-static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem)

+static int check_memory_region_flags(const struct kvm_userspace_memory_region2 
*mem)
  {
u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
  
@@ -1973,7 +1973,7 @@ static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id,

   * Must be called holding kvm->slots_lock for write.
   */
  int __kvm_set_memory_region(struct kvm *kvm,
-   const struct kvm_userspace_memory_region *mem)
+   const struct kvm_userspace_memory_region2 *mem)
  {
struct kvm_memory_slot *old, *new;
struct kvm_memslots *slots;
@@ -2077,7 +2077,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
  EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  
  int kvm_set_memory_region(struct kvm *kvm,

- const struct 

Re: [RFC PATCH v12 01/33] KVM: Tweak kvm_hva_range and hva_handler_t to allow reusing for gfn ranges

2023-09-15 Thread Xiaoyao Li

On 9/14/2023 9:54 AM, Sean Christopherson wrote:

Rework and rename "struct kvm_hva_range" into "kvm_mmu_notifier_range" so
that the structure can be used to handle notifications that operate on gfn
context, i.e. that aren't tied to a host virtual address.

Practically speaking, this is a nop for 64-bit kernels as the only
meaningful change is to store start+end as u64s instead of unsigned longs.

Reviewed-by: Paolo Bonzini 
Signed-off-by: Sean Christopherson 
---
  virt/kvm/kvm_main.c | 34 +++---
  1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 486800a7024b..0524933856d4 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -541,18 +541,22 @@ static inline struct kvm *mmu_notifier_to_kvm(struct 
mmu_notifier *mn)
return container_of(mn, struct kvm, mmu_notifier);
  }
  
-typedef bool (*hva_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range);

+typedef bool (*gfn_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range);


Is it worth mentioning the rename of it as well in changelog?

Anyway,

Reviewed-by: Xiaoyao Li 


Re: [RFC PATCH v12 14/33] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory

2023-09-15 Thread Yan Zhao
On Wed, Sep 13, 2023 at 06:55:12PM -0700, Sean Christopherson wrote:
> +static struct folio *kvm_gmem_get_folio(struct file *file, pgoff_t index)
> +{
> + struct folio *folio;
> +
> + /* TODO: Support huge pages. */
> + folio = filemap_grab_folio(file->f_mapping, index);
> + if (IS_ERR_OR_NULL(folio))
> + return NULL;
> +
> + /*
> +  * Use the up-to-date flag to track whether or not the memory has been
> +  * zeroed before being handed off to the guest.  There is no backing
> +  * storage for the memory, so the folio will remain up-to-date until
> +  * it's removed.
> +  *
> +  * TODO: Skip clearing pages when trusted firmware will do it when
> +  * assigning memory to the guest.
> +  */
> + if (!folio_test_uptodate(folio)) {
> + unsigned long nr_pages = folio_nr_pages(folio);
> + unsigned long i;
> +
> + for (i = 0; i < nr_pages; i++)
> + clear_highpage(folio_page(folio, i));
> +
> + folio_mark_uptodate(folio);
> + }
> +
> + /*
> +  * Ignore accessed, referenced, and dirty flags.  The memory is
> +  * unevictable and there is no storage to write back to.
> +  */
> + return folio;
> +}
If VFIO wants to map a private page, is it required to call this function for 
PFN?



Re: [PATCH] hwmon: (ibmpowernv) refactor deprecated strncpy

2023-09-15 Thread Kees Cook
On Thu, Sep 14, 2023 at 10:40:37PM -0700, Guenter Roeck wrote:
> It is really sad that the submitters of such "cleanup" patches can't be 
> bothered
> to check what they are doing. They can't even be bothered to write a 
> coccinelle
> script that would avoid pitfalls like this one, and they expect others to do 
> their
> homework for them.

Well I'm not sure that's entirely fair to Justin's efforts (I know he's
been studying these changes and everyone makes mistakes), but that's why
I'm helping review his findings -- some code behaviors are more obvious
than others. :)

-- 
Kees Cook


[PATCH] ASoC: imx-audmix: Fix return error with devm_clk_get()

2023-09-15 Thread Shengjiu Wang
The devm_clk_get() can return -EPROBE_DEFER error,
modify the error code to be -EINVAL is not correct, which
cause the -EPROBE_DEFER error is not correctly handled.

This patch is to fix the return error code.

Fixes: b86ef5367761 ("ASoC: fsl: Add Audio Mixer machine driver")
Signed-off-by: Shengjiu Wang 
---
 sound/soc/fsl/imx-audmix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/soc/fsl/imx-audmix.c b/sound/soc/fsl/imx-audmix.c
index 0b58df56f4da..aeb81aa61184 100644
--- a/sound/soc/fsl/imx-audmix.c
+++ b/sound/soc/fsl/imx-audmix.c
@@ -315,7 +315,7 @@ static int imx_audmix_probe(struct platform_device *pdev)
if (IS_ERR(priv->cpu_mclk)) {
ret = PTR_ERR(priv->cpu_mclk);
dev_err(_pdev->dev, "failed to get DAI mclk1: %d\n", ret);
-   return -EINVAL;
+   return ret;
}
 
priv->audmix_pdev = audmix_pdev;
-- 
2.34.1



Re: [PATCH] powerpc: add `cur_cpu_spec` symbol to vmcoreinfo

2023-09-15 Thread Hari Bathini




On 14/09/23 6:52 pm, Michael Ellerman wrote:

Aditya Gupta  writes:

Presently, while reading a vmcore, makedumpfile uses
`cur_cpu_spec.mmu_features` to decide whether the crashed system had
RADIX MMU or not.

Currently, makedumpfile fails to get the `cur_cpu_spec` symbol (unless
a vmlinux is passed with the `-x` flag to makedumpfile), and hence
assigns offsets and shifts (such as pgd_offset_l4) incorrecly considering
MMU to be hash MMU.

Add `cur_cpu_spec` symbol and offset of `mmu_features` in the
`cpu_spec` struct, to VMCOREINFO, so that the symbol address and offset
is accessible to makedumpfile, without needing the vmlinux file


This looks fine.

Seems like cpu_features would be needed or at least pretty useful too?


Yeah. Would be nice to have access to that, to accurately identify the
kind of system vmcore was generated for.

Thanks
Hari


Re: [RFC PATCH v12 18/33] KVM: x86/mmu: Handle page fault for private memory

2023-09-15 Thread Yan Zhao
On Wed, Sep 13, 2023 at 06:55:16PM -0700, Sean Christopherson wrote:

> +static void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
> +   struct kvm_page_fault *fault)
> +{
> + kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT,
> +   PAGE_SIZE, fault->write, fault->exec,
> +   fault->is_private);
> +}
> +
> +static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu,
> +struct kvm_page_fault *fault)
> +{
> + int max_order, r;
> +
> + if (!kvm_slot_can_be_private(fault->slot)) {
> + kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
> + return -EFAULT;
> + }
> +
> + r = kvm_gmem_get_pfn(vcpu->kvm, fault->slot, fault->gfn, >pfn,
> +  _order);
> + if (r) {
> + kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
> + return r;
> + }
> +
> + fault->max_level = min(kvm_max_level_for_order(max_order),
> +fault->max_level);
> + fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY);
> +
> + return RET_PF_CONTINUE;
> +}
> +
>  static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault 
> *fault)
>  {
>   struct kvm_memory_slot *slot = fault->slot;
> @@ -4293,6 +4356,14 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, 
> struct kvm_page_fault *fault
>   return RET_PF_EMULATE;
>   }
>  
> + if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) {
In patch 21,
fault->is_private is set as:
".is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT)",
then, the inequality here means memory attribute has been updated after
last check.
So, why an exit to user space for converting is required instead of a mere 
retry?

Or, is it because how .is_private is assigned in patch 21 is subjected to change
in future? 

> + kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
> + return -EFAULT;
> + }
> +
> + if (fault->is_private)
> + return kvm_faultin_pfn_private(vcpu, fault);
> +
>   async = false;
>   fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, false, 
> ,
> fault->write, >map_writable,
> @@ -7184,6 +7255,19 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm)
>  }
>