Received: by 10.223.176.5 with SMTP id f5csp3446235wra; Mon, 29 Jan 2018 13:18:13 -0800 (PST) X-Google-Smtp-Source: AH8x225rD9Ucp+2j6xHCTmCboEmO8uO5f9imz7v8Kb032ibxk1gQ87nbojeKby4ky9rB7uURwwjj X-Received: by 10.101.76.14 with SMTP id u14mr21810840pgq.363.1517260693122; Mon, 29 Jan 2018 13:18:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517260693; cv=none; d=google.com; s=arc-20160816; b=ajdtMEV4IA7u/8NW+LscOB4HnpU3bH6ZeAiHGAXuB0ZLVEVB4j09u2af3fUsGtQDkg uxqmQ8zqrU3O9hFPaU54iJE9yBAis8t1oNZ18qPoE+T5YUahChbDRWVvbw1x/CDCMgZf jo1KS3zgqv7dAwZRbQM1c/qfv6Lph51LfmKx1XGcTGSXScseTIgR6KYHWBhaEu2Rgu7O jfB6usA0M0fxvs9ASelGN0k9LiCgqmi4hC0Snlw7HiHWPkYwHHutGgOqDh/qn4FJInfK s88rMmhZjKSrl1cwYNi1Hh6m36iRSYsZAkwC2Cxb89XjAtUGE6v9nUaqnahTsJfOwE// yC7w== 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=AVlRrXfrSEotBJQaBc6nzZlyRMaHKao5b8ilZeiDHxs=; b=U7Jgs7jv7Vt2PcEUR4O7lee1BhPd5TjptwCZkwq9jSlCV+bHEbDFKKlLzsQdTZo5Y3 x2DNzgKUMHnGXwNkiuj3mONDECu5g9MoELcm33wycA5+hi780sqtS6mTMLfzGF4UX0y8 lYmqnNMDRrj12afKOSGBCa9y3XZbr82m/ssaWBxVIOLT6lLHCIcZbWmyerkdYS7F17aS OU0Nb+w/fklYX2hhLRFIOdTHsZg1rf5Ebr5YUNbckGvM1WT0df8qwLkjjiKrXQW2ke+w blGBYceGI1xQhMHscZ9ARrDp+d+cn30clZIj1T/eAJ7I74TAicHluvIenSzSRUUzwayv xomw== 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 k83si340395pfa.246.2018.01.29.13.17.58; Mon, 29 Jan 2018 13:18:13 -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 S1752292AbeA2UEb (ORCPT + 99 others); Mon, 29 Jan 2018 15:04:31 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:42604 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752013AbeA2UE3 (ORCPT ); Mon, 29 Jan 2018 15:04:29 -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 D4A4F2F75; Mon, 29 Jan 2018 13:05:01 +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.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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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)