Fix two bugs in iommu gather processing
Subsystems (3)
cover Fix two bugs in iommu gather processing
I've been working on an invalidation kunit test and found two issues worth fixing right away.
Jason Gunthorpe (2):
iommu: Do not call drivers for empty gathers
iommupt: Fix short gather if the unmap goes into a large mapping
drivers/iommu/generic_pt/iommu_pt.h | 2 +- include/linux/iommu.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
base-commit: 10595d49f542df65ad4107713017075d5b9b529c
Signature · 1 line
2.43.0
PATCH 1/2 iommu: Do not call drivers for empty gathers
An empty gather is coded with start=U64_MAX, end=0 and several drivers go on to convert that to a size with:
end - start + 1
Which gives 2 for an empty gather. This then causes Weird Stuff to happen (for example an UBSAN splat in VT-d) that is hopefully harmless, but maybe not.
Prevent drivers from being called right in iommu_iotlb_sync().
Auditing shows that AMD, Intel, Mediatek and RSIC-V drivers all do things on these empty gathers.
Further, there are several callers that can trigger empty gathers, especially in unusual conditions. For example iommu_map_nosync() will call a 0 size unmap on some error paths. Also in VFIO, iommupt and other places.
--- include/linux/iommu.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/iommu.h +2 -1
@@ -980,7 +980,8 @@ static inline void iommu_flush_iotlb_all(struct iommu_domain *domain)static inline void iommu_iotlb_sync (struct iommu_domain *domain,struct iommu_iotlb_gather *iotlb_gather) { -if (domain->ops->iotlb_sync) +if (domain->ops->iotlb_sync && +likely (iotlb_gather->start < iotlb_gather->end)) domain->ops->iotlb_sync (domain, iotlb_gather);iommu_iotlb_gather_init (iotlb_gather);
Signature · 1 line
2.43.0
Re: [PATCH rc 1/2] iommu: Do not call drivers for empty gathers
Quoted 19 lines · Jason Gunthorpe
An empty gather is coded with start=U64_MAX, end=0 and several drivers go on to convert that to a size with:
end - start + 1
Which gives 2 for an empty gather. This then causes Weird Stuff to happen (for example an UBSAN splat in VT-d) that is hopefully harmless, but maybe not.
Prevent drivers from being called right in
iommu_iotlb_sync().Auditing shows that AMD, Intel, Mediatek and RSIC-V drivers all do things on these empty gathers.
Further, there are several callers that can trigger empty gathers, especially in unusual conditions. For example
iommu_map_nosync()will call a 0 size unmap on some error paths. Also in VFIO, iommupt and other places.
Quoted 4 lines
Reported-by Janusz Krzysztofik <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>
-Vasant
Re: [PATCH rc 1/2] iommu: Do not call drivers for empty gathers
In reply to Jason GunthorpeQuoted 11 lines · Jason Gunthorpe
An empty gather is coded with start=U64_MAX, end=0 and several drivers go on to convert that to a size with:
end - start + 1
Which gives 2 for an empty gather. This then causes Weird Stuff to happen (for example an UBSAN splat in VT-d) that is hopefully harmless, but maybe not.
Prevent drivers from being called right in
iommu_iotlb_sync().
Quoted 7 lines
Auditing shows that AMD, Intel, Mediatek and RSIC-V drivers all do things on these empty gathers.
Further, there are several callers that can trigger empty gathers, especially in unusual conditions. For example
iommu_map_nosync()will call a 0 size unmap on some error paths. Also in VFIO, iommupt and other places.
My instinct is still to tidy up the 0-length unmap case(s), but I guess iommu_iotlb_sync() is itself also a public API where being more robust against erroneous usage is no bad thing. With one minor nit below,
Quoted 13 lines
Reported-by Janusz Krzysztofik <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>--- include/linux/iommu.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/linux/iommu.h +0
-0
include/linux/iommu.h static inline void iommu_flush_iotlb_all(struct iommu_domain *domain) · line 984
Quoted 6 lines
include/linux/iommu.h +2
-1@@ -980,7 +980,8 @@ static inline void iommu_flush_iotlb_all(struct iommu_domain *domain)static inline void iommu_iotlb_sync (struct iommu_domain *domain,struct iommu_iotlb_gather *iotlb_gather) { -if (domain->ops->iotlb_sync) +if (domain->ops->iotlb_sync && +likely (iotlb_gather->start < iotlb_gather->end))
Elsewhere we just use "gather->end != 0" as the "non-empty" condition; how concerned are we about defending against more-intentionally malformed gathers here?
Thanks,
Robin.
Quoted 3 lines
include/linux/iommu.h +0
-0domain->ops->iotlb_sync (domain, iotlb_gather);iommu_iotlb_gather_init (iotlb_gather);
Re: [PATCH rc 1/2] iommu: Do not call drivers for empty gathers
Quoted 8 lines · Robin Murphy
Quoted 4 lines
Further, there are several callers that can trigger empty gathers, especially in unusual conditions. For example
iommu_map_nosync()will call a 0 size unmap on some error paths. Also in VFIO, iommupt and other places.My instinct is still to tidy up the 0-length unmap case(s), but I guess
iommu_iotlb_sync()is itself also a public API where being more robust against erroneous usage is no bad thing.
I also wanted to do that but found enough problematic cases I lost confidence I could reliably catch them all..
Quoted 7 lines
include/linux/iommu.h static inline void iommu_flush_iotlb_all(struct iommu_domain *domain) · line 984
Quoted 3 lines
include/linux/iommu.h +2
-1-if (domain->ops->iotlb_sync) +if (domain->ops->iotlb_sync && +likely (iotlb_gather->start < iotlb_gather->end))Elsewhere we just use "
gather->end!= 0" as the "non-empty" condition; how concerned are we about defending against more-intentionally malformed gathers here?
I choose this deliberately to protect the driver, a malformed gather that is 0 sized, or negative sized looks like it will have Weird Things happen in drivers.
We could further classify the < and WARN_ON the malformed cases, but I don't want to pass negative sized gathers into drivers. We'd probably also have to de-inline the function if more is added. Do you have a preference?
Jason
Robin Murphy
reviewerRe: [PATCH rc 1/2] iommu: Do not call drivers for empty gathers
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Robin Murphy
Quoted 4 lines
Further, there are several callers that can trigger empty gathers, especially in unusual conditions. For example
iommu_map_nosync()will call a 0 size unmap on some error paths. Also in VFIO, iommupt and other places.
Quoted 6 lines
Quoted 3 lines
My instinct is still to tidy up the 0-length unmap case(s), but I guess
iommu_iotlb_sync()is itself also a public API where being more robust against erroneous usage is no bad thing.I also wanted to do that but found enough problematic cases I lost confidence I could reliably catch them all..
I reckon an early "if (!size) return 0;" in iommu_unmap() would suffice to cover the internal error cleanup paths and most careless external users. However if we don't trust iommu_unmap_fast() users to always do the right thing either then we want this check in iommu_iotlb_sync() anyway, at which point the iommu_unmap() check would really only serve to skip a bit more unnecessary work on error cleanup paths, and do we really care about optimising errors? Hence I'm satisfied that this patch does in fact seem to be the best option.
Quoted 8 lines
Quoted 7 lines
include/linux/iommu.h static inline void iommu_flush_iotlb_all(struct iommu_domain *domain) · line 984
Quoted 3 lines
include/linux/iommu.h +2
-1-if (domain->ops->iotlb_sync) +if (domain->ops->iotlb_sync && +likely (iotlb_gather->start < iotlb_gather->end))Elsewhere we just use "
gather->end!= 0" as the "non-empty" condition; how concerned are we about defending against more-intentionally malformed gathers here?
Quoted 8 lines
I choose this deliberately to protect the driver, a malformed gather that is 0 sized, or negative sized looks like it will have Weird Things happen in drivers.
We could further classify the < and
WARN_ONthe malformed cases, but I don't want to pass negative sized gathers into drivers. We'd probably also have to de-inline the function if more is added. Do you have a preference?
No, that's fine, I just wanted to confirm the intent - this isn't a place where we should need to be concerned about micro-optimising to maybe save a load and an extra ALU op or two, just that I don't think it's worth doing any more than strictly necessary for our own robustness. Thus there's no need to change the check in iommu_iotlb_gather_is_disjoint() either, as that now just serves to skip the redundant reinitialisation of an already-empty gather, which is justifiably a different thing from the actual validity-of-sync condition anyway.
Cheers,
Robin.
Re: [PATCH rc 1/2] iommu: Do not call drivers for empty gathers
Hi,
Quoted 15 lines · Robin Murphy
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Robin Murphy
Quoted 4 lines
Further, there are several callers that can trigger empty gathers, especially in unusual conditions. For example
iommu_map_nosync()will call a 0 size unmap on some error paths. Also in VFIO, iommupt and other places.Quoted 6 lines
Quoted 3 lines
My instinct is still to tidy up the 0-length unmap case(s), but I guess
iommu_iotlb_sync()is itself also a public API where being more robust against erroneous usage is no bad thing.I also wanted to do that but found enough problematic cases I lost confidence I could reliably catch them all..
Quoted 8 lines
I reckon an early "if (!size) return 0;" in
iommu_unmap()would suffice to cover the internal error cleanup paths and most careless external users. However if we don't trustiommu_unmap_fast()users to always do the right thing either then we want this check iniommu_iotlb_sync()anyway, at which point theiommu_unmap()check would really only serve to skip a bit more unnecessary work on error cleanup paths, and do we really care about optimising errors? Hence I'm satisfied that this patch does in fact seem to be the best option.
Is this patch going to be applied soon to one of your branches? I'm waiting for that to happen because I want to cherry-pick it and propose as a temporary fix to be applied to our core-for-CI sub-branch of drm-tip until the original lands in mainline.
Thanks,
Janusz
Quoted 30 lines
Quoted 9 lines
Quoted 8 lines
include/linux/iommu.h static inline void iommu_flush_iotlb_all(struct iommu_domain *domain) · line 984
Quoted 4 lines
include/linux/iommu.h +2
-1-if (domain->ops->iotlb_sync) +if (domain->ops->iotlb_sync && +likely (iotlb_gather->start < iotlb_gather->end))Elsewhere we just use "
gather->end!= 0" as the "non-empty" condition; how concerned are we about defending against more-intentionally malformed gathers here?Quoted 8 lines
I choose this deliberately to protect the driver, a malformed gather that is 0 sized, or negative sized looks like it will have Weird Things happen in drivers.
We could further classify the < and
WARN_ONthe malformed cases, but I don't want to pass negative sized gathers into drivers. We'd probably also have to de-inline the function if more is added. Do you have a preference?No, that's fine, I just wanted to confirm the intent - this isn't a place where we should need to be concerned about micro-optimising to maybe save a load and an extra ALU op or two, just that I don't think it's worth doing any more than strictly necessary for our own robustness. Thus there's no need to change the check in
iommu_iotlb_gather_is_disjoint()either, as that now just serves to skip the redundant reinitialisation of an already-empty gather, which is justifiably a different thing from the actual validity-of-sync condition anyway.Cheers,
Robin.
Re: [PATCH rc 1/2] iommu: Do not call drivers for empty gathers
In reply to Jason GunthorpeQuoted 44 lines · Jason Gunthorpe
An empty gather is coded with start=U64_MAX, end=0 and several drivers go on to convert that to a size with:
end - start + 1
Which gives 2 for an empty gather. This then causes Weird Stuff to happen (for example an UBSAN splat in VT-d) that is hopefully harmless, but maybe not.
Prevent drivers from being called right in
iommu_iotlb_sync().Auditing shows that AMD, Intel, Mediatek and RSIC-V drivers all do things on these empty gathers.
Further, there are several callers that can trigger empty gathers, especially in unusual conditions. For example
iommu_map_nosync()will call a 0 size unmap on some error paths. Also in VFIO, iommupt and other places.Reported-by Janusz Krzysztofik <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>---
include/linux/iommu.h| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)include/linux/iommu.h +2
-1@@ -980,7 +980,8 @@ static inline void iommu_flush_iotlb_all(struct iommu_domain *domain)static inline void iommu_iotlb_sync (struct iommu_domain *domain,struct iommu_iotlb_gather *iotlb_gather) { -if (domain->ops->iotlb_sync) +if (domain->ops->iotlb_sync && +likely (iotlb_gather->start < iotlb_gather->end)) domain->ops->iotlb_sync (domain, iotlb_gather);iommu_iotlb_gather_init (iotlb_gather);Signature · 1 line
2.43.0
Re: [PATCH rc 1/2] iommu: Do not call drivers for empty gathers
In reply to Jason GunthorpeQuoted 19 lines · Jason Gunthorpe
An empty gather is coded with start=U64_MAX, end=0 and several drivers go on to convert that to a size with:
end - start + 1
Which gives 2 for an empty gather. This then causes Weird Stuff to happen (for example an UBSAN splat in VT-d) that is hopefully harmless, but maybe not.
Prevent drivers from being called right in
iommu_iotlb_sync().Auditing shows that AMD, Intel, Mediatek and RSIC-V drivers all do things on these empty gathers.
Further, there are several callers that can trigger empty gathers, especially in unusual conditions. For example
iommu_map_nosync()will call a 0 size unmap on some error paths. Also in VFIO, iommupt and other places.
Quoted 4 lines
Reported-by Janusz Krzysztofik<[email protected]>Closes https://lore.kernel.org/r/11145826.aFP6jjVeTY@jkrzyszt- mobl2.ger.corp.intel.com open hereSigned-off-by Jason Gunthorpe<[email protected]>
Jon Hunter
maintainerRe: [PATCH rc 1/2] iommu: Do not call drivers for empty gathers
In reply to Jason GunthorpeHi Jason,
Quoted 32 lines · Jason Gunthorpe
An empty gather is coded with start=U64_MAX, end=0 and several drivers go on to convert that to a size with:
end - start + 1
Which gives 2 for an empty gather. This then causes Weird Stuff to happen (for example an UBSAN splat in VT-d) that is hopefully harmless, but maybe not.
Prevent drivers from being called right in
iommu_iotlb_sync().Auditing shows that AMD, Intel, Mediatek and RSIC-V drivers all do things on these empty gathers.
Further, there are several callers that can trigger empty gathers, especially in unusual conditions. For example
iommu_map_nosync()will call a 0 size unmap on some error paths. Also in VFIO, iommupt and other places.Reported-by Janusz Krzysztofik <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>--- include/linux/iommu.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/linux/iommu.h +0
-0
include/linux/iommu.h static inline void iommu_flush_iotlb_all(struct iommu_domain *domain) · line 987
Quoted 9 lines
include/linux/iommu.h +2
-1@@ -980,7 +980,8 @@ static inline void iommu_flush_iotlb_all(struct iommu_domain *domain)static inline void iommu_iotlb_sync (struct iommu_domain *domain,struct iommu_iotlb_gather *iotlb_gather) { -if (domain->ops->iotlb_sync) +if (domain->ops->iotlb_sync && +likely (iotlb_gather->start < iotlb_gather->end)) domain->ops->iotlb_sync (domain, iotlb_gather);iommu_iotlb_gather_init (iotlb_gather);
I noticed that a couple of our Tegra boards are no longer booting -next and bisect pointed to this commit. Reverting this does fix it.
This is impacting our Tegra186 (using nvidia,tegra186-smmu compatible string) and Tegra194 (using nvidia,tegra194-smmu compatible string) boards. There is no specific crash I see, but the boards just appear to hang on boot. If you have any thoughts or things to try let me know.
Thanks!
Jon
Signature · 1 line
nvpublic
Re: [PATCH rc 1/2] iommu: Do not call drivers for empty gathers
Quoted 7 lines · Jon Hunter
I noticed that a couple of our Tegra boards are no longer booting -next and bisect pointed to this commit. Reverting this does fix it.
This is impacting our Tegra186 (using nvidia,tegra186-smmu compatible string) and Tegra194 (using nvidia,tegra194-smmu compatible string) boards. There is no specific crash I see, but the boards just appear to hang on boot. If you have any thoughts or things to try let me know.
Thanks, I was able to guess what the problem is:
https://patch.msgid.link/r/[email protected]
Please let me know if it doesn't solve the problem
Jason
PATCH 2/2 iommupt: Fix short gather if the unmap goes into a large mapping
In reply to Jason Gunthorpeunmap has the odd behavior that it can unmap more than requested if the ending point lands within the middle of a large or contiguous IOPTE.
In this case the gather should flush everything unmapped which can be larger than what was requested to be unmapped. The gather was only flushing the range requested to be unmapped, not extending to the extra range, resulting in a short invalidation if the caller hits this special condition.
This was found by the new invalidation/gather test I am adding in preparation for ARMv8. Claude deduced the root cause.
As far as I remember nothing relies on unmapping a large entry, so this is likely not a triggerable bug.
--- drivers/iommu/generic_pt/iommu_pt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/generic_pt/iommu_pt.h +1 -1
@@ -1057,7 +1057,7 @@ size_t DOMAIN_NS(unmap_pages)(struct iommu_domain *domain, unsigned long iova,pt_walk_range (&range, __unmap_range, &unmap); -gather_range_pages (iotlb_gather, iommu_table, iova, len, +gather_range_pages (iotlb_gather, iommu_table, iova, unmap.unmapped, &unmap.free_list);return unmap.unmapped;
Signature · 1 line
2.43.0
Re: [PATCH rc 2/2] iommupt: Fix short gather if the unmap goes into a large mapping
Quoted 12 lines · Jason Gunthorpe
unmap has the odd behavior that it can unmap more than requested if the ending point lands within the middle of a large or contiguous IOPTE.
In this case the gather should flush everything unmapped which can be larger than what was requested to be unmapped. The gather was only flushing the range requested to be unmapped, not extending to the extra range, resulting in a short invalidation if the caller hits this special condition.
This was found by the new invalidation/gather test I am adding in preparation for ARMv8. Claude deduced the root cause.
Quoted 6 lines
As far as I remember nothing relies on unmapping a large entry, so this is likely not a triggerable bug.
Fixes 7c53f4238aa8 ("iommupt: Add unmap_pages op")Signed-off-by Jason Gunthorpe <[email protected]>
-Vasant
Re: [PATCH rc 2/2] iommupt: Fix short gather if the unmap goes into a large mapping
In reply to Jason GunthorpeQuoted 38 lines · Jason Gunthorpe
unmap has the odd behavior that it can unmap more than requested if the ending point lands within the middle of a large or contiguous IOPTE.
In this case the gather should flush everything unmapped which can be larger than what was requested to be unmapped. The gather was only flushing the range requested to be unmapped, not extending to the extra range, resulting in a short invalidation if the caller hits this special condition.
This was found by the new invalidation/gather test I am adding in preparation for ARMv8. Claude deduced the root cause.
As far as I remember nothing relies on unmapping a large entry, so this is likely not a triggerable bug.
Fixes 7c53f4238aa8 ("iommupt: Add unmap_pages op")Signed-off-by Jason Gunthorpe <[email protected]>---
drivers/iommu/generic_pt/iommu_pt.h| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)drivers/iommu/generic_pt/iommu_pt.h +1
-1@@ -1057,7 +1057,7 @@ size_t DOMAIN_NS(unmap_pages)(struct iommu_domain *domain, unsigned long iova,pt_walk_range (&range, __unmap_range, &unmap); -gather_range_pages (iotlb_gather, iommu_table, iova, len, +gather_range_pages (iotlb_gather, iommu_table, iova, unmap.unmapped, &unmap.free_list);return unmap.unmapped;Signature · 1 line
2.43.0
Re: [PATCH rc 2/2] iommupt: Fix short gather if the unmap goes into a large mapping
In reply to Jason GunthorpeQuoted 12 lines · Jason Gunthorpe
unmap has the odd behavior that it can unmap more than requested if the ending point lands within the middle of a large or contiguous IOPTE.
In this case the gather should flush everything unmapped which can be larger than what was requested to be unmapped. The gather was only flushing the range requested to be unmapped, not extending to the extra range, resulting in a short invalidation if the caller hits this special condition.
This was found by the new invalidation/gather test I am adding in preparation for ARMv8. Claude deduced the root cause.
Quoted 6 lines
As far as I remember nothing relies on unmapping a large entry, so this is likely not a triggerable bug.
Fixes 7c53f4238aa8 ("iommupt: Add unmap_pages op")Signed-off-by Jason Gunthorpe<[email protected]>