add patch iterate-over-buffer-heads-correctly-in-move_extent_per_page
[ext4-patch-queue.git] / fix-bh-b_state-corruption
blobf9d4f22c77663890de2b0aa4789cd7eaf54a7ad7
1 ext4: fix bh->b_state corruption
3 From: Jan Kara <jack@suse.com>
5 ext4 can update bh->b_state non-atomically in _ext4_get_block() and
6 ext4_da_get_block_prep(). Usually this is fine since bh is just a
7 temporary storage for mapping information on stack but in some cases it
8 can be fully living bh attached to a page. In such case non-atomic
9 update of bh->b_state can race with an atomic update which then gets
10 lost. Usually when we are mapping bh and thus updating bh->b_state
11 non-atomically, nobody else touches the bh and so things work out fine
12 but there is one case to especially worry about: ext4_finish_bio() uses
13 BH_Uptodate_Lock on the first bh in the page to synchronize handling of
14 PageWriteback state. So when blocksize < pagesize, we can be atomically
15 modifying bh->b_state of a buffer that actually isn't under IO and thus
16 can race e.g. with delalloc trying to map that buffer. The result is
17 that we can mistakenly set / clear BH_Uptodate_Lock bit resulting in the
18 corruption of PageWriteback state or missed unlock of BH_Uptodate_Lock.
20 Fix the problem by always updating bh->b_state bits atomically.
22 CC: stable@vger.kernel.org
23 Reported-by: Nikolay Borisov <kernel@kyup.com>
24 Signed-off-by: Jan Kara <jack@suse.cz>
25 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
26 ---
27  fs/ext4/inode.c | 32 ++++++++++++++++++++++++++++++--
28  1 file changed, 30 insertions(+), 2 deletions(-)
30 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
31 index f72dc04..e646abb 100644
32 --- a/fs/ext4/inode.c
33 +++ b/fs/ext4/inode.c
34 @@ -690,6 +690,34 @@ out_sem:
35         return retval;
36  }
38 +/*
39 + * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
40 + * we have to be careful as someone else may be manipulating b_state as well.
41 + */
42 +static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
44 +       unsigned long old_state;
45 +       unsigned long new_state;
47 +       flags &= EXT4_MAP_FLAGS;
49 +       /* Dummy buffer_head? Set non-atomically. */
50 +       if (!bh->b_page) {
51 +               bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
52 +               return;
53 +       }
54 +       /*
55 +        * Someone else may be modifying b_state. Be careful! This is ugly but
56 +        * once we get rid of using bh as a container for mapping information
57 +        * to pass to / from get_block functions, this can go away.
58 +        */
59 +       do {
60 +               old_state = READ_ONCE(bh->b_state);
61 +               new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
62 +       } while (unlikely(
63 +                cmpxchg(&bh->b_state, old_state, new_state) != old_state));
66  /* Maximum number of blocks we map for direct IO at once. */
67  #define DIO_MAX_BLOCKS 4096
69 @@ -726,7 +754,7 @@ static int _ext4_get_block(struct inode *inode, sector_t iblock,
70                 ext4_io_end_t *io_end = ext4_inode_aio(inode);
72                 map_bh(bh, inode->i_sb, map.m_pblk);
73 -               bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
74 +               ext4_update_bh_state(bh, map.m_flags);
75                 if (io_end && io_end->flag & EXT4_IO_END_UNWRITTEN)
76                         set_buffer_defer_completion(bh);
77                 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
78 @@ -1692,7 +1720,7 @@ int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
79                 return ret;
81         map_bh(bh, inode->i_sb, map.m_pblk);
82 -       bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
83 +       ext4_update_bh_state(bh, map.m_flags);
85         if (buffer_unwritten(bh)) {
86                 /* A delayed write to unwritten bh should be marked