From patchwork Fri Sep 21 16:07:38 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Liu X-Patchwork-Id: 185812 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 553EA2C0084 for ; Sat, 22 Sep 2012 02:07:52 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752589Ab2IUQHu (ORCPT ); Fri, 21 Sep 2012 12:07:50 -0400 Received: from mail-pb0-f46.google.com ([209.85.160.46]:36358 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751659Ab2IUQHt (ORCPT ); Fri, 21 Sep 2012 12:07:49 -0400 Received: by pbbrr4 with SMTP id rr4so2928632pbb.19 for ; Fri, 21 Sep 2012 09:07:49 -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:x-mailer:in-reply-to:references; bh=dto30Be3qTcgFFy26OuRWvXkKjrSOY8zVBoDTnggpy8=; b=GDR8U00aumD6+GygQRJsnxRUVBrsI3nCbtqkCKpOvE+pIULvoblXE2qiEkFraNXyzz J9CYcZBOP9xhuvGfRF5JnO5CL+DbnCiKnjcSLm8Et42oSjYbmijt4nXLnpdpQr6bRRB9 W3mG6mHcBeqxYqM7uGPcIDV/vvSszB2ompPHTIJO8ZzAuFIKZ18Gs2pLrxA2M5rrVh4u IaHhQu06O+lCJQpp0DrjM9BstTvm8HfEbuHQHhpc0GnTPH6BSYEj3V3OTELvyqrHPIM1 5CQeGhTMxmvuRBfE95GHjbjX4+uNdHjckSyx4TsQX5J5rMXrsinc7AhuUXQrskOpph0D KDFQ== Received: by 10.68.197.100 with SMTP id it4mr17154594pbc.16.1348243669070; Fri, 21 Sep 2012 09:07:49 -0700 (PDT) Received: from localhost.localdomain ([120.196.98.122]) by mx.google.com with ESMTPS id pj8sm5314385pbb.60.2012.09.21.09.07.45 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 21 Sep 2012 09:07:47 -0700 (PDT) From: Jiang Liu To: Bjorn Helgaas Cc: Jiang Liu , Yinghai Lu , Kenji Kaneshige , Yijing Wang , Jiang Liu , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH v4] PCI: introduce two interfaces to walk PCI buses Date: Sat, 22 Sep 2012 00:07:38 +0800 Message-Id: <1348243658-5127-1-git-send-email-jiang.liu@huawei.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The pci_find_next_bus() is not hotplug safe, so introduce PCI hotplug safe interfaces to walk PCI buses. To avoid some deadlock scenarios, two interfaces are introduced. The first one is pci_for_each_bus(), which walks all PCI buses holding read lock on the pci_bus_sem. The second one is pci_for_each_started_bus(), which walks all started PCI buses without holding any global locks. Started PCI buses are those which have been added to the device tree by calling device_add(). --- Hi Bjorn, How about this PCI bus iterator design? It's a little ugly that we need to two interfaces to work around some deadlock scenarios. And I plan to split the task into two parts: 1) a hotplug safe PCI bus iteraror to replace pci_find_next_bus 2) handle hotplug notifications to update bus related states. My patchset at http://www.spinics.net/lists/linux-pci/msg17515.html has patially solved issue 2 above for x86/ACPI, and will add more supports for other platforms. Thanks! Gerry --- drivers/pci/bus.c | 42 ++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 1 + 2 files changed, 43 insertions(+) diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c index e16a8f0f..21b0ade 100644 --- a/drivers/pci/bus.c +++ b/drivers/pci/bus.c @@ -327,6 +327,48 @@ void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), } EXPORT_SYMBOL_GPL(pci_walk_bus); +static int pci_bus_iter(struct pci_bus *bus, + int (* cb)(struct pci_bus *, void *), void *data) +{ + int rc; + + rc = cb(bus, data); + if (rc == 0) + list_for_each_entry(bus, &bus->children, node) { + rc = pci_bus_iter(bus, cb, data); + if (rc) + break; + } + + return rc; +} + +/** pci_for_each_bus - walk all PCI buses and call the provided callback. + * @cb callback to be called for each bus found + * @data arbitrary pointer to be passed to callback. + * + * Walk all PCI buses and call the provided callback with pci_bus_sem held. + * + * We check the return of @cb each time. If it returns anything + * other than 0, we break out. + */ +int pci_for_each_bus(int (* cb)(struct pci_bus *, void *), void *data) +{ + int rc = 0; + struct pci_bus *bus; + + down_read(&pci_bus_sem); + list_for_each_entry(bus, &pci_root_buses, node) { + rc = pci_bus_iter(bus, cb, data); + if (rc) + break; + } + up_read(&pci_bus_sem); + + return rc; +} +EXPORT_SYMBOL(pci_for_each_bus); + struct pci_bus *pci_bus_get(struct pci_bus *bus) { if (bus) diff --git a/include/linux/pci.h b/include/linux/pci.h index 3c5017d..1423a24 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1060,6 +1060,7 @@ int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), void *userdata); +int pci_for_each_bus(int (* cb)(struct pci_bus *, void *), void *data); int pci_cfg_space_size_ext(struct pci_dev *dev); int pci_cfg_space_size(struct pci_dev *dev); unsigned char pci_bus_max_busnr(struct pci_bus *bus);