hw/vfio/common: Refactor container initialization
PATCH v3 1/1 hw/vfio/common: Refactor container initialization
In vfio_connect_container() the code that selects the iommu type can benefit from helpers such as vfio_iommu_get_type() and vfio_init_container(). As a result we end up with a switch/case on the iommu type that makes the code a little bit more readable and ready for addition of new iommu types. Also ioctl's get called once per iommu_type.
---
v2 -> v3:
- fix ret/errno
v1 -> v2:
- handle SPAPR case, s/ret/errno in error error_setg_errno, fix ret value when vfio_iommu_get_type fails
- removed Greg's R-b
v1:
- originally submitted in [RFC v2 00/28] vSMMUv3/pSMMUv3 2 stage VFIO integration
--- hw/vfio/common.c | 112 +++++++++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 37 deletions(-)
hw/vfio/common.c +75 -37
@@ -1036,12 +1036,65 @@ static void vfio_put_address_space(VFIOAddressSpace *space) } } +/* +* vfio_iommu_get_type - selects the richest iommu_type (v2 first) +*/ +static int vfio_iommu_get_type (VFIOContainer *container, +Error **errp) +{ +int fd = container->fd; + +if (ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_TYPE1v2_IOMMU)) { +return VFIO_TYPE1v2_IOMMU; + }else if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_TYPE1_IOMMU )) { +return VFIO_TYPE1_IOMMU ; + }else if (ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_SPAPR_TCE_v2_IOMMU)) { +return VFIO_SPAPR_TCE_v2_IOMMU; + }else if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_SPAPR_TCE_IOMMU )) { +return VFIO_SPAPR_TCE_IOMMU ; + }else { +error_setg (errp,"No available IOMMU models" ); +return -EINVAL ; + } +} + +static int vfio_init_container (VFIOContainer *container,int group_fd, +int iommu_type,Error **errp) +{ +int ret; + + ret =ioctl (group_fd,VFIO_GROUP_SET_CONTAINER , &container->fd); +if (ret) { +error_setg_errno (errp, errno,"failed to set group container" ); +return -errno; + } + + ret =ioctl (container->fd,VFIO_SET_IOMMU , iommu_type); +if (ret && iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { +/* +* On sPAPR, despite the IOMMU subdriver always advertises v1 and v2, +* the running platform may not support v2 and there is no way to +* guess it until an IOMMU group gets added to the container. So in +* case it fails with v2, try v1 as a fallback +*/ + iommu_type =VFIO_SPAPR_TCE_IOMMU ; + ret =ioctl (container->fd,VFIO_SET_IOMMU , iommu_type); + } +if (ret) { +error_setg_errno (errp, errno,"failed to set iommu for container" ); +return -errno; + } + container->iommu_type = iommu_type; +return ret; +} +static int vfio_connect_container (VFIOGroup *group,AddressSpace *as,Error **errp) {VFIOContainer *container;int ret, fd;VFIOAddressSpace *space; +int iommu_type; space =vfio_get_address_space (as);
@@ -1101,23 +1154,21 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container->fd = fd;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list); -if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_TYPE1_IOMMU ) || -ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_TYPE1v2_IOMMU)) { -bool v2 = !!ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_TYPE1v2_IOMMU); + + iommu_type =vfio_iommu_get_type (container, errp); +if (iommu_type <0 ) { + ret = -EINVAL ; + goto free_container_exit; + } + +switch (iommu_type) { +case VFIO_TYPE1v2_IOMMU: +case VFIO_TYPE1_IOMMU : + {struct vfio_iommu_type1_info info; - ret =ioctl (group->fd,VFIO_GROUP_SET_CONTAINER , &fd); + ret =vfio_init_container (container, group->fd, iommu_type, errp);if (ret) { -error_setg_errno (errp, errno,"failed to set group container" ); - ret = -errno; - goto free_container_exit; - } - - container->iommu_type = v2 ? VFIO_TYPE1v2_IOMMU :VFIO_TYPE1_IOMMU ; - ret =ioctl (fd,VFIO_SET_IOMMU , container->iommu_type); -if (ret) { -error_setg_errno (errp, errno,"failed to set iommu for container" ); - ret = -errno; goto free_container_exit; }
@@ -1137,31 +1188,21 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, }vfio_host_win_add (container,0 , (hwaddr)-1 , info.iova_pgsizes); container->pgsizes = info.iova_pgsizes; - }else if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_SPAPR_TCE_IOMMU ) || -ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_SPAPR_TCE_v2_IOMMU)) { +break ; + } +case VFIO_SPAPR_TCE_v2_IOMMU: +case VFIO_SPAPR_TCE_IOMMU : + {struct vfio_iommu_spapr_tce_info info; -bool v2 = !!ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_SPAPR_TCE_v2_IOMMU); +bool v2; - ret =ioctl (group->fd,VFIO_GROUP_SET_CONTAINER , &fd); + ret =vfio_init_container (container, group->fd, iommu_type, errp);if (ret) { -error_setg_errno (errp, errno,"failed to set group container" ); - ret = -errno; - goto free_container_exit; - } - container->iommu_type = - v2 ? VFIO_SPAPR_TCE_v2_IOMMU :VFIO_SPAPR_TCE_IOMMU ; - ret =ioctl (fd,VFIO_SET_IOMMU , container->iommu_type); -if (ret) { - container->iommu_type =VFIO_SPAPR_TCE_IOMMU ; - v2 = false; - ret =ioctl (fd,VFIO_SET_IOMMU , container->iommu_type); - } -if (ret) { -error_setg_errno (errp, errno,"failed to set iommu for container" ); - ret = -errno; goto free_container_exit; } + v2 = container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU; +/* * The host kernel code implementing VFIO_IOMMU_DISABLE is called * when container fd is closed so we do not call it explicitly
@@ -1222,10 +1263,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, info.dma32_window_size -1 , 0x1000); } - }else { -error_setg (errp,"No available IOMMU models" ); - ret = -EINVAL ; - goto free_container_exit; + } }vfio_kvm_device_add_group (group);
Signature · 1 line
2.20.1
Quoted 10 lines · On Fri, 18 Jan 2019 09:55:25 +0100 Eric Auger <[email protected]> wrote:
In
vfio_connect_container()the code that selects the iommu type can benefit from helpers such asvfio_iommu_get_type()andvfio_init_container(). As a result we end up with a switch/case on the iommu type that makes the code a little bit more readable and ready for addition of new iommu types. Also ioctl's get called once periommu_type.Signed-off-by Eric Auger <[email protected]>
LGTM, just a minor comment below.
Quoted 88 lines
---
v2 -> v3:
- fix ret/errnov1 -> v2:
- handle SPAPR case, s/ret/errno in errorerror_setg_errno, fix ret value whenvfio_iommu_get_typefails
- removed Greg's R-bv1:
- originally submitted in [RFC v2 00/28] vSMMUv3/pSMMUv3 2 stage VFIO integration--- hw/vfio/common.c | 112 +++++++++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 37 deletions(-)hw/vfio/common.c +53
-0@@ -1036,12 +1036,65 @@ static void vfio_put_address_space(VFIOAddressSpace *space) } } +/* +* vfio_iommu_get_type - selects the richest iommu_type (v2 first) +*/ +static int vfio_iommu_get_type (VFIOContainer *container, +Error **errp) +{ +int fd = container->fd; + +if (ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_TYPE1v2_IOMMU)) { +return VFIO_TYPE1v2_IOMMU; + }else if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_TYPE1_IOMMU )) { +return VFIO_TYPE1_IOMMU ; + }else if (ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_SPAPR_TCE_v2_IOMMU)) { +return VFIO_SPAPR_TCE_v2_IOMMU; + }else if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_SPAPR_TCE_IOMMU )) { +return VFIO_SPAPR_TCE_IOMMU ; + }else { +error_setg (errp,"No available IOMMU models" ); +return -EINVAL ; + } +} + +static int vfio_init_container (VFIOContainer *container,int group_fd, +int iommu_type,Error **errp) +{ +int ret; + + ret =ioctl (group_fd,VFIO_GROUP_SET_CONTAINER , &container->fd); +if (ret) { +error_setg_errno (errp, errno,"failed to set group container" ); +return -errno; + } + + ret =ioctl (container->fd,VFIO_SET_IOMMU , iommu_type); +if (ret && iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { +/* +* On sPAPR, despite the IOMMU subdriver always advertises v1 and v2, +* the running platform may not support v2 and there is no way to +* guess it until an IOMMU group gets added to the container. So in +* case it fails with v2, try v1 as a fallback +*/ + iommu_type =VFIO_SPAPR_TCE_IOMMU ; + ret =ioctl (container->fd,VFIO_SET_IOMMU , iommu_type); + } +if (ret) { +error_setg_errno (errp, errno,"failed to set iommu for container" ); +return -errno; + } + container->iommu_type = iommu_type; +return ret; +} +static int vfio_connect_container (VFIOGroup *group,AddressSpace *as,Error **errp) {VFIOContainer *container;int ret, fd;VFIOAddressSpace *space; +int iommu_type; space =vfio_get_address_space (as);
hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1160
Quoted 10 lines
hw/vfio/common.c +4
-3@@ -1101,23 +1154,21 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container->fd = fd;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list); -if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_TYPE1_IOMMU ) || -ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_TYPE1v2_IOMMU)) { -bool v2 = !!ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_TYPE1v2_IOMMU); + + iommu_type =vfio_iommu_get_type (container, errp); +if (iommu_type <0 ) { + ret = -EINVAL ;
Since vfio_iommu_get_type() seems to return a negative errno on error, ret could maybe be set to iommu_type. No big deal though so:
Quoted 78 lines
hw/vfio/common.c +18
-34+ goto free_container_exit; + } + +switch (iommu_type) { +case VFIO_TYPE1v2_IOMMU: +case VFIO_TYPE1_IOMMU : + {struct vfio_iommu_type1_info info; - ret =ioctl (group->fd,VFIO_GROUP_SET_CONTAINER , &fd); + ret =vfio_init_container (container, group->fd, iommu_type, errp);if (ret) { -error_setg_errno (errp, errno,"failed to set group container" ); - ret = -errno; - goto free_container_exit; - } - - container->iommu_type = v2 ? VFIO_TYPE1v2_IOMMU :VFIO_TYPE1_IOMMU ; - ret =ioctl (fd,VFIO_SET_IOMMU , container->iommu_type); -if (ret) { -error_setg_errno (errp, errno,"failed to set iommu for container" ); - ret = -errno; goto free_container_exit; }@@ -1137,31 +1188,21 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, }vfio_host_win_add (container,0 , (hwaddr)-1 , info.iova_pgsizes); container->pgsizes = info.iova_pgsizes; - }else if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_SPAPR_TCE_IOMMU ) || -ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_SPAPR_TCE_v2_IOMMU)) { +break ; + } +case VFIO_SPAPR_TCE_v2_IOMMU: +case VFIO_SPAPR_TCE_IOMMU : + {struct vfio_iommu_spapr_tce_info info; -bool v2 = !!ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_SPAPR_TCE_v2_IOMMU); +bool v2; - ret =ioctl (group->fd,VFIO_GROUP_SET_CONTAINER , &fd); + ret =vfio_init_container (container, group->fd, iommu_type, errp);if (ret) { -error_setg_errno (errp, errno,"failed to set group container" ); - ret = -errno; - goto free_container_exit; - } - container->iommu_type = - v2 ? VFIO_SPAPR_TCE_v2_IOMMU :VFIO_SPAPR_TCE_IOMMU ; - ret =ioctl (fd,VFIO_SET_IOMMU , container->iommu_type); -if (ret) { - container->iommu_type =VFIO_SPAPR_TCE_IOMMU ; - v2 = false; - ret =ioctl (fd,VFIO_SET_IOMMU , container->iommu_type); - } -if (ret) { -error_setg_errno (errp, errno,"failed to set iommu for container" ); - ret = -errno; goto free_container_exit; } + v2 = container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU; +/* * The host kernel code implementing VFIO_IOMMU_DISABLE is called * when container fd is closed so we do not call it explicitly @@ -1222,10 +1263,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, info.dma32_window_size -1 , 0x1000); } - }else { -error_setg (errp,"No available IOMMU models" ); - ret = -EINVAL ; - goto free_container_exit; + } }vfio_kvm_device_add_group (group);
Quoted 32 lines · On Fri, 18 Jan 2019 09:55:25 +0100 Eric Auger <[email protected]> wrote:
In
vfio_connect_container()the code that selects the iommu type can benefit from helpers such asvfio_iommu_get_type()andvfio_init_container(). As a result we end up with a switch/case on the iommu type that makes the code a little bit more readable and ready for addition of new iommu types. Also ioctl's get called once periommu_type.Signed-off-by Eric Auger <[email protected]>---
v2 -> v3:
- fix ret/errnov1 -> v2:
- handle SPAPR case, s/ret/errno in errorerror_setg_errno, fix ret value whenvfio_iommu_get_typefails
- removed Greg's R-bv1:
- originally submitted in [RFC v2 00/28] vSMMUv3/pSMMUv3 2 stage VFIO integration--- hw/vfio/common.c | 112 +++++++++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 37 deletions(-)hw/vfio/common.c +0
-0
hw/vfio/common.c static void vfio_put_address_space(VFIOAddressSpace *space) · line 1043
Quoted 8 lines
hw/vfio/common.c +5
-0@@ -1036,12 +1036,65 @@ static void vfio_put_address_space(VFIOAddressSpace *space) } } +/* +* vfio_iommu_get_type - selects the richest iommu_type (v2 first) +*/ +static int vfio_iommu_get_type (VFIOContainer *container, +Error **errp)
vfio_get_iommu_type()?
Quoted 5 lines
hw/vfio/common.c +5
-0+{ +int fd = container->fd; + +if (ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_TYPE1v2_IOMMU)) { +return VFIO_TYPE1v2_IOMMU;
hw/vfio/common.c static void vfio_put_address_space(VFIOAddressSpace *space) · line 1058
Quoted 10 lines
hw/vfio/common.c +10
-0+ }else if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_TYPE1_IOMMU )) { +return VFIO_TYPE1_IOMMU ; + }else if (ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_SPAPR_TCE_v2_IOMMU)) { +return VFIO_SPAPR_TCE_v2_IOMMU; + }else if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_SPAPR_TCE_IOMMU )) { +return VFIO_SPAPR_TCE_IOMMU ; + }else { +error_setg (errp,"No available IOMMU models" ); +return -EINVAL ; + }
int i;
int iommu_types[] = { VFIO_TYPE1v2_IOMMU, VFIO_TYPE1_IOMMU,
VFIO_SPAPR_TCE_v2_IOMMU, VFIO_SPAPR_TCE_IOMMU };
for (i = 0; i < ARRAY_SIZE(iommu_types); i++) {
if (ioctl(fd, VFIO_CHECK_EXTENSION, iommu_types[i]) {
return iommu_types[i];
}
}error_setg(...
return -EINVAL;
Quoted 13 lines
hw/vfio/common.c +13
-0+} + +static int vfio_init_container (VFIOContainer *container,int group_fd, +int iommu_type,Error **errp) +{ +int ret; + + ret =ioctl (group_fd,VFIO_GROUP_SET_CONTAINER , &container->fd); +if (ret) { +error_setg_errno (errp, errno,"failed to set group container" ); +return -errno; + } +
hw/vfio/common.c static void vfio_put_address_space(VFIOAddressSpace *space) · line 1081
Quoted 10 lines
hw/vfio/common.c +10
-0+ ret =ioctl (container->fd,VFIO_SET_IOMMU , iommu_type); +if (ret && iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { +/* +* On sPAPR, despite the IOMMU subdriver always advertises v1 and v2, +* the running platform may not support v2 and there is no way to +* guess it until an IOMMU group gets added to the container. So in +* case it fails with v2, try v1 as a fallback +*/ + iommu_type =VFIO_SPAPR_TCE_IOMMU ; + ret =ioctl (container->fd,VFIO_SET_IOMMU , iommu_type);
This kind of break the semantics of the helper, which could be called vfio_init_container_type(), but the type requested is silently replaced. It looks like each case will first call this function, so why does vfio_connect_container() need to call vfio_get_iommu_type() directly? ie. vfio_connect_container() calls vfio_init_container(), vfio_init_container() calls vfio_get_iommu_type(), vfio_connect_container() acts on resulting container->iommu_type.
hw/vfio/common.c static void vfio_put_address_space(VFIOAddressSpace *space) · line 1086
Quoted 5 lines
hw/vfio/common.c +5
-0+ } +if (ret) { +error_setg_errno (errp, errno,"failed to set iommu for container" ); +return -errno; + }
Can avoid the double error test with a loop:
while (ioctl(container->fd, VFIO_SET_IOMMU, iommu_type)) {
if (iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
/* <explanation> */
iommu_type = VFIO_SPAPR_TCE_IOMMU;
continue;
}
error_setg_errno(errp, errno, "failed to set iommu for container");
return -errno;
}Quoted 77 lines
hw/vfio/common.c +24
-27+ container->iommu_type = iommu_type; +return ret; +} +static int vfio_connect_container (VFIOGroup *group,AddressSpace *as,Error **errp) {VFIOContainer *container;int ret, fd;VFIOAddressSpace *space; +int iommu_type; space =vfio_get_address_space (as);@@ -1101,23 +1154,21 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container->fd = fd;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list); -if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_TYPE1_IOMMU ) || -ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_TYPE1v2_IOMMU)) { -bool v2 = !!ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_TYPE1v2_IOMMU); + + iommu_type =vfio_iommu_get_type (container, errp); +if (iommu_type <0 ) { + ret = -EINVAL ; + goto free_container_exit; + } + +switch (iommu_type) { +case VFIO_TYPE1v2_IOMMU: +case VFIO_TYPE1_IOMMU : + {struct vfio_iommu_type1_info info; - ret =ioctl (group->fd,VFIO_GROUP_SET_CONTAINER , &fd); + ret =vfio_init_container (container, group->fd, iommu_type, errp);if (ret) { -error_setg_errno (errp, errno,"failed to set group container" ); - ret = -errno; - goto free_container_exit; - } - - container->iommu_type = v2 ? VFIO_TYPE1v2_IOMMU :VFIO_TYPE1_IOMMU ; - ret =ioctl (fd,VFIO_SET_IOMMU , container->iommu_type); -if (ret) { -error_setg_errno (errp, errno,"failed to set iommu for container" ); - ret = -errno; goto free_container_exit; }@@ -1137,31 +1188,21 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, }vfio_host_win_add (container,0 , (hwaddr)-1 , info.iova_pgsizes); container->pgsizes = info.iova_pgsizes; - }else if (ioctl (fd,VFIO_CHECK_EXTENSION ,VFIO_SPAPR_TCE_IOMMU ) || -ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_SPAPR_TCE_v2_IOMMU)) { +break ; + } +case VFIO_SPAPR_TCE_v2_IOMMU: +case VFIO_SPAPR_TCE_IOMMU : + {struct vfio_iommu_spapr_tce_info info; -bool v2 = !!ioctl (fd,VFIO_CHECK_EXTENSION , VFIO_SPAPR_TCE_v2_IOMMU); +bool v2; - ret =ioctl (group->fd,VFIO_GROUP_SET_CONTAINER , &fd); + ret =vfio_init_container (container, group->fd, iommu_type, errp);if (ret) { -error_setg_errno (errp, errno,"failed to set group container" ); - ret = -errno; - goto free_container_exit; - } - container->iommu_type = - v2 ? VFIO_SPAPR_TCE_v2_IOMMU :VFIO_SPAPR_TCE_IOMMU ; - ret =ioctl (fd,VFIO_SET_IOMMU , container->iommu_type); -if (ret) { - container->iommu_type =VFIO_SPAPR_TCE_IOMMU ;
hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1204
Quoted 10 lines
hw/vfio/common.c +1
-6- v2 = false; - ret =ioctl (fd,VFIO_SET_IOMMU , container->iommu_type); - } -if (ret) { -error_setg_errno (errp, errno,"failed to set iommu for container" ); - ret = -errno; goto free_container_exit; } + v2 = container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU;
Alternatively, convert the remaining users of v2 to test the iommu_type? Thanks,
Alex
Quoted 16 lines
hw/vfio/common.c +2
-4+/* * The host kernel code implementing VFIO_IOMMU_DISABLE is called * when container fd is closed so we do not call it explicitly @@ -1222,10 +1263,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, info.dma32_window_size -1 , 0x1000); } - }else { -error_setg (errp,"No available IOMMU models" ); - ret = -EINVAL ; - goto free_container_exit; + } }vfio_kvm_device_add_group (group);