virtio-iommu: Introduce lazy mode domains and IOTLB_FLUSH request
PATCH 1/1 virtio-iommu: Introduce lazy mode domains and IOTLB_FLUSH request
At the moment the virtio-iommu offers MAP and UNMAP requests. This means that if the virtio-iommu protects a device with an iotlb we have no other choice but invalidating the device iotlb on UNMAP. This proposal aims at allowing the decoupling of UNMAP commands and domain IOTLB invalidations. This enables lazy mode where iotlb invalidations are deferred and batched. In that prospect we add a new VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature which, when offered allows an endpoint to be attached to a domain in lazy mode. Alongside we introduce a new IOTLB_FLUSH request which allows to invalidate iotlb entries for the whole domain. When the domain is created in lazy mode, the virtio-iommu device knows it will receive IOTLB_FLUSH and it can avoid flushing the device IOTLB on individual UNMAPs.
On a virtio-iommu/vhost scp use case, this can improve the performance by ~10%.
---
As a side not virtio-iommu/vhost/scp use case was completely broken on aarch64 with guests older than v6.6-rc1. The scp stalled and we got soft-lockups on guest in viommu_iotlb_sync. It happened 295525e29a5b (“virtio_net: merge dma operations when filling mergeable buffers”) fixed the performance issue. Without that patch and with lazy mode, we do not observe any stalls although the perf is way worse than with the above patch. Without lazy mode, it looks like the number of vhost iotlb invalidation is large enough to cause serious performance issues.
This proposal can be tested with the following branches
guest branch: https://github.com/eauger/linux/tree/virtio-iommu-lazy-rfc
qemu branch: https://github.com/eauger/qemu/tree/virtio-iommu-device-lazy-rfc
This was tested with vhost. In qemu command line, you need to set ats=on to get benefit of lazy mode.
--- device-types/iommu/description.tex | 74 +++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-)
device-types/iommu/description.tex +73 -1
@@ -74,6 +74,14 @@ \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} whether endpoints that are attached to a domain are in bypass mode. +\item[VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH (7)] + This feature determines the capability to use deferred and batched + IOTLB flushes. If the feature is available, the driver can inform + a given domain works in lazy mode. For such domain, the device is + allowed to dissociate UNMAP requests from IOTLB flush operations and + accepts separate IOTLB flush requests that invalidate the IOTLB entries + for the whole domain. + \end{description} \drivernormative{\subsubsection}{Feature bits}{Device Types / IOMMU Device / Feature bits}
@@ -312,6 +320,7 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op }; #define VIRTIO_IOMMU_ATTACH_F_BYPASS (1 << 0) +#define VIRTIO_IOMMU_ATTACH_F_LAZY (1 << 1) \end{lstlisting} Attach an endpoint to a domain. \field{domain} uniquely identifies a
@@ -341,6 +350,18 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op can set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag to create a bypass domain. Endpoints attached to this domain are in bypass mode. +When the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature is available, the driver +can set the VIRTIO_IOMMU_ATTACH_F_LAZY flag to create a lazy mode domain. +Only such domains do support the IOTLB_FLUSH request. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver sets \field{flags} field to VIRTIO_IOMMU_ATTACH_F_LAZY, the +device MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL. + +If several endpoints are attached to the same domain, they must be attached +with the same value for that flag. Otherwise the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL. + \drivernormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request} The driver SHOULD set \field{reserved} to zero.
@@ -351,7 +372,8 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op If the domain already exists and is a bypass domain, the driver SHOULD set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag. If the domain exists and is not a bypass domain, the driver SHOULD NOT set the -VIRTIO_IOMMU_ATTACH_F_BYPASS flag. +VIRTIO_IOMMU_ATTACH_F_BYPASS flag. The same holds for the +VIRTIO_IOMMU_ATTACH_F_LAZY flag. \devicenormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request}
@@ -374,6 +396,10 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op flag is not consistent with that domain, the device SHOULD reject the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. +If the domain already exists and the VIRTIO_IOMMU_ATTACH_F_LAZY +flag is not consistent with that domain, the device SHOULD reject +the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + If the endpoint identified by \field{endpoint} is already attached to another domain, then the device SHOULD first detach it from that domain and attach it to the one identified by \field{domain}. In that case the
@@ -590,6 +616,11 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope This request is only available when VIRTIO_IOMMU_F_MAP_UNMAP has been negotiated. +In case the domain is created with the VIRTIO_IOMMU_ATTACH_F_LAZY flag, +the device is allowed to separate UNMAP from IOTLB invalidations. The driver +must explicitly invalidate the whole domain IOTLB entries using the separate +IOTLB_FLUSH request. + \drivernormative{\paragraph}{UNMAP request}{Device Types / IOMMU Device / Device operations / UNMAP request} The driver SHOULD set the \field{reserved} field to zero.
@@ -625,6 +656,47 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope mapping, then the device SHOULD remove all mappings affected by the range and set the request \field{status} to VIRTIO_IOMMU_S_OK. +\subsubsection{IOTLB_FLUSH request}\label{sec:Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +\begin{lstlisting} +struct virtio_iommu_req_iotlb_flush { + struct virtio_iommu_req_head head; + le32 domain; + u8 reserved[4]; + struct virtio_iommu_req_tail tail; +}; +\end{lstlisting} + +Flush IOTLB entries for a given domain. This domain must have been created +with lazy mode set (VIRTIO_IOMMU_ATTACH_F_LAZY flag set on attachment). + +\drivernormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +The driver SHOULD set the \field{reserved} field to zero. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a bypass domain. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a non lazy domain. + +\devicenormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / UNMAP request} + +If the \field{reserved} field of an IOTLB_FLUSH request is not zero, the device +MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL, in which case +the device MAY perform the IOTLB_FLUSH operation. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver calls the IOTLB_FLUSH request, the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL. + +If \field{domain} does not exist, the device SHOULD set the request +\field{status} to VIRTIO_IOMMU_S_NOENT. + +If the domain is a bypass domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + +If the domain is not a lazy domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + \subsubsection{PROBE request}\label{sec:Device Types / IOMMU Device / Device operations / PROBE request} If the VIRTIO_IOMMU_F_PROBE feature bit is present, the driver sends a
Signature · 1 line
2.41.0
Quoted 6 lines · Eric Auger
At the moment the virtio-iommu offers MAP and UNMAP requests. This means that if the virtio-iommu protects a device with an iotlb we have no other choice but invalidating the device iotlb on UNMAP. This proposal aims at allowing the decoupling of UNMAP commands and domain IOTLB invalidations.
I think there should be some difference between "device iotlb" and "domain IOTLB". My understanding is that usually "device iotlb" means the mapping cached by the individual device while "domain IOTLB" means the mapping cached by the IOMMU.
E.g in the case of intel IOMMU, it has two kind of invalidation
device iotlb invalidation: used to invalidate device IOTLB via ATS domain selective invalidation: used to invalidate all IOTLB for a specific domain
Quoted 6 lines
This enables lazy mode where iotlb
invalidations are deferred and batched. In that prospect we add a newVIRTIO_IOMMU_F_LAZY_IOTLB_FLUSHfeature which, when offered allows an endpoint to be attached to a domain in lazy mode. Alongside we introduce a newIOTLB_FLUSHrequest which allows to invalidate iotlb entries for the whole domain.
I am not sure "lazy" is the best, for example a driver may just want to invalidate all the mappings for some reason. Is this better to rename this as "_F_IOTLB_FLUSH" instead?
Quoted 3 lines
When the domain is created in
lazy mode, the virtio-iommu device knows it will receiveIOTLB_FLUSHand it can avoid flushing the device IOTLB on individual UNMAPs.
I wonder what's the advantage to make it per domain instead of per device.
Quoted 10 lines
On a virtio-iommu/vhost scp use case, this can improve the performance by ~10%.
Signed-off-by Eric Auger <[email protected]>---
As a side not virtio-iommu/vhost/scp use case was completely broken
For "not" I think you mean "now"?
Quoted 3 lines
on aarch64 with guests older than v6.6-rc1. The scp stalled and we got soft-lockups on guest in
viommu_iotlb_sync. It happened295525e29a5b(“virtio_net: merge dma operations when filling mergeable buffers”)
Interesting, it doesn't seem to be a fix.
Quoted 11 lines
fixed the performance issue. Without that patch and with lazy mode, we do not observe any stalls although the perf is way worse than with the above patch. Without lazy mode, it looks like the number of vhost iotlb invalidation is large enough to cause serious performance issues.
This proposal can be tested with the following branches
guest branch: https://github.com/eauger/linux/tree/virtio-iommu-lazy-rfc
qemu branch: https://github.com/eauger/qemu/tree/virtio-iommu-device-lazy-rfcThis was tested with vhost. In qemu command line, you need to set ats=on to get benefit of lazy mode.
Ok, I think what you really meant is the "device iotlb" as it ties to ATS.
Quoted 9 lines
--- device-types/iommu/description.tex | 74 +++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-)device-types/iommu/description.tex +0
-0
device-types/iommu/description.tex \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} · line 83
Quoted 10 lines
device-types/iommu/description.tex +7
-0@@ -74,6 +74,14 @@ \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} whether endpoints that are attached to a domain are in bypass mode. +\item[VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH (7)] + This feature determines the capability to use deferred and batched + IOTLB flushes. If the feature is available, the driver can inform + a given domain works in lazy mode. For such domain, the device is + allowed to dissociate UNMAP requests from IOTLB flush operations and + accepts separate IOTLB flush requests that invalidate the IOTLB entries + for the whole domain.
Does this imply a flush for device IOTLB invalidation? It need more clarification.
Or we probably need to do the same as other IOMMU to separate device IOTLB invalidation from the IOMMU IOTLB.
Quoted 113 lines
device-types/iommu/description.tex +66
-2+ \end{description} \drivernormative{\subsubsection}{Feature bits}{Device Types / IOMMU Device / Feature bits}@@ -312,6 +320,7 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op }; #define VIRTIO_IOMMU_ATTACH_F_BYPASS (1 << 0) +#define VIRTIO_IOMMU_ATTACH_F_LAZY (1 << 1) \end{lstlisting} Attach an endpoint to a domain. \field{domain} uniquely identifies a@@ -341,6 +350,18 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op can set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag to create a bypass domain. Endpoints attached to this domain are in bypass mode. +When the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature is available, the driver +can set the VIRTIO_IOMMU_ATTACH_F_LAZY flag to create a lazy mode domain. +Only such domains do support the IOTLB_FLUSH request. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver sets \field{flags} field to VIRTIO_IOMMU_ATTACH_F_LAZY, the +device MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL. + +If several endpoints are attached to the same domain, they must be attached +with the same value for that flag. Otherwise the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL. + \drivernormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request} The driver SHOULD set \field{reserved} to zero.@@ -351,7 +372,8 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op If the domain already exists and is a bypass domain, the driver SHOULD set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag. If the domain exists and is not a bypass domain, the driver SHOULD NOT set the -VIRTIO_IOMMU_ATTACH_F_BYPASS flag. +VIRTIO_IOMMU_ATTACH_F_BYPASS flag. The same holds for the +VIRTIO_IOMMU_ATTACH_F_LAZY flag. \devicenormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request}@@ -374,6 +396,10 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op flag is not consistent with that domain, the device SHOULD reject the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. +If the domain already exists and the VIRTIO_IOMMU_ATTACH_F_LAZY +flag is not consistent with that domain, the device SHOULD reject +the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + If the endpoint identified by \field{endpoint} is already attached to another domain, then the device SHOULD first detach it from that domain and attach it to the one identified by \field{domain}. In that case the@@ -590,6 +616,11 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope This request is only available when VIRTIO_IOMMU_F_MAP_UNMAP has been negotiated. +In case the domain is created with the VIRTIO_IOMMU_ATTACH_F_LAZY flag, +the device is allowed to separate UNMAP from IOTLB invalidations. The driver +must explicitly invalidate the whole domain IOTLB entries using the separate +IOTLB_FLUSH request. + \drivernormative{\paragraph}{UNMAP request}{Device Types / IOMMU Device / Device operations / UNMAP request} The driver SHOULD set the \field{reserved} field to zero.@@ -625,6 +656,47 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope mapping, then the device SHOULD remove all mappings affected by the range and set the request \field{status} to VIRTIO_IOMMU_S_OK. +\subsubsection{IOTLB_FLUSH request}\label{sec:Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +\begin{lstlisting} +struct virtio_iommu_req_iotlb_flush { + struct virtio_iommu_req_head head; + le32 domain; + u8 reserved[4]; + struct virtio_iommu_req_tail tail; +}; +\end{lstlisting} + +Flush IOTLB entries for a given domain. This domain must have been created +with lazy mode set (VIRTIO_IOMMU_ATTACH_F_LAZY flag set on attachment). + +\drivernormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +The driver SHOULD set the \field{reserved} field to zero. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a bypass domain. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a non lazy domain. + +\devicenormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / UNMAP request} + +If the \field{reserved} field of an IOTLB_FLUSH request is not zero, the device +MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL, in which case +the device MAY perform the IOTLB_FLUSH operation. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver calls the IOTLB_FLUSH request, the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL. + +If \field{domain} does not exist, the device SHOULD set the request +\field{status} to VIRTIO_IOMMU_S_NOENT. + +If the domain is a bypass domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + +If the domain is not a lazy domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + \subsubsection{PROBE request}\label{sec:Device Types / IOMMU Device / Device operations / PROBE request} If the VIRTIO_IOMMU_F_PROBE feature bit is present, the driver sends a --
Quoted 2 lines
2.41.0
Thanks
A message that is not in the archive (sent off-list or not yet received): 20240906134724.GA780139@myrica
Quoted 49 lines · Jean-Philippe Brucker
Quoted 13 lines · Jason Wang
Quoted 6 lines · Eric Auger
At the moment the virtio-iommu offers MAP and UNMAP requests. This means that if the virtio-iommu protects a device with an iotlb we have no other choice but invalidating the device iotlb on UNMAP. This proposal aims at allowing the decoupling of UNMAP commands and domain IOTLB invalidations.
I think there should be some difference between "device iotlb" and "domain IOTLB". My understanding is that usually "device iotlb" means the mapping cached by the individual device while "domain IOTLB" means the mapping cached by the IOMMU.
Quoted 5 lines
E.g in the case of intel IOMMU, it has two kind of invalidation
device iotlb invalidation: used to invalidate device IOTLB via ATS domain selective invalidation: used to invalidate all IOTLB for a specific domain
I agree, and we should describe the TLBs a little more since the spec didn't need to mention them so far. An implementation could have both IOTLBs:
dev <-> dev-IOTLB <-> IOTLB <-> mapping tree <-> virtio-iommu drv
At the moment, the MAP/UNMAP interface only acts on the mapping tree, and the virtio-iommu device implicitly invalidates both IOTLB and dev-IOTLB on UNMAP. Here you want to keep the UNMAP requests and add an explicit IOTLB FLUSH request. The IOMMU device would keep maintaining the mappings tree on UNMAP, but wait until a FLUSH request to clear IOTLB and dev-IOTLB.
Quoted 16 lines
Quoted 7 lines
This enables lazy mode where iotlb
invalidations are deferred and batched. In that prospect we add a newVIRTIO_IOMMU_F_LAZY_IOTLB_FLUSHfeature which, when offered allows an endpoint to be attached to a domain in lazy mode. Alongside we introduce a newIOTLB_FLUSHrequest which allows to invalidate iotlb entries for the whole domain.I am not sure "lazy" is the best, for example a driver may just want to invalidate all the mappings for some reason. Is this better to rename this as "
_F_IOTLB_FLUSH" instead?Quoted 3 lines
When the domain is created in
lazy mode, the virtio-iommu device knows it will receiveIOTLB_FLUSHand it can avoid flushing the device IOTLB on individual UNMAPs.
Quoted 8 lines
Quoted 1 line
I wonder what's the advantage to make it per domain instead of per device.
The UNMAP request works on domains (= address spaces), so at the moment when handling UNMAP the virtio-iommu device already needs to implicitly issue a "invalidate all dev-IOTLB attached to this domain". Since we're splitting out this operation into an explicit request, I think making the request per domain means less work on the virtio-iommu device side, because it's familiar with manipulating domains rather than devices.
Ok, I see.
Quoted 67 lines
Quoted 59 lines
Quoted 7 lines
On a virtio-iommu/vhost scp use case, this can improve the performance by ~10%.
Quoted 6 lines
Signed-off-by Eric Auger <[email protected]>---
As a side not virtio-iommu/vhost/scp use case was completely broken
For "not" I think you mean "now"?
Quoted 3 lines
on aarch64 with guests older than v6.6-rc1. The scp stalled and we got soft-lockups on guest in
viommu_iotlb_sync. It happened295525e29a5b(“virtio_net: merge dma operations when filling mergeable buffers”)Interesting, it doesn't seem to be a fix.
Quoted 11 lines
fixed the performance issue. Without that patch and with lazy mode, we do not observe any stalls although the perf is way worse than with the above patch. Without lazy mode, it looks like the number of vhost iotlb invalidation is large enough to cause serious performance issues.
This proposal can be tested with the following branches
guest branch: https://github.com/eauger/linux/tree/virtio-iommu-lazy-rfc
qemu branch: https://github.com/eauger/qemu/tree/virtio-iommu-device-lazy-rfcThis was tested with vhost. In qemu command line, you need to set ats=on to get benefit of lazy mode.
Ok, I think what you really meant is the "device iotlb" as it ties to ATS.
Quoted 9 lines
--- device-types/iommu/description.tex | 74 +++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-)device-types/iommu/description.tex +0
-0device-types/iommu/description.tex \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} · line 83
Quoted 10 lines
device-types/iommu/description.tex +7
-0@@ -74,6 +74,14 @@ \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} whether endpoints that are attached to a domain are in bypass mode. +\item[VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH (7)] + This feature determines the capability to use deferred and batched + IOTLB flushes. If the feature is available, the driver can inform + a given domain works in lazy mode. For such domain, the device is + allowed to dissociate UNMAP requests from IOTLB flush operations and + accepts separate IOTLB flush requests that invalidate the IOTLB entries + for the whole domain.Does this imply a flush for device IOTLB invalidation? It need more clarification.
Quoted 2 lines
Or we probably need to do the same as other IOMMU to separate device IOTLB invalidation from the IOMMU IOTLB.
It's a good question. When adding page table support to virtio-iommu I initially added separate flags for IOMMU IOTLB and dev-IOTLB, but in the end I couldn't find a reason to act on them separately, so now invalidate just means "all IOTLBs including ATCs".
Quoted 7 lines
If the IOTLB and ATC are invalidated through different channels (Arm broadcast TLB maintenance, where IOTLBs are invalidate with CPU instructions but not ATCs), the host knows about it and only invalidates the ATC when receiving a request.
So in my opinion a single request acting on all IOTLBs and dev-IOTLBs is all we need at the moment.
Ok, I see. Just a note that as both Intel and AMD IOMMU have dedicated device IOTLB flush commands. (But they have other advanced features with them). Anyhow the spec could be extended if we find one real use case.
Thanks
Hi Jason, Jean,
Quoted 44 lines · Jason Wang
Quoted 42 lines · Jean-Philippe Brucker
Quoted 11 lines · Jason Wang
Quoted 5 lines · Eric Auger
At the moment the virtio-iommu offers MAP and UNMAP requests. This means that if the virtio-iommu protects a device with an iotlb we have no other choice but invalidating the device iotlb on UNMAP. This proposal aims at allowing the decoupling of UNMAP commands and domain IOTLB invalidations.
I think there should be some difference between "device iotlb" and "domain IOTLB". My understanding is that usually "device iotlb" means the mapping cached by the individual device while "domain IOTLB" means the mapping cached by the IOMMU.
Quoted 5 lines
E.g in the case of intel IOMMU, it has two kind of invalidation
device iotlb invalidation: used to invalidate device IOTLB via ATS domain selective invalidation: used to invalidate all IOTLB for a specific domain
I agree, and we should describe the TLBs a little more since the spec didn't need to mention them so far. An implementation could have both IOTLBs:
dev <-> dev-IOTLB <-> IOTLB <-> mapping tree <-> virtio-iommu drv
At the moment, the MAP/UNMAP interface only acts on the mapping tree, and the virtio-iommu device implicitly invalidates both IOTLB and dev-IOTLB on UNMAP. Here you want to keep the UNMAP requests and add an explicit IOTLB FLUSH request. The IOMMU device would keep maintaining the mappings tree on UNMAP, but wait until a FLUSH request to clear IOTLB and dev-IOTLB.
Quoted 13 lines
Quoted 6 lines
This enables lazy mode where iotlb
invalidations are deferred and batched. In that prospect we add a newVIRTIO_IOMMU_F_LAZY_IOTLB_FLUSHfeature which, when offered allows an endpoint to be attached to a domain in lazy mode. Alongside we introduce a newIOTLB_FLUSHrequest which allows to invalidate iotlb entries for the whole domain.I am not sure "lazy" is the best, for example a driver may just want to invalidate all the mappings for some reason. Is this better to rename this as "
_F_IOTLB_FLUSH" instead?Quoted 3 lines
When the domain is created in
lazy mode, the virtio-iommu device knows it will receiveIOTLB_FLUSHand it can avoid flushing the device IOTLB on individual UNMAPs.
Quoted 8 lines
Quoted 7 lines
Quoted 1 line
I wonder what's the advantage to make it per domain instead of per device.
The UNMAP request works on domains (= address spaces), so at the moment when handling UNMAP the virtio-iommu device already needs to implicitly issue a "invalidate all dev-IOTLB attached to this domain". Since we're splitting out this operation into an explicit request, I think making the request per domain means less work on the virtio-iommu device side, because it's familiar with manipulating domains rather than devices.
Ok, I see.
Yes the 2 aspects that drove this choice were that
- UNMAP applied on domain and also
- lazy mode is set at domain level on kernel(IOMMU_DOMAIN_DMA_FQ domain type)
Also what I had in mind is the flush should invalidate both device and IOMMU IOTLB. The spec update proposal was really driven by the iommu lazy mode implementation.
Currently the virtio-iommu driver lazy mode implementation differs from other iommu drivers implementation in the fact it does not allow to reduce the number of IOTLB invalidations.
However when I came to the practical qemu device implementation I realized I needed to differentiate UNMAP notifications targeting vhost or VFIO which do not mean the same thing at qemu level. vhost unmap notifications mean dev IOTLB Invalidations whereas vfio unmap notifications mean "unmap at physical level". Obviously on VFIO you don't want to physically unmap the whole domain on flush while it is fine to invalidate the whole domain vhost ioltb cache. So at QEMU level I ended to the conclusion only the vhost unmap notification matched an IOTLB flush. So to discriminate between vhost and VFIO the best is to use ats which forces vhost to register DEVIOTLB_UNMAP notifiers.
Hope this clarifies
Quoted 65 lines
Quoted 65 lines
Quoted 53 lines
Quoted 11 lines
On a virtio-iommu/vhost scp use case, this can improve the performance by ~10%.
Signed-off-by Eric Auger <[email protected]>---
As a side not virtio-iommu/vhost/scp use case was completely broken
For "not" I think you mean "now"?
Quoted 3 lines
on aarch64 with guests older than v6.6-rc1. The scp stalled and we got soft-lockups on guest in
viommu_iotlb_sync. It happened295525e29a5b(“virtio_net: merge dma operations when filling mergeable buffers”)Interesting, it doesn't seem to be a fix.
Quoted 11 lines
fixed the performance issue. Without that patch and with lazy mode, we do not observe any stalls although the perf is way worse than with the above patch. Without lazy mode, it looks like the number of vhost iotlb invalidation is large enough to cause serious performance issues.
This proposal can be tested with the following branches
guest branch: https://github.com/eauger/linux/tree/virtio-iommu-lazy-rfc
qemu branch: https://github.com/eauger/qemu/tree/virtio-iommu-device-lazy-rfcThis was tested with vhost. In qemu command line, you need to set ats=on to get benefit of lazy mode.
Ok, I think what you really meant is the "device iotlb" as it ties to ATS.
Quoted 9 lines
--- device-types/iommu/description.tex | 74 +++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-)device-types/iommu/description.tex +0
-0device-types/iommu/description.tex \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} · line 83
Quoted 10 lines
device-types/iommu/description.tex +7
-0@@ -74,6 +74,14 @@ \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} whether endpoints that are attached to a domain are in bypass mode. +\item[VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH (7)] + This feature determines the capability to use deferred and batched + IOTLB flushes. If the feature is available, the driver can inform + a given domain works in lazy mode. For such domain, the device is + allowed to dissociate UNMAP requests from IOTLB flush operations and + accepts separate IOTLB flush requests that invalidate the IOTLB entries + for the whole domain.Does this imply a flush for device IOTLB invalidation? It need more clarification.
Quoted 2 lines
Or we probably need to do the same as other IOMMU to separate device IOTLB invalidation from the IOMMU IOTLB.
It's a good question. When adding page table support to virtio-iommu I initially added separate flags for IOMMU IOTLB and dev-IOTLB, but in the end I couldn't find a reason to act on them separately, so now invalidate just means "all IOTLBs including ATCs".
If the IOTLB and ATC are invalidated through different channels (Arm broadcast TLB maintenance, where IOTLBs are invalidate with CPU instructions but not ATCs), the host knows about it and only invalidates the ATC when receiving a request.
Quoted 6 lines
Quoted 2 lines
So in my opinion a single request acting on all IOTLBs and dev-IOTLBs is all we need at the moment.
Ok, I see. Just a note that as both Intel and AMD IOMMU have dedicated device IOTLB flush commands. (But they have other advanced features with them). Anyhow the spec could be extended if we find one real use case.
understood.
Thanks
Eric
Quoted 3 lines
Thanks
Hi Jean,
Quoted 21 lines · Jean-Philippe Brucker
Quoted 11 lines · Jason Wang
Quoted 5 lines · Eric Auger
At the moment the virtio-iommu offers MAP and UNMAP requests. This means that if the virtio-iommu protects a device with an iotlb we have no other choice but invalidating the device iotlb on UNMAP. This proposal aims at allowing the decoupling of UNMAP commands and domain IOTLB invalidations.
I think there should be some difference between "device iotlb" and "domain IOTLB". My understanding is that usually "device iotlb" means the mapping cached by the individual device while "domain IOTLB" means the mapping cached by the IOMMU.
Quoted 5 lines
E.g in the case of intel IOMMU, it has two kind of invalidation
device iotlb invalidation: used to invalidate device IOTLB via ATS domain selective invalidation: used to invalidate all IOTLB for a specific domain
I agree, and we should describe the TLBs a little more since the spec didn't need to mention them so far. An implementation could have both IOTLBs:
Quoted 7 lines
dev <-> dev-IOTLB <-> IOTLB <-> mapping tree <-> virtio-iommu drv
At the moment, the MAP/UNMAP interface only acts on the mapping tree, and the virtio-iommu device implicitly invalidates both IOTLB and dev-IOTLB on UNMAP. Here you want to keep the UNMAP requests and add an explicit IOTLB FLUSH request. The IOMMU device would keep maintaining the mappings tree on UNMAP, but wait until a FLUSH request to clear IOTLB and dev-IOTLB.
I agree I will add such IOTLB invalidation details in the spec.
Quoted 14 lines
Quoted 14 lines
Quoted 7 lines
This enables lazy mode where iotlb
invalidations are deferred and batched. In that prospect we add a newVIRTIO_IOMMU_F_LAZY_IOTLB_FLUSHfeature which, when offered allows an endpoint to be attached to a domain in lazy mode. Alongside we introduce a newIOTLB_FLUSHrequest which allows to invalidate iotlb entries for the whole domain.I am not sure "lazy" is the best, for example a driver may just want to invalidate all the mappings for some reason. Is this better to rename this as "
_F_IOTLB_FLUSH" instead?Quoted 3 lines
When the domain is created in
lazy mode, the virtio-iommu device knows it will receiveIOTLB_FLUSHand it can avoid flushing the device IOTLB on individual UNMAPs.
Quoted 7 lines
Quoted 1 line
I wonder what's the advantage to make it per domain instead of per device.
The UNMAP request works on domains (= address spaces), so at the moment when handling UNMAP the virtio-iommu device already needs to implicitly issue a "invalidate all dev-IOTLB attached to this domain". Since we're splitting out this operation into an explicit request, I think making the request per domain means less work on the virtio-iommu device side, because it's familiar with manipulating domains rather than devices.
+1
Quoted 60 lines
Quoted 53 lines
Quoted 11 lines
On a virtio-iommu/vhost scp use case, this can improve the performance by ~10%.
Signed-off-by Eric Auger <[email protected]>---
As a side not virtio-iommu/vhost/scp use case was completely broken
For "not" I think you mean "now"?
Quoted 3 lines
on aarch64 with guests older than v6.6-rc1. The scp stalled and we got soft-lockups on guest in
viommu_iotlb_sync. It happened295525e29a5b(“virtio_net: merge dma operations when filling mergeable buffers”)Interesting, it doesn't seem to be a fix.
Quoted 11 lines
fixed the performance issue. Without that patch and with lazy mode, we do not observe any stalls although the perf is way worse than with the above patch. Without lazy mode, it looks like the number of vhost iotlb invalidation is large enough to cause serious performance issues.
This proposal can be tested with the following branches
guest branch: https://github.com/eauger/linux/tree/virtio-iommu-lazy-rfc
qemu branch: https://github.com/eauger/qemu/tree/virtio-iommu-device-lazy-rfcThis was tested with vhost. In qemu command line, you need to set ats=on to get benefit of lazy mode.
Ok, I think what you really meant is the "device iotlb" as it ties to ATS.
Quoted 9 lines
--- device-types/iommu/description.tex | 74 +++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-)device-types/iommu/description.tex +0
-0device-types/iommu/description.tex \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} · line 83
Quoted 10 lines
device-types/iommu/description.tex +7
-0@@ -74,6 +74,14 @@ \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} whether endpoints that are attached to a domain are in bypass mode. +\item[VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH (7)] + This feature determines the capability to use deferred and batched + IOTLB flushes. If the feature is available, the driver can inform + a given domain works in lazy mode. For such domain, the device is + allowed to dissociate UNMAP requests from IOTLB flush operations and + accepts separate IOTLB flush requests that invalidate the IOTLB entries + for the whole domain.Does this imply a flush for device IOTLB invalidation? It need more clarification.
Quoted 2 lines
Or we probably need to do the same as other IOMMU to separate device IOTLB invalidation from the IOMMU IOTLB.
It's a good question. When adding page table support to virtio-iommu I initially added separate flags for IOMMU IOTLB and dev-IOTLB, but in the end I couldn't find a reason to act on them separately, so now invalidate just means "all IOTLBs including ATCs".
Quoted 7 lines
If the IOTLB and ATC are invalidated through different channels (Arm broadcast TLB maintenance, where IOTLBs are invalidate with CPU instructions but not ATCs), the host knows about it and only invalidates the ATC when receiving a request.
So in my opinion a single request acting on all IOTLBs and dev-IOTLBs is all we need at the moment.
OK so I am tempted to keep this single request atm.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
device-types/iommu/description.tex +2
-0+ \end{description} \drivernormative{\subsubsection}{Feature bits}{Device Types / IOMMU Device / Feature bits}@@ -312,6 +320,7 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op }; #define VIRTIO_IOMMU_ATTACH_F_BYPASS (1 << 0) +#define VIRTIO_IOMMU_ATTACH_F_LAZY (1 << 1) \end{lstlisting} Attach an endpoint to a domain. \field{domain} uniquely identifies a@@ -341,6 +350,18 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op can set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag to create a bypass domain. Endpoints attached to this domain are in bypass mode.
Quoted 6 lines
Quoted 2 lines
device-types/iommu/description.tex \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op · line 354
Quoted 2 lines
device-types/iommu/description.tex +2
-0+When the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature is available, the driver +can set the VIRTIO_IOMMU_ATTACH_F_LAZY flag to create a lazy mode domain.Maybe we could call this "deferral domain" rather than "lazy". In any case since we define the term here, then we don't have to refer to the attach flag in later descriptions, we can just call it "lazy domain" or "deferral domain" (like we did for bypass domains)
I am fine with deferral domain if prefered. Also I agree that once the terminology is properly introduced in the spec, I can avoid refering to the flag.
Quoted 8 lines
Quoted 6 lines
device-types/iommu/description.tex \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op · line 359
Quoted 6 lines
device-types/iommu/description.tex +5
-0+Only such domains do support the IOTLB_FLUSH request. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver sets \field{flags} field to VIRTIO_IOMMU_ATTACH_F_LAZY, the +device MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL.(any statement MAY/MUST/SHOULD has to go in the devicenormative or drivernormative section below)
sure I am not yet familiar with those rules
Quoted 5 lines
We already have:
"If the device does not recognize a flags bit, it MUST reject the request and set status to
VIRTIO_IOMMU_S_INVAL."
it is not really the same. Above I stated that you shall not accect a flush on a non deferral domain. I understand the above as reject any unknown flag.
Quoted 6 lines
Quoted 5 lines
device-types/iommu/description.tex \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op · line 363
Quoted 5 lines
device-types/iommu/description.tex +4
-0+ +If several endpoints are attached to the same domain, they must be attached +with the same value for that flag. Otherwise the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL.Already stated below
I will move/keep in the normative part
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
device-types/iommu/description.tex +7
-1+ \drivernormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request} The driver SHOULD set \field{reserved} to zero.@@ -351,7 +372,8 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op If the domain already exists and is a bypass domain, the driver SHOULD set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag. If the domain exists and is not a bypass domain, the driver SHOULD NOT set the -VIRTIO_IOMMU_ATTACH_F_BYPASS flag. +VIRTIO_IOMMU_ATTACH_F_BYPASS flag. The same holds for the +VIRTIO_IOMMU_ATTACH_F_LAZY flag. \devicenormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request}@@ -374,6 +396,10 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op flag is not consistent with that domain, the device SHOULD reject the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. +If the domain already exists and the VIRTIO_IOMMU_ATTACH_F_LAZY +flag is not consistent with that domain, the device SHOULD reject +the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + If the endpoint identified by \field{endpoint} is already attached to another domain, then the device SHOULD first detach it from that domain and attach it to the one identified by \field{domain}. In that case the@@ -590,6 +616,11 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope This request is only available when VIRTIO_IOMMU_F_MAP_UNMAP has been negotiated.
Quoted 9 lines
Quoted 4 lines
device-types/iommu/description.tex \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope · line 622
Quoted 4 lines
device-types/iommu/description.tex +4
-0+In case the domain is created with the VIRTIO_IOMMU_ATTACH_F_LAZY flag, +the device is allowed to separate UNMAP from IOTLB invalidations. The driver +must explicitly invalidate the whole domain IOTLB entries using the separate +IOTLB_FLUSH request.I think it's best to avoid "must" in non-normative section. Maybe we can simply say "the driver explicitly invalidates the whole domain..." here. Not sure it even makes sense to say "has to invalidate" because the whole thing is fuzzy. There is no well-defined period for the flushes so it could in theory never flush the TLB...
OK. I will put "the driver explicitly invalidates the whole domain". I guess nothing prevents the device from invalidating on unmap either if it can do differently.
Quoted 5 lines
We could put a little more details here and less in other places, so it's easier for the reader to figure out what this feature is about: the feature flag description could just say "
F_FLUSHattach flag is supported". And the attach flag description "INVALIDATE/FLUSH requests are
Yes I do agree.
Quoted 12 lines
supported on this domain". And here a more detailed description on how to use UNMAP alongside FLUSH and why it's useful, something like:
"To reduce the number of synchronizations between device and driver, the driver can send batches of UNMAP requests periodically instead of one at a time. Since this introduces a time window where the endpoint could access stale mappings and reallocated memory, the endpoint and its driver must be trusted not to misbehave. For <lazy/deferral> domains, the driver appends an <invalidate/flush> request at the end of each batch, allowing the device to invalidate TLBs once per batch instead of once per UNMAP request. For <lazy/deferral> domains, the device does not have to invalidate TLBs on UNMAP, it can wait until a <invalidate/flush> request."
Yes that's a good description. I will include that.
Quoted 8 lines
This assumes we already defined what are TLBs somewhere
The new feature raises one question: is a non-deferral domain forbidden from using lazy mode? The Linux driver already implements lazy, and such statement would make it illegal. I think we need to phrase this new feature as only adding the flush request, but not allowing/forbidding lazy mode. That remains guest policy and isn't reflected by supporting
F_LAZY.
I think I will remove any reference to lazy in this spec update proposal.
Quoted 39 lines
Quoted 9 lines
Quoted 9 lines
device-types/iommu/description.tex +1
-0+ \drivernormative{\paragraph}{UNMAP request}{Device Types / IOMMU Device / Device operations / UNMAP request} The driver SHOULD set the \field{reserved} field to zero.@@ -625,6 +656,47 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope mapping, then the device SHOULD remove all mappings affected by the range and set the request \field{status} to VIRTIO_IOMMU_S_OK.Quoted 9 lines
device-types/iommu/description.tex \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope · line 667
Quoted 9 lines
device-types/iommu/description.tex +9
-0+\subsubsection{IOTLB_FLUSH request}\label{sec:Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +\begin{lstlisting} +struct virtio_iommu_req_iotlb_flush { + struct virtio_iommu_req_head head; + le32 domain; + u8 reserved[4]; + struct virtio_iommu_req_tail tail; +};It would be nice to reconcile this with the INVALIDATE request needed for page table support (although we don't have anyone available to work on this at the moment I'm still getting requests for it). The concept is the same, only the location of the components change (the mapping tree is maintained by the driver directly instead of the device via MAP/UNMAP)
https://lore.kernel.org/virtio-comment/[email protected]/
The
LAZY_IOTLB_FLUSHrequest corresponds to INVALIDATE request with scopeVIRTIO_IOMMU_INVAL_S_DOMAIN, flagVIRTIO_IOMMU_INVAL_C_TLB. We certainly don't have to keep those values here but I'd rather not introduce a separate request later for exactly the same concept. How about we leave space for extension in the new request:struct
virtio_iommu_req_invalidate{
structvirtio_iommu_req_headhead;
le32 domain;
u8 reserved[56];
structvirtio_iommu_req_tailtail;
};
Quoted 3 lines
And maybe we call the new request INVALIDATE instead of
IOTLB_FLUSH, so it can later be extended with address ranges (I think "flush" only means invalidate-all?)
Sure I can do that.
Quoted 11 lines
Then when extending the INVALIDATE request for page table support, we'll have scope DOMAIN==0, and TLB cache always on (only the PASID cache will be selectable).
Quoted 4 lines
device-types/iommu/description.tex \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope · line 671
Quoted 4 lines
device-types/iommu/description.tex +4
-0+\end{lstlisting} + +Flush IOTLB entries for a given domain. This domain must have been created +with lazy mode set (VIRTIO_IOMMU_ATTACH_F_LAZY flag set on attachment).We can drop this...
Quoted 9 lines
Quoted 8 lines
device-types/iommu/description.tex \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope · line 679
Quoted 8 lines
device-types/iommu/description.tex +8
-0+ +\drivernormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +The driver SHOULD set the \field{reserved} field to zero. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a bypass domain. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a non lazy domain.... and replace these with "The domain MUST be a lazy/deferral domain"
OK
Quoted 14 lines
Quoted 10 lines
device-types/iommu/description.tex \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope · line 689
Quoted 10 lines
device-types/iommu/description.tex +9
-0+\devicenormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / UNMAP request} + +If the \field{reserved} field of an IOTLB_FLUSH request is not zero, the device +MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL, in which case +the device MAY perform the IOTLB_FLUSH operation. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver calls the IOTLB_FLUSH request, the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL.The ATTACH request should have a normative statement preventing creation of deferral requests if the feature is not offered. And below we have normative statements preventing the use of this on deferral domains, so we can drop this
OK
Quoted 10 lines
Quoted 7 lines
device-types/iommu/description.tex \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope · line 695
Quoted 7 lines
device-types/iommu/description.tex +6
-0+ +If \field{domain} does not exist, the device SHOULD set the request +\field{status} to VIRTIO_IOMMU_S_NOENT. + +If the domain is a bypass domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL.This should probably be an ATTACH normative statement: prevent the creation of a deferral+bypass domain.
Quoted 13 lines
Quoted 5 lines
device-types/iommu/description.tex \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope · line 700
Quoted 5 lines
device-types/iommu/description.tex +4
-0+ +If the domain is not a lazy domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + \subsubsection{PROBE request}\label{sec:Device Types / IOMMU Device / Device operations / PROBE request}Should we add a new PROBE property that says if the new mode is supported by each endpoint? So when we have a mix of VFIO, vhost and virtio endpoints, only the vhost endpoint would have this property, and the driver wouldn't add an invalidate request for domains attached to the others? One reason is that the device may implement TLBs differently for each kind of endpoints. For example in vhost it's more efficient to invalidate the whole TLB rather than doing it by range, so this feature improves performance, but for other TLB implementations it could be the opposite.
I am not sure this extra complexity is needed atm. Currently the qemu device implements the flush for vhost protected devices and does not change the behavior for VFIO.
Quoted 3 lines
If we make the attach rule about mixing domain types too strict though, this would prevent sharing a domain between the VFIO and vhost endpoints.
Yes I think we can manage without that new probe. I would be inclined to go without
Quoted 6 lines
Random thought: it may be interesting to actually implement this on VFIO endpoints at some point:
VFIO_DMA_UNMAPioctl only updates the page tables and FLUSH issues an invalidate-all in the HW IOMMU. But that requiresVFIO_DMA_UNMAPnot to unpin the guest pages so they aren't reallocated, which would be a big security hole.
At QEMU level we miss some notifiers to differentiate those and as you noted this would induce changes in the vfio/iommufd driver. If someone face perf issues in production use cases this can be further studied indeed.
Thank you for your thorough review!
Eric
Quoted 4 lines
Thanks,
Jean
Hi Jason,
Quoted 24 lines · Jason Wang
Quoted 5 lines · Eric Auger
At the moment the virtio-iommu offers MAP and UNMAP requests. This means that if the virtio-iommu protects a device with an iotlb we have no other choice but invalidating the device iotlb on UNMAP. This proposal aims at allowing the decoupling of UNMAP commands and domain IOTLB invalidations.
I think there should be some difference between "device iotlb" and "domain IOTLB". My understanding is that usually "device iotlb" means the mapping cached by the individual device while "domain IOTLB" means the mapping cached by the IOMMU.
E.g in the case of intel IOMMU, it has two kind of invalidation
device iotlb invalidation: used to invalidate device IOTLB via ATS domain selective invalidation: used to invalidate all IOTLB for a specific domain
Quoted 6 lines
This enables lazy mode where iotlb
invalidations are deferred and batched. In that prospect we add a newVIRTIO_IOMMU_F_LAZY_IOTLB_FLUSHfeature which, when offered allows an endpoint to be attached to a domain in lazy mode. Alongside we introduce a newIOTLB_FLUSHrequest which allows to invalidate iotlb entries for the whole domain.
Quoted 3 lines
I am not sure "lazy" is the best, for example a driver may just want to invalidate all the mappings for some reason. Is this better to rename this as "
_F_IOTLB_FLUSH" instead?
Yes I can get rid of lazy terminology either by just using flush or invalidate as suggested by Jean.
Quoted 9 lines
Quoted 4 lines
When the domain is created in
lazy mode, the virtio-iommu device knows it will receiveIOTLB_FLUSHand it can avoid flushing the device IOTLB on individual UNMAPs.I wonder what's the advantage to make it per domain instead of per device.
Quoted 3 lines
On a virtio-iommu/vhost scp use case, this can improve the performance by ~10%.
Quoted 7 lines
Quoted 6 lines
Signed-off-by Eric Auger <[email protected]>---
As a side not virtio-iommu/vhost/scp use case was completely broken
For "not" I think you mean "now"?
yes I meant side note
Quoted 5 lines
Quoted 4 lines
on aarch64 with guests older than v6.6-rc1. The scp stalled and we got soft-lockups on guest in
viommu_iotlb_sync. It happened295525e29a5b(“virtio_net: merge dma operations when filling mergeable buffers”)Interesting, it doesn't seem to be a fix.
Yes it is rather an optimization. But it looks like with virtio-iommu/vhsot/scp use case it brings such a huge perf boost that it fixes the guest lockups and scp stalls.
Quoted 6 lines
Quoted 6 lines
fixed the performance issue. Without that patch and with lazy mode, we do not observe any stalls although the perf is way worse than with the above patch. Without lazy mode, it looks like the number of vhost iotlb invalidation is large enough to cause serious performance issues.
Quoted 7 lines
Quoted 6 lines
This proposal can be tested with the following branches
guest branch: https://github.com/eauger/linux/tree/virtio-iommu-lazy-rfc
qemu branch: https://github.com/eauger/qemu/tree/virtio-iommu-device-lazy-rfcThis was tested with vhost. In qemu command line, you need to set ats=on to get benefit of lazy mode.
Ok, I think what you really meant is the "device iotlb" as it ties to ATS.
The actual device implementation brought some confusion on the wording. I hope I better explained in my previous reply.
Thanks
Eric
Quoted 143 lines
Quoted 10 lines
--- device-types/iommu/description.tex | 74 +++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-)device-types/iommu/description.tex +0
-0device-types/iommu/description.tex \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} · line 83
Quoted 10 lines
device-types/iommu/description.tex +7
-0@@ -74,6 +74,14 @@ \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} whether endpoints that are attached to a domain are in bypass mode. +\item[VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH (7)] + This feature determines the capability to use deferred and batched + IOTLB flushes. If the feature is available, the driver can inform + a given domain works in lazy mode. For such domain, the device is + allowed to dissociate UNMAP requests from IOTLB flush operations and + accepts separate IOTLB flush requests that invalidate the IOTLB entries + for the whole domain.Does this imply a flush for device IOTLB invalidation? It need more clarification.
Or we probably need to do the same as other IOMMU to separate device IOTLB invalidation from the IOMMU IOTLB.
Quoted 113 lines
device-types/iommu/description.tex +66
-2+ \end{description} \drivernormative{\subsubsection}{Feature bits}{Device Types / IOMMU Device / Feature bits}@@ -312,6 +320,7 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op }; #define VIRTIO_IOMMU_ATTACH_F_BYPASS (1 << 0) +#define VIRTIO_IOMMU_ATTACH_F_LAZY (1 << 1) \end{lstlisting} Attach an endpoint to a domain. \field{domain} uniquely identifies a@@ -341,6 +350,18 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op can set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag to create a bypass domain. Endpoints attached to this domain are in bypass mode. +When the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature is available, the driver +can set the VIRTIO_IOMMU_ATTACH_F_LAZY flag to create a lazy mode domain. +Only such domains do support the IOTLB_FLUSH request. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver sets \field{flags} field to VIRTIO_IOMMU_ATTACH_F_LAZY, the +device MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL. + +If several endpoints are attached to the same domain, they must be attached +with the same value for that flag. Otherwise the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL. + \drivernormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request} The driver SHOULD set \field{reserved} to zero.@@ -351,7 +372,8 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op If the domain already exists and is a bypass domain, the driver SHOULD set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag. If the domain exists and is not a bypass domain, the driver SHOULD NOT set the -VIRTIO_IOMMU_ATTACH_F_BYPASS flag. +VIRTIO_IOMMU_ATTACH_F_BYPASS flag. The same holds for the +VIRTIO_IOMMU_ATTACH_F_LAZY flag. \devicenormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request}@@ -374,6 +396,10 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op flag is not consistent with that domain, the device SHOULD reject the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. +If the domain already exists and the VIRTIO_IOMMU_ATTACH_F_LAZY +flag is not consistent with that domain, the device SHOULD reject +the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + If the endpoint identified by \field{endpoint} is already attached to another domain, then the device SHOULD first detach it from that domain and attach it to the one identified by \field{domain}. In that case the@@ -590,6 +616,11 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope This request is only available when VIRTIO_IOMMU_F_MAP_UNMAP has been negotiated. +In case the domain is created with the VIRTIO_IOMMU_ATTACH_F_LAZY flag, +the device is allowed to separate UNMAP from IOTLB invalidations. The driver +must explicitly invalidate the whole domain IOTLB entries using the separate +IOTLB_FLUSH request. + \drivernormative{\paragraph}{UNMAP request}{Device Types / IOMMU Device / Device operations / UNMAP request} The driver SHOULD set the \field{reserved} field to zero.@@ -625,6 +656,47 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope mapping, then the device SHOULD remove all mappings affected by the range and set the request \field{status} to VIRTIO_IOMMU_S_OK. +\subsubsection{IOTLB_FLUSH request}\label{sec:Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +\begin{lstlisting} +struct virtio_iommu_req_iotlb_flush { + struct virtio_iommu_req_head head; + le32 domain; + u8 reserved[4]; + struct virtio_iommu_req_tail tail; +}; +\end{lstlisting} + +Flush IOTLB entries for a given domain. This domain must have been created +with lazy mode set (VIRTIO_IOMMU_ATTACH_F_LAZY flag set on attachment). + +\drivernormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +The driver SHOULD set the \field{reserved} field to zero. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a bypass domain. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a non lazy domain. + +\devicenormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / UNMAP request} + +If the \field{reserved} field of an IOTLB_FLUSH request is not zero, the device +MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL, in which case +the device MAY perform the IOTLB_FLUSH operation. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver calls the IOTLB_FLUSH request, the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL. + +If \field{domain} does not exist, the device SHOULD set the request +\field{status} to VIRTIO_IOMMU_S_NOENT. + +If the domain is a bypass domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + +If the domain is not a lazy domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + \subsubsection{PROBE request}\label{sec:Device Types / IOMMU Device / Device operations / PROBE request} If the VIRTIO_IOMMU_F_PROBE feature bit is present, the driver sends a --Quoted 2 lines
2.41.0
Thanks
Quoted 48 lines · Eric Auger
Hi Jason,
Quoted 24 lines · Jason Wang
Quoted 5 lines · Eric Auger
At the moment the virtio-iommu offers MAP and UNMAP requests. This means that if the virtio-iommu protects a device with an iotlb we have no other choice but invalidating the device iotlb on UNMAP. This proposal aims at allowing the decoupling of UNMAP commands and domain IOTLB invalidations.
I think there should be some difference between "device iotlb" and "domain IOTLB". My understanding is that usually "device iotlb" means the mapping cached by the individual device while "domain IOTLB" means the mapping cached by the IOMMU.
E.g in the case of intel IOMMU, it has two kind of invalidation
device iotlb invalidation: used to invalidate device IOTLB via ATS domain selective invalidation: used to invalidate all IOTLB for a specific domain
Quoted 6 lines
This enables lazy mode where iotlb
invalidations are deferred and batched. In that prospect we add a newVIRTIO_IOMMU_F_LAZY_IOTLB_FLUSHfeature which, when offered allows an endpoint to be attached to a domain in lazy mode. Alongside we introduce a newIOTLB_FLUSHrequest which allows to invalidate iotlb entries for the whole domain.Quoted 3 lines
I am not sure "lazy" is the best, for example a driver may just want to invalidate all the mappings for some reason. Is this better to rename this as "
_F_IOTLB_FLUSH" instead?Yes I can get rid of lazy terminology either by just using flush or invalidate as suggested by Jean.
Quoted 9 lines
Quoted 4 lines
When the domain is created in
lazy mode, the virtio-iommu device knows it will receiveIOTLB_FLUSHand it can avoid flushing the device IOTLB on individual UNMAPs.I wonder what's the advantage to make it per domain instead of per device.
Quoted 3 lines
On a virtio-iommu/vhost scp use case, this can improve the performance by ~10%.
Quoted 7 lines
Quoted 6 lines
Signed-off-by Eric Auger <[email protected]>---
As a side not virtio-iommu/vhost/scp use case was completely broken
For "not" I think you mean "now"?
Quoted 8 lines
yes I meant side note
Quoted 5 lines
Quoted 4 lines
on aarch64 with guests older than v6.6-rc1. The scp stalled and we got soft-lockups on guest in
viommu_iotlb_sync. It happened295525e29a5b(“virtio_net: merge dma operations when filling mergeable buffers”)Interesting, it doesn't seem to be a fix.
Yes it is rather an optimization. But it looks like with virtio-iommu/vhsot/scp use case it brings such a huge perf boost
Right, this is what we want to achieve in that patch.
Quoted 2 lines
that it
fixes the guest lockups and scp stalls.
Ok, it seems that there's some bug somewhere. Btw, the pre mapping code seems to introduce a regression which might be disabled for this Linux release:
https://lore.kernel.org/all/[email protected]/
(Note that we haven't decided if this is the right way to go).
Haven't had time to check if it can re-introduce the issues or not.
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
fixed the performance issue. Without that patch and with lazy mode, we do not observe any stalls although the perf is way worse than with the above patch. Without lazy mode, it looks like the number of vhost iotlb invalidation is large enough to cause serious performance issues.
This proposal can be tested with the following branches
guest branch: https://github.com/eauger/linux/tree/virtio-iommu-lazy-rfc
qemu branch: https://github.com/eauger/qemu/tree/virtio-iommu-device-lazy-rfcThis was tested with vhost. In qemu command line, you need to set ats=on to get benefit of lazy mode.
Quoted 151 lines
Quoted 1 line
Ok, I think what you really meant is the "device iotlb" as it ties to ATS.
The actual device implementation brought some confusion on the wording. I hope I better explained in my previous reply.
Thanks
Eric
Quoted 143 lines
Quoted 10 lines
--- device-types/iommu/description.tex | 74 +++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-)device-types/iommu/description.tex +0
-0device-types/iommu/description.tex \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} · line 83
Quoted 10 lines
device-types/iommu/description.tex +7
-0@@ -74,6 +74,14 @@ \subsection{Feature bits}\label{sec:Device Types / IOMMU Device / Feature bits} whether endpoints that are attached to a domain are in bypass mode. +\item[VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH (7)] + This feature determines the capability to use deferred and batched + IOTLB flushes. If the feature is available, the driver can inform + a given domain works in lazy mode. For such domain, the device is + allowed to dissociate UNMAP requests from IOTLB flush operations and + accepts separate IOTLB flush requests that invalidate the IOTLB entries + for the whole domain.Does this imply a flush for device IOTLB invalidation? It need more clarification.
Or we probably need to do the same as other IOMMU to separate device IOTLB invalidation from the IOMMU IOTLB.
Quoted 113 lines
device-types/iommu/description.tex +66
-2+ \end{description} \drivernormative{\subsubsection}{Feature bits}{Device Types / IOMMU Device / Feature bits}@@ -312,6 +320,7 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op }; #define VIRTIO_IOMMU_ATTACH_F_BYPASS (1 << 0) +#define VIRTIO_IOMMU_ATTACH_F_LAZY (1 << 1) \end{lstlisting} Attach an endpoint to a domain. \field{domain} uniquely identifies a@@ -341,6 +350,18 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op can set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag to create a bypass domain. Endpoints attached to this domain are in bypass mode. +When the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature is available, the driver +can set the VIRTIO_IOMMU_ATTACH_F_LAZY flag to create a lazy mode domain. +Only such domains do support the IOTLB_FLUSH request. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver sets \field{flags} field to VIRTIO_IOMMU_ATTACH_F_LAZY, the +device MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL. + +If several endpoints are attached to the same domain, they must be attached +with the same value for that flag. Otherwise the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL. + \drivernormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request} The driver SHOULD set \field{reserved} to zero.@@ -351,7 +372,8 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op If the domain already exists and is a bypass domain, the driver SHOULD set the VIRTIO_IOMMU_ATTACH_F_BYPASS flag. If the domain exists and is not a bypass domain, the driver SHOULD NOT set the -VIRTIO_IOMMU_ATTACH_F_BYPASS flag. +VIRTIO_IOMMU_ATTACH_F_BYPASS flag. The same holds for the +VIRTIO_IOMMU_ATTACH_F_LAZY flag. \devicenormative{\paragraph}{ATTACH request}{Device Types / IOMMU Device / Device operations / ATTACH request}@@ -374,6 +396,10 @@ \subsubsection{ATTACH request}\label{sec:Device Types / IOMMU Device / Device op flag is not consistent with that domain, the device SHOULD reject the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. +If the domain already exists and the VIRTIO_IOMMU_ATTACH_F_LAZY +flag is not consistent with that domain, the device SHOULD reject +the request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + If the endpoint identified by \field{endpoint} is already attached to another domain, then the device SHOULD first detach it from that domain and attach it to the one identified by \field{domain}. In that case the@@ -590,6 +616,11 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope This request is only available when VIRTIO_IOMMU_F_MAP_UNMAP has been negotiated. +In case the domain is created with the VIRTIO_IOMMU_ATTACH_F_LAZY flag, +the device is allowed to separate UNMAP from IOTLB invalidations. The driver +must explicitly invalidate the whole domain IOTLB entries using the separate +IOTLB_FLUSH request. + \drivernormative{\paragraph}{UNMAP request}{Device Types / IOMMU Device / Device operations / UNMAP request} The driver SHOULD set the \field{reserved} field to zero.@@ -625,6 +656,47 @@ \subsubsection{UNMAP request}\label{sec:Device Types / IOMMU Device / Device ope mapping, then the device SHOULD remove all mappings affected by the range and set the request \field{status} to VIRTIO_IOMMU_S_OK. +\subsubsection{IOTLB_FLUSH request}\label{sec:Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +\begin{lstlisting} +struct virtio_iommu_req_iotlb_flush { + struct virtio_iommu_req_head head; + le32 domain; + u8 reserved[4]; + struct virtio_iommu_req_tail tail; +}; +\end{lstlisting} + +Flush IOTLB entries for a given domain. This domain must have been created +with lazy mode set (VIRTIO_IOMMU_ATTACH_F_LAZY flag set on attachment). + +\drivernormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / IOTLB_FLUSH request} + +The driver SHOULD set the \field{reserved} field to zero. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a bypass domain. + +The driver SHOULD NOT send IOTLB_FLUSH requests on a non lazy domain. + +\devicenormative{\paragraph}{IOTLB_FLUSH request}{Device Types / IOMMU Device / Device operations / UNMAP request} + +If the \field{reserved} field of an IOTLB_FLUSH request is not zero, the device +MAY set the request \field{status} to VIRTIO_IOMMU_S_INVAL, in which case +the device MAY perform the IOTLB_FLUSH operation. + +If the device does not offer the VIRTIO_IOMMU_F_LAZY_IOTLB_FLUSH feature and +if the driver calls the IOTLB_FLUSH request, the device MAY set the request +\field{status} to VIRTIO_IOMMU_S_INVAL. + +If \field{domain} does not exist, the device SHOULD set the request +\field{status} to VIRTIO_IOMMU_S_NOENT. + +If the domain is a bypass domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + +If the domain is not a lazy domain, the device SHOULD reject the +request and set \field{status} to VIRTIO_IOMMU_S_INVAL. + \subsubsection{PROBE request}\label{sec:Device Types / IOMMU Device / Device operations / PROBE request} If the VIRTIO_IOMMU_F_PROBE feature bit is present, the driver sends a --Quoted 2 lines
2.41.0
Thanks
Thanks