Skip to contentSkip to conversation

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

PATCH In review James Bottomley 4 participants 13 messages Sep 9, 2009 – Sep 11, 2009

Subsystems (7)
Arm (32-bit) VFS MM core SuperH +3 more subsystems
lore
Share & tools
lore
#0 lore raw

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

Here's version two of the patch set. It actually compiles on both x86 and parisc. I could do with someone to test it on arm and sh.

The key test is how xfs behaves. What I did to recreate the problem on parisc was simply create an 8GB xfs filesystem, use cp -a to pump about a GB of data into it from my git trees, then unmount and run xfs_check. Before the patches, xfs_check reports the whole fs to be corrupt. After the patches it reports everything to be OK.

James

---

 arch/arm/include/asm/cacheflush.h    |   10 ++++++++++
 arch/parisc/include/asm/cacheflush.h |    8 ++++++++
 arch/sh/include/asm/cacheflush.h     |    8 ++++++++
 fs/bio.c                             |   19 +++++++++++++++++--
 fs/xfs/linux-2.6/xfs_buf.c           |   10 ++++++++++
 include/linux/highmem.h              |    6 ++++++
 6 files changed, 59 insertions(+), 2 deletions(-)
#1 lore raw

PATCH 1/6 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

---
 include/linux/highmem.h |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
include/linux/highmem.h +6 -0
index 211ff44..eb99c70 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_dcache_addr(void *vaddr)
+{
+}
+static inline void invalidate_kernel_dcache_addr(void *vaddr)
+{
+}
 #endif
 
 #include <asm/kmap_types.h>
Signature · 1 line
1.6.3.3
#2 lore raw

PATCH 2/6 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 |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
arch/parisc/include/asm/cacheflush.h +8 -0
index 7243951..2536a00 100644
@@ -90,6 +90,14 @@ static inline void flush_kernel_dcache_page(struct page *page)
 {
 	flush_kernel_dcache_page_addr(page_address(page));
 }
+static inline void flush_kernel_dcache_addr(void *addr)
+{
+	flush_kernel_dcache_page_addr(addr);
+}
+static inline void invalidate_kernel_dcache_addr(void *addr)
+{
+	/* nop .. the flush prevents move in until the page is touched */
+}
 
 #ifdef CONFIG_DEBUG_RODATA
 void mark_rodata_ro(void);
Signature · 1 line
1.6.3.3
#3 lore raw

PATCH 3/6 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 1a711ea..1104ee9 100644
@@ -436,6 +436,16 @@ static inline void flush_kernel_dcache_page(struct page *page)
 	if ((cache_is_vivt() || cache_is_vipt_aliasing()) && !PageHighMem(page))
 		__cpuc_flush_dcache_page(page_address(page));
 }
+static inline void flush_kernel_dcache_addr(void *addr)
+{
+	if ((cache_is_vivt() || cache_is_vipt_aliasing()))
+		__cpuc_flush_dcache_page(addr);
+}
+static inline void invalidate_kernel_dcache_addr(void *addr)
+{
+	if ((cache_is_vivt() || cache_is_vipt_aliasing()))
+		__cpuc_flush_dcache_page(addr);
+}
 
 #define flush_dcache_mmap_lock(mapping) \
 	spin_lock_irq(&(mapping)->tree_lock)
Signature · 1 line
1.6.3.3
#4 lore raw

PATCH 4/6 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 4c5462d..db06611 100644
@@ -48,6 +48,14 @@ static inline void flush_kernel_dcache_page(struct page *page)
 {
 	flush_dcache_page(page);
 }
+static inline void flush_kernel_dcache_addr(void *addr)
+{
+	__flush_invalidate_region(addr, PAGE_SIZE);
+}
+static inline void invalidate_kernel_dcache_addr(void *addr)
+{
+	__flush_invalidate_region(addr, PAGE_SIZE);
+}
 
 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_CACHE_OFF)
 extern void copy_to_user_page(struct vm_area_struct *vma,
Signature · 1 line
1.6.3.3
#5 lore raw

PATCH 5/6 block: permit I/O to vmalloc/vmap kernel pages

This updates bio_map_kern() to check for pages in the vmalloc address range and call the new kernel flushing APIs if the are. This should allow any kernel user to pass a vmalloc/vmap area to block.

---
 fs/bio.c |   19 +++++++++++++++++--
 1 files changed, 17 insertions(+), 2 deletions(-)
fs/bio.c +17 -2
index 7673800..ea346b4 100644
@@ -1120,6 +1120,13 @@ void bio_unmap_user(struct bio *bio)
 
 static void bio_map_kern_endio(struct bio *bio, int err)
 {
+	void *kaddr = bio->bi_private;
+	if (is_vmalloc_addr(kaddr)) {
+		void *addr;
+		for (addr = kaddr; addr < kaddr + bio->bi_size;
+		     addr += PAGE_SIZE)
+			invalidate_kernel_dcache_addr(addr);
+	}
 	bio_put(bio);
 }
 
@@ -1138,9 +1145,12 @@ static struct bio *__bio_map_kern(struct request_queue *q, void *data,
 	if (!bio)
 		return ERR_PTR(-ENOMEM);
 
+	bio->bi_private = data;
+
 	offset = offset_in_page(kaddr);
 	for (i = 0; i < nr_pages; i++) {
 		unsigned int bytes = PAGE_SIZE - offset;
+		struct page *page;
 
 		if (len <= 0)
 			break;
@@ -1148,8 +1158,13 @@ static struct bio *__bio_map_kern(struct request_queue *q, void *data,
 		if (bytes > len)
 			bytes = len;
 
-		if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
-				    offset) < bytes)
+		if (is_vmalloc_addr(data)) {
+			flush_kernel_dcache_addr(data);
+			page = vmalloc_to_page(data);
+		} else
+			page = virt_to_page(data);
+
+		if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
 			break;
 
 		data += bytes;
Signature · 1 line
1.6.3.3
#6 lore raw

PATCH 6/6 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 |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
fs/xfs/linux-2.6/xfs_buf.c +10 -0
index 965df12..62ae977 100644
@@ -1138,6 +1138,10 @@ xfs_buf_bio_end_io(
 	do {
 		struct page	*page = bvec->bv_page;
 
+		if (is_vmalloc_addr(bp->b_addr))
+			invalidate_kernel_dcache_addr(bp->b_addr +
+						      bvec->bv_offset);
+
 		ASSERT(!PagePrivate(page));
 		if (unlikely(bp->b_error)) {
 			if (bp->b_flags & XBF_READ)
@@ -1202,6 +1206,9 @@ _xfs_buf_ioapply(
 		bio->bi_end_io = xfs_buf_bio_end_io;
 		bio->bi_private = bp;
 
+		if (is_vmalloc_addr(bp->b_addr))
+			flush_kernel_dcache_addr(bp->b_addr);
+
 		bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
 		size = 0;
 
@@ -1228,6 +1235,9 @@ next_chunk:
 		if (nbytes > size)
 			nbytes = size;
 
+		if (is_vmalloc_addr(bp->b_addr))
+			flush_kernel_dcache_addr(bp->b_addr + PAGE_SIZE*map_i);
+
 		rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
 		if (rbytes < nbytes)
 			break;
Signature · 1 line
1.6.3.3
#7 lore raw

Re: [PATCH 6/6] xfs: fix xfs to work with Virtually Indexed architectures

Quoted 4 lines · James Bottomley

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.

It's not really a handcrafted version of blk_rq_map_kern because it can add discontinuous into a single bio.

The patches look fine to (not pretty but fine :)), and I'll make sure they get added once those two helpers made it, probably augmented by a comment explaining what's going on here.

#8 lore raw

Re: [PATCH 6/6] xfs: fix xfs to work with Virtually Indexed architectures

In reply to James Bottomley
Quoted 15 lines · James Bottomley

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 |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
fs/xfs/linux-2.6/xfs_buf.c +0 -0
index 965df12..62ae977 100644

fs/xfs/linux-2.6/xfs_buf.c xfs_buf_bio_end_io( · line 1143

Quoted 6 lines
fs/xfs/linux-2.6/xfs_buf.c +3 -0
@@ -1138,6 +1138,10 @@ xfs_buf_bio_end_io(
 	do {
 		struct page	*page = bvec->bv_page;
 
+		if (is_vmalloc_addr(bp->b_addr))
+			invalidate_kernel_dcache_addr(bp->b_addr +
+						      bvec->bv_offset);

OK, so this invalidation logic is completely wrong. For large vmalloc buffers, xfs will split them up over several bios. The only way I can think to fix this is below ... comments?

If everyone is OK, I'll reroll the patches with this built in.

James

---

fs/xfs/linux-2.6/xfs_buf.c +13 -3
index 62ae977..320a6e4 100644
@@ -1132,15 +1132,25 @@ xfs_buf_bio_end_io(
 	xfs_buf_t		*bp = (xfs_buf_t *)bio->bi_private;
 	unsigned int		blocksize = bp->b_target->bt_bsize;
 	struct bio_vec		*bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
+	void			*vaddr = NULL;
+	int			i;
 
 	xfs_buf_ioerror(bp, -error);
 
+	if (is_vmalloc_addr(bp->b_addr))
+		for (i = 0; i < bp->b_page_count; i++)
+			if (bvec->bv_page == bp->b_pages[i]) {
+				vaddr = bp->b_addr + i*PAGE_SIZE;
+				break;
+			}
+
 	do {
 		struct page	*page = bvec->bv_page;
 
-		if (is_vmalloc_addr(bp->b_addr))
-			invalidate_kernel_dcache_addr(bp->b_addr +
-						      bvec->bv_offset);
+		if (is_vmalloc_addr(bp->b_addr)) {
+			invalidate_kernel_dcache_addr(vaddr);
+			vaddr -= PAGE_SIZE;
+		}
 
 		ASSERT(!PagePrivate(page));
 		if (unlikely(bp->b_error)) {
#9 lore raw

Re: [PATCH 4/6] sh: add mm API for DMA to vmalloc/vmap areas

In reply to James Bottomley
Quoted 12 lines · James Bottomley
---
 arch/sh/include/asm/cacheflush.h |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
arch/sh/include/asm/cacheflush.h +0 -0
index 4c5462d..db06611 100644
@@ -48,6 +48,14 @@ static inline void flush_kernel_dcache_page(struct page *page)
 {
 	flush_dcache_page(page);

arch/sh/include/asm/cacheflush.h static inline void flush_kernel_dcache_page(struct page *page) · line 59

Quoted 10 lines
arch/sh/include/asm/cacheflush.h +8 -0
 }
+static inline void flush_kernel_dcache_addr(void *addr)
+{
+	__flush_invalidate_region(addr, PAGE_SIZE);
+}
+static inline void invalidate_kernel_dcache_addr(void *addr)
+{
+	__flush_invalidate_region(addr, PAGE_SIZE);
+}
 

Actually, I spoke too soon. The first one should be either a __flush_purge_region() or __flush_wback_region() I suspect. The former does both a writeback and invalidate, while the latter only does the writeback.

#10 lore raw

Re: [PATCH 4/6] sh: add mm API for DMA to vmalloc/vmap areas

Quoted 19 lines · Paul Mundt
Quoted 18 lines · James Bottomley
---
 arch/sh/include/asm/cacheflush.h |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
arch/sh/include/asm/cacheflush.h +5 -0
index 4c5462d..db06611 100644
@@ -48,6 +48,14 @@ static inline void flush_kernel_dcache_page(struct page *page)
 {
 	flush_dcache_page(page);
 }
+static inline void flush_kernel_dcache_addr(void *addr)
+{
+	__flush_invalidate_region(addr, PAGE_SIZE);
+}
+static inline void invalidate_kernel_dcache_addr(void *addr)
Quoted 8 lines

arch/sh/include/asm/cacheflush.h static inline void flush_kernel_dcache_page(struct page *page) · line 59

Quoted 4 lines
arch/sh/include/asm/cacheflush.h +3 -0
+{
+	__flush_invalidate_region(addr, PAGE_SIZE);
+}
 

Actually, I spoke too soon. The first one should be either a __flush_purge_region() or __flush_wback_region() I suspect. The former does both a writeback and invalidate, while the latter only does the writeback.

OK, will update. I used the _flush_invalidate on the grounds that it must completely kill the cacheline, so it was the safest semantic ... actually, it's the only parisc semantic, so it's pretty much what occurs to me most of the time.

James

#11 lore raw
In reply to James Bottomley
Quoted 11 lines · James Bottomley

Here's version two of the patch set. It actually compiles on both x86 and parisc. I could do with someone to test it on arm and sh.

The key test is how xfs behaves. What I did to recreate the problem on parisc was simply create an 8GB xfs filesystem, use cp -a to pump about a GB of data into it from my git trees, then unmount and run xfs_check. Before the patches, xfs_check reports the whole fs to be corrupt. After the patches it reports everything to be OK.

James

I'm seeing some corruption with these patches on SH and a 4GB XFS filesystem. However, I am currently seeing other cache aliasing issues on this board anyway. I'll do some more analysis tonight, unless Paul can prove it's just my setup being weird again.

#12 lore raw
Quoted 10 lines · Matt Fleming
Quoted 9 lines · James Bottomley

Here's version two of the patch set. It actually compiles on both x86 and parisc. I could do with someone to test it on arm and sh.

The key test is how xfs behaves. What I did to recreate the problem on parisc was simply create an 8GB xfs filesystem, use cp -a to pump about a GB of data into it from my git trees, then unmount and run xfs_check. Before the patches, xfs_check reports the whole fs to be corrupt. After the patches it reports everything to be OK.

Quoted 7 lines
Quoted 2 lines

James

I'm seeing some corruption with these patches on SH and a 4GB XFS filesystem. However, I am currently seeing other cache aliasing issues on this board anyway. I'll do some more analysis tonight, unless Paul can prove it's just my setup being weird again.

Did some debug checking, and it turns out that the invalidation logic is bogus (the bio parameters get updated before the request completes). Of course, we wouldn't see this on parisc because we don't require invalidation ....

I'll try to fix and then repost.

Thanks for testing.

James