All of lore.kernel.org
 help / color / mirror / Atom feed
From: Claudio Imbrenda <imbrenda@linux.ibm.com>
To: pbonzini@redhat.com
Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org,
	frankja@linux.ibm.com, borntraeger@linux.ibm.com,
	hca@linux.ibm.com, gor@linux.ibm.com, agordeev@linux.ibm.com,
	thuth@redhat.com, david@redhat.com,
	Nico Boehr <nrb@linux.ibm.com>
Subject: [GIT PULL 30/42] KVM: s390: pv: refactor s390_reset_acc
Date: Thu, 21 Jul 2022 18:12:50 +0200	[thread overview]
Message-ID: <20220721161302.156182-31-imbrenda@linux.ibm.com> (raw)
In-Reply-To: <20220721161302.156182-1-imbrenda@linux.ibm.com>

Refactor s390_reset_acc so that it can be reused in upcoming patches.

We don't want to hold all the locks used in a walk_page_range for too
long, and the destroy page UVC does take some time to complete.
Therefore we quickly gather the pages to destroy, and then destroy them
without holding all the locks.

The new refactored function optionally allows to return early without
completing if a fatal signal is pending (and return and appropriate
error code). Two wrappers are provided to call the new function.

Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Reviewed-by: Janosch Frank <frankja@linux.ibm.com>
Reviewed-by: Nico Boehr <nrb@linux.ibm.com>
Link: https://lore.kernel.org/r/20220628135619.32410-5-imbrenda@linux.ibm.com
Message-Id: <20220628135619.32410-5-imbrenda@linux.ibm.com>
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
---
 arch/s390/include/asm/gmap.h | 37 +++++++++++++-
 arch/s390/kvm/pv.c           | 12 ++++-
 arch/s390/mm/gmap.c          | 99 +++++++++++++++++++++++++-----------
 3 files changed, 116 insertions(+), 32 deletions(-)

diff --git a/arch/s390/include/asm/gmap.h b/arch/s390/include/asm/gmap.h
index f4073106e1f3..5cc46e0dde62 100644
--- a/arch/s390/include/asm/gmap.h
+++ b/arch/s390/include/asm/gmap.h
@@ -147,7 +147,42 @@ int gmap_mprotect_notify(struct gmap *, unsigned long start,
 void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long dirty_bitmap[4],
 			     unsigned long gaddr, unsigned long vmaddr);
 int gmap_mark_unmergeable(void);
-void s390_reset_acc(struct mm_struct *mm);
 void s390_unlist_old_asce(struct gmap *gmap);
 int s390_replace_asce(struct gmap *gmap);
+void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns);
+int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
+			    unsigned long end, bool interruptible);
+
+/**
+ * s390_uv_destroy_range - Destroy a range of pages in the given mm.
+ * @mm: the mm on which to operate on
+ * @start: the start of the range
+ * @end: the end of the range
+ *
+ * This function will call cond_sched, so it should not generate stalls, but
+ * it will otherwise only return when it completed.
+ */
+static inline void s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
+					 unsigned long end)
+{
+	(void)__s390_uv_destroy_range(mm, start, end, false);
+}
+
+/**
+ * s390_uv_destroy_range_interruptible - Destroy a range of pages in the
+ * given mm, but stop when a fatal signal is received.
+ * @mm: the mm on which to operate on
+ * @start: the start of the range
+ * @end: the end of the range
+ *
+ * This function will call cond_sched, so it should not generate stalls. If
+ * a fatal signal is received, it will return with -EINTR immediately,
+ * without finishing destroying the whole range. Upon successful
+ * completion, 0 is returned.
+ */
+static inline int s390_uv_destroy_range_interruptible(struct mm_struct *mm, unsigned long start,
+						      unsigned long end)
+{
+	return __s390_uv_destroy_range(mm, start, end, true);
+}
 #endif /* _ASM_S390_GMAP_H */
diff --git a/arch/s390/kvm/pv.c b/arch/s390/kvm/pv.c
index f1733812a263..a556db3912a1 100644
--- a/arch/s390/kvm/pv.c
+++ b/arch/s390/kvm/pv.c
@@ -13,6 +13,8 @@
 #include <asm/gmap.h>
 #include <asm/uv.h>
 #include <asm/mman.h>
+#include <linux/pagewalk.h>
+#include <linux/sched/mm.h>
 #include "kvm-s390.h"
 
 int kvm_s390_pv_destroy_cpu(struct kvm_vcpu *vcpu, u16 *rc, u16 *rrc)
@@ -153,8 +155,14 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
 {
 	int cc;
 
-	/* make all pages accessible before destroying the guest */
-	s390_reset_acc(kvm->mm);
+	/*
+	 * if the mm still has a mapping, make all its pages accessible
+	 * before destroying the guest
+	 */
+	if (mmget_not_zero(kvm->mm)) {
+		s390_uv_destroy_range(kvm->mm, 0, TASK_SIZE);
+		mmput(kvm->mm);
+	}
 
 	cc = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
 			   UVC_CMD_DESTROY_SEC_CONF, rc, rrc);
diff --git a/arch/s390/mm/gmap.c b/arch/s390/mm/gmap.c
index 85cab61d87a9..62758cb5872f 100644
--- a/arch/s390/mm/gmap.c
+++ b/arch/s390/mm/gmap.c
@@ -2697,44 +2697,85 @@ void s390_reset_cmma(struct mm_struct *mm)
 }
 EXPORT_SYMBOL_GPL(s390_reset_cmma);
 
-/*
- * make inaccessible pages accessible again
- */
-static int __s390_reset_acc(pte_t *ptep, unsigned long addr,
-			    unsigned long next, struct mm_walk *walk)
+#define GATHER_GET_PAGES 32
+
+struct reset_walk_state {
+	unsigned long next;
+	unsigned long count;
+	unsigned long pfns[GATHER_GET_PAGES];
+};
+
+static int s390_gather_pages(pte_t *ptep, unsigned long addr,
+			     unsigned long next, struct mm_walk *walk)
 {
+	struct reset_walk_state *p = walk->private;
 	pte_t pte = READ_ONCE(*ptep);
 
-	/* There is a reference through the mapping */
-	if (pte_present(pte))
-		WARN_ON_ONCE(uv_destroy_owned_page(pte_val(pte) & PAGE_MASK));
-
-	return 0;
+	if (pte_present(pte)) {
+		/* we have a reference from the mapping, take an extra one */
+		get_page(phys_to_page(pte_val(pte)));
+		p->pfns[p->count] = phys_to_pfn(pte_val(pte));
+		p->next = next;
+		p->count++;
+	}
+	return p->count >= GATHER_GET_PAGES;
 }
 
-static const struct mm_walk_ops reset_acc_walk_ops = {
-	.pte_entry		= __s390_reset_acc,
+static const struct mm_walk_ops gather_pages_ops = {
+	.pte_entry = s390_gather_pages,
 };
 
-#include <linux/sched/mm.h>
-void s390_reset_acc(struct mm_struct *mm)
+/*
+ * Call the Destroy secure page UVC on each page in the given array of PFNs.
+ * Each page needs to have an extra reference, which will be released here.
+ */
+void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns)
 {
-	if (!mm_is_protected(mm))
-		return;
-	/*
-	 * we might be called during
-	 * reset:                             we walk the pages and clear
-	 * close of all kvm file descriptors: we walk the pages and clear
-	 * exit of process on fd closure:     vma already gone, do nothing
-	 */
-	if (!mmget_not_zero(mm))
-		return;
-	mmap_read_lock(mm);
-	walk_page_range(mm, 0, TASK_SIZE, &reset_acc_walk_ops, NULL);
-	mmap_read_unlock(mm);
-	mmput(mm);
+	unsigned long i;
+
+	for (i = 0; i < count; i++) {
+		/* we always have an extra reference */
+		uv_destroy_owned_page(pfn_to_phys(pfns[i]));
+		/* get rid of the extra reference */
+		put_page(pfn_to_page(pfns[i]));
+		cond_resched();
+	}
+}
+EXPORT_SYMBOL_GPL(s390_uv_destroy_pfns);
+
+/**
+ * __s390_uv_destroy_range - Call the destroy secure page UVC on each page
+ * in the given range of the given address space.
+ * @mm: the mm to operate on
+ * @start: the start of the range
+ * @end: the end of the range
+ * @interruptible: if not 0, stop when a fatal signal is received
+ *
+ * Walk the given range of the given address space and call the destroy
+ * secure page UVC on each page. Optionally exit early if a fatal signal is
+ * pending.
+ *
+ * Return: 0 on success, -EINTR if the function stopped before completing
+ */
+int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
+			    unsigned long end, bool interruptible)
+{
+	struct reset_walk_state state = { .next = start };
+	int r = 1;
+
+	while (r > 0) {
+		state.count = 0;
+		mmap_read_lock(mm);
+		r = walk_page_range(mm, state.next, end, &gather_pages_ops, &state);
+		mmap_read_unlock(mm);
+		cond_resched();
+		s390_uv_destroy_pfns(state.count, state.pfns);
+		if (interruptible && fatal_signal_pending(current))
+			return -EINTR;
+	}
+	return 0;
 }
-EXPORT_SYMBOL_GPL(s390_reset_acc);
+EXPORT_SYMBOL_GPL(__s390_uv_destroy_range);
 
 /**
  * s390_unlist_old_asce - Remove the topmost level of page tables from the
-- 
2.36.1


  parent reply	other threads:[~2022-07-21 16:13 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-21 16:12 [GIT PULL 00/42] KVM: s390: PCI, CPU topology, PV features Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 01/42] s390/sclp: detect the zPCI load/store interpretation facility Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 02/42] s390/sclp: detect the AISII facility Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 03/42] s390/sclp: detect the AENI facility Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 04/42] s390/sclp: detect the AISI facility Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 05/42] s390/airq: pass more TPI info to airq handlers Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 06/42] s390/airq: allow for airq structure that uses an input vector Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 07/42] s390/pci: externalize the SIC operation controls and routine Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 08/42] s390/pci: stash associated GISA designation Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 09/42] s390/pci: stash dtsm and maxstbl Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 10/42] vfio/pci: introduce CONFIG_VFIO_PCI_ZDEV_KVM Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 11/42] KVM: s390: pci: add basic kvm_zdev structure Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 12/42] KVM: s390: pci: do initial setup for AEN interpretation Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 13/42] KVM: s390: pci: enable host forwarding of Adapter Event Notifications Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 14/42] KVM: s390: mechanism to enable guest zPCI Interpretation Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 15/42] KVM: s390: pci: provide routines for enabling/disabling interrupt forwarding Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 16/42] KVM: s390: pci: add routines to start/stop interpretive execution Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 17/42] vfio-pci/zdev: add open/close device hooks Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 18/42] vfio-pci/zdev: add function handle to clp base capability Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 19/42] vfio-pci/zdev: different maxstbl for interpreted devices Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 20/42] KVM: s390: add KVM_S390_ZPCI_OP to manage guest zPCI devices Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 21/42] MAINTAINERS: additional files related kvm s390 pci passthrough Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 22/42] KVM: s390: drop unexpected word 'and' in the comments Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 23/42] s390: Add attestation query information Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 24/42] KVM: s390/pci: fix include duplicates Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 25/42] Documentation: kvm: extend KVM_S390_ZPCI_OP subheading underline Claudio Imbrenda
2022-07-22  1:40   ` Bagas Sanjaya
2022-07-21 16:12 ` [GIT PULL 26/42] KVM: s390: Add facility 197 to the allow list Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 27/42] KVM: s390: pv: leak the topmost page table when destroy fails Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 28/42] KVM: s390: pv: handle secure storage violations for protected guests Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 29/42] KVM: s390: pv: handle secure storage exceptions for normal guests Claudio Imbrenda
2022-07-21 16:12 ` Claudio Imbrenda [this message]
2022-07-21 16:12 ` [GIT PULL 31/42] KVM: s390: pv: usage counter instead of flag Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 32/42] KVM: s390: pv: add export before import Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 33/42] KVM: s390: pv: clear the state without memset Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 34/42] KVM: s390: pv: Add kvm_s390_cpus_from_pv to kvm-s390.h and add documentation Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 35/42] KVM: s390: pv: add mmu_notifier Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 36/42] s390/mm: KVM: pv: when tearing down, try to destroy protected pages Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 37/42] KVM: s390: pv: refactoring of kvm_s390_pv_deinit_vm Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 38/42] KVM: s390: pv: destroy the configuration before its memory Claudio Imbrenda
2022-07-21 16:12 ` [GIT PULL 39/42] KVM: s390: pv: don't present the ecall interrupt twice Claudio Imbrenda
2022-07-21 16:13 ` [GIT PULL 40/42] KVM: s390: Cleanup ipte lock access and SIIF facility checks Claudio Imbrenda
2022-07-21 16:13 ` [GIT PULL 41/42] KVM: s390: guest support for topology function Claudio Imbrenda
2022-07-21 16:13 ` [GIT PULL 42/42] KVM: s390: resetting the Topology-Change-Report Claudio Imbrenda
2022-07-22  7:17 ` [GIT PULL 00/42] KVM: s390: PCI, CPU topology, PV features Paolo Bonzini
2022-07-22 10:52   ` Christian Borntraeger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220721161302.156182-31-imbrenda@linux.ibm.com \
    --to=imbrenda@linux.ibm.com \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=david@redhat.com \
    --cc=frankja@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=nrb@linux.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.