OSDN Git Service

reiserfs: fix race in prealloc discard
authorJeff Mahoney <jeffm@suse.com>
Thu, 22 Jun 2017 20:47:34 +0000 (16:47 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 31 Jan 2018 11:06:11 +0000 (12:06 +0100)
commit 08db141b5313ac2f64b844fb5725b8d81744b417 upstream.

The main loop in __discard_prealloc is protected by the reiserfs write lock
which is dropped across schedules like the BKL it replaced.  The problem is
that it checks the value, calls a routine that schedules, and then adjusts
the state.  As a result, two threads that are calling
reiserfs_prealloc_discard at the same time can race when one calls
reiserfs_free_prealloc_block, the lock is dropped, and the other calls
reiserfs_free_prealloc_block with the same block number.  In the right
circumstances, it can cause the prealloc count to go negative.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/reiserfs/bitmap.c

index dc198bc..73705d4 100644 (file)
@@ -513,9 +513,17 @@ static void __discard_prealloc(struct reiserfs_transaction_handle *th,
                               "inode has negative prealloc blocks count.");
 #endif
        while (ei->i_prealloc_count > 0) {
-               reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
-               ei->i_prealloc_block++;
+               b_blocknr_t block_to_free;
+
+               /*
+                * reiserfs_free_prealloc_block can drop the write lock,
+                * which could allow another caller to free the same block.
+                * We can protect against it by modifying the prealloc
+                * state before calling it.
+                */
+               block_to_free = ei->i_prealloc_block++;
                ei->i_prealloc_count--;
+               reiserfs_free_prealloc_block(th, inode, block_to_free);
                dirty = 1;
        }
        if (dirty)