vPCI: extended capability handling
v5 cover vPCI: extended capability handling
This is a follow-on to 'vPCI: avoid bogus "overlap in extended cap list" warnings', addressing further issues noted there.
v5: One new patch and some other re-work. See individual patches.
1: introduce private header
2: move vpci_init_capabilities() to a separate file
3: move capability-list init
4: ReBAR: improve cleanup
5: re-init extended-capabilities when MMCFG availability changed
Jan
PATCH v5 1/5 vPCI: introduce private header
Before adding more private stuff to xen/vpci.h, split it up. In order to be able to include the private header first in a CU, the per-arch struct decls also need to move (to new asm/vpci.h files).
While adjusting the test harness'es Makefile, also switch the pre-existing header symlink-ing rule to a pattern one.
Apart from in the test harness code, things only move; no functional change intended.
---
Subsequently, at least on x86 more stuff may want moving into asm/vpci.h.
---
v5: Add new generated header to test harness clean rule and to .gitignore.
Also move vpci_init_header().
v4: New.
.gitignore +1 -0
@@ -154,6 +154,7 @@ tools/tests/x86_emulator/test_x86_emulat tools/tests/x86_emulator/x86_emulate tools/tests/x86_emulator/xop*.[ch] tools/tests/vpci/list.h +tools/tests/vpci/private.h tools/tests/vpci/vpci.[hc] tools/tests/vpci/test_vpci tools/xcutils/lsevtchn
tools/tests/vpci/Makefile +8 -8
@@ -14,12 +14,12 @@ else $(warning HOSTCC != CC, will not run test) endif -$(TARGET): vpci.c vpci.h list.h main.c emul.h - $(CC) $(CFLAGS_xeninclude) -g -o $@ vpci.c main.c +$(TARGET): vpci.c vpci.h list.h private.h main.c emul.h + $(CC) $(CFLAGS_xeninclude) -include emul.h -g -o $@ vpci.c main.c .PHONY: clean clean: - rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h + rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h private.h .PHONY: distclean distclean: clean
@@ -34,10 +34,10 @@ uninstall: $(RM) -- $(DESTDIR)$(LIBEXEC)/tests/$(TARGET) vpci.c: $(XEN_ROOT)/xen/drivers/vpci/vpci.c - # Remove includes and add the test harness header - sed -e '/#include/d' -e '1s/^/#include "emul.h"/' <$< >$@ + sed -e '/#include/d' <$< >$@ + +private.h: %.h: $(XEN_ROOT)/xen/drivers/vpci/%.h + sed -e '/#include/d' <$< >$@ -list.h: $(XEN_ROOT)/xen/include/xen/list.h -vpci.h: $(XEN_ROOT)/xen/include/xen/vpci.h -list.h vpci.h: +list.h vpci.h: %.h: $(XEN_ROOT)/xen/include/xen/%.h sed -e '/#include/d' <$< >$@
tools/tests/vpci/emul.h +1 -0
@@ -86,6 +86,7 @@ typedef union {#define CONFIG_HAS_VPCI #include "vpci.h" +#include "private.h" #define __hwdom_init
xen/arch/arm/include/asm/pci.h +0 -8
@@ -31,14 +31,6 @@ struct arch_pci_dev {struct device dev; }; -/* Arch-specific MSI data for vPCI. */ -struct vpci_arch_msi { -}; - -/* Arch-specific MSI-X entry data for vPCI. */ -struct vpci_arch_msix_entry { -}; -/* * Because of the header cross-dependencies, e.g. we need both * struct pci_dev and struct arch_pci_dev at the same time, this cannot be
xen/arch/arm/include/asm/vpci.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef ARM_VPCI_H +#define ARM_VPCI_H + +/* Arch-specific MSI data for vPCI. */ +struct vpci_arch_msi { +}; + +/* Arch-specific MSI-X entry data for vPCI. */ +struct vpci_arch_msix_entry { +}; + +#endif /* ARM_VPCI_H */
xen/arch/x86/include/asm/hvm/io.h +0 -11
@@ -97,17 +97,6 @@ void msixtbl_init(struct domain *d);static inline void msixtbl_init (struct domain *d) {}#endif -/* Arch-specific MSI data for vPCI. */ -struct vpci_arch_msi { -int pirq; -bool bound; -}; - -/* Arch-specific MSI-X entry data for vPCI. */ -struct vpci_arch_msix_entry { -int pirq; -}; -void stdvga_init (struct domain *d);extern void hvm_dpci_msi_eoi (struct domain *d,int vector);
xen/arch/x86/include/asm/vpci.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef X86_VPCI_H +#define X86_VPCI_H + +#include <xen/stdbool.h> + +/* Arch-specific MSI data for vPCI. */ +struct vpci_arch_msi { +int pirq; +bool bound; +}; + +/* Arch-specific MSI-X entry data for vPCI. */ +struct vpci_arch_msix_entry { +int pirq; +}; + +#endif /* X86_VPCI_H */ + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
xen/drivers/vpci/header.c +2 -1
@@ -17,11 +17,12 @@* License along with this program; If not, see <http://www.gnu.org/licenses/>. */ +#include "private.h" +#include <xen/iocap.h> #include <xen/lib.h> #include <xen/sched.h> #include <xen/softirq.h> -#include <xen/vpci.h> #include <xsm/xsm.h>
xen/drivers/vpci/msi.c +2 -1
@@ -16,9 +16,10 @@* License along with this program; If not, see <http://www.gnu.org/licenses/>. */ +#include "private.h" +#include <xen/sched.h> #include <xen/softirq.h> -#include <xen/vpci.h> #include <asm/msi.h>
xen/drivers/vpci/msix.c +2 -1
@@ -17,10 +17,11 @@* License along with this program; If not, see <http://www.gnu.org/licenses/>. */ +#include "private.h" +#include <xen/io.h> #include <xen/lib.h> #include <xen/sched.h> -#include <xen/vpci.h> #include <asm/msi.h> #include <asm/p2m.h>
xen/drivers/vpci/private.h new file +126 -0
126 lines not on this page · raw message
xen/drivers/vpci/rebar.c +2 -1
@@ -5,8 +5,9 @@* Author: Jiqian Chen <[email protected]> */ +#include "private.h" +#include <xen/sched.h> -#include <xen/vpci.h> static void cf_check rebar_ctrl_write(const struct pci_dev *pdev, unsigned int reg,
xen/drivers/vpci/vpci.c +2 -1
@@ -17,8 +17,9 @@* License along with this program; If not, see <http://www.gnu.org/licenses/>. */ +#include "private.h" +#include <xen/sched.h> -#include <xen/vpci.h> #include <xen/vmap.h> /* Internal struct to store the emulated PCI registers. */
xen/include/xen/vpci.h +1 -106
142 lines not on this page · raw message
Re: [PATCH v5 1/5] vPCI: introduce private header
Quoted 11 lines · Jan Beulich
Before adding more private stuff to
xen/vpci.h, split it up. In order to be able to include the private header first in a CU, the per-arch struct decls also need to move (to newasm/vpci.hfiles).While adjusting the test harness'es
Makefile, also switch the pre-existing header symlink-ing rule to a pattern one.Apart from in the test harness code, things only move; no functional change intended.
Signed-off-by Jan Beulich <[email protected]>
Re: [PATCH v5 1/5] vPCI: introduce private header
In reply to Jan BeulichQuoted 11 lines · Jan Beulich
Before adding more private stuff to
xen/vpci.h, split it up. In order to be able to include the private header first in a CU, the per-arch struct decls also need to move (to newasm/vpci.hfiles).While adjusting the test harness'es
Makefile, also switch the pre-existing header symlink-ing rule to a pattern one.Apart from in the test harness code, things only move; no functional change intended.
Signed-off-by Jan Beulich <[email protected]>
One comment below. The Ack stands regardless of whether you want to change it or not.
Quoted 36 lines
---
Subsequently, at least on x86 more stuff may want moving intoasm/vpci.h.
---
v5: Add new generated header to test harness clean rule and to .gitignore.
Also movevpci_init_header().
v4: New..gitignore +1
-0@@ -154,6 +154,7 @@ tools/tests/x86_emulator/test_x86_emulat tools/tests/x86_emulator/x86_emulate tools/tests/x86_emulator/xop*.[ch] tools/tests/vpci/list.h +tools/tests/vpci/private.h tools/tests/vpci/vpci.[hc] tools/tests/vpci/test_vpci tools/xcutils/lsevtchntools/tests/vpci/Makefile +3
-3@@ -14,12 +14,12 @@ else $(warning HOSTCC != CC, will not run test) endif -$(TARGET): vpci.c vpci.h list.h main.c emul.h - $(CC) $(CFLAGS_xeninclude) -g -o $@ vpci.c main.c +$(TARGET): vpci.c vpci.h list.h private.h main.c emul.h + $(CC) $(CFLAGS_xeninclude) -include emul.h -g -o $@ vpci.c main.c .PHONY: clean clean: - rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h + rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h private.h .PHONY: distclean distclean: clean
tools/tests/vpci/Makefile uninstall: · line 40
Quoted 9 lines
tools/tests/vpci/Makefile +4
-2@@ -34,10 +34,10 @@ uninstall: $(RM) -- $(DESTDIR)$(LIBEXEC)/tests/$(TARGET) vpci.c: $(XEN_ROOT)/xen/drivers/vpci/vpci.c - # Remove includes and add the test harness header - sed -e '/#include/d' -e '1s/^/#include "emul.h"/' <$< >$@ + sed -e '/#include/d' <$< >$@ + +private.h: %.h: $(XEN_ROOT)/xen/drivers/vpci/%.h + sed -e '/#include/d' <$< >$@
Nit: if you are changing/adding those we might as well do /^#[[:space:]]*include/d, as sometimes we add spaces if the header inclusion is conditional.
Thanks, Roger.
Re: [PATCH v5 1/5] vPCI: introduce private header
Quoted 8 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich
Before adding more private stuff to
xen/vpci.h, split it up. In order to be able to include the private header first in a CU, the per-arch struct decls also need to move (to newasm/vpci.hfiles).While adjusting the test harness'es
Makefile, also switch the pre-existing header symlink-ing rule to a pattern one.
Quoted 6 lines
Quoted 4 lines
Apart from in the test harness code, things only move; no functional change intended.
Signed-off-by Jan Beulich <[email protected]>Acked-by Roger Pau Monné <[email protected]>
Thanks.
Quoted 21 lines
Quoted 21 lines
tools/tests/vpci/Makefile +3
-3@@ -14,12 +14,12 @@ else $(warning HOSTCC != CC, will not run test) endif -$(TARGET): vpci.c vpci.h list.h main.c emul.h - $(CC) $(CFLAGS_xeninclude) -g -o $@ vpci.c main.c +$(TARGET): vpci.c vpci.h list.h private.h main.c emul.h + $(CC) $(CFLAGS_xeninclude) -include emul.h -g -o $@ vpci.c main.c .PHONY: clean clean: - rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h + rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h private.h .PHONY: distclean distclean: clean@@ -34,10 +34,10 @@ uninstall: $(RM) -- $(DESTDIR)$(LIBEXEC)/tests/$(TARGET)
Quoted 11 lines
tools/tests/vpci/Makefile uninstall: · line 40
Quoted 7 lines
tools/tests/vpci/Makefile +4
-2vpci.c: $(XEN_ROOT)/xen/drivers/vpci/vpci.c - # Remove includes and add the test harness header - sed -e '/#include/d' -e '1s/^/#include "emul.h"/' <$< >$@ + sed -e '/#include/d' <$< >$@ + +private.h: %.h: $(XEN_ROOT)/xen/drivers/vpci/%.h + sed -e '/#include/d' <$< >$@Nit: if you are changing/adding those we might as well do /^#[[:space:]]*include/d, as sometimes we add spaces if the header inclusion is conditional.
Editing these two like you say would leave an inconsistency with what's further down (and what isn't being touched). I think I'd hence prefer that to be a separate change. Nevertheless, I agree with the intention.
Jan
PATCH v5 2/5 vPCI: move vpci_init_capabilities() to a separate file
In reply to Jan BeulichLet's keep capability handling together. Start with moving vpci_init_capabilities() and its helpers, plus splitting out of its cleanup counterpart.
---vpci_get_register(), while now only used by cap.c, didn't look like it would want moving there.
---
v4: New.
xen/drivers/vpci/Makefile +1 -0
@@ -1,2 +1,3 @@ +obj-y += cap.o obj-y += vpci.o header.o rebar.o obj-$(CONFIG_HAS_PCI_MSI) += msi.o msix.o
xen/drivers/vpci/cap.c new file +252 -0
252 lines not on this page · raw message
xen/drivers/vpci/private.h +21 -0
@@ -9,6 +9,20 @@ typedef uint32_t vpci_read_t(const structypedef void vpci_write_t (const struct pci_dev *pdev,unsigned int reg,uint32_t val,void *data); +/* Internal struct to store the emulated PCI registers. */ +struct vpci_register { +vpci_read_t *read; +vpci_write_t *write; +unsigned int size; +unsigned int offset; +void *private; +struct list_head node; +uint32_t ro_mask; +uint32_t rw1c_mask; +uint32_t rsvdp_mask; +uint32_t rsvdz_mask; +}; + typedef struct { unsigned int id; bool is_ext;
@@ -32,6 +46,9 @@ typedef struct {int __must_check vpci_init_header (struct pci_dev *pdev); +int vpci_init_capabilities (struct pci_dev *pdev); +void vpci_cleanup_capabilities (struct pci_dev *pdev); +/* Add/remove a register handler. */ int __must_check vpci_add_register_mask(struct vpci *vpci, vpci_read_t *read_handler,
@@ -49,6 +66,10 @@ int __must_check vpci_add_register(strucint vpci_remove_registers (struct vpci *vpci,unsigned int start,unsigned int size); +struct vpci_register *vpci_get_register (const struct vpci *vpci, +unsigned int offset, +unsigned int size); +/* Helper to return the value passed in data. */ uint32_t cf_check vpci_read_val(const struct pci_dev *pdev,unsigned int reg,void *data);
xen/drivers/vpci/vpci.c +4 -246
275 lines not on this page · raw message
PATCH v5 3/5 vPCI: move capability-list init
In reply to Jan Beulich... both for when the functions are invoked and where they live in source. Don't invoke them directly in vpci_init_header(), but instead first thing in vpci_init_capabilities().
---
v4: New.
xen/drivers/vpci/cap.c +136 -1
@@ -159,15 +159,150 @@ static int vpci_ext_capability_hide(return 0 ; } +static int vpci_init_capability_list (struct pci_dev *pdev) +{ +int rc; +bool mask_cap_list = false; +bool is_hwdom =is_hardware_domain (pdev->domain); + +if (pci_conf_read16 (pdev->sbdf,PCI_STATUS ) &PCI_STATUS_CAP_LIST ) + { +/* Only expose capabilities to the guest that vPCI can handle. */ +unsigned int next, ttl =48 ; +static const unsigned int supported_caps[] = { +PCI_CAP_ID_MSI , +PCI_CAP_ID_MSIX , + }; +/* +* For dom0, we should expose all capabilities instead of a fixed +* capabilities array, so setting n to 0 here is to get the next +* capability position directly in pci_find_next_cap_ttl. +*/ +const unsigned int n = is_hwdom ?0 :ARRAY_SIZE (supported_caps); + + next =pci_find_next_cap_ttl (pdev->sbdf,PCI_CAPABILITY_LIST , + supported_caps, n, &ttl); + + rc =vpci_add_register (pdev->vpci, vpci_read_val, + is_hwdom ? vpci_hw_write8 :NULL , +PCI_CAPABILITY_LIST ,1 , + (void *)(uintptr_t )next); +if ( rc ) +return rc; + + next &= ~3 ; + +if ( !next && !is_hwdom ) +/* +* If we don't have any supported capabilities to expose to the +* guest, mask the PCI_STATUS_CAP_LIST bit in the status +* register. +*/ + mask_cap_list = true; + +while ( next && ttl ) + { +unsigned int pos = next; + + next =pci_find_next_cap_ttl (pdev->sbdf, + pos +PCI_CAP_LIST_NEXT , + supported_caps, n, &ttl); + +if ( !is_hwdom ) + { + rc =vpci_add_register (pdev->vpci, vpci_hw_read8,NULL , + pos +PCI_CAP_LIST_ID ,1 ,NULL ); +if ( rc ) +return rc; + } + + rc =vpci_add_register (pdev->vpci, vpci_read_val, + is_hwdom ? vpci_hw_write8 :NULL , + pos +PCI_CAP_LIST_NEXT ,1 , + (void *)(uintptr_t )next); +if ( rc ) +return rc; + + next &= ~3 ; + } + } + +/* Return early for the hw domain, no masking of PCI_STATUS. */ +if ( is_hwdom ) +return 0 ; + +/* Utilize rsvdp_mask to hide PCI_STATUS_CAP_LIST from the guest. */ +return vpci_add_register_mask (pdev->vpci, vpci_hw_read16, vpci_hw_write16, +PCI_STATUS ,2 ,NULL , +PCI_STATUS_RO_MASK & + ~(mask_cap_list ?PCI_STATUS_CAP_LIST :0 ), +PCI_STATUS_RW1C_MASK , + mask_cap_list ?PCI_STATUS_CAP_LIST :0 , +PCI_STATUS_RSVDZ_MASK ); +} + +static int vpci_init_ext_capability_list (const struct pci_dev *pdev) +{ +unsigned int pos =PCI_CFG_SPACE_SIZE ; + +if ( !pdev->ext_cfg ) +return 0 ; + +if ( !is_hardware_domain (pdev->domain) ) +/* Extended capabilities read as zero, write ignore for DomU */ +return vpci_add_register (pdev->vpci, vpci_read_val,NULL , + pos,4 , (void *)0 ); + +do + { +uint32_t header =pci_conf_read32 (pdev->sbdf, pos); +int rc; + +if ( header ==0xffffffffU ) + { +printk (XENLOG_WARNING +"%pd %pp: broken extended cap list, offset %#x\n" , + pdev->domain, &pdev->sbdf, pos); +return 0 ; + } + + rc =vpci_add_register (pdev->vpci, vpci_read_val,NULL , + pos,4 , (void *)(uintptr_t )header); +if ( rc == -EEXIST ) + { +printk (XENLOG_WARNING +"%pd %pp: overlap in extended cap list, offset %#x\n" , + pdev->domain, &pdev->sbdf, pos); +return 0 ; + } + +if ( rc ) +return rc; + + pos =PCI_EXT_CAP_NEXT (header); + }while ( pos >=PCI_CFG_SPACE_SIZE ); + +return 0 ; +} +int vpci_init_capabilities (struct pci_dev *pdev) { +int rc; + + rc =vpci_init_capability_list (pdev); +if ( rc ) +return rc; + + rc =vpci_init_ext_capability_list (pdev); +if ( rc ) +return rc; +for (unsigned int i =0 ; i <NUM_VPCI_INIT ; i++ ) {const vpci_capability_t *capability = &__start_vpci_array[i];const unsigned int cap = capability->id;const bool is_ext = capability->is_ext;unsigned int pos =0 ; -int rc;if ( !is_ext ) pos =pci_find_cap_offset (pdev->sbdf, cap);
xen/drivers/vpci/header.c +0 -134
146 lines not on this page · raw message
PATCH v5 4/5 vPCI/ReBAR: improve cleanup
In reply to Jan BeulichWe cannot assume extended config space to (still) be accessible when cleaning up. Necessary value need caching instead. In fact, as the caller also cannot look up extended capabilities, the cleanup function needs to cope with being called when there's no ReBAR capability at all.
As kind of a side effect nbars being 0 (which init_rebar() doesn't explicitly reject) no longer results in a bogus call to vpci_remove_registers().
---
v5: New.
xen/drivers/vpci/rebar.c +7 -8
@@ -53,17 +53,12 @@ static void cf_check rebar_ctrl_write(co static int cf_check cleanup_rebar(const struct pci_dev *pdev, bool hide) {int rc; -uint32_t ctrl; -unsigned int nbars; -unsigned int rebar_offset =pci_find_ext_capability (pdev, -PCI_EXT_CAP_ID_REBAR ); +unsigned int nbars = pdev->vpci->rebar.nbars; +unsigned int rebar_offset = pdev->vpci->rebar.offset; -if ( !hide ) +if ( !rebar_offset || !nbars || !hide )return 0 ; - ctrl =pci_conf_read32 (pdev->sbdf, rebar_offset +PCI_REBAR_CTRL (0 )); - nbars =MASK_EXTR (ctrl,PCI_REBAR_CTRL_NBAR_MASK ); - rc =vpci_remove_registers (pdev->vpci, rebar_offset +PCI_REBAR_CAP (0 ),PCI_REBAR_CTRL (nbars -1 ));if ( rc )
@@ -121,6 +116,10 @@ static int cf_check init_rebar(struct pc ctrl =pci_conf_read32 (pdev->sbdf, rebar_offset +PCI_REBAR_CTRL (0 )); nbars =MASK_EXTR (ctrl,PCI_REBAR_CTRL_NBAR_MASK ); + + pdev->vpci->rebar.offset = rebar_offset; + pdev->vpci->rebar.nbars = nbars; +for (unsigned int i =0 ; i < nbars; i++ ) {int rc;
xen/include/xen/vpci.h +7 -0
@@ -135,6 +135,13 @@ struct vpci {struct vpci_arch_msix_entry arch; } entries[]; } *msix; + +/* Resizable BARs data */ +struct vpci_rebar { +unsigned int offset:12 ; +unsigned int nbars:3 ; + } rebar; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT /* Guest SBDF of the device. */ #define INVALID_GUEST_SBDF ((pci_sbdf_t){ .sbdf = ~0U })
Re: [PATCH v5 4/5] vPCI/ReBAR: improve cleanup
Quoted 4 lines · Jan Beulich
We cannot assume extended config space to (still) be accessible when cleaning up. Necessary value need caching instead. In fact, as the caller also cannot look up extended capabilities, the cleanup function needs to cope with being called when there's no ReBAR capability at all.
I see that you adjust the code in vpci_cleanup_capabilities() in the next patch, so that it also doesn't check for extended capability presence as part of cleanup.
Quoted 6 lines
As kind of a side effect nbars being 0 (which
init_rebar()doesn't explicitly reject) no longer results in a bogus call tovpci_remove_registers().Fixes ee459aeac096 ("vpci/rebar: Implement cleanup function for Rebar")Signed-off-by Jan Beulich <[email protected]>
Thanks, Roger.
PATCH v5 5/5 vPCI: re-init extended-capabilities when MMCFG availability changed
In reply to Jan BeulichWhen Dom0 informs us about MMCFG usability, this may change whether extended capabilities are available (accessible) for devices. Zap what might be on record, and re-initialize things.
No synchronization is added for the case where devices may already be in use. That'll need sorting when (a) DomU support was added and (b) DomU-s may run already while Dom0 / hwdom still boots (dom0less, Hyperlaunch).
vpci_cleanup_capabilities() also shouldn't have used pci_find_ext_capability(), as already when the function was introduced extended config space may not have been (properly) accessible anymore, no matter whether it was during init. Extended capability cleanup hooks need to cope with being called when the respective capability doesn't exist (and hence the corresponding ->init() hook was never called).
---
vpci_reinit_ext_capabilities()'es return value is checked only to log an error; it doesn't feel quite right to fail the hypercall because of this. Roger brought up the idea of de-assigning the device in such a case, but if a driver doesn't use extended capabilities the device would likely continue to work fine, for Dom0 this probably wouldn't be quite right anyway, and it's also unclear whether calling deassign_device() could be done from this context. Something like what pci_check_disable_device() does may be an option, if we really think we need to "break" the device.
The use of is_hardware_domain() in vpci_cleanup_capabilities() was uncommented and hence is left so. Shouldn't there be a DomU-related TODO or FIXME?
---
v5: Don't use pci_find_ext_capability() in vpci_cleanup_capabilities().
Add assertion in vpci_reinit_ext_capabilities().
v4: Make sure ->cleanup() and ->init() are invoked.
v3: New.
xen/arch/x86/physdev.c +11 -0
@@ -8,6 +8,8 @@#include <xen/guest_access.h> #include <xen/iocap.h> #include <xen/serial.h> +#include <xen/vpci.h> +#include <asm/current.h> #include <asm/io_apic.h> #include <asm/msi.h>
@@ -169,8 +171,17 @@ int cf_check physdev_check_pci_extcfg(stASSERT (pdev->seg == info->segment);if ( pdev->bus >= info->start_bus && pdev->bus <= info->end_bus ) + { +int rc; +pci_check_extcfg (pdev); + rc =vpci_reinit_ext_capabilities (pdev); +if ( rc ) +gprintk (XENLOG_ERR ,"%pp(%pd): vPCI extcap reinit failed: %d\n" , + &pdev->sbdf, pdev->domain, rc); + } + return 0; }#endif /* COMPAT */
xen/drivers/vpci/cap.c +38 -12
86 lines not on this page · raw message
xen/drivers/vpci/private.h +2 -2
@@ -46,8 +46,8 @@ typedef struct {int __must_check vpci_init_header (struct pci_dev *pdev); -int vpci_init_capabilities (struct pci_dev *pdev); -void vpci_cleanup_capabilities (struct pci_dev *pdev); +int vpci_init_capabilities (struct pci_dev *pdev,bool ext_only); +void vpci_cleanup_capabilities (struct pci_dev *pdev,bool ext_only);/* Add/remove a register handler. */ int __must_check vpci_add_register_mask(struct vpci *vpci,
xen/drivers/vpci/vpci.c +2 -2
@@ -102,7 +102,7 @@ void vpci_deassign_device(struct pci_dev &pdev->domain->vpci_dev_assigned_map);#endif -vpci_cleanup_capabilities (pdev); +vpci_cleanup_capabilities (pdev, false);spin_lock (&pdev->vpci->lock);while ( !list_empty (&pdev->vpci->handlers) )
@@ -159,7 +159,7 @@ int vpci_assign_device(struct pci_dev *pif ( rc ) goto out; - rc =vpci_init_capabilities (pdev); + rc =vpci_init_capabilities (pdev, false); out:if ( rc )
xen/include/xen/vpci.h +7 -0
@@ -25,6 +25,8 @@ int __must_check vpci_assign_device(stru/* Remove all handlers and free vpci related structures. */ void vpci_deassign_device (struct pci_dev *pdev); +int vpci_reinit_ext_capabilities (struct pci_dev *pdev); +/* Generic read/write handlers for the PCI config space. */ uint32_t vpci_read (pci_sbdf_t sbdf,unsigned int reg,unsigned int size); void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
@@ -202,6 +204,11 @@ bool vpci_ecam_read(pci_sbdf_t sbdf, uns#else /* !CONFIG_HAS_VPCI */ struct vpci_vcpu {}; +static inline int vpci_reinit_ext_capabilities (struct pci_dev *pdev) +{ +return 0 ; +} +static inline int vpci_assign_device (struct pci_dev *pdev) {return 0 ;
Re: [PATCH v5 5/5] vPCI: re-init extended-capabilities when MMCFG availability changed
Quoted 17 lines · Jan Beulich
When Dom0 informs us about MMCFG usability, this may change whether extended capabilities are available (accessible) for devices. Zap what might be on record, and re-initialize things.
No synchronization is added for the case where devices may already be in use. That'll need sorting when (a) DomU support was added and (b) DomU-s may run already while Dom0 / hwdom still boots (dom0less, Hyperlaunch).
vpci_cleanup_capabilities()also shouldn't have usedpci_find_ext_capability(), as already when the function was introduced extended config space may not have been (properly) accessible anymore, no matter whether it was during init. Extended capability cleanup hooks need to cope with being called when the respective capability doesn't exist (and hence the corresponding ->init() hook was never called).Fixes 70e6dace747e ("vpci: Use cleanup to free capability resource during deassign")Signed-off-by Jan Beulich <[email protected]>
Quoted 9 lines
---
vpci_reinit_ext_capabilities()'es return value is checked only to log an error; it doesn't feel quite right to fail the hypercall because of this. Roger brought up the idea of de-assigning the device in such a case, but if a driver doesn't use extended capabilities the device would likely continue to work fine, for Dom0 this probably wouldn't be quite right anyway, and it's also unclear whether callingdeassign_device()could be done from this context. Something like whatpci_check_disable_device()does may be an option, if we really think we need to "break" the device.
We may want to add a note there, stating that we have considered all possible options, and hiding the capability and hoping the owner domain would continue to work as expected seems the less bad of all of them?
Quoted 3 lines
The use of
is_hardware_domain()invpci_cleanup_capabilities()was uncommented and hence is left so. Shouldn't there be a DomU-related TODO or FIXME?
Hm, yes, possibly. I think limiting extended space availability to the hardware domain only has been done "just" because we have no extended capabilities to expose to domUs so far, and I don't think we even setup the extended capability list in the domU case.
Quoted 68 lines
---
v5: Don't usepci_find_ext_capability()invpci_cleanup_capabilities().
Add assertion invpci_reinit_ext_capabilities().
v4: Make sure ->cleanup() and ->init() are invoked.
v3: New.xen/arch/x86/physdev.c +11
-0@@ -8,6 +8,8 @@#include <xen/guest_access.h> #include <xen/iocap.h> #include <xen/serial.h> +#include <xen/vpci.h> +#include <asm/current.h> #include <asm/io_apic.h> #include <asm/msi.h> @@ -169,8 +171,17 @@ int cf_check physdev_check_pci_extcfg(stASSERT (pdev->seg == info->segment);if ( pdev->bus >= info->start_bus && pdev->bus <= info->end_bus ) + { +int rc; +pci_check_extcfg (pdev); + rc =vpci_reinit_ext_capabilities (pdev); +if ( rc ) +gprintk (XENLOG_ERR ,"%pp(%pd): vPCI extcap reinit failed: %d\n" , + &pdev->sbdf, pdev->domain, rc); + } + return 0; }#endif /* COMPAT */ xen/drivers/vpci/cap.c +8
-5@@ -285,13 +285,16 @@ static int vpci_init_ext_capability_listreturn 0 ; } -int vpci_init_capabilities (struct pci_dev *pdev) +int vpci_init_capabilities (struct pci_dev *pdev,bool ext_only) {int rc; - rc =vpci_init_capability_list (pdev); -if ( rc ) -return rc; +if ( !ext_only ) + { + rc =vpci_init_capability_list (pdev); +if ( rc ) +return rc; + } rc =vpci_init_ext_capability_list (pdev);if ( rc )@@ -305,7 +308,7 @@ int vpci_init_capabilities(struct pci_deunsigned int pos =0 ;if ( !is_ext ) - pos =pci_find_cap_offset (pdev->sbdf, cap); + pos = !ext_only ?pci_find_cap_offset (pdev->sbdf, cap) :0 ;else if (is_hardware_domain (pdev->domain) ) pos =pci_find_ext_capability (pdev, cap);
xen/drivers/vpci/cap.c int vpci_init_capabilities(struct pci_de · line 356
Quoted 6 lines
xen/drivers/vpci/cap.c +1
-1@@ -349,22 +352,23 @@ int vpci_init_capabilities(struct pci_dereturn 0 ; } -void vpci_cleanup_capabilities (struct pci_dev *pdev) +void vpci_cleanup_capabilities (struct pci_dev *pdev,bool ext_only) {
You could short-circuit the function here, ie:
if ( ext_only && !is_hardware_domain(pdev->domain) )
return;
But I'm not sure that would simplify the code of the function much? Likewise for vpci_init_capabilities().
Quoted 11 lines
xen/drivers/vpci/cap.c +0
-3for (unsigned int i =0 ; i <NUM_VPCI_INIT ; i++ ) {const vpci_capability_t *capability = &__start_vpci_array[i];const unsigned int cap = capability->id; -unsigned int pos =0 ;if ( !capability->cleanup )continue ; -if ( !capability->is_ext ) - pos =pci_find_cap_offset (pdev->sbdf, cap);
xen/drivers/vpci/cap.c int vpci_init_capabilities(struct pci_de · line 371
Quoted 10 lines
xen/drivers/vpci/cap.c +7
-3-else if (is_hardware_domain (pdev->domain) ) - pos =pci_find_ext_capability (pdev, cap); -if ( pos ) +/* +* Cannot call pci_find_ext_capability() here, as extended config +* space may (no longer) be accessible. +*/ +if ( capability->is_ext + ?is_hardware_domain (pdev->domain) + : !ext_only &&pci_find_cap_offset (pdev->sbdf, cap) )
Given the changes you have done to the reBAR cleanup function, we could even call capability->cleanup() on domUs, as the handler has to deal with uninitialized capabilities either way?
Quoted 9 lines
xen/drivers/vpci/cap.c +2
-0{int rc = capability->cleanup (pdev, false);@@ -376,6 +380,28 @@ void vpci_cleanup_capabilities(struct pc } } +int vpci_reinit_ext_capabilities (struct pci_dev *pdev) +{
xen/drivers/vpci/cap.c void vpci_cleanup_capabilities(struct pc · line 394
Quoted 10 lines
xen/drivers/vpci/cap.c +10
-0+if ( !pdev->vpci ) +return 0 ; + +/* +* FIXME: DomU support is missing. For already running domains we may +* need to pause them around the entire re-evaluation of extended config +* space accessibility. +*/ +if ( pdev->domain ) +ASSERT (pdev->domain == hardware_domain || pdev->domain == dom_io);
Is this to cope around races? I don't think it's a valid state to have pdev->vpci != NULL and pdev->domain == NULL?
Neither you can have pdev->domain == dom_io and pdev->vpci != NULL?
xen/drivers/vpci/cap.c void vpci_cleanup_capabilities(struct pc · line 400
Quoted 6 lines
xen/drivers/vpci/cap.c +6
-0+ +vpci_cleanup_capabilities (pdev, true); + +if (vpci_remove_registers (pdev->vpci,PCI_CFG_SPACE_SIZE , +PCI_CFG_SPACE_EXP_SIZE -PCI_CFG_SPACE_SIZE ) ) +ASSERT_UNREACHABLE ();
Ideally this would better be done the other way around. We first remove the handlers, and the cleanup the capabilities. Just to ensure no stray handler could end up having cached references to data that's been freed by vpci_cleanup_capabilities().
And we should take the write_lock(&pdev->domain->pci_lock).
Thanks, Roger.
Re: [PATCH v5 5/5] vPCI: re-init extended-capabilities when MMCFG availability changed
Quoted 28 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
When Dom0 informs us about MMCFG usability, this may change whether extended capabilities are available (accessible) for devices. Zap what might be on record, and re-initialize things.
No synchronization is added for the case where devices may already be in use. That'll need sorting when (a) DomU support was added and (b) DomU-s may run already while Dom0 / hwdom still boots (dom0less, Hyperlaunch).
vpci_cleanup_capabilities()also shouldn't have usedpci_find_ext_capability(), as already when the function was introduced extended config space may not have been (properly) accessible anymore, no matter whether it was during init. Extended capability cleanup hooks need to cope with being called when the respective capability doesn't exist (and hence the corresponding ->init() hook was never called).Fixes 70e6dace747e ("vpci: Use cleanup to free capability resource during deassign")Signed-off-by Jan Beulich <[email protected]>Quoted 9 lines
---
vpci_reinit_ext_capabilities()'es return value is checked only to log an error; it doesn't feel quite right to fail the hypercall because of this. Roger brought up the idea of de-assigning the device in such a case, but if a driver doesn't use extended capabilities the device would likely continue to work fine, for Dom0 this probably wouldn't be quite right anyway, and it's also unclear whether callingdeassign_device()could be done from this context. Something like whatpci_check_disable_device()does may be an option, if we really think we need to "break" the device.
Quoted 4 lines
We may want to add a note there, stating that we have considered all possible options, and hiding the capability and hoping the owner domain would continue to work as expected seems the less bad of all of them?
I'll see what I can do.
Quoted 8 lines
Quoted 3 lines
The use of
is_hardware_domain()invpci_cleanup_capabilities()was uncommented and hence is left so. Shouldn't there be a DomU-related TODO or FIXME?Hm, yes, possibly. I think limiting extended space availability to the hardware domain only has been done "just" because we have no extended capabilities to expose to domUs so far, and I don't think we even setup the extended capability list in the domU case.
Considering how many things there are to be done for DomU support, I think it would help if every place where e.g. is_hardware_domain() is used only as surrogate would be properly annotated. Or perhaps properly named predicates could be introduced right away, so one can actually go hunt for all of them. Then again is_hardware_domain() is also something one can go hunt for.
Quoted 10 lines
xen/drivers/vpci/cap.c int vpci_init_capabilities(struct pci_de · line 356
Quoted 7 lines
xen/drivers/vpci/cap.c +1
-1@@ -349,22 +352,23 @@ int vpci_init_capabilities(struct pci_dereturn 0 ; } -void vpci_cleanup_capabilities (struct pci_dev *pdev) +void vpci_cleanup_capabilities (struct pci_dev *pdev,bool ext_only) {You could short-circuit the function here, ie:
Quoted 5 lines
if (
ext_only&& !is_hardware_domain(pdev->domain) )return;But I'm not sure that would simplify the code of the function much? Likewise for
vpci_init_capabilities().
Such a short-circuit would need replacing / dropping once DomU support is added. I was hoping the chosen arrangement would make for a little less churn at that time. I'll listen to your advice, though, just that the question gives the impression you're not quite sure either.
Quoted 22 lines
Quoted 11 lines
xen/drivers/vpci/cap.c +0
-3for (unsigned int i =0 ; i <NUM_VPCI_INIT ; i++ ) {const vpci_capability_t *capability = &__start_vpci_array[i];const unsigned int cap = capability->id; -unsigned int pos =0 ;if ( !capability->cleanup )continue ; -if ( !capability->is_ext ) - pos =pci_find_cap_offset (pdev->sbdf, cap);xen/drivers/vpci/cap.c int vpci_init_capabilities(struct pci_de · line 371
Quoted 10 lines
xen/drivers/vpci/cap.c +7
-3-else if (is_hardware_domain (pdev->domain) ) - pos =pci_find_ext_capability (pdev, cap); -if ( pos ) +/* +* Cannot call pci_find_ext_capability() here, as extended config +* space may (no longer) be accessible. +*/ +if ( capability->is_ext + ?is_hardware_domain (pdev->domain) + : !ext_only &&pci_find_cap_offset (pdev->sbdf, cap) )
Quoted 3 lines
Given the changes you have done to the reBAR cleanup function, we could even call
capability->cleanup()on domUs, as the handler has to deal with uninitialized capabilities either way?
Hmm, yes, looks like we could.
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0
-0@@ -376,6 +380,28 @@ void vpci_cleanup_capabilities(struct pc } }
Quoted 15 lines
xen/drivers/vpci/cap.c void vpci_cleanup_capabilities(struct pc · line 394
Quoted 12 lines
xen/drivers/vpci/cap.c +12
-0+int vpci_reinit_ext_capabilities (struct pci_dev *pdev) +{ +if ( !pdev->vpci ) +return 0 ; + +/* +* FIXME: DomU support is missing. For already running domains we may +* need to pause them around the entire re-evaluation of extended config +* space accessibility. +*/ +if ( pdev->domain ) +ASSERT (pdev->domain == hardware_domain || pdev->domain == dom_io);Is this to cope around races? I don't think it's a valid state to have
pdev->vpci!= NULL andpdev->domain== NULL?
Knowing that I had seen pdev-s with NULL domains, I'm perhaps overly cautious. Yes, ->vpci being non-NULL ought to demand a proper owner.
Quoted 1 line
Neither you can have
pdev->domain==dom_ioandpdev->vpci!= NULL?
Same here, looks like I went too far.
Quoted 11 lines
xen/drivers/vpci/cap.c void vpci_cleanup_capabilities(struct pc · line 400
Quoted 6 lines
xen/drivers/vpci/cap.c +6
-0+ +vpci_cleanup_capabilities (pdev, true); + +if (vpci_remove_registers (pdev->vpci,PCI_CFG_SPACE_SIZE , +PCI_CFG_SPACE_EXP_SIZE -PCI_CFG_SPACE_SIZE ) ) +ASSERT_UNREACHABLE ();Ideally this would better be done the other way around. We first remove the handlers, and the cleanup the capabilities. Just to ensure no stray handler could end up having cached references to data that's been freed by
vpci_cleanup_capabilities().
And maybe not just that: For the hwdom case cleanup_rebar() adds new handlers, which we'd wrongly purge again right away. (Because we pass "false" for "hide", this isn't an active issue right now.)
Quoted 1 line
And we should take the
write_lock(&pdev->domain->pci_lock).
Now this is a request that I'm struggling with some. I can see that callers of vpci_{init,cleanup}_capabilities() assert that the lock is being held, yet it's not quite clear to me why that's needed. Shouldn't vPCI internals all synchronize on the vPCI lock of the domain?
Jan
Re: [PATCH v5 5/5] vPCI: re-init extended-capabilities when MMCFG availability changed
Quoted 45 lines · Jan Beulich
Quoted 28 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
When Dom0 informs us about MMCFG usability, this may change whether extended capabilities are available (accessible) for devices. Zap what might be on record, and re-initialize things.
No synchronization is added for the case where devices may already be in use. That'll need sorting when (a) DomU support was added and (b) DomU-s may run already while Dom0 / hwdom still boots (dom0less, Hyperlaunch).
vpci_cleanup_capabilities()also shouldn't have usedpci_find_ext_capability(), as already when the function was introduced extended config space may not have been (properly) accessible anymore, no matter whether it was during init. Extended capability cleanup hooks need to cope with being called when the respective capability doesn't exist (and hence the corresponding ->init() hook was never called).Fixes 70e6dace747e ("vpci: Use cleanup to free capability resource during deassign")Signed-off-by Jan Beulich <[email protected]>Quoted 9 lines
---
vpci_reinit_ext_capabilities()'es return value is checked only to log an error; it doesn't feel quite right to fail the hypercall because of this. Roger brought up the idea of de-assigning the device in such a case, but if a driver doesn't use extended capabilities the device would likely continue to work fine, for Dom0 this probably wouldn't be quite right anyway, and it's also unclear whether callingdeassign_device()could be done from this context. Something like whatpci_check_disable_device()does may be an option, if we really think we need to "break" the device.Quoted 4 lines
We may want to add a note there, stating that we have considered all possible options, and hiding the capability and hoping the owner domain would continue to work as expected seems the less bad of all of them?
I'll see what I can do.
Quoted 8 lines
Quoted 3 lines
The use of
is_hardware_domain()invpci_cleanup_capabilities()was uncommented and hence is left so. Shouldn't there be a DomU-related TODO or FIXME?Hm, yes, possibly. I think limiting extended space availability to the hardware domain only has been done "just" because we have no extended capabilities to expose to domUs so far, and I don't think we even setup the extended capability list in the domU case.
Quoted 6 lines
Considering how many things there are to be done for DomU support, I think it would help if every place where e.g.
is_hardware_domain()is used only as surrogate would be properly annotated. Or perhaps properly named predicates could be introduced right away, so one can actually go hunt for all of them. Then againis_hardware_domain()is also something one can go hunt for.
I would mind having more concrete predicates, even if right now they are some kind of dummy checks. However it might be difficult to formulate those know without having the full picture of what domU support requires.
Quoted 13 lines
Quoted 13 lines
xen/drivers/vpci/cap.c int vpci_init_capabilities(struct pci_de · line 356
Quoted 7 lines
xen/drivers/vpci/cap.c +1
-1@@ -349,22 +352,23 @@ int vpci_init_capabilities(struct pci_dereturn 0 ; } -void vpci_cleanup_capabilities (struct pci_dev *pdev) +void vpci_cleanup_capabilities (struct pci_dev *pdev,bool ext_only) {You could short-circuit the function here, ie:
if (
ext_only&& !is_hardware_domain(pdev->domain) )return;
Quoted 7 lines
Quoted 2 lines
But I'm not sure that would simplify the code of the function much? Likewise for
vpci_init_capabilities().Such a short-circuit would need replacing / dropping once DomU support is added. I was hoping the chosen arrangement would make for a little less churn at that time. I'll listen to your advice, though, just that the question gives the impression you're not quite sure either.
Yeah, I wasn't fully sure. IT would be nice if we could add those short circuits now, and then once domU support is in place we just remove teh shortcuts and it works for domU also. But I fear more changes will be needed anyway, at which point the short-circuit is not that attractive to use.
Quoted 16 lines
Quoted 11 lines
xen/drivers/vpci/cap.c void vpci_cleanup_capabilities(struct pc · line 400
Quoted 6 lines
xen/drivers/vpci/cap.c +6
-0+ +vpci_cleanup_capabilities (pdev, true); + +if (vpci_remove_registers (pdev->vpci,PCI_CFG_SPACE_SIZE , +PCI_CFG_SPACE_EXP_SIZE -PCI_CFG_SPACE_SIZE ) ) +ASSERT_UNREACHABLE ();Ideally this would better be done the other way around. We first remove the handlers, and the cleanup the capabilities. Just to ensure no stray handler could end up having cached references to data that's been freed by
vpci_cleanup_capabilities().And maybe not just that: For the hwdom case
cleanup_rebar()adds new handlers, which we'd wrongly purge again right away. (Because we pass "false" for "hide", this isn't an active issue right now.)
Quoted 6 lines
Quoted 1 line
And we should take the
write_lock(&pdev->domain->pci_lock).Now this is a request that I'm struggling with some. I can see that callers of vpci_{init,cleanup}_capabilities() assert that the lock is being held, yet it's not quite clear to me why that's needed. Shouldn't vPCI internals all synchronize on the vPCI lock of the domain?
Right, the callers of the handlers already hold the locks, and the removal of the handlers should also hold the locks. The point of taking the d->pci_lock is to avoid the device from being removed while there are vPCI accesses against it being done. The vPCI lock is fine for vPCI internals, but functions that deal with addition or removal of devices need the d->pci_lock to avoid races with possibly freeing pdev->vpci while in use.
I think you are right, and for the usage here (that doesn't add or remove pdev->vpci itself), the internal vPCI lock should be enough.
Thanks, Roger.
Re: [PATCH v5 5/5] vPCI: re-init extended-capabilities when MMCFG availability changed
Quoted 24 lines · Roger Pau Monné
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monné
xen/drivers/vpci/cap.c int vpci_init_capabilities(struct pci_de · line 356
Quoted 7 lines · Jan Beulich
xen/drivers/vpci/cap.c +1
-1@@ -349,22 +352,23 @@ int vpci_init_capabilities(struct pci_dereturn 0 ; } -void vpci_cleanup_capabilities (struct pci_dev *pdev) +void vpci_cleanup_capabilities (struct pci_dev *pdev,bool ext_only) {You could short-circuit the function here, ie:
if (
ext_only&& !is_hardware_domain(pdev->domain) )return;Quoted 7 lines
Quoted 2 lines
But I'm not sure that would simplify the code of the function much? Likewise for
vpci_init_capabilities().Such a short-circuit would need replacing / dropping once DomU support is added. I was hoping the chosen arrangement would make for a little less churn at that time. I'll listen to your advice, though, just that the question gives the impression you're not quite sure either.
Quoted 5 lines
Yeah, I wasn't fully sure. IT would be nice if we could add those short circuits now, and then once domU support is in place we just remove teh shortcuts and it works for domU also. But I fear more changes will be needed anyway, at which point the short-circuit is not that attractive to use.
As per your other request (calling ->cleanup() even for DomU-s) the use of is_hardware_domain() would go away anyway, and the function would be ready for use for DomU-s as well.
Quoted 31 lines
Quoted 16 lines
Quoted 11 lines
xen/drivers/vpci/cap.c void vpci_cleanup_capabilities(struct pc · line 400
Quoted 6 lines
xen/drivers/vpci/cap.c +6
-0+ +vpci_cleanup_capabilities (pdev, true); + +if (vpci_remove_registers (pdev->vpci,PCI_CFG_SPACE_SIZE , +PCI_CFG_SPACE_EXP_SIZE -PCI_CFG_SPACE_SIZE ) ) +ASSERT_UNREACHABLE ();Ideally this would better be done the other way around. We first remove the handlers, and the cleanup the capabilities. Just to ensure no stray handler could end up having cached references to data that's been freed by
vpci_cleanup_capabilities().And maybe not just that: For the hwdom case
cleanup_rebar()adds new handlers, which we'd wrongly purge again right away. (Because we pass "false" for "hide", this isn't an active issue right now.)Quoted 6 lines
Quoted 1 line
And we should take the
write_lock(&pdev->domain->pci_lock).Now this is a request that I'm struggling with some. I can see that callers of vpci_{init,cleanup}_capabilities() assert that the lock is being held, yet it's not quite clear to me why that's needed. Shouldn't vPCI internals all synchronize on the vPCI lock of the domain?
Right, the callers of the handlers already hold the locks, and the removal of the handlers should also hold the locks. The point of taking the
d->pci_lockis to avoid the device from being removed while there are vPCI accesses against it being done. The vPCI lock is fine for vPCI internals, but functions that deal with addition or removal of devices need thed->pci_lockto avoid races with possibly freeingpdev->vpciwhile in use.
Quoted 2 lines
I think you are right, and for the usage here (that doesn't add or remove
pdev->vpciitself), the internal vPCI lock should be enough.
Well, we could take two positions: Either we say that as we're being called from a context where the PCI device is being operated on anyway, we can assume it can't go away. Then no further locking would be needed here. Or we want to explicitly guard against that, in which case (seeing that nothing is added / removed), d->pci_lock may want read-locking?
Jan
Re: [PATCH v5 5/5] vPCI: re-init extended-capabilities when MMCFG availability changed
Quoted 31 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monné
xen/drivers/vpci/cap.c int vpci_init_capabilities(struct pci_de · line 356
Quoted 7 lines · Jan Beulich
xen/drivers/vpci/cap.c +1
-1@@ -349,22 +352,23 @@ int vpci_init_capabilities(struct pci_dereturn 0 ; } -void vpci_cleanup_capabilities (struct pci_dev *pdev) +void vpci_cleanup_capabilities (struct pci_dev *pdev,bool ext_only) {You could short-circuit the function here, ie:
if (
ext_only&& !is_hardware_domain(pdev->domain) )return;Quoted 7 lines
Quoted 2 lines
But I'm not sure that would simplify the code of the function much? Likewise for
vpci_init_capabilities().Such a short-circuit would need replacing / dropping once DomU support is added. I was hoping the chosen arrangement would make for a little less churn at that time. I'll listen to your advice, though, just that the question gives the impression you're not quite sure either.
Quoted 5 lines
Yeah, I wasn't fully sure. IT would be nice if we could add those short circuits now, and then once domU support is in place we just remove teh shortcuts and it works for domU also. But I fear more changes will be needed anyway, at which point the short-circuit is not that attractive to use.
Quoted 3 lines
As per your other request (calling ->cleanup() even for DomU-s) the use of
is_hardware_domain()would go away anyway, and the function would be ready for use for DomU-s as well.
OK, so that one is (possibly) sorted then.
Quoted 31 lines
Quoted 31 lines
Quoted 16 lines
Quoted 11 lines
xen/drivers/vpci/cap.c void vpci_cleanup_capabilities(struct pc · line 400
Quoted 6 lines
xen/drivers/vpci/cap.c +6
-0+ +vpci_cleanup_capabilities (pdev, true); + +if (vpci_remove_registers (pdev->vpci,PCI_CFG_SPACE_SIZE , +PCI_CFG_SPACE_EXP_SIZE -PCI_CFG_SPACE_SIZE ) ) +ASSERT_UNREACHABLE ();Ideally this would better be done the other way around. We first remove the handlers, and the cleanup the capabilities. Just to ensure no stray handler could end up having cached references to data that's been freed by
vpci_cleanup_capabilities().And maybe not just that: For the hwdom case
cleanup_rebar()adds new handlers, which we'd wrongly purge again right away. (Because we pass "false" for "hide", this isn't an active issue right now.)Quoted 6 lines
Quoted 1 line
And we should take the
write_lock(&pdev->domain->pci_lock).Now this is a request that I'm struggling with some. I can see that callers of vpci_{init,cleanup}_capabilities() assert that the lock is being held, yet it's not quite clear to me why that's needed. Shouldn't vPCI internals all synchronize on the vPCI lock of the domain?
Right, the callers of the handlers already hold the locks, and the removal of the handlers should also hold the locks. The point of taking the
d->pci_lockis to avoid the device from being removed while there are vPCI accesses against it being done. The vPCI lock is fine for vPCI internals, but functions that deal with addition or removal of devices need thed->pci_lockto avoid races with possibly freeingpdev->vpciwhile in use.
Quoted 8 lines
Quoted 2 lines
I think you are right, and for the usage here (that doesn't add or remove
pdev->vpciitself), the internal vPCI lock should be enough.Well, we could take two positions: Either we say that as we're being called from a context where the PCI device is being operated on anyway, we can assume it can't go away. Then no further locking would be needed here. Or we want to explicitly guard against that, in which case (seeing that nothing is added / removed),
d->pci_lockmay want read-locking?
In this context the caller is already holding the pcidevs lock, so there's no risk of the device being de-assigned.
My reasoning for taking the d->pci_lock in write mode was to prevent any concurrent access to vPCI while the changes to the emulated config space is taken place, but you are right that the vPCI lock if used properly should prevent races.
Thanks, Roger.
Re: [PATCH v5 5/5] vPCI: re-init extended-capabilities when MMCFG availability changed
In reply to Roger Pau MonnéQuoted 28 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
When Dom0 informs us about MMCFG usability, this may change whether extended capabilities are available (accessible) for devices. Zap what might be on record, and re-initialize things.
No synchronization is added for the case where devices may already be in use. That'll need sorting when (a) DomU support was added and (b) DomU-s may run already while Dom0 / hwdom still boots (dom0less, Hyperlaunch).
vpci_cleanup_capabilities()also shouldn't have usedpci_find_ext_capability(), as already when the function was introduced extended config space may not have been (properly) accessible anymore, no matter whether it was during init. Extended capability cleanup hooks need to cope with being called when the respective capability doesn't exist (and hence the corresponding ->init() hook was never called).Fixes 70e6dace747e ("vpci: Use cleanup to free capability resource during deassign")Signed-off-by Jan Beulich <[email protected]>Quoted 9 lines
---
vpci_reinit_ext_capabilities()'es return value is checked only to log an error; it doesn't feel quite right to fail the hypercall because of this. Roger brought up the idea of de-assigning the device in such a case, but if a driver doesn't use extended capabilities the device would likely continue to work fine, for Dom0 this probably wouldn't be quite right anyway, and it's also unclear whether callingdeassign_device()could be done from this context. Something like whatpci_check_disable_device()does may be an option, if we really think we need to "break" the device.
Quoted 4 lines
We may want to add a note there, stating that we have considered all possible options, and hiding the capability and hoping the owner domain would continue to work as expected seems the less bad of all of them?
While adding that note it occurred to me that in order to keep the device as functioning as possible, in the re-init case vpci_init_capabilities() might better not bail upon encountering a failure, but accumulate the error while continuing its loop in a best-effort manner. Thoughts? (One of the two return-s is already guarded by !is_hardware_domain(), so that could be left alone for the immediate purpose.)
Jan
Re: [PATCH v5 5/5] vPCI: re-init extended-capabilities when MMCFG availability changed
Quoted 34 lines · Jan Beulich
Quoted 28 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
When Dom0 informs us about MMCFG usability, this may change whether extended capabilities are available (accessible) for devices. Zap what might be on record, and re-initialize things.
No synchronization is added for the case where devices may already be in use. That'll need sorting when (a) DomU support was added and (b) DomU-s may run already while Dom0 / hwdom still boots (dom0less, Hyperlaunch).
vpci_cleanup_capabilities()also shouldn't have usedpci_find_ext_capability(), as already when the function was introduced extended config space may not have been (properly) accessible anymore, no matter whether it was during init. Extended capability cleanup hooks need to cope with being called when the respective capability doesn't exist (and hence the corresponding ->init() hook was never called).Fixes 70e6dace747e ("vpci: Use cleanup to free capability resource during deassign")Signed-off-by Jan Beulich <[email protected]>Quoted 9 lines
---
vpci_reinit_ext_capabilities()'es return value is checked only to log an error; it doesn't feel quite right to fail the hypercall because of this. Roger brought up the idea of de-assigning the device in such a case, but if a driver doesn't use extended capabilities the device would likely continue to work fine, for Dom0 this probably wouldn't be quite right anyway, and it's also unclear whether callingdeassign_device()could be done from this context. Something like whatpci_check_disable_device()does may be an option, if we really think we need to "break" the device.Quoted 4 lines
We may want to add a note there, stating that we have considered all possible options, and hiding the capability and hoping the owner domain would continue to work as expected seems the less bad of all of them?
Quoted 6 lines
While adding that note it occurred to me that in order to keep the device as functioning as possible, in the re-init case
vpci_init_capabilities()might better not bail upon encountering a failure, but accumulate the error while continuing its loop in a best-effort manner. Thoughts? (One of the two return-s is already guarded by !is_hardware_domain(), so that could be left alone for the immediate purpose.)
Right, yes, that would be preferable. We already print a message for the failed to init capabilities, so there's no need to print another one in the caller.
Thanks, Roger.
Re: [PATCH v5 5/5] vPCI: re-init extended-capabilities when MMCFG availability changed
Quoted 42 lines · Roger Pau Monné
Quoted 34 lines · Jan Beulich
Quoted 28 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
When Dom0 informs us about MMCFG usability, this may change whether extended capabilities are available (accessible) for devices. Zap what might be on record, and re-initialize things.
No synchronization is added for the case where devices may already be in use. That'll need sorting when (a) DomU support was added and (b) DomU-s may run already while Dom0 / hwdom still boots (dom0less, Hyperlaunch).
vpci_cleanup_capabilities()also shouldn't have usedpci_find_ext_capability(), as already when the function was introduced extended config space may not have been (properly) accessible anymore, no matter whether it was during init. Extended capability cleanup hooks need to cope with being called when the respective capability doesn't exist (and hence the corresponding ->init() hook was never called).Fixes 70e6dace747e ("vpci: Use cleanup to free capability resource during deassign")Signed-off-by Jan Beulich <[email protected]>Quoted 9 lines
---
vpci_reinit_ext_capabilities()'es return value is checked only to log an error; it doesn't feel quite right to fail the hypercall because of this. Roger brought up the idea of de-assigning the device in such a case, but if a driver doesn't use extended capabilities the device would likely continue to work fine, for Dom0 this probably wouldn't be quite right anyway, and it's also unclear whether callingdeassign_device()could be done from this context. Something like whatpci_check_disable_device()does may be an option, if we really think we need to "break" the device.Quoted 4 lines
We may want to add a note there, stating that we have considered all possible options, and hiding the capability and hoping the owner domain would continue to work as expected seems the less bad of all of them?
Quoted 6 lines
While adding that note it occurred to me that in order to keep the device as functioning as possible, in the re-init case
vpci_init_capabilities()might better not bail upon encountering a failure, but accumulate the error while continuing its loop in a best-effort manner. Thoughts? (One of the two return-s is already guarded by !is_hardware_domain(), so that could be left alone for the immediate purpose.)
Quoted 3 lines
Right, yes, that would be preferable. We already print a message for the failed to init capabilities, so there's no need to print another one in the caller.
Hmm, that's another aspect I didn't consider. Yes, the log message in the caller is redundant with the present code structure. If we expect that to remain like that, I can drop logging anything from physdev_check_pci_extcfg(). Which then re-raises the question whether vpci_reinit_ext_capabilities() might better return void. At which point the comment I put in physdev_check_pci_extcfg() (upon your request) would want to move there.
But my earlier question went in a different direction, and you didn't comment on that at all.
Jan
Re: [PATCH v5 5/5] vPCI: re-init extended-capabilities when MMCFG availability changed
Quoted 55 lines · Jan Beulich
Quoted 42 lines · Roger Pau Monné
Quoted 34 lines · Jan Beulich
Quoted 28 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
When Dom0 informs us about MMCFG usability, this may change whether extended capabilities are available (accessible) for devices. Zap what might be on record, and re-initialize things.
No synchronization is added for the case where devices may already be in use. That'll need sorting when (a) DomU support was added and (b) DomU-s may run already while Dom0 / hwdom still boots (dom0less, Hyperlaunch).
vpci_cleanup_capabilities()also shouldn't have usedpci_find_ext_capability(), as already when the function was introduced extended config space may not have been (properly) accessible anymore, no matter whether it was during init. Extended capability cleanup hooks need to cope with being called when the respective capability doesn't exist (and hence the corresponding ->init() hook was never called).Fixes 70e6dace747e ("vpci: Use cleanup to free capability resource during deassign")Signed-off-by Jan Beulich <[email protected]>Quoted 9 lines
---
vpci_reinit_ext_capabilities()'es return value is checked only to log an error; it doesn't feel quite right to fail the hypercall because of this. Roger brought up the idea of de-assigning the device in such a case, but if a driver doesn't use extended capabilities the device would likely continue to work fine, for Dom0 this probably wouldn't be quite right anyway, and it's also unclear whether callingdeassign_device()could be done from this context. Something like whatpci_check_disable_device()does may be an option, if we really think we need to "break" the device.Quoted 4 lines
We may want to add a note there, stating that we have considered all possible options, and hiding the capability and hoping the owner domain would continue to work as expected seems the less bad of all of them?
Quoted 6 lines
While adding that note it occurred to me that in order to keep the device as functioning as possible, in the re-init case
vpci_init_capabilities()might better not bail upon encountering a failure, but accumulate the error while continuing its loop in a best-effort manner. Thoughts? (One of the two return-s is already guarded by !is_hardware_domain(), so that could be left alone for the immediate purpose.)Quoted 3 lines
Right, yes, that would be preferable. We already print a message for the failed to init capabilities, so there's no need to print another one in the caller.
Hmm, that's another aspect I didn't consider. Yes, the log message in the caller is redundant with the present code structure. If we expect that to remain like that, I can drop logging anything from
physdev_check_pci_extcfg(). Which then re-raises the question whethervpci_reinit_ext_capabilities()might better return void. At which point the comment I put inphysdev_check_pci_extcfg()(upon your request) would want to move there.
Quoted 2 lines
But my earlier question went in a different direction, and you didn't comment on that at all.
Yes, I think we should accumulate errors. One device failing doesn't mean the rest will also fail. We should continue the loop.
Thanks, Roger.