From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AH8x224QPxomKpgY/buf3GI/IESPFjU/YxX3qC2c2pBUwp3I1HN5eEe5Fmwzla5E/kCmMEvHQnKN ARC-Seal: i=1; a=rsa-sha256; t=1517256391; cv=none; d=google.com; s=arc-20160816; b=guFQsQmDgvM3u5fgTJ78rT5pwcIOQwgyK/a9xh7FBgA5gPE7AalcAAHvm7VmPp8Fmv xeEVzvbCawaTfcwOQ09JdObTlwIM7xZ9xjoR25K9ljRw3Kkx4BPCiB6G72COaVTBeF0V QiD+0t8njw1hU9pHNB02e2idh3mQTFeB7vVF8OvmihMOLzBI1mrYZNR7LBEGhLFHFm1/ yUxpSUDuyvr3wV8O4snnA7tVuXn8cFfN7Ggs930P1anMpK/g9U/fIUA6xlBvbm4yb4BB 0PuVyza1QgtSKtSSTup0imopBBJDB7WoqsYkRLFCNMio0n2zVMdq7iTLihgYtDYV6cGz lVmg== 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=6605MHThN8wtTfiVfBwosScrtRlxiockwWQMaojg3n8=; b=hZh0Md0gwgwISefK8+faLhX3w2D3Q5Mk2c6XmPF3QGh5+Bo9uegV6NKfpBNzK+MM++ 4NP1dg4011kFjcLKyDgXJhNo4LWVrbiNlY3OHBfy9GnFmZZZmD3CpTCtfm66LCb+l34k TVL1q2hYfOoa2F5Ufe5xUr2uZFHggcbCtPBCYrRwzLddFIyTTsJVOR7aqGSB3xbKlP/k O63VwjXVgVNAY/TwXFnKUe03sbpZrBgvwk4v8oD067CsaiJBwtgVWTVBqOH+NfeUk48R y+FXm+CNj6YnWdCv7xbQa0iI8ZYjTwavDYvOAioJ1+74Mzprk9jkzFHmLSncWnWIDsqQ UqOw== 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.4 42/74] reiserfs: fix race in prealloc discard Date: Mon, 29 Jan 2018 13:56:47 +0100 Message-Id: <20180129123849.475323029@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180129123847.507563674@linuxfoundation.org> References: <20180129123847.507563674@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?1590958638079498482?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: 4.4-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)