From patchwork Mon May 13 16:08:32 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Liu X-Patchwork-Id: 2559291 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 52BDEDF2E5 for ; Mon, 13 May 2013 16:10:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753078Ab3EMQKZ (ORCPT ); Mon, 13 May 2013 12:10:25 -0400 Received: from mail-pb0-f48.google.com ([209.85.160.48]:53637 "EHLO mail-pb0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754863Ab3EMQJ3 (ORCPT ); Mon, 13 May 2013 12:09:29 -0400 Received: by mail-pb0-f48.google.com with SMTP id md4so1526238pbc.21 for ; Mon, 13 May 2013 09:09:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=0hhrtk3Bl7artpJQkJNNM65A4mtefrDi6JOB66Va4Sc=; b=hY7ksav/hMZffWNY707melJdxf0jO+lzse4rn/76R4eC1cYZ4IvpbGMLgB2zWOFgj1 aX6ZIOThi4kyiNECqfof6GcbR4Fn9q/mHqrfVSprmX4wpGfhjuj06uCrBZWXgEncMYAM VFM+V1+zDNeqIrnSRVyWjLIbZ4bhUB/xhmxV4whB65d6dWTam5kkUCEqCe+nOwdAwTif DMWl+zFWi6xpxvjkIVBTl8J4x+lS0CrXa4VA2sto78YsrMSueXSlUlt/wj98S5Qzh0jX C/srEIw+P1wD9VSL87K9mFBVNrKBFfq5MkzjfNeBU9IGr/kU1BloAJFrVkVodfVAfsb2 yDmg== X-Received: by 10.68.218.8 with SMTP id pc8mr29762335pbc.115.1368461368651; Mon, 13 May 2013 09:09:28 -0700 (PDT) Received: from localhost.localdomain ([120.196.98.100]) by mx.google.com with ESMTPSA id nt2sm14580193pbc.17.2013.05.13.09.09.24 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 13 May 2013 09:09:27 -0700 (PDT) From: Jiang Liu To: Bjorn Helgaas , Yinghai Lu Cc: Jiang Liu , "Rafael J . Wysocki" , Greg Kroah-Hartman , Gu Zheng , Toshi Kani , Myron Stowe , Yijing Wang , Jiang Liu , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2, part 1 8/9] PCI: make PCI host bridge/bus creating and destroying logic symmetric Date: Tue, 14 May 2013 00:08:32 +0800 Message-Id: <1368461313-4371-9-git-send-email-jiang.liu@huawei.com> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1368461313-4371-1-git-send-email-jiang.liu@huawei.com> References: <1368461313-4371-1-git-send-email-jiang.liu@huawei.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org This patch makes PCI host bridge/bus creating and destroying logic symmetric by using device_initialize()/device_add()/device_del()/put_device() pairs as discussed in thread at: http://comments.gmane.org/gmane.linux.kernel.pci/22073 It also fixes a bug in error recovery path in pci_create_root_bus() which may kfree() an in-use host bridge object. Signed-off-by: Jiang Liu Cc: Yinghai Lu --- drivers/pci/probe.c | 84 +++++++++++++++++++++++----------------------------- drivers/pci/remove.c | 3 +- 2 files changed, 39 insertions(+), 48 deletions(-) diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index bc075a3..a2617c2 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -451,7 +451,7 @@ void pci_read_bridge_bases(struct pci_bus *child) } } -static struct pci_bus * pci_alloc_bus(void) +static struct pci_bus *pci_alloc_bus(struct pci_ops *ops, void *sd, int bus) { struct pci_bus *b; @@ -464,10 +464,32 @@ static struct pci_bus * pci_alloc_bus(void) INIT_LIST_HEAD(&b->resources); b->max_bus_speed = PCI_SPEED_UNKNOWN; b->cur_bus_speed = PCI_SPEED_UNKNOWN; + b->sysdata = sd; + b->ops = ops; + b->number = bus; + b->busn_res.start = bus; + b->busn_res.end = 0xff; + b->busn_res.flags = IORESOURCE_BUS; + b->dev.class = &pcibus_class; + dev_set_name(&b->dev, "%04x:%02x", pci_domain_nr(b), bus); + device_initialize(&b->dev); } + return b; } +static void pci_release_host_bridge_dev(struct device *dev) +{ + struct pci_host_bridge *bridge = to_pci_host_bridge(dev); + + if (bridge->release_fn) + bridge->release_fn(bridge); + + pci_free_resource_list(&bridge->windows); + + kfree(bridge); +} + static struct pci_host_bridge *pci_alloc_host_bridge(struct pci_bus *b) { struct pci_host_bridge *bridge; @@ -476,6 +498,10 @@ static struct pci_host_bridge *pci_alloc_host_bridge(struct pci_bus *b) if (bridge) { INIT_LIST_HEAD(&bridge->windows); bridge->bus = b; + bridge->dev.release = pci_release_host_bridge_dev; + dev_set_name(&bridge->dev, "pci%04x:%02x", + pci_domain_nr(b), b->number); + device_initialize(&bridge->dev); } return bridge; @@ -628,28 +654,13 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent, /* * Allocate a new bus, and inherit stuff from the parent.. */ - child = pci_alloc_bus(); + child = pci_alloc_bus(parent->ops, parent->sysdata, busnr); if (!child) return NULL; child->parent = parent; - child->ops = parent->ops; - child->sysdata = parent->sysdata; child->bus_flags = parent->bus_flags; - - /* initialize some portions of the bus device, but don't register it - * now as the parent is not properly set up yet. - */ - child->dev.class = &pcibus_class; - dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr); - - /* - * Set up the primary, secondary and subordinate - * bus numbers. - */ - child->number = child->busn_res.start = busnr; child->primary = parent->busn_res.start; - child->busn_res.end = 0xff; if (!bridge) { child->dev.parent = parent->bridge; @@ -670,7 +681,7 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent, bridge->subordinate = child; add_dev: - ret = device_register(&child->dev); + ret = device_add(&child->dev); WARN_ON(ret < 0); pcibios_add_bus(child); @@ -1190,18 +1201,6 @@ int pci_cfg_space_size(struct pci_dev *dev) return PCI_CFG_SPACE_SIZE; } -static void pci_release_bus_bridge_dev(struct device *dev) -{ - struct pci_host_bridge *bridge = to_pci_host_bridge(dev); - - if (bridge->release_fn) - bridge->release_fn(bridge); - - pci_free_resource_list(&bridge->windows); - - kfree(bridge); -} - struct pci_dev *pci_alloc_dev(struct pci_bus *bus) { struct pci_dev *dev; @@ -1688,13 +1687,10 @@ struct pci_bus *pci_create_root_bus(struct device *parent, int bus, char bus_addr[64]; char *fmt; - b = pci_alloc_bus(); + b = pci_alloc_bus(ops, sysdata, bus); if (!b) return NULL; - b->sysdata = sysdata; - b->ops = ops; - b->number = b->busn_res.start = bus; b2 = pci_find_bus(pci_domain_nr(b), bus); if (b2) { /* If we already got to this bus through a different bridge, ignore it */ @@ -1706,27 +1702,22 @@ struct pci_bus *pci_create_root_bus(struct device *parent, int bus, if (!bridge) goto err_out; - bridge->dev.parent = parent; - bridge->dev.release = pci_release_bus_bridge_dev; - dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(b), bus); error = pcibios_root_bridge_prepare(bridge); if (error) goto bridge_dev_reg_err; - error = device_register(&bridge->dev); + bridge->dev.parent = parent; + error = device_add(&bridge->dev); if (error) goto bridge_dev_reg_err; + b->bridge = get_device(&bridge->dev); device_enable_async_suspend(b->bridge); pci_set_bus_of_node(b); - if (!parent) set_dev_node(b->bridge, pcibus_to_node(b)); - - b->dev.class = &pcibus_class; b->dev.parent = b->bridge; - dev_set_name(&b->dev, "%04x:%02x", pci_domain_nr(b), bus); - error = device_register(&b->dev); + error = device_add(&b->dev); if (error) goto class_dev_reg_err; @@ -1769,12 +1760,11 @@ struct pci_bus *pci_create_root_bus(struct device *parent, int bus, return b; class_dev_reg_err: - put_device(&bridge->dev); - device_unregister(&bridge->dev); + device_del(&bridge->dev); bridge_dev_reg_err: - kfree(bridge); + put_device(&bridge->dev); err_out: - kfree(b); + put_device(&b->dev); return NULL; } diff --git a/drivers/pci/remove.c b/drivers/pci/remove.c index 8fc54b7..b0ce875 100644 --- a/drivers/pci/remove.c +++ b/drivers/pci/remove.c @@ -52,7 +52,8 @@ void pci_remove_bus(struct pci_bus *bus) up_write(&pci_bus_sem); pci_remove_legacy_files(bus); pcibios_remove_bus(bus); - device_unregister(&bus->dev); + device_del(&bus->dev); + put_device(&bus->dev); } EXPORT_SYMBOL(pci_remove_bus);