Received: by 10.223.176.5 with SMTP id f5csp3436491wra; Mon, 29 Jan 2018 13:09:21 -0800 (PST) X-Google-Smtp-Source: AH8x2258/RG32m1H4ZMuB3TUMxAnfCJHiYOVOOXZP7nDw7HGEcNllI0YdTPhZhtPfZELgkBX/0W3 X-Received: by 2002:a17:902:6b82:: with SMTP id p2-v6mr23827390plk.30.1517260161171; Mon, 29 Jan 2018 13:09:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517260161; cv=none; d=google.com; s=arc-20160816; b=RrDo+WZ6A/XJZVJzgRpmaRlOs5t9XES/B2wUq5hEB7z9GnlAC7bR/J1cyniQ1JPfAR wGHo1N44fGPZcriKCWJwCpC20A/w0DiSE2dXsESXN1nz0ensdL7jmJVYqJZDp76cZKbI b12caXG6D8YHVR2e1KYoWLwYHCXtsmwu7ouyA98xgd7aTlibg9NzBj+krZb4b+XyfCTm T8tAsCJreS1hFByiLiVpCaiEPp7w1aTIWh9aMVL0/uTCOqWQ+FxWGAD/4tW2kFBymExh vMb6VAdXo+8R31FJxd/9PcVTnKj5FwWeAq+NBf3Vag+oZMhK7unPpxF5xlG43AyXwc04 fGiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=6605MHThN8wtTfiVfBwosScrtRlxiockwWQMaojg3n8=; b=R7gJFi4b93pNx+rP3e5SPJxKgQReWppXIidz2c1soQvh2VDlZHh6aJqWL2MnmaRqiy fxO2sc+nOYJe7Wni8RQrvBCQpnqqnkOB4UWa4Jdl8FAzCxG6XYq/4O74FaHkV0et/WY4 2EXSp/Ry6QikW1PpNhE+GDd8UQebOw66bQ0e85m+wQGeQh8DzcGv11BneHVRpeW/ITWH Tu/jkGI/HPovaCFUfss1kCJ4Njz/3Yc/JESjw6eWyDFEsXTR6Xe5Hc3LcKZ9c3KMufHz TGdLdiEsmYKrU+112cLvqUQZZfp6ZKNTYVNhaJOAdLcEFncJZmFOsfQYLKfiQeKMA/6B 5Zhw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g10-v6si668589plt.206.2018.01.29.13.09.06; Mon, 29 Jan 2018 13:09:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752046AbeA2VIP (ORCPT + 99 others); Mon, 29 Jan 2018 16:08:15 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:48450 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752717AbeA2UGa (ORCPT ); Mon, 29 Jan 2018 15:06:30 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 2D9D12F10; Mon, 29 Jan 2018 13:02:19 +0000 (UTC) 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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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)