ext4: Fix data races when using cached status extents
Jan Kara
reviewerPATCH 1/1 ext4: Fix data races when using cached status extents
When using cached extent stored in extent status tree in tree->cache_es another process holding ei->i_es_lock for reading can be racing with us setting new value of tree->cache_es. If the compiler would decide to refetch tree->cache_es at an unfortunate moment, it could result in a bogus in_range() check. Fix the possible race by using READ_ONCE() when using tree->cache_es only under ei->i_es_lock for reading.
--- fs/ext4/extents_status.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-)
fs/ext4/extents_status.c +12 -16
@@ -267,14 +267,12 @@ static void __es_find_extent_range(struct inode *inode,/* see if the extent has been cached */ es->es_lblk = es->es_len = es->es_pblk =0 ; -if (tree->cache_es) { - es1 = tree->cache_es; -if (in_range (lblk, es1->es_lblk, es1->es_len)) { -es_debug ("%u cached by [%u/%u) %llu %x\n" , - lblk, es1->es_lblk, es1->es_len, -ext4_es_pblock (es1),ext4_es_status (es1)); - goto out; - } + es1 =READ_ONCE (tree->cache_es); +if (es1 &&in_range (lblk, es1->es_lblk, es1->es_len)) { +es_debug ("%u cached by [%u/%u) %llu %x\n" , + lblk, es1->es_lblk, es1->es_len, +ext4_es_pblock (es1),ext4_es_status (es1)); + goto out; } es1 =__es_tree_search (&tree->root, lblk);
@@ -931,14 +929,12 @@ int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,/* find extent in cache firstly */ es->es_lblk = es->es_len = es->es_pblk =0 ; -if (tree->cache_es) { - es1 = tree->cache_es; -if (in_range (lblk, es1->es_lblk, es1->es_len)) { -es_debug ("%u cached by [%u/%u)\n" , - lblk, es1->es_lblk, es1->es_len); - found =1 ; - goto out; - } + es1 =READ_ONCE (tree->cache_es); +if (es1 &&in_range (lblk, es1->es_lblk, es1->es_len)) { +es_debug ("%u cached by [%u/%u)\n" , + lblk, es1->es_lblk, es1->es_len); + found =1 ; + goto out; } node = tree->root.rb_node;
Signature · 1 line
2.35.3
Theodore Ts'o
maintainerQuoted 11 lines · Jan Kara
When using cached extent stored in extent status tree in
tree->cache_esanother process holdingei->i_es_lockfor reading can be racing with us setting new value oftree->cache_es. If the compiler would decide to refetchtree->cache_esat an unfortunate moment, it could result in a bogusin_range()check. Fix the possible race by usingREAD_ONCE()when usingtree->cache_esonly underei->i_es_lockfor reading.Reported-by [email protected]Suggested-by Dmitry Vyukov <[email protected]>Signed-off-by Jan Kara <[email protected]>
Don't we also need a WRITE_ONCE here?
fs/ext4/extents_status.c +1 -1
@@ -293,7 +293,7 @@ static void __es_find_extent_range(struct inode *inode, }if (es1 &&matching_fn (es1)) { - tree->cache_es = es1; +WRITE_ONCE (tree->cache_es, es1); es->es_lblk = es1->es_lblk; es->es_len = es1->es_len; es->es_pblk = es1->es_pblk;
- Ted
Jan Kara
reviewerQuoted 8 lines · Theodore Ts'o
Quoted 7 lines · Jan Kara
When using cached extent stored in extent status tree in
tree->cache_esanother process holdingei->i_es_lockfor reading can be racing with us setting new value oftree->cache_es. If the compiler would decide to refetchtree->cache_esat an unfortunate moment, it could result in a bogusin_range()check. Fix the possible race by usingREAD_ONCE()when usingtree->cache_esonly underei->i_es_lockfor reading.
Quoted 6 lines
Quoted 4 lines
Reported-by [email protected]Suggested-by Dmitry Vyukov <[email protected]>Signed-off-by Jan Kara <[email protected]>Don't we also need a
WRITE_ONCEhere?
Right, we should do that as well. I'll update the patch.
Honza
Quoted 16 lines
fs/ext4/extents_status.c +1
-1@@ -293,7 +293,7 @@ static void __es_find_extent_range(struct inode *inode, }if (es1 &&matching_fn (es1)) { - tree->cache_es = es1; +WRITE_ONCE (tree->cache_es, es1); es->es_lblk = es1->es_lblk; es->es_len = es1->es_len; es->es_pblk = es1->es_pblk;- Ted
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR