From patchwork Mon Oct 2 22:43:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 147552 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1732692vqb; Mon, 2 Oct 2023 15:44:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEgXKFC2qjVFYbqrD3bZz72wROQLsdKujCwVy6hpxOlRO3Q9u8WOkxFM8grsVqNUDl3FKQm X-Received: by 2002:a17:902:efc4:b0:1c7:365a:566d with SMTP id ja4-20020a170902efc400b001c7365a566dmr8839122plb.66.1696286686922; Mon, 02 Oct 2023 15:44:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696286686; cv=none; d=google.com; s=arc-20160816; b=uwi+Fh1jSPqK1fVFaQQ1RqwqAchmP4z7aNX+W4A7kuDHQMVjPBriBjcSXlAbMriQ6t sZKpiYDujRX5bCC7Ry3QaZ5brWgIi22SClBOlMzGcSUiXSUAJP/IOvfRNMmhoxwmpqwb LmEiuYg0ZtHLHzAQ6cMIEvE76d5HoKxLT6bDNrqd7sR3QQX3D0WdlX/QU5MKhpUFaQ+9 dvHiNQO/bfUxXAn2IU5D8E0Cj3Q7oRdtU94oy/H3TpQX3EkgocJHiN/DmF2e6vgQUpji G/dIGBKrupyKYuFQu9MLjaav99NSDuelR6DsJI9sA3zF+tf7clCFnPpi+C8G5AzS8aMr /Aog== 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 Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id b4-20020a170902650400b001c4329b77bfsi25968101plk.123.2023.10.02.15.44.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 15:44:46 -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 2E8DD80490A0; Mon, 2 Oct 2023 15:44:46 -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 S236188AbjJBWom (ORCPT + 18 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 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:46 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778685509243917183 X-GMAIL-MSGID: 1778685509243917183 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 Reviewed-by: Brett Creeley Reviewed-by: Jason Gunthorpe --- 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--;