All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oleksandr Tyshchenko <olekstysh@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>,
	Stefano Stabellini <sstabellini@kernel.org>,
	Julien Grall <julien@xen.org>,
	Volodymyr Babchuk <Volodymyr_Babchuk@epam.com>,
	Julien Grall <julien.grall@arm.com>
Subject: [PATCH V5 22/22] xen/arm: Add mapcache invalidation handling
Date: Mon, 25 Jan 2021 21:08:29 +0200	[thread overview]
Message-ID: <1611601709-28361-23-git-send-email-olekstysh@gmail.com> (raw)
In-Reply-To: <1611601709-28361-1-git-send-email-olekstysh@gmail.com>

From: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>

We need to send mapcache invalidation request to qemu/demu everytime
the page gets removed from a guest.

At the moment, the Arm code doesn't explicitely remove the existing
mapping before inserting the new mapping. Instead, this is done
implicitely by __p2m_set_entry().

So we need to recognize a case when old entry is a RAM page *and*
the new MFN is different in order to set the corresponding flag.
The most suitable place to do this is p2m_free_entry(), there
we can find the correct leaf type. The invalidation request
will be sent in do_trap_hypercall() later on.

Taking into the account the following the do_trap_hypercall()
is the best place to send invalidation request:
 - The only way a guest can modify its P2M on Arm is via an hypercall
 - When sending the invalidation request, the vCPU will be blocked
   until all the IOREQ servers have acknowledged the invalidation

Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
CC: Julien Grall <julien.grall@arm.com>
Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>
[On Arm only]
Tested-by: Wei Chen <Wei.Chen@arm.com>

---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"

***
Please note, this patch depends on the following which is
on review:
https://patchwork.kernel.org/patch/11803383/

This patch is on par with x86 code (whether it is buggy or not).
If there is a need to improve/harden something, this can be done on
a follow-up.
***

Changes V1 -> V2:
   - new patch, some changes were derived from (+ new explanation):
     xen/ioreq: Make x86's invalidate qemu mapcache handling common
   - put setting of the flag into __p2m_set_entry()
   - clarify the conditions when the flag should be set
   - use domain_has_ioreq_server()
   - update do_trap_hypercall() by adding local variable

Changes V2 -> V3:
   - update patch description
   - move check to p2m_free_entry()
   - add a comment
   - use "curr" instead of "v" in do_trap_hypercall()

Changes V3 -> V4:
   - update patch description
   - re-order check in p2m_free_entry() to call domain_has_ioreq_server()
     only if p2m->domain == current->domain
   - add a comment in do_trap_hypercall()

Changes V4 -> V5:
   - add Stefano's R-b
   - update comment in do_trap_hypercall()
---
---
 xen/arch/arm/p2m.c   | 25 +++++++++++++++++--------
 xen/arch/arm/traps.c | 20 +++++++++++++++++---
 2 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/xen/arch/arm/p2m.c b/xen/arch/arm/p2m.c
index d41c4fa..26acb95d 100644
--- a/xen/arch/arm/p2m.c
+++ b/xen/arch/arm/p2m.c
@@ -1,6 +1,7 @@
 #include <xen/cpu.h>
 #include <xen/domain_page.h>
 #include <xen/iocap.h>
+#include <xen/ioreq.h>
 #include <xen/lib.h>
 #include <xen/sched.h>
 #include <xen/softirq.h>
@@ -749,17 +750,25 @@ static void p2m_free_entry(struct p2m_domain *p2m,
     if ( !p2m_is_valid(entry) )
         return;
 
-    /* Nothing to do but updating the stats if the entry is a super-page. */
-    if ( p2m_is_superpage(entry, level) )
+    if ( p2m_is_superpage(entry, level) || (level == 3) )
     {
-        p2m->stats.mappings[level]--;
-        return;
-    }
+#ifdef CONFIG_IOREQ_SERVER
+        /*
+         * If this gets called (non-recursively) then either the entry
+         * was replaced by an entry with a different base (valid case) or
+         * the shattering of a superpage was failed (error case).
+         * So, at worst, the spurious mapcache invalidation might be sent.
+         */
+        if ( (p2m->domain == current->domain) &&
+              domain_has_ioreq_server(p2m->domain) &&
+              p2m_is_ram(entry.p2m.type) )
+            p2m->domain->mapcache_invalidate = true;
+#endif
 
-    if ( level == 3 )
-    {
         p2m->stats.mappings[level]--;
-        p2m_put_l3_page(entry);
+        /* Nothing to do if the entry is a super-page. */
+        if ( level == 3 )
+            p2m_put_l3_page(entry);
         return;
     }
 
diff --git a/xen/arch/arm/traps.c b/xen/arch/arm/traps.c
index 4cdd343..64b740b 100644
--- a/xen/arch/arm/traps.c
+++ b/xen/arch/arm/traps.c
@@ -1443,6 +1443,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,
                               const union hsr hsr)
 {
     arm_hypercall_fn_t call = NULL;
+    struct vcpu *curr = current;
 
     BUILD_BUG_ON(NR_hypercalls < ARRAY_SIZE(arm_hypercall_table) );
 
@@ -1459,7 +1460,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,
         return;
     }
 
-    current->hcall_preempted = false;
+    curr->hcall_preempted = false;
 
     perfc_incra(hypercalls, *nr);
     call = arm_hypercall_table[*nr].fn;
@@ -1472,7 +1473,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,
     HYPERCALL_RESULT_REG(regs) = call(HYPERCALL_ARGS(regs));
 
 #ifndef NDEBUG
-    if ( !current->hcall_preempted )
+    if ( !curr->hcall_preempted )
     {
         /* Deliberately corrupt parameter regs used by this hypercall. */
         switch ( arm_hypercall_table[*nr].nr_args ) {
@@ -1489,8 +1490,21 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,
 #endif
 
     /* Ensure the hypercall trap instruction is re-executed. */
-    if ( current->hcall_preempted )
+    if ( curr->hcall_preempted )
         regs->pc -= 4;  /* re-execute 'hvc #XEN_HYPERCALL_TAG' */
+
+#ifdef CONFIG_IOREQ_SERVER
+    /*
+     * We call ioreq_signal_mapcache_invalidate from do_trap_hypercall()
+     * because the only way a guest can modify its P2M on Arm is via an
+     * hypercall.
+     * Note that sending the invalidation request causes the vCPU to block
+     * until all the IOREQ servers have acknowledged the invalidation.
+     */
+    if ( unlikely(curr->domain->mapcache_invalidate) &&
+         test_and_clear_bool(curr->domain->mapcache_invalidate) )
+        ioreq_signal_mapcache_invalidate();
+#endif
 }
 
 void arch_hypercall_tasklet_result(struct vcpu *v, long res)
-- 
2.7.4



  parent reply	other threads:[~2021-01-25 19:21 UTC|newest]

Thread overview: 93+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-25 19:08 [PATCH V5 00/22] IOREQ feature (+ virtio-mmio) on Arm Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 01/22] x86/ioreq: Prepare IOREQ feature for making it common Oleksandr Tyshchenko
2021-01-27 16:48   ` Jan Beulich
2021-01-25 19:08 ` [PATCH V5 02/22] x86/ioreq: Add IOREQ_STATUS_* #define-s and update code for moving Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 03/22] x86/ioreq: Provide out-of-line wrapper for the handle_mmio() Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 04/22] xen/ioreq: Make x86's IOREQ feature common Oleksandr Tyshchenko
2021-01-25 23:13   ` Julien Grall
2021-01-25 23:40     ` Oleksandr
2021-01-27 18:43       ` Julien Grall
2021-01-27 20:22         ` Oleksandr
2021-01-27 20:46           ` Stefano Stabellini
2021-01-28 11:01             ` Oleksandr
2021-01-28 11:21               ` Jan Beulich
2021-01-28 12:49                 ` Oleksandr
2021-01-28 17:13                   ` Stefano Stabellini
2021-01-27 16:58   ` Jan Beulich
2021-01-27 20:14     ` Oleksandr
2021-01-28  8:06       ` Jan Beulich
2021-01-28 11:16         ` Oleksandr
2021-01-28 11:24           ` Jan Beulich
2021-01-25 19:08 ` [PATCH V5 05/22] xen/ioreq: Make x86's hvm_ioreq_needs_completion() common Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 06/22] xen/ioreq: Make x86's hvm_mmio_first(last)_byte() common Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 07/22] xen/ioreq: Make x86's hvm_ioreq_(page/vcpu/server) structs common Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 08/22] xen/ioreq: Move x86's ioreq_server to struct domain Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 09/22] xen/ioreq: Make x86's IOREQ related dm-op handling common Oleksandr Tyshchenko
2021-01-26 13:31   ` Paul Durrant
2021-01-28 10:52   ` Jan Beulich
2021-01-28 12:06     ` Oleksandr
2021-01-28 13:18       ` Jan Beulich
2021-01-28 20:01         ` Oleksandr
2021-01-25 19:08 ` [PATCH V5 10/22] xen/ioreq: Move x86's io_completion/io_req fields to struct vcpu Oleksandr Tyshchenko
2021-01-28 13:41   ` Julien Grall
2021-01-28 13:53     ` Jan Beulich
2021-01-28 14:21       ` Julien Grall
2021-01-28 14:36         ` Jan Beulich
2021-01-28 14:49           ` Andrew Cooper
2021-01-28 14:51           ` Ian Jackson
2021-01-28 14:54             ` Jan Beulich
2021-01-28 16:15               ` Andrew Cooper
2021-01-28 15:01           ` Julien Grall
2021-01-28 14:05     ` Oleksandr
2021-01-25 19:08 ` [PATCH V5 11/22] xen/mm: Make x86's XENMEM_resource_ioreq_server handling common Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 12/22] xen/ioreq: Remove "hvm" prefixes from involved function names Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 13/22] xen/ioreq: Use guest_cmpxchg64() instead of cmpxchg() Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 14/22] arm/ioreq: Introduce arch specific bits for IOREQ/DM features Oleksandr Tyshchenko
2021-01-25 20:19   ` Stefano Stabellini
2021-01-26  9:15   ` Jan Beulich
2021-01-27  9:54     ` Oleksandr
2021-01-27 10:15       ` Jan Beulich
2021-01-27 17:01     ` Julien Grall
2021-01-27 17:04       ` Jan Beulich
2021-01-27 18:33   ` Julien Grall
2021-01-27 19:20     ` Oleksandr
2021-01-28  9:40       ` Julien Grall
2021-01-28 11:33         ` Oleksandr
2021-01-28 13:39           ` Oleksandr
2021-01-28 14:29             ` Oleksandr
2021-01-28 14:41               ` Julien Grall
2021-01-28 14:52                 ` Oleksandr
2021-01-28 15:08                   ` Julien Grall
2021-01-28 17:50                     ` Oleksandr
2021-01-25 19:08 ` [PATCH V5 15/22] xen/arm: Call vcpu_ioreq_handle_completion() in check_for_vcpu_work() Oleksandr Tyshchenko
2021-01-27 14:49   ` Julien Grall
2021-01-27 15:56     ` Oleksandr
2021-01-27 20:34       ` Stefano Stabellini
2021-01-25 19:08 ` [PATCH V5 16/22] xen/mm: Handle properly reference in set_foreign_p2m_entry() on Arm Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 17/22] xen/ioreq: Introduce domain_has_ioreq_server() Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 18/22] xen/dm: Introduce xendevicemodel_set_irq_level DM op Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 19/22] xen/arm: io: Abstract sign-extension Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 20/22] xen/arm: io: Harden sign extension check Oleksandr Tyshchenko
2021-01-25 19:08 ` [PATCH V5 21/22] xen/ioreq: Make x86's send_invalidate_req() common Oleksandr Tyshchenko
2021-01-25 19:08 ` Oleksandr Tyshchenko [this message]
2021-01-28  9:55   ` [PATCH V5 22/22] xen/arm: Add mapcache invalidation handling Julien Grall
2021-01-28 13:12     ` Oleksandr
2021-01-27 16:43 ` [PATCH V5 00/22] IOREQ feature (+ virtio-mmio) on Arm Julien Grall
2021-01-27 16:50   ` Oleksandr
2021-01-27 17:33     ` Julien Grall
2021-01-27 17:37       ` Oleksandr
2021-01-27 17:42         ` Julien Grall
2021-01-27 17:45           ` Oleksandr
2021-01-28 14:37             ` Oleksandr
2021-01-28 15:14               ` Julien Grall
2021-01-28 17:55                 ` Oleksandr
2021-01-28 16:11 ` Julien Grall
2021-01-28 17:24   ` Stefano Stabellini
2021-01-28 17:44     ` Julien Grall
2021-01-28 18:10       ` Andrew Cooper
2021-01-28 18:16         ` Julien Grall
2021-01-28 18:21           ` Julien Grall
2021-01-28 20:10           ` Andrew Cooper
2021-01-28 21:19             ` Julien Grall
2021-01-28 19:44         ` Oleksandr
2021-01-29  1:15           ` Oleksandr

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=1611601709-28361-23-git-send-email-olekstysh@gmail.com \
    --to=olekstysh@gmail.com \
    --cc=Volodymyr_Babchuk@epam.com \
    --cc=julien.grall@arm.com \
    --cc=julien@xen.org \
    --cc=oleksandr_tyshchenko@epam.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /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.