From nobody Sun May 19 01:43:07 2024 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; 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 ARC-Seal: i=1; a=rsa-sha256; t=1585032035; cv=none; d=zohomail.com; s=zohoarc; b=CNqdRbLjMglw72lOvFSEP7mx3S97OleNgDIf6518YLo1kTVI+iNBjeuYo32B6N9/6YkzBc4UOFOLBRq1LxfZPo9YhL/HO3DRnNdx+r4KeEl119On54BFRKF63WjyGSHfrVnhpt/qGKM+e1WJWruV0Krd5DNGZ7wOTaAN9mTQ5DE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585032035; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=3hN0D9ajRvejwYvopHClPvLeKj7scLAom6ECfMC8qCc=; b=HvStj0BedKHQ+2qC9lU+j4quYOr+GTcLsREe30x2OXDeOKy8v2KEIaO2MiP/+k7b0Z7f8IXYYfpKE48fKlSGOlYIR0rqYGSI9wiq0Wd/8EauYnIMu/r3hdpMw/hT7B8FzGUG3SvMF8F9WLStCRKc6nxOoh6r8P78aPZXmjuEIoU= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1585032035599845.3360639414107; Mon, 23 Mar 2020 23:40:35 -0700 (PDT) Received: from localhost ([::1]:43710 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jGdEn-0006IV-OE for importer@patchew.org; Tue, 24 Mar 2020 02:40:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49421) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jGdE6-0005mN-Ip for qemu-devel@nongnu.org; Tue, 24 Mar 2020 02:39:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jGdE4-0006Ni-Eo for qemu-devel@nongnu.org; Tue, 24 Mar 2020 02:39:49 -0400 Received: from [107.174.27.60] (port=52372 helo=ozlabs.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1jGdE4-0006N0-9X; Tue, 24 Mar 2020 02:39:48 -0400 Received: from fstn1-p1.ozlabs.ibm.com (localhost [IPv6:::1]) by ozlabs.ru (Postfix) with ESMTP id 967B6AE80013; Tue, 24 Mar 2020 02:37:28 -0400 (EDT) From: Alexey Kardashevskiy To: qemu-devel@nongnu.org Subject: [PATCH qemu] vfio/spapr: Fix page size calculation Date: Tue, 24 Mar 2020 17:39:12 +1100 Message-Id: <20200324063912.25063-1-aik@ozlabs.ru> X-Mailer: git-send-email 2.17.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 107.174.27.60 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: Alexey Kardashevskiy , Alex Williamson , qemu-ppc@nongnu.org, David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Coverity detected an issue (CID 1421903) with potential call of clz64(0) which returns 64 which make it do "<<" with a negative number. This checks the mask and avoids undefined behaviour. In practice pgsizes and memory_region_iommu_get_min_page_size() always have some common page sizes and even if they did not, the resulting page size would be 0x8000.0000.0000.0000 (gcc 9.2) and ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway. Signed-off-by: Alexey Kardashevskiy Reviewed-by: Greg Kurz --- hw/vfio/spapr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hw/vfio/spapr.c b/hw/vfio/spapr.c index 33692fc86fd6..2900bd19417a 100644 --- a/hw/vfio/spapr.c +++ b/hw/vfio/spapr.c @@ -147,7 +147,7 @@ int vfio_spapr_create_window(VFIOContainer *container, { int ret =3D 0; IOMMUMemoryRegion *iommu_mr =3D IOMMU_MEMORY_REGION(section->mr); - uint64_t pagesize =3D memory_region_iommu_get_min_page_size(iommu_mr); + uint64_t pagesize =3D memory_region_iommu_get_min_page_size(iommu_mr),= pgmask; unsigned entries, bits_total, bits_per_level, max_levels; struct vfio_iommu_spapr_tce_create create =3D { .argsz =3D sizeof(crea= te) }; long rampagesize =3D qemu_minrampagesize(); @@ -159,8 +159,8 @@ int vfio_spapr_create_window(VFIOContainer *container, if (pagesize > rampagesize) { pagesize =3D rampagesize; } - pagesize =3D 1ULL << (63 - clz64(container->pgsizes & - (pagesize | (pagesize - 1)))); + pgmask =3D container->pgsizes & (pagesize | (pagesize - 1)); + pagesize =3D pgmask ? (1ULL << (63 - clz64(pgmask))) : 0; if (!pagesize) { error_report("Host doesn't support page size 0x%"PRIx64 ", the supported mask is 0x%lx", --=20 2.17.1