vtd: don't assume addresses are aligned in sync_cache
authorRoger Pau Monné <roger.pau@citrix.com>
Tue, 7 Jul 2020 12:39:05 +0000 (14:39 +0200)
committerJan Beulich <jbeulich@suse.com>
Tue, 7 Jul 2020 12:39:05 +0000 (14:39 +0200)
Current code in sync_cache assume that the address passed in is
aligned to a cache line size. Fix the code to support passing in
arbitrary addresses not necessarily aligned to a cache line size.

This is part of XSA-321.

Reported-by: Jan Beulich <jbeulich@suse.com>
Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
xen/drivers/passthrough/vtd/iommu.c

index 93bcd72f848d502d253e0b7b419abb4ffd8fb892..8a2c105f72b629f3f93868b108885a4ae3ffcd44 100644 (file)
@@ -149,8 +149,8 @@ static int iommus_incoherent;
 
 static void sync_cache(const void *addr, unsigned int size)
 {
-    int i;
-    static unsigned int clflush_size = 0;
+    static unsigned long clflush_size = 0;
+    const void *end = addr + size;
 
     if ( !iommus_incoherent )
         return;
@@ -158,8 +158,9 @@ static void sync_cache(const void *addr, unsigned int size)
     if ( clflush_size == 0 )
         clflush_size = get_cache_line_size();
 
-    for ( i = 0; i < size; i += clflush_size )
-        cacheline_flush((char *)addr + i);
+    addr -= (unsigned long)addr & (clflush_size - 1);
+    for ( ; addr < end; addr += clflush_size )
+        cacheline_flush((char *)addr);
 }
 
 /* Allocate page table, return its machine address */