PCI devices passthrough on Arm, part 3
v5 cover PCI devices passthrough on Arm, part 3
From: Oleksandr Andrushchenko <[email protected]>
Hi, all!
1. This patch series is focusing on vPCI and adds support for non-identity PCI BAR mappings which is required while passing through a PCI device to a guest. The highlights are:
- Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
- Emulate guest BAR register values based on physical BAR values. This allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
- Instead of handling a single range set, that contains all the memory regions of all the BARs and ROM, have them per BAR.
- Take into account guest's BAR view and program its p2m accordingly:
gfn is guest's view of the BAR and mfn is the physical BAR value as set up by the host bridge in the hardware domain.
This way hardware doamin sees physical BAR values and guest sees emulated ones.
2. The series also adds support for virtual PCI bus topology for guests:
- We emulate a single host bridge for the guest, so segment is always 0.
- The implementation is limited to 32 devices which are allowed on a single PCI bus.
- The virtual bus number is set to 0, so virtual devices are seen as embedded endpoints behind the root complex.
3. The series has complete re-work of the locking scheme used/absent before with the help of the work started by Roger [1]:
[PATCH v5 03/13] vpci: move lock outside of struct vpci
This way the lock can be used to check whether vpci is present, and removal can be performed while holding the lock, in order to make sure there are no accesses to the contents of the vpci struct. Previously removal could race with vpci_read for example, since the lock was dropped prior to freeing pdev->vpci.
This also solves synchronization issues between all vPCI code entities which could run in parallel.
4. There is an outstanding TODO left unimplemented by this series:
for unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.
Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
5. The series was also tested on:
- x86 PVH Dom0 and doesn't break it.
- x86 HVM with PCI passthrough to DomU and doesn't break it.
Thank you,
Oleksandr
[1] https://lore.kernel.org/xen-devel/[email protected]/
Oleksandr Andrushchenko (13):
rangeset: add RANGESETF_no_print flag
vpci: fix function attributes for vpci_process_pending
vpci: cancel pending map/unmap on vpci removal
vpci: add hooks for PCI device assign/de-assign vpci/header: implement guest BAR register handlers vpci/header: handle p2m range sets per BAR vpci/header: program p2m with guest BAR view vpci/header: emulate PCI_COMMAND register for guests vpci/header: reset the command register when adding devices
vpci: add initial support for virtual PCI bus topology xen/arm: translate virtual PCI bus topology for guests xen/arm: account IO handlers for emulated PCI MSI-X
vpci: add TODO for the registers not explicitly handled
Roger Pau Monne (1):
vpci: move lock outside of struct vpci
tools/tests/vpci/emul.h | 5 +- tools/tests/vpci/main.c | 4 +- xen/arch/arm/vpci.c | 33 +++- xen/arch/x86/hvm/vmsi.c | 8 +- xen/common/rangeset.c | 5 +- xen/drivers/Kconfig | 4 + xen/drivers/passthrough/pci.c | 11 ++ xen/drivers/vpci/header.c | 352 +++++++++++++++++++++++++++------- xen/drivers/vpci/msi.c | 11 +- xen/drivers/vpci/msix.c | 8 +- xen/drivers/vpci/vpci.c | 252 +++++++++++++++++++++--- xen/include/xen/pci.h | 6 + xen/include/xen/rangeset.h | 7 +- xen/include/xen/sched.h | 8 + xen/include/xen/vpci.h | 47 ++++- 15 files changed, 644 insertions(+), 117 deletions(-)
Signature · 1 line
2.25.1
PATCH v5 1/14 rangeset: add RANGESETF_no_print flag
From: Oleksandr Andrushchenko <[email protected]>
There are range sets which should not be printed, so introduce a flag which allows marking those as such. Implement relevant logic to skip such entries while printing.
While at it also simplify the definition of the flags by directly defining those without helpers.
---
Since v1:
- update BUG_ON with new flag
- simplify the definition of the flags
--- xen/common/rangeset.c | 5 ++++- xen/include/xen/rangeset.h | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-)
xen/common/rangeset.c +4 -1
@@ -433,7 +433,7 @@ struct rangeset *rangeset_new(INIT_LIST_HEAD (&r->range_list); r->nr_ranges =-1 ; -BUG_ON (flags & ~RANGESETF_prettyprint_hex); +BUG_ON (flags & ~(RANGESETF_prettyprint_hex | RANGESETF_no_print)); r->flags = flags;safe_strcpy (r->name, name ?:"(no name)" );
@@ -575,6 +575,9 @@ void rangeset_domain_printk( list_for_each_entry ( r, &d->rangesets, rangeset_list ) { +if ( r->flags & RANGESETF_no_print ) +continue ; +printk (" " );rangeset_printk (r);printk ("\n" );
xen/include/xen/rangeset.h +4 -3
@@ -48,9 +48,10 @@ void rangeset_limit( struct rangeset *r, unsigned int limit);/* Flags for passing to rangeset_new(). */ -/* Pretty-print range limits in hexadecimal. */ -#define _RANGESETF_prettyprint_hex 0 -#define RANGESETF_prettyprint_hex (1U << _RANGESETF_prettyprint_hex) +/* Pretty-print range limits in hexadecimal. */ +#define RANGESETF_prettyprint_hex (1U << 0) +/* Do not print entries marked with this flag. */ +#define RANGESETF_no_print (1U << 1)bool_t __must_check rangeset_is_empty (const struct rangeset *r);
Signature · 1 line
2.25.1
Re: [PATCH v5 01/14] rangeset: add RANGESETF_no_print flag
Quoted 11 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
There are range sets which should not be printed, so introduce a flag which allows marking those as such. Implement relevant logic to skip such entries while printing.
While at it also simplify the definition of the flags by directly defining those without helpers.
Suggested-by Jan Beulich <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>
albeit with a remark:
xen/include/xen/rangeset.h void rangeset_limit( · line 51
Quoted 10 lines
xen/include/xen/rangeset.h +1
-3@@ -48,9 +48,10 @@ void rangeset_limit( struct rangeset *r, unsigned int limit);/* Flags for passing to rangeset_new(). */ -/* Pretty-print range limits in hexadecimal. */ -#define _RANGESETF_prettyprint_hex 0 -#define RANGESETF_prettyprint_hex (1U << _RANGESETF_prettyprint_hex) +/* Pretty-print range limits in hexadecimal. */
I would guess this comment was intentionally indented by a blank, to visually separate it from the comment covering all flags. I'd prefer if that was kept and if the new comment you add followed suit.
Jan
Re: [PATCH v5 01/14] rangeset: add RANGESETF_no_print flag
Quoted 20 lines · Jan Beulich
Quoted 11 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
There are range sets which should not be printed, so introduce a flag which allows marking those as such. Implement relevant logic to skip such entries while printing.
While at it also simplify the definition of the flags by directly defining those without helpers.
Suggested-by Jan Beulich <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>Reviewed-by Jan Beulich <[email protected]>albeit with a remark:
Quoted 5 lines
xen/include/xen/rangeset.h +0
-0@@ -48,9 +48,10 @@ void rangeset_limit( struct rangeset *r, unsigned int limit);
Quoted 9 lines
xen/include/xen/rangeset.h void rangeset_limit( · line 51
Quoted 5 lines
xen/include/xen/rangeset.h +1
-3/* Flags for passing to rangeset_new(). */ -/* Pretty-print range limits in hexadecimal. */ -#define _RANGESETF_prettyprint_hex 0 -#define RANGESETF_prettyprint_hex (1U << _RANGESETF_prettyprint_hex) +/* Pretty-print range limits in hexadecimal. */ I would guess this comment was intentionally indented by a blank, to visually separate it from the comment covering all flags. I'd prefer if that was kept and if the new comment you add followed suit.
Ah, ok, so I will add a space for the new flag's comment as well then
Quoted 2 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 01/14] rangeset: add RANGESETF_no_print flag
In reply to Oleksandr AndrushchenkoHi Oleksandr,
Sorry for jumping in amid v5, but...
Quoted 49 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
There are range sets which should not be printed, so introduce a flag which allows marking those as such. Implement relevant logic to skip such entries while printing.
While at it also simplify the definition of the flags by directly defining those without helpers.
Suggested-by Jan Beulich <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v1:
- updateBUG_ONwith new flag
- simplify the definition of the flags--- xen/common/rangeset.c | 5 ++++- xen/include/xen/rangeset.h | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-)xen/common/rangeset.c +4
-1@@ -433,7 +433,7 @@ struct rangeset *rangeset_new(INIT_LIST_HEAD (&r->range_list); r->nr_ranges =-1 ; -BUG_ON (flags & ~RANGESETF_prettyprint_hex); +BUG_ON (flags & ~(RANGESETF_prettyprint_hex | RANGESETF_no_print)); r->flags = flags;safe_strcpy (r->name, name ?:"(no name)" );@@ -575,6 +575,9 @@ void rangeset_domain_printk( list_for_each_entry ( r, &d->rangesets, rangeset_list ) { +if ( r->flags & RANGESETF_no_print ) +continue ; +printk (" " );rangeset_printk (r);printk ("\n" );xen/include/xen/rangeset.h +0
-0
xen/include/xen/rangeset.h void rangeset_limit( · line 52
Quoted 8 lines
xen/include/xen/rangeset.h +2
-3@@ -48,9 +48,10 @@ void rangeset_limit( struct rangeset *r, unsigned int limit);/* Flags for passing to rangeset_new(). */ -/* Pretty-print range limits in hexadecimal. */ -#define _RANGESETF_prettyprint_hex 0 -#define RANGESETF_prettyprint_hex (1U << _RANGESETF_prettyprint_hex) +/* Pretty-print range limits in hexadecimal. */ +#define RANGESETF_prettyprint_hex (1U << 0)
If you already touching all the flags, why not to use BIT()?
Quoted 5 lines
xen/include/xen/rangeset.h +2
-0+/* Do not print entries marked with this flag. */ +#define RANGESETF_no_print (1U << 1)bool_t __must_check rangeset_is_empty (const struct rangeset *r);
Signature · 1 line
Volodymyr Babchuk at EPAM
Re: [PATCH v5 01/14] rangeset: add RANGESETF_no_print flag
Hi, Volodymyr!
Quoted 57 lines · Volodymyr Babchuk
Hi Oleksandr,
Sorry for jumping in amid v5, but...
Quoted 51 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
There are range sets which should not be printed, so introduce a flag which allows marking those as such. Implement relevant logic to skip such entries while printing.
While at it also simplify the definition of the flags by directly defining those without helpers.
Suggested-by Jan Beulich <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v1:
- updateBUG_ONwith new flag
- simplify the definition of the flags--- xen/common/rangeset.c | 5 ++++- xen/include/xen/rangeset.h | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-)xen/common/rangeset.c +4
-1@@ -433,7 +433,7 @@ struct rangeset *rangeset_new(INIT_LIST_HEAD (&r->range_list); r->nr_ranges =-1 ; -BUG_ON (flags & ~RANGESETF_prettyprint_hex); +BUG_ON (flags & ~(RANGESETF_prettyprint_hex | RANGESETF_no_print)); r->flags = flags;safe_strcpy (r->name, name ?:"(no name)" );@@ -575,6 +575,9 @@ void rangeset_domain_printk( list_for_each_entry ( r, &d->rangesets, rangeset_list ) { +if ( r->flags & RANGESETF_no_print ) +continue ; +printk (" " );rangeset_printk (r);printk ("\n" );xen/include/xen/rangeset.h +0
-0@@ -48,9 +48,10 @@ void rangeset_limit( struct rangeset *r, unsigned int limit);
Quoted 7 lines
xen/include/xen/rangeset.h void rangeset_limit( · line 52
Quoted 6 lines
xen/include/xen/rangeset.h +2
-3/* Flags for passing to rangeset_new(). */ -/* Pretty-print range limits in hexadecimal. */ -#define _RANGESETF_prettyprint_hex 0 -#define RANGESETF_prettyprint_hex (1U << _RANGESETF_prettyprint_hex) +/* Pretty-print range limits in hexadecimal. */ +#define RANGESETF_prettyprint_hex (1U << 0)If you already touching all the flags, why not to use
BIT()?
It was discussed previously [1] and we decided not to use the BIT macro
Thank you,
Oleksandr
Quoted 7 lines
xen/include/xen/rangeset.h void rangeset_limit( · line 57
Quoted 6 lines
xen/include/xen/rangeset.h +2
-0+/* Do not print entries marked with this flag. */ +#define RANGESETF_no_print (1U << 1)bool_t __must_check rangeset_is_empty (const struct rangeset *r);
[1] https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/
PATCH v5 2/14 vpci: fix function attributes for vpci_process_pending
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
vpci_process_pending is defined with different attributes, e.g. with __must_check if CONFIG_HAS_VPCI enabled and not otherwise. Fix this by defining both of the definitions with __must_check.
---
Cc: Roger Pau Monné <[email protected]>
New in v4
--- xen/include/xen/vpci.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/xen/vpci.h +1 -1
@@ -247,7 +247,7 @@ static inline void vpci_write(pci_sbdf_t sbdf, unsigned int reg,ASSERT_UNREACHABLE (); } -static inline bool vpci_process_pending (struct vcpu *v) +static inline bool __must_check vpci_process_pending (struct vcpu *v) {ASSERT_UNREACHABLE ();return false;
Signature · 1 line
2.25.1
Re: [PATCH v5 02/14] vpci: fix function attributes for vpci_process_pending
Hi Oleksandr,
Quoted 9 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
vpci_process_pendingis defined with different attributes, e.g. with__must_checkifCONFIG_HAS_VPCIenabled and not otherwise. Fix this by defining both of the definitions with__must_check.Fixes 14583a590783 ("7fbb096bf345 kconfig: don't select VPCI if building a shim-only binary")Signed-off-by Oleksandr Andrushchenko <[email protected]>
Cheers,
Signature · 1 line
Julien Grall
Roger Pau Monné
Reviewed-byRe: [PATCH v5 02/14] vpci: fix function attributes for vpci_process_pending
Quoted 9 lines · Julien Grall
Hi Oleksandr,
Quoted 6 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
vpci_process_pendingis defined with different attributes, e.g. with__must_checkifCONFIG_HAS_VPCIenabled and not otherwise. Fix this by defining both of the definitions with__must_check.
Quoted 5 lines
Quoted 3 lines
Fixes 14583a590783 ("7fbb096bf345 kconfig: don't select VPCI if building a shim-only binary")Signed-off-by Oleksandr Andrushchenko <[email protected]>Reviewed-by Julien Grall <[email protected]>
I think this can be committed independently of the rest of the series?
Thanks, Roger.
Re: [PATCH v5 02/14] vpci: fix function attributes for vpci_process_pending
Hi, Roger!
Quoted 10 lines · Roger Pau Monné
Quoted 9 lines · Julien Grall
Hi Oleksandr,
Quoted 6 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
vpci_process_pendingis defined with different attributes, e.g. with__must_checkifCONFIG_HAS_VPCIenabled and not otherwise. Fix this by defining both of the definitions with__must_check.
Quoted 8 lines
Quoted 4 lines
Quoted 3 lines
Fixes 14583a590783 ("7fbb096bf345 kconfig: don't select VPCI if building a shim-only binary")Signed-off-by Oleksandr Andrushchenko <[email protected]>Reviewed-by Julien Grall <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>I think this can be committed independently of the rest of the series?
I think so
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 02/14] vpci: fix function attributes for vpci_process_pending
Hi, Roger!
Quoted 15 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 12 lines · Roger Pau Monné
Quoted 11 lines · Julien Grall
Hi Oleksandr,
Quoted 8 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
vpci_process_pendingis defined with different attributes, e.g. with__must_checkifCONFIG_HAS_VPCIenabled and not otherwise. Fix this by defining both of the definitions with__must_check.Fixes 14583a590783 ("7fbb096bf345 kconfig: don't select VPCI if building a shim-only binary")
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
Quoted 1 line
Signed-off-by Oleksandr Andrushchenko <[email protected]>Reviewed-by Julien Grall <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>I think this can be committed independently of the rest of the series?
I think so
Could you please commit this one, so I don't have to keep it in the v6 of the series?
Thank you in advance,
Oleksandr
Quoted 3 lines
Quoted 1 line
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 02/14] vpci: fix function attributes for vpci_process_pending
Quoted 6 lines · Oleksandr Andrushchenko
Quoted 4 lines · Oleksandr Andrushchenko
Quoted 2 lines · Roger Pau Monné
I think this can be committed independently of the rest of the series?
I think so
Could you please commit this one, so I don't have to keep it in the v6 of the series?
Did you actually check before asking? See commit 7dc0233f534f from Dec 14th.
Jan
PATCH v5 3/14 vpci: move lock outside of struct vpci
In reply to Oleksandr AndrushchenkoFrom: Roger Pau Monne <[email protected]>
This way the lock can be used to check whether vpci is present, and removal can be performed while holding the lock, in order to make sure there are no accesses to the contents of the vpci struct. Previously removal could race with vpci_read for example, since the lock was dropped prior to freeing pdev->vpci.
---
Cc: Andrew Cooper <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
---
New in v5 of this series: this is an updated version of the patch published at
https://lore.kernel.org/xen-devel/[email protected]/
Changes since v2:
- fixed pdev->vpci = xzalloc(struct vpci); under spin_lock (Jan)
Changes since v1:
- Assert that vpci_lock is locked in vpci_remove_device_locked.
- Remove double newline.
- Shrink critical section in vpci_{read/write}.
--- tools/tests/vpci/emul.h | 5 ++- tools/tests/vpci/main.c | 4 +-- xen/arch/x86/hvm/vmsi.c | 8 ++--- xen/drivers/passthrough/pci.c | 1 + xen/drivers/vpci/header.c | 21 +++++++---- xen/drivers/vpci/msi.c | 11 ++++-- xen/drivers/vpci/msix.c | 8 ++--- xen/drivers/vpci/vpci.c | 68 +++++++++++++++++++++++------------ xen/include/xen/pci.h | 1 + xen/include/xen/vpci.h | 5 +-- 10 files changed, 85 insertions(+), 47 deletions(-)
tools/tests/vpci/emul.h +2 -3
@@ -44,6 +44,7 @@ struct domain { };struct pci_dev { +bool vpci_lock;struct vpci *vpci; };
@@ -53,10 +54,8 @@ struct vcpu };extern const struct vcpu *current; -extern const struct pci_dev test_pdev; +extern struct pci_dev test_pdev; -typedef bool spinlock_t ; -#define spin_lock_init (l) (*(l) = false)#define spin_lock (l) (*(l) = true)#define spin_unlock (l) (*(l) = false)
tools/tests/vpci/main.c +2 -2
@@ -23,7 +23,8 @@ static struct vpci vpci;const static struct domain d; -const struct pci_dev test_pdev = { +struct pci_dev test_pdev = { + .vpci_lock = false, .vpci = &vpci, };
@@ -158,7 +159,6 @@ main(int argc, char **argv)int rc;INIT_LIST_HEAD (&vpci.handlers); -spin_lock_init (&vpci.lock);VPCI_ADD_REG (vpci_read32, vpci_write32,0 ,4 , r0);VPCI_READ_CHECK (0 ,4 , r0);
xen/arch/x86/hvm/vmsi.c +4 -4
@@ -910,14 +910,14 @@ int vpci_msix_arch_print(const struct vpci_msix *msix) {struct pci_dev *pdev = msix->pdev; -spin_unlock (&msix->pdev->vpci->lock); +spin_unlock (&msix->pdev->vpci_lock);process_pending_softirqs ();/* NB: we assume that pdev cannot go away for an alive domain. */ -if ( !pdev->vpci || !spin_trylock (&pdev->vpci->lock) ) +if ( !spin_trylock (&pdev->vpci_lock) )return -EBUSY ; -if ( pdev->vpci->msix != msix ) +if ( !pdev->vpci || pdev->vpci->msix != msix ) { -spin_unlock (&pdev->vpci->lock); +spin_unlock (&pdev->vpci_lock);return -EAGAIN ; } }
xen/drivers/passthrough/pci.c +1 -0
@@ -328,6 +328,7 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn) *((u8 *) &pdev->bus) = bus; *((u8 *) &pdev->devfn) = devfn; pdev->domain =NULL ; +spin_lock_init (&pdev->vpci_lock);arch_pci_init_pdev (pdev);
xen/drivers/vpci/header.c +15 -6
@@ -142,12 +142,13 @@ bool vpci_process_pending(struct vcpu *v)if ( rc == -ERESTART )return true; -spin_lock (&v->vpci.pdev->vpci->lock); -/* Disable memory decoding unconditionally on failure. */ -modify_decoding (v->vpci.pdev, - rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, - !rc && v->vpci.rom_only); -spin_unlock (&v->vpci.pdev->vpci->lock); +spin_lock (&v->vpci.pdev->vpci_lock); +if ( v->vpci.pdev->vpci ) +/* Disable memory decoding unconditionally on failure. */ +modify_decoding (v->vpci.pdev, + rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, + !rc && v->vpci.rom_only); +spin_unlock (&v->vpci.pdev->vpci_lock);rangeset_destroy (v->vpci.mem); v->vpci.mem =NULL ;
@@ -285,6 +286,12 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)continue ; } +spin_lock (&tmp->vpci_lock); +if ( !tmp->vpci ) + { +spin_unlock (&tmp->vpci_lock); +continue ; + }for ( i =0 ; i <ARRAY_SIZE (tmp->vpci->header.bars); i++ ) {const struct vpci_bar *bar = &tmp->vpci->header.bars[i];
@@ -303,12 +310,14 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) rc =rangeset_remove_range (mem, start, end);if ( rc ) { +spin_unlock (&tmp->vpci_lock);printk (XENLOG_G_WARNING "Failed to remove [%lx, %lx]: %d\n" , start, end, rc);rangeset_destroy (mem);return rc; } } +spin_unlock (&tmp->vpci_lock); }ASSERT (dev);
xen/drivers/vpci/msi.c +8 -3
@@ -270,7 +270,7 @@ void vpci_dump_msi(void)rcu_read_lock (&domlist_read_lock);for_each_domain ( d ) { -const struct pci_dev *pdev; +struct pci_dev *pdev;if ( !has_vpci (d) )continue ;
@@ -282,8 +282,13 @@ void vpci_dump_msi(void)const struct vpci_msi *msi;const struct vpci_msix *msix; -if ( !pdev->vpci || !spin_trylock (&pdev->vpci->lock) ) +if ( !spin_trylock (&pdev->vpci_lock) )continue ; +if ( !pdev->vpci ) + { +spin_unlock (&pdev->vpci_lock); +continue ; + } msi = pdev->vpci->msi;if ( msi && msi->enabled )
@@ -323,7 +328,7 @@ void vpci_dump_msi(void) } } -spin_unlock (&pdev->vpci->lock); +spin_unlock (&pdev->vpci_lock);process_pending_softirqs (); } }
xen/drivers/vpci/msix.c +4 -4
@@ -225,7 +225,7 @@ static int msix_read(struct vcpu *v, unsigned long addr, unsigned int len,return X86EMUL_OKAY ; } -spin_lock (&msix->pdev->vpci->lock); +spin_lock (&msix->pdev->vpci_lock); entry =get_entry (msix, addr); offset = addr & (PCI_MSIX_ENTRY_SIZE -1 );
@@ -254,7 +254,7 @@ static int msix_read(struct vcpu *v, unsigned long addr, unsigned int len,ASSERT_UNREACHABLE ();break ; } -spin_unlock (&msix->pdev->vpci->lock); +spin_unlock (&msix->pdev->vpci_lock);return X86EMUL_OKAY ; }
@@ -297,7 +297,7 @@ static int msix_write(struct vcpu *v, unsigned long addr, unsigned int len,return X86EMUL_OKAY ; } -spin_lock (&msix->pdev->vpci->lock); +spin_lock (&msix->pdev->vpci_lock); entry =get_entry (msix, addr); offset = addr & (PCI_MSIX_ENTRY_SIZE -1 );
@@ -370,7 +370,7 @@ static int msix_write(struct vcpu *v, unsigned long addr, unsigned int len,ASSERT_UNREACHABLE ();break ; } -spin_unlock (&msix->pdev->vpci->lock); +spin_unlock (&msix->pdev->vpci_lock);return X86EMUL_OKAY ; }
xen/drivers/vpci/vpci.c +45 -23
188 lines not on this page · raw message
xen/include/xen/pci.h +1 -0
@@ -134,6 +134,7 @@ struct pci_dev {u64 vf_rlen[6 ];/* Data for vPCI. */ +spinlock_t vpci_lock;struct vpci *vpci; };
xen/include/xen/vpci.h +3 -2
@@ -30,8 +30,9 @@ int __must_check vpci_add_handlers(struct pci_dev *dev);/* Remove all handlers and free vpci related structures. */ void vpci_remove_device (struct pci_dev *pdev); +void vpci_remove_device_locked (struct pci_dev *pdev); -/* Add/remove a register handler. */ +/* Add/remove a register handler. Must be called holding the vpci_lock. */ int __must_check vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler, vpci_write_t *write_handler,
@@ -60,7 +61,6 @@ bool __must_check vpci_process_pending(struct vcpu *v);struct vpci {/* List of vPCI handlers for a device. */ struct list_head handlers; -spinlock_t lock;#ifdef __XEN__/* Hide the rest of the vpci struct from the user-space test harness. */
@@ -231,6 +231,7 @@ static inline int vpci_add_handlers(struct pci_dev *pdev) }static inline void vpci_remove_device (struct pci_dev *pdev) { } +static inline void vpci_remove_device_locked (struct pci_dev *pdev) { }static inline void vpci_dump_msi (void ) { }
Signature · 1 line
2.25.1
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Quoted 20 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +2
-4@@ -35,12 +35,10 @@ extern vpci_register_init_t *const __start_vpci_array[];extern vpci_register_init_t *const __end_vpci_array[];#define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array) -void vpci_remove_device (struct pci_dev *pdev) +static void vpci_remove_device_handlers_locked (struct pci_dev *pdev) { -if ( !has_vpci (pdev->domain) ) -return ; +ASSERT (spin_is_locked (&pdev->vpci_lock)); -spin_lock (&pdev->vpci->lock);while ( !list_empty (&pdev->vpci->handlers) ) { struct vpci_register *r = list_first_entry(&pdev->vpci->handlers,
xen/drivers/vpci/vpci.c void vpci_remove_device(struct pci_dev *pdev) · line 53
Quoted 7 lines
xen/drivers/vpci/vpci.c +3
-1@@ -50,15 +48,33 @@ void vpci_remove_device(struct pci_dev *pdev)list_del (&r->node);xfree (r); } -spin_unlock (&pdev->vpci->lock); +} + +void vpci_remove_device_locked (struct pci_dev *pdev)
I think this could be static instead, as it's only used by vpci_remove_device and vpci_add_handlers which are local to the file.
Thanks, Roger.
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +2
-3@@ -35,12 +35,10 @@ extern vpci_register_init_t *const __start_vpci_array[];extern vpci_register_init_t *const __end_vpci_array[];#define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array) -void vpci_remove_device (struct pci_dev *pdev) +static void vpci_remove_device_handlers_locked (struct pci_dev *pdev) { -if ( !has_vpci (pdev->domain) ) -return ; +ASSERT (spin_is_locked (&pdev->vpci_lock));
Quoted 16 lines
xen/drivers/vpci/vpci.c void vpci_remove_device(struct pci_dev *pdev) · line 53
Quoted 12 lines
xen/drivers/vpci/vpci.c +3
-2-spin_lock (&pdev->vpci->lock);while ( !list_empty (&pdev->vpci->handlers) ) { struct vpci_register *r = list_first_entry(&pdev->vpci->handlers,@@ -50,15 +48,33 @@ void vpci_remove_device(struct pci_dev *pdev)list_del (&r->node);xfree (r); } -spin_unlock (&pdev->vpci->lock); +} + +void vpci_remove_device_locked (struct pci_dev *pdev)I think this could be static instead, as it's only used by
vpci_remove_deviceandvpci_add_handlerswhich are local to the file.
Does the splitting out of vpci_remove_device_handlers_locked() belong in this patch in the first place? There's no second caller being added, so this looks to be an orthogonal adjustment.
Jan
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Hello, Roger, Jan!
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +2
-3@@ -35,12 +35,10 @@ extern vpci_register_init_t *const __start_vpci_array[];extern vpci_register_init_t *const __end_vpci_array[];#define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array) -void vpci_remove_device (struct pci_dev *pdev) +static void vpci_remove_device_handlers_locked (struct pci_dev *pdev) { -if ( !has_vpci (pdev->domain) ) -return ; +ASSERT (spin_is_locked (&pdev->vpci_lock));
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
xen/drivers/vpci/vpci.c +0
-1-spin_lock (&pdev->vpci->lock);while ( !list_empty (&pdev->vpci->handlers) ) { struct vpci_register *r = list_first_entry(&pdev->vpci->handlers,@@ -50,15 +48,33 @@ void vpci_remove_device(struct pci_dev *pdev)list_del (&r->node);xfree (r);Quoted 8 lines
xen/drivers/vpci/vpci.c void vpci_remove_device(struct pci_dev *pdev) · line 53
Quoted 5 lines
xen/drivers/vpci/vpci.c +3
-1} -spin_unlock (&pdev->vpci->lock); +} + +void vpci_remove_device_locked (struct pci_dev *pdev)I think this could be static instead, as it's only used by
vpci_remove_deviceandvpci_add_handlerswhich are local to the file.
This is going to be used outside later on while processing pending mappings,
so I think it is not worth it defining it static here and then removing the static key word later on: please see [PATCH v5 04/14] vpci: cancel pending map/unmap on vpci removal [1]
Quoted 3 lines
Does the splitting out of
vpci_remove_device_handlers_locked()belong in this patch in the first place? There's no second caller being added, so this looks to be an orthogonal adjustment.
I think of it as a preparation for the upcoming code: although the reason for the change might not be immediately seen in this patch it is still in line with what happens next.
So, I would prefer to keep the change as is: anyways the whole series should probably be committed as a single piece of work, so it won't look inconsistent then
Thank you,
Oleksandr
Quoted 3 lines
Jan
[1] https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Quoted 35 lines · Oleksandr Andrushchenko
Hello, Roger, Jan!
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +2
-3@@ -35,12 +35,10 @@ extern vpci_register_init_t *const __start_vpci_array[];extern vpci_register_init_t *const __end_vpci_array[];#define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array) -void vpci_remove_device (struct pci_dev *pdev) +static void vpci_remove_device_handlers_locked (struct pci_dev *pdev) { -if ( !has_vpci (pdev->domain) ) -return ; +ASSERT (spin_is_locked (&pdev->vpci_lock));Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
xen/drivers/vpci/vpci.c +0
-1-spin_lock (&pdev->vpci->lock);while ( !list_empty (&pdev->vpci->handlers) ) { struct vpci_register *r = list_first_entry(&pdev->vpci->handlers,@@ -50,15 +48,33 @@ void vpci_remove_device(struct pci_dev *pdev)list_del (&r->node);xfree (r);Quoted 8 lines
xen/drivers/vpci/vpci.c void vpci_remove_device(struct pci_dev *pdev) · line 53
Quoted 5 lines
xen/drivers/vpci/vpci.c +3
-1} -spin_unlock (&pdev->vpci->lock); +} + +void vpci_remove_device_locked (struct pci_dev *pdev)I think this could be static instead, as it's only used by
vpci_remove_deviceandvpci_add_handlerswhich are local to the file.
Quoted 3 lines
This is going to be used outside later on while processing pending mappings,
so I think it is not worth it defining it static here and then removing the static key word later on: please see [PATCH v5 04/14] vpci: cancel pending map/unmap on vpci removal [1]
I have some comments there also, which might change the approach you are using.
Quoted 6 lines
Quoted 3 lines
Does the splitting out of
vpci_remove_device_handlers_locked()belong in this patch in the first place? There's no second caller being added, so this looks to be an orthogonal adjustment.I think of it as a preparation for the upcoming code: although the reason for the change might not be immediately seen in this patch it is still in line with what happens next.
Right - it's generally best if the change is done together as the new callers are added. Otherwise it's hard to understand why certain changes are made, and you will likely get asked the same question on next rounds.
It's also possible that the code that requires this is changed in further iterations so there's no longer a need for the splitting.
Thanks, Roger.
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Hi, Jan, Roger!
Quoted 48 lines · Roger Pau Monné
Quoted 35 lines · Oleksandr Andrushchenko
Hello, Roger, Jan!
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +2
-3@@ -35,12 +35,10 @@ extern vpci_register_init_t *const __start_vpci_array[];extern vpci_register_init_t *const __end_vpci_array[];#define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array) -void vpci_remove_device (struct pci_dev *pdev) +static void vpci_remove_device_handlers_locked (struct pci_dev *pdev) { -if ( !has_vpci (pdev->domain) ) -return ; +ASSERT (spin_is_locked (&pdev->vpci_lock));Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
xen/drivers/vpci/vpci.c +0
-1-spin_lock (&pdev->vpci->lock);while ( !list_empty (&pdev->vpci->handlers) ) { struct vpci_register *r = list_first_entry(&pdev->vpci->handlers,@@ -50,15 +48,33 @@ void vpci_remove_device(struct pci_dev *pdev)list_del (&r->node);xfree (r);Quoted 8 lines
xen/drivers/vpci/vpci.c void vpci_remove_device(struct pci_dev *pdev) · line 53
Quoted 5 lines
xen/drivers/vpci/vpci.c +3
-1} -spin_unlock (&pdev->vpci->lock); +} + +void vpci_remove_device_locked (struct pci_dev *pdev)I think this could be static instead, as it's only used by
vpci_remove_deviceandvpci_add_handlerswhich are local to the file.Quoted 3 lines
This is going to be used outside later on while processing pending mappings,
so I think it is not worth it defining it static here and then removing the static key word later on: please see [PATCH v5 04/14] vpci: cancel pending map/unmap on vpci removal [1]I have some comments there also, which might change the approach you are using.
Quoted 6 lines
Quoted 3 lines
Does the splitting out of
vpci_remove_device_handlers_locked()belong in this patch in the first place? There's no second caller being added, so this looks to be an orthogonal adjustment.I think of it as a preparation for the upcoming code: although the reason for the change might not be immediately seen in this patch it is still in line with what happens next.
Quoted 7 lines
Right - it's generally best if the change is done together as the new callers are added. Otherwise it's hard to understand why certain changes are made, and you will likely get asked the same question on next rounds.
It's also possible that the code that requires this is changed in further iterations so there's no longer a need for the splitting.
Ok, sounds reasonable
I will not split the functions without the obvious need
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
In reply to Oleksandr AndrushchenkoQuoted 4 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +0
-0@@ -68,12 +84,13 @@ int vpci_add_handlers(struct pci_dev *pdev)/* We should not get here twice for the same device. */ ASSERT (!pdev->vpci);
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 76
Quoted 10 lines
xen/drivers/vpci/vpci.c +4
-3- pdev->vpci = xzalloc(struct vpci); -if ( !pdev->vpci ) + vpci = xzalloc(struct vpci); +if ( !vpci )return -ENOMEM ; +spin_lock (&pdev->vpci_lock); + pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); -spin_lock_init (&pdev->vpci->lock);
INIT_LIST_HEAD() can occur ahead of taking the lock, and can also act on &vpci->handlers rather than &pdev->vpci->handlers.
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 100
Quoted 4 lines
xen/drivers/vpci/vpci.c +0
-0for ( i =0 ; i <NUM_VPCI_INIT ; i++ ) {@@ -83,7 +100,8 @@ int vpci_add_handlers(struct pci_dev *pdev) }
This loop wants to live in the locked region because you need to install vpci into pdev->vpci up front, afaict. I wonder whether that couldn't be relaxed, but perhaps that's an improvement that can be thought about later.
The main reason I'm looking at this closely is because from the patch title I didn't expect new locking regions to be introduced right here; instead I did expect strictly a mechanical conversion.
xen/drivers/vpci/vpci.c int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler, · line 155
Quoted 5 lines
xen/drivers/vpci/vpci.c +0
-1@@ -152,8 +170,6 @@ int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler, r->offset = offset; r->private = data; -spin_lock (&vpci->lock);
From the description I can't deduce why this lock is fine to go away now, i.e. that all call sites have the lock now acquire earlier. Therefore I'd expect at least an assertion to be left here ...
xen/drivers/vpci/vpci.c int vpci_remove_register(struct vpci *vpci, unsigned int offset, · line 186
Quoted 5 lines
xen/drivers/vpci/vpci.c +0
-1@@ -183,7 +197,6 @@ int vpci_remove_register(struct vpci *vpci, unsigned int offset,const struct vpci_register r = { .offset = offset, .size = size };struct vpci_register *rm; -spin_lock (&vpci->lock);
... and here.
Quoted 9 lines
xen/drivers/vpci/vpci.c +1
-0@@ -370,6 +386,7 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size)break ;ASSERT (data_offset < size); } +spin_unlock (&pdev->vpci_lock);if ( data_offset < size ) {
xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 401
Quoted 7 lines
xen/drivers/vpci/vpci.c +0
-1@@ -379,7 +396,6 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) data =merge_result (data, tmp_data, size - data_offset, data_offset); } -spin_unlock (&pdev->vpci->lock);return data & (0xffffffff >> (32 -8 * size)); }
Here and ...
xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 507
Quoted 13 lines
xen/drivers/vpci/vpci.c +1
-2@@ -475,13 +498,12 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,break ;ASSERT (data_offset < size); } +spin_unlock (&pdev->vpci_lock);if ( data_offset < size )/* Tailing gap, write the remaining. */ vpci_write_hw (sbdf, reg + data_offset, size - data_offset, data >> (data_offset *8 )); - -spin_unlock (&pdev->vpci->lock); }
... even more so here I'm not sure of the correctness of the moving you do: While pdev->vpci indeed doesn't get accessed, I wonder whether there wasn't an intention to avoid racing calls to vpci_{read,write}_hw() this way. In any event I think such movement would need justification in the description.
Jan
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Quoted 18 lines · Jan Beulich
xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 401
Quoted 8 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +0
-1@@ -379,7 +396,6 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) data =merge_result (data, tmp_data, size - data_offset, data_offset); } -spin_unlock (&pdev->vpci->lock);return data & (0xffffffff >> (32 -8 * size)); }Here and ...
Quoted 6 lines
xen/drivers/vpci/vpci.c +1
-0@@ -475,13 +498,12 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,break ;ASSERT (data_offset < size); } +spin_unlock (&pdev->vpci_lock);
Quoted 13 lines
xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 507
Quoted 7 lines
xen/drivers/vpci/vpci.c +0
-2if ( data_offset < size )/* Tailing gap, write the remaining. */ vpci_write_hw (sbdf, reg + data_offset, size - data_offset, data >> (data_offset *8 )); - -spin_unlock (&pdev->vpci->lock); }... even more so here I'm not sure of the correctness of the moving you do: While
pdev->vpciindeed doesn't get accessed, I wonder whether there wasn't an intention to avoid racing calls to vpci_{read,write}_hw() this way. In any event I think such movement would need justification in the description.
I agree about the need for justification in the commit message, or even better this being split into a pre-patch, as it's not related to the lock switching done here.
I do think this is fine however, as racing calls to vpci_{read,write}_hw() shouldn't be a problem. Those are just wrappers around pci_conf_{read,write} functions, and the required locking (in case of using the IO ports) is already taken care in pci_conf_{read,write}.
Thanks, Roger.
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Quoted 37 lines · Roger Pau Monné
Quoted 18 lines · Jan Beulich
xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 401
Quoted 8 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +0
-1@@ -379,7 +396,6 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) data =merge_result (data, tmp_data, size - data_offset, data_offset); } -spin_unlock (&pdev->vpci->lock);return data & (0xffffffff >> (32 -8 * size)); }Here and ...
Quoted 6 lines
xen/drivers/vpci/vpci.c +1
-0@@ -475,13 +498,12 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,break ;ASSERT (data_offset < size); } +spin_unlock (&pdev->vpci_lock);Quoted 13 lines
xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 507
Quoted 7 lines
xen/drivers/vpci/vpci.c +0
-2if ( data_offset < size )/* Tailing gap, write the remaining. */ vpci_write_hw (sbdf, reg + data_offset, size - data_offset, data >> (data_offset *8 )); - -spin_unlock (&pdev->vpci->lock); }... even more so here I'm not sure of the correctness of the moving you do: While
pdev->vpciindeed doesn't get accessed, I wonder whether there wasn't an intention to avoid racing calls to vpci_{read,write}_hw() this way. In any event I think such movement would need justification in the description.I agree about the need for justification in the commit message, or even better this being split into a pre-patch, as it's not related to the lock switching done here.
Quoted 5 lines
I do think this is fine however, as racing calls to vpci_{read,write}_hw() shouldn't be a problem. Those are just wrappers around pci_conf_{read,write} functions, and the required locking (in case of using the IO ports) is already taken care in pci_conf_{read,write}.
IOW you consider it acceptable for a guest (really: Dom0) read racing a write to read back only part of what was written (so far)? I would think individual multi-byte reads and writes should appear atomic to the guest.
Jan
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Quoted 44 lines · Jan Beulich
Quoted 37 lines · Roger Pau Monné
Quoted 18 lines · Jan Beulich
xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 401
Quoted 8 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +0
-1@@ -379,7 +396,6 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) data =merge_result (data, tmp_data, size - data_offset, data_offset); } -spin_unlock (&pdev->vpci->lock);return data & (0xffffffff >> (32 -8 * size)); }Here and ...
Quoted 6 lines
xen/drivers/vpci/vpci.c +1
-0@@ -475,13 +498,12 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,break ;ASSERT (data_offset < size); } +spin_unlock (&pdev->vpci_lock);Quoted 13 lines
xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 507
Quoted 7 lines
xen/drivers/vpci/vpci.c +0
-2if ( data_offset < size )/* Tailing gap, write the remaining. */ vpci_write_hw (sbdf, reg + data_offset, size - data_offset, data >> (data_offset *8 )); - -spin_unlock (&pdev->vpci->lock); }... even more so here I'm not sure of the correctness of the moving you do: While
pdev->vpciindeed doesn't get accessed, I wonder whether there wasn't an intention to avoid racing calls to vpci_{read,write}_hw() this way. In any event I think such movement would need justification in the description.I agree about the need for justification in the commit message, or even better this being split into a pre-patch, as it's not related to the lock switching done here.
Quoted 5 lines
I do think this is fine however, as racing calls to vpci_{read,write}_hw() shouldn't be a problem. Those are just wrappers around pci_conf_{read,write} functions, and the required locking (in case of using the IO ports) is already taken care in pci_conf_{read,write}.
Quoted 4 lines
IOW you consider it acceptable for a guest (really: Dom0) read racing a write to read back only part of what was written (so far)? I would think individual multi-byte reads and writes should appear atomic to the guest.
We split 64bit writes into two 32bit ones without taking the lock for the whole duration of the access, so it's already possible to see a partially updated state as a result of a 64bit write.
I'm going over the PCI(e) spec but I don't seem to find anything about whether the ECAM is allowed to split memory transactions into multiple Configuration Requests, and whether those could then interleave with requests from a different CPU.
Thanks, Roger.
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Hi, Roger, Jan!
Quoted 45 lines · Roger Pau Monné
Quoted 40 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 401
Quoted 8 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +0
-1@@ -379,7 +396,6 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) data =merge_result (data, tmp_data, size - data_offset, data_offset); } -spin_unlock (&pdev->vpci->lock);return data & (0xffffffff >> (32 -8 * size)); }Here and ...
Quoted 6 lines
xen/drivers/vpci/vpci.c +1
-0@@ -475,13 +498,12 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,break ;ASSERT (data_offset < size); } +spin_unlock (&pdev->vpci_lock);Quoted 12 lines
xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 507
Quoted 7 lines
xen/drivers/vpci/vpci.c +0
-2if ( data_offset < size )/* Tailing gap, write the remaining. */ vpci_write_hw (sbdf, reg + data_offset, size - data_offset, data >> (data_offset *8 )); - -spin_unlock (&pdev->vpci->lock); }... even more so here I'm not sure of the correctness of the moving you do: While
pdev->vpciindeed doesn't get accessed, I wonder whether there wasn't an intention to avoid racing calls to vpci_{read,write}_hw() this way. In any event I think such movement would need justification in the description.I agree about the need for justification in the commit message, or even better this being split into a pre-patch, as it's not related to the lock switching done here.
Quoted 5 lines
I do think this is fine however, as racing calls to vpci_{read,write}_hw() shouldn't be a problem. Those are just wrappers around pci_conf_{read,write} functions, and the required locking (in case of using the IO ports) is already taken care in pci_conf_{read,write}.
Quoted 4 lines
IOW you consider it acceptable for a guest (really: Dom0) read racing a write to read back only part of what was written (so far)? I would think individual multi-byte reads and writes should appear atomic to the guest.
Quoted 8 lines
We split 64bit writes into two 32bit ones without taking the lock for the whole duration of the access, so it's already possible to see a partially updated state as a result of a 64bit write.
I'm going over the PCI(e) spec but I don't seem to find anything about whether the ECAM is allowed to split memory transactions into multiple Configuration Requests, and whether those could then interleave with requests from a different CPU.
So, with the above is it still fine for you to have the change as is or you want this optimization to go into a dedicated patch before this one?
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Quoted 52 lines · Oleksandr Andrushchenko
Hi, Roger, Jan!
Quoted 49 lines · Roger Pau Monné
Quoted 40 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 401
Quoted 8 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +0
-1@@ -379,7 +396,6 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) data =merge_result (data, tmp_data, size - data_offset, data_offset); } -spin_unlock (&pdev->vpci->lock);return data & (0xffffffff >> (32 -8 * size)); }Here and ...
Quoted 6 lines
xen/drivers/vpci/vpci.c +1
-0@@ -475,13 +498,12 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,break ;ASSERT (data_offset < size); } +spin_unlock (&pdev->vpci_lock);Quoted 12 lines
xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 507
Quoted 7 lines
xen/drivers/vpci/vpci.c +0
-2if ( data_offset < size )/* Tailing gap, write the remaining. */ vpci_write_hw (sbdf, reg + data_offset, size - data_offset, data >> (data_offset *8 )); - -spin_unlock (&pdev->vpci->lock); }... even more so here I'm not sure of the correctness of the moving you do: While
pdev->vpciindeed doesn't get accessed, I wonder whether there wasn't an intention to avoid racing calls to vpci_{read,write}_hw() this way. In any event I think such movement would need justification in the description.I agree about the need for justification in the commit message, or even better this being split into a pre-patch, as it's not related to the lock switching done here.
Quoted 5 lines
I do think this is fine however, as racing calls to vpci_{read,write}_hw() shouldn't be a problem. Those are just wrappers around pci_conf_{read,write} functions, and the required locking (in case of using the IO ports) is already taken care in pci_conf_{read,write}.
Quoted 4 lines
IOW you consider it acceptable for a guest (really: Dom0) read racing a write to read back only part of what was written (so far)? I would think individual multi-byte reads and writes should appear atomic to the guest.
We split 64bit writes into two 32bit ones without taking the lock for the whole duration of the access, so it's already possible to see a partially updated state as a result of a 64bit write.
Quoted 6 lines
Quoted 4 lines
I'm going over the PCI(e) spec but I don't seem to find anything about whether the ECAM is allowed to split memory transactions into multiple Configuration Requests, and whether those could then interleave with requests from a different CPU.
So, with the above is it still fine for you to have the change as is or you want this optimization to go into a dedicated patch before this one?
The change seems slightly controversial, so I think it would be best if it was split into a separate patch with a proper reasoning in the commit message.
Thanks, Roger.
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
Hi, Roger!
Quoted 57 lines · Roger Pau Monné
Quoted 56 lines · Oleksandr Andrushchenko
Hi, Roger, Jan!
Quoted 53 lines · Roger Pau Monné
Quoted 40 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
Review uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 401
Quoted 8 lines · Oleksandr Andrushchenko
(quoted diff) +0
-1@@ -379,7 +396,6 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) data = merge_result(data, tmp_data, size - data_offset, data_offset); } - spin_unlock(&pdev->vpci->lock); return data & (0xffffffff >> (32 - 8 * size)); }Here and ...
Quoted 6 lines
(quoted diff) +1
-0@@ -475,13 +498,12 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, break; ASSERT(data_offset < size); } + spin_unlock(&pdev->vpci_lock);Quoted 12 lines
Review void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 507
Quoted 7 lines
(quoted diff) +0
-2if ( data_offset < size ) /* Tailing gap, write the remaining. */ vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8)); - - spin_unlock(&pdev->vpci->lock); }... even more so here I'm not sure of the correctness of the moving you do: While
pdev->vpciindeed doesn't get accessed, I wonder whether there wasn't an intention to avoid racing calls to vpci_{read,write}_hw() this way. In any event I think such movement would need justification in the description.I agree about the need for justification in the commit message, or even better this being split into a pre-patch, as it's not related to the lock switching done here.
Quoted 5 lines
I do think this is fine however, as racing calls to vpci_{read,write}_hw() shouldn't be a problem. Those are just wrappers around pci_conf_{read,write} functions, and the required locking (in case of using the IO ports) is already taken care in pci_conf_{read,write}.
Quoted 4 lines
IOW you consider it acceptable for a guest (really: Dom0) read racing a write to read back only part of what was written (so far)? I would think individual multi-byte reads and writes should appear atomic to the guest.
We split 64bit writes into two 32bit ones without taking the lock for the whole duration of the access, so it's already possible to see a partially updated state as a result of a 64bit write.
I'm going over the PCI(e) spec but I don't seem to find anything about whether the ECAM is allowed to split memory transactions into multiple Configuration Requests, and whether those could then interleave with requests from a different CPU.
Quoted 5 lines
Quoted 2 lines
So, with the above is it still fine for you to have the change as is or you want this optimization to go into a dedicated patch before this one?
The change seems slightly controversial, so I think it would be best if it was split into a separate patch with a proper reasoning in the commit message.
Sure, will move into a dedicated patch then
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 03/14] vpci: move lock outside of struct vpci
In reply to Jan BeulichHi, Jan!
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +2
-2@@ -68,12 +84,13 @@ int vpci_add_handlers(struct pci_dev *pdev)/* We should not get here twice for the same device. */ ASSERT (!pdev->vpci); - pdev->vpci = xzalloc(struct vpci); -if ( !pdev->vpci ) + vpci = xzalloc(struct vpci); +if ( !vpci )return -ENOMEM ;
Quoted 6 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 76
Quoted 4 lines
xen/drivers/vpci/vpci.c +2
-1+spin_lock (&pdev->vpci_lock); + pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); -spin_lock_init (&pdev->vpci->lock);
INIT_LIST_HEAD()can occur ahead of taking the lock, and can also act on&vpci->handlersrather than&pdev->vpci->handlers.
Yes, I will move it, good catch
Quoted 8 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 100
Quoted 4 lines
xen/drivers/vpci/vpci.c +0
-0for ( i =0 ; i <NUM_VPCI_INIT ; i++ ) {@@ -83,7 +100,8 @@ int vpci_add_handlers(struct pci_dev *pdev) }This loop wants to live in the locked region because you need to install vpci into
pdev->vpciup front, afaict. I wonder whether that couldn't be relaxed, but perhaps that's an improvement that can be thought about later.
Ok, so I'll leave it as is
Quoted 14 lines
The main reason I'm looking at this closely is because from the patch title I didn't expect new locking regions to be introduced right here; instead I did expect strictly a mechanical conversion.
xen/drivers/vpci/vpci.c int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler, · line 155
Quoted 5 lines
xen/drivers/vpci/vpci.c +0
-1@@ -152,8 +170,6 @@ int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler, r->offset = offset; r->private = data; -spin_lock (&vpci->lock);From the description I can't deduce why this lock is fine to go away
now, i.e. that all call sites have the lock now acquire earlier. Therefore I'd expect at least an assertion to be left here ...
Quoted 6 lines
xen/drivers/vpci/vpci.c int vpci_remove_register(struct vpci *vpci, unsigned int offset, · line 186
Quoted 5 lines
xen/drivers/vpci/vpci.c +0
-1@@ -183,7 +197,6 @@ int vpci_remove_register(struct vpci *vpci, unsigned int offset,const struct vpci_register r = { .offset = offset, .size = size };struct vpci_register *rm; -spin_lock (&vpci->lock);... and here.
Previously the lock lived in struct vpci and now it lives in struct pci_dev which is not visible here, so:
1. we cannot take that lock here and do expect for it to be acquired outside
2. we cannot add an ASSERT here as we would need ASSERT(spin_is_locked(&pdev->vpci_lock));
and pdev is not here
All the callers of the vpci_{add|remove}_register are REGISTER_VPCI_INIT functions which are called with &pdev->vpci_lock held.
So, while I agree that it would be indeed a good check with ASSERT here, but adding an additional argument to the respective functions just for that might not be a good idea IMO
I will describe this lock removal in the commit message
Thank you,
Oleksandr
PATCH v5 4/14 vpci: cancel pending map/unmap on vpci removal
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
When a vPCI is removed for a PCI device it is possible that we have scheduled a delayed work for map/unmap operations for that device. For example, the following scenario can illustrate the problem:
pci_physdev_op
pci_add_device
init_bars -> modify_bars -> defer_map -> raise_softirq(SCHEDULE_SOFTIRQ)
iommu_add_device <- FAILS
vpci_remove_device -> xfree(pdev->vpci)
leave_hypervisor_to_guest
vpci_process_pending: v->vpci.mem != NULL; v->vpci.pdev->vpci == NULL
For the hardware domain we continue execution as the worse that could happen is that MMIO mappings are left in place when the device has been deassigned.
For unprivileged domains that get a failure in the middle of a vPCI {un}map operation we need to destroy them, as we don't know in which state the p2m is. This can only happen in vpci_process_pending for DomUs as they won't be allowed to call pci_add_device.
---
Cc: Roger Pau Monné <[email protected]>
---
Since v4:
- crash guest domain if map/unmap operation didn't succeed
- re-work vpci cancel work to cancel work on all vCPUs
- use new locking scheme with pdev->vpci_lock
New in v4
---
--- xen/drivers/vpci/header.c | 49 ++++++++++++++++++++++++++++++--------- xen/drivers/vpci/vpci.c | 2 ++ xen/include/xen/pci.h | 5 ++++ xen/include/xen/vpci.h | 6 +++++ 4 files changed, 51 insertions(+), 11 deletions(-)
xen/drivers/vpci/header.c +38 -11
@@ -131,7 +131,13 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,bool vpci_process_pending (struct vcpu *v) { -if ( v->vpci.mem ) +struct pci_dev *pdev = v->vpci.pdev; + +if ( !pdev ) +return false; + +spin_lock (&pdev->vpci_lock); +if ( !pdev->vpci_cancel_pending && v->vpci.mem ) {struct map_data data = { .d = v->domain,
@@ -140,32 +146,53 @@ bool vpci_process_pending(struct vcpu *v)int rc =rangeset_consume_ranges (v->vpci.mem, map_range, &data);if ( rc == -ERESTART ) + { +spin_unlock (&pdev->vpci_lock);return true; + } -spin_lock (&v->vpci.pdev->vpci_lock); -if ( v->vpci.pdev->vpci ) +if ( pdev->vpci )/* Disable memory decoding unconditionally on failure. */ -modify_decoding (v->vpci.pdev, +modify_decoding (pdev, rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, !rc && v->vpci.rom_only); -spin_unlock (&v->vpci.pdev->vpci_lock); -rangeset_destroy (v->vpci.mem); - v->vpci.mem =NULL ;if ( rc ) + {/* * FIXME: in case of failure remove the device from the domain. * Note that there might still be leftover mappings. While this is -* safe for Dom0, for DomUs the domain will likely need to be -* killed in order to avoid leaking stale p2m mappings on -* failure. +* safe for Dom0, for DomUs the domain needs to be killed in order +* to avoid leaking stale p2m mappings on failure. */ -vpci_remove_device (v->vpci.pdev); +if (is_hardware_domain (v->domain) ) +vpci_remove_device_locked (pdev); +else +domain_crash (v->domain); + } } +spin_unlock (&pdev->vpci_lock);return false; } +void vpci_cancel_pending_locked (struct pci_dev *pdev) +{ +struct vcpu *v; + +ASSERT (spin_is_locked (&pdev->vpci_lock)); + +/* Cancel any pending work now on all vCPUs. */ +for_each_vcpu ( pdev->domain, v ) + { +if ( v->vpci.mem && (v->vpci.pdev == pdev) ) + { +rangeset_destroy (v->vpci.mem); + v->vpci.mem =NULL ; + } + } +} +static int __init apply_map (struct domain *d,const struct pci_dev *pdev,struct rangeset *mem,uint16_t cmd) {
xen/drivers/vpci/vpci.c +2 -0
@@ -54,7 +54,9 @@ void vpci_remove_device_locked(struct pci_dev *pdev) {ASSERT (spin_is_locked (&pdev->vpci_lock)); + pdev->vpci_cancel_pending = true;vpci_remove_device_handlers_locked (pdev); +vpci_cancel_pending_locked (pdev);xfree (pdev->vpci->msix);xfree (pdev->vpci->msi);xfree (pdev->vpci);
xen/include/xen/pci.h +5 -0
@@ -135,6 +135,11 @@ struct pci_dev {/* Data for vPCI. */ spinlock_t vpci_lock; +/* +* Set if PCI device is being removed now and we need to cancel any +* pending map/unmap operations. +*/ +bool vpci_cancel_pending;struct vpci *vpci; };
xen/include/xen/vpci.h +6 -0
@@ -57,6 +57,7 @@ uint32_t vpci_hw_read32(const struct pci_dev *pdev, unsigned int reg,* should not run. */ bool __must_check vpci_process_pending (struct vcpu *v); +void vpci_cancel_pending_locked (struct pci_dev *pdev);struct vpci {/* List of vPCI handlers for a device. */
@@ -253,6 +254,11 @@ static inline bool __must_check vpci_process_pending(struct vcpu *v)ASSERT_UNREACHABLE ();return false; } + +static inline void vpci_cancel_pending_locked (struct pci_dev *pdev) +{ +ASSERT_UNREACHABLE (); +}#endif #endif
Signature · 1 line
2.25.1
Re: [PATCH v5 04/14] vpci: cancel pending map/unmap on vpci removal
Quoted 53 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
When a vPCI is removed for a PCI device it is possible that we have scheduled a delayed work for map/unmap operations for that device. For example, the following scenario can illustrate the problem:
pci_physdev_op
pci_add_deviceinit_bars -> modify_bars -> defer_map -> raise_softirq(SCHEDULE_SOFTIRQ)
iommu_add_device<- FAILS
vpci_remove_device->xfree(pdev->vpci)
leave_hypervisor_to_guest
vpci_process_pending:v->vpci.mem!= NULL;v->vpci.pdev->vpci== NULLFor the hardware domain we continue execution as the worse that could happen is that MMIO mappings are left in place when the device has been deassigned.
For unprivileged domains that get a failure in the middle of a vPCI {un}map operation we need to destroy them, as we don't know in which state the p2m is. This can only happen in
vpci_process_pendingfor DomUs as they won't be allowed to callpci_add_device.Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Cc: Roger Pau Monné <[email protected]>
---
Since v4:
- crash guest domain if map/unmap operation didn't succeed
- re-work vpci cancel work to cancel work on all vCPUs
- use new locking scheme withpdev->vpci_lock
New in v4Fixes 86dbcf6e30cb ("vpci: cancel pending map/unmap on vpci removal")---
Signed-off-by Oleksandr Andrushchenko <[email protected]>--- xen/drivers/vpci/header.c | 49 ++++++++++++++++++++++++++++++--------- xen/drivers/vpci/vpci.c | 2 ++ xen/include/xen/pci.h | 5 ++++ xen/include/xen/vpci.h | 6 +++++ 4 files changed, 51 insertions(+), 11 deletions(-)xen/drivers/vpci/header.c +0
-0@@ -131,7 +131,13 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
xen/drivers/vpci/header.c static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd, · line 140
Quoted 10 lines
xen/drivers/vpci/header.c +7
-1bool vpci_process_pending (struct vcpu *v) { -if ( v->vpci.mem ) +struct pci_dev *pdev = v->vpci.pdev; + +if ( !pdev ) +return false; + +spin_lock (&pdev->vpci_lock); +if ( !pdev->vpci_cancel_pending && v->vpci.mem )
Could you just check for pdev->vpci != NULL instead of having to add a new vpci_cancel_pending field?
I also have a suggestion below which could make the code here simpler.
Quoted 47 lines
xen/drivers/vpci/header.c +14
-10{struct map_data data = { .d = v->domain,@@ -140,32 +146,53 @@ bool vpci_process_pending(struct vcpu *v)int rc =rangeset_consume_ranges (v->vpci.mem, map_range, &data);if ( rc == -ERESTART ) + { +spin_unlock (&pdev->vpci_lock);return true; + } -spin_lock (&v->vpci.pdev->vpci_lock); -if ( v->vpci.pdev->vpci ) +if ( pdev->vpci )/* Disable memory decoding unconditionally on failure. */ -modify_decoding (v->vpci.pdev, +modify_decoding (pdev, rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, !rc && v->vpci.rom_only); -spin_unlock (&v->vpci.pdev->vpci_lock); -rangeset_destroy (v->vpci.mem); - v->vpci.mem =NULL ;if ( rc ) + {/* * FIXME: in case of failure remove the device from the domain. * Note that there might still be leftover mappings. While this is -* safe for Dom0, for DomUs the domain will likely need to be -* killed in order to avoid leaking stale p2m mappings on -* failure. +* safe for Dom0, for DomUs the domain needs to be killed in order +* to avoid leaking stale p2m mappings on failure. */ -vpci_remove_device (v->vpci.pdev); +if (is_hardware_domain (v->domain) ) +vpci_remove_device_locked (pdev); +else +domain_crash (v->domain); + } } +spin_unlock (&pdev->vpci_lock);return false; }
xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 188
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+void vpci_cancel_pending_locked (struct pci_dev *pdev) +{ +struct vcpu *v; + +ASSERT (spin_is_locked (&pdev->vpci_lock)); + +/* Cancel any pending work now on all vCPUs. */ +for_each_vcpu ( pdev->domain, v ) + { +if ( v->vpci.mem && (v->vpci.pdev == pdev) )
I'm unsure this is correct. You are protecting the access to v->vpci.pdev with an expectation that v->vpci.pdev->vpci_lock is being held.
I wonder if it would be better to just pause all the domain vCPUs and then perform the cleaning of any pending operations. That would assure that there are no changes to v->vpci. vpci_cancel_pending_locked shouldn't be a frequent operation, so the overhead of pausing all domain vCPUs here is likely fine.
Thanks, Roger.
Re: [PATCH v5 04/14] vpci: cancel pending map/unmap on vpci removal
In reply to Oleksandr AndrushchenkoQuoted 35 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
When a vPCI is removed for a PCI device it is possible that we have scheduled a delayed work for map/unmap operations for that device. For example, the following scenario can illustrate the problem:
pci_physdev_op
pci_add_deviceinit_bars -> modify_bars -> defer_map -> raise_softirq(SCHEDULE_SOFTIRQ)
iommu_add_device<- FAILS
vpci_remove_device->xfree(pdev->vpci)
leave_hypervisor_to_guest
vpci_process_pending:v->vpci.mem!= NULL;v->vpci.pdev->vpci== NULLFor the hardware domain we continue execution as the worse that could happen is that MMIO mappings are left in place when the device has been deassigned.
For unprivileged domains that get a failure in the middle of a vPCI {un}map operation we need to destroy them, as we don't know in which state the p2m is. This can only happen in
vpci_process_pendingfor DomUs as they won't be allowed to callpci_add_device.Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Cc: Roger Pau Monné <[email protected]>
---
Since v4:
- crash guest domain if map/unmap operation didn't succeed
- re-work vpci cancel work to cancel work on all vCPUs
- use new locking scheme withpdev->vpci_lock
New in v4
Quoted 1 line
Fixes 86dbcf6e30cb ("vpci: cancel pending map/unmap on vpci removal")
What is this about?
Jan
Re: [PATCH v5 04/14] vpci: cancel pending map/unmap on vpci removal
Quoted 36 lines · Jan Beulich
Quoted 35 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
When a vPCI is removed for a PCI device it is possible that we have scheduled a delayed work for map/unmap operations for that device. For example, the following scenario can illustrate the problem:
pci_physdev_oppci_add_device init_bars -> modify_bars -> defer_map -> raise_softirq(SCHEDULE_SOFTIRQ) iommu_add_device <- FAILS vpci_remove_device -> xfree(pdev->vpci)
leave_hypervisor_to_guestvpci_process_pending: v->vpci.mem != NULL; v->vpci.pdev->vpci == NULLFor the hardware domain we continue execution as the worse that could happen is that MMIO mappings are left in place when the device has been deassigned.
For unprivileged domains that get a failure in the middle of a vPCI {un}map operation we need to destroy them, as we don't know in which state the p2m is. This can only happen in
vpci_process_pendingfor DomUs as they won't be allowed to callpci_add_device.Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Cc: Roger Pau Monné <[email protected]>
---
Since v4:
- crash guest domain if map/unmap operation didn't succeed
- re-work vpci cancel work to cancel work on all vCPUs
- use new locking scheme withpdev->vpci_lock
New in v4
Quoted 2 lines
Quoted 1 line
Fixes 86dbcf6e30cb ("vpci: cancel pending map/unmap on vpci removal")What is this about?
Just a leftover after squashing WIP patches, sorry
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 04/14] vpci: cancel pending map/unmap on vpci removal
In reply to Oleksandr AndrushchenkoI am going to postpone this patch as it does need major re-thinking on the approach to take. This is possible as it fixes a really rare use-case seen during development phase, so shouldn't hurt the run-time
Thank you,
Oleksandr
Quoted 184 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
When a vPCI is removed for a PCI device it is possible that we have scheduled a delayed work for map/unmap operations for that device. For example, the following scenario can illustrate the problem:
pci_physdev_oppci_add_device init_bars -> modify_bars -> defer_map -> raise_softirq(SCHEDULE_SOFTIRQ) iommu_add_device <- FAILS vpci_remove_device -> xfree(pdev->vpci)
leave_hypervisor_to_guestvpci_process_pending: v->vpci.mem != NULL; v->vpci.pdev->vpci == NULLFor the hardware domain we continue execution as the worse that could happen is that MMIO mappings are left in place when the device has been deassigned.
For unprivileged domains that get a failure in the middle of a vPCI {un}map operation we need to destroy them, as we don't know in which state the p2m is. This can only happen in
vpci_process_pendingfor DomUs as they won't be allowed to callpci_add_device.Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Cc: Roger Pau Monné <[email protected]>
---
Since v4:
- crash guest domain if map/unmap operation didn't succeed
- re-work vpci cancel work to cancel work on all vCPUs
- use new locking scheme withpdev->vpci_lock
New in v4Fixes 86dbcf6e30cb ("vpci: cancel pending map/unmap on vpci removal")---
Signed-off-by Oleksandr Andrushchenko <[email protected]>--- xen/drivers/vpci/header.c | 49 ++++++++++++++++++++++++++++++--------- xen/drivers/vpci/vpci.c | 2 ++ xen/include/xen/pci.h | 5 ++++ xen/include/xen/vpci.h | 6 +++++ 4 files changed, 51 insertions(+), 11 deletions(-)xen/drivers/vpci/header.c +38
-11@@ -131,7 +131,13 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,bool vpci_process_pending (struct vcpu *v) { -if ( v->vpci.mem ) +struct pci_dev *pdev = v->vpci.pdev; + +if ( !pdev ) +return false; + +spin_lock (&pdev->vpci_lock); +if ( !pdev->vpci_cancel_pending && v->vpci.mem ) {struct map_data data = { .d = v->domain,@@ -140,32 +146,53 @@ bool vpci_process_pending(struct vcpu *v)int rc =rangeset_consume_ranges (v->vpci.mem, map_range, &data);if ( rc == -ERESTART ) + { +spin_unlock (&pdev->vpci_lock);return true; + } -spin_lock (&v->vpci.pdev->vpci_lock); -if ( v->vpci.pdev->vpci ) +if ( pdev->vpci )/* Disable memory decoding unconditionally on failure. */ -modify_decoding (v->vpci.pdev, +modify_decoding (pdev, rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, !rc && v->vpci.rom_only); -spin_unlock (&v->vpci.pdev->vpci_lock); -rangeset_destroy (v->vpci.mem); - v->vpci.mem =NULL ;if ( rc ) + {/* * FIXME: in case of failure remove the device from the domain. * Note that there might still be leftover mappings. While this is -* safe for Dom0, for DomUs the domain will likely need to be -* killed in order to avoid leaking stale p2m mappings on -* failure. +* safe for Dom0, for DomUs the domain needs to be killed in order +* to avoid leaking stale p2m mappings on failure. */ -vpci_remove_device (v->vpci.pdev); +if (is_hardware_domain (v->domain) ) +vpci_remove_device_locked (pdev); +else +domain_crash (v->domain); + } } +spin_unlock (&pdev->vpci_lock);return false; } +void vpci_cancel_pending_locked (struct pci_dev *pdev) +{ +struct vcpu *v; + +ASSERT (spin_is_locked (&pdev->vpci_lock)); + +/* Cancel any pending work now on all vCPUs. */ +for_each_vcpu ( pdev->domain, v ) + { +if ( v->vpci.mem && (v->vpci.pdev == pdev) ) + { +rangeset_destroy (v->vpci.mem); + v->vpci.mem =NULL ; + } + } +} +static int __init apply_map (struct domain *d,const struct pci_dev *pdev,struct rangeset *mem,uint16_t cmd) {xen/drivers/vpci/vpci.c +2
-0@@ -54,7 +54,9 @@ void vpci_remove_device_locked(struct pci_dev *pdev) {ASSERT (spin_is_locked (&pdev->vpci_lock)); + pdev->vpci_cancel_pending = true;vpci_remove_device_handlers_locked (pdev); +vpci_cancel_pending_locked (pdev);xfree (pdev->vpci->msix);xfree (pdev->vpci->msi);xfree (pdev->vpci);xen/include/xen/pci.h +5
-0@@ -135,6 +135,11 @@ struct pci_dev {/* Data for vPCI. */ spinlock_t vpci_lock; +/* +* Set if PCI device is being removed now and we need to cancel any +* pending map/unmap operations. +*/ +bool vpci_cancel_pending;struct vpci *vpci; };xen/include/xen/vpci.h +6
-0@@ -57,6 +57,7 @@ uint32_t vpci_hw_read32(const struct pci_dev *pdev, unsigned int reg,* should not run. */ bool __must_check vpci_process_pending (struct vcpu *v); +void vpci_cancel_pending_locked (struct pci_dev *pdev);struct vpci {/* List of vPCI handlers for a device. */ @@ -253,6 +254,11 @@ static inline bool __must_check vpci_process_pending(struct vcpu *v)ASSERT_UNREACHABLE ();return false; } + +static inline void vpci_cancel_pending_locked (struct pci_dev *pdev) +{ +ASSERT_UNREACHABLE (); +}#endif #endif
PATCH v5 5/14 vpci: add hooks for PCI device assign/de-assign
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
When a PCI device gets assigned/de-assigned some work on vPCI side needs to be done for that device. Introduce a pair of hooks so vPCI can handle that.
---
Since v4:
- de-assign vPCI from the previous domain on device assignment
- do not remove handlers in vpci_assign_device as those must not exist at that point
Since v3:
- remove toolstack roll-back description from the commit message as error are to be handled with proper cleanup in Xen itself
- remove __must_check
- remove redundant rc check while assigning devices
- fix redundant CONFIG_HAS_VPCI check for CONFIG_HAS_VPCI_GUEST_SUPPORT
- use REGISTER_VPCI_INIT machinery to run required steps on device init/assign: add run_vpci_init helper
Since v2:
- define CONFIG_HAS_VPCI_GUEST_SUPPORT so dead code is not compiled for x86
Since v1:
- constify struct pci_dev where possible
- do not open code is_system_domain()
- extended the commit message
--- xen/drivers/Kconfig | 4 +++ xen/drivers/passthrough/pci.c | 10 ++++++ xen/drivers/vpci/vpci.c | 61 +++++++++++++++++++++++++++++------ xen/include/xen/vpci.h | 16 +++++++++ 4 files changed, 82 insertions(+), 9 deletions(-)
xen/drivers/Kconfig +4 -0
@@ -15,4 +15,8 @@ source "drivers/video/Kconfig" config HAS_VPCI bool +config HAS_VPCI_GUEST_SUPPORT + bool + depends on HAS_VPCI + endmenu
xen/drivers/passthrough/pci.c +10 -0
@@ -874,6 +874,10 @@ static int deassign_device(struct domain *d, uint16_t seg, uint8_t bus,if ( ret ) goto out; + ret =vpci_deassign_device (d, pdev); +if ( ret ) + goto out; +if ( pdev->domain == hardware_domain ) pdev->quarantine = false;
@@ -1429,6 +1433,10 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn, u32 flag)ASSERT (pdev && (pdev->domain == hardware_domain || pdev->domain == dom_io)); + rc =vpci_deassign_device (pdev->domain, pdev); +if ( rc ) + goto done; + rc =pdev_msix_assign (d, pdev);if ( rc ) goto done;
@@ -1446,6 +1454,8 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn, u32 flag) rc = hd->platform_ops->assign_device (d, devfn,pci_to_dev (pdev), flag); } + rc =vpci_assign_device (d, pdev); + done:if ( rc ) printk(XENLOG_G_WARNING "%pd: assign (%pp) failed (%d)\n",
xen/drivers/vpci/vpci.c +52 -9
83 lines not on this page · raw message
xen/include/xen/vpci.h +16 -0
@@ -261,6 +261,22 @@ static inline void vpci_cancel_pending_locked(struct pci_dev *pdev) }#endif +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Notify vPCI that device is assigned/de-assigned to/from guest. */ +int vpci_assign_device (struct domain *d,struct pci_dev *pdev); +int vpci_deassign_device (struct domain *d,struct pci_dev *pdev); +#else +static inline int vpci_assign_device (struct domain *d,struct pci_dev *pdev) +{ +return 0 ; +}; + +static inline int vpci_deassign_device (struct domain *d,struct pci_dev *pdev) +{ +return 0 ; +}; +#endif +#endif /*
Signature · 1 line
2.25.1
Re: [PATCH v5 05/14] vpci: add hooks for PCI device assign/de-assign
Quoted 52 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
When a PCI device gets assigned/de-assigned some work on vPCI side needs to be done for that device. Introduce a pair of hooks so vPCI can handle that.
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- de-assign vPCI from the previous domain on device assignment
- do not remove handlers invpci_assign_deviceas those must not exist at that point
Since v3:
- remove toolstack roll-back description from the commit message as error are to be handled with proper cleanup in Xen itself
- remove__must_check
- remove redundant rc check while assigning devices
- fix redundantCONFIG_HAS_VPCIcheck forCONFIG_HAS_VPCI_GUEST_SUPPORT
- useREGISTER_VPCI_INITmachinery to run required steps on device init/assign: addrun_vpci_inithelper
Since v2:
- defineCONFIG_HAS_VPCI_GUEST_SUPPORTso dead code is not compiled for x86
Since v1:
- constify structpci_devwhere possible
- do not open codeis_system_domain()
- extended the commit message--- xen/drivers/Kconfig | 4 +++ xen/drivers/passthrough/pci.c | 10 ++++++ xen/drivers/vpci/vpci.c | 61 +++++++++++++++++++++++++++++------ xen/include/xen/vpci.h | 16 +++++++++ 4 files changed, 82 insertions(+), 9 deletions(-)xen/drivers/Kconfig +4
-0@@ -15,4 +15,8 @@ source "drivers/video/Kconfig" config HAS_VPCI bool +config HAS_VPCI_GUEST_SUPPORT + bool + depends on HAS_VPCI + endmenuxen/drivers/passthrough/pci.c +0
-0
xen/drivers/passthrough/pci.c static int deassign_device(struct domain *d, uint16_t seg, uint8_t bus, · line 879
Quoted 6 lines
xen/drivers/passthrough/pci.c +3
-0@@ -874,6 +874,10 @@ static int deassign_device(struct domain *d, uint16_t seg, uint8_t bus,if ( ret ) goto out; + ret =vpci_deassign_device (d, pdev); +if ( ret ) + goto out;
Following my comment below, this won't be allowed to fail.
Quoted 29 lines
xen/drivers/passthrough/pci.c +7
-0+if ( pdev->domain == hardware_domain ) pdev->quarantine = false;@@ -1429,6 +1433,10 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn, u32 flag)ASSERT (pdev && (pdev->domain == hardware_domain || pdev->domain == dom_io)); + rc =vpci_deassign_device (pdev->domain, pdev); +if ( rc ) + goto done; + rc =pdev_msix_assign (d, pdev);if ( rc ) goto done;@@ -1446,6 +1454,8 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn, u32 flag) rc = hd->platform_ops->assign_device (d, devfn,pci_to_dev (pdev), flag); } + rc =vpci_assign_device (d, pdev); + done:if ( rc ) printk(XENLOG_G_WARNING "%pd: assign (%pp) failed (%d)\n",xen/drivers/vpci/vpci.c +0
-0
xen/drivers/vpci/vpci.c void vpci_remove_device(struct pci_dev *pdev) · line 77
Quoted 5 lines
xen/drivers/vpci/vpci.c +1
-1@@ -74,12 +74,26 @@ void vpci_remove_device(struct pci_dev *pdev)spin_unlock (&pdev->vpci_lock); } -int vpci_add_handlers (struct pci_dev *pdev) +static int run_vpci_init (struct pci_dev *pdev)
Just using add_handlers as function name would be clearer IMO.
Quoted 40 lines
xen/drivers/vpci/vpci.c +16
-8{ -struct vpci *vpci;unsigned int i;int rc =0 ; +for ( i =0 ; i <NUM_VPCI_INIT ; i++ ) + { + rc = __start_vpci_array[i](pdev); +if ( rc ) +break ; + } + +return rc; +} + +int vpci_add_handlers (struct pci_dev *pdev) +{ +struct vpci *vpci; +int rc; +if ( !has_vpci (pdev->domain) )return 0 ;@@ -94,19 +108,48 @@ int vpci_add_handlers(struct pci_dev *pdev) pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); -for ( i =0 ; i <NUM_VPCI_INIT ; i++ ) - { - rc = __start_vpci_array[i](pdev); -if ( rc ) -break ; - } - + rc =run_vpci_init (pdev);if ( rc )vpci_remove_device_locked (pdev);spin_unlock (&pdev->vpci_lock);return rc;
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 126
Quoted 10 lines
xen/drivers/vpci/vpci.c +9
-0} + +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Notify vPCI that device is assigned to guest. */ +int vpci_assign_device (struct domain *d,struct pci_dev *pdev) +{ +int rc; + +/* It only makes sense to assign for hwdom or guest domain. */ +if (is_system_domain (d) || !has_vpci (d) )
Do you really need the is_system_domain check? System domains shouldn't have the VPCI flag set anyway, so should fail the has_vpci test.
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 139
Quoted 13 lines
xen/drivers/vpci/vpci.c +13
-0+return 0 ; + +spin_lock (&pdev->vpci_lock); + rc =run_vpci_init (pdev); +spin_unlock (&pdev->vpci_lock); +if ( rc ) +vpci_deassign_device (d, pdev); + +return rc; +} + +/* Notify vPCI that device is de-assigned from guest. */ +int vpci_deassign_device (struct domain *d,struct pci_dev *pdev)
There's no need to return any value from this function AFAICT. It should have void return type.
Thanks, Roger.
Re: [PATCH v5 05/14] vpci: add hooks for PCI device assign/de-assign
Hi, Roger!
Quoted 93 lines · Roger Pau Monné
Quoted 52 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
When a PCI device gets assigned/de-assigned some work on vPCI side needs to be done for that device. Introduce a pair of hooks so vPCI can handle that.
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- de-assign vPCI from the previous domain on device assignment
- do not remove handlers invpci_assign_deviceas those must not exist at that point
Since v3:
- remove toolstack roll-back description from the commit message as error are to be handled with proper cleanup in Xen itself
- remove__must_check
- remove redundant rc check while assigning devices
- fix redundantCONFIG_HAS_VPCIcheck forCONFIG_HAS_VPCI_GUEST_SUPPORT
- useREGISTER_VPCI_INITmachinery to run required steps on device init/assign: addrun_vpci_inithelper
Since v2:
- defineCONFIG_HAS_VPCI_GUEST_SUPPORTso dead code is not compiled for x86
Since v1:
- constify structpci_devwhere possible
- do not open codeis_system_domain()
- extended the commit message--- xen/drivers/Kconfig | 4 +++ xen/drivers/passthrough/pci.c | 10 ++++++ xen/drivers/vpci/vpci.c | 61 +++++++++++++++++++++++++++++------ xen/include/xen/vpci.h | 16 +++++++++ 4 files changed, 82 insertions(+), 9 deletions(-)xen/drivers/Kconfig +4
-0@@ -15,4 +15,8 @@ source "drivers/video/Kconfig" config HAS_VPCI bool +config HAS_VPCI_GUEST_SUPPORT + bool + depends on HAS_VPCI + endmenuxen/drivers/passthrough/pci.c +0
-0xen/drivers/passthrough/pci.c static int deassign_device(struct domain *d, uint16_t seg, uint8_t bus, · line 879
Quoted 6 lines
xen/drivers/passthrough/pci.c +3
-0@@ -874,6 +874,10 @@ static int deassign_device(struct domain *d, uint16_t seg, uint8_t bus,if ( ret ) goto out; + ret =vpci_deassign_device (d, pdev); +if ( ret ) + goto out;Following my comment below, this won't be allowed to fail.
Quoted 32 lines
xen/drivers/passthrough/pci.c +7
-0+if ( pdev->domain == hardware_domain ) pdev->quarantine = false;@@ -1429,6 +1433,10 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn, u32 flag)ASSERT (pdev && (pdev->domain == hardware_domain || pdev->domain == dom_io)); + rc =vpci_deassign_device (pdev->domain, pdev); +if ( rc ) + goto done; + rc =pdev_msix_assign (d, pdev);if ( rc ) goto done;@@ -1446,6 +1454,8 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn, u32 flag) rc = hd->platform_ops->assign_device (d, devfn,pci_to_dev (pdev), flag); } + rc =vpci_assign_device (d, pdev); + done:if ( rc ) printk(XENLOG_G_WARNING "%pd: assign (%pp) failed (%d)\n",xen/drivers/vpci/vpci.c +0
-0@@ -74,12 +74,26 @@ void vpci_remove_device(struct pci_dev *pdev)spin_unlock (&pdev->vpci_lock); }
Quoted 3 lines
xen/drivers/vpci/vpci.c void vpci_remove_device(struct pci_dev *pdev) · line 77
Quoted 2 lines
xen/drivers/vpci/vpci.c +1
-1-int vpci_add_handlers (struct pci_dev *pdev) +static int run_vpci_init (struct pci_dev *pdev)Just using
add_handlersas function name would be clearer IMO.
Ok, will change
Quoted 40 lines
Quoted 40 lines
xen/drivers/vpci/vpci.c +16
-8{ -struct vpci *vpci;unsigned int i;int rc =0 ; +for ( i =0 ; i <NUM_VPCI_INIT ; i++ ) + { + rc = __start_vpci_array[i](pdev); +if ( rc ) +break ; + } + +return rc; +} + +int vpci_add_handlers (struct pci_dev *pdev) +{ +struct vpci *vpci; +int rc; +if ( !has_vpci (pdev->domain) )return 0 ;@@ -94,19 +108,48 @@ int vpci_add_handlers(struct pci_dev *pdev) pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); -for ( i =0 ; i <NUM_VPCI_INIT ; i++ ) - { - rc = __start_vpci_array[i](pdev); -if ( rc ) -break ; - } - + rc =run_vpci_init (pdev);if ( rc )vpci_remove_device_locked (pdev);spin_unlock (&pdev->vpci_lock);
Quoted 14 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 126
Quoted 11 lines
xen/drivers/vpci/vpci.c +9
-0return rc; } + +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Notify vPCI that device is assigned to guest. */ +int vpci_assign_device (struct domain *d,struct pci_dev *pdev) +{ +int rc; + +/* It only makes sense to assign for hwdom or guest domain. */ +if (is_system_domain (d) || !has_vpci (d) )Do you really need the
is_system_domaincheck? System domains shouldn't have the VPCI flag set anyway, so should fail thehas_vpcitest.
No, it seems we do not need this check: will remove
Quoted 16 lines
Quoted 4 lines
xen/drivers/vpci/vpci.c +3
-0+return 0 ; + +spin_lock (&pdev->vpci_lock);xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 139
Quoted 10 lines
xen/drivers/vpci/vpci.c +10
-0+ rc =run_vpci_init (pdev); +spin_unlock (&pdev->vpci_lock); +if ( rc ) +vpci_deassign_device (d, pdev); + +return rc; +} + +/* Notify vPCI that device is de-assigned from guest. */ +int vpci_deassign_device (struct domain *d,struct pci_dev *pdev)There's no need to return any value from this function AFAICT. It should have void return type.
Makes sense, I will s/int/void
Quoted 1 line
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 05/14] vpci: add hooks for PCI device assign/de-assign
In reply to Oleksandr Andrushchenkoxen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 130
Quoted 13 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
xen/drivers/vpci/vpci.c +12
-0+#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Notify vPCI that device is assigned to guest. */ +int vpci_assign_device (struct domain *d,struct pci_dev *pdev) +{ +int rc; + +/* It only makes sense to assign for hwdom or guest domain. */ +if (is_system_domain (d) || !has_vpci (d) ) +return 0 ; + +spin_lock (&pdev->vpci_lock); + rc =run_vpci_init (pdev);
Following my comment below, this will likely need to call vpci_add_handlers in order to allocate the pdev->vpci field.
It's not OK to carry the contents of pdev->vpci across domain assignations, as the device should be reset, and thus the content of pdev->vpci would be stale.
Quoted 6 lines
xen/drivers/vpci/vpci.c +6
-0+spin_unlock (&pdev->vpci_lock); +if ( rc ) +vpci_deassign_device (d, pdev); + +return rc; +}
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 146
Quoted 10 lines
xen/drivers/vpci/vpci.c +10
-0+ +/* Notify vPCI that device is de-assigned from guest. */ +int vpci_deassign_device (struct domain *d,struct pci_dev *pdev) +{ +/* It only makes sense to de-assign from hwdom or guest domain. */ +if (is_system_domain (d) || !has_vpci (d) ) +return 0 ; + +spin_lock (&pdev->vpci_lock); +vpci_remove_device_handlers_locked (pdev);
You need to free the pdev->vpci structure on deassign. I would expect the device to be reset on deassign, so keeping the pdev->vpci contents would be wrong.
Thanks, Roger.
Re: [PATCH v5 05/14] vpci: add hooks for PCI device assign/de-assign
Hi, Roger!
Quoted 32 lines · Roger Pau Monné
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 130
Quoted 13 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
xen/drivers/vpci/vpci.c +12
-0+#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Notify vPCI that device is assigned to guest. */ +int vpci_assign_device (struct domain *d,struct pci_dev *pdev) +{ +int rc; + +/* It only makes sense to assign for hwdom or guest domain. */ +if (is_system_domain (d) || !has_vpci (d) ) +return 0 ; + +spin_lock (&pdev->vpci_lock); + rc =run_vpci_init (pdev);Following my comment below, this will likely need to call
vpci_add_handlersin order to allocate thepdev->vpcifield.It's not OK to carry the contents of
pdev->vpciacross domain assignations, as the device should be reset, and thus the content ofpdev->vpciwould be stale.Quoted 11 lines
xen/drivers/vpci/vpci.c +11
-0+spin_unlock (&pdev->vpci_lock); +if ( rc ) +vpci_deassign_device (d, pdev); + +return rc; +} + +/* Notify vPCI that device is de-assigned from guest. */ +int vpci_deassign_device (struct domain *d,struct pci_dev *pdev) +{ +/* It only makes sense to de-assign from hwdom or guest domain. */
Quoted 8 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 146
Quoted 5 lines
xen/drivers/vpci/vpci.c +5
-0+if (is_system_domain (d) || !has_vpci (d) ) +return 0 ; + +spin_lock (&pdev->vpci_lock); +vpci_remove_device_handlers_locked (pdev);You need to free the
pdev->vpcistructure on deassign. I would expect the device to be reset on deassign, so keeping thepdev->vpcicontents would be wrong.
Sure, I will re-allocate pdev->vpci then
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 05/14] vpci: add hooks for PCI device assign/de-assign
Hi, Roger!
Quoted 43 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 32 lines · Roger Pau Monné
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 130
Quoted 13 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
xen/drivers/vpci/vpci.c +12
-0+#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Notify vPCI that device is assigned to guest. */ +int vpci_assign_device (struct domain *d,struct pci_dev *pdev) +{ +int rc; + +/* It only makes sense to assign for hwdom or guest domain. */ +if (is_system_domain (d) || !has_vpci (d) ) +return 0 ; + +spin_lock (&pdev->vpci_lock); + rc =run_vpci_init (pdev);Following my comment below, this will likely need to call
vpci_add_handlersin order to allocate thepdev->vpcifield.It's not OK to carry the contents of
pdev->vpciacross domain assignations, as the device should be reset, and thus the content ofpdev->vpciwould be stale.Quoted 11 lines
xen/drivers/vpci/vpci.c +11
-0+spin_unlock (&pdev->vpci_lock); +if ( rc ) +vpci_deassign_device (d, pdev); + +return rc; +} + +/* Notify vPCI that device is de-assigned from guest. */ +int vpci_deassign_device (struct domain *d,struct pci_dev *pdev) +{ +/* It only makes sense to de-assign from hwdom or guest domain. */ Quoted 8 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 146
Quoted 5 lines
xen/drivers/vpci/vpci.c +5
-0+if (is_system_domain (d) || !has_vpci (d) ) +return 0 ; + +spin_lock (&pdev->vpci_lock); +vpci_remove_device_handlers_locked (pdev);You need to free the
pdev->vpcistructure on deassign. I would expect the device to be reset on deassign, so keeping thepdev->vpcicontents would be wrong.
Quoted 1 line
Sure, I will re-allocate
pdev->vpcithen
After thinking a bit more on this I have realized that we cannot free pdev->vpci on de-assign. The reason for that is the fact that vpci structure contains vital data which is collected and managed at different
stages: for example, BAR types are collected while we run for the hardware domain and in init_bars we collect the types of the BARS etc. This is then used while assigning device to construct guest's representation of the device. Freeing vpci will lead to that data is lost and the required data is not populated into vpci.
So, it is no possible to free vpci structure and I am about to leave the approach as it is.
Quoted 3 lines
Quoted 1 line
Thanks, Roger.
Thank you,
Oleksandr
Thank you,
Oleksandr
Re: [PATCH v5 05/14] vpci: add hooks for PCI device assign/de-assign
Quoted 47 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 43 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 32 lines · Roger Pau Monné
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 130
Quoted 13 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
xen/drivers/vpci/vpci.c +12
-0+#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Notify vPCI that device is assigned to guest. */ +int vpci_assign_device (struct domain *d,struct pci_dev *pdev) +{ +int rc; + +/* It only makes sense to assign for hwdom or guest domain. */ +if (is_system_domain (d) || !has_vpci (d) ) +return 0 ; + +spin_lock (&pdev->vpci_lock); + rc =run_vpci_init (pdev);Following my comment below, this will likely need to call
vpci_add_handlersin order to allocate thepdev->vpcifield.It's not OK to carry the contents of
pdev->vpciacross domain assignations, as the device should be reset, and thus the content ofpdev->vpciwould be stale.Quoted 11 lines
xen/drivers/vpci/vpci.c +11
-0+spin_unlock (&pdev->vpci_lock); +if ( rc ) +vpci_deassign_device (d, pdev); + +return rc; +} + +/* Notify vPCI that device is de-assigned from guest. */ +int vpci_deassign_device (struct domain *d,struct pci_dev *pdev) +{ +/* It only makes sense to de-assign from hwdom or guest domain. */ Quoted 8 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 146
Quoted 5 lines
xen/drivers/vpci/vpci.c +5
-0+if (is_system_domain (d) || !has_vpci (d) ) +return 0 ; + +spin_lock (&pdev->vpci_lock); +vpci_remove_device_handlers_locked (pdev);You need to free the
pdev->vpcistructure on deassign. I would expect the device to be reset on deassign, so keeping thepdev->vpcicontents would be wrong.Quoted 1 line
Sure, I will re-allocate
pdev->vpcithen
Quoted 10 lines
After thinking a bit more on this I have realized that we cannot free
pdev->vpcion de-assign. The reason for that is the fact that vpci structure contains vital data which is collected and managed at different
stages: for example, BAR types are collected while we run for the hardware domain and ininit_barswe collect the types of the BARS etc. This is then used while assigning device to construct guest's representation of the device. Freeing vpci will lead to that data is lost and the required data is not populated into vpci.
So, it is no possible to free vpci structure and I am about to leave the approach as it is.
We discussed this on IRC, and we have agreed that it's possible to free pdev->vpci on deassign since in any case we need to call init_bars (and other capability init functions) when the device is assigned to setup the register traps and fetch the required information in order to fill pdev->vpci.
Roger.
PATCH v5 6/14 vpci/header: implement guest BAR register handlers
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
---
Since v4:
- updated commit message
- s/guest_addr/guest_reg
Since v3:
- squashed two patches: dynamic add/remove handlers and guest BAR handler implementation
- fix guest BAR read of the high part of a 64bit BAR (Roger)
- add error handling to vpci_assign_device
- s/dom%pd/%pd
- blank line before return
Since v2:
- remove unneeded ifdefs for CONFIG_HAS_VPCI_GUEST_SUPPORT as more code has been eliminated from being built on x86
Since v1:
- constify struct pci_dev where possible
- do not open code is_system_domain()
- simplify some code3. simplify
- use gdprintk + error code instead of gprintk
- gate vpci_bar_{add|remove}_handlers with CONFIG_HAS_VPCI_GUEST_SUPPORT, so these do not get compiled for x86
- removed unneeded is_system_domain check
- re-work guest read/write to be much simpler and do more work on write than read which is expected to be called more frequently
- removed one too obvious comment
--- xen/drivers/vpci/header.c | 72 +++++++++++++++++++++++++++++++++++---- xen/include/xen/vpci.h | 3 ++ 2 files changed, 69 insertions(+), 6 deletions(-)
xen/drivers/vpci/header.c +66 -6
@@ -433,6 +433,48 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write32 (pdev->sbdf, reg, val); } +static void guest_bar_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } +else + { + val &=PCI_BASE_ADDRESS_MEM_MASK ; + val |= bar->type ==VPCI_BAR_MEM32 ?PCI_BASE_ADDRESS_MEM_TYPE_32 + :PCI_BASE_ADDRESS_MEM_TYPE_64 ; + val |= bar->prefetchable ?PCI_BASE_ADDRESS_MEM_PREFETCH :0 ; + } + + bar->guest_reg &= ~(0xffffffffull << (hi ?32 :0 )); + bar->guest_reg |= (uint64_t )val << (hi ?32 :0 ); + + bar->guest_reg &= ~(bar->size -1 ) | ~PCI_BASE_ADDRESS_MEM_MASK ; +} + +static uint32_t guest_bar_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +const struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } + +return bar->guest_reg >> (hi ?32 :0 ); +} +static void rom_write (const struct pci_dev *pdev,unsigned int reg,uint32_t val,void *data) {
@@ -481,6 +523,17 @@ static void rom_write(const struct pci_dev *pdev, unsigned int reg, rom->addr = val &PCI_ROM_ADDRESS_MASK ; } +static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +} +static int init_bars (struct pci_dev *pdev) {uint16_t cmd;
@@ -489,6 +542,7 @@ static int init_bars(struct pci_dev *pdev)struct vpci_header *header = &pdev->vpci->header;struct vpci_bar *bars = header->bars;int rc; +bool is_hwdom =is_hardware_domain (pdev->domain);switch (pci_conf_read8 (pdev->sbdf,PCI_HEADER_TYPE ) &0x7f ) {
@@ -528,8 +582,10 @@ static int init_bars(struct pci_dev *pdev)if ( i && bars[i -1 ].type ==VPCI_BAR_MEM64_LO ) { bars[i].type =VPCI_BAR_MEM64_HI ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg, -4 , &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);
@@ -569,8 +625,10 @@ static int init_bars(struct pci_dev *pdev) bars[i].size = size; bars[i].prefetchable = val &PCI_BASE_ADDRESS_MEM_PREFETCH ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg,4 , - &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);
@@ -590,8 +648,10 @@ static int init_bars(struct pci_dev *pdev) header->rom_enabled =pci_conf_read32 (pdev->sbdf, rom_reg) &PCI_ROM_ADDRESS_ENABLE ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, rom_write, rom_reg, -4 , rom); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_rom_read, + is_hwdom ? rom_write : guest_rom_write, + rom_reg,4 , rom);if ( rc ) rom->type =VPCI_BAR_EMPTY ; }
xen/include/xen/vpci.h +3 -0
@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */ uint64_t addr; + /* Guest view of the BAR: address and lower bits. */ + uint64_t guest_reg; uint64_t size; enum { VPCI_BAR_EMPTY,
Signature · 1 line
2.25.1
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Hi Oleksandr,
Quoted 12 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
Quoted 6 lines
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
In the generic code, bars for ioports are actually skipped (check code before in header.c, in case of ioports there is a continue) and no handler is registered for them. The consequence will be that a guest will access hardware when reading those BARs.
I think we should instead make sure that we intercept all accesses to BARs and return something empty for IOPORTS BARs.
Regards
Bertrand
Quoted 168 lines
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- updated commit message
- s/guest_addr/guest_reg
Since v3:
- squashed two patches: dynamic add/remove handlers and guest BAR handler implementation
- fix guest BAR read of the high part of a 64bit BAR (Roger)
- add error handling tovpci_assign_device
- s/dom%pd/%pd
- blank line before return
Since v2:
- remove unneeded ifdefs forCONFIG_HAS_VPCI_GUEST_SUPPORTas more code has been eliminated from being built on x86
Since v1:
- constify structpci_devwhere possible
- do not open codeis_system_domain()
- simplify some code3. simplify
- use gdprintk + error code instead of gprintk
- gate vpci_bar_{add|remove}_handlers withCONFIG_HAS_VPCI_GUEST_SUPPORT, so these do not get compiled for x86
- removed unneededis_system_domaincheck
- re-work guest read/write to be much simpler and do more work on write than read which is expected to be called more frequently
- removed one too obvious comment
---xen/drivers/vpci/header.c| 72 +++++++++++++++++++++++++++++++++++----xen/include/xen/vpci.h| 3 ++2 files changed, 69 insertions(+), 6 deletions(-)xen/drivers/vpci/header.c +66
-6@@ -433,6 +433,48 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write32 (pdev->sbdf, reg, val); } +static void guest_bar_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } +else + { + val &=PCI_BASE_ADDRESS_MEM_MASK ; + val |= bar->type ==VPCI_BAR_MEM32 ?PCI_BASE_ADDRESS_MEM_TYPE_32 + :PCI_BASE_ADDRESS_MEM_TYPE_64 ; + val |= bar->prefetchable ?PCI_BASE_ADDRESS_MEM_PREFETCH :0 ; + } + + bar->guest_reg &= ~(0xffffffffull << (hi ?32 :0 )); + bar->guest_reg |= (uint64_t )val << (hi ?32 :0 ); + + bar->guest_reg &= ~(bar->size -1 ) | ~PCI_BASE_ADDRESS_MEM_MASK ; +} + +static uint32_t guest_bar_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +const struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } + +return bar->guest_reg >> (hi ?32 :0 ); +} +static void rom_write (const struct pci_dev *pdev,unsigned int reg,uint32_t val,void *data) {@@ -481,6 +523,17 @@ static void rom_write(const struct pci_dev *pdev, unsigned int reg, rom->addr = val &PCI_ROM_ADDRESS_MASK ; } +static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +} +static int init_bars (struct pci_dev *pdev) {uint16_t cmd;@@ -489,6 +542,7 @@ static int init_bars(struct pci_dev *pdev)struct vpci_header *header = &pdev->vpci->header;struct vpci_bar *bars = header->bars;int rc; +bool is_hwdom =is_hardware_domain (pdev->domain);switch (pci_conf_read8 (pdev->sbdf,PCI_HEADER_TYPE ) &0x7f ) {@@ -528,8 +582,10 @@ static int init_bars(struct pci_dev *pdev)if ( i && bars[i -1 ].type ==VPCI_BAR_MEM64_LO ) { bars[i].type =VPCI_BAR_MEM64_HI ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg, -4 , &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -569,8 +625,10 @@ static int init_bars(struct pci_dev *pdev) bars[i].size = size; bars[i].prefetchable = val &PCI_BASE_ADDRESS_MEM_PREFETCH ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg,4 , - &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -590,8 +648,10 @@ static int init_bars(struct pci_dev *pdev) header->rom_enabled =pci_conf_read32 (pdev->sbdf, rom_reg) &PCI_ROM_ADDRESS_ENABLE ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, rom_write, rom_reg, -4 , rom); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_rom_read, + is_hwdom ? rom_write : guest_rom_write, + rom_reg,4 , rom);if ( rc ) rom->type =VPCI_BAR_EMPTY ; }xen/include/xen/vpci.h +3
-0@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */ uint64_t addr; + /* Guest view of the BAR: address and lower bits. */ + uint64_t guest_reg; uint64_t size; enum { VPCI_BAR_EMPTY,Signature · 1 line
2.25.1
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Hi, Bertrand!
Quoted 20 lines · Bertrand Marquis
Hi Oleksandr,
Quoted 12 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
Quoted 6 lines
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
Quoted 3 lines
In the generic code, bars for ioports are actually skipped (check code before in
header.c, in case of ioports there is a continue) and no handler is registered for them. The consequence will be that a guest will access hardware when reading those BARs.
Yes, this seems to be a valid point
Quoted 3 lines
I think we should instead make sure that we intercept all accesses to BARs and return something empty for IOPORTS BARs.
I would like to hear from Roger on what was the initial plan for that, so we are aligned between the different architectures, Arm and x86 here for now
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Hi, Bertrand!
Quoted 23 lines · Oleksandr Andrushchenko
Hi, Bertrand!
Quoted 20 lines · Bertrand Marquis
Hi Oleksandr,
Quoted 18 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
Quoted 4 lines
Quoted 3 lines
In the generic code, bars for ioports are actually skipped (check code before in
header.c, in case of ioports there is a continue) and no handler is registered for them. The consequence will be that a guest will access hardware when reading those BARs.Yes, this seems to be a valid point
So, with the approach we have developed these days we will ignore all writes and return ~0 for reads for all unhandled ops, e.g. those which do not have explicit register handlers employed. Thus, this case will fall into unhandled clause.
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 26 lines · Oleksandr Andrushchenko
Hi, Bertrand!
Quoted 23 lines · Oleksandr Andrushchenko
Hi, Bertrand!
Quoted 20 lines · Bertrand Marquis
Hi Oleksandr,
Quoted 18 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
In the generic code, bars for ioports are actually skipped (check code before in
header.c, in case of ioports there is a continue) and no handler is registered for them. The consequence will be that a guest will access hardware when reading those BARs.Yes, this seems to be a valid point
So, with the approach we have developed these days we will ignore all writes and return ~0 for reads for all unhandled ops, e.g. those which do not have explicit register handlers employed. Thus, this case will fall into unhandled clause.
Except that I guess BARs are special in that reads may not return ~0, or else the low bits carry a meaning we don't want to convey. Unused BARs need to be hard-wired to 0, I think.
Jan
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Hi, Jan!
Quoted 30 lines · Jan Beulich
Quoted 29 lines · Oleksandr Andrushchenko
Hi, Bertrand!
Quoted 26 lines · Oleksandr Andrushchenko
Hi, Bertrand!
Quoted 23 lines · Bertrand Marquis
Hi Oleksandr,
Quoted 12 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
Quoted 6 lines
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
In the generic code, bars for ioports are actually skipped (check code before in
header.c, in case of ioports there is a continue) and no handler is registered for them. The consequence will be that a guest will access hardware when reading those BARs.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Yes, this seems to be a valid point
So, with the approach we have developed these days we will ignore all writes and return ~0 for reads for all unhandled ops, e.g. those which do not have explicit register handlers employed. Thus, this case will fall into unhandled clause.
Except that I guess BARs are special in that reads may not return ~0, or else the low bits carry a meaning we don't want to convey. Unused BARs need to be hard-wired to 0, I think.
So, you mean we should have 2 sets of BAR handlers for guests:
1. normal emulation (these are implemented in this patch)
2. all other BARs: read 0/ignore write for all other BARs, including ROM, IO etc.
Is this what you mean?
Quoted 2 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 37 lines · Oleksandr Andrushchenko
Hi, Jan!
Quoted 34 lines · Jan Beulich
Quoted 33 lines · Oleksandr Andrushchenko
Hi, Bertrand!
Quoted 23 lines · Oleksandr Andrushchenko
Hi, Bertrand!
Quoted 20 lines · Bertrand Marquis
Hi Oleksandr,
Quoted 18 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
Quoted 4 lines
Quoted 3 lines
In the generic code, bars for ioports are actually skipped (check code before in
header.c, in case of ioports there is a continue) and no handler is registered for them. The consequence will be that a guest will access hardware when reading those BARs.Yes, this seems to be a valid point
So, with the approach we have developed these days we will ignore all writes and return ~0 for reads for all unhandled ops, e.g. those which do not have explicit register handlers employed. Thus, this case will fall into unhandled clause.
Quoted 8 lines
Quoted 3 lines
Except that I guess BARs are special in that reads may not return ~0, or else the low bits carry a meaning we don't want to convey. Unused BARs need to be hard-wired to 0, I think.
So, you mean we should have 2 sets of BAR handlers for guests:
1. normal emulation (these are implemented in this patch)
2. all other BARs: read 0/ignore write for all other BARs, including ROM, IO etc.Is this what you mean?
I think that's what we're going to need, yes.
Jan
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Oleksandr Andrushchenko
Hi, Jan!
Quoted 37 lines · Jan Beulich
Quoted 26 lines · Oleksandr Andrushchenko
Hi, Bertrand!
Quoted 23 lines · Oleksandr Andrushchenko
Hi, Bertrand!
Quoted 20 lines · Bertrand Marquis
Hi Oleksandr,
Quoted 18 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
In the generic code, bars for ioports are actually skipped (check code before in
header.c, in case of ioports there is a continue) and no handler is registered for them. The consequence will be that a guest will access hardware when reading those BARs.Yes, this seems to be a valid point
So, with the approach we have developed these days we will ignore all writes and return ~0 for reads for all unhandled ops, e.g. those which do not have explicit register handlers employed. Thus, this case will fall into unhandled clause.
Except that I guess BARs are special in that reads may not return ~0, or else the low bits carry a meaning we don't want to convey. Unused BARs need to be hard-wired to 0, I think.
Quoted 6 lines
Quoted 5 lines
So, you mean we should have 2 sets of BAR handlers for guests:
1. normal emulation (these are implemented in this patch)
2. all other BARs: read 0/ignore write for all other BARs, including ROM, IO etc.Is this what you mean?
I think that's what we're going to need, yes.
Ok, then I'll stuff that into this patch v6
Quoted 2 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
In reply to Oleksandr AndrushchenkoQuoted 105 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- updated commit message
- s/guest_addr/guest_reg
Since v3:
- squashed two patches: dynamic add/remove handlers and guest BAR handler implementation
- fix guest BAR read of the high part of a 64bit BAR (Roger)
- add error handling tovpci_assign_device
- s/dom%pd/%pd
- blank line before return
Since v2:
- remove unneeded ifdefs forCONFIG_HAS_VPCI_GUEST_SUPPORTas more code has been eliminated from being built on x86
Since v1:
- constify structpci_devwhere possible
- do not open codeis_system_domain()
- simplify some code3. simplify
- use gdprintk + error code instead of gprintk
- gate vpci_bar_{add|remove}_handlers withCONFIG_HAS_VPCI_GUEST_SUPPORT, so these do not get compiled for x86
- removed unneededis_system_domaincheck
- re-work guest read/write to be much simpler and do more work on write than read which is expected to be called more frequently
- removed one too obvious comment--- xen/drivers/vpci/header.c | 72 +++++++++++++++++++++++++++++++++++---- xen/include/xen/vpci.h | 3 ++ 2 files changed, 69 insertions(+), 6 deletions(-)xen/drivers/vpci/header.c +42
-0@@ -433,6 +433,48 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write32 (pdev->sbdf, reg, val); } +static void guest_bar_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } +else + { + val &=PCI_BASE_ADDRESS_MEM_MASK ; + val |= bar->type ==VPCI_BAR_MEM32 ?PCI_BASE_ADDRESS_MEM_TYPE_32 + :PCI_BASE_ADDRESS_MEM_TYPE_64 ; + val |= bar->prefetchable ?PCI_BASE_ADDRESS_MEM_PREFETCH :0 ; + } + + bar->guest_reg &= ~(0xffffffffull << (hi ?32 :0 )); + bar->guest_reg |= (uint64_t )val << (hi ?32 :0 ); + + bar->guest_reg &= ~(bar->size -1 ) | ~PCI_BASE_ADDRESS_MEM_MASK ; +} + +static uint32_t guest_bar_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +const struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } + +return bar->guest_reg >> (hi ?32 :0 ); +} +static void rom_write (const struct pci_dev *pdev,unsigned int reg,uint32_t val,void *data) {@@ -481,6 +523,17 @@ static void rom_write(const struct pci_dev *pdev, unsigned int reg, rom->addr = val &PCI_ROM_ADDRESS_MASK ; }
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}
There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Quoted 39 lines
xen/drivers/vpci/header.c +10
-4+static int init_bars (struct pci_dev *pdev) {uint16_t cmd;@@ -489,6 +542,7 @@ static int init_bars(struct pci_dev *pdev)struct vpci_header *header = &pdev->vpci->header;struct vpci_bar *bars = header->bars;int rc; +bool is_hwdom =is_hardware_domain (pdev->domain);switch (pci_conf_read8 (pdev->sbdf,PCI_HEADER_TYPE ) &0x7f ) {@@ -528,8 +582,10 @@ static int init_bars(struct pci_dev *pdev)if ( i && bars[i -1 ].type ==VPCI_BAR_MEM64_LO ) { bars[i].type =VPCI_BAR_MEM64_HI ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg, -4 , &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -569,8 +625,10 @@ static int init_bars(struct pci_dev *pdev) bars[i].size = size; bars[i].prefetchable = val &PCI_BASE_ADDRESS_MEM_PREFETCH ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg,4 , - &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 654
Quoted 9 lines
xen/drivers/vpci/header.c +4
-2@@ -590,8 +648,10 @@ static int init_bars(struct pci_dev *pdev) header->rom_enabled =pci_conf_read32 (pdev->sbdf, rom_reg) &PCI_ROM_ADDRESS_ENABLE ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, rom_write, rom_reg, -4 , rom); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_rom_read, + is_hwdom ? rom_write : guest_rom_write, + rom_reg,4 , rom);
This whole call should be made conditional to is_hwdom, as said above there's no need for the guest_rom handlers.
Likewise I assume you expect IO BARs to simply return ~0 and drop writes, as there's no explicit handler added for those?
xen/include/xen/vpci.h struct vpci { · line 71
Quoted 12 lines
xen/drivers/vpci/header.c +0
-0if ( rc ) rom->type =VPCI_BAR_EMPTY ; }xen/include/xen/vpci.h +1
-0@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */
No, that's not the physical view, it's the physical (host) address.
xen/include/xen/vpci.h struct vpci { · line 74
Quoted 3 lines
xen/include/xen/vpci.h +2
-0uint64_t addr; +/* Guest view of the BAR: address and lower bits. */ +uint64_t guest_reg;
I continue to think it would be clearer if you store the guest address here (gaddr, without the low bits) and add those in guest_bar_read based on bar->{type,prefetchable}. Then it would be equivalent to the existing 'addr' field.
I wonder whether we need to protect the added code with CONFIG_HAS_VPCI_GUEST_SUPPORT, this would effectively be dead code otherwise. Long term I don't think we wish to differentiate between dom0 and domU vPCI support at build time, so I'm unsure whether it's helpful to pollute the code with CONFIG_HAS_VPCI_GUEST_SUPPORT when the plan is to remove those long term.
Thanks, Roger.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Hi, Roger!
Quoted 106 lines · Roger Pau Monné
Quoted 105 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- updated commit message
- s/guest_addr/guest_reg
Since v3:
- squashed two patches: dynamic add/remove handlers and guest BAR handler implementation
- fix guest BAR read of the high part of a 64bit BAR (Roger)
- add error handling tovpci_assign_device
- s/dom%pd/%pd
- blank line before return
Since v2:
- remove unneeded ifdefs forCONFIG_HAS_VPCI_GUEST_SUPPORTas more code has been eliminated from being built on x86
Since v1:
- constify structpci_devwhere possible
- do not open codeis_system_domain()
- simplify some code3. simplify
- use gdprintk + error code instead of gprintk
- gate vpci_bar_{add|remove}_handlers withCONFIG_HAS_VPCI_GUEST_SUPPORT, so these do not get compiled for x86
- removed unneededis_system_domaincheck
- re-work guest read/write to be much simpler and do more work on write than read which is expected to be called more frequently
- removed one too obvious comment--- xen/drivers/vpci/header.c | 72 +++++++++++++++++++++++++++++++++++---- xen/include/xen/vpci.h | 3 ++ 2 files changed, 69 insertions(+), 6 deletions(-)xen/drivers/vpci/header.c +42
-0@@ -433,6 +433,48 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write32 (pdev->sbdf, reg, val); } +static void guest_bar_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } +else + { + val &=PCI_BASE_ADDRESS_MEM_MASK ; + val |= bar->type ==VPCI_BAR_MEM32 ?PCI_BASE_ADDRESS_MEM_TYPE_32 + :PCI_BASE_ADDRESS_MEM_TYPE_64 ; + val |= bar->prefetchable ?PCI_BASE_ADDRESS_MEM_PREFETCH :0 ; + } + + bar->guest_reg &= ~(0xffffffffull << (hi ?32 :0 )); + bar->guest_reg |= (uint64_t )val << (hi ?32 :0 ); + + bar->guest_reg &= ~(bar->size -1 ) | ~PCI_BASE_ADDRESS_MEM_MASK ; +} + +static uint32_t guest_bar_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +const struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } + +return bar->guest_reg >> (hi ?32 :0 ); +} +static void rom_write (const struct pci_dev *pdev,unsigned int reg,uint32_t val,void *data) {@@ -481,6 +523,17 @@ static void rom_write(const struct pci_dev *pdev, unsigned int reg, rom->addr = val &PCI_ROM_ADDRESS_MASK ; }
Quoted 13 lines
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in vpci_read that we end up reading ~0 if no handler exists (which is what I do here with guest_rom_read). But I am not that sure about the dropped writes:
void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_t data)
{
unsigned int data_offset = 0;
[snip]
if ( data_offset < size )
/* Tailing gap, write the remaining. */ vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));
so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g. guest_rom_write here?
Quoted 42 lines
Quoted 42 lines
xen/drivers/vpci/header.c +10
-4+static int init_bars (struct pci_dev *pdev) {uint16_t cmd;@@ -489,6 +542,7 @@ static int init_bars(struct pci_dev *pdev)struct vpci_header *header = &pdev->vpci->header;struct vpci_bar *bars = header->bars;int rc; +bool is_hwdom =is_hardware_domain (pdev->domain);switch (pci_conf_read8 (pdev->sbdf,PCI_HEADER_TYPE ) &0x7f ) {@@ -528,8 +582,10 @@ static int init_bars(struct pci_dev *pdev)if ( i && bars[i -1 ].type ==VPCI_BAR_MEM64_LO ) { bars[i].type =VPCI_BAR_MEM64_HI ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg, -4 , &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -569,8 +625,10 @@ static int init_bars(struct pci_dev *pdev) bars[i].size = size; bars[i].prefetchable = val &PCI_BASE_ADDRESS_MEM_PREFETCH ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg,4 , - &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -590,8 +648,10 @@ static int init_bars(struct pci_dev *pdev) header->rom_enabled =pci_conf_read32 (pdev->sbdf, rom_reg) &PCI_ROM_ADDRESS_ENABLE ;
Quoted 8 lines
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 654
Quoted 6 lines
xen/drivers/vpci/header.c +4
-2- rc =vpci_add_register (pdev->vpci, vpci_hw_read32, rom_write, rom_reg, -4 , rom); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_rom_read, + is_hwdom ? rom_write : guest_rom_write, + rom_reg,4 , rom);This whole call should be made conditional to
is_hwdom, as said above there's no need for theguest_romhandlers.
Yes, if writes are indeed dropped, please see question above
Quoted 3 lines
Likewise I assume you expect IO BARs to simply return ~0 and drop writes, as there's no explicit handler added for those?
Yes, but that was not my intention: I simply didn't handle IO BARs and now we do need that handling: either with the default behavior for the unhandled read/write (drop writes, read ~0) or by introducing the handlers. I hope we can rely on the "unhandled read/write" and get what we want
Quoted 14 lines
xen/include/xen/vpci.h struct vpci { · line 71
Quoted 13 lines
xen/drivers/vpci/header.c +0
-0if ( rc ) rom->type =VPCI_BAR_EMPTY ; }xen/include/xen/vpci.h +1
-0@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */No, that's not the physical view, it's the physical (host) address.
Ok
Quoted 8 lines
xen/include/xen/vpci.h struct vpci { · line 74
Quoted 4 lines
xen/include/xen/vpci.h +2
-0uint64_t addr; +/* Guest view of the BAR: address and lower bits. */ +uint64_t guest_reg;I continue to think it would be clearer if you store the guest address here (gaddr, without the low bits) and add those in
guest_bar_readbased on bar->{type,prefetchable}. Then it would be equivalent to the existing 'addr' field.
Ok, I'll re-work the code with this approach in mind: s/guest_reg/gaddr + required code to handle that
Quoted 7 lines
I wonder whether we need to protect the added code with
CONFIG_HAS_VPCI_GUEST_SUPPORT, this would effectively be dead code otherwise. Long term I don't think we wish to differentiate between dom0 and domU vPCI support at build time, so I'm unsure whether it's helpful to pollute the code withCONFIG_HAS_VPCI_GUEST_SUPPORTwhen the plan is to remove those long term.
I would have it without CONFIG_HAS_VPCI_GUEST_SUPPORT if you don't mind
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 33 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 14 lines · Roger Pau Monné
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 11 lines
xen/drivers/vpci/header.c +10
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in
vpci_readthat we end up reading ~0 if no handler exists (which is what I do here withguest_rom_read). But I am not that sure about the dropped writes:void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_tdata)
{
unsigned intdata_offset= 0;[snip]
if (
data_offset< size )
/* Tailing gap, write the remaining. */vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));
Quoted 4 lines
so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g.
guest_rom_writehere?
Hm, but the same applies to the reads as well... And this is no surprise, as for the guests I can see that it accesses all the configuration space registers that I don't handle. Without that I would have guests unable to properly setup a PCI device being passed through... And this is why I have a big TODO in this series describing unhandled registers. So, it seems that I do need to provide those handlers which I need to drop writes and return ~0 on reads.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 38 lines · Oleksandr Andrushchenko
Quoted 33 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 14 lines · Roger Pau Monné
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 11 lines
xen/drivers/vpci/header.c +10
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in
vpci_readthat we end up reading ~0 if no handler exists (which is what I do here withguest_rom_read). But I am not that sure about the dropped writes:void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_tdata)
{
unsigned intdata_offset= 0;[snip]
if (
data_offset< size )
/* Tailing gap, write the remaining. */vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));Quoted 4 lines
so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g.
guest_rom_writehere?
Quoted 7 lines
Hm, but the same applies to the reads as well... And this is no surprise, as for the guests I can see that it accesses all the configuration space registers that I don't handle. Without that I would have guests unable to properly setup a PCI device being passed through... And this is why I have a big TODO in this series describing unhandled registers. So, it seems that I do need to provide those handlers which I need to drop writes and return ~0 on reads.
It feels like we had been there before: For your initial purposes it may be fine to do as you suggest, but any such patches should carry RFC tags or alike to indicate they're not considered ready. Once you're aiming for things to go in, I think there's no good way around white-listing what guests may access. You may know that we've been bitten by starting out with black-listing in the past, first and foremost with x86'es MSRs.
Jan
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Hi, Jan!
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Oleksandr Andrushchenko
Quoted 36 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 5 lines · Roger Pau Monné
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} +Quoted 8 lines
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in
vpci_readthat we end up reading ~0 if no handler exists (which is what I do here withguest_rom_read). But I am not that sure about the dropped writes:void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_tdata){ unsigned int data_offset = 0;[snip]
if (
data_offset< size )
/* Tailing gap, write the remaining. */vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g.
guest_rom_writehere?
Quoted 7 lines
Quoted 7 lines
Hm, but the same applies to the reads as well... And this is no surprise, as for the guests I can see that it accesses all the configuration space registers that I don't handle. Without that I would have guests unable to properly setup a PCI device being passed through... And this is why I have a big TODO in this series describing unhandled registers. So, it seems that I do need to provide those handlers which I need to drop writes and return ~0 on reads.
Replying to myself: it is still possible to have vpci_ignored_{read|write} to handle defaults if, when vpci_add_register is called, the handler provided is NULL
Quoted 6 lines
It feels like we had been there before: For your initial purposes it may be fine to do as you suggest, but any such patches should carry RFC tags or alike to indicate they're not considered ready. Once you're aiming for things to go in, I think there's no good way around white-listing what guests may access. You may know that we've been bitten by starting out with black-listing in the past, first and foremost with x86'es MSRs.
I already have a big TODO patch describing the issue. Do you want it to have a list of handlers that we support as of now? What sort of while/black list would you expect?
I do understand that we do need proper handling for all the PCI registers and capabilities long term, but this can't be done at the moment when we have nothing working at all. Requesting proper handling now will turn this series into a huge amount of code and undefined time frame.
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 57 lines · Oleksandr Andrushchenko
Hi, Jan!
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Oleksandr Andrushchenko
Quoted 36 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 5 lines · Roger Pau Monné
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} +Quoted 8 lines
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in
vpci_readthat we end up reading ~0 if no handler exists (which is what I do here withguest_rom_read). But I am not that sure about the dropped writes:void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_tdata){ unsigned int data_offset = 0;[snip]
if (
data_offset< size )
/* Tailing gap, write the remaining. */vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g.
guest_rom_writehere?Quoted 7 lines
Quoted 7 lines
Hm, but the same applies to the reads as well... And this is no surprise, as for the guests I can see that it accesses all the configuration space registers that I don't handle. Without that I would have guests unable to properly setup a PCI device being passed through... And this is why I have a big TODO in this series describing unhandled registers. So, it seems that I do need to provide those handlers which I need to drop writes and return ~0 on reads.
Replying to myself: it is still possible to have vpci_ignored_{read|write} to handle defaults if, when
vpci_add_registeris called, the handler provided is NULLQuoted 6 lines
It feels like we had been there before: For your initial purposes it may be fine to do as you suggest, but any such patches should carry RFC tags or alike to indicate they're not considered ready. Once you're aiming for things to go in, I think there's no good way around white-listing what guests may access. You may know that we've been bitten by starting out with black-listing in the past, first and foremost with x86'es MSRs.
Quoted 7 lines
I already have a big TODO patch describing the issue. Do you want it to have a list of handlers that we support as of now? What sort of while/black list would you expect?
I do understand that we do need proper handling for all the PCI registers and capabilities long term, but this can't be done at the moment when we have nothing working at all. Requesting proper handling now will turn this series into a huge amount of code and undefined time frame.
We should at least make sure the code added now doesn't need to be changed in the future when the default is switched. If you don't want to switch the default handling for domUs to ignore writes and return ~0 from reads to unhandled registers right now you should keep the patches that add the ignore handlers to the end of the series and mark them as 'HACK' or some such in order to notice they are just used for testing purposes.
Thanks, Roger.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 65 lines · Roger Pau Monné
Quoted 57 lines · Oleksandr Andrushchenko
Hi, Jan!
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Oleksandr Andrushchenko
Quoted 36 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 5 lines · Roger Pau Monné
Quoted 5 lines
(quoted diff) +5
-0+static void guest_rom_write(const struct pci_dev *pdev, unsigned int reg, + uint32_t val, void *data) +{ +} +Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+static uint32_t guest_rom_read(const struct pci_dev *pdev, unsigned int reg, + void *data) +{ + return 0xffffffff; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in
vpci_readthat we end up reading ~0 if no handler exists (which is what I do here withguest_rom_read). But I am not that sure about the dropped writes:void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_t data) { unsigned int data_offset = 0;[snip]
if (
data_offset< size )
/* Tailing gap, write the remaining. */vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g.
guest_rom_writehere?Quoted 7 lines
Quoted 7 lines
Hm, but the same applies to the reads as well... And this is no surprise, as for the guests I can see that it accesses all the configuration space registers that I don't handle. Without that I would have guests unable to properly setup a PCI device being passed through... And this is why I have a big TODO in this series describing unhandled registers. So, it seems that I do need to provide those handlers which I need to drop writes and return ~0 on reads.
Replying to myself: it is still possible to have vpci_ignored_{read|write} to handle defaults if, when
vpci_add_registeris called, the handler provided is NULLQuoted 6 lines
It feels like we had been there before: For your initial purposes it may be fine to do as you suggest, but any such patches should carry RFC tags or alike to indicate they're not considered ready. Once you're aiming for things to go in, I think there's no good way around white-listing what guests may access. You may know that we've been bitten by starting out with black-listing in the past, first and foremost with x86'es MSRs.
Quoted 7 lines
I already have a big TODO patch describing the issue. Do you want it to have a list of handlers that we support as of now? What sort of while/black list would you expect?
I do understand that we do need proper handling for all the PCI registers and capabilities long term, but this can't be done at the moment when we have nothing working at all. Requesting proper handling now will turn this series into a huge amount of code and undefined time frame.
Quoted 7 lines
We should at least make sure the code added now doesn't need to be changed in the future when the default is switched. If you don't want to switch the default handling for domUs to ignore writes and return ~0 from reads to unhandled registers right now you should keep the patches that add the ignore handlers to the end of the series and mark them as 'HACK' or some such in order to notice they are just used for testing purposes.
Or for all the registers that I do want the writes to be rejected and reads return ~0 I can pass NULL while calling vpci_add_register, so the following works:
int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler,
vpci_write_t *write_handler, unsigned int offset, unsigned int size, void *data)
{
[snip]
r->read = read_handler ?: vpci_ignored_read;
r->write = write_handler ?: vpci_ignored_write;
which does what we want.
Quoted 1 line
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
In reply to Oleksandr AndrushchenkoQuoted 38 lines · Oleksandr Andrushchenko
Quoted 33 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 14 lines · Roger Pau Monné
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 11 lines
xen/drivers/vpci/header.c +10
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in
vpci_readthat we end up reading ~0 if no handler exists (which is what I do here withguest_rom_read). But I am not that sure about the dropped writes:void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_tdata)
{
unsigned intdata_offset= 0;[snip]
if (
data_offset< size )
/* Tailing gap, write the remaining. */vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));Quoted 4 lines
so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g.
guest_rom_writehere?
Quoted 7 lines
Hm, but the same applies to the reads as well... And this is no surprise, as for the guests I can see that it accesses all the configuration space registers that I don't handle. Without that I would have guests unable to properly setup a PCI device being passed through... And this is why I have a big TODO in this series describing unhandled registers. So, it seems that I do need to provide those handlers which I need to drop writes and return ~0 on reads.
Right (see my previous reply to this comment). I think it would be easier (and cleaner) if you switched the default behavior regarding unhandled register access for domUs at the start of the series (drop writes, reads returns ~0), and then you won't need to add all those dummy handler to drop writes and return ~0 for reads.
It's going to be more work initially as you would need to support passthrough of more registers, but it's the right approach that we need implementation wise.
Thanks, Roger.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 51 lines · Roger Pau Monné
Quoted 37 lines · Oleksandr Andrushchenko
Quoted 32 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 5 lines · Roger Pau Monné
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} +Quoted 8 lines
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in
vpci_readthat we end up reading ~0 if no handler exists (which is what I do here withguest_rom_read). But I am not that sure about the dropped writes:void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_tdata){ unsigned int data_offset = 0;[snip]
if (
data_offset< size )
/* Tailing gap, write the remaining. */vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));Quoted 4 lines
so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g.
guest_rom_writehere?Quoted 7 lines
Hm, but the same applies to the reads as well... And this is no surprise, as for the guests I can see that it accesses all the configuration space registers that I don't handle. Without that I would have guests unable to properly setup a PCI device being passed through... And this is why I have a big TODO in this series describing unhandled registers. So, it seems that I do need to provide those handlers which I need to drop writes and return ~0 on reads.
Right (see my previous reply to this comment). I think it would be easier (and cleaner) if you switched the default behavior regarding unhandled register access for domUs at the start of the series (drop writes, reads returns ~0), and then you won't need to add all those dummy handler to drop writes and return ~0 for reads.
Quoted 3 lines
It's going to be more work initially as you would need to support passthrough of more registers, but it's the right approach that we need implementation wise.
While I agree in general, this effectively means that I'll need to provide handling for all PCIe registers and capabilities from the very start. Otherwise no guest be able to properly initialize a PCI device without that. Of course, we may want starting from stubs instead of proper emulation, which will direct the access to real HW and later on we add proper emulation. But, again, this is going to be a rather big piece of code where we need to explicitly handle every possible capability.
At the moment we are not going to claim that vPCI provides all means to pass through a PCI device safely with this respect and this is why the feature itself won't even be a tech preview yet. For that reason I think we can still have implemented only crucial set of handlers and still allow the rest to be read/write directly without emulation.
Another question is what needs to be done for vendor specific capabilities? How these are going to be emulated?
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 54 lines · Oleksandr Andrushchenko
Quoted 51 lines · Roger Pau Monné
Quoted 37 lines · Oleksandr Andrushchenko
Quoted 32 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 5 lines · Roger Pau Monné
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} +Quoted 8 lines
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in
vpci_readthat we end up reading ~0 if no handler exists (which is what I do here withguest_rom_read). But I am not that sure about the dropped writes:void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_tdata){ unsigned int data_offset = 0;[snip]
if (
data_offset< size )
/* Tailing gap, write the remaining. */vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));Quoted 4 lines
so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g.
guest_rom_writehere?Quoted 7 lines
Hm, but the same applies to the reads as well... And this is no surprise, as for the guests I can see that it accesses all the configuration space registers that I don't handle. Without that I would have guests unable to properly setup a PCI device being passed through... And this is why I have a big TODO in this series describing unhandled registers. So, it seems that I do need to provide those handlers which I need to drop writes and return ~0 on reads.
Right (see my previous reply to this comment). I think it would be easier (and cleaner) if you switched the default behavior regarding unhandled register access for domUs at the start of the series (drop writes, reads returns ~0), and then you won't need to add all those dummy handler to drop writes and return ~0 for reads.
Quoted 5 lines
Quoted 3 lines
It's going to be more work initially as you would need to support passthrough of more registers, but it's the right approach that we need implementation wise.
While I agree in general, this effectively means that I'll need to provide handling for all PCIe registers and capabilities from the very start.
Well, we can only offer handling of the header and the MSI and MSI-X capabilities right now, because that's all vPCI currently knows about.
Quoted 11 lines
Otherwise no guest be able to properly initialize a PCI device without that. Of course, we may want starting from stubs instead of proper emulation, which will direct the access to real HW and later on we add proper emulation. But, again, this is going to be a rather big piece of code where we need to explicitly handle every possible capability.
At the moment we are not going to claim that vPCI provides all means to pass through a PCI device safely with this respect and this is why the feature itself won't even be a tech preview yet. For that reason I think we can still have implemented only crucial set of handlers and still allow the rest to be read/write directly without emulation.
See my other reply, you can probably move the special handlers into a separate patch at the end of the series in order to test the functionality without adding code that will need to be removed when the defaults for domUs are changed.
Quoted 2 lines
Another question is what needs to be done for vendor specific capabilities? How these are going to be emulated?
I think you will need some kind of permissive mode in order to allow a guest to access those, as they shouldn't be exposed by default.
Thanks, Roger.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
In reply to Oleksandr AndrushchenkoQuoted 10 lines · Oleksandr Andrushchenko
Quoted 9 lines · Roger Pau Monné
Right (see my previous reply to this comment). I think it would be easier (and cleaner) if you switched the default behavior regarding unhandled register access for domUs at the start of the series (drop writes, reads returns ~0), and then you won't need to add all those dummy handler to drop writes and return ~0 for reads.
It's going to be more work initially as you would need to support passthrough of more registers, but it's the right approach that we need implementation wise.
Quoted 7 lines
While I agree in general, this effectively means that I'll need to provide handling for all PCIe registers and capabilities from the very start. Otherwise no guest be able to properly initialize a PCI device without that. Of course, we may want starting from stubs instead of proper emulation, which will direct the access to real HW and later on we add proper emulation. But, again, this is going to be a rather big piece of code where we need to explicitly handle every possible capability.
Since the two sub-threads are now about exactly the same topic, I'm answering here instead of there.
No, you are not going to need to emulate all possible capabilities. We (or really qemu) don't do this on x86 either. Certain capabilities may be a must, but not everything. There are also device specific registers not covered by any capability structures - what to do with those is even more of a question.
Furthermore for some of the fields justification why access to the raw hardware value is fine is going to be easy: r/o fields like vendor and device ID, for example. But every bit you allow direct access to needs to come with justification.
Quoted 5 lines
At the moment we are not going to claim that vPCI provides all means to pass through a PCI device safely with this respect and this is why the feature itself won't even be a tech preview yet. For that reason I think we can still have implemented only crucial set of handlers and still allow the rest to be read/write directly without emulation.
I think you need to separate what you need for development from what goes upstream: For dev purposes you can very well invert the policy from white- to black-listing. But if we accepted the latter into the main tree, the risk would be there that something gets missed at the time where the permission model gets changed around.
You could even have a non-default mode operating the way you want it (along the lines of pciback's permissive mode), allowing you to get away without needing to carry private patches. Things may also initially only work in that mode. But the default should be a mode which is secure (and which perhaps initially offers only very limited functionality).
Quoted 2 lines
Another question is what needs to be done for vendor specific capabilities? How these are going to be emulated?
By vendor specific code, I'm afraid. Assuming these capabilities really need exposing in the first place.
Jan
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 43 lines · Jan Beulich
Quoted 10 lines · Oleksandr Andrushchenko
Quoted 9 lines · Roger Pau Monné
Right (see my previous reply to this comment). I think it would be easier (and cleaner) if you switched the default behavior regarding unhandled register access for domUs at the start of the series (drop writes, reads returns ~0), and then you won't need to add all those dummy handler to drop writes and return ~0 for reads.
It's going to be more work initially as you would need to support passthrough of more registers, but it's the right approach that we need implementation wise.
Quoted 7 lines
While I agree in general, this effectively means that I'll need to provide handling for all PCIe registers and capabilities from the very start. Otherwise no guest be able to properly initialize a PCI device without that. Of course, we may want starting from stubs instead of proper emulation, which will direct the access to real HW and later on we add proper emulation. But, again, this is going to be a rather big piece of code where we need to explicitly handle every possible capability.
Since the two sub-threads are now about exactly the same topic, I'm answering here instead of there.
No, you are not going to need to emulate all possible capabilities. We (or really qemu) don't do this on x86 either. Certain capabilities may be a must, but not everything. There are also device specific registers not covered by any capability structures - what to do with those is even more of a question.
Furthermore for some of the fields justification why access to the raw hardware value is fine is going to be easy: r/o fields like vendor and device ID, for example. But every bit you allow direct access to needs to come with justification.
Quoted 5 lines
At the moment we are not going to claim that vPCI provides all means to pass through a PCI device safely with this respect and this is why the feature itself won't even be a tech preview yet. For that reason I think we can still have implemented only crucial set of handlers and still allow the rest to be read/write directly without emulation.
I think you need to separate what you need for development from what goes upstream: For dev purposes you can very well invert the policy from white- to black-listing. But if we accepted the latter into the main tree, the risk would be there that something gets missed at the time where the permission model gets changed around.
Quoted 6 lines
You could even have a non-default mode operating the way you want it (along the lines of pciback's permissive mode), allowing you to get away without needing to carry private patches. Things may also initially only work in that mode. But the default should be a mode which is secure (and which perhaps initially offers only very limited functionality).
Ok, so to make it clear:
1. We do not allow unhandled access for guests: for that I will create a dedicated patch which will implement such restrictions. Something like the below (for both vPCI read and write):
xen/drivers/vpci/vpci.c +7 -0
@@ -347,6 +347,7 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) const struct vpci_register *r; unsigned int data_offset = 0; uint32_t data = ~(uint32_t)0; + bool handled = false; if ( !size ) {
@@ -405,6 +406,8 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) if ( cmp > 0 ) continue; + handled = true; /* Found the handler for this access. */ + if ( emu.offset < r->offset ) { /* Heading gap, read partial content from hardware. */
@@ -432,6 +435,10 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) } spin_unlock(&pdev->vpci_lock); + /* All unhandled guest requests return all 1's. */ + if ( !is_hardware_domain(d) && !handled ) + return ~(uint32_t)0; + if ( data_offset < size ) { /* Tailing gap, read the remaining. */
@Roger: does the above work for you?
2. For the time being, while only a reduced set of registers is emulated, everyone who wants to test vPCI for guests should either create their own patch with those handlers implemented or overcome that in any other suitable way, e.g. with a hack that removes HW register access protection or by any other means.
Quoted 5 lines
Quoted 3 lines
Another question is what needs to be done for vendor specific capabilities? How these are going to be emulated?
By vendor specific code, I'm afraid. Assuming these capabilities really need exposing in the first place.
I have a feeling this is not going to happen...
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 79 lines · Oleksandr Andrushchenko
Quoted 43 lines · Jan Beulich
Quoted 10 lines · Oleksandr Andrushchenko
Quoted 9 lines · Roger Pau Monné
Right (see my previous reply to this comment). I think it would be easier (and cleaner) if you switched the default behavior regarding unhandled register access for domUs at the start of the series (drop writes, reads returns ~0), and then you won't need to add all those dummy handler to drop writes and return ~0 for reads.
It's going to be more work initially as you would need to support passthrough of more registers, but it's the right approach that we need implementation wise.
Quoted 7 lines
While I agree in general, this effectively means that I'll need to provide handling for all PCIe registers and capabilities from the very start. Otherwise no guest be able to properly initialize a PCI device without that. Of course, we may want starting from stubs instead of proper emulation, which will direct the access to real HW and later on we add proper emulation. But, again, this is going to be a rather big piece of code where we need to explicitly handle every possible capability.
Since the two sub-threads are now about exactly the same topic, I'm answering here instead of there.
No, you are not going to need to emulate all possible capabilities. We (or really qemu) don't do this on x86 either. Certain capabilities may be a must, but not everything. There are also device specific registers not covered by any capability structures - what to do with those is even more of a question.
Furthermore for some of the fields justification why access to the raw hardware value is fine is going to be easy: r/o fields like vendor and device ID, for example. But every bit you allow direct access to needs to come with justification.
Quoted 5 lines
At the moment we are not going to claim that vPCI provides all means to pass through a PCI device safely with this respect and this is why the feature itself won't even be a tech preview yet. For that reason I think we can still have implemented only crucial set of handlers and still allow the rest to be read/write directly without emulation.
I think you need to separate what you need for development from what goes upstream: For dev purposes you can very well invert the policy from white- to black-listing. But if we accepted the latter into the main tree, the risk would be there that something gets missed at the time where the permission model gets changed around.
Quoted 6 lines
You could even have a non-default mode operating the way you want it (along the lines of pciback's permissive mode), allowing you to get away without needing to carry private patches. Things may also initially only work in that mode. But the default should be a mode which is secure (and which perhaps initially offers only very limited functionality).
Ok, so to make it clear:
1. We do not allow unhandled access for guests: for that I will create a dedicated patch which will implement such restrictions. Something like the below (for both vPCI read and write):xen/drivers/vpci/vpci.c +3
-0@@ -347,6 +347,7 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) const struct vpci_register *r; unsigned int data_offset = 0; uint32_t data = ~(uint32_t)0; + bool handled = false; if ( !size ) {@@ -405,6 +406,8 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) if ( cmp > 0 ) continue; + handled = true; /* Found the handler for this access. */ + if ( emu.offset < r->offset ) { /* Heading gap, read partial content from hardware. */
xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 444
Quoted 10 lines
xen/drivers/vpci/vpci.c +4
-0@@ -432,6 +435,10 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) } spin_unlock(&pdev->vpci_lock); + /* All unhandled guest requests return all 1's. */ + if ( !is_hardware_domain(d) && !handled ) + return ~(uint32_t)0; + if ( data_offset < size ) { /* Tailing gap, read the remaining. */
Except that like for the "tailing gap" you also need to avoid the "heading gap" ending up in a read of the underlying hardware register. Effectively you want to deal properly with all vpci_read_hw() invocations (including the one when no pdev was found, which for a DomU may simply mean domain_crash()).
Jan
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 82 lines · Jan Beulich
Quoted 81 lines · Oleksandr Andrushchenko
Quoted 43 lines · Jan Beulich
Quoted 10 lines · Oleksandr Andrushchenko
Quoted 9 lines · Roger Pau Monné
Right (see my previous reply to this comment). I think it would be easier (and cleaner) if you switched the default behavior regarding unhandled register access for domUs at the start of the series (drop writes, reads returns ~0), and then you won't need to add all those dummy handler to drop writes and return ~0 for reads.
It's going to be more work initially as you would need to support passthrough of more registers, but it's the right approach that we need implementation wise.
Quoted 7 lines
While I agree in general, this effectively means that I'll need to provide handling for all PCIe registers and capabilities from the very start. Otherwise no guest be able to properly initialize a PCI device without that. Of course, we may want starting from stubs instead of proper emulation, which will direct the access to real HW and later on we add proper emulation. But, again, this is going to be a rather big piece of code where we need to explicitly handle every possible capability.
Since the two sub-threads are now about exactly the same topic, I'm answering here instead of there.
No, you are not going to need to emulate all possible capabilities. We (or really qemu) don't do this on x86 either. Certain capabilities may be a must, but not everything. There are also device specific registers not covered by any capability structures - what to do with those is even more of a question.
Furthermore for some of the fields justification why access to the raw hardware value is fine is going to be easy: r/o fields like vendor and device ID, for example. But every bit you allow direct access to needs to come with justification.
Quoted 5 lines
At the moment we are not going to claim that vPCI provides all means to pass through a PCI device safely with this respect and this is why the feature itself won't even be a tech preview yet. For that reason I think we can still have implemented only crucial set of handlers and still allow the rest to be read/write directly without emulation.
I think you need to separate what you need for development from what goes upstream: For dev purposes you can very well invert the policy from white- to black-listing. But if we accepted the latter into the main tree, the risk would be there that something gets missed at the time where the permission model gets changed around.
Quoted 6 lines
You could even have a non-default mode operating the way you want it (along the lines of pciback's permissive mode), allowing you to get away without needing to carry private patches. Things may also initially only work in that mode. But the default should be a mode which is secure (and which perhaps initially offers only very limited functionality).
Ok, so to make it clear:
1. We do not allow unhandled access for guests: for that I will create a dedicated patch which will implement such restrictions. Something like the below (for both vPCI read and write):xen/drivers/vpci/vpci.c +3
-0@@ -347,6 +347,7 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) const struct vpci_register *r; unsigned int data_offset = 0; uint32_t data = ~(uint32_t)0; + bool handled = false; if ( !size ) {@@ -405,6 +406,8 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) if ( cmp > 0 ) continue; + handled = true; /* Found the handler for this access. */ + if ( emu.offset < r->offset ) { /* Heading gap, read partial content from hardware. */@@ -432,6 +435,10 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) } spin_unlock(&pdev->vpci_lock);
Quoted 12 lines
xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 444
Quoted 7 lines
xen/drivers/vpci/vpci.c +4
-0+ /* All unhandled guest requests return all 1's. */ + if ( !is_hardware_domain(d) && !handled ) + return ~(uint32_t)0; + if ( data_offset < size ) { /* Tailing gap, read the remaining. */Except that like for the "tailing gap" you also need to avoid the "heading gap" ending up in a read of the underlying hardware register. Effectively you want to deal properly with all
vpci_read_hw()invocations (including the one when no pdev was found, which for a DomU may simply meandomain_crash()).
Yes. And with the above patch I can now remove the "TODO patch" then? Because it is saying that we allow access to the registers, but it is not safe. And now, if we disable that access, then TODO should be about the need to implement emulation for all the registers which are not yet handled which is obvious.
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 94 lines · Oleksandr Andrushchenko
Quoted 81 lines · Jan Beulich
Quoted 80 lines · Oleksandr Andrushchenko
Quoted 43 lines · Jan Beulich
Quoted 10 lines · Oleksandr Andrushchenko
Quoted 9 lines · Roger Pau Monné
Right (see my previous reply to this comment). I think it would be easier (and cleaner) if you switched the default behavior regarding unhandled register access for domUs at the start of the series (drop writes, reads returns ~0), and then you won't need to add all those dummy handler to drop writes and return ~0 for reads.
It's going to be more work initially as you would need to support passthrough of more registers, but it's the right approach that we need implementation wise.
Quoted 7 lines
While I agree in general, this effectively means that I'll need to provide handling for all PCIe registers and capabilities from the very start. Otherwise no guest be able to properly initialize a PCI device without that. Of course, we may want starting from stubs instead of proper emulation, which will direct the access to real HW and later on we add proper emulation. But, again, this is going to be a rather big piece of code where we need to explicitly handle every possible capability.
Since the two sub-threads are now about exactly the same topic, I'm answering here instead of there.
No, you are not going to need to emulate all possible capabilities. We (or really qemu) don't do this on x86 either. Certain capabilities may be a must, but not everything. There are also device specific registers not covered by any capability structures - what to do with those is even more of a question.
Furthermore for some of the fields justification why access to the raw hardware value is fine is going to be easy: r/o fields like vendor and device ID, for example. But every bit you allow direct access to needs to come with justification.
Quoted 5 lines
At the moment we are not going to claim that vPCI provides all means to pass through a PCI device safely with this respect and this is why the feature itself won't even be a tech preview yet. For that reason I think we can still have implemented only crucial set of handlers and still allow the rest to be read/write directly without emulation.
I think you need to separate what you need for development from what goes upstream: For dev purposes you can very well invert the policy from white- to black-listing. But if we accepted the latter into the main tree, the risk would be there that something gets missed at the time where the permission model gets changed around.
Quoted 6 lines
You could even have a non-default mode operating the way you want it (along the lines of pciback's permissive mode), allowing you to get away without needing to carry private patches. Things may also initially only work in that mode. But the default should be a mode which is secure (and which perhaps initially offers only very limited functionality).
Ok, so to make it clear:
1. We do not allow unhandled access for guests: for that I will create a dedicated patch which will implement such restrictions. Something like the below (for both vPCI read and write):xen/drivers/vpci/vpci.c +3
-0@@ -347,6 +347,7 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) const struct vpci_register *r; unsigned int data_offset = 0; uint32_t data = ~(uint32_t)0; + bool handled = false; if ( !size ) {@@ -405,6 +406,8 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) if ( cmp > 0 ) continue; + handled = true; /* Found the handler for this access. */ + if ( emu.offset < r->offset ) { /* Heading gap, read partial content from hardware. */@@ -432,6 +435,10 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) } spin_unlock(&pdev->vpci_lock);Quoted 12 lines
xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 444
Quoted 7 lines
xen/drivers/vpci/vpci.c +4
-0+ /* All unhandled guest requests return all 1's. */ + if ( !is_hardware_domain(d) && !handled ) + return ~(uint32_t)0; + if ( data_offset < size ) { /* Tailing gap, read the remaining. */Except that like for the "tailing gap" you also need to avoid the "heading gap" ending up in a read of the underlying hardware register. Effectively you want to deal properly with all
vpci_read_hw()invocations (including the one when no pdev was found, which for a DomU may simply meandomain_crash()).
Quoted 5 lines
Yes. And with the above patch I can now remove the "TODO patch" then? Because it is saying that we allow access to the registers, but it is not safe. And now, if we disable that access, then TODO should be about the need to implement emulation for all the registers which are not yet handled which is obvious.
Yes, I think that other patch then should have no use anymore. (To be honest I don't recall such a patch anyway.)
Jan
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 95 lines · Jan Beulich
Quoted 94 lines · Oleksandr Andrushchenko
Quoted 93 lines · Jan Beulich
Quoted 77 lines · Oleksandr Andrushchenko
Quoted 43 lines · Jan Beulich
Quoted 10 lines · Oleksandr Andrushchenko
Quoted 9 lines · Roger Pau Monné
Right (see my previous reply to this comment). I think it would be easier (and cleaner) if you switched the default behavior regarding unhandled register access for domUs at the start of the series (drop writes, reads returns ~0), and then you won't need to add all those dummy handler to drop writes and return ~0 for reads.
It's going to be more work initially as you would need to support passthrough of more registers, but it's the right approach that we need implementation wise.
Quoted 7 lines
While I agree in general, this effectively means that I'll need to provide handling for all PCIe registers and capabilities from the very start. Otherwise no guest be able to properly initialize a PCI device without that. Of course, we may want starting from stubs instead of proper emulation, which will direct the access to real HW and later on we add proper emulation. But, again, this is going to be a rather big piece of code where we need to explicitly handle every possible capability.
Since the two sub-threads are now about exactly the same topic, I'm answering here instead of there.
No, you are not going to need to emulate all possible capabilities. We (or really qemu) don't do this on x86 either. Certain capabilities may be a must, but not everything. There are also device specific registers not covered by any capability structures - what to do with those is even more of a question.
Furthermore for some of the fields justification why access to the raw hardware value is fine is going to be easy: r/o fields like vendor and device ID, for example. But every bit you allow direct access to needs to come with justification.
Quoted 5 lines
At the moment we are not going to claim that vPCI provides all means to pass through a PCI device safely with this respect and this is why the feature itself won't even be a tech preview yet. For that reason I think we can still have implemented only crucial set of handlers and still allow the rest to be read/write directly without emulation.
I think you need to separate what you need for development from what goes upstream: For dev purposes you can very well invert the policy from white- to black-listing. But if we accepted the latter into the main tree, the risk would be there that something gets missed at the time where the permission model gets changed around.
Quoted 6 lines
You could even have a non-default mode operating the way you want it (along the lines of pciback's permissive mode), allowing you to get away without needing to carry private patches. Things may also initially only work in that mode. But the default should be a mode which is secure (and which perhaps initially offers only very limited functionality).
Ok, so to make it clear:
1. We do not allow unhandled access for guests: for that I will create a dedicated patch which will implement such restrictions. Something like the below (for both vPCI read and write):xen/drivers/vpci/vpci.c +3
-0@@ -347,6 +347,7 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) const struct vpci_register *r; unsigned int data_offset = 0; uint32_t data = ~(uint32_t)0; + bool handled = false; if ( !size ) {@@ -405,6 +406,8 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) if ( cmp > 0 ) continue; + handled = true; /* Found the handler for this access. */ + if ( emu.offset < r->offset ) { /* Heading gap, read partial content from hardware. */xen/drivers/vpci/vpci.c uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) · line 444
Quoted 10 lines
xen/drivers/vpci/vpci.c +4
-0@@ -432,6 +435,10 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) } spin_unlock(&pdev->vpci_lock); + /* All unhandled guest requests return all 1's. */ + if ( !is_hardware_domain(d) && !handled ) + return ~(uint32_t)0; + if ( data_offset < size ) { /* Tailing gap, read the remaining. */Except that like for the "tailing gap" you also need to avoid the "heading gap" ending up in a read of the underlying hardware register. Effectively you want to deal properly with all
vpci_read_hw()invocations (including the one when no pdev was found, which for a DomU may simply meandomain_crash()).
Quoted 7 lines
Quoted 5 lines
Yes. And with the above patch I can now remove the "TODO patch" then? Because it is saying that we allow access to the registers, but it is not safe. And now, if we disable that access, then TODO should be about the need to implement emulation for all the registers which are not yet handled which is obvious.
Yes, I think that other patch then should have no use anymore. (To be honest I don't recall such a patch anyway.)
This is "[PATCH v5 14/14] vpci: add TODO for the registers not explicitly handled" in this series
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
In reply to Oleksandr AndrushchenkoQuoted 34 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 15 lines · Roger Pau Monné
xen/drivers/vpci/header.c static void rom_write(const struct pci_dev *pdev, unsigned int reg, · line 535
Quoted 11 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
xen/drivers/vpci/header.c +10
-0+static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +}There should be no need for those handlers. As said elsewhere: for guests registers not explicitly handled should return ~0 for reads and drop writes, which is what you are proposing here.
Yes, you are right: I can see in
vpci_readthat we end up reading ~0 if no handler exists (which is what I do here withguest_rom_read). But I am not that sure about the dropped writes:void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
uint32_tdata)
{
unsigned intdata_offset= 0;[snip]
if (
data_offset< size )
/* Tailing gap, write the remaining. */vpci_write_hw(sbdf, reg + data_offset, size - data_offset, data >> (data_offset * 8));
Quoted 4 lines
so it looks like for the un-handled writes we still reach the HW register. Could you please tell if the code above needs improvement (like checking if the write was handled) or I still need to provide a write handler, e.g.
guest_rom_writehere?
Right now (given the current code) unhandled reads and writes will all end up being forwarded to the hardware. This is intended for dom0, but this is not how it's going to work for domUs, where accesses will be discarded based on an accept list. IOW the handlers that you are adding here should be the default behavior for registers not explicitly handled in the domU case, and shouldn't require explicit handling.
Quoted 52 lines
Quoted 42 lines
Quoted 42 lines
xen/drivers/vpci/header.c +10
-4+static int init_bars (struct pci_dev *pdev) {uint16_t cmd;@@ -489,6 +542,7 @@ static int init_bars(struct pci_dev *pdev)struct vpci_header *header = &pdev->vpci->header;struct vpci_bar *bars = header->bars;int rc; +bool is_hwdom =is_hardware_domain (pdev->domain);switch (pci_conf_read8 (pdev->sbdf,PCI_HEADER_TYPE ) &0x7f ) {@@ -528,8 +582,10 @@ static int init_bars(struct pci_dev *pdev)if ( i && bars[i -1 ].type ==VPCI_BAR_MEM64_LO ) { bars[i].type =VPCI_BAR_MEM64_HI ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg, -4 , &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -569,8 +625,10 @@ static int init_bars(struct pci_dev *pdev) bars[i].size = size; bars[i].prefetchable = val &PCI_BASE_ADDRESS_MEM_PREFETCH ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg,4 , - &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -590,8 +648,10 @@ static int init_bars(struct pci_dev *pdev) header->rom_enabled =pci_conf_read32 (pdev->sbdf, rom_reg) &PCI_ROM_ADDRESS_ENABLE ;Quoted 8 lines
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 654
Quoted 6 lines
xen/drivers/vpci/header.c +4
-2- rc =vpci_add_register (pdev->vpci, vpci_hw_read32, rom_write, rom_reg, -4 , rom); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_rom_read, + is_hwdom ? rom_write : guest_rom_write, + rom_reg,4 , rom);This whole call should be made conditional to
is_hwdom, as said above there's no need for theguest_romhandlers.Yes, if writes are indeed dropped, please see question above
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
Likewise I assume you expect IO BARs to simply return ~0 and drop writes, as there's no explicit handler added for those?
Yes, but that was not my intention: I simply didn't handle IO BARs and now we do need that handling: either with the default behavior for the unhandled read/write (drop writes, read ~0) or by introducing the handlers. I hope we can rely on the "unhandled read/write" and get what we want
Indeed, the default behavior should be changed for domUs to drop writes, return ~0 for unhandled reads, then you won't need to add dummy handlers for the registers you don't want to expose.
Quoted 26 lines
Quoted 14 lines
xen/include/xen/vpci.h struct vpci { · line 71
Quoted 13 lines
xen/drivers/vpci/header.c +0
-0if ( rc ) rom->type =VPCI_BAR_EMPTY ; }xen/include/xen/vpci.h +1
-0@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */No, that's not the physical view, it's the physical (host) address.
Ok
Quoted 8 lines
xen/include/xen/vpci.h struct vpci { · line 74
Quoted 4 lines
xen/include/xen/vpci.h +2
-0uint64_t addr; +/* Guest view of the BAR: address and lower bits. */ +uint64_t guest_reg;I continue to think it would be clearer if you store the guest address here (gaddr, without the low bits) and add those in
guest_bar_readbased on bar->{type,prefetchable}. Then it would be equivalent to the existing 'addr' field.Ok, I'll re-work the code with this approach in mind: s/guest_reg/gaddr + required code to handle that
Quoted 1 line
Quoted 8 lines
Quoted 6 lines
I wonder whether we need to protect the added code with
CONFIG_HAS_VPCI_GUEST_SUPPORT, this would effectively be dead code otherwise. Long term I don't think we wish to differentiate between dom0 and domU vPCI support at build time, so I'm unsure whether it's helpful to pollute the code withCONFIG_HAS_VPCI_GUEST_SUPPORTwhen the plan is to remove those long term.I would have it without
CONFIG_HAS_VPCI_GUEST_SUPPORTif you don't mind
Well, I guess if it's not too intrusive it's fine to add the defines, removing them afterwards should be easy.
Thanks, Roger.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 11 lines
Quoted 9 lines
Quoted 7 lines
I wonder whether we need to protect the added code with
CONFIG_HAS_VPCI_GUEST_SUPPORT, this would effectively be dead code otherwise. Long term I don't think we wish to differentiate between dom0 and domU vPCI support at build time, so I'm unsure whether it's helpful to pollute the code withCONFIG_HAS_VPCI_GUEST_SUPPORTwhen the plan is to remove those long term.I would have it without
CONFIG_HAS_VPCI_GUEST_SUPPORTif you don't mindWell, I guess if it's not too intrusive it's fine to add the defines, removing them afterwards should be easy.
It is intrusive: it is easy to add such a define in struct vpci, but then you need ifdefery in xen/drivers/vpci/header.c to sort out the case when it is defined or not. I can still do that if you insist
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
In reply to Roger Pau MonnéHi, Roger!
Quoted 13 lines
xen/include/xen/vpci.h struct vpci { · line 71
Quoted 11 lines
rom->type = VPCI_BAR_EMPTY; }xen/include/xen/vpci.h +1
-0@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */No, that's not the physical view, it's the physical (host) address.
Quoted 8 lines
xen/include/xen/vpci.h struct vpci { · line 74
Quoted 3 lines
xen/include/xen/vpci.h +2
-0uint64_t addr; +/* Guest view of the BAR: address and lower bits. */ +uint64_t guest_reg;I continue to think it would be clearer if you store the guest address here (gaddr, without the low bits) and add those in
guest_bar_readbased on bar->{type,prefetchable}. Then it would be equivalent to the existing 'addr' field.
I agreed first to do such a change, but then recalled our discussion with Jan [1]. And then we decided that in order for it to be efficient it is better if we setup all the things during the write phase (rare), rather then during the write phase (more often). If you still see it clearer I can re-work the code
Thank you,
Oleksandr
[1] https://www.mail-archive.com/[email protected]/msg103431.html
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 17 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 16 lines
xen/include/xen/vpci.h struct vpci { · line 71
Quoted 11 lines
rom->type = VPCI_BAR_EMPTY; }xen/include/xen/vpci.h +1
-0@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */No, that's not the physical view, it's the physical (host) address.
Quoted 3 lines
xen/include/xen/vpci.h +2
-0uint64_t addr; +/* Guest view of the BAR: address and lower bits. */ +uint64_t guest_reg;
Quoted 8 lines
Quoted 5 lines
I continue to think it would be clearer if you store the guest address here (gaddr, without the low bits) and add those in
guest_bar_readbased on bar->{type,prefetchable}. Then it would be equivalent to the existing 'addr' field.I agreed first to do such a change, but then recalled our discussion with Jan [1]. And then we decided that in order for it to be efficient it is better if we setup all the things during the write phase (rare), rather then during the write phase (more often).
Small correction: The 2nd "write" was likely meant to be "read". But please recall that Roger is the maintainer of the code, so he gets the final say.
Jan
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 21 lines
xen/include/xen/vpci.h struct vpci { · line 71
Quoted 11 lines
rom->type = VPCI_BAR_EMPTY; }xen/include/xen/vpci.h +1
-0@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */No, that's not the physical view, it's the physical (host) address.
xen/include/xen/vpci.h struct vpci { · line 74
Quoted 3 lines
xen/include/xen/vpci.h +2
-0uint64_t addr; +/* Guest view of the BAR: address and lower bits. */ +uint64_t guest_reg;I continue to think it would be clearer if you store the guest address here (gaddr, without the low bits) and add those in
guest_bar_readbased on bar->{type,prefetchable}. Then it would be equivalent to the existing 'addr' field.
Quoted 4 lines
Quoted 3 lines
I agreed first to do such a change, but then recalled our discussion with Jan [1]. And then we decided that in order for it to be efficient it is better if we setup all the things during the write phase (rare), rather then during the write phase (more often).
Small correction: The 2nd "write" was likely meant to be "read".
Yes, this is correct.
Quoted 3 lines
But
please recall that Roger is the maintainer of the code, so he gets the final say.
Agree, but would vote for the current approach as it still saves some CPU cycles making the read operation really tiny
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 29 lines · Oleksandr Andrushchenko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 13 lines
xen/include/xen/vpci.h struct vpci { · line 71
Quoted 11 lines
rom->type = VPCI_BAR_EMPTY; }xen/include/xen/vpci.h +1
-0@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */No, that's not the physical view, it's the physical (host) address.
Quoted 8 lines
xen/include/xen/vpci.h struct vpci { · line 74
Quoted 3 lines
xen/include/xen/vpci.h +2
-0uint64_t addr; +/* Guest view of the BAR: address and lower bits. */ +uint64_t guest_reg;I continue to think it would be clearer if you store the guest address here (gaddr, without the low bits) and add those in
guest_bar_readbased on bar->{type,prefetchable}. Then it would be equivalent to the existing 'addr' field.I agreed first to do such a change, but then recalled our discussion with Jan [1]. And then we decided that in order for it to be efficient it is better if we setup all the things during the write phase (rare), rather then during the write phase (more often).
Quoted 7 lines
Quoted 1 line
Small correction: The 2nd "write" was likely meant to be "read".
Yes, this is correct.
Quoted 3 lines
But
please recall that Roger is the maintainer of the code, so he gets the final say.Agree, but would vote for the current approach as it still saves some CPU cycles making the read operation really tiny
I think you need to build the mapping rangeset(s) based on guest addresses, not host ones, so it's likely going to be easier if you store the address here in order to use it when building the rangeset.
Overall the cost of the vmexit will shadow the cost of doing a couple of ORs here in order to return the guest view of the BAR.
If you think storing the guest view of the BAR register will make the code easier to understand, then please go ahead. Otherwise I would recommend to store the address like we do for the host position of the BAR (ie: addr field).
Thanks, Roger.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Hi, Roger!
Quoted 40 lines · Roger Pau Monné
Quoted 28 lines · Oleksandr Andrushchenko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 13 lines
xen/include/xen/vpci.h struct vpci { · line 71
Quoted 11 lines
rom->type = VPCI_BAR_EMPTY; }xen/include/xen/vpci.h +1
-0@@ -68,7 +68,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical view of the BAR. */No, that's not the physical view, it's the physical (host) address.
Quoted 8 lines
xen/include/xen/vpci.h struct vpci { · line 74
Quoted 3 lines
xen/include/xen/vpci.h +2
-0uint64_t addr; +/* Guest view of the BAR: address and lower bits. */ +uint64_t guest_reg;I continue to think it would be clearer if you store the guest address here (gaddr, without the low bits) and add those in
guest_bar_readbased on bar->{type,prefetchable}. Then it would be equivalent to the existing 'addr' field.I agreed first to do such a change, but then recalled our discussion with Jan [1]. And then we decided that in order for it to be efficient it is better if we setup all the things during the write phase (rare), rather then during the write phase (more often).
Quoted 7 lines
Quoted 1 line
Small correction: The 2nd "write" was likely meant to be "read".
Yes, this is correct.
Quoted 3 lines
But
please recall that Roger is the maintainer of the code, so he gets the final say.Agree, but would vote for the current approach as it still saves some CPU cycles making the read operation really tiny
I think you need to build the mapping rangeset(s) based on guest addresses, not host ones, so it's likely going to be easier if you store the address here in order to use it when building the rangeset.
Quoted 7 lines
Overall the cost of the vmexit will shadow the cost of doing a couple of ORs here in order to return the guest view of the BAR.
If you think storing the guest view of the BAR register will make the code easier to understand, then please go ahead. Otherwise I would recommend to store the address like we do for the host position of the BAR (ie: addr field).
I still think it is easier to understand: if you take a look at what we do for BAR write for both host and guest you'll see that we do almost the same operations, but in host case we end up writing bar->addr + low bits to the HW register and in case of a guest we store the complete thing into bar->guest_reg. Read operation doesn't require any processing for host, so it is equivalent to direct hw read and in case of a guest it is as simple as possible and implements the equivalent by returning part of bar->guest_reg (hi or lo). So, from this POV it is IMO easier to understand the logic.
That being said, I do agree that the contents of the bar->addr is not equivalent to bar->guest_reg, but we have already taken care of it by naming the guest's one with guest_reg, not guest_addr.
I will keep the code as is then.
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
In reply to Oleksandr AndrushchenkoA couple more comments I realized while walking the dog.
Quoted 70 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- updated commit message
- s/guest_addr/guest_reg
Since v3:
- squashed two patches: dynamic add/remove handlers and guest BAR handler implementation
- fix guest BAR read of the high part of a 64bit BAR (Roger)
- add error handling tovpci_assign_device
- s/dom%pd/%pd
- blank line before return
Since v2:
- remove unneeded ifdefs forCONFIG_HAS_VPCI_GUEST_SUPPORTas more code has been eliminated from being built on x86
Since v1:
- constify structpci_devwhere possible
- do not open codeis_system_domain()
- simplify some code3. simplify
- use gdprintk + error code instead of gprintk
- gate vpci_bar_{add|remove}_handlers withCONFIG_HAS_VPCI_GUEST_SUPPORT, so these do not get compiled for x86
- removed unneededis_system_domaincheck
- re-work guest read/write to be much simpler and do more work on write than read which is expected to be called more frequently
- removed one too obvious comment--- xen/drivers/vpci/header.c | 72 +++++++++++++++++++++++++++++++++++---- xen/include/xen/vpci.h | 3 ++ 2 files changed, 69 insertions(+), 6 deletions(-)xen/drivers/vpci/header.c +14
-0@@ -433,6 +433,48 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write32 (pdev->sbdf, reg, val); } +static void guest_bar_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } +else + {
xen/drivers/vpci/header.c static void bar_write(const struct pci_dev *pdev, unsigned int reg, · line 459
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+ val &=PCI_BASE_ADDRESS_MEM_MASK ; + val |= bar->type ==VPCI_BAR_MEM32 ?PCI_BASE_ADDRESS_MEM_TYPE_32 + :PCI_BASE_ADDRESS_MEM_TYPE_64 ; + val |= bar->prefetchable ?PCI_BASE_ADDRESS_MEM_PREFETCH :0 ; + } + + bar->guest_reg &= ~(0xffffffffull << (hi ?32 :0 )); + bar->guest_reg |= (uint64_t )val << (hi ?32 :0 ); + + bar->guest_reg &= ~(bar->size -1 ) | ~PCI_BASE_ADDRESS_MEM_MASK ;
You need to assert that the guest set address has the same page offset as the physical address on the host, or otherwise things won't work as expected. Ie: guest_addr & ~PAGE_MASK == addr & ~PAGE_MASK.
Quoted 61 lines
xen/drivers/vpci/header.c +34
-2+} + +static uint32_t guest_bar_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +const struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } + +return bar->guest_reg >> (hi ?32 :0 ); +} +static void rom_write (const struct pci_dev *pdev,unsigned int reg,uint32_t val,void *data) {@@ -481,6 +523,17 @@ static void rom_write(const struct pci_dev *pdev, unsigned int reg, rom->addr = val &PCI_ROM_ADDRESS_MASK ; } +static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +} +static int init_bars (struct pci_dev *pdev) {uint16_t cmd;@@ -489,6 +542,7 @@ static int init_bars(struct pci_dev *pdev)struct vpci_header *header = &pdev->vpci->header;struct vpci_bar *bars = header->bars;int rc; +bool is_hwdom =is_hardware_domain (pdev->domain);switch (pci_conf_read8 (pdev->sbdf,PCI_HEADER_TYPE ) &0x7f ) {@@ -528,8 +582,10 @@ static int init_bars(struct pci_dev *pdev)if ( i && bars[i -1 ].type ==VPCI_BAR_MEM64_LO ) { bars[i].type =VPCI_BAR_MEM64_HI ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg, -4 , &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 631
Quoted 9 lines
xen/drivers/vpci/header.c +4
-2@@ -569,8 +625,10 @@ static int init_bars(struct pci_dev *pdev) bars[i].size = size; bars[i].prefetchable = val &PCI_BASE_ADDRESS_MEM_PREFETCH ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg,4 , - &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);
You need to initialize guest_reg to the physical host value also.
Quoted 5 lines
xen/drivers/vpci/header.c +0
-0if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -590,8 +648,10 @@ static int init_bars(struct pci_dev *pdev) header->rom_enabled = pci_conf_read32(pdev->sbdf, rom_reg) &
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 656
Quoted 10 lines
xen/drivers/vpci/header.c +4
-2PCI_ROM_ADDRESS_ENABLE ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, rom_write, rom_reg, -4 , rom); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_rom_read, + is_hwdom ? rom_write : guest_rom_write, + rom_reg,4 , rom);if ( rc ) rom->type =VPCI_BAR_EMPTY ;
Also memory decoding needs to be initially disabled when used by guests, in order to prevent the BAR being placed on top of a RAM region. The guest physmap will be different from the host one, so it's possible for BARs to end up placed on top of RAM regions initially until the firmware or OS places them at a suitable address.
Thanks, Roger.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Hi, Roger!
Quoted 78 lines · Roger Pau Monné
A couple more comments I realized while walking the dog.
Quoted 75 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
ROM BAR is only handled for the hardware domain and for guest domains there is a stub: at the moment PCI expansion ROM handling is supported for x86 only and it might not be used by other architectures without emulating x86. Other use-cases may include using that expansion ROM before Xen boots, hence no emulation is needed in Xen itself. Or when a guest wants to use the ROM code which seems to be rare.
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- updated commit message
- s/guest_addr/guest_reg
Since v3:
- squashed two patches: dynamic add/remove handlers and guest BAR handler implementation
- fix guest BAR read of the high part of a 64bit BAR (Roger)
- add error handling tovpci_assign_device
- s/dom%pd/%pd
- blank line before return
Since v2:
- remove unneeded ifdefs forCONFIG_HAS_VPCI_GUEST_SUPPORTas more code has been eliminated from being built on x86
Since v1:
- constify structpci_devwhere possible
- do not open codeis_system_domain()
- simplify some code3. simplify
- use gdprintk + error code instead of gprintk
- gate vpci_bar_{add|remove}_handlers withCONFIG_HAS_VPCI_GUEST_SUPPORT, so these do not get compiled for x86
- removed unneededis_system_domaincheck
- re-work guest read/write to be much simpler and do more work on write than read which is expected to be called more frequently
- removed one too obvious comment--- xen/drivers/vpci/header.c | 72 +++++++++++++++++++++++++++++++++++---- xen/include/xen/vpci.h | 3 ++ 2 files changed, 69 insertions(+), 6 deletions(-)xen/drivers/vpci/header.c +19
-0@@ -433,6 +433,48 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write32 (pdev->sbdf, reg, val); } +static void guest_bar_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } +else + { + val &=PCI_BASE_ADDRESS_MEM_MASK ; + val |= bar->type ==VPCI_BAR_MEM32 ?PCI_BASE_ADDRESS_MEM_TYPE_32 + :PCI_BASE_ADDRESS_MEM_TYPE_64 ; + val |= bar->prefetchable ?PCI_BASE_ADDRESS_MEM_PREFETCH :0 ; + }
Quoted 8 lines
xen/drivers/vpci/header.c static void bar_write(const struct pci_dev *pdev, unsigned int reg, · line 459
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+ + bar->guest_reg &= ~(0xffffffffull << (hi ?32 :0 )); + bar->guest_reg |= (uint64_t )val << (hi ?32 :0 ); + + bar->guest_reg &= ~(bar->size -1 ) | ~PCI_BASE_ADDRESS_MEM_MASK ;You need to assert that the guest set address has the same page offset as the physical address on the host, or otherwise things won't work as expected. Ie:
guest_addr& ~PAGE_MASK == addr & ~PAGE_MASK.
Good catch, thank you
Quoted 65 lines
Quoted 65 lines
xen/drivers/vpci/header.c +34
-2+} + +static uint32_t guest_bar_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +const struct vpci_bar *bar = data; +bool hi = false; + +if ( bar->type ==VPCI_BAR_MEM64_HI ) + { +ASSERT (reg >PCI_BASE_ADDRESS_0 ); + bar--; + hi = true; + } + +return bar->guest_reg >> (hi ?32 :0 ); +} +static void rom_write (const struct pci_dev *pdev,unsigned int reg,uint32_t val,void *data) {@@ -481,6 +523,17 @@ static void rom_write(const struct pci_dev *pdev, unsigned int reg, rom->addr = val &PCI_ROM_ADDRESS_MASK ; } +static void guest_rom_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t val,void *data) +{ +} + +static uint32_t guest_rom_read (const struct pci_dev *pdev,unsigned int reg, +void *data) +{ +return 0xffffffff ; +} +static int init_bars (struct pci_dev *pdev) {uint16_t cmd;@@ -489,6 +542,7 @@ static int init_bars(struct pci_dev *pdev)struct vpci_header *header = &pdev->vpci->header;struct vpci_bar *bars = header->bars;int rc; +bool is_hwdom =is_hardware_domain (pdev->domain);switch (pci_conf_read8 (pdev->sbdf,PCI_HEADER_TYPE ) &0x7f ) {@@ -528,8 +582,10 @@ static int init_bars(struct pci_dev *pdev)if ( i && bars[i -1 ].type ==VPCI_BAR_MEM64_LO ) { bars[i].type =VPCI_BAR_MEM64_HI ; - rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg, -4 , &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -569,8 +625,10 @@ static int init_bars(struct pci_dev *pdev) bars[i].size = size; bars[i].prefetchable = val &PCI_BASE_ADDRESS_MEM_PREFETCH ;
Quoted 7 lines
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 631
Quoted 6 lines
xen/drivers/vpci/header.c +4
-2- rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg,4 , - &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);You need to initialize
guest_regto the physical host value also.
But why? There was a concern that exposing host's value to a guest may be a security issue. And wouldn't it be possible for a guest to decide that the firmware has setup the BAR and it doesn't need to care of it and hence use a wrong value instead of setting it up by itself? I had an issue with that if I'm not mistaken that guest's Linux didn't set the BAR properly and used what was programmed
Quoted 8 lines
Quoted 8 lines
xen/drivers/vpci/header.c +0
-0if ( rc ) {pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);@@ -590,8 +648,10 @@ static int init_bars(struct pci_dev *pdev) header->rom_enabled =pci_conf_read32 (pdev->sbdf, rom_reg) &PCI_ROM_ADDRESS_ENABLE ;
Quoted 13 lines
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 656
Quoted 8 lines
xen/drivers/vpci/header.c +4
-2- rc =vpci_add_register (pdev->vpci, vpci_hw_read32, rom_write, rom_reg, -4 , rom); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_rom_read, + is_hwdom ? rom_write : guest_rom_write, + rom_reg,4 , rom);if ( rc ) rom->type =VPCI_BAR_EMPTY ;Also memory decoding needs to be initially disabled when used by guests, in order to prevent the BAR being placed on top of a RAM region. The guest physmap will be different from the host one, so it's possible for BARs to end up placed on top of RAM regions initially until the firmware or OS places them at a suitable address.
Agree, memory decoding must be disabled
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 18 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 8 lines · Roger Pau Monné
A couple more comments I realized while walking the dog.
Quoted 5 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
xen/drivers/vpci/header.c +0
-0@@ -569,8 +625,10 @@ static int init_bars(struct pci_dev *pdev) bars[i].size = size; bars[i].prefetchable = val &PCI_BASE_ADDRESS_MEM_PREFETCH ;Quoted 7 lines
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 631
Quoted 6 lines
xen/drivers/vpci/header.c +4
-2- rc =vpci_add_register (pdev->vpci, vpci_hw_read32, bar_write, reg,4 , - &bars[i]); + rc =vpci_add_register (pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_bar_read, + is_hwdom ? bar_write : guest_bar_write, + reg,4 , &bars[i]);You need to initialize
guest_regto the physical host value also.
Quoted 6 lines
But why? There was a concern that exposing host's value to a guest may be a security issue. And wouldn't it be possible for a guest to decide that the firmware has setup the BAR and it doesn't need to care of it and hence use a wrong value instead of setting it up by itself? I had an issue with that if I'm not mistaken that guest's Linux didn't set the BAR properly and used what was programmed
I think I've made that comment before realizing that all BARs must start with memory decoding disabled for guests, so that the guest firmware can position them. Using the host value as a starting point doesn't make sense because there's no relation between the guest and the host memory maps. You can drop this comment.
Thanks, Roger.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
In reply to Oleksandr AndrushchenkoHi, Roger!
Quoted 6 lines
Quoted 5 lines
Also memory decoding needs to be initially disabled when used by guests, in order to prevent the BAR being placed on top of a RAM region. The guest physmap will be different from the host one, so it's possible for BARs to end up placed on top of RAM regions initially until the firmware or OS places them at a suitable address.
Agree, memory decoding must be disabled
Isn't it already achieved by the toolstack resetting the PCI device while assigning it to a guest?
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 8 lines · Oleksandr Andrushchenko
Quoted 6 lines
Quoted 5 lines
Also memory decoding needs to be initially disabled when used by guests, in order to prevent the BAR being placed on top of a RAM region. The guest physmap will be different from the host one, so it's possible for BARs to end up placed on top of RAM regions initially until the firmware or OS places them at a suitable address.
Agree, memory decoding must be disabled
Isn't it already achieved by the toolstack resetting the PCI device while assigning it to a guest?
Iirc the tool stack would reset a device only after getting it back from a DomU. When coming straight from Dom0 or DomIO, no reset would be performed. Furthermore, (again iirc) there are cases where there's no known (standard) way to reset a device. Assigning such to a guest when it previously was owned by another one is risky (and hence needs an admin knowing what they're doing), but may be acceptable in particular when e.g. simply rebooting a guest.
IOW - I don't think you can rely on the bit being in a particular state.
Jan
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 17 lines · Jan Beulich
Quoted 8 lines · Oleksandr Andrushchenko
Quoted 6 lines
Quoted 5 lines
Also memory decoding needs to be initially disabled when used by guests, in order to prevent the BAR being placed on top of a RAM region. The guest physmap will be different from the host one, so it's possible for BARs to end up placed on top of RAM regions initially until the firmware or OS places them at a suitable address.
Agree, memory decoding must be disabled
Isn't it already achieved by the toolstack resetting the PCI device while assigning it to a guest?
Iirc the tool stack would reset a device only after getting it back from a DomU. When coming straight from Dom0 or DomIO, no reset would be performed. Furthermore, (again iirc) there are cases where there's no known (standard) way to reset a device. Assigning such to a guest when it previously was owned by another one is risky (and hence needs an admin knowing what they're doing), but may be acceptable in particular when e.g. simply rebooting a guest.
Quoted 1 line
IOW - I don't think you can rely on the bit being in a particular state.
So, you mean something like:
xen/drivers/vpci/header.c +8 -0
@@ -808,6 +808,14 @@ static int init_bars(struct pci_dev *pdev) return rc; } + /* + * Memory decoding needs to be initially disabled when used by + * guests, in order to prevent the BAR being placed on top of a RAM + * region. + */ + if ( !is_hwdom ) + pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd & ~PCI_COMMAND_MEMORY); + return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0; } REGISTER_VPCI_INIT(init_bars, VPCI_PRIORITY_MIDDLE);
Quoted 2 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 20 lines · Oleksandr Andrushchenko
Quoted 17 lines · Jan Beulich
Quoted 8 lines · Oleksandr Andrushchenko
Quoted 6 lines
Quoted 5 lines
Also memory decoding needs to be initially disabled when used by guests, in order to prevent the BAR being placed on top of a RAM region. The guest physmap will be different from the host one, so it's possible for BARs to end up placed on top of RAM regions initially until the firmware or OS places them at a suitable address.
Agree, memory decoding must be disabled
Isn't it already achieved by the toolstack resetting the PCI device while assigning it to a guest?
Iirc the tool stack would reset a device only after getting it back from a DomU. When coming straight from Dom0 or DomIO, no reset would be performed. Furthermore, (again iirc) there are cases where there's no known (standard) way to reset a device. Assigning such to a guest when it previously was owned by another one is risky (and hence needs an admin knowing what they're doing), but may be acceptable in particular when e.g. simply rebooting a guest.
Quoted 2 lines
Quoted 1 line
IOW - I don't think you can rely on the bit being in a particular state.
So, you mean something like:
Perhaps, but then I think ...
Quoted 5 lines
xen/drivers/vpci/header.c +0
-0@@ -808,6 +808,14 @@ static int init_bars(struct pci_dev *pdev) return rc; }
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 819
Quoted 10 lines
xen/drivers/vpci/header.c +8
-0+ /* + * Memory decoding needs to be initially disabled when used by + * guests, in order to prevent the BAR being placed on top of a RAM + * region. + */ + if ( !is_hwdom ) + pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd & ~PCI_COMMAND_MEMORY); + return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0;
... you also want to update cmd, thus avoiding the call to modify_bars().
And btw, from an abstract pov the same is true for I/O decoding: I realize that you mean to leave I/O port BARs aside for the moment, but I think the command register handling could very well take care of both.
Which quickly gets us to the bus master enable bit: I think that one should initially be off too. Making me wonder: Doesn't the PCI spec define what the reset state of this register is? If so, that's what I think we want to put in place for DomU-s.
Jan
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 41 lines · Jan Beulich
Quoted 19 lines · Oleksandr Andrushchenko
Quoted 17 lines · Jan Beulich
Quoted 8 lines · Oleksandr Andrushchenko
Quoted 6 lines
Quoted 5 lines
Also memory decoding needs to be initially disabled when used by guests, in order to prevent the BAR being placed on top of a RAM region. The guest physmap will be different from the host one, so it's possible for BARs to end up placed on top of RAM regions initially until the firmware or OS places them at a suitable address.
Agree, memory decoding must be disabled
Isn't it already achieved by the toolstack resetting the PCI device while assigning it to a guest?
Iirc the tool stack would reset a device only after getting it back from a DomU. When coming straight from Dom0 or DomIO, no reset would be performed. Furthermore, (again iirc) there are cases where there's no known (standard) way to reset a device. Assigning such to a guest when it previously was owned by another one is risky (and hence needs an admin knowing what they're doing), but may be acceptable in particular when e.g. simply rebooting a guest.
Quoted 2 lines
Quoted 1 line
IOW - I don't think you can rely on the bit being in a particular state.
So, you mean something like:
Perhaps, but then I think ...
Quoted 5 lines
xen/drivers/vpci/header.c +0
-0@@ -808,6 +808,14 @@ static int init_bars(struct pci_dev *pdev) return rc; }xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 819
Quoted 10 lines
xen/drivers/vpci/header.c +8
-0+ /* + * Memory decoding needs to be initially disabled when used by + * guests, in order to prevent the BAR being placed on top of a RAM + * region. + */ + if ( !is_hwdom ) + pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd & ~PCI_COMMAND_MEMORY); + return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0;... you also want to update cmd, thus avoiding the call to
modify_bars().
Quoted 8 lines
And btw, from an abstract pov the same is true for I/O decoding: I realize that you mean to leave I/O port BARs aside for the moment, but I think the command register handling could very well take care of both.
Which quickly gets us to the bus master enable bit: I think that one should initially be off too. Making me wonder: Doesn't the PCI spec define what the reset state of this register is? If so, that's what I think we want to put in place for DomU-s.
The spec I have says that all bits are typically 0 after reset. So, it seems to be reasonable to just write 0 to PCI_COMMAND
Quoted 2 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
In reply to Oleksandr AndrushchenkoQuoted 28 lines · Oleksandr Andrushchenko
Quoted 17 lines · Jan Beulich
Quoted 8 lines · Oleksandr Andrushchenko
Quoted 6 lines
Quoted 5 lines
Also memory decoding needs to be initially disabled when used by guests, in order to prevent the BAR being placed on top of a RAM region. The guest physmap will be different from the host one, so it's possible for BARs to end up placed on top of RAM regions initially until the firmware or OS places them at a suitable address.
Agree, memory decoding must be disabled
Isn't it already achieved by the toolstack resetting the PCI device while assigning it to a guest?
Iirc the tool stack would reset a device only after getting it back from a DomU. When coming straight from Dom0 or DomIO, no reset would be performed. Furthermore, (again iirc) there are cases where there's no known (standard) way to reset a device. Assigning such to a guest when it previously was owned by another one is risky (and hence needs an admin knowing what they're doing), but may be acceptable in particular when e.g. simply rebooting a guest.
Quoted 1 line
IOW - I don't think you can rely on the bit being in a particular state.
So, you mean something like:
xen/drivers/vpci/header.c +0
-0
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 817
Quoted 10 lines
xen/drivers/vpci/header.c +7
-0@@ -808,6 +808,14 @@ static int init_bars(struct pci_dev *pdev) return rc; } + /* + * Memory decoding needs to be initially disabled when used by + * guests, in order to prevent the BAR being placed on top of a RAM + * region. + */ + if ( !is_hwdom ) + pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd & ~PCI_COMMAND_MEMORY);
Memory decoding is already disabled here, so you just need to avoid enabling it, for example:
/*
* Memory decoding needs to be initially disabled when used by
* guests, in order to prevent the BARs being mapped at gfn 0 by
* default.
*/
if ( !is_hwdom )
cmd &= ~PCI_COMMAND_MEMORY;
xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 819
Quoted 1 line
xen/drivers/vpci/header.c +0
-0return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0;
This is important here because guest_reg won't be set (ie: will be set to 0) so if for some reason memory decoding was enabled you would end up with BARs mappings overlapping at gfn 0.
Thanks, Roger.
Re: [PATCH v5 06/14] vpci/header: implement guest BAR register handlers
Quoted 47 lines · Roger Pau Monné
Quoted 27 lines · Oleksandr Andrushchenko
Quoted 17 lines · Jan Beulich
Quoted 8 lines · Oleksandr Andrushchenko
Quoted 6 lines
Quoted 5 lines
Also memory decoding needs to be initially disabled when used by guests, in order to prevent the BAR being placed on top of a RAM region. The guest physmap will be different from the host one, so it's possible for BARs to end up placed on top of RAM regions initially until the firmware or OS places them at a suitable address.
Agree, memory decoding must be disabled
Isn't it already achieved by the toolstack resetting the PCI device while assigning it to a guest?
Iirc the tool stack would reset a device only after getting it back from a DomU. When coming straight from Dom0 or DomIO, no reset would be performed. Furthermore, (again iirc) there are cases where there's no known (standard) way to reset a device. Assigning such to a guest when it previously was owned by another one is risky (and hence needs an admin knowing what they're doing), but may be acceptable in particular when e.g. simply rebooting a guest.
Quoted 1 line
IOW - I don't think you can rely on the bit being in a particular state.
So, you mean something like:
xen/drivers/vpci/header.c +0
-0xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 817
Quoted 10 lines
xen/drivers/vpci/header.c +7
-0@@ -808,6 +808,14 @@ static int init_bars(struct pci_dev *pdev) return rc; } + /* + * Memory decoding needs to be initially disabled when used by + * guests, in order to prevent the BAR being placed on top of a RAM + * region. + */ + if ( !is_hwdom ) + pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd & ~PCI_COMMAND_MEMORY);Memory decoding is already disabled here, so you just need to avoid enabling it, for example:
/*
* Memory decoding needs to be initially disabled when used by
* guests, in order to prevent the BARs being mapped at gfn 0 by
* default.
*/
if ( !is_hwdom )
Quoted 6 lines
cmd &= ~PCI_COMMAND_MEMORY;xen/drivers/vpci/header.c static int init_bars(struct pci_dev *pdev) · line 819
Quoted 1 line
xen/drivers/vpci/header.c +0
-0return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0;This is important here because
guest_regwon't be set (ie: will be set to 0) so if for some reason memory decoding was enabled you would end up with BARs mappings overlapping at gfn 0.
Then the patch [1] will do what we need to be done for the guest I guess I am thinking to still have it in the series which will solve exactly the problem we are trying to solve
Quoted 2 lines
Thanks, Roger.
[1] https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/
PATCH v5 7/14 vpci/header: handle p2m range sets per BAR
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
Instead of handling a single range set, that contains all the memory regions of all the BARs and ROM, have them per BAR.
As the range sets are now created when a PCI device is added and destroyed when it is removed so make them named and accounted.
Note that rangesets were chosen here despite there being only up to 3 separate ranges in each set (typically just 1). But rangeset per BAR was chosen for the ease of implementation and existing code re-usability.
This is in preparation of making non-identity mappings in p2m for the MMIOs/ROM.
---
Since v4:
- use named range sets for BARs (Jan)
- changes required by the new locking scheme
- updated commit message (Jan)
Since v3:
- re-work vpci_cancel_pending accordingly to the per-BAR handling
- s/num_mem_ranges/map_pending and s/uint8_t/bool
- ASSERT(bar->mem) in modify_bars
- create and destroy the rangesets on add/remove
--- xen/drivers/vpci/header.c | 190 +++++++++++++++++++++++++++----------- xen/drivers/vpci/vpci.c | 30 +++++- xen/include/xen/vpci.h | 3 +- 3 files changed, 166 insertions(+), 57 deletions(-)
xen/drivers/vpci/header.c +135 -55
@@ -137,45 +137,86 @@ bool vpci_process_pending(struct vcpu *v)return false;spin_lock (&pdev->vpci_lock); -if ( !pdev->vpci_cancel_pending && v->vpci.mem ) +if ( !pdev->vpci ) + { +spin_unlock (&pdev->vpci_lock); +return false; + } + +if ( !pdev->vpci_cancel_pending && v->vpci.map_pending ) {struct map_data data = { .d = v->domain, .map = v->vpci.cmd &PCI_COMMAND_MEMORY , }; -int rc =rangeset_consume_ranges (v->vpci.mem, map_range, &data); +struct vpci_header *header = &pdev->vpci->header; +unsigned int i; -if ( rc == -ERESTART ) +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) { -spin_unlock (&pdev->vpci_lock); -return true; - } +struct vpci_bar *bar = &header->bars[i]; +int rc; + +if (rangeset_is_empty (bar->mem) ) +continue ; + + rc =rangeset_consume_ranges (bar->mem, map_range, &data); + +if ( rc == -ERESTART ) + { +spin_unlock (&pdev->vpci_lock); +return true; + } -if ( pdev->vpci )/* Disable memory decoding unconditionally on failure. */ -modify_decoding (pdev, - rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, +modify_decoding (pdev, rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, !rc && v->vpci.rom_only); -if ( rc ) - { -/* -* FIXME: in case of failure remove the device from the domain. -* Note that there might still be leftover mappings. While this is -* safe for Dom0, for DomUs the domain needs to be killed in order -* to avoid leaking stale p2m mappings on failure. -*/ -if (is_hardware_domain (v->domain) ) -vpci_remove_device_locked (pdev); -else -domain_crash (v->domain); +if ( rc ) + { +/* +* FIXME: in case of failure remove the device from the domain. +* Note that there might still be leftover mappings. While this is +* safe for Dom0, for DomUs the domain needs to be killed in order +* to avoid leaking stale p2m mappings on failure. +*/ +if (is_hardware_domain (v->domain) ) +vpci_remove_device_locked (pdev); +else +domain_crash (v->domain); + +break ; + } } + + v->vpci.map_pending = false; }spin_unlock (&pdev->vpci_lock);return false; } +static void vpci_bar_remove_ranges (const struct pci_dev *pdev) +{ +struct vpci_header *header = &pdev->vpci->header; +unsigned int i; +int rc; + +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i]; + +if (rangeset_is_empty (bar->mem) ) +continue ; + + rc =rangeset_remove_range (bar->mem,0 , ~0ULL ); +if ( !rc ) +printk (XENLOG_ERR +"%pd %pp failed to remove range set for BAR: %d\n" , + pdev->domain, &pdev->sbdf, rc); + } +} +void vpci_cancel_pending_locked (struct pci_dev *pdev) {struct vcpu *v;
@@ -185,23 +226,33 @@ void vpci_cancel_pending_locked(struct pci_dev *pdev)/* Cancel any pending work now on all vCPUs. */ for_each_vcpu ( pdev->domain, v ) { -if ( v->vpci.mem && (v->vpci.pdev == pdev) ) +if ( v->vpci.map_pending && (v->vpci.pdev == pdev) ) { -rangeset_destroy (v->vpci.mem); - v->vpci.mem =NULL ; +vpci_bar_remove_ranges (pdev); + v->vpci.map_pending = false; } } }static int __init apply_map (struct domain *d,const struct pci_dev *pdev, -struct rangeset *mem,uint16_t cmd) +uint16_t cmd) {struct map_data data = { .d = d, .map = true }; -int rc; +struct vpci_header *header = &pdev->vpci->header; +int rc =0 ; +unsigned int i; + +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i]; -while ( (rc =rangeset_consume_ranges (mem, map_range, &data)) == -ERESTART ) -process_pending_softirqs (); -rangeset_destroy (mem); +if (rangeset_is_empty (bar->mem) ) +continue ; + +while ( (rc =rangeset_consume_ranges (bar->mem, map_range, + &data)) == -ERESTART ) +process_pending_softirqs (); + }if ( !rc )modify_decoding (pdev, cmd, false);
@@ -209,7 +260,7 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev, }static void defer_map (struct domain *d,struct pci_dev *pdev, -struct rangeset *mem,uint16_t cmd,bool rom_only) +uint16_t cmd,bool rom_only) {struct vcpu *curr = current;
@@ -220,7 +271,7 @@ static void defer_map(struct domain *d, struct pci_dev *pdev,* started for the same device if the domain is not well-behaved. */ curr->vpci.pdev = pdev; - curr->vpci.mem = mem; + curr->vpci.map_pending = true; curr->vpci.cmd = cmd; curr->vpci.rom_only = rom_only;/*
@@ -234,42 +285,40 @@ static void defer_map(struct domain *d, struct pci_dev *pdev,static int modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only) {struct vpci_header *header = &pdev->vpci->header; -struct rangeset *mem =rangeset_new (NULL ,NULL ,0 );struct pci_dev *tmp, *dev =NULL ;const struct vpci_msix *msix = pdev->vpci->msix; -unsigned int i; +unsigned int i, j;int rc; - -if ( !mem ) -return -ENOMEM ; +bool map_pending;/* -* Create a rangeset that represents the current device BARs memory region +* Create a rangeset per BAR that represents the current device memory region * and compare it against all the currently active BAR memory regions. If * an overlap is found, subtract it from the region to be mapped/unmapped. * -* First fill the rangeset with all the BARs of this device or with the ROM +* First fill the rangesets with all the BARs of this device or with the ROM * BAR only, depending on whether the guest is toggling the memory decode * bit of the command register, or the enable bit of the ROM BAR register. */ for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) { -const struct vpci_bar *bar = &header->bars[i]; +struct vpci_bar *bar = &header->bars[i];unsigned long start =PFN_DOWN (bar->addr);unsigned long end =PFN_DOWN (bar->addr + bar->size -1 ); +ASSERT (bar->mem); +if ( !MAPPABLE_BAR (bar) || (rom_only ? bar->type !=VPCI_BAR_ROM : (bar->type ==VPCI_BAR_ROM && !header->rom_enabled)) )continue ; - rc =rangeset_add_range (mem, start, end); + rc =rangeset_add_range (bar->mem, start, end);if ( rc ) {printk (XENLOG_G_WARNING "Failed to add [%lx, %lx]: %d\n" , start, end, rc); -rangeset_destroy (mem); -return rc; + goto fail; } }
@@ -280,14 +329,21 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)unsigned long end =PFN_DOWN (vmsix_table_addr (pdev->vpci, i) +vmsix_table_size (pdev->vpci, i) -1 ); - rc =rangeset_remove_range (mem, start, end); -if ( rc ) +for ( j =0 ; j <ARRAY_SIZE (header->bars); j++ ) { -printk (XENLOG_G_WARNING -"Failed to remove MSIX table [%lx, %lx]: %d\n" , - start, end, rc); -rangeset_destroy (mem); -return rc; +const struct vpci_bar *bar = &header->bars[j]; + +if (rangeset_is_empty (bar->mem) ) +continue ; + + rc =rangeset_remove_range (bar->mem, start, end); +if ( rc ) + { +printk (XENLOG_G_WARNING +"Failed to remove MSIX table [%lx, %lx]: %d\n" , + start, end, rc); + goto fail; + } } }
@@ -325,7 +381,8 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)unsigned long start =PFN_DOWN (bar->addr);unsigned long end =PFN_DOWN (bar->addr + bar->size -1 ); - if ( !bar->enabled || !rangeset_overlaps_range(mem, start, end) || + if ( !bar->enabled || + !rangeset_overlaps_range(bar->mem, start, end) ||/* * If only the ROM enable bit is toggled check against other * BARs in the same device for overlaps, but not against the
@@ -334,14 +391,13 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) (rom_only && tmp == pdev && bar->type == VPCI_BAR_ROM) )continue ; - rc =rangeset_remove_range (mem, start, end); + rc =rangeset_remove_range (bar->mem, start, end);if ( rc ) {spin_unlock (&tmp->vpci_lock);printk (XENLOG_G_WARNING "Failed to remove [%lx, %lx]: %d\n" , start, end, rc); -rangeset_destroy (mem); -return rc; + goto fail; } }spin_unlock (&tmp->vpci_lock);
@@ -360,12 +416,36 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)* will always be to establish mappings and process all the BARs. */ ASSERT ((cmd &PCI_COMMAND_MEMORY ) && !rom_only); -return apply_map (pdev->domain, pdev, mem, cmd); +return apply_map (pdev->domain, pdev, cmd); } -defer_map (dev->domain, dev, mem, cmd, rom_only); +/* Find out how many memory ranges has left after MSI and overlaps. */ + map_pending = false; +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) +if ( !rangeset_is_empty (header->bars[i].mem) ) + { + map_pending = true; +break ; + } + +/* +* There are cases when PCI device, root port for example, has neither +* memory space nor IO. In this case PCI command register write is +* missed resulting in the underlying PCI device not functional, so: +* - if there are no regions write the command register now +* - if there are regions then defer work and write later on +*/ +if ( !map_pending ) +pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd); +else +defer_map (dev->domain, dev, cmd, rom_only);return 0 ; + +fail: +/* Destroy all the ranges we may have added. */ +vpci_bar_remove_ranges (pdev); +return rc; } static void cmd_write(const struct pci_dev *pdev, unsigned int reg,
xen/drivers/vpci/vpci.c +29 -1
@@ -52,11 +52,16 @@ static void vpci_remove_device_handlers_locked(struct pci_dev *pdev)void vpci_remove_device_locked (struct pci_dev *pdev) { +struct vpci_header *header = &pdev->vpci->header; +unsigned int i; +ASSERT (spin_is_locked (&pdev->vpci_lock)); pdev->vpci_cancel_pending = true;vpci_remove_device_handlers_locked (pdev);vpci_cancel_pending_locked (pdev); +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) +rangeset_destroy (header->bars[i].mem);xfree (pdev->vpci->msix);xfree (pdev->vpci->msi);xfree (pdev->vpci);
@@ -92,6 +97,8 @@ static int run_vpci_init(struct pci_dev *pdev)int vpci_add_handlers (struct pci_dev *pdev) {struct vpci *vpci; +struct vpci_header *header; +unsigned int i;int rc;if ( !has_vpci (pdev->domain) )
@@ -108,11 +115,32 @@ int vpci_add_handlers(struct pci_dev *pdev) pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); + header = &pdev->vpci->header; +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i]; +char str[32 ]; + +snprintf (str,sizeof (str),"%pp:BAR%d" , &pdev->sbdf, i); + bar->mem =rangeset_new (pdev->domain, str, RANGESETF_no_print); +if ( !bar->mem ) + { + rc = -ENOMEM ; + goto fail; + } + } + rc =run_vpci_init (pdev);if ( rc ) -vpci_remove_device_locked (pdev); + goto fail; +spin_unlock (&pdev->vpci_lock); +return 0 ; + + fail: +vpci_remove_device_locked (pdev); +spin_unlock (&pdev->vpci_lock);return rc; }
xen/include/xen/vpci.h +2 -1
@@ -73,6 +73,7 @@ struct vpci {/* Guest view of the BAR: address and lower bits. */ uint64_t guest_reg;uint64_t size; +struct rangeset *mem;enum {VPCI_BAR_EMPTY ,VPCI_BAR_IO ,
@@ -147,9 +148,9 @@ struct vpci {struct vpci_vcpu {/* Per-vcpu structure to store state while {un}mapping of PCI BARs. */ -struct rangeset *mem;struct pci_dev *pdev;uint16_t cmd; +bool map_pending :1 ;bool rom_only :1 ; };
Signature · 1 line
2.25.1
Re: [PATCH v5 07/14] vpci/header: handle p2m range sets per BAR
Quoted 7 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Instead of handling a single range set, that contains all the memory regions of all the BARs and ROM, have them per BAR.
As the range sets are now created when a PCI device is added and destroyed when it is removed so make them named and accounted.
Quoted 6 lines
Note that rangesets were chosen here despite there being only up to 3 separate ranges in each set (typically just 1). But rangeset per BAR was chosen for the ease of implementation and existing code re-usability.
This is in preparation of making non-identity mappings in p2m for the MMIOs/ROM.
I think we don't want to support ROM for guests (at least initially), so no need to mention it here.
Quoted 43 lines
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- use named range sets for BARs (Jan)
- changes required by the new locking scheme
- updated commit message (Jan)
Since v3:
- re-workvpci_cancel_pendingaccordingly to the per-BAR handling
- s/num_mem_ranges/map_pending and s/uint8_t/bool
-ASSERT(bar->mem)inmodify_bars
- create and destroy the rangesets on add/remove--- xen/drivers/vpci/header.c | 190 +++++++++++++++++++++++++++----------- xen/drivers/vpci/vpci.c | 30 +++++- xen/include/xen/vpci.h | 3 +- 3 files changed, 166 insertions(+), 57 deletions(-)xen/drivers/vpci/header.c +9
-2@@ -137,45 +137,86 @@ bool vpci_process_pending(struct vcpu *v)return false;spin_lock (&pdev->vpci_lock); -if ( !pdev->vpci_cancel_pending && v->vpci.mem ) +if ( !pdev->vpci ) + { +spin_unlock (&pdev->vpci_lock); +return false; + } + +if ( !pdev->vpci_cancel_pending && v->vpci.map_pending ) {struct map_data data = { .d = v->domain, .map = v->vpci.cmd &PCI_COMMAND_MEMORY , }; -int rc =rangeset_consume_ranges (v->vpci.mem, map_range, &data); +struct vpci_header *header = &pdev->vpci->header; +unsigned int i;
xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 159
Quoted 10 lines
xen/drivers/vpci/header.c +4
-4-if ( rc == -ERESTART ) +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) { -spin_unlock (&pdev->vpci_lock); -return true; - } +struct vpci_bar *bar = &header->bars[i]; +int rc; +
You should check bar->mem != NULL here, there's no need to allocate a rangeset for non-mappable BARs.
Quoted 6 lines
xen/drivers/vpci/header.c +6
-0+if (rangeset_is_empty (bar->mem) ) +continue ; + + rc =rangeset_consume_ranges (bar->mem, map_range, &data); + +if ( rc == -ERESTART )
xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 172
Quoted 10 lines
xen/drivers/vpci/header.c +5
-3+ { +spin_unlock (&pdev->vpci_lock); +return true; + } -if ( pdev->vpci )/* Disable memory decoding unconditionally on failure. */ - modify_decoding(pdev, - rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, + modify_decoding(pdev, rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd,
The above seems to be an unrelated change, and also exceeds the max line length.
Quoted 42 lines
xen/drivers/vpci/header.c +21
-12!rc && v->vpci.rom_only); -if ( rc ) - { -/* -* FIXME: in case of failure remove the device from the domain. -* Note that there might still be leftover mappings. While this is -* safe for Dom0, for DomUs the domain needs to be killed in order -* to avoid leaking stale p2m mappings on failure. -*/ -if (is_hardware_domain (v->domain) ) -vpci_remove_device_locked (pdev); -else -domain_crash (v->domain); +if ( rc ) + { +/* +* FIXME: in case of failure remove the device from the domain. +* Note that there might still be leftover mappings. While this is +* safe for Dom0, for DomUs the domain needs to be killed in order +* to avoid leaking stale p2m mappings on failure. +*/ +if (is_hardware_domain (v->domain) ) +vpci_remove_device_locked (pdev); +else +domain_crash (v->domain); + +break ; + } } + + v->vpci.map_pending = false; }spin_unlock (&pdev->vpci_lock);return false; } +static void vpci_bar_remove_ranges (const struct pci_dev *pdev) +{ +struct vpci_header *header = &pdev->vpci->header; +unsigned int i;
xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 212
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+int rc; + +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i]; + +if (rangeset_is_empty (bar->mem) ) +continue ; + + rc =rangeset_remove_range (bar->mem,0 , ~0ULL );
Might be interesting to introduce a rangeset_reset function that removes all ranges. That would never fail, and thus there would be no need to check for rc.
Also I think the current rangeset_remove_range should never fail when removing all ranges, as there's nothing to allocate. Hence you can add an ASSERT_UNREACHABLE below.
Quoted 83 lines
xen/drivers/vpci/header.c +28
-15+if ( !rc ) +printk (XENLOG_ERR +"%pd %pp failed to remove range set for BAR: %d\n" , + pdev->domain, &pdev->sbdf, rc); + } +} +void vpci_cancel_pending_locked (struct pci_dev *pdev) {struct vcpu *v;@@ -185,23 +226,33 @@ void vpci_cancel_pending_locked(struct pci_dev *pdev)/* Cancel any pending work now on all vCPUs. */ for_each_vcpu ( pdev->domain, v ) { -if ( v->vpci.mem && (v->vpci.pdev == pdev) ) +if ( v->vpci.map_pending && (v->vpci.pdev == pdev) ) { -rangeset_destroy (v->vpci.mem); - v->vpci.mem =NULL ; +vpci_bar_remove_ranges (pdev); + v->vpci.map_pending = false; } } }static int __init apply_map (struct domain *d,const struct pci_dev *pdev, -struct rangeset *mem,uint16_t cmd) +uint16_t cmd) {struct map_data data = { .d = d, .map = true }; -int rc; +struct vpci_header *header = &pdev->vpci->header; +int rc =0 ; +unsigned int i; + +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i]; -while ( (rc =rangeset_consume_ranges (mem, map_range, &data)) == -ERESTART ) -process_pending_softirqs (); -rangeset_destroy (mem); +if (rangeset_is_empty (bar->mem) ) +continue ; + +while ( (rc =rangeset_consume_ranges (bar->mem, map_range, + &data)) == -ERESTART ) +process_pending_softirqs (); + }if ( !rc )modify_decoding (pdev, cmd, false);@@ -209,7 +260,7 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev, }static void defer_map (struct domain *d,struct pci_dev *pdev, -struct rangeset *mem,uint16_t cmd,bool rom_only) +uint16_t cmd,bool rom_only) {struct vcpu *curr = current;@@ -220,7 +271,7 @@ static void defer_map(struct domain *d, struct pci_dev *pdev,* started for the same device if the domain is not well-behaved. */ curr->vpci.pdev = pdev; - curr->vpci.mem = mem; + curr->vpci.map_pending = true; curr->vpci.cmd = cmd; curr->vpci.rom_only = rom_only;/* @@ -234,42 +285,40 @@ static void defer_map(struct domain *d, struct pci_dev *pdev,static int modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only) {struct vpci_header *header = &pdev->vpci->header; -struct rangeset *mem =rangeset_new (NULL ,NULL ,0 );struct pci_dev *tmp, *dev =NULL ;const struct vpci_msix *msix = pdev->vpci->msix; -unsigned int i; +unsigned int i, j;int rc; - -if ( !mem ) -return -ENOMEM ;
xen/drivers/vpci/header.c static void defer_map(struct domain *d, struct pci_dev *pdev, · line 299
Quoted 10 lines
xen/drivers/vpci/header.c +3
-2+bool map_pending;/* -* Create a rangeset that represents the current device BARs memory region +* Create a rangeset per BAR that represents the current device memory region * and compare it against all the currently active BAR memory regions. If * an overlap is found, subtract it from the region to be mapped/unmapped. * -* First fill the rangeset with all the BARs of this device or with the ROM +* First fill the rangesets with all the BARs of this device or with the ROM
^ 'all' doesn't apply anymore.
Quoted 17 lines
xen/drivers/vpci/header.c +3
-1* BAR only, depending on whether the guest is toggling the memory decode * bit of the command register, or the enable bit of the ROM BAR register. */ for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) { -const struct vpci_bar *bar = &header->bars[i]; +struct vpci_bar *bar = &header->bars[i];unsigned long start =PFN_DOWN (bar->addr);unsigned long end =PFN_DOWN (bar->addr + bar->size -1 ); +ASSERT (bar->mem); +if ( !MAPPABLE_BAR (bar) || (rom_only ? bar->type !=VPCI_BAR_ROM : (bar->type ==VPCI_BAR_ROM && !header->rom_enabled)) )continue ;
xen/drivers/vpci/header.c static void defer_map(struct domain *d, struct pci_dev *pdev, · line 322
Quoted 10 lines
xen/drivers/vpci/header.c +2
-3- rc =rangeset_add_range (mem, start, end); + rc =rangeset_add_range (bar->mem, start, end);if ( rc ) {printk (XENLOG_G_WARNING "Failed to add [%lx, %lx]: %d\n" , start, end, rc); -rangeset_destroy (mem); -return rc; + goto fail; }
I think you also need to check that BARs from the same device don't overlap themselves. This wasn't needed before because all BARs shared the same rangeset. It's not uncommon for BARs of the same device to share a page.
So you would need something like the following added to the loop:
/* Check for overlap with the already setup BAR ranges. */ for ( j = 0; j < i; j++ )
rangeset_remove_range(header->bars[j].mem, start, end);
Quoted 72 lines
xen/drivers/vpci/header.c +24
-13}@@ -280,14 +329,21 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)unsigned long end =PFN_DOWN (vmsix_table_addr (pdev->vpci, i) +vmsix_table_size (pdev->vpci, i) -1 ); - rc =rangeset_remove_range (mem, start, end); -if ( rc ) +for ( j =0 ; j <ARRAY_SIZE (header->bars); j++ ) { -printk (XENLOG_G_WARNING -"Failed to remove MSIX table [%lx, %lx]: %d\n" , - start, end, rc); -rangeset_destroy (mem); -return rc; +const struct vpci_bar *bar = &header->bars[j]; + +if (rangeset_is_empty (bar->mem) ) +continue ; + + rc =rangeset_remove_range (bar->mem, start, end); +if ( rc ) + { +printk (XENLOG_G_WARNING +"Failed to remove MSIX table [%lx, %lx]: %d\n" , + start, end, rc); + goto fail; + } } }@@ -325,7 +381,8 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)unsigned long start =PFN_DOWN (bar->addr);unsigned long end =PFN_DOWN (bar->addr + bar->size -1 ); - if ( !bar->enabled || !rangeset_overlaps_range(mem, start, end) || + if ( !bar->enabled || + !rangeset_overlaps_range(bar->mem, start, end) ||/* * If only the ROM enable bit is toggled check against other * BARs in the same device for overlaps, but not against the @@ -334,14 +391,13 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) (rom_only && tmp == pdev && bar->type == VPCI_BAR_ROM) )continue ; - rc =rangeset_remove_range (mem, start, end); + rc =rangeset_remove_range (bar->mem, start, end);if ( rc ) {spin_unlock (&tmp->vpci_lock);printk (XENLOG_G_WARNING "Failed to remove [%lx, %lx]: %d\n" , start, end, rc); -rangeset_destroy (mem); -return rc; + goto fail; } }spin_unlock (&tmp->vpci_lock);@@ -360,12 +416,36 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)* will always be to establish mappings and process all the BARs. */ ASSERT ((cmd &PCI_COMMAND_MEMORY ) && !rom_only); -return apply_map (pdev->domain, pdev, mem, cmd); +return apply_map (pdev->domain, pdev, cmd); } -defer_map (dev->domain, dev, mem, cmd, rom_only); +/* Find out how many memory ranges has left after MSI and overlaps. */ + map_pending = false; +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) +if ( !rangeset_is_empty (header->bars[i].mem) ) + {
xen/drivers/vpci/header.c static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) · line 436
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+ map_pending = true; +break ; + } + +/* +* There are cases when PCI device, root port for example, has neither +* memory space nor IO. In this case PCI command register write is +* missed resulting in the underlying PCI device not functional, so: +* - if there are no regions write the command register now +* - if there are regions then defer work and write later on
I would just say:
/* If there's no mapping work write the command register now. */
Quoted 53 lines
xen/drivers/vpci/header.c +10
-0+*/ +if ( !map_pending ) +pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd); +else +defer_map (dev->domain, dev, cmd, rom_only);return 0 ; + +fail: +/* Destroy all the ranges we may have added. */ +vpci_bar_remove_ranges (pdev); +return rc; } static void cmd_write(const struct pci_dev *pdev, unsigned int reg,xen/drivers/vpci/vpci.c +11
-0@@ -52,11 +52,16 @@ static void vpci_remove_device_handlers_locked(struct pci_dev *pdev)void vpci_remove_device_locked (struct pci_dev *pdev) { +struct vpci_header *header = &pdev->vpci->header; +unsigned int i; +ASSERT (spin_is_locked (&pdev->vpci_lock)); pdev->vpci_cancel_pending = true;vpci_remove_device_handlers_locked (pdev);vpci_cancel_pending_locked (pdev); +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) +rangeset_destroy (header->bars[i].mem);xfree (pdev->vpci->msix);xfree (pdev->vpci->msi);xfree (pdev->vpci);@@ -92,6 +97,8 @@ static int run_vpci_init(struct pci_dev *pdev)int vpci_add_handlers (struct pci_dev *pdev) {struct vpci *vpci; +struct vpci_header *header; +unsigned int i;int rc;if ( !has_vpci (pdev->domain) )@@ -108,11 +115,32 @@ int vpci_add_handlers(struct pci_dev *pdev) pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); + header = &pdev->vpci->header; +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i];
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 131
Quoted 10 lines
xen/drivers/vpci/vpci.c +10
-0+char str[32 ]; + +snprintf (str,sizeof (str),"%pp:BAR%d" , &pdev->sbdf, i); + bar->mem =rangeset_new (pdev->domain, str, RANGESETF_no_print); +if ( !bar->mem ) + { + rc = -ENOMEM ; + goto fail; + } + }
You just need the ranges for the VPCI_BAR_MEM32, VPCI_BAR_MEM64_LO and VPCI_BAR_ROM BAR types (see the MAPPABLE_BAR macro). Would it be possible to only allocate the rangeset for those BAR types?
Also this should be done in init_bars rather than here, as you would know the BAR types.
Thanks, Roger.
Re: [PATCH v5 07/14] vpci/header: handle p2m range sets per BAR
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Oleksandr Andrushchenko
xen/drivers/vpci/header.c +9
-2@@ -137,45 +137,86 @@ bool vpci_process_pending(struct vcpu *v)return false;spin_lock (&pdev->vpci_lock); -if ( !pdev->vpci_cancel_pending && v->vpci.mem ) +if ( !pdev->vpci ) + { +spin_unlock (&pdev->vpci_lock); +return false; + } + +if ( !pdev->vpci_cancel_pending && v->vpci.map_pending ) {struct map_data data = { .d = v->domain, .map = v->vpci.cmd &PCI_COMMAND_MEMORY , }; -int rc =rangeset_consume_ranges (v->vpci.mem, map_range, &data); +struct vpci_header *header = &pdev->vpci->header; +unsigned int i;
Quoted 12 lines
xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 159
Quoted 9 lines
xen/drivers/vpci/header.c +4
-4-if ( rc == -ERESTART ) +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) { -spin_unlock (&pdev->vpci_lock); -return true; - } +struct vpci_bar *bar = &header->bars[i]; +int rc; +You should check
bar->mem!= NULL here, there's no need to allocate a rangeset for non-mappable BARs.
There's a NULL check ...
Quoted 2 lines
xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 161
Quoted 2 lines
xen/drivers/vpci/header.c +2
-0+if (rangeset_is_empty (bar->mem) ) +continue ;
... inside rangeset_is_empty() (to help callers like this one).
Jan
Re: [PATCH v5 07/14] vpci/header: handle p2m range sets per BAR
In reply to Roger Pau MonnéHi, Roger!
Quoted 8 lines · Roger Pau Monné
Quoted 7 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko<[email protected]>
Instead of handling a single range set, that contains all the memory regions of all the BARs and ROM, have them per BAR.
As the range sets are now created when a PCI device is added and destroyed when it is removed so make them named and accounted.
Quoted 8 lines
Quoted 6 lines
Note that rangesets were chosen here despite there being only up to 3 separate ranges in each set (typically just 1). But rangeset per BAR was chosen for the ease of implementation and existing code re-usability.
This is in preparation of making non-identity mappings in p2m for the MMIOs/ROM.
I think we don't want to support ROM for guests (at least initially), so no need to mention it here.
Will add
Quoted 43 lines
Quoted 43 lines
Signed-off-by Oleksandr Andrushchenko<[email protected]>---
Since v4:
- use named range sets for BARs (Jan)
- changes required by the new locking scheme
- updated commit message (Jan)
Since v3:
- re-workvpci_cancel_pendingaccordingly to the per-BAR handling
- s/num_mem_ranges/map_pending and s/uint8_t/bool
-ASSERT(bar->mem)inmodify_bars
- create and destroy the rangesets on add/remove--- xen/drivers/vpci/header.c | 190 +++++++++++++++++++++++++++----------- xen/drivers/vpci/vpci.c | 30 +++++- xen/include/xen/vpci.h | 3 +- 3 files changed, 166 insertions(+), 57 deletions(-)xen/drivers/vpci/header.c +9
-2@@ -137,45 +137,86 @@ bool vpci_process_pending(struct vcpu *v)return false;spin_lock (&pdev->vpci_lock); -if ( !pdev->vpci_cancel_pending && v->vpci.mem ) +if ( !pdev->vpci ) + { +spin_unlock (&pdev->vpci_lock); +return false; + } + +if ( !pdev->vpci_cancel_pending && v->vpci.map_pending ) {struct map_data data = { .d = v->domain, .map = v->vpci.cmd &PCI_COMMAND_MEMORY , }; -int rc =rangeset_consume_ranges (v->vpci.mem, map_range, &data); +struct vpci_header *header = &pdev->vpci->header; +unsigned int i;
Quoted 11 lines
xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 159
Quoted 9 lines
xen/drivers/vpci/header.c +4
-4-if ( rc == -ERESTART ) +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) { -spin_unlock (&pdev->vpci_lock); -return true; - } +struct vpci_bar *bar = &header->bars[i]; +int rc; +You should check
bar->mem!= NULL here, there's no need to allocate a rangeset for non-mappable BARs.
Answered by Jan already: no need as rangeset_is_empty already handles NULL pointer
Quoted 11 lines
Quoted 11 lines
xen/drivers/vpci/header.c +10
-0+if (rangeset_is_empty (bar->mem) ) +continue ; + + rc =rangeset_consume_ranges (bar->mem, map_range, &data); + +if ( rc == -ERESTART ) + { +spin_unlock (&pdev->vpci_lock); +return true; + }
Quoted 7 lines
xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 172
Quoted 5 lines
xen/drivers/vpci/header.c +1
-3-if ( pdev->vpci )/* Disable memory decoding unconditionally on failure. */ - modify_decoding(pdev, - rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, + modify_decoding(pdev, rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd,The above seems to be an unrelated change, and also exceeds the max line length.
Sure, will try to fit
Quoted 47 lines
Quoted 47 lines
xen/drivers/vpci/header.c +26
-12!rc && v->vpci.rom_only); -if ( rc ) - { -/* -* FIXME: in case of failure remove the device from the domain. -* Note that there might still be leftover mappings. While this is -* safe for Dom0, for DomUs the domain needs to be killed in order -* to avoid leaking stale p2m mappings on failure. -*/ -if (is_hardware_domain (v->domain) ) -vpci_remove_device_locked (pdev); -else -domain_crash (v->domain); +if ( rc ) + { +/* +* FIXME: in case of failure remove the device from the domain. +* Note that there might still be leftover mappings. While this is +* safe for Dom0, for DomUs the domain needs to be killed in order +* to avoid leaking stale p2m mappings on failure. +*/ +if (is_hardware_domain (v->domain) ) +vpci_remove_device_locked (pdev); +else +domain_crash (v->domain); + +break ; + } } + + v->vpci.map_pending = false; }spin_unlock (&pdev->vpci_lock);return false; } +static void vpci_bar_remove_ranges (const struct pci_dev *pdev) +{ +struct vpci_header *header = &pdev->vpci->header; +unsigned int i; +int rc; + +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i];
Quoted 8 lines
xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 212
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+ +if (rangeset_is_empty (bar->mem) ) +continue ; + + rc =rangeset_remove_range (bar->mem,0 , ~0ULL );Might be interesting to introduce a
rangeset_resetfunction that removes all ranges. That would never fail, and thus there would be no need to check for rc.
Well, there is a single user of that as of now, so not sure it is worth it yet And if we re-allocate pdev->vpci then there might be no need for this at all
Quoted 2 lines
Also I think the current
rangeset_remove_rangeshould never fail when removing all ranges, as there's nothing to allocate.
Agree
Quoted 88 lines
Hence you can add
anASSERT_UNREACHABLEbelow.Quoted 85 lines
xen/drivers/vpci/header.c +29
-15+if ( !rc ) +printk (XENLOG_ERR +"%pd %pp failed to remove range set for BAR: %d\n" , + pdev->domain, &pdev->sbdf, rc); + } +} +void vpci_cancel_pending_locked (struct pci_dev *pdev) {struct vcpu *v;@@ -185,23 +226,33 @@ void vpci_cancel_pending_locked(struct pci_dev *pdev)/* Cancel any pending work now on all vCPUs. */ for_each_vcpu ( pdev->domain, v ) { -if ( v->vpci.mem && (v->vpci.pdev == pdev) ) +if ( v->vpci.map_pending && (v->vpci.pdev == pdev) ) { -rangeset_destroy (v->vpci.mem); - v->vpci.mem =NULL ; +vpci_bar_remove_ranges (pdev); + v->vpci.map_pending = false; } } }static int __init apply_map (struct domain *d,const struct pci_dev *pdev, -struct rangeset *mem,uint16_t cmd) +uint16_t cmd) {struct map_data data = { .d = d, .map = true }; -int rc; +struct vpci_header *header = &pdev->vpci->header; +int rc =0 ; +unsigned int i; + +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i]; -while ( (rc =rangeset_consume_ranges (mem, map_range, &data)) == -ERESTART ) -process_pending_softirqs (); -rangeset_destroy (mem); +if (rangeset_is_empty (bar->mem) ) +continue ; + +while ( (rc =rangeset_consume_ranges (bar->mem, map_range, + &data)) == -ERESTART ) +process_pending_softirqs (); + }if ( !rc )modify_decoding (pdev, cmd, false);@@ -209,7 +260,7 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev, }static void defer_map (struct domain *d,struct pci_dev *pdev, -struct rangeset *mem,uint16_t cmd,bool rom_only) +uint16_t cmd,bool rom_only) {struct vcpu *curr = current;@@ -220,7 +271,7 @@ static void defer_map(struct domain *d, struct pci_dev *pdev,* started for the same device if the domain is not well-behaved. */ curr->vpci.pdev = pdev; - curr->vpci.mem = mem; + curr->vpci.map_pending = true; curr->vpci.cmd = cmd; curr->vpci.rom_only = rom_only;/* @@ -234,42 +285,40 @@ static void defer_map(struct domain *d, struct pci_dev *pdev,static int modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only) {struct vpci_header *header = &pdev->vpci->header; -struct rangeset *mem =rangeset_new (NULL ,NULL ,0 );struct pci_dev *tmp, *dev =NULL ;const struct vpci_msix *msix = pdev->vpci->msix; -unsigned int i; +unsigned int i, j;int rc; - -if ( !mem ) -return -ENOMEM ; +bool map_pending;
Quoted 9 lines
xen/drivers/vpci/header.c static void defer_map(struct domain *d, struct pci_dev *pdev, · line 299
Quoted 8 lines
xen/drivers/vpci/header.c +2
-2/* -* Create a rangeset that represents the current device BARs memory region +* Create a rangeset per BAR that represents the current device memory region * and compare it against all the currently active BAR memory regions. If * an overlap is found, subtract it from the region to be mapped/unmapped. * -* First fill the rangeset with all the BARs of this device or with the ROM +* First fill the rangesets with all the BARs of this device or with the ROM ^ 'all' doesn't apply anymore.
Will fix
Quoted 32 lines
Quoted 17 lines
xen/drivers/vpci/header.c +3
-1* BAR only, depending on whether the guest is toggling the memory decode * bit of the command register, or the enable bit of the ROM BAR register. */ for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) { -const struct vpci_bar *bar = &header->bars[i]; +struct vpci_bar *bar = &header->bars[i];unsigned long start =PFN_DOWN (bar->addr);unsigned long end =PFN_DOWN (bar->addr + bar->size -1 ); +ASSERT (bar->mem); +if ( !MAPPABLE_BAR (bar) || (rom_only ? bar->type !=VPCI_BAR_ROM : (bar->type ==VPCI_BAR_ROM && !header->rom_enabled)) )continue ;xen/drivers/vpci/header.c static void defer_map(struct domain *d, struct pci_dev *pdev, · line 322
Quoted 10 lines
xen/drivers/vpci/header.c +2
-3- rc =rangeset_add_range (mem, start, end); + rc =rangeset_add_range (bar->mem, start, end);if ( rc ) {printk (XENLOG_G_WARNING "Failed to add [%lx, %lx]: %d\n" , start, end, rc); -rangeset_destroy (mem); -return rc; + goto fail; }I think you also need to check that BARs from the same device don't overlap themselves. This wasn't needed before because all BARs shared the same rangeset. It's not uncommon for BARs of the same device to share a page.
Quoted 5 lines
So you would need something like the following added to the loop:
/* Check for overlap with the already setup BAR ranges. */ for ( j = 0; j < i; j++ )
rangeset_remove_range(header->bars[j].mem, start, end);
Good point
Quoted 84 lines
Quoted 72 lines
xen/drivers/vpci/header.c +24
-13}@@ -280,14 +329,21 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)unsigned long end =PFN_DOWN (vmsix_table_addr (pdev->vpci, i) +vmsix_table_size (pdev->vpci, i) -1 ); - rc =rangeset_remove_range (mem, start, end); -if ( rc ) +for ( j =0 ; j <ARRAY_SIZE (header->bars); j++ ) { -printk (XENLOG_G_WARNING -"Failed to remove MSIX table [%lx, %lx]: %d\n" , - start, end, rc); -rangeset_destroy (mem); -return rc; +const struct vpci_bar *bar = &header->bars[j]; + +if (rangeset_is_empty (bar->mem) ) +continue ; + + rc =rangeset_remove_range (bar->mem, start, end); +if ( rc ) + { +printk (XENLOG_G_WARNING +"Failed to remove MSIX table [%lx, %lx]: %d\n" , + start, end, rc); + goto fail; + } } }@@ -325,7 +381,8 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)unsigned long start =PFN_DOWN (bar->addr);unsigned long end =PFN_DOWN (bar->addr + bar->size -1 ); - if ( !bar->enabled || !rangeset_overlaps_range(mem, start, end) || + if ( !bar->enabled || + !rangeset_overlaps_range(bar->mem, start, end) ||/* * If only the ROM enable bit is toggled check against other * BARs in the same device for overlaps, but not against the @@ -334,14 +391,13 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) (rom_only && tmp == pdev && bar->type == VPCI_BAR_ROM) )continue ; - rc =rangeset_remove_range (mem, start, end); + rc =rangeset_remove_range (bar->mem, start, end);if ( rc ) {spin_unlock (&tmp->vpci_lock);printk (XENLOG_G_WARNING "Failed to remove [%lx, %lx]: %d\n" , start, end, rc); -rangeset_destroy (mem); -return rc; + goto fail; } }spin_unlock (&tmp->vpci_lock);@@ -360,12 +416,36 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)* will always be to establish mappings and process all the BARs. */ ASSERT ((cmd &PCI_COMMAND_MEMORY ) && !rom_only); -return apply_map (pdev->domain, pdev, mem, cmd); +return apply_map (pdev->domain, pdev, cmd); } -defer_map (dev->domain, dev, mem, cmd, rom_only); +/* Find out how many memory ranges has left after MSI and overlaps. */ + map_pending = false; +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) +if ( !rangeset_is_empty (header->bars[i].mem) ) + {xen/drivers/vpci/header.c static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) · line 436
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+ map_pending = true; +break ; + } + +/* +* There are cases when PCI device, root port for example, has neither +* memory space nor IO. In this case PCI command register write is +* missed resulting in the underlying PCI device not functional, so: +* - if there are no regions write the command register now +* - if there are regions then defer work and write later on I would just say:
Quoted 1 line
/* If there's no mapping work write the command register now. */
Ok
Quoted 58 lines
Quoted 58 lines
xen/drivers/vpci/header.c +10
-0+*/ +if ( !map_pending ) +pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd); +else +defer_map (dev->domain, dev, cmd, rom_only);return 0 ; + +fail: +/* Destroy all the ranges we may have added. */ +vpci_bar_remove_ranges (pdev); +return rc; } static void cmd_write(const struct pci_dev *pdev, unsigned int reg,xen/drivers/vpci/vpci.c +16
-0@@ -52,11 +52,16 @@ static void vpci_remove_device_handlers_locked(struct pci_dev *pdev)void vpci_remove_device_locked (struct pci_dev *pdev) { +struct vpci_header *header = &pdev->vpci->header; +unsigned int i; +ASSERT (spin_is_locked (&pdev->vpci_lock)); pdev->vpci_cancel_pending = true;vpci_remove_device_handlers_locked (pdev);vpci_cancel_pending_locked (pdev); +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) +rangeset_destroy (header->bars[i].mem);xfree (pdev->vpci->msix);xfree (pdev->vpci->msi);xfree (pdev->vpci);@@ -92,6 +97,8 @@ static int run_vpci_init(struct pci_dev *pdev)int vpci_add_handlers (struct pci_dev *pdev) {struct vpci *vpci; +struct vpci_header *header; +unsigned int i;int rc;if ( !has_vpci (pdev->domain) )@@ -108,11 +115,32 @@ int vpci_add_handlers(struct pci_dev *pdev) pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); + header = &pdev->vpci->header; +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i]; +char str[32 ]; + +snprintf (str,sizeof (str),"%pp:BAR%d" , &pdev->sbdf, i); + bar->mem =rangeset_new (pdev->domain, str, RANGESETF_no_print); +if ( !bar->mem )
Quoted 8 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 131
Quoted 5 lines
xen/drivers/vpci/vpci.c +5
-0+ { + rc = -ENOMEM ; + goto fail; + } + }You just need the ranges for the
VPCI_BAR_MEM32,VPCI_BAR_MEM64_LOandVPCI_BAR_ROMBAR types (see theMAPPABLE_BARmacro). Would it be possible to only allocate the rangeset for those BAR types?
I guess so
Quoted 2 lines
Also this should be done in
init_barsrather than here, as you would know the BAR types.
So, if we allocate these in init_bars so where are they destroyed then? I think this should be vpci_remove_device and from this POV it would be good to keep alloc/free code close to each other, e.g. vpci_add_handlers/vpci_remove_device in the same file
Quoted 1 line
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 07/14] vpci/header: handle p2m range sets per BAR
Quoted 25 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 14 lines · Roger Pau Monné
Quoted 13 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +9
-0@@ -108,11 +115,32 @@ int vpci_add_handlers(struct pci_dev *pdev) pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); + header = &pdev->vpci->header; +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i]; +char str[32 ]; + +snprintf (str,sizeof (str),"%pp:BAR%d" , &pdev->sbdf, i); + bar->mem =rangeset_new (pdev->domain, str, RANGESETF_no_print); +if ( !bar->mem )Quoted 8 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 131
Quoted 5 lines
xen/drivers/vpci/vpci.c +5
-0+ { + rc = -ENOMEM ; + goto fail; + } + }You just need the ranges for the
VPCI_BAR_MEM32,VPCI_BAR_MEM64_LOandVPCI_BAR_ROMBAR types (see theMAPPABLE_BARmacro). Would it be possible to only allocate the rangeset for those BAR types?
Quoted 7 lines
I guess so
Quoted 2 lines
Also this should be done in
init_barsrather than here, as you would know the BAR types.So, if we allocate these in
init_barsso where are they destroyed then? I think this should bevpci_remove_deviceand from this POV it would be good to keep alloc/free code close to each other, e.g. vpci_add_handlers/vpci_remove_device in the same file
The alloc/free is asymmetric already, as vpci->{msix,msi} gets allocated in init_msi{x} but freed at vpci_remove_device.
Thanks, Roger.
Re: [PATCH v5 07/14] vpci/header: handle p2m range sets per BAR
Hi, Roger!
Quoted 27 lines · Roger Pau Monné
Quoted 26 lines · Oleksandr Andrushchenko
Hi, Roger!
Quoted 14 lines · Roger Pau Monné
Quoted 13 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +9
-0@@ -108,11 +115,32 @@ int vpci_add_handlers(struct pci_dev *pdev) pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); + header = &pdev->vpci->header; +for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) + { +struct vpci_bar *bar = &header->bars[i]; +char str[32 ]; + +snprintf (str,sizeof (str),"%pp:BAR%d" , &pdev->sbdf, i); + bar->mem =rangeset_new (pdev->domain, str, RANGESETF_no_print); +if ( !bar->mem )Quoted 8 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 131
Quoted 5 lines
xen/drivers/vpci/vpci.c +5
-0+ { + rc = -ENOMEM ; + goto fail; + } + }You just need the ranges for the
VPCI_BAR_MEM32,VPCI_BAR_MEM64_LOandVPCI_BAR_ROMBAR types (see theMAPPABLE_BARmacro). Would it be possible to only allocate the rangeset for those BAR types?I guess so
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Also this should be done in
init_barsrather than here, as you would know the BAR types.So, if we allocate these in
init_barsso where are they destroyed then? I think this should bevpci_remove_deviceand from this POV it would be good to keep alloc/free code close to each other, e.g. vpci_add_handlers/vpci_remove_device in the same fileThe alloc/free is asymmetric already, as vpci->{msix,msi} gets allocated in init_msi{x} but freed at
vpci_remove_device.
Makes sense, I will implement as you suggest
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
PATCH v5 8/14 vpci/header: program p2m with guest BAR view
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
Take into account guest's BAR view and program its p2m accordingly:
gfn is guest's view of the BAR and mfn is the physical BAR value as set up by the PCI bus driver in the hardware domain.
This way hardware domain sees physical BAR values and guest sees emulated ones.
---
Since v4:
- moved start_{gfn|mfn} calculation into map_range
- pass vpci_bar in the map_data instead of start_{gfn|mfn}
- s/guest_addr/guest_reg
Since v3:
- updated comment (Roger)
- removed gfn_add(map->start_gfn, rc); which is wrong
- use v->domain instead of v->vpci.pdev->domain
- removed odd e.g. in comment
- s/d%d/%pd in altered code
- use gdprintk for map/unmap logs
Since v2:
- improve readability for data.start_gfn and restructure ?: construct Since v1:
- s/MSI/MSI-X in comments
---
--- xen/drivers/vpci/header.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)
xen/drivers/vpci/header.c +26 -4
@@ -30,6 +30,7 @@struct map_data {struct domain *d; +const struct vpci_bar *bar;bool map; };
@@ -41,8 +42,25 @@ static int map_range(unsigned long s, unsigned long e, void *data,for ( ; ; ) { +/* Start address of the BAR as seen by the guest. */ +gfn_t start_gfn =_gfn (PFN_DOWN (is_hardware_domain (map->d) + ? map->bar->addr + : map->bar->guest_reg)); +/* Physical start address of the BAR. */ +mfn_t start_mfn =_mfn (PFN_DOWN (map->bar->addr));unsigned long size = e - s +1 ; +/* +* Ranges to be mapped don't always start at the BAR start address, as +* there can be holes or partially consumed ranges. Account for the +* offset of the current address from the BAR start. +*/ + start_gfn =gfn_add (start_gfn, s -mfn_x (start_mfn)); + +gdprintk (XENLOG_G_DEBUG , +"%smap [%lx, %lx] -> %#" PRI_gfn" for %pd\n" , + map->map ?"" :"un" , s, e,gfn_x (start_gfn), + map->d);/* * ARM TODOs: * - On ARM whether the memory is prefetchable or not should be passed
@@ -52,8 +70,10 @@ static int map_range(unsigned long s, unsigned long e, void *data,* - {un}map_mmio_regions doesn't support preemption. */ - rc = map->map ?map_mmio_regions (map->d,_gfn (s), size,_mfn (s)) - :unmap_mmio_regions (map->d,_gfn (s), size,_mfn (s)); + rc = map->map ?map_mmio_regions (map->d, start_gfn, + size,_mfn (s)) + :unmap_mmio_regions (map->d, start_gfn, + size,_mfn (s));if ( rc ==0 ) { *c += size;
@@ -62,8 +82,8 @@ static int map_range(unsigned long s, unsigned long e, void *data,if ( rc <0 ) {printk (XENLOG_G_WARNING -"Failed to identity %smap [%lx, %lx] for d%d: %d\n" , - map->map ?"" :"un" , s, e, map->d->domain_id, rc); +"Failed to identity %smap [%lx, %lx] for %pd: %d\n" , + map->map ?"" :"un" , s, e, map->d, rc);break ; }ASSERT (rc < size);
@@ -160,6 +180,7 @@ bool vpci_process_pending(struct vcpu *v)if (rangeset_is_empty (bar->mem) )continue ; + data.bar = bar; rc =rangeset_consume_ranges (bar->mem, map_range, &data);if ( rc == -ERESTART )
@@ -249,6 +270,7 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev,if (rangeset_is_empty (bar->mem) )continue ; + data.bar = bar;while ( (rc =rangeset_consume_ranges (bar->mem, map_range, &data)) == -ERESTART )process_pending_softirqs ();
Signature · 1 line
2.25.1
Re: [PATCH v5 08/14] vpci/header: program p2m with guest BAR view
Quoted 51 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Take into account guest's BAR view and program its p2m accordingly:
gfn is guest's view of the BAR and mfn is the physical BAR value as set up by the PCI bus driver in the hardware domain.
This way hardware domain sees physical BAR values and guest sees emulated ones.Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- moved start_{gfn|mfn} calculation intomap_range
- passvpci_barin themap_datainstead of start_{gfn|mfn}
- s/guest_addr/guest_reg
Since v3:
- updated comment (Roger)
- removedgfn_add(map->start_gfn, rc); which is wrong
- usev->domaininstead ofv->vpci.pdev->domain
- removed odd e.g. in comment
- s/d%d/%pd in altered code
- use gdprintk for map/unmap logs
Since v2:
- improve readability for data.start_gfn and restructure ?: construct Since v1:
- s/MSI/MSI-X in comments---
--- xen/drivers/vpci/header.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)xen/drivers/vpci/header.c +5
-0@@ -30,6 +30,7 @@struct map_data {struct domain *d; +const struct vpci_bar *bar;bool map; };@@ -41,8 +42,25 @@ static int map_range(unsigned long s, unsigned long e, void *data,for ( ; ; ) { +/* Start address of the BAR as seen by the guest. */ +gfn_t start_gfn =_gfn (PFN_DOWN (is_hardware_domain (map->d) + ? map->bar->addr + : map->bar->guest_reg));
xen/drivers/vpci/header.c static int map_range(unsigned long s, unsigned long e, void *data, · line 58
Quoted 10 lines
xen/drivers/vpci/header.c +8
-0+/* Physical start address of the BAR. */ +mfn_t start_mfn =_mfn (PFN_DOWN (map->bar->addr));unsigned long size = e - s +1 ; +/* +* Ranges to be mapped don't always start at the BAR start address, as +* there can be holes or partially consumed ranges. Account for the +* offset of the current address from the BAR start. +*/ + start_gfn =gfn_add (start_gfn, s -mfn_x (start_mfn));
When doing guests mappings the rangeset should represent the guest physical memory space, not the host one. So that collisions in the guest p2m can be avoided. Also a guest should be allowed to map the same mfn into multiple gfn. For example multiple BARs could share the same physical page on the host and the guest might like to map them at different pages in it's physmap.
xen/drivers/vpci/header.c static int map_range(unsigned long s, unsigned long e, void *data, · line 63
Quoted 5 lines
xen/drivers/vpci/header.c +5
-0+ +gdprintk (XENLOG_G_DEBUG , +"%smap [%lx, %lx] -> %#" PRI_gfn" for %pd\n" , + map->map ?"" :"un" , s, e,gfn_x (start_gfn), + map->d);
That's too chatty IMO, I could be fine with printing something along this lines from modify_bars, but not here because that function can be preempted and called multiple times.
Quoted 17 lines
xen/drivers/vpci/header.c +4
-2/* * ARM TODOs: * - On ARM whether the memory is prefetchable or not should be passed @@ -52,8 +70,10 @@ static int map_range(unsigned long s, unsigned long e, void *data,* - {un}map_mmio_regions doesn't support preemption. */ - rc = map->map ?map_mmio_regions (map->d,_gfn (s), size,_mfn (s)) - :unmap_mmio_regions (map->d,_gfn (s), size,_mfn (s)); + rc = map->map ?map_mmio_regions (map->d, start_gfn, + size,_mfn (s)) + :unmap_mmio_regions (map->d, start_gfn, + size,_mfn (s));if ( rc ==0 ) { *c += size;
xen/drivers/vpci/header.c static int map_range(unsigned long s, unsigned long e, void *data, · line 86
Quoted 7 lines
xen/drivers/vpci/header.c +2
-2@@ -62,8 +82,8 @@ static int map_range(unsigned long s, unsigned long e, void *data,if ( rc <0 ) {printk (XENLOG_G_WARNING -"Failed to identity %smap [%lx, %lx] for d%d: %d\n" , - map->map ?"" :"un" , s, e, map->d->domain_id, rc); +"Failed to identity %smap [%lx, %lx] for %pd: %d\n" , + map->map ?"" :"un" , s, e, map->d, rc);
You need to adjust the message here, as this is no longer an identity map for domUs.
Thanks, Roger.
Re: [PATCH v5 08/14] vpci/header: program p2m with guest BAR view
Hi, Roger!
Quoted 56 lines · Roger Pau Monné
Quoted 55 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Take into account guest's BAR view and program its p2m accordingly:
gfn is guest's view of the BAR and mfn is the physical BAR value as set up by the PCI bus driver in the hardware domain.
This way hardware domain sees physical BAR values and guest sees emulated ones.Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- moved start_{gfn|mfn} calculation intomap_range
- passvpci_barin themap_datainstead of start_{gfn|mfn}
- s/guest_addr/guest_reg
Since v3:
- updated comment (Roger)
- removedgfn_add(map->start_gfn, rc); which is wrong
- usev->domaininstead ofv->vpci.pdev->domain
- removed odd e.g. in comment
- s/d%d/%pd in altered code
- use gdprintk for map/unmap logs
Since v2:
- improve readability for data.start_gfn and restructure ?: construct Since v1:
- s/MSI/MSI-X in comments---
--- xen/drivers/vpci/header.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)xen/drivers/vpci/header.c +7
-0@@ -30,6 +30,7 @@struct map_data {struct domain *d; +const struct vpci_bar *bar;bool map; };@@ -41,8 +42,25 @@ static int map_range(unsigned long s, unsigned long e, void *data,for ( ; ; ) { +/* Start address of the BAR as seen by the guest. */ +gfn_t start_gfn =_gfn (PFN_DOWN (is_hardware_domain (map->d) + ? map->bar->addr + : map->bar->guest_reg)); +/* Physical start address of the BAR. */ +mfn_t start_mfn =_mfn (PFN_DOWN (map->bar->addr));unsigned long size = e - s +1 ;
Quoted 8 lines
xen/drivers/vpci/header.c static int map_range(unsigned long s, unsigned long e, void *data, · line 58
Quoted 6 lines
xen/drivers/vpci/header.c +6
-0+/* +* Ranges to be mapped don't always start at the BAR start address, as +* there can be holes or partially consumed ranges. Account for the +* offset of the current address from the BAR start. +*/ + start_gfn =gfn_add (start_gfn, s -mfn_x (start_mfn));When doing guests mappings the rangeset should represent the guest physical memory space, not the host one.
So, it does
Quoted 5 lines
So that collisions in the
guest p2m can be avoided. Also a guest should be allowed to map the same mfn into multiple gfn. For example multiple BARs could share the same physical page on the host and the guest might like to map them at different pages in it's physmap.
There is no such restriction imposed
Quoted 9 lines
xen/drivers/vpci/header.c static int map_range(unsigned long s, unsigned long e, void *data, · line 63
Quoted 6 lines
xen/drivers/vpci/header.c +5
-0+ +gdprintk (XENLOG_G_DEBUG , +"%smap [%lx, %lx] -> %#" PRI_gfn" for %pd\n" , + map->map ?"" :"un" , s, e,gfn_x (start_gfn), + map->d);That's too chatty IMO, I could be fine with printing something along this lines from
modify_bars, but not here because that function can be preempted and called multiple times.
Ok, will move to modify_bars as these prints are really helpful for debug
Quoted 19 lines
Quoted 19 lines
xen/drivers/vpci/header.c +4
-2/* * ARM TODOs: * - On ARM whether the memory is prefetchable or not should be passed @@ -52,8 +70,10 @@ static int map_range(unsigned long s, unsigned long e, void *data,* - {un}map_mmio_regions doesn't support preemption. */ - rc = map->map ?map_mmio_regions (map->d,_gfn (s), size,_mfn (s)) - :unmap_mmio_regions (map->d,_gfn (s), size,_mfn (s)); + rc = map->map ?map_mmio_regions (map->d, start_gfn, + size,_mfn (s)) + :unmap_mmio_regions (map->d, start_gfn, + size,_mfn (s));if ( rc ==0 ) { *c += size;@@ -62,8 +82,8 @@ static int map_range(unsigned long s, unsigned long e, void *data,if ( rc <0 )
Quoted 8 lines
xen/drivers/vpci/header.c static int map_range(unsigned long s, unsigned long e, void *data, · line 86
Quoted 6 lines
xen/drivers/vpci/header.c +2
-2{printk (XENLOG_G_WARNING -"Failed to identity %smap [%lx, %lx] for d%d: %d\n" , - map->map ?"" :"un" , s, e, map->d->domain_id, rc); +"Failed to identity %smap [%lx, %lx] for %pd: %d\n" , + map->map ?"" :"un" , s, e, map->d, rc);You need to adjust the message here, as this is no longer an identity map for domUs.
Sure
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 08/14] vpci/header: program p2m with guest BAR view
Quoted 9 lines
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +4
-0+ gdprintk(XENLOG_G_DEBUG, + "%smap [%lx, %lx] -> %#"PRI_gfn" for %pd\n", + map->map ? "" : "un", s, e, gfn_x(start_gfn), + map->d);That's too chatty IMO, I could be fine with printing something along this lines from
modify_bars, but not here because that function can be preempted and called multiple times.Ok, will move to
modify_barsas these prints are really helpful for debug
I tried to implement the same, but now in init_bars:
xen/drivers/vpci/header.c +40 -4
@@ -57,10 +57,6 @@ static int map_range(unsigned long s, unsigned long e, void *data,*/ start_gfn = gfn_add(start_gfn, s - mfn_x(start_mfn)); - gdprintk(XENLOG_G_DEBUG, - "%smap [%lx, %lx] -> %#"PRI_gfn" for %pd\n", - map->map ? "" : "un", s, e, gfn_x(start_gfn), - map->d); /** ARM TODOs: * - On ARM whether the memory is prefetchable or not should be passed
@@ -258,6 +254,28 @@ static void defer_map(struct domain *d, struct pci_dev *pdev, raise_softirq(SCHEDULE_SOFTIRQ); } +static int print_range (unsigned long s,unsigned long e,void *data) +{ + const struct map_data *map = data; + + for ( ; ; ) + { + gfn_t start_gfn = _gfn(PFN_DOWN(is_hardware_domain(map->d) + ? map->bar->addr + : map->bar->guest_reg)); + mfn_t start_mfn = _mfn(PFN_DOWN(map->bar->addr)); + + start_gfn = gfn_add(start_gfn, s - mfn_x(start_mfn)); + + gdprintk(XENLOG_G_DEBUG, + "%smap [%lx, %lx] -> %#"PRI_gfn" for %pd\n", + map->map ? "" : "un", s, e, gfn_x(start_gfn), + map->d); + } + + return 0; +} + static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) { struct vpci_header *header = &pdev->vpci->header;
@@ -423,7 +441,25 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) if ( !map_pending ) pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd); else + { + struct map_data data = { + .d = pdev->domain, + .map = cmd & PCI_COMMAND_MEMORY, + }; + + for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) + { + const struct vpci_bar *bar = &header->bars[i]; + + if ( rangeset_is_empty(bar->mem) ) + continue; + + data.bar = bar; + rc = rangeset_report_ranges(bar->mem, 0, ~0ul, print_range, &data); + } + defer_map(dev->domain, dev, cmd, rom_only); + } return 0;
To me, to implement a single DEBUG print, it is a bit an overkill. I do understand your concerns that "that function can be preempted and called multiple times", but taking look at the code above I think we can accept that for DEBUG builds.
Could you please let me know if I:
1. Still need to implement (the patch above)
2. Drop DEBUG prints (those are really useful while debugging)
3. Leave the print where it was in map_range
Thank you in advance,
Oleksandr
Re: [PATCH v5 08/14] vpci/header: program p2m with guest BAR view
Quoted 38 lines · Oleksandr Andrushchenko
Quoted 9 lines
Quoted 8 lines
xen/drivers/vpci/header.c static void defer_map(struct domain *d, struct pci_dev *pdev, · line 273
Quoted 5 lines
xen/drivers/vpci/header.c +4
-0+gdprintk (XENLOG_G_DEBUG , +"%smap [%lx, %lx] -> %#" PRI_gfn" for %pd\n" , + map->map ?"" :"un" , s, e,gfn_x (start_gfn), + map->d);That's too chatty IMO, I could be fine with printing something along this lines from
modify_bars, but not here because that function can be preempted and called multiple times.Ok, will move to
modify_barsas these prints are really helpful for debugI tried to implement the same, but now in
init_bars:xen/drivers/vpci/header.c +8
-4@@ -57,10 +57,6 @@ static int map_range(unsigned long s, unsigned long e, void *data,*/ start_gfn = gfn_add(start_gfn, s - mfn_x(start_mfn)); - gdprintk(XENLOG_G_DEBUG, - "%smap [%lx, %lx] -> %#"PRI_gfn" for %pd\n", - map->map ? "" : "un", s, e, gfn_x(start_gfn), - map->d); /** ARM TODOs: * - On ARM whether the memory is prefetchable or not should be passed @@ -258,6 +254,28 @@ static void defer_map(struct domain *d, struct pci_dev *pdev, raise_softirq(SCHEDULE_SOFTIRQ); } +static int print_range (unsigned long s,unsigned long e,void *data) +{ + const struct map_data *map = data; + + for ( ; ; ) + { + gfn_t start_gfn = _gfn(PFN_DOWN(is_hardware_domain(map->d) + ? map->bar->addr
xen/drivers/vpci/header.c static void defer_map(struct domain *d, struct pci_dev *pdev, · line 274
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+ : map->bar->guest_reg)); + mfn_t start_mfn = _mfn(PFN_DOWN(map->bar->addr)); + + start_gfn = gfn_add(start_gfn, s - mfn_x(start_mfn)); + + gdprintk(XENLOG_G_DEBUG, + "%smap [%lx, %lx] -> %#"PRI_gfn" for %pd\n", + map->map ? "" : "un", s, e, gfn_x(start_gfn), + map->d); + }
This is an infinite loop AFAICT. Why do you need the for for?
Quoted 16 lines
xen/drivers/vpci/header.c +9
-0+ + return 0; +} + static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) { struct vpci_header *header = &pdev->vpci->header;@@ -423,7 +441,25 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) if ( !map_pending ) pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd); else + { + struct map_data data = { + .d = pdev->domain, + .map = cmd & PCI_COMMAND_MEMORY, + };
xen/drivers/vpci/header.c static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) · line 458
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+ + for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) + { + const struct vpci_bar *bar = &header->bars[i]; + + if ( rangeset_is_empty(bar->mem) ) + continue; + + data.bar = bar; + rc = rangeset_report_ranges(bar->mem, 0, ~0ul, print_range, &data);
Since this is per-BAR we should also print that information and the SBDF of the device, ie:
%pd SBDF: (ROM)BAR%u %map [%lx, %lx] -> ...
Quoted 8 lines
xen/drivers/vpci/header.c +3
-0+ } + defer_map(dev->domain, dev, cmd, rom_only); + } return 0;
Quoted 4 lines
To me, to implement a single DEBUG print, it is a bit an overkill. I do understand your concerns that "that function can be preempted and called multiple times", but taking look at the code above I think we can accept that for DEBUG builds.
It might be better if you print the per BAR positions at the top of modify_bars, where each BAR is added to the rangeset? Or do you care about reporting the holes also?
Thanks, Roger.
Re: [PATCH v5 08/14] vpci/header: program p2m with guest BAR view
Quoted 89 lines · Roger Pau Monné
Quoted 37 lines · Oleksandr Andrushchenko
Quoted 8 lines
Quoted 7 lines
xen/drivers/vpci/header.c static void defer_map(struct domain *d, struct pci_dev *pdev, · line 273
Quoted 4 lines
xen/drivers/vpci/header.c +4
-0+gdprintk (XENLOG_G_DEBUG , +"%smap [%lx, %lx] -> %#" PRI_gfn" for %pd\n" , + map->map ?"" :"un" , s, e,gfn_x (start_gfn), + map->d);That's too chatty IMO, I could be fine with printing something along this lines from
modify_bars, but not here because that function can be preempted and called multiple times.Ok, will move to
modify_barsas these prints are really helpful for debugI tried to implement the same, but now in
init_bars:xen/drivers/vpci/header.c +8
-4@@ -57,10 +57,6 @@ static int map_range(unsigned long s, unsigned long e, void *data,*/ start_gfn = gfn_add(start_gfn, s - mfn_x(start_mfn)); - gdprintk(XENLOG_G_DEBUG, - "%smap [%lx, %lx] -> %#"PRI_gfn" for %pd\n", - map->map ? "" : "un", s, e, gfn_x(start_gfn), - map->d); /** ARM TODOs: * - On ARM whether the memory is prefetchable or not should be passed @@ -258,6 +254,28 @@ static void defer_map(struct domain *d, struct pci_dev *pdev, raise_softirq(SCHEDULE_SOFTIRQ); } +static int print_range (unsigned long s,unsigned long e,void *data) +{ + const struct map_data *map = data; + + for ( ; ; ) + { + gfn_t start_gfn = _gfn(PFN_DOWN(is_hardware_domain(map->d) + ? map->bar->addrxen/drivers/vpci/header.c static void defer_map(struct domain *d, struct pci_dev *pdev, · line 274
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+ : map->bar->guest_reg)); + mfn_t start_mfn = _mfn(PFN_DOWN(map->bar->addr)); + + start_gfn = gfn_add(start_gfn, s - mfn_x(start_mfn)); + + gdprintk(XENLOG_G_DEBUG, + "%smap [%lx, %lx] -> %#"PRI_gfn" for %pd\n", + map->map ? "" : "un", s, e, gfn_x(start_gfn), + map->d); + }This is an infinite loop AFAICT. Why do you need the for for?
Quoted 16 lines
xen/drivers/vpci/header.c +9
-0+ + return 0; +} + static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) { struct vpci_header *header = &pdev->vpci->header;@@ -423,7 +441,25 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) if ( !map_pending ) pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd); else + { + struct map_data data = { + .d = pdev->domain, + .map = cmd & PCI_COMMAND_MEMORY, + };xen/drivers/vpci/header.c static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) · line 458
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+ + for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) + { + const struct vpci_bar *bar = &header->bars[i]; + + if ( rangeset_is_empty(bar->mem) ) + continue; + + data.bar = bar; + rc = rangeset_report_ranges(bar->mem, 0, ~0ul, print_range, &data);Since this is per-BAR we should also print that information and the SBDF of the device, ie:
%pd SBDF: (ROM)BAR%u %map [%lx, %lx] -> ...
Quoted 8 lines
xen/drivers/vpci/header.c +3
-0+ } + defer_map(dev->domain, dev, cmd, rom_only); + } return 0;
Quoted 7 lines
Quoted 4 lines
To me, to implement a single DEBUG print, it is a bit an overkill. I do understand your concerns that "that function can be preempted and called multiple times", but taking look at the code above I think we can accept that for DEBUG builds.
It might be better if you print the per BAR positions at the top of
modify_bars, where each BAR is added to the rangeset? Or do you care about reporting the holes also?
First of all I didn't run this code, so it is just to show the complexity If the approach itself is ok. If it is then I'll get it working: please do not review it literally yet.
The original print was used to show only those {un}mappings that we actually do, no holes etc., so we need to print at the bottom of the init_bars, e.g. when the rangesets are all ready.
Again, IMO, adding such a big piece of DEBUG code instead of printing a single DEBUG message could be a bit expansive.
I still hear your concerns on *when* it is printed, but still think we can allow that.
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 08/14] vpci/header: program p2m with guest BAR view
Quoted 4 lines · Oleksandr Andrushchenko
Again, IMO, adding such a big piece of DEBUG code instead of printing a single DEBUG message could be a bit expansive.
I still hear your concerns on *when* it is printed, but still think we can allow that.
You do realize though that the mere act of logging a message may cause the need for preemption, and hence logging messages in such cases is detrimental to forward progress?
Jan
Re: [PATCH v5 08/14] vpci/header: program p2m with guest BAR view
Quoted 8 lines · Jan Beulich
Quoted 4 lines · Oleksandr Andrushchenko
Again, IMO, adding such a big piece of DEBUG code instead of printing a single DEBUG message could be a bit expansive.
I still hear your concerns on *when* it is printed, but still think we can allow that.You do realize though that the mere act of logging a message may cause the need for preemption, and hence logging messages in such cases is detrimental to forward progress?
Then I will probably remove the print at all. It is easy to add if needed
Quoted 3 lines
Jan
Thank you,
Oleksandr
PATCH v5 9/14 vpci/header: emulate PCI_COMMAND register for guests
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
Add basic emulation support for guests. At the moment only emulate PCI_COMMAND_INTX_DISABLE bit, the rest is not emulated yet and left as TODO.
---
Since v3:
- gate more code on CONFIG_HAS_MSI
- removed logic for the case when MSI/MSI-X not enabled
--- xen/drivers/vpci/header.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
xen/drivers/vpci/header.c +19 -2
@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); } +static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled ) + { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ; + } +#endif + +cmd_write (pdev, reg, cmd, data); +} +static void bar_write (const struct pci_dev *pdev,unsigned int reg,uint32_t val,void *data) {
@@ -663,8 +679,9 @@ static int init_bars(struct pci_dev *pdev) }/* Setup a handler for the command register. */ - rc =vpci_add_register (pdev->vpci, vpci_hw_read16, cmd_write,PCI_COMMAND , -2 , header); + rc =vpci_add_register (pdev->vpci, vpci_hw_read16, + is_hwdom ? cmd_write : guest_cmd_write, +PCI_COMMAND ,2 , header);if ( rc )return rc;
Signature · 1 line
2.25.1
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 20 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add basic emulation support for guests. At the moment only emulate
PCI_COMMAND_INTX_DISABLEbit, the rest is not emulated yet and left as TODO.Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v3:
- gate more code onCONFIG_HAS_MSI
- removed logic for the case when MSI/MSI-X not enabled--- xen/drivers/vpci/header.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)xen/drivers/vpci/header.c +0
-0
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 10 lines
xen/drivers/vpci/header.c +7
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); } +static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )
You need to check for MSI-X also, pdev->vpci->msix->enabled.
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 3 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;
You will also need to make sure PCI_COMMAND_INTX_DISABLE is set in the command register when attempting to enable MSI or MSIX capabilities.
Thanks, Roger.
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Hi, Roger!
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Add basic emulation support for guests. At the moment only emulate
PCI_COMMAND_INTX_DISABLEbit, the rest is not emulated yet and left as TODO.Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v3:
- gate more code onCONFIG_HAS_MSI
- removed logic for the case when MSI/MSI-X not enabled--- xen/drivers/vpci/header.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)xen/drivers/vpci/header.c +0
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); }
Quoted 8 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 7 lines
xen/drivers/vpci/header.c +7
-0+static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )You need to check for MSI-X also,
pdev->vpci->msix->enabled.
Indeed, thank you
Quoted 6 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 4 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;You will also need to make sure
PCI_COMMAND_INTX_DISABLEis set in the command register when attempting to enable MSI or MSIX capabilities.
Isn't it enough that we just check above if MSI/MSI-X enabled then make sure INTX disabled? I am not following you here on what else needs to be done.
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 18 lines · Oleksandr Andrushchenko
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Oleksandr Andrushchenko
xen/drivers/vpci/header.c +0
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); }Quoted 8 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 7 lines
xen/drivers/vpci/header.c +7
-0+static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )You need to check for MSI-X also,
pdev->vpci->msix->enabled.Indeed, thank you
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 3 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;You will also need to make sure
PCI_COMMAND_INTX_DISABLEis set in the command register when attempting to enable MSI or MSIX capabilities.Isn't it enough that we just check above if MSI/MSI-X enabled then make sure INTX disabled? I am not following you here on what else needs to be done.
No, you need to deal with the potentially bad combination on both paths - command register writes (here) and MSI/MSI-X control register writes (which is what Roger points you at). I would like to suggest to consider simply forcing INTX_DISABLE on behind the guest's back for those other two paths.
Jan
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Oleksandr Andrushchenko
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Oleksandr Andrushchenko
xen/drivers/vpci/header.c +0
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); }Quoted 8 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 7 lines
xen/drivers/vpci/header.c +7
-0+static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )You need to check for MSI-X also,
pdev->vpci->msix->enabled.Indeed, thank you
Quoted 5 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 3 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;You will also need to make sure
PCI_COMMAND_INTX_DISABLEis set in the command register when attempting to enable MSI or MSIX capabilities.
Quoted 8 lines
Quoted 3 lines
Isn't it enough that we just check above if MSI/MSI-X enabled then make sure INTX disabled? I am not following you here on what else needs to be done.
No, you need to deal with the potentially bad combination on both paths - command register writes (here) and MSI/MSI-X control register writes (which is what Roger points you at). I would like to suggest to consider simply forcing
INTX_DISABLEon behind the guest's back for those other two paths.
Do you suggest that we need to have some code which will write PCI_COMMAND while we write MSI/MSI-X control register for that kind of consistency? E.g. control register handler will need to write to PCI_COMMAND and go through emulation for guests?
If so, why didn't we have that before?
If it was ok before, then I guess the code I add does ensure INTX is set if pdev->vpci->msi->enabled || pdev->vpci->msix->enabled which is enough at least for PCI_COMMAND writes.
Sorry if I still didn't get to the point how to do that
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 31 lines · Oleksandr Andrushchenko
Quoted 23 lines · Oleksandr Andrushchenko
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Oleksandr Andrushchenko
Quoted 6 lines
xen/drivers/vpci/header.c +0
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); }Quoted 8 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 7 lines
xen/drivers/vpci/header.c +7
-0+static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )You need to check for MSI-X also,
pdev->vpci->msix->enabled.Indeed, thank you
Quoted 5 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 3 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;You will also need to make sure
PCI_COMMAND_INTX_DISABLEis set in the command register when attempting to enable MSI or MSIX capabilities.Quoted 8 lines
Quoted 3 lines
Isn't it enough that we just check above if MSI/MSI-X enabled then make sure INTX disabled? I am not following you here on what else needs to be done.
No, you need to deal with the potentially bad combination on both paths - command register writes (here) and MSI/MSI-X control register writes (which is what Roger points you at). I would like to suggest to consider simply forcing
INTX_DISABLEon behind the guest's back for those other two paths.
Quoted 5 lines
Do you suggest that we need to have some code which will write
PCI_COMMANDwhile we write MSI/MSI-X control register for that kind of consistency? E.g. control register handler will need to write toPCI_COMMANDand go through emulation for guests?
Either check or write, yes. Since you're setting the bit here behind the guest's back, setting it on the other paths as well would only look consistent to me.
Quoted 1 line
If so, why didn't we have that before?
Because we assume Dom0 to be behaving itself.
Jan
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 32 lines · Jan Beulich
Quoted 31 lines · Oleksandr Andrushchenko
Quoted 31 lines · Oleksandr Andrushchenko
Quoted 17 lines · Roger Pau Monné
Quoted 7 lines · Oleksandr Andrushchenko
Quoted 6 lines
xen/drivers/vpci/header.c +0
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); }Quoted 8 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 7 lines
xen/drivers/vpci/header.c +7
-0+static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )You need to check for MSI-X also,
pdev->vpci->msix->enabled.Indeed, thank you
Quoted 8 lines
Quoted 5 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 3 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;You will also need to make sure
PCI_COMMAND_INTX_DISABLEis set in the command register when attempting to enable MSI or MSIX capabilities.Isn't it enough that we just check above if MSI/MSI-X enabled then make sure INTX disabled? I am not following you here on what else needs to be done.
No, you need to deal with the potentially bad combination on both paths - command register writes (here) and MSI/MSI-X control register writes (which is what Roger points you at). I would like to suggest to consider simply forcing
INTX_DISABLEon behind the guest's back for those other two paths.
Quoted 8 lines
Quoted 5 lines
Do you suggest that we need to have some code which will write
PCI_COMMANDwhile we write MSI/MSI-X control register for that kind of consistency? E.g. control register handler will need to write toPCI_COMMANDand go through emulation for guests?Either check or write, yes. Since you're setting the bit here behind the guest's back, setting it on the other paths as well would only look consistent to me.
I can't find any access to PCI_COMMAND register from vMSI/vMSI-X code, so what's the concern? This seems to be the only place in vPCI which touches PCI_COMMAND register.
Quoted 3 lines
Quoted 2 lines
If so, why didn't we have that before?
Because we assume Dom0 to be behaving itself.
ok...
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 40 lines · Oleksandr Andrushchenko
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Oleksandr Andrushchenko
Quoted 23 lines · Oleksandr Andrushchenko
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Oleksandr Andrushchenko
Quoted 6 lines
xen/drivers/vpci/header.c +0
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); }Quoted 8 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 7 lines
xen/drivers/vpci/header.c +7
-0+static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )You need to check for MSI-X also,
pdev->vpci->msix->enabled.Indeed, thank you
Quoted 5 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 3 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;You will also need to make sure
PCI_COMMAND_INTX_DISABLEis set in the command register when attempting to enable MSI or MSIX capabilities.Quoted 8 lines
Quoted 3 lines
Isn't it enough that we just check above if MSI/MSI-X enabled then make sure INTX disabled? I am not following you here on what else needs to be done.
No, you need to deal with the potentially bad combination on both paths - command register writes (here) and MSI/MSI-X control register writes (which is what Roger points you at). I would like to suggest to consider simply forcing
INTX_DISABLEon behind the guest's back for those other two paths.Do you suggest that we need to have some code which will write
PCI_COMMANDwhile we write MSI/MSI-X control register for that kind of consistency? E.g. control register handler will need to write toPCI_COMMANDand go through emulation for guests?
Quoted 5 lines
Quoted 3 lines
Either check or write, yes. Since you're setting the bit here behind the guest's back, setting it on the other paths as well would only look consistent to me.
I can't find any access to
PCI_COMMANDregister from vMSI/vMSI-X code, so what's the concern?
Again: Only one of INTX, MSI, or MSI-X may be enabled at a time. This needs to be checked whenever any one of the three is about to change state. Since failing config space writes isn't really an option (there's no error code to hand back and raising an exception is nothing real hardware would do), adjusting state to be sane behind the back of the guest looks to be the least bad option.
Quoted 1 line
This seems to be the only place in vPCI which touches
PCI_COMMANDregister.
How is this relevant?
Jan
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 40 lines · Jan Beulich
Quoted 39 lines · Oleksandr Andrushchenko
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Oleksandr Andrushchenko
Quoted 23 lines · Oleksandr Andrushchenko
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Oleksandr Andrushchenko
Quoted 6 lines
xen/drivers/vpci/header.c +0
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); }Quoted 8 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 7 lines
xen/drivers/vpci/header.c +7
-0+static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )You need to check for MSI-X also,
pdev->vpci->msix->enabled.Indeed, thank you
Quoted 5 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 3 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;You will also need to make sure
PCI_COMMAND_INTX_DISABLEis set in the command register when attempting to enable MSI or MSIX capabilities.Quoted 8 lines
Quoted 3 lines
Isn't it enough that we just check above if MSI/MSI-X enabled then make sure INTX disabled? I am not following you here on what else needs to be done.
No, you need to deal with the potentially bad combination on both paths - command register writes (here) and MSI/MSI-X control register writes (which is what Roger points you at). I would like to suggest to consider simply forcing
INTX_DISABLEon behind the guest's back for those other two paths.Do you suggest that we need to have some code which will write
PCI_COMMANDwhile we write MSI/MSI-X control register for that kind of consistency? E.g. control register handler will need to write toPCI_COMMANDand go through emulation for guests?
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Either check or write, yes. Since you're setting the bit here behind the guest's back, setting it on the other paths as well would only look consistent to me.
I can't find any access to
PCI_COMMANDregister from vMSI/vMSI-X code, so what's the concern?Again: Only one of INTX, MSI, or MSI-X may be enabled at a time.
This is clear and I don't question that
Quoted 6 lines
This needs to be checked whenever any one of the three is about to change state. Since failing config space writes isn't really an option (there's no error code to hand back and raising an exception is nothing real hardware would do), adjusting state to be sane behind the back of the guest looks to be the least bad option.
Would it be enough if I read PCI_MSIX_FLAGS_ENABLE and PCI_MSI_FLAGS_ENABLE in guest_cmd_write to make a decision on INTX?
On the other hand msi->enabled and msix->enabled
already have this information if I understand the
MSI/MSI-X code correctly.
Or do we want some additional code in MSI/MSI-X's control_write functions to set INTX bit there as well?
I mean that in this guest_cmd_write handler we can only see if we write a consistent wrt MSI/MSI-X PCI_COMMAND value
If we want some more checks when we alter PCI_MSIX_FLAGS_ENABLE and/or PCI_MSI_FLAGS_ENABLE bits, this means we need a relevant PCI_COMMAND write there to be added (which doesn't exist now) to make sure INTX bit is set.
Please help me understand how you gentlemen want it
Quoted 6 lines
Quoted 2 lines
This seems to be the only place in vPCI which touches
PCI_COMMANDregister.How is this relevant?
Jan
Thank you,
Oleksandr
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 60 lines · Oleksandr Andrushchenko
Quoted 40 lines · Jan Beulich
Quoted 39 lines · Oleksandr Andrushchenko
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Oleksandr Andrushchenko
Quoted 23 lines · Oleksandr Andrushchenko
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Oleksandr Andrushchenko
Quoted 6 lines
xen/drivers/vpci/header.c +0
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); }Quoted 8 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 7 lines
xen/drivers/vpci/header.c +7
-0+static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )You need to check for MSI-X also,
pdev->vpci->msix->enabled.Indeed, thank you
Quoted 5 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 3 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;You will also need to make sure
PCI_COMMAND_INTX_DISABLEis set in the command register when attempting to enable MSI or MSIX capabilities.Quoted 8 lines
Quoted 3 lines
Isn't it enough that we just check above if MSI/MSI-X enabled then make sure INTX disabled? I am not following you here on what else needs to be done.
No, you need to deal with the potentially bad combination on both paths - command register writes (here) and MSI/MSI-X control register writes (which is what Roger points you at). I would like to suggest to consider simply forcing
INTX_DISABLEon behind the guest's back for those other two paths.Do you suggest that we need to have some code which will write
PCI_COMMANDwhile we write MSI/MSI-X control register for that kind of consistency? E.g. control register handler will need to write toPCI_COMMANDand go through emulation for guests?Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Either check or write, yes. Since you're setting the bit here behind the guest's back, setting it on the other paths as well would only look consistent to me.
I can't find any access to
PCI_COMMANDregister from vMSI/vMSI-X code, so what's the concern?Again: Only one of INTX, MSI, or MSI-X may be enabled at a time.
This is clear and I don't question that
Quoted 6 lines
This needs to be checked whenever any one of the three is about to change state. Since failing config space writes isn't really an option (there's no error code to hand back and raising an exception is nothing real hardware would do), adjusting state to be sane behind the back of the guest looks to be the least bad option.
Would it be enough if I read
PCI_MSIX_FLAGS_ENABLEandPCI_MSI_FLAGS_ENABLEinguest_cmd_writeto make a decision on INTX?
Quoted 6 lines
On the other hand
msi->enabledandmsix->enabled
already have this information if I understand the
MSI/MSI-X code correctly.Or do we want some additional code in MSI/MSI-X's
control_writefunctions to set INTX bit there as well?
Well, yes, this is what Roger and I have been asking you to add.
Quoted 7 lines
I mean that in this
guest_cmd_writehandler we can only see if we write a consistent wrt MSI/MSI-XPCI_COMMANDvalueIf we want some more checks when we alter
PCI_MSIX_FLAGS_ENABLEand/orPCI_MSI_FLAGS_ENABLEbits, this means we need a relevantPCI_COMMANDwrite there to be added (which doesn't exist now) to make sure INTX bit is set.
Exactly.
Jan
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 59 lines · Jan Beulich
Quoted 58 lines · Oleksandr Andrushchenko
Quoted 39 lines · Jan Beulich
Quoted 38 lines · Oleksandr Andrushchenko
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Oleksandr Andrushchenko
Quoted 23 lines · Oleksandr Andrushchenko
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Oleksandr Andrushchenko
Quoted 6 lines
xen/drivers/vpci/header.c +0
-0@@ -491,6 +491,22 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); }Quoted 8 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 500
Quoted 7 lines
xen/drivers/vpci/header.c +7
-0+static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +/* TODO: Add proper emulation for all bits of the command register. */ + +#ifdef CONFIG_HAS_PCI_MSI +if ( pdev->vpci->msi->enabled )You need to check for MSI-X also,
pdev->vpci->msix->enabled.Indeed, thank you
Quoted 5 lines
xen/drivers/vpci/header.c static void cmd_write(const struct pci_dev *pdev, unsigned int reg, · line 503
Quoted 3 lines
xen/drivers/vpci/header.c +3
-0+ { +/* Guest wants to enable INTx. It can't be enabled if MSI/MSI-X enabled. */ + cmd |=PCI_COMMAND_INTX_DISABLE ;You will also need to make sure
PCI_COMMAND_INTX_DISABLEis set in the command register when attempting to enable MSI or MSIX capabilities.Quoted 8 lines
Quoted 3 lines
Isn't it enough that we just check above if MSI/MSI-X enabled then make sure INTX disabled? I am not following you here on what else needs to be done.
No, you need to deal with the potentially bad combination on both paths - command register writes (here) and MSI/MSI-X control register writes (which is what Roger points you at). I would like to suggest to consider simply forcing
INTX_DISABLEon behind the guest's back for those other two paths.Do you suggest that we need to have some code which will write
PCI_COMMANDwhile we write MSI/MSI-X control register for that kind of consistency? E.g. control register handler will need to write toPCI_COMMANDand go through emulation for guests?Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Either check or write, yes. Since you're setting the bit here behind the guest's back, setting it on the other paths as well would only look consistent to me.
I can't find any access to
PCI_COMMANDregister from vMSI/vMSI-X code, so what's the concern?Again: Only one of INTX, MSI, or MSI-X may be enabled at a time.
This is clear and I don't question that
Quoted 6 lines
This needs to be checked whenever any one of the three is about to change state. Since failing config space writes isn't really an option (there's no error code to hand back and raising an exception is nothing real hardware would do), adjusting state to be sane behind the back of the guest looks to be the least bad option.
Would it be enough if I read
PCI_MSIX_FLAGS_ENABLEandPCI_MSI_FLAGS_ENABLEinguest_cmd_writeto make a decision on INTX?
Quoted 7 lines
Quoted 6 lines
On the other hand
msi->enabledandmsix->enabled
already have this information if I understand the
MSI/MSI-X code correctly.Or do we want some additional code in MSI/MSI-X's
control_writefunctions to set INTX bit there as well?Well, yes, this is what Roger and I have been asking you to add.
Do we only want this for !is_hardware_domain(d) or unconditionally?
Quoted 9 lines
Quoted 8 lines
I mean that in this
guest_cmd_writehandler we can only see if we write a consistent wrt MSI/MSI-XPCI_COMMANDvalueIf we want some more checks when we alter
PCI_MSIX_FLAGS_ENABLEand/orPCI_MSI_FLAGS_ENABLEbits, this means we need a relevantPCI_COMMANDwrite there to be added (which doesn't exist now) to make sure INTX bit is set.Exactly.
Ok
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 09/14] vpci/header: emulate PCI_COMMAND register for guests
Quoted 6 lines · Oleksandr Andrushchenko
Quoted 4 lines · Jan Beulich
Quoted 2 lines · Oleksandr Andrushchenko
Or do we want some additional code in MSI/MSI-X's
control_writefunctions to set INTX bit there as well?Well, yes, this is what Roger and I have been asking you to add.
Do we only want this for !is_hardware_domain(d) or unconditionally?
To keep present behavior unaltered, I'd suggest to do it only conditionally.
Jan
PATCH v5 10/14 vpci/header: reset the command register when adding devices
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
Reset the command register when passing through a PCI device:
it is possible that when passing through a PCI device its memory decoding bits in the command register are already set. Thus, a guest OS may not write to the command register to update memory decoding, so guest mappings (guest's view of the BARs) are left not updated.
---
Since v1:
- do not write 0 to the command register, but respect host settings.
--- xen/drivers/vpci/header.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
xen/drivers/vpci/header.c +12 -3
@@ -491,8 +491,7 @@ static void cmd_write(const struct pci_dev *pdev, unsigned int reg,pci_conf_write16 (pdev->sbdf, reg, cmd); } -static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, -uint32_t cmd,void *data) +static uint32_t emulate_cmd_reg (const struct pci_dev *pdev,uint32_t cmd) {/* TODO: Add proper emulation for all bits of the command register. */
@@ -504,7 +503,13 @@ static void guest_cmd_write(const struct pci_dev *pdev, unsigned int reg, }#endif -cmd_write (pdev, reg, cmd, data); +return cmd; +} + +static void guest_cmd_write (const struct pci_dev *pdev,unsigned int reg, +uint32_t cmd,void *data) +{ +cmd_write (pdev, reg,emulate_cmd_reg (pdev, cmd), data); } static void bar_write(const struct pci_dev *pdev, unsigned int reg,
@@ -678,6 +683,10 @@ static int init_bars(struct pci_dev *pdev)return -EOPNOTSUPP ; } +/* Reset the command register for the guest. */ +if ( !is_hwdom ) +pci_conf_write16 (pdev->sbdf,PCI_COMMAND ,emulate_cmd_reg (pdev,0 )); +/* Setup a handler for the command register. */ rc = vpci_add_register(pdev->vpci, vpci_hw_read16, is_hwdom ? cmd_write : guest_cmd_write,
Signature · 1 line
2.25.1
Re: [PATCH v5 10/14] vpci/header: reset the command register when adding devices
Quoted 9 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Reset the command register when passing through a PCI device:
it is possible that when passing through a PCI device its memory decoding bits in the command register are already set. Thus, a guest OS may not write to the command register to update memory decoding, so guest mappings (guest's view of the BARs) are left not updated.
Quoted 4 lines
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v1:
- do not write 0 to the command register, but respect host settings.
There's not much respect of host setting here, are you are basically writing 0 except for the INTX_DISABLE which will be set if MSI(X) is enabled.
I wonder whether you really need this anyway. I would expect that a device that's being assigned to a guest has just been reset globally, so there should be no need to reset the command register explicitly.
Thanks, Roger.
Re: [PATCH v5 10/14] vpci/header: reset the command register when adding devices
Hi, Roger!
Quoted 10 lines · Roger Pau Monné
Quoted 9 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Reset the command register when passing through a PCI device:
it is possible that when passing through a PCI device its memory decoding bits in the command register are already set. Thus, a guest OS may not write to the command register to update memory decoding, so guest mappings (guest's view of the BARs) are left not updated.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v1:
- do not write 0 to the command register, but respect host settings.There's not much respect of host setting here, are you are basically writing 0 except for the
INTX_DISABLEwhich will be set if MSI(X) is enabled.
Yes, and this is because we only support INTX emulation at the moment
Quoted 4 lines
I wonder whether you really need this anyway. I would expect that a device that's being assigned to a guest has just been reset globally, so there should be no need to reset the command register explicitly.
From my experience it was a real case when the device was not
reset making troubles. I'll remove this patch for now and see if I can still run without it relying on the device reset which must be in place while assigning a PCI device (here we rely on the toolstack, right?).
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
PATCH v5 11/14 vpci: add initial support for virtual PCI bus topology
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
Assign SBDF to the PCI devices being passed through with bus 0. The resulting topology is where PCIe devices reside on the bus 0 of the root complex itself (embedded endpoints).
This implementation is limited to 32 devices which are allowed on a single PCI bus.
Please note, that at the moment only function 0 of a multifunction device can be passed through.
---
Since v4:
- moved and re-worked guest sbdf initializers
- s/set_bit/__set_bit
- s/clear_bit/__clear_bit
- minor comment fix s/Virtual/Guest/
- added VPCI_MAX_VIRT_DEV constant (PCI_SLOT(~0) + 1) which will be used later for counting the number of MMIO handlers required for a guest (Julien)
Since v3:
- make use of VPCI_INIT
- moved all new code to vpci.c which belongs to it
- changed open-coded 31 to PCI_SLOT(~0)
- added comments and code to reject multifunction devices with functions other than 0
- updated comment about vpci_dev_next and made it unsigned int
- implement roll back in case of error while assigning/deassigning devices
- s/dom%pd/%pd
Since v2:
- remove casts that are (a) malformed and (b) unnecessary
- add new line for better readability
- remove CONFIG_HAS_VPCI_GUEST_SUPPORT ifdef's as the relevant vPCI functions are now completely gated with this config
- gate common code with CONFIG_HAS_VPCI_GUEST_SUPPORT
New in v2
--- xen/drivers/vpci/vpci.c | 51 +++++++++++++++++++++++++++++++++++++++++ xen/include/xen/sched.h | 8 +++++++ xen/include/xen/vpci.h | 11 +++++++++ 3 files changed, 70 insertions(+)
xen/drivers/vpci/vpci.c +51 -0
@@ -114,6 +114,9 @@ int vpci_add_handlers(struct pci_dev *pdev)spin_lock (&pdev->vpci_lock); pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT + pdev->vpci->guest_sbdf.sbdf = ~0 ; +#endif header = &pdev->vpci->header;for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ )
@@ -145,6 +148,53 @@ int vpci_add_handlers(struct pci_dev *pdev) }#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +int vpci_add_virtual_device (struct pci_dev *pdev) +{ +struct domain *d = pdev->domain; +pci_sbdf_t sbdf = {0 }; +unsigned long new_dev_number; + +/* +* Each PCI bus supports 32 devices/slots at max or up to 256 when +* there are multi-function ones which are not yet supported. +*/ +if ( pdev->info.is_extfn ) + { +gdprintk (XENLOG_ERR ,"%pp: only function 0 passthrough supported\n" , + &pdev->sbdf); +return -EOPNOTSUPP ; + } + + new_dev_number =find_first_zero_bit (&d->vpci_dev_assigned_map, +VPCI_MAX_VIRT_DEV ); +if ( new_dev_number >=VPCI_MAX_VIRT_DEV ) +return -ENOSPC ; + +__set_bit (new_dev_number, &d->vpci_dev_assigned_map); + +/* +* Both segment and bus number are 0: +* - we emulate a single host bridge for the guest, e.g. segment 0 +* - with bus 0 the virtual devices are seen as embedded +* endpoints behind the root complex +* +* TODO: add support for multi-function devices. +*/ + sbdf.devfn =PCI_DEVFN (new_dev_number,0 ); + pdev->vpci->guest_sbdf = sbdf; + +return 0 ; + +} +REGISTER_VPCI_INIT (vpci_add_virtual_device,VPCI_PRIORITY_MIDDLE ); + +static void vpci_remove_virtual_device (struct domain *d, +const struct pci_dev *pdev) +{ +__clear_bit (pdev->vpci->guest_sbdf.dev, &d->vpci_dev_assigned_map); + pdev->vpci->guest_sbdf.sbdf = ~0 ; +} +/* Notify vPCI that device is assigned to guest. */ int vpci_assign_device (struct domain *d,struct pci_dev *pdev) {
@@ -171,6 +221,7 @@ int vpci_deassign_device(struct domain *d, struct pci_dev *pdev)return 0 ;spin_lock (&pdev->vpci_lock); +vpci_remove_virtual_device (d, pdev);vpci_remove_device_handlers_locked (pdev);spin_unlock (&pdev->vpci_lock);
xen/include/xen/sched.h +8 -0
@@ -444,6 +444,14 @@ struct domain#ifdef CONFIG_HAS_PCI struct list_head pdev_list; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* +* The bitmap which shows which device numbers are already used by the +* virtual PCI bus topology and is used to assign a unique SBDF to the +* next passed through virtual PCI device. +*/ +unsigned long vpci_dev_assigned_map; +#endif #endif #ifdef CONFIG_HAS_PASSTHROUGH
xen/include/xen/vpci.h +11 -0
@@ -21,6 +21,13 @@ typedef int vpci_register_init_t(struct pci_dev *dev);#define VPCI_ECAM_BDF (addr) (((addr) & 0x0ffff000) >> 12) +/* +* Maximum number of devices supported by the virtual bus topology: +* each PCI bus supports 32 devices/slots at max or up to 256 when +* there are multi-function ones which are not yet supported. +*/ +#define VPCI_MAX_VIRT_DEV (PCI_SLOT(~0) + 1) +#define REGISTER_VPCI_INIT (x, p) \ static vpci_register_init_t *const x##_entry \ __used_section(".data.vpci." p) = x
@@ -143,6 +150,10 @@ struct vpci {struct vpci_arch_msix_entry arch; } entries[]; } *msix; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Guest SBDF of the device. */ +pci_sbdf_t guest_sbdf; +#endif #endif };
Signature · 1 line
2.25.1
Re: [PATCH v5 11/14] vpci: add initial support for virtual PCI bus topology
Quoted 33 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +29
-0@@ -145,6 +148,53 @@ int vpci_add_handlers(struct pci_dev *pdev) }#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +int vpci_add_virtual_device (struct pci_dev *pdev) +{ +struct domain *d = pdev->domain; +pci_sbdf_t sbdf = {0 }; +unsigned long new_dev_number; + +/* +* Each PCI bus supports 32 devices/slots at max or up to 256 when +* there are multi-function ones which are not yet supported. +*/ +if ( pdev->info.is_extfn ) + { +gdprintk (XENLOG_ERR ,"%pp: only function 0 passthrough supported\n" , + &pdev->sbdf); +return -EOPNOTSUPP ; + } + + new_dev_number =find_first_zero_bit (&d->vpci_dev_assigned_map, +VPCI_MAX_VIRT_DEV ); +if ( new_dev_number >=VPCI_MAX_VIRT_DEV ) +return -ENOSPC ; + +__set_bit (new_dev_number, &d->vpci_dev_assigned_map); + +/* +* Both segment and bus number are 0: +* - we emulate a single host bridge for the guest, e.g. segment 0 +* - with bus 0 the virtual devices are seen as embedded +* endpoints behind the root complex
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 189
Quoted 10 lines
xen/drivers/vpci/vpci.c +10
-0+* +* TODO: add support for multi-function devices. +*/ + sbdf.devfn =PCI_DEVFN (new_dev_number,0 ); + pdev->vpci->guest_sbdf = sbdf; + +return 0 ; + +} +REGISTER_VPCI_INIT (vpci_add_virtual_device,VPCI_PRIORITY_MIDDLE );
Is this function guaranteed to always be invoked ahead of ...
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 196
Quoted 6 lines
xen/drivers/vpci/vpci.c +6
-0+static void vpci_remove_virtual_device (struct domain *d, +const struct pci_dev *pdev) +{ +__clear_bit (pdev->vpci->guest_sbdf.dev, &d->vpci_dev_assigned_map); + pdev->vpci->guest_sbdf.sbdf = ~0 ; +}
... this one, even when considering error paths? Otherwise you may wrongly clear bit 31 here afaict.
Jan
Re: [PATCH v5 11/14] vpci: add initial support for virtual PCI bus topology
Hi, Jan!
Quoted 46 lines · Jan Beulich
Quoted 33 lines · Oleksandr Andrushchenko
xen/drivers/vpci/vpci.c +29
-0@@ -145,6 +148,53 @@ int vpci_add_handlers(struct pci_dev *pdev) }#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +int vpci_add_virtual_device (struct pci_dev *pdev) +{ +struct domain *d = pdev->domain; +pci_sbdf_t sbdf = {0 }; +unsigned long new_dev_number; + +/* +* Each PCI bus supports 32 devices/slots at max or up to 256 when +* there are multi-function ones which are not yet supported. +*/ +if ( pdev->info.is_extfn ) + { +gdprintk (XENLOG_ERR ,"%pp: only function 0 passthrough supported\n" , + &pdev->sbdf); +return -EOPNOTSUPP ; + } + + new_dev_number =find_first_zero_bit (&d->vpci_dev_assigned_map, +VPCI_MAX_VIRT_DEV ); +if ( new_dev_number >=VPCI_MAX_VIRT_DEV ) +return -ENOSPC ; + +__set_bit (new_dev_number, &d->vpci_dev_assigned_map); + +/* +* Both segment and bus number are 0: +* - we emulate a single host bridge for the guest, e.g. segment 0 +* - with bus 0 the virtual devices are seen as embedded +* endpoints behind the root complex xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 189
Quoted 10 lines
xen/drivers/vpci/vpci.c +10
-0+* +* TODO: add support for multi-function devices. +*/ + sbdf.devfn =PCI_DEVFN (new_dev_number,0 ); + pdev->vpci->guest_sbdf = sbdf; + +return 0 ; + +} +REGISTER_VPCI_INIT (vpci_add_virtual_device,VPCI_PRIORITY_MIDDLE );Is this function guaranteed to always be invoked ahead of ...
Quoted 8 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 196
Quoted 6 lines
xen/drivers/vpci/vpci.c +6
-0+static void vpci_remove_virtual_device (struct domain *d, +const struct pci_dev *pdev) +{ +__clear_bit (pdev->vpci->guest_sbdf.dev, &d->vpci_dev_assigned_map); + pdev->vpci->guest_sbdf.sbdf = ~0 ; +}... this one, even when considering error paths? Otherwise you may wrongly clear bit 31 here afaict.
According to Roger's comment I will not use REGISTER_VPCI_INIT machinery for this.
Quoted 3 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 11/14] vpci: add initial support for virtual PCI bus topology
In reply to Oleksandr AndrushchenkoQuoted 58 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Assign SBDF to the PCI devices being passed through with bus 0. The resulting topology is where PCIe devices reside on the bus 0 of the root complex itself (embedded endpoints).
This implementation is limited to 32 devices which are allowed on a single PCI bus.Please note, that at the moment only function 0 of a multifunction device can be passed through.
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- moved and re-worked guest sbdf initializers
- s/set_bit/__set_bit
- s/clear_bit/__clear_bit
- minor comment fix s/Virtual/Guest/
- addedVPCI_MAX_VIRT_DEVconstant (PCI_SLOT(~0)+ 1) which will be used later for counting the number of MMIO handlers required for a guest (Julien)
Since v3:
- make use ofVPCI_INIT
- moved all new code tovpci.cwhich belongs to it
- changed open-coded 31 toPCI_SLOT(~0)
- added comments and code to reject multifunction devices with functions other than 0
- updated comment aboutvpci_dev_nextand made it unsigned int
- implement roll back in case of error while assigning/deassigning devices
- s/dom%pd/%pd
Since v2:
- remove casts that are (a) malformed and (b) unnecessary
- add new line for better readability
- removeCONFIG_HAS_VPCI_GUEST_SUPPORTifdef's as the relevant vPCI functions are now completely gated with this config
- gate common code withCONFIG_HAS_VPCI_GUEST_SUPPORT
New in v2--- xen/drivers/vpci/vpci.c | 51 +++++++++++++++++++++++++++++++++++++++++ xen/include/xen/sched.h | 8 +++++++ xen/include/xen/vpci.h | 11 +++++++++ 3 files changed, 70 insertions(+)xen/drivers/vpci/vpci.c +3
-0@@ -114,6 +114,9 @@ int vpci_add_handlers(struct pci_dev *pdev)spin_lock (&pdev->vpci_lock); pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT + pdev->vpci->guest_sbdf.sbdf = ~0 ; +#endif header = &pdev->vpci->header;for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ )
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 155
Quoted 8 lines
xen/drivers/vpci/vpci.c +5
-0@@ -145,6 +148,53 @@ int vpci_add_handlers(struct pci_dev *pdev) }#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +int vpci_add_virtual_device (struct pci_dev *pdev) +{ +struct domain *d = pdev->domain; +pci_sbdf_t sbdf = {0 }; +unsigned long new_dev_number;
I think this needs to be limited to non-hardware domains?
Or else you will report failures for the hardware domain even if it's not using the virtual topology at all.
Quoted 7 lines
xen/drivers/vpci/vpci.c +7
-0+/* +* Each PCI bus supports 32 devices/slots at max or up to 256 when +* there are multi-function ones which are not yet supported. +*/ +if ( pdev->info.is_extfn ) + { + gdprintk(XENLOG_ERR, "%pp: only function 0 passthrough supported\n",
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 173
Quoted 10 lines
xen/drivers/vpci/vpci.c +10
-0+ &pdev->sbdf); +return -EOPNOTSUPP ; + } + + new_dev_number =find_first_zero_bit (&d->vpci_dev_assigned_map, +VPCI_MAX_VIRT_DEV ); +if ( new_dev_number >=VPCI_MAX_VIRT_DEV ) +return -ENOSPC ; + +__set_bit (new_dev_number, &d->vpci_dev_assigned_map);
How is vpci_dev_assigned_map protected from concurrent accesses? Does it rely on the pcidevs lock being held while accessing it?
If so it needs spelling out (and likely an assert added).
Quoted 5 lines
xen/drivers/vpci/vpci.c +5
-0+/* +* Both segment and bus number are 0: +* - we emulate a single host bridge for the guest, e.g. segment 0 +* - with bus 0 the virtual devices are seen as embedded +* endpoints behind the root complex
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 189
Quoted 10 lines
xen/drivers/vpci/vpci.c +10
-0+* +* TODO: add support for multi-function devices. +*/ + sbdf.devfn =PCI_DEVFN (new_dev_number,0 ); + pdev->vpci->guest_sbdf = sbdf; + +return 0 ; + +} +REGISTER_VPCI_INIT (vpci_add_virtual_device,VPCI_PRIORITY_MIDDLE );
I'm unsure this is the right place to do virtual SBDF assignment, my plan was to use REGISTER_VPCI_INIT exclusively with PCI capabilities.
I think it would be better to do the virtual SBDF assignment from vpci_assign_device.
Quoted 24 lines
xen/drivers/vpci/vpci.c +9
-0+ +static void vpci_remove_virtual_device (struct domain *d, +const struct pci_dev *pdev) +{ +__clear_bit (pdev->vpci->guest_sbdf.dev, &d->vpci_dev_assigned_map); + pdev->vpci->guest_sbdf.sbdf = ~0 ; +} +/* Notify vPCI that device is assigned to guest. */ int vpci_assign_device (struct domain *d,struct pci_dev *pdev) {@@ -171,6 +221,7 @@ int vpci_deassign_device(struct domain *d, struct pci_dev *pdev)return 0 ;spin_lock (&pdev->vpci_lock); +vpci_remove_virtual_device (d, pdev);vpci_remove_device_handlers_locked (pdev);spin_unlock (&pdev->vpci_lock);xen/include/xen/sched.h +0
-0
xen/include/xen/sched.h struct domain · line 453
Quoted 10 lines
xen/include/xen/sched.h +7
-0@@ -444,6 +444,14 @@ struct domain#ifdef CONFIG_HAS_PCI struct list_head pdev_list; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* +* The bitmap which shows which device numbers are already used by the +* virtual PCI bus topology and is used to assign a unique SBDF to the +* next passed through virtual PCI device. +*/ +unsigned long vpci_dev_assigned_map;
Please use DECLARE_BITMAP with the maximum number of supported devices as parameter.
Quoted 36 lines
xen/include/xen/sched.h +1
-0+#endif #endif #ifdef CONFIG_HAS_PASSTHROUGH xen/include/xen/vpci.h +11
-0@@ -21,6 +21,13 @@ typedef int vpci_register_init_t(struct pci_dev *dev);#define VPCI_ECAM_BDF (addr) (((addr) & 0x0ffff000) >> 12) +/* +* Maximum number of devices supported by the virtual bus topology: +* each PCI bus supports 32 devices/slots at max or up to 256 when +* there are multi-function ones which are not yet supported. +*/ +#define VPCI_MAX_VIRT_DEV (PCI_SLOT(~0) + 1) +#define REGISTER_VPCI_INIT (x, p) \ static vpci_register_init_t *const x##_entry \ __used_section(".data.vpci." p) = x@@ -143,6 +150,10 @@ struct vpci {struct vpci_arch_msix_entry arch; } entries[]; } *msix; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Guest SBDF of the device. */ +pci_sbdf_t guest_sbdf; +#endif #endif };Signature · 1 line
2.25.1
Re: [PATCH v5 11/14] vpci: add initial support for virtual PCI bus topology
Hi, Roger!
Quoted 69 lines · Roger Pau Monné
Quoted 58 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Assign SBDF to the PCI devices being passed through with bus 0. The resulting topology is where PCIe devices reside on the bus 0 of the root complex itself (embedded endpoints).
This implementation is limited to 32 devices which are allowed on a single PCI bus.Please note, that at the moment only function 0 of a multifunction device can be passed through.
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- moved and re-worked guest sbdf initializers
- s/set_bit/__set_bit
- s/clear_bit/__clear_bit
- minor comment fix s/Virtual/Guest/
- addedVPCI_MAX_VIRT_DEVconstant (PCI_SLOT(~0)+ 1) which will be used later for counting the number of MMIO handlers required for a guest (Julien)
Since v3:
- make use ofVPCI_INIT
- moved all new code tovpci.cwhich belongs to it
- changed open-coded 31 toPCI_SLOT(~0)
- added comments and code to reject multifunction devices with functions other than 0
- updated comment aboutvpci_dev_nextand made it unsigned int
- implement roll back in case of error while assigning/deassigning devices
- s/dom%pd/%pd
Since v2:
- remove casts that are (a) malformed and (b) unnecessary
- add new line for better readability
- removeCONFIG_HAS_VPCI_GUEST_SUPPORTifdef's as the relevant vPCI functions are now completely gated with this config
- gate common code withCONFIG_HAS_VPCI_GUEST_SUPPORT
New in v2--- xen/drivers/vpci/vpci.c | 51 +++++++++++++++++++++++++++++++++++++++++ xen/include/xen/sched.h | 8 +++++++ xen/include/xen/vpci.h | 11 +++++++++ 3 files changed, 70 insertions(+)xen/drivers/vpci/vpci.c +3
-0@@ -114,6 +114,9 @@ int vpci_add_handlers(struct pci_dev *pdev)spin_lock (&pdev->vpci_lock); pdev->vpci = vpci;INIT_LIST_HEAD (&pdev->vpci->handlers); +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT + pdev->vpci->guest_sbdf.sbdf = ~0 ; +#endif header = &pdev->vpci->header;for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ )xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 155
Quoted 8 lines
xen/drivers/vpci/vpci.c +5
-0@@ -145,6 +148,53 @@ int vpci_add_handlers(struct pci_dev *pdev) }#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +int vpci_add_virtual_device (struct pci_dev *pdev) +{ +struct domain *d = pdev->domain; +pci_sbdf_t sbdf = {0 }; +unsigned long new_dev_number;I think this needs to be limited to non-hardware domains?
Quoted 2 lines
Or else you will report failures for the hardware domain even if it's not using the virtual topology at all.
Yes, this wants an is_hardware_domain check
Quoted 12 lines
Quoted 12 lines
xen/drivers/vpci/vpci.c +11
-0+/* +* Each PCI bus supports 32 devices/slots at max or up to 256 when +* there are multi-function ones which are not yet supported. +*/ +if ( pdev->info.is_extfn ) + { +gdprintk (XENLOG_ERR ,"%pp: only function 0 passthrough supported\n" , + &pdev->sbdf); +return -EOPNOTSUPP ; + } +
Quoted 8 lines
xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 173
Quoted 6 lines
xen/drivers/vpci/vpci.c +6
-0+ new_dev_number =find_first_zero_bit (&d->vpci_dev_assigned_map, +VPCI_MAX_VIRT_DEV ); +if ( new_dev_number >=VPCI_MAX_VIRT_DEV ) +return -ENOSPC ; + +__set_bit (new_dev_number, &d->vpci_dev_assigned_map);How is
vpci_dev_assigned_mapprotected from concurrent accesses? Does it rely on the pcidevs lock being held while accessing it?
It does rely on pcidevs lock, I'll add an assert here
Quoted 21 lines
If so it needs spelling out (and likely an assert added).
Quoted 5 lines
xen/drivers/vpci/vpci.c +5
-0+/* +* Both segment and bus number are 0: +* - we emulate a single host bridge for the guest, e.g. segment 0 +* - with bus 0 the virtual devices are seen as embedded +* endpoints behind the root complex xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 189
Quoted 10 lines
xen/drivers/vpci/vpci.c +10
-0+* +* TODO: add support for multi-function devices. +*/ + sbdf.devfn =PCI_DEVFN (new_dev_number,0 ); + pdev->vpci->guest_sbdf = sbdf; + +return 0 ; + +} +REGISTER_VPCI_INIT (vpci_add_virtual_device,VPCI_PRIORITY_MIDDLE );I'm unsure this is the right place to do virtual SBDF assignment, my plan was to use
REGISTER_VPCI_INITexclusively with PCI capabilities.
Quoted 2 lines
I think it would be better to do the virtual SBDF assignment from
vpci_assign_device.
Ok, will do
Quoted 26 lines
Quoted 26 lines
xen/drivers/vpci/vpci.c +9
-0+ +static void vpci_remove_virtual_device (struct domain *d, +const struct pci_dev *pdev) +{ +__clear_bit (pdev->vpci->guest_sbdf.dev, &d->vpci_dev_assigned_map); + pdev->vpci->guest_sbdf.sbdf = ~0 ; +} +/* Notify vPCI that device is assigned to guest. */ int vpci_assign_device (struct domain *d,struct pci_dev *pdev) {@@ -171,6 +221,7 @@ int vpci_deassign_device(struct domain *d, struct pci_dev *pdev)return 0 ;spin_lock (&pdev->vpci_lock); +vpci_remove_virtual_device (d, pdev);vpci_remove_device_handlers_locked (pdev);spin_unlock (&pdev->vpci_lock);xen/include/xen/sched.h +0
-0@@ -444,6 +444,14 @@ struct domain
Quoted 11 lines
xen/include/xen/sched.h struct domain · line 453
Quoted 9 lines
xen/include/xen/sched.h +7
-0#ifdef CONFIG_HAS_PCI struct list_head pdev_list; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* +* The bitmap which shows which device numbers are already used by the +* virtual PCI bus topology and is used to assign a unique SBDF to the +* next passed through virtual PCI device. +*/ +unsigned long vpci_dev_assigned_map;Please use
DECLARE_BITMAPwith the maximum number of supported devices as parameter.
Will use
Quoted 34 lines
Quoted 34 lines
xen/include/xen/sched.h +1
-0+#endif #endif #ifdef CONFIG_HAS_PASSTHROUGH xen/include/xen/vpci.h +11
-0@@ -21,6 +21,13 @@ typedef int vpci_register_init_t(struct pci_dev *dev);#define VPCI_ECAM_BDF (addr) (((addr) & 0x0ffff000) >> 12) +/* +* Maximum number of devices supported by the virtual bus topology: +* each PCI bus supports 32 devices/slots at max or up to 256 when +* there are multi-function ones which are not yet supported. +*/ +#define VPCI_MAX_VIRT_DEV (PCI_SLOT(~0) + 1) +#define REGISTER_VPCI_INIT (x, p) \ static vpci_register_init_t *const x##_entry \ __used_section(".data.vpci." p) = x@@ -143,6 +150,10 @@ struct vpci {struct vpci_arch_msix_entry arch; } entries[]; } *msix; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* Guest SBDF of the device. */ +pci_sbdf_t guest_sbdf; +#endif #endif };
Quoted 3 lines
Quoted 3 lines
Signature · 1 line
2.25.1
Thank you,
Oleksandr
PATCH v5 12/14 xen/arm: translate virtual PCI bus topology for guests
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
There are three originators for the PCI configuration space access:
1. The domain that owns physical host bridge: MMIO handlers are there so we can update vPCI register handlers with the values written by the hardware domain, e.g. physical view of the registers vs guest's view on the configuration space.
2. Guest access to the passed through PCI devices: we need to properly map virtual bus topology to the physical one, e.g. pass the configuration space access to the corresponding physical devices.
3. Emulated host PCI bridge access. It doesn't exist in the physical topology, e.g. it can't be mapped to some physical host bridge. So, all access to the host bridge itself needs to be trapped and emulated.
---
Since v4:
- indentation fixes
- constify struct domain
- updated commit message
- updates to the new locking scheme (pdev->vpci_lock) Since v3:
- revisit locking
- move code to vpci.c
Since v2:
- pass struct domain instead of struct vcpu
- constify arguments where possible
- gate relevant code with CONFIG_HAS_VPCI_GUEST_SUPPORT
New in v2
--- xen/arch/arm/vpci.c | 18 ++++++++++++++++++ xen/drivers/vpci/vpci.c | 27 +++++++++++++++++++++++++++ xen/include/xen/vpci.h | 1 + 3 files changed, 46 insertions(+)
xen/arch/arm/vpci.c +18 -0
@@ -41,6 +41,15 @@ static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info,/* data is needed to prevent a pointer cast on 32bit */ unsigned long data; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* +* For the passed through devices we need to map their virtual SBDF +* to the physical PCI device being passed through. +*/ +if ( !bridge && !vpci_translate_virtual_device (v->domain, &sbdf) ) +return 1 ; +#endif +if (vpci_ecam_read (sbdf,ECAM_REG_OFFSET (info->gpa),1U << info->dabt.size, &data) ) {
@@ -59,6 +68,15 @@ static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info,struct pci_host_bridge *bridge = p;pci_sbdf_t sbdf =vpci_sbdf_from_gpa (bridge, info->gpa); +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* +* For the passed through devices we need to map their virtual SBDF +* to the physical PCI device being passed through. +*/ +if ( !bridge && !vpci_translate_virtual_device (v->domain, &sbdf) ) +return 1 ; +#endif +return vpci_ecam_write (sbdf,ECAM_REG_OFFSET (info->gpa),1U << info->dabt.size, r); }
xen/drivers/vpci/vpci.c +27 -0
@@ -195,6 +195,33 @@ static void vpci_remove_virtual_device(struct domain *d, pdev->vpci->guest_sbdf.sbdf = ~0 ; } +/* +* Find the physical device which is mapped to the virtual device +* and translate virtual SBDF to the physical one. +*/ +bool vpci_translate_virtual_device (const struct domain *d,pci_sbdf_t *sbdf) +{ +struct pci_dev *pdev; + +for_each_pdev ( d, pdev ) + { +bool found; + +spin_lock (&pdev->vpci_lock); + found = pdev->vpci && (pdev->vpci->guest_sbdf.sbdf == sbdf->sbdf); +spin_unlock (&pdev->vpci_lock); + +if ( found ) + { +/* Replace guest SBDF with the physical one. */ + *sbdf = pdev->sbdf; +return true; + } + } + +return false; +} +/* Notify vPCI that device is assigned to guest. */ int vpci_assign_device (struct domain *d,struct pci_dev *pdev) {
xen/include/xen/vpci.h +1 -0
@@ -280,6 +280,7 @@ static inline void vpci_cancel_pending_locked(struct pci_dev *pdev)/* Notify vPCI that device is assigned/de-assigned to/from guest. */ int vpci_assign_device (struct domain *d,struct pci_dev *pdev);int vpci_deassign_device (struct domain *d,struct pci_dev *pdev); +bool vpci_translate_virtual_device (const struct domain *d,pci_sbdf_t *sbdf);#else static inline int vpci_assign_device (struct domain *d,struct pci_dev *pdev) {
Signature · 1 line
2.25.1
Re: [PATCH v5 12/14] xen/arm: translate virtual PCI bus topology for guests
Quoted 14 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
There are three originators for the PCI configuration space access:
1. The domain that owns physical host bridge: MMIO handlers are there so we can update vPCI register handlers with the values written by the hardware domain, e.g. physical view of the registers vs guest's view on the configuration space.
2. Guest access to the passed through PCI devices: we need to properly map virtual bus topology to the physical one, e.g. pass the configuration space access to the corresponding physical devices.
3. Emulated host PCI bridge access. It doesn't exist in the physical topology, e.g. it can't be mapped to some physical host bridge. So, all access to the host bridge itself needs to be trapped and emulated.
I'm kind of lost in this commit message. You are just adding a translate function in order for domUs to translate from virtual SBDF to the physical SBDF of the device. I realize you do that based on whether 'bridge' is set or not, so I assume this is just a way to signal whether the domain is a hardware domain or not. Ie:
!!bridge == is_hardware_domain(v->domain).
Quoted 27 lines
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- indentation fixes
- constify struct domain
- updated commit message
- updates to the new locking scheme (pdev->vpci_lock) Since v3:
- revisit locking
- move code tovpci.c
Since v2:
- pass struct domain instead of struct vcpu
- constify arguments where possible
- gate relevant code withCONFIG_HAS_VPCI_GUEST_SUPPORT
New in v2--- xen/arch/arm/vpci.c | 18 ++++++++++++++++++ xen/drivers/vpci/vpci.c | 27 +++++++++++++++++++++++++++ xen/include/xen/vpci.h | 1 + 3 files changed, 46 insertions(+)xen/arch/arm/vpci.c +0
-0
xen/arch/arm/vpci.c static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info, · line 50
Quoted 10 lines
xen/arch/arm/vpci.c +7
-0@@ -41,6 +41,15 @@ static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info,/* data is needed to prevent a pointer cast on 32bit */ unsigned long data; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* +* For the passed through devices we need to map their virtual SBDF +* to the physical PCI device being passed through. +*/ +if ( !bridge && !vpci_translate_virtual_device (v->domain, &sbdf) ) +return 1 ;
I'm unsure what returning 1 implies for Arm here, but you likely need to set '*r = ~0ul;'.
Quoted 27 lines
xen/arch/arm/vpci.c +11
-0+#endif +if (vpci_ecam_read (sbdf,ECAM_REG_OFFSET (info->gpa),1U << info->dabt.size, &data) ) {@@ -59,6 +68,15 @@ static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info,struct pci_host_bridge *bridge = p;pci_sbdf_t sbdf =vpci_sbdf_from_gpa (bridge, info->gpa); +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* +* For the passed through devices we need to map their virtual SBDF +* to the physical PCI device being passed through. +*/ +if ( !bridge && !vpci_translate_virtual_device (v->domain, &sbdf) ) +return 1 ; +#endif +return vpci_ecam_write (sbdf,ECAM_REG_OFFSET (info->gpa),1U << info->dabt.size, r); }xen/drivers/vpci/vpci.c +0
-0@@ -195,6 +195,33 @@ static void vpci_remove_virtual_device(struct domain *d, pdev->vpci->guest_sbdf.sbdf = ~0 ;
xen/drivers/vpci/vpci.c static void vpci_remove_virtual_device(struct domain *d, · line 205
Quoted 10 lines
xen/drivers/vpci/vpci.c +8
-0} +/* +* Find the physical device which is mapped to the virtual device +* and translate virtual SBDF to the physical one. +*/ +bool vpci_translate_virtual_device (const struct domain *d,pci_sbdf_t *sbdf) +{ +struct pci_dev *pdev; +
I would add:
ASSERT(!is_hardware_domain(d));
To make sure this is not used for the hardware domain.
Quoted 27 lines
xen/drivers/vpci/vpci.c +19
-0+for_each_pdev ( d, pdev ) + { +bool found; + +spin_lock (&pdev->vpci_lock); + found = pdev->vpci && (pdev->vpci->guest_sbdf.sbdf == sbdf->sbdf); +spin_unlock (&pdev->vpci_lock); + +if ( found ) + { +/* Replace guest SBDF with the physical one. */ + *sbdf = pdev->sbdf; +return true; + } + } + +return false; +} +/* Notify vPCI that device is assigned to guest. */ int vpci_assign_device (struct domain *d,struct pci_dev *pdev) {xen/include/xen/vpci.h +0
-0
xen/include/xen/vpci.h static inline void vpci_cancel_pending_locked(struct pci_dev *pdev) · line 286
Quoted 7 lines
xen/include/xen/vpci.h +1
-0@@ -280,6 +280,7 @@ static inline void vpci_cancel_pending_locked(struct pci_dev *pdev)/* Notify vPCI that device is assigned/de-assigned to/from guest. */ int vpci_assign_device (struct domain *d,struct pci_dev *pdev);int vpci_deassign_device (struct domain *d,struct pci_dev *pdev); +bool vpci_translate_virtual_device (const struct domain *d,pci_sbdf_t *sbdf);#else static inline int vpci_assign_device (struct domain *d,struct pci_dev *pdev) {
If you add a dummy vpci_translate_virtual_device helper that returns false unconditionally here you could drop the #ifdefs in arm/vpci.c AFAICT.
Thanks, Roger.
Re: [PATCH v5 12/14] xen/arm: translate virtual PCI bus topology for guests
Hi, Roger!
Quoted 15 lines · Roger Pau Monné
Quoted 14 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
There are three originators for the PCI configuration space access:
1. The domain that owns physical host bridge: MMIO handlers are there so we can update vPCI register handlers with the values written by the hardware domain, e.g. physical view of the registers vs guest's view on the configuration space.
2. Guest access to the passed through PCI devices: we need to properly map virtual bus topology to the physical one, e.g. pass the configuration space access to the corresponding physical devices.
3. Emulated host PCI bridge access. It doesn't exist in the physical topology, e.g. it can't be mapped to some physical host bridge. So, all access to the host bridge itself needs to be trapped and emulated.
Quoted 6 lines
I'm kind of lost in this commit message. You are just adding a translate function in order for domUs to translate from virtual SBDF to the physical SBDF of the device. I realize you do that based on whether 'bridge' is set or not, so I assume this is just a way to signal whether the domain is a hardware domain or not. Ie:
!!bridge ==is_hardware_domain(v->domain).
Simply put: yes
Quoted 30 lines
Quoted 30 lines
Signed-off-by Oleksandr Andrushchenko <[email protected]>---
Since v4:
- indentation fixes
- constify struct domain
- updated commit message
- updates to the new locking scheme (pdev->vpci_lock) Since v3:
- revisit locking
- move code tovpci.c
Since v2:
- pass struct domain instead of struct vcpu
- constify arguments where possible
- gate relevant code withCONFIG_HAS_VPCI_GUEST_SUPPORT
New in v2--- xen/arch/arm/vpci.c | 18 ++++++++++++++++++ xen/drivers/vpci/vpci.c | 27 +++++++++++++++++++++++++++ xen/include/xen/vpci.h | 1 + 3 files changed, 46 insertions(+)xen/arch/arm/vpci.c +0
-0@@ -41,6 +41,15 @@ static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info,/* data is needed to prevent a pointer cast on 32bit */ unsigned long data;
Quoted 9 lines
xen/arch/arm/vpci.c static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info, · line 50
Quoted 7 lines
xen/arch/arm/vpci.c +7
-0+#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* +* For the passed through devices we need to map their virtual SBDF +* to the physical PCI device being passed through. +*/ +if ( !bridge && !vpci_translate_virtual_device (v->domain, &sbdf) ) +return 1 ;I'm unsure what returning 1 implies for Arm here, but you likely need to set '*r = ~0ul;'.
Good catch, will add
Quoted 40 lines
Quoted 28 lines
xen/arch/arm/vpci.c +11
-0+#endif +if (vpci_ecam_read (sbdf,ECAM_REG_OFFSET (info->gpa),1U << info->dabt.size, &data) ) {@@ -59,6 +68,15 @@ static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info,struct pci_host_bridge *bridge = p;pci_sbdf_t sbdf =vpci_sbdf_from_gpa (bridge, info->gpa); +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +/* +* For the passed through devices we need to map their virtual SBDF +* to the physical PCI device being passed through. +*/ +if ( !bridge && !vpci_translate_virtual_device (v->domain, &sbdf) ) +return 1 ; +#endif +return vpci_ecam_write (sbdf,ECAM_REG_OFFSET (info->gpa),1U << info->dabt.size, r); }xen/drivers/vpci/vpci.c +0
-0@@ -195,6 +195,33 @@ static void vpci_remove_virtual_device(struct domain *d, pdev->vpci->guest_sbdf.sbdf = ~0 ;xen/drivers/vpci/vpci.c static void vpci_remove_virtual_device(struct domain *d, · line 205
Quoted 10 lines
xen/drivers/vpci/vpci.c +8
-0} +/* +* Find the physical device which is mapped to the virtual device +* and translate virtual SBDF to the physical one. +*/ +bool vpci_translate_virtual_device (const struct domain *d,pci_sbdf_t *sbdf) +{ +struct pci_dev *pdev; +I would add:
Quoted 3 lines
ASSERT(!is_hardware_domain(d));
To make sure this is not used for the hardware domain.
Will add
Quoted 30 lines
Quoted 30 lines
xen/drivers/vpci/vpci.c +19
-0+for_each_pdev ( d, pdev ) + { +bool found; + +spin_lock (&pdev->vpci_lock); + found = pdev->vpci && (pdev->vpci->guest_sbdf.sbdf == sbdf->sbdf); +spin_unlock (&pdev->vpci_lock); + +if ( found ) + { +/* Replace guest SBDF with the physical one. */ + *sbdf = pdev->sbdf; +return true; + } + } + +return false; +} +/* Notify vPCI that device is assigned to guest. */ int vpci_assign_device (struct domain *d,struct pci_dev *pdev) {xen/include/xen/vpci.h +0
-0@@ -280,6 +280,7 @@ static inline void vpci_cancel_pending_locked(struct pci_dev *pdev)/* Notify vPCI that device is assigned/de-assigned to/from guest. */ int vpci_assign_device (struct domain *d,struct pci_dev *pdev);
Quoted 8 lines
xen/include/xen/vpci.h static inline void vpci_cancel_pending_locked(struct pci_dev *pdev) · line 286
Quoted 5 lines
xen/include/xen/vpci.h +1
-0int vpci_deassign_device (struct domain *d,struct pci_dev *pdev); +bool vpci_translate_virtual_device (const struct domain *d,pci_sbdf_t *sbdf);#else static inline int vpci_assign_device (struct domain *d,struct pci_dev *pdev) {If you add a dummy
vpci_translate_virtual_devicehelper that returns false unconditionally here you could drop the #ifdefs inarm/vpci.cAFAICT.
Will try to do so
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
PATCH v5 13/14 xen/arm: account IO handlers for emulated PCI MSI-X
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
At the moment, we always allocate an extra 16 slots for IO handlers (see MAX_IO_HANDLER). So while adding IO trap handlers for the emulated MSI-X registers we need to explicitly tell that we have additional IO handlers, so those are accounted.
---
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
---
This actually moved here from the part 2 of the prep work for PCI passthrough on Arm as it seems to be the proper place for it.
New in v5
--- xen/arch/arm/vpci.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
xen/arch/arm/vpci.c +14 -1
@@ -134,6 +134,8 @@ static int vpci_get_num_handlers_cb(struct domain *d,unsigned int domain_vpci_get_num_mmio_handlers (struct domain *d) { +unsigned int count; +if ( !has_vpci (d) )return 0 ;
@@ -145,7 +147,18 @@ unsigned int domain_vpci_get_num_mmio_handlers(struct domain *d) }/* For a single emulated host bridge's configuration space. */ -return 1 ; + count =1 ; + +#ifdef CONFIG_HAS_PCI_MSI +/* +* There's a single MSI-X MMIO handler that deals with both PBA +* and MSI-X tables per each PCI device being passed through. +* Maximum number of emulated virtual devices is VPCI_MAX_VIRT_DEV. +*/ + count +=VPCI_MAX_VIRT_DEV ; +#endif + +return count; }/*
Signature · 1 line
2.25.1
Re: [PATCH v5 13/14] xen/arm: account IO handlers for emulated PCI MSI-X
Quoted 8 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
At the moment, we always allocate an extra 16 slots for IO handlers (see
MAX_IO_HANDLER). So while adding IO trap handlers for the emulated MSI-X registers we need to explicitly tell that we have additional IO handlers, so those are accounted.Signed-off-by Oleksandr Andrushchenko <[email protected]>
LGTM, just one comment below. This will require an Ack from the Arm guys.
Quoted 29 lines
---
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
---
This actually moved here from the part 2 of the prep work for PCI passthrough on Arm as it seems to be the proper place for it.New in v5
--- xen/arch/arm/vpci.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)xen/arch/arm/vpci.c +2
-0@@ -134,6 +134,8 @@ static int vpci_get_num_handlers_cb(struct domain *d,unsigned int domain_vpci_get_num_mmio_handlers (struct domain *d) { +unsigned int count; +if ( !has_vpci (d) )return 0 ;@@ -145,7 +147,18 @@ unsigned int domain_vpci_get_num_mmio_handlers(struct domain *d) }/* For a single emulated host bridge's configuration space. */
xen/arch/arm/vpci.c unsigned int domain_vpci_get_num_mmio_handlers(struct domain *d) · line 158
Quoted 10 lines
xen/arch/arm/vpci.c +9
-1-return 1 ; + count =1 ; + +#ifdef CONFIG_HAS_PCI_MSI +/* +* There's a single MSI-X MMIO handler that deals with both PBA +* and MSI-X tables per each PCI device being passed through. +* Maximum number of emulated virtual devices is VPCI_MAX_VIRT_DEV. +*/ + count +=VPCI_MAX_VIRT_DEV ;
You could also use IS_ENABLED(CONFIG_HAS_PCI_MSI) since VPCI_MAX_VIRT_DEV is defined unconditionally.
Thanks, Roger.
Re: [PATCH v5 13/14] xen/arm: account IO handlers for emulated PCI MSI-X
Hi, Roger!
Quoted 40 lines · Roger Pau Monné
Quoted 8 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
At the moment, we always allocate an extra 16 slots for IO handlers (see
MAX_IO_HANDLER). So while adding IO trap handlers for the emulated MSI-X registers we need to explicitly tell that we have additional IO handlers, so those are accounted.Signed-off-by Oleksandr Andrushchenko <[email protected]>LGTM, just one comment below. This will require an Ack from the Arm guys.
Quoted 28 lines
---
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
---
This actually moved here from the part 2 of the prep work for PCI passthrough on Arm as it seems to be the proper place for it.New in v5
--- xen/arch/arm/vpci.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)xen/arch/arm/vpci.c +2
-0@@ -134,6 +134,8 @@ static int vpci_get_num_handlers_cb(struct domain *d,unsigned int domain_vpci_get_num_mmio_handlers (struct domain *d) { +unsigned int count; +if ( !has_vpci (d) )return 0 ;@@ -145,7 +147,18 @@ unsigned int domain_vpci_get_num_mmio_handlers(struct domain *d) }
Quoted 13 lines
xen/arch/arm/vpci.c unsigned int domain_vpci_get_num_mmio_handlers(struct domain *d) · line 158
Quoted 11 lines
xen/arch/arm/vpci.c +9
-1/* For a single emulated host bridge's configuration space. */ -return 1 ; + count =1 ; + +#ifdef CONFIG_HAS_PCI_MSI +/* +* There's a single MSI-X MMIO handler that deals with both PBA +* and MSI-X tables per each PCI device being passed through. +* Maximum number of emulated virtual devices is VPCI_MAX_VIRT_DEV. +*/ + count +=VPCI_MAX_VIRT_DEV ;You could also use
IS_ENABLED(CONFIG_HAS_PCI_MSI)sinceVPCI_MAX_VIRT_DEVis defined unconditionally.
Yes, will use, thank you
Quoted 2 lines
Thanks, Roger.
Thank you,
Oleksandr
PATCH v5 14/14 vpci: add TODO for the registers not explicitly handled
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
For unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.
Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
Add the corresponding TODO comment to highlight there is a problem that needs to be fixed.
---
New in v5
--- xen/drivers/vpci/vpci.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
xen/drivers/vpci/vpci.c +23 -0
@@ -493,6 +493,29 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size)if ( !pdev->vpci ) {spin_unlock (&pdev->vpci_lock); +/* +* TODO: for unprivileged guests vpci_{read|write} need to be re-worked +* to not passthrough accesses to the registers not explicitly handled +* by the corresponding vPCI handlers: without fixing that passthrough +* to guests is completely unsafe as Xen allows them full access to +* the registers. +* +* Xen needs to be sure that every register a guest accesses is not +* going to cause the system to malfunction, so Xen needs to keep a +* list of the registers it is safe for a guest to access. +* +* For example, we should only expose the PCI capabilities that we know +* are safe for a guest to use, i.e.: MSI and MSI-X initially. +* The rest of the capabilities should be blocked from guest access, +* unless we audit them and declare safe for a guest to access. +* +* As a reference we might want to look at the approach currently used +* by QEMU in order to do PCI passthrough. A very limited set of PCI +* capabilities known to be safe for untrusted access are exposed to the +* guest and registers need to be explicitly handled or else access is +* rejected. Xen needs a fairly similar model in vPCI or else none of +* this will be safe for unprivileged access. +*/ return vpci_read_hw (sbdf, reg, size); }
Signature · 1 line
2.25.1
Re: [PATCH v5 14/14] vpci: add TODO for the registers not explicitly handled
Quoted 24 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
For unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.
Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
Quoted 6 lines
Add the corresponding TODO comment to highlight there is a problem that needs to be fixed.
Suggested-by Roger Pau Monné <[email protected]>Suggested-by Jan Beulich <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>
Looks okay to me in principle, but imo needs to come earlier in the series, before things actually get exposed to DomU-s.
Jan
Re: [PATCH v5 14/14] vpci: add TODO for the registers not explicitly handled
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
For unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.
Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
Quoted 7 lines
Quoted 6 lines
Add the corresponding TODO comment to highlight there is a problem that needs to be fixed.
Suggested-by Roger Pau Monné <[email protected]>Suggested-by Jan Beulich <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>Looks okay to me in principle,
Thanks Roger for writing most of the text in e-mails while discussing the issue
Quoted 2 lines
but imo needs to come earlier in the
series, before things actually get exposed to DomU-s.
I can have it after "[PATCH v5 05/14] vpci: add hooks for PCI device assign/de-assign"
Quoted 2 lines
Jan
Thank you,
Oleksandr
Re: [PATCH v5 14/14] vpci: add TODO for the registers not explicitly handled
In reply to Jan BeulichQuoted 28 lines · Jan Beulich
Quoted 27 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
For unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.
Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
Add the corresponding TODO comment to highlight there is a problem that needs to be fixed.
Quoted 6 lines
Quoted 3 lines
Suggested-by Roger Pau Monné <[email protected]>Suggested-by Jan Beulich <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>Looks okay to me in principle, but imo needs to come earlier in the series, before things actually get exposed to DomU-s.
Are domUs really allowed to use this code? Maybe it's done in a separate series, but has_vpci is hardcoded to false on Arm, and X86_EMU_VPCI can only be set for the hardware domain on x86.
Roger.
Re: [PATCH v5 14/14] vpci: add TODO for the registers not explicitly handled
Quoted 33 lines · Roger Pau Monné
Quoted 32 lines · Jan Beulich
Quoted 30 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
For unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.
Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
Add the corresponding TODO comment to highlight there is a problem that needs to be fixed.
Suggested-by Roger Pau Monné <[email protected]>Suggested-by Jan Beulich <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>
Quoted 6 lines
Quoted 2 lines
Looks okay to me in principle, but imo needs to come earlier in the series, before things actually get exposed to DomU-s.
Are domUs really allowed to use this code? Maybe it's done in a separate series, but
has_vpciis hardcoded to false on Arm, andX86_EMU_VPCIcan only be set for the hardware domain on x86.
I'm not sure either. This series gives the impression of exposing things, but I admit I didn't pay attention to has_vpci() being hardcoded on Arm. Then again there were at least 3 series in parallel originally, with interdependencies (iirc) not properly spelled out ...
Jan
Re: [PATCH v5 14/14] vpci: add TODO for the registers not explicitly handled
Hello, Roger, Jan!
Quoted 30 lines · Jan Beulich
Quoted 29 lines · Roger Pau Monné
Quoted 28 lines · Jan Beulich
Quoted 27 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
For unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.
Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
Add the corresponding TODO comment to highlight there is a problem that needs to be fixed.
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Suggested-by Roger Pau Monné <[email protected]>Suggested-by Jan Beulich <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>Looks okay to me in principle, but imo needs to come earlier in the series, before things actually get exposed to DomU-s.
Are domUs really allowed to use this code? Maybe it's done in a separate series, but
has_vpciis hardcoded to false on Arm, andX86_EMU_VPCIcan only be set for the hardware domain on x86.
That is by intention: we do not want to have this enabled on Arm until it can really be used...
Quoted 2 lines
I'm not sure either. This series gives the impression of exposing things, but I admit I didn't pay attention to
has_vpci()being hardcoded on Arm.
...so we enable vPCI on Arm right after we are all set
Quoted 2 lines
Then again there were at least 3 series in parallel originally, with interdependencies (iirc) not properly spelled out ...
Sorry about that, we should have said that explicitly
Quoted 3 lines
Jan
Thank you,
Oleksandr
Dear rest maintainers!
Could you please review this series which seems to get stuck?
Thank you in advance,
Oleksandr
Quoted 112 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Hi, all!
1. This patch series is focusing on vPCI and adds support for non-identity PCI BAR mappings which is required while passing through a PCI device to a guest. The highlights are:
- Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
- Emulate guest BAR register values based on physical BAR values. This allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
- Instead of handling a single range set, that contains all the memory regions of all the BARs and ROM, have them per BAR.
- Take into account guest's BAR view and program its p2m accordingly:
gfn is guest's view of the BAR and mfn is the physical BAR value as set up by the host bridge in the hardware domain.
This way hardware doamin sees physical BAR values and guest sees emulated ones.2. The series also adds support for virtual PCI bus topology for guests:
- We emulate a single host bridge for the guest, so segment is always 0.
- The implementation is limited to 32 devices which are allowed on a single PCI bus.
- The virtual bus number is set to 0, so virtual devices are seen as embedded endpoints behind the root complex.3. The series has complete re-work of the locking scheme used/absent before with the help of the work started by Roger [1]:
[PATCH v5 03/13] vpci: move lock outside of struct vpciThis way the lock can be used to check whether vpci is present, and removal can be performed while holding the lock, in order to make sure there are no accesses to the contents of the vpci struct. Previously removal could race with
vpci_readfor example, since the lock was dropped prior to freeingpdev->vpci.
This also solves synchronization issues between all vPCI code entities which could run in parallel.4. There is an outstanding TODO left unimplemented by this series:
for unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
5. The series was also tested on:
- x86 PVH Dom0 and doesn't break it.
- x86 HVM with PCI passthrough to DomU and doesn't break it.Thank you,
Oleksandr[1] https://lore.kernel.org/xen-devel/[email protected]/
Oleksandr Andrushchenko (13):
rangeset: addRANGESETF_no_printflag
vpci: fix function attributes forvpci_process_pending
vpci: cancel pending map/unmap on vpci removal
vpci: add hooks for PCI device assign/de-assign vpci/header: implement guest BAR register handlers vpci/header: handle p2m range sets per BAR vpci/header: program p2m with guest BAR view vpci/header: emulatePCI_COMMANDregister for guests vpci/header: reset the command register when adding devices
vpci: add initial support for virtual PCI bus topology xen/arm: translate virtual PCI bus topology for guests xen/arm: account IO handlers for emulated PCI MSI-X
vpci: add TODO for the registers not explicitly handledRoger Pau Monne (1):
vpci: move lock outside of struct vpcitools/tests/vpci/emul.h | 5 +- tools/tests/vpci/main.c | 4 +- xen/arch/arm/vpci.c | 33 +++- xen/arch/x86/hvm/vmsi.c | 8 +- xen/common/rangeset.c | 5 +- xen/drivers/Kconfig | 4 + xen/drivers/passthrough/pci.c | 11 ++ xen/drivers/vpci/header.c | 352 +++++++++++++++++++++++++++------- xen/drivers/vpci/msi.c | 11 +- xen/drivers/vpci/msix.c | 8 +- xen/drivers/vpci/vpci.c | 252 +++++++++++++++++++++--- xen/include/xen/pci.h | 6 + xen/include/xen/rangeset.h | 7 +- xen/include/xen/sched.h | 8 + xen/include/xen/vpci.h | 47 ++++- 15 files changed, 644 insertions(+), 117 deletions(-)
Quoted 3 lines · Oleksandr Andrushchenko
Dear rest maintainers!
Could you please review this series which seems to get stuck?
I don't seem to have any record of you having pinged Roger as the vPCI maintainer. Also, as said on the Community Call when discussing this, I don't think I'd view this series as in a state where an emergency fallback to REST would be appropriate. As indicated, in particular I wouldn't want to commit any of it without Roger's basic agreement. IOW while REST maintainer reviews may help making progress (but as much would reviews by anyone else), they may not put the series in a state where it could go in.
In any event, as also said on the call, afaic this series is in my to- be-reviewed folder, alongside a few dozen more patches. I'll get to it if nobody else would, but I can't predict when that's going to be. There's simply too much other stuff in need of taking care of.
Jan
Quoted 116 lines
Thank you in advance,
OleksandrQuoted 112 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Hi, all!
1. This patch series is focusing on vPCI and adds support for non-identity PCI BAR mappings which is required while passing through a PCI device to a guest. The highlights are:
- Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
- Emulate guest BAR register values based on physical BAR values. This allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
- Instead of handling a single range set, that contains all the memory regions of all the BARs and ROM, have them per BAR.
- Take into account guest's BAR view and program its p2m accordingly:
gfn is guest's view of the BAR and mfn is the physical BAR value as set up by the host bridge in the hardware domain.
This way hardware doamin sees physical BAR values and guest sees emulated ones.2. The series also adds support for virtual PCI bus topology for guests:
- We emulate a single host bridge for the guest, so segment is always 0.
- The implementation is limited to 32 devices which are allowed on a single PCI bus.
- The virtual bus number is set to 0, so virtual devices are seen as embedded endpoints behind the root complex.3. The series has complete re-work of the locking scheme used/absent before with the help of the work started by Roger [1]:
[PATCH v5 03/13] vpci: move lock outside of struct vpciThis way the lock can be used to check whether vpci is present, and removal can be performed while holding the lock, in order to make sure there are no accesses to the contents of the vpci struct. Previously removal could race with
vpci_readfor example, since the lock was dropped prior to freeingpdev->vpci.
This also solves synchronization issues between all vPCI code entities which could run in parallel.4. There is an outstanding TODO left unimplemented by this series:
for unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
5. The series was also tested on:
- x86 PVH Dom0 and doesn't break it.
- x86 HVM with PCI passthrough to DomU and doesn't break it.Thank you,
Oleksandr[1] https://lore.kernel.org/xen-devel/[email protected]/
Oleksandr Andrushchenko (13):
rangeset: addRANGESETF_no_printflag
vpci: fix function attributes forvpci_process_pending
vpci: cancel pending map/unmap on vpci removal
vpci: add hooks for PCI device assign/de-assign vpci/header: implement guest BAR register handlers vpci/header: handle p2m range sets per BAR vpci/header: program p2m with guest BAR view vpci/header: emulatePCI_COMMANDregister for guests vpci/header: reset the command register when adding devices
vpci: add initial support for virtual PCI bus topology xen/arm: translate virtual PCI bus topology for guests xen/arm: account IO handlers for emulated PCI MSI-X
vpci: add TODO for the registers not explicitly handledRoger Pau Monne (1):
vpci: move lock outside of struct vpcitools/tests/vpci/emul.h | 5 +- tools/tests/vpci/main.c | 4 +- xen/arch/arm/vpci.c | 33 +++- xen/arch/x86/hvm/vmsi.c | 8 +- xen/common/rangeset.c | 5 +- xen/drivers/Kconfig | 4 + xen/drivers/passthrough/pci.c | 11 ++ xen/drivers/vpci/header.c | 352 +++++++++++++++++++++++++++------- xen/drivers/vpci/msi.c | 11 +- xen/drivers/vpci/msix.c | 8 +- xen/drivers/vpci/vpci.c | 252 +++++++++++++++++++++--- xen/include/xen/pci.h | 6 + xen/include/xen/rangeset.h | 7 +- xen/include/xen/sched.h | 8 + xen/include/xen/vpci.h | 47 ++++- 15 files changed, 644 insertions(+), 117 deletions(-)
Hi, Jan!
Quoted 6 lines · Jan Beulich
Quoted 3 lines · Oleksandr Andrushchenko
Dear rest maintainers!
Could you please review this series which seems to get stuck?
I don't seem to have any record of you having pinged Roger as the vPCI maintainer.
No, I didn't. Roger is on CC, so he might shed some light on when it might happen, so we, those who work on PCI passthrough on Arm,
can also plan the relevant upcoming (re)work: we still miss MSI/MSI-X and IOMMU series which do depend on this one
Quoted 3 lines
Also, as said on the Community Call when discussing this,
I don't think I'd view this series as in a state where an emergency fallback to REST would be appropriate.
No emergency here, but v5 without any ack/nack might ring a bell Which made me write this e-mail
Quoted 2 lines
As indicated, in particular I
wouldn't want to commit any of it without Roger's basic agreement.
This is clear as it is up to the relevant maintainer to commit which I might not expect from the rest maintainers
Quoted 3 lines
IOW
while REST maintainer reviews may help making progress (but as much would reviews by anyone else),
This is my goal: to have ack/nack at least from the REST mainatainers
Quoted 2 lines
they may not put the series in a state
where it could go in.
Fair enough
Quoted 3 lines
In any event, as also said on the call, afaic this series is in my to- be-reviewed folder,
Appreciate this
Quoted 2 lines
alongside a few dozen more patches. I'll get to it
if nobody else would, but I can't predict when that's going to be.
Thank you
Quoted 1 line
There's simply too much other stuff in need of taking care of.
Sure, our companies do want us to do something useful for them as well, but review
Thank you,
Oleksandr
Quoted 119 lines
Jan
Quoted 116 lines
Thank you in advance,
OleksandrQuoted 112 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
Hi, all!
1. This patch series is focusing on vPCI and adds support for non-identity PCI BAR mappings which is required while passing through a PCI device to a guest. The highlights are:
- Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests.
- Emulate guest BAR register values based on physical BAR values. This allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest.
- Instead of handling a single range set, that contains all the memory regions of all the BARs and ROM, have them per BAR.
- Take into account guest's BAR view and program its p2m accordingly:
gfn is guest's view of the BAR and mfn is the physical BAR value as set up by the host bridge in the hardware domain.
This way hardware doamin sees physical BAR values and guest sees emulated ones.2. The series also adds support for virtual PCI bus topology for guests:
- We emulate a single host bridge for the guest, so segment is always 0.
- The implementation is limited to 32 devices which are allowed on a single PCI bus.
- The virtual bus number is set to 0, so virtual devices are seen as embedded endpoints behind the root complex.3. The series has complete re-work of the locking scheme used/absent before with the help of the work started by Roger [1]:
[PATCH v5 03/13] vpci: move lock outside of struct vpciThis way the lock can be used to check whether vpci is present, and removal can be performed while holding the lock, in order to make sure there are no accesses to the contents of the vpci struct. Previously removal could race with
vpci_readfor example, since the lock was dropped prior to freeingpdev->vpci.
This also solves synchronization issues between all vPCI code entities which could run in parallel.4. There is an outstanding TODO left unimplemented by this series:
for unprivileged guests vpci_{read|write} need to be re-worked to not passthrough accesses to the registers not explicitly handled by the corresponding vPCI handlers: without fixing that passthrough to guests is completely unsafe as Xen allows them full access to the registers.Xen needs to be sure that every register a guest accesses is not going to cause the system to malfunction, so Xen needs to keep a list of the registers it is safe for a guest to access.
For example, we should only expose the PCI capabilities that we know are safe for a guest to use, i.e.: MSI and MSI-X initially. The rest of the capabilities should be blocked from guest access, unless we audit them and declare safe for a guest to access.
As a reference we might want to look at the approach currently used by QEMU in order to do PCI passthrough. A very limited set of PCI capabilities known to be safe for untrusted access are exposed to the guest and registers need to be explicitly handled or else access is rejected. Xen needs a fairly similar model in vPCI or else none of this will be safe for unprivileged access.
5. The series was also tested on:
- x86 PVH Dom0 and doesn't break it.
- x86 HVM with PCI passthrough to DomU and doesn't break it.Thank you,
Oleksandr[1] https://urldefense.com/v3/__https://lore.kernel.org/xen-devel/[email protected]/__;!!GF_29dbcQIUBPA!ntDLQ-kiosLLPDLG_D7C7Sdeb1Ad1j-43XjuCGTMgeJNboANStsYFP6a1hR43s67GNuFAx7Hug$ [lore[.]kernel[.]org]
Oleksandr Andrushchenko (13):
rangeset: addRANGESETF_no_printflag
vpci: fix function attributes forvpci_process_pending
vpci: cancel pending map/unmap on vpci removal
vpci: add hooks for PCI device assign/de-assign vpci/header: implement guest BAR register handlers vpci/header: handle p2m range sets per BAR vpci/header: program p2m with guest BAR view vpci/header: emulatePCI_COMMANDregister for guests vpci/header: reset the command register when adding devices
vpci: add initial support for virtual PCI bus topology xen/arm: translate virtual PCI bus topology for guests xen/arm: account IO handlers for emulated PCI MSI-X
vpci: add TODO for the registers not explicitly handledRoger Pau Monne (1):
vpci: move lock outside of struct vpcitools/tests/vpci/emul.h | 5 +- tools/tests/vpci/main.c | 4 +- xen/arch/arm/vpci.c | 33 +++- xen/arch/x86/hvm/vmsi.c | 8 +- xen/common/rangeset.c | 5 +- xen/drivers/Kconfig | 4 + xen/drivers/passthrough/pci.c | 11 ++ xen/drivers/vpci/header.c | 352 +++++++++++++++++++++++++++------- xen/drivers/vpci/msi.c | 11 +- xen/drivers/vpci/msix.c | 8 +- xen/drivers/vpci/vpci.c | 252 +++++++++++++++++++++--- xen/include/xen/pci.h | 6 + xen/include/xen/rangeset.h | 7 +- xen/include/xen/sched.h | 8 + xen/include/xen/vpci.h | 47 ++++- 15 files changed, 644 insertions(+), 117 deletions(-)
Quoted 6 lines · Oleksandr Andrushchenko
Hi, Jan!
Quoted 3 lines · Jan Beulich
Quoted 2 lines · Oleksandr Andrushchenko
Dear rest maintainers!
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Could you please review this series which seems to get stuck?
I don't seem to have any record of you having pinged Roger as the vPCI maintainer.
No, I didn't. Roger is on CC, so he might shed some light on when it might happen, so we, those who work on PCI passthrough on Arm,
can also plan the relevant upcoming (re)work: we still miss MSI/MSI-X and IOMMU series which do depend on this one
Hello,
I'm quite overloaded with patch review and other stuff, since I've taken over the Community Manager role while George is away.
There are series on the mailing list that have been pending for way longer, and while I understand that this is of no help or relief for you it wouldn't be fair for me to review this piece for work before other series that have been pending for longer, as other submitters also deserve review.
Sorry, but I think it's unlikely I will get to it until after new year.
Thanks, Roger.
Hi, Roger!
Quoted 16 lines · Roger Pau Monné
Quoted 6 lines · Oleksandr Andrushchenko
Hi, Jan!
Quoted 3 lines · Jan Beulich
Quoted 2 lines · Oleksandr Andrushchenko
Dear rest maintainers!
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Could you please review this series which seems to get stuck?
I don't seem to have any record of you having pinged Roger as the vPCI maintainer.
No, I didn't. Roger is on CC, so he might shed some light on when it might happen, so we, those who work on PCI passthrough on Arm,
can also plan the relevant upcoming (re)work: we still miss MSI/MSI-X and IOMMU series which do depend on this oneHello,
Quoted 8 lines
I'm quite overloaded with patch review and other stuff, since I've taken over the Community Manager role while George is away.
There are series on the mailing list that have been pending for way longer, and while I understand that this is of no help or relief for you it wouldn't be fair for me to review this piece for work before other series that have been pending for longer, as other submitters also deserve review.
This is fair
Quoted 3 lines
Sorry, but I think it's unlikely I will get to it until after new year.
Thank you in advance,
Oleksandr
Quoted 2 lines
Thanks, Roger.