From patchwork Fri Sep 11 23:21:57 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Daney X-Patchwork-Id: 7165611 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id EDB109F314 for ; Fri, 11 Sep 2015 23:23:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 98C0F206EC for ; Fri, 11 Sep 2015 23:23:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BBF6A206D0 for ; Fri, 11 Sep 2015 23:23:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754986AbbIKXXm (ORCPT ); Fri, 11 Sep 2015 19:23:42 -0400 Received: from mail-io0-f178.google.com ([209.85.223.178]:36306 "EHLO mail-io0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754559AbbIKXWJ (ORCPT ); Fri, 11 Sep 2015 19:22:09 -0400 Received: by ioii196 with SMTP id i196so114904142ioi.3; Fri, 11 Sep 2015 16:22:08 -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=1CyBKX2M8kLjgII+adKb5mFtCVeOVQ14FpIIjDS/17c=; b=a6Uuat7Y4nBovYPY8UEfjy30F+WG+/Ab2RTk6GtAgrjQarsx38FraQ/79WmDoToBoZ WEY469Rmebf9rwMQJZ8p6fPBE4ss0HenISLb+qvtd2SYuW7OYYr8Yf4Y2HucdiRGRBjY YrhYVwHD6WKUZQ71C/tvVGybKDzu5fR+TqZ5JAaXvNsH4pIhN3yavX7UVsJlTg8ZJIh3 MLAn2VvpJz/F+/s44DXzSQz/OSbsR6NlfoDTbrN0lKfmkDAYrr5MOVH8CVLuz5W3Vxeu quJNLe3qURBeUXVeMaXHhDXjJk0oZZPj8TpKXWyXCrYqdbfpNtZlEG1PMFmzZAahjiQB /Rhg== X-Received: by 10.107.168.230 with SMTP id e99mr3302951ioj.95.1442013728261; Fri, 11 Sep 2015 16:22:08 -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 10sm2479230igt.2.2015.09.11.16.22.04 (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 11 Sep 2015 16:22:04 -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 t8BNM3GJ005058; Fri, 11 Sep 2015 16:22:03 -0700 Received: (from ddaney@localhost) by dl.caveonetworks.com (8.14.5/8.14.5/Submit) id t8BNM3Fw005057; Fri, 11 Sep 2015 16:22:03 -0700 From: David Daney To: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Rob Herring , Frank Rowand , Grant Likely , Bjorn Helgaas , linux-pci@vger.kernel.org, Will Deacon , Pawel Moll , Mark Rutland , Ian Campbell , Kumar Gala , linux-arm-kernel@lists.infradead.org Cc: David Daney Subject: [PATCH 4/6] PCI: generic: Correct, and avoid overflow, in bus_max calculation. Date: Fri, 11 Sep 2015 16:21:57 -0700 Message-Id: <1442013719-5001-5-git-send-email-ddaney.cavm@gmail.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1442013719-5001-1-git-send-email-ddaney.cavm@gmail.com> References: <1442013719-5001-1-git-send-email-ddaney.cavm@gmail.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_RP_MATCHES_RCVD, 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. Signed-off-by: David Daney --- drivers/pci/host/pci-host-generic.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/pci/host/pci-host-generic.c b/drivers/pci/host/pci-host-generic.c index cd6f898..fce5bf7 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);