Skip to contentSkip to conversation

ext4: fix DAX dma vs truncate/hole-punch

PATCH Superseded Ross Zwisler 2 participants 7 messages Jun 20, 2018 – Jun 27, 2018 4 messages not in the archive

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover ext4: fix DAX dma vs truncate/hole-punch

This series from Dan:

https://lists.01.org/pipermail/linux-nvdimm/2018-March/014913.html

added synchronization between DAX dma and truncate/hole-punch in XFS. This short series adds analogous support to ext4.

I've added calls to ext4_break_layouts() everywhere that ext4 removes blocks from an inode's map.

The timings in XFS are such that it's difficult to hit this race. Dan was able to show the race by manually introducing delays in the direct I/O path.

For ext4, though, its trivial to hit this race, and a hit will result in a trigger of this WARN_ON_ONCE() in dax_disassociate_entry():

	WARN_ON_ONCE(trunc && page_ref_count(page) > 1);

I've made an xfstest which tests all the paths where we now call ext4_break_layouts(), with the exception of the site in ext4_truncate_failed_write() which is an error path. Each of the other 4 paths easily hits this race many times in my test setup with the xfstest. I'll post this test shortly.

With these patches applied, I've still seen occasional hits of the above WARN_ON_ONCE(), which tells me that we still have some work to do. I'll continue looking at these more rare hits while we review this set.

Ross Zwisler (2):
dax: dax_layout_busy_page() warn on !exceptional
ext4: handle layout changes to pinned DAX mappings

 fs/dax.c           |  9 ++++++++-
 fs/ext4/ext4.h     |  1 +
 fs/ext4/extents.c  | 12 ++++++++++++
 fs/ext4/inode.c    | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/truncate.h |  1 +
 5 files changed, 70 insertions(+), 1 deletion(-)
Signature · 1 line
2.14.4

A message that is not in the archive (sent off-list or not yet received): 20180620221503.25237-1-ross.zwisler-VuQAYsv1563Yd54FQh9/[email protected]

#2 lore raw

PATCH 1/2 dax: dax_layout_busy_page() warn on !exceptional

Inodes using DAX should only ever have exceptional entries in their page caches. Make this clear by warning if the iteration in dax_layout_busy_page() ever sees a non-exceptional entry, and by adding a comment for the pagevec_release() call which only deals with struct page pointers.

---
 fs/dax.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/dax.c +8 -1
index 641192808bb6..4a5e31f8a2d4 100644
@@ -566,7 +566,7 @@ struct page *dax_layout_busy_page(struct address_space *mapping)
 			if (index >= end)
 				break;
 
-			if (!radix_tree_exceptional_entry(pvec_ent))
+			if (WARN_ON_ONCE(!radix_tree_exceptional_entry(pvec_ent)))
 				continue;
 
 			xa_lock_irq(&mapping->i_pages);
@@ -578,6 +578,13 @@ struct page *dax_layout_busy_page(struct address_space *mapping)
 			if (page)
 				break;
 		}
+
+		/*
+		 * We don't expect normal struct page entries to exist in our
+		 * tree, but we keep these pagevec calls so that this code is
+		 * consistent with the common pattern for handling pagevecs
+		 * throughout the kernel.
+		 */
 		pagevec_remove_exceptionals(&pvec);
 		pagevec_release(&pvec);
 		index++;
Signature · 1 line
2.14.4

A message that is not in the archive (sent off-list or not yet received): 20180620221503.25237-2-ross.zwisler-VuQAYsv1563Yd54FQh9/[email protected]

Reviewed-by
#4 lore raw

Re: [PATCH 1/2] dax: dax_layout_busy_page() warn on !exceptional

Quoted 16 lines · Ross Zwisler

Inodes using DAX should only ever have exceptional entries in their page caches. Make this clear by warning if the iteration in dax_layout_busy_page() ever sees a non-exceptional entry, and by adding a comment for the pagevec_release() call which only deals with struct page pointers.

---
 fs/dax.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/dax.c +0 -0
index 641192808bb6..4a5e31f8a2d4 100644

fs/dax.c struct page *dax_layout_busy_page(struct address_space *mapping) · line 569

Quoted 5 lines
fs/dax.c +1 -1
@@ -566,7 +566,7 @@ struct page *dax_layout_busy_page(struct address_space *mapping)
 			if (index >= end)
 				break;
 
-			if (!radix_tree_exceptional_entry(pvec_ent))
+			if (WARN_ON_ONCE(!radix_tree_exceptional_entry(pvec_ent)))

Please wrap this line. Otherwise the patch looks good. You can add:

Honza

Signature · 2 lines
Jan Kara <jack-IBi9RG/[email protected]>
SUSE Labs, CR
#5 lore raw

PATCH 2/2 ext4: handle layout changes to pinned DAX mappings

In reply to a message not in the archive

Follow the lead of xfs_break_dax_layouts() and add synchronization between operations in ext4 which remove blocks from an inode (hole punch, truncate down, etc.) and pages which are pinned due to DAX DMA operations.

---
 fs/ext4/ext4.h     |  1 +
 fs/ext4/extents.c  | 12 ++++++++++++
 fs/ext4/inode.c    | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/truncate.h |  1 +
 4 files changed, 62 insertions(+)
fs/ext4/ext4.h +1 -0
index 0b127853c584..34bccd64d83d 100644
@@ -2460,6 +2460,7 @@ extern int ext4_get_inode_loc(struct inode *, struct ext4_iloc *);
 extern int ext4_inode_attach_jinode(struct inode *inode);
 extern int ext4_can_truncate(struct inode *inode);
 extern int ext4_truncate(struct inode *);
+extern int ext4_break_layouts(struct inode *);
 extern int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length);
 extern int ext4_truncate_restart_trans(handle_t *, struct inode *, int nblocks);
 extern void ext4_set_inode_flags(struct inode *);
fs/ext4/extents.c +12 -0
index 0057fe3f248d..a6aef06f455b 100644
@@ -4820,6 +4820,13 @@ static long ext4_zero_range(struct file *file, loff_t offset,
 		 * released from page cache.
 		 */
 		down_write(&EXT4_I(inode)->i_mmap_sem);
+
+		ret = ext4_break_layouts(inode);
+		if (ret) {
+			up_write(&EXT4_I(inode)->i_mmap_sem);
+			goto out_mutex;
+		}
+
 		ret = ext4_update_disksize_before_punch(inode, offset, len);
 		if (ret) {
 			up_write(&EXT4_I(inode)->i_mmap_sem);
@@ -5493,6 +5500,11 @@ int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
 	 * page cache.
 	 */
 	down_write(&EXT4_I(inode)->i_mmap_sem);
+
+	ret = ext4_break_layouts(inode);
+	if (ret)
+		goto out_mmap;
+
 	/*
 	 * Need to round down offset to be aligned with page size boundary
 	 * for page size > block size.
fs/ext4/inode.c +48 -0

66 lines not on this page · raw message

fs/ext4/truncate.h +1 -0
index 0cb13badf473..a3b78241e9f6 100644
@@ -12,6 +12,7 @@
 static inline void ext4_truncate_failed_write(struct inode *inode)
 {
 	down_write(&EXT4_I(inode)->i_mmap_sem);
+	ext4_break_layouts(inode);
 	truncate_inode_pages(inode->i_mapping, inode->i_size);
 	ext4_truncate(inode);
 	up_write(&EXT4_I(inode)->i_mmap_sem);
Signature · 1 line
2.14.4

A message that is not in the archive (sent off-list or not yet received): 20180620221503.25237-3-ross.zwisler-VuQAYsv1563Yd54FQh9/[email protected]

Reviewed-by
#7 lore raw

Re: [PATCH 2/2] ext4: handle layout changes to pinned DAX mappings

Quoted 71 lines · Ross Zwisler

Follow the lead of xfs_break_dax_layouts() and add synchronization between operations in ext4 which remove blocks from an inode (hole punch, truncate down, etc.) and pages which are pinned due to DAX DMA operations.

---
 fs/ext4/ext4.h     |  1 +
 fs/ext4/extents.c  | 12 ++++++++++++
 fs/ext4/inode.c    | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/truncate.h |  1 +
 4 files changed, 62 insertions(+)
fs/ext4/ext4.h +1 -0
index 0b127853c584..34bccd64d83d 100644
@@ -2460,6 +2460,7 @@ extern int ext4_get_inode_loc(struct inode *, struct ext4_iloc *);
 extern int ext4_inode_attach_jinode(struct inode *inode);
 extern int ext4_can_truncate(struct inode *inode);
 extern int ext4_truncate(struct inode *);
+extern int ext4_break_layouts(struct inode *);
 extern int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length);
 extern int ext4_truncate_restart_trans(handle_t *, struct inode *, int nblocks);
 extern void ext4_set_inode_flags(struct inode *);
fs/ext4/extents.c +12 -0
index 0057fe3f248d..a6aef06f455b 100644
@@ -4820,6 +4820,13 @@ static long ext4_zero_range(struct file *file, loff_t offset,
 		 * released from page cache.
 		 */
 		down_write(&EXT4_I(inode)->i_mmap_sem);
+
+		ret = ext4_break_layouts(inode);
+		if (ret) {
+			up_write(&EXT4_I(inode)->i_mmap_sem);
+			goto out_mutex;
+		}
+
 		ret = ext4_update_disksize_before_punch(inode, offset, len);
 		if (ret) {
 			up_write(&EXT4_I(inode)->i_mmap_sem);
@@ -5493,6 +5500,11 @@ int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
 	 * page cache.
 	 */
 	down_write(&EXT4_I(inode)->i_mmap_sem);
+
+	ret = ext4_break_layouts(inode);
+	if (ret)
+		goto out_mmap;
+
 	/*
 	 * Need to round down offset to be aligned with page size boundary
 	 * for page size > block size.
fs/ext4/inode.c +9 -0
index 2ea07efbe016..c795e5118745 100644
@@ -4193,6 +4193,41 @@ int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
 	return 0;
 }
 
+static void ext4_wait_dax_page(struct ext4_inode_info *ei, bool *did_unlock)
+{
+	*did_unlock = true;
+	up_write(&ei->i_mmap_sem);
+	schedule();
+	down_write(&ei->i_mmap_sem);
+}
+
+int ext4_break_layouts(struct inode *inode)

fs/ext4/inode.c int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, · line 4214

Quoted 10 lines
fs/ext4/inode.c +10 -0
+{
+	struct ext4_inode_info *ei = EXT4_I(inode);
+	struct page *page;
+	bool retry;
+	int error;
+
+	if (unlikely(!rwsem_is_locked(&ei->i_mmap_sem))) {
+		WARN_ON_ONCE(1);
+		return -EINVAL;
+	}

This could be shortened as:

if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem))) { }

couldn't it?

Besides that the patch looks to me. You can add:

And I'm really wondering which protection are we still missing that you are still able to hit the warning with these patches applied.

Honza

Signature · 2 lines
Jan Kara <jack-IBi9RG/[email protected]>
SUSE Labs, CR

A message that is not in the archive (sent off-list or not yet received): 20180622081915.dvqd5sxx7vzoijw3-4I4JzKEfoa/[email protected]

#9 lore raw

Re: [PATCH 2/2] ext4: handle layout changes to pinned DAX mappings

Quoted 85 lines · Jan Kara
Quoted 71 lines · Ross Zwisler

Follow the lead of xfs_break_dax_layouts() and add synchronization between operations in ext4 which remove blocks from an inode (hole punch, truncate down, etc.) and pages which are pinned due to DAX DMA operations.

---
 fs/ext4/ext4.h     |  1 +
 fs/ext4/extents.c  | 12 ++++++++++++
 fs/ext4/inode.c    | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/truncate.h |  1 +
 4 files changed, 62 insertions(+)
fs/ext4/ext4.h +1 -0
index 0b127853c584..34bccd64d83d 100644
@@ -2460,6 +2460,7 @@ extern int ext4_get_inode_loc(struct inode *, struct ext4_iloc *);
 extern int ext4_inode_attach_jinode(struct inode *inode);
 extern int ext4_can_truncate(struct inode *inode);
 extern int ext4_truncate(struct inode *);
+extern int ext4_break_layouts(struct inode *);
 extern int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length);
 extern int ext4_truncate_restart_trans(handle_t *, struct inode *, int nblocks);
 extern void ext4_set_inode_flags(struct inode *);
fs/ext4/extents.c +12 -0
index 0057fe3f248d..a6aef06f455b 100644
@@ -4820,6 +4820,13 @@ static long ext4_zero_range(struct file *file, loff_t offset,
 		 * released from page cache.
 		 */
 		down_write(&EXT4_I(inode)->i_mmap_sem);
+
+		ret = ext4_break_layouts(inode);
+		if (ret) {
+			up_write(&EXT4_I(inode)->i_mmap_sem);
+			goto out_mutex;
+		}
+
 		ret = ext4_update_disksize_before_punch(inode, offset, len);
 		if (ret) {
 			up_write(&EXT4_I(inode)->i_mmap_sem);
@@ -5493,6 +5500,11 @@ int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
 	 * page cache.
 	 */
 	down_write(&EXT4_I(inode)->i_mmap_sem);
+
+	ret = ext4_break_layouts(inode);
+	if (ret)
+		goto out_mmap;
+
 	/*
 	 * Need to round down offset to be aligned with page size boundary
 	 * for page size > block size.
fs/ext4/inode.c +9 -0
index 2ea07efbe016..c795e5118745 100644
@@ -4193,6 +4193,41 @@ int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
 	return 0;
 }
 
+static void ext4_wait_dax_page(struct ext4_inode_info *ei, bool *did_unlock)
+{
+	*did_unlock = true;
+	up_write(&ei->i_mmap_sem);
+	schedule();
+	down_write(&ei->i_mmap_sem);
+}
+
+int ext4_break_layouts(struct inode *inode)

fs/ext4/inode.c int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, · line 4214

Quoted 10 lines
fs/ext4/inode.c +10 -0
+{
+	struct ext4_inode_info *ei = EXT4_I(inode);
+	struct page *page;
+	bool retry;
+	int error;
+
+	if (unlikely(!rwsem_is_locked(&ei->i_mmap_sem))) {
+		WARN_ON_ONCE(1);
+		return -EINVAL;
+	}

This could be shortened as:

Quoted 4 lines

if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem))) { }

couldn't it?

Yep, that's much better. Thank you for the review.

Quoted 6 lines

Besides that the patch looks to me. You can add:

And I'm really wondering which protection are we still missing that you are still able to hit the warning with these patches applied.

I'm also very curious and am going to dig into that this week.

#10 lore raw

Re: [PATCH 2/2] ext4: handle layout changes to pinned DAX mappings

In reply to a message not in the archive

fs/ext4/truncate.h line 18

Quoted 12 lines · Ross Zwisler
fs/ext4/truncate.h +1 -0
index 0cb13badf473..a3b78241e9f6 100644
@@ -12,6 +12,7 @@
 static inline void ext4_truncate_failed_write(struct inode *inode)
 {
 	down_write(&EXT4_I(inode)->i_mmap_sem);
+	ext4_break_layouts(inode);
 	truncate_inode_pages(inode->i_mapping, inode->i_size);
 	ext4_truncate(inode);
 	up_write(&EXT4_I(inode)->i_mmap_sem);

One comment here: I don't think ext4_break_layouts() is necessary here. ext4_truncate_failed_write() exists to truncate blocks beyond EOF. As such these blocks could never be even mmaped, let alone pinned by GUP. Maybe just add a comment that blocks we are truncating here were never visible to userspace and so we don't need ext4_break_layouts() protection.

Honza

Signature · 2 lines
Jan Kara <jack-IBi9RG/[email protected]>
SUSE Labs, CR