Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1616665rdb; Mon, 2 Oct 2023 15:44:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IERoqBGHOPuR9Hibs/LM9o56Gvzf0ht+H+5dGq08mUddOMeUZt041N6DuGQLKzvT9lLA0U/ X-Received: by 2002:a05:6a00:244d:b0:691:2d4:23a2 with SMTP id d13-20020a056a00244d00b0069102d423a2mr11053871pfj.31.1696286685147; Mon, 02 Oct 2023 15:44:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696286685; cv=none; d=google.com; s=arc-20160816; b=K5WWGO5XhPbLpaD4mf1epKfkj8rU4wejgCsvkMKpoJ/uk7F0LVjubve+J9iI+XG9oZ fcRBtVolvNFpi6sXpI2LRhZBIT75YwPnlGdZZMEpxeGsZ6a8+LpYT95ClY21ty6WNV0U L8orolHfFCXGbZ2xNekP/Ymv3D1kDuWRWn90GjPtiWvyuaSYWXMHYfPcqrBG/PzcP9FH +1IOAEZbVJouxaXw6Z/mf6Av4sINeTtdTRNCS6l/IyxpkkzdArJxsGoGYbq9w5REA0OI b8WJfSYZUcq+NhnQdYC0oTH+fZEFkjh4C02B79aB0KcER36xNz0eAy2+0DMubQvY7D6x nyWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1awiNTJbHPwF4KLgernZOq6U4A0vnzn8In3XQ7oBM7I=; fh=Fj0jdyZi14azpbHAyspnYCgRsTe+IMcHLX/Y8spAR7E=; b=pVVv9b7F2TSr6PLV39DXK4JfPK1Z6PTV3hfzQKU7qMEcdVSrhQsEyQguLIfKSPN0SU bWowp677/Jmc6bH/qHPADj0y9zmlBDu4hdWXGPlJJBnb3rTs6C2H+IR5m+St/drfOnD4 IdKPUk3La9y1sx+6wD4uHRiocjBabdO4icSnu1yq0rEdOjK9hPI/Ls1KmlTQl/M0opNw zHfDDjAiQrsquor8GGOvW95VeVZeBfQgnb3PhWuUfQubRVcql3eCHUdvwlA63ojoWQLC ivasaH5TYt6CU5hcFB/BfuIZkzUVPROV2hxW3XF/nvCqS7dG1tEITh+kQLwmzPsTGfVP nHwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Vrjpx7Iy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id 19-20020a631753000000b00577ae8b603fsi27102453pgx.314.2023.10.02.15.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 15:44:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Vrjpx7Iy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 4D0BB804909C; Mon, 2 Oct 2023 15:44:44 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236181AbjJBWom (ORCPT + 99 others); Mon, 2 Oct 2023 18:44:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229753AbjJBWol (ORCPT ); Mon, 2 Oct 2023 18:44:41 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9179E9D for ; Mon, 2 Oct 2023 15:43:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696286631; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1awiNTJbHPwF4KLgernZOq6U4A0vnzn8In3XQ7oBM7I=; b=Vrjpx7IytDY/kKLoJsCVBAZHfyIF/CBLnnC04bbPB5lfWc/d94dlkkKN2sNEeeJvLB3Qpo KASEieKB1dMlF6xZcoLktnG/7lcHWK9qmd262DQSLt/QnkID63eX/gG3xXyFDxkxdxAxBj 44ifT3ZYTpecqdUNAKB/Wl7Jbqz8EYI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-179-f6WqBdJkOfW3Y01MTBghUA-1; Mon, 02 Oct 2023 18:43:48 -0400 X-MC-Unique: f6WqBdJkOfW3Y01MTBghUA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2A109811E7E; Mon, 2 Oct 2023 22:43:48 +0000 (UTC) Received: from omen.home.shazbot.org (unknown [10.22.10.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id E8E3440C6EBF; Mon, 2 Oct 2023 22:43:46 +0000 (UTC) From: Alex Williamson To: alex.williamson@redhat.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, liucong2@kylinos.cn, yishaih@nvidia.com, brett.creeley@amd.com Subject: [PATCH] vfio: Fix smatch errors in vfio_combine_iova_ranges() Date: Mon, 2 Oct 2023 16:43:25 -0600 Message-Id: <20231002224325.3150842-1-alex.williamson@redhat.com> In-Reply-To: <20230920095532.88135-1-liucong2@kylinos.cn> References: <20230920095532.88135-1-liucong2@kylinos.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 02 Oct 2023 15:44:44 -0700 (PDT) smatch reports: vfio_combine_iova_ranges() error: uninitialized symbol 'last'. vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_end'. vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_start'. These errors are only reachable via invalid input, in the case of @last when we receive an empty rb-tree or for @comb_{start,end} if the rb-tree is empty or otherwise fails to produce a second node that reduces the gap. Add tests with warnings for these cases. Reported-by: Cong Liu Link: https://lore.kernel.org/all/20230920095532.88135-1-liucong2@kylinos.cn Cc: Yishai Hadas Cc: Brett Creeley Signed-off-by: Alex Williamson --- drivers/vfio/vfio_main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 40732e8ed4c6..e31e1952d7b8 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -946,6 +946,11 @@ void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes, unsigned long last; comb_start = interval_tree_iter_first(root, 0, ULONG_MAX); + + /* Empty list */ + if (WARN_ON_ONCE(!comb_start)) + return; + curr = comb_start; while (curr) { last = curr->last; @@ -975,6 +980,11 @@ void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes, prev = curr; curr = interval_tree_iter_next(curr, 0, ULONG_MAX); } + + /* Empty list or no nodes to combine */ + if (WARN_ON_ONCE(min_gap == ULONG_MAX)) + break; + comb_start->last = comb_end->last; interval_tree_remove(comb_end, root); cur_nodes--; -- 2.40.1