From patchwork Tue Sep 22 23:49:15 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Daney X-Patchwork-Id: 7246511 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7E02DBF036 for ; Tue, 22 Sep 2015 23:53:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 924AB209B3 for ; Tue, 22 Sep 2015 23:53:15 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B35A220972 for ; Tue, 22 Sep 2015 23:53:14 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZeXKV-0006zJ-LB; Tue, 22 Sep 2015 23:50:35 +0000 Received: from mail-io0-x230.google.com ([2607:f8b0:4001:c06::230]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZeXJl-0005UQ-16 for linux-arm-kernel@lists.infradead.org; Tue, 22 Sep 2015 23:49:50 +0000 Received: by iofb144 with SMTP id b144so30204897iof.1 for ; Tue, 22 Sep 2015 16:49:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SoyvpyBjZ/vztPg2ahgM56fhcEmTxViU8q/qd2UTZwU=; b=KSA/YWL0FeqVwjPI+thjxWcZIBKqDGEqUp+eJw7YfwfMFcLROxaTxYzL1XeipX+zSC EIwoBryHxReqoqUqmygpOgrZ0npdIbjuT+Q82GwXko9ypKHfp9qp+t1reDfXzWCGZLxY wF4sE0a2Wld52QfIkeoYRqYyfgVdIWJtlbplYfKYJJNEstzgzkE6gMLCzkau5aNKiS+t zAL7S/Lu93WmYEmorTSYEwTSBzxl5BJTuP9ZbvgKJ58hMGVfTFFdbDKcGvAC3akunMOO Ff0M3S+KRul3D+3OmGndJdQYB1OZjWYSxOm/mS06Gul7IMaoNSH0iOLwFAHSzqC/bFxP 7avQ== X-Received: by 10.107.128.41 with SMTP id b41mr34712594iod.74.1442965768207; Tue, 22 Sep 2015 16:49:28 -0700 (PDT) Received: from dl.caveonetworks.com (64.2.3.194.ptr.us.xo.net. [64.2.3.194]) by smtp.gmail.com with ESMTPSA id y15sm419770igg.3.2015.09.22.16.49.24 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 22 Sep 2015 16:49:25 -0700 (PDT) Received: from dl.caveonetworks.com (localhost.localdomain [127.0.0.1]) by dl.caveonetworks.com (8.14.5/8.14.5) with ESMTP id t8MNnNhI012982; Tue, 22 Sep 2015 16:49:23 -0700 Received: (from ddaney@localhost) by dl.caveonetworks.com (8.14.5/8.14.5/Submit) id t8MNnN8L012981; Tue, 22 Sep 2015 16:49:23 -0700 From: David Daney To: linux-kernel@vger.kernel.org, Bjorn Helgaas , linux-pci@vger.kernel.org, Will Deacon , Rob Herring , Pawel Moll , Mark Rutland , Ian Campbell , Kumar Gala , linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, Marc Zyngier Subject: [PATCH v3 4/6] PCI: generic: Correct, and avoid overflow, in bus_max calculation. Date: Tue, 22 Sep 2015 16:49:15 -0700 Message-Id: <1442965757-12925-5-git-send-email-ddaney.cavm@gmail.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1442965757-12925-1-git-send-email-ddaney.cavm@gmail.com> References: <1442965757-12925-1-git-send-email-ddaney.cavm@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150922_164949_283200_4DDA7559 X-CRM114-Status: GOOD ( 14.71 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: David Daney MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: David Daney There are two problems with the bus_max calculation: 1) The u8 data type can overflow for large config space windows. 2) The calculation is incorrect for a bus range that doesn't start at zero. Since the configuration space is relative to bus zero, make bus_max just be the size of the config window scaled by bus_shift. Then clamp it to a maximum of 255, per PCI. Use a data type of int to avoid overflow problems. Update host-generic-pci.txt to clarify the semantics of the "reg" property with respect to non-zero starting bus numbers. Signed-off-by: David Daney --- No change from v2. Change from v1: Added text to host-generic-pci.txt Documentation/devicetree/bindings/pci/host-generic-pci.txt | 4 +++- drivers/pci/host/pci-host-generic.c | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Documentation/devicetree/bindings/pci/host-generic-pci.txt b/Documentation/devicetree/bindings/pci/host-generic-pci.txt index cf3e205..105a968 100644 --- a/Documentation/devicetree/bindings/pci/host-generic-pci.txt +++ b/Documentation/devicetree/bindings/pci/host-generic-pci.txt @@ -34,7 +34,9 @@ Properties of the host controller node: - #size-cells : Must be 2. - reg : The Configuration Space base address and size, as accessed - from the parent bus. + from the parent bus. The base address corresponds to + bus zero, even though the "bus-range" property may specify + a different starting bus number. Properties of the /chosen node: diff --git a/drivers/pci/host/pci-host-generic.c b/drivers/pci/host/pci-host-generic.c index 77cf4bd..0a9c453 100644 --- a/drivers/pci/host/pci-host-generic.c +++ b/drivers/pci/host/pci-host-generic.c @@ -164,7 +164,7 @@ out_release_res: static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci) { int err; - u8 bus_max; + int bus_max; resource_size_t busn; struct resource *bus_range; struct device *dev = pci->host.dev.parent; @@ -177,8 +177,9 @@ static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci) } /* Limit the bus-range to fit within reg */ - bus_max = pci->cfg.bus_range->start + - (resource_size(&pci->cfg.res) >> pci->cfg.ops.bus_shift) - 1; + bus_max = (resource_size(&pci->cfg.res) >> pci->cfg.ops.bus_shift) - 1; + if (bus_max > 255) + bus_max = 255; pci->cfg.bus_range->end = min_t(resource_size_t, pci->cfg.bus_range->end, bus_max);