Allow memory_region_register_iommu_notifier() to fail
v3 cover Allow memory_region_register_iommu_notifier() to fail
This series allows the memory_region_register_iommu_notifier() to fail. As of now, when a MAP notifier is attempted to be registered along with SMMUv3 or AMD IOMMU, we exit in the IOMMU MR notify_flag_changed() callback.
In case of VFIO assigned device hotplug, this could be handled more nicely directly within the VFIO code, simply rejecting the hotplug without exiting. This is what the series achieves by handling the memory_region_register_iommu_notifier() returned value and Error object.
To propagate errors collected during vfio_listener_region_add() we now store the error handle inside the VFIO container instead of a returned value.
The message now is:
(QEMU) device_add id=hot0 driver=vfio-pci host=0000:89:00.0 bus=pcie.1 {"error": {"class": "GenericError", "desc": "vfio 0000:89:00.0: failed to setup container for group 2: memory listener initialization failed:
Region smmuv3-iommu-memory-region-0-6: device 01.00.0 requires iommu MAP notifier which is not currently supported"}}
Best Regards
Eric
This series can be found at:
https://github.com/eauger/qemu/tree/v4.1.0_register_iommu_notifier_fail_v3
History:
v2 -> v3:
- also pass an Error handle (suggested by Peter)
- VFIO container error becomes and Error handle
v1 -> v2:
- Intel IOMMU now handles the problem differently with machine init done notifier and machine hotplug allowed hook.
- use assert(!ret)
- message rewording in SMMUv3
Follow-up of "VFIO/SMMUv3: Fail on VFIO/HW nested paging detection"
https://patchew.org/QEMU/[email protected]/
Eric Auger (2):
vfio: Turn the container error into an Error handle
memory: allow memory_region_register_iommu_notifier() to fail
exec.c | 10 ++++-- hw/arm/smmuv3.c | 18 +++++----- hw/i386/amd_iommu.c | 17 +++++---- hw/i386/intel_iommu.c | 8 +++-- hw/ppc/spapr_iommu.c | 8 +++-- hw/vfio/common.c | 67 +++++++++++++++++++++-------------- hw/vfio/spapr.c | 4 ++- hw/virtio/vhost.c | 9 +++-- include/exec/memory.h | 21 ++++++++--- include/hw/vfio/vfio-common.h | 2 +- memory.c | 31 ++++++++++------ 11 files changed, 126 insertions(+), 69 deletions(-)
Signature · 1 line
2.20.1
PATCH v3 1/2 vfio: Turn the container error into an Error handle
The container error integer field is currently used to store the first error potentially encountered during any vfio_listener_region_add() call. However this fails to propagate detailed error messages up to the vfio_connect_container caller. Instead of using an integer, let's use an Error handle.
Messages are slightly reworded to accomodate the propagation.
--- hw/vfio/common.c | 61 +++++++++++++++++++++-------------- hw/vfio/spapr.c | 4 ++- include/hw/vfio/vfio-common.h | 2 +- 3 files changed, 40 insertions(+), 27 deletions(-)
hw/vfio/common.c +36 -25
@@ -503,12 +503,14 @@ static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) {VFIOContainer *container =container_of (listener, VFIOContainer, listener); +MemoryRegion *mr = section->mr;hwaddr iova, end;Int128 llend, llsize;void *vaddr;int ret;VFIOHostDMAWindow *hostwin;bool hostwin_found; +Error *err =NULL ;if (vfio_listener_skipped_section (section)) { trace_vfio_listener_region_add_skip(
@@ -543,6 +545,9 @@ static void vfio_listener_region_add(MemoryListener *listener, hostwin->max_iova - hostwin->min_iova + 1, section->offset_within_address_space, int128_get64(section->size))) { +error_setg (&err,"Overlap with existing IOMMU range " +"[0x%" PRIx64",0x%" PRIx64"]" , hostwin->min_iova, + hostwin->max_iova - hostwin->min_iova +1 ); ret =-1 ; goto fail; }
@@ -550,6 +555,7 @@ static void vfio_listener_region_add(MemoryListener *listener, ret =vfio_spapr_create_window (container, section, &pgsize);if (ret) { +error_setg_errno (&err, -ret,"Failed to create SPAPR window" ); goto fail; }
@@ -559,7 +565,7 @@ static void vfio_listener_region_add(MemoryListener *listener, #ifdef CONFIG_KVM if (kvm_enabled()) { VFIOGroup *group; - IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); + IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(mr); struct kvm_vfio_spapr_tce param; struct kvm_device_attr attr = { .group = KVM_DEV_VFIO_GROUP,
@@ -594,18 +600,17 @@ static void vfio_listener_region_add(MemoryListener *listener, }if (!hostwin_found) { -error_report ("vfio: IOMMU container %p can't map guest IOVA region" -" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, - container, iova, end); +error_setg (&err,"Container %p can't map guest IOVA region" +" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end); ret = -EFAULT ; goto fail; } -memory_region_ref (section->mr); +memory_region_ref (mr); -if (memory_region_is_iommu (section->mr)) { +if (memory_region_is_iommu (mr)) {VFIOGuestIOMMU *giommu; -IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (section->mr); +IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (mr);int iommu_idx;trace_vfio_listener_region_add_iommu (iova, end);
@@ -632,15 +637,15 @@ static void vfio_listener_region_add(MemoryListener *listener, iommu_idx);QLIST_INSERT_HEAD (&container->giommu_list, giommu, giommu_next); -memory_region_register_iommu_notifier (section->mr, &giommu->n); +memory_region_register_iommu_notifier (mr, &giommu->n);memory_region_iommu_replay (giommu->iommu, &giommu->n);return ; } -/* Here we assume that memory_region_is_ram(section->mr)==true */ +/* Here we assume that memory_region_is_ram(mr)==true */ - vaddr =memory_region_get_ram_ptr (section->mr) + + vaddr =memory_region_get_ram_ptr (mr) + section->offset_within_region + (iova - section->offset_within_address_space);
@@ -648,12 +653,12 @@ static void vfio_listener_region_add(MemoryListener *listener, llsize =int128_sub (llend,int128_make64 (iova)); -if (memory_region_is_ram_device (section->mr)) { +if (memory_region_is_ram_device (mr)) {hwaddr pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ;if ((iova & pgmask) || (int128_get64 (llsize) & pgmask)) {trace_vfio_listener_region_add_no_dma_map ( -memory_region_name (section->mr), +memory_region_name (mr), section->offset_within_address_space,int128_getlo (section->size), pgmask +1 );
@@ -664,11 +669,12 @@ static void vfio_listener_region_add(MemoryListener *listener, ret =vfio_dma_map (container, iova,int128_get64 (llsize), vaddr, section->readonly);if (ret) { -error_report ("vfio_dma_map(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx", %p) = %d (%m)" , - container, iova,int128_get64 (llsize), vaddr, ret); -if (memory_region_is_ram_device (section->mr)) { +error_setg (&err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx", %p) = %d (%m)" , + container, iova,int128_get64 (llsize), vaddr, ret); +if (memory_region_is_ram_device (mr)) {/* Allow unexpected mappings not to be fatal for RAM devices */ +error_report_err (err);return ; } goto fail;
@@ -677,7 +683,7 @@ static void vfio_listener_region_add(MemoryListener *listener,return ; fail: -if (memory_region_is_ram_device (section->mr)) { +if (memory_region_is_ram_device (mr)) {error_report ("failed to vfio_dma_map. pci p2p may not work" );return ; }
@@ -688,10 +694,14 @@ fail:*/ if (!container->initialized) {if (!container->error) { - container->error = ret; +error_propagate_prepend (&container->error, err, +"Region %s: " ,memory_region_name (mr)); + }else { +error_free (err); } }else { -hw_error ("vfio: DMA mapping failed, unable to continue" ); +error_reportf_err (err, +"vfio: DMA mapping failed, unable to continue: " ); } }
@@ -1251,6 +1261,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container =g_malloc0 (sizeof (*container)); container->space = space; container->fd = fd; + container->error =NULL ;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list);
@@ -1308,9 +1319,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, &address_space_memory);if (container->error) {memory_listener_unregister (&container->prereg_listener); - ret = container->error; -error_setg (errp, -"RAM memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"RAM memory listener initialization failed: " ); goto free_container_exit; } }
@@ -1365,9 +1376,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,memory_listener_register (&container->listener, container->space->as);if (container->error) { - ret = container->error; -error_setg_errno (errp, -ret, -"memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"memory listener initialization failed: " ); goto listener_release_exit; }
hw/vfio/spapr.c +3 -1
@@ -17,6 +17,7 @@#include "hw/hw.h" #include "exec/ram_addr.h" #include "qemu/error-report.h" +#include "qapi/error.h" #include "trace.h" static bool vfio_prereg_listener_skipped_section (MemoryRegionSection *section)
@@ -85,7 +86,8 @@ static void vfio_prereg_listener_region_add(MemoryListener *listener,*/ if (!container->initialized) {if (!container->error) { - container->error = ret; +error_setg_errno (&container->error, -ret, +"Memory registering failed" ); } }else {hw_error ("vfio: Memory registering failed, unable to continue" );
include/hw/vfio/vfio-common.h +1 -1
@@ -71,7 +71,7 @@ typedef struct VFIOContainer {MemoryListener listener;MemoryListener prereg_listener;unsigned iommu_type; -int error; +Error *error;bool initialized;unsigned long pgsizes; QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
Signature · 1 line
2.20.1
Re: [PATCH v3 1/2] vfio: Turn the container error into an Error handle
Quoted 7 lines · Eric Auger
The container error integer field is currently used to store the first error potentially encountered during any
vfio_listener_region_add()call. However this fails to propagate detailed error messages up to thevfio_connect_containercaller. Instead of using an integer, let's use an Error handle.Messages are slightly reworded to accomodate the propagation.
Thanks for working on this. Mostly good at least to me, though I still have a few nitpickings below.
hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 551
Quoted 8 lines
hw/vfio/common.c +3
-0@@ -543,6 +545,9 @@ static void vfio_listener_region_add(MemoryListener *listener, hostwin->max_iova - hostwin->min_iova + 1, section->offset_within_address_space, int128_get64(section->size))) { +error_setg (&err,"Overlap with existing IOMMU range " +"[0x%" PRIx64",0x%" PRIx64"]" , hostwin->min_iova, + hostwin->max_iova - hostwin->min_iova +1 ); ret =-1 ;
This line seems to be useless now after we dropped the integer version of container->error and start to use Error*.
Quoted 20 lines
hw/vfio/common.c +2
-1goto fail; }@@ -550,6 +555,7 @@ static void vfio_listener_region_add(MemoryListener *listener, ret =vfio_spapr_create_window (container, section, &pgsize);if (ret) { +error_setg_errno (&err, -ret,"Failed to create SPAPR window" ); goto fail; }@@ -559,7 +565,7 @@ static void vfio_listener_region_add(MemoryListener *listener, #ifdef CONFIG_KVM if (kvm_enabled()) { VFIOGroup *group; - IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); + IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(mr); struct kvm_vfio_spapr_tce param; struct kvm_device_attr attr = { .group = KVM_DEV_VFIO_GROUP,
hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 605
Quoted 9 lines
hw/vfio/common.c +2
-3@@ -594,18 +600,17 @@ static void vfio_listener_region_add(MemoryListener *listener, }if (!hostwin_found) { -error_report ("vfio: IOMMU container %p can't map guest IOVA region" -" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, - container, iova, end); +error_setg (&err,"Container %p can't map guest IOVA region" +" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end); ret = -EFAULT ;
Same here.
Quoted 2 lines
(quoted diff) +0
-0goto fail; }
[...]
Quoted 4 lines
hw/vfio/common.c +0
-0@@ -688,10 +694,14 @@ fail:*/ if (!container->initialized) {if (!container->error) {
hw/vfio/common.c fail: · line 704
Quoted 10 lines
hw/vfio/common.c +6
-2- container->error = ret; +error_propagate_prepend (&container->error, err, +"Region %s: " ,memory_region_name (mr)); + }else { +error_free (err); } }else { -hw_error ("vfio: DMA mapping failed, unable to continue" ); +error_reportf_err (err, +"vfio: DMA mapping failed, unable to continue: " );
Probably need to keep hw_error() because it asserts inside. Maybe an error_report_err() before hw_error()?
Quoted 12 lines
hw/vfio/common.c +1
-0} }@@ -1251,6 +1261,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container =g_malloc0 (sizeof (*container)); container->space = space; container->fd = fd; + container->error =NULL ;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list);
hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1324
Quoted 9 lines
hw/vfio/common.c +3
-3@@ -1308,9 +1319,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, &address_space_memory);if (container->error) {memory_listener_unregister (&container->prereg_listener); - ret = container->error; -error_setg (errp, -"RAM memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"RAM memory listener initialization failed: " );
(I saw that we've got plenty of prepended prefixes for an error
messages. For me I'll disgard quite a few of them because the errors will directly be delivered to the top level user, but this might be too personal as a comment)
Thanks,
Quoted 3 lines
hw/vfio/common.c +0
-0goto free_container_exit; } }
Signature · 1 line
Peter Xu
Re: [PATCH v3 1/2] vfio: Turn the container error into an Error handle
Hi Peter,
Quoted 12 lines · Peter Xu
Quoted 7 lines · Eric Auger
The container error integer field is currently used to store the first error potentially encountered during any
vfio_listener_region_add()call. However this fails to propagate detailed error messages up to thevfio_connect_containercaller. Instead of using an integer, let's use an Error handle.Messages are slightly reworded to accomodate the propagation.
Thanks for working on this. Mostly good at least to me, though I still have a few nitpickings below.
Quoted 11 lines
hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 551
Quoted 8 lines
hw/vfio/common.c +3
-0@@ -543,6 +545,9 @@ static void vfio_listener_region_add(MemoryListener *listener, hostwin->max_iova - hostwin->min_iova + 1, section->offset_within_address_space, int128_get64(section->size))) { +error_setg (&err,"Overlap with existing IOMMU range " +"[0x%" PRIx64",0x%" PRIx64"]" , hostwin->min_iova, + hostwin->max_iova - hostwin->min_iova +1 ); ret =-1 ;This line seems to be useless now after we dropped the integer version of
container->errorand start to use Error*.
correct
Quoted 23 lines
Quoted 23 lines
hw/vfio/common.c +2
-1goto fail; }@@ -550,6 +555,7 @@ static void vfio_listener_region_add(MemoryListener *listener, ret =vfio_spapr_create_window (container, section, &pgsize);if (ret) { +error_setg_errno (&err, -ret,"Failed to create SPAPR window" ); goto fail; }@@ -559,7 +565,7 @@ static void vfio_listener_region_add(MemoryListener *listener, #ifdef CONFIG_KVM if (kvm_enabled()) { VFIOGroup *group; - IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); + IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(mr); struct kvm_vfio_spapr_tce param; struct kvm_device_attr attr = { .group = KVM_DEV_VFIO_GROUP,@@ -594,18 +600,17 @@ static void vfio_listener_region_add(MemoryListener *listener, }
Quoted 9 lines
hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 605
Quoted 7 lines
hw/vfio/common.c +2
-3if (!hostwin_found) { -error_report ("vfio: IOMMU container %p can't map guest IOVA region" -" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, - container, iova, end); +error_setg (&err,"Container %p can't map guest IOVA region" +" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end); ret = -EFAULT ;Same here.
OK
Quoted 15 lines
Quoted 3 lines
(quoted diff) +0
-0goto fail; }[...]
Quoted 9 lines
hw/vfio/common.c +4
-1@@ -688,10 +694,14 @@ fail:*/ if (!container->initialized) {if (!container->error) { - container->error = ret; +error_propagate_prepend (&container->error, err, +"Region %s: " ,memory_region_name (mr)); + }else { +error_free (err);
Quoted 8 lines
hw/vfio/common.c fail: · line 704
Quoted 5 lines
hw/vfio/common.c +2
-1} }else { -hw_error ("vfio: DMA mapping failed, unable to continue" ); +error_reportf_err (err, +"vfio: DMA mapping failed, unable to continue: " );Probably need to keep
hw_error()because it asserts inside. Maybe anerror_report_err()beforehw_error()?
that's correct.
Quoted 12 lines
Quoted 12 lines
hw/vfio/common.c +1
-0} }@@ -1251,6 +1261,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container =g_malloc0 (sizeof (*container)); container->space = space; container->fd = fd; + container->error =NULL ;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list);
Quoted 15 lines
hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1324
Quoted 10 lines
hw/vfio/common.c +3
-3@@ -1308,9 +1319,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, &address_space_memory);if (container->error) {memory_listener_unregister (&container->prereg_listener); - ret = container->error; -error_setg (errp, -"RAM memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"RAM memory listener initialization failed: " );(I saw that we've got plenty of prepended prefixes for an error
messages. For me I'll disgard quite a few of them because the errors will directly be delivered to the top level user, but this might be too personal as a comment)
That's true we have a lot of prefix messages.
The output message now is:
"vfio 0000:89:00.0: failed
to setup container for group 2: memory listener initialization failed:
Region smmuv3-iommu-memory-region-0-6: device 01.00.0 requires iommu MAP notifier which is not currently supported"
Alex, any opinion?
Thanks
Eric
Quoted 7 lines
Thanks,
Quoted 3 lines
hw/vfio/common.c +0
-0goto free_container_exit; } }
Re: [PATCH v3 1/2] vfio: Turn the container error into an Error handle
Quoted 19 lines · On Mon, 23 Sep 2019 13:43:08 +0200 Auger Eric <[email protected]> wrote:
Quoted 16 lines · Peter Xu
hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1324
Quoted 10 lines · Eric Auger
hw/vfio/common.c +3
-3@@ -1308,9 +1319,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, &address_space_memory);if (container->error) {memory_listener_unregister (&container->prereg_listener); - ret = container->error; -error_setg (errp, -"RAM memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"RAM memory listener initialization failed: " );(I saw that we've got plenty of prepended prefixes for an error
messages. For me I'll disgard quite a few of them because the errors will directly be delivered to the top level user, but this might be too personal as a comment)That's true we have a lot of prefix messages.
Quoted 8 lines
The output message now is:
"vfio 0000:89:00.0: failed
to setup container for group 2: memory listener initialization failed:
Region smmuv3-iommu-memory-region-0-6: device 01.00.0 requires iommu MAP notifier which is not currently supported"Alex, any opinion?
Peter, I don't really understand what the comment is here. Is it the number of prepends on the error message? I don't really have an opinion on that so long as the end message makes sense. Seems like if we're familiar with the error generation it helps to unwind the context. Thanks,
Alex
Re: [PATCH v3 1/2] vfio: Turn the container error into an Error handle
Quoted 29 lines · Alex Williamson
Quoted 26 lines · On Mon, 23 Sep 2019 13:43:08 +0200 Auger Eric <[email protected]> wrote:
Quoted 16 lines · Peter Xu
hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1324
Quoted 10 lines · Eric Auger
hw/vfio/common.c +3
-3@@ -1308,9 +1319,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, &address_space_memory);if (container->error) {memory_listener_unregister (&container->prereg_listener); - ret = container->error; -error_setg (errp, -"RAM memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"RAM memory listener initialization failed: " );(I saw that we've got plenty of prepended prefixes for an error
messages. For me I'll disgard quite a few of them because the errors will directly be delivered to the top level user, but this might be too personal as a comment)That's true we have a lot of prefix messages.
The output message now is:
"vfio 0000:89:00.0: failed
to setup container for group 2: memory listener initialization failed:
Region smmuv3-iommu-memory-region-0-6: device 01.00.0 requires iommu MAP notifier which is not currently supported"
Quoted 7 lines
Quoted 1 line
Alex, any opinion?
Peter, I don't really understand what the comment is here. Is it the number of prepends on the error message? I don't really have an opinion on that so long as the end message makes sense. Seems like if we're familiar with the error generation it helps to unwind the context. Thanks,
True, the only major difference of the error that this series is working on is that the user can easily trigger this simply by plugging a device hence I'm not sure whether that's too long (it's not really a comment and that's why I put it in brackets :). Let's just keep them.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v3 1/2] vfio: Turn the container error into an Error handle
In reply to Eric AugerQuoted 20 lines · On Mon, 23 Sep 2019 08:55:51 +0200 Eric Auger <[email protected]> wrote:
The container error integer field is currently used to store the first error potentially encountered during any
vfio_listener_region_add()call. However this fails to propagate detailed error messages up to thevfio_connect_containercaller. Instead of using an integer, let's use an Error handle.Messages are slightly reworded to accomodate the propagation.
Signed-off-by Eric Auger <[email protected]>--- hw/vfio/common.c | 61 +++++++++++++++++++++-------------- hw/vfio/spapr.c | 4 ++- include/hw/vfio/vfio-common.h | 2 +- 3 files changed, 40 insertions(+), 27 deletions(-)hw/vfio/common.c +0
-0
hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 506
Quoted 4 lines
hw/vfio/common.c +1
-0@@ -503,12 +503,14 @@ static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) {VFIOContainer *container =container_of (listener, VFIOContainer, listener); +MemoryRegion *mr = section->mr;
This looks like an entirely secondary change that obscures the primary purpose of this patch and isn't mentioned in the changelog. It's also a bit inconsistent in places where we're references section->size and section->offset_within_address_space, but now mr instead of section->mr.
Quoted 11 lines
hw/vfio/common.c +1
-0hwaddr iova, end;Int128 llend, llsize;void *vaddr;int ret;VFIOHostDMAWindow *hostwin;bool hostwin_found; +Error *err =NULL ;if (vfio_listener_skipped_section (section)) { trace_vfio_listener_region_add_skip(
hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 551
Quoted 7 lines
hw/vfio/common.c +3
-0@@ -543,6 +545,9 @@ static void vfio_listener_region_add(MemoryListener *listener, hostwin->max_iova - hostwin->min_iova + 1, section->offset_within_address_space, int128_get64(section->size))) { +error_setg (&err,"Overlap with existing IOMMU range " +"[0x%" PRIx64",0x%" PRIx64"]" , hostwin->min_iova, + hostwin->max_iova - hostwin->min_iova +1 ); ret =-1 ;
Agree with Peter here, we should no longer be gratuitously setting ret when it's not consumed.
Alexey or David might want to comment on the error message here since we didn't have one previously, but we're only providing half the story above, the existing window that interferes but not the range we attempted to add that it interferes with.
Quoted 20 lines
hw/vfio/common.c +2
-1goto fail; }@@ -550,6 +555,7 @@ static void vfio_listener_region_add(MemoryListener *listener, ret =vfio_spapr_create_window (container, section, &pgsize);if (ret) { +error_setg_errno (&err, -ret,"Failed to create SPAPR window" ); goto fail; }@@ -559,7 +565,7 @@ static void vfio_listener_region_add(MemoryListener *listener, #ifdef CONFIG_KVM if (kvm_enabled()) { VFIOGroup *group; - IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); + IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(mr); struct kvm_vfio_spapr_tce param; struct kvm_device_attr attr = { .group = KVM_DEV_VFIO_GROUP,
hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 604
Quoted 8 lines
hw/vfio/common.c +2
-3@@ -594,18 +600,17 @@ static void vfio_listener_region_add(MemoryListener *listener, }if (!hostwin_found) { -error_report ("vfio: IOMMU container %p can't map guest IOVA region" -" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, - container, iova, end); +error_setg (&err,"Container %p can't map guest IOVA region" +" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end);
Note that here we print the start and end addresses, so I'm not sure why we chose to print [start,size] in the new message commented on above.
Quoted 77 lines
hw/vfio/common.c +14
-13ret = -EFAULT ; goto fail; } -memory_region_ref (section->mr); +memory_region_ref (mr); -if (memory_region_is_iommu (section->mr)) { +if (memory_region_is_iommu (mr)) {VFIOGuestIOMMU *giommu; -IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (section->mr); +IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (mr);int iommu_idx;trace_vfio_listener_region_add_iommu (iova, end);@@ -632,15 +637,15 @@ static void vfio_listener_region_add(MemoryListener *listener, iommu_idx);QLIST_INSERT_HEAD (&container->giommu_list, giommu, giommu_next); -memory_region_register_iommu_notifier (section->mr, &giommu->n); +memory_region_register_iommu_notifier (mr, &giommu->n);memory_region_iommu_replay (giommu->iommu, &giommu->n);return ; } -/* Here we assume that memory_region_is_ram(section->mr)==true */ +/* Here we assume that memory_region_is_ram(mr)==true */ - vaddr =memory_region_get_ram_ptr (section->mr) + + vaddr =memory_region_get_ram_ptr (mr) + section->offset_within_region + (iova - section->offset_within_address_space);@@ -648,12 +653,12 @@ static void vfio_listener_region_add(MemoryListener *listener, llsize =int128_sub (llend,int128_make64 (iova)); -if (memory_region_is_ram_device (section->mr)) { +if (memory_region_is_ram_device (mr)) {hwaddr pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ;if ((iova & pgmask) || (int128_get64 (llsize) & pgmask)) {trace_vfio_listener_region_add_no_dma_map ( -memory_region_name (section->mr), +memory_region_name (mr), section->offset_within_address_space,int128_getlo (section->size), pgmask +1 );@@ -664,11 +669,12 @@ static void vfio_listener_region_add(MemoryListener *listener, ret =vfio_dma_map (container, iova,int128_get64 (llsize), vaddr, section->readonly);if (ret) { -error_report ("vfio_dma_map(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx", %p) = %d (%m)" , - container, iova,int128_get64 (llsize), vaddr, ret); -if (memory_region_is_ram_device (section->mr)) { +error_setg (&err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx", %p) = %d (%m)" , + container, iova,int128_get64 (llsize), vaddr, ret); +if (memory_region_is_ram_device (mr)) {/* Allow unexpected mappings not to be fatal for RAM devices */ +error_report_err (err);return ; } goto fail;@@ -677,7 +683,7 @@ static void vfio_listener_region_add(MemoryListener *listener,return ; fail: -if (memory_region_is_ram_device (section->mr)) { +if (memory_region_is_ram_device (mr)) {error_report ("failed to vfio_dma_map. pci p2p may not work" );return ; }@@ -688,10 +694,14 @@ fail:*/
hw/vfio/common.c fail: · line 694
Quoted 10 lines
hw/vfio/common.c +4
-2if (!container->initialized) {if (!container->error) { - container->error = ret; +error_propagate_prepend (&container->error, err, +"Region %s: " ,memory_region_name (mr)); + }else { +error_free (err); } }else { -hw_error ("vfio: DMA mapping failed, unable to continue" );
As Peter notes, this removal is troubling. Thanks,
Alex
Quoted 74 lines
hw/vfio/common.c +9
-6+error_reportf_err (err, +"vfio: DMA mapping failed, unable to continue: " ); } }@@ -1251,6 +1261,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container =g_malloc0 (sizeof (*container)); container->space = space; container->fd = fd; + container->error =NULL ;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list);@@ -1308,9 +1319,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, &address_space_memory);if (container->error) {memory_listener_unregister (&container->prereg_listener); - ret = container->error; -error_setg (errp, -"RAM memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"RAM memory listener initialization failed: " ); goto free_container_exit; } }@@ -1365,9 +1376,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,memory_listener_register (&container->listener, container->space->as);if (container->error) { - ret = container->error; -error_setg_errno (errp, -ret, -"memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"memory listener initialization failed: " ); goto listener_release_exit; }hw/vfio/spapr.c +3
-1@@ -17,6 +17,7 @@#include "hw/hw.h" #include "exec/ram_addr.h" #include "qemu/error-report.h" +#include "qapi/error.h" #include "trace.h" static bool vfio_prereg_listener_skipped_section (MemoryRegionSection *section)@@ -85,7 +86,8 @@ static void vfio_prereg_listener_region_add(MemoryListener *listener,*/ if (!container->initialized) {if (!container->error) { - container->error = ret; +error_setg_errno (&container->error, -ret, +"Memory registering failed" ); } }else {hw_error ("vfio: Memory registering failed, unable to continue" );include/hw/vfio/vfio-common.h +1
-1@@ -71,7 +71,7 @@ typedef struct VFIOContainer {MemoryListener listener;MemoryListener prereg_listener;unsigned iommu_type; -int error; +Error *error;bool initialized;unsigned long pgsizes; QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
Re: [PATCH v3 1/2] vfio: Turn the container error into an Error handle
Hi Alex,
Quoted 18 lines · Alex Williamson
Quoted 15 lines · On Mon, 23 Sep 2019 08:55:51 +0200 Eric Auger <[email protected]> wrote:
The container error integer field is currently used to store the first error potentially encountered during any
vfio_listener_region_add()call. However this fails to propagate detailed error messages up to thevfio_connect_containercaller. Instead of using an integer, let's use an Error handle.Messages are slightly reworded to accomodate the propagation.
Signed-off-by Eric Auger <[email protected]>--- hw/vfio/common.c | 61 +++++++++++++++++++++-------------- hw/vfio/spapr.c | 4 ++- include/hw/vfio/vfio-common.h | 2 +- 3 files changed, 40 insertions(+), 27 deletions(-)
Quoted 14 lines
hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 506
Quoted 9 lines
hw/vfio/common.c +1
-0@@ -503,12 +503,14 @@ static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) {VFIOContainer *container =container_of (listener, VFIOContainer, listener); +MemoryRegion *mr = section->mr;This looks like an entirely secondary change that obscures the primary purpose of this patch and isn't mentioned in the changelog. It's also a bit inconsistent in places where we're references
section->sizeandsection->offset_within_address_space, but now mr instead ofsection->mr.
OK. I removed it.
Quoted 24 lines
Quoted 13 lines
hw/vfio/common.c +1
-0hwaddr iova, end;Int128 llend, llsize;void *vaddr;int ret;VFIOHostDMAWindow *hostwin;bool hostwin_found; +Error *err =NULL ;if (vfio_listener_skipped_section (section)) { trace_vfio_listener_region_add_skip(hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 551
Quoted 7 lines
hw/vfio/common.c +3
-0@@ -543,6 +545,9 @@ static void vfio_listener_region_add(MemoryListener *listener, hostwin->max_iova - hostwin->min_iova + 1, section->offset_within_address_space, int128_get64(section->size))) { +error_setg (&err,"Overlap with existing IOMMU range " +"[0x%" PRIx64",0x%" PRIx64"]" , hostwin->min_iova, + hostwin->max_iova - hostwin->min_iova +1 ); ret =-1 ;Agree with Peter here, we should no longer be gratuitously setting ret when it's not consumed.
Quoted 4 lines
Alexey or David might want to comment on the error message here since we didn't have one previously, but we're only providing half the story above, the existing window that interferes but not the range we attempted to add that it interferes with.
Now both the new range and the existing window are output
Quoted 23 lines
Quoted 23 lines
hw/vfio/common.c +2
-1goto fail; }@@ -550,6 +555,7 @@ static void vfio_listener_region_add(MemoryListener *listener, ret =vfio_spapr_create_window (container, section, &pgsize);if (ret) { +error_setg_errno (&err, -ret,"Failed to create SPAPR window" ); goto fail; }@@ -559,7 +565,7 @@ static void vfio_listener_region_add(MemoryListener *listener, #ifdef CONFIG_KVM if (kvm_enabled()) { VFIOGroup *group; - IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); + IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(mr); struct kvm_vfio_spapr_tce param; struct kvm_device_attr attr = { .group = KVM_DEV_VFIO_GROUP,@@ -594,18 +600,17 @@ static void vfio_listener_region_add(MemoryListener *listener, }
Quoted 10 lines
hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 604
Quoted 6 lines
hw/vfio/common.c +2
-3if (!hostwin_found) { -error_report ("vfio: IOMMU container %p can't map guest IOVA region" -" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, - container, iova, end); +error_setg (&err,"Container %p can't map guest IOVA region" +" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end);Note that here we print the start and end addresses, so I'm not sure why we chose to print [start,size] in the new message commented on above.
fixed
Quoted 82 lines
Quoted 82 lines
hw/vfio/common.c +15
-14ret = -EFAULT ; goto fail; } -memory_region_ref (section->mr); +memory_region_ref (mr); -if (memory_region_is_iommu (section->mr)) { +if (memory_region_is_iommu (mr)) {VFIOGuestIOMMU *giommu; -IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (section->mr); +IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (mr);int iommu_idx;trace_vfio_listener_region_add_iommu (iova, end);@@ -632,15 +637,15 @@ static void vfio_listener_region_add(MemoryListener *listener, iommu_idx);QLIST_INSERT_HEAD (&container->giommu_list, giommu, giommu_next); -memory_region_register_iommu_notifier (section->mr, &giommu->n); +memory_region_register_iommu_notifier (mr, &giommu->n);memory_region_iommu_replay (giommu->iommu, &giommu->n);return ; } -/* Here we assume that memory_region_is_ram(section->mr)==true */ +/* Here we assume that memory_region_is_ram(mr)==true */ - vaddr =memory_region_get_ram_ptr (section->mr) + + vaddr =memory_region_get_ram_ptr (mr) + section->offset_within_region + (iova - section->offset_within_address_space);@@ -648,12 +653,12 @@ static void vfio_listener_region_add(MemoryListener *listener, llsize =int128_sub (llend,int128_make64 (iova)); -if (memory_region_is_ram_device (section->mr)) { +if (memory_region_is_ram_device (mr)) {hwaddr pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ;if ((iova & pgmask) || (int128_get64 (llsize) & pgmask)) {trace_vfio_listener_region_add_no_dma_map ( -memory_region_name (section->mr), +memory_region_name (mr), section->offset_within_address_space,int128_getlo (section->size), pgmask +1 );@@ -664,11 +669,12 @@ static void vfio_listener_region_add(MemoryListener *listener, ret =vfio_dma_map (container, iova,int128_get64 (llsize), vaddr, section->readonly);if (ret) { -error_report ("vfio_dma_map(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx", %p) = %d (%m)" , - container, iova,int128_get64 (llsize), vaddr, ret); -if (memory_region_is_ram_device (section->mr)) { +error_setg (&err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx", %p) = %d (%m)" , + container, iova,int128_get64 (llsize), vaddr, ret); +if (memory_region_is_ram_device (mr)) {/* Allow unexpected mappings not to be fatal for RAM devices */ +error_report_err (err);return ; } goto fail;@@ -677,7 +683,7 @@ static void vfio_listener_region_add(MemoryListener *listener,return ; fail: -if (memory_region_is_ram_device (section->mr)) { +if (memory_region_is_ram_device (mr)) {error_report ("failed to vfio_dma_map. pci p2p may not work" );return ; }@@ -688,10 +694,14 @@ fail:*/ if (!container->initialized) {if (!container->error) { - container->error = ret; + error_propagate_prepend(&container->error, err,
Quoted 8 lines
hw/vfio/common.c fail: · line 694
Quoted 6 lines
hw/vfio/common.c +3
-1+ "Region %s: ", memory_region_name(mr)); + }else { +error_free (err); } }else { -hw_error ("vfio: DMA mapping failed, unable to continue" );As Peter notes, this removal is troubling. Thanks,
Corrected.
Thanks
Eric
Quoted 78 lines
Alex
Quoted 74 lines
hw/vfio/common.c +9
-6+error_reportf_err (err, +"vfio: DMA mapping failed, unable to continue: " ); } }@@ -1251,6 +1261,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container =g_malloc0 (sizeof (*container)); container->space = space; container->fd = fd; + container->error =NULL ;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list);@@ -1308,9 +1319,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, &address_space_memory);if (container->error) {memory_listener_unregister (&container->prereg_listener); - ret = container->error; -error_setg (errp, -"RAM memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"RAM memory listener initialization failed: " ); goto free_container_exit; } }@@ -1365,9 +1376,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,memory_listener_register (&container->listener, container->space->as);if (container->error) { - ret = container->error; -error_setg_errno (errp, -ret, -"memory listener initialization failed for container" ); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"memory listener initialization failed: " ); goto listener_release_exit; }hw/vfio/spapr.c +3
-1@@ -17,6 +17,7 @@#include "hw/hw.h" #include "exec/ram_addr.h" #include "qemu/error-report.h" +#include "qapi/error.h" #include "trace.h" static bool vfio_prereg_listener_skipped_section (MemoryRegionSection *section)@@ -85,7 +86,8 @@ static void vfio_prereg_listener_region_add(MemoryListener *listener,*/ if (!container->initialized) {if (!container->error) { - container->error = ret; +error_setg_errno (&container->error, -ret, +"Memory registering failed" ); } }else {hw_error ("vfio: Memory registering failed, unable to continue" );include/hw/vfio/vfio-common.h +1
-1@@ -71,7 +71,7 @@ typedef struct VFIOContainer {MemoryListener listener;MemoryListener prereg_listener;unsigned iommu_type; -int error; +Error *error;bool initialized;unsigned long pgsizes; QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
PATCH v3 2/2 memory: allow memory_region_register_iommu_notifier() to fail
In reply to Eric AugerCurrently, when a notifier is attempted to be registered and its flags are not supported (especially the MAP one) by the IOMMU MR, we generally abruptly exit in the IOMMU code. The failure could be handled more nicely in the caller and especially in the VFIO code.
So let's allow memory_region_register_iommu_notifier() to fail as well as notify_flag_changed() callback.
All sites implementing the callback are updated. This patch does not yet remove the exit(1) in the amd_iommu code.
in SMMUv3 we turn the warning message into an error message saying that the assigned device would not work properly.
---
v2 -> v3:
- turn the warning message into an error message
--- exec.c | 10 ++++++++-- hw/arm/smmuv3.c | 18 ++++++++++-------- hw/i386/amd_iommu.c | 17 ++++++++++------- hw/i386/intel_iommu.c | 8 +++++--- hw/ppc/spapr_iommu.c | 8 +++++--- hw/vfio/common.c | 8 ++++++-- hw/virtio/vhost.c | 9 +++++++-- include/exec/memory.h | 21 ++++++++++++++++----- memory.c | 31 ++++++++++++++++++++----------- 9 files changed, 87 insertions(+), 43 deletions(-)
exec.c +8 -2
@@ -663,7 +663,8 @@ static void tcg_register_iommu_notifier(CPUState *cpu,*/ MemoryRegion *mr =MEMORY_REGION (iommu_mr);TCGIOMMUNotifier *notifier; -int i; +Error *err =NULL ; +int i, ret;for (i =0 ; i < cpu->iommu_notifiers->len; i++) { notifier = g_array_index(cpu->iommu_notifiers, TCGIOMMUNotifier *, i);
@@ -692,7 +693,12 @@ static void tcg_register_iommu_notifier(CPUState *cpu,0 ,HWADDR_MAX , iommu_idx); -memory_region_register_iommu_notifier (notifier->mr, ¬ifier->n); + ret =memory_region_register_iommu_notifier (notifier->mr, ¬ifier->n, + &err); +if (ret) { +error_report_err (err); +exit (1 ); + } }if (!notifier->active) {
hw/arm/smmuv3.c +10 -8
@@ -1469,20 +1469,21 @@ static void smmuv3_class_init(ObjectClass *klass, void *data) dc->realize = smmu_realize; } -static void smmuv3_notify_flag_changed (IOMMUMemoryRegion *iommu, -IOMMUNotifierFlag old, -IOMMUNotifierFlag new) +static int smmuv3_notify_flag_changed (IOMMUMemoryRegion *iommu, +IOMMUNotifierFlag old, +IOMMUNotifierFlag new, +Error **errp) {SMMUDevice *sdev =container_of (iommu, SMMUDevice, iommu);SMMUv3State *s3 = sdev->smmu;SMMUState *s = &(s3->smmu_state);if (new &IOMMU_NOTIFIER_MAP ) { -int bus_num =pci_bus_num (sdev->bus); -PCIDevice *pcidev =pci_find_device (sdev->bus, bus_num, sdev->devfn); - -warn_report ("SMMUv3 does not support notification on MAP: " -"device %s will not function properly" , pcidev->name); +error_setg (errp, +"device %02x.%02x.%x requires iommu MAP notifier which is " +"not currently supported" ,pci_bus_num (sdev->bus), +PCI_SLOT (sdev->devfn),PCI_FUNC (sdev->devfn)); +return -EINVAL ; }if (old ==IOMMU_NOTIFIER_NONE ) {
@@ -1492,6 +1493,7 @@ static void smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu,trace_smmuv3_notify_flag_del (iommu->parent_obj.name);QLIST_REMOVE (sdev, next); } +return 0 ; } static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass,
hw/i386/amd_iommu.c +10 -7
@@ -1466,18 +1466,21 @@ static const MemoryRegionOps mmio_mem_ops = { } }; -static void amdvi_iommu_notify_flag_changed (IOMMUMemoryRegion *iommu, -IOMMUNotifierFlag old, -IOMMUNotifierFlag new) +static int amdvi_iommu_notify_flag_changed (IOMMUMemoryRegion *iommu, +IOMMUNotifierFlag old, +IOMMUNotifierFlag new, +Error **errp) {AMDVIAddressSpace *as =container_of (iommu, AMDVIAddressSpace, iommu);if (new &IOMMU_NOTIFIER_MAP ) { -error_report ("device %02x.%02x.%x requires iommu notifier which is not " -"currently supported" , as->bus_num,PCI_SLOT (as->devfn), -PCI_FUNC (as->devfn)); -exit (1 ); +error_setg (errp, +"device %02x.%02x.%x requires iommu notifier which is not " +"currently supported" , as->bus_num,PCI_SLOT (as->devfn), +PCI_FUNC (as->devfn)); +return -EINVAL ; } +return 0 ; }static void amdvi_init (AMDVIState *s)
hw/i386/intel_iommu.c +5 -3
@@ -2929,9 +2929,10 @@ static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,return iotlb; } -static void vtd_iommu_notify_flag_changed (IOMMUMemoryRegion *iommu, -IOMMUNotifierFlag old, -IOMMUNotifierFlag new) +static int vtd_iommu_notify_flag_changed (IOMMUMemoryRegion *iommu, +IOMMUNotifierFlag old, +IOMMUNotifierFlag new, +Error **errp) {VTDAddressSpace *vtd_as =container_of (iommu, VTDAddressSpace, iommu);IntelIOMMUState *s = vtd_as->iommu_state;
@@ -2944,6 +2945,7 @@ static void vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, }else if (new ==IOMMU_NOTIFIER_NONE ) {QLIST_REMOVE (vtd_as, next); } +return 0 ; }static int vtd_post_load (void *opaque,int version_id)
hw/ppc/spapr_iommu.c +5 -3
@@ -205,9 +205,10 @@ static int spapr_tce_get_attr(IOMMUMemoryRegion *iommu,return -EINVAL ; } -static void spapr_tce_notify_flag_changed (IOMMUMemoryRegion *iommu, -IOMMUNotifierFlag old, -IOMMUNotifierFlag new) +static int spapr_tce_notify_flag_changed (IOMMUMemoryRegion *iommu, +IOMMUNotifierFlag old, +IOMMUNotifierFlag new, +Error **errp) {struct SpaprTceTable *tbl =container_of (iommu, SpaprTceTable, iommu);
@@ -216,6 +217,7 @@ static void spapr_tce_notify_flag_changed(IOMMUMemoryRegion *iommu, }else if (old !=IOMMU_NOTIFIER_NONE && new ==IOMMU_NOTIFIER_NONE ) {spapr_tce_set_need_vfio (tbl, false); } +return 0 ; }static int spapr_tce_table_post_load (void *opaque,int version_id)
hw/vfio/common.c +6 -2
@@ -635,9 +635,13 @@ static void vfio_listener_region_add(MemoryListener *listener, section->offset_within_region,int128_get64 (llend), iommu_idx); -QLIST_INSERT_HEAD (&container->giommu_list, giommu, giommu_next); -memory_region_register_iommu_notifier (mr, &giommu->n); + ret =memory_region_register_iommu_notifier (mr, &giommu->n, &err); +if (ret) { +g_free (giommu); + goto fail; + } +QLIST_INSERT_HEAD (&container->giommu_list, giommu, giommu_next);memory_region_iommu_replay (giommu->iommu, &giommu->n);return ;
hw/virtio/vhost.c +7 -2
@@ -672,8 +672,9 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu_listener);struct vhost_iommu *iommu;Int128 end; -int iommu_idx; +int iommu_idx, ret;IOMMUMemoryRegion *iommu_mr; +Error *err =NULL ;if (!memory_region_is_iommu (section->mr)) {return ;
@@ -696,7 +697,11 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu->iommu_offset = section->offset_within_address_space - section->offset_within_region; iommu->hdev = dev; -memory_region_register_iommu_notifier (section->mr, &iommu->n); + ret =memory_region_register_iommu_notifier (section->mr, &iommu->n, &err); +if (ret) { +error_report_err (err); +exit (1 ); + }QLIST_INSERT_HEAD (&dev->iommu_list, iommu, iommu_next);/* TODO: can replay help performance here? */ }
include/exec/memory.h +16 -5
@@ -288,10 +288,16 @@ typedef struct IOMMUMemoryRegionClass {* @iommu: the IOMMUMemoryRegion * @old_flags: events which previously needed to be notified * @new_flags: events which now need to be notified +* +* Returns 0 on success, or a negative errno; in particular +* returns -EINVAL if the new flag bitmap is not supported by the +* IOMMU memory region. In case of failure, the error object +* must be created */ -void (*notify_flag_changed)(IOMMUMemoryRegion *iommu, -IOMMUNotifierFlag old_flags, -IOMMUNotifierFlag new_flags); +int (*notify_flag_changed)(IOMMUMemoryRegion *iommu, +IOMMUNotifierFlag old_flags, +IOMMUNotifierFlag new_flags, +Error **errp);/* Called to handle memory_region_iommu_replay(). * * The default implementation of memory_region_iommu_replay() is to
@@ -1067,13 +1073,18 @@ void memory_region_notify_one(IOMMUNotifier *notifier,* memory_region_register_iommu_notifier: register a notifier for changes to * IOMMU translation entries. * +* Returns 0 on success, or a negative errno otherwise. In particular, +* -EINVAL indicates that at least one of the attributes of the notifier +* is not supported (flag/range) by the IOMMU memory region. In case of error +* the error object must be created. +* * @mr: the memory region to observe * @n: the IOMMUNotifier to be added; the notify callback receives a * pointer to an #IOMMUTLBEntry as the opaque value; the pointer * ceases to be valid on exit from the notifier. */ -void memory_region_register_iommu_notifier (MemoryRegion *mr, -IOMMUNotifier *n); +int memory_region_register_iommu_notifier (MemoryRegion *mr, +IOMMUNotifier *n,Error **errp);/** * memory_region_iommu_replay: replay existing IOMMU translations to
memory.c +20 -11
@@ -1837,33 +1837,38 @@ bool memory_region_is_logging(MemoryRegion *mr, uint8_t client)return memory_region_get_dirty_log_mask (mr) & (1 << client); } -static void memory_region_update_iommu_notify_flags (IOMMUMemoryRegion *iommu_mr) +static int memory_region_update_iommu_notify_flags (IOMMUMemoryRegion *iommu_mr, +Error **errp) {IOMMUNotifierFlag flags =IOMMU_NOTIFIER_NONE ;IOMMUNotifier *iommu_notifier;IOMMUMemoryRegionClass *imrc =IOMMU_MEMORY_REGION_GET_CLASS (iommu_mr); +int ret =0 ; IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) { flags |= iommu_notifier->notifier_flags; }if (flags != iommu_mr->iommu_notify_flags && imrc->notify_flag_changed) { - imrc->notify_flag_changed (iommu_mr, - iommu_mr->iommu_notify_flags, - flags); + ret = imrc->notify_flag_changed (iommu_mr, + iommu_mr->iommu_notify_flags, + flags, errp); } - iommu_mr->iommu_notify_flags = flags; +if (!ret) { + iommu_mr->iommu_notify_flags = flags; + } +return ret; } -void memory_region_register_iommu_notifier (MemoryRegion *mr, -IOMMUNotifier *n) +int memory_region_register_iommu_notifier (MemoryRegion *mr, +IOMMUNotifier *n,Error **errp) {IOMMUMemoryRegion *iommu_mr; +int ret;if (mr->alias) { -memory_region_register_iommu_notifier (mr->alias, n); -return ; +return memory_region_register_iommu_notifier (mr->alias, n, errp); }/* We need to register for at least one bitfield */
@@ -1874,7 +1879,11 @@ void memory_region_register_iommu_notifier(MemoryRegion *mr, n->iommu_idx < memory_region_iommu_num_indexes(iommu_mr));QLIST_INSERT_HEAD (&iommu_mr->iommu_notify, n, node); -memory_region_update_iommu_notify_flags (iommu_mr); + ret =memory_region_update_iommu_notify_flags (iommu_mr, errp); +if (ret) { +QLIST_REMOVE (n, node); + } +return ret; }uint64_t memory_region_iommu_get_min_page_size (IOMMUMemoryRegion *iommu_mr)
@@ -1927,7 +1936,7 @@ void memory_region_unregister_iommu_notifier(MemoryRegion *mr, }QLIST_REMOVE (n, node); iommu_mr =IOMMU_MEMORY_REGION (mr); -memory_region_update_iommu_notify_flags (iommu_mr); +memory_region_update_iommu_notify_flags (iommu_mr,NULL ); } void memory_region_notify_one(IOMMUNotifier *notifier,
Signature · 1 line
2.20.1
Re: [PATCH v3 2/2] memory: allow memory_region_register_iommu_notifier() to fail
Quoted 8 lines · Eric Auger
Currently, when a notifier is attempted to be registered and its flags are not supported (especially the MAP one) by the IOMMU MR, we generally abruptly exit in the IOMMU code. The failure could be handled more nicely in the caller and especially in the VFIO code.
So let's allow
memory_region_register_iommu_notifier()to fail as well asnotify_flag_changed()callback.
Quoted 7 lines
All sites implementing the callback are updated. This patch does not yet remove the exit(1) in the
amd_iommucode.in SMMUv3 we turn the warning message into an error message saying that the assigned device would not work properly.
Signed-off-by Eric Auger <[email protected]>
Signature · 1 line
Peter Xu