iommu: Clear IOMMU DMA ops when detaching a device from its IOMMU group
Subsystems (2)
PATCH 1/1 iommu: Clear IOMMU DMA ops when detaching a device from its IOMMU group
In recent changes to the IOMMU subsystem seen in v6.15-rc1, the process and order of how IOMMU linked devices are brought up has changed. Now when the IOMMU is probed, the IOMMU subsystem core will attempt to link or attach devices described in the device tree as using the IOMMU to the IOMMU's associated group or domain. If any part of this fails, the whole process is (mostly) undone. The IOMMU failure is then treated as a deferred probe; after sufficient time, the deferred probe times out and the attached devices are brought out without the IOMMU.
In the process of undoing changes, one part was missed. When a device is initialized for use with the IOMMU, iommu_setup_dma_ops() is also called, which signals that DMA operations should go through the IOMMU. This part was not reverted when the IOMMU changes were undone. When the device later probes without the IOMMU, DMA operations would attempt to use IOMMU operations with no IOMMU domain present, causing a crash.
The above was observed on an MT8188 Chromebook. The MT8188 device tree had incorrectly described the IOMMU endpoint for part of its display pipeline; the IOMMU driver would successfully attach a couple hardware blocks, fail at the incorrectly described one, and roll back all changes. Later when the deferred probe times out, the display pipleine probes without the IOMMU, but when a framebuffer is allocated, it goes through the IOMMU DMA ops, causing a NULL pointer dereference crash.
Add a helper that is the opposite of iommu_setup_dma_ops(), and call it when the IOMMU-enabled device is being detached from its IOMMU.
---
This patch should be applied for v6.15. It's not immediately clear to me which commit is the actual cause, so I did not add a Fixes tag.
--- drivers/iommu/dma-iommu.c | 5 +++++ drivers/iommu/dma-iommu.h | 5 +++++ drivers/iommu/iommu.c | 2 ++ 3 files changed, 12 insertions(+)
drivers/iommu/dma-iommu.c +5 -0
@@ -1739,6 +1739,11 @@ void iommu_setup_dma_ops(struct device *dev) dev->dma_iommu = false; } +void iommu_clear_dma_ops (struct device *dev) +{ + dev->dma_iommu = false; +} +static bool has_msi_cookie (const struct iommu_domain *domain) { return domain && (domain->cookie_type == IOMMU_COOKIE_DMA_IOVA ||
drivers/iommu/dma-iommu.h +5 -0
@@ -10,6 +10,7 @@#ifdef CONFIG_IOMMU_DMA void iommu_setup_dma_ops (struct device *dev); +void iommu_clear_dma_ops (struct device *dev);int iommu_get_dma_cookie (struct iommu_domain *domain);void iommu_put_dma_cookie (struct iommu_domain *domain);
@@ -30,6 +31,10 @@ static inline void iommu_setup_dma_ops(struct device *dev) { } +static inline void iommu_clear_dma_ops (struct device *dev) +{ +} +static inline int iommu_dma_init_fq (struct iommu_domain *domain) {return -EINVAL ;
drivers/iommu/iommu.c +2 -0
@@ -677,6 +677,8 @@ static void __iommu_group_remove_device(struct device *dev)struct iommu_group *group = dev->iommu_group;struct group_device *device; +iommu_clear_dma_ops (dev); +mutex_lock (&group->mutex);for_each_group_device (group, device) {if (device->dev != dev)
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
Quoted 24 lines · Chen-Yu Tsai
In recent changes to the IOMMU subsystem seen in v6.15-rc1, the process and order of how IOMMU linked devices are brought up has changed. Now when the IOMMU is probed, the IOMMU subsystem core will attempt to link or attach devices described in the device tree as using the IOMMU to the IOMMU's associated group or domain. If any part of this fails, the whole process is (mostly) undone. The IOMMU failure is then treated as a deferred probe; after sufficient time, the deferred probe times out and the attached devices are brought out without the IOMMU.
In the process of undoing changes, one part was missed. When a device is initialized for use with the IOMMU,
iommu_setup_dma_ops()is also called, which signals that DMA operations should go through the IOMMU. This part was not reverted when the IOMMU changes were undone. When the device later probes without the IOMMU, DMA operations would attempt to use IOMMU operations with no IOMMU domain present, causing a crash.The above was observed on an MT8188 Chromebook. The MT8188 device tree had incorrectly described the IOMMU endpoint for part of its display pipeline; the IOMMU driver would successfully attach a couple hardware blocks, fail at the incorrectly described one, and roll back all changes. Later when the deferred probe times out, the display pipleine probes without the IOMMU, but when a framebuffer is allocated, it goes through the IOMMU DMA ops, causing a NULL pointer dereference crash.
Quoted 8 lines
Add a helper that is the opposite of
iommu_setup_dma_ops(), and call it when the IOMMU-enabled device is being detached from its IOMMU.Signed-off-by Chen-Yu Tsai<[email protected]>---
This patch should be applied for v6.15. It's not immediately clear to me which commit is the actual cause, so I did not add a Fixes tag.
I guess it might be:
Or, something near that. Please verify before apply.
Quoted 50 lines
--- drivers/iommu/dma-iommu.c | 5 +++++ drivers/iommu/dma-iommu.h | 5 +++++ drivers/iommu/iommu.c | 2 ++ 3 files changed, 12 insertions(+)drivers/iommu/dma-iommu.c +5
-0@@ -1739,6 +1739,11 @@ void iommu_setup_dma_ops(struct device *dev) dev->dma_iommu = false; } +void iommu_clear_dma_ops (struct device *dev) +{ + dev->dma_iommu = false; +} +static bool has_msi_cookie (const struct iommu_domain *domain) { return domain && (domain->cookie_type == IOMMU_COOKIE_DMA_IOVA ||drivers/iommu/dma-iommu.h +5
-0@@ -10,6 +10,7 @@#ifdef CONFIG_IOMMU_DMA void iommu_setup_dma_ops (struct device *dev); +void iommu_clear_dma_ops (struct device *dev);int iommu_get_dma_cookie (struct iommu_domain *domain);void iommu_put_dma_cookie (struct iommu_domain *domain);@@ -30,6 +31,10 @@ static inline void iommu_setup_dma_ops(struct device *dev) { } +static inline void iommu_clear_dma_ops (struct device *dev) +{ +} +static inline int iommu_dma_init_fq (struct iommu_domain *domain) {return -EINVAL ;drivers/iommu/iommu.c +0
-0
drivers/iommu/iommu.c static void __iommu_group_remove_device(struct device *dev) · line 684
Quoted 8 lines
drivers/iommu/iommu.c +2
-0@@ -677,6 +677,8 @@ static void __iommu_group_remove_device(struct device *dev)struct iommu_group *group = dev->iommu_group;struct group_device *device; +iommu_clear_dma_ops (dev); +mutex_lock (&group->mutex);for_each_group_device (group, device) {if (device->dev != dev)
I don't think there needs to be an inline helper for this one-time call. Perhaps you can simply do it like this:
drivers/iommu/iommu.c +1 -0
@@ -536,6 +536,7 @@ static void iommu_deinit_device(struct device *dev)/* Caller must put iommu_group */ dev->iommu_group =NULL ; + dev->dma_iommu = false;module_put (ops->owner);dev_iommu_free (dev); }
?
Thanks,
baolu
Quoted 36 lines · Baolu Lu
Quoted 27 lines · Chen-Yu Tsai
In recent changes to the IOMMU subsystem seen in v6.15-rc1, the process and order of how IOMMU linked devices are brought up has changed. Now when the IOMMU is probed, the IOMMU subsystem core will attempt to link or attach devices described in the device tree as using the IOMMU to the IOMMU's associated group or domain. If any part of this fails, the whole process is (mostly) undone. The IOMMU failure is then treated as a deferred probe; after sufficient time, the deferred probe times out and the attached devices are brought out without the IOMMU.
In the process of undoing changes, one part was missed. When a device is initialized for use with the IOMMU,
iommu_setup_dma_ops()is also called, which signals that DMA operations should go through the IOMMU. This part was not reverted when the IOMMU changes were undone. When the device later probes without the IOMMU, DMA operations would attempt to use IOMMU operations with no IOMMU domain present, causing a crash.The above was observed on an MT8188 Chromebook. The MT8188 device tree had incorrectly described the IOMMU endpoint for part of its display pipeline; the IOMMU driver would successfully attach a couple hardware blocks, fail at the incorrectly described one, and roll back all changes. Later when the deferred probe times out, the display pipleine probes without the IOMMU, but when a framebuffer is allocated, it goes through the IOMMU DMA ops, causing a NULL pointer dereference crash.
Add a helper that is the opposite of
iommu_setup_dma_ops(), and call it when the IOMMU-enabled device is being detached from its IOMMU.Quoted 6 lines
Signed-off-by Chen-Yu Tsai<[email protected]>---
This patch should be applied for v6.15. It's not immediately clear to me which commit is the actual cause, so I did not add a Fixes tag.
Quoted 6 lines
I guess it might be:
Fixes bcb81ac6ae3c ("iommu: Get DT/ACPI parsing into the proper probepath")
Or, something near that. Please verify before apply.
I can't really tell.
Quoted 67 lines
Quoted 50 lines
--- drivers/iommu/dma-iommu.c | 5 +++++ drivers/iommu/dma-iommu.h | 5 +++++ drivers/iommu/iommu.c | 2 ++ 3 files changed, 12 insertions(+)drivers/iommu/dma-iommu.c +5
-0@@ -1739,6 +1739,11 @@ void iommu_setup_dma_ops(struct device *dev) dev->dma_iommu = false; } +void iommu_clear_dma_ops (struct device *dev) +{ + dev->dma_iommu = false; +} +static bool has_msi_cookie (const struct iommu_domain *domain) { return domain && (domain->cookie_type == IOMMU_COOKIE_DMA_IOVA ||drivers/iommu/dma-iommu.h +5
-0@@ -10,6 +10,7 @@#ifdef CONFIG_IOMMU_DMA void iommu_setup_dma_ops (struct device *dev); +void iommu_clear_dma_ops (struct device *dev);int iommu_get_dma_cookie (struct iommu_domain *domain);void iommu_put_dma_cookie (struct iommu_domain *domain);@@ -30,6 +31,10 @@ static inline void iommu_setup_dma_ops(struct device *dev) { } +static inline void iommu_clear_dma_ops (struct device *dev) +{ +} +static inline int iommu_dma_init_fq (struct iommu_domain *domain) {return -EINVAL ;drivers/iommu/iommu.c +0
-0drivers/iommu/iommu.c static void __iommu_group_remove_device(struct device *dev) · line 684
Quoted 8 lines
drivers/iommu/iommu.c +2
-0@@ -677,6 +677,8 @@ static void __iommu_group_remove_device(struct device *dev)struct iommu_group *group = dev->iommu_group;struct group_device *device; +iommu_clear_dma_ops (dev); +mutex_lock (&group->mutex);for_each_group_device (group, device) {if (device->dev != dev)I don't think there needs to be an inline helper for this one-time call. Perhaps you can simply do it like this:
drivers/iommu/iommu.c +0
-0
drivers/iommu/iommu.c static void iommu_deinit_device(struct device *dev) · line 539
Quoted 4 lines
drivers/iommu/iommu.c +1
-0@@ -536,6 +536,7 @@ static void iommu_deinit_device(struct device *dev)/* Caller must put iommu_group */ dev->iommu_group =NULL ; + dev->dma_iommu = false;
I wanted to keep the reverting part in the cleanup path corresponding to where it was set up, with corresponding helpers, so that they are symmetric. That said I'm not that familiar with the IOMMU code, but it makes more sense to me than sticking a random line somewhere.
ChenYu
Quoted 8 lines
drivers/iommu/iommu.c +0
-0module_put (ops->owner);dev_iommu_free (dev); }?
Thanks,
baolu
Robin Murphy
maintainerQuoted 24 lines · Chen-Yu Tsai
In recent changes to the IOMMU subsystem seen in v6.15-rc1, the process and order of how IOMMU linked devices are brought up has changed. Now when the IOMMU is probed, the IOMMU subsystem core will attempt to link or attach devices described in the device tree as using the IOMMU to the IOMMU's associated group or domain. If any part of this fails, the whole process is (mostly) undone. The IOMMU failure is then treated as a deferred probe; after sufficient time, the deferred probe times out and the attached devices are brought out without the IOMMU.
In the process of undoing changes, one part was missed. When a device is initialized for use with the IOMMU,
iommu_setup_dma_ops()is also called, which signals that DMA operations should go through the IOMMU. This part was not reverted when the IOMMU changes were undone. When the device later probes without the IOMMU, DMA operations would attempt to use IOMMU operations with no IOMMU domain present, causing a crash.The above was observed on an MT8188 Chromebook. The MT8188 device tree had incorrectly described the IOMMU endpoint for part of its display pipeline; the IOMMU driver would successfully attach a couple hardware blocks, fail at the incorrectly described one, and roll back all changes. Later when the deferred probe times out, the display pipleine probes without the IOMMU, but when a framebuffer is allocated, it goes through the IOMMU DMA ops, causing a NULL pointer dereference crash.
Quoted 2 lines
Add a helper that is the opposite of
iommu_setup_dma_ops(), and call it when the IOMMU-enabled device is being detached from its IOMMU.
Apologies for the crossover - seems you sent this just as I got to finishing up the response and patch I'd left open on my office machine the other day... and now I find that I forgot to actually send *this* mail when I wrote it yesterday either, sigh :(
As per my version I don't think this is really a fix as there was never a consistent well-defined behaviour to begin with. From an arm64 perspective I think it would have last changed with b67483b3c44e ("iommu/dma: Centralise iommu_setup_dma_ops()"), but that's not the case for other architectures/drivers which either already had this condition beforehand or still didn't afterwards.
Thanks,
Robin.
Quoted 63 lines
Closes https://lore.kernel.org/all/CAGXv+5HJpTYmQ2h-GD7GjyeYT7bL9EBCvu0mz5LgpzJZtzfW0w@mail.gmail.com/ open hereSigned-off-by Chen-Yu Tsai <[email protected]>---
This patch should be applied for v6.15. It's not immediately clear to me which commit is the actual cause, so I did not add a Fixes tag.--- drivers/iommu/dma-iommu.c | 5 +++++ drivers/iommu/dma-iommu.h | 5 +++++ drivers/iommu/iommu.c | 2 ++ 3 files changed, 12 insertions(+)drivers/iommu/dma-iommu.c +5
-0@@ -1739,6 +1739,11 @@ void iommu_setup_dma_ops(struct device *dev) dev->dma_iommu = false; } +void iommu_clear_dma_ops (struct device *dev) +{ + dev->dma_iommu = false; +} +static bool has_msi_cookie (const struct iommu_domain *domain) { return domain && (domain->cookie_type == IOMMU_COOKIE_DMA_IOVA ||drivers/iommu/dma-iommu.h +5
-0@@ -10,6 +10,7 @@#ifdef CONFIG_IOMMU_DMA void iommu_setup_dma_ops (struct device *dev); +void iommu_clear_dma_ops (struct device *dev);int iommu_get_dma_cookie (struct iommu_domain *domain);void iommu_put_dma_cookie (struct iommu_domain *domain);@@ -30,6 +31,10 @@ static inline void iommu_setup_dma_ops(struct device *dev) { } +static inline void iommu_clear_dma_ops (struct device *dev) +{ +} +static inline int iommu_dma_init_fq (struct iommu_domain *domain) {return -EINVAL ;drivers/iommu/iommu.c +2
-0@@ -677,6 +677,8 @@ static void __iommu_group_remove_device(struct device *dev)struct iommu_group *group = dev->iommu_group;struct group_device *device; +iommu_clear_dma_ops (dev); +mutex_lock (&group->mutex);for_each_group_device (group, device) {if (device->dev != dev)