PCI/TSM: Core infrastructure for PCI device security (TDISP)
Subsystems (8)
v3 cover PCI/TSM: Core infrastructure for PCI device security (TDISP)
Changes since v2 [1]:
- drivers/virt/coco/guest rename merged to tsm.git#next
- Clarify usage and requirements for pci_ide_init_nr_streams() (Dionna)
- Misc fixups (Dionna)
- Fix sel_ide_offset() to incorporate ide_cap (Aneesh, Yilun)
- Allow at least 1 stream when enforcing uniform address association register layout (Yilun)
- Fix host-bridge-emulation for PCI_DOMAINS_GENERIC platform (Suzuki)
- Export pci_ide_to_settings() as a helper for TSM drivers (Yilun)
- Set Stream ID early, prior to IDE_KM (Alexey)
- Catch IDE_KM initial setup failures with pci_ide_stream_enable() errors (Yilun).
- Fix missing initialization of nr_link_ide (caught by samples/devsec/bus test)
- Add some reference documentation to the devsec_tsm_connect() sample operation to clarify implementation expectations (Zhi)
- Expand the possible Device Security Managers from only PF0 of a device hosting TDIs, to include Upstream Ports with downstream endpoints as TDIs
- Add bind, unbind, guest_req, and accept operations (Yilun)
[1]: http://lore.kernel.org/174107245357.1288555.10863541957822891561.stgit@dwillia2-xfh.jf.intel.com
Launch of tsm.git#staging [2]
-----------------------------
As mentioned on v2, tsm.git#staging is proposed as a neutral location to collect device-security infrastructure from multiple vendors. I.e. collect all the vendor trees to resolve conflicts, code or otherwise. For now it does not contain kvm-coco-queue, but am open to merging that if needed for some device-security-flows.
Yilun showed a potential flow for the end-to-end API changes here [1], do review that and point out where it may not work for a different architecture. A goal of mine is to catch sample/devsec/ up with that diagram to prove out and unit test the end-to-end mechanism without needing hardware. It has already found bugs while revising this new set.
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/devsec/tsm.git/log/?h=staging
[3]: http://lore.kernel.org/aCYsNSFQJZzHVOFI@yilunxu-OptiPlex-7050
Original Cover letter:
----------------------
Trusted execution environment (TEE) Device Interface Security Protocol (TDISP) is a chapter name in the PCI specification. It describes an alphabet soup of mechanisms, SPDM, CMA, IDE, TSM/DSM, that system software uses to establish trust in a device and assign it to a confidential virtual machine (CVM). It is protocol for dynamically extending the trusted computing boundary (TCB) of a CVM with a PCI device interface that can issue DMA to CVM private memory.
The acronym soup problem is enhanced by every major platform vendor having distinct TEE Security Manager (TSM) API implementations / capabilities, and to a lesser extent, every potential endpoint Device Security Manager (DSM) having its own idiosyncratic behaviors around TDISP state transitions.
Despite all that opportunity for differentiation, there is a significant portion of the implementation that is cross-vendor common. However, it is difficult to develop, debate, test and settle all those pieces absent a low level TSM driver implementation to pull it all together.
The proposal is incrementally develop the shared infrastructure on top of a sample TSM driver implementation to enable clean vendor agnostic discussions about the commons. "samples/devsec/" is meant to be: just enough emulation to exercise all the core infrastructure, a reference implementation, and a simple unit test. The sample also enables coordination with the native PCI device security effort [4].
[4]: http://lore.kernel.org/[email protected]
Dan Williams (11):
coco/tsm: Introduce a core device for TEE Security Managers PCI/IDE: Enumerate Selective Stream IDE capabilities PCI/TSM: Authenticate devices via platform TSM
PCI: Enable host-bridge emulation for PCI_DOMAINS_GENERIC platforms
PCI: vmd: Switch to pci_bus_find_emul_domain_nr() samples/devsec: Introduce a PCI device-security bus + endpoint sample
PCI: Add PCIe Device 3 Extended Capability enumeration PCI/IDE: Add IDE establishment helpers
PCI/IDE: Report available IDE streams
PCI/TSM: Report active IDE streams
samples/devsec: Add sample IDE establishment
Xu Yilun (2):
PCI/TSM: support TDI related operations for host TSM driver PCI/TSM: Add Guest TSM Support
Documentation/ABI/testing/sysfs-bus-pci | 45 + Documentation/ABI/testing/sysfs-class-tsm | 20 + .../ABI/testing/sysfs-devices-pci-host-bridge | 51 ++ MAINTAINERS | 7 +- drivers/pci/Kconfig | 28 + drivers/pci/Makefile | 2 + drivers/pci/controller/pci-hyperv.c | 53 +- drivers/pci/controller/vmd.c | 33 +- drivers/pci/ide.c | 525 ++++++++++++ drivers/pci/pci-sysfs.c | 4 + drivers/pci/pci.c | 43 +- drivers/pci/pci.h | 19 + drivers/pci/probe.c | 34 +- drivers/pci/remove.c | 3 + drivers/pci/tsm.c | 782 ++++++++++++++++++ drivers/virt/coco/Kconfig | 2 + drivers/virt/coco/Makefile | 1 + drivers/virt/coco/host/Kconfig | 6 + drivers/virt/coco/host/Makefile | 6 + drivers/virt/coco/host/tsm-core.c | 144 ++++ include/linux/pci-ide.h | 76 ++ include/linux/pci-tsm.h | 211 +++++ include/linux/pci.h | 29 + include/linux/tsm.h | 11 + include/uapi/linux/pci_regs.h | 91 +- samples/Kconfig | 16 + samples/Makefile | 1 + samples/devsec/Makefile | 10 + samples/devsec/bus.c | 711 ++++++++++++++++ samples/devsec/common.c | 26 + samples/devsec/devsec.h | 40 + samples/devsec/tsm.c | 218 +++++ 32 files changed, 3170 insertions(+), 78 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-class-tsm create mode 100644 Documentation/ABI/testing/sysfs-devices-pci-host-bridge create mode 100644 drivers/pci/ide.c create mode 100644 drivers/pci/tsm.c create mode 100644 drivers/virt/coco/host/Kconfig create mode 100644 drivers/virt/coco/host/Makefile create mode 100644 drivers/virt/coco/host/tsm-core.c create mode 100644 include/linux/pci-ide.h create mode 100644 include/linux/pci-tsm.h create mode 100644 samples/devsec/Makefile create mode 100644 samples/devsec/bus.c create mode 100644 samples/devsec/common.c create mode 100644 samples/devsec/devsec.h create mode 100644 samples/devsec/tsm.c
base-commit: 7515f45c165269b72ee739e6fc26cc2ef928fc1b
Signature · 1 line
2.49.0
PATCH v3 1/13 coco/tsm: Introduce a core device for TEE Security Managers
A "TSM" is a platform component that provides an API for securely provisioning resources for a confidential guest (TVM) to consume. The name originates from the PCI specification for platform agent that carries out operations for PCIe TDISP (TEE Device Interface Security Protocol).
Instances of this core device are parented by a device representing the platform security function like CONFIG_CRYPTO_DEV_CCP or CONFIG_INTEL_TDX_HOST.
This device interface is a frontend to the aspects of a TSM and TEE I/O that are cross-architecture common. This includes mechanisms like enumerating available platform TEE I/O capabilities and provisioning connections between the platform TSM and device DSMs (Device Security Manager (TDISP)).
For now this is just the scaffolding for registering a TSM device sysfs interface.
--- Documentation/ABI/testing/sysfs-class-tsm | 10 ++ MAINTAINERS | 3 +- drivers/virt/coco/Kconfig | 2 + drivers/virt/coco/Makefile | 1 + drivers/virt/coco/host/Kconfig | 6 ++ drivers/virt/coco/host/Makefile | 6 ++ drivers/virt/coco/host/tsm-core.c | 112 ++++++++++++++++++++++ include/linux/tsm.h | 5 + 8 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 Documentation/ABI/testing/sysfs-class-tsm create mode 100644 drivers/virt/coco/host/Kconfig create mode 100644 drivers/virt/coco/host/Makefile create mode 100644 drivers/virt/coco/host/tsm-core.c
Documentation/ABI/testing/sysfs-class-tsm new file +10 -0
@@ -0,0 +1,10 @@ +What: /sys/class/tsm/tsm0 +Date: Dec, 2024 +Contact: [email protected] +Description: + "tsm0" is a singleton device that represents the generic + attributes of a platform TEE Security Manager. It is a child of + the platform TSM device. /sys/class/tsm/tsm0/uevent + signals when the PCI layer is able to support establishment of + link encryption and other device-security features coordinated + through the platform tsm.
MAINTAINERS +2 -1
@@ -24555,12 +24555,13 @@ M: David Lechner <[email protected]> S: Maintained F: Documentation/devicetree/bindings/trigger-source/pwm-trigger.yaml -TRUSTED SECURITY MODULE (TSM) ATTESTATION REPORTS +TRUSTED EXECUTION ENVIRONMENT SECURITY MANAGER (TSM) M: Dan Williams <[email protected]> L: [email protected] S: Maintained F: Documentation/ABI/testing/configfs-tsm-report F: drivers/virt/coco/guest/ +F: drivers/virt/coco/host/ F: include/linux/tsm.h TRUSTED SERVICES TEE DRIVER
drivers/virt/coco/Kconfig +2 -0
@@ -14,3 +14,5 @@ source "drivers/virt/coco/tdx-guest/Kconfig" source "drivers/virt/coco/arm-cca-guest/Kconfig" source "drivers/virt/coco/guest/Kconfig" + +source "drivers/virt/coco/host/Kconfig"
drivers/virt/coco/Makefile +1 -0
@@ -8,3 +8,4 @@ obj-$(CONFIG_SEV_GUEST) += sev-guest/ obj-$(CONFIG_INTEL_TDX_GUEST) += tdx-guest/ obj-$(CONFIG_ARM_CCA_GUEST) += arm-cca-guest/ obj-$(CONFIG_TSM_REPORTS) += guest/ +obj-y += host/
drivers/virt/coco/host/Kconfig new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# TSM (TEE Security Manager) Common infrastructure and host drivers +# +config TSM + tristate
drivers/virt/coco/host/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# TSM (TEE Security Manager) Common infrastructure and host drivers + +obj-$(CONFIG_TSM) += tsm.o +tsm-y := tsm-core.o
drivers/virt/coco/host/tsm-core.c new file +112 -0
112 lines not on this page · raw message
include/linux/tsm.h +5 -0
@@ -5,6 +5,7 @@#include <linux/sizes.h> #include <linux/types.h> #include <linux/uuid.h> +#include <linux/device.h> #define TSM_REPORT_INBLOB_MAX 64#define TSM_REPORT_OUTBLOB_MAX SZ_32K
@@ -109,4 +110,8 @@ struct tsm_report_ops {int tsm_report_register (const struct tsm_report_ops *ops,void *priv);int tsm_report_unregister (const struct tsm_report_ops *ops); +struct tsm_core_dev ; +struct tsm_core_dev *tsm_register (struct device *parent, +const struct attribute_group **groups); +void tsm_unregister (struct tsm_core_dev *tsm_core);#endif /* __TSM_H */
Signature · 1 line
2.49.0
Jason Gunthorpe
maintainerRe: [PATCH v3 01/13] coco/tsm: Introduce a core device for TEE Security Managers
drivers/virt/coco/host/tsm-core.c line 16
Quoted 4 lines · Dan Williams
drivers/virt/coco/host/tsm-core.c new file +4
-0+static struct class *tsm_class; +static struct tsm_core_dev { +struct device dev; +} *tsm_core;
This is gross, do we really need to have a global?
Jason
Re: [PATCH v3 01/13] coco/tsm: Introduce a core device for TEE Security Managers
Quoted 7 lines · Jason Gunthorpe
drivers/virt/coco/host/tsm-core.c line 16
Quoted 4 lines · Dan Williams
drivers/virt/coco/host/tsm-core.c new file +4
-0+static struct class *tsm_class; +static struct tsm_core_dev { +struct device dev; +} *tsm_core;This is gross, do we really need to have a global?
Let me restate the assumptions that led to this, because if we disagree there then that is more interesting and may lead to a better solution.
* The "TSM" (TEE Security Manager) concept in the PCIe TDISP specification and, by implication, all the CC arch implementations, instantiate this platform object / agent as a singleton. There is one TDX Module in SEAM mode, one SEV-SNP CCP firmware context, one RISC-V COVE module etc...
* PCIe TDISP is the first of potentially a class of confidential computing platform capabilities that span across platforms.
* There are generally useful details that platform owners want to know, like number of available / in-use PCIe link encryption stream resources, that are suitable to publish in sysfs.
* Userspace is better served by a static /sys/class/tsm/tsm... path to those common attributes vs trawling through arch-specific sysfs paths. E.g. SEV-SNP device object for their "TSM" is on the PCI bus, the TDX Module device object lives on the "virtual" bus etc...
So, create a singleton tsm_core_dev to anchor attributes in that "cross-TSM" class.
Re: [PATCH v3 01/13] coco/tsm: Introduce a core device for TEE Security Managers
Quoted 25 lines · Dan Williams
Quoted 7 lines · Jason Gunthorpe
drivers/virt/coco/host/tsm-core.c line 16
Quoted 4 lines · Dan Williams
drivers/virt/coco/host/tsm-core.c new file +4
-0+static struct class *tsm_class; +static struct tsm_core_dev { +struct device dev; +} *tsm_core;This is gross, do we really need to have a global?
Let me restate the assumptions that led to this, because if we disagree there then that is more interesting and may lead to a better solution.
* The "TSM" (TEE Security Manager) concept in the PCIe TDISP specification and, by implication, all the CC arch implementations, instantiate this platform object / agent as a singleton. There is one TDX Module in SEAM mode, one SEV-SNP CCP firmware context, one RISC-V COVE module etc...
* PCIe TDISP is the first of potentially a class of confidential computing platform capabilities that span across platforms.
* There are generally useful details that platform owners want to know, like number of available / in-use PCIe link encryption stream resources, that are suitable to publish in sysfs.
Quoted 7 lines
* Userspace is better served by a static
/sys/class/tsm/tsm... path to those common attributes vs trawling through arch-specific sysfs paths. E.g. SEV-SNP device object for their "TSM" is on the PCI bus, the TDX Module device object lives on the "virtual" bus etc...So, create a singleton
tsm_core_devto anchor attributes in that "cross-TSM" class.
However, after spelling all that out it occurs to me that a class dev already meets most of that requirement. The name of the class dev does not matter much, and other paths can enforce that there is only one TSM class dev registered at a time.
So userspace could lookup /sys/class/tsm/*/$attribute and as long that is a single result, great. If that ever returns more than one instance then we will have entered some advanced future where there are multiple TSMs per platform.
Jason Gunthorpe
maintainerRe: [PATCH v3 01/13] coco/tsm: Introduce a core device for TEE Security Managers
Quoted 4 lines · Dan Williams
So userspace could lookup
/sys/class/tsm/*/$attributeand as long that is a single result, great. If that ever returns more than one instance then we will have entered some advanced future where there are multiple TSMs per platform.
Yes, exactly
Jason
Jason Gunthorpe
maintainerRe: [PATCH v3 01/13] coco/tsm: Introduce a core device for TEE Security Managers
In reply to Dan WilliamsQuoted 25 lines · Dan Williams
Quoted 7 lines · Jason Gunthorpe
drivers/virt/coco/host/tsm-core.c line 16
Quoted 4 lines · Dan Williams
drivers/virt/coco/host/tsm-core.c new file +4
-0+static struct class *tsm_class; +static struct tsm_core_dev { +struct device dev; +} *tsm_core;This is gross, do we really need to have a global?
Let me restate the assumptions that led to this, because if we disagree there then that is more interesting and may lead to a better solution.
* The "TSM" (TEE Security Manager) concept in the PCIe TDISP specification and, by implication, all the CC arch implementations, instantiate this platform object / agent as a singleton. There is one TDX Module in SEAM mode, one SEV-SNP CCP firmware context, one RISC-V COVE module etc...
* PCIe TDISP is the first of potentially a class of confidential computing platform capabilities that span across platforms.
* There are generally useful details that platform owners want to know, like number of available / in-use PCIe link encryption stream resources, that are suitable to publish in sysfs.
Quoted 7 lines
* Userspace is better served by a static
/sys/class/tsm/tsm... path to those common attributes vs trawling through arch-specific sysfs paths. E.g. SEV-SNP device object for their "TSM" is on the PCI bus, the TDX Module device object lives on the "virtual" bus etc...So, create a singleton
tsm_core_devto anchor attributes in that "cross-TSM" class.
We will be very sad if we need multiple TSMs or TSM flavours (like PCI, CHI, whatever) down the road as single TSM was baked permanently into the uapi.
It is far saner to have paths like /sys/class/tsm0/tsm/.. and remove the global than take the risk that one and only one is the right answer forever for everyone.
Jason
Alexey Kardashevskiy
maintainerRe: [PATCH v3 01/13] coco/tsm: Introduce a core device for TEE Security Managers
Quoted 34 lines · Jason Gunthorpe
Quoted 25 lines · Dan Williams
Quoted 7 lines · Jason Gunthorpe
drivers/virt/coco/host/tsm-core.c line 16
Quoted 4 lines · Dan Williams
drivers/virt/coco/host/tsm-core.c new file +4
-0+static struct class *tsm_class; +static struct tsm_core_dev { +struct device dev; +} *tsm_core;This is gross, do we really need to have a global?
Let me restate the assumptions that led to this, because if we disagree there then that is more interesting and may lead to a better solution.
* The "TSM" (TEE Security Manager) concept in the PCIe TDISP specification and, by implication, all the CC arch implementations, instantiate this platform object / agent as a singleton. There is one TDX Module in SEAM mode, one SEV-SNP CCP firmware context, one RISC-V COVE module etc...
* PCIe TDISP is the first of potentially a class of confidential computing platform capabilities that span across platforms.
* There are generally useful details that platform owners want to know, like number of available / in-use PCIe link encryption stream resources, that are suitable to publish in sysfs.
Quoted 7 lines
* Userspace is better served by a static
/sys/class/tsm/tsm... path to those common attributes vs trawling through arch-specific sysfs paths. E.g. SEV-SNP device object for their "TSM" is on the PCI bus, the TDX Module device object lives on the "virtual" bus etc...So, create a singleton
tsm_core_devto anchor attributes in that "cross-TSM" class.
Quoted 7 lines
We will be very sad if we need multiple TSMs or TSM flavours (like PCI, CHI, whatever) down the road as single TSM was baked permanently into the uapi.
It is far saner to have paths like
/sys/class/tsm0/tsm/.. and remove the global than take the risk that one and only one is the right answer forever for everyone.
In my tree I do just that, ccp (a5:00.5 is it) and sev-guest modules each register themselves in TSM:
aik@purico-ec3dhost ~> ls -la /sys/class/tsm/tsm0
lrwxrwxrwx 1 root root 0 Jun 6 03:29 /sys/class/tsm/tsm0 -> ../../devices/pci0000:a0/0000:a0:07.1/0000:a5:00.5/tsm/tsm0
aik@purico-ec3dhost ~> ssh tvm ls -la /sys/class/tsm/tsm0
lrwxrwxrwx 1 root root 0 Jun 6 13:29 /sys/class/tsm/tsm0 -> ../../devices/platform/sev-guest/tsm/tsm0
No globals anywhere.
Signature · 1 line
Alexey
Alexey Kardashevskiy
maintainerRe: [PATCH v3 01/13] coco/tsm: Introduce a core device for TEE Security Managers
In reply to Jason GunthorpeQuoted 7 lines · Jason Gunthorpe
drivers/virt/coco/host/tsm-core.c line 16
Quoted 4 lines · Dan Williams
drivers/virt/coco/host/tsm-core.c new file +4
-0+static struct class *tsm_class; +static struct tsm_core_dev { +struct device dev; +} *tsm_core;This is gross, do we really need to have a global?
Sure we do not, such pointer happily lives in the CCP driver in my case.
Signature · 1 line
Alexey
PATCH v3 2/13 PCI/IDE: Enumerate Selective Stream IDE capabilities
In reply to Dan WilliamsLink encryption is a new PCIe feature enumerated by "PCIe 6.2 section 7.9.26 IDE Extended Capability".
It is both a standalone port + endpoint capability, and a building block for the security protocol defined by "PCIe 6.2 section 11 TEE Device Interface Security Protocol (TDISP)". That protocol coordinates device security setup between a platform TSM (TEE Security Manager) and a device DSM (Device Security Manager). While the platform TSM can allocate resources like Stream ID and manage keys, it still requires system software to manage the IDE capability register block.
Add register definitions and basic enumeration in preparation for Selective IDE Stream establishment. A follow on change selects the new CONFIG_PCI_IDE symbol. Note that while the IDE specification defines both a point-to-point "Link Stream" and a Root Port to endpoint "Selective Stream", only "Selective Stream" is considered for Linux as that is the predominant mode expected by Trusted Execution Environment Security Managers (TSMs), and it is the security model that limits the number of PCI components within the TCB in a PCIe topology with switches.
--- drivers/pci/Kconfig | 14 +++++ drivers/pci/Makefile | 1 + drivers/pci/ide.c | 100 ++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 6 ++ drivers/pci/probe.c | 1 + include/linux/pci.h | 7 +++ include/uapi/linux/pci_regs.h | 81 ++++++++++++++++++++++++++- 7 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 drivers/pci/ide.c
drivers/pci/Kconfig +14 -0
@@ -121,6 +121,20 @@ config XEN_PCIDEV_FRONTEND config PCI_ATS bool +config PCI_IDE + bool + +config PCI_IDE_STREAM_MAX + int "Maximum number of Selective IDE Streams supported per host bridge" if EXPERT + depends on PCI_IDE + range 1 256 + default 64 + help + Set a kernel limit for the number of streams. The expectation + is that the platform limit is 4 to 8, so the kernel need not + track the maximum possibility of 256 streams per host bridge + in the typical case. + config PCI_DOE bool "Enable PCI Data Object Exchange (DOE) support" help
drivers/pci/Makefile +1 -0
@@ -34,6 +34,7 @@ obj-$(CONFIG_PCI_P2PDMA) += p2pdma.o obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o obj-$(CONFIG_VGA_ARB) += vgaarb.o obj-$(CONFIG_PCI_DOE) += doe.o +obj-$(CONFIG_PCI_IDE) += ide.o obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o obj-$(CONFIG_PCI_NPEM) += npem.o obj-$(CONFIG_PCIE_TPH) += tph.o
drivers/pci/ide.c new file +100 -0
100 lines not on this page · raw message
drivers/pci/pci.h +6 -0
@@ -511,6 +511,12 @@ static inline void pci_doe_sysfs_init(struct pci_dev *pdev) { }static inline void pci_doe_sysfs_teardown (struct pci_dev *pdev) { }#endif +#ifdef CONFIG_PCI_IDE +void pci_ide_init (struct pci_dev *dev); +#else +static inline void pci_ide_init (struct pci_dev *dev) { } +#endif +/** * pci_dev_set_io_state - Set the new error state if possible. *
drivers/pci/probe.c +1 -0
@@ -2619,6 +2619,7 @@ static void pci_init_capabilities(struct pci_dev *dev)pci_doe_init (dev);/* Data Object Exchange */ pci_tph_init (dev);/* TLP Processing Hints */ pci_rebar_init (dev);/* Resizable BAR */ +pci_ide_init (dev);/* Link Integrity and Data Encryption */ pcie_report_downtraining (dev);pci_init_reset_methods (dev);
include/linux/pci.h +7 -0
@@ -532,6 +532,13 @@ struct pci_dev {#endif #ifdef CONFIG_PCI_NPEM struct npem *npem;/* Native PCIe Enclosure Management */ +#endif +#ifdef CONFIG_PCI_IDE +u16 ide_cap;/* Link Integrity & Data Encryption */ +u8 nr_ide_mem;/* Address association resources for streams */ +u8 nr_link_ide;/* Link Stream count (Selective Stream offset) */ + unsigned int ide_cfg:1; /* Config cycles over IDE */ + unsigned int ide_tee_limit:1; /* Disallow T=0 traffic over IDE */#endif u16 acs_cap;/* ACS Capability offset */ u8 supported_speeds;/* Supported Link Speeds Vector */
include/uapi/linux/pci_regs.h +80 -1
91 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v3 02/13] PCI/IDE: Enumerate Selective Stream IDE capabilities
Quoted 21 lines · On Thu, 15 May 2025 22:47:21 -0700 Dan Williams <[email protected]>…
Link encryption is a new PCIe feature enumerated by "PCIe 6.2 section 7.9.26 IDE Extended Capability".
It is both a standalone port + endpoint capability, and a building block for the security protocol defined by "PCIe 6.2 section 11 TEE Device Interface Security Protocol (TDISP)". That protocol coordinates device security setup between a platform TSM (TEE Security Manager) and a device DSM (Device Security Manager). While the platform TSM can allocate resources like Stream ID and manage keys, it still requires system software to manage the IDE capability register block.
Add register definitions and basic enumeration in preparation for Selective IDE Stream establishment. A follow on change selects the new
CONFIG_PCI_IDEsymbol. Note that while the IDE specification defines both a point-to-point "Link Stream" and a Root Port to endpoint "Selective Stream", only "Selective Stream" is considered for Linux as that is the predominant mode expected by Trusted Execution Environment Security Managers (TSMs), and it is the security model that limits the number of PCI components within the TCB in a PCIe topology with switches.
Quoted 8 lines
Cc Yilun Xu <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Aneesh Kumar K.V <[email protected]>Co-developed-by Alexey Kardashevskiy <[email protected]>Signed-off-by Alexey Kardashevskiy <[email protected]>Co-developed-by Yilun Xu <[email protected]>Signed-off-by Yilun Xu <[email protected]>Signed-off-by Dan Williams <[email protected]>
This has been sat in my to read list for too long. Sorry about that!
A few trivial things inline.
Jonathan
Quoted 22 lines
--- drivers/pci/Kconfig | 14 +++++ drivers/pci/Makefile | 1 + drivers/pci/ide.c | 100 ++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 6 ++ drivers/pci/probe.c | 1 + include/linux/pci.h | 7 +++ include/uapi/linux/pci_regs.h | 81 ++++++++++++++++++++++++++- 7 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 drivers/pci/ide.cdrivers/pci/Kconfig +3
-0@@ -121,6 +121,20 @@ config XEN_PCIDEV_FRONTEND config PCI_ATS bool +config PCI_IDE + bool +
drivers/pci/Kconfig config XEN_PCIDEV_FRONTEND · line 136
Quoted 10 lines
drivers/pci/Kconfig +10
-0+config PCI_IDE_STREAM_MAX + int "Maximum number of Selective IDE Streams supported per host bridge" if EXPERT + depends on PCI_IDE + range 1 256 + default 64 + help + Set a kernel limit for the number of streams. The expectation + is that the platform limit is 4 to 8, so the kernel need not + track the maximum possibility of 256 streams per host bridge + in the typical case.
Maybe suggest why a kernel might want to limit this? Testing only?
Quoted 24 lines
drivers/pci/Kconfig +1
-0+ config PCI_DOE bool "Enable PCI Data Object Exchange (DOE) support" helpdrivers/pci/ide.c new file +13
-0@@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright(c) 2024 Intel Corporation. All rights reserved. */ + +/* PCIe 6.2 section 6.33 Integrity & Data Encryption (IDE) */ + +#define dev_fmt (fmt) "PCI/IDE: " fmt +#include <linux/pci.h> +#include <linux/bitfield.h> +#include "pci.h" + +static int __sel_ide_offset (int ide_cap,int nr_link_ide,int stream_index, +int nr_ide_mem) +{
drivers/pci/ide.c line 23
Quoted 10 lines
drivers/pci/ide.c new file +10
-0+int offset; + + offset = ide_cap +PCI_IDE_LINK_STREAM_0 + nr_link_ide *PCI_IDE_LINK_BLOCK_SIZE ; + +/* +* Assume a constant number of address association resources per +* stream index +*/ +if (stream_index >0 ) + offset += stream_index *PCI_IDE_SEL_BLOCK_SIZE (nr_ide_mem);
Is stream_index ever < 0? Doesn't look like it. So why not do this unconditionally as it doesn't do anything if stream_index == 0?
Better yet, why not make all the parameters unsigned given I don' think any of them can be < 0
Quoted 22 lines
drivers/pci/ide.c new file +2
-0+return offset; +}include/uapi/linux/pci_regs.h +2
-1@@ -750,7 +750,8 @@#define PCI_EXT_CAP_ID_NPEM 0x29/* Native PCIe Enclosure Management */ #define PCI_EXT_CAP_ID_PL_32GT 0x2A/* Physical Layer 32.0 GT/s */ #define PCI_EXT_CAP_ID_DOE 0x2E/* Data Object Exchange */ -#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_DOE +#define PCI_EXT_CAP_ID_IDE 0x30/* Integrity and Data Encryption */ +#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_IDE#define PCI_EXT_CAP_DSN_SIZEOF 12#define PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF 40@@ -1220,4 +1221,82 @@#define PCI_DVSEC_CXL_PORT_CTL 0x0c#define PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR 0x00000001
include/uapi/linux/pci_regs.h line 1233
Quoted 10 lines
include/uapi/linux/pci_regs.h +10
-0+/* Integrity and Data Encryption Extended Capability */ +#define PCI_IDE_CAP 0x4 +#define PCI_IDE_CAP_LINK 0x1/* Link IDE Stream Supported */ +#define PCI_IDE_CAP_SELECTIVE 0x2/* Selective IDE Streams Supported */ +#define PCI_IDE_CAP_FLOWTHROUGH 0x4/* Flow-Through IDE Stream Supported */ +#define PCI_IDE_CAP_PARTIAL_HEADER_ENC 0x8/* Partial Header Encryption Supported */ +#define PCI_IDE_CAP_AGGREGATION 0x10/* Aggregation Supported */ +#define PCI_IDE_CAP_PCRC 0x20/* PCRC Supported */ +#define PCI_IDE_CAP_IDE_KM 0x40/* IDE_KM Protocol Supported */ +#define PCI_IDE_CAP_SEL_CFG 0x80/* Selective IDE for Config Cycles Support */
Not sure we care but it's called Requests Support in the 6.2 spec at at least rather than Cycles.
include/uapi/linux/pci_regs.h line 1238
Quoted 5 lines
include/uapi/linux/pci_regs.h +5
-0+#define PCI_IDE_CAP_ALG_MASK __GENMASK(12, 8)/* Supported Algorithms */ +#define PCI_IDE_CAP_ALG_AES_GCM_256 0/* AES-GCM 256 key size, 96b MAC */ +#define PCI_IDE_CAP_LINK_TC_NUM_MASK __GENMASK(15, 13)/* Link IDE TCs */ +#define PCI_IDE_CAP_SEL_NUM_MASK __GENMASK(23, 16)/* Supported Selective IDE Streams */ +#define PCI_IDE_CAP_TEE_LIMITED 0x1000000/* TEE-Limited Stream Supported */
If we are going to start using __GENMASK in here (which I'm in favour of) maybe we could use _BIT()/ _BITUL() from uapi/linux/const.h as well. Counting zeros is annoying given the spec is all by bit number.
Quoted 49 lines
include/uapi/linux/pci_regs.h +49
-0+#define PCI_IDE_CTL 0x8 +#define PCI_IDE_CTL_FLOWTHROUGH_IDE 0x4/* Flow-Through IDE Stream Enabled */ + +#define PCI_IDE_LINK_STREAM_0 0xc/* First Link Stream Register Block */ +#define PCI_IDE_LINK_BLOCK_SIZE 8 +/* Link IDE Stream block, up to PCI_IDE_CAP_LINK_TC_NUM */ +#define PCI_IDE_LINK_CTL_0 0x0/* First Link Control Register Offset in block */ +#define PCI_IDE_LINK_CTL_EN 0x1/* Link IDE Stream Enable */ +#define PCI_IDE_LINK_CTL_TX_AGGR_NPR_MASK __GENMASK(3, 2)/* Tx Aggregation Mode NPR */ +#define PCI_IDE_LINK_CTL_TX_AGGR_PR_MASK __GENMASK(5, 4)/* Tx Aggregation Mode PR */ +#define PCI_IDE_LINK_CTL_TX_AGGR_CPL_MASK __GENMASK(7, 6)/* Tx Aggregation Mode CPL */ +#define PCI_IDE_LINK_CTL_PCRC_EN 0x100/* PCRC Enable */ +#define PCI_IDE_LINK_CTL_PART_ENC_MASK __GENMASK(13, 10)/* Partial Header Encryption Mode */ +#define PCI_IDE_LINK_CTL_ALG_MASK __GENMASK(18, 14)/* Selection from PCI_IDE_CAP_ALG */ +#define PCI_IDE_LINK_CTL_TC_MASK __GENMASK(21, 19)/* Traffic Class */ +#define PCI_IDE_LINK_CTL_ID_MASK __GENMASK(31, 24)/* Stream ID */ +#define PCI_IDE_LINK_STS_0 0x4/* First Link Status Register Offset in block */ +#define PCI_IDE_LINK_STS_STATE __GENMASK(3, 0)/* Link IDE Stream State */ +#define PCI_IDE_LINK_STS_RECVD_INTEGRITY_CHECK 0x80000000/* Received Integrity Check Fail Msg */ + +/* Selective IDE Stream block, up to PCI_IDE_CAP_SELECTIVE_STREAMS_NUM */ +/* Selective IDE Stream Capability Register */ +#define PCI_IDE_SEL_CAP 0 +#define PCI_IDE_SEL_CAP_ASSOC_NUM_MASK __GENMASK(3, 0) +/* Selective IDE Stream Control Register */ +#define PCI_IDE_SEL_CTL 4 +#define PCI_IDE_SEL_CTL_EN 0x1/* Selective IDE Stream Enable */ +#define PCI_IDE_SEL_CTL_TX_AGGR_NPR_MASK __GENMASK(3, 2)/* Tx Aggregation Mode NPR */ +#define PCI_IDE_SEL_CTL_TX_AGGR_PR_MASK __GENMASK(5, 4)/* Tx Aggregation Mode PR */ +#define PCI_IDE_SEL_CTL_TX_AGGR_CPL_MASK __GENMASK(7, 6)/* Tx Aggregation Mode CPL */ +#define PCI_IDE_SEL_CTL_PCRC_EN 0x100/* PCRC Enable */ +#define PCI_IDE_SEL_CTL_CFG_EN 0x200/* Selective IDE for Configuration Requests */ +#define PCI_IDE_SEL_CTL_PART_ENC_MASK __GENMASK(13, 10)/* Partial Header Encryption Mode */ +#define PCI_IDE_SEL_CTL_ALG_MASK __GENMASK(18, 14)/* Selection from PCI_IDE_CAP_ALG */ +#define PCI_IDE_SEL_CTL_TC_MASK __GENMASK(21, 19)/* Traffic Class */ +#define PCI_IDE_SEL_CTL_DEFAULT 0x400000/* Default Stream */ +#define PCI_IDE_SEL_CTL_TEE_LIMITED 0x800000/* TEE-Limited Stream */ +#define PCI_IDE_SEL_CTL_ID_MASK __GENMASK(31, 24)/* Stream ID */ +#define PCI_IDE_SEL_CTL_ID_MAX 255 +/* Selective IDE Stream Status Register */ +#define PCI_IDE_SEL_STS 8 +#define PCI_IDE_SEL_STS_STATE_MASK __GENMASK(3, 0)/* Selective IDE Stream State */ +#define PCI_IDE_SEL_STS_RECVD_INTEGRITY_CHECK 0x80000000/* Received Integrity Check Fail Msg */ +/* IDE RID Association Register 1 */ +#define PCI_IDE_SEL_RID_1 0xc +#define PCI_IDE_SEL_RID_1_LIMIT_MASK __GENMASK(23, 8) +/* IDE RID Association Register 2 */ +#define PCI_IDE_SEL_RID_2 0x10 +#define PCI_IDE_SEL_RID_2_VALID 0x1
include/uapi/linux/pci_regs.h line 1297
Quoted 10 lines
include/uapi/linux/pci_regs.h +10
-0+#define PCI_IDE_SEL_RID_2_BASE_MASK __GENMASK(23, 8) +#define PCI_IDE_SEL_RID_2_SEG_MASK __GENMASK(31, 24) +/* Selective IDE Address Association Register Block, up to PCI_IDE_SEL_CAP_ASSOC_NUM */ +#define PCI_IDE_SEL_ADDR_BLOCK_SIZE 12 +#define PCI_IDE_SEL_ADDR_1 (x) (20 + (x) * PCI_IDE_SEL_ADDR_BLOCK_SIZE) +#define PCI_IDE_SEL_ADDR_1_VALID 0x1 +#define PCI_IDE_SEL_ADDR_1_BASE_LOW_MASK __GENMASK(19, 8) +#define PCI_IDE_SEL_ADDR_1_LIMIT_LOW_MASK __GENMASK(31, 20) +/* IDE Address Association Register 2 is "Memory Limit Upper" */ +/* IDE Address Association Register 3 is "Memory Base Upper" */
Why not move this comment down one line? Match where the def is.
Quoted 5 lines
include/uapi/linux/pci_regs.h +4
-0+#define PCI_IDE_SEL_ADDR_2 (x) (24 + (x) * PCI_IDE_SEL_ADDR_BLOCK_SIZE) +#define PCI_IDE_SEL_ADDR_3 (x) (28 + (x) * PCI_IDE_SEL_ADDR_BLOCK_SIZE) +#define PCI_IDE_SEL_BLOCK_SIZE (nr_assoc) (20 + PCI_IDE_SEL_ADDR_BLOCK_SIZE * (nr_assoc)) +#endif /* LINUX_PCI_REGS_H */
Re: [PATCH v3 02/13] PCI/IDE: Enumerate Selective Stream IDE capabilities
Quoted 58 lines · Jonathan Cameron
Quoted 21 lines · On Thu, 15 May 2025 22:47:21 -0700 Dan Williams <[email protected]>…
Link encryption is a new PCIe feature enumerated by "PCIe 6.2 section 7.9.26 IDE Extended Capability".
It is both a standalone port + endpoint capability, and a building block for the security protocol defined by "PCIe 6.2 section 11 TEE Device Interface Security Protocol (TDISP)". That protocol coordinates device security setup between a platform TSM (TEE Security Manager) and a device DSM (Device Security Manager). While the platform TSM can allocate resources like Stream ID and manage keys, it still requires system software to manage the IDE capability register block.
Add register definitions and basic enumeration in preparation for Selective IDE Stream establishment. A follow on change selects the new
CONFIG_PCI_IDEsymbol. Note that while the IDE specification defines both a point-to-point "Link Stream" and a Root Port to endpoint "Selective Stream", only "Selective Stream" is considered for Linux as that is the predominant mode expected by Trusted Execution Environment Security Managers (TSMs), and it is the security model that limits the number of PCI components within the TCB in a PCIe topology with switches.Quoted 8 lines
Cc Yilun Xu <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Aneesh Kumar K.V <[email protected]>Co-developed-by Alexey Kardashevskiy <[email protected]>Signed-off-by Alexey Kardashevskiy <[email protected]>Co-developed-by Yilun Xu <[email protected]>Signed-off-by Yilun Xu <[email protected]>Signed-off-by Dan Williams <[email protected]>This has been sat in my to read list for too long. Sorry about that!
A few trivial things inline.
Jonathan
Quoted 19 lines
--- drivers/pci/Kconfig | 14 +++++ drivers/pci/Makefile | 1 + drivers/pci/ide.c | 100 ++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 6 ++ drivers/pci/probe.c | 1 + include/linux/pci.h | 7 +++ include/uapi/linux/pci_regs.h | 81 ++++++++++++++++++++++++++- 7 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 drivers/pci/ide.cdrivers/pci/Kconfig +0
-0@@ -121,6 +121,20 @@ config XEN_PCIDEV_FRONTEND config PCI_ATS bool
Quoted 15 lines
drivers/pci/Kconfig config XEN_PCIDEV_FRONTEND · line 136
Quoted 13 lines
drivers/pci/Kconfig +13
-0+config PCI_IDE + bool + +config PCI_IDE_STREAM_MAX + int "Maximum number of Selective IDE Streams supported per host bridge" if EXPERT + depends on PCI_IDE + range 1 256 + default 64 + help + Set a kernel limit for the number of streams. The expectation + is that the platform limit is 4 to 8, so the kernel need not + track the maximum possibility of 256 streams per host bridge + in the typical case.Maybe suggest why a kernel might want to limit this? Testing only?
Yes, that is the only reason I can think of to mess with this value. Updated the description to:
Set a kernel max for the number of IDE streams the PCI core supports per device. While the PCI specification max is 256, the hardware platform capability for the foreseeable future is 4 to 8 streams. Bump this value up if you have an expert testing need.
Quoted 38 lines
Quoted 24 lines
drivers/pci/Kconfig +1
-0+ config PCI_DOE bool "Enable PCI Data Object Exchange (DOE) support" helpdrivers/pci/ide.c new file +13
-0@@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright(c) 2024 Intel Corporation. All rights reserved. */ + +/* PCIe 6.2 section 6.33 Integrity & Data Encryption (IDE) */ + +#define dev_fmt (fmt) "PCI/IDE: " fmt +#include <linux/pci.h> +#include <linux/bitfield.h> +#include "pci.h" + +static int __sel_ide_offset (int ide_cap,int nr_link_ide,int stream_index, +int nr_ide_mem) +{drivers/pci/ide.c line 23
Quoted 10 lines
drivers/pci/ide.c new file +10
-0+int offset; + + offset = ide_cap +PCI_IDE_LINK_STREAM_0 + nr_link_ide *PCI_IDE_LINK_BLOCK_SIZE ; + +/* +* Assume a constant number of address association resources per +* stream index +*/ +if (stream_index >0 ) + offset += stream_index *PCI_IDE_SEL_BLOCK_SIZE (nr_ide_mem);Is
stream_indexever < 0? Doesn't look like it. So why not do this unconditionally as it doesn't do anything ifstream_index== 0?
Quoted 2 lines
Better yet, why not make all the parameters unsigned given I don' think any of them can be < 0
Yeah, they are already all unsigned in the caller, done.
Quoted 23 lines
Quoted 23 lines
drivers/pci/ide.c new file +2
-0+return offset; +}include/uapi/linux/pci_regs.h +2
-1@@ -750,7 +750,8 @@#define PCI_EXT_CAP_ID_NPEM 0x29/* Native PCIe Enclosure Management */ #define PCI_EXT_CAP_ID_PL_32GT 0x2A/* Physical Layer 32.0 GT/s */ #define PCI_EXT_CAP_ID_DOE 0x2E/* Data Object Exchange */ -#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_DOE +#define PCI_EXT_CAP_ID_IDE 0x30/* Integrity and Data Encryption */ +#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_IDE#define PCI_EXT_CAP_DSN_SIZEOF 12#define PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF 40@@ -1220,4 +1221,82 @@#define PCI_DVSEC_CXL_PORT_CTL 0x0c#define PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR 0x00000001
Quoted 13 lines
include/uapi/linux/pci_regs.h line 1233
Quoted 10 lines
include/uapi/linux/pci_regs.h +10
-0+/* Integrity and Data Encryption Extended Capability */ +#define PCI_IDE_CAP 0x4 +#define PCI_IDE_CAP_LINK 0x1/* Link IDE Stream Supported */ +#define PCI_IDE_CAP_SELECTIVE 0x2/* Selective IDE Streams Supported */ +#define PCI_IDE_CAP_FLOWTHROUGH 0x4/* Flow-Through IDE Stream Supported */ +#define PCI_IDE_CAP_PARTIAL_HEADER_ENC 0x8/* Partial Header Encryption Supported */ +#define PCI_IDE_CAP_AGGREGATION 0x10/* Aggregation Supported */ +#define PCI_IDE_CAP_PCRC 0x20/* PCRC Supported */ +#define PCI_IDE_CAP_IDE_KM 0x40/* IDE_KM Protocol Supported */ +#define PCI_IDE_CAP_SEL_CFG 0x80/* Selective IDE for Config Cycles Support */ Not sure we care but it's called Requests Support in the 6.2 spec at at least rather than Cycles.
Yeah, that's my parallel-PCI upbringing showing. While the PCIe spec still says "configuration cycle" in places "configuration request" dominates. Fixed.
Quoted 65 lines
include/uapi/linux/pci_regs.h line 1238
Quoted 7 lines
include/uapi/linux/pci_regs.h +5
-0+#define PCI_IDE_CAP_ALG_MASK __GENMASK(12, 8)/* Supported Algorithms */ +#define PCI_IDE_CAP_ALG_AES_GCM_256 0/* AES-GCM 256 key size, 96b MAC */ +#define PCI_IDE_CAP_LINK_TC_NUM_MASK __GENMASK(15, 13)/* Link IDE TCs */ +#define PCI_IDE_CAP_SEL_NUM_MASK __GENMASK(23, 16)/* Supported Selective IDE Streams */ +#define PCI_IDE_CAP_TEE_LIMITED 0x1000000/* TEE-Limited Stream Supported */ If we are going to start using
__GENMASKin here (which I'm in favour of) maybe we could use _BIT()/_BITUL()fromuapi/linux/const.has well. Counting zeros is annoying given the spec is all by bit number.Quoted 53 lines
include/uapi/linux/pci_regs.h +53
-0+#define PCI_IDE_CTL 0x8 +#define PCI_IDE_CTL_FLOWTHROUGH_IDE 0x4/* Flow-Through IDE Stream Enabled */ + +#define PCI_IDE_LINK_STREAM_0 0xc/* First Link Stream Register Block */ +#define PCI_IDE_LINK_BLOCK_SIZE 8 +/* Link IDE Stream block, up to PCI_IDE_CAP_LINK_TC_NUM */ +#define PCI_IDE_LINK_CTL_0 0x0/* First Link Control Register Offset in block */ +#define PCI_IDE_LINK_CTL_EN 0x1/* Link IDE Stream Enable */ +#define PCI_IDE_LINK_CTL_TX_AGGR_NPR_MASK __GENMASK(3, 2)/* Tx Aggregation Mode NPR */ +#define PCI_IDE_LINK_CTL_TX_AGGR_PR_MASK __GENMASK(5, 4)/* Tx Aggregation Mode PR */ +#define PCI_IDE_LINK_CTL_TX_AGGR_CPL_MASK __GENMASK(7, 6)/* Tx Aggregation Mode CPL */ +#define PCI_IDE_LINK_CTL_PCRC_EN 0x100/* PCRC Enable */ +#define PCI_IDE_LINK_CTL_PART_ENC_MASK __GENMASK(13, 10)/* Partial Header Encryption Mode */ +#define PCI_IDE_LINK_CTL_ALG_MASK __GENMASK(18, 14)/* Selection from PCI_IDE_CAP_ALG */ +#define PCI_IDE_LINK_CTL_TC_MASK __GENMASK(21, 19)/* Traffic Class */ +#define PCI_IDE_LINK_CTL_ID_MASK __GENMASK(31, 24)/* Stream ID */ +#define PCI_IDE_LINK_STS_0 0x4/* First Link Status Register Offset in block */ +#define PCI_IDE_LINK_STS_STATE __GENMASK(3, 0)/* Link IDE Stream State */ +#define PCI_IDE_LINK_STS_RECVD_INTEGRITY_CHECK 0x80000000/* Received Integrity Check Fail Msg */ + +/* Selective IDE Stream block, up to PCI_IDE_CAP_SELECTIVE_STREAMS_NUM */ +/* Selective IDE Stream Capability Register */ +#define PCI_IDE_SEL_CAP 0 +#define PCI_IDE_SEL_CAP_ASSOC_NUM_MASK __GENMASK(3, 0) +/* Selective IDE Stream Control Register */ +#define PCI_IDE_SEL_CTL 4 +#define PCI_IDE_SEL_CTL_EN 0x1/* Selective IDE Stream Enable */ +#define PCI_IDE_SEL_CTL_TX_AGGR_NPR_MASK __GENMASK(3, 2)/* Tx Aggregation Mode NPR */ +#define PCI_IDE_SEL_CTL_TX_AGGR_PR_MASK __GENMASK(5, 4)/* Tx Aggregation Mode PR */ +#define PCI_IDE_SEL_CTL_TX_AGGR_CPL_MASK __GENMASK(7, 6)/* Tx Aggregation Mode CPL */ +#define PCI_IDE_SEL_CTL_PCRC_EN 0x100/* PCRC Enable */ +#define PCI_IDE_SEL_CTL_CFG_EN 0x200/* Selective IDE for Configuration Requests */ +#define PCI_IDE_SEL_CTL_PART_ENC_MASK __GENMASK(13, 10)/* Partial Header Encryption Mode */ +#define PCI_IDE_SEL_CTL_ALG_MASK __GENMASK(18, 14)/* Selection from PCI_IDE_CAP_ALG */ +#define PCI_IDE_SEL_CTL_TC_MASK __GENMASK(21, 19)/* Traffic Class */ +#define PCI_IDE_SEL_CTL_DEFAULT 0x400000/* Default Stream */ +#define PCI_IDE_SEL_CTL_TEE_LIMITED 0x800000/* TEE-Limited Stream */ +#define PCI_IDE_SEL_CTL_ID_MASK __GENMASK(31, 24)/* Stream ID */ +#define PCI_IDE_SEL_CTL_ID_MAX 255 +/* Selective IDE Stream Status Register */ +#define PCI_IDE_SEL_STS 8 +#define PCI_IDE_SEL_STS_STATE_MASK __GENMASK(3, 0)/* Selective IDE Stream State */ +#define PCI_IDE_SEL_STS_RECVD_INTEGRITY_CHECK 0x80000000/* Received Integrity Check Fail Msg */ +/* IDE RID Association Register 1 */ +#define PCI_IDE_SEL_RID_1 0xc +#define PCI_IDE_SEL_RID_1_LIMIT_MASK __GENMASK(23, 8) +/* IDE RID Association Register 2 */ +#define PCI_IDE_SEL_RID_2 0x10 +#define PCI_IDE_SEL_RID_2_VALID 0x1 +#define PCI_IDE_SEL_RID_2_BASE_MASK __GENMASK(23, 8) +#define PCI_IDE_SEL_RID_2_SEG_MASK __GENMASK(31, 24) +/* Selective IDE Address Association Register Block, up to PCI_IDE_SEL_CAP_ASSOC_NUM */ +#define PCI_IDE_SEL_ADDR_BLOCK_SIZE 12
Quoted 8 lines
include/uapi/linux/pci_regs.h line 1297
Quoted 6 lines
include/uapi/linux/pci_regs.h +6
-0+#define PCI_IDE_SEL_ADDR_1 (x) (20 + (x) * PCI_IDE_SEL_ADDR_BLOCK_SIZE) +#define PCI_IDE_SEL_ADDR_1_VALID 0x1 +#define PCI_IDE_SEL_ADDR_1_BASE_LOW_MASK __GENMASK(19, 8) +#define PCI_IDE_SEL_ADDR_1_LIMIT_LOW_MASK __GENMASK(31, 20) +/* IDE Address Association Register 2 is "Memory Limit Upper" */ +/* IDE Address Association Register 3 is "Memory Base Upper" */ Why not move this comment down one line? Match where the def is.
Sure.
Quoted 7 lines
Quoted 6 lines
include/uapi/linux/pci_regs.h +4
-0+#define PCI_IDE_SEL_ADDR_2 (x) (24 + (x) * PCI_IDE_SEL_ADDR_BLOCK_SIZE) +#define PCI_IDE_SEL_ADDR_3 (x) (28 + (x) * PCI_IDE_SEL_ADDR_BLOCK_SIZE) +#define PCI_IDE_SEL_BLOCK_SIZE (nr_assoc) (20 + PCI_IDE_SEL_ADDR_BLOCK_SIZE * (nr_assoc)) +#endif /* LINUX_PCI_REGS_H */
PATCH v3 3/13 PCI/TSM: Authenticate devices via platform TSM
In reply to Dan WilliamsThe PCIe 6.1 specification, section 11, introduces the Trusted Execution Environment (TEE) Device Interface Security Protocol (TDISP). This protocol definition builds upon Component Measurement and Authentication (CMA), and link Integrity and Data Encryption (IDE). It adds support for assigning devices (PCI physical or virtual function) to a confidential VM such that the assigned device is enabled to access guest private memory protected by technologies like Intel TDX, AMD SEV-SNP, RISCV COVE, or ARM CCA.
The "TSM" (TEE Security Manager) is a concept in the TDISP specification of an agent that mediates between a "DSM" (Device Security Manager) and system software in both a VMM and a confidential VM. A VMM uses TSM ABIs to setup link security and assign devices. A confidential VM uses TSM ABIs to transition an assigned device into the TDISP "RUN" state and validate its configuration. From a Linux perspective the TSM abstracts many of the details of TDISP, IDE, and CMA. Some of those details leak through at times, but for the most part TDISP is an internal implementation detail of the TSM.
CONFIG_PCI_TSM adds an "authenticated" attribute and "tsm/" subdirectory to pci-sysfs. Consider that the TSM driver may itself be a PCI driver. Userspace can watch for the arrival of the "TSM" core device, /sys/class/tsm/tsm0/uevent, to know when the PCI core has initialized TSM services.
The common verbs that the low-level TSM drivers implement are defined by 'struct pci_tsm_ops'. For now only 'connect' and 'disconnect' are defined for secure session and IDE establishment. The 'probe' and 'remove' operations setup per-device context objects starting with 'struct pci_tsm_pf0', the device Physical Function 0 that mediates communication to the device's Security Manager (DSM).
The locking allows for multiple devices to be executing commands simultaneously, one outstanding command per-device and an rwsem synchronizes the implementation relative to TSM registration/unregistration events.
Thanks to Wu Hao for his work on an early draft of this support.
--- Documentation/ABI/testing/sysfs-bus-pci | 45 +++ MAINTAINERS | 2 + drivers/pci/Kconfig | 14 + drivers/pci/Makefile | 1 + drivers/pci/pci-sysfs.c | 4 + drivers/pci/pci.h | 10 + drivers/pci/probe.c | 1 + drivers/pci/remove.c | 3 + drivers/pci/tsm.c | 437 ++++++++++++++++++++++++ drivers/virt/coco/host/tsm-core.c | 19 +- include/linux/pci-tsm.h | 138 ++++++++ include/linux/pci.h | 3 + include/linux/tsm.h | 4 +- include/uapi/linux/pci_regs.h | 1 + 14 files changed, 679 insertions(+), 3 deletions(-) create mode 100644 drivers/pci/tsm.c create mode 100644 include/linux/pci-tsm.h
Documentation/ABI/testing/sysfs-bus-pci +45 -0
@@ -612,3 +612,48 @@ Description: # ls doe_features 0001:01 0001:02 doe_discovery + +What: /sys/bus/pci/devices/.../tsm/ +Date: July 2024 +Contact: [email protected] +Description: + This directory only appears if a physical device function + supports authentication (PCIe CMA-SPDM), interface security + (PCIe TDISP), and is accepted for secure operation by the + platform TSM driver. This attribute directory appears + dynamically after the platform TSM driver loads. So, only after + the /sys/class/tsm/tsm0 device arrives can tools assume that + devices without a tsm/ attribute directory will never have one, + before that, the security capabilities of the device relative to + the platform TSM are unknown. See + Documentation/ABI/testing/sysfs-class-tsm. + +What: /sys/bus/pci/devices/.../tsm/connect +Date: July 2024 +Contact: [email protected] +Description: + (RW) Writing "1" to this file triggers the platform TSM (TEE + Security Manager) to establish a connection with the device. + This typically includes an SPDM (DMTF Security Protocols and + Data Models) session over PCIe DOE (Data Object Exchange) and + may also include PCIe IDE (Integrity and Data Encryption) + establishment. + +What: /sys/bus/pci/devices/.../authenticated +Date: July 2024 +Contact: [email protected] +Description: + When the device's tsm/ directory is present device + authentication (PCIe CMA-SPDM) and link encryption (PCIe IDE) + are handled by the platform TSM (TEE Security Manager). When the + tsm/ directory is not present this attribute reflects only the + native CMA-SPDM authentication state with the kernel's + certificate store. + + If the attribute is not present, it indicates that + authentication is unsupported by the device, or the TSM has no + available authentication methods for the device. + + When present and the tsm/ attribute directory is present, the + authenticated attribute is an alias for the device 'connect' + state. See the 'tsm/connect' attribute for more details.
MAINTAINERS +2 -0
@@ -24560,8 +24560,10 @@ M: Dan Williams <[email protected]> L: [email protected] S: Maintained F: Documentation/ABI/testing/configfs-tsm-report +F: drivers/pci/tsm.c F: drivers/virt/coco/guest/ F: drivers/virt/coco/host/ +F: include/linux/pci-tsm.h F: include/linux/tsm.h TRUSTED SERVICES TEE DRIVER
drivers/pci/Kconfig +14 -0
@@ -135,6 +135,20 @@ config PCI_IDE_STREAM_MAX track the maximum possibility of 256 streams per host bridge in the typical case. +config PCI_TSM + bool "PCI TSM: Device security protocol support" + select PCI_IDE + select PCI_DOE + help + The TEE (Trusted Execution Environment) Device Interface + Security Protocol (TDISP) defines a "TSM" as a platform agent + that manages device authentication, link encryption, link + integrity protection, and assignment of PCI device functions + (virtual or physical) to confidential computing VMs that can + access (DMA) guest private memory. + + Enable a platform TSM driver to use this capability. + config PCI_DOE bool "Enable PCI Data Object Exchange (DOE) support" help
drivers/pci/Makefile +1 -0
@@ -35,6 +35,7 @@ obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o obj-$(CONFIG_VGA_ARB) += vgaarb.o obj-$(CONFIG_PCI_DOE) += doe.o obj-$(CONFIG_PCI_IDE) += ide.o +obj-$(CONFIG_PCI_TSM) += tsm.o obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o obj-$(CONFIG_PCI_NPEM) += npem.o obj-$(CONFIG_PCIE_TPH) += tph.o
drivers/pci/pci-sysfs.c +4 -0
@@ -1811,6 +1811,10 @@ const struct attribute_group *pci_dev_attr_groups[] = {#endif #ifdef CONFIG_PCI_DOE &pci_doe_sysfs_group, +#endif +#ifdef CONFIG_PCI_TSM + &pci_tsm_auth_attr_group, + &pci_tsm_pf0_attr_group, #endif NULL, };
drivers/pci/pci.h +10 -0
@@ -517,6 +517,16 @@ void pci_ide_init(struct pci_dev *dev);static inline void pci_ide_init (struct pci_dev *dev) { }#endif +#ifdef CONFIG_PCI_TSM +void pci_tsm_init (struct pci_dev *pdev); +void pci_tsm_destroy (struct pci_dev *pdev); +extern const struct attribute_group pci_tsm_pf0_attr_group; +extern const struct attribute_group pci_tsm_auth_attr_group; +#else +static inline void pci_tsm_init (struct pci_dev *pdev) { } +static inline void pci_tsm_destroy (struct pci_dev *pdev) { } +#endif +/** * pci_dev_set_io_state - Set the new error state if possible. *
drivers/pci/probe.c +1 -0
@@ -2620,6 +2620,7 @@ static void pci_init_capabilities(struct pci_dev *dev)pci_tph_init (dev);/* TLP Processing Hints */ pci_rebar_init (dev);/* Resizable BAR */ pci_ide_init (dev);/* Link Integrity and Data Encryption */ +pci_tsm_init (dev);/* TEE Security Manager connection */ pcie_report_downtraining (dev);pci_init_reset_methods (dev);
drivers/pci/remove.c +3 -0
@@ -55,6 +55,9 @@ static void pci_destroy_dev(struct pci_dev *dev)pci_doe_sysfs_teardown (dev);pci_npem_remove (dev); +/* before device_del() to keep config cycle access */ +pci_tsm_destroy (dev); +device_del (&dev->dev);down_write (&pci_bus_sem);
drivers/pci/tsm.c new file +437 -0
437 lines not on this page · raw message
drivers/virt/coco/host/tsm-core.c +17 -2
@@ -8,11 +8,13 @@#include <linux/device.h> #include <linux/module.h> #include <linux/cleanup.h> +#include <linux/pci-tsm.h> static DECLARE_RWSEM (tsm_core_rwsem);static struct class *tsm_class;static struct tsm_core_dev {struct device dev; +const struct pci_tsm_ops *pci_ops; } *tsm_core; static struct tsm_core_dev *
@@ -39,7 +41,8 @@ static void put_tsm_core(struct tsm_core_dev *core) DEFINE_FREE(put_tsm_core, struct tsm_core_dev *, if (!IS_ERR_OR_NULL(_T)) put_tsm_core(_T))struct tsm_core_dev *tsm_register (struct device *parent, -const struct attribute_group **groups) +const struct attribute_group **groups, +const struct pci_tsm_ops *pci_ops) {struct device *dev;int rc;
@@ -61,10 +64,20 @@ struct tsm_core_dev *tsm_register(struct device *parent,if (rc)return ERR_PTR (rc); + rc =pci_tsm_core_register (pci_ops,NULL ); +if (rc) { +dev_err (parent,"PCI initialization failure: %pe\n" , +ERR_PTR (rc)); +return ERR_PTR (rc); + } + rc =device_add (dev); -if (rc) +if (rc) { +pci_tsm_core_unregister (pci_ops);return ERR_PTR (rc); + } + core->pci_ops = pci_ops; tsm_core =no_free_ptr (core);return tsm_core;
@@ -79,7 +92,9 @@ void tsm_unregister(struct tsm_core_dev *core)return ; } +pci_tsm_core_unregister (core->pci_ops);device_unregister (&core->dev); + tsm_core =NULL ; }EXPORT_SYMBOL_GPL (tsm_unregister);
include/linux/pci-tsm.h new file +138 -0
138 lines not on this page · raw message
include/linux/pci.h +3 -0
@@ -539,6 +539,9 @@ struct pci_dev {u8 nr_link_ide;/* Link Stream count (Selective Stream offset) */ unsigned int ide_cfg:1; /* Config cycles over IDE */ unsigned int ide_tee_limit:1; /* Disallow T=0 traffic over IDE */ +#endif +#ifdef CONFIG_PCI_TSM +struct pci_tsm *tsm;/* TSM operation state */ #endif u16 acs_cap;/* ACS Capability offset */ u8 supported_speeds;/* Supported Link Speeds Vector */
include/linux/tsm.h +3 -1
@@ -111,7 +111,9 @@ struct tsm_report_ops {int tsm_report_register (const struct tsm_report_ops *ops,void *priv);int tsm_report_unregister (const struct tsm_report_ops *ops);struct tsm_core_dev ; +struct pci_tsm_ops ;struct tsm_core_dev *tsm_register (struct device *parent, -const struct attribute_group **groups); +const struct attribute_group **groups, +const struct pci_tsm_ops *ops);void tsm_unregister (struct tsm_core_dev *tsm_core);#endif /* __TSM_H */
include/uapi/linux/pci_regs.h +1 -0
@@ -500,6 +500,7 @@#define PCI_EXP_DEVCAP_PWR_VAL 0x03fc0000/* Slot Power Limit Value */ #define PCI_EXP_DEVCAP_PWR_SCL 0x0c000000/* Slot Power Limit Scale */ #define PCI_EXP_DEVCAP_FLR 0x10000000/* Function Level Reset */ +#define PCI_EXP_DEVCAP_TEE 0x40000000/* TEE I/O (TDISP) Support */ #define PCI_EXP_DEVCTL 0x08/* Device Control */ #define PCI_EXP_DEVCTL_CERE 0x0001/* Correctable Error Reporting En. */ #define PCI_EXP_DEVCTL_NFERE 0x0002/* Non-Fatal Error Reporting Enable */
Signature · 1 line
2.49.0
Aneesh Kumar K.V
maintainerRe: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Dan Williams <[email protected]> writes:
....
drivers/pci/tsm.c line 304
Quoted 9 lines
drivers/pci/tsm.c new file +8
-0+static void pci_tsm_pf0_init (struct pci_dev *pdev) +{ +bool tee_cap; + + tee_cap = pdev->devcap &PCI_EXP_DEVCAP_TEE ; + +if (!(pdev->ide_cap || tee_cap)) +return ;
If we expect to use pci_tsm_pf0_init and is_pci_tsm_pf0() from the guest, can we have the ide_cap and tee_cap check here? Will that be true for all devices assigned to the guest?
Quoted 26 lines
drivers/pci/tsm.c new file +25
-0+ +lockdep_assert_held_write (&pci_tsm_rwsem); +if (!tsm_ops) +return ; + +/* +* If a physical device has any security capabilities it may be +* a candidate to connect with the platform TSM +*/ + struct pci_tsm *pci_tsm __free(tsm_remove) = tsm_ops->probe(pdev); + +pci_dbg (pdev,"Device security capabilities detected (%s%s ), TSM %s\n" , + pdev->ide_cap ?" ide" :"" , tee_cap ?" tee" :"" , + pci_tsm ?"attach" :"skip" ); + +if (!pci_tsm) +return ; + + pdev->tsm =no_free_ptr (pci_tsm); +sysfs_update_group (&pdev->dev.kobj, &pci_tsm_auth_attr_group); +sysfs_update_group (&pdev->dev.kobj, &pci_tsm_pf0_attr_group); +if (pci_tsm_owner_attr_group) +sysfs_merge_group (&pdev->dev.kobj, pci_tsm_owner_attr_group); +} +
....
Quoted 13 lines
include/linux/pci-tsm.h new file +12
-0+/* physical function0 and capable of 'connect' */ +static inline bool is_pci_tsm_pf0 (struct pci_dev *pdev) +{ +if (!pci_is_pcie (pdev)) +return false; + +if (pdev->is_virtfn) +return false; + +/* +* Allow for a Device Security Manager (DSM) associated with function0 +* of an Endpoint to coordinate TDISP requests for other functions
include/linux/pci-tsm.h line 81
Quoted 10 lines
include/linux/pci-tsm.h new file +10
-0+* (physical or virtual) of the device, or allow for an Upstream Port +* DSM to accept TDISP requests for switch Downstream Endpoints. +*/ +switch (pci_pcie_type (pdev)) { +case PCI_EXP_TYPE_ENDPOINT : +case PCI_EXP_TYPE_UPSTREAM : +case PCI_EXP_TYPE_RC_END : +if (pdev->ide_cap || (pdev->devcap &PCI_EXP_DEVCAP_TEE )) +break ; + fallthrough;
here
include/linux/pci-tsm.h line 88
Quoted 7 lines
include/linux/pci-tsm.h new file +7
-0+default : +return false; + } + +return PCI_FUNC (pdev->devfn) ==0 ; +} +
-aneesh
Re: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Quoted 4 lines · Aneesh Kumar K.V
Dan Williams <[email protected]> writes:
....
Quoted 13 lines
drivers/pci/tsm.c line 304
Quoted 9 lines
drivers/pci/tsm.c new file +8
-0+static void pci_tsm_pf0_init (struct pci_dev *pdev) +{ +bool tee_cap; + + tee_cap = pdev->devcap &PCI_EXP_DEVCAP_TEE ; + +if (!(pdev->ide_cap || tee_cap)) +return ;If we expect to use
pci_tsm_pf0_initandis_pci_tsm_pf0()from the guest, can we have theide_capandtee_capcheck here? Will that be true for all devices assigned to the guest?
I do not expect this path to be taken for a guest device. IDE is not relevant for TDIs in the guest and function0 is not a requirement guest BDFs. I still need to add this to samples/devsec/, but the expectation is that in "TVM mode" the presence of PCI_EXP_DEVCAP_TEE is sufficient to route any PCI function to tsm_ops->probe().
Aneesh Kumar K.V
maintainerRe: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Quoted 19 lines · Dan Williams
Quoted 4 lines · Aneesh Kumar K.V
Dan Williams <[email protected]> writes:
....
Quoted 13 lines
drivers/pci/tsm.c line 304
Quoted 9 lines
drivers/pci/tsm.c new file +8
-0+static void pci_tsm_pf0_init (struct pci_dev *pdev) +{ +bool tee_cap; + + tee_cap = pdev->devcap &PCI_EXP_DEVCAP_TEE ; + +if (!(pdev->ide_cap || tee_cap)) +return ;If we expect to use
pci_tsm_pf0_initandis_pci_tsm_pf0()from the guest, can we have theide_capandtee_capcheck here? Will that be true for all devices assigned to the guest?
Quoted 5 lines
I do not expect this path to be taken for a guest device. IDE is not relevant for TDIs in the guest and function0 is not a requirement guest BDFs. I still need to add this to
samples/devsec/, but the expectation is that in "TVM mode" the presence ofPCI_EXP_DEVCAP_TEEis sufficient to route any PCI function totsm_ops->probe().
We do that because we expose /sys/bus/pci/devices/<x>/tsm/connect to guest and we have
static int pci_tsm_connect(struct pci_dev *pdev)
{
struct pci_tsm_pf0 *tsm = to_pci_tsm_pf0(pdev->tsm);
int rc;
....
if (tvm_mode())
rc = __driver_idle_connect(pdev);
else
rc = tsm_ops->connect(pdev);-aneesh
Re: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
In reply to Dan WilliamsQuoted 39 lines · On Thu, 15 May 2025 22:47:22 -0700 Dan Williams <[email protected]>…
The PCIe 6.1 specification, section 11, introduces the Trusted Execution Environment (TEE) Device Interface Security Protocol (TDISP). This protocol definition builds upon Component Measurement and Authentication (CMA), and link Integrity and Data Encryption (IDE). It adds support for assigning devices (PCI physical or virtual function) to a confidential VM such that the assigned device is enabled to access guest private memory protected by technologies like Intel TDX, AMD SEV-SNP, RISCV COVE, or ARM CCA.
The "TSM" (TEE Security Manager) is a concept in the TDISP specification of an agent that mediates between a "DSM" (Device Security Manager) and system software in both a VMM and a confidential VM. A VMM uses TSM ABIs to setup link security and assign devices. A confidential VM uses TSM ABIs to transition an assigned device into the TDISP "RUN" state and validate its configuration. From a Linux perspective the TSM abstracts many of the details of TDISP, IDE, and CMA. Some of those details leak through at times, but for the most part TDISP is an internal implementation detail of the TSM.
CONFIG_PCI_TSMadds an "authenticated" attribute and "tsm/" subdirectory to pci-sysfs. Consider that the TSM driver may itself be a PCI driver. Userspace can watch for the arrival of the "TSM" core device,/sys/class/tsm/tsm0/uevent, to know when the PCI core has initialized TSM services.The common verbs that the low-level TSM drivers implement are defined by 'struct
pci_tsm_ops'. For now only 'connect' and 'disconnect' are defined for secure session and IDE establishment. The 'probe' and 'remove' operations setup per-device context objects starting with 'structpci_tsm_pf0', the device Physical Function 0 that mediates communication to the device's Security Manager (DSM).The locking allows for multiple devices to be executing commands simultaneously, one outstanding command per-device and an rwsem synchronizes the implementation relative to TSM registration/unregistration events.
Thanks to Wu Hao for his work on an early draft of this support.
Quoted 7 lines
Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Acked-by Bjorn Helgaas <[email protected]>Co-developed-by Xu Yilun <[email protected]>Signed-off-by Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>
Trivial stuff on this one. See inline.
Jonathan
Quoted 24 lines
--- Documentation/ABI/testing/sysfs-bus-pci | 45 +++ MAINTAINERS | 2 + drivers/pci/Kconfig | 14 + drivers/pci/Makefile | 1 + drivers/pci/pci-sysfs.c | 4 + drivers/pci/pci.h | 10 + drivers/pci/probe.c | 1 + drivers/pci/remove.c | 3 + drivers/pci/tsm.c | 437 ++++++++++++++++++++++++ drivers/virt/coco/host/tsm-core.c | 19 +- include/linux/pci-tsm.h | 138 ++++++++ include/linux/pci.h | 3 + include/linux/tsm.h | 4 +- include/uapi/linux/pci_regs.h | 1 + 14 files changed, 679 insertions(+), 3 deletions(-) create mode 100644 drivers/pci/tsm.c create mode 100644 include/linux/pci-tsm.hDocumentation/ABI/testing/sysfs-bus-pci +0
-0
Documentation/ABI/testing/sysfs-bus-pci Description: · line 617
Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-pci +3
-0@@ -612,3 +612,48 @@ Description: # ls doe_features 0001:01 0001:02 doe_discovery + +What: /sys/bus/pci/devices/.../tsm/ +Date: July 2024
Guess the date for merge?
Quoted 11 lines
Documentation/ABI/testing/sysfs-bus-pci +2
-0+Contact: [email protected] +Description:drivers/pci/tsm.c new file +2
-0@@ -0,0 +1,437 @@ +// SPDX-License-Identifier: GPL-2.0 +/*
drivers/pci/tsm.c line 12
Quoted 10 lines
drivers/pci/tsm.c new file +10
-0+* TEE Security Manager for the TEE Device Interface Security Protocol +* (TDISP, PCIe r6.1 sec 11) +* +* Copyright(c) 2024 Intel Corporation. All rights reserved. +*/ + +#define dev_fmt (fmt) "TSM: " fmt + +#include <linux/bitfield.h> +#include <linux/xarray.h>
Not seeing an xa stuff yet.
Check the others are all needed or push them forwards to appropriate patch.
Quoted 6 lines
drivers/pci/tsm.c new file +6
-0+#include <linux/sysfs.h> + +#include <linux/pci.h> +#include <linux/pci-doe.h> +#include <linux/pci-tsm.h> +#include "pci.h"
drivers/pci/tsm.c line 151
Quoted 10 lines
drivers/pci/tsm.c new file +8
-0+static bool pci_tsm_pf0_group_visible (struct kobject *kobj) +{ +struct device *dev =kobj_to_dev (kobj); +struct pci_dev *pdev =to_pci_dev (dev); + +if (pdev->tsm &&is_pci_tsm_pf0 (pdev)) +return true; +return false;
Unless this is going to get more complex later
return pdev->tsm && is_pci_tsm_pf0(pdev);
Quoted 5 lines
drivers/pci/tsm.c new file +4
-0+} +DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE (pci_tsm_pf0); + +const struct attribute_group pci_tsm_auth_attr_group = {
drivers/pci/tsm.c
Quoted 10 lines
drivers/pci/tsm.c new file +8
-0+ .attrs = pci_tsm_auth_attrs, + .is_visible = SYSFS_GROUP_VISIBLE(pci_tsm_pf0), +}; +static void pci_tsm_pf0_init (struct pci_dev *pdev) +{ +bool tee_cap; + + tee_cap = pdev->devcap &PCI_EXP_DEVCAP_TEE ;
Might as well put that on first line.
Quoted 58 lines
drivers/pci/tsm.c new file +3
-0+ +if (!(pdev->ide_cap || tee_cap)) +return ;drivers/virt/coco/host/tsm-core.c +15
-2@@ -8,11 +8,13 @@#include <linux/device.h> #include <linux/module.h> #include <linux/cleanup.h> +#include <linux/pci-tsm.h> static DECLARE_RWSEM (tsm_core_rwsem);static struct class *tsm_class;static struct tsm_core_dev {struct device dev; +const struct pci_tsm_ops *pci_ops; } *tsm_core; static struct tsm_core_dev *@@ -39,7 +41,8 @@ static void put_tsm_core(struct tsm_core_dev *core) DEFINE_FREE(put_tsm_core, struct tsm_core_dev *, if (!IS_ERR_OR_NULL(_T)) put_tsm_core(_T))struct tsm_core_dev *tsm_register (struct device *parent, -const struct attribute_group **groups) +const struct attribute_group **groups, +const struct pci_tsm_ops *pci_ops) {struct device *dev;int rc;@@ -61,10 +64,20 @@ struct tsm_core_dev *tsm_register(struct device *parent,if (rc)return ERR_PTR (rc); + rc =pci_tsm_core_register (pci_ops,NULL ); +if (rc) { +dev_err (parent,"PCI initialization failure: %pe\n" , +ERR_PTR (rc)); +return ERR_PTR (rc); + } + rc =device_add (dev); -if (rc) +if (rc) { +pci_tsm_core_unregister (pci_ops);return ERR_PTR (rc); + } + core->pci_ops = pci_ops; tsm_core =no_free_ptr (core);return tsm_core;
drivers/virt/coco/host/tsm-core.c void tsm_unregister(struct tsm_core_dev *core) · line 96
Quoted 5 lines
drivers/virt/coco/host/tsm-core.c +1
-0@@ -79,7 +92,9 @@ void tsm_unregister(struct tsm_core_dev *core)return ; } +pci_tsm_core_unregister (core->pci_ops);device_unregister (&core->dev);
Using device_initialize() and device_add() in probe but device_unregister() in remove results in trivial ordering mess like this. I'd split the remove() path so we can take down in the reverse of setup with pci_tsm_core_unregister() between device_del() and put_device()
This ordering thing is common enough though that maybe we can just not worry about it.
drivers/virt/coco/host/tsm-core.c void tsm_unregister(struct tsm_core_dev *core) · line 97
Quoted 1 line
drivers/virt/coco/host/tsm-core.c +1
-0+
Push whitespace change back to earlier patch.
Quoted 9 lines
drivers/virt/coco/host/tsm-core.c +0
-0tsm_core =NULL ; }EXPORT_SYMBOL_GPL (tsm_unregister);include/linux/pci-tsm.h new file +0
-0
include/linux/pci-tsm.h line 7
Quoted 7 lines
include/linux/pci-tsm.h new file +7
-0@@ -0,0 +1,138 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PCI_TSM_H +#define __PCI_TSM_H +#include <linux/mutex.h> +#include <linux/pci.h> + +struct pci_dev ;
Given you have linux/pci.h no need for the forwards def.
include/linux/pci-tsm.h line 16
Quoted 9 lines
include/linux/pci-tsm.h new file +9
-0+ +enum pci_tsm_state { +PCI_TSM_ERR =-1 , +PCI_TSM_INIT , +PCI_TSM_CONNECT , +}; + +/** +* enum pci_tsm_type - 'struct pci_tsm' object types
Kernel-doc should warn on incomplete docs. I'd add trivial comment for INVALID to avoid that.
include/linux/pci-tsm.h line 20
Quoted 4 lines
include/linux/pci-tsm.h new file +4
-0+* @PCI_TSM_PF0: function0 that hosts a DOE mailbox that comprehends an +* Interface ID per potential TDI +* @PCI_TSM_VIRTFN: physfn-0 of this device is "tsm_pf0" +* @PCI_TSM_MFD: function0 of this device is "tsm_pf0"
Double space after "is" seems odd.
include/linux/pci-tsm.h line 33
Quoted 13 lines
include/linux/pci-tsm.h new file +13
-0+* @PCI_TSM_DOWNSTREAM: immediate Upstream Port of this device is "tsm_pf0" +*/ +enum pci_tsm_type { +PCI_TSM_INVALID , +PCI_TSM_PF0 , +PCI_TSM_VIRTFN , +PCI_TSM_MFD , +PCI_TSM_DOWNSTREAM , +}; + +/** +* struct pci_tsm - Core TSM context for a given PCIe endpoint +* @pdev: indicates the type of pci_tsm object
How does a pci_dev indicate a type? Maybe: Used to distinguish the type of pci_tsm object.
Quoted 5 lines
include/linux/pci-tsm.h new file +5
-0+* @type: pci_tsm object type to disambiguate PCI_TSM_DOWNSTREAM and PCI_TSM_PF0 +* +* This structure is wrapped by a low level TSM driver and returned by +* tsm_ops.probe(), it is freed by tsm_ops.remove(). Depending on +* whether @pdev is physical function 0, another physical function, or a
include/linux/pci-tsm.h line 48
Quoted 10 lines
include/linux/pci-tsm.h new file +10
-0+* virtual function determines the pci_tsm object type. E.g. see 'struct +* pci_tsm_pf0'. +*/ +struct pci_tsm { +struct pci_dev *pdev; +enum pci_tsm_type type; +}; + +/** +* struct pci_tsm_pf0 - Physical Function 0 TDISP context
Missing tsm and kernel-doc warns if docs are complete.
Quoted 10 lines
include/linux/pci-tsm.h new file +10
-0+* @state: reflect device initialized, connected, or bound +* @lock: protect @state vs pci_tsm_ops invocation +* @doe_mb: PCIe Data Object Exchange mailbox +*/ +struct pci_tsm_pf0 { +struct pci_tsm tsm; +enum pci_tsm_state state; +struct mutex lock; +struct pci_doe_mb *doe_mb; +};
Re: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Quoted 78 lines · Jonathan Cameron
Quoted 39 lines · On Thu, 15 May 2025 22:47:22 -0700 Dan Williams <[email protected]>…
The PCIe 6.1 specification, section 11, introduces the Trusted Execution Environment (TEE) Device Interface Security Protocol (TDISP). This protocol definition builds upon Component Measurement and Authentication (CMA), and link Integrity and Data Encryption (IDE). It adds support for assigning devices (PCI physical or virtual function) to a confidential VM such that the assigned device is enabled to access guest private memory protected by technologies like Intel TDX, AMD SEV-SNP, RISCV COVE, or ARM CCA.
The "TSM" (TEE Security Manager) is a concept in the TDISP specification of an agent that mediates between a "DSM" (Device Security Manager) and system software in both a VMM and a confidential VM. A VMM uses TSM ABIs to setup link security and assign devices. A confidential VM uses TSM ABIs to transition an assigned device into the TDISP "RUN" state and validate its configuration. From a Linux perspective the TSM abstracts many of the details of TDISP, IDE, and CMA. Some of those details leak through at times, but for the most part TDISP is an internal implementation detail of the TSM.
CONFIG_PCI_TSMadds an "authenticated" attribute and "tsm/" subdirectory to pci-sysfs. Consider that the TSM driver may itself be a PCI driver. Userspace can watch for the arrival of the "TSM" core device,/sys/class/tsm/tsm0/uevent, to know when the PCI core has initialized TSM services.The common verbs that the low-level TSM drivers implement are defined by 'struct
pci_tsm_ops'. For now only 'connect' and 'disconnect' are defined for secure session and IDE establishment. The 'probe' and 'remove' operations setup per-device context objects starting with 'structpci_tsm_pf0', the device Physical Function 0 that mediates communication to the device's Security Manager (DSM).The locking allows for multiple devices to be executing commands simultaneously, one outstanding command per-device and an rwsem synchronizes the implementation relative to TSM registration/unregistration events.
Thanks to Wu Hao for his work on an early draft of this support.
Quoted 7 lines
Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Acked-by Bjorn Helgaas <[email protected]>Co-developed-by Xu Yilun <[email protected]>Signed-off-by Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>Trivial stuff on this one. See inline.
Jonathan
Quoted 25 lines
--- Documentation/ABI/testing/sysfs-bus-pci | 45 +++ MAINTAINERS | 2 + drivers/pci/Kconfig | 14 + drivers/pci/Makefile | 1 + drivers/pci/pci-sysfs.c | 4 + drivers/pci/pci.h | 10 + drivers/pci/probe.c | 1 + drivers/pci/remove.c | 3 + drivers/pci/tsm.c | 437 ++++++++++++++++++++++++ drivers/virt/coco/host/tsm-core.c | 19 +- include/linux/pci-tsm.h | 138 ++++++++ include/linux/pci.h | 3 + include/linux/tsm.h | 4 +- include/uapi/linux/pci_regs.h | 1 + 14 files changed, 679 insertions(+), 3 deletions(-) create mode 100644 drivers/pci/tsm.c create mode 100644 include/linux/pci-tsm.hDocumentation/ABI/testing/sysfs-bus-pci +0
-0@@ -612,3 +612,48 @@ Description:
Quoted 7 lines
Documentation/ABI/testing/sysfs-bus-pci Description: · line 617
Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-pci +3
-0# ls doe_features 0001:01 0001:02 doe_discovery + +What: /sys/bus/pci/devices/.../tsm/ +Date: July 2024Guess the date for merge?
No, date authored / created, but the Date: tag in these ABI entries is not generally useful, just going to drop it.
Quoted 44 lines
Quoted 11 lines
Documentation/ABI/testing/sysfs-bus-pci +2
-0+Contact: [email protected] +Description:drivers/pci/tsm.c new file +2
-0@@ -0,0 +1,437 @@ +// SPDX-License-Identifier: GPL-2.0 +/* drivers/pci/tsm.c line 12
Quoted 10 lines
drivers/pci/tsm.c new file +10
-0+* TEE Security Manager for the TEE Device Interface Security Protocol +* (TDISP, PCIe r6.1 sec 11) +* +* Copyright(c) 2024 Intel Corporation. All rights reserved. +*/ + +#define dev_fmt (fmt) "TSM: " fmt + +#include <linux/bitfield.h> +#include <linux/xarray.h> Not seeing an xa stuff yet.
Check the others are all needed or push them forwards to appropriate patch.Quoted 6 lines
drivers/pci/tsm.c new file +6
-0+#include <linux/sysfs.h> + +#include <linux/pci.h> +#include <linux/pci-doe.h> +#include <linux/pci-tsm.h> +#include "pci.h" drivers/pci/tsm.c line 151
Quoted 10 lines
drivers/pci/tsm.c new file +8
-0+static bool pci_tsm_pf0_group_visible (struct kobject *kobj) +{ +struct device *dev =kobj_to_dev (kobj); +struct pci_dev *pdev =to_pci_dev (dev); + +if (pdev->tsm &&is_pci_tsm_pf0 (pdev)) +return true; +return false;Unless this is going to get more complex later
Quoted 1 line
return pdev->tsm && is_pci_tsm_pf0(pdev);
One line works for me.
Quoted 11 lines
Quoted 9 lines
drivers/pci/tsm.c new file +7
-0+} +DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE (pci_tsm_pf0); + +const struct attribute_group pci_tsm_auth_attr_group = { + .attrs = pci_tsm_auth_attrs, + .is_visible =SYSFS_GROUP_VISIBLE (pci_tsm_pf0), +};
Quoted 7 lines
drivers/pci/tsm.c line 301
Quoted 5 lines
drivers/pci/tsm.c new file +5
-0+static void pci_tsm_pf0_init (struct pci_dev *pdev) +{ +bool tee_cap; + + tee_cap = pdev->devcap &PCI_EXP_DEVCAP_TEE ;Might as well put that on first line.
Done.
Quoted 62 lines
Quoted 62 lines
drivers/pci/tsm.c new file +3
-0+ +if (!(pdev->ide_cap || tee_cap)) +return ;drivers/virt/coco/host/tsm-core.c +15
-2@@ -8,11 +8,13 @@#include <linux/device.h> #include <linux/module.h> #include <linux/cleanup.h> +#include <linux/pci-tsm.h> static DECLARE_RWSEM (tsm_core_rwsem);static struct class *tsm_class;static struct tsm_core_dev {struct device dev; +const struct pci_tsm_ops *pci_ops; } *tsm_core; static struct tsm_core_dev *@@ -39,7 +41,8 @@ static void put_tsm_core(struct tsm_core_dev *core) DEFINE_FREE(put_tsm_core, struct tsm_core_dev *, if (!IS_ERR_OR_NULL(_T)) put_tsm_core(_T))struct tsm_core_dev *tsm_register (struct device *parent, -const struct attribute_group **groups) +const struct attribute_group **groups, +const struct pci_tsm_ops *pci_ops) {struct device *dev;int rc;@@ -61,10 +64,20 @@ struct tsm_core_dev *tsm_register(struct device *parent,if (rc)return ERR_PTR (rc); + rc =pci_tsm_core_register (pci_ops,NULL ); +if (rc) { +dev_err (parent,"PCI initialization failure: %pe\n" , +ERR_PTR (rc)); +return ERR_PTR (rc); + } + rc =device_add (dev); -if (rc) +if (rc) { +pci_tsm_core_unregister (pci_ops);return ERR_PTR (rc); + } + core->pci_ops = pci_ops; tsm_core =no_free_ptr (core);return tsm_core;@@ -79,7 +92,9 @@ void tsm_unregister(struct tsm_core_dev *core)return ; }
Quoted 7 lines
drivers/virt/coco/host/tsm-core.c void tsm_unregister(struct tsm_core_dev *core) · line 96
Quoted 2 lines
drivers/virt/coco/host/tsm-core.c +1
-0+pci_tsm_core_unregister (core->pci_ops);device_unregister (&core->dev);Using
device_initialize()anddevice_add()in probe butdevice_unregister()in remove results in trivial ordering mess like this. I'd split theremove()path so we can take down in the reverse of setup withpci_tsm_core_unregister()betweendevice_del()andput_device()
Turns out in the new version I came to the same conclusion.
Quoted 6 lines
This ordering thing is common enough though that maybe we can just not worry about it.
drivers/virt/coco/host/tsm-core.c void tsm_unregister(struct tsm_core_dev *core) · line 97
Quoted 1 line
drivers/virt/coco/host/tsm-core.c +1
-0+Push whitespace change back to earlier patch.
Lost that whitespace along the way...
Quoted 11 lines
Quoted 11 lines
drivers/virt/coco/host/tsm-core.c +0
-0tsm_core =NULL ; }EXPORT_SYMBOL_GPL (tsm_unregister);include/linux/pci-tsm.h new file +1
-0@@ -0,0 +1,138 @@ +/* SPDX-License-Identifier: GPL-2.0 */
Quoted 8 lines
include/linux/pci-tsm.h line 7
Quoted 6 lines
include/linux/pci-tsm.h new file +6
-0+#ifndef __PCI_TSM_H +#define __PCI_TSM_H +#include <linux/mutex.h> +#include <linux/pci.h> + +struct pci_dev ;Given you have
linux/pci.hno need for the forwards def.
Ack.
Quoted 14 lines
include/linux/pci-tsm.h line 16
Quoted 11 lines
include/linux/pci-tsm.h new file +9
-0+ +enum pci_tsm_state { +PCI_TSM_ERR =-1 , +PCI_TSM_INIT , +PCI_TSM_CONNECT , +}; + +/** +* enum pci_tsm_type - 'struct pci_tsm' object types Kernel-doc should warn on incomplete docs. I'd add trivial comment for INVALID to avoid that.
pci_tsm_type is gone in the new version per Aneesh's insight that it can be derived from other data in the pci_tsm context.
Quoted 7 lines
include/linux/pci-tsm.h line 20
Quoted 4 lines
include/linux/pci-tsm.h new file +4
-0+* @PCI_TSM_PF0: function0 that hosts a DOE mailbox that comprehends an +* Interface ID per potential TDI +* @PCI_TSM_VIRTFN: physfn-0 of this device is "tsm_pf0" +* @PCI_TSM_MFD: function0 of this device is "tsm_pf0" Double space after "is" seems odd.
Quoted 15 lines
include/linux/pci-tsm.h line 33
Quoted 13 lines
include/linux/pci-tsm.h new file +13
-0+* @PCI_TSM_DOWNSTREAM: immediate Upstream Port of this device is "tsm_pf0" +*/ +enum pci_tsm_type { +PCI_TSM_INVALID , +PCI_TSM_PF0 , +PCI_TSM_VIRTFN , +PCI_TSM_MFD , +PCI_TSM_DOWNSTREAM , +}; + +/** +* struct pci_tsm - Core TSM context for a given PCIe endpoint +* @pdev: indicates the type of pci_tsm object How does a
pci_devindicate a type? Maybe: Used to distinguish the type ofpci_tsmobject.
Changed to:
@pdev: Back ref to device function, distinguishes type of pci_tsm context.
Quoted 10 lines
Quoted 10 lines
include/linux/pci-tsm.h new file +9
-0+* @type: pci_tsm object type to disambiguate PCI_TSM_DOWNSTREAM and PCI_TSM_PF0 +* +* This structure is wrapped by a low level TSM driver and returned by +* tsm_ops.probe(), it is freed by tsm_ops.remove(). Depending on +* whether @pdev is physical function 0, another physical function, or a +* virtual function determines the pci_tsm object type. E.g. see 'struct +* pci_tsm_pf0'. +*/ +struct pci_tsm {
Quoted 8 lines
include/linux/pci-tsm.h line 48
Quoted 6 lines
include/linux/pci-tsm.h new file +6
-0+struct pci_dev *pdev; +enum pci_tsm_type type; +}; + +/** +* struct pci_tsm_pf0 - Physical Function 0 TDISP context Missing tsm and kernel-doc warns if docs are complete.
Went ahead and added a simple Documentation/drivers-api/pci/tsm.rst that includes pci-tsm.h and tsm.c. Yes, this warning was still there in the latest version.
Alexey Kardashevskiy
maintainerRe: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
In reply to Dan WilliamsQuoted 495 lines · Dan Williams
The PCIe 6.1 specification, section 11, introduces the Trusted Execution Environment (TEE) Device Interface Security Protocol (TDISP). This protocol definition builds upon Component Measurement and Authentication (CMA), and link Integrity and Data Encryption (IDE). It adds support for assigning devices (PCI physical or virtual function) to a confidential VM such that the assigned device is enabled to access guest private memory protected by technologies like Intel TDX, AMD SEV-SNP, RISCV COVE, or ARM CCA.
The "TSM" (TEE Security Manager) is a concept in the TDISP specification of an agent that mediates between a "DSM" (Device Security Manager) and system software in both a VMM and a confidential VM. A VMM uses TSM ABIs to setup link security and assign devices. A confidential VM uses TSM ABIs to transition an assigned device into the TDISP "RUN" state and validate its configuration. From a Linux perspective the TSM abstracts many of the details of TDISP, IDE, and CMA. Some of those details leak through at times, but for the most part TDISP is an internal implementation detail of the TSM.
CONFIG_PCI_TSMadds an "authenticated" attribute and "tsm/" subdirectory to pci-sysfs. Consider that the TSM driver may itself be a PCI driver. Userspace can watch for the arrival of the "TSM" core device,/sys/class/tsm/tsm0/uevent, to know when the PCI core has initialized TSM services.The common verbs that the low-level TSM drivers implement are defined by 'struct
pci_tsm_ops'. For now only 'connect' and 'disconnect' are defined for secure session and IDE establishment. The 'probe' and 'remove' operations setup per-device context objects starting with 'structpci_tsm_pf0', the device Physical Function 0 that mediates communication to the device's Security Manager (DSM).The locking allows for multiple devices to be executing commands simultaneously, one outstanding command per-device and an rwsem synchronizes the implementation relative to TSM registration/unregistration events.
Thanks to Wu Hao for his work on an early draft of this support.
Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Acked-by Bjorn Helgaas <[email protected]>Co-developed-by Xu Yilun <[email protected]>Signed-off-by Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>--- Documentation/ABI/testing/sysfs-bus-pci | 45 +++ MAINTAINERS | 2 + drivers/pci/Kconfig | 14 + drivers/pci/Makefile | 1 + drivers/pci/pci-sysfs.c | 4 + drivers/pci/pci.h | 10 + drivers/pci/probe.c | 1 + drivers/pci/remove.c | 3 + drivers/pci/tsm.c | 437 ++++++++++++++++++++++++ drivers/virt/coco/host/tsm-core.c | 19 +- include/linux/pci-tsm.h | 138 ++++++++ include/linux/pci.h | 3 + include/linux/tsm.h | 4 +- include/uapi/linux/pci_regs.h | 1 + 14 files changed, 679 insertions(+), 3 deletions(-)create mode 100644
drivers/pci/tsm.c
create mode 100644include/linux/pci-tsm.hDocumentation/ABI/testing/sysfs-bus-pci +45
-0@@ -612,3 +612,48 @@ Description: # ls doe_features 0001:01 0001:02 doe_discovery + +What: /sys/bus/pci/devices/.../tsm/ +Date: July 2024 +Contact: [email protected] +Description: + This directory only appears if a physical device function + supports authentication (PCIe CMA-SPDM), interface security + (PCIe TDISP), and is accepted for secure operation by the + platform TSM driver. This attribute directory appears + dynamically after the platform TSM driver loads. So, only after + the /sys/class/tsm/tsm0 device arrives can tools assume that + devices without a tsm/ attribute directory will never have one, + before that, the security capabilities of the device relative to + the platform TSM are unknown. See + Documentation/ABI/testing/sysfs-class-tsm. + +What: /sys/bus/pci/devices/.../tsm/connect +Date: July 2024 +Contact: [email protected] +Description: + (RW) Writing "1" to this file triggers the platform TSM (TEE + Security Manager) to establish a connection with the device. + This typically includes an SPDM (DMTF Security Protocols and + Data Models) session over PCIe DOE (Data Object Exchange) and + may also include PCIe IDE (Integrity and Data Encryption) + establishment. + +What: /sys/bus/pci/devices/.../authenticated +Date: July 2024 +Contact: [email protected] +Description: + When the device's tsm/ directory is present device + authentication (PCIe CMA-SPDM) and link encryption (PCIe IDE) + are handled by the platform TSM (TEE Security Manager). When the + tsm/ directory is not present this attribute reflects only the + native CMA-SPDM authentication state with the kernel's + certificate store. + + If the attribute is not present, it indicates that + authentication is unsupported by the device, or the TSM has no + available authentication methods for the device. + + When present and the tsm/ attribute directory is present, the + authenticated attribute is an alias for the device 'connect' + state. See the 'tsm/connect' attribute for more details.MAINTAINERS +2
-0@@ -24560,8 +24560,10 @@ M: Dan Williams <[email protected]> L: [email protected] S: Maintained F: Documentation/ABI/testing/configfs-tsm-report +F: drivers/pci/tsm.c F: drivers/virt/coco/guest/ F: drivers/virt/coco/host/ +F: include/linux/pci-tsm.h F: include/linux/tsm.h TRUSTED SERVICES TEE DRIVERdrivers/pci/Kconfig +14
-0@@ -135,6 +135,20 @@ config PCI_IDE_STREAM_MAX track the maximum possibility of 256 streams per host bridge in the typical case. +config PCI_TSM + bool "PCI TSM: Device security protocol support" + select PCI_IDE + select PCI_DOE + help + The TEE (Trusted Execution Environment) Device Interface + Security Protocol (TDISP) defines a "TSM" as a platform agent + that manages device authentication, link encryption, link + integrity protection, and assignment of PCI device functions + (virtual or physical) to confidential computing VMs that can + access (DMA) guest private memory. + + Enable a platform TSM driver to use this capability. + config PCI_DOE bool "Enable PCI Data Object Exchange (DOE) support" helpdrivers/pci/Makefile +1
-0@@ -35,6 +35,7 @@ obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o obj-$(CONFIG_VGA_ARB) += vgaarb.o obj-$(CONFIG_PCI_DOE) += doe.o obj-$(CONFIG_PCI_IDE) += ide.o +obj-$(CONFIG_PCI_TSM) += tsm.o obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o obj-$(CONFIG_PCI_NPEM) += npem.o obj-$(CONFIG_PCIE_TPH) += tph.odrivers/pci/pci-sysfs.c +4
-0@@ -1811,6 +1811,10 @@ const struct attribute_group *pci_dev_attr_groups[] = {#endif #ifdef CONFIG_PCI_DOE &pci_doe_sysfs_group, +#endif +#ifdef CONFIG_PCI_TSM + &pci_tsm_auth_attr_group, + &pci_tsm_pf0_attr_group, #endif NULL, };drivers/pci/pci.h +10
-0@@ -517,6 +517,16 @@ void pci_ide_init(struct pci_dev *dev);static inline void pci_ide_init (struct pci_dev *dev) { }#endif +#ifdef CONFIG_PCI_TSM +void pci_tsm_init (struct pci_dev *pdev); +void pci_tsm_destroy (struct pci_dev *pdev); +extern const struct attribute_group pci_tsm_pf0_attr_group; +extern const struct attribute_group pci_tsm_auth_attr_group; +#else +static inline void pci_tsm_init (struct pci_dev *pdev) { } +static inline void pci_tsm_destroy (struct pci_dev *pdev) { } +#endif +/** * pci_dev_set_io_state - Set the new error state if possible. * drivers/pci/probe.c +1
-0@@ -2620,6 +2620,7 @@ static void pci_init_capabilities(struct pci_dev *dev)pci_tph_init (dev);/* TLP Processing Hints */ pci_rebar_init (dev);/* Resizable BAR */ pci_ide_init (dev);/* Link Integrity and Data Encryption */ +pci_tsm_init (dev);/* TEE Security Manager connection */ pcie_report_downtraining (dev);pci_init_reset_methods (dev);drivers/pci/remove.c +3
-0@@ -55,6 +55,9 @@ static void pci_destroy_dev(struct pci_dev *dev)pci_doe_sysfs_teardown (dev);pci_npem_remove (dev); +/* before device_del() to keep config cycle access */ +pci_tsm_destroy (dev); +device_del (&dev->dev);down_write (&pci_bus_sem);drivers/pci/tsm.c new file +257
-0@@ -0,0 +1,437 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* TEE Security Manager for the TEE Device Interface Security Protocol +* (TDISP, PCIe r6.1 sec 11) +* +* Copyright(c) 2024 Intel Corporation. All rights reserved. +*/ + +#define dev_fmt (fmt) "TSM: " fmt + +#include <linux/bitfield.h> +#include <linux/xarray.h> +#include <linux/sysfs.h> + +#include <linux/pci.h> +#include <linux/pci-doe.h> +#include <linux/pci-tsm.h> +#include "pci.h" + +/* +* Provide a read/write lock against the init / exit of pdev tsm +* capabilities and arrival/departure of a tsm instance +*/ +static DECLARE_RWSEM (pci_tsm_rwsem); +static const struct pci_tsm_ops *tsm_ops; + +/* supplemental attributes to surface when pci_tsm_attr_group is active */ +static const struct attribute_group *pci_tsm_owner_attr_group; + +static struct pci_tsm_pf0 *to_pci_tsm_pf0 (struct pci_tsm *pci_tsm) +{ +struct pci_dev *pdev = pci_tsm->pdev; + +if (!is_pci_tsm_pf0 (pdev) || pci_tsm->type !=PCI_TSM_PF0 ) { +dev_WARN_ONCE (&pdev->dev,1 ,"invalid context object\n" ); +return NULL ; + } + + return container_of(pci_tsm, struct pci_tsm_pf0, tsm); +} + +/* TODO: switch to ACQUIRE() and ACQUIRE_ERR() */ +static struct mutex *tsm_ops_lock (struct pci_tsm_pf0 *tsm) +{ +lockdep_assert_held (&pci_tsm_rwsem); + +if (mutex_lock_interruptible (&tsm->lock) !=0 ) +return NULL ; +return &tsm->lock; +} +DEFINE_FREE(tsm_ops_unlock, struct mutex *, if (_T) mutex_unlock(_T)) + +static int pci_tsm_disconnect (struct pci_dev *pdev) +{ +struct pci_tsm_pf0 *tsm =to_pci_tsm_pf0 (pdev->tsm); + + struct mutex *lock __free(tsm_ops_unlock) = tsm_ops_lock(tsm); +if (!lock) +return -EINTR ; + +if (tsm->state <PCI_TSM_INIT ) +return -ENXIO ; +if (tsm->state <PCI_TSM_CONNECT ) +return 0 ; + + tsm_ops->disconnect (pdev); + tsm->state =PCI_TSM_INIT ; + +return 0 ; +} + +static int pci_tsm_connect (struct pci_dev *pdev) +{ +struct pci_tsm_pf0 *tsm =to_pci_tsm_pf0 (pdev->tsm); +int rc; + + struct mutex *lock __free(tsm_ops_unlock) = tsm_ops_lock(tsm); +if (!lock) +return -EINTR ; + +if (tsm->state <PCI_TSM_INIT ) +return -ENXIO ; +if (tsm->state >=PCI_TSM_CONNECT ) +return 0 ; + + rc = tsm_ops->connect (pdev); +if (rc) +return rc; + tsm->state =PCI_TSM_CONNECT ; +return 0 ; +} + +/* TODO: switch to ACQUIRE() and ACQUIRE_ERR() */ +static struct rw_semaphore *tsm_read_lock (void ) +{ +if (down_read_interruptible (&pci_tsm_rwsem)) +return NULL ; +return &pci_tsm_rwsem; +} +DEFINE_FREE(tsm_read_unlock, struct rw_semaphore *, if (_T) up_read(_T)) + +static ssize_t connect_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t len) +{ +int rc; +bool connect; +struct pci_dev *pdev =to_pci_dev (dev); + + rc =kstrtobool (buf, &connect); +if (rc) +return rc; + + struct rw_semaphore *lock __free(tsm_read_unlock) = tsm_read_lock(); +if (!lock) +return -EINTR ; + +if (connect) + rc =pci_tsm_connect (pdev); +else + rc =pci_tsm_disconnect (pdev); +if (rc) +return rc; +return len; +} + +static ssize_t connect_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct pci_tsm_pf0 *tsm; + + struct rw_semaphore *lock __free(tsm_read_unlock) = tsm_read_lock(); +if (!lock) +return -EINTR ; + +if (!pdev->tsm) +return -ENXIO ; + + tsm =to_pci_tsm_pf0 (pdev->tsm); +return sysfs_emit (buf,"%d\n" , tsm->state >=PCI_TSM_CONNECT ); +} +static DEVICE_ATTR_RW (connect); + +static bool pci_tsm_pf0_group_visible (struct kobject *kobj) +{ +struct device *dev =kobj_to_dev (kobj); +struct pci_dev *pdev =to_pci_dev (dev); + +if (pdev->tsm &&is_pci_tsm_pf0 (pdev)) +return true; +return false; +} +DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE (pci_tsm_pf0); + +static struct attribute *pci_tsm_pf0_attrs[] = { + &dev_attr_connect.attr, +NULL +}; + +const struct attribute_group pci_tsm_pf0_attr_group = { + .name ="tsm" , + .attrs = pci_tsm_pf0_attrs, + .is_visible =SYSFS_GROUP_VISIBLE (pci_tsm_pf0), +}; + +static ssize_t authenticated_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +/* +* When device authentication is TSM owned, 'authenticated' is +* identical to the connect state. +*/ +return connect_show (dev, attr, buf); +} +static DEVICE_ATTR_RO (authenticated); + +static struct attribute *pci_tsm_auth_attrs[] = { + &dev_attr_authenticated.attr, +NULL +}; + +const struct attribute_group pci_tsm_auth_attr_group = { + .attrs = pci_tsm_auth_attrs, + .is_visible =SYSFS_GROUP_VISIBLE (pci_tsm_pf0), +}; + +/* +* Retrieve physical function0 device whether it has TEE capability or not +*/ +static struct pci_dev *pf0_dev_get (struct pci_dev *pdev) +{ +struct pci_dev *pf_dev =pci_physfn (pdev); + +if (PCI_FUNC (pf_dev->devfn) ==0 ) +return pci_dev_get (pf_dev); + +return pci_get_slot (pf_dev->bus, + pf_dev->devfn -PCI_FUNC (pf_dev->devfn)); +} + +static bool is_pci_tsm_downstream (struct pci_dev *pdev) +{ +struct pci_dev *uport; + +if (pci_pcie_type (pdev) !=PCI_EXP_TYPE_ENDPOINT ) +return false; + +/* "grandparent" of an endpoint is an Upstream Port (or Root Port) */ +if (!pdev->dev.parent) +return false; +if (!pdev->dev.parent->parent) +return false; + + uport =to_pci_dev (pdev->dev.parent->parent); +if (pci_pcie_type (uport) !=PCI_EXP_TYPE_UPSTREAM ) +return false; + +if (!uport->tsm) +return false; + +/* Upstream Port has a 'tsm' context, probe downstream devices. */ +return true; +} + +static enum pci_tsm_type pci_tsm_type (struct pci_dev *pdev) +{ +if (is_pci_tsm_pf0 (pdev)) +return PCI_TSM_PF0 ; + + struct pci_dev *pf0 __free(pci_dev_put) = pf0_dev_get(pdev); +if (!pf0) +return PCI_TSM_INVALID ; + +if (pf0->tsm && pf0->tsm->type ==PCI_TSM_PF0 ) { +if (pdev->is_virtfn) +return PCI_TSM_VIRTFN ; +else +return PCI_TSM_MFD ; + } + +/* +* Allow for Device Security Managers (DSMs) at a Switch level +* to host TDISP services for downstream devices +*/ +if (is_pci_tsm_downstream (pdev)) +return PCI_TSM_DOWNSTREAM ; +return PCI_TSM_INVALID ; +} + +/** +* pci_tsm_initialize() - base 'struct pci_tsm' initialization +* @pdev: The PCI device +* @tsm: context to initialize +*/ +void pci_tsm_initialize (struct pci_dev *pdev,struct pci_tsm *tsm) +{ + tsm->type =pci_tsm_type (pdev);
drivers/pci/tsm.c line 267
Quoted 10 lines
drivers/pci/tsm.c new file +10
-0+ tsm->pdev = pdev; +} +EXPORT_SYMBOL_GPL (pci_tsm_initialize); + +/** +* pci_tsm_pf0_initialize() - common 'struct pci_tsm_pf0' initialization +* @pdev: Physical Function 0 PCI device +* @tsm: context to initialize +*/ +int pci_tsm_pf0_initialize (struct pci_dev *pdev,struct pci_tsm_pf0 *tsm)
Here it is: struct pci_tsm_pf0 *tsm (it is really a "dsm") In pci_tsm: struct pci_dev *dsm (alright)
May be we need some distinction between PF0's pci_dev and pci_tsm_pf0 but still these are DSMs.
In pci_tsm_pf0 it is: struct pci_tsm tsm, imho "base" is less confusing (I keep catching myself thinking it is a pointer to tsm_dev).
"tsm" would be what you call "tsm_dev" which is ok but seeing short "tsm" used as "dsm" or "TSM data for this pci_dev" is confusing.
s/pci_tsm/pci_tsm_ctx/ and s/tsm/tsm_ctx/ ? Thanks,
Signature · 1 line
Alexey
Re: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Alexey Kardashevskiy wrote:
[..]
[trim multiple pages of uncommented context, please trim your replies]
Quoted 8 lines
drivers/pci/tsm.c line 267
Quoted 6 lines
drivers/pci/tsm.c new file +6
-0+/** +* pci_tsm_pf0_initialize() - common 'struct pci_tsm_pf0' initialization +* @pdev: Physical Function 0 PCI device +* @tsm: context to initialize +*/ +int pci_tsm_pf0_initialize (struct pci_dev *pdev,struct pci_tsm_pf0 *tsm)Here it is: struct
pci_tsm_pf0*tsm (it is really a "dsm")
All of the context returned by the TSM driver is a "tsm" context, the only time use "dsm" is in referring to the actual pci_dev that runs the SPDM session.
Quoted 5 lines
In
pci_tsm: structpci_dev*dsm (alright)May be we need some distinction between PF0's
pci_devandpci_tsm_pf0but still these are DSMs.In
pci_tsm_pf0it is: structpci_tsmtsm, imho "base" is less confusing (I keep catching myself thinking it is a pointer totsm_dev).
Ok, I can change it to base.
Quoted 3 lines
"tsm" would be what you call "
tsm_dev" which is ok but seeing short "tsm" used as "dsm" or "TSM data for thispci_dev" is confusing.s/pci_tsm/pci_tsm_ctx/ and s/tsm/tsm_ctx/ ? Thanks,
What is a tsm_ctx? The s/pci_tsm/pci_tsm_ctx/ rename is not adding more clarity for me. If Aneesh or Yilun also find that rename clarifying I will switch. For v5 I will stick with 'struct pci_tsm' as the PCI object that wraps TSM driver produced objects.
The reason I do not think of "pci_tsm" as a "tsm_dev" is because PCI is always a consumer of an outside of PCI TSM service provided, PCI does not have a TSM concept internal to it.
Alexey Kardashevskiy
maintainerRe: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Quoted 13 lines · On 27/8/25 09:54, [email protected] wrote:
Alexey Kardashevskiy wrote:
[..]
[trim multiple pages of uncommented context, please trim your replies]Quoted 8 lines
drivers/pci/tsm.c line 267
Quoted 6 lines
drivers/pci/tsm.c new file +6
-0+/** +* pci_tsm_pf0_initialize() - common 'struct pci_tsm_pf0' initialization +* @pdev: Physical Function 0 PCI device +* @tsm: context to initialize +*/ +int pci_tsm_pf0_initialize (struct pci_dev *pdev,struct pci_tsm_pf0 *tsm)Here it is: struct
pci_tsm_pf0*tsm (it is really a "dsm")
Quoted 3 lines
All of the context returned by the TSM driver is a "tsm" context, the only time use "dsm" is in referring to the actual
pci_devthat runs the SPDM session.
ah ok. Just seems a bit counterintuitive to use a short "tsm" acronym for something else than the actual TSM as defined in the PCI spec and in this case - it is the opposite to the spec's "TSM" - it is a DSM, the other end of trust chain. And if I wanted to reference the actual TSM in the same function - that would be "tsm_dev". And the actual DSM pci_dev is barely used, it is mostly "struct pci_tsm_pf0".
Quoted 6 lines
Quoted 5 lines
In
pci_tsm: structpci_dev*dsm (alright)May be we need some distinction between PF0's
pci_devandpci_tsm_pf0but still these are DSMs.In
pci_tsm_pf0it is: structpci_tsmtsm, imho "base" is less confusing (I keep catching myself thinking it is a pointer totsm_dev).
Quoted 8 lines
Ok, I can change it to base.
Quoted 3 lines
"tsm" would be what you call "
tsm_dev" which is ok but seeing short "tsm" used as "dsm" or "TSM data for thispci_dev" is confusing.s/pci_tsm/pci_tsm_ctx/ and s/tsm/tsm_ctx/ ? Thanks,
What is a
tsm_ctx? The s/pci_tsm/pci_tsm_ctx/ rename is not adding more clarity for me.
TSM-related attributes of a PCI device. Not the best name, true.
Quoted 3 lines
If Aneesh or Yilun also find that rename clarifying I
will switch. For v5 I will stick with 'structpci_tsm' as the PCI object that wraps TSM driver produced objects.
yeah, if it is just me, then never mind, I'll get used to it.
Quoted 3 lines
The reason I do not think of "
pci_tsm" as a "tsm_dev" is because PCI is always a consumer of an outside of PCI TSM service provided, PCI does not have a TSM concept internal to it.
"struct pci_dev" describes a PCI device, "struct pci_ide" - a PCI IDE stream but "struct pci_tsm" does not describe PCI TSM... Hm. Thanks,
Signature · 1 line
Alexey
Re: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Quoted 16 lines · Alexey Kardashevskiy
Quoted 13 lines · On 27/8/25 09:54, [email protected] wrote:
Alexey Kardashevskiy wrote:
[..]
[trim multiple pages of uncommented context, please trim your replies]Quoted 8 lines
drivers/pci/tsm.c line 267
Quoted 6 lines
drivers/pci/tsm.c new file +6
-0+/** +* pci_tsm_pf0_initialize() - common 'struct pci_tsm_pf0' initialization +* @pdev: Physical Function 0 PCI device +* @tsm: context to initialize +*/ +int pci_tsm_pf0_initialize (struct pci_dev *pdev,struct pci_tsm_pf0 *tsm)Here it is: struct
pci_tsm_pf0*tsm (it is really a "dsm")
Quoted 5 lines
Quoted 3 lines
All of the context returned by the TSM driver is a "tsm" context, the only time use "dsm" is in referring to the actual
pci_devthat runs the SPDM session.ah ok. Just seems a bit counterintuitive to use a short "tsm" acronym for something else than the actual TSM as defined in the PCI spec and in this case - it is the opposite to the spec's "TSM" - it is a DSM, the other end of trust chain. And if I wanted to reference the actual TSM in the same function - that would be "tsm_dev". And the actual DSM
pci_devis barely used, it is mostly "structpci_tsm_pf0".
The "dsm" pci_dev is used for type-checking assignable subfunctions vs the device that runs the SPDM session, and it is used as the device to lock when running operations on any subfunction.
Quoted 20 lines
Quoted 6 lines
Quoted 5 lines
In
pci_tsm: structpci_dev*dsm (alright)May be we need some distinction between PF0's
pci_devandpci_tsm_pf0but still these are DSMs.In
pci_tsm_pf0it is: structpci_tsmtsm, imho "base" is less confusing (I keep catching myself thinking it is a pointer totsm_dev).Quoted 8 lines
Ok, I can change it to base.
Quoted 3 lines
"tsm" would be what you call "
tsm_dev" which is ok but seeing short "tsm" used as "dsm" or "TSM data for thispci_dev" is confusing.s/pci_tsm/pci_tsm_ctx/ and s/tsm/tsm_ctx/ ? Thanks,
What is a
tsm_ctx? The s/pci_tsm/pci_tsm_ctx/ rename is not adding more clarity for me.TSM-related attributes of a PCI device. Not the best name, true.
Quoted 3 lines
If Aneesh or Yilun also find that rename clarifying I
will switch. For v5 I will stick with 'structpci_tsm' as the PCI object that wraps TSM driver produced objects.
Quoted 6 lines
yeah, if it is just me, then never mind, I'll get used to it.
Quoted 3 lines
The reason I do not think of "
pci_tsm" as a "tsm_dev" is because PCI is always a consumer of an outside of PCI TSM service provided, PCI does not have a TSM concept internal to it."struct
pci_dev" describes a PCI device, "structpci_ide" - a PCI IDE stream but "structpci_tsm" does not describe PCI TSM... Hm. Thanks,
How about keep ->tsm as the short name in 'struct pci_dev', but rename the type 'struct pci_tsm_ctl'? It is a core control data structure for TSM operations. It is not a "context" like device drvdata where the owner can do whatever it wants, it is a core control structure that a low-level TSM driver can wrap with its own control attributes.
Alexey Kardashevskiy
maintainerRe: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
In reply to Dan WilliamsQuoted 651 lines · Dan Williams
The PCIe 6.1 specification, section 11, introduces the Trusted Execution Environment (TEE) Device Interface Security Protocol (TDISP). This protocol definition builds upon Component Measurement and Authentication (CMA), and link Integrity and Data Encryption (IDE). It adds support for assigning devices (PCI physical or virtual function) to a confidential VM such that the assigned device is enabled to access guest private memory protected by technologies like Intel TDX, AMD SEV-SNP, RISCV COVE, or ARM CCA.
The "TSM" (TEE Security Manager) is a concept in the TDISP specification of an agent that mediates between a "DSM" (Device Security Manager) and system software in both a VMM and a confidential VM. A VMM uses TSM ABIs to setup link security and assign devices. A confidential VM uses TSM ABIs to transition an assigned device into the TDISP "RUN" state and validate its configuration. From a Linux perspective the TSM abstracts many of the details of TDISP, IDE, and CMA. Some of those details leak through at times, but for the most part TDISP is an internal implementation detail of the TSM.
CONFIG_PCI_TSMadds an "authenticated" attribute and "tsm/" subdirectory to pci-sysfs. Consider that the TSM driver may itself be a PCI driver. Userspace can watch for the arrival of the "TSM" core device,/sys/class/tsm/tsm0/uevent, to know when the PCI core has initialized TSM services.The common verbs that the low-level TSM drivers implement are defined by 'struct
pci_tsm_ops'. For now only 'connect' and 'disconnect' are defined for secure session and IDE establishment. The 'probe' and 'remove' operations setup per-device context objects starting with 'structpci_tsm_pf0', the device Physical Function 0 that mediates communication to the device's Security Manager (DSM).The locking allows for multiple devices to be executing commands simultaneously, one outstanding command per-device and an rwsem synchronizes the implementation relative to TSM registration/unregistration events.
Thanks to Wu Hao for his work on an early draft of this support.
Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Acked-by Bjorn Helgaas <[email protected]>Co-developed-by Xu Yilun <[email protected]>Signed-off-by Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>--- Documentation/ABI/testing/sysfs-bus-pci | 45 +++ MAINTAINERS | 2 + drivers/pci/Kconfig | 14 + drivers/pci/Makefile | 1 + drivers/pci/pci-sysfs.c | 4 + drivers/pci/pci.h | 10 + drivers/pci/probe.c | 1 + drivers/pci/remove.c | 3 + drivers/pci/tsm.c | 437 ++++++++++++++++++++++++ drivers/virt/coco/host/tsm-core.c | 19 +- include/linux/pci-tsm.h | 138 ++++++++ include/linux/pci.h | 3 + include/linux/tsm.h | 4 +- include/uapi/linux/pci_regs.h | 1 + 14 files changed, 679 insertions(+), 3 deletions(-)create mode 100644
drivers/pci/tsm.c
create mode 100644include/linux/pci-tsm.hDocumentation/ABI/testing/sysfs-bus-pci +45
-0@@ -612,3 +612,48 @@ Description: # ls doe_features 0001:01 0001:02 doe_discovery + +What: /sys/bus/pci/devices/.../tsm/ +Date: July 2024 +Contact: [email protected] +Description: + This directory only appears if a physical device function + supports authentication (PCIe CMA-SPDM), interface security + (PCIe TDISP), and is accepted for secure operation by the + platform TSM driver. This attribute directory appears + dynamically after the platform TSM driver loads. So, only after + the /sys/class/tsm/tsm0 device arrives can tools assume that + devices without a tsm/ attribute directory will never have one, + before that, the security capabilities of the device relative to + the platform TSM are unknown. See + Documentation/ABI/testing/sysfs-class-tsm. + +What: /sys/bus/pci/devices/.../tsm/connect +Date: July 2024 +Contact: [email protected] +Description: + (RW) Writing "1" to this file triggers the platform TSM (TEE + Security Manager) to establish a connection with the device. + This typically includes an SPDM (DMTF Security Protocols and + Data Models) session over PCIe DOE (Data Object Exchange) and + may also include PCIe IDE (Integrity and Data Encryption) + establishment. + +What: /sys/bus/pci/devices/.../authenticated +Date: July 2024 +Contact: [email protected] +Description: + When the device's tsm/ directory is present device + authentication (PCIe CMA-SPDM) and link encryption (PCIe IDE) + are handled by the platform TSM (TEE Security Manager). When the + tsm/ directory is not present this attribute reflects only the + native CMA-SPDM authentication state with the kernel's + certificate store. + + If the attribute is not present, it indicates that + authentication is unsupported by the device, or the TSM has no + available authentication methods for the device. + + When present and the tsm/ attribute directory is present, the + authenticated attribute is an alias for the device 'connect' + state. See the 'tsm/connect' attribute for more details.MAINTAINERS +2
-0@@ -24560,8 +24560,10 @@ M: Dan Williams <[email protected]> L: [email protected] S: Maintained F: Documentation/ABI/testing/configfs-tsm-report +F: drivers/pci/tsm.c F: drivers/virt/coco/guest/ F: drivers/virt/coco/host/ +F: include/linux/pci-tsm.h F: include/linux/tsm.h TRUSTED SERVICES TEE DRIVERdrivers/pci/Kconfig +14
-0@@ -135,6 +135,20 @@ config PCI_IDE_STREAM_MAX track the maximum possibility of 256 streams per host bridge in the typical case. +config PCI_TSM + bool "PCI TSM: Device security protocol support" + select PCI_IDE + select PCI_DOE + help + The TEE (Trusted Execution Environment) Device Interface + Security Protocol (TDISP) defines a "TSM" as a platform agent + that manages device authentication, link encryption, link + integrity protection, and assignment of PCI device functions + (virtual or physical) to confidential computing VMs that can + access (DMA) guest private memory. + + Enable a platform TSM driver to use this capability. + config PCI_DOE bool "Enable PCI Data Object Exchange (DOE) support" helpdrivers/pci/Makefile +1
-0@@ -35,6 +35,7 @@ obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o obj-$(CONFIG_VGA_ARB) += vgaarb.o obj-$(CONFIG_PCI_DOE) += doe.o obj-$(CONFIG_PCI_IDE) += ide.o +obj-$(CONFIG_PCI_TSM) += tsm.o obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o obj-$(CONFIG_PCI_NPEM) += npem.o obj-$(CONFIG_PCIE_TPH) += tph.odrivers/pci/pci-sysfs.c +4
-0@@ -1811,6 +1811,10 @@ const struct attribute_group *pci_dev_attr_groups[] = {#endif #ifdef CONFIG_PCI_DOE &pci_doe_sysfs_group, +#endif +#ifdef CONFIG_PCI_TSM + &pci_tsm_auth_attr_group, + &pci_tsm_pf0_attr_group, #endif NULL, };drivers/pci/pci.h +10
-0@@ -517,6 +517,16 @@ void pci_ide_init(struct pci_dev *dev);static inline void pci_ide_init (struct pci_dev *dev) { }#endif +#ifdef CONFIG_PCI_TSM +void pci_tsm_init (struct pci_dev *pdev); +void pci_tsm_destroy (struct pci_dev *pdev); +extern const struct attribute_group pci_tsm_pf0_attr_group; +extern const struct attribute_group pci_tsm_auth_attr_group; +#else +static inline void pci_tsm_init (struct pci_dev *pdev) { } +static inline void pci_tsm_destroy (struct pci_dev *pdev) { } +#endif +/** * pci_dev_set_io_state - Set the new error state if possible. * drivers/pci/probe.c +1
-0@@ -2620,6 +2620,7 @@ static void pci_init_capabilities(struct pci_dev *dev)pci_tph_init (dev);/* TLP Processing Hints */ pci_rebar_init (dev);/* Resizable BAR */ pci_ide_init (dev);/* Link Integrity and Data Encryption */ +pci_tsm_init (dev);/* TEE Security Manager connection */ pcie_report_downtraining (dev);pci_init_reset_methods (dev);drivers/pci/remove.c +3
-0@@ -55,6 +55,9 @@ static void pci_destroy_dev(struct pci_dev *dev)pci_doe_sysfs_teardown (dev);pci_npem_remove (dev); +/* before device_del() to keep config cycle access */ +pci_tsm_destroy (dev); +device_del (&dev->dev);down_write (&pci_bus_sem);drivers/pci/tsm.c new file +413
-0@@ -0,0 +1,437 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* TEE Security Manager for the TEE Device Interface Security Protocol +* (TDISP, PCIe r6.1 sec 11) +* +* Copyright(c) 2024 Intel Corporation. All rights reserved. +*/ + +#define dev_fmt (fmt) "TSM: " fmt + +#include <linux/bitfield.h> +#include <linux/xarray.h> +#include <linux/sysfs.h> + +#include <linux/pci.h> +#include <linux/pci-doe.h> +#include <linux/pci-tsm.h> +#include "pci.h" + +/* +* Provide a read/write lock against the init / exit of pdev tsm +* capabilities and arrival/departure of a tsm instance +*/ +static DECLARE_RWSEM (pci_tsm_rwsem); +static const struct pci_tsm_ops *tsm_ops; + +/* supplemental attributes to surface when pci_tsm_attr_group is active */ +static const struct attribute_group *pci_tsm_owner_attr_group; + +static struct pci_tsm_pf0 *to_pci_tsm_pf0 (struct pci_tsm *pci_tsm) +{ +struct pci_dev *pdev = pci_tsm->pdev; + +if (!is_pci_tsm_pf0 (pdev) || pci_tsm->type !=PCI_TSM_PF0 ) { +dev_WARN_ONCE (&pdev->dev,1 ,"invalid context object\n" ); +return NULL ; + } + + return container_of(pci_tsm, struct pci_tsm_pf0, tsm); +} + +/* TODO: switch to ACQUIRE() and ACQUIRE_ERR() */ +static struct mutex *tsm_ops_lock (struct pci_tsm_pf0 *tsm) +{ +lockdep_assert_held (&pci_tsm_rwsem); + +if (mutex_lock_interruptible (&tsm->lock) !=0 ) +return NULL ; +return &tsm->lock; +} +DEFINE_FREE(tsm_ops_unlock, struct mutex *, if (_T) mutex_unlock(_T)) + +static int pci_tsm_disconnect (struct pci_dev *pdev) +{ +struct pci_tsm_pf0 *tsm =to_pci_tsm_pf0 (pdev->tsm); + + struct mutex *lock __free(tsm_ops_unlock) = tsm_ops_lock(tsm); +if (!lock) +return -EINTR ; + +if (tsm->state <PCI_TSM_INIT ) +return -ENXIO ; +if (tsm->state <PCI_TSM_CONNECT ) +return 0 ; + + tsm_ops->disconnect (pdev); + tsm->state =PCI_TSM_INIT ; + +return 0 ; +} + +static int pci_tsm_connect (struct pci_dev *pdev) +{ +struct pci_tsm_pf0 *tsm =to_pci_tsm_pf0 (pdev->tsm); +int rc; + + struct mutex *lock __free(tsm_ops_unlock) = tsm_ops_lock(tsm); +if (!lock) +return -EINTR ; + +if (tsm->state <PCI_TSM_INIT ) +return -ENXIO ; +if (tsm->state >=PCI_TSM_CONNECT ) +return 0 ; + + rc = tsm_ops->connect (pdev); +if (rc) +return rc; + tsm->state =PCI_TSM_CONNECT ; +return 0 ; +} + +/* TODO: switch to ACQUIRE() and ACQUIRE_ERR() */ +static struct rw_semaphore *tsm_read_lock (void ) +{ +if (down_read_interruptible (&pci_tsm_rwsem)) +return NULL ; +return &pci_tsm_rwsem; +} +DEFINE_FREE(tsm_read_unlock, struct rw_semaphore *, if (_T) up_read(_T)) + +static ssize_t connect_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t len) +{ +int rc; +bool connect; +struct pci_dev *pdev =to_pci_dev (dev); + + rc =kstrtobool (buf, &connect); +if (rc) +return rc; + + struct rw_semaphore *lock __free(tsm_read_unlock) = tsm_read_lock(); +if (!lock) +return -EINTR ; + +if (connect) + rc =pci_tsm_connect (pdev); +else + rc =pci_tsm_disconnect (pdev); +if (rc) +return rc; +return len; +} + +static ssize_t connect_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct pci_tsm_pf0 *tsm; + + struct rw_semaphore *lock __free(tsm_read_unlock) = tsm_read_lock(); +if (!lock) +return -EINTR ; + +if (!pdev->tsm) +return -ENXIO ; + + tsm =to_pci_tsm_pf0 (pdev->tsm); +return sysfs_emit (buf,"%d\n" , tsm->state >=PCI_TSM_CONNECT ); +} +static DEVICE_ATTR_RW (connect); + +static bool pci_tsm_pf0_group_visible (struct kobject *kobj) +{ +struct device *dev =kobj_to_dev (kobj); +struct pci_dev *pdev =to_pci_dev (dev); + +if (pdev->tsm &&is_pci_tsm_pf0 (pdev)) +return true; +return false; +} +DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE (pci_tsm_pf0); + +static struct attribute *pci_tsm_pf0_attrs[] = { + &dev_attr_connect.attr, +NULL +}; + +const struct attribute_group pci_tsm_pf0_attr_group = { + .name ="tsm" , + .attrs = pci_tsm_pf0_attrs, + .is_visible =SYSFS_GROUP_VISIBLE (pci_tsm_pf0), +}; + +static ssize_t authenticated_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +/* +* When device authentication is TSM owned, 'authenticated' is +* identical to the connect state. +*/ +return connect_show (dev, attr, buf); +} +static DEVICE_ATTR_RO (authenticated); + +static struct attribute *pci_tsm_auth_attrs[] = { + &dev_attr_authenticated.attr, +NULL +}; + +const struct attribute_group pci_tsm_auth_attr_group = { + .attrs = pci_tsm_auth_attrs, + .is_visible =SYSFS_GROUP_VISIBLE (pci_tsm_pf0), +}; + +/* +* Retrieve physical function0 device whether it has TEE capability or not +*/ +static struct pci_dev *pf0_dev_get (struct pci_dev *pdev) +{ +struct pci_dev *pf_dev =pci_physfn (pdev); + +if (PCI_FUNC (pf_dev->devfn) ==0 ) +return pci_dev_get (pf_dev); + +return pci_get_slot (pf_dev->bus, + pf_dev->devfn -PCI_FUNC (pf_dev->devfn)); +} + +static bool is_pci_tsm_downstream (struct pci_dev *pdev) +{ +struct pci_dev *uport; + +if (pci_pcie_type (pdev) !=PCI_EXP_TYPE_ENDPOINT ) +return false; + +/* "grandparent" of an endpoint is an Upstream Port (or Root Port) */ +if (!pdev->dev.parent) +return false; +if (!pdev->dev.parent->parent) +return false; + + uport =to_pci_dev (pdev->dev.parent->parent); +if (pci_pcie_type (uport) !=PCI_EXP_TYPE_UPSTREAM ) +return false; + +if (!uport->tsm) +return false; + +/* Upstream Port has a 'tsm' context, probe downstream devices. */ +return true; +} + +static enum pci_tsm_type pci_tsm_type (struct pci_dev *pdev) +{ +if (is_pci_tsm_pf0 (pdev)) +return PCI_TSM_PF0 ; + + struct pci_dev *pf0 __free(pci_dev_put) = pf0_dev_get(pdev); +if (!pf0) +return PCI_TSM_INVALID ; + +if (pf0->tsm && pf0->tsm->type ==PCI_TSM_PF0 ) { +if (pdev->is_virtfn) +return PCI_TSM_VIRTFN ; +else +return PCI_TSM_MFD ; + } + +/* +* Allow for Device Security Managers (DSMs) at a Switch level +* to host TDISP services for downstream devices +*/ +if (is_pci_tsm_downstream (pdev)) +return PCI_TSM_DOWNSTREAM ; +return PCI_TSM_INVALID ; +} + +/** +* pci_tsm_initialize() - base 'struct pci_tsm' initialization +* @pdev: The PCI device +* @tsm: context to initialize +*/ +void pci_tsm_initialize (struct pci_dev *pdev,struct pci_tsm *tsm) +{ + tsm->type =pci_tsm_type (pdev); + tsm->pdev = pdev; +} +EXPORT_SYMBOL_GPL (pci_tsm_initialize); + +/** +* pci_tsm_pf0_initialize() - common 'struct pci_tsm_pf0' initialization +* @pdev: Physical Function 0 PCI device +* @tsm: context to initialize +*/ +int pci_tsm_pf0_initialize (struct pci_dev *pdev,struct pci_tsm_pf0 *tsm) +{ +mutex_init (&tsm->lock); + tsm->doe_mb =pci_find_doe_mailbox (pdev,PCI_VENDOR_ID_PCI_SIG , +PCI_DOE_PROTO_CMA ); +if (!tsm->doe_mb) { +pci_warn (pdev,"TSM init failure, no CMA mailbox\n" ); +return -ENODEV ; + } + + tsm->state =PCI_TSM_INIT ; +pci_tsm_initialize (pdev, &tsm->tsm); + +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_tsm_pf0_initialize); + +static void __pci_tsm_pf0_destroy (struct pci_tsm_pf0 *tsm) +{ +mutex_destroy (&tsm->lock); +} + +static void tsm_remove (struct pci_tsm *tsm) +{ +if (!tsm) +return ; + tsm_ops->remove (tsm); +} +DEFINE_FREE(tsm_remove, struct pci_tsm *, if (_T) tsm_remove(_T)) + +static void pci_tsm_pf0_init (struct pci_dev *pdev) +{ +bool tee_cap; + + tee_cap = pdev->devcap &PCI_EXP_DEVCAP_TEE ; + +if (!(pdev->ide_cap || tee_cap)) +return ; + +lockdep_assert_held_write (&pci_tsm_rwsem); +if (!tsm_ops) +return ; + +/* +* If a physical device has any security capabilities it may be +* a candidate to connect with the platform TSM +*/ + struct pci_tsm *pci_tsm __free(tsm_remove) = tsm_ops->probe(pdev); + +pci_dbg (pdev,"Device security capabilities detected (%s%s ), TSM %s\n" , + pdev->ide_cap ?" ide" :"" , tee_cap ?" tee" :"" , + pci_tsm ?"attach" :"skip" ); + +if (!pci_tsm) +return ; + + pdev->tsm =no_free_ptr (pci_tsm); +sysfs_update_group (&pdev->dev.kobj, &pci_tsm_auth_attr_group); +sysfs_update_group (&pdev->dev.kobj, &pci_tsm_pf0_attr_group); +if (pci_tsm_owner_attr_group) +sysfs_merge_group (&pdev->dev.kobj, pci_tsm_owner_attr_group); +} + +static void __pci_tsm_init (struct pci_dev *pdev) +{ +enum pci_tsm_type type =pci_tsm_type (pdev); + +switch (type) { +case PCI_TSM_PF0 : +pci_tsm_pf0_init (pdev); +break ; +case PCI_TSM_VIRTFN : +case PCI_TSM_MFD : +case PCI_TSM_DOWNSTREAM : + pdev->tsm = tsm_ops->probe (pdev); +break ; +case PCI_TSM_INVALID : +default : +break ; + } +} + +void pci_tsm_init (struct pci_dev *pdev) +{ +guard (rwsem_write)(&pci_tsm_rwsem); +__pci_tsm_init (pdev); +} + +int pci_tsm_core_register (const struct pci_tsm_ops *ops,const struct attribute_group *grp) +{ +struct pci_dev *pdev =NULL ; + +if (!ops) +return 0 ; +guard (rwsem_write)(&pci_tsm_rwsem); +if (tsm_ops) +return -EBUSY ; + tsm_ops = ops; + pci_tsm_owner_attr_group = grp; +for_each_pci_dev (pdev) +__pci_tsm_init (pdev); +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_tsm_core_register); + +static void pci_tsm_pf0_destroy (struct pci_dev *pdev) +{ +struct pci_tsm_pf0 *tsm =to_pci_tsm_pf0 (pdev->tsm); + +if (tsm->state >PCI_TSM_INIT ) +pci_tsm_disconnect (pdev); + pdev->tsm =NULL ; +if (pci_tsm_owner_attr_group) +sysfs_unmerge_group (&pdev->dev.kobj, pci_tsm_owner_attr_group); +sysfs_update_group (&pdev->dev.kobj, &pci_tsm_pf0_attr_group); +sysfs_update_group (&pdev->dev.kobj, &pci_tsm_auth_attr_group); +__pci_tsm_pf0_destroy (tsm); +} + +static void __pci_tsm_destroy (struct pci_dev *pdev) +{ +struct pci_tsm *pci_tsm = pdev->tsm; + +if (!pci_tsm) +return ; + +lockdep_assert_held_write (&pci_tsm_rwsem); + +if (is_pci_tsm_pf0 (pdev)) +pci_tsm_pf0_destroy (pdev); + tsm_ops->remove (pci_tsm); +} + +void pci_tsm_destroy (struct pci_dev *pdev) +{ +guard (rwsem_write)(&pci_tsm_rwsem); +__pci_tsm_destroy (pdev); +} + +void pci_tsm_core_unregister (const struct pci_tsm_ops *ops) +{ +struct pci_dev *pdev =NULL ; + +if (!ops) +return ; +guard (rwsem_write)(&pci_tsm_rwsem); +if (ops != tsm_ops)
drivers/pci/tsm.c line 423
Quoted 10 lines
drivers/pci/tsm.c new file +10
-0+return ; +for_each_pci_dev (pdev) +__pci_tsm_destroy (pdev); + tsm_ops =NULL ; +} +EXPORT_SYMBOL_GPL (pci_tsm_core_unregister); + +int pci_tsm_doe_transfer (struct pci_dev *pdev,enum pci_doe_proto type, +const void *req,size_t req_sz,void *resp, +size_t resp_sz)
This does not belong here yet - no user.
But if you still want it - "enum pci_doe_proto" should go to pci-doe.h like this https://github.com/AMDESE/linux-kvm/commit/af12dec97ed98a9f365bbbb6925e76c556937d01
drivers/pci/tsm.c line 435
Quoted 12 lines
drivers/pci/tsm.c new file +12
-0+{ +struct pci_tsm_pf0 *tsm; + +if (!pdev->tsm || !is_pci_tsm_pf0 (pdev)) +return -ENXIO ; + + tsm =to_pci_tsm_pf0 (pdev->tsm); +if (!tsm->doe_mb) +return -ENXIO ; + +return pci_doe (tsm->doe_mb,PCI_VENDOR_ID_PCI_SIG , type, req, req_sz, + resp, resp_sz);
The wrapper does not seem to be very helpful - the platform driver (==TSM ==CCP) which is going to call it already knows it is a DSM and mailboxes are initialized (otherwise the DSM's pci_tsm_ops::probe() would've failed) so it can just call pci_doe() directly. Thanks,
Signature · 1 line
Alexey
Re: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Alexey Kardashevskiy wrote:
[..trim reply..]
Quoted 6 lines
drivers/pci/tsm.c line 423
Quoted 3 lines
drivers/pci/tsm.c new file +3
-0+int pci_tsm_doe_transfer (struct pci_dev *pdev,enum pci_doe_proto type, +const void *req,size_t req_sz,void *resp, +size_t resp_sz)This does not belong here yet - no user.
I had been pulling things out without users, but Yilun and Aneesh ask for them to be included for staging purposes. When this staging branch goes upstream a user for all exported APIs is a requirement. However, per your comment below this is worth deleting.
Quoted 2 lines
But if you still want it - "enum
pci_doe_proto" should go topci-doe.hlike this https://github.com/AMDESE/linux-kvm/commit/af12dec97ed98a9f365bbbb6925e76c556937d01
Yeah, I will move it over there.
Quoted 9 lines
Quoted 9 lines
drivers/pci/tsm.c new file +9
-0+{ +struct pci_tsm_pf0 *tsm; + +if (!pdev->tsm || !is_pci_tsm_pf0 (pdev)) +return -ENXIO ; + + tsm =to_pci_tsm_pf0 (pdev->tsm); +if (!tsm->doe_mb) +return -ENXIO ;
Quoted 8 lines
drivers/pci/tsm.c line 435
Quoted 3 lines
drivers/pci/tsm.c new file +3
-0+ +return pci_doe (tsm->doe_mb,PCI_VENDOR_ID_PCI_SIG , type, req, req_sz, + resp, resp_sz);The wrapper does not seem to be very helpful - the platform driver (==TSM ==CCP) which is going to call it already knows it is a DSM and mailboxes are initialized (otherwise the DSM's
pci_tsm_ops::probe()would've failed) so it can just callpci_doe()directly. Thanks,
True, this is a weak helper the TSM driver already knows when and if it should be using the already exported pci_doe().
Alexey Kardashevskiy
maintainerRe: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Quoted 9 lines · On 27/8/25 09:58, [email protected] wrote:
Alexey Kardashevskiy wrote:
[..trim reply..]Quoted 6 lines
drivers/pci/tsm.c line 423
Quoted 3 lines
drivers/pci/tsm.c new file +3
-0+int pci_tsm_doe_transfer (struct pci_dev *pdev,enum pci_doe_proto type, +const void *req,size_t req_sz,void *resp, +size_t resp_sz)This does not belong here yet - no user.
Quoted 4 lines
I had been pulling things out without users, but Yilun and Aneesh ask for them to be included for staging purposes. When this staging branch goes upstream a user for all exported APIs is a requirement. However, per your comment below this is worth deleting.
I did not mean to drop it just because of no users but it could stay in a separate patch as this one is quite big (679 insertions) and no-users stuff just adds unnecessary noise.
Quoted 23 lines
Quoted 2 lines
But if you still want it - "enum
pci_doe_proto" should go topci-doe.hlike this https://github.com/AMDESE/linux-kvm/commit/af12dec97ed98a9f365bbbb6925e76c556937d01Yeah, I will move it over there.
Quoted 9 lines
Quoted 9 lines
drivers/pci/tsm.c new file +9
-0+{ +struct pci_tsm_pf0 *tsm; + +if (!pdev->tsm || !is_pci_tsm_pf0 (pdev)) +return -ENXIO ; + + tsm =to_pci_tsm_pf0 (pdev->tsm); +if (!tsm->doe_mb) +return -ENXIO ;Quoted 8 lines
drivers/pci/tsm.c line 435
Quoted 3 lines
drivers/pci/tsm.c new file +3
-0+ +return pci_doe (tsm->doe_mb,PCI_VENDOR_ID_PCI_SIG , type, req, req_sz, + resp, resp_sz);The wrapper does not seem to be very helpful - the platform driver (==TSM ==CCP) which is going to call it already knows it is a DSM and mailboxes are initialized (otherwise the DSM's
pci_tsm_ops::probe()would've failed) so it can just callpci_doe()directly. Thanks,
Quoted 2 lines
True, this is a weak helper the TSM driver already knows when and if it should be using the already exported
pci_doe().
right, thanks,
Signature · 1 line
Alexey
Alexey Kardashevskiy
maintainerRe: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
In reply to Dan WilliamsQuoted 138 lines · Dan Williams
The PCIe 6.1 specification, section 11, introduces the Trusted Execution Environment (TEE) Device Interface Security Protocol (TDISP). This protocol definition builds upon Component Measurement and Authentication (CMA), and link Integrity and Data Encryption (IDE). It adds support for assigning devices (PCI physical or virtual function) to a confidential VM such that the assigned device is enabled to access guest private memory protected by technologies like Intel TDX, AMD SEV-SNP, RISCV COVE, or ARM CCA.
The "TSM" (TEE Security Manager) is a concept in the TDISP specification of an agent that mediates between a "DSM" (Device Security Manager) and system software in both a VMM and a confidential VM. A VMM uses TSM ABIs to setup link security and assign devices. A confidential VM uses TSM ABIs to transition an assigned device into the TDISP "RUN" state and validate its configuration. From a Linux perspective the TSM abstracts many of the details of TDISP, IDE, and CMA. Some of those details leak through at times, but for the most part TDISP is an internal implementation detail of the TSM.
CONFIG_PCI_TSMadds an "authenticated" attribute and "tsm/" subdirectory to pci-sysfs. Consider that the TSM driver may itself be a PCI driver. Userspace can watch for the arrival of the "TSM" core device,/sys/class/tsm/tsm0/uevent, to know when the PCI core has initialized TSM services.The common verbs that the low-level TSM drivers implement are defined by 'struct
pci_tsm_ops'. For now only 'connect' and 'disconnect' are defined for secure session and IDE establishment. The 'probe' and 'remove' operations setup per-device context objects starting with 'structpci_tsm_pf0', the device Physical Function 0 that mediates communication to the device's Security Manager (DSM).The locking allows for multiple devices to be executing commands simultaneously, one outstanding command per-device and an rwsem synchronizes the implementation relative to TSM registration/unregistration events.
Thanks to Wu Hao for his work on an early draft of this support.
Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Acked-by Bjorn Helgaas <[email protected]>Co-developed-by Xu Yilun <[email protected]>Signed-off-by Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>--- Documentation/ABI/testing/sysfs-bus-pci | 45 +++ MAINTAINERS | 2 + drivers/pci/Kconfig | 14 + drivers/pci/Makefile | 1 + drivers/pci/pci-sysfs.c | 4 + drivers/pci/pci.h | 10 + drivers/pci/probe.c | 1 + drivers/pci/remove.c | 3 + drivers/pci/tsm.c | 437 ++++++++++++++++++++++++ drivers/virt/coco/host/tsm-core.c | 19 +- include/linux/pci-tsm.h | 138 ++++++++ include/linux/pci.h | 3 + include/linux/tsm.h | 4 +- include/uapi/linux/pci_regs.h | 1 + 14 files changed, 679 insertions(+), 3 deletions(-)create mode 100644
drivers/pci/tsm.c
create mode 100644include/linux/pci-tsm.hDocumentation/ABI/testing/sysfs-bus-pci +45
-0@@ -612,3 +612,48 @@ Description: # ls doe_features 0001:01 0001:02 doe_discovery + +What: /sys/bus/pci/devices/.../tsm/ +Date: July 2024 +Contact: [email protected] +Description: + This directory only appears if a physical device function + supports authentication (PCIe CMA-SPDM), interface security + (PCIe TDISP), and is accepted for secure operation by the + platform TSM driver. This attribute directory appears + dynamically after the platform TSM driver loads. So, only after + the /sys/class/tsm/tsm0 device arrives can tools assume that + devices without a tsm/ attribute directory will never have one, + before that, the security capabilities of the device relative to + the platform TSM are unknown. See + Documentation/ABI/testing/sysfs-class-tsm. + +What: /sys/bus/pci/devices/.../tsm/connect +Date: July 2024 +Contact: [email protected] +Description: + (RW) Writing "1" to this file triggers the platform TSM (TEE + Security Manager) to establish a connection with the device. + This typically includes an SPDM (DMTF Security Protocols and + Data Models) session over PCIe DOE (Data Object Exchange) and + may also include PCIe IDE (Integrity and Data Encryption) + establishment. + +What: /sys/bus/pci/devices/.../authenticated +Date: July 2024 +Contact: [email protected] +Description: + When the device's tsm/ directory is present device + authentication (PCIe CMA-SPDM) and link encryption (PCIe IDE) + are handled by the platform TSM (TEE Security Manager). When the + tsm/ directory is not present this attribute reflects only the + native CMA-SPDM authentication state with the kernel's + certificate store. + + If the attribute is not present, it indicates that + authentication is unsupported by the device, or the TSM has no + available authentication methods for the device. + + When present and the tsm/ attribute directory is present, the + authenticated attribute is an alias for the device 'connect' + state. See the 'tsm/connect' attribute for more details.MAINTAINERS +2
-0@@ -24560,8 +24560,10 @@ M: Dan Williams <[email protected]> L: [email protected] S: Maintained F: Documentation/ABI/testing/configfs-tsm-report +F: drivers/pci/tsm.c F: drivers/virt/coco/guest/ F: drivers/virt/coco/host/ +F: include/linux/pci-tsm.h F: include/linux/tsm.h TRUSTED SERVICES TEE DRIVERdrivers/pci/Kconfig +0
-0
drivers/pci/Kconfig config PCI_IDE_STREAM_MAX · line 141
Quoted 7 lines
drivers/pci/Kconfig +4
-0@@ -135,6 +135,20 @@ config PCI_IDE_STREAM_MAX track the maximum possibility of 256 streams per host bridge in the typical case. +config PCI_TSM + bool "PCI TSM: Device security protocol support" + select PCI_IDE + select PCI_DOE
select TSM
as otherwise:
|| ld: drivers/pci/ide.o: in function `pci_ide_stream_release': drivers/pci/ide.c|276| (.text+0x620): undefined reference to `tsm_ide_stream_unregister' || ld: drivers/pci/tsm.o: in function `pci_tsm_pf0_destructor': drivers/pci/tsm.c|434| (.text+0xa1): undefined reference to `tsm_pci_group' ld: /home/aik/p/tsm/drivers/pci/tsm.c|435| (.text+0xae): undefined reference to `tsm_pci_group' || ld: drivers/pci/tsm.o: in function `connect_show': drivers/pci/tsm.c|201| (.text+0x1a4): undefined reference to `tsm_name' || ld: drivers/pci/tsm.o: in function `pci_tsm_register': drivers/pci/tsm.c|462| (.text+0x28e): undefined reference to `tsm_pci_ops'
etc.
but this is kinda wrong as it is quite bizarre to call drivers/virt/coco/tsm-core.c's tsm_ide_stream_unregister() from drivers/pci/ide.c's pci_ide_stream_release(), i.e. "virt" from "pci core". imho the caller of pci_ide_stream_release() should just call tsm_ide_stream_unregister() too, and so on. Thanks,
Signature · 1 line
Alexey
Re: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Alexey Kardashevskiy wrote:
[..]
Quoted 8 lines
Quoted 8 lines
drivers/pci/Kconfig +0
-0@@ -135,6 +135,20 @@ config PCI_IDE_STREAM_MAX track the maximum possibility of 256 streams per host bridge in the typical case.
Quoted 6 lines
drivers/pci/Kconfig config PCI_IDE_STREAM_MAX · line 141
Quoted 4 lines
drivers/pci/Kconfig +4
-0+config PCI_TSM + bool "PCI TSM: Device security protocol support" + select PCI_IDE + select PCI_DOEselect TSM
Yup, already in v5.
Quoted 7 lines
etc.
but this is kinda wrong as it is quite bizarre to call drivers/virt/coco/tsm-core.c's
tsm_ide_stream_unregister()from drivers/pci/ide.c'spci_ide_stream_release(), i.e. "virt" from "pci core". imho the caller ofpci_ide_stream_release()should just calltsm_ide_stream_unregister()too, and so on. Thanks,
So I agree it is odd, and I orginally did not have this tie until the DEFINE_FREE(pci_ide_stream_release, ...) proposal. With that I want to allow for TSM drivers to teardown everything associated with IDE setup with one scope-based-cleanup helper.
It is not a mid-layer because nothing requires a TSM driver to call tsm_ide_stream_register(), but if they do register it then the PCI core helper will handle cleaning it up on error along with the rest of the resources.
Alexey Kardashevskiy
maintainerRe: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Quoted 32 lines · On 27/8/25 10:15, [email protected] wrote:
Alexey Kardashevskiy wrote:
[..]Quoted 8 lines
Quoted 8 lines
drivers/pci/Kconfig +0
-0@@ -135,6 +135,20 @@ config PCI_IDE_STREAM_MAX track the maximum possibility of 256 streams per host bridge in the typical case.Quoted 6 lines
drivers/pci/Kconfig config PCI_IDE_STREAM_MAX · line 141
Quoted 4 lines
drivers/pci/Kconfig +4
-0+config PCI_TSM + bool "PCI TSM: Device security protocol support" + select PCI_IDE + select PCI_DOEselect TSM
Yup, already in v5.
Quoted 7 lines
etc.
but this is kinda wrong as it is quite bizarre to call drivers/virt/coco/tsm-core.c's
tsm_ide_stream_unregister()from drivers/pci/ide.c'spci_ide_stream_release(), i.e. "virt" from "pci core". imho the caller ofpci_ide_stream_release()should just calltsm_ide_stream_unregister()too, and so on. Thanks,So I agree it is odd, and I orginally did not have this tie until the
DEFINE_FREE(pci_ide_stream_release, ...)proposal. With that I want to allow for TSM drivers to teardown everything associated with IDE setup with one scope-based-cleanup helper.
Quoted 4 lines
It is not a mid-layer because nothing requires a TSM driver to call
tsm_ide_stream_register(), but if they do register it then the PCI core helper will handle cleaning it up on error along with the rest of the resources.
I see but imho DEFINE_FREE() is weak justification for such cross-subsystem jumping, still. I was looking at the linker error as, like, "find a Wally". May be then drop EXPORT_SYMBOL_GPL() for tsm_ide_stream_unregister()? Thanks,
Signature · 1 line
Alexey
Re: [PATCH v3 03/13] PCI/TSM: Authenticate devices via platform TSM
Alexey Kardashevskiy wrote:
[..]
Quoted 11 lines
Quoted 11 lines
Quoted 5 lines
but this is kinda wrong as it is quite bizarre to call drivers/virt/coco/tsm-core.c's
tsm_ide_stream_unregister()from drivers/pci/ide.c'spci_ide_stream_release(), i.e. "virt" from "pci core". imho the caller ofpci_ide_stream_release()should just calltsm_ide_stream_unregister()too, and so on. Thanks,So I agree it is odd, and I orginally did not have this tie until the
DEFINE_FREE(pci_ide_stream_release, ...)proposal. With that I want to allow for TSM drivers to teardown everything associated with IDE setup with one scope-based-cleanup helper.
Quoted 7 lines
Quoted 4 lines
It is not a mid-layer because nothing requires a TSM driver to call
tsm_ide_stream_register(), but if they do register it then the PCI core helper will handle cleaning it up on error along with the rest of the resources.I see but imho
DEFINE_FREE()is weak justification for such cross-subsystem jumping,
Interesting, I do not see 'struct pci_ide' understanding that one of the primary methods of establishing IDE (coordination through a TSM) as cross-subsystem jumping. It is unlikely that Linux will ever understand IDE establishment in any other form especially because, as far as I understand, all but Intel platforms enforce IDE establishment through TSM ABI calls.
Quoted 1 line
still. I was looking at the linker error as, like, "find a Wally".
not sure what that means...
Quoted 1 line
May be then drop
EXPORT_SYMBOL_GPL()fortsm_ide_stream_unregister()?
True, that can be dropped now.
PATCH v3 4/13 PCI: Enable host-bridge emulation for PCI_DOMAINS_GENERIC platforms
In reply to Dan WilliamsThe ability to emulate a host-bridge is useful not only for hardware PCI controllers like CONFIG_VMD, or virtual PCI controllers like CONFIG_PCI_HYPERV, but also for test and development scenarios like CONFIG_SAMPLES_DEVSEC [1].
One stumbling block for defining CONFIG_SAMPLES_DEVSEC, a sample implementation of a platform TSM for PCI Device Security, is the need to accommodate PCI_DOMAINS_GENERIC architectures alongside x86 [2].
In support of supplementing the existing CONFIG_PCI_BRIDGE_EMUL infrastructure for host bridges:
* Introduce pci_bus_find_emul_domain_nr() as a common way to find a free PCI domain number whether that is to reuse the existing dynamic allocation code in the !ACPI case, or to assign an unused domain above the last ACPI segment.
* Convert pci-hyperv to the new allocator so that the PCI core can unconditionally assume that bridge->domain_nr != PCI_DOMAIN_NR_NOT_SET is the dynamically allocated case.
A follow on patch can also convert vmd to the new scheme. Currently vmd is limited to CONFIG_PCI_DOMAINS_GENERIC=n (x86) so, unlike pci-hyperv, it does not immediately conflict with this new pci_bus_find_emul_domain_nr() mechanism.
--- drivers/pci/controller/pci-hyperv.c | 53 ++--------------------------- drivers/pci/pci.c | 43 ++++++++++++++++++++++- drivers/pci/probe.c | 8 ++++- include/linux/pci.h | 4 +++ 4 files changed, 56 insertions(+), 52 deletions(-)
drivers/pci/controller/pci-hyperv.c +3 -50
@@ -3574,48 +3574,6 @@ static int hv_send_resources_released(struct hv_device *hdev)return 0 ; } -#define HVPCI_DOM_MAP_SIZE (64 * 1024) -static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE); - -/* -* PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0 -* as invalid for passthrough PCI devices of this driver. -*/ -#define HVPCI_DOM_INVALID 0 - -/** -* hv_get_dom_num() - Get a valid PCI domain number -* Check if the PCI domain number is in use, and return another number if -* it is in use. -* -* @dom: Requested domain number -* -* return: domain number on success, HVPCI_DOM_INVALID on failure -*/ -static u16 hv_get_dom_num (u16 dom) -{ -unsigned int i; - -if (test_and_set_bit (dom, hvpci_dom_map) ==0 ) -return dom; - -for_each_clear_bit (i, hvpci_dom_map,HVPCI_DOM_MAP_SIZE ) { -if (test_and_set_bit (i, hvpci_dom_map) ==0 ) -return i; - } - -return HVPCI_DOM_INVALID ; -} - -/** -* hv_put_dom_num() - Mark the PCI domain number as free -* @dom: Domain number to be freed -*/ -static void hv_put_dom_num (u16 dom) -{ -clear_bit (dom, hvpci_dom_map); -} -/** * hv_pci_probe() - New VMBus channel probe, for a root PCI bus * @hdev: VMBus's tracking struct for this root PCI bus
@@ -3659,9 +3617,9 @@ static int hv_pci_probe(struct hv_device *hdev,* collisions) in the same VM. */ dom_req = hdev->dev_instance.b[5 ] <<8 | hdev->dev_instance.b[4 ]; - dom =hv_get_dom_num (dom_req); + dom =pci_bus_find_emul_domain_nr (dom_req); -if (dom ==HVPCI_DOM_INVALID ) { +if (dom <0 ) {dev_err (&hdev->device,"Unable to use dom# 0x%x or other numbers" , dom_req); ret = -EINVAL ;
@@ -3795,7 +3753,7 @@ static int hv_pci_probe(struct hv_device *hdev, destroy_wq:destroy_workqueue (hbus->wq); free_dom: -hv_put_dom_num (hbus->bridge->domain_nr); +pci_bus_release_emul_domain_nr (hbus->bridge->domain_nr); free_bus:kfree (hbus);return ret;
@@ -3919,8 +3877,6 @@ static void hv_pci_remove(struct hv_device *hdev)irq_domain_remove (hbus->irq_domain);irq_domain_free_fwnode (hbus->fwnode); -hv_put_dom_num (hbus->bridge->domain_nr); -kfree (hbus); }
@@ -4097,9 +4053,6 @@ static int __init init_hv_pci_drv(void)if (ret)return ret; -/* Set the invalid domain number's bit, so it will not be used */ -set_bit (HVPCI_DOM_INVALID , hvpci_dom_map); -/* Initialize PCI block r/w interface */ hvpci_block_ops.read_block = hv_read_config_block; hvpci_block_ops.write_block = hv_write_config_block;
drivers/pci/pci.c +42 -1
51 lines not on this page · raw message
drivers/pci/probe.c +7 -1
@@ -632,6 +632,11 @@ static void pci_release_host_bridge_dev(struct device *dev)pci_free_resource_list (&bridge->windows);pci_free_resource_list (&bridge->dma_ranges); + +/* Host bridges only have domain_nr set in the emulation case */ +if (bridge->domain_nr !=PCI_DOMAIN_NR_NOT_SET ) +pci_bus_release_emul_domain_nr (bridge->domain_nr); +kfree (bridge); }
@@ -1112,7 +1117,8 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)device_del (&bridge->dev); free:#ifdef CONFIG_PCI_DOMAINS_GENERIC -pci_bus_release_domain_nr (parent, bus->domain_nr); +if (bridge->domain_nr ==PCI_DOMAIN_NR_NOT_SET ) +pci_bus_release_domain_nr (parent, bus->domain_nr);#endif if (bus_registered)put_device (&bus->dev);
include/linux/pci.h +4 -0
@@ -1894,10 +1894,14 @@ DEFINE_GUARD(pci_dev, struct pci_dev *, pci_dev_lock(_T), pci_dev_unlock(_T))*/ #ifdef CONFIG_PCI_DOMAINS extern int pci_domains_supported; +int pci_bus_find_emul_domain_nr (int hint); +void pci_bus_release_emul_domain_nr (int domain_nr);#else enum { pci_domains_supported =0 };static inline int pci_domain_nr (struct pci_bus *bus) {return 0 ; }static inline int pci_proc_domain (struct pci_bus *bus) {return 0 ; } +static inline int pci_bus_find_emul_domain_nr (int hint) {return 0 ; } +static inline void pci_bus_release_emul_domain_nr (int domain_nr) { }#endif /* CONFIG_PCI_DOMAINS */ /*
Signature · 1 line
2.49.0
PATCH v3 5/13 PCI: vmd: Switch to pci_bus_find_emul_domain_nr()
In reply to Dan WilliamsThe new common domain number allocator can replace the custom allocator in VMD.
Beyond some code reuse benefits it does close a potential race whereby vmd_find_free_domain() collides with new PCI buses coming online with a conflicting domain number. Such a race has not been observed in practice, hence not tagging this change as a fix.
As VMD uses pci_create_root_bus() rather than pci_alloc_host_bridge() + pci_scan_root_bus_bridge() it has no chance to set ->domain_nr in the bridge so needs to manage freeing the domain number on its own.
--- drivers/pci/controller/vmd.c | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-)
drivers/pci/controller/vmd.c +10 -23
@@ -565,22 +565,6 @@ static void vmd_detach_resources(struct vmd_dev *vmd) vmd->dev->resource[VMD_MEMBAR2 ].child =NULL ; } -/* -* VMD domains start at 0x10000 to not clash with ACPI _SEG domains. -* Per ACPI r6.0, sec 6.5.6, _SEG returns an integer, of which the lower -* 16 bits are the PCI Segment Group (domain) number. Other bits are -* currently reserved. -*/ -static int vmd_find_free_domain (void ) -{ -int domain =0xffff ; -struct pci_bus *bus =NULL ; - -while ((bus =pci_find_next_bus (bus)) !=NULL ) - domain =max_t (int, domain,pci_domain_nr (bus)); -return domain +1 ; -} -static int vmd_get_phys_offsets (struct vmd_dev *vmd,bool native_hint,resource_size_t *offset1,resource_size_t *offset2)
@@ -865,13 +849,6 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features) .parent = res, }; - sd->vmd_dev = vmd->dev; - sd->domain =vmd_find_free_domain (); -if (sd->domain <0 ) -return sd->domain; - - sd->node =pcibus_to_node (vmd->dev->bus); -/* * Currently MSI remapping must be enabled in guest passthrough mode * due to some missing interrupt remapping plumbing. This is probably
@@ -903,9 +880,17 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)pci_add_resource_offset (&resources, &vmd->resources[1 ], offset[0 ]);pci_add_resource_offset (&resources, &vmd->resources[2 ], offset[1 ]); + sd->vmd_dev = vmd->dev; + sd->domain =pci_bus_find_emul_domain_nr (PCI_DOMAIN_NR_NOT_SET ); +if (sd->domain <0 ) +return sd->domain; + + sd->node =pcibus_to_node (vmd->dev->bus); + vmd->bus =pci_create_root_bus (&vmd->dev->dev, vmd->busn_start, &vmd_ops, sd, &resources);if (!vmd->bus) { +pci_bus_release_emul_domain_nr (sd->domain);pci_free_resource_list (&resources);vmd_remove_irq_domain (vmd);return -ENODEV ;
@@ -998,6 +983,7 @@ static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)return -ENOMEM ; vmd->dev = dev; + vmd->sysdata.domain =PCI_DOMAIN_NR_NOT_SET ; vmd->instance =ida_alloc (&vmd_instance_ida,GFP_KERNEL );if (vmd->instance <0 )return vmd->instance;
@@ -1063,6 +1049,7 @@ static void vmd_remove(struct pci_dev *dev)vmd_detach_resources (vmd);vmd_remove_irq_domain (vmd);ida_free (&vmd_instance_ida, vmd->instance); +pci_bus_release_emul_domain_nr (vmd->sysdata.domain); }static void vmd_shutdown (struct pci_dev *dev)
Signature · 1 line
2.49.0
PATCH v3 6/13 samples/devsec: Introduce a PCI device-security bus + endpoint sample
In reply to Dan WilliamsEstablish just enough emulated PCI infrastructure to register a sample TSM (platform security manager) driver and have it discover an IDE + TEE (link encryption + device-interface security protocol (TDISP)) capable device.
Use the existing a CONFIG_PCI_BRIDGE_EMUL to emulate an IDE capable root port, and open code the emulation of an endpoint device via simulated configuration cycle responses.
The devsec_tsm driver responds to the PCI core TSM operations as if it successfully exercised the given interface security protocol message.
The devsec_bus and devsec_tsm drivers can be loaded in either order to reflect cases like SEV-TIO where the TSM is PCI-device firmware, and cases like TDX Connect where the TSM is a software agent running on the host CPU.
Follow-on patches add common code for TSM managed IDE establishment. For now, just successfully complete setup and teardown of the DSM (device security manager) context as a building block for management of TDI (trusted device interface) instances.
# modprobe devsec_bus
devsec_bus devsec_bus: PCI host bridge to bus 10000:00
pci_bus 10000:00: root bus resource [bus 00-01]
pci_bus 10000:00: root bus resource [mem 0xf000000000-0xffffffffff 64bit]
pci 10000:00:00.0: [8086:7075] type 01 class 0x060400 PCIe Root Port
pci 10000:00:00.0: PCI bridge to [bus 00]
pci 10000:00:00.0: bridge window [io 0x0000-0x0fff]
pci 10000:00:00.0: bridge window [mem 0x00000000-0x000fffff]
pci 10000:00:00.0: bridge window [mem 0x00000000-0x000fffff 64bit pref]
pci 10000:00:00.0: bridge configuration invalid ([bus 00-00]), reconfiguring
pci 10000:01:00.0: [8086:ffff] type 00 class 0x000000 PCIe Endpoint
pci 10000:01:00.0: BAR 0 [mem 0xf000000000-0xf0001fffff 64bit pref]
pci_doe_abort: pci 10000:01:00.0: DOE: [100] Issuing Abort
pci_doe_cache_protocols: pci 10000:01:00.0: DOE: [100] Found protocol 0 vid: 1 prot: 1
pci 10000:01:00.0: disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'
pci 10000:00:00.0: PCI bridge to [bus 01]
pci_bus 10000:01: busn_res: [bus 01] end is updated to 01
# modprobe devsec_tsm
devsec_tsm_pci_probe: pci 10000:01:00.0: devsec: tsm enabled
__pci_tsm_init: pci 10000:01:00.0: TSM: Device security capabilities detected ( ide tee ), TSM attach--- MAINTAINERS | 1 + samples/Kconfig | 16 + samples/Makefile | 1 + samples/devsec/Makefile | 10 + samples/devsec/bus.c | 708 ++++++++++++++++++++++++++++++++++++++++ samples/devsec/common.c | 26 ++ samples/devsec/devsec.h | 40 +++ samples/devsec/tsm.c | 143 ++++++++ 8 files changed, 945 insertions(+) create mode 100644 samples/devsec/Makefile create mode 100644 samples/devsec/bus.c create mode 100644 samples/devsec/common.c create mode 100644 samples/devsec/devsec.h create mode 100644 samples/devsec/tsm.c
MAINTAINERS +1 -0
@@ -24565,6 +24565,7 @@ F: drivers/virt/coco/guest/ F: drivers/virt/coco/host/ F: include/linux/pci-tsm.h F: include/linux/tsm.h +F: samples/devsec/ TRUSTED SERVICES TEE DRIVER M: Balint Dobszay <[email protected]>
samples/Kconfig +16 -0
@@ -313,6 +313,22 @@ source "samples/rust/Kconfig" source "samples/damon/Kconfig" +config SAMPLE_DEVSEC + tristate "Build a sample TEE Security Manager with an emulated PCI endpoint" + depends on PCI + depends on VIRT_DRIVERS + depends on PCI_DOMAINS_GENERIC || X86 + select PCI_BRIDGE_EMUL + select PCI_TSM + select TSM + help + Build a sample platform TEE Security Manager (TSM) driver with a + corresponding emulated PCIe topology. The resulting sample modules, + devsec_bus and devsec_tsm, exercise device-security enumeration, PCI + subsystem use ABIs, device security flows. For example, exercise IDE + (link encryption) establishment and TDISP state transitions via a + Device Security Manager (DSM). + endif # SAMPLES config HAVE_SAMPLE_FTRACE_DIRECT
samples/Makefile +1 -0
@@ -43,3 +43,4 @@ obj-$(CONFIG_SAMPLES_RUST) += rust/ obj-$(CONFIG_SAMPLE_DAMON_WSSE) += damon/ obj-$(CONFIG_SAMPLE_DAMON_PRCL) += damon/ obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task/ +obj-y += devsec/
samples/devsec/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_SAMPLE_DEVSEC) += devsec_common.o +devsec_common-y := common.o + +obj-$(CONFIG_SAMPLE_DEVSEC) += devsec_bus.o +devsec_bus-y := bus.o + +obj-$(CONFIG_SAMPLE_DEVSEC) += devsec_tsm.o +devsec_tsm-y := tsm.o
samples/devsec/bus.c new file +708 -0
708 lines not on this page · raw message
samples/devsec/common.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0-only +// Copyright(c) 2024 - 2025 Intel Corporation. All rights reserved. + +#include <linux/pci.h> +#include <linux/export.h> + +/* +* devsec_bus and devsec_tsm need a common location for this data to +* avoid depending on each other. Enables load order testing +*/ +struct pci_sysdata *devsec_sysdata; +EXPORT_SYMBOL_GPL (devsec_sysdata); + +static int __init common_init (void ) +{ +return 0 ; +} +module_init (common_init); + +static void __exit common_exit(void) +{ +} +module_exit (common_exit); + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Device Security Sample Infrastructure: Shared data" );
samples/devsec/devsec.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +// Copyright(c) 2024 - 2025 Intel Corporation. All rights reserved. + +#ifndef __DEVSEC_H__ +#define __DEVSEC_H__ +struct devsec_sysdata { +#ifdef CONFIG_X86 +/* +* Must be first member to that x86::pci_domain_nr() can type +* pun devsec_sysdata and pci_sysdata. +*/ +struct pci_sysdata sd; +#else +int domain_nr; +#endif +}; + +#ifdef CONFIG_X86 +static inline void devsec_set_domain_nr (struct devsec_sysdata *sd, +int domain_nr) +{ + sd->sd.domain = domain_nr; +} +static inline int devsec_get_domain_nr (struct devsec_sysdata *sd) +{ +return sd->sd.domain; +} +#else +static inline void devsec_set_domain_nr (struct devsec_sysdata *sd, +int domain_nr) +{ + sd->domain_nr = domain_nr; +} +static inline int devsec_get_domain_nr (struct devsec_sysdata *sd) +{ +return sd->domain_nr; +} +#endif +extern struct devsec_sysdata *devsec_sysdata; +#endif /* __DEVSEC_H__ */
samples/devsec/tsm.c new file +143 -0
143 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v3 06/13] samples/devsec: Introduce a PCI device-security bus + endpoint sample
Quoted 43 lines · On Thu, 15 May 2025 22:47:25 -0700 Dan Williams <[email protected]>…
Establish just enough emulated PCI infrastructure to register a sample TSM (platform security manager) driver and have it discover an IDE + TEE (link encryption + device-interface security protocol (TDISP)) capable device.
Use the existing a
CONFIG_PCI_BRIDGE_EMULto emulate an IDE capable root port, and open code the emulation of an endpoint device via simulated configuration cycle responses.The
devsec_tsmdriver responds to the PCI core TSM operations as if it successfully exercised the given interface security protocol message.The
devsec_busanddevsec_tsmdrivers can be loaded in either order to reflect cases like SEV-TIO where the TSM is PCI-device firmware, and cases like TDX Connect where the TSM is a software agent running on the host CPU.Follow-on patches add common code for TSM managed IDE establishment. For now, just successfully complete setup and teardown of the DSM (device security manager) context as a building block for management of TDI (trusted device interface) instances.
# modprobe devsec_bus devsec_bus devsec_bus: PCI host bridge to bus 10000:00 pci_bus 10000:00: root bus resource [bus 00-01] pci_bus 10000:00: root bus resource [mem 0xf000000000-0xffffffffff 64bit] pci 10000:00:00.0: [8086:7075] type 01 class 0x060400 PCIe Root Port pci 10000:00:00.0: PCI bridge to [bus 00] pci 10000:00:00.0: bridge window [io 0x0000-0x0fff] pci 10000:00:00.0: bridge window [mem 0x00000000-0x000fffff] pci 10000:00:00.0: bridge window [mem 0x00000000-0x000fffff 64bit pref] pci 10000:00:00.0: bridge configuration invalid ([bus 00-00]), reconfiguring pci 10000:01:00.0: [8086:ffff] type 00 class 0x000000 PCIe Endpoint pci 10000:01:00.0: BAR 0 [mem 0xf000000000-0xf0001fffff 64bit pref] pci_doe_abort: pci 10000:01:00.0: DOE: [100] Issuing Abort pci_doe_cache_protocols: pci 10000:01:00.0: DOE: [100] Found protocol 0 vid: 1 prot: 1 pci 10000:01:00.0: disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force' pci 10000:00:00.0: PCI bridge to [bus 01] pci_bus 10000:01: busn_res: [bus 01] end is updated to 01 # modprobe devsec_tsm devsec_tsm_pci_probe: pci 10000:01:00.0: devsec: tsm enabled __pci_tsm_init: pci 10000:01:00.0: TSM: Device security capabilities detected ( ide tee ), TSM attach
Quoted 6 lines
Cc Bjorn Helgaas <[email protected]>Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Cc Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>
Interesting bit of emulation. Only real question I have is whether you can switch from platform devices to faux bus?
Quoted 31 lines
samples/devsec/tsm.c new file +24
-0@@ -0,0 +1,143 @@ +static const struct pci_tsm_ops devsec_pci_ops = { + .probe = devsec_tsm_pci_probe, + .remove = devsec_tsm_pci_remove, + .connect = devsec_tsm_connect, + .disconnect = devsec_tsm_disconnect, +}; + +static void devsec_tsm_remove (void *tsm_core) +{ +tsm_unregister (tsm_core); +} + +static int devsec_tsm_probe (struct platform_device *pdev) +{ +struct tsm_core_dev *tsm_core; + + tsm_core =tsm_register (&pdev->dev,NULL , &devsec_pci_ops); +if (IS_ERR (tsm_core)) +return PTR_ERR (tsm_core); + +return devm_add_action_or_reset (&pdev->dev, devsec_tsm_remove, + tsm_core); +} +
samples/devsec/tsm.c line 117
Quoted 10 lines
samples/devsec/tsm.c new file +10
-0+static struct platform_driver devsec_tsm_driver = { + .driver = { + .name ="devsec_tsm" , + }, +}; + +static struct platform_device *devsec_tsm; + +static int __init devsec_tsm_init (void ) +{
Could this use the faux bus stuff or does it need to be a platform device for some reason? That support may well have crossed with this work.
Quoted 26 lines
samples/devsec/tsm.c new file +26
-0+struct platform_device_info devsec_tsm_info = { + .name ="devsec_tsm" , + .id =-1 , + }; +int rc; + + devsec_tsm =platform_device_register_full (&devsec_tsm_info); +if (IS_ERR (devsec_tsm)) +return PTR_ERR (devsec_tsm); + + rc =platform_driver_probe (&devsec_tsm_driver, devsec_tsm_probe); +if (rc) +platform_device_unregister (devsec_tsm); +return rc; +} +module_init (devsec_tsm_init); + +static void __exit devsec_tsm_exit(void) +{ +platform_driver_unregister (&devsec_tsm_driver); +platform_device_unregister (devsec_tsm); +} +module_exit (devsec_tsm_exit); + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Device Security Sample Infrastructure: Platform TSM Driver" );
Re: [PATCH v3 06/13] samples/devsec: Introduce a PCI device-security bus + endpoint sample
Quoted 55 lines · Jonathan Cameron
Quoted 43 lines · On Thu, 15 May 2025 22:47:25 -0700 Dan Williams <[email protected]>…
Establish just enough emulated PCI infrastructure to register a sample TSM (platform security manager) driver and have it discover an IDE + TEE (link encryption + device-interface security protocol (TDISP)) capable device.
Use the existing a
CONFIG_PCI_BRIDGE_EMULto emulate an IDE capable root port, and open code the emulation of an endpoint device via simulated configuration cycle responses.The
devsec_tsmdriver responds to the PCI core TSM operations as if it successfully exercised the given interface security protocol message.The
devsec_busanddevsec_tsmdrivers can be loaded in either order to reflect cases like SEV-TIO where the TSM is PCI-device firmware, and cases like TDX Connect where the TSM is a software agent running on the host CPU.Follow-on patches add common code for TSM managed IDE establishment. For now, just successfully complete setup and teardown of the DSM (device security manager) context as a building block for management of TDI (trusted device interface) instances.
# modprobe devsec_bus devsec_bus devsec_bus: PCI host bridge to bus 10000:00 pci_bus 10000:00: root bus resource [bus 00-01] pci_bus 10000:00: root bus resource [mem 0xf000000000-0xffffffffff 64bit] pci 10000:00:00.0: [8086:7075] type 01 class 0x060400 PCIe Root Port pci 10000:00:00.0: PCI bridge to [bus 00] pci 10000:00:00.0: bridge window [io 0x0000-0x0fff] pci 10000:00:00.0: bridge window [mem 0x00000000-0x000fffff] pci 10000:00:00.0: bridge window [mem 0x00000000-0x000fffff 64bit pref] pci 10000:00:00.0: bridge configuration invalid ([bus 00-00]), reconfiguring pci 10000:01:00.0: [8086:ffff] type 00 class 0x000000 PCIe Endpoint pci 10000:01:00.0: BAR 0 [mem 0xf000000000-0xf0001fffff 64bit pref] pci_doe_abort: pci 10000:01:00.0: DOE: [100] Issuing Abort pci_doe_cache_protocols: pci 10000:01:00.0: DOE: [100] Found protocol 0 vid: 1 prot: 1 pci 10000:01:00.0: disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force' pci 10000:00:00.0: PCI bridge to [bus 01] pci_bus 10000:01: busn_res: [bus 01] end is updated to 01 # modprobe devsec_tsm devsec_tsm_pci_probe: pci 10000:01:00.0: devsec: tsm enabled __pci_tsm_init: pci 10000:01:00.0: TSM: Device security capabilities detected ( ide tee ), TSM attachQuoted 6 lines
Cc Bjorn Helgaas <[email protected]>Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Cc Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>Interesting bit of emulation. Only real question I have is whether you can switch from platform devices to faux bus?
[..]
Quoted 8 lines
samples/devsec/tsm.c line 117
Quoted 5 lines
samples/devsec/tsm.c new file +4
-0+static struct platform_device *devsec_tsm; + +static int __init devsec_tsm_init (void ) +{Could this use the faux bus stuff or does it need to be a platform device for some reason? That support may well have crossed with this work.
Yes, this was conceived before that existed, but it is a perfect fit for what I need here. Switched.
PATCH v3 7/13 PCI: Add PCIe Device 3 Extended Capability enumeration
In reply to Dan WilliamsPCIe 6.2 Section 7.7.9 Device 3 Extended Capability Structure, enumerates new link capabilities and status added for Gen 6 devices. One of the link details enumerated in that register block is the "Segment Captured" status in the Device Status 3 register. That status is relevant for enabling IDE (Integrity & Data Encryption) whereby Selective IDE streams can be limited to a given Requester ID range within a given segment.
If a device has captured its Segment value then it knows that PCIe Flit Mode is enabled via all links in the path that a configuration write traversed. IDE establishment requires that "Segment Base" in IDE RID Association Register 2 (PCIe 6.2 Section 7.9.26.5.4.2) be programmed if the RID association mechanism is in effect.
When / if IDE + Flit Mode capable devices arrive, the PCI core needs to setup the segment base when using the RID association facility, but no known deployments today depend on this.
--- drivers/pci/probe.c | 12 ++++++++++++ include/linux/pci.h | 1 + include/uapi/linux/pci_regs.h | 7 +++++++ 3 files changed, 20 insertions(+)
drivers/pci/probe.c +12 -0
@@ -2271,6 +2271,17 @@ int pci_configure_extended_tags(struct pci_dev *dev, void *ign)return 0 ; } +static void pci_dev3_init (struct pci_dev *pdev) +{ +u16 cap =pci_find_ext_capability (pdev,PCI_EXT_CAP_ID_DEV3 ); +u32 val =0 ; + +if (!cap) +return ; +pci_read_config_dword (pdev, cap +PCI_DEV3_STA , &val); + pdev->fm_enabled = !!(val &PCI_DEV3_STA_SEGMENT ); +} +/** * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable * @dev: PCI device to query
@@ -2625,6 +2636,7 @@ static void pci_init_capabilities(struct pci_dev *dev)pci_doe_init (dev);/* Data Object Exchange */ pci_tph_init (dev);/* TLP Processing Hints */ pci_rebar_init (dev);/* Resizable BAR */ +pci_dev3_init (dev);/* Device 3 capabilities */ pci_ide_init (dev);/* Link Integrity and Data Encryption */ pci_tsm_init (dev);/* TEE Security Manager connection */
include/linux/pci.h +1 -0
@@ -444,6 +444,7 @@ struct pci_dev { unsigned int pasid_enabled:1; /* Process Address Space ID */ unsigned int pri_enabled:1; /* Page Request Interface */ unsigned int tph_enabled:1; /* TLP Processing Hints */ + unsigned int fm_enabled:1; /* Flit Mode (segment captured) */ unsigned int is_managed:1; /* Managed via devres */ unsigned int is_msi_managed:1; /* MSI release via devres installed */ unsigned int needs_freset:1; /* Requires fundamental reset */
include/uapi/linux/pci_regs.h +7 -0
@@ -751,6 +751,7 @@#define PCI_EXT_CAP_ID_NPEM 0x29/* Native PCIe Enclosure Management */ #define PCI_EXT_CAP_ID_PL_32GT 0x2A/* Physical Layer 32.0 GT/s */ #define PCI_EXT_CAP_ID_DOE 0x2E/* Data Object Exchange */ +#define PCI_EXT_CAP_ID_DEV3 0x2F/* Device 3 Capability/Control/Status */ #define PCI_EXT_CAP_ID_IDE 0x30/* Integrity and Data Encryption */ #define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_IDE
@@ -1217,6 +1218,12 @@/* Deprecated old name, replaced with PCI_DOE_DATA_OBJECT_DISC_RSP_3_TYPE */ #define PCI_DOE_DATA_OBJECT_DISC_RSP_3_PROTOCOL PCI_DOE_DATA_OBJECT_DISC_RSP_3_TYPE +/* Device 3 Extended Capability */ +#define PCI_DEV3_CAP 0x4/* Device 3 Capabilities Register */ +#define PCI_DEV3_CTL 0x8/* Device 3 Control Register */ +#define PCI_DEV3_STA 0xc/* Device 3 Status Register */ +#define PCI_DEV3_STA_SEGMENT 0x8/* Segment Captured (end-to-end flit-mode detected) */ +/* Compute Express Link (CXL r3.1, sec 8.1.5) */ #define PCI_DVSEC_CXL_PORT 3#define PCI_DVSEC_CXL_PORT_CTL 0x0c
Signature · 1 line
2.49.0
Re: [PATCH v3 07/13] PCI: Add PCIe Device 3 Extended Capability enumeration
Quoted 18 lines · On Thu, 15 May 2025 22:47:26 -0700 Dan Williams <[email protected]>…
PCIe 6.2 Section 7.7.9 Device 3 Extended Capability Structure, enumerates new link capabilities and status added for Gen 6 devices. One of the link details enumerated in that register block is the "Segment Captured" status in the Device Status 3 register. That status is relevant for enabling IDE (Integrity & Data Encryption) whereby Selective IDE streams can be limited to a given Requester ID range within a given segment.
If a device has captured its Segment value then it knows that PCIe Flit Mode is enabled via all links in the path that a configuration write traversed. IDE establishment requires that "Segment Base" in IDE RID Association Register 2 (PCIe 6.2 Section 7.9.26.5.4.2) be programmed if the RID association mechanism is in effect.
When / if IDE + Flit Mode capable devices arrive, the PCI core needs to setup the segment base when using the RID association facility, but no known deployments today depend on this.
Quoted 7 lines
Cc Lukas Wunner <[email protected]>Cc Ilpo Järvinen <[email protected]>Cc Bjorn Helgaas <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Cc Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>
PATCH v3 8/13 PCI/IDE: Add IDE establishment helpers
In reply to Dan WilliamsThere are two components to establishing an encrypted link, provisioning the stream in Partner Port config-space, and programming the keys into the link layer via IDE_KM (IDE Key Management). This new library, drivers/pci/ide.c, enables the former. IDE_KM, via a TSM low-level driver, is saved for later.
With the platform TSM implementations of SEV-TIO and TDX Connect in mind this library abstracts small differences in those implementations. For example, TDX Connect handles Root Port register setup while SEV-TIO expects System Software to update the Root Port registers. This is the rationale for fine-grained 'setup' + 'enable' verbs.
The other design detail for TSM-coordinated IDE establishment is that the TSM may manage allocation of Stream IDs, this is why the Stream ID value is passed in to pci_ide_stream_setup().
The flow is:
pci_ide_stream_alloc()
Allocate a Selective IDE Stream Register Block in each Partner Port (Endpoint + Root Port), and reserve a host bridge / platform stream slot. Gather Partner Port specific stream settings like Requester ID.pci_ide_stream_register()
Publish the stream in sysfs after allocating a Stream ID. In the TSM case the TSM allocates the Stream ID for the Partner Port pair.pci_ide_stream_setup()
Program the stream settings to a Partner Port. Caller is responsible for optionally calling this for the Root Port as well if the TSM implementation requires it.pci_ide_stream_enable()
Try to run the stream after IDE_KM.
In support of system administrators auditing where platform, Root Port, and Endpoint IDE stream resources are being spent, the allocated stream is reflected as a symlink from the host bridge to the endpoint with the name:
stream%d.%d.%d:%s
Where the tuple of integers reflects the allocated platform, Root Port, and Endpoint stream index (Selective IDE Stream Register Block) values, and the %s is the endpoint device name.
Thanks to Wu Hao for a draft implementation of this infrastructure.
--- .../ABI/testing/sysfs-devices-pci-host-bridge | 38 ++ MAINTAINERS | 1 + drivers/pci/ide.c | 366 ++++++++++++++++++ include/linux/pci-ide.h | 76 ++++ include/linux/pci.h | 6 + include/uapi/linux/pci_regs.h | 2 + 6 files changed, 489 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-devices-pci-host-bridge create mode 100644 include/linux/pci-ide.h
Documentation/ABI/testing/sysfs-devices-pci-host-bridge new file +38 -0
@@ -0,0 +1,38 @@ +What: /sys/devices/pciDDDD:BB + /sys/devices/.../pciDDDD:BB +Date: December, 2024 +Contact: [email protected] +Description: + A PCI host bridge device parents a PCI bus device topology. PCI + controllers may also parent host bridges. The DDDD:BB format + conveys the PCI domain (ACPI segment) number and root bus number + (in hexadecimal) of the host bridge. Note that the domain number + may be larger than the 16-bits that the "DDDD" format implies + for emulated host-bridges. + +What: pciDDDD:BB/firmware_node +Date: December, 2024 +Contact: [email protected] +Description: + (RO) Symlink to the platform firmware device object "companion" + of the host bridge. For example, an ACPI device with an _HID of + PNP0A08 (/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00). See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format. + +What: pciDDDD:BB/streamH.R.E:DDDD:BB:DD:F +Date: December, 2024 +Contact: [email protected] +Description: + (RO) When a platform has established a secure connection, PCIe + IDE, between two Partner Ports, this symlink appears. The + primary function is to account the stream slot / resources + consumed in each of the (H)ost bridge, (R)oot Port and + (E)ndpoint that will be freed when invoking the tsm/disconnect + flow. The link points to the endpoint PCI device at domain:DDDD + bus:BB device:DD function:F. Where R and E represent the + assigned Selective IDE Stream Register Block in the Root Port + and Endpoint, and H represents a platform specific pool of + stream resources shared by the Root Ports in a host bridge. See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format.
MAINTAINERS +1 -0
@@ -18677,6 +18677,7 @@ Q: https://patchwork.kernel.org/project/linux-pci/list/ B: https://bugzilla.kernel.org C: irc://irc.oftc.net/linux-pci T: git git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git +F: Documentation/ABI/testing/sysfs-devices-pci-host-bridge F: Documentation/PCI/ F: Documentation/devicetree/bindings/pci/ F: arch/x86/kernel/early-quirks.c
drivers/pci/ide.c +366 -0
377 lines not on this page · raw message
include/linux/pci-ide.h new file +76 -0
76 lines not on this page · raw message
include/linux/pci.h +6 -0
@@ -538,6 +538,8 @@ struct pci_dev {u16 ide_cap;/* Link Integrity & Data Encryption */ u8 nr_ide_mem;/* Address association resources for streams */ u8 nr_link_ide;/* Link Stream count (Selective Stream offset) */ +u8 nr_sel_ide;/* Selective Stream count (register block allocator) */ +DECLARE_BITMAP (ide_stream_map,CONFIG_PCI_IDE_STREAM_MAX ); unsigned int ide_cfg:1; /* Config cycles over IDE */ unsigned int ide_tee_limit:1; /* Disallow T=0 traffic over IDE */#endif
@@ -605,6 +607,10 @@ struct pci_host_bridge {int domain_nr;struct list_head windows;/* resource_entry */ struct list_head dma_ranges;/* dma ranges resource list */ +#ifdef CONFIG_PCI_IDE +u8 nr_ide_streams;/* Track available vs in-use streams */ +DECLARE_BITMAP (ide_stream_map,CONFIG_PCI_IDE_STREAM_MAX ); +#endif u8 (*swizzle_irq)(struct pci_dev *,u8 *);/* Platform IRQ swizzler */ int (*map_irq)(const struct pci_dev *,u8 ,u8 );void (*release_fn)(struct pci_host_bridge *);
include/uapi/linux/pci_regs.h +2 -0
@@ -1286,6 +1286,8 @@/* Selective IDE Stream Status Register */ #define PCI_IDE_SEL_STS 8#define PCI_IDE_SEL_STS_STATE_MASK __GENMASK(3, 0)/* Selective IDE Stream State */ +#define PCI_IDE_SEL_STS_STATE_INSECURE 0 +#define PCI_IDE_SEL_STS_STATE_SECURE 2#define PCI_IDE_SEL_STS_RECVD_INTEGRITY_CHECK 0x80000000/* Received Integrity Check Fail Msg */ /* IDE RID Association Register 1 */ #define PCI_IDE_SEL_RID_1 0xc
Signature · 1 line
2.49.0
Re: [PATCH v3 08/13] PCI/IDE: Add IDE establishment helpers
Quoted 45 lines · On Thu, 15 May 2025 22:47:27 -0700 Dan Williams <[email protected]>…
There are two components to establishing an encrypted link, provisioning the stream in Partner Port config-space, and programming the keys into the link layer via
IDE_KM(IDE Key Management). This new library,drivers/pci/ide.c, enables the former.IDE_KM, via a TSM low-level driver, is saved for later.With the platform TSM implementations of SEV-TIO and TDX Connect in mind this library abstracts small differences in those implementations. For example, TDX Connect handles Root Port register setup while SEV-TIO expects System Software to update the Root Port registers. This is the rationale for fine-grained 'setup' + 'enable' verbs.
The other design detail for TSM-coordinated IDE establishment is that the TSM may manage allocation of Stream IDs, this is why the Stream ID value is passed in to
pci_ide_stream_setup().The flow is:
pci_ide_stream_alloc()
Allocate a Selective IDE Stream Register Block in each Partner Port (Endpoint + Root Port), and reserve a host bridge / platform stream slot. Gather Partner Port specific stream settings like Requester ID.pci_ide_stream_register()
Publish the stream in sysfs after allocating a Stream ID. In the TSM case the TSM allocates the Stream ID for the Partner Port pair.pci_ide_stream_setup()
Program the stream settings to a Partner Port. Caller is responsible for optionally calling this for the Root Port as well if the TSM implementation requires it.pci_ide_stream_enable()
Try to run the stream afterIDE_KM.In support of system administrators auditing where platform, Root Port, and Endpoint IDE stream resources are being spent, the allocated stream is reflected as a symlink from the host bridge to the endpoint with the name:
stream%d.%d.%d:%sWhere the tuple of integers reflects the allocated platform, Root Port, and Endpoint stream index (Selective IDE Stream Register Block) values, and the %s is the endpoint device name.
Thanks to Wu Hao for a draft implementation of this infrastructure.
Quoted 8 lines
Cc Bjorn Helgaas <[email protected]>Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Co-developed-by Alexey Kardashevskiy <[email protected]>Signed-off-by Alexey Kardashevskiy <[email protected]>Co-developed-by Yilun Xu <[email protected]>Signed-off-by Yilun Xu <[email protected]>Signed-off-by Dan Williams <[email protected]>
A few little comments inline.
Thanks,
Jonathan
Quoted 28 lines
--- .../ABI/testing/sysfs-devices-pci-host-bridge | 38 ++ MAINTAINERS | 1 + drivers/pci/ide.c | 366 ++++++++++++++++++ include/linux/pci-ide.h | 76 ++++ include/linux/pci.h | 6 + include/uapi/linux/pci_regs.h | 2 + 6 files changed, 489 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-devices-pci-host-bridge create mode 100644 include/linux/pci-ide.hDocumentation/ABI/testing/sysfs-devices-pci-host-bridge new file +11
-0@@ -0,0 +1,38 @@ +What: /sys/devices/pciDDDD:BB + /sys/devices/.../pciDDDD:BB +Date: December, 2024 +Contact: [email protected] +Description: + A PCI host bridge device parents a PCI bus device topology. PCI + controllers may also parent host bridges. The DDDD:BB format + conveys the PCI domain (ACPI segment) number and root bus number + (in hexadecimal) of the host bridge. Note that the domain number + may be larger than the 16-bits that the "DDDD" format implies + for emulated host-bridges.
Documentation/ABI/testing/sysfs-devices-pci-host-bridge line 21
Quoted 10 lines
Documentation/ABI/testing/sysfs-devices-pci-host-bridge new file +10
-0+ +What: pciDDDD:BB/firmware_node +Date: December, 2024 +Contact: [email protected] +Description: + (RO) Symlink to the platform firmware device object "companion" + of the host bridge. For example, an ACPI device with an _HID of + PNP0A08 (/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00). See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format.
No problem with this documentation but not I think related to this patch and could go upstream before this?
Quoted 37 lines
Documentation/ABI/testing/sysfs-devices-pci-host-bridge new file +17
-0+ +What: pciDDDD:BB/streamH.R.E:DDDD:BB:DD:F +Date: December, 2024 +Contact: [email protected] +Description: + (RO) When a platform has established a secure connection, PCIe + IDE, between two Partner Ports, this symlink appears. The + primary function is to account the stream slot / resources + consumed in each of the (H)ost bridge, (R)oot Port and + (E)ndpoint that will be freed when invoking the tsm/disconnect + flow. The link points to the endpoint PCI device at domain:DDDD + bus:BB device:DD function:F. Where R and E represent the + assigned Selective IDE Stream Register Block in the Root Port + and Endpoint, and H represents a platform specific pool of + stream resources shared by the Root Ports in a host bridge. See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format.drivers/pci/ide.c +14
-0+/** +* pci_ide_stream_enable() - after setup, enable the stream +* @pdev: PCIe device object for either a Root Port or Endpoint Partner Port +* @ide: registered and setup IDE settings descriptor +* +* Activate the stream by writing to the Selective IDE Stream Control Register. +*/ +int pci_ide_stream_enable (struct pci_dev *pdev,struct pci_ide *ide) +{ +struct pci_ide_partner *settings =pci_ide_to_settings (pdev, ide); +int pos; +u32 val; + +if (!settings)
drivers/pci/ide.c void pci_ide_init(struct pci_dev *pdev) · line 441
Quoted 10 lines
drivers/pci/ide.c +10
-0+return -ENXIO ; + + pos =sel_ide_offset (pdev, settings); + +set_ide_sel_ctl (pdev, ide, pos, true); + +pci_read_config_dword (pdev, pos +PCI_IDE_SEL_STS , &val); +if (FIELD_GET (PCI_IDE_SEL_STS_STATE_MASK , val) != +PCI_IDE_SEL_STS_STATE_SECURE ) +return -ENXIO ;
Trivial but blank line here would I think help readability a tiny bit.
Quoted 10 lines
drivers/pci/ide.c +3
-0+return 0 ; +} +EXPORT_SYMBOL_GPL (pci_ide_stream_enable);include/linux/pci-ide.h new file +0
-0
include/linux/pci-ide.h line 9
Quoted 9 lines
include/linux/pci-ide.h new file +9
-0@@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright(c) 2024 Intel Corporation. All rights reserved. */ + +/* PCIe 6.2 section 6.33 Integrity & Data Encryption (IDE) */ + +#ifndef __PCI_IDE_H__ +#define __PCI_IDE_H__ + +#include <linux/range.h>
Needed? I'm guessing it was and isn't any more.
include/linux/pci-ide.h line 13
Quoted 4 lines
include/linux/pci-ide.h new file +4
-0+ +#define SEL_ADDR1_LOWER_MASK GENMASK(31, 20) +#define SEL_ADDR_UPPER_MASK GENMASK_ULL(63, 32) +#define PREP_PCI_IDE_SEL_ADDR1 (base, limit) \
ADDR_1 would be more consistent.
However, unless we are going to see a lot of these I'd personally prefer to see this lot inline in the code.
include/linux/pci-ide.h line 16
Quoted 3 lines
include/linux/pci-ide.h new file +3
-0+ (FIELD_PREP(PCI_IDE_SEL_ADDR_1_VALID, 1) | \ + FIELD_PREP(PCI_IDE_SEL_ADDR_1_BASE_LOW_MASK, \ + FIELD_GET(SEL_ADDR1_LOWER_MASK, (base))) | \
This is a case I'd just not use FIELD_PREP / GET for. Just ends up confusing and needs definitions that make little sense on their own.
lower_32_bits(base) & PCI_IDE_SEL_ADDR_1_BASE_LOW_MASK
perhaps.
include/linux/pci-ide.h line 18
Quoted 2 lines
include/linux/pci-ide.h new file +2
-0+FIELD_PREP (PCI_IDE_SEL_ADDR_1_LIMIT_LOW_MASK , \ + FIELD_GET(SEL_ADDR1_LOWER_MASK, (limit))))
Maybe use upper_32_bits() for this one.
However it is an odd macro and I can't immediately find where it is used so maybe just drop it?
include/linux/pci-ide.h line 23
Quoted 5 lines
include/linux/pci-ide.h new file +5
-0+ +#define PREP_PCI_IDE_SEL_RID_2 (base, domain) \ + (FIELD_PREP(PCI_IDE_SEL_RID_2_VALID, 1) | \ + FIELD_PREP(PCI_IDE_SEL_RID_2_BASE_MASK, (base)) | \ + FIELD_PREP(PCI_IDE_SEL_RID_2_SEG_MASK, (domain)))
This one I'd prefer to see inline.
Quoted 7 lines
include/linux/pci-ide.h new file +7
-0+/** +* struct pci_ide_partner - Per port IDE Stream settings +* @rid_start: Partner Port Requester ID range start +* @rid_start: Partner Port Requester ID range end +* @stream_index: Selective IDE Stream Register Block selection +*/ +struct pci_ide_partner {
include/linux/pci-ide.h line 49
Quoted 10 lines
include/linux/pci-ide.h new file +10
-0+u16 rid_start; +u16 rid_end; +u8 stream_index; +}; + +/** +* struct pci_ide - PCIe Selective IDE Stream descriptor +* @pdev: PCIe Endpoint for the stream +* @partner: settings for both partner ports in a stream +* @host_bridge_stream: track platform Stream index
Why the capital S? Seems a little inconsistent across different comments.
Quoted 14 lines
include/linux/pci-ide.h new file +14
-0+* @stream_id: unique id (within Partner Port pairing) for the stream +* @name: name of the stream in sysfs +* +* Negative @stream_id values indicate "uninitialized" on the +* expectation that with TSM established IDE the TSM owns the stream_id +* allocation. +*/ +struct pci_ide { +struct pci_dev *pdev; +struct pci_ide_partner partner[PCI_IDE_PARTNER_MAX ]; +u8 host_bridge_stream; +int stream_id; +const char *name; +};
Re: [PATCH v3 08/13] PCI/IDE: Add IDE establishment helpers
Quoted 95 lines · Jonathan Cameron
Quoted 45 lines · On Thu, 15 May 2025 22:47:27 -0700 Dan Williams <[email protected]>…
There are two components to establishing an encrypted link, provisioning the stream in Partner Port config-space, and programming the keys into the link layer via
IDE_KM(IDE Key Management). This new library,drivers/pci/ide.c, enables the former.IDE_KM, via a TSM low-level driver, is saved for later.With the platform TSM implementations of SEV-TIO and TDX Connect in mind this library abstracts small differences in those implementations. For example, TDX Connect handles Root Port register setup while SEV-TIO expects System Software to update the Root Port registers. This is the rationale for fine-grained 'setup' + 'enable' verbs.
The other design detail for TSM-coordinated IDE establishment is that the TSM may manage allocation of Stream IDs, this is why the Stream ID value is passed in to
pci_ide_stream_setup().The flow is:
pci_ide_stream_alloc()
Allocate a Selective IDE Stream Register Block in each Partner Port (Endpoint + Root Port), and reserve a host bridge / platform stream slot. Gather Partner Port specific stream settings like Requester ID.pci_ide_stream_register()
Publish the stream in sysfs after allocating a Stream ID. In the TSM case the TSM allocates the Stream ID for the Partner Port pair.pci_ide_stream_setup()
Program the stream settings to a Partner Port. Caller is responsible for optionally calling this for the Root Port as well if the TSM implementation requires it.pci_ide_stream_enable()
Try to run the stream afterIDE_KM.In support of system administrators auditing where platform, Root Port, and Endpoint IDE stream resources are being spent, the allocated stream is reflected as a symlink from the host bridge to the endpoint with the name:
stream%d.%d.%d:%sWhere the tuple of integers reflects the allocated platform, Root Port, and Endpoint stream index (Selective IDE Stream Register Block) values, and the %s is the endpoint device name.
Thanks to Wu Hao for a draft implementation of this infrastructure.
Quoted 8 lines
Cc Bjorn Helgaas <[email protected]>Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Co-developed-by Alexey Kardashevskiy <[email protected]>Signed-off-by Alexey Kardashevskiy <[email protected]>Co-developed-by Yilun Xu <[email protected]>Signed-off-by Yilun Xu <[email protected]>Signed-off-by Dan Williams <[email protected]>A few little comments inline.
Thanks,
Jonathan
Quoted 33 lines
--- .../ABI/testing/sysfs-devices-pci-host-bridge | 38 ++ MAINTAINERS | 1 + drivers/pci/ide.c | 366 ++++++++++++++++++ include/linux/pci-ide.h | 76 ++++ include/linux/pci.h | 6 + include/uapi/linux/pci_regs.h | 2 + 6 files changed, 489 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-devices-pci-host-bridge create mode 100644 include/linux/pci-ide.hDocumentation/ABI/testing/sysfs-devices-pci-host-bridge new file +16
-0@@ -0,0 +1,38 @@ +What: /sys/devices/pciDDDD:BB + /sys/devices/.../pciDDDD:BB +Date: December, 2024 +Contact: [email protected] +Description: + A PCI host bridge device parents a PCI bus device topology. PCI + controllers may also parent host bridges. The DDDD:BB format + conveys the PCI domain (ACPI segment) number and root bus number + (in hexadecimal) of the host bridge. Note that the domain number + may be larger than the 16-bits that the "DDDD" format implies + for emulated host-bridges. + +What: pciDDDD:BB/firmware_node +Date: December, 2024 +Contact: [email protected] +Description:
Quoted 8 lines
Documentation/ABI/testing/sysfs-devices-pci-host-bridge line 21
Quoted 5 lines
Documentation/ABI/testing/sysfs-devices-pci-host-bridge new file +5
-0+ (RO) Symlink to the platform firmware device object "companion" + of the host bridge. For example, an ACPI device with an _HID of + PNP0A08 (/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00). See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format.No problem with this documentation but not I think related to this patch and could go upstream before this?
Sure, given there are a few other things in this series that could go upstream in advance of the TSM bits, I will pull this and those out.
Quoted 42 lines
Quoted 42 lines
Documentation/ABI/testing/sysfs-devices-pci-host-bridge new file +17
-0+ +What: pciDDDD:BB/streamH.R.E:DDDD:BB:DD:F +Date: December, 2024 +Contact: [email protected] +Description: + (RO) When a platform has established a secure connection, PCIe + IDE, between two Partner Ports, this symlink appears. The + primary function is to account the stream slot / resources + consumed in each of the (H)ost bridge, (R)oot Port and + (E)ndpoint that will be freed when invoking the tsm/disconnect + flow. The link points to the endpoint PCI device at domain:DDDD + bus:BB device:DD function:F. Where R and E represent the + assigned Selective IDE Stream Register Block in the Root Port + and Endpoint, and H represents a platform specific pool of + stream resources shared by the Root Ports in a host bridge. See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format.drivers/pci/ide.c +18
-0+/** +* pci_ide_stream_enable() - after setup, enable the stream +* @pdev: PCIe device object for either a Root Port or Endpoint Partner Port +* @ide: registered and setup IDE settings descriptor +* +* Activate the stream by writing to the Selective IDE Stream Control Register. +*/ +int pci_ide_stream_enable (struct pci_dev *pdev,struct pci_ide *ide) +{ +struct pci_ide_partner *settings =pci_ide_to_settings (pdev, ide); +int pos; +u32 val; + +if (!settings) +return -ENXIO ; + + pos =sel_ide_offset (pdev, settings); +
Quoted 8 lines
drivers/pci/ide.c void pci_ide_init(struct pci_dev *pdev) · line 441
Quoted 6 lines
drivers/pci/ide.c +6
-0+set_ide_sel_ctl (pdev, ide, pos, true); + +pci_read_config_dword (pdev, pos +PCI_IDE_SEL_STS , &val); +if (FIELD_GET (PCI_IDE_SEL_STS_STATE_MASK , val) != +PCI_IDE_SEL_STS_STATE_SECURE ) +return -ENXIO ;Trivial but blank line here would I think help readability a tiny bit.
Ack.
Quoted 14 lines
Quoted 14 lines
drivers/pci/ide.c +3
-0+return 0 ; +} +EXPORT_SYMBOL_GPL (pci_ide_stream_enable);include/linux/pci-ide.h new file +3
-0@@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright(c) 2024 Intel Corporation. All rights reserved. */ +
Quoted 8 lines
include/linux/pci-ide.h line 9
Quoted 6 lines
include/linux/pci-ide.h new file +6
-0+/* PCIe 6.2 section 6.33 Integrity & Data Encryption (IDE) */ + +#ifndef __PCI_IDE_H__ +#define __PCI_IDE_H__ + +#include <linux/range.h> Needed? I'm guessing it was and isn't any more.
Yup, good catch.
Quoted 19 lines
include/linux/pci-ide.h line 13
Quoted 4 lines
include/linux/pci-ide.h new file +4
-0+ +#define SEL_ADDR1_LOWER_MASK GENMASK(31, 20) +#define SEL_ADDR_UPPER_MASK GENMASK_ULL(63, 32) +#define PREP_PCI_IDE_SEL_ADDR1 (base, limit) \
ADDR_1would be more consistent.However, unless we are going to see a lot of these I'd personally prefer to see this lot inline in the code.
include/linux/pci-ide.h line 16
Quoted 3 lines
include/linux/pci-ide.h new file +3
-0+ (FIELD_PREP(PCI_IDE_SEL_ADDR_1_VALID, 1) | \ + FIELD_PREP(PCI_IDE_SEL_ADDR_1_BASE_LOW_MASK, \ + FIELD_GET(SEL_ADDR1_LOWER_MASK, (base))) | \This is a case I'd just not use
FIELD_PREP/ GET for. Just ends up confusing and needs definitions that make little sense on their own.lower_32_bits(base) & PCI_IDE_SEL_ADDR_1_BASE_LOW_MASKperhaps.
Quoted 7 lines
include/linux/pci-ide.h line 18
Quoted 2 lines
include/linux/pci-ide.h new file +2
-0+FIELD_PREP (PCI_IDE_SEL_ADDR_1_LIMIT_LOW_MASK , \ + FIELD_GET(SEL_ADDR1_LOWER_MASK, (limit))))Maybe use
upper_32_bits()for this one.However it is an odd macro and I can't immediately find where it is used so maybe just drop it?
It was in an earlier rev, but I dropped it for simplicity. For now, there is no address limitations within the the stream.
Quoted 19 lines
include/linux/pci-ide.h line 23
Quoted 6 lines
include/linux/pci-ide.h new file +5
-0+ +#define PREP_PCI_IDE_SEL_RID_2 (base, domain) \ + (FIELD_PREP(PCI_IDE_SEL_RID_2_VALID, 1) | \ + FIELD_PREP(PCI_IDE_SEL_RID_2_BASE_MASK, (base)) | \ + FIELD_PREP(PCI_IDE_SEL_RID_2_SEG_MASK, (domain)))This one I'd prefer to see inline.
Quoted 11 lines
include/linux/pci-ide.h new file +11
-0+/** +* struct pci_ide_partner - Per port IDE Stream settings +* @rid_start: Partner Port Requester ID range start +* @rid_start: Partner Port Requester ID range end +* @stream_index: Selective IDE Stream Register Block selection +*/ +struct pci_ide_partner { +u16 rid_start; +u16 rid_end; +u8 stream_index; +};
Quoted 8 lines
include/linux/pci-ide.h line 49
Quoted 6 lines
include/linux/pci-ide.h new file +6
-0+ +/** +* struct pci_ide - PCIe Selective IDE Stream descriptor +* @pdev: PCIe Endpoint for the stream +* @partner: settings for both partner ports in a stream +* @host_bridge_stream: track platform Stream index Why the capital S? Seems a little inconsistent across different comments.
So this was deliberate, but does indeed look strange. Bjorn has asked, and I agree with him, that specification terms be capitalized. So the "Stream index" is the PCI defined number that gets programmed into hardware registers for a Selective IDE Stream, and "stream" is the name of the related Linux software collateral.
That is indeed too subtle. I think adding the full Selective IDE Stream for those cases will help distinguish.
Alexey Kardashevskiy
maintainerRe: [PATCH v3 08/13] PCI/IDE: Add IDE establishment helpers
In reply to Dan WilliamsQuoted 468 lines · Dan Williams
There are two components to establishing an encrypted link, provisioning the stream in Partner Port config-space, and programming the keys into the link layer via
IDE_KM(IDE Key Management). This new library,drivers/pci/ide.c, enables the former.IDE_KM, via a TSM low-level driver, is saved for later.With the platform TSM implementations of SEV-TIO and TDX Connect in mind this library abstracts small differences in those implementations. For example, TDX Connect handles Root Port register setup while SEV-TIO expects System Software to update the Root Port registers. This is the rationale for fine-grained 'setup' + 'enable' verbs.
The other design detail for TSM-coordinated IDE establishment is that the TSM may manage allocation of Stream IDs, this is why the Stream ID value is passed in to
pci_ide_stream_setup().The flow is:
pci_ide_stream_alloc()
Allocate a Selective IDE Stream Register Block in each Partner Port (Endpoint + Root Port), and reserve a host bridge / platform stream slot. Gather Partner Port specific stream settings like Requester ID.pci_ide_stream_register()
Publish the stream in sysfs after allocating a Stream ID. In the TSM case the TSM allocates the Stream ID for the Partner Port pair.pci_ide_stream_setup()
Program the stream settings to a Partner Port. Caller is responsible for optionally calling this for the Root Port as well if the TSM implementation requires it.pci_ide_stream_enable()
Try to run the stream afterIDE_KM.In support of system administrators auditing where platform, Root Port, and Endpoint IDE stream resources are being spent, the allocated stream is reflected as a symlink from the host bridge to the endpoint with the name:
stream%d.%d.%d:%sWhere the tuple of integers reflects the allocated platform, Root Port, and Endpoint stream index (Selective IDE Stream Register Block) values, and the %s is the endpoint device name.
Thanks to Wu Hao for a draft implementation of this infrastructure.
Cc Bjorn Helgaas <[email protected]>Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Co-developed-by Alexey Kardashevskiy <[email protected]>Signed-off-by Alexey Kardashevskiy <[email protected]>Co-developed-by Yilun Xu <[email protected]>Signed-off-by Yilun Xu <[email protected]>Signed-off-by Dan Williams <[email protected]>--- .../ABI/testing/sysfs-devices-pci-host-bridge | 38 ++ MAINTAINERS | 1 + drivers/pci/ide.c | 366 ++++++++++++++++++ include/linux/pci-ide.h | 76 ++++ include/linux/pci.h | 6 + include/uapi/linux/pci_regs.h | 2 + 6 files changed, 489 insertions(+)create mode 100644
Documentation/ABI/testing/sysfs-devices-pci-host-bridgecreate mode 100644include/linux/pci-ide.hDocumentation/ABI/testing/sysfs-devices-pci-host-bridge new file +38
-0@@ -0,0 +1,38 @@ +What: /sys/devices/pciDDDD:BB + /sys/devices/.../pciDDDD:BB +Date: December, 2024 +Contact: [email protected] +Description: + A PCI host bridge device parents a PCI bus device topology. PCI + controllers may also parent host bridges. The DDDD:BB format + conveys the PCI domain (ACPI segment) number and root bus number + (in hexadecimal) of the host bridge. Note that the domain number + may be larger than the 16-bits that the "DDDD" format implies + for emulated host-bridges. + +What: pciDDDD:BB/firmware_node +Date: December, 2024 +Contact: [email protected] +Description: + (RO) Symlink to the platform firmware device object "companion" + of the host bridge. For example, an ACPI device with an _HID of + PNP0A08 (/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00). See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format. + +What: pciDDDD:BB/streamH.R.E:DDDD:BB:DD:F +Date: December, 2024 +Contact: [email protected] +Description: + (RO) When a platform has established a secure connection, PCIe + IDE, between two Partner Ports, this symlink appears. The + primary function is to account the stream slot / resources + consumed in each of the (H)ost bridge, (R)oot Port and + (E)ndpoint that will be freed when invoking the tsm/disconnect + flow. The link points to the endpoint PCI device at domain:DDDD + bus:BB device:DD function:F. Where R and E represent the + assigned Selective IDE Stream Register Block in the Root Port + and Endpoint, and H represents a platform specific pool of + stream resources shared by the Root Ports in a host bridge. See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format.MAINTAINERS +1
-0@@ -18677,6 +18677,7 @@ Q: https://patchwork.kernel.org/project/linux-pci/list/ B: https://bugzilla.kernel.org C: irc://irc.oftc.net/linux-pci T: git git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git +F: Documentation/ABI/testing/sysfs-devices-pci-host-bridge F: Documentation/PCI/ F: Documentation/devicetree/bindings/pci/ F: arch/x86/kernel/early-quirks.cdrivers/pci/ide.c +331
-0@@ -5,6 +5,8 @@#define dev_fmt (fmt) "PCI/IDE: " fmt#include <linux/pci.h> +#include <linux/sysfs.h> +#include <linux/pci-ide.h> #include <linux/bitfield.h> #include "pci.h" @@ -96,5 +98,369 @@ void pci_ide_init(struct pci_dev *pdev) pdev->ide_cap = ide_cap; pdev->nr_link_ide = nr_link_ide; + pdev->nr_sel_ide = nr_streams; pdev->nr_ide_mem = nr_ide_mem; } + +struct stream_index { +unsigned long *map; +u8 max, stream_index; +}; + +static void free_stream_index (struct stream_index *stream) +{ +clear_bit_unlock (stream->stream_index, stream->map); +} + +DEFINE_FREE(free_stream, struct stream_index *, if (_T) free_stream_index(_T)) +static struct stream_index *alloc_stream_index (unsigned long *map,u8 max, +struct stream_index *stream) +{ +if (!max) +return NULL ; + +do { +u8 stream_index =find_first_zero_bit (map, max); + +if (stream_index == max) +return NULL ; +if (!test_and_set_bit_lock (stream_index, map)) { + *stream = (struct stream_index ) { + .map = map, + .max = max, + .stream_index = stream_index, + }; +return stream; + } +/* collided with another stream acquisition */ + }while (1 ); +} + +/** +* pci_ide_stream_alloc() - Reserve stream indices and probe for settings +* @pdev: IDE capable PCIe Endpoint Physical Function +* +* Retrieve the Requester ID range of @pdev for programming its Root +* Port IDE RID Association registers, and conversely retrieve the +* Requester ID of the Root Port for programming @pdev's IDE RID +* Association registers. +* +* Allocate a Selective IDE Stream Register Block instance per port. +* +* Allocate a platform stream resource from the associated host bridge. +* Retrieve stream association parameters for Requester ID range and +* address range restrictions for the stream. +*/ +struct pci_ide *pci_ide_stream_alloc (struct pci_dev *pdev) +{ +/* EP, RP, + HB Stream allocation */ +struct stream_index __stream[PCI_IDE_PARTNER_MAX +1 ]; +struct pci_host_bridge *hb; +struct pci_dev *rp; +int num_vf, rid_end; + +if (!pci_is_pcie (pdev)) +return NULL ; + +if (pci_pcie_type (pdev) !=PCI_EXP_TYPE_ENDPOINT ) +return NULL ; + +if (!pdev->ide_cap) +return NULL ; + +/* +* Catch buggy PCI platform initialization (missing +* pci_ide_init_nr_streams()) +*/ + hb =pci_find_host_bridge (pdev->bus); +if (WARN_ON_ONCE (!hb->nr_ide_streams)) +return NULL ; + + struct pci_ide *ide __free(kfree) = kzalloc(sizeof(*ide), GFP_KERNEL); +if (!ide) +return NULL ; + + struct stream_index *hb_stream __free(free_stream) = alloc_stream_index( + hb->ide_stream_map, hb->nr_ide_streams, &__stream[PCI_IDE_HB ]); +if (!hb_stream) +return NULL ; + + rp =pcie_find_root_port (pdev); + struct stream_index *rp_stream __free(free_stream) = alloc_stream_index( + rp->ide_stream_map, rp->nr_sel_ide, &__stream[PCI_IDE_RP ]); +if (!rp_stream) +return NULL ; + + struct stream_index *ep_stream __free(free_stream) = alloc_stream_index( + pdev->ide_stream_map, pdev->nr_sel_ide, &__stream[PCI_IDE_EP ]); +if (!ep_stream) +return NULL ; + +/* for SR-IOV case, cover all VFs */ + num_vf =pci_num_vf (pdev); +if (num_vf) + rid_end =PCI_DEVID (pci_iov_virtfn_bus (pdev, num_vf), +pci_iov_virtfn_devfn (pdev, num_vf)); +else + rid_end =pci_dev_id (pdev); + + *ide = (struct pci_ide ) { + .pdev = pdev, + .partner = { + [PCI_IDE_EP ] = { + .rid_start =pci_dev_id (rp), + .rid_end =pci_dev_id (rp), + .stream_index =no_free_ptr (ep_stream)->stream_index, + }, + [PCI_IDE_RP ] = { + .rid_start =pci_dev_id (pdev), + .rid_end = rid_end, + .stream_index =no_free_ptr (rp_stream)->stream_index, + }, + }, + .host_bridge_stream =no_free_ptr (hb_stream)->stream_index, + .stream_id =-1 , + }; + +return_ptr (ide); +} +EXPORT_SYMBOL_GPL (pci_ide_stream_alloc); + +/** +* pci_ide_stream_free() - unwind pci_ide_stream_alloc() +* @ide: idle IDE settings descriptor +* +* Free all of the stream index (register block) allocations acquired by +* pci_ide_stream_alloc(). The stream represented by @ide is assumed to +* be unregistered and not instantiated in any device. +*/ +void pci_ide_stream_free (struct pci_ide *ide) +{ +struct pci_dev *pdev = ide->pdev; +struct pci_dev *rp =pcie_find_root_port (pdev); +struct pci_host_bridge *hb =pci_find_host_bridge (pdev->bus); + +clear_bit_unlock (ide->partner[PCI_IDE_EP ].stream_index, + pdev->ide_stream_map); +clear_bit_unlock (ide->partner[PCI_IDE_RP ].stream_index, + rp->ide_stream_map); +clear_bit_unlock (ide->host_bridge_stream, hb->ide_stream_map); +kfree (ide); +} +EXPORT_SYMBOL_GPL (pci_ide_stream_free); + +/** +* pci_ide_stream_register() - Prepare to activate an IDE Stream +* @ide: IDE settings descriptor +* +* After a Stream ID has been acquired for @ide, record the presence of +* the stream in sysfs. The expectation is that @ide is immutable while +* registered. +*/ +int pci_ide_stream_register (struct pci_ide *ide) +{ +struct pci_dev *pdev = ide->pdev; +struct pci_host_bridge *hb =pci_find_host_bridge (pdev->bus); +u8 ep_stream, rp_stream; +int rc; + +if (ide->stream_id <0 || ide->stream_id >U8_MAX ) { +pci_err (pdev,"Setup fail: Invalid Stream ID: %d\n" , ide->stream_id); +return -ENXIO ; + } + + ep_stream = ide->partner[PCI_IDE_EP ].stream_index; + rp_stream = ide->partner[PCI_IDE_RP ].stream_index; + const char *name __free(kfree) = kasprintf( +GFP_KERNEL ,"stream%d.%d.%d:%s" , ide->host_bridge_stream, + rp_stream, ep_stream,dev_name (&pdev->dev)); +if (!name) +return -ENOMEM ; + + rc =sysfs_create_link (&hb->dev.kobj, &pdev->dev.kobj, name); +if (rc) +return rc; + + ide->name =no_free_ptr (name); + +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_ide_stream_register); + +/** +* pci_ide_stream_unregister() - unwind pci_ide_stream_register() +* @ide: idle IDE settings descriptor +* +* In preparation for freeing @ide, remove sysfs enumeration for the +* stream. +*/ +void pci_ide_stream_unregister (struct pci_ide *ide) +{ +struct pci_dev *pdev = ide->pdev; +struct pci_host_bridge *hb =pci_find_host_bridge (pdev->bus); + +sysfs_remove_link (&hb->dev.kobj, ide->name); +kfree (ide->name); +} +EXPORT_SYMBOL_GPL (pci_ide_stream_unregister); + +int pci_ide_domain (struct pci_dev *pdev) +{ +if (pdev->fm_enabled) +return pci_domain_nr (pdev->bus); +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_ide_domain); + +struct pci_ide_partner *pci_ide_to_settings (struct pci_dev *pdev,struct pci_ide *ide) +{ +if (!pci_is_pcie (pdev)) { +pci_warn_once (pdev,"not a PCIe device\n" ); +return NULL ; + } + +switch (pci_pcie_type (pdev)) { +case PCI_EXP_TYPE_ENDPOINT : +if (pdev != ide->pdev) { +pci_warn_once (pdev,"setup expected Endpoint: %s\n" ,pci_name (ide->pdev)); +return NULL ; + } +return &ide->partner[PCI_IDE_EP ]; +case PCI_EXP_TYPE_ROOT_PORT : { +struct pci_dev *rp =pcie_find_root_port (ide->pdev); + +if (pdev != rp) { +pci_warn_once (pdev,"setup expected Root Port: %s\n" , +pci_name (rp)); +return NULL ; + } +return &ide->partner[PCI_IDE_RP ]; + } +default : +pci_warn_once (pdev,"invalid device type\n" ); +return NULL ; + } +} +EXPORT_SYMBOL_GPL (pci_ide_to_settings); + +static void set_ide_sel_ctl (struct pci_dev *pdev,struct pci_ide *ide,int pos, +bool enable) +{ +u32 val =FIELD_PREP (PCI_IDE_SEL_CTL_ID_MASK , ide->stream_id) | +FIELD_PREP (PCI_IDE_SEL_CTL_DEFAULT ,1 ) | +FIELD_PREP (PCI_IDE_SEL_CTL_CFG_EN , pdev->ide_cfg) | +FIELD_PREP (PCI_IDE_SEL_CTL_TEE_LIMITED , pdev->ide_tee_limit) | +FIELD_PREP (PCI_IDE_SEL_CTL_EN , enable); + +pci_write_config_dword (pdev, pos +PCI_IDE_SEL_CTL , val); +} + +/** +* pci_ide_stream_setup() - program settings to Selective IDE Stream registers +* @pdev: PCIe device object for either a Root Port or Endpoint Partner Port +* @ide: registered IDE settings descriptor +* +* When @pdev is a PCI_EXP_TYPE_ENDPOINT then the PCI_IDE_EP partner +* settings are written to @pdev's Selective IDE Stream register block, +* and when @pdev is a PCI_EXP_TYPE_ROOT_PORT, the PCI_IDE_RP settings +* are selected. +*/ +void pci_ide_stream_setup (struct pci_dev *pdev,struct pci_ide *ide) +{ +struct pci_ide_partner *settings =pci_ide_to_settings (pdev, ide); +int pos; +u32 val; + +if (!settings) +return ; + + pos =sel_ide_offset (pdev, settings); + + val =FIELD_PREP (PCI_IDE_SEL_RID_1_LIMIT_MASK , settings->rid_end); +pci_write_config_dword (pdev, pos +PCI_IDE_SEL_RID_1 , val); + + val =PREP_PCI_IDE_SEL_RID_2 (settings->rid_start,pci_ide_domain (pdev)); +pci_write_config_dword (pdev, pos +PCI_IDE_SEL_RID_2 , val); + +/* +* Setup control register early for devices that expect +* stream_id is set during key programming. +*/ +set_ide_sel_ctl (pdev, ide, pos, false); +} +EXPORT_SYMBOL_GPL (pci_ide_stream_setup); + +/** +* pci_ide_stream_teardown() - disable the stream and clear all settings +* @pdev: PCIe device object for either a Root Port or Endpoint Partner Port +* @ide: registered IDE settings descriptor +* +* For stream destruction, zero all registers that may have been written +* by pci_ide_stream_setup(). Consider pci_ide_stream_disable() to leave +* settings in place while temporarily disabling the stream. +*/ +void pci_ide_stream_teardown (struct pci_dev *pdev,struct pci_ide *ide) +{ +struct pci_ide_partner *settings =pci_ide_to_settings (pdev, ide); +int pos; + +if (!settings) +return ; + + pos =sel_ide_offset (pdev, settings); + +pci_write_config_dword (pdev, pos +PCI_IDE_SEL_CTL ,0 ); +pci_write_config_dword (pdev, pos +PCI_IDE_SEL_RID_2 ,0 ); +pci_write_config_dword (pdev, pos +PCI_IDE_SEL_RID_1 ,0 ); +} +EXPORT_SYMBOL_GPL (pci_ide_stream_teardown); + +/** +* pci_ide_stream_enable() - after setup, enable the stream +* @pdev: PCIe device object for either a Root Port or Endpoint Partner Port +* @ide: registered and setup IDE settings descriptor +* +* Activate the stream by writing to the Selective IDE Stream Control Register. +*/ +int pci_ide_stream_enable (struct pci_dev *pdev,struct pci_ide *ide) +{ +struct pci_ide_partner *settings =pci_ide_to_settings (pdev, ide); +int pos; +u32 val; + +if (!settings)
drivers/pci/ide.c void pci_ide_init(struct pci_dev *pdev) · line 441
Quoted 10 lines
drivers/pci/ide.c +10
-0+return -ENXIO ; + + pos =sel_ide_offset (pdev, settings); + +set_ide_sel_ctl (pdev, ide, pos, true); + +pci_read_config_dword (pdev, pos +PCI_IDE_SEL_STS , &val); +if (FIELD_GET (PCI_IDE_SEL_STS_STATE_MASK , val) != +PCI_IDE_SEL_STS_STATE_SECURE ) +return -ENXIO ;
The PCIe spec allows (but funnily "does not recommend") enabling the stream before the keys are programmed and one of my test devices insists on doing it this way (the other one is fine) so at least a different code should be used here? Thanks,
Quoted 144 lines
drivers/pci/ide.c +25
-0+return 0 ; +} +EXPORT_SYMBOL_GPL (pci_ide_stream_enable); + +/** +* pci_ide_stream_disable() - disable the given stream +* @pdev: PCIe device object for either a Root Port or Endpoint Partner Port +* @ide: registered and setup IDE settings descriptor +* +* Clear the Selective IDE Stream Control Register, but leave all other +* registers untouched. +*/ +void pci_ide_stream_disable (struct pci_dev *pdev,struct pci_ide *ide) +{ +struct pci_ide_partner *settings =pci_ide_to_settings (pdev, ide); +int pos; + +if (!settings) +return ; + + pos =sel_ide_offset (pdev, settings); + +pci_write_config_dword (pdev, pos +PCI_IDE_SEL_CTL ,0 ); +} +EXPORT_SYMBOL_GPL (pci_ide_stream_disable);include/linux/pci-ide.h new file +76
-0@@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright(c) 2024 Intel Corporation. All rights reserved. */ + +/* PCIe 6.2 section 6.33 Integrity & Data Encryption (IDE) */ + +#ifndef __PCI_IDE_H__ +#define __PCI_IDE_H__ + +#include <linux/range.h> + +#define SEL_ADDR1_LOWER_MASK GENMASK(31, 20) +#define SEL_ADDR_UPPER_MASK GENMASK_ULL(63, 32) +#define PREP_PCI_IDE_SEL_ADDR1 (base, limit) \ + (FIELD_PREP(PCI_IDE_SEL_ADDR_1_VALID, 1) | \ + FIELD_PREP(PCI_IDE_SEL_ADDR_1_BASE_LOW_MASK, \ + FIELD_GET(SEL_ADDR1_LOWER_MASK, (base))) | \ + FIELD_PREP(PCI_IDE_SEL_ADDR_1_LIMIT_LOW_MASK, \ + FIELD_GET(SEL_ADDR1_LOWER_MASK, (limit)))) + +#define PREP_PCI_IDE_SEL_RID_2 (base, domain) \ + (FIELD_PREP(PCI_IDE_SEL_RID_2_VALID, 1) | \ + FIELD_PREP(PCI_IDE_SEL_RID_2_BASE_MASK, (base)) | \ + FIELD_PREP(PCI_IDE_SEL_RID_2_SEG_MASK, (domain))) + +enum pci_ide_partner_select { +PCI_IDE_EP , +PCI_IDE_RP , +PCI_IDE_PARTNER_MAX , +/* pci_ide_stream_alloc() uses this for stream index allocation */ +PCI_IDE_HB =PCI_IDE_PARTNER_MAX , +}; + +/** +* struct pci_ide_partner - Per port IDE Stream settings +* @rid_start: Partner Port Requester ID range start +* @rid_start: Partner Port Requester ID range end +* @stream_index: Selective IDE Stream Register Block selection +*/ +struct pci_ide_partner { +u16 rid_start; +u16 rid_end; +u8 stream_index; +}; + +/** +* struct pci_ide - PCIe Selective IDE Stream descriptor +* @pdev: PCIe Endpoint for the stream +* @partner: settings for both partner ports in a stream +* @host_bridge_stream: track platform Stream index +* @stream_id: unique id (within Partner Port pairing) for the stream +* @name: name of the stream in sysfs +* +* Negative @stream_id values indicate "uninitialized" on the +* expectation that with TSM established IDE the TSM owns the stream_id +* allocation. +*/ +struct pci_ide { +struct pci_dev *pdev; +struct pci_ide_partner partner[PCI_IDE_PARTNER_MAX ]; +u8 host_bridge_stream; +int stream_id; +const char *name; +}; + +int pci_ide_domain (struct pci_dev *pdev); +struct pci_ide_partner *pci_ide_to_settings (struct pci_dev *pdev,struct pci_ide *ide); +struct pci_ide *pci_ide_stream_alloc (struct pci_dev *pdev); +void pci_ide_stream_free (struct pci_ide *ide); +DEFINE_FREE(pci_ide_stream_free, struct pci_ide *, if (_T) pci_ide_stream_free(_T)) +int pci_ide_stream_register (struct pci_ide *ide); +void pci_ide_stream_unregister (struct pci_ide *ide); +void pci_ide_stream_setup (struct pci_dev *pdev,struct pci_ide *ide); +void pci_ide_stream_teardown (struct pci_dev *pdev,struct pci_ide *ide); +int pci_ide_stream_enable (struct pci_dev *pdev,struct pci_ide *ide); +void pci_ide_stream_disable (struct pci_dev *pdev,struct pci_ide *ide); +#endif /* __PCI_IDE_H__ */ include/linux/pci.h +6
-0@@ -538,6 +538,8 @@ struct pci_dev {u16 ide_cap;/* Link Integrity & Data Encryption */ u8 nr_ide_mem;/* Address association resources for streams */ u8 nr_link_ide;/* Link Stream count (Selective Stream offset) */ +u8 nr_sel_ide;/* Selective Stream count (register block allocator) */ +DECLARE_BITMAP (ide_stream_map,CONFIG_PCI_IDE_STREAM_MAX ); unsigned int ide_cfg:1; /* Config cycles over IDE */ unsigned int ide_tee_limit:1; /* Disallow T=0 traffic over IDE */#endif @@ -605,6 +607,10 @@ struct pci_host_bridge {int domain_nr;struct list_head windows;/* resource_entry */ struct list_head dma_ranges;/* dma ranges resource list */ +#ifdef CONFIG_PCI_IDE +u8 nr_ide_streams;/* Track available vs in-use streams */ +DECLARE_BITMAP (ide_stream_map,CONFIG_PCI_IDE_STREAM_MAX ); +#endif u8 (*swizzle_irq)(struct pci_dev *,u8 *);/* Platform IRQ swizzler */ int (*map_irq)(const struct pci_dev *,u8 ,u8 );void (*release_fn)(struct pci_host_bridge *);include/uapi/linux/pci_regs.h +2
-0@@ -1286,6 +1286,8 @@/* Selective IDE Stream Status Register */ #define PCI_IDE_SEL_STS 8#define PCI_IDE_SEL_STS_STATE_MASK __GENMASK(3, 0)/* Selective IDE Stream State */ +#define PCI_IDE_SEL_STS_STATE_INSECURE 0 +#define PCI_IDE_SEL_STS_STATE_SECURE 2#define PCI_IDE_SEL_STS_RECVD_INTEGRITY_CHECK 0x80000000/* Received Integrity Check Fail Msg */ /* IDE RID Association Register 1 */ #define PCI_IDE_SEL_RID_1 0xc
Signature · 1 line
Alexey
PATCH v3 9/13 PCI/IDE: Report available IDE streams
In reply to Dan WilliamsThe limited number of link-encryption (IDE) streams that a given set of host bridges supports is a platform specific detail. Provide pci_ide_init_nr_streams() as a generic facility for either platform TSM drivers, or PCI core native IDE, to report the number available streams. After invoking pci_ide_init_nr_streams() an "available_secure_streams" attribute appears in PCI host bridge sysfs to convey that count.
--- .../ABI/testing/sysfs-devices-pci-host-bridge | 13 ++++ drivers/pci/ide.c | 59 +++++++++++++++++++ drivers/pci/pci.h | 3 + drivers/pci/probe.c | 12 +++- include/linux/pci.h | 8 +++ 5 files changed, 94 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-devices-pci-host-bridge +13 -0
@@ -36,3 +36,16 @@ Description: stream resources shared by the Root Ports in a host bridge. See /sys/devices/pciDDDD:BB entry for details about the DDDD:BB format. + +What: pciDDDD:BB/available_secure_streams +Date: December, 2024 +Contact: [email protected] +Description: + (RO) When a host bridge has Root Ports that support PCIe IDE + (link encryption and integrity protection) there may be a + limited number of streams that can be used for establishing new + secure links. This attribute decrements upon secure link setup, + and increments upon secure link teardown. The in-use stream + count is determined by counting stream symlinks. See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format.
drivers/pci/ide.c +59 -0
62 lines not on this page · raw message
drivers/pci/pci.h +3 -0
@@ -513,8 +513,11 @@ static inline void pci_doe_sysfs_teardown(struct pci_dev *pdev) { }#ifdef CONFIG_PCI_IDE void pci_ide_init (struct pci_dev *dev); +extern struct attribute_group pci_ide_attr_group; +#define PCI_IDE_ATTR_GROUP (&pci_ide_attr_group)#else static inline void pci_ide_init (struct pci_dev *dev) { } +#define PCI_IDE_ATTR_GROUP NULL#endif #ifdef CONFIG_PCI_TSM
drivers/pci/probe.c +11 -1
@@ -640,6 +640,16 @@ static void pci_release_host_bridge_dev(struct device *dev)kfree (bridge); } +static const struct attribute_group *pci_host_bridge_groups[] = { +PCI_IDE_ATTR_GROUP , +NULL , +}; + +static const struct device_type pci_host_bridge_type = { + .groups = pci_host_bridge_groups, + .release = pci_release_host_bridge_dev, +}; +static void pci_init_host_bridge (struct pci_host_bridge *bridge) {INIT_LIST_HEAD (&bridge->windows);
@@ -659,6 +669,7 @@ static void pci_init_host_bridge(struct pci_host_bridge *bridge) bridge->native_dpc =1 ; bridge->domain_nr =PCI_DOMAIN_NR_NOT_SET ; bridge->native_cxl_error =1 ; + bridge->dev.type = &pci_host_bridge_type;device_initialize (&bridge->dev); }
@@ -672,7 +683,6 @@ struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)return NULL ;pci_init_host_bridge (bridge); - bridge->dev.release = pci_release_host_bridge_dev;return bridge; }
include/linux/pci.h +8 -0
@@ -662,6 +662,14 @@ void pci_set_host_bridge_release(struct pci_host_bridge *bridge,void (*release_fn)(struct pci_host_bridge *), void *release_data); +#ifdef CONFIG_PCI_IDE +void pci_ide_init_nr_streams (struct pci_host_bridge *hb,int nr); +#else +static inline void pci_ide_init_nr_streams (struct pci_host_bridge *hb,int nr) +{ +} +#endif +int pcibios_root_bridge_prepare (struct pci_host_bridge *bridge);#define PCI_REGION_FLAG_MASK 0x0fU/* These bits of resource flags tell us the PCI region flags */
Signature · 1 line
2.49.0
kernel test robot
build botRe: [PATCH v3 09/13] PCI/IDE: Report available IDE streams
kernel test robot noticed the following build warnings:
Hi Dan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 7515f45c165269b72ee739e6fc26cc2ef928fc1b]
url: https://github.com/intel-lab-lkp/linux/commits/Dan-Williams/coco-tsm-Introduce-a-core-device-for-TEE-Security-Managers/20250516-135307 base: 7515f45c165269b72ee739e6fc26cc2ef928fc1b patch link: https://lore.kernel.org/r/20250516054732.2055093-10-dan.j.williams%40intel.com patch subject: [PATCH v3 09/13] PCI/IDE: Report available IDE streams config: i386-randconfig-r072-20250518 (https://download.01.org/0day-ci/archive/20250518/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
smatch warnings:drivers/pci/ide.c:475 available_secure_streams_show() warn: unsigned 'hb->nr_ide_streams' is never less than zero. drivers/pci/ide.c:495 pci_ide_attr_visible() warn: unsigned 'hb->nr_ide_streams' is never less than zero.
vim +475 drivers/pci/ide.c
467
468 static ssize_t available_secure_streams_show(struct device *dev,
469 struct device_attribute *attr,
470 char *buf)
471 {
472 struct pci_host_bridge *hb = to_pci_host_bridge(dev);
473 int avail;
474
> 475 if (hb->nr_ide_streams < 0)
476 return -ENXIO;
477
478 avail = hb->nr_ide_streams -
479 bitmap_weight(hb->ide_stream_map, hb->nr_ide_streams);
480 return sysfs_emit(buf, "%d\n", avail);
481 }
482 static DEVICE_ATTR_RO(available_secure_streams);
483
484 static struct attribute *pci_ide_attrs[] = {
485 &dev_attr_available_secure_streams.attr,
486 NULL,
487 };
488
489 static umode_t pci_ide_attr_visible(struct kobject *kobj, struct attribute *a, int n)
490 {
491 struct device *dev = kobj_to_dev(kobj);
492 struct pci_host_bridge *hb = to_pci_host_bridge(dev);
493
494 if (a == &dev_attr_available_secure_streams.attr)
> 495 if (hb->nr_ide_streams < 0) 496 return 0;
497
498 return a->mode;
499 }
500
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v3 09/13] PCI/IDE: Report available IDE streams
In reply to Dan WilliamsQuoted 7 lines · On Thu, 15 May 2025 22:47:28 -0700 Dan Williams <[email protected]>…
The limited number of link-encryption (IDE) streams that a given set of host bridges supports is a platform specific detail. Provide
pci_ide_init_nr_streams()as a generic facility for either platform TSM drivers, or PCI core native IDE, to report the number available streams. After invokingpci_ide_init_nr_streams()an "available_secure_streams" attribute appears in PCI host bridge sysfs to convey that count.
Quoted 6 lines
Cc Bjorn Helgaas <[email protected]>Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Cc Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>
Trivial stuff inline.
Quoted 19 lines
--- .../ABI/testing/sysfs-devices-pci-host-bridge | 13 ++++ drivers/pci/ide.c | 59 +++++++++++++++++++ drivers/pci/pci.h | 3 + drivers/pci/probe.c | 12 +++- include/linux/pci.h | 8 +++ 5 files changed, 94 insertions(+), 1 deletion(-)Documentation/ABI/testing/sysfs-devices-pci-host-bridge +3
-0@@ -36,3 +36,16 @@ Description: stream resources shared by the Root Ports in a host bridge. See /sys/devices/pciDDDD:BB entry for details about the DDDD:BB format. + +What: pciDDDD:BB/available_secure_streams +Date: December, 2024
Documentation/ABI/testing/sysfs-devices-pci-host-bridge Description: · line 51
Quoted 10 lines
Documentation/ABI/testing/sysfs-devices-pci-host-bridge +10
-0+Contact: [email protected] +Description: + (RO) When a host bridge has Root Ports that support PCIe IDE + (link encryption and integrity protection) there may be a + limited number of streams that can be used for establishing new + secure links. This attribute decrements upon secure link setup, + and increments upon secure link teardown. The in-use stream + count is determined by counting stream symlinks. See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format.
Is this name specific enough given mix of link and selective streams, both of which are limited? Nice to use generic terms but this one feels too generic!
drivers/pci/ide.c void pci_ide_stream_disable(struct pci_dev *pdev, struct pci_ide *ide) · line 486
Quoted 8 lines
drivers/pci/ide.c +3
-0+static struct attribute *pci_ide_attrs[] = { + &dev_attr_available_secure_streams.attr, +NULL ,
As below. No trailing comma here.
Quoted 9 lines
drivers/pci/ide.c +1
-0+};drivers/pci/probe.c +0
-0
drivers/pci/probe.c static void pci_release_host_bridge_dev(struct device *dev) · line 645
Quoted 6 lines
drivers/pci/probe.c +3
-0@@ -640,6 +640,16 @@ static void pci_release_host_bridge_dev(struct device *dev)kfree (bridge); } +static const struct attribute_group *pci_host_bridge_groups[] = { +PCI_IDE_ATTR_GROUP , +NULL ,
One of my favourite comments :) No comma on terminators. Let's not make it easy to accidentally put something after them.
drivers/pci/probe.c static void pci_release_host_bridge_dev(struct device *dev) · line 650
Quoted 5 lines
drivers/pci/probe.c +5
-0+}; + +static const struct device_type pci_host_bridge_type = { + .groups = pci_host_bridge_groups, + .release = pci_release_host_bridge_dev,
I've no problem with this as a clean up but you could have just used the bridge->dev.groups instead I think? If you are clearing that out for some other use alter, mention that in the patch description.
Quoted 21 lines
drivers/pci/probe.c +3
-1+}; +static void pci_init_host_bridge (struct pci_host_bridge *bridge) {INIT_LIST_HEAD (&bridge->windows);@@ -659,6 +669,7 @@ static void pci_init_host_bridge(struct pci_host_bridge *bridge) bridge->native_dpc =1 ; bridge->domain_nr =PCI_DOMAIN_NR_NOT_SET ; bridge->native_cxl_error =1 ; + bridge->dev.type = &pci_host_bridge_type;device_initialize (&bridge->dev); }@@ -672,7 +683,6 @@ struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)return NULL ;pci_init_host_bridge (bridge); - bridge->dev.release = pci_release_host_bridge_dev;return bridge; }
Re: [PATCH v3 09/13] PCI/IDE: Report available IDE streams
Quoted 42 lines · Jonathan Cameron
Quoted 7 lines · On Thu, 15 May 2025 22:47:28 -0700 Dan Williams <[email protected]>…
The limited number of link-encryption (IDE) streams that a given set of host bridges supports is a platform specific detail. Provide
pci_ide_init_nr_streams()as a generic facility for either platform TSM drivers, or PCI core native IDE, to report the number available streams. After invokingpci_ide_init_nr_streams()an "available_secure_streams" attribute appears in PCI host bridge sysfs to convey that count.Quoted 6 lines
Cc Bjorn Helgaas <[email protected]>Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Cc Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>Trivial stuff inline.
Quoted 24 lines
--- .../ABI/testing/sysfs-devices-pci-host-bridge | 13 ++++ drivers/pci/ide.c | 59 +++++++++++++++++++ drivers/pci/pci.h | 3 + drivers/pci/probe.c | 12 +++- include/linux/pci.h | 8 +++ 5 files changed, 94 insertions(+), 1 deletion(-)Documentation/ABI/testing/sysfs-devices-pci-host-bridge +8
-0@@ -36,3 +36,16 @@ Description: stream resources shared by the Root Ports in a host bridge. See /sys/devices/pciDDDD:BB entry for details about the DDDD:BB format. + +What: pciDDDD:BB/available_secure_streams +Date: December, 2024 +Contact: [email protected] +Description: + (RO) When a host bridge has Root Ports that support PCIe IDE + (link encryption and integrity protection) there may be a + limited number of streams that can be used for establishing new
Quoted 8 lines
Documentation/ABI/testing/sysfs-devices-pci-host-bridge Description: · line 51
Quoted 5 lines
Documentation/ABI/testing/sysfs-devices-pci-host-bridge +5
-0+ secure links. This attribute decrements upon secure link setup, + and increments upon secure link teardown. The in-use stream + count is determined by counting stream symlinks. See + /sys/devices/pciDDDD:BB entry for details about the DDDD:BB + format.Is this name specific enough given mix of link and selective streams, both of which are limited? Nice to use generic terms but this one feels too generic!
I will update the description to call out that these are Selective IDE Stream resources, but keep the generic name of the attribute.
If Linux ever grows Link IDE support that can come in with a different name. Especially because the security properties of Link IDE are weaker in the presence of switches. I.e. you need to trust a switch to decrypt/re-crypt.
So if Link IDE support ever arrives it can call its related attribute "available_local_secure_streams" or something similar as "Selective" and "Link" are not saying much to an admin that has not ready the PCIe specification.
Quoted 23 lines
drivers/pci/ide.c void pci_ide_stream_disable(struct pci_dev *pdev, struct pci_ide *ide) · line 486
Quoted 8 lines
drivers/pci/ide.c +3
-0+static struct attribute *pci_ide_attrs[] = { + &dev_attr_available_secure_streams.attr, +NULL ,As below. No trailing comma here.
Quoted 12 lines
drivers/pci/ide.c +1
-0+};drivers/pci/probe.c +0
-0@@ -640,6 +640,16 @@ static void pci_release_host_bridge_dev(struct device *dev)kfree (bridge); }
Quoted 6 lines
drivers/pci/probe.c static void pci_release_host_bridge_dev(struct device *dev) · line 645
Quoted 3 lines
drivers/pci/probe.c +3
-0+static const struct attribute_group *pci_host_bridge_groups[] = { +PCI_IDE_ATTR_GROUP , +NULL ,One of my favourite comments :) No comma on terminators. Let's not make it easy to accidentally put something after them.
Fixed. ...might be worth a checkpatch update for this, my Perl-fu is rusty though.
Quoted 10 lines
drivers/pci/probe.c static void pci_release_host_bridge_dev(struct device *dev) · line 650
Quoted 6 lines
drivers/pci/probe.c +5
-0+}; + +static const struct device_type pci_host_bridge_type = { + .groups = pci_host_bridge_groups, + .release = pci_release_host_bridge_dev,I've no problem with this as a clean up but you could have just used the
bridge->dev.groupsinstead I think? If you are clearing that out for some other use alter, mention that in the patch description.
Sure, I will mention it.
PATCH v3 10/13 PCI/TSM: Report active IDE streams
In reply to Dan WilliamsGiven that the platform TSM owns IDE Stream ID allocation, report the active streams via the TSM class device. Establish a symlink from the class device to the PCI endpoint device consuming the stream, named by the Stream ID.
--- Documentation/ABI/testing/sysfs-class-tsm | 10 ++++++++++ drivers/virt/coco/host/tsm-core.c | 17 +++++++++++++++++ include/linux/tsm.h | 4 ++++ 3 files changed, 31 insertions(+)
Documentation/ABI/testing/sysfs-class-tsm +10 -0
@@ -8,3 +8,13 @@ Description: signals when the PCI layer is able to support establishment of link encryption and other device-security features coordinated through the platform tsm. + +What: /sys/class/tsm/tsm0/streamN:DDDD:BB:DD:F +Date: December, 2024 +Contact: [email protected] +Description: + (RO) When a host bridge has established a secure connection via + the platform TSM, symlink appears. The primary function of this + is have a system global review of TSM resource consumption + across host bridges. The link points to the endpoint PCI device + at domain:DDDD bus:BB device:DD function:F.
drivers/virt/coco/host/tsm-core.c +17 -0
@@ -2,13 +2,16 @@/* Copyright(c) 2024 Intel Corporation. All rights reserved. */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#define dev_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/tsm.h> +#include <linux/pci.h> #include <linux/rwsem.h> #include <linux/device.h> #include <linux/module.h> #include <linux/cleanup.h> #include <linux/pci-tsm.h> +#include <linux/pci-ide.h> static DECLARE_RWSEM (tsm_core_rwsem);static struct class *tsm_class;
@@ -99,6 +102,20 @@ void tsm_unregister(struct tsm_core_dev *core) }EXPORT_SYMBOL_GPL (tsm_unregister); +/* must be invoked between tsm_register / tsm_unregister */ +int tsm_ide_stream_register (struct pci_dev *pdev,struct pci_ide *ide) +{ +return sysfs_create_link (&tsm_core->dev.kobj, &pdev->dev.kobj, + ide->name); +} +EXPORT_SYMBOL_GPL (tsm_ide_stream_register); + +void tsm_ide_stream_unregister (struct pci_ide *ide) +{ +sysfs_remove_link (&tsm_core->dev.kobj, ide->name); +} +EXPORT_SYMBOL_GPL (tsm_ide_stream_unregister); +static void tsm_release (struct device *dev) {struct tsm_core_dev *core =container_of (dev,typeof (*core), dev);
include/linux/tsm.h +4 -0
@@ -116,4 +116,8 @@ struct tsm_core_dev *tsm_register(struct device *parent,const struct attribute_group **groups, const struct pci_tsm_ops *ops);void tsm_unregister (struct tsm_core_dev *tsm_core); +struct pci_dev ; +struct pci_ide ; +int tsm_ide_stream_register (struct pci_dev *pdev,struct pci_ide *ide); +void tsm_ide_stream_unregister (struct pci_ide *ide);#endif /* __TSM_H */
Signature · 1 line
2.49.0
Re: [PATCH v3 10/13] PCI/TSM: Report active IDE streams
Quoted 20 lines · On Thu, 15 May 2025 22:47:29 -0700 Dan Williams <[email protected]>…
Given that the platform TSM owns IDE Stream ID allocation, report the active streams via the TSM class device. Establish a symlink from the class device to the PCI endpoint device consuming the stream, named by the Stream ID.
Signed-off-by Dan Williams <[email protected]>--- Documentation/ABI/testing/sysfs-class-tsm | 10 ++++++++++ drivers/virt/coco/host/tsm-core.c | 17 +++++++++++++++++ include/linux/tsm.h | 4 ++++ 3 files changed, 31 insertions(+)Documentation/ABI/testing/sysfs-class-tsm +0
-0@@ -8,3 +8,13 @@ Description: signals when the PCI layer is able to support establishment of link encryption and other device-security features coordinated through the platform tsm.
Documentation/ABI/testing/sysfs-class-tsm Description: · line 20
Quoted 10 lines
Documentation/ABI/testing/sysfs-class-tsm +10
-0+ +What: /sys/class/tsm/tsm0/streamN:DDDD:BB:DD:F +Date: December, 2024 +Contact: [email protected] +Description: + (RO) When a host bridge has established a secure connection via + the platform TSM, symlink appears. The primary function of this + is have a system global review of TSM resource consumption + across host bridges. The link points to the endpoint PCI device + at domain:DDDD bus:BB device:DD function:F.
Do we need the name to link to include the sbdf? Maybe just streamN is enough. It's a little fiddly to get the spdf from where that goes, but not that challenging.
For user ls -lf /sys/class/tsm/tsm0/* should work for instance.
I don't care strongly about this. Maybe one for Bjorn.
Re: [PATCH v3 10/13] PCI/TSM: Report active IDE streams
Quoted 38 lines · Jonathan Cameron
Quoted 20 lines · On Thu, 15 May 2025 22:47:29 -0700 Dan Williams <[email protected]>…
Given that the platform TSM owns IDE Stream ID allocation, report the active streams via the TSM class device. Establish a symlink from the class device to the PCI endpoint device consuming the stream, named by the Stream ID.
Signed-off-by Dan Williams <[email protected]>--- Documentation/ABI/testing/sysfs-class-tsm | 10 ++++++++++ drivers/virt/coco/host/tsm-core.c | 17 +++++++++++++++++ include/linux/tsm.h | 4 ++++ 3 files changed, 31 insertions(+)Documentation/ABI/testing/sysfs-class-tsm +0
-0@@ -8,3 +8,13 @@ Description: signals when the PCI layer is able to support establishment of link encryption and other device-security features coordinated through the platform tsm.Documentation/ABI/testing/sysfs-class-tsm Description: · line 20
Quoted 10 lines
Documentation/ABI/testing/sysfs-class-tsm +10
-0+ +What: /sys/class/tsm/tsm0/streamN:DDDD:BB:DD:F +Date: December, 2024 +Contact: [email protected] +Description: + (RO) When a host bridge has established a secure connection via + the platform TSM, symlink appears. The primary function of this + is have a system global review of TSM resource consumption + across host bridges. The link points to the endpoint PCI device + at domain:DDDD bus:BB device:DD function:F.Do we need the name to link to include the sbdf? Maybe just streamN is enough. It's a little fiddly to get the spdf from where that goes, but not that challenging.
Quoted 1 line
For user ls -lf
/sys/class/tsm/tsm0/*should work for instance.
I had the target PCI device name in the link name before the rewrite to name the stream with the tuple of {host_bridge_stream_id, root_port_stream_id, endpoint_stream_id}. With that in place the stream name is disambiguated and the PCI device name can be dropped.
Quoted 1 line
I don't care strongly about this. Maybe one for Bjorn.
The shorter name is less complicated.
PATCH v3 11/13 samples/devsec: Add sample IDE establishment
In reply to Dan WilliamsExercise common setup and teardown flows for a sample platform TSM driver that implements the TSM 'connect' and 'disconnect' flows.
This is both a template for platform specific implementations and a simple integration test for the PCI core infrastructure + ABI.
--- samples/devsec/bus.c | 3 ++ samples/devsec/tsm.c | 77 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-)
samples/devsec/bus.c +3 -0
@@ -15,6 +15,7 @@#define NR_DEVSEC_BUSES 1#define NR_DEVSEC_ROOT_PORTS 1 +#define NR_PLATFORM_STREAMS 4#define NR_PORT_STREAMS 1#define NR_ADDR_ASSOC 1#define NR_DEVSEC_DEVS 1
@@ -662,6 +663,7 @@ static int __init devsec_bus_probe(struct platform_device *pdev) hb->dev.parent = dev; hb->sysdata = sd; hb->ops = &devsec_ops; +pci_ide_init_nr_streams (hb,NR_PLATFORM_STREAMS ); rc =pci_scan_root_bus_bridge (hb);if (rc)
@@ -704,5 +706,6 @@ static void __exit devsec_bus_exit(void) }module_exit (devsec_bus_exit); +MODULE_IMPORT_NS ("PCI_IDE" );MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("Device Security Sample Infrastructure: TDISP Device Emulation" );
samples/devsec/tsm.c +76 -1
99 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v3 11/13] samples/devsec: Add sample IDE establishment
Quoted 6 lines · On Thu, 15 May 2025 22:47:30 -0700 Dan Williams <[email protected]>…
Exercise common setup and teardown flows for a sample platform TSM driver that implements the TSM 'connect' and 'disconnect' flows.
This is both a template for platform specific implementations and a simple integration test for the PCI core infrastructure + ABI.
Quoted 6 lines
Cc Bjorn Helgaas <[email protected]>Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Cc Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>
Trivial comment inline.
Quoted 8 lines
index
7a8d33dc54c6..aa852ac1c16d100644samples/devsec/tsm.c +0
-0/* * Reference consumer for a TSM driver "connect" operation callback. The * low-level TSM driver understands details about the platform the PCI
samples/devsec/tsm.c static void devsec_tsm_pci_remove(struct pci_tsm *tsm) · line 87
Quoted 10 lines
samples/devsec/tsm.c +6
-1@@ -74,11 +79,81 @@ static void devsec_tsm_pci_remove(struct pci_tsm *tsm)*/ static int devsec_tsm_connect (struct pci_dev *pdev) { -return -ENXIO ; +struct pci_dev *rp =pcie_find_root_port (pdev); +struct pci_ide *ide; +int rc, stream_id; + + stream_id = +find_first_zero_bit (devsec_stream_ids,NR_TSM_STREAMS );
Ugly and it's under 80 chars on one line.
Quoted 5 lines
samples/devsec/tsm.c +2
-0+if (stream_id ==NR_TSM_STREAMS ) +return -EBUSY ;static void devsec_tsm_disconnect (struct pci_dev *pdev)
samples/devsec/tsm.c static void devsec_tsm_pci_remove(struct pci_tsm *tsm) · line 145
Quoted 10 lines
samples/devsec/tsm.c +9
-0{ +struct pci_dev *rp =pcie_find_root_port (pdev); +struct pci_ide *ide; +int i; + +for_each_set_bit (i, devsec_stream_ids,NR_TSM_STREAMS ) +if (devsec_streams[i]->pdev == pdev) +break ; + +if (i >=NR_TSM_STREAMS )
== NR_TSM_STREAMS
not that it really matters but it can never be greater.
Quoted 14 lines
samples/devsec/tsm.c +11
-0+return ; + + ide = devsec_streams[i]; + devsec_streams[i] =NULL ; +pci_ide_stream_disable (pdev, ide); +tsm_ide_stream_unregister (ide); +pci_ide_stream_teardown (rp, ide); +pci_ide_stream_teardown (pdev, ide); +pci_ide_stream_unregister (ide); +pci_ide_stream_free (ide); +clear_bit (i, devsec_stream_ids); }static const struct pci_tsm_ops devsec_pci_ops = {
Re: [PATCH v3 11/13] samples/devsec: Add sample IDE establishment
Quoted 21 lines · Jonathan Cameron
Quoted 6 lines · On Thu, 15 May 2025 22:47:30 -0700 Dan Williams <[email protected]>…
Exercise common setup and teardown flows for a sample platform TSM driver that implements the TSM 'connect' and 'disconnect' flows.
This is both a template for platform specific implementations and a simple integration test for the PCI core infrastructure + ABI.
Quoted 6 lines
Cc Bjorn Helgaas <[email protected]>Cc Lukas Wunner <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Alexey Kardashevskiy <[email protected]>Cc Xu Yilun <[email protected]>Signed-off-by Dan Williams <[email protected]>Trivial comment inline.
Quoted 3 lines
index
7a8d33dc54c6..aa852ac1c16d100644samples/devsec/tsm.c +0
-0
Quoted 16 lines
Quoted 4 lines
/*
* Reference consumer for a TSM driver "connect" operation callback. The
* low-level TSM driver understands details about the platform the PCIsamples/devsec/tsm.c static void devsec_tsm_pci_remove(struct pci_tsm *tsm) · line 87
Quoted 10 lines
samples/devsec/tsm.c +6
-1@@ -74,11 +79,81 @@ static void devsec_tsm_pci_remove(struct pci_tsm *tsm)*/ static int devsec_tsm_connect (struct pci_dev *pdev) { -return -ENXIO ; +struct pci_dev *rp =pcie_find_root_port (pdev); +struct pci_ide *ide; +int rc, stream_id; + + stream_id = +find_first_zero_bit (devsec_stream_ids,NR_TSM_STREAMS );Ugly and it's under 80 chars on one line.
Just a missed clang-format, fixed.
Quoted 6 lines
Quoted 6 lines
samples/devsec/tsm.c +2
-0+if (stream_id ==NR_TSM_STREAMS ) +return -EBUSY ;
Quoted 13 lines
samples/devsec/tsm.c static void devsec_tsm_pci_remove(struct pci_tsm *tsm) · line 145
Quoted 11 lines
samples/devsec/tsm.c +9
-0static void devsec_tsm_disconnect (struct pci_dev *pdev) { +struct pci_dev *rp =pcie_find_root_port (pdev); +struct pci_ide *ide; +int i; + +for_each_set_bit (i, devsec_stream_ids,NR_TSM_STREAMS ) +if (devsec_streams[i]->pdev == pdev) +break ; + +if (i >=NR_TSM_STREAMS )==
NR_TSM_STREAMS
not that it really matters but it can never be greater.
I does not matter in practice but if the valid values are "< size" then the invalid values are ">= size".
PATCH v3 12/13 PCI/TSM: support TDI related operations for host TSM driver
In reply to Dan WilliamsFrom: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to CONFIG_LOCKED TDISP state by LOCK_INTERFACE_REQUEST TDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
pci_tsm_guest_req() supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.
A problem to solve here is the TDI operation lock. The TDI operations involve TDISP message communication with devices, which is transferred via PF0's DOE. When multiple VFs or MFDs are involved at the same time, these messages are not intended to interleave with each other. So serialize all TSM operations of one slot by holding the DSM device (PF0) pci_tsm.lock.
Add a struct pci_tdi to represent the TDI context, which is common to all PFs/VFs/MFDs so embedded it in struct pci_tsm. The appearing of the tsm::tdi means the device is in BOUND state and vice versa. So no extra enum pci_tsm_state value is added for bind. That also means the access to tsm::tdi must with the DEM device (PF0) TSM lock.
--- drivers/pci/tsm.c | 227 +++++++++++++++++++++++++++++++++++++++- include/linux/pci-tsm.h | 64 +++++++++++ 2 files changed, 290 insertions(+), 1 deletion(-)
drivers/pci/tsm.c +226 -1
@@ -50,10 +50,65 @@ static struct mutex *tsm_ops_lock(struct pci_tsm_pf0 *tsm) } DEFINE_FREE(tsm_ops_unlock, struct mutex *, if (_T) mutex_unlock(_T)) +static int __pci_tsm_unbind (struct pci_dev *pdev); +static void pci_tsm_unbind_all_vfs (struct pci_dev *pdev) +{ +struct pci_dev *virtfn; + +for (int i =0 ; i <pci_num_vf (pdev); i++) { + virtfn =pci_get_domain_bus_and_slot (pci_domain_nr (pdev->bus), +pci_iov_virtfn_bus (pdev, i), +pci_iov_virtfn_devfn (pdev, i)); +if (virtfn) { +__pci_tsm_unbind (virtfn); +pci_dev_put (virtfn); + } + } +} + +static void pci_tsm_unbind_all_mfds (struct pci_dev *pdev) +{ +struct pci_dev *phyfn; + +for (int i =0 ; i <8 ; i++) { + phyfn =pci_get_slot (pdev->bus,PCI_DEVFN (PCI_SLOT (pdev->devfn), i)); +if (phyfn) { +__pci_tsm_unbind (phyfn); +pci_dev_put (phyfn); + } + } +} + +static int unbind_downstream (struct pci_dev *pdev,void *uport_subordinate) +{ +if (pdev->bus->parent != uport_subordinate) +return 0 ; + +if (pdev->tsm && pdev->tsm->type ==PCI_TSM_DOWNSTREAM ) +__pci_tsm_unbind (pdev); + +return 0 ; +} + +static void pci_tsm_unbind_all_downstream (struct pci_dev *pdev) +{ +if (pci_pcie_type (pdev) !=PCI_EXP_TYPE_UPSTREAM ) +return ; + +if (!pdev->tsm) +return ; + +pci_walk_bus (pdev->subordinate, unbind_downstream, pdev->subordinate); +} +static int pci_tsm_disconnect (struct pci_dev *pdev) {struct pci_tsm_pf0 *tsm =to_pci_tsm_pf0 (pdev->tsm); +pci_tsm_unbind_all_downstream (pdev); +pci_tsm_unbind_all_vfs (pdev); +pci_tsm_unbind_all_mfds (pdev); + struct mutex *lock __free(tsm_ops_unlock) = tsm_ops_lock(tsm);if (!lock)return -EINTR ;
@@ -392,8 +447,12 @@ static void __pci_tsm_destroy(struct pci_dev *pdev)lockdep_assert_held_write (&pci_tsm_rwsem); -if (is_pci_tsm_pf0 (pdev)) +if (is_pci_tsm_pf0 (pdev)) {pci_tsm_pf0_destroy (pdev); + }else { +__pci_tsm_unbind (pdev); + pdev->tsm =NULL ; + } tsm_ops->remove (pci_tsm); }
@@ -435,3 +494,169 @@ int pci_tsm_doe_transfer(struct pci_dev *pdev, enum pci_doe_proto type, resp, resp_sz); }EXPORT_SYMBOL_GPL (pci_tsm_doe_transfer); + +/* lookup the 'DSM' pf0 for @pdev */ +static struct pci_dev *tsm_pf0_get (struct pci_dev *pdev) +{ +struct pci_dev *uport_pf0; + + struct pci_dev *pf0 __free(pci_dev_put) = pf0_dev_get(pdev); +if (!pf0) +return NULL ; + +/* Check that @pf0 was not initialized as PCI_TSM_DOWNSTREAM */ +if (pf0->tsm && pf0->tsm->type ==PCI_TSM_PF0 ) +return no_free_ptr (pf0); + +/* +* For cases where a switch may be hosting TDISP services on +* behalf of downstream devices, check the first usptream port +* relative to this endpoint. +*/ +if (!pdev->dev.parent || !pdev->dev.parent->parent) +return NULL ; + + uport_pf0 =to_pci_dev (pdev->dev.parent->parent); +if (!uport_pf0->tsm) +return NULL ; +return pci_dev_get (uport_pf0); +} + +/* Only implement non-interruptible lock for now */ +static struct mutex *tdi_ops_lock (struct pci_dev *pf0_dev) +{ +struct pci_tsm_pf0 *pf0_tsm; + +lockdep_assert_held (&pci_tsm_rwsem); + +if (!pf0_dev->tsm) +return ERR_PTR (-EINVAL ); + + pf0_tsm =to_pci_tsm_pf0 (pf0_dev->tsm); +mutex_lock (&pf0_tsm->lock); + +if (pf0_tsm->state <PCI_TSM_CONNECT ) { +mutex_unlock (&pf0_tsm->lock); +return ERR_PTR (-EINVAL ); + } + +return &pf0_tsm->lock; +} +DEFINE_FREE(tdi_ops_unlock, struct mutex *, if (!IS_ERR(_T)) mutex_unlock(_T)) + +int pci_tsm_bind (struct pci_dev *pdev,struct kvm *kvm,u64 tdi_id) +{ +struct pci_tdi *tdi; + +if (!kvm) +return -EINVAL ; + + struct rw_semaphore *lock __free(tsm_read_unlock) = tsm_read_lock(); +if (!lock) +return -EINTR ; + +if (!pdev->tsm) +return -EINVAL ; + + struct pci_dev *pf0_dev __free(pci_dev_put) = tsm_pf0_get(pdev); +if (!pf0_dev) +return -EINVAL ; + + struct mutex *ops_lock __free(tdi_ops_unlock) = tdi_ops_lock(pf0_dev); +if (IS_ERR (ops_lock)) +return PTR_ERR (ops_lock); + +if (pdev->tsm->tdi) { +if (pdev->tsm->tdi->kvm == kvm) +return 0 ; +else +return -EBUSY ; + } + + tdi = tsm_ops->bind (pdev, pf0_dev, kvm, tdi_id); +if (!tdi) +return -ENXIO ; + + pdev->tsm->tdi = tdi; + +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_tsm_bind); + +static int __pci_tsm_unbind (struct pci_dev *pdev) +{ +struct pci_tdi *tdi; + +lockdep_assert_held (&pci_tsm_rwsem); + +if (!pdev->tsm) +return -EINVAL ; + + struct pci_dev *pf0_dev __free(pci_dev_put) = tsm_pf0_get(pdev); +if (!pf0_dev) +return -EINVAL ; + + struct mutex *lock __free(tdi_ops_unlock) = tdi_ops_lock(pf0_dev); +if (IS_ERR (lock)) +return PTR_ERR (lock); + + tdi = pdev->tsm->tdi; +if (!tdi) +return 0 ; + + tsm_ops->unbind (tdi); + pdev->tsm->tdi =NULL ; + +return 0 ; +} + +int pci_tsm_unbind (struct pci_dev *pdev) +{ + struct rw_semaphore *lock __free(tsm_read_unlock) = tsm_read_lock(); +if (!lock) +return -EINTR ; + +return __pci_tsm_unbind (pdev); +} +EXPORT_SYMBOL_GPL (pci_tsm_unbind); + +/** +* pci_tsm_guest_req - VFIO/IOMMUFD helper to handle guest requests +* @pdev: @pdev representing a bound tdi +* @info: envelope for the request +* +* Expected flow is guest low-level TSM driver initiates a guest request +* like "transition TDISP state to RUN", "fetch report" via a +* technology specific guest-host-interface and KVM exit reason. KVM +* posts to userspace (e.g. QEMU) that holds the host-to-guest RID +* mapping. +*/ +int pci_tsm_guest_req (struct pci_dev *pdev,struct pci_tsm_guest_req_info *info) +{ +struct pci_tdi *tdi; +int rc; + +lockdep_assert_held_read (&pci_tsm_rwsem); + +if (!pdev->tsm) +return -ENODEV ; + + struct pci_dev *pf0_dev __free(pci_dev_put) = tsm_pf0_get(pdev); +if (!pf0_dev) +return -EINVAL ; + + struct mutex *lock __free(tdi_ops_unlock) = tdi_ops_lock(pf0_dev); +if (IS_ERR (lock)) +return -ENODEV ; + + tdi = pdev->tsm->tdi; +if (!tdi) +return -ENODEV ; + + rc = tsm_ops->guest_req (pdev, info); +if (rc) +return -EIO ; + +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_tsm_guest_req);
include/linux/pci-tsm.h +64 -0
115 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 13 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
Quoted 4 lines
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.
To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Thanks,
Yilun
Aneesh Kumar K.V
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 14 lines · Xu Yilun
Quoted 13 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
Quoted 8 lines
Quoted 4 lines
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
-aneesh
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 21 lines · Aneesh Kumar K.V
Quoted 19 lines · Xu Yilun
Quoted 17 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.
Quoted 6 lines
Quoted 3 lines
To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Thanks,
Yilun
Quoted 2 lines
-aneesh
Aneesh Kumar K.V
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 26 lines · Xu Yilun
Quoted 25 lines · Aneesh Kumar K.V
Quoted 22 lines · Xu Yilun
Quoted 13 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.Quoted 4 lines
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Quoted 7 lines
Quoted 2 lines
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Since the bind/unbind operations are PCI-specific callbacks, and iommufd doesn’t seem to have a PCI-specific abstraction layer (unlike vfio, which uses vfio_pci.c), I’m wondering how iommufd intends to support PCI-specific TSM binding. Will there be a new interface for this, or is it expected to hook into something existing?
-aneesh
Alexey Kardashevskiy
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 31 lines · Aneesh Kumar K.V
Quoted 29 lines · Xu Yilun
Quoted 27 lines · Aneesh Kumar K.V
Quoted 14 lines · Xu Yilun
Quoted 13 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.Quoted 8 lines
Quoted 4 lines
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
Quoted 6 lines
Quoted 4 lines
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Since the bind/unbind operations are PCI-specific callbacks, and iommufd
Not really, it is PCI-specific in TSM (for DOE) but since IOMMUFD is not doing any of that, it can work with struct device (not pci_dev). Thanks,
Quoted 6 lines
doesn’t seem to have a PCI-specific abstraction layer (unlike vfio, which uses
vfio_pci.c), I’m wondering how iommufd intends to support PCI-specific TSM binding. Will there be a new interface for this, or is it expected to hook into something existing?-aneesh
Signature · 1 line
Alexey
Aneesh Kumar K.V
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 32 lines · Alexey Kardashevskiy
Quoted 31 lines · Aneesh Kumar K.V
Quoted 29 lines · Xu Yilun
Quoted 27 lines · Aneesh Kumar K.V
Quoted 14 lines · Xu Yilun
Quoted 13 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.Quoted 8 lines
Quoted 4 lines
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Since the bind/unbind operations are PCI-specific callbacks, and iommufd
Not really, it is PCI-specific in TSM (for DOE) but since IOMMUFD is not doing any of that, it can work with struct device (not
pci_dev). Thanks,
Ok, something like this? and iommufd will call tsm_bind()?
int tsm_bind(struct device *dev, struct kvm *kvm, u64 tdi_id)
{
if (!dev_is_pci(dev))
return -EINVAL;
return pci_tsm_bind(to_pci_dev(dev), kvm, tdi_id);
}EXPORT_SYMBOL_GPL(tsm_bind);
int tsm_unbind(struct device *dev)
{
if (!dev_is_pci(dev))
return -EINVAL;
return pci_tsm_unbind(to_pci_dev(dev));
}EXPORT_SYMBOL_GPL(tsm_unbind);
-aneesh
Alexey Kardashevskiy
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 39 lines · Aneesh Kumar K.V
Quoted 37 lines · Alexey Kardashevskiy
Quoted 36 lines · Aneesh Kumar K.V
Quoted 33 lines · Xu Yilun
Quoted 21 lines · Aneesh Kumar K.V
Quoted 19 lines · Xu Yilun
Quoted 17 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.Quoted 6 lines
Quoted 3 lines
To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Since the bind/unbind operations are PCI-specific callbacks, and iommufd
Not really, it is PCI-specific in TSM (for DOE) but since IOMMUFD is not doing any of that, it can work with struct device (not
pci_dev). Thanks,Ok, something like this? and iommufd will call
tsm_bind()?
yeah, I guess, there is a couple of places like this
git grep pci_dev drivers/iommu/iommufd/
drivers/iommu/iommufd/device.c: struct pci_dev *pdev = to_pci_dev(idev->dev); drivers/iommu/iommufd/eventq.c: struct pci_dev *pdev = to_pci_dev(dev);
Although I do not see any compelling reason to have pci_dev in the TSM API, struct device should just work and not spill any PCI details to IOMMUFD but whatever... Thanks,
Quoted 21 lines
int
tsm_bind(struct device *dev, struct kvm *kvm, u64 tdi_id){ if (!dev_is_pci(dev)) return -EINVAL; return pci_tsm_bind(to_pci_dev(dev), kvm, tdi_id); }
EXPORT_SYMBOL_GPL(tsm_bind);int
tsm_unbind(struct device *dev){ if (!dev_is_pci(dev)) return -EINVAL; return pci_tsm_unbind(to_pci_dev(dev)); }
EXPORT_SYMBOL_GPL(tsm_unbind);-aneesh
Signature · 1 line
Alexey
Aneesh Kumar K.V
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 47 lines · Alexey Kardashevskiy
Quoted 39 lines · Aneesh Kumar K.V
Quoted 37 lines · Alexey Kardashevskiy
Quoted 36 lines · Aneesh Kumar K.V
Quoted 33 lines · Xu Yilun
Quoted 21 lines · Aneesh Kumar K.V
Quoted 19 lines · Xu Yilun
Quoted 17 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.Quoted 6 lines
Quoted 3 lines
To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Since the bind/unbind operations are PCI-specific callbacks, and iommufd
Not really, it is PCI-specific in TSM (for DOE) but since IOMMUFD is not doing any of that, it can work with struct device (not
pci_dev). Thanks,Ok, something like this? and iommufd will call
tsm_bind()?
Quoted 8 lines
yeah, I guess, there is a couple of places like this
git grep
pci_devdrivers/iommu/iommufd/drivers/iommu/iommufd/device.c: struct pci_dev *pdev = to_pci_dev(idev->dev); drivers/iommu/iommufd/eventq.c: struct pci_dev *pdev = to_pci_dev(dev);Although I do not see any compelling reason to have
pci_devin the TSM API, struct device should just work and not spill any PCI details to IOMMUFD but whatever... Thanks,
Getting the kvm reference is tricky here. Also the locking while updating vdevice->tsm_bound needs some solution. Here is what I am improving. Are you also planning something similar?
drivers/iommu/iommufd/device.c | 4 +- drivers/iommu/iommufd/iommufd_private.h | 5 ++ drivers/iommu/iommufd/main.c | 5 ++ drivers/iommu/iommufd/viommu.c | 62 +++++++++++++++++++++++++ drivers/vfio/iommufd.c | 2 +- include/linux/iommufd.h | 3 +- include/uapi/linux/iommufd.h | 16 +++++++ 7 files changed, 94 insertions(+), 3 deletions(-)
drivers/iommu/iommufd/device.c +3 -1
@@ -165,7 +165,7 @@ void iommufd_device_destroy(struct iommufd_object *obj)* The caller must undo this with iommufd_device_unbind() */ struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, -struct device *dev,u32 *id) +struct device *dev,struct kvm *kvm,u32 *id) {struct iommufd_device *idev;struct iommufd_group *igroup;
@@ -221,6 +221,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,refcount_inc (&idev->obj.users);/* igroup refcount moves into iommufd_device */ idev->igroup = igroup; + idev->kvm = kvm;mutex_init (&idev->iopf_lock);/*
@@ -1009,6 +1010,7 @@ void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid)if (!hwpt)return ;iommufd_hw_pagetable_put (idev->ictx, hwpt); + idev->kvm =NULL ;refcount_dec (&idev->obj.users); }EXPORT_SYMBOL_NS_GPL (iommufd_device_detach,"IOMMUFD" );
drivers/iommu/iommufd/iommufd_private.h +5 -0
@@ -424,6 +424,7 @@ struct iommufd_device {struct list_head group_item;/* always the physical device */ struct device *dev; +struct kvm *kvm;bool enforce_cache_coherency;/* protect iopf_enabled counter */ struct mutex iopf_lock;
@@ -606,13 +607,17 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);void iommufd_viommu_destroy (struct iommufd_object *obj);int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_vdevice_destroy (struct iommufd_object *obj); +int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd); +int iommufd_vdevice_tsm_unbind_ioctl (struct iommufd_ucmd *ucmd);struct iommufd_vdevice {struct iommufd_object obj;struct iommufd_ctx *ictx;struct iommufd_viommu *viommu;struct device *dev; +struct kvm *kvm;u64 id;/* per-vIOMMU virtual ID */ +bool tsm_bound; };#ifdef CONFIG_IOMMUFD_TEST
drivers/iommu/iommufd/main.c +5 -0
@@ -320,6 +320,7 @@ union ucmd_buffer {struct iommu_veventq_alloc veventq;struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu; +struct iommu_vdevice_id vdev_id;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif
@@ -379,6 +380,10 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { __reserved),IOCTL_OP (IOMMU_VIOMMU_ALLOC ,iommufd_viommu_alloc_ioctl ,struct iommu_viommu_alloc ,out_viommu_id ), +IOCTL_OP (IOMMU_VDEVICE_TSM_BIND ,iommufd_vdevice_tsm_bind_ioctl , +struct iommu_vdevice_id ,vdevice_id ), +IOCTL_OP (IOMMU_VDEVICE_TSM_UNBIND ,iommufd_vdevice_tsm_unbind_ioctl , +struct iommu_vdevice_id ,vdevice_id ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif
drivers/iommu/iommufd/viommu.c +62 -0
83 lines not on this page · raw message
drivers/vfio/iommufd.c +1 -1
@@ -115,7 +115,7 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev, {struct iommufd_device *idev; - idev =iommufd_device_bind (ictx, vdev->dev, out_device_id); + idev =iommufd_device_bind (ictx, vdev->dev, vdev->kvm, out_device_id);if (IS_ERR (idev))return PTR_ERR (idev); vdev->iommufd_device = idev;
include/linux/iommufd.h +2 -1
@@ -51,8 +51,9 @@ struct iommufd_object {unsigned int id; }; +struct kvm ;struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, -struct device *dev,u32 *id); +struct device *dev,struct kvm *kvm,u32 *id);void iommufd_device_unbind (struct iommufd_device *idev); int iommufd_device_attach(struct iommufd_device *idev, ioasid_t pasid,
include/uapi/linux/iommufd.h +16 -0
@@ -56,6 +56,8 @@ enum {IOMMUFD_CMD_VDEVICE_ALLOC =0x91 ,IOMMUFD_CMD_IOAS_CHANGE_PROCESS =0x92 ,IOMMUFD_CMD_VEVENTQ_ALLOC =0x93 , +IOMMUFD_CMD_VDEVICE_TSM_BIND =0x94 , + IOMMUFD_CMD_VDEVICE_TSM_UNBIND = 0x95, };/**
@@ -1038,6 +1040,20 @@ enum iommu_veventq_flag { IOMMU_VEVENTQ_FLAG_LOST_EVENTS = (1U << 0), }; +/** +* struct iommu_vdevice_tsm_unbind - ioctl(IOMMU_VDEVICE_TSM_UNBIND) +* @size: sizeof(struct iommu_vdevice_tsm_unbind) +* @vdevice_id: +* +*/ +struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND) + +/** * struct iommufd_vevent_header - Virtual Event Header for a vEVENTQ Status * @flags: Combination of enum iommu_veventq_flag
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 10 lines · Aneesh Kumar K.V
Quoted 8 lines
yeah, I guess, there is a couple of places like this
git grep
pci_devdrivers/iommu/iommufd/drivers/iommu/iommufd/device.c: struct pci_dev *pdev = to_pci_dev(idev->dev); drivers/iommu/iommufd/eventq.c: struct pci_dev *pdev = to_pci_dev(dev);Although I do not see any compelling reason to have
pci_devin the TSM API, struct device should just work and not spill any PCI details to IOMMUFD but whatever... Thanks,Getting the kvm reference is tricky here.
The KVM will come from the viommu object, passed in by userspace that is the plan at least.. If you are not presenting a viommu to the guest then I imagine we would still have some kind of NOP viommu object..
We need an association between the viommu and vdevice to tell the TSM world what it is when we tell the TSM to create the vPCI function..
There is a missing ioctl in this sequence, you have to register the vdev with the viommu to create a vPCI function, and that may trigger a TSM call too.
The registration should link the vdev to the viommu and then you can get the viommu's kvm for a later bind.
Quoted 11 lines
drivers/iommu/iommufd/viommu.c +11
-0+int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_vdevice_id *cmd = ucmd->cmd; +struct iommufd_vdevice *vdev; +int rc =0 ; + + vdev =container_of (iommufd_get_object (ucmd->ictx, cmd->vdevice_id, +IOMMUFD_OBJ_VDEVICE ), + struct iommufd_vdevice, obj); +if (IS_ERR (vdev)) +return PTR_ERR (vdev);
drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 187
Quoted 10 lines
drivers/iommu/iommufd/viommu.c +10
-0+ + rc =tsm_bind (vdev->dev, vdev->kvm, vdev->id); +if (rc) { + rc = -ENODEV ; + goto out_put_vdev; + } + +/* locking? */ + vdev->tsm_bound = true; +refcount_inc (&vdev->obj.users);
This refcount isn't going to work, it will make an error close() crash..
You need to auto-unbind on destruction I think.
Jason
Aneesh Kumar K.V
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 8 lines · Jason Gunthorpe
Quoted 7 lines · Aneesh Kumar K.V
Quoted 7 lines
yeah, I guess, there is a couple of places like this
git grep
pci_devdrivers/iommu/iommufd/drivers/iommu/iommufd/device.c: struct pci_dev *pdev = to_pci_dev(idev->dev); drivers/iommu/iommufd/eventq.c: struct pci_dev *pdev = to_pci_dev(dev);
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Although I do not see any compelling reason to have
pci_devin the TSM API, struct device should just work and not spill any PCI details to IOMMUFD but whatever... Thanks,Getting the kvm reference is tricky here.
The KVM will come from the viommu object, passed in by userspace that is the plan at least.. If you are not presenting a viommu to the guest then I imagine we would still have some kind of NOP viommu object..
I assume you are not suggesting using IOMMU_VIOMMU_ALLOC? That would break the ABI, which we need to maintain.
Instead, my approach uses VFIO_DEVICE_BIND_IOMMUFD to associate the KVM context. The vfio device file descriptor had already been linked to the KVM instance via KVM_DEV_VFIO_FILE_ADD.
Through VFIO_DEVICE_BIND_IOMMUFD, we inherit the necessary KVM details and pass them along to iommufd_device, and subsequently to iommufd_vdevice, using IOMMU_VDEVICE_ALLOC.
Quoted 36 lines
We need an association between the viommu and vdevice to tell the TSM world what it is when we tell the TSM to create the vPCI function..
There is a missing ioctl in this sequence, you have to register the vdev with the viommu to create a vPCI function, and that may trigger a TSM call too.
The registration should link the vdev to the viommu and then you can get the viommu's kvm for a later bind.
Quoted 11 lines
drivers/iommu/iommufd/viommu.c +11
-0+int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_vdevice_id *cmd = ucmd->cmd; +struct iommufd_vdevice *vdev; +int rc =0 ; + + vdev =container_of (iommufd_get_object (ucmd->ictx, cmd->vdevice_id, +IOMMUFD_OBJ_VDEVICE ), + struct iommufd_vdevice, obj); +if (IS_ERR (vdev)) +return PTR_ERR (vdev);drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 187
Quoted 10 lines
drivers/iommu/iommufd/viommu.c +10
-0+ + rc =tsm_bind (vdev->dev, vdev->kvm, vdev->id); +if (rc) { + rc = -ENODEV ; + goto out_put_vdev; + } + +/* locking? */ + vdev->tsm_bound = true; +refcount_inc (&vdev->obj.users);This refcount isn't going to work, it will make an error
close()crash..
Quoted 1 line
You need to auto-unbind on destruction I think.
Can you elaborate on that? if vdevice is tsm_bound, iommufd_vdevice_destroy() do call tsm_unbind in the changes I shared.
-aneesh
Jason Gunthorpe
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 12 lines · Aneesh Kumar K.V
Quoted 10 lines · Jason Gunthorpe
Quoted 9 lines · Aneesh Kumar K.V
Quoted 8 lines
yeah, I guess, there is a couple of places like this
git grep
pci_devdrivers/iommu/iommufd/drivers/iommu/iommufd/device.c: struct pci_dev *pdev = to_pci_dev(idev->dev); drivers/iommu/iommufd/eventq.c: struct pci_dev *pdev = to_pci_dev(dev);Although I do not see any compelling reason to have
pci_devin the TSM API, struct device should just work and not spill any PCI details to IOMMUFD but whatever... Thanks,
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Getting the kvm reference is tricky here.
The KVM will come from the viommu object, passed in by userspace that is the plan at least.. If you are not presenting a viommu to the guest then I imagine we would still have some kind of NOP viommu object..
I assume you are not suggesting using
IOMMU_VIOMMU_ALLOC? That would break the ABI, which we need to maintain.
Yes I am, what ABI are you talking about? CC is all new.
Quoted 7 lines
Instead, my approach uses
VFIO_DEVICE_BIND_IOMMUFDto associate the KVM context. The vfio device file descriptor had already been linked to the KVM instance viaKVM_DEV_VFIO_FILE_ADD.Through
VFIO_DEVICE_BIND_IOMMUFD, we inherit the necessary KVM details and pass them along toiommufd_device, and subsequently toiommufd_vdevice, usingIOMMU_VDEVICE_ALLOC.
It is not OK, we want this in the viommu not the device for a bunch of other reasons. I don't want two copies of the KVM running around inside iommfd..
Quoted 12 lines
Quoted 12 lines
drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 187
Quoted 8 lines
drivers/iommu/iommufd/viommu.c +8
-0+if (rc) { + rc = -ENODEV ; + goto out_put_vdev; + } + +/* locking? */ + vdev->tsm_bound = true; +refcount_inc (&vdev->obj.users);This refcount isn't going to work, it will make an error
close()crash..
Quoted 4 lines
Quoted 1 line
You need to auto-unbind on destruction I think.
Can you elaborate on that? if vdevice is
tsm_bound,iommufd_vdevice_destroy()do calltsm_unbindin the changes I shared.
You are driving it from the vfio side? Then you don't need the refcount at all here because the vfio facing APIs already take one.
Jason
Aneesh Kumar K.V
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 28 lines · Jason Gunthorpe
Quoted 12 lines · Aneesh Kumar K.V
Quoted 10 lines · Jason Gunthorpe
Quoted 9 lines · Aneesh Kumar K.V
Quoted 8 lines
yeah, I guess, there is a couple of places like this
git grep
pci_devdrivers/iommu/iommufd/drivers/iommu/iommufd/device.c: struct pci_dev *pdev = to_pci_dev(idev->dev); drivers/iommu/iommufd/eventq.c: struct pci_dev *pdev = to_pci_dev(dev);Although I do not see any compelling reason to have
pci_devin the TSM API, struct device should just work and not spill any PCI details to IOMMUFD but whatever... Thanks,Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Getting the kvm reference is tricky here.
The KVM will come from the viommu object, passed in by userspace that is the plan at least.. If you are not presenting a viommu to the guest then I imagine we would still have some kind of NOP viommu object..
I assume you are not suggesting using
IOMMU_VIOMMU_ALLOC? That would break the ABI, which we need to maintain.Yes I am, what ABI are you talking about? CC is all new.
Quoted 4 lines
Instead, my approach uses
VFIO_DEVICE_BIND_IOMMUFDto associate the KVM context. The vfio device file descriptor had already been linked to the KVM instance viaKVM_DEV_VFIO_FILE_ADD.
Quoted 8 lines
Quoted 3 lines
Through
VFIO_DEVICE_BIND_IOMMUFD, we inherit the necessary KVM details and pass them along toiommufd_device, and subsequently toiommufd_vdevice, usingIOMMU_VDEVICE_ALLOC.It is not OK, we want this in the viommu not the device for a bunch of other reasons. I don't want two copies of the KVM running around inside iommfd..
Ok, I updated the changes as below.
5 files changed, 161 insertions(+), 2 deletions(-) drivers/iommu/iommufd/iommufd_private.h | 3 + drivers/iommu/iommufd/main.c | 5 ++ drivers/iommu/iommufd/viommu.c | 134 +++++++++++++++++++++++++++++++- include/linux/iommufd.h | 5 +- include/uapi/linux/iommufd.h | 16 ++++
modified drivers/iommu/iommufd/iommufd_private.h
@@ -606,6 +606,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);
void iommufd_viommu_destroy(struct iommufd_object *obj);
int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd);
void iommufd_vdevice_destroy(struct iommufd_object *obj);
+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd); +int iommufd_vdevice_tsm_unbind_ioctl(struct iommufd_ucmd *ucmd);
struct iommufd_vdevice {
struct iommufd_object obj;
@@ -613,6 +615,7 @@ struct iommufd_vdevice {
struct iommufd_viommu *viommu;
struct device *dev;
u64 id; /* per-vIOMMU virtual ID */
+ bool tsm_bound;
};
#ifdef CONFIG_IOMMUFD_TEST
modified drivers/iommu/iommufd/main.c
@@ -320,6 +320,7 @@ union ucmd_buffer {
struct iommu_veventq_alloc veventq;
struct iommu_vfio_ioas vfio_ioas;
struct iommu_viommu_alloc viommu;
+ struct iommu_vdevice_id vdev_id;
#ifdef CONFIG_IOMMUFD_TEST
struct iommu_test_cmd test;
#endif
@@ -379,6 +380,10 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
__reserved),
IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
struct iommu_viommu_alloc, out_viommu_id),
+ IOCTL_OP(IOMMU_VDEVICE_TSM_BIND, iommufd_vdevice_tsm_bind_ioctl,
+ struct iommu_vdevice_id, vdevice_id),
+ IOCTL_OP(IOMMU_VDEVICE_TSM_UNBIND, iommufd_vdevice_tsm_unbind_ioctl,
+ struct iommu_vdevice_id, vdevice_id),
#ifdef CONFIG_IOMMUFD_TEST
IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
#endif
modified drivers/iommu/iommufd/viommu.c
@@ -2,6 +2,57 @@
/* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
*/
#include "iommufd_private.h"
+#include "linux/tsm.h"
+
+#if IS_ENABLED(CONFIG_KVM)
+#include <linux/kvm_host.h>
+
+static void viommu_get_kvm_safe(struct iommufd_viommu *viommu, struct kvm *kvm) +{
+ void (*put_fn)(struct kvm *kvm);
+ bool (*get_fn)(struct kvm *kvm);
+ bool ret;
+
+ if (!kvm)
+ return;
+
+ put_fn = symbol_get(kvm_put_kvm);
+ if (WARN_ON(!put_fn))
+ return;
+
+ get_fn = symbol_get(kvm_get_kvm_safe);
+ if (WARN_ON(!get_fn)) {
+ symbol_put(kvm_put_kvm);
+ return;
+ }
+
+ ret = get_fn(kvm);
+ symbol_put(kvm_get_kvm_safe);
+ if (!ret) {
+ symbol_put(kvm_put_kvm);
+ return;
+ }
+
+ viommu->put_kvm = put_fn;
+ viommu->kvm = kvm;
+}
+
+static void viommu_put_kvm(struct iommufd_viommu *viommu)
+{
+ if (!viommu->kvm)
+ return;
+
+ if (WARN_ON(!viommu->put_kvm))
+ goto clear;
+
+ viommu->put_kvm(viommu->kvm);
+ viommu->put_kvm = NULL;
+ symbol_put(kvm_put_kvm);
+
+clear:
+ viommu->kvm = NULL;
+}
+#endif
void iommufd_viommu_destroy(struct iommufd_object *obj)
{
@@ -12,6 +63,8 @@ void iommufd_viommu_destroy(struct iommufd_object *obj)
viommu->ops->destroy(viommu);
refcount_dec(&viommu->hwpt->common.obj.users); xa_destroy(&viommu->vdevs);
+
+ viommu_put_kvm(viommu);
}
int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
@@ -68,10 +121,32 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
*/
viommu->iommu_dev = __iommu_get_iommu_dev(idev->dev);
+ /* get the kvm details if specified. */
+ if (cmd->kvm_vm_fd) {
+ struct kvm *kvm;
+ struct fd f = fdget(cmd->kvm_vm_fd);
+
+ if (!fd_file(f)) {
+ rc = -EBADF;
+ goto out_abort;
+ }
+
+ if (!file_is_kvm(fd_file(f))) {
+ rc = -EBADF;
+ fdput(f);
+ goto out_abort;
+ }
+ kvm = fd_file(f)->private_data;
+ viommu_get_kvm_safe(viommu, kvm);
+ fdput(f);
+ }
+
cmd->out_viommu_id = viommu->obj.id;
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
- if (rc)
+ if (rc) {
+ viommu_put_kvm(viommu);
goto out_abort;
+ }
iommufd_object_finalize(ucmd->ictx, &viommu->obj); goto out_put_hwpt;
@@ -90,6 +165,9 @@ void iommufd_vdevice_destroy(struct iommufd_object *obj)
container_of(obj, struct iommufd_vdevice, obj);
struct iommufd_viommu *viommu = vdev->viommu;
+ if (vdev->tsm_bound)
+ tsm_unbind(vdev->dev);
+
/* xa_cmpxchg is okay to fail if alloc failed xa_cmpxchg previously */ xa_cmpxchg(&viommu->vdevs, vdev->id, vdev, NULL, GFP_KERNEL); refcount_dec(&viommu->obj.users);
@@ -157,3 +235,57 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
iommufd_put_object(ucmd->ictx, &viommu->obj);
return rc;
}
+
+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd)
+{
+ struct iommu_vdevice_id *cmd = ucmd->cmd;
+ struct iommufd_vdevice *vdev;
+ int rc = 0;
+
+ vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+ IOMMUFD_OBJ_VDEVICE),
+ struct iommufd_vdevice, obj);
+ if (IS_ERR(vdev))
+ return PTR_ERR(vdev);
+
+ rc = tsm_bind(vdev->dev, vdev->viommu->kvm, vdev->id);
+ if (rc) {
+ rc = -ENODEV;
+ goto out_put_vdev;
+ }
+
+ /* locking? */
+ vdev->tsm_bound = true;
+ rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
+
+out_put_vdev:
+ iommufd_put_object(ucmd->ictx, &vdev->obj);
+ return rc;
+}
+
+int iommufd_vdevice_tsm_unbind_ioctl(struct iommufd_ucmd *ucmd) +{
+ struct iommu_vdevice_id *cmd = ucmd->cmd;
+ struct iommufd_vdevice *vdev;
+ int rc = 0;
+
+ vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+ IOMMUFD_OBJ_VDEVICE),
+ struct iommufd_vdevice, obj);
+ if (IS_ERR(vdev))
+ return PTR_ERR(vdev);
+
+ rc = tsm_unbind(vdev->dev);
+ if (rc) {
+ rc = -ENODEV;
+ goto out_put_vdev;
+ }
+
+ /* locking ? */
+ vdev->tsm_bound = false;
+ rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
+
+out_put_vdev:
+ iommufd_put_object(ucmd->ictx, &vdev->obj);
+ return rc;
+}
modified include/linux/iommufd.h
@@ -51,8 +51,9 @@ struct iommufd_object {
unsigned int id;
};
+struct kvm;
struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
- struct device *dev, u32 *id);
+ struct device *dev, struct kvm *kvm, u32 *id);
void iommufd_device_unbind(struct iommufd_device *idev);
int iommufd_device_attach(struct iommufd_device *idev, ioasid_t pasid,
@@ -94,6 +95,8 @@ struct iommufd_viommu {
struct iommufd_ctx *ictx;
struct iommu_device *iommu_dev;
struct iommufd_hwpt_paging *hwpt;
+ struct kvm *kvm;
+ void (*put_kvm)(struct kvm *kvm);
const struct iommufd_viommu_ops *ops;
modified include/uapi/linux/iommufd.h
@@ -56,6 +56,8 @@ enum {
IOMMUFD_CMD_VDEVICE_ALLOC = 0x91,
IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92,
IOMMUFD_CMD_VEVENTQ_ALLOC = 0x93,
+ IOMMUFD_CMD_VDEVICE_TSM_BIND = 0x94,
+ IOMMUFD_CMD_VDEVICE_TSM_UNBIND = 0x95,
};
/**
@@ -985,6 +987,7 @@ struct iommu_viommu_alloc {
__u32 dev_id;
__u32 hwpt_id;
__u32 out_viommu_id;
+ __u32 kvm_vm_fd;
};
#define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC)
@@ -1038,6 +1041,19 @@ enum iommu_veventq_flag {
IOMMU_VEVENTQ_FLAG_LOST_EVENTS = (1U << 0),
};
+/**
+ * struct iommu_vdevice_id - ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size: sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned from IOMMU_VDEVICE_ALLOC
+ */
+struct iommu_vdevice_id {
+ __u32 size;
+ __u32 vdevice_id;
+} __packed;
+#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND) +
+
/**
* struct iommufd_vevent_header - Virtual Event Header for a vEVENTQ Status
* @flags: Combination of enum iommu_veventq_flag
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 187
Quoted 9 lines
drivers/iommu/iommufd/viommu.c +8
-0+if (rc) { + rc = -ENODEV ; + goto out_put_vdev; + } + +/* locking? */ + vdev->tsm_bound = true; +refcount_inc (&vdev->obj.users);This refcount isn't going to work, it will make an error
close()crash..
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
You need to auto-unbind on destruction I think.
Can you elaborate on that? if vdevice is
tsm_bound,iommufd_vdevice_destroy()do calltsm_unbindin the changes I shared.You are driving it from the vfio side? Then you don't need the refcount at all here because the vfio facing APIs already take one.
I am using iommufd ioctl to bind/unbind. The goal was to call tsm_unbind when we close the iommu file descriptor( So when a vdevice object is destroyed).
-aneesh
Jason Gunthorpe
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 22 lines · Aneesh Kumar K.V
(quoted diff) +22
-0+#if IS_ENABLED(CONFIG_KVM) +#include <linux/kvm_host.h> + +static void viommu_get_kvm_safe(struct iommufd_viommu *viommu, struct kvm *kvm) +{ + void (*put_fn)(struct kvm *kvm); + bool (*get_fn)(struct kvm *kvm); + bool ret; + + if (!kvm) + return; + + put_fn = symbol_get(kvm_put_kvm); + if (WARN_ON(!put_fn)) + return; + + get_fn = symbol_get(kvm_get_kvm_safe); + if (WARN_ON(!get_fn)) { + symbol_put(kvm_put_kvm); + return; + } +
Review
Quoted 10 lines
(quoted diff) +10
-0+ ret = get_fn(kvm); + symbol_put(kvm_get_kvm_safe); + if (!ret) { + symbol_put(kvm_put_kvm); + return; + } + + viommu->put_kvm = put_fn; + viommu->kvm = kvm; +}
Shameer was working on something like this too
I would probably split just the viommu kvm stuff into one patch so you two can share it.
Review int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 125
Quoted 6 lines
(quoted diff) +2
-0@@ -68,10 +121,32 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) */ viommu->iommu_dev = __iommu_get_iommu_dev(idev->dev); + /* get the kvm details if specified. */ + if (cmd->kvm_vm_fd) {
Pedantically a 0 fd is still valid, you should add a flag to indicate if the KVM is being supplied.
Quoted 6 lines
(quoted diff) +6
-0+ struct kvm *kvm; + struct fd f = fdget(cmd->kvm_vm_fd); + + if (!fd_file(f)) { + rc = -EBADF; + goto out_abort;
Review int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 141
Quoted 10 lines
(quoted diff) +10
-0+ } + + if (!file_is_kvm(fd_file(f))) { + rc = -EBADF; + fdput(f); + goto out_abort; + } + kvm = fd_file(f)->private_data; + viommu_get_kvm_safe(viommu, kvm); + fdput(f);
I mentioned this to Sean a while back, but can we just use the fdget reference here and forget about kvm_get_kvm_safe()?
drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
Quoted 13 lines
drivers/iommu/iommufd/viommu.c +13
-0+int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_vdevice_id *cmd = ucmd->cmd; +struct iommufd_vdevice *vdev; +int rc =0 ; + + vdev =container_of (iommufd_get_object (ucmd->ictx, cmd->vdevice_id, +IOMMUFD_OBJ_VDEVICE ), + struct iommufd_vdevice, obj); +if (IS_ERR (vdev)) +return PTR_ERR (vdev); + + rc =tsm_bind (vdev->dev, vdev->viommu->kvm, vdev->id);
Yeah, that makes alot of sense now, you are passing in the KVM for the VIOMMU and both the vBDF and pBDF to the TSM layer, that should be enough for it to figure out what to do. The only other data would be the TSM's VIOMMU handle..
Quoted 7 lines
drivers/iommu/iommufd/viommu.c +7
-0+int iommufd_vdevice_tsm_unbind_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_vdevice_id *cmd = ucmd->cmd; +struct iommufd_vdevice *vdev; +int rc =0 ; + + vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 211
Quoted 10 lines
drivers/iommu/iommufd/viommu.c +10
-0+ IOMMUFD_OBJ_VDEVICE), + struct iommufd_vdevice, obj); +if (IS_ERR (vdev)) +return PTR_ERR (vdev); + + rc =tsm_unbind (vdev->dev); +if (rc) { + rc = -ENODEV ; + goto out_put_vdev; + }
But there is no locking here so userspace could race tsm_unbind with tsm_bind, which doesn't sound great. Shouldn't iommufd protect against that?
You could abuse the device_lock(vdev->dev) ?
I think we still have an existing bug where the vdevice can outlive the idevice, but that is not your issue, just FYI
Jason
Jason Gunthorpe
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 10 lines · Jason Gunthorpe
Quoted 10 lines
drivers/iommu/iommufd/viommu.c +10
-0+int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_vdevice_id *cmd = ucmd->cmd; +struct iommufd_vdevice *vdev; +int rc =0 ; + + vdev =container_of (iommufd_get_object (ucmd->ictx, cmd->vdevice_id, +IOMMUFD_OBJ_VDEVICE ), + struct iommufd_vdevice, obj); +if (IS_ERR (vdev))
Quoted 8 lines
drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
Quoted 3 lines
drivers/iommu/iommufd/viommu.c +3
-0+return PTR_ERR (vdev); + + rc =tsm_bind (vdev->dev, vdev->viommu->kvm, vdev->id);Yeah, that makes alot of sense now, you are passing in the KVM for the VIOMMU and both the vBDF and pBDF to the TSM layer, that should be enough for it to figure out what to do. The only other data would be the TSM's VIOMMU handle..
Actually it should also check that the viommu type is compatible with the TSM, somehow.
The way I imagine this working is userspace would create a IOMMU_VIOMMU_TYPE_TSM_VTD (for example) viommu object which will do a TSM call to setup the secure vIOMMU
Then when you create a VDEVICE against the IOMMU_VIOMMU_TYPE_TSM_VTD it will do a TSM call to create the secure vPCI function attached to the vIOMMU and register the vBDF. [1]
And finally bind will switch to T=1 mode.
But if someone creates a VIOMMU with IOMMU_VIOMMU_TYPE_ARM_SMMUV3 then the vdevice shouldn't be allowed to work in TSM mode at all.
Finally IOMMU_VIOMMU_TYPE_TSM_NO_VIOMMU would be a "NOP" viommu type that enables TSM support but has no vIOMMU and works with all the iommu drivers.
Not sure exactly how to wrangle this all, but it should be done here..
1 - IMHO alot of the architectures I've seen have messed up the VIOMMU design by having completely separate IOMMUs for T=1 and T=0 traffic. I hope people will fix this and allow the secure VIOMMU to translate both T=0 and T=1 traffic as walking page tables in secure memory and then rejecting T=0 if the final physical is secure memory. Meaning from an API perspective we want the vPCI to possibly have a working secure vIOMMU before we reach bind.
Jason
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 23 lines · Jason Gunthorpe
Quoted 10 lines · Jason Gunthorpe
Quoted 10 lines
(quoted diff) +10
-0+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd) +{ + struct iommu_vdevice_id *cmd = ucmd->cmd; + struct iommufd_vdevice *vdev; + int rc = 0; + + vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id, + IOMMUFD_OBJ_VDEVICE), + struct iommufd_vdevice, obj); + if (IS_ERR(vdev))Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ return PTR_ERR(vdev); + + rc = tsm_bind(vdev->dev, vdev->viommu->kvm, vdev->id);Yeah, that makes alot of sense now, you are passing in the KVM for the VIOMMU and both the vBDF and pBDF to the TSM layer, that should be enough for it to figure out what to do. The only other data would be the TSM's VIOMMU handle..
Actually it should also check that the viommu type is compatible with the TSM, somehow.
Quoted 7 lines
The way I imagine this working is userspace would create a
IOMMU_VIOMMU_TYPE_TSM_VTD(for example) viommu object which will do a TSM call to setup the secure vIOMMUThen when you create a VDEVICE against the
IOMMU_VIOMMU_TYPE_TSM_VTDit will do a TSM call to create the secure vPCI function attached to the vIOMMU and register the vBDF. [1]
Then we should have more verbose TSM APIs, pci_tsm_tdi_create/bind/unbind/free(), which seems workable. Now according to Dan's series, we only have bind() which creates secure vPCI and switch to T=1, and unbind() which switch to T=0 and free secure vPCI.
Thanks,
Yilun
Quoted 22 lines
And finally bind will switch to T=1 mode.
But if someone creates a VIOMMU with
IOMMU_VIOMMU_TYPE_ARM_SMMUV3then the vdevice shouldn't be allowed to work in TSM mode at all.Finally
IOMMU_VIOMMU_TYPE_TSM_NO_VIOMMUwould be a "NOP" viommu type that enables TSM support but has no vIOMMU and works with all the iommu drivers.Not sure exactly how to wrangle this all, but it should be done here..
1 - IMHO alot of the architectures I've seen have messed up the VIOMMU design by having completely separate IOMMUs for T=1 and T=0 traffic. I hope people will fix this and allow the secure VIOMMU to translate both T=0 and T=1 traffic as walking page tables in secure memory and then rejecting T=0 if the final physical is secure memory. Meaning from an API perspective we want the vPCI to possibly have a working secure vIOMMU before we reach bind.
Jason
Aneesh Kumar K.V
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
In reply to Jason GunthorpeQuoted 23 lines · Jason Gunthorpe
Quoted 10 lines · Jason Gunthorpe
Quoted 10 lines
(quoted diff) +10
-0+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd) +{ + struct iommu_vdevice_id *cmd = ucmd->cmd; + struct iommufd_vdevice *vdev; + int rc = 0; + + vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id, + IOMMUFD_OBJ_VDEVICE), + struct iommufd_vdevice, obj); + if (IS_ERR(vdev))Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ return PTR_ERR(vdev); + + rc = tsm_bind(vdev->dev, vdev->viommu->kvm, vdev->id);Yeah, that makes alot of sense now, you are passing in the KVM for the VIOMMU and both the vBDF and pBDF to the TSM layer, that should be enough for it to figure out what to do. The only other data would be the TSM's VIOMMU handle..
Actually it should also check that the viommu type is compatible with the TSM, somehow.
Quoted 8 lines
The way I imagine this working is userspace would create a
IOMMU_VIOMMU_TYPE_TSM_VTD(for example) viommu object which will do a TSM call to setup the secure vIOMMUThen when you create a VDEVICE against the
IOMMU_VIOMMU_TYPE_TSM_VTDit will do a TSM call to create the secure vPCI function attached to the vIOMMU and register the vBDF. [1]
Don’t we create the vdevice before the guest starts? If I understand correctly, we expect tsm_bind to be triggered by the guest’s request—specifically, when it writes to /sys/bus/pci/devices/X/tsm/connect.
-aneesh
Jason Gunthorpe
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 29 lines · Aneesh Kumar K.V
Quoted 27 lines · Jason Gunthorpe
Quoted 10 lines · Jason Gunthorpe
Quoted 10 lines
(quoted diff) +10
-0+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd) +{ + struct iommu_vdevice_id *cmd = ucmd->cmd; + struct iommufd_vdevice *vdev; + int rc = 0; + + vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id, + IOMMUFD_OBJ_VDEVICE), + struct iommufd_vdevice, obj); + if (IS_ERR(vdev))Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ return PTR_ERR(vdev); + + rc = tsm_bind(vdev->dev, vdev->viommu->kvm, vdev->id);Yeah, that makes alot of sense now, you are passing in the KVM for the VIOMMU and both the vBDF and pBDF to the TSM layer, that should be enough for it to figure out what to do. The only other data would be the TSM's VIOMMU handle..
Actually it should also check that the viommu type is compatible with the TSM, somehow.
The way I imagine this working is userspace would create a
IOMMU_VIOMMU_TYPE_TSM_VTD(for example) viommu object which will do a TSM call to setup the secure vIOMMU
Quoted 6 lines
Quoted 4 lines
Then when you create a VDEVICE against the
IOMMU_VIOMMU_TYPE_TSM_VTDit will do a TSM call to create the secure vPCI function attached to the vIOMMU and register the vBDF. [1]Don’t we create the vdevice before the guest starts?
Yes, vdevice/vPCI creation is before the guest start.
Quoted 3 lines
If I understand correctly, we expect
tsm_bindto be triggered by the guest’s request—specifically, when it writes to/sys/bus/pci/devices/X/tsm/connect.
Yes, vdevice creation does not set the device to T=1.
If the device is T=1/0 mode is a dynamic choice controlled by the guest.
vPCI device creation is controlled by the hypervisor and is done before starting the VM. It just informs the TSM that a vPCI function exists, should the TSM need to know that, which it usually will if a secure vIOMMU is involved.
Jason
Alexey Kardashevskiy
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 30 lines · Jason Gunthorpe
Quoted 29 lines · Aneesh Kumar K.V
Quoted 27 lines · Jason Gunthorpe
Quoted 10 lines · Jason Gunthorpe
Quoted 10 lines
(quoted diff) +10
-0+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd) +{ + struct iommu_vdevice_id *cmd = ucmd->cmd; + struct iommufd_vdevice *vdev; + int rc = 0; + + vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id, + IOMMUFD_OBJ_VDEVICE), + struct iommufd_vdevice, obj); + if (IS_ERR(vdev))Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ return PTR_ERR(vdev); + + rc = tsm_bind(vdev->dev, vdev->viommu->kvm, vdev->id);Yeah, that makes alot of sense now, you are passing in the KVM for the VIOMMU and both the vBDF and pBDF to the TSM layer, that should be enough for it to figure out what to do. The only other data would be the TSM's VIOMMU handle..
Actually it should also check that the viommu type is compatible with the TSM, somehow.
The way I imagine this working is userspace would create a
IOMMU_VIOMMU_TYPE_TSM_VTD(for example) viommu object which will do a TSM call to setup the secure vIOMMU
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Then when you create a VDEVICE against the
IOMMU_VIOMMU_TYPE_TSM_VTDit will do a TSM call to create the secure vPCI function attached to the vIOMMU and register the vBDF. [1]Don’t we create the vdevice before the guest starts?
Yes, vdevice/vPCI creation is before the guest start.
sorry but I still need clarification :)
vPCI == passed through PCI function (ethernet nic, etc), visible in guest's "lspci"
vdevice == slice (say, AMD's DTE/sDTE) of viommu device (say, AMD vIOMMU PCI device) to handle a specific vPCI
is that right?
Quoted 5 lines
Quoted 4 lines
If I understand correctly, we expect
tsm_bindto be triggered by the guest’s request—specifically, when it writes to/sys/bus/pci/devices/X/tsm/connect.
Quoted 7 lines
Yes, vdevice creation does not set the device to T=1.
If the device is T=1/0 mode is a dynamic choice controlled by the guest.
vPCI device creation is controlled by the hypervisor and is done before starting the VM.
I am asking (again) because with PCIe hotplug it is not done before starting the VM. Thanks,
Quoted 5 lines
It just informs the TSM that a vPCI function
exists, should the TSM need to know that, which it usually will if a secure vIOMMU is involved.Jason
Signature · 1 line
Alexey
Jason Gunthorpe
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 38 lines · Alexey Kardashevskiy
Quoted 35 lines · Jason Gunthorpe
Quoted 34 lines · Aneesh Kumar K.V
Quoted 31 lines · Jason Gunthorpe
Quoted 10 lines · Jason Gunthorpe
Quoted 10 lines
(quoted diff) +10
-0+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd) +{ + struct iommu_vdevice_id *cmd = ucmd->cmd; + struct iommufd_vdevice *vdev; + int rc = 0; + + vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id, + IOMMUFD_OBJ_VDEVICE), + struct iommufd_vdevice, obj); + if (IS_ERR(vdev))Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ return PTR_ERR(vdev); + + rc = tsm_bind(vdev->dev, vdev->viommu->kvm, vdev->id);Yeah, that makes alot of sense now, you are passing in the KVM for the VIOMMU and both the vBDF and pBDF to the TSM layer, that should be enough for it to figure out what to do. The only other data would be the TSM's VIOMMU handle..
Actually it should also check that the viommu type is compatible with the TSM, somehow.
The way I imagine this working is userspace would create a
IOMMU_VIOMMU_TYPE_TSM_VTD(for example) viommu object which will do a TSM call to setup the secure vIOMMUThen when you create a VDEVICE against the
IOMMU_VIOMMU_TYPE_TSM_VTDit will do a TSM call to create the secure vPCI function attached to the vIOMMU and register the vBDF. [1]
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Don’t we create the vdevice before the guest starts?
Yes, vdevice/vPCI creation is before the guest start.
sorry but I still need clarification :)
vPCI == passed through PCI function (ethernet nic, etc), visible in guest's "lspci"
Yes
Quoted 1 line
vdevice == slice (say, AMD's DTE/sDTE) of viommu device (say, AMD vIOMMU PCI device) to handle a specific vPCI
Yes.. with general extensions of what "slice" means into the TSM world.
vdevice is the hypervisor handle for the vPCI function the guest sees.
Quoted 4 lines
Quoted 2 lines
vPCI device creation is controlled by the hypervisor and is done before starting the VM.
I am asking (again) because with PCIe hotplug it is not done before starting the VM. Thanks,
Sure, hotplug has to create the vPCI and VDEVICE before notifying the guest of the hotplug.
Jason
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
In reply to Jason GunthorpeQuoted 44 lines · Jason Gunthorpe
Quoted 22 lines · Aneesh Kumar K.V
(quoted diff) +22
-0+#if IS_ENABLED(CONFIG_KVM) +#include <linux/kvm_host.h> + +static void viommu_get_kvm_safe(struct iommufd_viommu *viommu, struct kvm *kvm) +{ + void (*put_fn)(struct kvm *kvm); + bool (*get_fn)(struct kvm *kvm); + bool ret; + + if (!kvm) + return; + + put_fn = symbol_get(kvm_put_kvm); + if (WARN_ON(!put_fn)) + return; + + get_fn = symbol_get(kvm_get_kvm_safe); + if (WARN_ON(!get_fn)) { + symbol_put(kvm_put_kvm); + return; + } +Review
Quoted 10 lines
(quoted diff) +10
-0+ ret = get_fn(kvm); + symbol_put(kvm_get_kvm_safe); + if (!ret) { + symbol_put(kvm_put_kvm); + return; + } + + viommu->put_kvm = put_fn; + viommu->kvm = kvm; +}Shameer was working on something like this too
I would probably split just the viommu kvm stuff into one patch so you two can share it.
Quoted 4 lines
(quoted diff) +0
-0@@ -68,10 +121,32 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) */ viommu->iommu_dev = __iommu_get_iommu_dev(idev->dev);
Quoted 5 lines
Review int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 125
Quoted 2 lines
(quoted diff) +2
-0+ /* get the kvm details if specified. */ + if (cmd->kvm_vm_fd) {Pedantically a 0 fd is still valid, you should add a flag to indicate if the KVM is being supplied.
Did I miss something? Shameer's patch passed in struct kvm* through iommufd_device_bind() then to viommu, and has your Reviewed-by. I'm a little confused...
https://lore.kernel.org/all/[email protected]/
Thanks,
Yilun
Jason Gunthorpe
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 10 lines · Xu Yilun
Quoted 9 lines
Review int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 125
Quoted 6 lines
(quoted diff) +2
-0@@ -68,10 +121,32 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) */ viommu->iommu_dev = __iommu_get_iommu_dev(idev->dev); + /* get the kvm details if specified. */ + if (cmd->kvm_vm_fd) {Pedantically a 0 fd is still valid, you should add a flag to indicate if the KVM is being supplied.
Quoted 5 lines
Did I miss something? Shameer's patch passed in struct kvm* through
iommufd_device_bind()then to viommu, and has your Reviewed-by. I'm a little confused...
He was doing something different, and IIRC posted it before viommu was merged
Jason
Alexey Kardashevskiy
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
In reply to Aneesh Kumar K.VQuoted 53 lines · Aneesh Kumar K.V
Quoted 51 lines · Alexey Kardashevskiy
Quoted 39 lines · Aneesh Kumar K.V
Quoted 37 lines · Alexey Kardashevskiy
Quoted 36 lines · Aneesh Kumar K.V
Quoted 33 lines · Xu Yilun
Quoted 21 lines · Aneesh Kumar K.V
Quoted 19 lines · Xu Yilun
Quoted 17 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.Quoted 6 lines
Quoted 3 lines
To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Since the bind/unbind operations are PCI-specific callbacks, and iommufd
Not really, it is PCI-specific in TSM (for DOE) but since IOMMUFD is not doing any of that, it can work with struct device (not
pci_dev). Thanks,Ok, something like this? and iommufd will call
tsm_bind()?yeah, I guess, there is a couple of places like this
git grep
pci_devdrivers/iommu/iommufd/
Quoted 8 lines
Quoted 4 lines
drivers/iommu/iommufd/device.c: struct pci_dev *pdev = to_pci_dev(idev->dev); drivers/iommu/iommufd/eventq.c: struct pci_dev *pdev = to_pci_dev(dev);Although I do not see any compelling reason to have
pci_devin the TSM API, struct device should just work and not spill any PCI details to IOMMUFD but whatever... Thanks,Getting the kvm reference is tricky here. Also the locking while updating
vdevice->tsm_boundneeds some solution. Here is what I am improving. Are you also planning something similar?
At the moment I am planning getting/holding the KVM reference in the TSM:
https://lore.kernel.org/r/[email protected]
but may push it even further to the AMD TSM (CCP, the firmware driver) as this where I actually need the kvm struct to get GCTX+ASID from kvm_svm; Intel folks have a similar intimate knowledge sharing between kvm_intel and TDX-connect. Thanks,
Quoted 245 lines
drivers/iommu/iommufd/device.c | 4 +- drivers/iommu/iommufd/iommufd_private.h | 5 ++ drivers/iommu/iommufd/main.c | 5 ++ drivers/iommu/iommufd/viommu.c | 62 +++++++++++++++++++++++++ drivers/vfio/iommufd.c | 2 +- include/linux/iommufd.h | 3 +- include/uapi/linux/iommufd.h | 16 +++++++ 7 files changed, 94 insertions(+), 3 deletions(-)drivers/iommu/iommufd/device.c +3
-1@@ -165,7 +165,7 @@ void iommufd_device_destroy(struct iommufd_object *obj)* The caller must undo this with iommufd_device_unbind() */ struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, -struct device *dev,u32 *id) +struct device *dev,struct kvm *kvm,u32 *id) {struct iommufd_device *idev;struct iommufd_group *igroup;@@ -221,6 +221,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,refcount_inc (&idev->obj.users);/* igroup refcount moves into iommufd_device */ idev->igroup = igroup; + idev->kvm = kvm;mutex_init (&idev->iopf_lock);/* @@ -1009,6 +1010,7 @@ void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid)if (!hwpt)return ;iommufd_hw_pagetable_put (idev->ictx, hwpt); + idev->kvm =NULL ;refcount_dec (&idev->obj.users); }EXPORT_SYMBOL_NS_GPL (iommufd_device_detach,"IOMMUFD" );drivers/iommu/iommufd/iommufd_private.h +5
-0@@ -424,6 +424,7 @@ struct iommufd_device {struct list_head group_item;/* always the physical device */ struct device *dev; +struct kvm *kvm;bool enforce_cache_coherency;/* protect iopf_enabled counter */ struct mutex iopf_lock;@@ -606,13 +607,17 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);void iommufd_viommu_destroy (struct iommufd_object *obj);int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_vdevice_destroy (struct iommufd_object *obj); +int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd); +int iommufd_vdevice_tsm_unbind_ioctl (struct iommufd_ucmd *ucmd);struct iommufd_vdevice {struct iommufd_object obj;struct iommufd_ctx *ictx;struct iommufd_viommu *viommu;struct device *dev; +struct kvm *kvm;u64 id;/* per-vIOMMU virtual ID */ +bool tsm_bound; };#ifdef CONFIG_IOMMUFD_TEST drivers/iommu/iommufd/main.c +5
-0@@ -320,6 +320,7 @@ union ucmd_buffer {struct iommu_veventq_alloc veventq;struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu; +struct iommu_vdevice_id vdev_id;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif @@ -379,6 +380,10 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { __reserved),IOCTL_OP (IOMMU_VIOMMU_ALLOC ,iommufd_viommu_alloc_ioctl ,struct iommu_viommu_alloc ,out_viommu_id ), +IOCTL_OP (IOMMU_VDEVICE_TSM_BIND ,iommufd_vdevice_tsm_bind_ioctl , +struct iommu_vdevice_id ,vdevice_id ), +IOCTL_OP (IOMMU_VDEVICE_TSM_UNBIND ,iommufd_vdevice_tsm_unbind_ioctl , +struct iommu_vdevice_id ,vdevice_id ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endifdrivers/iommu/iommufd/viommu.c +62
-0@@ -2,6 +2,7 @@/* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES */ #include "iommufd_private.h" +#include "linux/tsm.h" void iommufd_viommu_destroy (struct iommufd_object *obj) {@@ -90,6 +91,9 @@ void iommufd_vdevice_destroy(struct iommufd_object *obj) container_of(obj, struct iommufd_vdevice, obj);struct iommufd_viommu *viommu = vdev->viommu; +if (vdev->tsm_bound) +tsm_unbind (vdev->dev); +/* xa_cmpxchg is okay to fail if alloc failed xa_cmpxchg previously */ xa_cmpxchg (&viommu->vdevs, vdev->id, vdev,NULL ,GFP_KERNEL );refcount_dec (&viommu->obj.users);@@ -134,6 +138,8 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) vdev->dev = idev->dev;get_device (idev->dev); vdev->viommu = viommu; + vdev->kvm = idev->kvm; +pr_info ("Assigning kvm 0x%lx\n" , vdev->kvm);refcount_inc (&viommu->obj.users); curr =xa_cmpxchg (&viommu->vdevs, virt_id,NULL , vdev,GFP_KERNEL );@@ -157,3 +163,59 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)iommufd_put_object (ucmd->ictx, &viommu->obj);return rc; } + +int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_vdevice_id *cmd = ucmd->cmd; +struct iommufd_vdevice *vdev; +int rc =0 ; + + vdev =container_of (iommufd_get_object (ucmd->ictx, cmd->vdevice_id, +IOMMUFD_OBJ_VDEVICE ), + struct iommufd_vdevice, obj); +if (IS_ERR (vdev)) +return PTR_ERR (vdev); + + rc =tsm_bind (vdev->dev, vdev->kvm, vdev->id); +if (rc) { + rc = -ENODEV ; + goto out_put_vdev; + } + +/* locking? */ + vdev->tsm_bound = true; +refcount_inc (&vdev->obj.users); + rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd)); + +out_put_vdev: +iommufd_put_object (ucmd->ictx, &vdev->obj); +return rc; +} + +int iommufd_vdevice_tsm_unbind_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_vdevice_id *cmd = ucmd->cmd; +struct iommufd_vdevice *vdev; +int rc =0 ; + + vdev =container_of (iommufd_get_object (ucmd->ictx, cmd->vdevice_id, +IOMMUFD_OBJ_VDEVICE ), + struct iommufd_vdevice, obj); +if (IS_ERR (vdev)) +return PTR_ERR (vdev); + + rc =tsm_unbind (vdev->dev); +if (rc) { + rc = -ENODEV ; + goto out_put_vdev; + } + +refcount_dec (&vdev->obj.users); +/* locking ? */ + vdev->tsm_bound = false; + rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd)); + +out_put_vdev: +iommufd_put_object (ucmd->ictx, &vdev->obj); +return rc; +}drivers/vfio/iommufd.c +1
-1@@ -115,7 +115,7 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev, {struct iommufd_device *idev; - idev =iommufd_device_bind (ictx, vdev->dev, out_device_id); + idev =iommufd_device_bind (ictx, vdev->dev, vdev->kvm, out_device_id);if (IS_ERR (idev))return PTR_ERR (idev); vdev->iommufd_device = idev;include/linux/iommufd.h +2
-1@@ -51,8 +51,9 @@ struct iommufd_object {unsigned int id; }; +struct kvm ;struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, -struct device *dev,u32 *id); +struct device *dev,struct kvm *kvm,u32 *id);void iommufd_device_unbind (struct iommufd_device *idev); int iommufd_device_attach(struct iommufd_device *idev, ioasid_t pasid,include/uapi/linux/iommufd.h +16
-0@@ -56,6 +56,8 @@ enum {IOMMUFD_CMD_VDEVICE_ALLOC =0x91 ,IOMMUFD_CMD_IOAS_CHANGE_PROCESS =0x92 ,IOMMUFD_CMD_VEVENTQ_ALLOC =0x93 , +IOMMUFD_CMD_VDEVICE_TSM_BIND =0x94 , + IOMMUFD_CMD_VDEVICE_TSM_UNBIND = 0x95, };/** @@ -1038,6 +1040,20 @@ enum iommu_veventq_flag { IOMMU_VEVENTQ_FLAG_LOST_EVENTS = (1U << 0), }; +/** +* struct iommu_vdevice_tsm_unbind - ioctl(IOMMU_VDEVICE_TSM_UNBIND) +* @size: sizeof(struct iommu_vdevice_tsm_unbind) +* @vdevice_id: +* +*/ +struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND) + +/** * struct iommufd_vevent_header - Virtual Event Header for a vEVENTQ Status * @flags: Combination of enum iommu_veventq_flag
Signature · 1 line
Alexey
Aneesh Kumar K.V
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 64 lines · Alexey Kardashevskiy
Quoted 53 lines · Aneesh Kumar K.V
Quoted 51 lines · Alexey Kardashevskiy
Quoted 39 lines · Aneesh Kumar K.V
Quoted 37 lines · Alexey Kardashevskiy
Quoted 36 lines · Aneesh Kumar K.V
Quoted 33 lines · Xu Yilun
Quoted 21 lines · Aneesh Kumar K.V
Quoted 19 lines · Xu Yilun
Quoted 17 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.Quoted 6 lines
Quoted 3 lines
To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Since the bind/unbind operations are PCI-specific callbacks, and iommufd
Not really, it is PCI-specific in TSM (for DOE) but since IOMMUFD is not doing any of that, it can work with struct device (not
pci_dev). Thanks,Ok, something like this? and iommufd will call
tsm_bind()?yeah, I guess, there is a couple of places like this
git grep
pci_devdrivers/iommu/iommufd/Quoted 8 lines
Quoted 4 lines
drivers/iommu/iommufd/device.c: struct pci_dev *pdev = to_pci_dev(idev->dev); drivers/iommu/iommufd/eventq.c: struct pci_dev *pdev = to_pci_dev(dev);Although I do not see any compelling reason to have
pci_devin the TSM API, struct device should just work and not spill any PCI details to IOMMUFD but whatever... Thanks,Getting the kvm reference is tricky here. Also the locking while updating
vdevice->tsm_boundneeds some solution. Here is what I am improving. Are you also planning something similar?
Quoted 5 lines
At the moment I am planning getting/holding the KVM reference in the TSM:
https://lore.kernel.org/r/[email protected]
but may push it even further to the AMD TSM (CCP, the firmware driver) as this where I actually need the kvm struct to get GCTX+ASID from
kvm_svm; Intel folks have a similar intimate knowledge sharing betweenkvm_inteland TDX-connect. Thanks,
So you won't be able to work with already available kvm reference in viommu alloc? I will send the tsm_bind changes i have done so that we can share the diff against that with explanation of why things can't work that way?
-aneesh
Aneesh Kumar K.V (Arm)
maintainerRFC PATCH 1/3 coco: tsm: Add tsm_bind/unbind helpers
This will be later used by iommufd bind a tdi to guest.
--- drivers/virt/coco/tsm-core.c | 18 ++++++++++++++++++ include/linux/tsm.h | 3 +++ 2 files changed, 21 insertions(+)
drivers/virt/coco/tsm-core.c +18 -0
@@ -116,6 +116,24 @@ void tsm_ide_stream_unregister(struct pci_ide *ide) }EXPORT_SYMBOL_GPL (tsm_ide_stream_unregister); +int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id) +{ +if (!dev_is_pci (dev)) +return -EINVAL ; + +return pci_tsm_bind (to_pci_dev (dev), kvm, tdi_id); +} +EXPORT_SYMBOL_GPL (tsm_bind); + +int tsm_unbind (struct device *dev) +{ +if (!dev_is_pci (dev)) +return -EINVAL ; + +return pci_tsm_unbind (to_pci_dev (dev)); +} +EXPORT_SYMBOL_GPL (tsm_unbind); +static void tsm_release (struct device *dev) {struct tsm_core_dev *core =container_of (dev,typeof (*core), dev);
include/linux/tsm.h +3 -0
@@ -118,6 +118,9 @@ struct tsm_core_dev *tsm_register(struct device *parent,void tsm_unregister (struct tsm_core_dev *tsm_core);struct pci_dev ;struct pci_ide ; +struct kvm ;int tsm_ide_stream_register (struct pci_dev *pdev,struct pci_ide *ide);void tsm_ide_stream_unregister (struct pci_ide *ide); +int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id); +int tsm_unbind (struct device *dev);#endif /* __TSM_H */
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerRFC PATCH 2/3 iommufd/viommu: Add support to associate viommu with kvm instance
The associated kvm instance will be used in later patch by iommufd to bind a tdi to kvm.
--- drivers/iommu/iommufd/viommu.c | 48 ++++++++++++++++++++++++++++++++-- include/linux/iommufd.h | 3 +++ include/uapi/linux/iommufd.h | 2 ++ 3 files changed, 51 insertions(+), 2 deletions(-)
drivers/iommu/iommufd/viommu.c +46 -2
@@ -2,6 +2,37 @@/* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES */ #include "iommufd_private.h" +#include "linux/tsm.h" + +#if IS_ENABLED (CONFIG_KVM ) +#include <linux/kvm_host.h> + +static int viommu_get_kvm (struct iommufd_viommu *viommu,int kvm_vm_fd) +{ +int rc = -EBADF ; +struct kvm *kvm; +struct fd f =fdget (kvm_vm_fd); + +if (!fd_file (f) || !file_is_kvm (fd_file (f))) + goto err_out; + + kvm =fd_file (f)->private_data; +if (!kvm) + goto err_out; + +/* hold the kvm reference via file descriptor */ + viommu->kvm_fd = f; +return rc; +err_out: +fdput (f); +return rc; +} + +static void viommu_put_kvm (struct iommufd_viommu *viommu) +{ +fdput (viommu->kvm_fd); +} +#endif void iommufd_viommu_destroy (struct iommufd_object *obj) {
@@ -12,6 +43,8 @@ void iommufd_viommu_destroy(struct iommufd_object *obj) viommu->ops->destroy (viommu);refcount_dec (&viommu->hwpt->common.obj.users);xa_destroy (&viommu->vdevs); + +viommu_put_kvm (viommu); }int iommufd_viommu_alloc_ioctl (struct iommufd_ucmd *ucmd)
@@ -23,7 +56,9 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)const struct iommu_ops *ops;int rc; -if (cmd->flags || cmd->type ==IOMMU_VIOMMU_TYPE_DEFAULT ) +if (cmd->flags & ~IOMMU_VIOMMU_KVM_FD ) +return -EOPNOTSUPP ; +if (cmd->type ==IOMMU_VIOMMU_TYPE_DEFAULT )return -EOPNOTSUPP ; idev =iommufd_get_device (ucmd, cmd->dev_id);
@@ -68,10 +103,19 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)*/ viommu->iommu_dev =__iommu_get_iommu_dev (idev->dev); +/* get the kvm details if specified. */ +if (cmd->flags &IOMMU_VIOMMU_KVM_FD ) { + rc =viommu_get_kvm (viommu, cmd->kvm_vm_fd); +if (rc) + goto out_abort; + } + cmd->out_viommu_id = viommu->obj.id; rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd)); -if (rc) +if (rc) { +viommu_put_kvm (viommu); goto out_abort; + }iommufd_object_finalize (ucmd->ictx, &viommu->obj); goto out_put_hwpt;
include/linux/iommufd.h +3 -0
@@ -12,6 +12,7 @@#include <linux/refcount.h> #include <linux/types.h> #include <linux/xarray.h> +#include <linux/file.h> #include <uapi/linux/iommufd.h> struct device ;
@@ -51,6 +52,7 @@ struct iommufd_object {unsigned int id; }; +struct kvm ;struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx,struct device *dev,u32 *id);void iommufd_device_unbind (struct iommufd_device *idev);
@@ -94,6 +96,7 @@ struct iommufd_viommu {struct iommufd_ctx *ictx;struct iommu_device *iommu_dev;struct iommufd_hwpt_paging *hwpt; +struct fd kvm_fd;const struct iommufd_viommu_ops *ops;
include/uapi/linux/iommufd.h +2 -0
@@ -957,6 +957,7 @@ enum iommu_viommu_type { IOMMU_VIOMMU_TYPE_ARM_SMMUV3 = 1, }; +#define IOMMU_VIOMMU_KVM_FD BIT(0)/** * struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC) * @size: sizeof(struct iommu_viommu_alloc)
@@ -985,6 +986,7 @@ struct iommu_viommu_alloc {__u32 dev_id;__u32 hwpt_id;__u32 out_viommu_id; +__u32 kvm_vm_fd; };#define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC)
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerRe: [RFC PATCH 2/3] iommufd/viommu: Add support to associate viommu with kvm instance
drivers/iommu/iommufd/viommu.c line 21
Quoted 12 lines
drivers/iommu/iommufd/viommu.c +12
-0+static int viommu_get_kvm (struct iommufd_viommu *viommu,int kvm_vm_fd) +{ +int rc = -EBADF ; +struct kvm *kvm; +struct fd f =fdget (kvm_vm_fd); + +if (!fd_file (f) || !file_is_kvm (fd_file (f))) + goto err_out; + + kvm =fd_file (f)->private_data; +if (!kvm) + goto err_out;
Is this actually possible? Doesn't it suggest that the file refcount is not sufficient? If not possible then remove it.
drivers/iommu/iommufd/viommu.c line 24
Quoted 3 lines
drivers/iommu/iommufd/viommu.c +3
-0+ +/* hold the kvm reference via file descriptor */ + viommu->kvm_fd = f;
You can't store a 'struct fd', fdget is a special "fast" function that only works within system calls. You must use the normal fget flow here.
include/uapi/linux/iommufd.h enum iommu_viommu_type { · line 960
Quoted 9 lines
include/uapi/linux/iommufd.h +1
-0@@ -957,6 +957,7 @@ enum iommu_viommu_type { IOMMU_VIOMMU_TYPE_ARM_SMMUV3 = 1, }; +#define IOMMU_VIOMMU_KVM_FD BIT(0)
Needs a kdoc
Jason
Aneesh Kumar K.V (Arm)
maintainerRFC PATCH 3/3 iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Aneesh Kumar K.V (Arm)--- drivers/iommu/iommufd/iommufd_private.h | 3 + drivers/iommu/iommufd/main.c | 5 ++ drivers/iommu/iommufd/viommu.c | 78 +++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 15 +++++ 4 files changed, 101 insertions(+)
drivers/iommu/iommufd/iommufd_private.h +3 -0
@@ -606,6 +606,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);void iommufd_viommu_destroy (struct iommufd_object *obj);int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_vdevice_destroy (struct iommufd_object *obj); +int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd); +int iommufd_vdevice_tsm_unbind_ioctl (struct iommufd_ucmd *ucmd);struct iommufd_vdevice {struct iommufd_object obj;
@@ -613,6 +615,7 @@ struct iommufd_vdevice {struct iommufd_viommu *viommu;struct device *dev;u64 id;/* per-vIOMMU virtual ID */ +bool tsm_bound; };#ifdef CONFIG_IOMMUFD_TEST
drivers/iommu/iommufd/main.c +5 -0
@@ -320,6 +320,7 @@ union ucmd_buffer {struct iommu_veventq_alloc veventq;struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu; +struct iommu_vdevice_id vdev_id;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif
@@ -379,6 +380,10 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { __reserved),IOCTL_OP (IOMMU_VIOMMU_ALLOC ,iommufd_viommu_alloc_ioctl ,struct iommu_viommu_alloc ,out_viommu_id ), +IOCTL_OP (IOMMU_VDEVICE_TSM_BIND ,iommufd_vdevice_tsm_bind_ioctl , +struct iommu_vdevice_id ,vdevice_id ), +IOCTL_OP (IOMMU_VDEVICE_TSM_UNBIND ,iommufd_vdevice_tsm_unbind_ioctl , +struct iommu_vdevice_id ,vdevice_id ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif
drivers/iommu/iommufd/viommu.c +78 -0
87 lines not on this page · raw message
include/uapi/linux/iommufd.h +15 -0
@@ -56,6 +56,8 @@ enum {IOMMUFD_CMD_VDEVICE_ALLOC =0x91 ,IOMMUFD_CMD_IOAS_CHANGE_PROCESS =0x92 ,IOMMUFD_CMD_VEVENTQ_ALLOC =0x93 , +IOMMUFD_CMD_VDEVICE_TSM_BIND =0x94 , + IOMMUFD_CMD_VDEVICE_TSM_UNBIND = 0x95, };/**
@@ -1040,6 +1042,19 @@ enum iommu_veventq_flag { IOMMU_VEVENTQ_FLAG_LOST_EVENTS = (1U << 0), }; +/** +* struct iommu_vdevice_id - ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND) +* @size: sizeof(struct iommu_vdevice_id) +* @vdevice_id: Object handle for the vDevice. Returned from IOMMU_VDEVICE_ALLOC +*/ +struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND) + +/** * struct iommufd_vevent_header - Virtual Event Header for a vEVENTQ Status * @flags: Combination of enum iommu_veventq_flag
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 215
Quoted 8 lines · Aneesh Kumar K.V (Arm)
drivers/iommu/iommufd/viommu.c +8
-0+static struct mutex *vdev_lock (struct iommufd_vdevice *vdev) +{ + +if (device_lock_interruptible (vdev->dev) !=0 ) +return NULL ; +return &vdev->dev->mutex; +} +DEFINE_FREE(vdev_unlock, struct mutex *, if (_T) mutex_unlock(_T))
I know I suggested this, but maybe it would be happier to use a mutex in the viommu?
What is the locking model you need for TSM calls here anyhow? Can you concurrently call tsms for vommu creation with bind/unbind or so on?
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1053
Quoted 9 lines
include/uapi/linux/iommufd.h +9
-0+/** +* struct iommu_vdevice_id - ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND) +* @size: sizeof(struct iommu_vdevice_id) +* @vdevice_id: Object handle for the vDevice. Returned from IOMMU_VDEVICE_ALLOC +*/ +struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed;
???
Why is it called vdevice_id?
Why is it packed?
The struct should be per-ioctl. Does anyone need a TSM specific argument blob for bind?
Jason
Aneesh Kumar K.V
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 14 lines · Jason Gunthorpe
drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 215
Quoted 8 lines · Aneesh Kumar K.V (Arm)
drivers/iommu/iommufd/viommu.c +8
-0+static struct mutex *vdev_lock (struct iommufd_vdevice *vdev) +{ + +if (device_lock_interruptible (vdev->dev) !=0 ) +return NULL ; +return &vdev->dev->mutex; +} +DEFINE_FREE(vdev_unlock, struct mutex *, if (_T) mutex_unlock(_T))I know I suggested this, but maybe it would be happier to use a mutex in the viommu?
Quoted 3 lines
What is the locking model you need for TSM calls here anyhow? Can you concurrently call tsms for vommu creation with bind/unbind or so on?
Thinking about this more, I guess we likely don’t need a lock here. I initially added it to handle vdevice->tsm_bind, but concurrent TSM calls are already serialized via tsm_ops_lock.
Additionally, if tsm_bind is invoked on an already bound TDI, the TSM layer handles it gracefully. This suggests that maintaining vdevice->tsm_bound is unnecessary.
Since we're not modifying any vdevice state here, it appears safe to remove the vdev_lock() call?
Quoted 14 lines
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1053
Quoted 9 lines
include/uapi/linux/iommufd.h +9
-0+/** +* struct iommu_vdevice_id - ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND) +* @size: sizeof(struct iommu_vdevice_id) +* @vdevice_id: Object handle for the vDevice. Returned from IOMMU_VDEVICE_ALLOC +*/ +struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed;???
Why is it calledvdevice_id?
Why is it packed?
Quoted 2 lines
The struct should be per-ioctl. Does anyone need a TSM specific argument blob for bind?
For both tsm_bind and tsm_unbind, we need the vdevice id. How do we pass that?
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 24 lines · Aneesh Kumar K.V
Quoted 14 lines · Jason Gunthorpe
drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 215
Quoted 8 lines · Aneesh Kumar K.V (Arm)
drivers/iommu/iommufd/viommu.c +8
-0+static struct mutex *vdev_lock (struct iommufd_vdevice *vdev) +{ + +if (device_lock_interruptible (vdev->dev) !=0 ) +return NULL ; +return &vdev->dev->mutex; +} +DEFINE_FREE(vdev_unlock, struct mutex *, if (_T) mutex_unlock(_T))I know I suggested this, but maybe it would be happier to use a mutex in the viommu?
Quoted 3 lines
What is the locking model you need for TSM calls here anyhow? Can you concurrently call tsms for vommu creation with bind/unbind or so on?
Thinking about this more, I guess we likely don’t need a lock here. I initially added it to handle
vdevice->tsm_bind, but concurrent TSM calls are already serialized viatsm_ops_lock.
Quoted 6 lines
Additionally, if
tsm_bindis invoked on an already bound TDI, the TSM layer handles it gracefully. This suggests that maintainingvdevice->tsm_boundis unnecessary.Since we're not modifying any vdevice state here, it appears safe to remove the
vdev_lock()call?
Okay, that's a reasonable answer
Quoted 9 lines
Quoted 9 lines
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1053
Quoted 4 lines
include/uapi/linux/iommufd.h +4
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed;???
Why is it calledvdevice_id?
Why is it packed?
Quoted 5 lines
Quoted 2 lines
The struct should be per-ioctl. Does anyone need a TSM specific argument blob for bind?
For both
tsm_bindandtsm_unbind, we need the vdevice id. How do we pass that?
You should have a struct attached to the ioctl, and not packed. Maybe this is a sign you don't need two ioctls, or maybe you should have two structs.
What you may really want is a TSM_OPERATION iommufd operation where bind/unbind are just sub-ops there. It could unify the viommu and vdevice related TSM ops that will be needed into one ioctl.
I think all the ops will have the same basic format of an id and a blob of TSM specific information?
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Aneesh Kumar K.V (Arm)include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 10 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +6
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)
Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of. At least TDX Connect cares about this problem now. And the conclusion seems to be "have a VFIO_DEVICE_BIND(iommufd vdevice id), then have VFIO reach into iommufd".
And some further findings [2] indicate this problem may also exist on AMD when p2p is involved.
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/aDnXxk46kwrOcl0i@yilunxu-OptiPlex-7050/
Thanks,
Yilun
Quoted 8 lines
include/uapi/linux/iommufd.h +2
-0+ +/** * struct iommufd_vevent_header - Virtual Event Header for a vEVENTQ Status * @flags: Combination of enum iommu_veventq_flag Signature · 1 line
2.43.0
Alexey Kardashevskiy
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 15 lines · Xu Yilun
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 10 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +6
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
Quoted 2 lines
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of.
What will the host VFIO-PCI driver do differently? I only remember "stop mmaping to the userspace", is that all? Or, more to the point, what is that exact thing which cannot be done from QEMU? Thanks,
Quoted 21 lines
At least TDX Connect cares about this problem now. And the conclusion seems to be "have a
VFIO_DEVICE_BIND(iommufd vdevice id), then have VFIO reach into iommufd".And some further findings [2] indicate this problem may also exist on AMD when p2p is involved.
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/aDnXxk46kwrOcl0i@yilunxu-OptiPlex-7050/Thanks,
YilunQuoted 8 lines
include/uapi/linux/iommufd.h +2
-0+ +/** * struct iommufd_vevent_header - Virtual Event Header for a vEVENTQ Status * @flags: Combination of enum iommu_veventq_flag Signature · 1 line
2.43.0
Signature · 1 line
Alexey
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 18 lines · Alexey Kardashevskiy
Quoted 15 lines · Xu Yilun
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 10 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +6
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
Quoted 5 lines
Quoted 2 lines
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of.
What will the host VFIO-PCI driver do differently? I only remember "stop mmaping to the userspace", is that all?
And do unbind before zapping MMIO.
Quoted 1 line
Or, more to the point, what is that exact thing which cannot be done from QEMU? Thanks,
But kernel don't want incorrect userspace calls crash kernel, e.g. VFIO zaps MMIO on TDI bound then KVM just crashes. So you need to check if zapping MMIOs are allowed in VFIO, that means VFIO still needs to know if device is bound. Scatter BIND/UNBIND & other device controls in both IOMMUFD & VFIO makes life harder.
Thanks,
Yilun
Alexey Kardashevskiy
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 27 lines · Xu Yilun
Quoted 18 lines · Alexey Kardashevskiy
Quoted 15 lines · Xu Yilun
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 10 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +6
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
Quoted 5 lines
Quoted 2 lines
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of.
What will the host VFIO-PCI driver do differently? I only remember "stop mmaping to the userspace", is that all?
And do unbind before zapping MMIO.
Quoted 7 lines
Quoted 1 line
Or, more to the point, what is that exact thing which cannot be done from QEMU? Thanks,
But kernel don't want incorrect userspace calls crash kernel, e.g. VFIO zaps MMIO on TDI bound then KVM just crashes. So you need to check if zapping MMIOs are allowed in VFIO, that means VFIO still needs to know if device is bound. Scatter BIND/UNBIND & other device controls in both IOMMUFD & VFIO makes life harder.
I am confused. What is that userspace call, ioctl(VFIO_DEVICE_RESET)? The userspace (==QEMU) knows it is bound and can unbind first, no?
If it is the case of killing QEMU - then there is no usespace and then it is a matter of what fd is closed first - VFIO-PCI or IOMMUFD, we could teach IOMMUFD to hold an VFIO-PCI fd to guarantee the order. Thanks,
Quoted 3 lines
Thanks,
Yilun
Signature · 1 line
Alexey
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 32 lines · Alexey Kardashevskiy
Quoted 29 lines · Xu Yilun
Quoted 18 lines · Alexey Kardashevskiy
Quoted 15 lines · Xu Yilun
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 10 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +6
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
Quoted 5 lines
Quoted 2 lines
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of.
What will the host VFIO-PCI driver do differently? I only remember "stop mmaping to the userspace", is that all?
And do unbind before zapping MMIO.
Quoted 1 line
Or, more to the point, what is that exact thing which cannot be done from QEMU? Thanks,
Quoted 7 lines
Quoted 5 lines
But kernel don't want incorrect userspace calls crash kernel, e.g. VFIO zaps MMIO on TDI bound then KVM just crashes. So you need to check if zapping MMIOs are allowed in VFIO, that means VFIO still needs to know if device is bound. Scatter BIND/UNBIND & other device controls in both IOMMUFD & VFIO makes life harder.
I am confused. What is that userspace call,
ioctl(VFIO_DEVICE_RESET)?
Yes, this can be one.
Quoted 1 line
The userspace (==QEMU) knows it is bound and can unbind first, no?
The userspace can, this is the case of correct userspace call. There are other cases of incorrect userspace call, e.g. QEMU doesn't unbind first and just call ioctl(VFIO_DEVICE_RESET).
Thanks,
Yilun
Quoted 13 lines
If it is the case of killing QEMU - then there is no usespace and then it is a matter of what fd is closed first - VFIO-PCI or IOMMUFD, we could teach IOMMUFD to hold an VFIO-PCI fd to guarantee the order. Thanks,
Quoted 3 lines
Thanks,
YilunSignature · 1 line
Alexey
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Alexey KardashevskiyQuoted 4 lines · Alexey Kardashevskiy
If it is the case of killing QEMU - then there is no usespace and then it is a matter of what fd is closed first - VFIO-PCI or IOMMUFD, we could teach IOMMUFD to hold an VFIO-PCI fd to guarantee the order. Thanks,
It is the other way around VFIO holds the iommufd and it always destroys first.
We are missing a bit where the vfio destruction of the idevice does not clean up the vdevice too.
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 7 lines · Jason Gunthorpe
Quoted 4 lines · Alexey Kardashevskiy
If it is the case of killing QEMU - then there is no usespace and then it is a matter of what fd is closed first - VFIO-PCI or IOMMUFD, we could teach IOMMUFD to hold an VFIO-PCI fd to guarantee the order. Thanks,
Quoted 5 lines
It is the other way around VFIO holds the iommufd and it always destroys first.
We are missing a bit where the vfio destruction of the idevice does not clean up the vdevice too.
Seems there is still problem, the suggested flow is:
1. VFIO fops release
2. vfio_pci_core_close_device()
3. vfio_df_iommufd_unbind()
4. iommufd_device_unbind()
5. iommufd_device_destroy()
6. iommufd_vdevice_destroy() (not implemented)
7. iommufd_vdevice_tsm_unbind()
In step 2, vfio pci does all cleanup, including invalidate MMIO. In step 7 vdevice does tsm unbind, this is not correct. TSM unbind should be done before invalidating MMIO.
TSM unbind should always the first thing to do to release lockdown, then cleanup physical device configuration.
Thanks,
Yilun
Quoted 2 lines
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 24 lines · Xu Yilun
Quoted 7 lines · Jason Gunthorpe
Quoted 4 lines · Alexey Kardashevskiy
If it is the case of killing QEMU - then there is no usespace and then it is a matter of what fd is closed first - VFIO-PCI or IOMMUFD, we could teach IOMMUFD to hold an VFIO-PCI fd to guarantee the order. Thanks,
Quoted 5 lines
It is the other way around VFIO holds the iommufd and it always destroys first.
We are missing a bit where the vfio destruction of the idevice does not clean up the vdevice too.
Seems there is still problem, the suggested flow is:
1. VFIO fops release
2.vfio_pci_core_close_device()
3.vfio_df_iommufd_unbind()
4.iommufd_device_unbind()
5.iommufd_device_destroy()
6.iommufd_vdevice_destroy()(not implemented)
7.iommufd_vdevice_tsm_unbind()
Quoted 6 lines
In step 2, vfio pci does all cleanup, including invalidate MMIO. In step 7 vdevice does tsm unbind, this is not correct. TSM unbind should be done before invalidating MMIO.
TSM unbind should always the first thing to do to release lockdown, then cleanup physical device configuration.
I think you'd have to re-order things so that vfio_df_iommufd_unbind() happens before the mmio invalidate..
And if you can succeed in moving the MMIO mapping to bind/unbind then the invalidate from vfio won't matter.
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 29 lines · Jason Gunthorpe
Quoted 28 lines · Xu Yilun
Quoted 7 lines · Jason Gunthorpe
Quoted 4 lines · Alexey Kardashevskiy
If it is the case of killing QEMU - then there is no usespace and then it is a matter of what fd is closed first - VFIO-PCI or IOMMUFD, we could teach IOMMUFD to hold an VFIO-PCI fd to guarantee the order. Thanks,
Quoted 5 lines
It is the other way around VFIO holds the iommufd and it always destroys first.
We are missing a bit where the vfio destruction of the idevice does not clean up the vdevice too.
Seems there is still problem, the suggested flow is:
1. VFIO fops release
2.vfio_pci_core_close_device()
3.vfio_df_iommufd_unbind()
4.iommufd_device_unbind()
5.iommufd_device_destroy()
6.iommufd_vdevice_destroy()(not implemented)
7.iommufd_vdevice_tsm_unbind()In step 2, vfio pci does all cleanup, including invalidate MMIO. In step 7 vdevice does tsm unbind, this is not correct. TSM unbind should be done before invalidating MMIO.
Quoted 5 lines
Quoted 2 lines
TSM unbind should always the first thing to do to release lockdown, then cleanup physical device configuration.
I think you'd have to re-order things so that
vfio_df_iommufd_unbind()happens before the mmio invalidate..
That seems an easier solution.
Quoted 3 lines
And if you can succeed in moving the MMIO mapping to bind/unbind then the invalidate from vfio won't matter.
It still matters. If VFIO tries to invalidate MMIOs before Unbind, DMA Silent Drop protection still triggers. Ensuring a correct bind/unbind operation in IOMMUFD cannot make VFIO agnostic to bind/unbind.
BTW: Let me summarize what I've got about Bind-MMIO interaction.
1. All vendors need TSM Unbind first, then invalidate MMIOs, this is to avoid DMA Silent Drop protection which exists on all vendors.
2. TDX Connect additionally requires finer operation sequence during TSM Unbind, i.e. invalidate MMIOs after TDI stop and before TDI metadata free, this is for TDX firmware internal TDI management logic.
Thanks,
Yilun
Quoted 2 lines
Jason
Aneesh Kumar K.V
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Xu YilunQuoted 21 lines · Xu Yilun
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 10 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +6
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of. At least TDX Connect cares about this problem now. And the conclusion seems to be "have a
VFIO_DEVICE_BIND(iommufd vdevice id), then have VFIO reach into iommufd".
Quoted 6 lines
And some further findings [2] indicate this problem may also exist on AMD when p2p is involved.
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/aDnXxk46kwrOcl0i@yilunxu-OptiPlex-7050/
Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
In another thread, I asked whether this might be better handled by pci_tsm instead of vfio. I'd be interested in your thoughts on that.
I also noticed you want to unbind the TDI before unmapping the BAR in vfio. From what I understand, this should still be possible if we use an iommufd ioctl. Either approach—a vfio or iommufd ioctl—works fine for my needs. We can continue that discussion in your patch series thread.
-aneesh
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 26 lines · Aneesh Kumar K.V
Quoted 24 lines · Xu Yilun
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 10 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +6
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of. At least TDX Connect cares about this problem now. And the conclusion seems to be "have a
VFIO_DEVICE_BIND(iommufd vdevice id), then have VFIO reach into iommufd".And some further findings [2] indicate this problem may also exist on AMD when p2p is involved.
Quoted 6 lines
Quoted 3 lines
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/aDnXxk46kwrOcl0i@yilunxu-OptiPlex-7050/Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
The immediate reason is to unbind the TDI before unmapping the BAR.
Quoted 7 lines
In another thread, I asked whether this might be better handled by
pci_tsminstead of vfio. I'd be interested in your thoughts on that.I also noticed you want to unbind the TDI before unmapping the BAR in vfio. From what I understand, this should still be possible if we use an iommufd ioctl.
I'm not sure how is that possible.
Quoted 2 lines
Either approach—a vfio or iommufd ioctl—works fine for my needs. We can continue that discussion in your patch series thread.
Yeah, let's discuss in that thread.
Quoted 2 lines
-aneesh
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 4 lines · Xu Yilun
Quoted 2 lines
Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
The immediate reason is to unbind the TDI before unmapping the BAR.
Maybe you should just do this directly, require the TSM layer to issue an unbind if it gets any requests to change the secure EPT?
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines · Xu Yilun
Quoted 2 lines
Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
The immediate reason is to unbind the TDI before unmapping the BAR.
Maybe you should just do this directly, require the TSM layer to issue an unbind if it gets any requests to change the secure EPT?
The TSM layer won't touch S-EPT, KVM manages S-EPT.
Similarly IOMMUFD/IOMMU driver manages IOMMUPT. When p2p is involved, still need to unbind the TDI first then unmap the BAR for IOMMUPT.
Thanks,
Yilun
Quoted 2 lines
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 6 lines · Xu Yilun
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
Quoted 2 lines
Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
The immediate reason is to unbind the TDI before unmapping the BAR.
Maybe you should just do this directly, require the TSM layer to issue an unbind if it gets any requests to change the secure EPT?
The TSM layer won't touch S-EPT, KVM manages S-EPT.
Why not? This cross layering mess has to live someplace.
If the actual issue is the KVM S-EPT interacting with TSM bind/unbind only on Intel platforms then it would be better to address it there and stop trying to dance around the problem in higher levels.
Quoted 3 lines
Similarly IOMMUFD/IOMMU driver manages IOMMUPT. When p2p is involved, still need to unbind the TDI first then unmap the BAR for IOMMUPT.
Huh? I thought if the device is in T=1 mode then it's MMIO should not be in the non-secure IOMMU page table at all for Intel? Only T=1 P2P DMA should reach its MMIO and that goes through the TSM controlled IOMMU which uses the S-EPT ???
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 20 lines · Jason Gunthorpe
Quoted 6 lines · Xu Yilun
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
Quoted 2 lines
Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
The immediate reason is to unbind the TDI before unmapping the BAR.
Maybe you should just do this directly, require the TSM layer to issue an unbind if it gets any requests to change the secure EPT?
The TSM layer won't touch S-EPT, KVM manages S-EPT.
Why not? This cross layering mess has to live someplace.
If the actual issue is the KVM S-EPT interacting with TSM bind/unbind only on Intel platforms then it would be better to address it there and stop trying to dance around the problem in higher levels.
Quoted 8 lines
Quoted 3 lines
Similarly IOMMUFD/IOMMU driver manages IOMMUPT. When p2p is involved, still need to unbind the TDI first then unmap the BAR for IOMMUPT.
Huh? I thought if the device is in T=1 mode then it's MMIO should not be in the non-secure IOMMU page table at all for Intel? Only T=1 P2P DMA should reach its MMIO and that goes through the TSM controlled IOMMU which uses the S-EPT ???
Correct.
But the p2p case may impact AMD, AMD have legacy IOMMUPT on its secure DMA path. And if you invalidate MMIO (in turn unmaps IOMMUPT) when bound, may trigger HW protection mechanism against DMA silent drop.
SEV-TIO Firmware Interface SPEC, Section 2.11
"If a bound TDI sends a request to the root complex, and the IOMMU detects a fault caused by host configuration, the root complex fences the ASID from all further I/O to or from that guest. A host fault is either a host page table fault or an RMP check violation. ASID fencing means that the IOMMU blocks all further I/O from the root complex to the guest that the TDI was bound, and the root complex blocks all MMIO accesses by the guest. When a guest writes to MMIO, the write is silently dropped. When a guest reads from MMIO, the guest reads 1s."
BTW: What is ARM's secure DMA path, does it goes through independent Secure IOPT? So for p2p when VFIO invalidates MMIO, how the Secure IOPT react? How to avoid DMA slient drop?
Thanks,
Yilun
Quoted 2 lines
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 3 lines · Xu Yilun
But the p2p case may impact AMD, AMD have legacy IOMMUPT on its secure DMA path. And if you invalidate MMIO (in turn unmaps IOMMUPT) when bound, may trigger HW protection mechanism against DMA silent drop.
As I understand AMD it sort of has a single translation and relies on its RMP for security. So I think the MMIO remains mapped always in the iommufd IOAS on AMD?
Quoted 8 lines
SEV-TIO Firmware Interface SPEC, Section 2.11
"If a bound TDI sends a request to the root complex, and the IOMMU detects a fault caused by host configuration, the root complex fences the ASID from all further I/O to or from that guest. A host fault is either a host page table fault or an RMP check violation. ASID fencing means that the IOMMU blocks all further I/O from the root complex to the guest that the TDI was bound, and the root complex blocks all MMIO accesses by the guest. When a guest writes to MMIO, the write is silently dropped. When a guest reads from MMIO, the guest reads 1s."
Sounds to me like the guest has to do things properly or the guest gets itself killed. I wonder how feasible this really is..
Quoted 3 lines
BTW: What is ARM's secure DMA path, does it goes through independent Secure IOPT? So for p2p when VFIO invalidates MMIO, how the Secure IOPT react? How to avoid DMA slient drop?
On ARM T=1/0 traffic goes to two different iommu instances.
As I understand it the T=1 traffic will go through an TSM controlled IOMMU that uses the ARM equivalent of the S-EPT for translation. Ie the CPU and IOMMU translation are enforced to be identical.
T=0 traffic will go through an iommufd controlled iommu and it will use the IOAS for translation.
I've also understood this is quite similar to Intel.
(IMHO this design is a mistake, but oh well)
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
But the p2p case may impact AMD, AMD have legacy IOMMUPT on its secure DMA path. And if you invalidate MMIO (in turn unmaps IOMMUPT) when bound, may trigger HW protection mechanism against DMA silent drop.
As I understand AMD it sort of has a single translation and relies on its RMP for security. So I think the MMIO remains mapped always in the iommufd IOAS on AMD?
Depends on how IOMMUFD/IOMMU driver reacts to VFIO's MMIO invalidation when bound. Legacy IOMMUPT is not controlled by firmware, but it's part of AMD's secure DMA path, so we have to do something on VMM side.
In legacy case I assume IOMMUFD should unmap MMIO range in response to MMIO invalidation (via DMABUF move notify), is it? Will do something different when bound?
Anyway, seems we all agreed VFIO should ensure device unbound first, or no MMIO invalidation. This blocks the issue from happening.
Quoted 10 lines
Quoted 9 lines
SEV-TIO Firmware Interface SPEC, Section 2.11
"If a bound TDI sends a request to the root complex, and the IOMMU detects a fault caused by host configuration, the root complex fences the ASID from all further I/O to or from that guest. A host fault is either a host page table fault or an RMP check violation. ASID fencing means that the IOMMU blocks all further I/O from the root complex to the guest that the TDI was bound, and the root complex blocks all MMIO accesses by the guest. When a guest writes to MMIO, the write is silently dropped. When a guest reads from MMIO, the guest reads 1s."
Quoted 2 lines
Sounds to me like the guest has to do things properly or the guest gets itself killed. I wonder how feasible this really is..
I think both guest & host. If host unmaps some legacy IOMMUPT entry delibrately or accidently, the issue happens.
Quoted 10 lines
Quoted 4 lines
BTW: What is ARM's secure DMA path, does it goes through independent Secure IOPT? So for p2p when VFIO invalidates MMIO, how the Secure IOPT react? How to avoid DMA slient drop?
On ARM T=1/0 traffic goes to two different iommu instances.
As I understand it the T=1 traffic will go through an TSM controlled IOMMU that uses the ARM equivalent of the S-EPT for translation. Ie the CPU and IOMMU translation are enforced to be identical.
We are on the same boat... I need to check how ARM operates on this S-EPT equivalent, also in KVM?
Based on this I doubt ARM also has the immediate DMA silent drop issue.
Anyway, unbind the device first.
Quoted 9 lines
T=0 traffic will go through an iommufd controlled iommu and it will use the IOAS for translation.
I've also understood this is quite similar to Intel.
(IMHO this design is a mistake, but oh well)
Jason
Alexey Kardashevskiy
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Jason GunthorpeQuoted 8 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
But the p2p case may impact AMD, AMD have legacy IOMMUPT on its secure DMA path. And if you invalidate MMIO (in turn unmaps IOMMUPT) when bound, may trigger HW protection mechanism against DMA silent drop.
As I understand AMD it sort of has a single translation and relies on its RMP for security.
Well, two levels with the first page table living in the guest memory + RMP for the second page table in the host memory.
Quoted 2 lines
So I think the MMIO remains mapped always in
the iommufd IOAS on AMD?
Yup.
Quoted 10 lines
Quoted 9 lines
SEV-TIO Firmware Interface SPEC, Section 2.11
"If a bound TDI sends a request to the root complex, and the IOMMU detects a fault caused by host configuration, the root complex fences the ASID from all further I/O to or from that guest. A host fault is either a host page table fault or an RMP check violation. ASID fencing means that the IOMMU blocks all further I/O from the root complex to the guest that the TDI was bound, and the root complex blocks all MMIO accesses by the guest. When a guest writes to MMIO, the write is silently dropped. When a guest reads from MMIO, the guest reads 1s."
Quoted 2 lines
Sounds to me like the guest has to do things properly or the guest gets itself killed. I wonder how feasible this really is..
What does look especially worrying? So far the process has been pretty straightforward. Thanks,
Quoted 18 lines
Quoted 3 lines
BTW: What is ARM's secure DMA path, does it goes through independent Secure IOPT? So for p2p when VFIO invalidates MMIO, how the Secure IOPT react? How to avoid DMA slient drop?
On ARM T=1/0 traffic goes to two different iommu instances.
As I understand it the T=1 traffic will go through an TSM controlled IOMMU that uses the ARM equivalent of the S-EPT for translation. Ie the CPU and IOMMU translation are enforced to be identical.
T=0 traffic will go through an iommufd controlled iommu and it will use the IOAS for translation.
I've also understood this is quite similar to Intel.
(IMHO this design is a mistake, but oh well)
Jason
Signature · 1 line
Alexey
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 9 lines · Alexey Kardashevskiy
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
But the p2p case may impact AMD, AMD have legacy IOMMUPT on its secure DMA path. And if you invalidate MMIO (in turn unmaps IOMMUPT) when bound, may trigger HW protection mechanism against DMA silent drop.
Quoted 5 lines
Quoted 2 lines
As I understand AMD it sort of has a single translation and relies on its RMP for security.
Well, two levels with the first page table living in the guest memory + RMP for the second page table in the host memory.
Yes, 'level' is a bit unclear here.. Intel and ARM have four levels.
T=0 S2 lives in hypervisor memory
T=1 S2 lives in TSM memory (S-EPT)
T=0 S1 lives in guest shared memory
T=1 S1 lives in guest private memory
So compared to AMD both the RMP equivalent and the secure S2 live in TSM memory and provide protection..
I've forgotten exactly how AMD manages to secure the IOMMU S2 in hypervisor memory, but it seemed unique to AMD.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
"If a bound TDI sends a request to the root complex, and the IOMMU detects a fault caused by host configuration, the root complex fences the ASID from all further I/O to or from that guest. A host fault is either a host page table fault or an RMP check violation. ASID fencing means that the IOMMU blocks all further I/O from the root complex to the guest that the TDI was bound, and the root complex blocks all MMIO accesses by the guest. When a guest writes to MMIO, the write is silently dropped. When a guest reads from MMIO, the guest reads 1s."
Quoted 5 lines
Quoted 2 lines
Sounds to me like the guest has to do things properly or the guest gets itself killed. I wonder how feasible this really is..
What does look especially worrying? So far the process has been pretty straightforward.
I think it makes debugging guest bugs hard if the guest explodes on an errant DMA.
Jason
Alexey Kardashevskiy
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 23 lines · Jason Gunthorpe
Quoted 9 lines · Alexey Kardashevskiy
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
But the p2p case may impact AMD, AMD have legacy IOMMUPT on its secure DMA path. And if you invalidate MMIO (in turn unmaps IOMMUPT) when bound, may trigger HW protection mechanism against DMA silent drop.
Quoted 5 lines
Quoted 2 lines
As I understand AMD it sort of has a single translation and relies on its RMP for security.
Well, two levels with the first page table living in the guest memory + RMP for the second page table in the host memory.
Yes, 'level' is a bit unclear here.. Intel and ARM have four levels.
T=0 S2 lives in hypervisor memory
T=1 S2 lives in TSM memory (S-EPT)
T=0 S1 lives in guest shared memory
T=1 S1 lives in guest private memory
Quoted 5 lines
So compared to AMD both the RMP equivalent and the secure S2 live in TSM memory and provide protection..
I've forgotten exactly how AMD manages to secure the IOMMU S2 in hypervisor memory, but it seemed unique to AMD.
S2 lives in the host memory, unencrypted, same table can work for trusted and untrusted devices. But S2 lookup results are then checked against RMP, and this is unique to AMD.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
"If a bound TDI sends a request to the root complex, and the IOMMU detects a fault caused by host configuration, the root complex fences the ASID from all further I/O to or from that guest. A host fault is either a host page table fault or an RMP check violation. ASID fencing means that the IOMMU blocks all further I/O from the root complex to the guest that the TDI was bound, and the root complex blocks all MMIO accesses by the guest. When a guest writes to MMIO, the write is silently dropped. When a guest reads from MMIO, the guest reads 1s."
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Sounds to me like the guest has to do things properly or the guest gets itself killed. I wonder how feasible this really is..
What does look especially worrying? So far the process has been pretty straightforward.
I think it makes debugging guest bugs hard if the guest explodes on an errant DMA.
Well, my machine prints IOMMU RMP event in the host's dmesg, the guest does not receive the write, the device driver barfs timeouts or errors, but nothing crashes.
Quoted 2 lines
Jason
Signature · 1 line
Alexey
Alexey Kardashevskiy
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Xu YilunQuoted 6 lines · Xu Yilun
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
Quoted 2 lines
Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
The immediate reason is to unbind the TDI before unmapping the BAR.
Maybe you should just do this directly, require the TSM layer to issue an unbind if it gets any requests to change the secure EPT?
The TSM layer won't touch S-EPT, KVM manages S-EPT.
Is not it the TDX fw which manages _S_-EPT? And the TDX host driver (what is it called btw? Intel's "CCP") registers itself as TSM in the TSM core so it is somewhere near S-EPT logic? Thanks,
Quoted 10 lines
Similarly IOMMUFD/IOMMU driver manages IOMMUPT. When p2p is involved, still need to unbind the TDI first then unmap the BAR for IOMMUPT.
Thanks,
YilunQuoted 2 lines
Jason
Signature · 1 line
Alexey
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 11 lines · Alexey Kardashevskiy
Quoted 8 lines · Xu Yilun
Quoted 7 lines · Jason Gunthorpe
Quoted 4 lines · Xu Yilun
Quoted 2 lines
Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
The immediate reason is to unbind the TDI before unmapping the BAR.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Maybe you should just do this directly, require the TSM layer to issue an unbind if it gets any requests to change the secure EPT?
The TSM layer won't touch S-EPT, KVM manages S-EPT.
Is not it the TDX fw which manages _S_-EPT? And the TDX host driver (what is it called btw? Intel's "CCP") registers itself as TSM in the TSM core so it is somewhere near S-EPT logic? Thanks,
Yeah, I wonder the same things..
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Alexey KardashevskiyQuoted 9 lines · Alexey Kardashevskiy
Quoted 6 lines · Xu Yilun
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
Quoted 2 lines
Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
The immediate reason is to unbind the TDI before unmapping the BAR.
Maybe you should just do this directly, require the TSM layer to issue an unbind if it gets any requests to change the secure EPT?
The TSM layer won't touch S-EPT, KVM manages S-EPT.
Is not it the TDX fw which manages _S_-EPT?
TDX fw writes the S-EPT entries, but to "manage" S-EPT, there are more works for VMM.
The S-EPT related TDX fwcalls are verbose, it is not a fwcall like "TDX_SEPT_MAP/UNMAP(gfn_range, pfn_range)" then the tree is there.
I wanna briefly describe the S-EPT fwcalls
- A SEPT_ADD fwcall links a page-table page to a specifc S-EPT non-leaf entry.
- A PAGE_AUG fwcall links a guest memory page to a specific leaf entry.
- The MEM_TRACK fwcall tracks if VMM kicks all VCPUs out of guest mode, to ensure TLB for S-EPT are flushed/synced for every VCPU.
- A PAGE_REMOVE fwcall clears a specific leaf entry, only when TLB flush are all done.
So it is KVM's job to orchestrate what a S-EPT tree should look like, and request TDX fw to add/remove each S-EPT node one by one. TDX fw is responsible for the security check for each adding/removing request, if the check passed, TDX fw writes the actual S-EPT entry.
These TDX base implementations are already in linux-next.
Quoted 1 line
And the TDX host driver (what is it called btw? Intel's "CCP") registers itself as TSM in the TSM core so it is somewhere near S-EPT logic? Thanks,
Currently kvm_intel is near S-EPT for private memory, just as kvm_amd is near RMP for private memory.
I see in AMD's solution, CCP TSM driver could directly operate RMP for MMIO. I was trying to figure out if TDX TSM driver could also operate MMIO part of S-EPT, but I didn't see the chance. Kicking VCPUs is not what TSM driver could do. Another thing is the existing KVM mirror page table mechenism (allow me to keep simple).
Thanks,
Yilun
Quoted 15 lines
Quoted 11 lines
Similarly IOMMUFD/IOMMU driver manages IOMMUPT. When p2p is involved, still need to unbind the TDI first then unmap the BAR for IOMMUPT.
Thanks,
YilunQuoted 2 lines
Jason
Signature · 1 line
Alexey
Aneesh Kumar K.V
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Xu YilunQuoted 37 lines · Xu Yilun
Quoted 26 lines · Aneesh Kumar K.V
Quoted 24 lines · Xu Yilun
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 10 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +6
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of. At least TDX Connect cares about this problem now. And the conclusion seems to be "have a
VFIO_DEVICE_BIND(iommufd vdevice id), then have VFIO reach into iommufd".And some further findings [2] indicate this problem may also exist on AMD when p2p is involved.
Quoted 6 lines
Quoted 3 lines
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/aDnXxk46kwrOcl0i@yilunxu-OptiPlex-7050/Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
The immediate reason is to unbind the TDI before unmapping the BAR.
Quoted 1 line
Quoted 9 lines
Quoted 6 lines
In another thread, I asked whether this might be better handled by
pci_tsminstead of vfio. I'd be interested in your thoughts on that.I also noticed you want to unbind the TDI before unmapping the BAR in vfio. From what I understand, this should still be possible if we use an iommufd ioctl.
I'm not sure how is that possible.
IIUC, what you need is the below interface
int iommufd_device_tsm_unbind(struct iommufd_device *idev) so that vfio
can use vfio_iommufd_tsm_unbind() -> iommufd_device_tsm_unbind(vdev->iommufd_device);
The below iommufd changes can get that
static struct mutex *vdev_lock(struct iommufd_vdevice *vdev)
{
if (mutex_lock_interruptible(&vdev->mutex) != 0)
return NULL;
return &vdev->mutex;
}DEFINE_FREE(vdev_unlock, struct mutex *, if (_T) mutex_unlock(_T))
static struct mutex *idev_lock(struct iommufd_device *idev)
{
if (mutex_lock_interruptible(&idev->igroup->lock) != 0)
return NULL;
return &idev->igroup->lock;
}DEFINE_FREE(idev_unlock, struct mutex *, if (_T) mutex_unlock(_T))
int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd)
{
struct iommu_vdevice_tsm_bind *cmd = ucmd->cmd;
struct iommufd_vdevice *vdev;
struct iommufd_device *idev;
struct mutex *ilock __free(idev_unlock) = NULL;
struct mutex *vlock __free(vdev_unlock) = NULL;
struct kvm *kvm;
int rc = 0;
if (cmd->flags)
return -EOPNOTSUPP;
idev = iommufd_get_device(ucmd, cmd->dev_id);
if (IS_ERR(idev))
return PTR_ERR(idev);
vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
IOMMUFD_OBJ_VDEVICE),
struct iommufd_vdevice, obj);
if (IS_ERR(vdev)) {
rc = PTR_ERR(vdev);
goto out_put_idev;
}
ilock = idev_lock(idev);
if (!ilock) {
rc = -EINTR;
goto out_put_vdev;
}
if (idev->vdev) {
/* if it is already bound */
rc = -EINVAL;
goto out_put_vdev;
}
vlock = vdev_lock(vdev);
if (!vlock) {
rc = -EINTR;
goto out_put_vdev;
}
if (WARN_ON(vdev->idev)) {
rc = -EINVAL;
goto out_put_vdev;
}
kvm = vdev->viommu->kvm_filp->private_data;
if (kvm) {
/*
* tsm layer will make take care of parallel calls to tsm_bind/unbind
*/
rc = tsm_bind(vdev->dev, kvm, vdev->id);
if (rc) {
rc = -ENODEV;
goto out_put_vdev;
}
} else {
rc = -ENODEV;
goto out_put_vdev;
}
idev->vdev = vdev;
vdev->idev = idev;
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));out_put_idev:
iommufd_put_object(ucmd->ictx, &idev->obj); out_put_vdev: iommufd_put_object(ucmd->ictx, &vdev->obj); return rc; }
static int iommufd_vdevice_tsm_unbind(struct iommufd_vdevice *vdev)
{
int rc = -EINVAL;
struct mutex *lock __free(vdev_unlock) = vdev_lock(vdev);
if (!lock)
return -EINTR;
if (!vdev->idev) {
tsm_unbind(vdev->dev);
vdev->idev = NULL;
rc = 0;
}
return rc;
}/**
* iommufd_device_tsm_unbind - Move a device out of TSM bind state
* @idev: device to detach
*
* Undo iommufd_device_tsm_bind(). This removes all Confidential Computing
* configurations, Once this completes the device is unlocked (TDISP
* CONFIG_UNLOCKED).
*/
int iommufd_device_tsm_unbind(struct iommufd_device *idev)
{
struct mutex *lock __free(idev_unlock) = idev_lock(idev);
if (!lock)
return -EINTR;
if (!idev->vdev)
return -EINVAL;
iommufd_vdevice_tsm_unbind(idev->vdev);
idev->vdev = NULL;
return 0;
}EXPORT_SYMBOL_NS_GPL(iommufd_device_tsm_unbind, "IOMMUFD");
int iommufd_vdevice_tsm_unbind_ioctl(struct iommufd_ucmd *ucmd)
{
struct iommu_vdevice_tsm_unbind *cmd = ucmd->cmd;
struct iommufd_vdevice *vdev;
int rc = 0;
vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
IOMMUFD_OBJ_VDEVICE),
struct iommufd_vdevice, obj);
if (IS_ERR(vdev))
return PTR_ERR(vdev);
rc = iommufd_device_tsm_unbind(vdev->idev);
if (rc) {
rc = -ENODEV;
goto out_put_vdev;
}
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));out_put_vdev:
iommufd_put_object(ucmd->ictx, &vdev->obj); return rc; }
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 46 lines · Aneesh Kumar K.V
Quoted 44 lines · Xu Yilun
Quoted 26 lines · Aneesh Kumar K.V
Quoted 24 lines · Xu Yilun
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 10 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +6
-0+struct iommu_vdevice_id { +__u32 size; +__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of. At least TDX Connect cares about this problem now. And the conclusion seems to be "have a
VFIO_DEVICE_BIND(iommufd vdevice id), then have VFIO reach into iommufd".And some further findings [2] indicate this problem may also exist on AMD when p2p is involved.
Quoted 6 lines
Quoted 3 lines
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/aDnXxk46kwrOcl0i@yilunxu-OptiPlex-7050/Looking at your patch series, I understand the reason you need a vfio ioctl is to call pci_request_regions_exclusive—is that correct?
The immediate reason is to unbind the TDI before unmapping the BAR.
Quoted 7 lines
In another thread, I asked whether this might be better handled by
pci_tsminstead of vfio. I'd be interested in your thoughts on that.I also noticed you want to unbind the TDI before unmapping the BAR in vfio. From what I understand, this should still be possible if we use an iommufd ioctl.
Quoted 6 lines
Quoted 2 lines
I'm not sure how is that possible.
IIUC, what you need is the below interface
intiommufd_device_tsm_unbind(struct iommufd_device *idev)so that vfio
can usevfio_iommufd_tsm_unbind()->iommufd_device_tsm_unbind(vdev->iommufd_device);
I see. But I'm not sure if it can be a better story than ioctl(VFIO_TSM_BIND). You want VFIO unaware of TSM bind, e.g. try to hide pci_request/release_region(), but make VFIO aware of TSM unbind, which seems odd ...
Thanks,
Yilun
Quoted 154 lines
The below iommufd changes can get that
static struct mutex *vdev_lock(struct
iommufd_vdevice*vdev){ if (mutex_lock_interruptible(&vdev->mutex) != 0) return NULL; return &vdev->mutex; }
DEFINE_FREE(vdev_unlock, struct mutex *, if (_T) mutex_unlock(_T))static struct mutex *idev_lock(struct
iommufd_device*idev){ if (mutex_lock_interruptible(&idev->igroup->lock) != 0) return NULL; return &idev->igroup->lock; }
DEFINE_FREE(idev_unlock, struct mutex *, if (_T) mutex_unlock(_T))int
iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd){ struct iommu_vdevice_tsm_bind *cmd = ucmd->cmd; struct iommufd_vdevice *vdev; struct iommufd_device *idev; struct mutex *ilock __free(idev_unlock) = NULL; struct mutex *vlock __free(vdev_unlock) = NULL; struct kvm *kvm; int rc = 0; if (cmd->flags) return -EOPNOTSUPP; idev = iommufd_get_device(ucmd, cmd->dev_id); if (IS_ERR(idev)) return PTR_ERR(idev); vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id, IOMMUFD_OBJ_VDEVICE), struct iommufd_vdevice, obj); if (IS_ERR(vdev)) { rc = PTR_ERR(vdev); goto out_put_idev; } ilock = idev_lock(idev); if (!ilock) { rc = -EINTR; goto out_put_vdev; } if (idev->vdev) { /* if it is already bound */ rc = -EINVAL; goto out_put_vdev; } vlock = vdev_lock(vdev); if (!vlock) { rc = -EINTR; goto out_put_vdev; } if (WARN_ON(vdev->idev)) { rc = -EINVAL; goto out_put_vdev; } kvm = vdev->viommu->kvm_filp->private_data; if (kvm) { /* * tsm layer will make take care of parallel calls to tsm_bind/unbind */ rc = tsm_bind(vdev->dev, kvm, vdev->id); if (rc) { rc = -ENODEV; goto out_put_vdev; } } else { rc = -ENODEV; goto out_put_vdev; } idev->vdev = vdev; vdev->idev = idev; rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
out_put_idev:iommufd_put_object(ucmd->ictx, &idev->obj); out_put_vdev: iommufd_put_object(ucmd->ictx, &vdev->obj); return rc; }static int
iommufd_vdevice_tsm_unbind(struct iommufd_vdevice *vdev){ int rc = -EINVAL; struct mutex *lock __free(vdev_unlock) = vdev_lock(vdev); if (!lock) return -EINTR; if (!vdev->idev) { tsm_unbind(vdev->dev); vdev->idev = NULL; rc = 0; } return rc; }/**
*iommufd_device_tsm_unbind- Move a device out of TSM bind state
* @idev: device to detach
*
* Undoiommufd_device_tsm_bind(). This removes all Confidential Computing
* configurations, Once this completes the device is unlocked (TDISP
*CONFIG_UNLOCKED).
*/
intiommufd_device_tsm_unbind(struct iommufd_device *idev){ struct mutex *lock __free(idev_unlock) = idev_lock(idev); if (!lock) return -EINTR; if (!idev->vdev) return -EINVAL; iommufd_vdevice_tsm_unbind(idev->vdev); idev->vdev = NULL; return 0; }
EXPORT_SYMBOL_NS_GPL(iommufd_device_tsm_unbind, "IOMMUFD");int
iommufd_vdevice_tsm_unbind_ioctl(struct iommufd_ucmd *ucmd){ struct iommu_vdevice_tsm_unbind *cmd = ucmd->cmd; struct iommufd_vdevice *vdev; int rc = 0; vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id, IOMMUFD_OBJ_VDEVICE), struct iommufd_vdevice, obj); if (IS_ERR(vdev)) return PTR_ERR(vdev); rc = iommufd_device_tsm_unbind(vdev->idev); if (rc) { rc = -ENODEV; goto out_put_vdev; } rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
out_put_vdev:iommufd_put_object(ucmd->ictx, &vdev->obj); return rc; }
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 3 lines · Xu Yilun
I see. But I'm not sure if it can be a better story than
ioctl(VFIO_TSM_BIND). You want VFIO unaware of TSM bind, e.g. try to hide pci_request/release_region(), but make VFIO aware of TSM unbind, which seems odd ...
request_region does not need to be done dynamically. It should be done once when the VFIO cdev is opened. If you need some new ioctl to put VFIO in a CC compatible mode then it should do all this stuff once. It doesn't need to be dynamic.
I think all you want is to trigger VFIO to invalidate its MMIOs when bind/unbind happens.
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
I see. But I'm not sure if it can be a better story than
ioctl(VFIO_TSM_BIND). You want VFIO unaware of TSM bind, e.g. try to hide pci_request/release_region(), but make VFIO aware of TSM unbind, which seems odd ...
request_regiondoes not need to be done dynamically. It should be done once when the VFIO cdev is opened. If you need some new ioctl to put VFIO in a CC compatible mode then it should do all this stuff once. It doesn't need to be dynamic.
But the unbind needs to be dynamic.
Quoted 3 lines
I think all you want is to trigger VFIO to invalidate its MMIOs when bind/unbind happens.
Trigger VFIO to passively invalidate MMIOs during unbind is a TDX specific requirement.
Another more general requirement is, VFIO needs to trigger unbind when VFIO wants to actively invalidate MMIOs. e.g. before VFIO resets device. That is the dynamic unbind thing.
The reason is the secure DMA silent drop issue. Intel, and seems AMD (Alexey please confirm) both implemented some policy in FW/HW to block this issue. But the consequences are fatal to OS, so better we avoid this.
[1]: https://lore.kernel.org/all/aDnXxk46kwrOcl0i@yilunxu-OptiPlex-7050/
Thanks,
Yilun
Quoted 2 lines
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 7 lines · Xu Yilun
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
I see. But I'm not sure if it can be a better story than
ioctl(VFIO_TSM_BIND). You want VFIO unaware of TSM bind, e.g. try to hide pci_request/release_region(), but make VFIO aware of TSM unbind, which seems odd ...
Quoted 6 lines
Quoted 4 lines
request_regiondoes not need to be done dynamically. It should be done once when the VFIO cdev is opened. If you need some new ioctl to put VFIO in a CC compatible mode then it should do all this stuff once. It doesn't need to be dynamic.But the unbind needs to be dynamic.
That has nothing to do with request_region.
Quoted 5 lines
Quoted 2 lines
I think all you want is to trigger VFIO to invalidate its MMIOs when bind/unbind happens.
Trigger VFIO to passively invalidate MMIOs during unbind is a TDX specific requirement.
I still think TDX is making this too hard, the S-EPT is controled by the TSM right? Why doesn't it do the map/unmap of the MMIO as part of the bind/unbind instead of this weird thing where the vPCI function creation is split up between KVM and iommufd?
Quoted 3 lines
Another more general requirement is, VFIO needs to trigger unbind when VFIO wants to actively invalidate MMIOs. e.g. before VFIO resets device. That is the dynamic unbind thing.
Alexey is right here, this is a userspace problem. VFIO should block FLR on an bound device. Userspace has to unbind as part of its FLR flow.
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 20 lines · Jason Gunthorpe
Quoted 7 lines · Xu Yilun
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
I see. But I'm not sure if it can be a better story than
ioctl(VFIO_TSM_BIND). You want VFIO unaware of TSM bind, e.g. try to hide pci_request/release_region(), but make VFIO aware of TSM unbind, which seems odd ...Quoted 6 lines
Quoted 4 lines
request_regiondoes not need to be done dynamically. It should be done once when the VFIO cdev is opened. If you need some new ioctl to put VFIO in a CC compatible mode then it should do all this stuff once. It doesn't need to be dynamic.But the unbind needs to be dynamic.
That has nothing to do with
request_region.Quoted 3 lines
Quoted 2 lines
I think all you want is to trigger VFIO to invalidate its MMIOs when bind/unbind happens.
Quoted 7 lines
Quoted 2 lines
Trigger VFIO to passively invalidate MMIOs during unbind is a TDX specific requirement.
I still think TDX is making this too hard, the S-EPT is controled by the TSM right? Why doesn't it do the map/unmap of the MMIO as part of the bind/unbind instead of this weird thing where the vPCI function creation is split up between KVM and iommufd?
That's good point, thanks. S-EPT is controlled by TSM, but the fact is, unlike RMP it needs too much help from VMM side, and now KVM is the helper. I will continue to investigate if TDX TSM driver could opt in to become another helper and how to coordinate with KVM.
Quoted 7 lines
Quoted 4 lines
Another more general requirement is, VFIO needs to trigger unbind when VFIO wants to actively invalidate MMIOs. e.g. before VFIO resets device. That is the dynamic unbind thing.
Alexey is right here, this is a userspace problem. VFIO should block FLR on an bound device.
That means VFIO should know the bound state. if VFIO cannot receive the initial bind/unbind request from userspace, VFIO needs a callback from IOMMUFD. I think that's what you recently suggest.
Thanks,
Yilun
Quoted 3 lines
Userspace has to unbind as part of its FLR flow.
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 4 lines · Xu Yilun
That's good point, thanks. S-EPT is controlled by TSM, but the fact is, unlike RMP it needs too much help from VMM side, and now KVM is the helper. I will continue to investigate if TDX TSM driver could opt in to become another helper and how to coordinate with KVM.
I think it would be quite a simplification if the iommufd operation would also cause the TSM to setup the secure MMIO directly from the pPCI device and remove hypervisor access to it.
Then you don't need DMABUF to KVM at all.
The create vPCI call would have to specify the base virtual addresses of all the BARs from userspace, which is probably OK as I suspose you also cannot disable or relocate the MMIO BAR while in T=1 mode.
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 7 lines · Jason Gunthorpe
Quoted 4 lines · Xu Yilun
That's good point, thanks. S-EPT is controlled by TSM, but the fact is, unlike RMP it needs too much help from VMM side, and now KVM is the helper. I will continue to investigate if TDX TSM driver could opt in to become another helper and how to coordinate with KVM.
Quoted 5 lines
I think it would be quite a simplification if the iommufd operation would also cause the TSM to setup the secure MMIO directly from the pPCI device and remove hypervisor access to it.
Then you don't need DMABUF to KVM at all.
I thought about this for sometime. It may be possible to trigger KVM to populate/zap the S-EPT but cannot let TSM direct setup/remove S-EPT. RMP could be updated by a single instruction, but S-EPT update involves generic KVM MMU flow like Page Table Page management, TLB invalidation, even mirror EPT management (specific to x86 KVM MMU).
To make KVM populate S-EPT, we need KVM memory slots and in turn need DMABUF.
Thanks,
Yilun
Quoted 6 lines
The create vPCI call would have to specify the base virtual addresses of all the BARs from userspace, which is probably OK as I suspose you also cannot disable or relocate the MMIO BAR while in T=1 mode.
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 14 lines · Xu Yilun
Quoted 7 lines · Jason Gunthorpe
Quoted 4 lines · Xu Yilun
That's good point, thanks. S-EPT is controlled by TSM, but the fact is, unlike RMP it needs too much help from VMM side, and now KVM is the helper. I will continue to investigate if TDX TSM driver could opt in to become another helper and how to coordinate with KVM.
Quoted 5 lines
I think it would be quite a simplification if the iommufd operation would also cause the TSM to setup the secure MMIO directly from the pPCI device and remove hypervisor access to it.
Then you don't need DMABUF to KVM at all.
Quoted 8 lines
I thought about this for sometime. It may be possible to trigger KVM to populate/zap the S-EPT but cannot let TSM direct setup/remove S-EPT. RMP could be updated by a single instruction, but S-EPT update involves generic KVM MMU flow like Page Table Page management, TLB invalidation, even mirror EPT management (specific to x86 KVM MMU).
To make KVM populate S-EPT, we need KVM memory slots and in turn need DMABUF.
May be this is answered/discussed already, but why can't we use (vfio->fd, offset) similar to the gmem_fd for KVM memory slot ? VFIO could prevent mmap if the device is bound and also pervent bind, when there is a mapping ?
Suzuki
Quoted 10 lines
Thanks,
YilunQuoted 6 lines
The create vPCI call would have to specify the base virtual addresses of all the BARs from userspace, which is probably OK as I suspose you also cannot disable or relocate the MMIO BAR while in T=1 mode.
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 3 lines · Suzuki K Poulose
May be this is answered/discussed already, but why can't we use (
vfio->fd, offset) similar to thegmem_fdfor KVM memory slot ? VFIO could prevent mmap if the device is bound and also pervent bind, when there is a mapping ?
This creates a mess of circular dependencies where kvm and vfio are both needing to hold references on each other's FDs.
Jason
Alexey Kardashevskiy
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Xu YilunQuoted 22 lines · Xu Yilun
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
I see. But I'm not sure if it can be a better story than
ioctl(VFIO_TSM_BIND). You want VFIO unaware of TSM bind, e.g. try to hide pci_request/release_region(), but make VFIO aware of TSM unbind, which seems odd ...
request_regiondoes not need to be done dynamically. It should be done once when the VFIO cdev is opened. If you need some new ioctl to put VFIO in a CC compatible mode then it should do all this stuff once. It doesn't need to be dynamic.But the unbind needs to be dynamic.
Quoted 3 lines
I think all you want is to trigger VFIO to invalidate its MMIOs when bind/unbind happens.
Trigger VFIO to passively invalidate MMIOs during unbind is a TDX specific requirement.
Quoted 8 lines
Another more general requirement is, VFIO needs to trigger unbind when VFIO wants to actively invalidate MMIOs. e.g. before VFIO resets device. That is the dynamic unbind thing.
The reason is the secure DMA silent drop issue. Intel, and seems AMD (Alexey please confirm) both implemented some policy in FW/HW to block this issue. But the consequences are fatal to OS, so better we avoid this.
Why does it have to be fatal to any OS? A device which suddenly stops working is not something unheard of, not a good reason to kill an OS. Blocking MMIO or DMA seems like an adequate response.
Quoted 7 lines
[1]: https://lore.kernel.org/all/aDnXxk46kwrOcl0i@yilunxu-OptiPlex-7050/
Thanks,
YilunQuoted 2 lines
Jason
Signature · 1 line
Alexey
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 29 lines · Alexey Kardashevskiy
Quoted 26 lines · Xu Yilun
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun
I see. But I'm not sure if it can be a better story than
ioctl(VFIO_TSM_BIND). You want VFIO unaware of TSM bind, e.g. try to hide pci_request/release_region(), but make VFIO aware of TSM unbind, which seems odd ...
request_regiondoes not need to be done dynamically. It should be done once when the VFIO cdev is opened. If you need some new ioctl to put VFIO in a CC compatible mode then it should do all this stuff once. It doesn't need to be dynamic.But the unbind needs to be dynamic.
Quoted 3 lines
I think all you want is to trigger VFIO to invalidate its MMIOs when bind/unbind happens.
Trigger VFIO to passively invalidate MMIOs during unbind is a TDX specific requirement.
Another more general requirement is, VFIO needs to trigger unbind when VFIO wants to actively invalidate MMIOs. e.g. before VFIO resets device. That is the dynamic unbind thing.
Quoted 6 lines
Quoted 4 lines
The reason is the secure DMA silent drop issue. Intel, and seems AMD (Alexey please confirm) both implemented some policy in FW/HW to block this issue. But the consequences are fatal to OS, so better we avoid this.
Why does it have to be fatal to any OS? A device which suddenly stops working is not something unheard of, not a good reason to kill an OS. Blocking MMIO or DMA seems like an adequate response.
It is fatal before any recovery solution is already in the OS. There are plenty of BUG_ON()s in kernel but from HW's POV they may not be the end of world. If recovery is more complex than prevention from SW's POV, let's prevent it and bail out if we failed to prevent.
AMD's ASID fence (and Intel & ARM's DMA Silent Drop protection) are the ways to ensure security, but let's try best not to trigger them.
Thanks,
Yilun
Quoted 13 lines
Quoted 9 lines
[1]: https://lore.kernel.org/all/aDnXxk46kwrOcl0i@yilunxu-OptiPlex-7050/
Thanks,
YilunQuoted 2 lines
Jason
Signature · 1 line
Alexey
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Aneesh Kumar K.VQuoted 7 lines · Aneesh Kumar K.V
static struct mutex *vdev_lock(struct
iommufd_vdevice*vdev){ if (mutex_lock_interruptible(&vdev->mutex) != 0) return NULL; return &vdev->mutex; }
DEFINE_FREE(vdev_unlock, struct mutex *, if (_T) mutex_unlock(_T))
Dn't do things like this.
We already have scoped_cond_guard(mutex_intr) for this pattern and there was a big debate about its design.
It doesn't make alot of sense to use that here, this is a place where you should not use cleanup.h.
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 10 lines · Jason Gunthorpe
Quoted 7 lines · Aneesh Kumar K.V
static struct mutex *vdev_lock(struct
iommufd_vdevice*vdev){ if (mutex_lock_interruptible(&vdev->mutex) != 0) return NULL; return &vdev->mutex; }
DEFINE_FREE(vdev_unlock, struct mutex *, if (_T) mutex_unlock(_T))
Quoted 4 lines
Dn't do things like this.
We already have
scoped_cond_guard(mutex_intr)for this pattern and there was a big debate about its design.
The work in progress proposal to improve upon the ergonomics of scoped_cond_guard() is the ACQUIRE() + ACQUIRE_ERR() proposal [1]. I need to circle back with Peter about moving that forward:
https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
It doesn't make alot of sense to use that here, this is a place where you should not use
cleanup.h.
What are those situations in your mind? We can capture that in include/linux/cleanup.h doc.
My main "don't use cleanup" is when the function still has goto for other reasons. Make it all or nothing which is already documented that header:
"I.e. for a given routine, convert all resources that need a "goto"
cleanup to scope-based cleanup, or convert none of them."
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 13 lines · Dan Williams
Quoted 12 lines · Jason Gunthorpe
Quoted 7 lines · Aneesh Kumar K.V
static struct mutex *vdev_lock(struct
iommufd_vdevice*vdev){ if (mutex_lock_interruptible(&vdev->mutex) != 0) return NULL; return &vdev->mutex; }
DEFINE_FREE(vdev_unlock, struct mutex *, if (_T) mutex_unlock(_T))Dn't do things like this.
Quoted 8 lines
Quoted 2 lines
We already have
scoped_cond_guard(mutex_intr)for this pattern and there was a big debate about its design.The work in progress proposal to improve upon the ergonomics of
scoped_cond_guard()is theACQUIRE()+ACQUIRE_ERR()proposal [1]. I need to circle back with Peter about moving that forward:
Yeah, maybe if you can get people to agree..
Quoted 5 lines
Quoted 2 lines
It doesn't make alot of sense to use that here, this is a place where you should not use
cleanup.h.What are those situations in your mind? We can capture that in
include/linux/cleanup.hdoc.
I wouldn't indent the whole function, for instance :)
And I think I saw some agreement you shouldn't do tricky things to the holder variable, like nulling it or whatnot..
cleanup.h seems to be generally accepted for very simple direct non-clever things.
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Xu YilunQuoted 6 lines · Xu Yilun
Quoted 6 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +2
-0+struct iommu_vdevice_id { +__u32 size;
Quoted 8 lines
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 4 lines
include/uapi/linux/iommufd.h +4
-0+__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
I thought you guys had moved past that? VFIO doesn't have enough information so iommufd would be a better place to make the call?
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 16 lines · Jason Gunthorpe
Quoted 6 lines · Xu Yilun
Quoted 6 lines
+ * struct
iommu_vdevice_id-ioctl(IOMMU_VDEVICE_TSM_BIND/UNBIND)
+ * @size:sizeof(struct iommu_vdevice_id)
+ * @vdevice_id: Object handle for the vDevice. Returned fromIOMMU_VDEVICE_ALLOC
+ */include/uapi/linux/iommufd.h +2
-0+struct iommu_vdevice_id { +__u32 size;Quoted 8 lines
include/uapi/linux/iommufd.h enum iommu_veventq_flag { · line 1055
Quoted 4 lines
include/uapi/linux/iommufd.h +4
-0+__u32 vdevice_id; +} __packed; +#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND) +#define IOMMU_VDEVICE_TSM_UNBIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_UNBIND)Hello, I see you are talking about the detailed implementation. But could we firstly address the confusing whether this TSM Bind/Unbind should be a VFIO uAPI or IOMMUFD uAPI?
Quoted 1 line
I thought you guys had moved past that?
Not yet.
Quoted 2 lines
VFIO doesn't have enough
information so iommufd would be a better place to make the call?
VFIO doesn't have enough information, but VFIO needs to know about bound state. So comes the suggestion [1] that the VFIO uAPI, then VFIO reach into iommufd for real bind.
And my implementation [2] is:
ioctl(vfio_cdev_fd, VFIO_DEVICE_TSM_BIND)
-> vfio_iommufd_tsm_bind()
-> iommufd_device_tsm_bind()
-> iommufd_vdevice_tsm_bind()
-> pci_tsm_bind()[2]: https://lore.kernel.org/all/[email protected]/
Quoted 14 lines
In this thread [1], I was talking about TSM Bind/Unbind affects VFIO behavior so they cannot be iommufd uAPIs which VFIO is not aware of. At least TDX Connect cares about this problem now. And the conclusion seems to be "have a
VFIO_DEVICE_BIND(iommufd vdevice id), then have VFIO reach into iommufd".And some further findings [2] indicate this problem may also exist on AMD when p2p is involved.
[1]: https://lore.kernel.org/all/[email protected]/
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 11 lines · Xu Yilun
VFIO doesn't have enough information, but VFIO needs to know about bound state. So comes the suggestion [1] that the VFIO uAPI, then VFIO reach into iommufd for real bind.
And my implementation [2] is:
ioctl(vfio_cdev_fd, VFIO_DEVICE_TSM_BIND)
->vfio_iommufd_tsm_bind()
->iommufd_device_tsm_bind()-> iommufd_vdevice_tsm_bind() -> pci_tsm_bind()
This doesn't work, logically you are binding the vdevice, not the idevice, the uapi should provide the vdevice id, which VFIO doesn't have.
If you really need vfio involvement then you need callbacks, I think.
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 14 lines · Jason Gunthorpe
Quoted 11 lines · Xu Yilun
VFIO doesn't have enough information, but VFIO needs to know about bound state. So comes the suggestion [1] that the VFIO uAPI, then VFIO reach into iommufd for real bind.
And my implementation [2] is:
ioctl(vfio_cdev_fd, VFIO_DEVICE_TSM_BIND)
->vfio_iommufd_tsm_bind()
->iommufd_device_tsm_bind()-> iommufd_vdevice_tsm_bind() -> pci_tsm_bind()
Quoted 3 lines
This doesn't work, logically you are binding the vdevice, not the idevice, the uapi should provide the vdevice id, which VFIO doesn't have.
Yes. Sorry I just too lazy to provide the full API format.
The original suggestion [1] is to provide vdevice_id in VFIO uAPI.
[1] https://lore.kernel.org/all/[email protected]/
And here is a piece of the implementation in [2]:
+struct vfio_pci_tsm_bind {
+ __u32 argsz;
+ __u32 flags;
+ __u32 vdevice_id;
+ __u32 pad;
+};
+
+#define VFIO_DEVICE_TSM_BIND _IO(VFIO_TYPE, VFIO_BASE + 22)
[2] https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
If you really need vfio involvement then you need callbacks, I think.
Only callback is not enough, there are cases that VFIO wants actively invalidate MMIO, e.g. VFIO_DEVICE_RESET. In that case, VFIO needs dynamic unbind then invalidate MMIO.
Thanks,
Yilun
Quoted 2 lines
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 27 lines · Xu Yilun
Quoted 14 lines · Jason Gunthorpe
Quoted 11 lines · Xu Yilun
VFIO doesn't have enough information, but VFIO needs to know about bound state. So comes the suggestion [1] that the VFIO uAPI, then VFIO reach into iommufd for real bind.
And my implementation [2] is:
ioctl(vfio_cdev_fd, VFIO_DEVICE_TSM_BIND)
->vfio_iommufd_tsm_bind()
->iommufd_device_tsm_bind()-> iommufd_vdevice_tsm_bind() -> pci_tsm_bind()Quoted 3 lines
This doesn't work, logically you are binding the vdevice, not the idevice, the uapi should provide the vdevice id, which VFIO doesn't have.
Yes. Sorry I just too lazy to provide the full API format.
The original suggestion [1] is to provide
vdevice_idin VFIO uAPI.[1] https://lore.kernel.org/all/[email protected]/
And here is a piece of the implementation in [2]:
Review
Quoted 8 lines
(quoted diff) +8
-0+struct vfio_pci_tsm_bind { + __u32 argsz; + __u32 flags; + __u32 vdevice_id; + __u32 pad; +}; + +#define VFIO_DEVICE_TSM_BIND _IO(VFIO_TYPE, VFIO_BASE + 22)
I don't want to pass iommufd IDs through vfio as much as possibile, it makes no logical sense.
Quoted 5 lines
Quoted 1 line
If you really need vfio involvement then you need callbacks, I think.
Only callback is not enough, there are cases that VFIO wants actively invalidate MMIO, e.g.
VFIO_DEVICE_RESET. In that case, VFIO needs dynamic unbind then invalidate MMIO.
We've already talked about reset, Alexeys solution is good it is not a problem. Block FLR on bound devices.
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 28 lines · Jason Gunthorpe
Quoted 27 lines · Xu Yilun
Quoted 14 lines · Jason Gunthorpe
Quoted 11 lines · Xu Yilun
VFIO doesn't have enough information, but VFIO needs to know about bound state. So comes the suggestion [1] that the VFIO uAPI, then VFIO reach into iommufd for real bind.
And my implementation [2] is:
ioctl(vfio_cdev_fd, VFIO_DEVICE_TSM_BIND)
->vfio_iommufd_tsm_bind()
->iommufd_device_tsm_bind()-> iommufd_vdevice_tsm_bind() -> pci_tsm_bind()Quoted 3 lines
This doesn't work, logically you are binding the vdevice, not the idevice, the uapi should provide the vdevice id, which VFIO doesn't have.
Yes. Sorry I just too lazy to provide the full API format.
The original suggestion [1] is to provide
vdevice_idin VFIO uAPI.[1] https://lore.kernel.org/all/[email protected]/
And here is a piece of the implementation in [2]:
Quoted 11 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+struct vfio_pci_tsm_bind { + __u32 argsz; + __u32 flags; + __u32 vdevice_id; + __u32 pad; +}; + +#define VFIO_DEVICE_TSM_BIND _IO(VFIO_TYPE, VFIO_BASE + 22)I don't want to pass iommufd IDs through vfio as much as possibile, it makes no logical sense.
OK.
Thanks,
Yilun
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
In reply to Aneesh Kumar K.V (Arm)Quoted 13 lines · Aneesh Kumar K.V (Arm)
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/iommufd_private.h | 3 + drivers/iommu/iommufd/main.c | 5 ++ drivers/iommu/iommufd/viommu.c | 78 +++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 15 +++++ 4 files changed, 101 insertions(+)drivers/iommu/iommufd/iommufd_private.h +0
-0
drivers/iommu/iommufd/iommufd_private.h int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd); · line 610
Quoted 5 lines
drivers/iommu/iommufd/iommufd_private.h +2
-0@@ -606,6 +606,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);void iommufd_viommu_destroy (struct iommufd_object *obj);int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_vdevice_destroy (struct iommufd_object *obj); +int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd); +int iommufd_vdevice_tsm_unbind_ioctl (struct iommufd_ucmd *ucmd);
Hello:
I recently have another concern about the vdevice tsm bind/unbind API. And need your inputs.
According to this:
https://lore.kernel.org/all/[email protected]/
Sean illustrates the memory in-place conversion, that the memory owner - gmemfd should own & control the memory shareability) and the conversion. I.e. For in-place conversion, KVM_SET_MEMORY_ATTRIBUTES should be disabled.
Private/shared MMIO must be of in-place conversion, similarly it's the MMIO owner should be responsible for MMIO shareability, maybe adding some new ioctls like MMIO_CONVERT_SHARED/PRIVATE.
From previous discussion, VFIO is the MMIO owner (implement as dmabuf exporter), so manages MMIO shareability. And IOMMUFD vdevice is the TDI state owner for TSM bind/unbind. But MMIO shareability & TDI state are actually correlated, do we really want to manage them in 2 components?
Thanks,
Yilun
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 28 lines · Xu Yilun
Quoted 13 lines · Aneesh Kumar K.V (Arm)
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/iommufd_private.h | 3 + drivers/iommu/iommufd/main.c | 5 ++ drivers/iommu/iommufd/viommu.c | 78 +++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 15 +++++ 4 files changed, 101 insertions(+)drivers/iommu/iommufd/iommufd_private.h +0
-0drivers/iommu/iommufd/iommufd_private.h int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd); · line 610
Quoted 5 lines
drivers/iommu/iommufd/iommufd_private.h +2
-0@@ -606,6 +606,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);void iommufd_viommu_destroy (struct iommufd_object *obj);int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_vdevice_destroy (struct iommufd_object *obj); +int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd); +int iommufd_vdevice_tsm_unbind_ioctl (struct iommufd_ucmd *ucmd);Hello:
I recently have another concern about the vdevice tsm bind/unbind API. And need your inputs.
According to this:
https://lore.kernel.org/all/[email protected]/
Quoted 8 lines
Sean illustrates the memory in-place conversion, that the memory owner - gmemfd should own & control the memory shareability) and the conversion. I.e. For in-place conversion,
KVM_SET_MEMORY_ATTRIBUTESshould be disabled.Private/shared MMIO must be of in-place conversion, similarly it's the MMIO owner should be responsible for MMIO shareability, maybe adding some new ioctls like MMIO_CONVERT_SHARED/PRIVATE.
Except it doesn't work like that for MMIO. Shared/private is a TDI operation only and effects the whole device. We shouldn't split it into two actions.
I also don't think it needs to be strictly 'in-place' as we expect the VM to be idle on the MMIO during this change over. Faulting would be OK.
Quoted 4 lines
From previous discussion, VFIO is the MMIO owner (implement as dmabuf exporter), so manages MMIO shareability. And IOMMUFD vdevice is the TDI state owner for TSM bind/unbind. But MMIO shareability & TDI state are actually correlated, do we really want to manage them in 2 components?
Yes, we've been over this. There are two components, we have to split it somehow. It makes more sense for iommufd to lead the ioctls because it has more information about the full system.
Any case where we need to get back to vfio for something needs to be managed with a callback of some kind. We need to get a list of what those things are.
What do all the arches need here?
- ARM I suspect has the TDI locking operation install the MMIO in the secure S2, not KVM?
- AMD just leaves the MMIO mapped all the time?
- x86 presumably needs to carefully map/unmap to KVM and iommu in the right sequence or you get a MCE?
So what is the plan? You want to wrap this in DMABUF, but will there be two DMABUFS, one for secure and one for non-secure? Is userspace expected to map/unmap in the right sequence?
Something else?
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 34 lines · Jason Gunthorpe
Quoted 33 lines · Xu Yilun
Quoted 13 lines · Aneesh Kumar K.V (Arm)
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/iommufd_private.h | 3 + drivers/iommu/iommufd/main.c | 5 ++ drivers/iommu/iommufd/viommu.c | 78 +++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 15 +++++ 4 files changed, 101 insertions(+)drivers/iommu/iommufd/iommufd_private.h +0
-0drivers/iommu/iommufd/iommufd_private.h int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd); · line 610
Quoted 5 lines
drivers/iommu/iommufd/iommufd_private.h +2
-0@@ -606,6 +606,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);void iommufd_viommu_destroy (struct iommufd_object *obj);int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_vdevice_destroy (struct iommufd_object *obj); +int iommufd_vdevice_tsm_bind_ioctl (struct iommufd_ucmd *ucmd); +int iommufd_vdevice_tsm_unbind_ioctl (struct iommufd_ucmd *ucmd);Hello:
I recently have another concern about the vdevice tsm bind/unbind API. And need your inputs.
According to this:
https://lore.kernel.org/all/[email protected]/Sean illustrates the memory in-place conversion, that the memory owner - gmemfd should own & control the memory shareability) and the conversion. I.e. For in-place conversion,
KVM_SET_MEMORY_ATTRIBUTESshould be disabled.
Quoted 7 lines
Quoted 3 lines
Private/shared MMIO must be of in-place conversion, similarly it's the MMIO owner should be responsible for MMIO shareability, maybe adding some new ioctls like MMIO_CONVERT_SHARED/PRIVATE.
Except it doesn't work like that for MMIO. Shared/private is a TDI operation only and effects the whole device. We shouldn't split it into two actions.
OK. IIUC you want 1 uAPI, TSM Bind, to finish all secure configuration, including MMIO sharebility. I think it is possible, the MMIO shareability is fixed after TSM Bind. iommufd could fetch TDI report to get the private/shared MMIO ranges and callback to VFIO.
Quoted 2 lines
I also don't think it needs to be strictly 'in-place' as we expect the
When I said "must be in-place", I mean the MMIO resource (hpa) for one gfn is fixed, can't have 2 copies of backend as the current private/shared memory does.
Quoted 1 line
VM to be idle on the MMIO during this change over. Faulting would be OK.
Sorry, I don't get your point about 'strictly in-place' here?
Quoted 14 lines
Quoted 5 lines
From previous discussion, VFIO is the MMIO owner (implement as dmabuf exporter), so manages MMIO shareability. And IOMMUFD vdevice is the TDI state owner for TSM bind/unbind. But MMIO shareability & TDI state are actually correlated, do we really want to manage them in 2 components?
Yes, we've been over this. There are two components, we have to split it somehow. It makes more sense for iommufd to lead the ioctls because it has more information about the full system.
Any case where we need to get back to vfio for something needs to be managed with a callback of some kind. We need to get a list of what those things are.
Quoted 6 lines
What do all the arches need here?
- ARM I suspect has the TDI locking operation install the MMIO in the secure S2, not KVM?
- AMD just leaves the MMIO mapped all the time?
- x86 presumably needs to carefully map/unmap to KVM and iommu in the right sequence or you get a MCE?
Yeah, for Intel TDX, basically 2 things, zap the mapping on opposite side page table, mark the correct shareability for correct fault in.
Quoted 3 lines
So what is the plan? You want to wrap this in DMABUF, but will there be two DMABUFS, one for secure and one for non-secure? Is userspace
No I don't expect 2 dmabufs. I image shareability could be an physical attribute of dmabuf and the callback to VFIO changes the shareability. And VFIO, the dmabuf exporter, could notify KVM/iommufd about the shareability change. Then KVM/iommufd unmaps their page tables.
Thanks,
Yilun
Quoted 5 lines
expected to map/unmap in the right sequence?
Something else?
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 8 lines · Xu Yilun
Quoted 3 lines
Except it doesn't work like that for MMIO. Shared/private is a TDI operation only and effects the whole device. We shouldn't split it into two actions.
OK. IIUC you want 1 uAPI, TSM Bind, to finish all secure configuration, including MMIO sharebility. I think it is possible, the MMIO shareability is fixed after TSM Bind. iommufd could fetch TDI report to get the private/shared MMIO ranges and callback to VFIO.
Am I wrong? Isn't this one action? Once you do bind you MUST have no hypervisor mapping or x86 will explode. And once bind completes the hypervisor mapping is unusable on all other arches.
That's one operation as far as I can tell??
Quoted 5 lines
Quoted 1 line
I also don't think it needs to be strictly 'in-place' as we expect the
When I said "must be in-place", I mean the MMIO resource (hpa) for one gfn is fixed, can't have 2 copies of backend as the current private/shared memory does.
Ok, I would not call that in place..
Quoted 3 lines
Quoted 1 line
VM to be idle on the MMIO during this change over. Faulting would be OK.
Sorry, I don't get your point about 'strictly in-place' here?
Here I mean the iommu page table would have to atomically change "in place" from shared to private in a way that that is hitless to the guest. We don't need this, IMHO.
Quoted 11 lines
Quoted 10 lines
Any case where we need to get back to vfio for something needs to be managed with a callback of some kind. We need to get a list of what those things are.
What do all the arches need here?
- ARM I suspect has the TDI locking operation install the MMIO in the secure S2, not KVM?
- AMD just leaves the MMIO mapped all the time?
- x86 presumably needs to carefully map/unmap to KVM and iommu in the right sequence or you get a MCE?
Quoted 2 lines
Yeah, for Intel TDX, basically 2 things, zap the mapping on opposite side page table, mark the correct shareability for correct fault in.
I expect userspace to be doing this, which is why I asked about two DMABUFs..
Quoted 7 lines
Quoted 2 lines
So what is the plan? You want to wrap this in DMABUF, but will there be two DMABUFS, one for secure and one for non-secure? Is userspace
No I don't expect 2 dmabufs. I image shareability could be an physical attribute of dmabuf and the callback to VFIO changes the shareability. And VFIO, the dmabuf exporter, could notify KVM/iommufd about the shareability change. Then KVM/iommufd unmaps their page tables.
How can this work exactly?
Does Intel put shared/private MMIO at the same guest address when it changes around? I understand other arches do not do this.
Even if you do, the owning page table changes, right? In public mode it has to be mapped into an ioas and in private mode there is no ioas mapping?
Seems to me that two DMABUFs is easier than trying to teach DMABUf about some new attribute..
Userpsace can unmap all the shared DMABUFs, do a TDI BIND, then map private DMABUFs. DMABUFs do not change from private to shared on the fly.
Invalidation is only an error case situation to revoke if userspace didn't do the above sequence right.
Is that reasonable???
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 11 lines · Jason Gunthorpe
Quoted 8 lines · Xu Yilun
Quoted 3 lines
Except it doesn't work like that for MMIO. Shared/private is a TDI operation only and effects the whole device. We shouldn't split it into two actions.
OK. IIUC you want 1 uAPI, TSM Bind, to finish all secure configuration, including MMIO sharebility. I think it is possible, the MMIO shareability is fixed after TSM Bind. iommufd could fetch TDI report to get the private/shared MMIO ranges and callback to VFIO.
Am I wrong? Isn't this one action? Once you do bind you MUST have no
You are right and I'm good to this one operation.
Quoted 13 lines
hypervisor mapping or x86 will explode. And once bind completes the hypervisor mapping is unusable on all other arches.
That's one operation as far as I can tell??
Quoted 5 lines
Quoted 1 line
I also don't think it needs to be strictly 'in-place' as we expect the
When I said "must be in-place", I mean the MMIO resource (hpa) for one gfn is fixed, can't have 2 copies of backend as the current private/shared memory does.
Ok, I would not call that in place..
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
VM to be idle on the MMIO during this change over. Faulting would be OK.
Sorry, I don't get your point about 'strictly in-place' here?
Here I mean the iommu page table would have to atomically change "in place" from shared to private in a way that that is hitless to the guest. We don't need this, IMHO.
I agree.
Quoted 25 lines
Quoted 12 lines
Quoted 11 lines
Any case where we need to get back to vfio for something needs to be managed with a callback of some kind. We need to get a list of what those things are.
What do all the arches need here?
- ARM I suspect has the TDI locking operation install the MMIO in the secure S2, not KVM?
- AMD just leaves the MMIO mapped all the time?
- x86 presumably needs to carefully map/unmap to KVM and iommu in the right sequence or you get a MCE?Quoted 2 lines
Yeah, for Intel TDX, basically 2 things, zap the mapping on opposite side page table, mark the correct shareability for correct fault in.
I expect userspace to be doing this, which is why I asked about two DMABUFs..
Quoted 7 lines
Quoted 2 lines
So what is the plan? You want to wrap this in DMABUF, but will there be two DMABUFS, one for secure and one for non-secure? Is userspace
No I don't expect 2 dmabufs. I image shareability could be an physical attribute of dmabuf and the callback to VFIO changes the shareability. And VFIO, the dmabuf exporter, could notify KVM/iommufd about the shareability change. Then KVM/iommufd unmaps their page tables.
Quoted 4 lines
How can this work exactly?
Does Intel put shared/private MMIO at the same guest address when it changes around? I understand other arches do not do this.
No, The Shared bit is embedded in GPA, it toggles for shared/private change.
Quoted 4 lines
Even if you do, the owning page table changes, right? In public mode it has to be mapped into an ioas and in private mode there is no ioas mapping?
Yes.
Quoted 7 lines
Seems to me that two DMABUFs is easier than trying to teach DMABUf about some new attribute..
Userpsace can unmap all the shared DMABUFs, do a TDI BIND, then map private DMABUFs. DMABUFs do not change from private to shared on the fly.
But the shareability of each MMIO pfn should still be recorded at the time of TDI BIND. Shareability is not only about hypervisor can map the MMIO or not, it is mainly about Guest should access it in a shared or private manner. According to this KVM map the pfn in EPT or S-EPT.
For MMIO, the shareability layout is the TDI's inherence which can be get from TDI report. I.e. some MMIOs must still be accessed as shared by guest even if the device is converted to private. So I think a private DMABUF without sharebility layout can't support the TDI case.
The existing shareability layout for all guest memory space is recorded in KVM via KVM_SET_MEMORY_ATTRIBUTES, but now Sean's suggestion is giving it back to resource owners when in-place conversion makes guest_memfd fully aware of the shareability layout.
Thanks,
Yilun
Quoted 7 lines
Invalidation is only an error case situation to revoke if userspace didn't do the above sequence right.
Is that reasonable???
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 7 lines · Xu Yilun
Quoted 6 lines
Seems to me that two DMABUFs is easier than trying to teach DMABUf about some new attribute..
Userpsace can unmap all the shared DMABUFs, do a TDI BIND, then map private DMABUFs. DMABUFs do not change from private to shared on the fly.
Quoted 6 lines
But the shareability of each MMIO pfn should still be recorded at the time of TDI BIND.
Shareability is not only about hypervisor can map the
MMIO or not, it is mainly about Guest should access it in a shared or private manner. According to this KVM map the pfn in EPT or S-EPT.
Yes, which is why having two dmabufs might be nice because you can plug the public one into the EPT and the private one into the S-EPT and SW can validate the right one is in the right place.
Quoted 4 lines
For MMIO, the shareability layout is the TDI's inherence which can be get from TDI report. I.e. some MMIOs must still be accessed as shared by guest even if the device is converted to private. So I think a private DMABUF without sharebility layout can't support the TDI case.
IMHO this even more strongly says two DMABUFs. After binding you'd get a new shared DMABUF that was limited to only the actual shared pages while the private DMABUF would be limited to only the actual private pages.
It is much simpler considering the current DMABUF APIs than trying to convey per-pfn shared/private indication.
Quoted 4 lines
The existing shareability layout for all guest memory space is recorded in KVM via
KVM_SET_MEMORY_ATTRIBUTES, but now Sean's suggestion is giving it back to resource owners when in-place conversion makesguest_memfdfully aware of the shareability layout.
I would say this discussion is irrelevant to this case since we are not doing any kind of in-place conversion.
1) At VM start the VMM gets the shared DMABUF and maps it to IOAS and EPT
2) Bind request comes in, VMM unmaps shared DMABUF from IOAS and EPT then closes it.
3) Bind is done
4) VMM opens a shared and private DMABUF FD and learns the valid ranges in both DMABUFs (ie what PFNS are private/shared)
5) VMM maps the shared DMABUF fragments to the EPT and IOAS
6) VMM maps the private DMABUF fragments to the S-EPT
7) Unbind request comes in, VMM unmaps both shared and private DMABUFS
For all error cases the kernel revokes all open DMABUFS and userspace is expected to close & reopen them to recover.
From a KVM perspective when you tell it to map the DMABUF the VMM will also tell it the shared/private, which is basically implied by the GPA. There is no "conversion", userspace will destroy the memslot and create new ones.
Isn't this pretty simple?
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 24 lines · Jason Gunthorpe
Quoted 7 lines · Xu Yilun
Quoted 6 lines
Seems to me that two DMABUFs is easier than trying to teach DMABUf about some new attribute..
Userpsace can unmap all the shared DMABUFs, do a TDI BIND, then map private DMABUFs. DMABUFs do not change from private to shared on the fly.
Quoted 6 lines
But the shareability of each MMIO pfn should still be recorded at the time of TDI BIND.
Shareability is not only about hypervisor can map the
MMIO or not, it is mainly about Guest should access it in a shared or private manner. According to this KVM map the pfn in EPT or S-EPT.Yes, which is why having two dmabufs might be nice because you can plug the public one into the EPT and the private one into the S-EPT and SW can validate the right one is in the right place.
Quoted 4 lines
For MMIO, the shareability layout is the TDI's inherence which can be get from TDI report. I.e. some MMIOs must still be accessed as shared by guest even if the device is converted to private. So I think a private DMABUF without sharebility layout can't support the TDI case.
Quoted 7 lines
IMHO this even more strongly says two DMABUFs. After binding you'd get a new shared DMABUF that was limited to only the actual shared pages while the private DMABUF would be limited to only the actual private pages.
It is much simpler considering the current DMABUF APIs than trying to convey per-pfn shared/private indication.
I generally think the flow you describe is good for implementation. But still some details, see below.
Quoted 9 lines
Quoted 5 lines
The existing shareability layout for all guest memory space is recorded in KVM via
KVM_SET_MEMORY_ATTRIBUTES, but now Sean's suggestion is giving it back to resource owners when in-place conversion makesguest_memfdfully aware of the shareability layout.I would say this discussion is irrelevant to this case since we are not doing any kind of in-place conversion.
Quoted 7 lines
1) At VM start the VMM gets the shared DMABUF and maps it to IOAS and EPT
2) Bind request comes in, VMM unmaps shared DMABUF from IOAS and EPT then closes it.
3) Bind is done
4) VMM opens a shared and private DMABUF FD and learns the valid ranges in both DMABUFs (ie what PFNS are private/shared)
Who decides the dmabuf be shared or private? I assume you mean userspace, VFIO doesn't know about the shareability of each dmabuf?
Quoted 1 line
5) VMM maps the shared DMABUF fragments to the EPT and IOAS
Userspace could set a shared only memory slot.
Quoted 1 line
6) VMM maps the private DMABUF fragments to the S-EPT
Userspace could set a private only memory slot, but maybe the concern is KVM can't trust userspace about the assertion of private. Like for private memory, KVM now verifies the slot is backed by gmem before allowing the slot to be private. This is some in-kernel contract. Without the contract, userspace could assign arbitrary memory to KVM as private and explode. If VFIO is not aware of the shareability, KVM - VFIO can't build the contract.
If VFIO should be aware of the shareability, IOMMUFD to VFIO callback is needed after TSM bind.
Quoted 11 lines
7) Unbind request comes in, VMM unmaps both shared and private DMABUFS
For all error cases the kernel revokes all open DMABUFS and userspace is expected to close & reopen them to recover.
From a KVM perspective when you tell it to map the DMABUF the VMM will also tell it the shared/private, which is basically implied by the GPA. There is no "conversion", userspace will destroy the memslot and create new ones.
Isn't this pretty simple?
It is simpler on kernel side. FYR, may introduce some complexity in userspace, this may result in 3+ dmabufs. There may be shared holes in the BAR, and kvm slot can't be gfn sparse. This may not be a big problem.
Thanks,
Yilun
Quoted 2 lines
Jason
Jason Gunthorpe
maintainerRe: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 11 lines · Xu Yilun
Quoted 10 lines
I would say this discussion is irrelevant to this case since we are not doing any kind of in-place conversion.
1) At VM start the VMM gets the shared DMABUF and maps it to IOAS and EPT
2) Bind request comes in, VMM unmaps shared DMABUF from IOAS and EPT then closes it.
3) Bind is done
4) VMM opens a shared and private DMABUF FD and learns the valid ranges in both DMABUFs (ie what PFNS are private/shared)
Quoted 2 lines
Who decides the dmabuf be shared or private? I assume you mean userspace, VFIO doesn't know about the shareability of each dmabuf?
I imagined userspace would put a flag in the ioctl to VFIO to get the dmabuf kind it wants. VFIO would have to be told from iommufd what the per-pfn shared/private layout is and can use that to manage the dmabufs.
Quoted 8 lines
Quoted 1 line
5) VMM maps the shared DMABUF fragments to the EPT and IOAS
Userspace could set a shared only memory slot.
Quoted 1 line
6) VMM maps the private DMABUF fragments to the S-EPT
Userspace could set a private only memory slot, but maybe the concern is KVM can't trust userspace about the assertion of private.
I imagined kvm can query the dmabuf and learn it is all private from VFIO. The whole dmabuf, not per-pfn. Same for #5 it can check it is shared memory too.
I sort of imagine using the same mechanism for p2p where we can mark memory in the dmabuf with a 'provider' indicating these details. We will have to see.
Quoted 2 lines
If VFIO should be aware of the shareability, IOMMUFD to VFIO callback is needed after TSM bind.
Yes, whever this list of per-PFN shared/private must be shared between VFIO and the TSM.
Quoted 4 lines
It is simpler on kernel side. FYR, may introduce some complexity in userspace, this may result in 3+ dmabufs. There may be shared holes in the BAR, and kvm slot can't be gfn sparse. This may not be a big problem.
Userspace probably needs to create multiple slots for sparsity
Again this seems much better if userspace handles it and just uses simple mostly existing KVM interfaces.
Why 3+ dmabufs? Isn't just one shared and one private per BAR?
Jason
Re: [RFC PATCH 3/3] iommufd/tsm: Add tsm_bind/unbind iommufd ioctls
Quoted 24 lines · Jason Gunthorpe
Quoted 11 lines · Xu Yilun
Quoted 10 lines
I would say this discussion is irrelevant to this case since we are not doing any kind of in-place conversion.
1) At VM start the VMM gets the shared DMABUF and maps it to IOAS and EPT
2) Bind request comes in, VMM unmaps shared DMABUF from IOAS and EPT then closes it.
3) Bind is done
4) VMM opens a shared and private DMABUF FD and learns the valid ranges in both DMABUFs (ie what PFNS are private/shared)Quoted 2 lines
Who decides the dmabuf be shared or private? I assume you mean userspace, VFIO doesn't know about the shareability of each dmabuf?
I imagined userspace would put a flag in the ioctl to VFIO to get the dmabuf kind it wants. VFIO would have to be told from iommufd what the per-pfn shared/private layout is and can use that to manage the dmabufs.
Quoted 4 lines
Quoted 1 line
5) VMM maps the shared DMABUF fragments to the EPT and IOAS
Userspace could set a shared only memory slot.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
6) VMM maps the private DMABUF fragments to the S-EPT
Userspace could set a private only memory slot, but maybe the concern is KVM can't trust userspace about the assertion of private.
I imagined kvm can query the dmabuf and learn it is all private from VFIO. The whole dmabuf, not per-pfn. Same for #5 it can check it is shared memory too.
Yes.
On KVM side, userspace should firstly indicate the slot type in the ioctl and KVM verifies. Nowadays, KVM accepts 'private slot', which can be private or shared. And legacy slot, which can only be shared. We need to introduce a new type 'private only slot', then KVM verifies against dma-buf. We will see how KVM folks think about it.
Quoted 4 lines
I sort of imagine using the same mechanism for p2p where we can mark memory in the dmabuf with a 'provider' indicating these details. We will have to see.
Yes.
Quoted 12 lines
Quoted 3 lines
If VFIO should be aware of the shareability, IOMMUFD to VFIO callback is needed after TSM bind.
Yes, whever this list of per-PFN shared/private must be shared between VFIO and the TSM.
Quoted 4 lines
It is simpler on kernel side. FYR, may introduce some complexity in userspace, this may result in 3+ dmabufs. There may be shared holes in the BAR, and kvm slot can't be gfn sparse. This may not be a big problem.
Quoted 6 lines
Userspace probably needs to create multiple slots for sparsity
Again this seems much better if userspace handles it and just uses simple mostly existing KVM interfaces.
Why 3+ dmabufs? Isn't just one shared and one private per BAR?
Sorry, we must have 3+ KVM slots, but can have 2 dmabufs. We can map the file offset to pfn ranges.
KVM slot can't be for sparse GFNs. If there is a shared hole inside private space in the same bar, e.g.
0x10000000 - 0x10001fff private 0x10002000 - 0x10002fff shared 0x10003000 - 0x101fffff private
We need 3 slots.
Thanks,
Yilun
Quoted 2 lines
Jason
Alexey Kardashevskiy
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
In reply to Aneesh Kumar K.VQuoted 66 lines · Aneesh Kumar K.V
Quoted 64 lines · Alexey Kardashevskiy
Quoted 61 lines · Aneesh Kumar K.V
Quoted 47 lines · Alexey Kardashevskiy
Quoted 39 lines · Aneesh Kumar K.V
Quoted 37 lines · Alexey Kardashevskiy
Quoted 36 lines · Aneesh Kumar K.V
Quoted 33 lines · Xu Yilun
Quoted 21 lines · Aneesh Kumar K.V
Quoted 19 lines · Xu Yilun
Quoted 17 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.Quoted 6 lines
Quoted 3 lines
To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Since the bind/unbind operations are PCI-specific callbacks, and iommufd
Not really, it is PCI-specific in TSM (for DOE) but since IOMMUFD is not doing any of that, it can work with struct device (not
pci_dev). Thanks,Ok, something like this? and iommufd will call
tsm_bind()?Quoted 8 lines
yeah, I guess, there is a couple of places like this
git grep
pci_devdrivers/iommu/iommufd/drivers/iommu/iommufd/device.c: struct pci_dev *pdev = to_pci_dev(idev->dev); drivers/iommu/iommufd/eventq.c: struct pci_dev *pdev = to_pci_dev(dev);Although I do not see any compelling reason to have
pci_devin the TSM API, struct device should just work and not spill any PCI details to IOMMUFD but whatever... Thanks,Getting the kvm reference is tricky here. Also the locking while updating
vdevice->tsm_boundneeds some solution. Here is what I am improving. Are you also planning something similar?
Quoted 8 lines
Quoted 5 lines
At the moment I am planning getting/holding the KVM reference in the TSM:
https://lore.kernel.org/r/[email protected]
but may push it even further to the AMD TSM (CCP, the firmware driver) as this where I actually need the kvm struct to get GCTX+ASID from
kvm_svm; Intel folks have a similar intimate knowledge sharing betweenkvm_inteland TDX-connect. Thanks,So you won't be able to work with already available kvm reference in viommu alloc?
By "already" you mean 2/3 you posted in reply to this, do not you? :)
Quoted 3 lines
I will send the
tsm_bindchanges i have done so that we can share the diff against that with explanation of why things can't work that way?
I am missing the point in having a kvm pointer in iommufd which does not do anything with the kvm struct, fget(kvm_fd) should be enough. Thanks,
Quoted 2 lines
-aneesh
Signature · 1 line
Alexey
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
In reply to Aneesh Kumar K.VQuoted 39 lines · Aneesh Kumar K.V
Quoted 37 lines · Alexey Kardashevskiy
Quoted 36 lines · Aneesh Kumar K.V
Quoted 33 lines · Xu Yilun
Quoted 21 lines · Aneesh Kumar K.V
Quoted 19 lines · Xu Yilun
Quoted 17 lines · Dan Williams
From: Xu Yilun <[email protected]>
Add kAPIs pci_tsm_{bind,unbind,guest_req}() for PCI devices.
pci_tsm_bind/unbind() are supposed to be called by kernel components which manages the virtual device. The verb 'bind' means VMM does extra configurations to make the assigned device ready to be validated by CoCo VM as TDI (TEE Device Interface). Usually these configurations include assigning device ownership and MMIO ownership to CoCo VM, and move the TDI to
CONFIG_LOCKEDTDISP state byLOCK_INTERFACE_REQUESTTDISP message. The detailed operations are specific to platform TSM firmware so need to be supported by vendor TSM drivers.
pci_tsm_guest_req()supports a channel for CoCo VM to directly talk to TSM firmware about further TDI operations after TDI is bound, e.g. get device interface report, certifications & measurements. So this kAPI is supposed to be called from KVM vmexit handler.Quoted 6 lines
Quoted 3 lines
To clarify, this commit message is staled. We are proposing existing to QEMU, then pass to TSM through IOMMUFD VDEVICE.
Can you share the POC code/git repo implementing that? I am looking for pci_tsm_bind()/pci_tsm_unbind() example usage.
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Since the bind/unbind operations are PCI-specific callbacks, and iommufd
Not really, it is PCI-specific in TSM (for DOE) but since IOMMUFD is not doing any of that, it can work with struct device (not
pci_dev). Thanks,Ok, something like this? and iommufd will call
tsm_bind()?
Remember that there may be other devices, AMBA CHI based devices being assigned. Not sure if they pretend to be PCI or not.
Cheers
Suzuki
Quoted 21 lines
int
tsm_bind(struct device *dev, struct kvm *kvm, u64 tdi_id){ if (!dev_is_pci(dev)) return -EINVAL; return pci_tsm_bind(to_pci_dev(dev), kvm, tdi_id); }
EXPORT_SYMBOL_GPL(tsm_bind);int
tsm_unbind(struct device *dev){ if (!dev_is_pci(dev)) return -EINVAL; return pci_tsm_unbind(to_pci_dev(dev)); }
EXPORT_SYMBOL_GPL(tsm_unbind);-aneesh
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Suzuki K Poulose wrote:
[..]
Quoted 4 lines
Quoted 1 line
Ok, something like this? and iommufd will call
tsm_bind()?Remember that there may be other devices, AMBA CHI based devices being assigned. Not sure if they pretend to be PCI or not.
I have been thinking about this especially with the relative ease of creating samples/devsec/ given the existing Linux infrastructure emulating PCI host bridges.
Why not require PCI emulation for non-PCI devices? The tipping point is whether the relative maintenance burden of not needing to maintain multi-bus Device Security infrastructure outweighs the complexity of impedance matching those other buses to PCI.
Make "PCI" the lingua franca of Device Security.
Alexey Kardashevskiy
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 11 lines · Dan Williams
Suzuki K Poulose wrote:
[..]Quoted 4 lines
Quoted 1 line
Ok, something like this? and iommufd will call
tsm_bind()?Remember that there may be other devices, AMBA CHI based devices being assigned. Not sure if they pretend to be PCI or not.
I have been thinking about this especially with the relative ease of creating
samples/devsec/given the existing Linux infrastructure emulating PCI host bridges.
Quoted 6 lines
Why not require PCI emulation for non-PCI devices? The tipping point is whether the relative maintenance burden of not needing to maintain multi-bus Device Security infrastructure outweighs the complexity of impedance matching those other buses to PCI.
Make "PCI" the lingua franca of Device Security.
This is how virtio started, and now it has to behave like a proper PCI device, i.e. use DMA API. Or ivshmem which maps memory as "PCI" (which it is not PCI but the guest does not know it) and is deprecated now. Not the best idea to enforce PCI from day1 imho.
Signature · 1 line
Alexey
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 19 lines · Alexey Kardashevskiy
Quoted 16 lines · Dan Williams
Suzuki K Poulose wrote:
[..]Quoted 4 lines
Quoted 1 line
Ok, something like this? and iommufd will call
tsm_bind()?Remember that there may be other devices, AMBA CHI based devices being assigned. Not sure if they pretend to be PCI or not.
I have been thinking about this especially with the relative ease of creating
samples/devsec/given the existing Linux infrastructure emulating PCI host bridges.Why not require PCI emulation for non-PCI devices? The tipping point is whether the relative maintenance burden of not needing to maintain multi-bus Device Security infrastructure outweighs the complexity of impedance matching those other buses to PCI.
Quoted 6 lines
Quoted 1 line
Make "PCI" the lingua franca of Device Security.
This is how virtio started, and now it has to behave like a proper PCI device, i.e. use DMA API. Or ivshmem which maps memory as "PCI" (which it is not PCI but the guest does not know it) and is deprecated now. Not the best idea to enforce PCI from day1 imho.
VFIO is a Linux convention. PCIe TDISP is an industry standard protocol. The goal here is not have platform leak "oh, but my bus is special" details throughout the tree vs keep that limited to the PCIe bus adapter drivers for these things that want to speak a TDISP-ish like language.
That said it is difficult to speak in the abstract and the proof needs to be in demonstrating that "tipping point" I mention above has been reached.
As it is, we already have our hands full with the industry standard mechanism for the foreseeable future.
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 20 lines · Dan Williams
Quoted 19 lines · Alexey Kardashevskiy
Quoted 16 lines · Dan Williams
Suzuki K Poulose wrote:
[..]Quoted 4 lines
Quoted 1 line
Ok, something like this? and iommufd will call
tsm_bind()?Remember that there may be other devices, AMBA CHI based devices being assigned. Not sure if they pretend to be PCI or not.
I have been thinking about this especially with the relative ease of creating
samples/devsec/given the existing Linux infrastructure emulating PCI host bridges.Why not require PCI emulation for non-PCI devices? The tipping point is whether the relative maintenance burden of not needing to maintain multi-bus Device Security infrastructure outweighs the complexity of impedance matching those other buses to PCI.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
Make "PCI" the lingua franca of Device Security.
This is how virtio started, and now it has to behave like a proper PCI device, i.e. use DMA API. Or ivshmem which maps memory as "PCI" (which it is not PCI but the guest does not know it) and is deprecated now. Not the best idea to enforce PCI from day1 imho.
VFIO is a Linux convention. PCIe TDISP is an industry standard protocol.
Oh, sorry you said "virtio" not "vfio", but the point is still that we have not even got one implementation of a bus Device Security protocol upstream, let alone multiple.
Alexey Kardashevskiy
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 23 lines · Dan Williams
Quoted 22 lines · Dan Williams
Quoted 21 lines · Alexey Kardashevskiy
Quoted 17 lines · Dan Williams
Suzuki K Poulose wrote:
[..]Quoted 4 lines
Quoted 1 line
Ok, something like this? and iommufd will call
tsm_bind()?Remember that there may be other devices, AMBA CHI based devices being assigned. Not sure if they pretend to be PCI or not.
I have been thinking about this especially with the relative ease of creating
samples/devsec/given the existing Linux infrastructure emulating PCI host bridges.Why not require PCI emulation for non-PCI devices? The tipping point is whether the relative maintenance burden of not needing to maintain multi-bus Device Security infrastructure outweighs the complexity of impedance matching those other buses to PCI.
Make "PCI" the lingua franca of Device Security.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
This is how virtio started, and now it has to behave like a proper PCI device, i.e. use DMA API. Or ivshmem which maps memory as "PCI" (which it is not PCI but the guest does not know it) and is deprecated now. Not the best idea to enforce PCI from day1 imho.
VFIO is a Linux convention. PCIe TDISP is an industry standard protocol.
Oh, sorry you said "virtio" not "vfio",
"virtio" is just not a Linux convention, Windows (at least guests) uses it, and there were even punks developing physical devices implementing virtio, hence the recommendation of iommu_platform=on in QEMU command line for virtio devices.
Quoted 3 lines
but the point is still that we
have not even got one implementation of a bus Device Security protocol upstream, let alone multiple.
And my point is that TSM does not actually do anything with PCI except SPDM/DOE which can happily live in a library or DOE (and called from CCP or TDX drivers) and the rest can be just "device", not "pci_dev". I wonder if+how nailing TSM to PCI makes your life somehow easier, it is not going to help my case. Thanks,
Signature · 1 line
Alexey
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Alexey Kardashevskiy wrote:
[..]
Quoted 9 lines
Quoted 3 lines
but the point is still that we
have not even got one implementation of a bus Device Security protocol upstream, let alone multiple.And my point is that TSM does not actually do anything with PCI except SPDM/DOE which can happily live in a library or DOE (and called from CCP or TDX drivers) and the rest can be just "device", not "
pci_dev". I wonder if+how nailing TSM to PCI makes your life somehow easier, it is not going to help my case. Thanks,
The goal is not to solve Alexey's case, the goal is to solve the TDISP enabling problem in a way that all impacted subsystem owners (PCI, Device core, DMA, IOMMU, VFIO/IOMMUFD, KVM, CPU arch/...), and all TSM platform vendors are willing to accept.
"TSM" is literally a PCI-introduced term. It comes with a full device-model and state machine for a protocol that we, OS practitioners, have a chance to agree what it means. If another bus wants to do "Device Security" ideally it would map as a strict subset of the TDISP model. If / when that happens it is easy enough for userspace to see "oh hey, bus $foo now has tsm/connect and tsm/accept mechanisms too".
Just like the evolution of the "new_id / remove_id, and authorized" bus attributes, other buses add workalike functionality as a matter of course. Other buses can add "TSM" mechanisms to their device model, "TSM" is not a device model unto itself. Similarly, nothing stops 'struct pci_dev' properties to be promoted to 'struct device' when needed.
I note IOMMMUFD has the bulk of all the interesting cross-bus shared work to do here and it already has a generic device object model for that purpose. It is another example of "extend existing objects with 'Device Security' properties", not "add a new tdi_dev object to manage".
I am frustrated that we are still spinning in this philosophical debate. In terms of progress towards the goal of "shared commons that all impacted subsystem owners are willing to accept":
* Bjorn acked the PCI/TSM approach [1]
* Lukas is doing native CMA and SPDM work that may yield a shared transport for multiple use cases (SPDM/CMA and TDISP) [2]
* Greg gave a nod to the PCI/TSM staging approach [3].
* Aneesh and Suzuki are helping out with ideas [4], and fixes to move this forward [5]
This is not a competition, this is carrying a shared upstream burden by consensus for the benefit of ecosystem use cases.
[1]: http://lore.kernel.org/20240419220729.GA307280@bhelgaas
[2]: https://github.com/l1k/linux/commits/doe
[3]: http://lore.kernel.org/2024120625-baggage-balancing-48c5@gregkh
[4]: http://lore.kernel.org/[email protected]
[5]: http://lore.kernel.org/[email protected]
Alexey Kardashevskiy
maintainerRe: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 12 lines · Dan Williams
Alexey Kardashevskiy wrote:
[..]Quoted 9 lines
Quoted 3 lines
but the point is still that we
have not even got one implementation of a bus Device Security protocol upstream, let alone multiple.And my point is that TSM does not actually do anything with PCI except SPDM/DOE which can happily live in a library or DOE (and called from CCP or TDX drivers) and the rest can be just "device", not "
pci_dev". I wonder if+how nailing TSM to PCI makes your life somehow easier, it is not going to help my case. Thanks,
Quoted 4 lines
The goal is not to solve Alexey's case, the goal is to solve the TDISP enabling problem in a way that all impacted subsystem owners (PCI, Device core, DMA, IOMMU, VFIO/IOMMUFD, KVM, CPU
arch/...), and all TSM platform vendors are willing to accept.
Right, so I need to understand how this TSM makes your life easier. I did show my complete solution, still waiting to see yours or any other really. For example, DOE bouncing.
Quoted 6 lines
"TSM" is literally a PCI-introduced term. It comes with a full device-model and state machine for a protocol that we, OS practitioners, have a chance to agree what it means. If another bus wants to do "Device Security" ideally it would map as a strict subset of the TDISP model. If / when that happens it is easy enough for userspace to see "oh hey, bus $foo now has tsm/connect and tsm/accept mechanisms too".
Quite a chunk of it is in the SPDM specs which have all sorts of bindings. No strict PCI.
VFIO started as PCI and look at it now with all these platform and mediated devices.
Quoted 7 lines
Just like the evolution of the "
new_id/remove_id, and authorized" bus attributes, other buses add workalike functionality as a matter of course. Other buses can add "TSM" mechanisms to their device model, "TSM" is not a device model unto itself. Similarly, nothing stops 'structpci_dev' properties to be promoted to 'struct device' when needed.
Quoted 6 lines
I note IOMMMUFD has the bulk of all the interesting cross-bus shared work to do here and it already has a generic device object model for that purpose. It is another example of "extend existing objects with 'Device Security' properties", not "add a new
tdi_devobject to manage".I am frustrated that we are still spinning in this philosophical debate.
That's because I did not do very good job explaining my TSM, my bad, I'll do better, it is too bloated now, and violates sysfs, and should integrate with Lukas'es work, my bad.
But having this all in a single built-in (1) with PCI nailed down (2), globals (3), one tsm_ops struct for both host and guest - this frustrates me.
(1) means annoyingly many reboots vs rmmod+modprobe
(2) TSM does not IDE (the platform calls the IDE library) and does not do DOE (the DOE library should, called by the platform)
(3) bites every time when there are development bugs
(4) leads to ugly "if (tvm_mode())" checks and bugs (when missed), been there, done that with my first TSM, did not like it.
1/2/3/5 are not necessary, do not really make anything simpler and most likely will requite untangling later.
Say, there are assumptions already made for IDE which I believe we do not have to make (like, same number association blocks in all streams) but it is internal IDE detail, can be changed later if needed, but the API is sane so I am ok with the limitations (thanks btw!). But the TSM just is not there yet imho. Hope it all makes sense. Trying now to move to v6.16-rc1 + dmabuf + this series as we speak so you'll hear from me soon :) Thanks,
Quoted 16 lines
In terms of progress towards the goal of "shared commons that all impacted subsystem owners are willing to accept":
Quoted 1 line
* Bjorn acked the PCI/TSM approach [1]
* Lukas is doing native CMA and SPDM work that may yield a shared transport for multiple use cases (SPDM/CMA and TDISP) [2]
* Greg gave a nod to the PCI/TSM staging approach [3].
* Aneesh and Suzuki are helping out with ideas [4], and fixes to move this forward [5]This is not a competition, this is carrying a shared upstream burden by consensus for the benefit of ecosystem use cases.
[1]: http://lore.kernel.org/20240419220729.GA307280@bhelgaas
[2]: https://github.com/l1k/linux/commits/doe
[3]: http://lore.kernel.org/2024120625-baggage-balancing-48c5@gregkh
[4]: http://lore.kernel.org/[email protected]
[5]: http://lore.kernel.org/[email protected]
Signature · 1 line
Alexey
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
Quoted 15 lines · Alexey Kardashevskiy
Quoted 12 lines · Dan Williams
Alexey Kardashevskiy wrote:
[..]Quoted 9 lines
Quoted 3 lines
but the point is still that we
have not even got one implementation of a bus Device Security protocol upstream, let alone multiple.And my point is that TSM does not actually do anything with PCI except SPDM/DOE which can happily live in a library or DOE (and called from CCP or TDX drivers) and the rest can be just "device", not "
pci_dev". I wonder if+how nailing TSM to PCI makes your life somehow easier, it is not going to help my case. Thanks,
Quoted 6 lines
Quoted 4 lines
The goal is not to solve Alexey's case, the goal is to solve the TDISP enabling problem in a way that all impacted subsystem owners (PCI, Device core, DMA, IOMMU, VFIO/IOMMUFD, KVM, CPU
arch/...), and all TSM platform vendors are willing to accept.Right, so I need to understand how this TSM makes your life easier.
The goal is make other subsystem owner's lives easier.
I read Bjorn's ack as "oh hey, you added new PCI/TSM functionality in the same way all other new PCI device capabilities get added with lifetime bounded by pci_init_capabilities() and pci_destroy_dev(), and the sysfs attributes follow normal PCI device rules too". I also expect I have an implied ack from Lukas who also does the same lifetime for native authentication.
Now, they are free to change their mind if new information makes them rethink this, but I have been operating under the assumption this question is settled. I read ARM folks as tentatively on board as well. Greg did not balk yet either.
I expect other subsystem maintainers want to see vendor consensus before weighing. "Ongoing debate" almost always == "wait".
Quoted 2 lines
I did show my complete solution, still waiting to see yours or any other really.
This is a fair criticism and I hope to share more soon in the meantime Yilun and I have been working on the skeleton pieces and samples/devsec/ to unit test the proposals.
Quoted 1 line
For example, DOE bouncing.
The tsm operations and bind have been taking up all of the focus. What are the specific concerns around DOE bouncing proposal? The only criteria I currently have in mind is:
* if there is common boilerplate lets make that a library helper
* a TDISP operation / state change should be atomic regardless of how many DOE messages are involved.
Quoted 8 lines
Quoted 6 lines
"TSM" is literally a PCI-introduced term. It comes with a full device-model and state machine for a protocol that we, OS practitioners, have a chance to agree what it means. If another bus wants to do "Device Security" ideally it would map as a strict subset of the TDISP model. If / when that happens it is easy enough for userspace to see "oh hey, bus $foo now has tsm/connect and tsm/accept mechanisms too".
Quite a chunk of it is in the SPDM specs which have all sorts of bindings. No strict PCI.
Agree, but most of the interesting potential for shared code there is buried within the TSM implementations with TDISP we mostly sit behind TSM calls.
I note that Lukas has spdm common code in lib/spdm/, but all of protocol work there is not usable for TDISP since TSMs run that protocol.
Quoted 1 line
VFIO started as PCI and look at it now with all these platform and mediated devices.
Yes, and VFIO still has specific support for PCI semantics... but the meat of your concern is below.
Quoted 12 lines
Quoted 12 lines
Just like the evolution of the "
new_id/remove_id, and authorized" bus attributes, other buses add workalike functionality as a matter of course. Other buses can add "TSM" mechanisms to their device model, "TSM" is not a device model unto itself. Similarly, nothing stops 'structpci_dev' properties to be promoted to 'struct device' when needed.I note IOMMMUFD has the bulk of all the interesting cross-bus shared work to do here and it already has a generic device object model for that purpose. It is another example of "extend existing objects with 'Device Security' properties", not "add a new
tdi_devobject to manage".
Quoted 5 lines
Quoted 1 line
I am frustrated that we are still spinning in this philosophical debate.
That's because I did not do very good job explaining my TSM, my bad, I'll do better, it is too bloated now, and violates sysfs, and should integrate with Lukas'es work, my bad.
No need to apologize, this stuff is complicated and I needed significant changes from v1 to v2, getting better for v3, but v4 still needed.
Quoted 3 lines
But having this all in a single built-in (1) with PCI nailed down (2), globals (3), one
tsm_opsstruct for both host and guest - this frustrates me.
These are good review comments, and need to be addressed.
Quoted 1 line
(1) means annoyingly many reboots vs rmmod+modprobe
Once you buy the idea that a PCI device capability should have lifetime bounded by pci_init_capabilities()/pci_destroy_dev() and attributes defined by pci_dev_attr_groups() then the built-in design comes along for the ride.
I do not agree that this near term developer ergonomics issue, for this core infrastructure piece that will eventually settle and be slow moving thereafter, should affect the long term design.
That was also another motivation for samples/devsec/, i.e. stand up an environment that can quickly target different corners of the TSM stack by restarting QEMU.
Quoted 2 lines
(2) TSM does not IDE (the platform calls the IDE library) and does not do DOE (the DOE library should, called by the platform)
The rationale for this organization is that IDE is not required. TSM may have knowledge that the link is secure by other means.
Quoted 1 line
(3) bites every time when there are development bugs
I take this as par for the course for PCI capability development. I also expect the bulk of the development complexity to live in the low-level TSM driver, not the TSM skeleton.
Quoted 1 line
(4) leads to ugly "if (
tvm_mode())" checks and bugs (when missed), been there, done that with my first TSM, did not like it.
Fair enough. I will take a look at replacing tvm_mode() with guest specific ops structure. It likely still ends up being the same ops signature though.
Quoted 10 lines
1/2/3/5 are not necessary, do not really make anything simpler and most likely will requite untangling later.
Say, there are assumptions already made for IDE which I believe we do not have to make (like, same number association blocks in all streams) but it is internal IDE detail, can be changed later if needed, but the API is sane so I am ok with the limitations (thanks btw!). But the TSM just is not there yet imho. Hope it all makes sense. Trying now to move to v6.16-rc1 + dmabuf + this series as we speak
Which dmabuf series are you trying to integrate? There is yours, Yilun's, and Aneesh was looking to publish a third.
Quoted 1 line
so you'll hear from me soon :) Thanks,
Sounds good, the whole goal of this is get to the point where 2 vendors agree and they can drag the 3rd vendor along.
Re: [PATCH v3 12/13] PCI/TSM: support TDI related operations for host TSM driver
In reply to Alexey KardashevskiyAlexey Kardashevskiy wrote:
[..]
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
The usage of these kAPIs should be in IOMMUFD, that's what I'm doing for Stage 2 patchset. I need to rebase this series, adopt suggestions from Jason, and make TDX Connect work to verify, so need more time...
Since the bind/unbind operations are PCI-specific callbacks, and iommufd
Not really, it is PCI-specific in TSM (for DOE) but since IOMMUFD is not doing any of that, it can work with struct device (not
pci_dev). Thanks,
Certainly the iommufd call signature will be in terms of iommufd_device, but that call ultimately needs to be bus aware, because "Device Security" is a bus specific protocol.