From nobody Fri Nov 14 00:43:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1583674440; cv=none; d=zohomail.com; s=zohoarc; b=BafibOUtOOvFvaaQwjv4pJdpTdkWzWyTePoP4YaZwXHun9tyxJZx3RC85YWnxNZwqPG0OVSMhPyJTWc1vW/tOb8ewHTfDss3OztPqw3NQO85C7vHiPGhephP6L2Url8LimUUtTrmKAPQ2c7xMJzFm79RrFJnAXaTpzxzusV8M0M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1583674440; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=WD83umNxm2ky0VXPPRkxHn9Dl02EzVTi3i3J6pm7IYQ=; b=DxVEgQ6uNhy2z/d/25946YrzIGjEb5lf5a7KW3OGbymPF0FRhdrHiYSC/HqdgEkgN8P0/MbgdCGiZu5ls3MdP5y13e0D+CwOtlfTze42i4TUfrYFgjCAYgGjgCElpk9w68ZBNEbxNDtStj8pK8whZEsx9xhptKsuPRX7zfr/Xy8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1583674440791463.0371833808183; Sun, 8 Mar 2020 06:34:00 -0700 (PDT) Received: from localhost ([::1]:57954 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jAw47-0007IQ-RB for importer@patchew.org; Sun, 08 Mar 2020 09:33:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53087) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jAw1s-0004uk-BE for qemu-devel@nongnu.org; Sun, 08 Mar 2020 09:31:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jAw1o-0006hF-QX for qemu-devel@nongnu.org; Sun, 08 Mar 2020 09:31:39 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:51863 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jAw1o-0006gR-MI for qemu-devel@nongnu.org; Sun, 08 Mar 2020 09:31:36 -0400 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-279-DCS703EIMsCvZ4-e6arM1g-1; Sun, 08 Mar 2020 09:31:32 -0400 Received: by mail-qt1-f199.google.com with SMTP id f25so4957431qtp.12 for ; Sun, 08 Mar 2020 06:31:32 -0700 (PDT) Received: from redhat.com (bzq-79-178-2-19.red.bezeqint.net. [79.178.2.19]) by smtp.gmail.com with ESMTPSA id w41sm21001422qtj.49.2020.03.08.06.31.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Mar 2020 06:31:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583674295; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WD83umNxm2ky0VXPPRkxHn9Dl02EzVTi3i3J6pm7IYQ=; b=CDYFBWWZ0iQicSG3JlMQ+CdNzF1oDfW2yZ6/LVoE90nR3thX19KtJiW1CLILYClICDWsnC 9NOq8H9hjdcCdm8Bp6bYUQ4VsXLO5yixsTwZhgtIeqjrN6XhWyKDZrjubstS8LmJ6EMnfN C5SFssv1gp9p01XX3D3EXsorDDO9xuk= X-MC-Unique: DCS703EIMsCvZ4-e6arM1g-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=hDANYJtJJ6fz/Foo93IRg8oS9InICE/RlX6g+g3k1KI=; b=NDssei9ZI8dHEpfmcP0mHAOsI+yeOLW0QrO8w8uCipARXf3m6+Isb7xrc2pXncD2oY ilQ3nSxpUbD7tUrj4gj9osyG+6uk1sNQJlnMQwNt6oCYpE+vP1aS4E5PXTe28fGi51F3 Hbg3i7VYuDhbGShZnMOZGLaGoGxriCjO9+czUEL/IlpLJQ8H47har72VwF9h6lEADFvF 4LQEaGDubilxulUw6eTlk7DRtl54K8CVNojGqOcyN3IF+eGqiKqay2j+jkW4IQq3dD/7 A2CKTiqs+u67nWrbT3FoAmfOmxDskjVJmabgj2A+jbYIwPOTFzVBDLBq7kkfPc6l0ikI MWrw== X-Gm-Message-State: ANhLgQ0GZj5PERzyl3J4qr2TwEPdpJKkqpqapwVysslfyGpxja1UO8Dt iIIVkxKJ62cFFAqPos3nMO9kFOt0WR0xtSB0gsHUzCOnj+j5Um2zGA4KrDq1pk4IIdR+BbBWOcW Keam2yQbvWqRhaLg= X-Received: by 2002:a05:620a:2209:: with SMTP id m9mr10778077qkh.71.1583674291392; Sun, 08 Mar 2020 06:31:31 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsp4dv1bezoMHrXC/3zAlA2zYBMlRcCbUtQwyB7yUKScheb3XnsU/3yJQQa5uiJRMhsL7tZZA== X-Received: by 2002:a05:620a:2209:: with SMTP id m9mr10778060qkh.71.1583674291069; Sun, 08 Mar 2020 06:31:31 -0700 (PDT) Date: Sun, 8 Mar 2020 09:31:26 -0400 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Subject: [PULL 4/4] hw/i386/intel_iommu: Simplify vtd_find_as_from_bus_num() logic Message-ID: <20200308133054.1563705-5-mst@redhat.com> References: <20200308133054.1563705-1-mst@redhat.com> MIME-Version: 1.0 In-Reply-To: <20200308133054.1563705-1-mst@redhat.com> X-Mailer: git-send-email 2.24.1.751.gd10ce2899c X-Mutt-Fcc: =sent X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Eduardo Habkost , Peter Xu , Eric Auger , Paolo Bonzini , Philippe =?utf-8?Q?Mathieu-Daud=C3=A9?= , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Philippe Mathieu-Daud=C3=A9 The vtd_find_as_from_bus_num() function was introduced (in commit dbaabb25f) in a code format that could return an incorrect pointer, which was later fixed by commit a2e1cd41ccf. We could have avoided this by writing the if() statement differently. Do it now, in case this function is re-used. The code is easier to review (harder to miss bugs). Reviewed-by: Peter Xu Reviewed-by: Eric Auger Signed-off-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20200305102702.31512-1-philmd@redhat.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/i386/intel_iommu.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 6258c58ac9..204b6841ec 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -987,24 +987,26 @@ static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, ui= nt32_t level) static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_nu= m) { VTDBus *vtd_bus =3D s->vtd_as_by_bus_num[bus_num]; - if (!vtd_bus) { - /* - * Iterate over the registered buses to find the one which - * currently hold this bus number, and update the bus_num - * lookup table: - */ - GHashTableIter iter; + GHashTableIter iter; =20 - g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); - while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { - if (pci_bus_num(vtd_bus->bus) =3D=3D bus_num) { - s->vtd_as_by_bus_num[bus_num] =3D vtd_bus; - return vtd_bus; - } - } - vtd_bus =3D NULL; + if (vtd_bus) { + return vtd_bus; } - return vtd_bus; + + /* + * Iterate over the registered buses to find the one which + * currently holds this bus number and update the bus_num + * lookup table. + */ + g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); + while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { + if (pci_bus_num(vtd_bus->bus) =3D=3D bus_num) { + s->vtd_as_by_bus_num[bus_num] =3D vtd_bus; + return vtd_bus; + } + } + + return NULL; } =20 /* Given the @iova, get relevant @slptep. @slpte_level will be the last le= vel --=20 MST