Skip to contentSkip to conversation

fix xfs by making I/O to vmap/vmalloc areas work

PATCH In review James Bottomley 3 participants 8 messages Feb 5, 2010 – Feb 7, 2010

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v3 cover fix xfs by making I/O to vmap/vmalloc areas work

From: James Bottomley <[email protected]>

This is essentially a small tidy up from the previous series. I thought about the Ben H additions, but since power doesn't seem to need this, they seemed a bit moot (we can expand the API when an actual user comes along).

The patch series adds a flush/invalidate_kernel_vmap_range() API that drivers using vmap/vmalloc areas must use before sending tohse areas for I/O. This makes it crystal clear that coherence on these areas is the responsibility of the driver alone. Fortunately xfs is the only thing in the kernel actually doing I/O to vmap areas.

Sin ce xfs is completely broken on most VIPT architectures without this, I'd like to submit it as a bug fix for 2.6.33. Unfortunately, we actually have some parisc xfs users whose data is curently at severe risk.

James

---

James Bottomley (5):
mm: add coherence API for DMA to vmalloc/vmap areas
parisc: add mm API for DMA to vmalloc/vmap areas
arm: add mm API for DMA to vmalloc/vmap areas
sh: add mm API for DMA to vmalloc/vmap areas
xfs: fix xfs to work with Virtually Indexed architectures

 Documentation/cachetlb.txt           |   24 ++++++++++++++++++++++++
 arch/arm/include/asm/cacheflush.h    |   10 ++++++++++
 arch/parisc/include/asm/cacheflush.h |   12 ++++++++++++
 arch/sh/include/asm/cacheflush.h     |    8 ++++++++
 fs/xfs/linux-2.6/xfs_buf.c           |   30 +++++++++++++++++++++++++++++-
 include/linux/highmem.h              |    6 ++++++
 6 files changed, 89 insertions(+), 1 deletions(-)
#1 lore raw

PATCH v3 1/5 mm: add coherence API for DMA to vmalloc/vmap areas

On Virtually Indexed architectures (which don't do automatic alias resolution in their caches), we have to flush via the correct virtual address to prepare pages for DMA. On some architectures (like arm) we cannot prevent the CPU from doing data movein along the alias (and thus giving stale read data), so we not only have to introduce a flush API to push dirty cache lines out, but also an invalidate API to kill inconsistent cache lines that may have moved in before DMA changed the data

---
 Documentation/cachetlb.txt |   24 ++++++++++++++++++++++++
 include/linux/highmem.h    |    6 ++++++
 2 files changed, 30 insertions(+), 0 deletions(-)
Documentation/cachetlb.txt +24 -0
index da42ab4..b231414 100644
@@ -377,3 +377,27 @@ maps this page at its virtual address.
 	All the functionality of flush_icache_page can be implemented in
 	flush_dcache_page and update_mmu_cache. In 2.7 the hope is to
 	remove this interface completely.
+
+The final category of APIs is for I/O to deliberately aliased address
+ranges inside the kernel.  Such aliases are set up by use of the
+vmap/vmalloc API.  Since kernel I/O goes via physical pages, the I/O
+subsystem assumes that the user mapping and kernel offset mapping are
+the only aliases.  This isn't true for vmap aliases, so anything in
+the kernel trying to do I/O to vmap areas must manually manage
+coherency.  It must do this by flushing the vmap range before doing
+I/O and invalidating it after the I/O returns.
+
+  void flush_kernel_vmap_range(void *vaddr, int size)
+       flushes the kernel cache for a given virtual address range in
+       the vmap area.  This is to make sure that any data the kernel
+       modified in the vmap range is made visible to the physical
+       page.  The design is to make this area safe to perform I/O on.
+       Note that this API does *not* also flush the offset map alias
+       of the area.
+
+  void invalidate_kernel_vmap_range(void *vaddr, int size) invalidates
+       the cache for a given virtual address range in the vmap area
+       which prevents the processor from making the cache stale by
+       speculatively reading data while the I/O was occurring to the
+       physical pages.  This is only necessary for data reads into the
+       vmap area.
include/linux/highmem.h +6 -0
index 211ff44..adfe101 100644
@@ -17,6 +17,12 @@ static inline void flush_anon_page(struct vm_area_struct *vma, struct page *page
 static inline void flush_kernel_dcache_page(struct page *page)
 {
 }
+static inline void flush_kernel_vmap_range(void *vaddr, int size)
+{
+}
+static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
+{
+}
 #endif
 
 #include <asm/kmap_types.h>
Signature · 1 line
1.6.5
#2 lore raw

PATCH v3 2/5 parisc: add mm API for DMA to vmalloc/vmap areas

We already have an API to flush a kernel page along an alias address, so use it. The TLB purge prevents the CPU from doing speculative moveins on the flushed address, so we don't need to implement and invalidate.

---
 arch/parisc/include/asm/cacheflush.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
arch/parisc/include/asm/cacheflush.h +12 -0
index 7a73b61..4772777 100644
@@ -38,6 +38,18 @@ void flush_cache_mm(struct mm_struct *mm);
 
 #define flush_kernel_dcache_range(start,size) \
 	flush_kernel_dcache_range_asm((start), (start)+(size));
+/* vmap range flushes and invalidates.  Architecturally, we don't need
+ * the invalidate, because the CPU should refuse to speculate once an
+ * area has been flushed, so invalidate is left empty */
+static inline void flush_kernel_vmap_range(void *vaddr, int size)
+{
+	unsigned long start = (unsigned long)vaddr;
+
+	flush_kernel_dcache_range_asm(start, start + size);
+}
+static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
+{
+}
 
 #define flush_cache_vmap(start, end)		flush_cache_all()
 #define flush_cache_vunmap(start, end)		flush_cache_all()
Signature · 1 line
1.6.5
#3 lore raw

PATCH v3 3/5 arm: add mm API for DMA to vmalloc/vmap areas

ARM cannot prevent cache movein, so this patch implements both the flush and invalidate pieces of the API.

---
 arch/arm/include/asm/cacheflush.h |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
arch/arm/include/asm/cacheflush.h +10 -0
index 730aefc..4ae503c 100644
@@ -432,6 +432,16 @@ static inline void __flush_icache_all(void)
 	    : "r" (0));
 #endif
 }
+static inline void flush_kernel_vmap_range(void *addr, int size)
+{
+	if ((cache_is_vivt() || cache_is_vipt_aliasing()))
+	  __cpuc_flush_dcache_area(addr, (size_t)size);
+}
+static inline void invalidate_kernel_vmap_range(void *addr, int size)
+{
+	if ((cache_is_vivt() || cache_is_vipt_aliasing()))
+	  __cpuc_flush_dcache_area(addr, (size_t)size);
+}
 
 #define ARCH_HAS_FLUSH_ANON_PAGE
 static inline void flush_anon_page(struct vm_area_struct *vma,
Signature · 1 line
1.6.5
#4 lore raw

PATCH v3 4/5 sh: add mm API for DMA to vmalloc/vmap areas

---
 arch/sh/include/asm/cacheflush.h |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
arch/sh/include/asm/cacheflush.h +8 -0
index dda96eb..da3ebec 100644
@@ -63,6 +63,14 @@ static inline void flush_anon_page(struct vm_area_struct *vma,
 	if (boot_cpu_data.dcache.n_aliases && PageAnon(page))
 		__flush_anon_page(page, vmaddr);
 }
+static inline void flush_kernel_vmap_range(void *addr, int size)
+{
+	__flush_wback_region(addr, size);
+}
+static inline void invalidate_kernel_vmap_range(void *addr, int size)
+{
+	__flush_invalidate_region(addr, size);
+}
 
 #define ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
 static inline void flush_kernel_dcache_page(struct page *page)
Signature · 1 line
1.6.5
#5 lore raw

PATCH v3 5/5 xfs: fix xfs to work with Virtually Indexed architectures

xfs_buf.c includes what is essentially a hand rolled version of blk_rq_map_kern(). In order to work properly with the vmalloc buffers that xfs uses, this hand rolled routine must also implement the flushing API for vmap/vmalloc areas.

---
 fs/xfs/linux-2.6/xfs_buf.c |   30 +++++++++++++++++++++++++++++-
 1 files changed, 29 insertions(+), 1 deletions(-)
fs/xfs/linux-2.6/xfs_buf.c +29 -1
index 77b8be8..6f3ebb6 100644
@@ -76,6 +76,27 @@ struct workqueue_struct *xfsconvertd_workqueue;
 #define xfs_buf_deallocate(bp) \
 	kmem_zone_free(xfs_buf_zone, (bp));
 
+static inline int
+xfs_buf_is_vmapped(
+	struct xfs_buf	*bp)
+{
+	/*
+	 * Return true if the buffer is vmapped.
+	 *
+	 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
+	 * code is clever enough to know it doesn't have to map a single page,
+	 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
+	 */
+	return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
+}
+
+static inline int
+xfs_buf_vmap_len(
+	struct xfs_buf	*bp)
+{
+	return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
+}
+
 /*
  *	Page Region interfaces.
  *
@@ -314,7 +335,7 @@ xfs_buf_free(
 	if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
 		uint		i;
 
-		if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
+		if (xfs_buf_is_vmapped(bp))
 			free_address(bp->b_addr - bp->b_offset);
 
 		for (i = 0; i < bp->b_page_count; i++) {
@@ -1107,6 +1128,9 @@ xfs_buf_bio_end_io(
 
 	xfs_buf_ioerror(bp, -error);
 
+	if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
+		invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
+
 	do {
 		struct page	*page = bvec->bv_page;
 
@@ -1216,6 +1240,10 @@ next_chunk:
 
 submit_io:
 	if (likely(bio->bi_size)) {
+		if (xfs_buf_is_vmapped(bp)) {
+			flush_kernel_vmap_range(bp->b_addr,
+						xfs_buf_vmap_len(bp));
+		}
 		submit_bio(rw, bio);
 		if (size)
 			goto next_chunk;
Signature · 1 line
1.6.5
#6 lore raw
In reply to James Bottomley
Quoted 13 lines · James Bottomley

From: James Bottomley <[email protected]>

This is essentially a small tidy up from the previous series. I thought about the Ben H additions, but since power doesn't seem to need this, they seemed a bit moot (we can expand the API when an actual user comes along).

The patch series adds a flush/invalidate_kernel_vmap_range() API that drivers using vmap/vmalloc areas must use before sending tohse areas for I/O. This makes it crystal clear that coherence on these areas is the responsibility of the driver alone. Fortunately xfs is the only thing in the kernel actually doing I/O to vmap areas.

Quoted 4 lines

Sin ce xfs is completely broken on most VIPT architectures without this, I'd like to submit it as a bug fix for 2.6.33. Unfortunately, we actually have some parisc xfs users whose data is curently at severe risk.

Agreed. There's also a lot of ARM users popping up with this recently, while others worked around it using local flushing hacks previously.

#7 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines

From: James Bottomley <[email protected]>

Agreed. There's also a lot of ARM users popping up with this recently, while others worked around it using local flushing hacks previously.

Thanks, I've applied these to parisc/next to give them a shakedown in linux-next.

regards, Kyle.