Skip to contentSkip to conversation

xen/arm: Fix under-mapping in map_range_to_domain()

PATCH In review Michal Orzel 2 participants 6 messages Oct 7 – Oct 21

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: Fix under-mapping in map_range_to_domain()

If the memory region to be mapped does not start at the page boundary and its end spans over the page boundary, map_range_to_domain() passes a number of pages that is short by one to map_regions_p2mt(). Domain's attempt to access memory within that page will fail. Fix it by calculating the number of pages to be mapped taking into account the start offset.

Fixes 88c10b0d6eee ("arch/arm: let map_mmio_regions() use start and count")
---
 xen/arch/arm/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/device.c +1 -1
index b11366d722ef..ad50838b9f11 100644
@@ -89,7 +89,7 @@ int __overlay_init map_range_to_domain(const struct dt_device_node *dev,
     {
         res = map_regions_p2mt(d,
                                gaddr_to_gfn(addr),
-                               PFN_UP(len),
+                               PFN_UP(addr + len) - PFN_DOWN(addr),
                                maddr_to_mfn(addr),
                                mr_data->p2mt);
 
Signature · 1 line
2.43.0
#1 lore raw

Hi Michal,

Quoted 5 lines · Michal Orzel

If the memory region to be mapped does not start at the page boundary and its end spans over the page boundary, map_range_to_domain() passes a number of pages that is short by one to map_regions_p2mt(). Domain's attempt to access memory within that page will fail. Fix it by calculating the number of pages to be mapped taking into account the start offset.

Can you provide an example where the caller doesn't suitably align?

Quoted 3 lines
Fixes 88c10b0d6eee ("arch/arm: let map_mmio_regions() use start and count")

The fix LGTM in the current context. However given map_range_to_domain() is now used in more places (e.g. overlay), I wonder whether this is the right decision to allow map_range_to_domain() with non-aligned region.

Imagine a user say "I want to map 0x400 - 0x1000 to domain A and map domain B "0x0 - 0x400". This could result to interesting behavior if the user is not aware of the alignment (this would become even more problematic if we decide to ever support different page granularity on Arm).

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 10 lines · Julien

Hi Michal,

Quoted 5 lines · Michal Orzel

If the memory region to be mapped does not start at the page boundary and its end spans over the page boundary, map_range_to_domain() passes a number of pages that is short by one to map_regions_p2mt(). Domain's attempt to access memory within that page will fail. Fix it by calculating the number of pages to be mapped taking into account the start offset.

Can you provide an example where the caller doesn't suitably align?

Sure. Example is handle_device() being called on the hwdom DTB creation time for a node whose address is not page aligned i.e. /reserved-memory node or basically any node, given that DT spec does not mention anything about memory region alignment and there are existing DTs like that. With reg = <0x0 0x800 0x0 0x1000>, today we only map 0x0-0xfff and an access to 0x1000-0x17ff faults.

Quoted 5 lines
Quoted 4 lines
Fixes 88c10b0d6eee ("arch/arm: let map_mmio_regions() use start and count")
Quoted 8 lines

The fix LGTM in the current context. However given map_range_to_domain() is now used in more places (e.g. overlay), I wonder whether this is the right decision to allow map_range_to_domain() with non-aligned region.

Imagine a user say "I want to map 0x400 - 0x1000 to domain A and map domain B "0x0 - 0x400". This could result to interesting behavior if the user is not aware of the alignment (this would become even more problematic if we decide to ever support different page granularity on Arm).

I agree this is not great, but this is not something introduced here:
iomem_permit_access() and the iomem_ranges rangeset already cover whole pages and we already map the full first page. This patch only makes the p2m mapping match what we already allow.

I don't think we can reject unaligned regions in map_range_to_domain() though, as the host DT is not under our control and hwdom would fail to boot on platforms with sub-page devices. I asked AI to look at the Linux boards for Arm and Arm64 (140 boards in total with unaligned regions) and even the boards we claim to support have nodes that would fail:
- RPI4: hdmi@7ef00700, <0x7ef01f00 0x400>
- IMX8QXP: clock-controller@5b290004, <0x5b290004 0x10000>

~Michal

#3 lore raw

Hi Michal,

Quoted 12 lines · Michal
Quoted 9 lines · Julien

Hi Michal,

Quoted 5 lines · Michal Orzel

If the memory region to be mapped does not start at the page boundary and its end spans over the page boundary, map_range_to_domain() passes a number of pages that is short by one to map_regions_p2mt(). Domain's attempt to access memory within that page will fail. Fix it by calculating the number of pages to be mapped taking into account the start offset.

Quoted 6 lines
Quoted 1 line

Can you provide an example where the caller doesn't suitably align?

Sure. Example is handle_device() being called on the hwdom DTB creation time for a node whose address is not page aligned i.e. /reserved-memory node or basically any node, given that DT spec does not mention anything about memory region alignment and there are existing DTs like that. With reg = <0x0 0x800 0x0 0x1000>, today we only map 0x0-0xfff and an access to 0x1000-0x17ff faults.

Thanks!

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines
Fixes 88c10b0d6eee ("arch/arm: let map_mmio_regions() use start and count")

The fix LGTM in the current context. However given map_range_to_domain() is now used in more places (e.g. overlay), I wonder whether this is the right decision to allow map_range_to_domain() with non-aligned region.

Quoted 8 lines
Quoted 4 lines

Imagine a user say "I want to map 0x400 - 0x1000 to domain A and map domain B "0x0 - 0x400". This could result to interesting behavior if the user is not aware of the alignment (this would become even more problematic if we decide to ever support different page granularity on Arm).

I agree this is not great, but this is not something introduced here:
iomem_permit_access() and the iomem_ranges rangeset already cover whole pages and we already map the full first page.This patch only makes the p2m mapping match what we already allow.

I am possibly misinterpreting what you wrote, but I disagree with such statement. The fact existing code may be wrong doesn't mean we should continue with the wrongness.

Quoted 8 lines

I don't think we can reject unaligned regions in map_range_to_domain() though, as the host DT is not under our control and hwdom would fail to boot on platforms with sub-page devices. I asked AI to look at the Linux boards for Arm and Arm64 (140 boards in total with unaligned regions) and even the boards we claim to support have nodes that would fail:
- RPI4: hdmi@7ef00700, <0x7ef01f00 0x400>
- IMX8QXP: clock-controller@5b290004, <0x5b290004 0x10000>

I am not suggesting to reject unaligned regions when mapped into the hardware domain. I am suggesting to reject it when we want to passthrough to another domain. So we don't end up in a situation two different devices on the same page are passthrough to two different domains (or between Xen and a domain).

Codewise, it would mean map_range_to_domain() would enforce the alignment and the caller will be left to decide what to do. We could also decide about the alignment base don whether this is the HW domain or not within the function.

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw
Quoted 37 lines · Julien Grall

Hi Michal,

Quoted 12 lines · Michal
Quoted 9 lines · Julien

Hi Michal,

Quoted 5 lines · Michal Orzel

If the memory region to be mapped does not start at the page boundary and its end spans over the page boundary, map_range_to_domain() passes a number of pages that is short by one to map_regions_p2mt(). Domain's attempt to access memory within that page will fail. Fix it by calculating the number of pages to be mapped taking into account the start offset.

Quoted 6 lines
Quoted 1 line

Can you provide an example where the caller doesn't suitably align?

Sure. Example is handle_device() being called on the hwdom DTB creation time for a node whose address is not page aligned i.e. /reserved-memory node or basically any node, given that DT spec does not mention anything about memory region alignment and there are existing DTs like that. With reg = <0x0 0x800 0x0 0x1000>, today we only map 0x0-0xfff and an access to 0x1000-0x17ff faults.

Thanks!

Quoted 14 lines
Quoted 14 lines
Quoted 5 lines
Fixes 88c10b0d6eee ("arch/arm: let map_mmio_regions() use start and count")

The fix LGTM in the current context. However given map_range_to_domain() is now used in more places (e.g. overlay), I wonder whether this is the right decision to allow map_range_to_domain() with non-aligned region.

Imagine a user say "I want to map 0x400 - 0x1000 to domain A and map domain B "0x0 - 0x400". This could result to interesting behavior if the user is not aware of the alignment (this would become even more problematic if we decide to ever support different page granularity on Arm).

Quoted 8 lines
Quoted 4 lines

I agree this is not great, but this is not something introduced here:
iomem_permit_access() and the iomem_ranges rangeset already cover whole pages and we already map the full first page.This patch only makes the p2m mapping match what we already allow.

I am possibly misinterpreting what you wrote, but I disagree with such statement. The fact existing code may be wrong doesn't mean we should continue with the wrongness.

It was just to clarify that this patch does not change who can access the page.

Quoted 16 lines
Quoted 9 lines

I don't think we can reject unaligned regions in map_range_to_domain() though, as the host DT is not under our control and hwdom would fail to boot on platforms with sub-page devices. I asked AI to look at the Linux boards for Arm and Arm64 (140 boards in total with unaligned regions) and even the boards we claim to support have nodes that would fail:
- RPI4: hdmi@7ef00700, <0x7ef01f00 0x400>
- IMX8QXP: clock-controller@5b290004, <0x5b290004 0x10000>

I am not suggesting to reject unaligned regions when mapped into the hardware domain. I am suggesting to reject it when we want to passthrough to another domain. So we don't end up in a situation two different devices on the same page are passthrough to two different domains (or between Xen and a domain).

Quoted 4 lines

Codewise, it would mean map_range_to_domain() would enforce the alignment and the caller will be left to decide what to do. We could also decide about the alignment base don whether this is the HW domain or not within the function.

For passthrough this is already the case. map_range_to_domain() does not map anything for nodes with xen,passthrough and the domU mapping is done by handle_passthrough_prop(), which rejects mstart, gstart and size that are not page aligned.

The remaining case is the overlay which can be attached to any 1:1 domain and this goes through handle_device() -> map_range_to_domain() without any alignment check. All the other callers operate on hwdom, so I think we could reject a region that is not page aligned if d is not the hardware domain, before granting the iomem permission in map_range_to_domain().

Shouldn't we accept this patch as is so that we can backport (overlay is not supported) it as a fix and then add a second one with a rejection mentioned above that will help the overlay (and any future) case?

~Michal

Acked-by
#5 lore raw

Hi Michal,

Sorry for the late reply.

Quoted 30 lines · Michal
Quoted 15 lines
Quoted 8 lines

I don't think we can reject unaligned regions in map_range_to_domain() though, as the host DT is not under our control and hwdom would fail to boot on platforms with sub-page devices. I asked AI to look at the Linux boards for Arm and Arm64 (140 boards in total with unaligned regions) and even the boards we claim to support have nodes that would fail:
- RPI4: hdmi@7ef00700, <0x7ef01f00 0x400>
- IMX8QXP: clock-controller@5b290004, <0x5b290004 0x10000>

I am not suggesting to reject unaligned regions when mapped into the hardware domain. I am suggesting to reject it when we want to passthrough to another domain. So we don't end up in a situation two different devices on the same page are passthrough to two different domains (or between Xen and a domain).

Quoted 4 lines

Codewise, it would mean map_range_to_domain() would enforce the alignment and the caller will be left to decide what to do. We could also decide about the alignment base don whether this is the HW domain or not within the function.

For passthrough this is already the case. map_range_to_domain() does not map anything for nodes with xen,passthrough and the domU mapping is done by handle_passthrough_prop(), which rejects mstart, gstart and size that are not page aligned.

The remaining case is the overlay which can be attached to any 1:1 domain and this goes through handle_device() -> map_range_to_domain() without any alignment check. All the other callers operate on hwdom, so I think we could reject a region that is not page aligned if d is not the hardware domain, before granting the iomem permission in map_range_to_domain().

Quoted 3 lines

Shouldn't we accept this patch as is so that we can backport (overlay is not supported) it as a fix and then add a second one with a rejection mentioned above that will help the overlay (and any future) case?

Thanks for the input. I didn't realise we would want to backport this patch. If so, I agree the split make sense.

Cheers,

Signature · 1 line
Julien Grall