Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1618436ybv; Fri, 21 Feb 2020 00:23:43 -0800 (PST) X-Google-Smtp-Source: APXvYqyuzyAzFF0HnJhXNuMiolkVtURRxavNkz3H1hGycsMoZdm/sIV8Kc15m6bRGq+7lzHU1EJr X-Received: by 2002:a05:6830:2015:: with SMTP id e21mr26276760otp.106.1582273422720; Fri, 21 Feb 2020 00:23:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582273422; cv=none; d=google.com; s=arc-20160816; b=yGImSfiXuPMcd+iZvygcRJA5illQvZvJGThKEXoOswmE33Z1K+DodWF5sFFyNKrvW2 cc8psnUXV+PMsPrjlRI74LxpHXAtIgHT0syDAW+c0IbJtEMPRY1KTXT94lUNm4yC/GA3 kpO8KL8c0rKM5OVLcqHklqMtYvNP2i4jmJBE+gv5Wt9F6HsYwuWXlB7PIajV3Lo/xxu7 ize3mrwFpR+VlcmiMoJsReBvJxnCRwTuB3T1PtPTC7NzC9NGwuCtklkQNKeF2LVmb5ER y7JKdgKWP6XxitZXpUJLBWET4IOjn7JwqCm5hZktX1WgXG8rMiAVfk6Z3HwMs7dnMr97 4N/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=g5mc2dD50eFJSy3M8eahENVm/MsmYaL0CusAuskv2Is=; b=lvkq5dIZiEBiyNfvjv1V+05WDVBlVIVMtJUxF26x7tTZ0rwm2DRJ0BV7cnhGu8SoK/ IfcXYmUh2/J/OvlnsVgoVejDF6jdZkLDsGUBVNugu+nF9XDUCS9gimf6j/BsevOD/TsH QJdAPMYz1k/u9tVLz2LSsq8wAtrHJRCD5EslTwP07UZSDybpo1JjsV0piQbom3+ehkKK /6i/EahiIWY8bAxS4FUlIsmDvMpZQPLrLX5vu2X6hXDumSNER2btscRK38Jr1Y2MKZEN LV92SMPoj7KD4sa6n02H56UUpBhEPXTzNtABV+a+ug98KvfqKIf4ZIH5FlNqOVr9IQ4T JWgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BuGGUvqU; 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 b191si497609oii.266.2020.02.21.00.23.30; Fri, 21 Feb 2020 00:23:42 -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; dkim=pass header.i=@kernel.org header.s=default header.b=BuGGUvqU; 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 S2388371AbgBUIWt (ORCPT + 99 others); Fri, 21 Feb 2020 03:22:49 -0500 Received: from mail.kernel.org ([198.145.29.99]:34506 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388363AbgBUIWr (ORCPT ); Fri, 21 Feb 2020 03:22:47 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AF57D2467D; Fri, 21 Feb 2020 08:22:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582273367; bh=Vo9ppF8tbSkSkbJ7l6Xs3X6/KUsvF3UdevzqhdqS7/Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BuGGUvqUjsJmg5ifnLQr28KIyhUF26Wh9e59OvEdWMyOETeHWqeNmqUjSB3pcXdCs RswMfu8p0A6UR9dIRneRhEI6SUZonC1Tq0cfy3QUGnrkYgGqMkEsdmr3C5O0lWyVE+ 5r1PzpJfn9ocz+X6Z84tX8DeaC3epDPvHpDJD0m4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Josef Bacik , David Sterba , Sasha Levin Subject: [PATCH 4.19 145/191] btrfs: safely advance counter when looking up bio csums Date: Fri, 21 Feb 2020 08:41:58 +0100 Message-Id: <20200221072308.005607611@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200221072250.732482588@linuxfoundation.org> References: <20200221072250.732482588@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 @@ found: 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