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=-9.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,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 35CBDC35641 for ; Fri, 21 Feb 2020 08:31:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0BA1120578 for ; Fri, 21 Feb 2020 08:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582273896; bh=UCSjKDijxwgxmzfU82UEdAxx5tkflRr2LCMf3KI4QlM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=hSsdj+yhgSa8YZes3EguwNHDde67KqUwQ485Xv5sZ1yyeGW9XNM/30uX6ORRxhQ+f jXDaRfSw1KTaUeJoH0R4rB5JS9uI7FFjhtbN8mAuqlXrZBF3FMrPYC8ipKOOxH4bhe z/BbETPnIxrHrMG9BkKZfT1I31+yyHmg7plY5CO0= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388066AbgBUIbe (ORCPT ); Fri, 21 Feb 2020 03:31:34 -0500 Received: from mail.kernel.org ([198.145.29.99]:50394 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733104AbgBUIN7 (ORCPT ); Fri, 21 Feb 2020 03:13:59 -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 D22992467E; Fri, 21 Feb 2020 08:13:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582272839; bh=UCSjKDijxwgxmzfU82UEdAxx5tkflRr2LCMf3KI4QlM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Dc/SUm8VjAV2lBylG6DZPcHYa+NiN7c4eIQh7PCZLiAGdfvofGx3oxb4YzB15s3uX FPxOsrlvxEsqqiCQrd77Pk5bZWwA80oFkTE+lLhLc9FgwUxfU9pam+eIIwtujH+L2W h25YcuOGt7qqMkPQI8R3t/6ETuc2TjNpLh3zkOEc= 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 5.4 265/344] btrfs: safely advance counter when looking up bio csums Date: Fri, 21 Feb 2020 08:41:04 +0100 Message-Id: <20200221072413.706422000@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200221072349.335551332@linuxfoundation.org> References: <20200221072349.335551332@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 c878bc25d0460..f62a179f85bb6 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -274,7 +274,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