vpci: allow 32-bit BAR writes with memory decoding enabled
v3 cover vpci: allow 32-bit BAR writes with memory decoding enabled
Pipeline: https://gitlab.com/xen-project/people/stewarthildebrand/xen/-/pipelines/2404206419
v2->v3:
* add ("vpci: Use pervcpu ranges for BAR mapping")
* rework with fixed array of map/unmap slots
v2: https://lore.kernel.org/xen-devel/[email protected]/T/#t
v1->v2:
* new approach with queued p2m operations
RFC->v1:
* rework BAR mapping machinery to support unmap-then-map operation
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 | 5 + xen/drivers/vpci/header.c | 258 +++++++++++++++++++++---------------- xen/drivers/vpci/vpci.c | 43 ++++++- xen/include/xen/rangeset.h | 7 + xen/include/xen/vpci.h | 24 +++- 5 files changed, 214 insertions(+), 123 deletions(-)
base-commit: efacf86685912a9249e5cd7f2ef748f0d02b9e23
Signature · 1 line
2.53.0
PATCH v3 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")
---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
@@ -454,6 +454,8 @@ static int vcpu_teardown(struct vcpu *v)*/ static void vcpu_destroy (struct vcpu *v) { +vpci_vcpu_destroy (v); +free_vcpu_struct (v); }
@@ -511,6 +513,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
PATCH v3 2/4 vpci: allow queueing of mapping operations
In reply to Stewart HildebrandIntroduce vPCI BAR mapping task queue. Store information necessary in an array in struct vpci_vcpu to perform multiple p2m operations associated with single 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 slot is needed in the array. However, when multiple operations are queued and pending, there is a check in modify_bars() to skip BARs already in the requested state that is not accurate. Remove this check.
---apply_map() and vpci_process_map_task() are very similar. Should we try to combine them into a single function?
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/drivers/vpci/header.c | 154 +++++++++++++++++++++++--------------- xen/drivers/vpci/vpci.c | 27 ++++--- xen/include/xen/vpci.h | 15 +++- 3 files changed, 122 insertions(+), 74 deletions(-)
xen/drivers/vpci/header.c +94 -60
@@ -35,7 +35,7 @@struct map_data {struct domain *d; -const struct vpci_bar *bar; +const struct vpci_bar_map *bar;bool map; };
@@ -174,32 +174,23 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,ASSERT_UNREACHABLE (); } -bool vpci_process_pending (struct vcpu *v) +static int vpci_process_map_task (const struct pci_dev *pdev, +struct vpci_map_task *task) { -const struct pci_dev *pdev = v->vpci.pdev; -struct vpci_header *header =NULL ;unsigned int i; -if ( !pdev ) -return false; +ASSERT (rw_is_locked (&pdev->domain->pci_lock)); -read_lock (&v->domain->pci_lock); - -if ( !pdev->vpci || (v->domain != pdev->domain) ) - { - v->vpci.pdev =NULL ; -read_unlock (&v->domain->pci_lock); -return false; - } +if ( !task->pending ) +return 0 ; - header = &pdev->vpci->header; -for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) +for ( i =0 ; i <ARRAY_SIZE (task->bars); i++ ) { -struct vpci_bar *bar = &header->bars[i]; -struct rangeset *mem = v->vpci.mem[i]; +struct vpci_bar_map *bar = &task->bars[i]; +struct rangeset *mem = bar->mem;struct map_data data = { - .d = v->domain, - .map = v->vpci.cmd &PCI_COMMAND_MEMORY , + .d = pdev->domain, + .map = task->cmd &PCI_COMMAND_MEMORY , .bar = bar, };int rc;
@@ -210,58 +201,91 @@ bool vpci_process_pending(struct vcpu *v) rc =rangeset_consume_ranges (mem, map_range, &data);if ( rc == -ERESTART ) - { -read_unlock (&v->domain->pci_lock); -return true; - } +return rc;if ( rc ) {spin_lock (&pdev->vpci->lock);/* Disable memory decoding unconditionally on failure. */ -modify_decoding (pdev, v->vpci.cmd & ~PCI_COMMAND_MEMORY , - false); +modify_decoding (pdev, task->cmd & ~PCI_COMMAND_MEMORY , false);spin_unlock (&pdev->vpci->lock);/* Clean all the rangesets */ -for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) -if ( !rangeset_is_empty (v->vpci.mem[i]) ) -rangeset_purge (v->vpci.mem[i]); - - v->vpci.pdev =NULL ; +for ( i =0 ; i <ARRAY_SIZE (task->bars); i++ ) +if ( !rangeset_is_empty (mem) ) +rangeset_purge (mem); -read_unlock (&v->domain->pci_lock); +if ( !is_hardware_domain (pdev->domain) ) +domain_crash (pdev->domain); -if ( !is_hardware_domain (v->domain) ) -domain_crash (v->domain); + task->pending = false; -return false; +return rc; } } - v->vpci.pdev =NULL ;spin_lock (&pdev->vpci->lock); -modify_decoding (pdev, v->vpci.cmd, v->vpci.rom_only); +modify_decoding (pdev, task->cmd, task->rom_only);spin_unlock (&pdev->vpci->lock); + task->pending = false; + +return 0 ; +} + +bool vpci_process_pending (struct vcpu *v) +{ +const struct pci_dev *pdev = v->vpci.pdev; +unsigned int i; + +if ( !pdev ) +return false; + +read_lock (&v->domain->pci_lock); + +if ( !pdev->vpci || (v->domain != pdev->domain) ) + { +for ( i =0 ; i <ARRAY_SIZE (v->vpci.task); i++ ) + v->vpci.task[i].pending = false; + + v->vpci.pdev =NULL ; +read_unlock (&v->domain->pci_lock); +return false; + } + +for ( i =0 ; i <ARRAY_SIZE (v->vpci.task); i++ ) + { +struct vpci_map_task *task = &(v->vpci.task[i]); +int rc =vpci_process_map_task (pdev, task); + +if ( rc == -ERESTART ) + { +read_unlock (&v->domain->pci_lock); +return true; + } + +if ( rc ) +break ; + } + v->vpci.pdev =NULL ; +read_unlock (&v->domain->pci_lock);return false; }static int __init apply_map (struct domain *d,const struct pci_dev *pdev, -uint16_t cmd) +struct vpci_map_task *task) { -struct vpci_header *header = &pdev->vpci->header;int rc =0 ;unsigned int i;ASSERT (rw_is_write_locked (&d->pci_lock)); -for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) +for ( i =0 ; i <ARRAY_SIZE (task->bars); i++ ) { -struct vpci_bar *bar = &header->bars[i]; -struct rangeset *mem = current->vpci.mem[i]; +struct vpci_bar_map *bar = &task->bars[i]; +struct rangeset *mem = bar->mem;struct map_data data = { .d = d, .map = true, .bar = bar };if (rangeset_is_empty (mem) )
@@ -281,12 +305,14 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev, } }if ( !rc ) -modify_decoding (pdev, cmd, false); +modify_decoding (pdev, task->cmd, false); + + task->pending = false;return rc; } -static void defer_map (const struct pci_dev *pdev,uint16_t cmd,bool rom_only) +static void defer_map (const struct pci_dev *pdev) {struct vcpu *curr = current;
@@ -297,8 +323,7 @@ static void defer_map(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)* started for the same device if the domain is not well-behaved. */ curr->vpci.pdev = pdev; - curr->vpci.cmd = cmd; - curr->vpci.rom_only = rom_only; +/* * Raise a scheduler softirq in order to prevent the guest from resuming * execution with pending mapping operations, to trigger the invocation
@@ -307,16 +332,20 @@ static void defer_map(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)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, +unsigned int map_slot) {struct vpci_header *header = &pdev->vpci->header;struct pci_dev *tmp;const struct domain *d;const struct vpci_msix *msix = pdev->vpci->msix; +struct vpci_map_task *task = ¤t->vpci.task[map_slot];unsigned int i, j;int rc;ASSERT (rw_is_write_locked (&pdev->domain->pci_lock)); +ASSERT (map_slot <ARRAY_SIZE (current->vpci.task)); +ASSERT (!task->pending);/* * Create a rangeset per BAR that represents the current device memory
@@ -333,7 +362,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) {struct vpci_bar *bar = &header->bars[i]; -struct rangeset *mem = current->vpci.mem[i]; +struct rangeset *mem = task->bars[i].mem;unsigned long start =PFN_DOWN (bar->addr);unsigned long end =PFN_DOWN (bar->addr + bar->size -1 );unsigned long start_guest =PFN_DOWN (bar->guest_addr);
@@ -343,9 +372,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)if ( !MAPPABLE_BAR (bar) || (rom_only ? bar->type !=VPCI_BAR_ROM - : (bar->type ==VPCI_BAR_ROM && !header->rom_enabled)) || -/* Skip BARs already in the requested state. */ - bar->enabled == !!(cmd &PCI_COMMAND_MEMORY ) ) + : (bar->type ==VPCI_BAR_ROM && !header->rom_enabled)) )continue ;if ( !pci_check_bar (pdev,_mfn (start),_mfn (end)) )
@@ -382,7 +409,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)/* Check for overlap with the already setup BAR ranges. */ for ( j =0 ; j < i; j++ ) { -struct rangeset *prev_mem = current->vpci.mem[j]; +struct rangeset *prev_mem = task->bars[j].mem;if (rangeset_is_empty (prev_mem) )continue ;
@@ -405,6 +432,9 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) &pdev->sbdf, i, rc);return rc; } + + task->bars[i].addr = bar->addr; + task->bars[i].guest_addr = bar->guest_addr; }/* Remove any MSIX regions if present. */
@@ -414,9 +444,9 @@ 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 ); -for ( j =0 ; j <ARRAY_SIZE (current->vpci.mem); j++ ) +for ( j =0 ; j <ARRAY_SIZE (task->bars); j++ ) { -struct rangeset *mem = current->vpci.mem[j]; +struct rangeset *mem = task->bars[j].mem;if (rangeset_is_empty (mem) )continue ;
@@ -471,7 +501,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)for ( j =0 ; j <ARRAY_SIZE (header->bars); j++) {const struct vpci_bar *bar = &header->bars[j]; -struct rangeset *mem = current->vpci.mem[j]; +struct rangeset *mem = task->bars[j].mem; if ( !rangeset_overlaps_range(mem, start, end) ||/*
@@ -502,6 +532,10 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) d = dom_xen; } + task->cmd = cmd; + task->rom_only = rom_only; + task->pending = true; +if ( system_state < SYS_STATE_active ) {/*
@@ -513,10 +547,10 @@ 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, cmd); +return apply_map (pdev->domain, pdev, task); } -defer_map (pdev, cmd, rom_only); +defer_map (pdev);return 0 ; }
@@ -549,7 +583,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,0 ); else pci_conf_write16(pdev->sbdf, reg, cmd); }
@@ -717,7 +751,7 @@ static void cf_check rom_write(* 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) ) + else if ( modify_bars(pdev, new_enabled ? PCI_COMMAND_MEMORY : 0, true, 0) )/* * 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
@@ -907,7 +941,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,0 ) :0 ; fail:pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);
xen/drivers/vpci/vpci.c +17 -10
@@ -26,30 +26,37 @@void vpci_vcpu_destroy (struct vcpu *v) { -unsigned int i; +unsigned int i, j;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]); +for ( i =0 ; i <ARRAY_SIZE (v->vpci.task); i++ ) + { +for ( j =0 ; j <ARRAY_SIZE (v->vpci.task[i].bars); j++ ) +RANGESET_DESTROY (v->vpci.task[i].bars[j].mem); + } }int vpci_vcpu_init (struct vcpu *v) { -unsigned int i; +unsigned int i, j;if ( !has_vpci (v->domain) && !is_idle_domain (v->domain) )return 0 ; -for ( i =0 ; i <ARRAY_SIZE (v->vpci.mem); i++ ) +for ( i =0 ; i <ARRAY_SIZE (v->vpci.task); i++ ) { -char str[32 ]; +for ( j =0 ; j <ARRAY_SIZE (v->vpci.task[i].bars); j++ ) + { +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 ; +snprintf (str,sizeof (str),"%pv:BAR[%u][%u]" , v, i, j); + v->vpci.task[i].bars[j].mem =rangeset_new (v->domain, str, + RANGESETF_no_print); +if ( !v->vpci.task[i].bars[j].mem ) +return -ENOMEM ; + } }return 0 ;
xen/include/xen/vpci.h +11 -4
@@ -156,11 +156,18 @@ struct vpci {#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 ; +/* Per-vcpu structure to store state while {un}mapping of PCI BARs. */ +struct vpci_map_task { +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 ; +bool pending :1 ; + } task[1 ]; };void vpci_dump_msi (void );
Signature · 1 line
2.53.0
Re: [PATCH v3 2/4] vpci: allow queueing of mapping operations
Quoted 17 lines · Stewart Hildebrand
Introduce vPCI BAR mapping task queue. Store information necessary in an array in struct
vpci_vcputo perform multiple p2m operations associated with single 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 slot is needed in the array. However, when multiple operations are queued and pending, there is a check in
modify_bars()to skip BARs already in the requested state that is not accurate. Remove this check.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 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
Hi everyone,
Would it be possible to move back to a dynamically allocated number of tasks? This would help with mapping SR-IOV virtual functions a lot. @Stewart @Roger, what are your thoughts?
Alternatively, I can continue with an approach described in SR-IOV series, where VFs are handled separately. I figured out how to return to do_softirq after mapping each VF, so it should not block the CPU for too long.
Signature · 1 line
Mykyta
Re: [PATCH v3 2/4] vpci: allow queueing of mapping operations
Quoted 27 lines · Mykyta Poturai
Quoted 17 lines · Stewart Hildebrand
Introduce vPCI BAR mapping task queue. Store information necessary in an array in struct
vpci_vcputo perform multiple p2m operations associated with single 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 slot is needed in the array. However, when multiple operations are queued and pending, there is a check in
modify_bars()to skip BARs already in the requested state that is not accurate. Remove this check.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 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
Quoted 6 lines
Hi everyone,
Would it be possible to move back to a dynamically allocated number of tasks? This would help with mapping SR-IOV virtual functions a lot. @Stewart @Roger, what are your thoughts?
Yes, assuming each VF will need another queued map operation, and there could be a lot of VFs, statically pre-allocating that much seems wasteful. Only the control and/or hardware domains will queue a lot of operations, and only when enabling SR-IOV. DomUs will only queue 1 operation (possibly 2 if we will allow domU BAR writes with memory decoding enabled).
So I'll plan to switch back to dynamically allocated map tasks for v4.
Quoted 5 lines
Alternatively, I can continue with an approach described in SR-IOV series, where VFs are handled separately. I figured out how to return to
do_softirqafter mapping each VF, so it should not block the CPU for too long.
PATCH v3 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]/
v2->v3:
* use bool
* switch to task->map in more places
v1->v2:
* new patch
--- xen/drivers/vpci/header.c | 38 ++++++++++++++++++++------------------ xen/include/xen/vpci.h | 1 + 2 files changed, 21 insertions(+), 18 deletions(-)
xen/drivers/vpci/header.c +20 -18
@@ -116,11 +116,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++ )
@@ -167,7 +168,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
@@ -190,7 +191,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;
@@ -205,9 +206,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);/* Clean all the rangesets */
@@ -225,7 +228,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); task->pending = false;
@@ -305,7 +308,7 @@ 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); task->pending = false;
@@ -333,7 +336,7 @@ static void defer_map(const struct pci_dev *pdev) }static int modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only, -unsigned int map_slot) +bool map,unsigned int map_slot) {struct vpci_header *header = &pdev->vpci->header;struct pci_dev *tmp;
@@ -534,6 +537,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only, task->cmd = cmd; task->rom_only = rom_only; + task->map = map; task->pending = true;if ( system_state < SYS_STATE_active )
@@ -546,7 +550,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);return apply_map (pdev->domain, pdev, task); }
@@ -583,7 +587,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,0 ); +modify_bars (pdev, cmd, false, cmd &PCI_COMMAND_MEMORY ,0 ); else pci_conf_write16(pdev->sbdf, reg, cmd); }
@@ -747,11 +751,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,0 ) ) +/* Note that the command value 0 will never be written to the register */ +else if (modify_bars (pdev,0 , true, new_enabled,0 ) )/* * 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
@@ -941,7 +942,8 @@ int vpci_init_header(struct pci_dev *pdev) goto fail; } -return (cmd &PCI_COMMAND_MEMORY ) ?modify_bars (pdev, cmd, false,0 ) :0 ; +return (cmd &PCI_COMMAND_MEMORY ) ?modify_bars (pdev, cmd, false, true,0 ) + :0 ; fail:pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);
xen/include/xen/vpci.h +1 -0
@@ -166,6 +166,7 @@ struct vpci_vcpu { } bars[ARRAY_SIZE (((struct vpci_header *)NULL )->bars)];uint16_t cmd; bool rom_only : 1; + bool map : 1; bool pending : 1; } task[1 ]; };
Signature · 1 line
2.53.0
PATCH v3 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.
Increase number of unmap/map slots to 2.
Resolves: https://gitlab.com/xen-project/xen/-/issues/197
---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 | 33 ++++++++++++++++++++++++--------- xen/include/xen/vpci.h | 2 +- 2 files changed, 25 insertions(+), 10 deletions(-)
xen/drivers/vpci/header.c +24 -9
@@ -605,6 +605,8 @@ static void cf_check bar_write( {struct vpci_bar *bar = data;bool hi = false; +uint16_t cmd =0 ; +unsigned int map_slot =0 ;ASSERT (is_hardware_domain (pdev->domain));
@@ -618,19 +620,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, map_slot++); + } +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
@@ -650,6 +662,9 @@ static void cf_check bar_write( }pci_conf_write32 (pdev->sbdf, reg, val); + +if ( bar->enabled ) +modify_bars (pdev, cmd, false, true, map_slot++); } static void cf_check guest_mem_bar_write(const struct pci_dev *pdev,
xen/include/xen/vpci.h +1 -1
@@ -168,7 +168,7 @@ struct vpci_vcpu { bool rom_only : 1; bool map : 1; bool pending : 1; - } task[1 ]; + } task[2 ]; };void vpci_dump_msi (void );
Signature · 1 line
2.53.0