Skip to contentSkip to conversation

KVM: s390: Introducing kvm_arch_set_irq_inatomic Fast Inject

PATCH Superseded Douglas Freimuth 4 participants 13 messages May 5 – May 8

Subsystems (3)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover KVM: s390: Introducing kvm_arch_set_irq_inatomic Fast Inject

S390 needs this series of three patches in order to enable a non-blocking path for irqfd injection on s390 via kvm_arch_set_irq_inatomic(). Before these changes, kvm_arch_set_irq_inatomic() would just return -EWOULDBLOCK and place all interrupts on the global work queue, which must subsequently be processed by a different thread. This series of patches implements an s390 version of inatomic and is relevant to virtio-blk and virtio-net and was tested against virtio-pci and virtio-ccw.

The inatomic fast path cannot lose control since it is running with interrupts disabled. This meant making the following changes that exist on the slow path today. First, the adapter_indicators page needs to be mapped since it is accessed with interrupts disabled, so we added map/unmap functions. Second, access to shared resources between the fast and slow paths needed to be changed from mutex and semaphores to raw_spin_lock's. Finally, the memory allocation on the slow path utilizes GFP_KERNEL_ACCOUNT but we had to implement the fast path with GFP_ATOMIC allocation. Each of these enhancements were required to prevent blocking on the fast inject path. In order to handle the RT kernel case, raw spin locks were added.

Statistical counters have been added to enable analysis of irq injection on the fast path and slow path including io_390_inatomic, io_flic_inject_airq, io_set_adapter_int and io_390_inatomic_adapter_masked. And counters have been added to analyze map/unmap of the adapter_indicator
pages in non-Secure Execution environments and to track fencing of Fast Inject in Secure Execution environments. In order to take advantage of this kernel series with virtio-pci, a QEMU that includes the 's390x/pci: set kvm_msi_via_irqfd_allowed' fix is needed. Additionally, the guest xml needs a thread pool and threads explicitly assigned per disk device using the common way of defining threads for disks.

Patch 1 enables map/unmap of adapter indicator pages but for Secure Execution environments it avoids the long term mapping.

v4->v5: Replace get_map_page using pin functions FOLL_WRITE | FOLL_LONGTERM v4->v5: Add shared function for destroy_adapters, unmap_all_adapters_pv v4->v5: Upon failed injection on fast path clear the summary bit only v4->v5: If allocation fails in inatomic, clear summary bit and do slow path v4->v5: Change fi->lock to a raw spin lock to handle the RT case

Douglas Freimuth (4):
KVM: s390: Add map/unmap ioctl and clean mappings post-guest
KVM: s390: Enable adapter_indicators_set to use mapped pages
KVM: s390: Change the fi->lock to a raw_spinlock for RT case
KVM: s390: Introducing kvm_arch_set_irq_inatomic fast inject

 arch/s390/include/asm/kvm_host.h |  13 +-
 arch/s390/kvm/intercept.c        |   4 +-
 arch/s390/kvm/interrupt.c        | 497 +++++++++++++++++++++++++------
 arch/s390/kvm/kvm-s390.c         |  31 +-
 arch/s390/kvm/kvm-s390.h         |   5 +-
 5 files changed, 444 insertions(+), 106 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v5 1/4 KVM: s390: Add map/unmap ioctl and clean mappings post-guest

S390 needs map/unmap ioctls, which map the adapter set indicator pages, so the pages can be accessed when interrupts are disabled. The mappings are cleaned up when the guest is removed.

Map/Unmap ioctls are fenced in order to avoid the longterm pinning in Secure Execution environments. In Secure Execution environments the path of execution available before this patch is followed.

Statistical counters to count map/unmap functions for adapter indicator pages are added. The counters can be used to analyze map/unmap functions in non-Secure Execution environments and similarly can be used to analyze Secure Execution environments where the counters will not be incremented as the adapter indicator pages are not mapped.

---
 arch/s390/include/asm/kvm_host.h |   5 +
 arch/s390/kvm/interrupt.c        | 190 ++++++++++++++++++++++++++-----
 arch/s390/kvm/kvm-s390.c         |   8 ++
 arch/s390/kvm/kvm-s390.h         |   2 +
 4 files changed, 176 insertions(+), 29 deletions(-)
arch/s390/include/asm/kvm_host.h +5 -0
index 8a4f4a39f7a2..fbb2406b31d2 100644
@@ -448,6 +448,8 @@ struct kvm_vcpu_arch {
 struct kvm_vm_stat {
 	struct kvm_vm_stat_generic generic;
 	u64 inject_io;
+	u64 io_390_adapter_map;
+	u64 io_390_adapter_unmap;
 	u64 inject_float_mchk;
 	u64 inject_pfault_done;
 	u64 inject_service_signal;
@@ -479,6 +481,9 @@ struct s390_io_adapter {
 	bool masked;
 	bool swap;
 	bool suppressible;
+	raw_spinlock_t maps_lock;
+	struct list_head maps;
+	unsigned int nr_maps;
 };
 
 #define MAX_S390_IO_ADAPTERS ((MAX_ISC + 1) * 8)
arch/s390/kvm/interrupt.c +161 -29

235 lines not on this page · raw message

arch/s390/kvm/kvm-s390.c +8 -0
index e09960c2e6ed..74f453f039a3 100644
@@ -68,6 +68,8 @@
 const struct kvm_stats_desc kvm_vm_stats_desc[] = {
 	KVM_GENERIC_VM_STATS(),
 	STATS_DESC_COUNTER(VM, inject_io),
+	STATS_DESC_COUNTER(VM, io_390_adapter_map),
+	STATS_DESC_COUNTER(VM, io_390_adapter_unmap),
 	STATS_DESC_COUNTER(VM, inject_float_mchk),
 	STATS_DESC_COUNTER(VM, inject_pfault_done),
 	STATS_DESC_COUNTER(VM, inject_service_signal),
@@ -2497,6 +2499,11 @@ static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd,
 	return r;
 }
 
+static void kvm_s390_unmap_all_adapters_pv(struct kvm *kvm)
+{
+	kvm_s390_unmap_all_adapters(kvm);
+}
+
 static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
 {
 	const bool need_lock = (cmd->cmd != KVM_PV_ASYNC_CLEANUP_PERFORM);
@@ -2513,6 +2520,7 @@ static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
 		if (kvm_s390_pv_is_protected(kvm))
 			break;
 
+		kvm_s390_unmap_all_adapters_pv(kvm);
 		mmap_write_lock(kvm->mm);
 		/*
 		 * Disable creation of new THPs. Existing THPs can stay, they
arch/s390/kvm/kvm-s390.h +2 -0
index dc0573b7aa4b..7ba885cb6bd1 100644
@@ -560,6 +560,8 @@ void kvm_s390_gisa_disable(struct kvm *kvm);
 void kvm_s390_gisa_enable(struct kvm *kvm);
 int __init kvm_s390_gib_init(u8 nisc);
 void kvm_s390_gib_destroy(void);
+void kvm_s390_unmap_all_adapters(struct kvm *kvm);
+
 
 /* implemented in guestdbg.c */
 void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu);
Signature · 1 line
2.52.0
#2 lore raw

PATCH v5 2/4 KVM: s390: Enable adapter_indicators_set to use mapped pages

In reply to Douglas Freimuth

The S390 adapter_indicators_set function needs to be able to use mapped pages so that worked can be processed,on a fast path when interrupts are disabled. If adapter indicator pages are not mapped then local mapping is done on a slow path as it is prior to this patch. For example, Secure Execution environments will take the local mapping path as it does prior to this patch.

---
 arch/s390/kvm/interrupt.c | 94 ++++++++++++++++++++++++++++-----------
 1 file changed, 69 insertions(+), 25 deletions(-)
arch/s390/kvm/interrupt.c +69 -25
index a9b418996225..12d8d38c260d 100644
@@ -2883,41 +2883,85 @@ static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
 	return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
 }
 
+static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
+					  u64 addr)
+{
+	struct s390_map_info *map;
+
+	if (!adapter)
+		return NULL;
+
+	list_for_each_entry(map, &adapter->maps, list) {
+		if (map->addr == addr)
+			return map;
+	}
+	return NULL;
+}
+
 static int adapter_indicators_set(struct kvm *kvm,
 				  struct s390_io_adapter *adapter,
 				  struct kvm_s390_adapter_int *adapter_int)
 {
 	unsigned long bit;
 	int summary_set, idx;
-	struct page *ind_page, *summary_page;
+	struct s390_map_info *ind_info, *summary_info;
 	void *map;
+	struct page *ind_page, *summary_page;
+	unsigned long flags;
 
-	ind_page = pin_map_page(kvm, adapter_int->ind_addr, 0);
-	if (!ind_page)
-		return -1;
-	summary_page = pin_map_page(kvm, adapter_int->summary_addr, 0);
-	if (!summary_page) {
-		put_page(ind_page);
-		return -1;
+	raw_spin_lock_irqsave(&adapter->maps_lock, flags);
+	ind_info = get_map_info(adapter, adapter_int->ind_addr);
+	if (!ind_info) {
+		raw_spin_unlock_irqrestore(&adapter->maps_lock, flags);
+		ind_page = pin_map_page(kvm, adapter_int->ind_addr, 0);
+		if (!ind_page)
+			return -1;
+		idx = srcu_read_lock(&kvm->srcu);
+		map = page_address(ind_page);
+		bit = get_ind_bit(adapter_int->ind_addr,
+				  adapter_int->ind_offset, adapter->swap);
+		set_bit(bit, map);
+		mark_page_dirty(kvm, adapter_int->ind_gaddr >> PAGE_SHIFT);
+		set_page_dirty_lock(ind_page);
+		srcu_read_unlock(&kvm->srcu, idx);
+	} else {
+		map = page_address(ind_info->page);
+		bit = get_ind_bit(ind_info->addr, adapter_int->ind_offset, adapter->swap);
+		set_bit(bit, map);
+		raw_spin_unlock_irqrestore(&adapter->maps_lock, flags);
+	}
+	raw_spin_lock_irqsave(&adapter->maps_lock, flags);
+	summary_info = get_map_info(adapter, adapter_int->summary_addr);
+	if (!summary_info) {
+		raw_spin_unlock_irqrestore(&adapter->maps_lock, flags);
+		summary_page = pin_map_page(kvm, adapter_int->summary_addr, 0);
+		if (!summary_page) {
+			if (!ind_info) {
+				WARN_ON_ONCE(!ind_page);
+				unpin_user_page(ind_page);
+			}
+			return -1;
+		}
+		idx = srcu_read_lock(&kvm->srcu);
+		map = page_address(summary_page);
+		bit = get_ind_bit(adapter_int->summary_addr,
+				  adapter_int->summary_offset, adapter->swap);
+		summary_set = test_and_set_bit(bit, map);
+		mark_page_dirty(kvm, adapter_int->summary_gaddr >> PAGE_SHIFT);
+		set_page_dirty_lock(summary_page);
+		srcu_read_unlock(&kvm->srcu, idx);
+	} else {
+		map = page_address(summary_info->page);
+		bit = get_ind_bit(summary_info->addr, adapter_int->summary_offset,
+				  adapter->swap);
+		summary_set = test_and_set_bit(bit, map);
+		raw_spin_unlock_irqrestore(&adapter->maps_lock, flags);
 	}
 
-	idx = srcu_read_lock(&kvm->srcu);
-	map = page_address(ind_page);
-	bit = get_ind_bit(adapter_int->ind_addr,
-			  adapter_int->ind_offset, adapter->swap);
-	set_bit(bit, map);
-	mark_page_dirty(kvm, adapter_int->ind_gaddr >> PAGE_SHIFT);
-	set_page_dirty_lock(ind_page);
-	map = page_address(summary_page);
-	bit = get_ind_bit(adapter_int->summary_addr,
-			  adapter_int->summary_offset, adapter->swap);
-	summary_set = test_and_set_bit(bit, map);
-	mark_page_dirty(kvm, adapter_int->summary_gaddr >> PAGE_SHIFT);
-	set_page_dirty_lock(summary_page);
-	srcu_read_unlock(&kvm->srcu, idx);
-
-	unpin_user_page(ind_page);
-	unpin_user_page(summary_page);
+	if (!ind_info)
+		unpin_user_page(ind_page);
+	if (!summary_info)
+		unpin_user_page(summary_page);
 	return summary_set ? 0 : 1;
 }
 
Signature · 1 line
2.52.0
#3 lore raw

PATCH v5 3/4 KVM: s390: Change the fi->lock to a raw_spinlock for RT case

In reply to Douglas Freimuth

s390 needs to maintain support for an RT kernel. This requires the floating interrupt lock, fi->lock to be changed to a raw spin lock since the fi->lock maybe called with interrupts disabled in __inject_io.

---
 arch/s390/include/asm/kvm_host.h |  2 +-
 arch/s390/kvm/intercept.c        |  4 +-
 arch/s390/kvm/interrupt.c        | 68 ++++++++++++++++----------------
 arch/s390/kvm/kvm-s390.c         |  2 +-
 4 files changed, 38 insertions(+), 38 deletions(-)
arch/s390/include/asm/kvm_host.h +1 -1
index fbb2406b31d2..9dd8a4986592 100644
@@ -353,7 +353,7 @@ struct kvm_s390_local_interrupt {
 struct kvm_s390_float_interrupt {
 	unsigned long pending_irqs;
 	unsigned long masked_irqs;
-	spinlock_t lock;
+	raw_spinlock_t lock;
 	struct list_head lists[FIRQ_LIST_COUNT];
 	int counters[FIRQ_MAX_COUNT];
 	struct kvm_s390_mchk_info mchk;
arch/s390/kvm/intercept.c +2 -2
index 39aff324203e..6e9ad58c0e90 100644
@@ -518,7 +518,7 @@ static int handle_pv_sclp(struct kvm_vcpu *vcpu)
 {
 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
 
-	spin_lock(&fi->lock);
+	raw_spin_lock(&fi->lock);
 	/*
 	 * 2 cases:
 	 * a: an sccb answering interrupt was already pending or in flight.
@@ -534,7 +534,7 @@ static int handle_pv_sclp(struct kvm_vcpu *vcpu)
 	fi->srv_signal.ext_params |= 0x43000;
 	set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
 	clear_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs);
-	spin_unlock(&fi->lock);
+	raw_spin_unlock(&fi->lock);
 	return 0;
 }
 
arch/s390/kvm/interrupt.c +34 -34

240 lines not on this page · raw message

arch/s390/kvm/kvm-s390.c +1 -1
index 74f453f039a3..d8011b6d6801 100644
@@ -3263,7 +3263,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	}
 
 	mutex_init(&kvm->arch.float_int.ais_lock);
-	spin_lock_init(&kvm->arch.float_int.lock);
+	raw_spin_lock_init(&kvm->arch.float_int.lock);
 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
 	init_waitqueue_head(&kvm->arch.ipte_wq);
Signature · 1 line
2.52.0
maintainer
#4 lore raw

Re: [PATCH v5 3/4] KVM: s390: Change the fi->lock to a raw_spinlock for RT case

Quoted 11 lines · Douglas Freimuth

s390 needs to maintain support for an RT kernel. This requires the floating interrupt lock, fi->lock to be changed to a raw spin lock since the fi->lock maybe called with interrupts disabled in __inject_io.

---
 arch/s390/include/asm/kvm_host.h |  2 +-
 arch/s390/kvm/intercept.c        |  4 +-
 arch/s390/kvm/interrupt.c        | 68 ++++++++++++++++----------------
 arch/s390/kvm/kvm-s390.c         |  2 +-
 4 files changed, 38 insertions(+), 38 deletions(-)

s390 does not support RT, but I guess you are referring to a lockdep splat which you would see without doing this change, similar like we have seen at other places.

Can you include the relevant parts of the splat for reference, please?

#5 lore raw

Re: [PATCH v5 3/4] KVM: s390: Change the fi->lock to a raw_spinlock for RT case

Quoted 13 lines · Heiko Carstens
Quoted 11 lines · Douglas Freimuth

s390 needs to maintain support for an RT kernel. This requires the floating interrupt lock, fi->lock to be changed to a raw spin lock since the fi->lock maybe called with interrupts disabled in __inject_io.

---
  arch/s390/include/asm/kvm_host.h |  2 +-
  arch/s390/kvm/intercept.c        |  4 +-
  arch/s390/kvm/interrupt.c        | 68 ++++++++++++++++----------------
  arch/s390/kvm/kvm-s390.c         |  2 +-
  4 files changed, 38 insertions(+), 38 deletions(-)
Quoted 5 lines

s390 does not support RT, but I guess you are referring to a lockdep splat which you would see without doing this change, similar like we have seen at other places.

Can you include the relevant parts of the splat for reference, please?

Heiko, thank you for you response. I dont recall trapping it with lockdep (while it was on) but discussion on the mailing list in an earlier version made us look closer (and we saw it across the AI models that reviewed the patch.) It appears that while RT isn't supported it can still be compiled in to the kernel so we wanted to mitigate the issues we would add to if someone does that while not impacting non-RT environments, the main use case.

maintainer
#6 lore raw

Re: [PATCH v5 3/4] KVM: s390: Change the fi->lock to a raw_spinlock for RT case

Quoted 18 lines · Douglas Freimuth
Quoted 17 lines · Heiko Carstens
Quoted 11 lines · Douglas Freimuth

s390 needs to maintain support for an RT kernel. This requires the floating interrupt lock, fi->lock to be changed to a raw spin lock since the fi->lock maybe called with interrupts disabled in __inject_io.

---
  arch/s390/include/asm/kvm_host.h |  2 +-
  arch/s390/kvm/intercept.c        |  4 +-
  arch/s390/kvm/interrupt.c        | 68 ++++++++++++++++----------------
  arch/s390/kvm/kvm-s390.c         |  2 +-
  4 files changed, 38 insertions(+), 38 deletions(-)

s390 does not support RT, but I guess you are referring to a lockdep splat which you would see without doing this change, similar like we have seen at other places.

Quoted 8 lines
Quoted 1 line

Can you include the relevant parts of the splat for reference, please?

Heiko, thank you for you response. I dont recall trapping it with lockdep (while it was on) but discussion on the mailing list in an earlier version made us look closer (and we saw it across the AI models that reviewed the patch.) It appears that while RT isn't supported it can still be compiled in to the kernel so we wanted to mitigate the issues we would add to if someone does that while not impacting non-RT environments, the main use case.

RT support cannot be compiled in for s390, because of the missing "select ARCH_SUPPORTS_RT", however you can still enable lockdep checks for raw_spinlock vs spinlock nesting, which this seems to appear about?

See PROVE_RAW_LOCK_NESTING config option for a more detailed description.

Therefore my question about a lockdep splat. However I don't see why using spin_lock() instead of raw_spin_lock() alone in irq disabled context could be problematic. On the other hand this patch does introduce a

      raw_spin_lock();
      spin_lock();
      spin_unlock();
      raw_spin_unlock();

sequence in __deliver_machine_check() which seems to be incorrect and indeed should generate a lockdep splat iff PROVE_RAW_LOCK_NESTING is enabled.

#7 lore raw

Re: [PATCH v5 3/4] KVM: s390: Change the fi->lock to a raw_spinlock for RT case

Quoted 32 lines · Heiko Carstens
Quoted 18 lines · Douglas Freimuth
Quoted 17 lines · Heiko Carstens
Quoted 11 lines · Douglas Freimuth

s390 needs to maintain support for an RT kernel. This requires the floating interrupt lock, fi->lock to be changed to a raw spin lock since the fi->lock maybe called with interrupts disabled in __inject_io.

---
  arch/s390/include/asm/kvm_host.h |  2 +-
  arch/s390/kvm/intercept.c        |  4 +-
  arch/s390/kvm/interrupt.c        | 68 ++++++++++++++++----------------
  arch/s390/kvm/kvm-s390.c         |  2 +-
  4 files changed, 38 insertions(+), 38 deletions(-)

s390 does not support RT, but I guess you are referring to a lockdep splat which you would see without doing this change, similar like we have seen at other places.

Quoted 8 lines
Quoted 1 line

Can you include the relevant parts of the splat for reference, please?

Heiko, thank you for you response. I dont recall trapping it with lockdep (while it was on) but discussion on the mailing list in an earlier version made us look closer (and we saw it across the AI models that reviewed the patch.) It appears that while RT isn't supported it can still be compiled in to the kernel so we wanted to mitigate the issues we would add to if someone does that while not impacting non-RT environments, the main use case.

RT support cannot be compiled in for s390, because of the missing "select ARCH_SUPPORTS_RT", however you can still enable lockdep checks for raw_spinlock vs spinlock nesting, which this seems to appear about?

Quoted 5 lines

See PROVE_RAW_LOCK_NESTING config option for a more detailed description.

Therefore my question about a lockdep splat. However I don't see why using spin_lock() instead of raw_spin_lock() alone in irq disabled context could be problematic. On the other hand this patch does

Hi Heiko,

AFAIU it is only problematic if we (s390) should ever want to support RT in the future.

As the name implies, the point of kvm_arch_set_irq_inatomic() is to inject the interrupt without the possibility of sleeping, or alternatively recognizing the need to sleep and fall back to a queued "slow path" that can safely sleep while delivering it.

My original thinking was 'well, it won't hurt to use the raw spinlocks in the new code' so I set Doug down this road with my review comments -- I did not consider that there would be a need for additional fallout like this patch, which means increased chance of regressions (see below) to accomodate a feature that we don't support today.

If you are saying it's OK to simply not care about RT for s390 now, then AFAICT it should be fine to just use s/raw_spin_)lock/spin_lock/ for this whole series, drop this patch and then ignore the subsequent Sashiko complaints about RT.

What do you think?

Quoted 10 lines

introduce a

      raw_spin_lock();
      spin_lock();
      spin_unlock();
      raw_spin_unlock();

sequence in __deliver_machine_check() which seems to be incorrect and indeed should generate a lockdep splat iff PROVE_RAW_LOCK_NESTING is enabled.

+1
Doug, I know you've run with lockdep enabled before on this series -- please make sure to test with lockdep for next version

Thanks,
Matt

maintainer
#8 lore raw

Re: [PATCH v5 3/4] KVM: s390: Change the fi->lock to a raw_spinlock for RT case

Adding Peter :)

Quoted 21 lines · Matthew Rosato
Quoted 20 lines · Heiko Carstens
Quoted 19 lines · Douglas Freimuth
Quoted 18 lines · Heiko Carstens
Quoted 11 lines · Douglas Freimuth

s390 needs to maintain support for an RT kernel. This requires the floating interrupt lock, fi->lock to be changed to a raw spin lock since the fi->lock maybe called with interrupts disabled in __inject_io.

---
  arch/s390/include/asm/kvm_host.h |  2 +-
  arch/s390/kvm/intercept.c        |  4 +-
  arch/s390/kvm/interrupt.c        | 68 ++++++++++++++++----------------
  arch/s390/kvm/kvm-s390.c         |  2 +-
  4 files changed, 38 insertions(+), 38 deletions(-)

s390 does not support RT, but I guess you are referring to a lockdep splat which you would see without doing this change, similar like we have seen at other places.

Can you include the relevant parts of the splat for reference, please?

...

Quoted 2 lines

AFAIU it is only problematic if we (s390) should ever want to support RT in the future.

I don't see that coming, but nobody knows what happens in future.

...

Quoted 6 lines

My original thinking was 'well, it won't hurt to use the raw spinlocks in the new code' so I set Doug down this road with my review comments -- I did not consider that there would be a need for additional fallout like this patch, which means increased chance of regressions (see below) to accomodate a feature that we don't support today.

Quoted 6 lines

If you are saying it's OK to simply not care about RT for s390 now, then AFAICT it should be fine to just use s/raw_spin_)lock/spin_lock/ for this whole series, drop this patch and then ignore the subsequent Sashiko complaints about RT.

What do you think?

So... after having given this a second thought: we do not have PROVE_RAW_LOCK_NESTING enabled in our debug_defconfig (either we missed it, or somebody (cough) thought it is not relevant for s390). That said, I believe we should enable it, fix all fallout and also make sure that new code does not generate any lockdep splats with PROVE_RAW_LOCK_NESTING enabled.

Rationale: even though it is not relevant for s390, we also change common code; and by ignoring PROVE_RAW_LOCK_NESTING we might cause problems for other architectures by introducing incorrect nesting of locks in common code. So yes, your thinking is correct.

Peter, I just added you to cc, so you can correct me if I'm entirely wrong.

#9 lore raw

Re: [PATCH v5 3/4] KVM: s390: Change the fi->lock to a raw_spinlock for RT case

Quoted 7 lines · Heiko Carstens

So... after having given this a second thought: we do not have PROVE_RAW_LOCK_NESTING enabled in our debug_defconfig (either we missed it, or somebody (cough) thought it is not relevant for s390). That said, I believe we should enable it, fix all fallout and also make sure that new code does not generate any lockdep splats with PROVE_RAW_LOCK_NESTING enabled.

Quoted 6 lines

Rationale: even though it is not relevant for s390, we also change common code; and by ignoring PROVE_RAW_LOCK_NESTING we might cause problems for other architectures by introducing incorrect nesting of locks in common code. So yes, your thinking is correct.

Peter, I just added you to cc, so you can correct me if I'm entirely wrong.

Makes sense to me; thanks for doing so!

#10 lore raw

Re: [PATCH v5 3/4] KVM: s390: Change the fi->lock to a raw_spinlock for RT case

In reply to Heiko Carstens
Quoted 54 lines · Heiko Carstens

Adding Peter :)

Quoted 21 lines · Matthew Rosato
Quoted 20 lines · Heiko Carstens
Quoted 19 lines · Douglas Freimuth
Quoted 18 lines · Heiko Carstens
Quoted 11 lines · Douglas Freimuth

s390 needs to maintain support for an RT kernel. This requires the floating interrupt lock, fi->lock to be changed to a raw spin lock since the fi->lock maybe called with interrupts disabled in __inject_io.

---
   arch/s390/include/asm/kvm_host.h |  2 +-
   arch/s390/kvm/intercept.c        |  4 +-
   arch/s390/kvm/interrupt.c        | 68 ++++++++++++++++----------------
   arch/s390/kvm/kvm-s390.c         |  2 +-
   4 files changed, 38 insertions(+), 38 deletions(-)

s390 does not support RT, but I guess you are referring to a lockdep splat which you would see without doing this change, similar like we have seen at other places.

Can you include the relevant parts of the splat for reference, please?

...

Quoted 2 lines

AFAIU it is only problematic if we (s390) should ever want to support RT in the future.

I don't see that coming, but nobody knows what happens in future.

...

Quoted 6 lines

My original thinking was 'well, it won't hurt to use the raw spinlocks in the new code' so I set Doug down this road with my review comments -- I did not consider that there would be a need for additional fallout like this patch, which means increased chance of regressions (see below) to accomodate a feature that we don't support today.

Quoted 6 lines

If you are saying it's OK to simply not care about RT for s390 now, then AFAICT it should be fine to just use s/raw_spin_)lock/spin_lock/ for this whole series, drop this patch and then ignore the subsequent Sashiko complaints about RT.

What do you think?

So... after having given this a second thought: we do not have PROVE_RAW_LOCK_NESTING enabled in our debug_defconfig (either we missed it, or somebody (cough) thought it is not relevant for s390). That said, I believe we should enable it, fix all fallout and also make sure that new code does not generate any lockdep splats with PROVE_RAW_LOCK_NESTING enabled.

Quoted 4 lines

Rationale: even though it is not relevant for s390, we also change common code; and by ignoring PROVE_RAW_LOCK_NESTING we might cause problems for other architectures by introducing incorrect nesting of locks in common code. So yes, your thinking is correct.

Heiko, to be complete, I went through the exercise of enabling PROVE_RAW_LOCK_NESTING. I created a small hack to generate a __deliver_machine_check to trap the nested locking issue. The requested splat is below. Here the floating interrupt lock is a raw_spin_lock and the nested call to local interrupt lock is a spin_lock thus the nesting issue. No other nesting issues were found.

Now we need to arrive at, do we keep the raw_spin_locks to cover the possibility of future RT support or common code? In that case I also make the li->lock a raw_spin_lock. OR should I drop this raw_spin_lock patch and back out any other raw_spin_locks since we dont currently support RT on s390? And end either choice by testing again with PROVE_RAW_LOCK_NESTING.

[  187.278926] =============================
[  187.278927] [ BUG: Invalid wait context ]
[  187.278930] 7.1.0-rc1-gb8e991a47d4c-dirty #6 Not tainted
[  187.278932] -----------------------------
[  187.278933] CPU 0/KVM/4263 is trying to lock:
[  187.278935] 000000c7448982a0 (&vcpu->arch.local_int.lock){+.+.}-{3:3}, at: __deliver_machine_check+0x44/0x1a0 [kvm]
[  187.278976] other info that might help us debug this:
[  187.278978] context-{5:5}
[  187.278979] 3 locks held by CPU 0/KVM/4263:
[  187.278981]  #0: 000000c7448980b8 (&vcpu->mutex){+.+.}-{4:4}, at: kvm_vcpu_ioctl+0xb8/0x9b0 [kvm]
[  187.279001]  #1: 000000c73a75b108 (&kvm->srcu){.+.+}-{0:0}, at: __vcpu_run+0x46/0x4f0 [kvm]
[  187.279024]  #2: 000000c73a758dd0 (&kvm->arch.float_int.lock){+.+.}-{2:2}, at: __deliver_machine_check+0x3a/0x1a0 [kvm]
[  187.279046] stack backtrace:
[  187.279048] CPU: 10 UID: 107 PID: 4263 Comm: CPU 0/KVM Not tainted 7.1.0-rc1-gb8e991a47d4c-dirty #6 PREEMPT
[  187.279050] Hardware name: IBM 9175 ME1 701 (LPAR)
[  187.279051] Call Trace:
[  187.279051]  [<000001cbdd2e7eea>] dump_stack_lvl+0xa2/0xe8
[  187.279054]  [<000001cbdd3ecd98>] __lock_acquire+0xe18/0x15c0
[  187.279057]  [<000001cbdd3ed62c>] lock_acquire.part.0+0xec/0x260
[  187.279059]  [<000001cbdd3ed84c>] lock_acquire+0xac/0x200
[  187.279061]  [<000001cbde401528>] _raw_spin_lock+0x58/0xb0
[  187.279063]  [<000001cb5dc5e734>] __deliver_machine_check+0x44/0x1a0 [kvm]
[  187.279082]  [<000001cb5dc6057e>] kvm_s390_deliver_pending_interrupts+0x7e/0x990 [kvm]
[  187.279099]  [<000001cb5dc49934>] vcpu_pre_run+0x74/0x2d0 [kvm]
[  187.279117]  [<000001cb5dc558e8>] __vcpu_run+0xa8/0x4f0 [kvm]
[  187.279134]  [<000001cb5dc56400>] kvm_arch_vcpu_ioctl_run+0x140/0x320 [kvm]
[  187.279152]  [<000001cb5dc35cc2>] kvm_vcpu_ioctl+0x142/0x9b0 [kvm]
[  187.279167]  [<000001cbdd7d0bda>] __s390x_sys_ioctl+0xea/0x120
[  187.279171]  [<000001cbde3ef868>] __do_syscall+0x168/0x750
[  187.279173]  [<000001cbde402d1a>] system_call+0x72/0x90
[  187.279175] INFO: lockdep is turned off.
Quoted 2 lines

Peter, I just added you to cc, so you can correct me if I'm entirely wrong.

maintainer
#11 lore raw

Re: [PATCH v5 3/4] KVM: s390: Change the fi->lock to a raw_spinlock for RT case

Quoted 12 lines · Douglas Freimuth
Quoted 4 lines

Rationale: even though it is not relevant for s390, we also change common code; and by ignoring PROVE_RAW_LOCK_NESTING we might cause problems for other architectures by introducing incorrect nesting of locks in common code. So yes, your thinking is correct.

Heiko, to be complete, I went through the exercise of enabling PROVE_RAW_LOCK_NESTING. I created a small hack to generate a __deliver_machine_check to trap the nested locking issue. The requested splat is below. Here the floating interrupt lock is a raw_spin_lock and the nested call to local interrupt lock is a spin_lock thus the nesting issue. No other nesting issues were found.

Quoted 5 lines

Now we need to arrive at, do we keep the raw_spin_locks to cover the possibility of future RT support or common code? In that case I also make the li->lock a raw_spin_lock. OR should I drop this raw_spin_lock patch and back out any other raw_spin_locks since we dont currently support RT on s390? And end either choice by testing again with PROVE_RAW_LOCK_NESTING.

Doug, we are going to enable PROVE_RAW_LOCK_NESTING in our debug_defconfig for the reasons I tried to outline above.

Or in other words: you need to convert li->lock too, since we want our code in a way that it doesn't trigger any lockdep splats, regardless if s390 will or will not support RT.

#12 lore raw

PATCH v5 4/4 KVM: s390: Introducing kvm_arch_set_irq_inatomic fast inject

In reply to Douglas Freimuth

S390 needs a fast path for irq injection, and along those lines we introduce kvm_arch_set_irq_inatomic. Instead of placing all interrupts on the global work queue as it does today, this patch provides a fast path for irq injection.

The inatomic fast path cannot lose control since it is running with interrupts disabled. This meant making the following changes that exist on the slow path today. First, the adapter_indicators page needs to be mapped since it is accessed with interrupts disabled, so we added map/unmap functions. Second, access to shared resources between the fast and slow paths needed to be changed from mutex and semaphores to raw_spin_lock's. Finally, the memory allocation on the slow path utilizes GFP_KERNEL_ACCOUNT but we had to implement the fast path with GFP_ATOMIC allocation. Each of these enhancements were required to prevent blocking on the fast inject path.

Fencing of Fast Inject in Secure Execution environments is enabled in the patch series by not mapping adapter indicator pages. In Secure Execution environments the path of execution available before this patch is followed.

Statistical counters have been added to enable analysis of irq injection on the fast path and slow path including io_390_inatomic, io_flic_inject_airq, io_set_adapter_int and io_390_inatomic_adapter_masked.

---
 arch/s390/include/asm/kvm_host.h |   6 +-
 arch/s390/kvm/interrupt.c        | 163 +++++++++++++++++++++++++++----
 arch/s390/kvm/kvm-s390.c         |  21 +++-
 arch/s390/kvm/kvm-s390.h         |   3 +-
 4 files changed, 170 insertions(+), 23 deletions(-)
arch/s390/include/asm/kvm_host.h +5 -1
index 9dd8a4986592..b485dee4c766 100644
@@ -359,7 +359,7 @@ struct kvm_s390_float_interrupt {
 	struct kvm_s390_mchk_info mchk;
 	struct kvm_s390_ext_info srv_signal;
 	int last_sleep_cpu;
-	struct mutex ais_lock;
+	raw_spinlock_t ais_lock;
 	u8 simm;
 	u8 nimm;
 };
@@ -450,6 +450,10 @@ struct kvm_vm_stat {
 	u64 inject_io;
 	u64 io_390_adapter_map;
 	u64 io_390_adapter_unmap;
+	u64 io_390_inatomic;
+	u64 io_flic_inject_airq;
+	u64 io_set_adapter_int;
+	u64 io_390_inatomic_adapter_masked;
 	u64 inject_float_mchk;
 	u64 inject_pfault_done;
 	u64 inject_service_signal;
arch/s390/kvm/interrupt.c +146 -17

278 lines not on this page · raw message

arch/s390/kvm/kvm-s390.c +17 -4
index d8011b6d6801..11b62fa8634f 100644
@@ -70,6 +70,10 @@ const struct kvm_stats_desc kvm_vm_stats_desc[] = {
 	STATS_DESC_COUNTER(VM, inject_io),
 	STATS_DESC_COUNTER(VM, io_390_adapter_map),
 	STATS_DESC_COUNTER(VM, io_390_adapter_unmap),
+	STATS_DESC_COUNTER(VM, io_390_inatomic),
+	STATS_DESC_COUNTER(VM, io_flic_inject_airq),
+	STATS_DESC_COUNTER(VM, io_set_adapter_int),
+	STATS_DESC_COUNTER(VM, io_390_inatomic_adapter_masked),
 	STATS_DESC_COUNTER(VM, inject_float_mchk),
 	STATS_DESC_COUNTER(VM, inject_pfault_done),
 	STATS_DESC_COUNTER(VM, inject_service_signal),
@@ -2856,6 +2860,7 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 	void __user *argp = (void __user *)arg;
 	struct kvm_device_attr attr;
 	int r;
+	struct kvm_s390_interrupt_info *inti;
 
 	switch (ioctl) {
 	case KVM_S390_INTERRUPT: {
@@ -2864,7 +2869,10 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 		r = -EFAULT;
 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
 			break;
-		r = kvm_s390_inject_vm(kvm, &s390int);
+		inti = kzalloc_obj(*inti, GFP_KERNEL_ACCOUNT);
+		if (!inti)
+			return -ENOMEM;
+		r = kvm_s390_inject_vm(kvm, &s390int, inti);
 		break;
 	}
 	case KVM_CREATE_IRQCHIP: {
@@ -3262,7 +3270,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 		mutex_unlock(&kvm->lock);
 	}
 
-	mutex_init(&kvm->arch.float_int.ais_lock);
+	raw_spin_lock_init(&kvm->arch.float_int.ais_lock);
 	raw_spin_lock_init(&kvm->arch.float_int.lock);
 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
@@ -4384,19 +4392,24 @@ int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clo
 }
 
 static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
-				      unsigned long token)
+				     unsigned long token)
 {
 	struct kvm_s390_interrupt inti;
 	struct kvm_s390_irq irq;
+	struct kvm_s390_interrupt_info *inti_mem = NULL;
 
 	if (start_token) {
 		irq.u.ext.ext_params2 = token;
 		irq.type = KVM_S390_INT_PFAULT_INIT;
 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
 	} else {
+		inti_mem = kzalloc_obj(*inti_mem, GFP_KERNEL_ACCOUNT);
+		if (WARN_ON_ONCE(!inti_mem))
+			return;
+
 		inti.type = KVM_S390_INT_PFAULT_DONE;
 		inti.parm64 = token;
-		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
+		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti, inti_mem));
 	}
 }
 
arch/s390/kvm/kvm-s390.h +2 -1
index 7ba885cb6bd1..6d2842fb71a3 100644
@@ -376,7 +376,8 @@ int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu);
 void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu);
 void kvm_s390_clear_float_irqs(struct kvm *kvm);
 int __must_check kvm_s390_inject_vm(struct kvm *kvm,
-				    struct kvm_s390_interrupt *s390int);
+				    struct kvm_s390_interrupt *s390int,
+				    struct kvm_s390_interrupt_info *inti);
 int __must_check kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
 				      struct kvm_s390_irq *irq);
 static inline int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
Signature · 1 line
2.52.0