vpci: allow 32-bit BAR writes with memory decoding enabled
v4 cover vpci: allow 32-bit BAR writes with memory decoding enabled
These 2 patches
("vpci: Use pervcpu ranges for BAR mapping")
("vpci: allow queueing of mapping operations")
are also pre-requisites for SR-IOV.
Pipeline: https://gitlab.com/xen-project/people/stewarthildebrand/xen/-/pipelines/2432615038
v3->v4:
* switch back to dynamically allocated queue elements
v2->v3:
* add ("vpci: Use pervcpu ranges for BAR mapping")
* rework with fixed array of map/unmap slots
v1->v2:
* new approach with queued p2m operations
RFC->v1:
* rework BAR mapping machinery to support unmap-then-map operation
v3: https://lore.kernel.org/xen-devel/[email protected]/T/#t
v2: https://lore.kernel.org/xen-devel/[email protected]/T/#t
v1: https://lore.kernel.org/xen-devel/[email protected]/T/#t
RFC: https://lore.kernel.org/xen-devel/[email protected]/T/#t
Mykyta Poturai (1):
vpci: Use pervcpu ranges for BAR mapping
Stewart Hildebrand (3):
vpci: allow queueing of mapping operations
vpci: allow BAR map/unmap without affecting memory decoding bit
vpci: allow 32-bit BAR writes with memory decoding enabled
xen/common/domain.c | 2 + xen/drivers/vpci/header.c | 333 ++++++++++++++++++++++++-------------- xen/drivers/vpci/vpci.c | 10 +- xen/include/xen/vpci.h | 22 ++- 4 files changed, 239 insertions(+), 128 deletions(-)
base-commit: 33ceaa28275ca4e298616689ef96f19efaa87c35
Signature · 1 line
2.53.0
PATCH v4 1/4 vpci: Use pervcpu ranges for BAR mapping
From: Mykyta Poturai <[email protected]>
There is no need to store ranges for each PCI device, as they are only used during the mapping/unmapping process and can be reused for each device. This also allows to avoid the need to allocate and destroy rangesets for each device.
Move the rangesets from struct vpci_bar to struct vpci_vcpu and perform (de-)allocation with vcpu (de-)allocation. Introduce RANGESET_DESTROY() macro to free a rangeset and set the pointer to NULL.
Amends: 622bdd962822 ("vpci/header: handle p2m range sets per BAR")
---
It seems a bit awkward to introduce various vpci vcpu alloc/dealloc functions here only to undo most of it in the next patch. Thoughts on folding the next patch into this one?
v3->v4:
* no change
v2->v3:
* new patch in this series, borrowed from [1]
* add Amends tag
* remove unused variable i due to rebasing over 998060dd9101 ("vPCI:
move vpci_init_capabilities() to a separate file")
* enclose entire struct vpci_vcpu inside #ifdef __XEN__
* s/bar_mem/mem/
* use ARRAY_SIZE
* put init/destroy in functions
* only allocate for domains with vPCI and idle domain
* replace 'if ( !mem ) continue;' with ASSERT
v1->v2 (in SR-IOV series [1]):
* new patch
[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t
--- xen/common/domain.c | 5 +++ xen/drivers/vpci/header.c | 67 ++++++++++++++------------------------ xen/drivers/vpci/vpci.c | 36 +++++++++++++++++--- xen/include/xen/rangeset.h | 7 ++++ xen/include/xen/vpci.h | 10 ++++-- 5 files changed, 75 insertions(+), 50 deletions(-)
xen/common/domain.c +5 -0
@@ -455,6 +455,8 @@ static int vcpu_teardown(struct vcpu *v)*/ static void vcpu_destroy (struct vcpu *v) { +vpci_vcpu_destroy (v); +free_vcpu_struct (v); }
@@ -512,6 +514,9 @@ struct vcpu *vcpu_create(struct domain *d, unsigned int vcpu_id)if (arch_vcpu_create (v) !=0 ) goto fail_sched; +if (vpci_vcpu_init (v) ) + goto fail_sched; + d->vcpu[vcpu_id] = v;if ( vcpu_id !=0 ) {
xen/drivers/vpci/header.c +24 -43
176 lines not on this page · raw message
xen/drivers/vpci/vpci.c +31 -5
@@ -24,6 +24,37 @@ #ifdef __XEN__ +void vpci_vcpu_destroy(struct vcpu *v) +{ + unsigned int i; + + if ( !has_vpci(v->domain) && !is_idle_domain(v->domain) ) + return; + + for ( i = 0; i < ARRAY_SIZE(v->vpci.mem); i++ ) + RANGESET_DESTROY(v->vpci.mem[i]); +} + +int vpci_vcpu_init(struct vcpu *v) +{ + unsigned int i; + + if ( !has_vpci(v->domain) && !is_idle_domain(v->domain) ) + return 0; + + for ( i = 0; i < ARRAY_SIZE(v->vpci.mem); i++ ) + { + char str[32]; + + snprintf(str, sizeof(str), "%pv:BAR%u", v, i); + v->vpci.mem[i] = rangeset_new(v->domain, str, RANGESETF_no_print); + if ( !v->vpci.mem[i] ) + return -ENOMEM; + } + + return 0; +} + #ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT static int assign_virtual_sbdf(struct pci_dev *pdev) {
@@ -89,8 +120,6 @@ struct vpci_register *vpci_get_register(const struct vpci *vpci,void vpci_deassign_device (struct pci_dev *pdev) { -unsigned int i; -ASSERT (rw_is_write_locked (&pdev->domain->pci_lock));if ( !has_vpci (pdev->domain) || !pdev->vpci )
@@ -116,9 +145,6 @@ void vpci_deassign_device(struct pci_dev *pdev) }spin_unlock (&pdev->vpci->lock); -for ( i =0 ; i <ARRAY_SIZE (pdev->vpci->header.bars); i++ ) -rangeset_destroy (pdev->vpci->header.bars[i].mem); -xfree (pdev->vpci); pdev->vpci =NULL ; }
xen/include/xen/rangeset.h +7 -0
@@ -40,6 +40,13 @@ struct rangeset *rangeset_new(void rangeset_destroy (struct rangeset *r); +/* Destroy a rangeset, and zero the pointer to it. */ +#define RANGESET_DESTROY (r) \ + ({ \ + rangeset_destroy(r); \ + (r) = NULL; \ + }) +/* * Set a limit on the number of ranges that may exist in set @r. * NOTE: This must be called while @r is empty.
xen/include/xen/vpci.h +8 -2
@@ -19,6 +19,9 @@*/ #define VPCI_MAX_VIRT_DEV (PCI_SLOT(~0) + 1) +void vpci_vcpu_destroy (struct vcpu *v); +int vpci_vcpu_init (struct vcpu *v); +/* Assign vPCI to device by adding handlers. */ int __must_check vpci_assign_device (struct pci_dev *pdev);
@@ -54,7 +57,6 @@ struct vpci {uint64_t guest_addr;uint64_t size;uint64_t resizable_sizes; -struct rangeset *mem;enum {VPCI_BAR_EMPTY ,VPCI_BAR_IO ,
@@ -152,14 +154,15 @@ struct vpci {#endif }; +#ifdef __XEN__struct vpci_vcpu {/* Per-vcpu structure to store state while {un}mapping of PCI BARs. */ const struct pci_dev *pdev; +struct rangeset *mem[ARRAY_SIZE (((struct vpci_header *)NULL )->bars)];uint16_t cmd;bool rom_only :1 ; }; -#ifdef __XEN__void vpci_dump_msi (void );/* Arch-specific vPCI MSI helpers. */
@@ -204,6 +207,9 @@ bool vpci_ecam_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,#else /* !CONFIG_HAS_VPCI */ struct vpci_vcpu {}; +static inline void vpci_vcpu_destroy (struct vcpu *v) { } +static inline int vpci_vcpu_init (struct vcpu *v) {return 0 ; } +static inline int vpci_reinit_ext_capabilities (struct pci_dev *pdev) {return 0 ;
Signature · 1 line
2.53.0
Re: [PATCH v4 1/4] vpci: Use pervcpu ranges for BAR mapping
Quoted 10 lines · Stewart Hildebrand
From: Mykyta Poturai <[email protected]>
There is no need to store ranges for each PCI device, as they are only used during the mapping/unmapping process and can be reused for each device. This also allows to avoid the need to allocate and destroy rangesets for each device.
Move the rangesets from struct
vpci_barto structvpci_vcpuand perform (de-)allocation with vcpu (de-)allocation. IntroduceRANGESET_DESTROY()macro to free a rangeset and set the pointer to NULL.
I'm struggling some with this description. On a typical system I might expect far more vCPU-s to be there than there are PCI devices. In which case "There is no need to ..." and "This also allows to avoid ..." feels like an attempt to mislead readers: We may end up with bigger memory footprint, when my reading of the description suggests you're trying to hint at a reduction.
xen/common/domain.c static int vcpu_teardown(struct vcpu *v) · line 461
Quoted 10 lines
xen/common/domain.c +2
-0@@ -455,6 +455,8 @@ static int vcpu_teardown(struct vcpu *v)*/ static void vcpu_destroy (struct vcpu *v) { +vpci_vcpu_destroy (v); +free_vcpu_struct (v); }
Can't the resources be released much earlier, somewhere during domain_relinquish_resources()?
xen/include/xen/rangeset.h struct rangeset *rangeset_new( · line 48
Quoted 12 lines
xen/include/xen/rangeset.h +6
-0@@ -40,6 +40,13 @@ struct rangeset *rangeset_new(void rangeset_destroy (struct rangeset *r); +/* Destroy a rangeset, and zero the pointer to it. */ +#define RANGESET_DESTROY (r) \ + ({ \ + rangeset_destroy(r); \ + (r) = NULL; \ + })
Please note the subtle but important difference (in ordering of operations) from e.g. XFREE() or FREE_XENHEAP_PAGES().
Jan
Re: [PATCH v4 1/4] vpci: Use pervcpu ranges for BAR mapping
In reply to Stewart HildebrandQuoted 14 lines · Stewart Hildebrand
From: Mykyta Poturai <[email protected]>
There is no need to store ranges for each PCI device, as they are only used during the mapping/unmapping process and can be reused for each device. This also allows to avoid the need to allocate and destroy rangesets for each device.
Move the rangesets from struct
vpci_barto structvpci_vcpuand perform (de-)allocation with vcpu (de-)allocation. IntroduceRANGESET_DESTROY()macro to free a rangeset and set the pointer to NULL.Amends:
622bdd962822("vpci/header: handle p2m range sets per BAR")Signed-off-by Mykyta Poturai <[email protected]>Signed-off-by Stewart Hildebrand <[email protected]>
Quoted 7 lines
---
It seems a bit awkward to introduce various vpci vcpu alloc/dealloc functions here only to undo most of it in the next patch. Thoughts on folding the next patch into this one?
v3->v4:
* no change
There are some complexities here due to the patch being part of two series. Once Mykyta re-submits the SR-IOV series, we'll have two (likely diverging) v4-s. Already here ...
Quoted 11 lines
v2->v3:
* new patch in this series, borrowed from [1]
* add Amends tag
* remove unused variable i due to rebasing over998060dd9101("vPCI:
movevpci_init_capabilities()to a separate file")
* enclose entire structvpci_vcpuinside #ifdef__XEN__
* s/bar_mem/mem/
* useARRAY_SIZE
* put init/destroy in functions
* only allocate for domains with vPCI and idle domain
* replace 'if ( !mem ) continue;' with ASSERT
... the v3 there has one more item on this ChangeLog list ("* synced with BAR write with memory decoding enabled series[1]"), albeit maybe (now that I read it again) this merely is the counterpart of the first bullet point here. It would be clearer if there the other series' title was supplied verbatim and in quotes.
Jan
Re: [PATCH v4 1/4] vpci: Use pervcpu ranges for BAR mapping
Quoted 39 lines · Jan Beulich
Quoted 14 lines · Stewart Hildebrand
From: Mykyta Poturai <[email protected]>
There is no need to store ranges for each PCI device, as they are only used during the mapping/unmapping process and can be reused for each device. This also allows to avoid the need to allocate and destroy rangesets for each device.
Move the rangesets from struct
vpci_barto structvpci_vcpuand perform (de-)allocation with vcpu (de-)allocation. IntroduceRANGESET_DESTROY()macro to free a rangeset and set the pointer to NULL.Amends:
622bdd962822("vpci/header: handle p2m range sets per BAR")Signed-off-by Mykyta Poturai <[email protected]>Signed-off-by Stewart Hildebrand <[email protected]>Quoted 7 lines
---
It seems a bit awkward to introduce various vpci vcpu alloc/dealloc functions here only to undo most of it in the next patch. Thoughts on folding the next patch into this one?
v3->v4:
* no changeThere are some complexities here due to the patch being part of two series. Once Mykyta re-submits the SR-IOV series, we'll have two (likely diverging) v4-s. Already here ...
Quoted 11 lines
v2->v3:
* new patch in this series, borrowed from [1]
* add Amends tag
* remove unused variable i due to rebasing over998060dd9101("vPCI:
movevpci_init_capabilities()to a separate file")
* enclose entire structvpci_vcpuinside #ifdef__XEN__
* s/bar_mem/mem/
* useARRAY_SIZE
* put init/destroy in functions
* only allocate for domains with vPCI and idle domain
* replace 'if ( !mem ) continue;' with ASSERT
Quoted 5 lines
... the v3 there has one more item on this ChangeLog list ("* synced with BAR write with memory decoding enabled series[1]"), albeit maybe (now that I read it again) this merely is the counterpart of the first bullet point here. It would be clearer if there the other series' title was supplied verbatim and in quotes.
Indeed. I reviewed the one from Mykyta part of the SR-IOV series. I see both Jan and myself made similar comments, I'm also a bit concerned about the claim that moving to per-vCPU ranges is better than using per-pdev ranges, see the comments on the SR-IOV series.
Overall however it seems to me for the SR-IOV series we will need to allocate mapping tasks on demand, so we will neither have per-vCPU or per-pdev per-allocated tasks (that include the rangeset). I think we need a pre-series that introduce the map queueing plus the on-demand allocation of tasks in a clean way (iow: there's no need to introduce per-vCPU tasks if they need to be replaced with runtime allocation tasks for the SR-IOV support).
Then both this series and the SR-IOV one should be rebased over that work.
Thanks, Roger.
PATCH v4 2/4 vpci: allow queueing of mapping operations
In reply to Stewart HildebrandIntroduce vPCI BAR mapping task queue. Store information needed to map/unmap BARs in struct vpci_map_task. Allow queueing of BAR map/unmap operations in a list, thus making it possible to perform multiple p2m operations associated with single PCI device.
This is preparatory work for further changes that need to perform multiple unmap/map operations before returning to guest.
At the moment, only a single operation will be queued. However, when multiple operations are queued, there is a check in modify_bars() to skip BARs already in the requested state that will no longer be accurate. Remove this check in preparation of upcoming changes.
---apply_map() and vpci_process_map_task() are very similar. Should we try to combine them into a single function?
I concede that the dynamic allocation/deallocation of struct vpci_map_task is not ideal. However, to support SR-IOV, there will be a need to queue many mapping operations (one per VF), and statically pre-allocating that much would seem wasteful. Only the hardware and/or control domain would need to queue many operations, and only when configuring SR-IOV.
v3->v4:
* switch back to dynamically allocated queue elements
v2->v3:
* base on ("vpci: Use pervcpu ranges for BAR mapping") from [1]
* rework with fixed array of map/unmap slots
[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t
v1->v2:
* new patch
--- xen/common/domain.c | 5 +- xen/drivers/vpci/header.c | 227 ++++++++++++++++++++++++++----------- xen/drivers/vpci/vpci.c | 30 +---- xen/include/xen/rangeset.h | 7 -- xen/include/xen/vpci.h | 21 ++-- 5 files changed, 179 insertions(+), 111 deletions(-)
xen/common/domain.c +1 -4
@@ -455,8 +455,6 @@ static int vcpu_teardown(struct vcpu *v)*/ static void vcpu_destroy (struct vcpu *v) { -vpci_vcpu_destroy (v); -free_vcpu_struct (v); }
@@ -514,8 +512,7 @@ struct vcpu *vcpu_create(struct domain *d, unsigned int vcpu_id)if (arch_vcpu_create (v) !=0 ) goto fail_sched; -if (vpci_vcpu_init (v) ) - goto fail_sched; +vpci_vcpu_init (v); d->vcpu[vcpu_id] = v;if ( vcpu_id !=0 )
xen/drivers/vpci/header.c +162 -65
394 lines not on this page · raw message
xen/drivers/vpci/vpci.c +2 -28
@@ -24,35 +24,9 @@#ifdef __XEN__ -void vpci_vcpu_destroy (struct vcpu *v) +void vpci_vcpu_init (struct vcpu *v) { -unsigned int i; - -if ( !has_vpci (v->domain) && !is_idle_domain (v->domain) ) -return ; - -for ( i =0 ; i <ARRAY_SIZE (v->vpci.mem); i++ ) -RANGESET_DESTROY (v->vpci.mem[i]); -} - -int vpci_vcpu_init (struct vcpu *v) -{ -unsigned int i; - -if ( !has_vpci (v->domain) && !is_idle_domain (v->domain) ) -return 0 ; - -for ( i =0 ; i <ARRAY_SIZE (v->vpci.mem); i++ ) - { -char str[32 ]; - -snprintf (str,sizeof (str),"%pv:BAR%u" , v, i); - v->vpci.mem[i] =rangeset_new (v->domain, str, RANGESETF_no_print); -if ( !v->vpci.mem[i] ) -return -ENOMEM ; - } - -return 0 ; +INIT_LIST_HEAD (&v->vpci.task_queue); }#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT
xen/include/xen/rangeset.h +0 -7
@@ -40,13 +40,6 @@ struct rangeset *rangeset_new(void rangeset_destroy (struct rangeset *r); -/* Destroy a rangeset, and zero the pointer to it. */ -#define RANGESET_DESTROY (r) \ - ({ \ - rangeset_destroy(r); \ - (r) = NULL; \ - }) -/* * Set a limit on the number of ranges that may exist in set @r. * NOTE: This must be called while @r is empty.
xen/include/xen/vpci.h +14 -7
@@ -19,8 +19,7 @@*/ #define VPCI_MAX_VIRT_DEV (PCI_SLOT(~0) + 1) -void vpci_vcpu_destroy (struct vcpu *v); -int vpci_vcpu_init (struct vcpu *v); +void vpci_vcpu_init (struct vcpu *v);/* Assign vPCI to device by adding handlers. */ int __must_check vpci_assign_device (struct pci_dev *pdev);
@@ -155,14 +154,23 @@ struct vpci { };#ifdef __XEN__ -struct vpci_vcpu { +struct vpci_map_task {/* Per-vcpu structure to store state while {un}mapping of PCI BARs. */ -const struct pci_dev *pdev; -struct rangeset *mem[ARRAY_SIZE (((struct vpci_header *)NULL )->bars)]; +struct list_head next; +struct vpci_bar_map { +uint64_t addr; +uint64_t guest_addr; +struct rangeset *mem; + } bars[ARRAY_SIZE (((struct vpci_header *)NULL )->bars)];uint16_t cmd;bool rom_only :1 ; }; +struct vpci_vcpu { +const struct pci_dev *pdev; +struct list_head task_queue; +}; +void vpci_dump_msi (void );/* Arch-specific vPCI MSI helpers. */
@@ -207,8 +215,7 @@ bool vpci_ecam_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,#else /* !CONFIG_HAS_VPCI */ struct vpci_vcpu {}; -static inline void vpci_vcpu_destroy (struct vcpu *v) { } -static inline int vpci_vcpu_init (struct vcpu *v) {return 0 ; } +static inline void vpci_vcpu_init (struct vcpu *v) { }static inline int vpci_reinit_ext_capabilities (struct pci_dev *pdev) {
Signature · 1 line
2.53.0
Re: [PATCH v4 2/4] vpci: allow queueing of mapping operations
Quoted 28 lines · Stewart Hildebrand
Introduce vPCI BAR mapping task queue. Store information needed to map/unmap BARs in struct
vpci_map_task. Allow queueing of BAR map/unmap operations in a list, thus making it possible to perform multiple p2m operations associated with single PCI device.This is preparatory work for further changes that need to perform multiple unmap/map operations before returning to guest.
At the moment, only a single operation will be queued. However, when multiple operations are queued, there is a check in
modify_bars()to skip BARs already in the requested state that will no longer be accurate. Remove this check in preparation of upcoming changes.Signed-off-by Stewart Hildebrand <[email protected]>---
apply_map()andvpci_process_map_task()are very similar. Should we try to combine them into a single function?I concede that the dynamic allocation/deallocation of struct
vpci_map_taskis not ideal. However, to support SR-IOV, there will be a need to queue many mapping operations (one per VF), and statically pre-allocating that much would seem wasteful. Only the hardware and/or control domain would need to queue many operations, and only when configuring SR-IOV.
v3->v4:
* switch back to dynamically allocated queue elements
Quoted 8 lines
v2->v3:
* base on ("vpci: Use pervcpu ranges for BAR mapping") from [1]
* rework with fixed array of map/unmap slots[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t
v1->v2:
* new patch
It seems like this patch is modifying a lot of the same code as the previous one. Maybe it will be a good idea to squash them into a single one?
Signature · 1 line
Mykyta
Re: [PATCH v4 2/4] vpci: allow queueing of mapping operations
In reply to Stewart HildebrandQuoted 18 lines · Stewart Hildebrand
Introduce vPCI BAR mapping task queue. Store information needed to map/unmap BARs in struct
vpci_map_task. Allow queueing of BAR map/unmap operations in a list, thus making it possible to perform multiple p2m operations associated with single PCI device.This is preparatory work for further changes that need to perform multiple unmap/map operations before returning to guest.
At the moment, only a single operation will be queued. However, when multiple operations are queued, there is a check in
modify_bars()to skip BARs already in the requested state that will no longer be accurate. Remove this check in preparation of upcoming changes.Signed-off-by Stewart Hildebrand <[email protected]>---
apply_map()andvpci_process_map_task()are very similar. Should we try to combine them into a single function?
Quoted 6 lines
I concede that the dynamic allocation/deallocation of struct
vpci_map_taskis not ideal. However, to support SR-IOV, there will be a need to queue many mapping operations (one per VF), and statically pre-allocating that much would seem wasteful. Only the hardware and/or control domain would need to queue many operations, and only when configuring SR-IOV.
The address ranges are pretty regular though across the VFs, so I wonder whether getting away with less than one instance per VF (perhaps one per PF) wouldn't be possible by adjusting what data is being put into the "task" structure.
Quoted 13 lines
xen/include/xen/vpci.h +1
-2@@ -19,8 +19,7 @@*/ #define VPCI_MAX_VIRT_DEV (PCI_SLOT(~0) + 1) -void vpci_vcpu_destroy (struct vcpu *v); -int vpci_vcpu_init (struct vcpu *v); +void vpci_vcpu_init (struct vcpu *v);/* Assign vPCI to device by adding handlers. */ int __must_check vpci_assign_device (struct pci_dev *pdev);
xen/include/xen/vpci.h struct vpci { · line 158
Quoted 6 lines
xen/include/xen/vpci.h +1
-1@@ -155,14 +154,23 @@ struct vpci { };#ifdef __XEN__ -struct vpci_vcpu { +struct vpci_map_task {/* Per-vcpu structure to store state while {un}mapping of PCI BARs. */
This comment needs adjusting then, too, doesn't it?
Jan
PATCH v4 3/4 vpci: allow BAR map/unmap without affecting memory decoding bit
In reply to Stewart HildebrandIntroduce 'bool map' and allow invoking modify_bars() without changing the memory decoding bit. This will allow hardware domain to reposition BARs without affecting the memory decoding bit.
---
This also lays some groundwork to allow domUs to toggle the guest view without affecting hardware, a step toward addressing the FIXME in [1].
[1] https://lore.kernel.org/xen-devel/[email protected]/
v3->v4:
* rebase on dynamically allocated map queue
v2->v3:
* use bool
* switch to task->map in more places
v1->v2:
* new patch
--- xen/drivers/vpci/header.c | 43 +++++++++++++++++++++------------------ xen/include/xen/vpci.h | 1 + 2 files changed, 24 insertions(+), 20 deletions(-)
xen/drivers/vpci/header.c +23 -20
@@ -117,11 +117,12 @@ static int cf_check map_range(* BAR's enable bit has changed with the memory decoding bit already enabled. * If rom_only is not set then it's the memory decoding bit that changed. */ -static void modify_decoding (const struct pci_dev *pdev,uint16_t cmd, -bool rom_only) +static void modify_decoding (const struct pci_dev *pdev, +struct vpci_map_task *task) {struct vpci_header *header = &pdev->vpci->header; -bool map = cmd &PCI_COMMAND_MEMORY ; +bool rom_only = task->rom_only; +bool map = task->map;unsigned int i;for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ )
@@ -168,7 +169,7 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,if ( !rom_only ) { -pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd); +pci_conf_write16 (pdev->sbdf,PCI_COMMAND , task->cmd); header->bars_mapped = map; }else
@@ -188,7 +189,7 @@ static int vpci_process_map_task(const struct pci_dev *pdev,struct rangeset *mem = bar->mem;struct map_data data = { .d = pdev->domain, - .map = task->cmd &PCI_COMMAND_MEMORY , + .map = task->map, .bar = bar, };int rc;
@@ -203,9 +204,11 @@ static int vpci_process_map_task(const struct pci_dev *pdev,if ( rc ) { -spin_lock (&pdev->vpci->lock);/* Disable memory decoding unconditionally on failure. */ -modify_decoding (pdev, task->cmd & ~PCI_COMMAND_MEMORY , false); + task->cmd &= ~PCI_COMMAND_MEMORY ; + task->map = false; +spin_lock (&pdev->vpci->lock); +modify_decoding (pdev, task);spin_unlock (&pdev->vpci->lock);if ( !is_hardware_domain (pdev->domain) )
@@ -216,7 +219,7 @@ static int vpci_process_map_task(const struct pci_dev *pdev, }spin_lock (&pdev->vpci->lock); -modify_decoding (pdev, task->cmd, task->rom_only); +modify_decoding (pdev, task);spin_unlock (&pdev->vpci->lock);return 0 ;
@@ -328,13 +331,14 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev, } }if ( !rc ) -modify_decoding (pdev, task->cmd, false); +modify_decoding (pdev, task);return rc; }static struct vpci_map_task *alloc_map_task (const struct pci_dev *pdev, -uint16_t cmd,bool rom_only) +uint16_t cmd,bool rom_only, +bool map) {struct vpci_map_task *task;unsigned int i;
@@ -364,6 +368,7 @@ static struct vpci_map_task *alloc_map_task(const struct pci_dev *pdev, task->cmd = cmd; task->rom_only = rom_only; + task->map = map;return task; }
@@ -391,7 +396,8 @@ static void defer_map(const struct pci_dev *pdev, struct vpci_map_task *task)raise_softirq (SCHEDULE_SOFTIRQ ); } -static int modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only) +static int modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only, +bool map) {struct vpci_header *header = &pdev->vpci->header;struct pci_dev *tmp;
@@ -403,7 +409,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)ASSERT (rw_is_write_locked (&pdev->domain->pci_lock)); - task =alloc_map_task (pdev, cmd, rom_only); + task =alloc_map_task (pdev, cmd, rom_only, map);if ( !task )return -ENOMEM ;
@@ -602,7 +608,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)* be called iff the memory decoding bit is enabled, thus the operation * will always be to establish mappings and process all the BARs. */ -ASSERT ((cmd &PCI_COMMAND_MEMORY ) && !rom_only); +ASSERT (map && !rom_only); rc =apply_map (pdev->domain, pdev, task);destroy_map_task (task);return rc;
@@ -646,7 +652,7 @@ static void cf_check cmd_write(* memory decoding bit has not been changed, so leave everything as-is, * hoping the guest will realize and try again. */ -modify_bars (pdev, cmd, false); +modify_bars (pdev, cmd, false, cmd &PCI_COMMAND_MEMORY ); else pci_conf_write16(pdev->sbdf, reg, cmd); }
@@ -810,11 +816,8 @@ static void cf_check rom_write( header->rom_enabled = new_enabled;pci_conf_write32 (pdev->sbdf, reg, val); } -/* -* Pass PCI_COMMAND_MEMORY or 0 to signal a map/unmap request, note that -* this fabricated command is never going to be written to the register. -*/ -else if (modify_bars (pdev, new_enabled ?PCI_COMMAND_MEMORY :0 , true) ) +/* Note that the command value 0 will never be written to the register */ +else if (modify_bars (pdev,0 , true, new_enabled) )/* * No memory has been added or removed from the p2m (because the actual * p2m changes are deferred in defer_map) and the ROM enable bit has
@@ -1004,7 +1007,7 @@ int vpci_init_header(struct pci_dev *pdev) goto fail; } -return (cmd &PCI_COMMAND_MEMORY ) ?modify_bars (pdev, cmd, false) :0 ; +return (cmd &PCI_COMMAND_MEMORY ) ?modify_bars (pdev, cmd, false, true) :0 ; fail:pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);
xen/include/xen/vpci.h +1 -0
@@ -164,6 +164,7 @@ struct vpci_map_task { } bars[ARRAY_SIZE (((struct vpci_header *)NULL )->bars)];uint16_t cmd; bool rom_only : 1; + bool map : 1; };struct vpci_vcpu {
Signature · 1 line
2.53.0
Re: [PATCH v4 3/4] vpci: allow BAR map/unmap without affecting memory decoding bit
Quoted 10 lines · Stewart Hildebrand
Introduce 'bool map' and allow invoking
modify_bars()without changing the memory decoding bit. This will allow hardware domain to reposition BARs without affecting the memory decoding bit.Signed-off-by Stewart Hildebrand <[email protected]>---
This also lays some groundwork to allow domUs to toggle the guest view without affecting hardware, a step toward addressing the FIXME in [1].
The deferred toggling of the command register bits seems overly complex IMO. It would be much simpler if domUs simply don't get to play with the real decoding bits. For the hardware domain we could toggle the decoding bits ahead of the mapping operations, so that we don't have to defer the toggling after mappings have been sorted.
Long term we should aim to remove the cmd field from vpci_map_task struct.
I don't mind if you want to pick the patch in [1] in your series, as we could then drop the cmd field and possibly simplify some of the logic in this patch by no longer requiring the deferred write to the command register?
Quoted 19 lines
v3->v4:
* rebase on dynamically allocated map queue
v2->v3:
* use bool
* switch totask->mapin more places
v1->v2:
* new patch--- xen/drivers/vpci/header.c | 43 +++++++++++++++++++++------------------ xen/include/xen/vpci.h | 1 + 2 files changed, 24 insertions(+), 20 deletions(-)xen/drivers/vpci/header.c +0
-0
xen/drivers/vpci/header.c static int cf_check map_range( · line 121
Quoted 7 lines
xen/drivers/vpci/header.c +2
-2@@ -117,11 +117,12 @@ static int cf_check map_range(* BAR's enable bit has changed with the memory decoding bit already enabled. * If rom_only is not set then it's the memory decoding bit that changed. */ -static void modify_decoding (const struct pci_dev *pdev,uint16_t cmd, -bool rom_only) +static void modify_decoding (const struct pci_dev *pdev, +struct vpci_map_task *task)
task wants to be const here, unless further patches require modifying task inside of modify_decoding().
Thanks, Roger.
PATCH v4 4/4 vpci: allow 32-bit BAR writes with memory decoding enabled
In reply to Stewart HildebrandCurrently, Xen vPCI refuses BAR writes if the BAR is mapped in p2m. If firmware initializes a 32-bit BAR to a bad address, Linux may try to write a new address to the 32-bit BAR without disabling memory decoding. Since Xen refuses such writes, the BAR (and thus PCI device) will be non-functional.
Allow the hardware domain to issue 32-bit BAR writes with memory decoding enabled. This increases the compatibility of PVH dom0 with more hardware.
Note that Linux aims at disabling memory decoding before writing 64-bit BARs. Continue to refuse 64-bit BAR writes in Xen while those BARs are mapped for now to avoid mapping half-updated BARs in p2m.
Resolves: https://gitlab.com/xen-project/xen/-/issues/197
---v3->v4:
* rebase on dynamically allocated map queue
v2->v3:
* minor tweaks for fixed number of map/unmap slots
v1->v2:
* rework on top of queued BAR map/unmap operation machinery
RFC->v1:
* keep memory decoding enabled in hardware
* allow write while memory decoding is enabled for 32-bit BARs only
* rework BAR mapping machinery to support unmap-then-map operation
--- xen/drivers/vpci/header.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)
xen/drivers/vpci/header.c +23 -9
@@ -670,6 +670,7 @@ static void cf_check bar_write( {struct vpci_bar *bar = data;bool hi = false; +uint16_t cmd =0 ;ASSERT (is_hardware_domain (pdev->domain));
@@ -683,19 +684,29 @@ static void cf_check bar_write( val &=PCI_BASE_ADDRESS_MEM_MASK ;/* -* Xen only cares whether the BAR is mapped into the p2m, so allow BAR -* writes as long as the BAR is not mapped into the p2m. +* Allow 64-bit BAR writes only when the BAR is not mapped in p2m. Always +* allow 32-bit BAR writes. */ if ( bar->enabled ) { -/* If the value written is the current one avoid printing a warning. */ -if ( val != (uint32_t )(bar->addr >> (hi ?32 :0 )) ) -gprintk (XENLOG_WARNING , -"%pp: ignored BAR %zu write while mapped\n" , - &pdev->sbdf, bar - pdev->vpci->header.bars + hi); -return ; - } +if ( bar->type ==VPCI_BAR_MEM32 ) + { +if ( val == bar->addr ) +return ; + cmd =pci_conf_read16 (pdev->sbdf,PCI_COMMAND ); +modify_bars (pdev, cmd, false, false); + } +else + { +/* If the value written is the same avoid printing a warning. */ +if ( val != (uint32_t )(bar->addr >> (hi ?32 :0 )) ) +gprintk (XENLOG_WARNING , +"%pp: ignored BAR %zu write while mapped\n" , + &pdev->sbdf, bar - pdev->vpci->header.bars + hi); +return ; + } + }/* * Update the cached address, so that when memory decoding is enabled
@@ -715,6 +726,9 @@ static void cf_check bar_write( }pci_conf_write32 (pdev->sbdf, reg, val); + +if ( bar->enabled ) +modify_bars (pdev, cmd, false, true); } static void cf_check guest_mem_bar_write(const struct pci_dev *pdev,
Signature · 1 line
2.53.0
Quoted 50 lines · Stewart Hildebrand
xen/drivers/vpci/header.c +20
-9@@ -670,6 +670,7 @@ static void cf_check bar_write( {struct vpci_bar *bar = data;bool hi = false; +uint16_t cmd =0 ;ASSERT (is_hardware_domain (pdev->domain));@@ -683,19 +684,29 @@ static void cf_check bar_write( val &=PCI_BASE_ADDRESS_MEM_MASK ;/* -* Xen only cares whether the BAR is mapped into the p2m, so allow BAR -* writes as long as the BAR is not mapped into the p2m. +* Allow 64-bit BAR writes only when the BAR is not mapped in p2m. Always +* allow 32-bit BAR writes. */ if ( bar->enabled ) { -/* If the value written is the current one avoid printing a warning. */ -if ( val != (uint32_t )(bar->addr >> (hi ?32 :0 )) ) -gprintk (XENLOG_WARNING , -"%pp: ignored BAR %zu write while mapped\n" , - &pdev->sbdf, bar - pdev->vpci->header.bars + hi); -return ; - } +if ( bar->type ==VPCI_BAR_MEM32 ) + { +if ( val == bar->addr ) +return ; + cmd =pci_conf_read16 (pdev->sbdf,PCI_COMMAND ); +modify_bars (pdev, cmd, false, false); + } +else + { +/* If the value written is the same avoid printing a warning. */ +if ( val != (uint32_t )(bar->addr >> (hi ?32 :0 )) ) +gprintk (XENLOG_WARNING , +"%pp: ignored BAR %zu write while mapped\n" , + &pdev->sbdf, bar - pdev->vpci->header.bars + hi); +return ; + } + }/* * Update the cached address, so that when memory decoding is enabled
xen/drivers/vpci/header.c static void cf_check bar_write( · line 734
Quoted 9 lines
xen/drivers/vpci/header.c +3
-0@@ -715,6 +726,9 @@ static void cf_check bar_write( }pci_conf_write32 (pdev->sbdf, reg, val); + +if ( bar->enabled ) +modify_bars (pdev, cmd, false, true); } static void cf_check guest_mem_bar_write(const struct pci_dev *pdev,
While this all looks plausible, isn't something similar needed in rom_write() then as well?
Jan
Quoted 6 lines · Stewart Hildebrand
Currently, Xen vPCI refuses BAR writes if the BAR is mapped in p2m. If firmware initializes a 32-bit BAR to a bad address, Linux may try to write a new address to the 32-bit BAR without disabling memory decoding. Since Xen refuses such writes, the BAR (and thus PCI device) will be non-functional.
Quoted 7 lines
Allow the hardware domain to issue 32-bit BAR writes with memory decoding enabled. This increases the compatibility of PVH dom0 with more hardware.
Note that Linux aims at disabling memory decoding before writing 64-bit BARs. Continue to refuse 64-bit BAR writes in Xen while those BARs are mapped for now to avoid mapping half-updated BARs in p2m.
While Linux does the disabling for 64bit BARs, I wonder if at some we will need to handle 64bit BAR writes anyway for other OSes, specially with domU support.
Quoted 72 lines
Resolves: https://gitlab.com/xen-project/xen/-/issues/197
Signed-off-by Stewart Hildebrand <[email protected]>---
v3->v4:
* rebase on dynamically allocated map queue
v2->v3:
* minor tweaks for fixed number of map/unmap slots
v1->v2:
* rework on top of queued BAR map/unmap operation machinery
RFC->v1:
* keep memory decoding enabled in hardware
* allow write while memory decoding is enabled for 32-bit BARs only
* rework BAR mapping machinery to support unmap-then-map operation--- xen/drivers/vpci/header.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)xen/drivers/vpci/header.c +20
-9@@ -670,6 +670,7 @@ static void cf_check bar_write( {struct vpci_bar *bar = data;bool hi = false; +uint16_t cmd =0 ;ASSERT (is_hardware_domain (pdev->domain));@@ -683,19 +684,29 @@ static void cf_check bar_write( val &=PCI_BASE_ADDRESS_MEM_MASK ;/* -* Xen only cares whether the BAR is mapped into the p2m, so allow BAR -* writes as long as the BAR is not mapped into the p2m. +* Allow 64-bit BAR writes only when the BAR is not mapped in p2m. Always +* allow 32-bit BAR writes. */ if ( bar->enabled ) { -/* If the value written is the current one avoid printing a warning. */ -if ( val != (uint32_t )(bar->addr >> (hi ?32 :0 )) ) -gprintk (XENLOG_WARNING , -"%pp: ignored BAR %zu write while mapped\n" , - &pdev->sbdf, bar - pdev->vpci->header.bars + hi); -return ; - } +if ( bar->type ==VPCI_BAR_MEM32 ) + { +if ( val == bar->addr ) +return ; + cmd =pci_conf_read16 (pdev->sbdf,PCI_COMMAND ); +modify_bars (pdev, cmd, false, false); + } +else + { +/* If the value written is the same avoid printing a warning. */ +if ( val != (uint32_t )(bar->addr >> (hi ?32 :0 )) ) +gprintk (XENLOG_WARNING , +"%pp: ignored BAR %zu write while mapped\n" , + &pdev->sbdf, bar - pdev->vpci->header.bars + hi); +return ; + } + }/* * Update the cached address, so that when memory decoding is enabled
xen/drivers/vpci/header.c static void cf_check bar_write( · line 728
Quoted 3 lines
xen/drivers/vpci/header.c +0
-0@@ -715,6 +726,9 @@ static void cf_check bar_write( }pci_conf_write32 (pdev->sbdf, reg, val);
I don't think it matters a lot, but here we are changing the position of the BAR in the host memory map while the mappings are still active.
xen/drivers/vpci/header.c static void cf_check bar_write( · line 734
Quoted 6 lines
xen/drivers/vpci/header.c +3
-0+ +if ( bar->enabled ) +modify_bars (pdev, cmd, false, true); } static void cf_check guest_mem_bar_write(const struct pci_dev *pdev,
As it's in context here - we possibly want to do something similar with guest writes now that we can?
While we might not have a user-case for domUs ATM, I think it's vest if we also fix that use case at the same time that dom0 is fixed (unless there's something that prevents it).
Thanks, Roger.
Quoted 53 lines · Roger Pau Monné
Quoted 52 lines · Stewart Hildebrand
xen/drivers/vpci/header.c +20
-9@@ -670,6 +670,7 @@ static void cf_check bar_write( {struct vpci_bar *bar = data;bool hi = false; +uint16_t cmd =0 ;ASSERT (is_hardware_domain (pdev->domain));@@ -683,19 +684,29 @@ static void cf_check bar_write( val &=PCI_BASE_ADDRESS_MEM_MASK ;/* -* Xen only cares whether the BAR is mapped into the p2m, so allow BAR -* writes as long as the BAR is not mapped into the p2m. +* Allow 64-bit BAR writes only when the BAR is not mapped in p2m. Always +* allow 32-bit BAR writes. */ if ( bar->enabled ) { -/* If the value written is the current one avoid printing a warning. */ -if ( val != (uint32_t )(bar->addr >> (hi ?32 :0 )) ) -gprintk (XENLOG_WARNING , -"%pp: ignored BAR %zu write while mapped\n" , - &pdev->sbdf, bar - pdev->vpci->header.bars + hi); -return ; - } +if ( bar->type ==VPCI_BAR_MEM32 ) + { +if ( val == bar->addr ) +return ; + cmd =pci_conf_read16 (pdev->sbdf,PCI_COMMAND ); +modify_bars (pdev, cmd, false, false); + } +else + { +/* If the value written is the same avoid printing a warning. */ +if ( val != (uint32_t )(bar->addr >> (hi ?32 :0 )) ) +gprintk (XENLOG_WARNING , +"%pp: ignored BAR %zu write while mapped\n" , + &pdev->sbdf, bar - pdev->vpci->header.bars + hi); +return ; + } + }/* * Update the cached address, so that when memory decoding is enabled @@ -715,6 +726,9 @@ static void cf_check bar_write( }
Quoted 4 lines
xen/drivers/vpci/header.c static void cf_check bar_write( · line 728
Quoted 1 line
xen/drivers/vpci/header.c +0
-0pci_conf_write32 (pdev->sbdf, reg, val);I don't think it matters a lot, but here we are changing the position of the BAR in the host memory map while the mappings are still active.
It would matter if the original address space could be re-used for another purpose while those mappings are still there?
Jan