Received: by 10.223.176.5 with SMTP id f5csp3413178wra; Mon, 29 Jan 2018 12:49:27 -0800 (PST) X-Google-Smtp-Source: AH8x2252MPouoK7smrRQdOi/vdrLRc1LPVNlEEvyvjmeLRhfOG8EGmTUBixxvq+uQhOnzZT8EJaH X-Received: by 2002:a17:902:bcc5:: with SMTP id o5-v6mr23373569pls.67.1517258967611; Mon, 29 Jan 2018 12:49:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517258967; cv=none; d=google.com; s=arc-20160816; b=op632JY+infi906qnZfOnL1Gs4baZ8xM/3dgPU2/URA+y3iTm9gyPbpCT7pVyVWeKr XLnmX3oZyAUJ/Ezn411Br0fll9m2whAKPjgZg6LM4H8Epmj94XiuHgcfi/TF3qXGtD3g pfrQ6qAVmUxNsUebKoR3OASi5fPTwPSnyEDx83eZXQvIm3Sgw6NTu8/x2vBzl/K0Cx/r Bx5QvulOHlYkbg54c+KmJb8oFcJlOiPWHF82Mcq0AHDIBvrHd9ZqifSq/sYWehl4Cxbi dGK6jJem5eGzSWg320JbvHp1NnNwVgD8if/DEU/NvTTT3ml7rl7A6YbfY2Z8XTbmWkPC S+3A== 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=vKlQFFKct0peFq7v6QT5q1kDXBxfSHaAQrnp1dtlm1U=; b=Rwe8PoHXw/gBkZ9ttFtFddeIVP7ZLU39JAF0k6q/8creDehM+S0HTFtXqkyEu1M7io jC/SXOZZlg42DT6lAhTBCF75Ak6o4JO8BuMRxooHo62FkmIZoyEuLylztKSG0vrWRfTW xfh9LRc6pbrAPsOyaZtCPytHsUCIRZzHRZBs9rknzcUaFZjg6INuPnZwHTSjkMt7ICH7 c6yypijFwQUMV6T9JgiBHM6d1j4DDSTD1lWW4P9v3h0xqwJUfOK/b1gxxyY8mXbhnh+r Lnx66a4e/SGk5u8FkDRo0fzJ1S8YZolcPN5bpU5CAIZvlpTN6hNK60leD+qNtR8V2nsa xUSg== 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 k29si531919pfj.228.2018.01.29.12.49.12; Mon, 29 Jan 2018 12:49:27 -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 S932067AbeA2UsZ (ORCPT + 99 others); Mon, 29 Jan 2018 15:48:25 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:32768 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753754AbeA2ULj (ORCPT ); Mon, 29 Jan 2018 15:11:39 -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 918262EA8; Mon, 29 Jan 2018 12:59:10 +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 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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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)