Skip to contentSkip to conversation

xen/arm: introduce GNTTABOP_cache_flush

PATCH Superseded Stefano Stabellini 5 participants 23 messages Oct 8, 2014 – Oct 10, 2014

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v3 cover xen/arm: introduce GNTTABOP_cache_flush

Hi all,
this patch series introduces a new hypercall to perform cache maintenance operations on behalf of the guest. It is useful for dom0 to be able to cache flush pages involved in a dma operation with non-coherent devices.

It also removes XENFEAT_grant_map_identity as the feature is no longer
necessary: it was used to achieve the same goal but the guest can now use the hypercall instead. Keeping the flag would also have a significant performance impact as a new p2m mapping gets created and then destroyed for every grant that is mapped and unmapped in dom0.

Changes in v3:
- introduce two different max_nr_dom0/domU_grant_frames parameters;
- x86: introduce more cache maintenance operations;
- reduce the time the grant_table lock is held;
- fix warning message;
- s/EFAULT/EPERM;
- s/llx/PRIx64;
- check offset and size independetly before checking their sum;
- rcu_lock_current_domain cannot fail;
- s/ENOSYS/EOPNOTSUPP;
- use clean_and_invalidate_xen_dcache_va_range to do both operations at once;
- fold grant_map_exists in this patch;
- support "count" argument;
- make correspondent changes to compat/grant_table.c;
- introduce GNTTAB_CACHE_SOURCE_GREF to select the type of input in the union;
- rename size field to length;
- make length and offset uint16_t;
- only take spin_lock if d != owner.

Changes in v2:
- make grant_map_exists static;
- remove the spin_lock in grant_map_exists;
- move the hypercall to GNTTABOP;
- do not check for mfn_to_page errors in GNTTABOP_cache_flush;
- take a reference to the page in GNTTABOP_cache_flush;
- replace printk with gdprintk in GNTTABOP_cache_flush;
- split long line in GNTTABOP_cache_flush;
- remove out label in GNTTABOP_cache_flush;
- move rcu_lock_current_domain down before the loop in GNTTABOP_cache_flush;
- take a spin_lock before calling grant_map_exists in GNTTABOP_cache_flush.

Stefano Stabellini (5):
xen: introduce two different max_nr_dom0/domU_grant_frames parameters xen/arm: introduce invalidate_xen_dcache_va_range xen/x86: introduce more cache maintenance operations xen/arm: introduce GNTTABOP_cache_flush
Revert "xen/arm: introduce XENFEAT_grant_map_identity"

 docs/misc/xen-command-line.markdown |    4 +-
 xen/arch/arm/domain.c               |    2 +-
 xen/arch/arm/mm.c                   |    2 +-
 xen/arch/x86/mm.c                   |    2 +-
 xen/common/compat/grant_table.c     |   16 ++-
 xen/common/grant_table.c            |  237 +++++++++++++++++++++++++++--------
 xen/common/kernel.c                 |    2 -
 xen/drivers/passthrough/arm/smmu.c  |   33 +++++
 xen/include/asm-arm/arm32/page.h    |    3 +
 xen/include/asm-arm/arm64/page.h    |    3 +
 xen/include/asm-arm/grant_table.h   |    5 +-
 xen/include/asm-arm/page.h          |   30 +++++
 xen/include/asm-x86/page.h          |    4 +
 xen/include/public/features.h       |    4 +-
 xen/include/public/grant_table.h    |   20 +++
 xen/include/xen/grant_table.h       |    6 +-
 xen/include/xlat.lst                |    1 +
 17 files changed, 305 insertions(+), 69 deletions(-)
#1 lore raw

PATCH v3 1/5 xen: introduce two different max_nr_dom0/domU_grant_frames parameters

Remove max_nr_grant_frames, both variable and preprocessor symbol. Introduce two global variables max_nr_dom0_grant_frames and max_nr_domU_grant_frames, separately configurable via the Xen command line option "gnttab_max_nr_frames".

---
 docs/misc/xen-command-line.markdown |    4 +-
 xen/arch/arm/domain.c               |    2 +-
 xen/arch/arm/mm.c                   |    2 +-
 xen/arch/x86/mm.c                   |    2 +-
 xen/common/compat/grant_table.c     |    8 ++--
 xen/common/grant_table.c            |   72 +++++++++++++++++++++--------------
 xen/include/asm-arm/grant_table.h   |    2 +-
 xen/include/xen/grant_table.h       |    6 +--
 8 files changed, 58 insertions(+), 40 deletions(-)
docs/misc/xen-command-line.markdown +3 -1
index 389701a..fa10f6c 100644
@@ -609,7 +609,9 @@ does not provide VM\_ENTRY\_LOAD\_GUEST\_PAT.
 Specify the serial parameters for the GDB stub.
 
 ### gnttab\_max\_nr\_frames
-> `= <integer>`
+> `= [<domU number>][,<dom0 number>]`
+
+> Default: `32,32`
 
 Specify the maximum number of frames per grant table operation.
 
xen/arch/arm/domain.c +1 -1
index 2b53931..8d60472 100644
@@ -409,7 +409,7 @@ struct domain *alloc_domain_struct(void)
         return NULL;
 
     clear_page(d);
-    d->arch.grant_table_gpfn = xzalloc_array(xen_pfn_t, max_nr_grant_frames);
+    d->arch.grant_table_gpfn = xzalloc_array(xen_pfn_t, max_nr_domU_grant_frames);
     return d;
 }
 
xen/arch/arm/mm.c +1 -1
index c5b48ef..b65c725 100644
@@ -1040,7 +1040,7 @@ int xenmem_add_to_physmap_one(
         else
         {
             if ( (idx >= nr_grant_frames(d->grant_table)) &&
-                    (idx < max_nr_grant_frames) )
+                    (idx < max_nr_grant_frames(d)) )
                 gnttab_grow_table(d, idx + 1);
 
             if ( idx < nr_grant_frames(d->grant_table) )
xen/arch/x86/mm.c +1 -1
index 5b3f06f..f72b23f 100644
@@ -4539,7 +4539,7 @@ int xenmem_add_to_physmap_one(
             else
             {
                 if ( (idx >= nr_grant_frames(d->grant_table)) &&
-                     (idx < max_nr_grant_frames) )
+                     (idx < max_nr_grant_frames(d)) )
                     gnttab_grow_table(d, idx + 1);
 
                 if ( idx < nr_grant_frames(d->grant_table) )
xen/common/compat/grant_table.c +4 -4
index 7ebbbc1..6c00b09 100644
@@ -146,11 +146,11 @@ int compat_grant_table_op(unsigned int cmd,
                 unsigned int max_frame_list_size_in_page =
                     (COMPAT_ARG_XLAT_SIZE - sizeof(*nat.setup)) /
                     sizeof(*nat.setup->frame_list.p);
-                if ( max_frame_list_size_in_page < max_nr_grant_frames )
+                if ( max_frame_list_size_in_page < max_nr_grant_frames(current->domain) )
                 {
                     gdprintk(XENLOG_WARNING,
                              "max_nr_grant_frames is too large (%u,%u)\n",
-                             max_nr_grant_frames, max_frame_list_size_in_page);
+                             max_nr_grant_frames(current->domain), max_frame_list_size_in_page);
                     rc = -EINVAL;
                 }
                 else
@@ -284,11 +284,11 @@ int compat_grant_table_op(unsigned int cmd,
                 break;
             }
             if ( max_frame_list_size_in_pages <
-                 grant_to_status_frames(max_nr_grant_frames) )
+                 grant_to_status_frames(max_nr_grant_frames(current->domain)) )
             {
                 gdprintk(XENLOG_WARNING,
                          "grant_to_status_frames(max_nr_grant_frames) is too large (%u,%u)\n",
-                         grant_to_status_frames(max_nr_grant_frames),
+                         grant_to_status_frames(max_nr_grant_frames(current->domain)),
                          max_frame_list_size_in_pages);
                 rc = -EINVAL;
                 break;
xen/common/grant_table.c +44 -28

180 lines not on this page · raw message

xen/include/asm-arm/grant_table.h +1 -1
index 47147ce..e81c973 100644
@@ -31,7 +31,7 @@ static inline int replace_grant_supported(void)
 
 #define gnttab_shared_gmfn(d, t, i)                                      \
     ( ((i >= nr_grant_frames(d->grant_table)) &&                         \
-     (i < max_nr_grant_frames)) ? 0 : (d->arch.grant_table_gpfn[i]))
+     (i < max_nr_grant_frames(d))) ? 0 : (d->arch.grant_table_gpfn[i]))
 
 #define gnttab_need_iommu_mapping(d)           (is_domain_direct_mapped(d))
 
xen/include/xen/grant_table.h +3 -3
index 5941191..7269d90 100644
@@ -49,10 +49,10 @@
 /* Default maximum size of a grant table. [POLICY] */
 #define DEFAULT_MAX_NR_GRANT_FRAMES   32
 #endif
-#ifndef max_nr_grant_frames /* to allow arch to override */
 /* The maximum size of a grant table. */
-extern unsigned int max_nr_grant_frames;
-#endif
+extern unsigned int max_nr_dom0_grant_frames;
+extern unsigned int max_nr_domU_grant_frames;
+unsigned int max_nr_grant_frames(struct domain *d);
 
 /*
  * Tracks a mapping of another domain's grant reference. Each domain has a
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH v3 1/5] xen: introduce two different max_nr_dom0/domU_grant_frames parameters

Quoted 4 lines · Stefano Stabellini

Remove max_nr_grant_frames, both variable and preprocessor symbol. Introduce two global variables max_nr_dom0_grant_frames and max_nr_domU_grant_frames, separately configurable via the Xen command line option "gnttab_max_nr_frames".

Why? I can't think of a sensible use case for this and you don't list one. dom0 doesn't really use grants so its current number grant frames is usually 1 so it doesn't seem useful to restrict it differently.

David

#3 lore raw

Re: [PATCH v3 1/5] xen: introduce two different max_nr_dom0/domU_grant_frames parameters

Quoted 9 lines · David Vrabel
Quoted 4 lines · Stefano Stabellini

Remove max_nr_grant_frames, both variable and preprocessor symbol. Introduce two global variables max_nr_dom0_grant_frames and max_nr_domU_grant_frames, separately configurable via the Xen command line option "gnttab_max_nr_frames".

Why? I can't think of a sensible use case for this and you don't list one. dom0 doesn't really use grants so its current number grant frames is usually 1 so it doesn't seem useful to restrict it differently.

See the recent discussion with Jan:

http://marc.info/?l=xen-devel&m=141260825209210

#4 lore raw

Re: [PATCH v3 1/5] xen: introduce two different max_nr_dom0/domU_grant_frames parameters

Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · David Vrabel
Quoted 4 lines · Stefano Stabellini

Remove max_nr_grant_frames, both variable and preprocessor symbol. Introduce two global variables max_nr_dom0_grant_frames and max_nr_domU_grant_frames, separately configurable via the Xen command line option "gnttab_max_nr_frames".

Quoted 7 lines
Quoted 3 lines

Why? I can't think of a sensible use case for this and you don't list one. dom0 doesn't really use grants so its current number grant frames is usually 1 so it doesn't seem useful to restrict it differently.

See the recent discussion with Jan:

http://marc.info/?l=xen-devel&m=141260825209210

I still don't see a use case. Either:

a) dom0 is the driver domain and doesn't need a large number of grants.

or

b) dom0 has a frontend driver, then the driver domain will have to scan dom0's grant table thus dom0's grant table must be sized the same as domUs.

David

#5 lore raw

Re: [PATCH v3 1/5] xen: introduce two different max_nr_dom0/domU_grant_frames parameters

Quoted 16 lines · David Vrabel
Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · David Vrabel
Quoted 4 lines · Stefano Stabellini

Remove max_nr_grant_frames, both variable and preprocessor symbol. Introduce two global variables max_nr_dom0_grant_frames and max_nr_domU_grant_frames, separately configurable via the Xen command line option "gnttab_max_nr_frames".

Quoted 7 lines
Quoted 3 lines

Why? I can't think of a sensible use case for this and you don't list one. dom0 doesn't really use grants so its current number grant frames is usually 1 so it doesn't seem useful to restrict it differently.

See the recent discussion with Jan:

http://marc.info/?l=xen-devel&m=141260825209210

Quoted 8 lines

I still don't see a use case. Either:

a) dom0 is the driver domain and doesn't need a large number of grants.

or

b) dom0 has a frontend driver, then the driver domain will have to scan dom0's grant table thus dom0's grant table must be sized the same as domUs.

I don't have an opinion on this, so I'll leave it up to you and Jan to come to an agreement.

#6 lore raw

Re: [PATCH v3 1/5] xen: introduce two different max_nr_dom0/domU_grant_frames parameters

Quoted 21 lines · Stefano Stabellini
Quoted 20 lines · David Vrabel
Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · David Vrabel
Quoted 4 lines · Stefano Stabellini

Remove max_nr_grant_frames, both variable and preprocessor symbol. Introduce two global variables max_nr_dom0_grant_frames and max_nr_domU_grant_frames, separately configurable via the Xen command line option "gnttab_max_nr_frames".

Quoted 7 lines
Quoted 3 lines

Why? I can't think of a sensible use case for this and you don't list one. dom0 doesn't really use grants so its current number grant frames is usually 1 so it doesn't seem useful to restrict it differently.

See the recent discussion with Jan:

http://marc.info/?l=xen-devel&m=141260825209210

I still don't see a use case. Either:

a) dom0 is the driver domain and doesn't need a large number of grants.

Quoted 7 lines
Quoted 4 lines

or

b) dom0 has a frontend driver, then the driver domain will have to scan dom0's grant table thus dom0's grant table must be sized the same as domUs.

I don't have an opinion on this, so I'll leave it up to you and Jan to come to an agreement.

I don't feel strongly on this so I'll defer to Jan.

David

#7 lore raw

Re: [PATCH v3 1/5] xen: introduce two different max_nr_dom0/domU_grant_frames parameters

In reply to David Vrabel
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 08.10.14 at 18:42, <[email protected]> wrote:
Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · David Vrabel
Quoted 4 lines · Stefano Stabellini

Remove max_nr_grant_frames, both variable and preprocessor symbol. Introduce two global variables max_nr_dom0_grant_frames and max_nr_domU_grant_frames, separately configurable via the Xen command line option "gnttab_max_nr_frames".

Quoted 7 lines
Quoted 3 lines

Why? I can't think of a sensible use case for this and you don't list one. dom0 doesn't really use grants so its current number grant frames is usually 1 so it doesn't seem useful to restrict it differently.

See the recent discussion with Jan:

http://marc.info/?l=xen-devel&m=141260825209210

Quoted 8 lines

I still don't see a use case. Either:

a) dom0 is the driver domain and doesn't need a large number of grants.

or

b) dom0 has a frontend driver, then the driver domain will have to scan dom0's grant table thus dom0's grant table must be sized the same as domUs.

Looks like you missed that max_nr_maptrack_frames(), which I think you agree is needed in Dom0, also depends on max_nr_grant_frames. Question then of course is whether the distinction should be made at that: Allow controlling grant frames and maptrack frames separately, rather than Dom0 and DomU.

Jan

#8 lore raw

Re: [PATCH v3 1/5] xen: introduce two different max_nr_dom0/domU_grant_frames parameters

Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 08.10.14 at 18:42, <[email protected]> wrote:
Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · David Vrabel
Quoted 4 lines · Stefano Stabellini

Remove max_nr_grant_frames, both variable and preprocessor symbol. Introduce two global variables max_nr_dom0_grant_frames and max_nr_domU_grant_frames, separately configurable via the Xen command line option "gnttab_max_nr_frames".

Quoted 7 lines
Quoted 3 lines

Why? I can't think of a sensible use case for this and you don't list one. dom0 doesn't really use grants so its current number grant frames is usually 1 so it doesn't seem useful to restrict it differently.

See the recent discussion with Jan:

http://marc.info/?l=xen-devel&m=141260825209210

I still don't see a use case. Either:

a) dom0 is the driver domain and doesn't need a large number of grants.

Quoted 8 lines
Quoted 4 lines

or

b) dom0 has a frontend driver, then the driver domain will have to scan dom0's grant table thus dom0's grant table must be sized the same as domUs.

Looks like you missed that max_nr_maptrack_frames(), which I think you agree is needed in Dom0, also depends on max_nr_grant_frames.

Agreed.

Quoted 3 lines

Question then of course is whether the
distinction should be made at that: Allow controlling grant frames and maptrack frames separately, rather than Dom0 and DomU.

This would seem sensible.

David

#9 lore raw

Re: [PATCH v3 1/5] xen: introduce two different max_nr_dom0/domU_grant_frames parameters

In reply to Stefano Stabellini

docs/misc/xen-command-line.markdown does not provide VM\_ENTRY\_LOAD\_GUEST\_PAT. · line 614

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 08.10.14 at 15:00, <[email protected]> wrote:
docs/misc/xen-command-line.markdown +3 -1
@@ -609,7 +609,9 @@ does not provide VM\_ENTRY\_LOAD\_GUEST\_PAT.
 Specify the serial parameters for the GDB stub.
 
 ### gnttab\_max\_nr\_frames
-> `= <integer>`
+> `= [<domU number>][,<dom0 number>]`
+
+> Default: `32,32`

If we stay with this, I think the two parts need to be reversed:
Needing to override the Dom0 value is imo more likely, and hence cases where the command line options is already getting used should result in Dom0 behavior being unchanged.

Jan

#10 lore raw

PATCH v3 2/5 xen/arm: introduce invalidate_xen_dcache_va_range

In reply to Stefano Stabellini

Take care of handling non-cacheline aligned addresses and sizes.

---
 xen/include/asm-arm/arm32/page.h |    3 +++
 xen/include/asm-arm/arm64/page.h |    3 +++
 xen/include/asm-arm/page.h       |   30 ++++++++++++++++++++++++++++++
 3 files changed, 36 insertions(+)
xen/include/asm-arm/arm32/page.h +3 -0
index 9740672..6fb2e68 100644
@@ -19,6 +19,9 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
         : : "r" (pte.bits), "r" (p) : "memory");
 }
 
+/* Inline ASM to invalidate dcache on register R (may be an inline asm operand) */
+#define __invalidate_xen_dcache_one(R) STORE_CP32(R, DCIMVAC)
+
 /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
 #define __clean_xen_dcache_one(R) STORE_CP32(R, DCCMVAC)
 
xen/include/asm-arm/arm64/page.h +3 -0
index bb10164..f181b1b 100644
@@ -14,6 +14,9 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
         : : "r" (pte.bits), "r" (p) : "memory");
 }
 
+/* Inline ASM to invalidate dcache on register R (may be an inline asm operand) */
+#define __invalidate_xen_dcache_one(R) "dc ivac, %" #R ";"
+
 /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
 #define __clean_xen_dcache_one(R) "dc cvac, %" #R ";"
 
xen/include/asm-arm/page.h +30 -0
index d758b61..da02e97 100644
@@ -268,6 +268,36 @@ extern size_t cacheline_bytes;
 /* Functions for flushing medium-sized areas.
  * if 'range' is large enough we might want to use model-specific
  * full-cache flushes. */
+
+static inline void invalidate_xen_dcache_va_range(const void *p, unsigned long size)
+{
+    size_t off;
+    const void *end = p + size;
+
+    dsb(sy);           /* So the CPU issues all writes to the range */
+
+    off = (unsigned long)p % cacheline_bytes;
+    if ( off )
+    {
+        p -= off;
+        asm volatile (__clean_and_invalidate_xen_dcache_one(0) : : "r" (p));
+        p += cacheline_bytes;
+        size -= cacheline_bytes - off;
+    }
+    off = (unsigned long)end % cacheline_bytes;
+    if ( off )
+    {
+        end -= off;
+        size -= off;
+        asm volatile (__clean_and_invalidate_xen_dcache_one(0) : : "r" (end));
+    }
+
+    for ( ; p < end; p += cacheline_bytes )
+        asm volatile (__invalidate_xen_dcache_one(0) : : "r" (p));
+
+    dsb(sy);           /* So we know the flushes happen before continuing */
+}
+
 static inline void clean_xen_dcache_va_range(const void *p, unsigned long size)
 {
     const void *end;
Signature · 1 line
1.7.10.4
#11 lore raw

PATCH v3 3/5 xen/x86: introduce more cache maintenance operations

In reply to Stefano Stabellini
---
 xen/include/asm-x86/page.h |    4 ++++
 1 file changed, 4 insertions(+)
xen/include/asm-x86/page.h +4 -0
index 9aa780e..9103d8e 100644
@@ -346,6 +346,10 @@ static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr)
 
 /* No cache maintenance required on x86 architecture. */
 static inline void flush_page_to_ram(unsigned long mfn) {}
+static inline void clean_xen_dcache_va_range(const void *p, unsigned long size) {}
+static inline void invalidate_xen_dcache_va_range(const void *p, unsigned long size) {}
+static inline void clean_and_invalidate_xen_dcache_va_range
+    (const void *p, unsigned long size) {}
 
 /* return true if permission increased */
 static inline bool_t
Signature · 1 line
1.7.10.4
#12 lore raw

Re: [PATCH v3 3/5] xen/x86: introduce more cache maintenance operations

xen/include/asm-x86/page.h static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr) · line 352

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 08.10.14 at 15:00, <[email protected]> wrote:
xen/include/asm-x86/page.h +4 -0
@@ -346,6 +346,10 @@ static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr)
 
 /* No cache maintenance required on x86 architecture. */
 static inline void flush_page_to_ram(unsigned long mfn) {}
+static inline void clean_xen_dcache_va_range(const void *p, unsigned long size) {}
+static inline void invalidate_xen_dcache_va_range(const void *p, unsigned long size) {}
+static inline void clean_and_invalidate_xen_dcache_va_range
+    (const void *p, unsigned long size) {}

Mind explaining what purpose the "_xen" in these names serves? Also, is simply stubbing these out correct? I.e. there are caches on x86, and those can both be invalidated and cleaned, so why would you not do so here?

Jan

#13 lore raw

Re: [PATCH v3 3/5] xen/x86: introduce more cache maintenance operations

Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 08.10.14 at 15:00, <[email protected]> wrote:
xen/include/asm-x86/page.h +0 -0
@@ -346,6 +346,10 @@ static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr)
 
Quoted 8 lines

xen/include/asm-x86/page.h static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr) · line 352

Quoted 6 lines
xen/include/asm-x86/page.h +4 -0
 /* No cache maintenance required on x86 architecture. */
 static inline void flush_page_to_ram(unsigned long mfn) {}
+static inline void clean_xen_dcache_va_range(const void *p, unsigned long size) {}
+static inline void invalidate_xen_dcache_va_range(const void *p, unsigned long size) {}
+static inline void clean_and_invalidate_xen_dcache_va_range
+    (const void *p, unsigned long size) {}

Mind explaining what purpose the "_xen" in these names serves?

It means a Xen virtual address (as opposed to a guest p2m address or a guest virtual address), but I guess it is pretty obvious so I should remove it?

Quoted 3 lines

Also, is simply stubbing these out correct? I.e. there are caches on x86, and those can both be invalidated and cleaned, so why would you not do so here?

Good point. The problem is that to be sure that I am doing the right thing I would need to know what kind of flushes are required with a non-coherent device on x86, but I don't have any examples at hand.

Quoted 1 line

From the amd64 manual, I would implement invalidate with CLFLUSH, clean

and invalidate with WBINVD (even though it operates on the entire cache so it is very heavyweight) and I would have no way to implement just "clean". Do you agree?

#14 lore raw

Re: [PATCH v3 3/5] xen/x86: introduce more cache maintenance operations

Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 10.10.14 at 11:23, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

xen/include/asm-x86/page.h static inline uint32_t cacheattr_to_pte_flags(uint32_t

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 08.10.14 at 15:00, <[email protected]> wrote:
xen/include/asm-x86/page.h +0 -0
@@ -346,6 +346,10 @@ static inline uint32_t cacheattr_to_pte_flags(uint32_t 

cacheattr)

Quoted 4 lines

xen/include/asm-x86/page.h static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr)

Quoted 4 lines
xen/include/asm-x86/page.h +1 -0
 /* No cache maintenance required on x86 architecture. */
 static inline void flush_page_to_ram(unsigned long mfn) {}
+static inline void clean_xen_dcache_va_range(const void *p, unsigned long 

size) {}

Quoted 1 line

xen/include/asm-x86/page.h static inline uint32_t cacheattr_to_pte_flags(uint32_t · line 349

Quoted 1 line
xen/include/asm-x86/page.h +1 -0
+static inline void invalidate_xen_dcache_va_range(const void *p, unsigned 

long size) {}

Quoted 8 lines
Quoted 4 lines

xen/include/asm-x86/page.h static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr) · line 352

Quoted 2 lines
xen/include/asm-x86/page.h +2 -0
+static inline void clean_and_invalidate_xen_dcache_va_range
+    (const void *p, unsigned long size) {}

Mind explaining what purpose the "_xen" in these names serves?

It means a Xen virtual address (as opposed to a guest p2m address or a guest virtual address), but I guess it is pretty obvious so I should remove it?

Yes please.

Quoted 4 lines
Quoted 3 lines

Also, is simply stubbing these out correct? I.e. there are caches on x86, and those can both be invalidated and cleaned, so why would you not do so here?

Quoted 8 lines

Good point. The problem is that to be sure that I am doing the right thing I would need to know what kind of flushes are required with a non-coherent device on x86, but I don't have any examples at hand.

From the amd64 manual, I would implement invalidate with CLFLUSH, clean and invalidate with WBINVD (even though it operates on the entire cache so it is very heavyweight) and I would have no way to implement just "clean". Do you agree?

Except that "clean-without-invalidate" can fall back to "clean-and- invalidate" just fine. The only thing you truly can't implement is invalidate-without-clean, since INVD is global, yet you would want range-restricted invalidation only. And no, CLFLUSH and WBINVD do the same thing (address restricted vs globally).

Also x86 already has all of the logic in place, you just need to call it (see xen/*/*x86/flushtlb.*).

Jan

#15 lore raw

PATCH v3 4/5 xen/arm: introduce GNTTABOP_cache_flush

In reply to Stefano Stabellini

Introduce a new hypercall to perform cache maintenance operation on behalf of the guest. The argument is a machine address and a size. The implementation checks that the memory range is owned by the guest or the guest has been granted access to it by another domain.

Introduce grant_map_exists: an internal grant table function to check whether an mfn has been granted to a given domain on a target grant table.

---

Changes in v3:
- reduce the time the grant_table lock is held;
- fix warning message;
- s/EFAULT/EPERM;
- s/llx/PRIx64;
- check offset and size independetly before checking their sum;
- rcu_lock_current_domain cannot fail;
- s/ENOSYS/EOPNOTSUPP;
- use clean_and_invalidate_xen_dcache_va_range to do both operations at once;
- fold grant_map_exists in this patch;
- support "count" argument;
- make correspondent changes to compat/grant_table.c;
- introduce GNTTAB_CACHE_SOURCE_GREF to select the type of input in the union;
- rename size field to length;
- make length and offset uint16_t;
- only take spin_lock if d != owner.

Changes in v2:
- do not check for mfn_to_page errors;
- take a reference to the page;
- replace printk with gdprintk;
- split long line;
- remove out label;
- move rcu_lock_current_domain down before the loop.
- move the hypercall to GNTTABOP;
- take a spin_lock before calling grant_map_exists.

---
 xen/common/compat/grant_table.c  |    8 +++
 xen/common/grant_table.c         |  135 ++++++++++++++++++++++++++++++++++++++
 xen/include/public/grant_table.h |   20 ++++++
 xen/include/xlat.lst             |    1 +
 4 files changed, 164 insertions(+)
xen/common/compat/grant_table.c +8 -0
index 6c00b09..4a3a23d 100644
@@ -51,6 +51,10 @@ CHECK_gnttab_get_version;
 CHECK_gnttab_swap_grant_ref;
 #undef xen_gnttab_swap_grant_ref
 
+#define xen_gnttab_cache_flush gnttab_cache_flush
+CHECK_gnttab_cache_flush;
+#undef xen_gnttab_cache_flush
+
 int compat_grant_table_op(unsigned int cmd,
                           XEN_GUEST_HANDLE_PARAM(void) cmp_uop,
                           unsigned int count)
@@ -106,6 +110,10 @@ int compat_grant_table_op(unsigned int cmd,
     CASE(swap_grant_ref);
 #endif
 
+#ifndef CHECK_gnttab_cache_flush
+    CASE(cache_flush);
+#endif
+
 #undef CASE
     default:
         return do_grant_table_op(cmd, cmp_uop, count);
xen/common/grant_table.c +135 -0

153 lines not on this page · raw message

xen/include/public/grant_table.h +20 -0
index b8a3d6c..20d4e77 100644
@@ -309,6 +309,7 @@ typedef uint16_t grant_status_t;
 #define GNTTABOP_get_status_frames    9
 #define GNTTABOP_get_version          10
 #define GNTTABOP_swap_grant_ref	      11
+#define GNTTABOP_cache_flush	      12
 #endif /* __XEN_INTERFACE_VERSION__ */
 /* ` } */
 
@@ -574,6 +575,25 @@ struct gnttab_swap_grant_ref {
 typedef struct gnttab_swap_grant_ref gnttab_swap_grant_ref_t;
 DEFINE_XEN_GUEST_HANDLE(gnttab_swap_grant_ref_t);
 
+/*
+ * Issue one or more cache maintenance operations on a portion of a
+ * page granted to the calling domain by a foreign domain.
+ */
+struct gnttab_cache_flush {
+    union {
+        uint64_t dev_bus_addr;
+        grant_ref_t ref;
+    } a;
+    uint16_t offset; /* offset from start of grant */
+    uint16_t length; /* size within the grant */
+#define GNTTAB_CACHE_CLEAN          (1<<0)
+#define GNTTAB_CACHE_INVAL          (1<<1)
+#define GNTTAB_CACHE_SOURCE_GREF    (1<<31)
+    uint32_t op;
+};
+typedef struct gnttab_cache_flush gnttab_cache_flush_t;
+DEFINE_XEN_GUEST_HANDLE(gnttab_cache_flush_t);
+
 #endif /* __XEN_INTERFACE_VERSION__ */
 
 /*
xen/include/xlat.lst +1 -0
index 9a35dd7..3822b00 100644
@@ -51,6 +51,7 @@
 ?       grant_entry_header              grant_table.h
 ?	grant_entry_v2			grant_table.h
 ?	gnttab_swap_grant_ref		grant_table.h
+?	gnttab_cache_flush		grant_table.h
 ?	kexec_exec			kexec.h
 !	kexec_image			kexec.h
 !	kexec_range			kexec.h
Signature · 1 line
1.7.10.4
#16 lore raw
Quoted 81 lines · Stefano Stabellini

Introduce a new hypercall to perform cache maintenance operation on behalf of the guest. The argument is a machine address and a size. The implementation checks that the memory range is owned by the guest or the guest has been granted access to it by another domain.

Introduce grant_map_exists: an internal grant table function to check whether an mfn has been granted to a given domain on a target grant table.

---

Changes in v3:
- reduce the time the grant_table lock is held;
- fix warning message;
- s/EFAULT/EPERM;
- s/llx/PRIx64;
- check offset and size independetly before checking their sum;
- rcu_lock_current_domain cannot fail;
- s/ENOSYS/EOPNOTSUPP;
- use clean_and_invalidate_xen_dcache_va_range to do both operations at once;
- fold grant_map_exists in this patch;
- support "count" argument;
- make correspondent changes to compat/grant_table.c;
- introduce GNTTAB_CACHE_SOURCE_GREF to select the type of input in the union;
- rename size field to length;
- make length and offset uint16_t;
- only take spin_lock if d != owner.

Changes in v2:
- do not check for mfn_to_page errors;
- take a reference to the page;
- replace printk with gdprintk;
- split long line;
- remove out label;
- move rcu_lock_current_domain down before the loop.
- move the hypercall to GNTTABOP;
- take a spin_lock before calling grant_map_exists.

---
 xen/common/compat/grant_table.c  |    8 +++
 xen/common/grant_table.c         |  135 ++++++++++++++++++++++++++++++++++++++
 xen/include/public/grant_table.h |   20 ++++++
 xen/include/xlat.lst             |    1 +
 4 files changed, 164 insertions(+)
xen/common/compat/grant_table.c +8 -0
index 6c00b09..4a3a23d 100644
@@ -51,6 +51,10 @@ CHECK_gnttab_get_version;
 CHECK_gnttab_swap_grant_ref;
 #undef xen_gnttab_swap_grant_ref
 
+#define xen_gnttab_cache_flush gnttab_cache_flush
+CHECK_gnttab_cache_flush;
+#undef xen_gnttab_cache_flush
+
 int compat_grant_table_op(unsigned int cmd,
                           XEN_GUEST_HANDLE_PARAM(void) cmp_uop,
                           unsigned int count)
@@ -106,6 +110,10 @@ int compat_grant_table_op(unsigned int cmd,
     CASE(swap_grant_ref);
 #endif
 
+#ifndef CHECK_gnttab_cache_flush
+    CASE(cache_flush);
+#endif
+
 #undef CASE
     default:
         return do_grant_table_op(cmd, cmp_uop, count);
xen/common/grant_table.c +0 -0
index a56a1bb..c16c11c 100644
@@ -501,6 +501,36 @@ static int _set_status(unsigned gt_version,
         return _set_status_v2(domid, readonly, mapflag, shah, act, status);
 }

xen/common/grant_table.c static int _set_status(unsigned gt_version, · line 512

Quoted 10 lines
xen/common/grant_table.c +9 -0
 
+static bool_t grant_map_exists(const struct domain *ld,
+                        struct grant_table *rgt,
+                        unsigned long mfn)
+{
+    const struct active_grant_entry *act;
+    grant_ref_t ref;
+    bool_t ret = 0;
+
+    spin_is_locked(&rgt->lock);

This is now dead code. spin_is_locked() is just a predicate.

Quoted 6 lines
xen/common/grant_table.c +6 -0
+
+    for ( ref = 0; ref != nr_grant_entries(rgt); ref++ )
+    {
+        act = &active_entry(rgt, ref);
+
+        if ( !act->pin )

xen/common/grant_table.c static int _set_status(unsigned gt_version, · line 528

Quoted 10 lines
xen/common/grant_table.c +10 -0
+            continue;
+
+        if ( act->domid != ld->domain_id )
+            continue;
+
+        if ( act->frame != mfn )
+            continue;
+
+        ret = 1;
+        break;

how about return 1 here...

xen/common/grant_table.c static int _set_status(unsigned gt_version, · line 531

Quoted 3 lines
xen/common/grant_table.c +3 -0
+    }
+
+    return ret;

and return 0 here?

xen/common/grant_table.c gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop, · line 2531

Quoted 10 lines
xen/common/grant_table.c +3 -0
+}
+
 static void mapcount(
     struct grant_table *lgt, struct domain *rd, unsigned long mfn,
     unsigned int *wrc, unsigned int *rdc)
@@ -2498,6 +2528,97 @@ gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop,
     return 0;
 }
 
+static int __gnttab_cache_flush(gnttab_cache_flush_t cflush)

This should be passed via pointer.

xen/common/grant_table.c gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop, · line 2540

Quoted 9 lines
xen/common/grant_table.c +9 -0
+{
+    struct domain *d, *owner;
+    struct page_info *page;
+    uint64_t mfn;
+    void *v;
+
+    if ( cflush.offset > PAGE_SIZE ||
+            cflush.length > PAGE_SIZE ||
+            cflush.offset + cflush.length > PAGE_SIZE )

Brackets, as per Xen style, please.

Quoted 57 lines
xen/common/grant_table.c +57 -0
+        return -EINVAL;
+
+    if ( cflush.length == 0 || cflush.op == 0 )
+        return 0;
+
+    /* currently unimplemented */
+    if ( cflush.op & GNTTAB_CACHE_SOURCE_GREF )
+        return -EOPNOTSUPP;
+
+    d = rcu_lock_current_domain();
+    mfn = cflush.a.dev_bus_addr >> PAGE_SHIFT;
+
+    if ( !mfn_valid(mfn) )
+    {
+        rcu_unlock_domain(d);
+        return -EINVAL;
+    }
+
+    page = mfn_to_page(mfn);
+    owner = page_get_owner_and_reference(page);
+    if ( !owner )
+    {
+        rcu_unlock_domain(d);
+        return -EPERM;
+    }
+
+    if ( d != owner )
+    {
+        spin_lock(&owner->grant_table->lock);
+
+        if ( !grant_map_exists(d, owner->grant_table, mfn) )
+        {
+            spin_unlock(&owner->grant_table->lock);
+            rcu_unlock_domain(d);
+            put_page(page);
+            gdprintk(XENLOG_G_ERR, "mfn %"PRIx64" hasn't been granted by d%d to d%d\n",
+                    mfn, owner->domain_id, d->domain_id);
+            return -EINVAL;
+        }
+    }
+
+    v = map_domain_page(mfn);
+    v += cflush.offset;
+
+    if ( (cflush.op & GNTTAB_CACHE_INVAL) && (cflush.op & GNTTAB_CACHE_CLEAN) )
+        clean_and_invalidate_xen_dcache_va_range(v, cflush.length);
+    else if ( cflush.op & GNTTAB_CACHE_INVAL )
+        invalidate_xen_dcache_va_range(v, cflush.length);
+    else if ( cflush.op & GNTTAB_CACHE_CLEAN )
+        clean_xen_dcache_va_range(v, cflush.length);
+
+    if ( d != owner )
+        spin_unlock(&owner->grant_table->lock);
+    unmap_domain_page(v);
+    put_page(page);
+
+    return 0;

xen/common/grant_table.c gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop, · line 2607

Quoted 10 lines
xen/common/grant_table.c +10 -0
+}
+
+static long
+gnttab_cache_flush(XEN_GUEST_HANDLE_PARAM(gnttab_cache_flush_t) uop,
+                      unsigned int count)
+{
+    int i, ret;
+    gnttab_cache_flush_t op;
+
+    for ( i = 0; i < count; i++ )

i must be unsigned, or this can be an infinite loop, as count in an unverified user parameter at this point.

~Andrew

Quoted 88 lines
xen/common/grant_table.c +28 -0
+    {
+        if ( i && hypercall_preempt_check() )
+            return i;
+        if ( unlikely(__copy_from_guest(&op, uop, 1)) )
+            return -EFAULT;
+        ret = __gnttab_cache_flush(op);
+        if ( ret < 0 )
+            return -ret;
+        guest_handle_add_offset(uop, 1);
+    }
+    return 0;
+}
+
+
 long
 do_grant_table_op(
     unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop, unsigned int count)
@@ -2627,6 +2748,20 @@ do_grant_table_op(
         }
         break;
     }
+    case GNTTABOP_cache_flush:
+    {
+        XEN_GUEST_HANDLE_PARAM(gnttab_cache_flush_t) cflush =
+            guest_handle_cast(uop, gnttab_cache_flush_t);
+        if ( unlikely(!guest_handle_okay(cflush, count)) )
+            goto out;
+        rc = gnttab_cache_flush(cflush, count);
+        if ( rc > 0 )
+        {
+            guest_handle_add_offset(cflush, rc);
+            uop = guest_handle_cast(cflush, void);
+        }
+        break;
+    }
     default:
         rc = -ENOSYS;
         break;
xen/include/public/grant_table.h +20 -0
index b8a3d6c..20d4e77 100644
@@ -309,6 +309,7 @@ typedef uint16_t grant_status_t;
 #define GNTTABOP_get_status_frames    9
 #define GNTTABOP_get_version          10
 #define GNTTABOP_swap_grant_ref	      11
+#define GNTTABOP_cache_flush	      12
 #endif /* __XEN_INTERFACE_VERSION__ */
 /* ` } */
 
@@ -574,6 +575,25 @@ struct gnttab_swap_grant_ref {
 typedef struct gnttab_swap_grant_ref gnttab_swap_grant_ref_t;
 DEFINE_XEN_GUEST_HANDLE(gnttab_swap_grant_ref_t);
 
+/*
+ * Issue one or more cache maintenance operations on a portion of a
+ * page granted to the calling domain by a foreign domain.
+ */
+struct gnttab_cache_flush {
+    union {
+        uint64_t dev_bus_addr;
+        grant_ref_t ref;
+    } a;
+    uint16_t offset; /* offset from start of grant */
+    uint16_t length; /* size within the grant */
+#define GNTTAB_CACHE_CLEAN          (1<<0)
+#define GNTTAB_CACHE_INVAL          (1<<1)
+#define GNTTAB_CACHE_SOURCE_GREF    (1<<31)
+    uint32_t op;
+};
+typedef struct gnttab_cache_flush gnttab_cache_flush_t;
+DEFINE_XEN_GUEST_HANDLE(gnttab_cache_flush_t);
+
 #endif /* __XEN_INTERFACE_VERSION__ */
 
 /*
xen/include/xlat.lst +1 -0
index 9a35dd7..3822b00 100644
@@ -51,6 +51,7 @@
 ?       grant_entry_header              grant_table.h
 ?	grant_entry_v2			grant_table.h
 ?	gnttab_swap_grant_ref		grant_table.h
+?	gnttab_cache_flush		grant_table.h
 ?	kexec_exec			kexec.h
 !	kexec_image			kexec.h
 !	kexec_range			kexec.h
#17 lore raw
In reply to Stefano Stabellini
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 08.10.14 at 15:00, <[email protected]> wrote:
xen/common/grant_table.c +8 -0
@@ -2498,6 +2528,97 @@ gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop,
     return 0;
 }
 
+static int __gnttab_cache_flush(gnttab_cache_flush_t cflush)
+{
+    struct domain *d, *owner;
+    struct page_info *page;
+    uint64_t mfn;
+    void *v;
+
+    if ( cflush.offset > PAGE_SIZE ||
 

= ?

xen/common/grant_table.c gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop, · line 2540

Quoted 2 lines
xen/common/grant_table.c +2 -0
+            cflush.length > PAGE_SIZE ||
+            cflush.offset + cflush.length > PAGE_SIZE )

Indentation.

Quoted 65 lines
xen/common/grant_table.c +65 -0
+        return -EINVAL;
+
+    if ( cflush.length == 0 || cflush.op == 0 )
+        return 0;
+
+    /* currently unimplemented */
+    if ( cflush.op & GNTTAB_CACHE_SOURCE_GREF )
+        return -EOPNOTSUPP;
+
+    d = rcu_lock_current_domain();
+    mfn = cflush.a.dev_bus_addr >> PAGE_SHIFT;
+
+    if ( !mfn_valid(mfn) )
+    {
+        rcu_unlock_domain(d);
+        return -EINVAL;
+    }
+
+    page = mfn_to_page(mfn);
+    owner = page_get_owner_and_reference(page);
+    if ( !owner )
+    {
+        rcu_unlock_domain(d);
+        return -EPERM;
+    }
+
+    if ( d != owner )
+    {
+        spin_lock(&owner->grant_table->lock);
+
+        if ( !grant_map_exists(d, owner->grant_table, mfn) )
+        {
+            spin_unlock(&owner->grant_table->lock);
+            rcu_unlock_domain(d);
+            put_page(page);
+            gdprintk(XENLOG_G_ERR, "mfn %"PRIx64" hasn't been granted by d%d to d%d\n",
+                    mfn, owner->domain_id, d->domain_id);
+            return -EINVAL;
+        }
+    }
+
+    v = map_domain_page(mfn);
+    v += cflush.offset;
+
+    if ( (cflush.op & GNTTAB_CACHE_INVAL) && (cflush.op & GNTTAB_CACHE_CLEAN) )
+        clean_and_invalidate_xen_dcache_va_range(v, cflush.length);
+    else if ( cflush.op & GNTTAB_CACHE_INVAL )
+        invalidate_xen_dcache_va_range(v, cflush.length);
+    else if ( cflush.op & GNTTAB_CACHE_CLEAN )
+        clean_xen_dcache_va_range(v, cflush.length);
+
+    if ( d != owner )
+        spin_unlock(&owner->grant_table->lock);
+    unmap_domain_page(v);
+    put_page(page);
+
+    return 0;
+}
+
+static long
+gnttab_cache_flush(XEN_GUEST_HANDLE_PARAM(gnttab_cache_flush_t) uop,
+                      unsigned int count)
+{
+    int i, ret;
+    gnttab_cache_flush_t op;

xen/common/grant_table.c gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop, · line 2615

Quoted 10 lines
xen/common/grant_table.c +10 -0
+
+    for ( i = 0; i < count; i++ )
+    {
+        if ( i && hypercall_preempt_check() )
+            return i;
+        if ( unlikely(__copy_from_guest(&op, uop, 1)) )
+            return -EFAULT;
+        ret = __gnttab_cache_flush(op);
+        if ( ret < 0 )
+            return -ret;

Why "-ret"? Especially with ...

Quoted 8 lines
xen/common/grant_table.c +4 -0
@@ -2627,6 +2748,20 @@ do_grant_table_op(
         }
         break;
     }
+    case GNTTABOP_cache_flush:
+    {
+        XEN_GUEST_HANDLE_PARAM(gnttab_cache_flush_t) cflush =
+            guest_handle_cast(uop, gnttab_cache_flush_t);

xen/common/grant_table.c do_grant_table_op( · line 2764

Quoted 10 lines
xen/common/grant_table.c +10 -0
+        if ( unlikely(!guest_handle_okay(cflush, count)) )
+            goto out;
+        rc = gnttab_cache_flush(cflush, count);
+        if ( rc > 0 )
+        {
+            guest_handle_add_offset(cflush, rc);
+            uop = guest_handle_cast(cflush, void);
+        }
+        break;
+    }

... a positive value here meaning "continuation needed".

Jan

#18 lore raw

PATCH v3 5/5 Revert "xen/arm: introduce XENFEAT_grant_map_identity"

In reply to Stefano Stabellini

Just keep the definition of XENFEAT_grant_map_identity.

---

Changes in v2:

- comment out the definition of XENFEAT_grant_map_identity.

---
 xen/common/grant_table.c           |   30 +++++-------------------------
 xen/common/kernel.c                |    2 --
 xen/drivers/passthrough/arm/smmu.c |   33 +++++++++++++++++++++++++++++++++
 xen/include/asm-arm/grant_table.h  |    3 ++-
 xen/include/public/features.h      |    4 +++-
 5 files changed, 43 insertions(+), 29 deletions(-)
xen/common/grant_table.c +5 -25
index c16c11c..dcedf87 100644
@@ -785,23 +785,13 @@ __gnttab_map_grant_ref(
              !(old_pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) )
         {
             if ( wrc == 0 )
-            {
-                if ( is_domain_direct_mapped(ld) )
-                    err = arch_grant_map_page_identity(ld, frame, 1);
-                else
-                    err = iommu_map_page(ld, frame, frame,
-                            IOMMUF_readable|IOMMUF_writable);
-            }
+                err = iommu_map_page(ld, frame, frame,
+                                     IOMMUF_readable|IOMMUF_writable);
         }
         else if ( act_pin && !old_pin )
         {
             if ( (wrc + rdc) == 0 )
-            {
-                if ( is_domain_direct_mapped(ld) )
-                    err = arch_grant_map_page_identity(ld, frame, 0);
-                else
-                    err = iommu_map_page(ld, frame, frame, IOMMUF_readable);
-            }
+                err = iommu_map_page(ld, frame, frame, IOMMUF_readable);
         }
         if ( err )
         {
@@ -998,19 +988,9 @@ __gnttab_unmap_common(
         int err = 0;
         mapcount(lgt, rd, op->frame, &wrc, &rdc);
         if ( (wrc + rdc) == 0 )
-        {
-            if ( is_domain_direct_mapped(ld) )
-                err = arch_grant_unmap_page_identity(ld, op->frame);
-            else
-                err = iommu_unmap_page(ld, op->frame);
-        }
+            err = iommu_unmap_page(ld, op->frame);
         else if ( wrc == 0 )
-        {
-            if ( is_domain_direct_mapped(ld) )
-                err = arch_grant_map_page_identity(ld, op->frame, 0);
-            else
-                err = iommu_map_page(ld, op->frame, op->frame, IOMMUF_readable);
-        }
+            err = iommu_map_page(ld, op->frame, op->frame, IOMMUF_readable);
         if ( err )
         {
             rc = GNTST_general_error;
xen/common/kernel.c +0 -2
index ce65486..d23c422 100644
@@ -332,8 +332,6 @@ DO(xen_version)(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
                 break;
             }
 #endif
-            if ( is_domain_direct_mapped(d) )
-                fi.submap |= 1U << XENFEAT_grant_map_identity;
             break;
         default:
             return -EINVAL;
xen/drivers/passthrough/arm/smmu.c +33 -0
index 3cbd206..9a95ac9 100644
@@ -1536,6 +1536,37 @@ static void arm_smmu_iommu_domain_teardown(struct domain *d)
     xfree(smmu_domain);
 }
 
+static int arm_smmu_map_page(struct domain *d, unsigned long gfn,
+                             unsigned long mfn, unsigned int flags)
+{
+    /* Grant mappings can be used for DMA requests. The dev_bus_addr returned by
+     * the hypercall is the MFN (not the IPA). For device protected by
+     * an IOMMU, Xen needs to add a 1:1 mapping in the domain p2m to
+     * allow DMA request to work.
+     * This is only valid when the domain is directed mapped. Hence this
+     * function should only be used by gnttab code with gfn == mfn.
+     */
+    BUG_ON(!is_domain_direct_mapped(d));
+    BUG_ON(mfn != gfn);
+
+    /* We only support readable and writable flags */
+    if ( !(flags & (IOMMUF_readable | IOMMUF_writable)) )
+        return -EINVAL;
+
+    return arch_grant_map_page_identity(d, mfn, flags & IOMMUF_writable);
+}
+
+static int arm_smmu_unmap_page(struct domain *d, unsigned long gfn)
+{
+    /* This function should only be used by gnttab code when the domain
+     * is direct mapped
+     */
+    if ( !is_domain_direct_mapped(d) )
+        return -EINVAL;
+
+    return arch_grant_unmap_page_identity(d, gfn);
+}
+
 static const struct iommu_ops arm_smmu_iommu_ops = {
     .init = arm_smmu_iommu_domain_init,
     .hwdom_init = arm_smmu_iommu_hwdom_init,
@@ -1544,6 +1575,8 @@ static const struct iommu_ops arm_smmu_iommu_ops = {
     .iotlb_flush_all = arm_smmu_iotlb_flush_all,
     .assign_dt_device = arm_smmu_attach_dev,
     .reassign_dt_device = arm_smmu_reassign_dt_dev,
+    .map_page = arm_smmu_map_page,
+    .unmap_page = arm_smmu_unmap_page,
 };
 
 static int __init smmu_init(struct dt_device_node *dev,
xen/include/asm-arm/grant_table.h +2 -1
index e81c973..a9f0291 100644
@@ -33,7 +33,8 @@ static inline int replace_grant_supported(void)
     ( ((i >= nr_grant_frames(d->grant_table)) &&                         \
      (i < max_nr_grant_frames(d))) ? 0 : (d->arch.grant_table_gpfn[i]))
 
-#define gnttab_need_iommu_mapping(d)           (is_domain_direct_mapped(d))
+#define gnttab_need_iommu_mapping(d)                    \
+    (is_domain_direct_mapped(d) && need_iommu(d))
 
 #endif /* __ASM_GRANT_TABLE_H__ */
 /*
xen/include/public/features.h +3 -1
index b7bf83f..16d92aa 100644
@@ -94,8 +94,10 @@
 /* operation as Dom0 is supported */
 #define XENFEAT_dom0                      11
 
-/* Xen also maps grant references at pfn = mfn */
+/* Xen also maps grant references at pfn = mfn.
+ * This feature flag is deprecated and should not be used.
 #define XENFEAT_grant_map_identity        12
+ */
 
 #define XENFEAT_NR_SUBMAPS 1
 
Signature · 1 line
1.7.10.4
#19 lore raw

Re: [PATCH v3 5/5] Revert "xen/arm: introduce XENFEAT_grant_map_identity"

Hi Stefano,

Quoted 1 line · Stefano Stabellini

Just keep the definition of XENFEAT_grant_map_identity.

Can you provide the commit ID in the message?

BTW, I would also revert e25a5f4 "xen: introduce arch_grant_(un)map_page_identity" which is not necessary anymore.

Regards,

Signature · 1 line
Julien Grall
#20 lore raw

Re: [PATCH v3 5/5] Revert "xen/arm: introduce XENFEAT_grant_map_identity"

Quoted 9 lines · Julien Grall

Hi Stefano,

Quoted 1 line · Stefano Stabellini

Just keep the definition of XENFEAT_grant_map_identity.

Can you provide the commit ID in the message?

BTW, I would also revert e25a5f4 "xen: introduce arch_grant_(un)map_page_identity" which is not necessary anymore.

Good idea

#21 lore raw
In reply to Stefano Stabellini
Quoted 5 lines · Stefano Stabellini

Hi all,
this patch series introduces a new hypercall to perform cache maintenance operations on behalf of the guest. It is useful for dom0 to be able to cache flush pages involved in a dma operation with non-coherent devices.

I was going to suggest requiring the use of the SWIOTLB for all foreign pages to non-coherent devices but Ian tells me you've already tried this and it performs worse than this proposal. Is this the case?

Using the SWIOTLB would require no Xen changes (except for the revert) which might be sensible at this point in the Xen 4.5 release cycle.

David

#22 lore raw
Quoted 7 lines · David Vrabel
Quoted 5 lines · Stefano Stabellini

Hi all,
this patch series introduces a new hypercall to perform cache maintenance operations on behalf of the guest. It is useful for dom0 to be able to cache flush pages involved in a dma operation with non-coherent devices.

Quoted 6 lines

I was going to suggest requiring the use of the SWIOTLB for all foreign pages to non-coherent devices but Ian tells me you've already tried this and it performs worse than this proposal. Is this the case?

Using the SWIOTLB would require no Xen changes (except for the revert) which might be sensible at this point in the Xen 4.5 release cycle.

I benchmarked the swiotlb solution a long time ago and was the worst of the lot. Back then I didn't try to narrow down its usage only to non-coherent devices though because it is actually quite an hack and I hadn't climbed down the ivory tower yet. At this point anything goes.

I have tried that approach now, and it is still the worst of the lot. It is slower than the hypercall approach by 37% (485 timer ticks on average vs 352).