memory: do not do out of bound notification
PATCH 1/1 memory: do not do out of bound notification
even if an entry overlaps with notifier's range, should not map/unmap out of bound part in the entry.
This would cause problem in below case:
1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from 0x3c000000 - 0x3c1fffff is in shadow page table.
2. in vfio, memory_region_register_iommu_notifier() is followed by memory_region_iommu_replay(), which will first call address space unmap, and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,
IOVAs from 0 - 0xffffffff get unmapped,
and IOVAs from 0x3c000000 - 0x3c1fffff get mapped
(2) for notifier 0xfef00000-0xffffffffffffffff
IOVAs from 0 - 0x7fffffffff get unmapped,
but IOVAs from 0x3c000000 - 0x3c1fffff cannot get mapped back.--- memory.c | 8 ++++++++ 1 file changed, 8 insertions(+)
memory.c +8 -0
@@ -1948,6 +1948,14 @@ void memory_region_notify_one(IOMMUNotifier *notifier,return ; } +if (entry->iova < notifier->start) { + entry->iova = notifier->start; + } + +if (entry->iova + entry->addr_mask > notifier->end) { + entry->addr_mask = notifier->end - entry->iova; + } +if (entry->perm &IOMMU_RW ) {printf ("map %lx %lx\n" , entry->iova, entry->iova + entry->addr_mask); request_flags =IOMMU_NOTIFIER_MAP ;
Signature · 1 line
2.7.4
Hi Yan,
[+ Peter]
Quoted 2 lines · Yan Zhao
even if an entry overlaps with notifier's range, should not map/unmap out of bound part in the entry.
I don't think the patch was based on the master as the trace at the very end if not part of the upstream code.
Quoted 6 lines
This would cause problem in below case:
1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from0x3c000000-0x3c1fffffis in shadow page table.
Quoted 6 lines
2. in vfio,
memory_region_register_iommu_notifier()is followed bymemory_region_iommu_replay(), which will first call address space unmap, and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,IOVAs from 0 - 0xffffffff get unmapped, and IOVAs from 0x3c000000 - 0x3c1fffff get mapped
While the patch looks sensible, the issue is the notifier scope used in vtd_address_space_unmap is not a valid mask (ctpop64(size) != 1). Then the size is recomputed (either using n = 64 - clz64(size) for the 1st notifier or n = s->aw_bits for the 2d) and also the entry (especially for the 2d notifier where it becomes 0) to get a proper alignment.
vtd_page_walk sends notifications per block or page (with valid addr_mask) so stays within the notifier.
Modifying the entry->iova/addr_mask again in memory_region_notify_one leads to unaligned start address / addr_mask. I don't think we want that.
Can't we modity the vtd_address_space_unmap() implementation to split the invalidation in smaller chunks instead?
Thanks
Eric
Quoted 28 lines
(2) for notifier 0xfef00000-0xffffffffffffffff
IOVAs from 0 - 0x7fffffffff get unmapped,> but IOVAs from 0x3c000000 - 0x3c1fffff cannot get mapped back.Signed-off-by Yan Zhao <[email protected]>--- memory.c | 8 ++++++++ 1 file changed, 8 insertions(+)memory.c +7
-0@@ -1948,6 +1948,14 @@ void memory_region_notify_one(IOMMUNotifier *notifier,return ; } +if (entry->iova < notifier->start) { + entry->iova = notifier->start; + } + +if (entry->iova + entry->addr_mask > notifier->end) { + entry->addr_mask = notifier->end - entry->iova;> + } +if (entry->perm &IOMMU_RW ) {printf ("map %lx %lx\n" , entry->iova, entry->iova + entry->addr_mask); request_flags =IOMMU_NOTIFIER_MAP ;
hi Eric,
Thanks for your reply.
Quoted 11 lines · Auger Eric
Hi Yan,
[+ Peter]
Quoted 2 lines · Yan Zhao
even if an entry overlaps with notifier's range, should not map/unmap out of bound part in the entry.
I don't think the patch was based on the master as the trace at the very end if not part of the upstream code.
Quoted 1 line
It's indeed based on the latest master branch. but I added a debug log and forgot to remove that before sending out the patch. sorry for that :)
Quoted 12 lines
Quoted 11 lines
This would cause problem in below case:
1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from0x3c000000-0x3c1fffffis in shadow page table.2. in vfio,
memory_region_register_iommu_notifier()is followed bymemory_region_iommu_replay(), which will first call address space unmap, and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,IOVAs from 0 - 0xffffffff get unmapped, and IOVAs from 0x3c000000 - 0x3c1fffff get mapped
Quoted 6 lines
While the patch looks sensible, the issue is the notifier scope used in
vtd_address_space_unmapis not a valid mask (ctpop64(size) != 1). Then the size is recomputed (either using n = 64 - clz64(size) for the 1st notifier or n =s->aw_bitsfor the 2d) and also the entry (especially for the 2d notifier where it becomes 0) to get a proper alignment.
maybe the size is calculated right, but 0 for the 2d notifier is because this line below ?
entry.iova = n->start & ~(size - 1);
Quoted 6 lines
vtd_page_walksends notifications per block or page (with validaddr_mask) so stays within the notifier.Modifying the entry->iova/addr_mask again in
memory_region_notify_oneleads to unaligned start address /addr_mask. I don't think we want that.
if the notifier's start and end is aligned, and entry->iova/addr_mask is aligned before modification, then after modification, the start addr /addr_mask are still aligned ?
Quoted 3 lines
Can't we modity the
vtd_address_space_unmap()implementation to split the invalidation in smaller chunks instead?
as this is an API, it cannot reply on the caller to ensure the entry is within its address range. Do you think it's reasonable?
Thanks
Yan
Quoted 33 lines
Thanks
Eric
Quoted 28 lines
(2) for notifier 0xfef00000-0xffffffffffffffff
IOVAs from 0 - 0x7fffffffff get unmapped,> but IOVAs from 0x3c000000 - 0x3c1fffff cannot get mapped back.Signed-off-by Yan Zhao <[email protected]>--- memory.c | 8 ++++++++ 1 file changed, 8 insertions(+)memory.c +7
-0@@ -1948,6 +1948,14 @@ void memory_region_notify_one(IOMMUNotifier *notifier,return ; } +if (entry->iova < notifier->start) { + entry->iova = notifier->start; + } + +if (entry->iova + entry->addr_mask > notifier->end) { + entry->addr_mask = notifier->end - entry->iova;> + } +if (entry->perm &IOMMU_RW ) {printf ("map %lx %lx\n" , entry->iova, entry->iova + entry->addr_mask); request_flags =IOMMU_NOTIFIER_MAP ;
Quoted 29 lines · Auger Eric
Hi Yan,
[+ Peter]
Quoted 2 lines · Yan Zhao
even if an entry overlaps with notifier's range, should not map/unmap out of bound part in the entry.
I don't think the patch was based on the master as the trace at the very end if not part of the upstream code.
Quoted 6 lines
This would cause problem in below case:
1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from0x3c000000-0x3c1fffffis in shadow page table.Quoted 6 lines
2. in vfio,
memory_region_register_iommu_notifier()is followed bymemory_region_iommu_replay(), which will first call address space unmap, and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,IOVAs from 0 - 0xffffffff get unmapped, and IOVAs from 0x3c000000 - 0x3c1fffff get mappedWhile the patch looks sensible, the issue is the notifier scope used in
vtd_address_space_unmapis not a valid mask (ctpop64(size) != 1). Then the size is recomputed (either using n = 64 - clz64(size) for the 1st notifier or n =s->aw_bitsfor the 2d) and also the entry (especially for the 2d notifier where it becomes 0) to get a proper alignment.
Quoted 8 lines
vtd_page_walksends notifications per block or page (with validaddr_mask) so stays within the notifier.Modifying the entry->iova/addr_mask again in
memory_region_notify_oneleads to unaligned start address /addr_mask. I don't think we want that.Can't we modity the
vtd_address_space_unmap()implementation to split the invalidation in smaller chunks instead?
Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient. Like, this range:
0x0e00_0000 - 0x1_0000_0000 (size 0xf200_0000)
can be one of this:
0x0e000000 - 0x1000_0000 (size 0x0200_0000)
plus one of this:
0x1000_0000 - 0x1_0000_0000 (size 0xf000_0000)
Yan, could you help explain the issue better on how to reproduce and what's the error when the problem occurs? For example, is that happened when a device hot-plugged into an existing VFIO container (with some mapped IOVAs)? Did you get host DMA errors later on?
Thanks,
Signature · 1 line
Peter Xu
Quoted 50 lines · Peter Xu
Quoted 29 lines · Auger Eric
Hi Yan,
[+ Peter]
Quoted 2 lines · Yan Zhao
even if an entry overlaps with notifier's range, should not map/unmap out of bound part in the entry.
I don't think the patch was based on the master as the trace at the very end if not part of the upstream code.
Quoted 6 lines
This would cause problem in below case:
1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from0x3c000000-0x3c1fffffis in shadow page table.Quoted 6 lines
2. in vfio,
memory_region_register_iommu_notifier()is followed bymemory_region_iommu_replay(), which will first call address space unmap, and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,IOVAs from 0 - 0xffffffff get unmapped, and IOVAs from 0x3c000000 - 0x3c1fffff get mappedWhile the patch looks sensible, the issue is the notifier scope used in
vtd_address_space_unmapis not a valid mask (ctpop64(size) != 1). Then the size is recomputed (either using n = 64 - clz64(size) for the 1st notifier or n =s->aw_bitsfor the 2d) and also the entry (especially for the 2d notifier where it becomes 0) to get a proper alignment.Quoted 8 lines
vtd_page_walksends notifications per block or page (with validaddr_mask) so stays within the notifier.Modifying the entry->iova/addr_mask again in
memory_region_notify_oneleads to unaligned start address /addr_mask. I don't think we want that.Can't we modity the
vtd_address_space_unmap()implementation to split the invalidation in smaller chunks instead?Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient. Like, this range:
0x0e00_0000 - 0x1_0000_0000 (size 0xf200_0000)
can be one of this:
0x0e000000- 0x1000_0000 (size 0x0200_0000)plus one of this:
Quoted 11 lines
0x1000_0000 - 0x1_0000_0000 (size 0xf000_0000)
Yan, could you help explain the issue better on how to reproduce and what's the error when the problem occurs? For example, is that happened when a device hot-plugged into an existing VFIO container (with some mapped IOVAs)? Did you get host DMA errors later on?
Thanks,
Signature · 1 line
Peter Xu
Hi Peter
it happens when there's an RMRR region in my guest iommu driver. if not adding this range check, IOVAs in this region would be unmapped and DMA faults are met in host.
Thanks
Yan
Quoted 58 lines · Yan Zhao
Quoted 57 lines · Peter Xu
Quoted 29 lines · Auger Eric
Hi Yan,
[+ Peter]
Quoted 2 lines · Yan Zhao
even if an entry overlaps with notifier's range, should not map/unmap out of bound part in the entry.
I don't think the patch was based on the master as the trace at the very end if not part of the upstream code.
Quoted 6 lines
This would cause problem in below case:
1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from0x3c000000-0x3c1fffffis in shadow page table.Quoted 6 lines
2. in vfio,
memory_region_register_iommu_notifier()is followed bymemory_region_iommu_replay(), which will first call address space unmap, and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,IOVAs from 0 - 0xffffffff get unmapped, and IOVAs from 0x3c000000 - 0x3c1fffff get mappedWhile the patch looks sensible, the issue is the notifier scope used in
vtd_address_space_unmapis not a valid mask (ctpop64(size) != 1). Then the size is recomputed (either using n = 64 - clz64(size) for the 1st notifier or n =s->aw_bitsfor the 2d) and also the entry (especially for the 2d notifier where it becomes 0) to get a proper alignment.Quoted 8 lines
vtd_page_walksends notifications per block or page (with validaddr_mask) so stays within the notifier.Modifying the entry->iova/addr_mask again in
memory_region_notify_oneleads to unaligned start address /addr_mask. I don't think we want that.Can't we modity the
vtd_address_space_unmap()implementation to split the invalidation in smaller chunks instead?Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient. Like, this range:
0x0e00_0000 - 0x1_0000_0000 (size 0xf200_0000)
can be one of this:
0x0e000000- 0x1000_0000 (size 0x0200_0000)plus one of this:
0x1000_0000 - 0x1_0000_0000 (size 0xf000_0000)
Yan, could you help explain the issue better on how to reproduce and what's the error when the problem occurs? For example, is that happened when a device hot-plugged into an existing VFIO container (with some mapped IOVAs)? Did you get host DMA errors later on?
Quoted 7 lines
Quoted 4 lines
Thanks,
Signature · 1 line
Peter XuHi Peter
it happens when there's an RMRR region in my guest iommu driver.
Do you mean a RMRR region in the ACPI table? AFAIK current QEMU VT-d does not have RMRR at all, so that's a customized QEMU?
Quoted 2 lines
if not adding this range check, IOVAs in this region would be unmapped and DMA faults are met in host.
I see, thanks.
Signature · 1 line
Peter Xu
Quoted 61 lines · Peter Xu
Quoted 60 lines · Yan Zhao
Quoted 59 lines · Peter Xu
Quoted 29 lines · Auger Eric
Hi Yan,
[+ Peter]
Quoted 2 lines · Yan Zhao
even if an entry overlaps with notifier's range, should not map/unmap out of bound part in the entry.
I don't think the patch was based on the master as the trace at the very end if not part of the upstream code.
Quoted 6 lines
This would cause problem in below case:
1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from0x3c000000-0x3c1fffffis in shadow page table.Quoted 6 lines
2. in vfio,
memory_region_register_iommu_notifier()is followed bymemory_region_iommu_replay(), which will first call address space unmap, and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,IOVAs from 0 - 0xffffffff get unmapped, and IOVAs from 0x3c000000 - 0x3c1fffff get mappedWhile the patch looks sensible, the issue is the notifier scope used in
vtd_address_space_unmapis not a valid mask (ctpop64(size) != 1). Then the size is recomputed (either using n = 64 - clz64(size) for the 1st notifier or n =s->aw_bitsfor the 2d) and also the entry (especially for the 2d notifier where it becomes 0) to get a proper alignment.Quoted 8 lines
vtd_page_walksends notifications per block or page (with validaddr_mask) so stays within the notifier.Modifying the entry->iova/addr_mask again in
memory_region_notify_oneleads to unaligned start address /addr_mask. I don't think we want that.Can't we modity the
vtd_address_space_unmap()implementation to split the invalidation in smaller chunks instead?Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient. Like, this range:
0x0e00_0000 - 0x1_0000_0000 (size 0xf200_0000)
can be one of this:
0x0e000000- 0x1000_0000 (size 0x0200_0000)plus one of this:
0x1000_0000 - 0x1_0000_0000 (size 0xf000_0000)
Yan, could you help explain the issue better on how to reproduce and what's the error when the problem occurs? For example, is that happened when a device hot-plugged into an existing VFIO container (with some mapped IOVAs)? Did you get host DMA errors later on?
Thanks,
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Signature · 1 line
Peter XuHi Peter
it happens when there's an RMRR region in my guest iommu driver.Do you mean a RMRR region in the ACPI table? AFAIK current QEMU VT-d does not have RMRR at all, so that's a customized QEMU?
it can be a customized QEMU with RMRR region in ACPI table. or simply hardcode in guest kernel.
Quoted 8 lines
Quoted 3 lines
if not adding this range check, IOVAs in this region would be unmapped and DMA faults are met in host.
I see, thanks.
Signature · 1 line
Peter Xu
Quoted 2 lines · Peter Xu
Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient.
Yes, this is not hard (completely untested):
hw/i386/intel_iommu.c +23 -28
@@ -3388,39 +3388,34 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; +while (end > start) { + size = end - start; +/* Only keep the lowest bit of either size or start. */ + size =MIN (size & -size, start & -start); +/* Should not happen, but limit to address width too just in case */ + size =MIN (size,1ULL << s->aw_bits); -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } +assert ((start & (size -1 )) ==0 ); - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); -/* This field is meaningless for unmap */ - entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + entry.target_as = &address_space_memory; + entry.iova = start; +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ; -trace_vtd_as_unmap_whole (pci_bus_num (as->bus), -VTD_PCI_SLOT (as->devfn), -VTD_PCI_FUNC (as->devfn), - entry.iova, size); +trace_vtd_as_unmap_whole (pci_bus_num (as->bus), +VTD_PCI_SLOT (as->devfn), +VTD_PCI_FUNC (as->devfn), + entry.iova, size); - map.iova = entry.iova; - map.size = entry.addr_mask; -iova_tree_remove (as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; +iova_tree_remove (as->iova_tree, &map); -memory_region_notify_one (n, &entry); +memory_region_notify_one (n, &entry); + start += size; + } }static void vtd_address_space_unmap_all (IntelIOMMUState *s)
Yan,
if something like this works for you, let me know and I will submit it as a proper patch.
Paolo
Quoted 75 lines · Paolo Bonzini
Quoted 2 lines · Peter Xu
Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient.
Yes, this is not hard (completely untested):
hw/i386/intel_iommu.c +23
-28@@ -3388,39 +3388,34 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; +while (end > start) { + size = end - start; +/* Only keep the lowest bit of either size or start. */ + size =MIN (size & -size, start & -start); +/* Should not happen, but limit to address width too just in case */ + size =MIN (size,1ULL << s->aw_bits); -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } +assert ((start & (size -1 )) ==0 ); - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); -/* This field is meaningless for unmap */ - entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + entry.target_as = &address_space_memory; + entry.iova = start; +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ; -trace_vtd_as_unmap_whole (pci_bus_num (as->bus), -VTD_PCI_SLOT (as->devfn), -VTD_PCI_FUNC (as->devfn), - entry.iova, size); +trace_vtd_as_unmap_whole (pci_bus_num (as->bus), +VTD_PCI_SLOT (as->devfn), +VTD_PCI_FUNC (as->devfn), + entry.iova, size); - map.iova = entry.iova; - map.size = entry.addr_mask; -iova_tree_remove (as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; +iova_tree_remove (as->iova_tree, &map); -memory_region_notify_one (n, &entry); +memory_region_notify_one (n, &entry); + start += size; + } }static void vtd_address_space_unmap_all (IntelIOMMUState *s)
Quoted 6 lines
Yan,
if something like this works for you, let me know and I will submit it as a proper patch.
Paolo
hi Paolo
Thanks and I'll try it tomorrow and let you know the result. But may I know why it cannot simply be like below?
Thanks
Yan
hw/i386/intel_iommu.c +1 -16
@@ -3374,7 +3374,6 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)IntelIOMMUState *s = as->iommu_state;DMAMap map;/* * Note: all the codes in this function has a assumption that IOVA * bits are no more than VTD_MGAW bits (which is restricted by
@@ -3392,23 +3391,8 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)assert (start <= end); size = end - start; -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } - - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); + entry.iova = n->start;/* This field is meaningless for unmap */ entry.translated_addr =0 ; entry.perm =IOMMU_NONE ;
Quoted 76 lines · Yan Zhao
Quoted 75 lines · Paolo Bonzini
Quoted 2 lines · Peter Xu
Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient.
Yes, this is not hard (completely untested):
hw/i386/intel_iommu.c +23
-28@@ -3388,39 +3388,34 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; +while (end > start) { + size = end - start; +/* Only keep the lowest bit of either size or start. */ + size =MIN (size & -size, start & -start); +/* Should not happen, but limit to address width too just in case */ + size =MIN (size,1ULL << s->aw_bits); -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } +assert ((start & (size -1 )) ==0 ); - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); -/* This field is meaningless for unmap */ - entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + entry.target_as = &address_space_memory; + entry.iova = start; +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ; -trace_vtd_as_unmap_whole (pci_bus_num (as->bus), -VTD_PCI_SLOT (as->devfn), -VTD_PCI_FUNC (as->devfn), - entry.iova, size); +trace_vtd_as_unmap_whole (pci_bus_num (as->bus), +VTD_PCI_SLOT (as->devfn), +VTD_PCI_FUNC (as->devfn), + entry.iova, size); - map.iova = entry.iova; - map.size = entry.addr_mask; -iova_tree_remove (as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; +iova_tree_remove (as->iova_tree, &map); -memory_region_notify_one (n, &entry); +memory_region_notify_one (n, &entry); + start += size; + } }static void vtd_address_space_unmap_all (IntelIOMMUState *s)
Quoted 7 lines
Quoted 4 lines
Yan,
if something like this works for you, let me know and I will submit it as a proper patch.
Thanks and I'll try it tomorrow and let you know the result. But may I know why it cannot simply be like below?
Because the API is that addr_mask is a power of two minus 1.
Paolo
Quoted 43 lines
Thanks
Yanhw/i386/intel_iommu.c +1
-16@@ -3374,7 +3374,6 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)IntelIOMMUState *s = as->iommu_state;DMAMap map;/* * Note: all the codes in this function has a assumption that IOVA * bits are no more than VTD_MGAW bits (which is restricted by @@ -3392,23 +3391,8 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)assert (start <= end); size = end - start; -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } - - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); + entry.iova = n->start;/* This field is meaningless for unmap */ entry.translated_addr =0 ; entry.perm =IOMMU_NONE ;
Quoted 11 lines · Paolo Bonzini
Quoted 2 lines · Peter Xu
Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient.
Yes, this is not hard (completely untested):
hw/i386/intel_iommu.c +0
-0
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3394
Quoted 8 lines
hw/i386/intel_iommu.c +4
-1@@ -3388,39 +3388,34 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; +while (end > start) { + size = end - start; +/* Only keep the lowest bit of either size or start. */ + size =MIN (size & -size, start & -start);
I feel like this can be problematic. I'm imaging:
start=0x1000_0000, size=0x1000_1000
This will get size=0x1000 but actually we can do size=0x1000_0000 as the first.
Quoted 20 lines
hw/i386/intel_iommu.c +3
-15+/* Should not happen, but limit to address width too just in case */ + size =MIN (size,1ULL << s->aw_bits); -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } +assert ((start & (size -1 )) ==0 ); - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 );
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3405
Quoted 10 lines
hw/i386/intel_iommu.c +6
-4-/* This field is meaningless for unmap */ - entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + entry.target_as = &address_space_memory; + entry.iova = start; +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ;
(some of the fields can be moved out of loop because they are
constants)
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3409
Quoted 9 lines
hw/i386/intel_iommu.c +4
-4-trace_vtd_as_unmap_whole (pci_bus_num (as->bus), -VTD_PCI_SLOT (as->devfn), -VTD_PCI_FUNC (as->devfn), - entry.iova, size); +trace_vtd_as_unmap_whole (pci_bus_num (as->bus), +VTD_PCI_SLOT (as->devfn), +VTD_PCI_FUNC (as->devfn), + entry.iova, size);
Can move this out because this is a trace only so we don't have restriction on mask?
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3412
Quoted 7 lines
hw/i386/intel_iommu.c +3
-3- map.iova = entry.iova; - map.size = entry.addr_mask; -iova_tree_remove (as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; +iova_tree_remove (as->iova_tree, &map);
Same here?
Quoted 12 lines
hw/i386/intel_iommu.c +3
-1-memory_region_notify_one (n, &entry); +memory_region_notify_one (n, &entry); + start += size; + } } static void vtd_address_space_unmap_all(IntelIOMMUState *s) Yan,
Quoted 4 lines
if something like this works for you, let me know and I will submit it as a proper patch.
Paolo
Since during review I'm thinking how to generate a correct sequence of these masks... here's my try below with above issues fixed... :)
I've tried compile but not tested. Yan can test it, or I can do it too tomorrow after I find some machines.
Thanks,
------------------------------------------------------------
hw/i386/intel_iommu.c +35 -25
@@ -3363,11 +3363,32 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)return vtd_dev_as; } +static uint64_t vtd_get_next_mask (uint64_t start,uint64_t size,int gaw) +{ +/* Tries to find smallest mask from start first */ +uint64_t rmask = start & -start, max_mask =1ULL << gaw; + +assert (size && gaw >0 && gaw <64 ); + +/* Zero start, or too big */ +if (!rmask || rmask > max_mask) { + rmask = max_mask; + } + +/* If the start mask worked, then use it */ +if (rmask <= size) { +return rmask; + } + +/* Find the largest page mask from size */ +return 1ULL << (63 -clz64 (size)); +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {IOMMUTLBEntry entry; -hwaddr size; +hwaddr size, remain;hwaddr start = n->start;hwaddr end = n->end;IntelIOMMUState *s = as->iommu_state;
@@ -3388,39 +3409,28 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; - -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } - + size = remain = end - start; entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); + entry.perm =IOMMU_NONE ;/* This field is meaningless for unmap */ entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + +while (remain) { +uint64_t mask =vtd_get_next_mask (start, remain, s->aw_bits); + + entry.iova = start; + entry.addr_mask = mask -1 ; +memory_region_notify_one (n, &entry); + }trace_vtd_as_unmap_whole (pci_bus_num (as->bus),VTD_PCI_SLOT (as->devfn),VTD_PCI_FUNC (as->devfn), - entry.iova, size); + n->start, size); - map.iova = entry.iova; - map.size = entry.addr_mask; + map.iova = n->start; + map.size = size;iova_tree_remove (as->iova_tree, &map); - -memory_region_notify_one (n, &entry); }static void vtd_address_space_unmap_all (IntelIOMMUState *s)
------------------------------------------------------------
Regards,
Signature · 1 line
Peter Xu
Quoted 6 lines · Peter Xu
I feel like this can be problematic. I'm imaging:
start=0x1000_0000, size=0x1000_1000
This will get size=0x1000 but actually we can do size=0x1000_0000 as the first.
Right, we can do:
/*
* If a naturally aligned region starting at "start" ends before "end",
* use it. Otherwise, keep the lowest bit of size.
*/
if (size > (start & -start))
size = start & -start;
else
size = size & -size;Quoted 10 lines
Review
Quoted 5 lines
(quoted diff) +4
-0+ trace_vtd_as_unmap_whole(pci_bus_num(as->bus), + VTD_PCI_SLOT(as->devfn), + VTD_PCI_FUNC(as->devfn), + entry.iova, size);Can move this out because this is a trace only so we don't have restriction on mask?
Quoted 1 line
Quoted 9 lines
Review
Quoted 6 lines
(quoted diff) +3
-3- map.iova = entry.iova; - map.size = entry.addr_mask; - iova_tree_remove(as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; + iova_tree_remove(as->iova_tree, &map);Same here?
Yes, I would move these and the iova_tree_remove outside the loop, while keeping entry's initialization inside looks cleaner.
Paolo
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Peter Xu
I feel like this can be problematic. I'm imaging:
start=0x1000_0000, size=0x1000_1000
This will get size=0x1000 but actually we can do size=0x1000_0000 as the first.
Quoted 8 lines
Right, we can do:
/*
* If a naturally aligned region starting at "start" ends before "end",
* use it. Otherwise, keep the lowest bit of size.
*/
if (size > (start & -start))size = start & -start;
May need to consider start==0, otherwise size will be zero here?
Quoted 2 lines
else
size = size & -size;
Should use MSB rather than LSB of size?
Quoted 18 lines
Quoted 18 lines
Review
Quoted 6 lines
(quoted diff) +4
-0+ trace_vtd_as_unmap_whole(pci_bus_num(as->bus), + VTD_PCI_SLOT(as->devfn), + VTD_PCI_FUNC(as->devfn), + entry.iova, size);Can move this out because this is a trace only so we don't have restriction on mask?
Quoted 7 lines
(quoted diff) +3
-3- map.iova = entry.iova; - map.size = entry.addr_mask; - iova_tree_remove(as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; + iova_tree_remove(as->iova_tree, &map);
Quoted 5 lines
Quoted 2 lines
Same here?
Yes, I would move these and the
iova_tree_removeoutside the loop, while keeping entry's initialization inside looks cleaner.
Yeah that's ok to me too.
Thanks,
Signature · 1 line
Peter Xu
Quoted 174 lines · Peter Xu
Quoted 11 lines · Paolo Bonzini
Quoted 2 lines · Peter Xu
Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient.
Yes, this is not hard (completely untested):
hw/i386/intel_iommu.c +0
-0hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3394
Quoted 8 lines
hw/i386/intel_iommu.c +4
-1@@ -3388,39 +3388,34 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; +while (end > start) { + size = end - start; +/* Only keep the lowest bit of either size or start. */ + size =MIN (size & -size, start & -start);I feel like this can be problematic. I'm imaging:
start=0x1000_0000, size=0x1000_1000
This will get size=0x1000 but actually we can do size=0x1000_0000 as the first.
Quoted 20 lines
hw/i386/intel_iommu.c +3
-15+/* Should not happen, but limit to address width too just in case */ + size =MIN (size,1ULL << s->aw_bits); -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } +assert ((start & (size -1 )) ==0 ); - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 );hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3405
Quoted 10 lines
hw/i386/intel_iommu.c +6
-4-/* This field is meaningless for unmap */ - entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + entry.target_as = &address_space_memory; + entry.iova = start; +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ;(some of the fields can be moved out of loop because they are
constants)hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3409
Quoted 9 lines
hw/i386/intel_iommu.c +4
-4-trace_vtd_as_unmap_whole (pci_bus_num (as->bus), -VTD_PCI_SLOT (as->devfn), -VTD_PCI_FUNC (as->devfn), - entry.iova, size); +trace_vtd_as_unmap_whole (pci_bus_num (as->bus), +VTD_PCI_SLOT (as->devfn), +VTD_PCI_FUNC (as->devfn), + entry.iova, size);Can move this out because this is a trace only so we don't have restriction on mask?
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3412
Quoted 7 lines
hw/i386/intel_iommu.c +3
-3- map.iova = entry.iova; - map.size = entry.addr_mask; -iova_tree_remove (as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; +iova_tree_remove (as->iova_tree, &map);Same here?
Quoted 12 lines
hw/i386/intel_iommu.c +3
-1-memory_region_notify_one (n, &entry); +memory_region_notify_one (n, &entry); + start += size; + } } static void vtd_address_space_unmap_all(IntelIOMMUState *s) Yan,Quoted 4 lines
if something like this works for you, let me know and I will submit it as a proper patch.
Paolo
Since during review I'm thinking how to generate a correct sequence of these masks... here's my try below with above issues fixed... :)
I've tried compile but not tested. Yan can test it, or I can do it too tomorrow after I find some machines.
Thanks,
------------------------------------------------------------
hw/i386/intel_iommu.c +24
-18@@ -3363,11 +3363,32 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)return vtd_dev_as; } +static uint64_t vtd_get_next_mask (uint64_t start,uint64_t size,int gaw) +{ +/* Tries to find smallest mask from start first */ +uint64_t rmask = start & -start, max_mask =1ULL << gaw; + +assert (size && gaw >0 && gaw <64 ); + +/* Zero start, or too big */ +if (!rmask || rmask > max_mask) { + rmask = max_mask; + } + +/* If the start mask worked, then use it */ +if (rmask <= size) { +return rmask; + } + +/* Find the largest page mask from size */ +return 1ULL << (63 -clz64 (size)); +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {IOMMUTLBEntry entry; -hwaddr size; +hwaddr size, remain;hwaddr start = n->start;hwaddr end = n->end;IntelIOMMUState *s = as->iommu_state;@@ -3388,39 +3409,28 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; - -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } - + size = remain = end - start; entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); + entry.perm =IOMMU_NONE ;/* This field is meaningless for unmap */
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3423
Quoted 10 lines
hw/i386/intel_iommu.c +7
-2entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + +while (remain) { +uint64_t mask =vtd_get_next_mask (start, remain, s->aw_bits); + + entry.iova = start; + entry.addr_mask = mask -1 ; +memory_region_notify_one (n, &entry);
Sorry, I at least missed these lines:
start += mask;
remain -= mask;Quoted 20 lines
hw/i386/intel_iommu.c +4
-6+ }trace_vtd_as_unmap_whole (pci_bus_num (as->bus),VTD_PCI_SLOT (as->devfn),VTD_PCI_FUNC (as->devfn), - entry.iova, size); + n->start, size); - map.iova = entry.iova; - map.size = entry.addr_mask; + map.iova = n->start; + map.size = size;iova_tree_remove (as->iova_tree, &map); - -memory_region_notify_one (n, &entry); }static void vtd_address_space_unmap_all (IntelIOMMUState *s) ------------------------------------------------------------
Quoted 4 lines
Regards,
Signature · 1 line
Peter Xu
Regards,
Signature · 1 line
Peter Xu
Quoted 211 lines · Peter Xu
Quoted 174 lines · Peter Xu
Quoted 11 lines · Paolo Bonzini
Quoted 2 lines · Peter Xu
Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient.
Yes, this is not hard (completely untested):
hw/i386/intel_iommu.c +0
-0hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3394
Quoted 8 lines
hw/i386/intel_iommu.c +4
-1@@ -3388,39 +3388,34 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; +while (end > start) { + size = end - start; +/* Only keep the lowest bit of either size or start. */ + size =MIN (size & -size, start & -start);I feel like this can be problematic. I'm imaging:
start=0x1000_0000, size=0x1000_1000
This will get size=0x1000 but actually we can do size=0x1000_0000 as the first.
Quoted 20 lines
hw/i386/intel_iommu.c +3
-15+/* Should not happen, but limit to address width too just in case */ + size =MIN (size,1ULL << s->aw_bits); -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } +assert ((start & (size -1 )) ==0 ); - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 );hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3405
Quoted 10 lines
hw/i386/intel_iommu.c +6
-4-/* This field is meaningless for unmap */ - entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + entry.target_as = &address_space_memory; + entry.iova = start; +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ;(some of the fields can be moved out of loop because they are
constants)hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3409
Quoted 9 lines
hw/i386/intel_iommu.c +4
-4-trace_vtd_as_unmap_whole (pci_bus_num (as->bus), -VTD_PCI_SLOT (as->devfn), -VTD_PCI_FUNC (as->devfn), - entry.iova, size); +trace_vtd_as_unmap_whole (pci_bus_num (as->bus), +VTD_PCI_SLOT (as->devfn), +VTD_PCI_FUNC (as->devfn), + entry.iova, size);Can move this out because this is a trace only so we don't have restriction on mask?
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3412
Quoted 7 lines
hw/i386/intel_iommu.c +3
-3- map.iova = entry.iova; - map.size = entry.addr_mask; -iova_tree_remove (as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; +iova_tree_remove (as->iova_tree, &map);Same here?
Quoted 12 lines
hw/i386/intel_iommu.c +3
-1-memory_region_notify_one (n, &entry); +memory_region_notify_one (n, &entry); + start += size; + } } static void vtd_address_space_unmap_all(IntelIOMMUState *s) Yan,Quoted 4 lines
if something like this works for you, let me know and I will submit it as a proper patch.
Paolo
Since during review I'm thinking how to generate a correct sequence of these masks... here's my try below with above issues fixed... :)
I've tried compile but not tested. Yan can test it, or I can do it too tomorrow after I find some machines.
Thanks,
------------------------------------------------------------
hw/i386/intel_iommu.c +24
-18@@ -3363,11 +3363,32 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)return vtd_dev_as; } +static uint64_t vtd_get_next_mask (uint64_t start,uint64_t size,int gaw) +{ +/* Tries to find smallest mask from start first */ +uint64_t rmask = start & -start, max_mask =1ULL << gaw; + +assert (size && gaw >0 && gaw <64 ); + +/* Zero start, or too big */ +if (!rmask || rmask > max_mask) { + rmask = max_mask; + } + +/* If the start mask worked, then use it */ +if (rmask <= size) { +return rmask; + } + +/* Find the largest page mask from size */ +return 1ULL << (63 -clz64 (size)); +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {IOMMUTLBEntry entry; -hwaddr size; +hwaddr size, remain;hwaddr start = n->start;hwaddr end = n->end;IntelIOMMUState *s = as->iommu_state;@@ -3388,39 +3409,28 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; - -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } - + size = remain = end - start; entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); + entry.perm =IOMMU_NONE ;/* This field is meaningless for unmap */ hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3423
Quoted 10 lines
hw/i386/intel_iommu.c +7
-2entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + +while (remain) { +uint64_t mask =vtd_get_next_mask (start, remain, s->aw_bits); + + entry.iova = start; + entry.addr_mask = mask -1 ; +memory_region_notify_one (n, &entry);Sorry, I at least missed these lines:
start += mask; remain -= mask;Quoted 20 lines
hw/i386/intel_iommu.c +4
-6+ }trace_vtd_as_unmap_whole (pci_bus_num (as->bus),VTD_PCI_SLOT (as->devfn),VTD_PCI_FUNC (as->devfn), - entry.iova, size); + n->start, size); - map.iova = entry.iova; - map.size = entry.addr_mask; + map.iova = n->start; + map.size = size;iova_tree_remove (as->iova_tree, &map); - -memory_region_notify_one (n, &entry); }static void vtd_address_space_unmap_all (IntelIOMMUState *s) ------------------------------------------------------------
Quoted 9 lines
Quoted 4 lines
Regards,
Signature · 1 line
Peter XuRegards,
Signature · 1 line
Peter Xu
hi Peter and Paolo,
I tested with code and it's fine in my side.
It's base on your version with some minor modifications, such as size is now (end - start + 1) now.
Thanks
Yan
+static uint64_t vtd_get_next_mask(uint64_t start, uint64_t size, int gaw) +{
+ /* Tries to find smallest mask from start first */
+ uint64_t rmask = start & -start, max_mask = 1ULL << gaw;
+ assert(size && gaw > 0 && gaw < 64);
+ /* Zero start, or too big */
+ if (!rmask || rmask > max_mask) {
+ rmask = max_mask;
+ }
+ /* If the start mask worked, then use it */
+ if (rmask <= size) {+ return rmask;
+ }
+
+ /* Find the largest page mask from size */
+ return 1ULL << (63 - clz64(size));
+}
+
/* Unmap the whole range in the notifier's scope. */
static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
{
IOMMUTLBEntry entry;
- hwaddr size;
+ hwaddr size, remain;
hwaddr start = n->start;
hwaddr end = n->end;
IntelIOMMUState *s = as->iommu_state;@@ -3380,48 +3398,46 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
* VT-d spec), otherwise we need to consider overflow of 64 bits.
*/
- if (end > VTD_ADDRESS_SIZE(s->aw_bits)) {
+ if (end > VTD_ADDRESS_SIZE(s->aw_bits) - 1) {
/*
* Don't need to unmap regions that is bigger than the whole
* VT-d supported address space size
*/
- end = VTD_ADDRESS_SIZE(s->aw_bits);
+ end = VTD_ADDRESS_SIZE(s->aw_bits) - 1;
}
assert(start <= end);- size = end - start;
- if (ctpop64(size) != 1) {
- /*
- * This size cannot format a correct mask. Let's enlarge it to - * suite the minimum available mask. - */
- int n = 64 - clz64(size);
- if (n > s->aw_bits) {
- /* should not happen, but in case it happens, limit it */
- n = s->aw_bits;
- }
- size = 1ULL << n;
- }
+ size = remain = end - start + 1;
entry.target_as = &address_space_memory;
- /* Adjust iova for the size */
- entry.iova = n->start & ~(size - 1);
+
+ entry.perm = IOMMU_NONE;
/* This field is meaningless for unmap */
entry.translated_addr = 0;- entry.perm = IOMMU_NONE;
- entry.addr_mask = size - 1;
+
+ while (remain >= VTD_PAGE_SIZE) {
+ uint64_t mask = vtd_get_next_mask(start, remain, s->aw_bits);
+
+ entry.iova = start; + entry.addr_mask = mask - 1; + memory_region_notify_one(n, &entry); + start += mask; + remain -= mask;
+ }
+
+ if (remain) {
+ warn_report("Unmapping unaligned range %lx-%lx", start, end);
+ }
trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
VTD_PCI_SLOT(as->devfn),
VTD_PCI_FUNC(as->devfn),- entry.iova, size);
-
- map.iova = entry.iova;
- map.size = entry.addr_mask;
+ n->start, size);
+ map.iova = n->start;
+ map.size = size;
iova_tree_remove(as->iova_tree, &map);- memory_region_notify_one(n, &entry);
}
static void vtd_address_space_unmap_all(IntelIOMMUState *s)
--
2.7.4
Quoted 222 lines · Yan Zhao
Quoted 211 lines · Peter Xu
Quoted 174 lines · Peter Xu
Quoted 11 lines · Paolo Bonzini
Quoted 2 lines · Peter Xu
Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient.
Yes, this is not hard (completely untested):
hw/i386/intel_iommu.c +0
-0hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3394
Quoted 8 lines
hw/i386/intel_iommu.c +4
-1@@ -3388,39 +3388,34 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; +while (end > start) { + size = end - start; +/* Only keep the lowest bit of either size or start. */ + size =MIN (size & -size, start & -start);I feel like this can be problematic. I'm imaging:
start=0x1000_0000, size=0x1000_1000
This will get size=0x1000 but actually we can do size=0x1000_0000 as the first.
Quoted 20 lines
hw/i386/intel_iommu.c +3
-15+/* Should not happen, but limit to address width too just in case */ + size =MIN (size,1ULL << s->aw_bits); -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } +assert ((start & (size -1 )) ==0 ); - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 );hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3405
Quoted 10 lines
hw/i386/intel_iommu.c +6
-4-/* This field is meaningless for unmap */ - entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + entry.target_as = &address_space_memory; + entry.iova = start; +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ;(some of the fields can be moved out of loop because they are
constants)hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3409
Quoted 9 lines
hw/i386/intel_iommu.c +4
-4-trace_vtd_as_unmap_whole (pci_bus_num (as->bus), -VTD_PCI_SLOT (as->devfn), -VTD_PCI_FUNC (as->devfn), - entry.iova, size); +trace_vtd_as_unmap_whole (pci_bus_num (as->bus), +VTD_PCI_SLOT (as->devfn), +VTD_PCI_FUNC (as->devfn), + entry.iova, size);Can move this out because this is a trace only so we don't have restriction on mask?
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3412
Quoted 7 lines
hw/i386/intel_iommu.c +3
-3- map.iova = entry.iova; - map.size = entry.addr_mask; -iova_tree_remove (as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; +iova_tree_remove (as->iova_tree, &map);Same here?
Quoted 12 lines
hw/i386/intel_iommu.c +3
-1-memory_region_notify_one (n, &entry); +memory_region_notify_one (n, &entry); + start += size; + } } static void vtd_address_space_unmap_all(IntelIOMMUState *s) Yan,Quoted 4 lines
if something like this works for you, let me know and I will submit it as a proper patch.
Paolo
Since during review I'm thinking how to generate a correct sequence of these masks... here's my try below with above issues fixed... :)
I've tried compile but not tested. Yan can test it, or I can do it too tomorrow after I find some machines.
Thanks,
------------------------------------------------------------
hw/i386/intel_iommu.c +24
-18@@ -3363,11 +3363,32 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)return vtd_dev_as; } +static uint64_t vtd_get_next_mask (uint64_t start,uint64_t size,int gaw) +{ +/* Tries to find smallest mask from start first */ +uint64_t rmask = start & -start, max_mask =1ULL << gaw; + +assert (size && gaw >0 && gaw <64 ); + +/* Zero start, or too big */ +if (!rmask || rmask > max_mask) { + rmask = max_mask; + } + +/* If the start mask worked, then use it */ +if (rmask <= size) { +return rmask; + } + +/* Find the largest page mask from size */ +return 1ULL << (63 -clz64 (size)); +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {IOMMUTLBEntry entry; -hwaddr size; +hwaddr size, remain;hwaddr start = n->start;hwaddr end = n->end;IntelIOMMUState *s = as->iommu_state;@@ -3388,39 +3409,28 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; - -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } - + size = remain = end - start; entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); + entry.perm =IOMMU_NONE ;/* This field is meaningless for unmap */ hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3423
Quoted 10 lines
hw/i386/intel_iommu.c +7
-2entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + +while (remain) { +uint64_t mask =vtd_get_next_mask (start, remain, s->aw_bits); + + entry.iova = start; + entry.addr_mask = mask -1 ; +memory_region_notify_one (n, &entry);Sorry, I at least missed these lines:
start += mask; remain -= mask;Quoted 20 lines
hw/i386/intel_iommu.c +4
-6+ }trace_vtd_as_unmap_whole (pci_bus_num (as->bus),VTD_PCI_SLOT (as->devfn),VTD_PCI_FUNC (as->devfn), - entry.iova, size); + n->start, size); - map.iova = entry.iova; - map.size = entry.addr_mask; + map.iova = n->start; + map.size = size;iova_tree_remove (as->iova_tree, &map); - -memory_region_notify_one (n, &entry); }static void vtd_address_space_unmap_all (IntelIOMMUState *s) ------------------------------------------------------------Quoted 9 lines
Quoted 4 lines
Regards,
Signature · 1 line
Peter XuRegards,
Signature · 1 line
Peter Xu
Quoted 6 lines
hi Peter and Paolo,
I tested with code and it's fine in my side.
It's base on your version with some minor modifications, such as size is now (end - start + 1) now.
Thanks
Yan
Hi, Yan,
Thanks for testing the patches. I think below change [1] is not related to the problem so I tend to split it out. For [2] I'll change to an assertion if you won't disagree. I'll reorganize the patches and post a formal version with proper authorships soon.
Thanks,
Quoted 30 lines
hw/i386/intel_iommu.c +19
-1+static uint64_t vtd_get_next_mask (uint64_t start,uint64_t size,int gaw) +{ +/* Tries to find smallest mask from start first */ +uint64_t rmask = start & -start, max_mask =1ULL << gaw; +assert (size && gaw >0 && gaw <64 ); +/* Zero start, or too big */ +if (!rmask || rmask > max_mask) { + rmask = max_mask; + } +/* If the start mask worked, then use it */ +if (rmask <= size) { +return rmask; + } + +/* Find the largest page mask from size */ +return 1ULL << (63 -clz64 (size)); +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {IOMMUTLBEntry entry; -hwaddr size; +hwaddr size, remain;hwaddr start = n->start;hwaddr end = n->end;IntelIOMMUState *s = as->iommu_state;@@ -3380,48 +3398,46 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)* VT-d spec), otherwise we need to consider overflow of 64 bits.
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3406
Quoted 10 lines
hw/i386/intel_iommu.c +2
-2*/ -if (end >VTD_ADDRESS_SIZE (s->aw_bits)) { +if (end >VTD_ADDRESS_SIZE (s->aw_bits) -1 ) {/* * Don't need to unmap regions that is bigger than the whole * VT-d supported address space size */ - end =VTD_ADDRESS_SIZE (s->aw_bits); + end =VTD_ADDRESS_SIZE (s->aw_bits) -1 ;
[1]
Quoted 31 lines
(quoted diff) +6
-17} assert(start <= end); - size = end - start; - if (ctpop64(size) != 1) { - /* - * This size cannot format a correct mask. Let's enlarge it to - * suite the minimum available mask. - */ - int n = 64 - clz64(size); - if (n > s->aw_bits) { - /* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size = 1ULL << n; - } + size = remain = end - start + 1; entry.target_as = &address_space_memory; - /* Adjust iova for the size */ - entry.iova = n->start & ~(size - 1); + + entry.perm = IOMMU_NONE; /* This field is meaningless for unmap */ entry.translated_addr = 0; - entry.perm = IOMMU_NONE; - entry.addr_mask = size - 1; + + while (remain >= VTD_PAGE_SIZE) { + uint64_t mask = vtd_get_next_mask(start, remain, s->aw_bits);
Review static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3430
Quoted 10 lines
(quoted diff) +10
-0+ + entry.iova = start; + entry.addr_mask = mask - 1; + memory_region_notify_one(n, &entry); + start += mask; + remain -= mask; + } + + if (remain) { + warn_report("Unmapping unaligned range %lx-%lx", start, end);
[2]
Quoted 19 lines
(quoted diff) +4
-6+ } trace_vtd_as_unmap_whole(pci_bus_num(as->bus), VTD_PCI_SLOT(as->devfn), VTD_PCI_FUNC(as->devfn), - entry.iova, size); - - map.iova = entry.iova; - map.size = entry.addr_mask; + n->start, size); + map.iova = n->start; + map.size = size; iova_tree_remove(as->iova_tree, &map); - memory_region_notify_one(n, &entry); } static void vtd_address_space_unmap_all(IntelIOMMUState *s) --
Quoted 4 lines
2.7.4
Regards,
Signature · 1 line
Peter Xu
Quoted 102 lines · Peter Xu
Quoted 11 lines · Paolo Bonzini
Quoted 2 lines · Peter Xu
Seems workable, to be explicit - we can even cut it into chunks with different size to be efficient.
Yes, this is not hard (completely untested):
hw/i386/intel_iommu.c +0
-0hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3394
Quoted 8 lines
hw/i386/intel_iommu.c +4
-1@@ -3388,39 +3388,34 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; +while (end > start) { + size = end - start; +/* Only keep the lowest bit of either size or start. */ + size =MIN (size & -size, start & -start);I feel like this can be problematic. I'm imaging:
start=0x1000_0000, size=0x1000_1000
This will get size=0x1000 but actually we can do size=0x1000_0000 as the first.
Quoted 20 lines
hw/i386/intel_iommu.c +3
-15+/* Should not happen, but limit to address width too just in case */ + size =MIN (size,1ULL << s->aw_bits); -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } +assert ((start & (size -1 )) ==0 ); - entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 );hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3405
Quoted 10 lines
hw/i386/intel_iommu.c +6
-4-/* This field is meaningless for unmap */ - entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + entry.target_as = &address_space_memory; + entry.iova = start; +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ;(some of the fields can be moved out of loop because they are
constants)hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3409
Quoted 9 lines
hw/i386/intel_iommu.c +4
-4-trace_vtd_as_unmap_whole (pci_bus_num (as->bus), -VTD_PCI_SLOT (as->devfn), -VTD_PCI_FUNC (as->devfn), - entry.iova, size); +trace_vtd_as_unmap_whole (pci_bus_num (as->bus), +VTD_PCI_SLOT (as->devfn), +VTD_PCI_FUNC (as->devfn), + entry.iova, size);Can move this out because this is a trace only so we don't have restriction on mask?
hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 3412
Quoted 7 lines
hw/i386/intel_iommu.c +3
-3- map.iova = entry.iova; - map.size = entry.addr_mask; -iova_tree_remove (as->iova_tree, &map); + map.iova = entry.iova; + map.size = entry.addr_mask; +iova_tree_remove (as->iova_tree, &map);Same here?
Quoted 12 lines
hw/i386/intel_iommu.c +3
-1-memory_region_notify_one (n, &entry); +memory_region_notify_one (n, &entry); + start += size; + } } static void vtd_address_space_unmap_all(IntelIOMMUState *s) Yan,Quoted 4 lines
if something like this works for you, let me know and I will submit it as a proper patch.
Paolo
Quoted 8 lines
Since during review I'm thinking how to generate a correct sequence of these masks... here's my try below with above issues fixed... :)
I've tried compile but not tested. Yan can test it, or I can do it too tomorrow after I find some machines.
Thanks,
hi Peter and Paolo,
I can help test it next week. Didn't manage to find a time for it today.
Thanks
Yan
Quoted 98 lines
------------------------------------------------------------
hw/i386/intel_iommu.c +35
-26@@ -3363,11 +3363,32 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)return vtd_dev_as; } +static uint64_t vtd_get_next_mask (uint64_t start,uint64_t size,int gaw) +{ +/* Tries to find smallest mask from start first */ +uint64_t rmask = start & -start, max_mask =1ULL << gaw; + +assert (size && gaw >0 && gaw <64 ); + +/* Zero start, or too big */ +if (!rmask || rmask > max_mask) { + rmask = max_mask; + } + +/* If the start mask worked, then use it */ +if (rmask <= size) { +return rmask; + } + +/* Find the largest page mask from size */ +return 1ULL << (63 -clz64 (size)); +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {IOMMUTLBEntry entry; -hwaddr size; +hwaddr size, remain;hwaddr start = n->start;hwaddr end = n->end;IntelIOMMUState *s = as->iommu_state;@@ -3388,39 +3409,28 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) }assert (start <= end); - size = end - start; - -if (ctpop64 (size) !=1 ) { -/* -* This size cannot format a correct mask. Let's enlarge it to -* suite the minimum available mask. -*/ -int n =64 -clz64 (size); -if (n > s->aw_bits) { -/* should not happen, but in case it happens, limit it */ - n = s->aw_bits; - } - size =1ULL << n; - } - + size = remain = end - start; entry.target_as = &address_space_memory; -/* Adjust iova for the size */ - entry.iova = n->start & ~(size -1 ); + entry.perm =IOMMU_NONE ;/* This field is meaningless for unmap */ entry.translated_addr =0 ; - entry.perm =IOMMU_NONE ; - entry.addr_mask = size -1 ; + +while (remain) { +uint64_t mask =vtd_get_next_mask (start, remain, s->aw_bits); + + entry.iova = start; + entry.addr_mask = mask -1 ; +memory_region_notify_one (n, &entry); + }trace_vtd_as_unmap_whole (pci_bus_num (as->bus),VTD_PCI_SLOT (as->devfn),VTD_PCI_FUNC (as->devfn), - entry.iova, size); + n->start, size); - map.iova = entry.iova; - map.size = entry.addr_mask; + map.iova = n->start; + map.size = size;iova_tree_remove (as->iova_tree, &map); - -memory_region_notify_one (n, &entry); }static void vtd_address_space_unmap_all (IntelIOMMUState *s) ------------------------------------------------------------Regards,
Signature · 1 line
Peter Xu