Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Btrfs: fix race of using total_bytes_pinned

This percpu counter @total_bytes_pinned is introduced to skip unnecessary
operations of 'commit transaction', it accounts for those space we may free
but are stuck in delayed refs.

And we zero out @space_info->total_bytes_pinned every transaction period so
we have a better idea of how much space we'll actually free up by committing
this transaction. However, we do the 'zero out' part a little earlier, before
we actually unpin space, so we end up returning ENOSPC when we actually have
free space that's just unpinned from committing transaction.

xfstests/generic/074 complained then.

This fixes it by actually accounting the percpu pinned number when 'unpin',
and since it's protected by space_info->lock, the race is gone now.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>

authored by

Liu Bo and committed by
Chris Mason
d288db5d 130d5b41

+1 -4
+1 -4
fs/btrfs/extent-tree.c
··· 5678 5678 struct btrfs_caching_control *next; 5679 5679 struct btrfs_caching_control *caching_ctl; 5680 5680 struct btrfs_block_group_cache *cache; 5681 - struct btrfs_space_info *space_info; 5682 5681 5683 5682 down_write(&fs_info->commit_root_sem); 5684 5683 ··· 5699 5700 fs_info->pinned_extents = &fs_info->freed_extents[0]; 5700 5701 5701 5702 up_write(&fs_info->commit_root_sem); 5702 - 5703 - list_for_each_entry_rcu(space_info, &fs_info->space_info, list) 5704 - percpu_counter_set(&space_info->total_bytes_pinned, 0); 5705 5703 5706 5704 update_global_block_rsv(fs_info); 5707 5705 } ··· 5737 5741 spin_lock(&cache->lock); 5738 5742 cache->pinned -= len; 5739 5743 space_info->bytes_pinned -= len; 5744 + percpu_counter_add(&space_info->total_bytes_pinned, -len); 5740 5745 if (cache->ro) { 5741 5746 space_info->bytes_readonly += len; 5742 5747 readonly = true;