Skip to contentSkip to conversation

Problems accessing passthrough PCI device

DISCUSSION Simon Martin 3 participants 17 messages Nov 13, 2014 – Nov 21, 2014 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

Hi all,

I am back on my virtual machine once again and have run into a bit of a problem (once again). So I am coming to you cap in hand...

I am having 2 major problems at the moment.

1.- Access to the PCI device from the PV will fail the second time I create it UNLESS I call xl pci-assignable-remove/pci-assignable-add between each creation. If I don't do this then all PCI accesses return -1. I get the same if I disable memory access in the PCI configuration register.

1.1.- Is this expected behaviour?

1.2.- If not, how do I work around it? I have looked at
HYPERVISOR_physdev_op but I'm not sure how/whether to use it.

1.3.- xl dmesg and dmesg in Dom0 do not show anything. I have set loglvl=all in the Xen command line.

2.- Whenever I perform a software reset on the PCI device (it is an Intel 82546 Ethernet NIC) the hypervisor crashes. There is no oops, kernel panic or the like, just a crash. My development device has no serial port so I can't do much debugging.

Any suggestions.

Signature · 2 lines
Best regards,
 Simon                          mailto:[email protected]
#1 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 13.11.14 at 14:29, <[email protected]> wrote:

I am having 2 major problems at the moment.

1.- Access to the PCI device from the PV will fail the second time I create it UNLESS I call xl pci-assignable-remove/pci-assignable-add between each creation. If I don't do this then all PCI accesses return -1. I get the same if I disable memory access in the PCI configuration register.

1.1.- Is this expected behaviour?

No. But did you verify the driver properly enables the device (i.e. doesn't make assumptions on its state)? Also iirc some kernel versions weren't properly resetting the device when coming back from guest use, but without you stating the software versions you're using that's just a remote possibility.

Quoted 2 lines

1.3.- xl dmesg and dmesg in Dom0 do not show anything. I have set loglvl=all in the Xen command line.

"iommu=debug"? But given the symptoms above I don't really think this is a problem with the IOMMU.

Quoted 4 lines

2.- Whenever I perform a software reset on the PCI device (it is an Intel 82546 Ethernet NIC) the hypervisor crashes. There is no oops, kernel panic or the like, just a crash. My development device has no serial port so I can't do much debugging.

A crash would imply you see something telling you it's a crash. If you see nothing, I'd rather call it a hang or spontaneous reboot. But even without serial port (assuming you can't even plug in a serial port PCI card) there are ways to get at eventual crash output (register+stack dump): For one, we've got USB debug port support. This requires a special cable and there not being an (internal) hub in between, but it's worth a consideration. And in the worst case, "vga=keep" allows the hypervisor to continue printing to the screen even post-boot. But that requires the video mode to not be changed from the one Xen uses at boot (i.e. you should not use DRM's KMS, and if you need to use X it would need to be configured to use the frame buffer driver rather than any accelerating one).

Jan

#2 lore raw

Thanks Jan,

Quoted 9 lines · you
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 13.11.14 at 14:29, <[email protected]> wrote:

I am having 2 major problems at the moment.

1.- Access to the PCI device from the PV will fail the second time I create it UNLESS I call xl pci-assignable-remove/pci-assignable-add between each creation. If I don't do this then all PCI accesses return -1. I get the same if I disable memory access in the PCI configuration register.

Quoted 7 lines
Quoted 1 line

1.1.- Is this expected behaviour?

No. But did you verify the driver properly enables the device (i.e. doesn't make assumptions on its state)? Also iirc some kernel versions weren't properly resetting the device when coming back from guest use, but without you stating the software versions you're using that's just a remote possibility.

Yes, the first thing I do in the driver is set the PCI configuration access bits to 7 that should enable IO space, Memory Space and Master BUS access.

As a test I disabled this and all reads to the PCI device return -1, even the first one.

Quoted 5 lines
Quoted 2 lines

1.3.- xl dmesg and dmesg in Dom0 do not show anything. I have set loglvl=all in the Xen command line.

"iommu=debug"? But given the symptoms above I don't really think this is a problem with the IOMMU.

Added this to the command line and when the PV starts I now see:

(XEN) [VT-D]iommu.c:1593: d0:PCI: unmap 0000:00:19.0 (XEN) [VT-D]iommu.c:1456: d4:PCI: map 0000:00:19.0

And when the PV stops I see

(XEN) [VT-D]iommu.c:1593: d4:PCI: unmap 0000:00:19.0 (XEN) [VT-D]iommu.c:1456: d0:PCI: map 0000:00:19.0

No more that this.

Quoted 5 lines
Quoted 4 lines

2.- Whenever I perform a software reset on the PCI device (it is an Intel 82546 Ethernet NIC) the hypervisor crashes. There is no oops, kernel panic or the like, just a crash. My development device has no serial port so I can't do much debugging.

Quoted 12 lines

A crash would imply you see something telling you it's a crash. If you see nothing, I'd rather call it a hang or spontaneous reboot. But even without serial port (assuming you can't even plug in a serial port PCI card) there are ways to get at eventual crash output (register+stack dump): For one, we've got USB debug port support. This requires a special cable and there not being an (internal) hub in between, but it's worth a consideration. And in the worst case, "vga=keep" allows the hypervisor to continue printing to the screen even post-boot. But that requires the video mode to not be changed from the one Xen uses at boot (i.e. you should not use DRM's KMS, and if you need to use X it would need to be configured to use the frame buffer driver rather than any accelerating one).

I agree, this looks more than a hang than a crash. I've just found a link to the USB debug cable. I've ordered one but it will take a while to get here (I live in Chile). I'll try to enable the vga keep, at least to see if I can debug this.

Signature · 2 lines
Best regards,
 Simon                            mailto:[email protected]
#3 lore raw
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 13.11.14 at 16:07, <[email protected]> wrote:

Thanks Jan,

Quoted 9 lines · you
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 13.11.14 at 14:29, <[email protected]> wrote:

I am having 2 major problems at the moment.

1.- Access to the PCI device from the PV will fail the second time I create it UNLESS I call xl pci-assignable-remove/pci-assignable-add between each creation. If I don't do this then all PCI accesses return -1. I get the same if I disable memory access in the PCI configuration register.

Quoted 7 lines
Quoted 1 line

1.1.- Is this expected behaviour?

No. But did you verify the driver properly enables the device (i.e. doesn't make assumptions on its state)? Also iirc some kernel versions weren't properly resetting the device when coming back from guest use, but without you stating the software versions you're using that's just a remote possibility.

Quoted 6 lines

Yes, the first thing I do in the driver is set the PCI configuration access bits to 7 that should enable IO space, Memory Space and Master BUS access.

As a test I disabled this and all reads to the PCI device return -1, even the first one.

I implied your earlier statement to mean that. But - did you also verify that the three flags actually end up set (ideally from both DomU and Dom0 perspective)? The PCI backend may be screwing up things...

Quoted 2 lines

I agree, this looks more than a hang than a crash. I've just found a link to the USB debug cable. I've ordered one but it will take a while

And I hope you first verified that your system meets the criteria for this to work in the first place?

Jan

#4 lore raw

Hello Jan,

Quoted 5 lines
Quoted 5 lines

Yes, the first thing I do in the driver is set the PCI configuration access bits to 7 that should enable IO space, Memory Space and Master BUS access.

Quoted 7 lines
Quoted 2 lines

As a test I disabled this and all reads to the PCI device return -1, even the first one.

I implied your earlier statement to mean that. But - did you also verify that the three flags actually end up set (ideally from both DomU and Dom0 perspective)? The PCI backend may be screwing up things...

Yes I do verify the write. How do I check this from Dom0?

Quoted 5 lines
Quoted 2 lines

I agree, this looks more than a hang than a crash. I've just found a link to the USB debug cable. I've ordered one but it will take a while

And I hope you first verified that your system meets the criteria for this to work in the first place?

I followed the checks in the Kernel earlyprintk debugging HOWTO, i.e. lspci -vvv to check the USB debug capability, and it verified. The device is ordered and now just to wait....

Signature · 2 lines
Best regards,
 Simon                            mailto:[email protected]
#5 lore raw
Quoted 10 lines · Simon Martin

Hello Jan,

Quoted 7 lines
Quoted 7 lines

Yes, the first thing I do in the driver is set the PCI configuration access bits to 7 that should enable IO space, Memory Space and Master BUS access.

As a test I disabled this and all reads to the PCI device return -1, even the first one.

Quoted 6 lines
Quoted 4 lines

I implied your earlier statement to mean that. But - did you also verify that the three flags actually end up set (ideally from both DomU and Dom0 perspective)? The PCI backend may be screwing up things...

Yes I do verify the write. How do I check this from Dom0?

You can crank up the debug volume in xen-pciback. Recompile the kernel and put '#define DEBUG 1' at the start of the .c files.

Interestingly enough I saw this issue as well - and it looked to me that Xen pciback was stuck in '7' state (Reconfiguring) and never excited it.

But I hadn't had a chance to dig in this.

Quoted 21 lines
Quoted 6 lines
Quoted 3 lines

I agree, this looks more than a hang than a crash. I've just found a link to the USB debug cable. I've ordered one but it will take a while

And I hope you first verified that your system meets the criteria for this to work in the first place?

I followed the checks in the Kernel earlyprintk debugging HOWTO, i.e. lspci -vvv to check the USB debug capability, and it verified. The device is ordered and now just to wait....

Signature · 8 lines
Best regards,
 Simon                            mailto:[email protected]


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#6 lore raw

Thanks Konrad,

Quoted 10 lines · you
Quoted 1 line

Yes I do verify the write. How do I check this from Dom0?

You can crank up the debug volume in xen-pciback. Recompile the kernel and put '#define DEBUG 1' at the start of the .c files.

Interestingly enough I saw this issue as well - and it looked to me that Xen pciback was stuck in '7' state (Reconfiguring) and never excited it.

But I hadn't had a chance to dig in this.

This might be related to something that I reported a while ago, that
when I shutdown the PV it takes a long time for the corresponding
Dom0 processes to stop.

I'll set the debug level and see what's going on. That'll be on Monday now.

Signature · 2 lines
Best regards,
 Simon                            mailto:[email protected]
#7 lore raw
Quoted 13 lines · Simon Martin

Thanks Konrad,

Quoted 8 lines · you
Quoted 1 line

Yes I do verify the write. How do I check this from Dom0?

You can crank up the debug volume in xen-pciback. Recompile the kernel and put '#define DEBUG 1' at the start of the .c files.

Interestingly enough I saw this issue as well - and it looked to me that Xen pciback was stuck in '7' state (Reconfiguring) and never excited it.

Quoted 8 lines
Quoted 1 line

But I hadn't had a chance to dig in this.

This might be related to something that I reported a while ago, that
when I shutdown the PV it takes a long time for the corresponding
Dom0 processes to stop.

I'll set the debug level and see what's going on. That'll be on Monday now.

Sure thing. Thank you for reporting it!

Quoted 6 lines
Signature · 3 lines
-- 
Best regards,
 Simon                            mailto:[email protected]
#8 lore raw

Hello Konrad,

Quoted 14 lines · you
Quoted 13 lines · Simon Martin

Thanks Konrad,

Quoted 8 lines · you
Quoted 1 line

Yes I do verify the write. How do I check this from Dom0?

You can crank up the debug volume in xen-pciback. Recompile the kernel and put '#define DEBUG 1' at the start of the .c files.

Interestingly enough I saw this issue as well - and it looked to me that Xen pciback was stuck in '7' state (Reconfiguring) and never excited it.

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

But I hadn't had a chance to dig in this.

This might be related to something that I reported a while ago, that
when I shutdown the PV it takes a long time for the corresponding
Dom0 processes to stop.

I'll set the debug level and see what's going on. That'll be on Monday now.

I attach the output from xl dmesg and dmesg. As far as I can see, everything looks to be OK.

Signature · 2 lines
Best regards,
 Simon                            mailto:[email protected]

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw
In reply to Simon Martin
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines · On 13.11.14 at 18:49, <[email protected]> wrote:

Yes, the first thing I do in the driver is set the PCI configuration access bits to 7 that should enable IO space, Memory Space and Master BUS access.

As a test I disabled this and all reads to the PCI device return -1, even the first one.

Quoted 6 lines
Quoted 4 lines

I implied your earlier statement to mean that. But - did you also verify that the three flags actually end up set (ideally from both DomU and Dom0 perspective)? The PCI backend may be screwing up things...

Yes I do verify the write. How do I check this from Dom0?

Just use lspci.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

I agree, this looks more than a hang than a crash. I've just found a link to the USB debug cable. I've ordered one but it will take a while

And I hope you first verified that your system meets the criteria for this to work in the first place?

I followed the checks in the Kernel earlyprintk debugging HOWTO, i.e. lspci -vvv to check the USB debug capability, and it verified. The device is ordered and now just to wait....

As said before, the less obvious thing to verify is that there must not be a (hidden) hub in between for the port that can be used as the debug one. Some systems (I actually have at least one of that kind) surface some or all of their USB port via some internal hub; lsusb should be able to tell you. Anyway - you'll see whether it works when the device arrives.

Jan

#10 lore raw

Hello Jan,

Quoted 8 lines · you
Quoted 6 lines
Quoted 4 lines

I implied your earlier statement to mean that. But - did you also verify that the three flags actually end up set (ideally from both DomU and Dom0 perspective)? The PCI backend may be screwing up things...

Yes I do verify the write. How do I check this from Dom0?

Just use lspci.

I've just checked this with lspci. I see that the IO is being enabled.
Any other idea on why I might be reading back 0xff for all PCI
memory area reads? The lspci output follows.

Before starting DomU

smartin@smartin-xen:~$ lspci -s 00:19.0 -x
00:19.0 Ethernet controller: Intel Corporation Device 1559 (rev 04)
00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00

After DomU initialisation

smartin@smartin-xen:~$ lspci -s 00:19.0 -x
00:19.0 Ethernet controller: Intel Corporation Device 1559 (rev 04)
00: 86 80 59 15 02 00 10 00 04 00 00 02 00 00 00 00

After stopping DomU

smartin@smartin-xen:~$ lspci -s 00:19.0 -x
00:19.0 Ethernet controller: Intel Corporation Device 1559 (rev 04)
00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00
Signature · 2 lines
Best regards,
 Simon                            mailto:[email protected]
#11 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 18.11.14 at 17:24, <[email protected]> wrote:

Hello Jan,

Quoted 5 lines · you
Quoted 5 lines
Quoted 4 lines

I implied your earlier statement to mean that. But - did you also verify that the three flags actually end up set (ideally from both DomU and Dom0 perspective)? The PCI backend may be screwing up things...

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Yes I do verify the write. How do I check this from Dom0?

Just use lspci.

I've just checked this with lspci. I see that the IO is being enabled.

Memory you mean.

Quoted 2 lines

Any other idea on why I might be reading back 0xff for all PCI
memory area reads? The lspci output follows.

Since this isn't behind a bridge - no, not really. Did you try this with any other device for comparison purposes?

Jan

#12 lore raw

Hello Jan and Konrad,

Quoted 4 lines · you
Quoted 2 lines

I've just checked this with lspci. I see that the IO is being enabled.

Memory you mean.

Yes. Sorry.

Quoted 5 lines
Quoted 2 lines

Any other idea on why I might be reading back 0xff for all PCI
memory area reads? The lspci output follows.

Since this isn't behind a bridge - no, not really. Did you try this with any other device for comparison purposes?

This is getting more interesting. It seems that something is
overwriting the pci-back configuration data.

Starting from a fresh reboot I checked the Dom0 pci configuration and got this:

root@smartin-xen:~# lspci -s 00:19.0 -x
00:19.0 Ethernet controller: Intel Corporation Device 1559 (rev 04)
00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00
10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20
30: 00 00 00 00 c8 00 00 00 00 00 00 00 05 01 00 00

I then start/stop my DomU and checked the Dom0 pci configuration again and got this:

root@smartin-xen:~# lspci -s 00:19.0 -x
00:19.0 Ethernet controller: Intel Corporation Device 1559 (rev 04)
00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00
10: 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20
30: 00 00 00 00 c8 00 00 00 00 00 00 00 05 01 00 00

Inside my DomU I added code to print the PCI configuration registers and what I get after restarting the DomU is:

(d18) 14:57:04.042 src/e1000e.c@00150: 00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00 (d18) 14:57:04.042 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 14:57:04.042 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 14:57:04.043 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00 (d18) 14:57:04.043 src/e1000e.c@00324: Enable PCI Memory Access
(d18) 14:57:05.043 src/e1000e.c@00150: 00: 86 80 59 15 03 00 10 00 04 00 00 02 00 00 00 00 (d18) 14:57:05.044 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 14:57:05.044 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 14:57:05.045 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00

As you can see the pci configuration read from the pci-back driver by my DomU is different to the data in the Dom0 pci configuration!

Just before leaving my DomU I disable the pci memory access and this is what I see

(d18) 15:01:02.051 src/e1000e.c@00150: 00: 86 80 59 15 03 00 10 00 04 00 00 02 00 00 00 00 (d18) 15:01:02.051 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 15:01:02.051 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 15:01:02.052 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00 (d18) 15:01:02.052 src/e1000e.c@00541: Disable PCI Memory Access
(d18) 15:01:02.052 src/e1000e.c@00150: 00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00 (d18) 15:01:02.052 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 15:01:02.052 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 15:01:02.053 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00

As you can see the data is consistent with just writing 0000 to the pci control register.

This is the output from the debug version of the xen-pciback module.

[ 5429.351231] pciback 0000:00:19.0: enabling device (0000 -> 0003)
[ 5429.351367] xen: registering gsi 20 triggering 0 polarity 1
[ 5429.351373] Already setup the GSI :20
[ 5429.351387] pciback 0000:00:19.0: xen-pciback[0000:00:19.0]: #20 on  disable-> enable
[ 5429.351436] pciback 0000:00:19.0: xen-pciback[0000:00:19.0]: #20 on  enabled
[ 5434.360078] pciback 0000:00:19.0: xen-pciback[0000:00:19.0]: #20 off  enable-> disable
[ 5434.360116] pciback 0000:00:19.0: xen-pciback[0000:00:19.0]: #0 off  disabled
[ 5434.361491] xen-pciback pci-20-0: fe state changed 5
[ 5434.362473] xen-pciback pci-20-0: fe state changed 6
[ 5434.363540] xen-pciback pci-20-0: fe state changed 0
[ 5434.363544] xen-pciback pci-20-0: frontend is gone! unregister device
[ 5434.467359] pciback 0000:00:19.0: resetting virtual configuration space
[ 5434.467376] pciback 0000:00:19.0: free-ing dynamically allocated virtual configuration space fields

Does this make any sense to you?

Signature · 2 lines
Best regards,
 Simon                            mailto:[email protected]
Reviewed-by Boris Ostrovsky
#13 lore raw
Quoted 88 lines · Simon Martin

Hello Jan and Konrad,

Quoted 4 lines · you
Quoted 2 lines

I've just checked this with lspci. I see that the IO is being enabled.

Memory you mean.

Yes. Sorry.

Quoted 5 lines
Quoted 2 lines

Any other idea on why I might be reading back 0xff for all PCI
memory area reads? The lspci output follows.

Since this isn't behind a bridge - no, not really. Did you try this with any other device for comparison purposes?

This is getting more interesting. It seems that something is
overwriting the pci-back configuration data.

Starting from a fresh reboot I checked the Dom0 pci configuration and got this:

root@smartin-xen:~# lspci -s 00:19.0 -x
00:19.0 Ethernet controller: Intel Corporation Device 1559 (rev 04)
00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00
10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20
30: 00 00 00 00 c8 00 00 00 00 00 00 00 05 01 00 00

I then start/stop my DomU and checked the Dom0 pci configuration again and got this:

root@smartin-xen:~# lspci -s 00:19.0 -x
00:19.0 Ethernet controller: Intel Corporation Device 1559 (rev 04)
00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00
10: 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20
30: 00 00 00 00 c8 00 00 00 00 00 00 00 05 01 00 00

Inside my DomU I added code to print the PCI configuration registers and what I get after restarting the DomU is:

(d18) 14:57:04.042 src/e1000e.c@00150: 00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00 (d18) 14:57:04.042 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 14:57:04.042 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 14:57:04.043 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00 (d18) 14:57:04.043 src/e1000e.c@00324: Enable PCI Memory Access
(d18) 14:57:05.043 src/e1000e.c@00150: 00: 86 80 59 15 03 00 10 00 04 00 00 02 00 00 00 00 (d18) 14:57:05.044 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 14:57:05.044 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 14:57:05.045 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00

As you can see the pci configuration read from the pci-back driver by my DomU is different to the data in the Dom0 pci configuration!

Just before leaving my DomU I disable the pci memory access and this is what I see

(d18) 15:01:02.051 src/e1000e.c@00150: 00: 86 80 59 15 03 00 10 00 04 00 00 02 00 00 00 00 (d18) 15:01:02.051 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 15:01:02.051 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 15:01:02.052 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00 (d18) 15:01:02.052 src/e1000e.c@00541: Disable PCI Memory Access
(d18) 15:01:02.052 src/e1000e.c@00150: 00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00 (d18) 15:01:02.052 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 15:01:02.052 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 15:01:02.053 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00

As you can see the data is consistent with just writing 0000 to the pci control register.

This is the output from the debug version of the xen-pciback module.

[ 5429.351231] pciback 0000:00:19.0: enabling device (0000 -> 0003)
[ 5429.351367] xen: registering gsi 20 triggering 0 polarity 1
[ 5429.351373] Already setup the GSI :20
[ 5429.351387] pciback 0000:00:19.0: xen-pciback[0000:00:19.0]: #20 on  disable-> enable
[ 5429.351436] pciback 0000:00:19.0: xen-pciback[0000:00:19.0]: #20 on  enabled
[ 5434.360078] pciback 0000:00:19.0: xen-pciback[0000:00:19.0]: #20 off  enable-> disable
[ 5434.360116] pciback 0000:00:19.0: xen-pciback[0000:00:19.0]: #0 off  disabled
[ 5434.361491] xen-pciback pci-20-0: fe state changed 5
[ 5434.362473] xen-pciback pci-20-0: fe state changed 6
[ 5434.363540] xen-pciback pci-20-0: fe state changed 0
[ 5434.363544] xen-pciback pci-20-0: frontend is gone! unregister device
[ 5434.467359] pciback 0000:00:19.0: resetting virtual configuration space
[ 5434.467376] pciback 0000:00:19.0: free-ing dynamically allocated virtual configuration space fields
Quoted 1 line

Does this make any sense to you?

There was a bug in Xen pcibackend that I thought I upstreamed which could be releated. It was not restoring the right registers to the PCI-device.

They are attached.

Quoted 6 lines
Signature · 5 lines
-- 
Best regards,
 Simon                            mailto:[email protected]

From b5935d70083123aae48e115c7ed027a0ca79657f Mon Sep 17 00:00:00 2001

From: Konrad Rzeszutek Wilk <[email protected]>
Date: Mon, 14 Jul 2014 12:18:52 -0400
Subject: [PATCH 01/10] xen/pciback: Don't deadlock when unbinding.

As commit 0a9fd0152929db372ff61b0d6c280fdd34ae8bdb 'xen/pciback: Document the entry points for 'pcistub_put_pci_dev'' explained there are four entry points in this function. Two of them are when the user fiddles in the SysFS to unbind a device which might be in use by a guest or not.

Both 'unbind' states will cause a deadlock as the the PCI lock has already been taken, which then pci_device_reset tries to take.

We can simplify this by requiring that all callers of pcistub_put_pci_dev MUST hold the device lock. And then we can just call the lockless version of pci_device_reset.

To make it even simpler we will modify xen_pcibk_release_pci_dev to quality whether it should take a lock or not - as it ends up calling xen_pcibk_release_pci_dev and needs to hold the lock.

---
 drivers/xen/xen-pciback/passthrough.c | 14 +++++++++++---
 drivers/xen/xen-pciback/pci_stub.c    | 12 ++++++------
 drivers/xen/xen-pciback/pciback.h     |  7 ++++---
 drivers/xen/xen-pciback/vpci.c        | 14 +++++++++++---
 drivers/xen/xen-pciback/xenbus.c      |  2 +-
 5 files changed, 33 insertions(+), 16 deletions(-)
drivers/xen/xen-pciback/passthrough.c +11 -3
index 828dddc..f16a30e 100644
@@ -69,7 +69,7 @@ static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
 }
 
 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
-					struct pci_dev *dev)
+					struct pci_dev *dev, bool lock)
 {
 	struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
 	struct pci_dev_entry *dev_entry, *t;
@@ -87,8 +87,13 @@ static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
 
 	mutex_unlock(&dev_data->lock);
 
-	if (found_dev)
+	if (found_dev) {
+		if (lock)
+			device_lock(&found_dev->dev);
 		pcistub_put_pci_dev(found_dev);
+		if (lock)
+			device_unlock(&found_dev->dev);
+	}
 }
 
 static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
@@ -156,8 +161,11 @@ static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
 	struct pci_dev_entry *dev_entry, *t;
 
 	list_for_each_entry_safe(dev_entry, t, &dev_data->dev_list, list) {
+		struct pci_dev *dev = dev_entry->dev;
 		list_del(&dev_entry->list);
-		pcistub_put_pci_dev(dev_entry->dev);
+		device_lock(&dev->dev);
+		pcistub_put_pci_dev(dev);
+		device_unlock(&dev->dev);
 		kfree(dev_entry);
 	}
 
drivers/xen/xen-pciback/pci_stub.c +6 -6
index 017069a..9cbe1a3 100644
@@ -250,6 +250,8 @@ struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
  *  - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
  *
  *  As such we have to be careful.
+ *
+ *  To make this easier, the caller has to hold the device lock.
  */
 void pcistub_put_pci_dev(struct pci_dev *dev)
 {
@@ -276,11 +278,8 @@ void pcistub_put_pci_dev(struct pci_dev *dev)
 	/* Cleanup our device
 	 * (so it's ready for the next domain)
 	 */
-
-	/* This is OK - we are running from workqueue context
-	 * and want to inhibit the user from fiddling with 'reset'
-	 */
-	pci_reset_function(dev);
+	lockdep_assert_held(&dev->dev.mutex);
+	__pci_reset_function_locked(dev);
 	pci_restore_state(dev);
 
 	/* This disables the device. */
@@ -567,7 +566,8 @@ static void pcistub_remove(struct pci_dev *dev)
 			/* N.B. This ends up calling pcistub_put_pci_dev which ends up
 			 * doing the FLR. */
 			xen_pcibk_release_pci_dev(found_psdev->pdev,
-						found_psdev->dev);
+						found_psdev->dev,
+						false /* caller holds the lock. */);
 		}
 
 		spin_lock_irqsave(&pcistub_devices_lock, flags);
drivers/xen/xen-pciback/pciback.h +4 -3
index f72af87..58e38d5 100644
@@ -99,7 +99,8 @@ struct xen_pcibk_backend {
 		    unsigned int *domain, unsigned int *bus,
 		    unsigned int *devfn);
 	int (*publish)(struct xen_pcibk_device *pdev, publish_pci_root_cb cb);
-	void (*release)(struct xen_pcibk_device *pdev, struct pci_dev *dev);
+	void (*release)(struct xen_pcibk_device *pdev, struct pci_dev *dev,
+                        bool lock);
 	int (*add)(struct xen_pcibk_device *pdev, struct pci_dev *dev,
 		   int devid, publish_pci_dev_cb publish_cb);
 	struct pci_dev *(*get)(struct xen_pcibk_device *pdev,
@@ -122,10 +123,10 @@ static inline int xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
 }
 
 static inline void xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
-					     struct pci_dev *dev)
+					     struct pci_dev *dev, bool lock)
 {
 	if (xen_pcibk_backend && xen_pcibk_backend->release)
-		return xen_pcibk_backend->release(pdev, dev);
+		return xen_pcibk_backend->release(pdev, dev, lock);
 }
 
 static inline struct pci_dev *
drivers/xen/xen-pciback/vpci.c +11 -3
index 51afff9..c99f8bb 100644
@@ -145,7 +145,7 @@ out:
 }
 
 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
-					struct pci_dev *dev)
+					struct pci_dev *dev, bool lock)
 {
 	int slot;
 	struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
@@ -169,8 +169,13 @@ static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
 out:
 	mutex_unlock(&vpci_dev->lock);
 
-	if (found_dev)
+	if (found_dev) {
+		if (lock)
+			device_lock(&found_dev->dev);
 		pcistub_put_pci_dev(found_dev);
+		if (lock)
+			device_unlock(&found_dev->dev);
+	}
 }
 
 static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
@@ -208,8 +213,11 @@ static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
 		struct pci_dev_entry *e, *tmp;
 		list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
 					 list) {
+			struct pci_dev *dev = e->dev;
 			list_del(&e->list);
-			pcistub_put_pci_dev(e->dev);
+			device_lock(&dev->dev);
+			pcistub_put_pci_dev(dev);
+			device_unlock(&dev->dev);
 			kfree(e);
 		}
 	}
drivers/xen/xen-pciback/xenbus.c +1 -1
index ad8d30c..eeee499 100644
@@ -291,7 +291,7 @@ static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
 
 	/* N.B. This ends up calling pcistub_put_pci_dev which ends up
 	 * doing the FLR. */
-	xen_pcibk_release_pci_dev(pdev, dev);
+	xen_pcibk_release_pci_dev(pdev, dev, true /* use the lock. */);
 
 out:
 	return err;
Signature · 1 line
1.8.4.2
Quoted 1 line

From 01e3c0895b0c9ee957b09f946429226c241bb5d2 Mon Sep 17 00:00:00 2001

From: Konrad Rzeszutek Wilk <[email protected]>
Date: Wed, 6 Aug 2014 16:21:32 -0400
Subject: [PATCH 06/10] xen/pciback: Restore configuration space when detaching
from a guest.

The commit 9eea3f7695226f9af9992cebf8e98ac0ad78b277 "xen/pciback: Don't deadlock when unbinding." was using the version of pci_reset_function which would lock the device lock. That is no good as we can dead-lock. As such we swapped to using the lock-less version and requiring that the callers
of 'pcistub_put_pci_dev' take the device lock. And as such this bug got exposed.

Using the lock-less version is OK, except that we tried to use 'pci_restore_state' after the lock-less version of __pci_reset_function_locked - which won't work as 'state_saved' is set to false. Said 'state_saved' is a toggle boolean that is to be used by the sequence of a) pci_save_state/pci_restore_state or b) pci_load_and_free_saved_state/pci_restore_state. We don't want to use a) as the guest might have messed up the PCI configuration space and we want it to revert to the state when the PCI device was binded to us. Therefore we pick
b) to restore the configuration space.

To still retain the PCI configuration space, we save it once more and store it on our private copy to be restored when:
- Device is unbinded from pciback
- Device is detached from a guest.

---
 drivers/xen/xen-pciback/pci_stub.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/xen/xen-pciback/pci_stub.c +21 -4
index 843a2ba..1126c5a 100644
@@ -105,7 +105,7 @@ static void pcistub_device_release(struct kref *kref)
 	 */
 	__pci_reset_function_locked(dev);
 	if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
-		dev_dbg(&dev->dev, "Could not reload PCI state\n");
+		dev_info(&dev->dev, "Could not reload PCI state\n");
 	else
 		pci_restore_state(dev);
 
@@ -257,6 +257,7 @@ void pcistub_put_pci_dev(struct pci_dev *dev)
 {
 	struct pcistub_device *psdev, *found_psdev = NULL;
 	unsigned long flags;
+	struct xen_pcibk_dev_data *dev_data;
 
 	spin_lock_irqsave(&pcistub_devices_lock, flags);
 
@@ -279,9 +280,25 @@ void pcistub_put_pci_dev(struct pci_dev *dev)
 	 * (so it's ready for the next domain)
 	 */
 	device_lock_assert(&dev->dev);
-	__pci_reset_function_locked(dev);
-	pci_restore_state(dev);
-
+	dev_data = pci_get_drvdata(dev);
+	if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
+		dev_info(&dev->dev, "Could not reload PCI state\n");
+	else {
+		__pci_reset_function_locked(dev);
+		/*
+		 * The usual sequence is pci_save_state & pci_restore_state
+		 * but the guest might have messed the configuration space up.
+		 * Use the initial version (when device was binded to us).
+		 */
+		pci_restore_state(dev);
+		/*
+		 * The next steps are to reload the configuration for the
+		 * next time we bind & unbind to a guest - or unload from
+		 * pciback.
+		 */
+		pci_save_state(dev);
+		dev_data->pci_saved_state = pci_store_saved_state(dev);
+	}
 	/* This disables the device. */
 	xen_pcibk_reset_device(dev);
 
Signature · 1 line
1.8.4.2

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#14 lore raw

Hello Konrad and Jan,

Quoted 4 lines

There was a bug in Xen pcibackend that I thought I upstreamed which could be releated. It was not restoring the right registers to the PCI-device.

They are attached.

Thanks for the patches, however I have been finding some very interesting things.

I decided to keep Xen out of the equation and booted into plain vanilla Linux. I then ran the FLR and got the same problem in the PCI configuration, all BARs were set to 0 and all reads to the PCI memory returned ff. I then manually wrote the correct values into the PCI BAR registers, and hey presto, reads to the PCI memory started working again!

Looks like my workaround for moment will be to store the PCI configuration registers before and resetting them after. Not cool, but something really strange is going on...

Thanks for your help.

Regards.

Signature · 2 lines
Best regards,
 Simon                            mailto:[email protected]
#15 lore raw
Quoted 18 lines · Simon Martin

Hello Konrad and Jan,

Quoted 4 lines

There was a bug in Xen pcibackend that I thought I upstreamed which could be releated. It was not restoring the right registers to the PCI-device.

They are attached.

Thanks for the patches, however I have been finding some very interesting things.

I decided to keep Xen out of the equation and booted into plain vanilla Linux. I then ran the FLR and got the same problem in the PCI configuration, all BARs were set to 0 and all reads to the PCI memory returned ff. I then manually wrote the correct values into the PCI BAR registers, and hey presto, reads to the PCI memory started working again!

Quoted 3 lines

Looks like my workaround for moment will be to store the PCI configuration registers before and resetting them after. Not cool, but something really strange is going on...

Right. Which is what the pciback does now (minus the bugs that the two patches fix).

Look for pci_restore_config and pci_save_config

Quoted 10 lines

Thanks for your help.

Regards.

Signature · 2 lines
Best regards,
 Simon                            mailto:[email protected]
#16 lore raw
In reply to Simon Martin
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 19.11.14 at 16:12, <[email protected]> wrote:

This is getting more interesting. It seems that something is
overwriting the pci-back configuration data.

Starting from a fresh reboot I checked the Dom0 pci configuration and got this:

root@smartin-xen:~# lspci -s 00:19.0 -x
00:19.0 Ethernet controller: Intel Corporation Device 1559 (rev 04)
00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00
10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20
30: 00 00 00 00 c8 00 00 00 00 00 00 00 05 01 00 00

I then start/stop my DomU and checked the Dom0 pci configuration again and got this:

Quoted 6 lines
root@smartin-xen:~# lspci -s 00:19.0 -x
00:19.0 Ethernet controller: Intel Corporation Device 1559 (rev 04)
00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00
10: 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20
30: 00 00 00 00 c8 00 00 00 00 00 00 00 05 01 00 00

I.e. the BARs got zapped. Not something that should happen. And certainly explaining why you would not be able to use the device a second time.

Quoted 13 lines

Inside my DomU I added code to print the PCI configuration registers and what I get after restarting the DomU is:

(d18) 14:57:04.042 src/e1000e.c@00150: 00: 86 80 59 15 00 00 10 00 04 00 00 02 00 00 00 00 (d18) 14:57:04.042 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 14:57:04.042 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 14:57:04.043 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00 (d18) 14:57:04.043 src/e1000e.c@00324: Enable PCI Memory Access
(d18) 14:57:05.043 src/e1000e.c@00150: 00: 86 80 59 15 03 00 10 00 04 00 00 02 00 00 00 00 (d18) 14:57:05.044 src/e1000e.c@00150: 10: 00 00 d0 f7 00 c0 d3 f7 81 f0 00 00 00 00 00 00 (d18) 14:57:05.044 src/e1000e.c@00150: 20: 00 00 00 00 00 00 00 00 00 00 00 00 86 80 54 20 (d18) 14:57:05.045 src/e1000e.c@00150: 30: 00 00 00 00 c8 00 00 00 00 00 00 00 14 01 00 00

Quoted 2 lines

As you can see the pci configuration read from the pci-back driver by my DomU is different to the data in the Dom0 pci configuration!

The only byte that is different afaics is the one at 0x3c, and that's fine. Plus comparing with what Dom0 sees before the guest was started or after the guest was stopped isn't really meaningful - instead you'd want to compare against what Dom0 sees while the guest is running.

But in the end your problem may have to do with the various warnings issued regarding unrecognized DMAR table data, all relating to the use of namespaces that our code doesn't have support for.

Jan