From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 235E2C3B1A4 for ; Fri, 14 Feb 2020 17:03:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E858C2067D for ; Fri, 14 Feb 2020 17:03:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581699806; bh=5eWnpuNF97wZpSCxSv5oQjhNwTwyBqxF0DiaBFyiu2o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=aq9a4/6dwApN4T82MUZoSZhxVaD/dzoq2kQDZxVxlvzfF1plUnxIYOFMKmhZie9SC TT0d3d8jMOWJJKTkH6y0LtMVaXdsrQDDq0P2aegw+J335cDKwderxTfBQ9nuN/mJYB pLiGZjksytVgNc6CSOcJ+GwSUIEy47hr05ClCEDQ= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404366AbgBNRDS (ORCPT ); Fri, 14 Feb 2020 12:03:18 -0500 Received: from mail.kernel.org ([198.145.29.99]:46476 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390191AbgBNQPz (ORCPT ); Fri, 14 Feb 2020 11:15:55 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D8877246EB; Fri, 14 Feb 2020 16:15:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581696954; bh=5eWnpuNF97wZpSCxSv5oQjhNwTwyBqxF0DiaBFyiu2o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R7lvux+zeH0BqZoi/NmyYmtBWFVcykjt1QksOoU2DlbFjvJs0s6mFYLiq+t16/+Dk qLkMXkBQlJHzIwvzWk0/5spbfDqKoL1eLPTM2dZVidmXCwPwAQKUAO6zLS9NKW1Kg9 gNxKli3ElkQBUVt50GE6yp5UI0PA2YCIfnx1mUc8= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: David Sterba , Dan Carpenter , Josef Bacik , Sasha Levin , linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 195/252] btrfs: safely advance counter when looking up bio csums Date: Fri, 14 Feb 2020 11:10:50 -0500 Message-Id: <20200214161147.15842-195-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200214161147.15842-1-sashal@kernel.org> References: <20200214161147.15842-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: David Sterba [ Upstream commit 4babad10198fa73fe73239d02c2e99e3333f5f5c ] Dan's smatch tool reports fs/btrfs/file-item.c:295 btrfs_lookup_bio_sums() warn: should this be 'count == -1' which points to the while (count--) loop. With count == 0 the check itself could decrement it to -1. There's a WARN_ON a few lines below that has never been seen in practice though. It turns out that the value of page_bytes_left matches the count (by sectorsize multiples). The loop never reaches the state where count would go to -1, because page_bytes_left == 0 is found first and this breaks out. For clarity, use only plain check on count (and only for positive value), decrement safely inside the loop. Any other discrepancy after the whole bio list processing should be reported by the exising WARN_ON_ONCE as well. Reported-by: Dan Carpenter Reviewed-by: Josef Bacik Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/file-item.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index 4cf2817ab1202..f9e280d0b44f3 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -275,7 +275,8 @@ static blk_status_t __btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio csum += count * csum_size; nblocks -= count; next: - while (count--) { + while (count > 0) { + count--; disk_bytenr += fs_info->sectorsize; offset += fs_info->sectorsize; page_bytes_left -= fs_info->sectorsize; -- 2.20.1