vpci/bars: add handlers to map the BARs
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
(re-sending with xen-devel re-added; not sure how it got lost)
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
--- tools/tests/vpci/emul.h | 1 + xen/arch/x86/hvm/ioreq.c | 4 +
Again the Cc to Paul is missing (no matter that it's just a tiny change).
Review
Quoted 9 lines
(quoted diff) +9
-0+static int map_range(unsigned long s, unsigned long e, void *data, + unsigned long *c) +{ + const struct map_data *map = data; + int rc; + + for ( ; ; ) + { + unsigned long size = e - s + 1;
Considering the lack of a condition in the for() and the inclusiveness of the range (which means you can't express an empty range) I don't understand how ...
Quoted 14 lines
(quoted diff) +14
-0+ /* + * ARM TODOs: + * - On ARM whether the memory is prefetchable or not should be passed + * to map_mmio_regions in order to decide which memory attributes + * should be used. + * + * - {un}map_mmio_regions doesn't support preemption, hence the bodge + * below in order to limit the amount of mappings to 64 pages for + * each function call. + */ + +#ifdef CONFIG_ARM + size = min(64ul, size); +#endif
Review
Quoted 10 lines
(quoted diff) +10
-0+ + rc = (map->map ? map_mmio_regions : unmap_mmio_regions) + (map->d, _gfn(s), size, _mfn(s)); + if ( rc == 0 ) + { + *c += size; +#ifdef CONFIG_ARM + rc = -ERESTART; +#endif + break;
... this works in the ARM case. If the whole thing doesn't work (and doesn't get built) for ARM right now, make this obvious by one or more #error directives.
Review
Quoted 11 lines
(quoted diff) +11
-0+static void modify_decoding(const struct pci_dev *pdev, bool map, bool rom) +{ + struct vpci_header *header = &pdev->vpci->header; + uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn); + unsigned int i; + + for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) + { + if ( rom && header->bars[i].type == VPCI_BAR_ROM ) + { + unsigned int rom_pos = (i == 6) ? PCI_ROM_ADDRESS
I probably should have mentioned this earlier, but I'm really unhappy about such literal "magic" numbers. Please use a suitable expression or #define.
Quoted 13 lines
(quoted diff) +13
-0+bool vpci_process_pending(struct vcpu *v) +{ + while ( v->vpci.mem ) + { + struct map_data data = { + .d = v->domain, + .map = v->vpci.map, + }; + + switch ( rangeset_consume_ranges(v->vpci.mem, map_range, &data) ) + { + case -ERESTART: + return true;
Review
Quoted 10 lines
(quoted diff) +10
-0+ + default: + if ( v->vpci.map ) + { + spin_lock(&v->vpci.pdev->vpci->lock); + modify_decoding(v->vpci.pdev, v->vpci.map, v->vpci.rom); + spin_unlock(&v->vpci.pdev->vpci->lock); + } + /* fallthrough. */ + case -ENOMEM:
You carefully handle this error here.
Quoted 5 lines
(quoted diff) +5
-0+static void maybe_defer_map(struct domain *d, const struct pci_dev *pdev, + struct rangeset *mem, bool map, bool rom) +{ + struct vcpu *curr = current; +
Review
Quoted 10 lines
(quoted diff) +10
-0+ if ( is_idle_vcpu(curr) ) + { + struct map_data data = { .d = d, .map = true }; + + /* + * Only used for domain construction in order to map the BARs + * of devices with memory decoding enabled. + */ + ASSERT(map && !rom); + rangeset_consume_ranges(mem, map_range, &data);
What if this produces -ENOMEM? And despite having looked at several revisions of this, I can't make the connection to why this is in an is_idle_vcpu() conditional (neither the direct caller nor the next level up make this obvious to me). There's clearly a need for extending the comment.
Review
Quoted 13 lines
(quoted diff) +13
-0+static void modify_bars(const struct pci_dev *pdev, bool map, bool rom) +{ + struct vpci_header *header = &pdev->vpci->header; + struct rangeset *mem = rangeset_new(NULL, NULL, 0); + const struct pci_dev *tmp; + unsigned int i; + int rc; + + if ( !map ) + modify_decoding(pdev, false, rom); + + if ( !mem ) + return;
Similarly here - why is it okay (or what effect will it have) to return here when we're out of memory, but the caller won't know?
Quoted 12 lines
(quoted diff) +12
-0+ /* + * Create a rangeset that represents the current device BARs 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. + * + * NB: the rangeset uses inclusive frame numbers. + */ + + /* + * First fill the rangeset with all the BARs of this device or with the ROM + * BAR only, depending on whether the guest is toggling the memory decode
Review
Quoted 10 lines
(quoted diff) +10
-0+ * 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]; + + if ( !MAPPABLE_BAR(bar) || + (rom ? bar->type != VPCI_BAR_ROM + : (bar->type == VPCI_BAR_ROM && !header->rom_enabled)) ) + continue;
Why does rom_enabled matter for the !rom case rather than for the rom one? I.e.
if ( !MAPPABLE_BAR(bar) ||
(rom ? bar->type != VPCI_BAR_ROM || !header->rom_enabled
: bar->type == VPCI_BAR_ROM) )
continue;?
Review
Quoted 7 lines
(quoted diff) +7
-0+ rc = rangeset_add_range(mem, PFN_DOWN(bar->addr), + PFN_UP(bar->addr + bar->size - 1)); + if ( rc ) + { + printk(XENLOG_G_WARNING + "Failed to add [%" PRI_gfn ", %" PRI_gfn "): %d\n", + PFN_DOWN(bar->addr), PFN_UP(bar->addr + bar->size - 1),
Either you use [a,b) and don't subtract 1, or you use [a,b] with the subtraction. Same below.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 8 lines · Jan Beulich
(re-sending with xen-devel re-added; not sure how it got lost)
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
--- tools/tests/vpci/emul.h | 1 + xen/arch/x86/hvm/ioreq.c | 4 +Again the Cc to Paul is missing (no matter that it's just a tiny change).
Sorry, I will run get_maintainer.pl against the whole patchset before sending a new version.
Quoted 34 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+static int map_range(unsigned long s, unsigned long e, void *data, + unsigned long *c) +{ + const struct map_data *map = data; + int rc; + + for ( ; ; ) + { + unsigned long size = e - s + 1;Considering the lack of a condition in the
for()and the inclusiveness of the range (which means you can't express an empty range) I don't understand how ...Quoted 20 lines
(quoted diff) +20
-0+ /* + * ARM TODOs: + * - On ARM whether the memory is prefetchable or not should be passed + * to map_mmio_regions in order to decide which memory attributes + * should be used. + * + * - {un}map_mmio_regions doesn't support preemption, hence the bodge + * below in order to limit the amount of mappings to 64 pages for + * each function call. + */ + +#ifdef CONFIG_ARM + size = min(64ul, size); +#endif + + rc = (map->map ? map_mmio_regions : unmap_mmio_regions) + (map->d, _gfn(s), size, _mfn(s)); + if ( rc == 0 ) + { + *c += size;
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+#ifdef CONFIG_ARM + rc = -ERESTART; +#endif + break;... this works in the ARM case. If the whole thing doesn't work (and doesn't get built) for ARM right now, make this obvious by one or more #error directives.
ARM will never iterate, instead it will rely on always returning -ERESTART and letting the caller of rangeset_consume_ranges deal with it. What's wrong here is that the call to rangeset_consume_ranges should be:
while ( rangeset_consume_ranges(...) == -ERESTART );
But that makes the code even more convoluted than what it already is. I've basically added the ARM part because Julien requested it, but I think the right way to fix this is to unify the behaviour of {un}map_mmio_regions between x86 and ARM.
I will drop the ARM chunks.
Quoted 33 lines
Review
Quoted 11 lines
(quoted diff) +11
-0+static void modify_decoding(const struct pci_dev *pdev, bool map, bool rom) +{ + struct vpci_header *header = &pdev->vpci->header; + uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn); + unsigned int i; + + for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) + { + if ( rom && header->bars[i].type == VPCI_BAR_ROM ) + { + unsigned int rom_pos = (i == 6) ? PCI_ROM_ADDRESSI probably should have mentioned this earlier, but I'm really unhappy about such literal "magic" numbers. Please use a suitable expression or #define.
Quoted 17 lines
(quoted diff) +17
-0+bool vpci_process_pending(struct vcpu *v) +{ + while ( v->vpci.mem ) + { + struct map_data data = { + .d = v->domain, + .map = v->vpci.map, + }; + + switch ( rangeset_consume_ranges(v->vpci.mem, map_range, &data) ) + { + case -ERESTART: + return true; + + default: + if ( v->vpci.map ) + {
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ spin_lock(&v->vpci.pdev->vpci->lock); + modify_decoding(v->vpci.pdev, v->vpci.map, v->vpci.rom); + spin_unlock(&v->vpci.pdev->vpci->lock); + } + /* fallthrough. */ + case -ENOMEM:You carefully handle this error here.
On second thought, I'm not sure handling ENOMEM separately makes sense. Unless you object I plan to remove this special casing.
Quoted 13 lines
Quoted 13 lines
(quoted diff) +13
-0+static void maybe_defer_map(struct domain *d, const struct pci_dev *pdev, + struct rangeset *mem, bool map, bool rom) +{ + struct vcpu *curr = current; + + if ( is_idle_vcpu(curr) ) + { + struct map_data data = { .d = d, .map = true }; + + /* + * Only used for domain construction in order to map the BARs + * of devices with memory decoding enabled. + */
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ASSERT(map && !rom); + rangeset_consume_ranges(mem, map_range, &data);What if this produces
-ENOMEM? And despite having looked at several revisions of this, I can't make the connection to why this is in anis_idle_vcpu()conditional (neither the direct caller nor the next level up make this obvious to me). There's clearly a need for extending the comment.
I thought the comment above that mentions domain construction would be enough. I can try to expand this, maybe like:
"This function will only be called from the idle vCPU while building the domain, in which case it's not possible to defer the operation (like done in the else branch). Call rangeset_consume_ranges in order to establish the mappings right away."
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8
-0+static void modify_bars(const struct pci_dev *pdev, bool map, bool rom) +{ + struct vpci_header *header = &pdev->vpci->header; + struct rangeset *mem = rangeset_new(NULL, NULL, 0); + const struct pci_dev *tmp; + unsigned int i; + int rc; +
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ if ( !map ) + modify_decoding(pdev, false, rom); + + if ( !mem ) + return;Similarly here - why is it okay (or what effect will it have) to return here when we're out of memory, but the caller won't know?
The behaviour here depends on the change to the memory decoding bit:
- Clearing: memory decoding on device will be disabled, BARs won't be unmapped.
- Setting: no change to device memory decoding bit, BARs won't be mapped.
Do you think this is suitable? IMO it's fine to disable the memory decoding bit on the device and leave the memory regions mapped.
Quoted 26 lines
Quoted 12 lines
(quoted diff) +12
-0+ /* + * Create a rangeset that represents the current device BARs 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. + * + * NB: the rangeset uses inclusive frame numbers. + */ + + /* + * First fill the rangeset with all the BARs of this device or with the ROM + * BAR only, depending on whether the guest is toggling the memory decodeReview
Quoted 10 lines
(quoted diff) +10
-0+ * 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]; + + if ( !MAPPABLE_BAR(bar) || + (rom ? bar->type != VPCI_BAR_ROM + : (bar->type == VPCI_BAR_ROM && !header->rom_enabled)) ) + continue;Why does
rom_enabledmatter for the !rom case rather than for the rom one? I.e.
Quoted 6 lines
if ( !MAPPABLE_BAR(bar) || (rom ? bar->type != VPCI_BAR_ROM || !header->rom_enabled : bar->type == VPCI_BAR_ROM) ) continue;?
No, for the ROM case we only want to map/unmap the ROM, so that's the only thing added to the rangeset. For the non-ROM case Xen will also map/unmap the ROM if the enable bit is set.
Your proposed code would always map/unmap the ROM into the p2m when the memory decoding bit is changed even if it's not enabled.
Quoted 10 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ rc = rangeset_add_range(mem, PFN_DOWN(bar->addr), + PFN_UP(bar->addr + bar->size - 1)); + if ( rc ) + { + printk(XENLOG_G_WARNING + "Failed to add [%" PRI_gfn ", %" PRI_gfn "): %d\n", + PFN_DOWN(bar->addr), PFN_UP(bar->addr + bar->size - 1),Either you use [a,b) and don't subtract 1, or you use [a,b] with the subtraction. Same below.
[a, b) seems better in order to avoid the -1.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 26.02.18 at 19:00, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
(quoted diff) +17
-0+bool vpci_process_pending(struct vcpu *v) +{ + while ( v->vpci.mem ) + { + struct map_data data = { + .d = v->domain, + .map = v->vpci.map, + }; + + switch ( rangeset_consume_ranges(v->vpci.mem, map_range, &data) ) + { + case -ERESTART: + return true; + + default: + if ( v->vpci.map ) + {Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ spin_lock(&v->vpci.pdev->vpci->lock); + modify_decoding(v->vpci.pdev, v->vpci.map, v->vpci.rom); + spin_unlock(&v->vpci.pdev->vpci->lock); + } + /* fallthrough. */ + case -ENOMEM:You carefully handle this error here.
Quoted 2 lines
On second thought, I'm not sure handling ENOMEM separately makes sense. Unless you object I plan to remove this special casing.
Indeed I was never really happy with the special casing, but I did recall this being done intentionally, so I also didn't complain.
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
(quoted diff) +13
-0+static void maybe_defer_map(struct domain *d, const struct pci_dev *pdev, + struct rangeset *mem, bool map, bool rom) +{ + struct vcpu *curr = current; + + if ( is_idle_vcpu(curr) ) + { + struct map_data data = { .d = d, .map = true }; + + /* + * Only used for domain construction in order to map the BARs + * of devices with memory decoding enabled. + */Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ASSERT(map && !rom); + rangeset_consume_ranges(mem, map_range, &data);What if this produces
-ENOMEM? And despite having looked at several revisions of this, I can't make the connection to why this is in anis_idle_vcpu()conditional (neither the direct caller nor the next level up make this obvious to me). There's clearly a need for extending the comment.
Quoted 7 lines
I thought the comment above that mentions domain construction would be enough. I can try to expand this, maybe like:
"This function will only be called from the idle vCPU while building the domain, in which case it's not possible to defer the operation (like done in the else branch). Call
rangeset_consume_rangesin order to establish the mappings right away."
And what again is the connection between is_idle_domain() and domain construction? I think the comment belongs ahead of the if(), and it needs to make that connection.
Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8
-0+static void modify_bars(const struct pci_dev *pdev, bool map, bool rom) +{ + struct vpci_header *header = &pdev->vpci->header; + struct rangeset *mem = rangeset_new(NULL, NULL, 0); + const struct pci_dev *tmp; + unsigned int i; + int rc; +Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ if ( !map ) + modify_decoding(pdev, false, rom); + + if ( !mem ) + return;Similarly here - why is it okay (or what effect will it have) to return here when we're out of memory, but the caller won't know?
The behaviour here depends on the change to the memory decoding bit:
Quoted 7 lines
- Clearing: memory decoding on device will be disabled, BARs won't be unmapped.
- Setting: no change to device memory decoding bit, BARs won't be mapped.Do you think this is suitable? IMO it's fine to disable the memory decoding bit on the device and leave the memory regions mapped.
As long as subsequent changes to the decoding bit can't leave stale mappings. Plus there needs to be a comment to explain this.
Quoted 31 lines
Quoted 31 lines
Quoted 12 lines
(quoted diff) +12
-0+ /* + * Create a rangeset that represents the current device BARs 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. + * + * NB: the rangeset uses inclusive frame numbers. + */ + + /* + * First fill the rangeset with all the BARs of this device or with the ROM + * BAR only, depending on whether the guest is toggling the memory decodeReview
Quoted 10 lines
(quoted diff) +10
-0+ * 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]; + + if ( !MAPPABLE_BAR(bar) || + (rom ? bar->type != VPCI_BAR_ROM + : (bar->type == VPCI_BAR_ROM && !header->rom_enabled)) ) + continue;Why does
rom_enabledmatter for the !rom case rather than for the rom one? I.e.if ( !MAPPABLE_BAR(bar) || (rom ? bar->type != VPCI_BAR_ROM || !header->rom_enabled : bar->type == VPCI_BAR_ROM) ) continue;
Quoted 8 lines
Quoted 1 line
?
No, for the ROM case we only want to map/unmap the ROM, so that's the only thing added to the rangeset. For the non-ROM case Xen will also map/unmap the ROM if the enable bit is set.
Your proposed code would always map/unmap the ROM into the p2m when the memory decoding bit is changed even if it's not enabled.
I don't understand. Taking apart the conditional I've suggested, and converting to human language:
- if the BAR is no mappable, continue
- if we want to deal with ROM (rom=true), if the BAR isn't ROM or isn't enabled, continue
- if we want to deal with non-ROM (rom=false), if the BAR is ROM, continue
To me this is in line with the 2nd paragraph of your reply. It's not in line with the first, which makes me wonder whether "rom" is misnamed and wants to be "rom_only". Still, the question would remain of why rom_enabled doesn't matter when the variable is true.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 29 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 26.02.18 at 19:00, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
(quoted diff) +13
-0+bool vpci_process_pending(struct vcpu *v) +{ + while ( v->vpci.mem ) + { + struct map_data data = { + .d = v->domain, + .map = v->vpci.map, + }; + + switch ( rangeset_consume_ranges(v->vpci.mem, map_range, &data) ) + { + case -ERESTART: + return true;Review
Quoted 10 lines
(quoted diff) +10
-0+ + default: + if ( v->vpci.map ) + { + spin_lock(&v->vpci.pdev->vpci->lock); + modify_decoding(v->vpci.pdev, v->vpci.map, v->vpci.rom); + spin_unlock(&v->vpci.pdev->vpci->lock); + } + /* fallthrough. */ + case -ENOMEM:You carefully handle this error here.
Quoted 5 lines
Quoted 2 lines
On second thought, I'm not sure handling ENOMEM separately makes sense. Unless you object I plan to remove this special casing.
Indeed I was never really happy with the special casing, but I did recall this being done intentionally, so I also didn't complain.
IIRC we had some discussion about the different error codes and somehow decided that ENOMEM might be worse than others, in the sense that less memory would be mapped and the chances of the device actually working would be slim. IMO we can always special case errors later, let's start with something simpler.
Quoted 25 lines
Quoted 25 lines
Quoted 13 lines
Quoted 13 lines
(quoted diff) +13
-0+static void maybe_defer_map(struct domain *d, const struct pci_dev *pdev, + struct rangeset *mem, bool map, bool rom) +{ + struct vcpu *curr = current; + + if ( is_idle_vcpu(curr) ) + { + struct map_data data = { .d = d, .map = true }; + + /* + * Only used for domain construction in order to map the BARs + * of devices with memory decoding enabled. + */Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ASSERT(map && !rom); + rangeset_consume_ranges(mem, map_range, &data);What if this produces
-ENOMEM? And despite having looked at several revisions of this, I can't make the connection to why this is in anis_idle_vcpu()conditional (neither the direct caller nor the next level up make this obvious to me). There's clearly a need for extending the comment.I thought the comment above that mentions domain construction would be enough. I can try to expand this, maybe like:
Quoted 8 lines
Quoted 4 lines
"This function will only be called from the idle vCPU while building the domain, in which case it's not possible to defer the operation (like done in the else branch). Call
rangeset_consume_rangesin order to establish the mappings right away."And what again is the connection between
is_idle_domain()and domain construction? I think the comment belongs ahead of theif(), and it needs to make that connection.
Oh, domain constructions runs on the idle vCPU, that's the relation.
"This function will only be called from the idle vCPU while building the domain (because Dom0 building runs on the idle vCPU), in which case it's not possible to defer the operation (like done in the else branch). Call rangeset_consume_ranges in order to establish the mappings right away."
Does that seem clearer if placed ahead of the if?
Quoted 24 lines
Quoted 24 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8
-0+static void modify_bars(const struct pci_dev *pdev, bool map, bool rom) +{ + struct vpci_header *header = &pdev->vpci->header; + struct rangeset *mem = rangeset_new(NULL, NULL, 0); + const struct pci_dev *tmp; + unsigned int i; + int rc; +Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ if ( !map ) + modify_decoding(pdev, false, rom); + + if ( !mem ) + return;Similarly here - why is it okay (or what effect will it have) to return here when we're out of memory, but the caller won't know?
The behaviour here depends on the change to the memory decoding bit:
- Clearing: memory decoding on device will be disabled, BARs won't be unmapped.
- Setting: no change to device memory decoding bit, BARs won't be mapped.
Quoted 5 lines
Quoted 2 lines
Do you think this is suitable? IMO it's fine to disable the memory decoding bit on the device and leave the memory regions mapped.
As long as subsequent changes to the decoding bit can't leave stale mappings. Plus there needs to be a comment to explain this.
With the current approach in the unmap case there will be stale mappings left behind.
I guess it's better then to not modify the memory decoding bit at all until the operation finishes. That also rises the question of whether the memory decoding bit should be modified if p2m mapping/unmapping reports an error.
Should we also attempt to rollback failed map/unmap operations? What happens if the rollback also fails?
What about the following:
+---------+ SUCCESS +---------------------------------+
|map/unmap+------------>Change decoding or ROM enable bit|
+----+----+ +---------------------------------+
|
|FAILURE
|
+--------v-------+ SUCCESS +---------------------------------------+
|revert operation+--------->No change to decoding or ROM enable bit|
+--------+-------+ +---------------------------------------+
|
|FAILURE
|
+-----v-----+
|Kill domain|
+-----------+Quoted 37 lines
Quoted 37 lines
Quoted 26 lines
Quoted 12 lines
(quoted diff) +12
-0+ /* + * Create a rangeset that represents the current device BARs 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. + * + * NB: the rangeset uses inclusive frame numbers. + */ + + /* + * First fill the rangeset with all the BARs of this device or with the ROM + * BAR only, depending on whether the guest is toggling the memory decodeReview
Quoted 10 lines
(quoted diff) +10
-0+ * 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]; + + if ( !MAPPABLE_BAR(bar) || + (rom ? bar->type != VPCI_BAR_ROM + : (bar->type == VPCI_BAR_ROM && !header->rom_enabled)) ) + continue;Why does
rom_enabledmatter for the !rom case rather than for the rom one? I.e.Quoted 6 lines
if ( !MAPPABLE_BAR(bar) || (rom ? bar->type != VPCI_BAR_ROM || !header->rom_enabled : bar->type == VPCI_BAR_ROM) ) continue;?
No, for the ROM case we only want to map/unmap the ROM, so that's the only thing added to the rangeset. For the non-ROM case Xen will also map/unmap the ROM if the enable bit is set.
Quoted 8 lines
Quoted 2 lines
Your proposed code would always map/unmap the ROM into the p2m when the memory decoding bit is changed even if it's not enabled.
I don't understand. Taking apart the conditional I've suggested, and converting to human language:
- if the BAR is no mappable, continue
- if we want to deal with ROM (rom=true), if the BAR isn't ROM or isn't enabled, continue
With the current flow rom_enabled is set after the ROM BAR mappings are established, which means that when modify_bars is called with rom=true rom_enabled is not yet set, and using the logic above the mappings won't be created.
Quoted 2 lines
- if we want to deal with non-ROM (rom=false), if the BAR is ROM, continue
Xen also has to deal with the ROM if it's enabled when the memory decoding bit is toggled, hence in rom=false case the ROM also needs to be mapped/unampped if it's enabled.
This dependency between the memory decoding bit and the ROM enable bit is quite convoluted TBH.
Quoted 5 lines
To me this is in line with the 2nd paragraph of your reply. It's not in line with the first, which makes me wonder whether "rom" is misnamed and wants to be "
rom_only". Still, the question would remain of whyrom_enableddoesn't matter when the variable is true.
Yes, rom means rom_only (ie: ROM enable bit has been toggled and memory decoding bit is enabled). I assume you would prefer to change the name to rom_only.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 27.02.18 at 10:21, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 26.02.18 at 19:00, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
(quoted diff) +13
-0+static void maybe_defer_map(struct domain *d, const struct pci_dev *pdev, + struct rangeset *mem, bool map, bool rom) +{ + struct vcpu *curr = current; + + if ( is_idle_vcpu(curr) ) + { + struct map_data data = { .d = d, .map = true }; + + /* + * Only used for domain construction in order to map the BARs + * of devices with memory decoding enabled. + */Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ASSERT(map && !rom); + rangeset_consume_ranges(mem, map_range, &data);What if this produces
-ENOMEM? And despite having looked at several revisions of this, I can't make the connection to why this is in anis_idle_vcpu()conditional (neither the direct caller nor the next level up make this obvious to me). There's clearly a need for extending the comment.I thought the comment above that mentions domain construction would be enough. I can try to expand this, maybe like:
Quoted 8 lines
Quoted 4 lines
"This function will only be called from the idle vCPU while building the domain, in which case it's not possible to defer the operation (like done in the else branch). Call
rangeset_consume_rangesin order to establish the mappings right away."And what again is the connection between
is_idle_domain()and domain construction? I think the comment belongs ahead of theif(), and it needs to make that connection.Oh, domain constructions runs on the idle vCPU, that's the relation.
Quoted 7 lines
"This function will only be called from the idle vCPU while building the domain (because Dom0 building runs on the idle vCPU), in which case it's not possible to defer the operation (like done in the else branch). Call
rangeset_consume_rangesin order to establish the mappings right away."Does that seem clearer if placed ahead of the if?
Can be shorter imo - the thing I didn't pay attention to is that this is all about _dom0_ building, not general _domain_ building. Hence perhaps:
"Dom0 building runs on the idle vCPU, in which case it's not possible
to defer the operation (like done in the else branch). Call rangeset_consume_ranges in order to establish the mappings right away."
Quoted 33 lines
Quoted 24 lines
Quoted 24 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8
-0+static void modify_bars(const struct pci_dev *pdev, bool map, bool rom) +{ + struct vpci_header *header = &pdev->vpci->header; + struct rangeset *mem = rangeset_new(NULL, NULL, 0); + const struct pci_dev *tmp; + unsigned int i; + int rc; +Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ if ( !map ) + modify_decoding(pdev, false, rom); + + if ( !mem ) + return;Similarly here - why is it okay (or what effect will it have) to return here when we're out of memory, but the caller won't know?
The behaviour here depends on the change to the memory decoding bit:
- Clearing: memory decoding on device will be disabled, BARs won't be unmapped.
- Setting: no change to device memory decoding bit, BARs won't be mapped.Quoted 5 lines
Quoted 2 lines
Do you think this is suitable? IMO it's fine to disable the memory decoding bit on the device and leave the memory regions mapped.
As long as subsequent changes to the decoding bit can't leave stale mappings. Plus there needs to be a comment to explain this.
With the current approach in the unmap case there will be stale mappings left behind.
Quoted 7 lines
I guess it's better then to not modify the memory decoding bit at all until the operation finishes. That also rises the question of whether the memory decoding bit should be modified if p2m mapping/unmapping reports an error.
Should we also attempt to rollback failed map/unmap operations? What happens if the rollback also fails?
It is in particular this last question why I don't think rollback makes sense. If there's any failure, I think the decode bit should be sticky clear; we may want (need?) to invent some magical mechanism to get a device back out of this state later on. But that way stale mappings are not an immediate problem (I think).
Quoted 17 lines
What about the following:
+---------+ SUCCESS +---------------------------------+ |map/unmap+------------>Change decoding or ROM enable bit| +----+----+ +---------------------------------+ | |FAILURE | +--------v-------+ SUCCESS +---------------------------------------+ |revert operation+--------->No change to decoding or ROM enable bit| +--------+-------+ +---------------------------------------+ | |FAILURE | +-----v-----+ |Kill domain| +-----------+
Possibly. Killing Dom0 is a bad thing though, if just some random device has a problem.
Quoted 53 lines
Quoted 23 lines
Quoted 23 lines
Quoted 12 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) + { + const struct vpci_bar *bar = &header->bars[i]; + + if ( !MAPPABLE_BAR(bar) || + (rom ? bar->type != VPCI_BAR_ROM + : (bar->type == VPCI_BAR_ROM && !header->rom_enabled)) ) + continue;Why does
rom_enabledmatter for the !rom case rather than for the rom one? I.e.Quoted 6 lines
if ( !MAPPABLE_BAR(bar) || (rom ? bar->type != VPCI_BAR_ROM || !header->rom_enabled : bar->type == VPCI_BAR_ROM) ) continue;?
No, for the ROM case we only want to map/unmap the ROM, so that's the only thing added to the rangeset. For the non-ROM case Xen will also map/unmap the ROM if the enable bit is set.
Quoted 8 lines
Quoted 2 lines
Your proposed code would always map/unmap the ROM into the p2m when the memory decoding bit is changed even if it's not enabled.
I don't understand. Taking apart the conditional I've suggested, and converting to human language:
- if the BAR is no mappable, continue
- if we want to deal with ROM (rom=true), if the BAR isn't ROM or isn't enabled, continueWith the current flow
rom_enabledis set after the ROM BAR mappings are established, which means that whenmodify_barsis called with rom=truerom_enabledis not yet set, and using the logic above the mappings won't be created.Quoted 2 lines
- if we want to deal with non-ROM (rom=false), if the BAR is ROM, continue
Xen also has to deal with the ROM if it's enabled when the memory decoding bit is toggled, hence in rom=false case the ROM also needs to be mapped/unampped if it's enabled.
This dependency between the memory decoding bit and the ROM enable bit is quite convoluted TBH.
Quoted 5 lines
To me this is in line with the 2nd paragraph of your reply. It's not in line with the first, which makes me wonder whether "rom" is misnamed and wants to be "
rom_only". Still, the question would remain of whyrom_enableddoesn't matter when the variable is true.
Quoted 3 lines
Yes, rom means
rom_only(ie: ROM enable bit has been toggled and memory decoding bit is enabled). I assume you would prefer to change the name torom_only.
Yes, and perhaps provide an abridged version of your explanation above in a comment next to the conditional.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 49 lines · Jan Beulich
Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 27.02.18 at 10:21, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 26.02.18 at 19:00, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
(quoted diff) +13
-0+static void maybe_defer_map(struct domain *d, const struct pci_dev *pdev, + struct rangeset *mem, bool map, bool rom) +{ + struct vcpu *curr = current; + + if ( is_idle_vcpu(curr) ) + { + struct map_data data = { .d = d, .map = true }; + + /* + * Only used for domain construction in order to map the BARs + * of devices with memory decoding enabled. + */Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ASSERT(map && !rom); + rangeset_consume_ranges(mem, map_range, &data);What if this produces
-ENOMEM? And despite having looked at several revisions of this, I can't make the connection to why this is in anis_idle_vcpu()conditional (neither the direct caller nor the next level up make this obvious to me). There's clearly a need for extending the comment.I thought the comment above that mentions domain construction would be enough. I can try to expand this, maybe like:
Quoted 8 lines
Quoted 4 lines
"This function will only be called from the idle vCPU while building the domain, in which case it's not possible to defer the operation (like done in the else branch). Call
rangeset_consume_rangesin order to establish the mappings right away."And what again is the connection between
is_idle_domain()and domain construction? I think the comment belongs ahead of theif(), and it needs to make that connection.Oh, domain constructions runs on the idle vCPU, that's the relation.
Quoted 7 lines
"This function will only be called from the idle vCPU while building the domain (because Dom0 building runs on the idle vCPU), in which case it's not possible to defer the operation (like done in the else branch). Call
rangeset_consume_rangesin order to establish the mappings right away."Does that seem clearer if placed ahead of the if?
Quoted 8 lines
Can be shorter imo - the thing I didn't pay attention to is that this is all about _dom0_ building, not general _domain_ building. Hence perhaps:
"Dom0 building runs on the idle vCPU, in which case it's not possible
to defer the operation (like done in the else branch). Callrangeset_consume_rangesin order to establish the mappings right away."
LGTM. Thanks for re-writing it.
Quoted 38 lines
Quoted 38 lines
Quoted 24 lines
Quoted 24 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8
-0+static void modify_bars(const struct pci_dev *pdev, bool map, bool rom) +{ + struct vpci_header *header = &pdev->vpci->header; + struct rangeset *mem = rangeset_new(NULL, NULL, 0); + const struct pci_dev *tmp; + unsigned int i; + int rc; +Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ if ( !map ) + modify_decoding(pdev, false, rom); + + if ( !mem ) + return;Similarly here - why is it okay (or what effect will it have) to return here when we're out of memory, but the caller won't know?
The behaviour here depends on the change to the memory decoding bit:
- Clearing: memory decoding on device will be disabled, BARs won't be unmapped.
- Setting: no change to device memory decoding bit, BARs won't be mapped.Quoted 5 lines
Quoted 2 lines
Do you think this is suitable? IMO it's fine to disable the memory decoding bit on the device and leave the memory regions mapped.
As long as subsequent changes to the decoding bit can't leave stale mappings. Plus there needs to be a comment to explain this.
With the current approach in the unmap case there will be stale mappings left behind.
I guess it's better then to not modify the memory decoding bit at all until the operation finishes. That also rises the question of whether the memory decoding bit should be modified if p2m mapping/unmapping reports an error.
Quoted 8 lines
Quoted 2 lines
Should we also attempt to rollback failed map/unmap operations? What happens if the rollback also fails?
It is in particular this last question why I don't think rollback makes sense. If there's any failure, I think the decode bit should be sticky clear; we may want (need?) to invent some magical mechanism to get a device back out of this state later on. But that way stale mappings are not an immediate problem (I think).
The only problem I see with this approach is that if an error happens in the unmap case we will disable memory decoding and leave some stale p2m mappings. Then the guest might change the position of the BARs, and those stale mappings would be completely forgotten.
I don't think this is a problem for Dom0, but DomUs need to be handled differently (possibly along the lines of the diagram below).
Quoted 18 lines
Quoted 17 lines
What about the following:
+---------+ SUCCESS +---------------------------------+ |map/unmap+------------>Change decoding or ROM enable bit| +----+----+ +---------------------------------+ | |FAILURE | +--------v-------+ SUCCESS +---------------------------------------+ |revert operation+--------->No change to decoding or ROM enable bit| +--------+-------+ +---------------------------------------+ | |FAILURE | +-----v-----+ |Kill domain| +-----------+
Quoted 2 lines
Possibly. Killing Dom0 is a bad thing though, if just some random device has a problem.
So for Dom0 the diagram would be the following:
+---------+ SUCCESS +---------------------------------+
|map/unmap+------------>Change decoding or ROM enable bit|
+----+----+ +---------------------------------+
|
|FAILURE
|
+--------v-----------------------+
|Clear decoding or ROM enable bit|
+--------------------------------+Quoted 53 lines
Quoted 53 lines
Quoted 23 lines
Quoted 23 lines
Quoted 12 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) + { + const struct vpci_bar *bar = &header->bars[i]; + + if ( !MAPPABLE_BAR(bar) || + (rom ? bar->type != VPCI_BAR_ROM + : (bar->type == VPCI_BAR_ROM && !header->rom_enabled)) ) + continue;Why does
rom_enabledmatter for the !rom case rather than for the rom one? I.e.Quoted 6 lines
if ( !MAPPABLE_BAR(bar) || (rom ? bar->type != VPCI_BAR_ROM || !header->rom_enabled : bar->type == VPCI_BAR_ROM) ) continue;?
No, for the ROM case we only want to map/unmap the ROM, so that's the only thing added to the rangeset. For the non-ROM case Xen will also map/unmap the ROM if the enable bit is set.
Quoted 8 lines
Quoted 2 lines
Your proposed code would always map/unmap the ROM into the p2m when the memory decoding bit is changed even if it's not enabled.
I don't understand. Taking apart the conditional I've suggested, and converting to human language:
- if the BAR is no mappable, continue
- if we want to deal with ROM (rom=true), if the BAR isn't ROM or isn't enabled, continueWith the current flow
rom_enabledis set after the ROM BAR mappings are established, which means that whenmodify_barsis called with rom=truerom_enabledis not yet set, and using the logic above the mappings won't be created.Quoted 2 lines
- if we want to deal with non-ROM (rom=false), if the BAR is ROM, continue
Xen also has to deal with the ROM if it's enabled when the memory decoding bit is toggled, hence in rom=false case the ROM also needs to be mapped/unampped if it's enabled.
This dependency between the memory decoding bit and the ROM enable bit is quite convoluted TBH.
Quoted 5 lines
To me this is in line with the 2nd paragraph of your reply. It's not in line with the first, which makes me wonder whether "rom" is misnamed and wants to be "
rom_only". Still, the question would remain of whyrom_enableddoesn't matter when the variable is true.
Quoted 6 lines
Quoted 3 lines
Yes, rom means
rom_only(ie: ROM enable bit has been toggled and memory decoding bit is enabled). I assume you would prefer to change the name torom_only.Yes, and perhaps provide an abridged version of your explanation above in a comment next to the conditional.
OK, I can do that.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 27.02.18 at 12:57, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 27.02.18 at 10:21, <[email protected]> wrote:
With the current approach in the unmap case there will be stale mappings left behind.
I guess it's better then to not modify the memory decoding bit at all until the operation finishes. That also rises the question of whether the memory decoding bit should be modified if p2m mapping/unmapping reports an error.
Quoted 8 lines
Quoted 2 lines
Should we also attempt to rollback failed map/unmap operations? What happens if the rollback also fails?
It is in particular this last question why I don't think rollback makes sense. If there's any failure, I think the decode bit should be sticky clear; we may want (need?) to invent some magical mechanism to get a device back out of this state later on. But that way stale mappings are not an immediate problem (I think).
Quoted 4 lines
The only problem I see with this approach is that if an error happens in the unmap case we will disable memory decoding and leave some stale p2m mappings. Then the guest might change the position of the BARs, and those stale mappings would be completely forgotten.
Well, the implication was that together with the decode enable bit not being allowed to be turned off would be that some recording would perhaps be needed of the stale mappings. Allowing the decode enable bit to be set again would then depend on the stale mappings first being dealt with. As long as the decode enable bit can't be set, the owning domain playing with the BARs is of no interest.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 25 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 27.02.18 at 12:57, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 27.02.18 at 10:21, <[email protected]> wrote:
With the current approach in the unmap case there will be stale mappings left behind.
I guess it's better then to not modify the memory decoding bit at all until the operation finishes. That also rises the question of whether the memory decoding bit should be modified if p2m mapping/unmapping reports an error.
Quoted 8 lines
Quoted 2 lines
Should we also attempt to rollback failed map/unmap operations? What happens if the rollback also fails?
It is in particular this last question why I don't think rollback makes sense. If there's any failure, I think the decode bit should be sticky clear; we may want (need?) to invent some magical mechanism to get a device back out of this state later on. But that way stale mappings are not an immediate problem (I think).
Quoted 4 lines
The only problem I see with this approach is that if an error happens in the unmap case we will disable memory decoding and leave some stale p2m mappings. Then the guest might change the position of the BARs, and those stale mappings would be completely forgotten.
Quoted 7 lines
Well, the implication was that together with the decode enable bit not being allowed to be turned off would be that some recording would perhaps be needed of the stale mappings. Allowing the decode enable bit to be set again would then depend on the stale mappings first being dealt with. As long as the decode enable bit can't be set, the owning domain playing with the BARs is of no interest.
I've been giving some thought to this, and I cannot find a good solution. So far the less worse one would be:
+--------------+ YES +---------+
|Is broken set?|------>|No action|
+--------------+ +---------+
|
| NO
|
+----v----+ SUCCESS +---------------------------------+
|map/unmap+------------>Change decoding or ROM enable bit|
+----+----+ +---------------------------------+
|
|FAILURE
|
+--------v----------------+
|Clear memory decoding bit|
|Set broken |
+-------------------------+Let me know what you think of this.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 28.02.18 at 12:20, <[email protected]> wrote:
I've been giving some thought to this, and I cannot find a good solution. So far the less worse one would be:
+--------------+ YES +---------+ |Is broken set?|------>|No action| +--------------+ +---------+ | | NO | +----v----+ SUCCESS +---------------------------------+ |map/unmap+------------>Change decoding or ROM enable bit| +----+----+ +---------------------------------+ | |FAILURE | +--------v----------------+ |Clear memory decoding bit| |Set broken | +-------------------------+
Quoted 1 line
Let me know what you think of this.
Looks reasonable, maybe with "set broken" replaced by "set broken if Dom0, else remove device from domain". This at least partly depends on how recovery from "broken" would look like, i.e. whether that would be applicable (and safe) for a DomU as well.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 28.02.18 at 12:20, <[email protected]> wrote:
I've been giving some thought to this, and I cannot find a good solution. So far the less worse one would be:
+--------------+ YES +---------+ |Is broken set?|------>|No action| +--------------+ +---------+ | | NO | +----v----+ SUCCESS +---------------------------------+ |map/unmap+------------>Change decoding or ROM enable bit| +----+----+ +---------------------------------+ | |FAILURE | +--------v----------------+ |Clear memory decoding bit| |Set broken | +-------------------------+
Quoted 7 lines
Quoted 1 line
Let me know what you think of this.
Looks reasonable, maybe with "set broken" replaced by "set broken if Dom0, else remove device from domain". This at least partly depends on how recovery from "broken" would look like, i.e. whether that would be applicable (and safe) for a DomU as well.
I will add the "remove device from domain" for the DomU case and put this as a comment somewhere. IMO it can always be improved in the future with better recovery options.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel