From: Jiang Liu <jiang.liu@huawei.com>
To: Bjorn Helgaas <bhelgaas@google.com>
Cc: Jiang Liu <liuj97@gmail.com>, Don Dutile <ddutile@redhat.com>,
Yinghai Lu <yinghai@kernel.org>,
Greg KH <gregkh@linuxfoundation.org>,
Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>,
Taku Izumi <izumi.taku@jp.fujitsu.com>,
"Rafael J . Wysocki" <rjw@sisk.pl>,
Yijing Wang <wangyijing@huawei.com>,
Xinwei Hu <huxinwei@huawei.com>, <linux-kernel@vger.kernel.org>,
<linux-pci@vger.kernel.org>
Subject: Re: [RFC PATCH v1 01/22] PCI: use pci_get_domain_bus_and_slot() to avoid race conditions
Date: Wed, 12 Sep 2012 16:37:09 +0800 [thread overview]
Message-ID: <505049B5.6030905@huawei.com> (raw)
In-Reply-To: <CAErSpo5nOJBCags9=e1y9ZcSDK9y69mZy=_+x0m_9bK78yVUJg@mail.gmail.com>
On 2012-9-12 6:00, Bjorn Helgaas wrote:
> On Tue, Aug 7, 2012 at 10:10 AM, Jiang Liu <liuj97@gmail.com> wrote:
>> There's a typical usage pattern to search a PCI device under a specific
>> PCI bus (domian, busno) as below:
>> struct pci_bus *pci_bus = pci_find_bus(domain, busno);
>> struct pci_dev *pci_dev = pci_get_slot(pci_bus, devfn);
>>
>> The above code has a race window between pci_find_bus() and pci_get_slot()
>> if PCI hotplug operations happen between them which removes the pci_bus.
>> So use PCI hotplug safe interface pci_get_domain_bus_and_slot() instead,
>> which also reduces code complexity.
>
> This makes sense to me. If we support hotplug, it's fundamentally
> unsafe to keep a struct pci_bus * without having a reference or some
> way to make sure it doesn't go away. I think we should try to
> eradicate all uses of pci_find_bus() and pci_find_next_bus().
Hi Bjorn
I have split out these into a separate patch set at
http://www.spinics.net/lists/linux-pci/msg17227.html.
In my latest PCI bus lock patch set, I have introduced a new
interface pci_get_bus() to replace pci_find_bus(). But pci_find_next_bus()
is a little harder to deal with, still need more work here.
Thanks!
>
>> Signed-off-by: Jiang Liu <liuj97@gmail.com>
>> ---
>> arch/ia64/sn/kernel/io_common.c | 4 +---
>> drivers/gpu/vga/vgaarb.c | 15 +++------------
>> drivers/pci/hotplug/cpcihp_generic.c | 8 ++------
>> drivers/pci/iov.c | 8 ++------
>> drivers/pci/xen-pcifront.c | 10 ++--------
>> 5 files changed, 10 insertions(+), 35 deletions(-)
>>
>> diff --git a/arch/ia64/sn/kernel/io_common.c b/arch/ia64/sn/kernel/io_common.c
>> index fbb5f2f..8630875 100644
>> --- a/arch/ia64/sn/kernel/io_common.c
>> +++ b/arch/ia64/sn/kernel/io_common.c
>> @@ -229,7 +229,6 @@ void sn_pci_fixup_slot(struct pci_dev *dev, struct pcidev_info *pcidev_info,
>> {
>> int segment = pci_domain_nr(dev->bus);
>> struct pcibus_bussoft *bs;
>> - struct pci_bus *host_pci_bus;
>> struct pci_dev *host_pci_dev;
>> unsigned int bus_no, devfn;
>>
>> @@ -245,8 +244,7 @@ void sn_pci_fixup_slot(struct pci_dev *dev, struct pcidev_info *pcidev_info,
>>
>> bus_no = (pcidev_info->pdi_slot_host_handle >> 32) & 0xff;
>> devfn = pcidev_info->pdi_slot_host_handle & 0xffffffff;
>> - host_pci_bus = pci_find_bus(segment, bus_no);
>> - host_pci_dev = pci_get_slot(host_pci_bus, devfn);
>> + host_pci_dev = pci_get_domain_bus_and_slot(segment, bus_no, devfn);
>>
>> pcidev_info->host_pci_dev = host_pci_dev;
>> pcidev_info->pdi_linux_pcidev = dev;
>> diff --git a/drivers/gpu/vga/vgaarb.c b/drivers/gpu/vga/vgaarb.c
>> index 3df8fc0..b6852b7 100644
>> --- a/drivers/gpu/vga/vgaarb.c
>> +++ b/drivers/gpu/vga/vgaarb.c
>> @@ -1066,7 +1066,6 @@ static ssize_t vga_arb_write(struct file *file, const char __user * buf,
>> }
>>
>> } else if (strncmp(curr_pos, "target ", 7) == 0) {
>> - struct pci_bus *pbus;
>> unsigned int domain, bus, devfn;
>> struct vga_device *vgadev;
>>
>> @@ -1085,19 +1084,11 @@ static ssize_t vga_arb_write(struct file *file, const char __user * buf,
>> pr_debug("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos,
>> domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
>>
>> - pbus = pci_find_bus(domain, bus);
>> - pr_debug("vgaarb: pbus %p\n", pbus);
>> - if (pbus == NULL) {
>> - pr_err("vgaarb: invalid PCI domain and/or bus address %x:%x\n",
>> - domain, bus);
>> - ret_val = -ENODEV;
>> - goto done;
>> - }
>> - pdev = pci_get_slot(pbus, devfn);
>> + pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
>> pr_debug("vgaarb: pdev %p\n", pdev);
>> if (!pdev) {
>> - pr_err("vgaarb: invalid PCI address %x:%x\n",
>> - bus, devfn);
>> + pr_err("vgaarb: invalid PCI address %x:%x:%x\n",
>> + domain, bus, devfn);
>> ret_val = -ENODEV;
>> goto done;
>> }
>> diff --git a/drivers/pci/hotplug/cpcihp_generic.c b/drivers/pci/hotplug/cpcihp_generic.c
>> index 81af764..a6a71c4 100644
>> --- a/drivers/pci/hotplug/cpcihp_generic.c
>> +++ b/drivers/pci/hotplug/cpcihp_generic.c
>> @@ -154,12 +154,8 @@ static int __init cpcihp_generic_init(void)
>> if(!r)
>> return -EBUSY;
>>
>> - bus = pci_find_bus(0, bridge_busnr);
>> - if (!bus) {
>> - err("Invalid bus number %d", bridge_busnr);
>> - return -EINVAL;
>> - }
>> - dev = pci_get_slot(bus, PCI_DEVFN(bridge_slot, 0));
>> + dev = pci_get_domain_bus_and_slot(0, bridge_busnr,
>> + PCI_DEVFN(bridge_slot, 0));
>> if(!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
>> err("Invalid bridge device %s", bridge);
>> pci_dev_put(dev);
>> diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
>> index 74bbaf8..c7d2969 100644
>> --- a/drivers/pci/iov.c
>> +++ b/drivers/pci/iov.c
>> @@ -152,15 +152,11 @@ failed1:
>> static void virtfn_remove(struct pci_dev *dev, int id, int reset)
>> {
>> char buf[VIRTFN_ID_LEN];
>> - struct pci_bus *bus;
>> struct pci_dev *virtfn;
>> struct pci_sriov *iov = dev->sriov;
>>
>> - bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id));
>> - if (!bus)
>> - return;
>> -
>> - virtfn = pci_get_slot(bus, virtfn_devfn(dev, id));
>> + virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
>> + virtfn_bus(dev, id), virtfn_devfn(dev, id));
>> if (!virtfn)
>> return;
>>
>> diff --git a/drivers/pci/xen-pcifront.c b/drivers/pci/xen-pcifront.c
>> index d6cc62c..def8d0b 100644
>> --- a/drivers/pci/xen-pcifront.c
>> +++ b/drivers/pci/xen-pcifront.c
>> @@ -982,7 +982,6 @@ static int pcifront_detach_devices(struct pcifront_device *pdev)
>> int err = 0;
>> int i, num_devs;
>> unsigned int domain, bus, slot, func;
>> - struct pci_bus *pci_bus;
>> struct pci_dev *pci_dev;
>> char str[64];
>>
>> @@ -1032,13 +1031,8 @@ static int pcifront_detach_devices(struct pcifront_device *pdev)
>> goto out;
>> }
>>
>> - pci_bus = pci_find_bus(domain, bus);
>> - if (!pci_bus) {
>> - dev_dbg(&pdev->xdev->dev, "Cannot get bus %04x:%02x\n",
>> - domain, bus);
>> - continue;
>> - }
>> - pci_dev = pci_get_slot(pci_bus, PCI_DEVFN(slot, func));
>> + pci_dev = pci_get_domain_bus_and_slot(domain, bus,
>> + PCI_DEVFN(slot, func));
>> if (!pci_dev) {
>> dev_dbg(&pdev->xdev->dev,
>> "Cannot get PCI device %04x:%02x:%02x.%d\n",
>> --
>> 1.7.9.5
>>
>
> .
>
next prev parent reply other threads:[~2012-09-12 8:39 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-08-07 16:10 [RFC PATCH v1 00/22] introduce PCI bus lock to serialize PCI hotplug operations Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 01/22] PCI: use pci_get_domain_bus_and_slot() to avoid race conditions Jiang Liu
2012-09-11 22:00 ` Bjorn Helgaas
2012-09-12 8:37 ` Jiang Liu [this message]
2012-08-07 16:10 ` [RFC PATCH v1 02/22] PCI: trivial cleanups for drivers/pci/remove.c Jiang Liu
2012-09-11 22:03 ` Bjorn Helgaas
2012-09-12 8:50 ` Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 03/22] PCI: change PCI device management code to better follow device model Jiang Liu
2012-09-11 22:03 ` Bjorn Helgaas
2012-08-07 16:10 ` [RFC PATCH v1 04/22] PCI: split PCI bus device registration into two stages Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 05/22] PCI: introduce pci_bus_{get|put}() to manage PCI bus reference count Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 06/22] PCI: use a global lock to serialize PCI root bridge hotplug operations Jiang Liu
2012-09-11 22:57 ` Bjorn Helgaas
2012-09-12 15:42 ` Jiang Liu
2012-09-12 16:51 ` Bjorn Helgaas
2012-09-13 16:00 ` [PATCH 1/2] PCI: introduce root bridge hotplug safe interfaces to walk root buses Jiang Liu
2012-09-13 17:40 ` Bjorn Helgaas
2012-09-17 15:55 ` Jiang Liu
2012-09-17 16:24 ` Bjorn Helgaas
2012-09-18 21:39 ` Bjorn Helgaas
2012-09-21 16:07 ` [PATCH v4] PCI: introduce two interfaces to walk PCI buses Jiang Liu
2012-09-26 20:14 ` Bjorn Helgaas
2012-09-13 16:00 ` [PATCH 2/2] PCI: remove host bridge hotplug unsafe interface pci_get_next_bus() Jiang Liu
2012-09-17 15:51 ` [RFC PATCH v1 06/22] PCI: use a global lock to serialize PCI root bridge hotplug operations Jiang Liu
2012-09-20 18:49 ` Paul E. McKenney
2012-08-07 16:10 ` [RFC PATCH v1 07/22] PCI: introduce PCI bus lock to serialize PCI " Jiang Liu
2012-09-11 23:24 ` Bjorn Helgaas
2012-08-07 16:10 ` [RFC PATCH v1 08/22] PCI: introduce hotplug safe search interfaces for PCI bus/device Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 09/22] PCI: enhance PCI probe logic to support PCI bus lock mechanism Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 10/22] PCI: enhance PCI bus specific " Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 11/22] PCI: enhance PCI resource assignment " Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 12/22] PCI: enhance PCI remove " Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 13/22] PCI: make each PCI device hold a reference to its parent PCI bus Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 14/22] PCI/sysfs: use PCI bus lock to avoid race conditions Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 15/22] PCI/eeepc: " Jiang Liu
2012-09-11 23:18 ` Bjorn Helgaas
2012-09-12 14:24 ` [PATCH] eeepc-laptop: fix device reference count leakage in eeepc_rfkill_hotplug() Jiang Liu
2012-09-12 19:59 ` Bjorn Helgaas
2012-08-07 16:10 ` [RFC PATCH v1 16/22] PCI/asus-wmi: use PCI bus lock to avoid race conditions Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 17/22] PCI/pciehp: " Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 18/22] PCI/acpiphp: " Jiang Liu
2012-08-07 16:10 ` [RFC PATCH v1 19/22] PCI/x86: enable PCI bus lock mechanism for x86 platforms Jiang Liu
2012-09-11 23:22 ` Bjorn Helgaas
2012-09-12 9:56 ` Jiang Liu
2012-08-07 16:11 ` [RFC PATCH v1 20/22] PCI/IA64: enable PCI bus lock mechanism for IA64 platforms Jiang Liu
2012-08-07 16:11 ` [RFC PATCH v1 21/22] PCI: cleanups for PCI bus lock implementation Jiang Liu
2012-09-11 23:21 ` Bjorn Helgaas
2012-09-12 8:58 ` Jiang Liu
2012-08-07 16:11 ` [RFC PATCH v1 22/22] PCI: unexport pci_root_buses Jiang Liu
2012-08-07 18:11 ` [RFC PATCH v1 00/22] introduce PCI bus lock to serialize PCI hotplug operations Don Dutile
2012-08-08 15:49 ` Jiang Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=505049B5.6030905@huawei.com \
--to=jiang.liu@huawei.com \
--cc=bhelgaas@google.com \
--cc=ddutile@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=huxinwei@huawei.com \
--cc=izumi.taku@jp.fujitsu.com \
--cc=kaneshige.kenji@jp.fujitsu.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=liuj97@gmail.com \
--cc=rjw@sisk.pl \
--cc=wangyijing@huawei.com \
--cc=yinghai@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).