On 19/02/2024 10:05, Avihai Horon wrote:
> Hi Joao,
> 
> On 12/02/2024 15:56, Joao Martins wrote:
>> External email: Use caution opening links or attachments
>>
>>
>> Allow disabling hugepages to be dirty track at base page
>> granularity in similar vein to vfio_type1_iommu.disable_hugepages
>> but per IOAS.
>>
>> Signed-off-by: Joao Martins <joao.m.mart...@oracle.com>
>> ---
>>   backends/iommufd.c       | 36 ++++++++++++++++++++++++++++++++++++
>>   backends/trace-events    |  1 +
>>   hw/vfio/iommufd.c        |  4 ++++
>>   include/sysemu/iommufd.h |  4 ++++
>>   qapi/qom.json            |  2 +-
>>   5 files changed, 46 insertions(+), 1 deletion(-)
>>
>> diff --git a/backends/iommufd.c b/backends/iommufd.c
>> index dd676d493c37..72fd98a9a50c 100644
>> --- a/backends/iommufd.c
>> +++ b/backends/iommufd.c
>> @@ -29,6 +29,7 @@ static void iommufd_backend_init(Object *obj)
>>       be->fd = -1;
>>       be->users = 0;
>>       be->owned = true;
>> +    be->hugepages = 1;
>>   }
>>
>>   static void iommufd_backend_finalize(Object *obj)
>> @@ -63,6 +64,14 @@ static bool iommufd_backend_can_be_deleted(UserCreatable 
>> *uc)
>>       return !be->users;
>>   }
>>
>> +static void iommufd_backend_set_hugepages(Object *obj, bool enabled,
>> +                                          Error **errp)
>> +{
>> +    IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
>> +
>> +    be->hugepages = enabled;
>> +}
>> +
>>   static void iommufd_backend_class_init(ObjectClass *oc, void *data)
>>   {
>>       UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
>> @@ -70,6 +79,11 @@ static void iommufd_backend_class_init(ObjectClass *oc,
>> void *data)
>>       ucc->can_be_deleted = iommufd_backend_can_be_deleted;
>>
>>       object_class_property_add_str(oc, "fd", NULL, iommufd_backend_set_fd);
>> +
>> +    object_class_property_add_bool(oc, "hugepages", NULL,
>> +                                   iommufd_backend_set_hugepages);
>> +    object_class_property_set_description(oc, "hugepages",
>> +                                          "Set to 'off' to disable 
>> hugepages");
>>   }
>>
>>   int iommufd_backend_connect(IOMMUFDBackend *be, Error **errp)
>> @@ -106,6 +120,28 @@ out:
>>       trace_iommufd_backend_disconnect(be->fd, be->users);
>>   }
>>
>> +int iommufd_backend_set_option(int fd, uint32_t object_id,
>> +                               uint32_t option_id, uint64_t val64)
>> +{
>> +    int ret;
>> +    struct iommu_option option = {
>> +        .size = sizeof(option),
>> +        .option_id = option_id,
>> +        .op = IOMMU_OPTION_OP_SET,
>> +        .val64 = val64,
>> +        .object_id = object_id,
>> +    };
>> +
>> +    ret = ioctl(fd, IOMMU_OPTION, &option);
>> +    if (ret) {
>> +        error_report("Failed to set option %x to value %"PRIx64" %m", 
>> option_id,
>> +                     val64);
>> +    }
>> +    trace_iommufd_backend_set_option(fd, object_id, option_id, val64, ret);
>> +
>> +    return ret;
>> +}
>> +
>>   int iommufd_backend_alloc_ioas(IOMMUFDBackend *be, uint32_t *ioas_id,
>>                                  Error **errp)
>>   {
>> diff --git a/backends/trace-events b/backends/trace-events
>> index 11a27cb114b6..076166552881 100644
>> --- a/backends/trace-events
>> +++ b/backends/trace-events
>> @@ -15,6 +15,7 @@ iommufd_backend_unmap_dma_non_exist(int iommufd, uint32_t
>> ioas, uint64_t iova, u
>>   iommufd_backend_unmap_dma(int iommufd, uint32_t ioas, uint64_t iova,
>> uint64_t size, int ret) " iommufd=%d ioas=%d iova=0x%"PRIx64" 
>> size=0x%"PRIx64"
>> (%d)"
>>   iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id,
>> uint32_t flags, uint32_t hwpt_type, uint32_t len, uint64_t data_ptr, uint32_t
>> out_hwpt_id, int ret) " iommufd=%d dev_id=%u pt_id=%u flags=0x%x hwpt_type=%u
>> len=%u data_ptr=0x%"PRIx64" out_hwpt=%u (%d)"
>>   iommufd_backend_alloc_ioas(int iommufd, uint32_t ioas, int ret) " 
>> iommufd=%d
>> ioas=%d (%d)"
>> +iommufd_backend_set_option(int iommufd, uint32_t object_id, uint32_t
>> option_id, uint64_t val, int ret) " iommufd=%d object_id=%u option_id=%u
>> val64=0x%"PRIx64" (%d)"
>>   iommufd_backend_free_id(int iommufd, uint32_t id, int ret) " iommufd=%d
>> id=%d (%d)"
>>   iommufd_backend_set_dirty(int iommufd, uint32_t hwpt_id, bool start, int
>> ret) " iommufd=%d hwpt=%d enable=%d (%d)"
>>   iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t
>> iova, uint64_t size, uint64_t page_size, int ret) " iommufd=%d hwpt=%d
>> iova=0x%"PRIx64" size=0x%"PRIx64" page_size=0x%"PRIx64" (%d)"
>> diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
>> index 79b13bd262cc..697d40841d7f 100644
>> --- a/hw/vfio/iommufd.c
>> +++ b/hw/vfio/iommufd.c
>> @@ -521,6 +521,10 @@ static int iommufd_cdev_attach(const char *name,
>> VFIODevice *vbasedev,
>>           goto err_alloc_ioas;
>>       }
>>
>> +    if (!vbasedev->iommufd_dev.iommufd->hugepages) {
>> +        iommufd_backend_set_option(vbasedev->iommufd_dev.iommufd->fd, 
>> ioas_id,
>> +                                   IOMMU_OPTION_HUGE_PAGES, 0);
> 
> Shouldn't we fail device attach if iommufd_backend_set_option() fails?
> 

Let handle error correctly and fail the attach.

Reply via email to