linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] Btrfs: fix the race between write back and nocow buffered write
@ 2013-12-27 13:11 Miao Xie
  2013-12-27 13:11 ` [PATCH 2/2] Btrfs: fix the wrong nocow range check Miao Xie
  2014-01-08 15:09 ` [PATCH 1/2] Btrfs: fix the race between write back and nocow buffered write Josef Bacik
  0 siblings, 2 replies; 4+ messages in thread
From: Miao Xie @ 2013-12-27 13:11 UTC (permalink / raw)
  To: linux-btrfs; +Cc: t-itoh

When we ran the 274th case of xfstests with nodatacow mount option,
We met the following warning message:
WARNING: CPU: 1 PID: 14185 at fs/btrfs/extent-tree.c:3734 btrfs_free_reserved_data_space+0xa6/0xd0

It is caused by the race between the write back and nocow buffered
write:
  Task1				Task2
  __btrfs_buffered_write()
    skip data reservation
    reserve the metadata space
    copy the data
    dirty the pages
    unlock the pages
				write back the pages
				release the data space
   				  becasue there is no
				  noreserve flag
   set the noreserve flag

This patch fixes this problem by unlocking the pages after
the noreserve flag is set.

Reported-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
---
 fs/btrfs/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 82d0342..f6960c2 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1523,7 +1523,6 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
 		}
 
 		release_bytes = 0;
-		btrfs_drop_pages(pages, num_pages);
 
 		if (only_release_metadata && copied > 0) {
 			u64 lockstart = round_down(pos, root->sectorsize);
@@ -1536,6 +1535,8 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
 			only_release_metadata = false;
 		}
 
+		btrfs_drop_pages(pages, num_pages);
+
 		cond_resched();
 
 		balance_dirty_pages_ratelimited(inode->i_mapping);
-- 
1.8.1.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] Btrfs: fix the wrong nocow range check
  2013-12-27 13:11 [PATCH 1/2] Btrfs: fix the race between write back and nocow buffered write Miao Xie
@ 2013-12-27 13:11 ` Miao Xie
  2014-01-08 15:09 ` [PATCH 1/2] Btrfs: fix the race between write back and nocow buffered write Josef Bacik
  1 sibling, 0 replies; 4+ messages in thread
From: Miao Xie @ 2013-12-27 13:11 UTC (permalink / raw)
  To: linux-btrfs; +Cc: t-itoh

The following warning message was outputed when running the 274th case
of xfstests with nodatacow option:
 BUG: Bad page state in process kswapd0  pfn:1c66f
 page:ffffea0000636848 count:0 mapcount:0 mapping:(null) index:0x78000
 page flags: 0x1000000000100a(error|uptodate|private_2)

It is because the check of nocow range was wrong, we should compare the
start and end position of the extent with the write position to verify
if the write position was in the extent, but the current code just used
the start postion to do the check, so we got the wrong extent and told
the caller that it was a nocow write. And then when we write back the
dirty pages, we found we should cow the extent, but at that time, there
was no space in the fs, we had to the error flag for the page. When
someone reclaimed that page, the above warning outputed. Fix it.

Reported-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
---
 fs/btrfs/inode.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index f1a7744..f2c6f91 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -6386,6 +6386,7 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
 	int slot;
 	int found_type;
 	bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
+
 	path = btrfs_alloc_path();
 	if (!path)
 		return -ENOMEM;
@@ -6429,6 +6430,10 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
 	if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
 		goto out;
 
+	extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
+	if (extent_end <= offset)
+		goto out;
+
 	disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
 	if (disk_bytenr == 0)
 		goto out;
@@ -6446,8 +6451,6 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
 		*ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
 	}
 
-	extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
-
 	if (btrfs_extent_readonly(root, disk_bytenr))
 		goto out;
 	btrfs_release_path(path);
-- 
1.8.1.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] Btrfs: fix the race between write back and nocow buffered write
  2013-12-27 13:11 [PATCH 1/2] Btrfs: fix the race between write back and nocow buffered write Miao Xie
  2013-12-27 13:11 ` [PATCH 2/2] Btrfs: fix the wrong nocow range check Miao Xie
@ 2014-01-08 15:09 ` Josef Bacik
  2014-01-09  2:06   ` [PATCH V2 " Miao Xie
  1 sibling, 1 reply; 4+ messages in thread
From: Josef Bacik @ 2014-01-08 15:09 UTC (permalink / raw)
  To: Miao Xie, linux-btrfs; +Cc: t-itoh


On 12/27/2013 08:11 AM, Miao Xie wrote:
> When we ran the 274th case of xfstests with nodatacow mount option,
> We met the following warning message:
> WARNING: CPU: 1 PID: 14185 at fs/btrfs/extent-tree.c:3734 btrfs_free_reserved_data_space+0xa6/0xd0
>
> It is caused by the race between the write back and nocow buffered
> write:
>    Task1				Task2
>    __btrfs_buffered_write()
>      skip data reservation
>      reserve the metadata space
>      copy the data
>      dirty the pages
>      unlock the pages
> 				write back the pages
> 				release the data space
>     				  becasue there is no
> 				  noreserve flag
>     set the noreserve flag
>
> This patch fixes this problem by unlocking the pages after
> the noreserve flag is set.
>
> Reported-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
> Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
> ---
>   fs/btrfs/file.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
> index 82d0342..f6960c2 100644
> --- a/fs/btrfs/file.c
> +++ b/fs/btrfs/file.c
> @@ -1523,7 +1523,6 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
>   		}
>   
>   		release_bytes = 0;
> -		btrfs_drop_pages(pages, num_pages);
>   

This isn't based on your other patch

     Btrfs: fix the reserved space leak caused by the race between 
nonlock dio and buffered io

You need to redo this one ontop of that patch, I'll take 2/2 tho. Thanks,

Josef

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH V2 1/2] Btrfs: fix the race between write back and nocow buffered write
  2014-01-08 15:09 ` [PATCH 1/2] Btrfs: fix the race between write back and nocow buffered write Josef Bacik
@ 2014-01-09  2:06   ` Miao Xie
  0 siblings, 0 replies; 4+ messages in thread
From: Miao Xie @ 2014-01-09  2:06 UTC (permalink / raw)
  To: linux-btrfs

When we ran the 274th case of xfstests with nodatacow mount option,
We met the following warning message:
WARNING: CPU: 1 PID: 14185 at fs/btrfs/extent-tree.c:3734 btrfs_free_reserved_data_space+0xa6/0xd0

It is caused by the race between the write back and nocow buffered
write:
  Task1				Task2
  __btrfs_buffered_write()
    skip data reservation
    reserve the metadata space
    copy the data
    dirty the pages
    unlock the pages
				write back the pages
				release the data space
   				  becasue there is no
				  noreserve flag
   set the noreserve flag

This patch fixes this problem by unlocking the pages after
the noreserve flag is set.

Reported-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
---
Changelog v1 -> v2:
- rebase the patch to the btrfs-next tree
---
 fs/btrfs/file.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 453e43c..0218288 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1555,9 +1555,10 @@ again:
 			unlock_extent_cached(&BTRFS_I(inode)->io_tree,
 					     lockstart, lockend, &cached_state,
 					     GFP_NOFS);
-		btrfs_drop_pages(pages, num_pages);
-		if (ret)
+		if (ret) {
+			btrfs_drop_pages(pages, num_pages);
 			break;
+		}
 
 		release_bytes = 0;
 		if (only_release_metadata && copied > 0) {
@@ -1571,6 +1572,8 @@ again:
 			only_release_metadata = false;
 		}
 
+		btrfs_drop_pages(pages, num_pages);
+
 		cond_resched();
 
 		balance_dirty_pages_ratelimited(inode->i_mapping);
-- 
1.8.1.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-01-09  2:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-27 13:11 [PATCH 1/2] Btrfs: fix the race between write back and nocow buffered write Miao Xie
2013-12-27 13:11 ` [PATCH 2/2] Btrfs: fix the wrong nocow range check Miao Xie
2014-01-08 15:09 ` [PATCH 1/2] Btrfs: fix the race between write back and nocow buffered write Josef Bacik
2014-01-09  2:06   ` [PATCH V2 " Miao Xie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).