From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AH8x224CJHlYDHlnT0CvPGZNbqevMS2qPmBE4yr7/WUDHHIVuDccdIj1Q8a1JzvTFf1v3iRGlVG3 ARC-Seal: i=1; a=rsa-sha256; t=1517256700; cv=none; d=google.com; s=arc-20160816; b=HCcZgPAboRTNCAqejqcfNNRycpRO4eGkN+yxqQ+QixPT8VkTvTVVP1jH0oqpUqhTJ1 ILDNyUwUgSurDyzqWJgBfAT7a1tbkq4TtAWLeCeaNHjYg0DHkCe+NPybduSp5Mj4GPHD fgcbviOoGQOg7pd60Wp2B1kuVRNa3bZ7HVPxCBEFUlgMHEqog67/bt+KAt/lpYVigyvc cdRvbMQWzZysiZUPIErCYX5TwzJnIZyljOaWzjNaa+TwlXxuiAQ+1v2sUHz9imuhaMMi hRHgAh0BaUD0CVyLwBTc7VJFmivHRt58AeEIlcbmsttiuXYj3TvaVUKtGuUoO7DVk7xa 9Byw== 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=vKlQFFKct0peFq7v6QT5q1kDXBxfSHaAQrnp1dtlm1U=; b=wDNIUGsd988Pz7UH4e5nHAfKkt8joMV3182u37WN6iGUx36Ajf5xKFK+dM86iM8T9h 6h9pwpJ+JDXRD0IcKYVY3Au/bCwPu7sqAdatU9W4ylf4gMqrEuFnDURkcUp7wqVzsvYD BPDZDuJ57UJCBUECdN1z03fCAdYo0wn9g/JtBGny4Kz25+jnlVi88e449IKtSOrfngJ+ HagL5Av5aI7GLNucUzDne56PHXBeb9ty5en1+Lyqlz9QsAD9Ymg+2Px78w23sz7Az1Iq UU50G6PikkvrYHLOcgBafGoXY6WLh1+WJZD+80UXrYpswAs7FqmCvMVeTQir62gPdRz3 8lOw== 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 3.18 32/52] reiserfs: fix race in prealloc discard Date: Mon, 29 Jan 2018 13:56:50 +0100 Message-Id: <20180129123629.596588720@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180129123628.168904217@linuxfoundation.org> References: <20180129123628.168904217@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?1590958962396870402?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: 3.18-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)