From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AH8x225Iz8J76XMD2bCs8lG7x6Uvy3q12ix9bfLJM7hSovZAJoLUEykJjbQFVlkFar5DDRfSUCUP ARC-Seal: i=1; a=rsa-sha256; t=1517256270; cv=none; d=google.com; s=arc-20160816; b=Yv3gdGHzF7/AlfkkU+Ln91msdfoqYDu1oTfhV32SWkzfp3x8zBzZ5Exa/aTqCU9vsY aAv9w3yeCe7FN5ExHMw3kP24HEcf8I53oAJCp6umyAWRsISvU85jqUDpszMxHWpnoPpG EAji/t9ls/I1/rsely16LDmGCCQxR+zKFNrMc0sjQsValeNA5w+/hYBvvD2u+JV/FCHy uULHxqX8zBHlyRdmr8NOHA9GmGn/KCx6cNStBJrRYlnXaNM/VuQfqGE1Ik8+Bn0JHhwp JxRFprekl0Sx+sFf+t+oFLqzAbisOpSqRtQquUraXWP7IO98EHTQn5JpeH+vUxA+wDsH /0Qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=AVlRrXfrSEotBJQaBc6nzZlyRMaHKao5b8ilZeiDHxs=; b=wpUlGhXeDYtI9MDBj/AaONucCA2H0xwWlmLA28D+zqdy3MwbVtEmyyR21gs1s3fncl +muMcTEnDlUL4Q2sbBmBa7trYM2WtxHZCQwsRzOCs2KICZyCoJMEJf6f6D1B0tw0Dgq8 FAe8GlprHuFcyqCxw0F+t1kEvCLt+uOXNvMxlZUGijzuuryQPNhBlJOMLvfDU1bVyaiP ZX2YokyVp2u0EA6AVeCjlyMskh4jkEeFXfCEqUNTGQ90C/JjFLI67OZbSB4ajotQyQ7W 7WDvtARCUR5fVvIDZoTM1oeypk4nlxKLgt4Ue2le4lTDL5mMYnPPjti8a83qx016I0Rb watQ== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning gregkh@linuxfoundation.org does not designate 90.92.71.90 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning gregkh@linuxfoundation.org does not designate 90.92.71.90 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jeff Mahoney , Jan Kara Subject: [PATCH 4.9 22/66] reiserfs: fix race in prealloc discard Date: Mon, 29 Jan 2018 13:56:46 +0100 Message-Id: <20180129123840.993659227@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180129123839.842860149@linuxfoundation.org> References: <20180129123839.842860149@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-LABELS: =?utf-8?b?IlxcU2VudCI=?= X-GMAIL-THRID: =?utf-8?q?1590958510614710010?= X-GMAIL-MSGID: =?utf-8?q?1590958510614710010?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jeff Mahoney 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 Signed-off-by: Jan Kara Signed-off-by: Greg Kroah-Hartman --- fs/reiserfs/bitmap.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) --- a/fs/reiserfs/bitmap.c +++ b/fs/reiserfs/bitmap.c @@ -513,9 +513,17 @@ static void __discard_prealloc(struct re "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)