Re: [PATCH v5 2/7] iommu/vt-d: Duplicate iommu_resv_region objects per device list

From: Auger Eric
Date: Wed May 29 2019 - 11:44:37 EST


Hi Lu,

On 5/29/19 4:04 AM, Lu Baolu wrote:
> Hi Eric,
>
> On 5/28/19 7:50 PM, Eric Auger wrote:
>> intel_iommu_get_resv_regions() aims to return the list of
>> reserved regions accessible by a given @device. However several
>> devices can access the same reserved memory region and when
>> building the list it is not safe to use a single iommu_resv_region
>> object, whose container is the RMRR. This iommu_resv_region must
>> be duplicated per device reserved region list.
>>
>> Let's remove the struct iommu_resv_region from the RMRR unit
>> and allocate the iommu_resv_region directly in
>> intel_iommu_get_resv_regions(). We hold the dmar_global_lock instead
>> of the rcu-lock to allow sleeping.
>>
>> Fixes: 0659b8dc45a6 ("iommu/vt-d: Implement reserved region get/put
>> callbacks")
>> Signed-off-by: Eric Auger <eric.auger@xxxxxxxxxx>
>>
>> ---
>>
>> v4 -> v5
>> - replace rcu-lock by the dmar_global_lock
>> ---
>> Â drivers/iommu/intel-iommu.c | 34 +++++++++++++++++-----------------
>> Â 1 file changed, 17 insertions(+), 17 deletions(-)
>>
>> diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c
>> index a209199f3af6..5ec8b5bd308f 100644
>> --- a/drivers/iommu/intel-iommu.c
>> +++ b/drivers/iommu/intel-iommu.c
>> @@ -322,7 +322,6 @@ struct dmar_rmrr_unit {
>> ÂÂÂÂÂ u64ÂÂÂ end_address;ÂÂÂÂÂÂÂ /* reserved end address */
>> ÂÂÂÂÂ struct dmar_dev_scope *devices;ÂÂÂ /* target devices */
>> ÂÂÂÂÂ intÂÂÂ devices_cnt;ÂÂÂÂÂÂÂ /* target device count */
>> -ÂÂÂ struct iommu_resv_region *resv; /* reserved region handle */
>> Â };
>> Â Â struct dmar_atsr_unit {
>> @@ -4205,7 +4204,6 @@ static inline void init_iommu_pm_ops(void) {}
>> Â int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void
>> *arg)
>> Â {
>> ÂÂÂÂÂ struct acpi_dmar_reserved_memory *rmrr;
>> -ÂÂÂ int prot = DMA_PTE_READ|DMA_PTE_WRITE;
>> ÂÂÂÂÂ struct dmar_rmrr_unit *rmrru;
>> ÂÂÂÂÂ size_t length;
>> Â @@ -4219,22 +4217,16 @@ int __init dmar_parse_one_rmrr(struct
>> acpi_dmar_header *header, void *arg)
>> ÂÂÂÂÂ rmrru->end_address = rmrr->end_address;
>> Â ÂÂÂÂÂ length = rmrr->end_address - rmrr->base_address + 1;
>> -ÂÂÂ rmrru->resv = iommu_alloc_resv_region(rmrr->base_address, length,
>> prot,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ IOMMU_RESV_DIRECT);
>> -ÂÂÂ if (!rmrru->resv)
>> -ÂÂÂÂÂÂÂ goto free_rmrru;
>> Â ÂÂÂÂÂ rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ ((void *)rmrr) + rmrr->header.length,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &rmrru->devices_cnt);
>> ÂÂÂÂÂ if (rmrru->devices_cnt && rmrru->devices == NULL)
>> -ÂÂÂÂÂÂÂ goto free_all;
>> +ÂÂÂÂÂÂÂ goto free_rmrru;
>> Â ÂÂÂÂÂ list_add(&rmrru->list, &dmar_rmrr_units);
>> Â ÂÂÂÂÂ return 0;
>> -free_all:
>> -ÂÂÂ kfree(rmrru->resv);
>> Â free_rmrru:
>> ÂÂÂÂÂ kfree(rmrru);
>> Â out:
>> @@ -4452,7 +4444,6 @@ static void intel_iommu_free_dmars(void)
>> ÂÂÂÂÂ list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
>> ÂÂÂÂÂÂÂÂÂ list_del(&rmrru->list);
>> ÂÂÂÂÂÂÂÂÂ dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
>> -ÂÂÂÂÂÂÂ kfree(rmrru->resv);
>> ÂÂÂÂÂÂÂÂÂ kfree(rmrru);
>> ÂÂÂÂÂ }
>> Â @@ -5470,22 +5461,33 @@ static void intel_iommu_remove_device(struct
>> device *dev)
>> Â static void intel_iommu_get_resv_regions(struct device *device,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct list_head *head)
>> Â {
>> +ÂÂÂ int prot = DMA_PTE_READ|DMA_PTE_WRITE;
>
> I know this is moved from above. How about adding spaces around the '|'?
sure
>
>> ÂÂÂÂÂ struct iommu_resv_region *reg;
>> ÂÂÂÂÂ struct dmar_rmrr_unit *rmrr;
>> ÂÂÂÂÂ struct device *i_dev;
>> ÂÂÂÂÂ int i;
>> Â -ÂÂÂ rcu_read_lock();
>> +ÂÂÂ down_write(&dmar_global_lock);
>
> Just out of curiosity, why not down_read()? We don't change the rmrr
> list here, right?
you're right, my mistake.
>
>> ÂÂÂÂÂ for_each_rmrr_units(rmrr) {
>> ÂÂÂÂÂÂÂÂÂ for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ i, i_dev) {
>> +ÂÂÂÂÂÂÂÂÂÂÂ struct iommu_resv_region *resv;
>> +ÂÂÂÂÂÂÂÂÂÂÂ size_t length;
>> +
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ if (i_dev != device)
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ continue;
>> Â -ÂÂÂÂÂÂÂÂÂÂÂ list_add_tail(&rmrr->resv->list, head);
>> +ÂÂÂÂÂÂÂÂÂÂÂ length = rmrr->end_address - rmrr->base_address + 1;
>> +ÂÂÂÂÂÂÂÂÂÂÂ resv = iommu_alloc_resv_region(rmrr->base_address,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ length, prot,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ IOMMU_RESV_DIRECT);
>> +ÂÂÂÂÂÂÂÂÂÂÂ if (!resv)
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
>> +
>> +ÂÂÂÂÂÂÂÂÂÂÂ list_add_tail(&resv->list, head);
>> ÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂ }
>> -ÂÂÂ rcu_read_unlock();
>> +ÂÂÂ up_write(&dmar_global_lock);
>> Â ÂÂÂÂÂ reg = iommu_alloc_resv_region(IOAPIC_RANGE_START,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ IOAPIC_RANGE_END - IOAPIC_RANGE_START + 1,
>> @@ -5500,10 +5502,8 @@ static void intel_iommu_put_resv_regions(struct
>> device *dev,
>> Â {
>> ÂÂÂÂÂ struct iommu_resv_region *entry, *next;
>> Â -ÂÂÂ list_for_each_entry_safe(entry, next, head, list) {
>> -ÂÂÂÂÂÂÂ if (entry->type == IOMMU_RESV_MSI)
>> -ÂÂÂÂÂÂÂÂÂÂÂ kfree(entry);
>> -ÂÂÂ }
>> +ÂÂÂ list_for_each_entry_safe(entry, next, head, list)
>> +ÂÂÂÂÂÂÂ kfree(entry);
>> Â }
>> Â Â int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct
>> device *dev)
>>
>
> Other looks good to me.
>
> Reviewed-by: Lu Baolu <baolu.lu@xxxxxxxxxxxxxxx>
Thanks!

Eric
>
> Best regards,
> Baolu