iommu: Sanity check on param list for iommu_get_resv_regions
PATCH 1/1 iommu: Sanity check on param list for iommu_get_resv_regions
In iommu_get_resv_regions(), param list is an argument supplied by caller, into which callee is supposed to insert resv regions.
In other words, this 'list' argument is expected to be an empty list, so make an explicit annotation on it.
--- drivers/iommu/iommu.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/iommu/iommu.c +5 -4
@@ -813,7 +813,7 @@ int iommu_get_group_resv_regions(struct iommu_group *group,mutex_lock (&group->mutex);for_each_group_device (group, device) { -struct list_head dev_resv_regions; +LIST_HEAD (dev_resv_regions);/* * Non-API groups still expose reserved_regions in sysfs,
@@ -822,7 +822,6 @@ int iommu_get_group_resv_regions(struct iommu_group *group,if (!device->dev->iommu)break ; -INIT_LIST_HEAD (&dev_resv_regions);iommu_get_resv_regions (device->dev, &dev_resv_regions); ret =iommu_insert_device_resv_regions (&dev_resv_regions, head);iommu_put_resv_regions (device->dev, &dev_resv_regions);
@@ -1061,12 +1060,11 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain, struct device *dev) {struct iommu_resv_region *entry; -struct list_head mappings;unsigned long pg_size; +LIST_HEAD (mappings);int ret =0 ; pg_size = domain->pgsize_bitmap ?1UL <<__ffs (domain->pgsize_bitmap) :0 ; -INIT_LIST_HEAD (&mappings);if (WARN_ON_ONCE (iommu_is_dma_domain (domain) && !pg_size))return -EINVAL ;
@@ -2813,6 +2811,9 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) {const struct iommu_ops *ops =dev_iommu_ops (dev); +if (WARN_ON (!list_empty (list))) +return ; +if (ops->get_resv_regions) ops->get_resv_regions (dev, list); }
Signature · 1 line
2.25.1
Quoted 47 lines · Dawei Li
In
iommu_get_resv_regions(), param list is an argument supplied by caller, into which callee is supposed to insert resv regions.In other words, this 'list' argument is expected to be an empty list, so make an explicit annotation on it.
Signed-off-by Dawei Li <[email protected]>--- drivers/iommu/iommu.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/iommu/iommu.c +2
-4@@ -813,7 +813,7 @@ int iommu_get_group_resv_regions(struct iommu_group *group,mutex_lock (&group->mutex);for_each_group_device (group, device) { -struct list_head dev_resv_regions; +LIST_HEAD (dev_resv_regions);/* * Non-API groups still expose reserved_regions in sysfs, @@ -822,7 +822,6 @@ int iommu_get_group_resv_regions(struct iommu_group *group,if (!device->dev->iommu)break ; -INIT_LIST_HEAD (&dev_resv_regions);iommu_get_resv_regions (device->dev, &dev_resv_regions); ret =iommu_insert_device_resv_regions (&dev_resv_regions, head);iommu_put_resv_regions (device->dev, &dev_resv_regions);@@ -1061,12 +1060,11 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain, struct device *dev) {struct iommu_resv_region *entry; -struct list_head mappings;unsigned long pg_size; +LIST_HEAD (mappings);int ret =0 ; pg_size = domain->pgsize_bitmap ?1UL <<__ffs (domain->pgsize_bitmap) :0 ; -INIT_LIST_HEAD (&mappings);if (WARN_ON_ONCE (iommu_is_dma_domain (domain) && !pg_size))return -EINVAL ;
drivers/iommu/iommu.c void iommu_get_resv_regions(struct device *dev, struct list_head *list) · line 2815
Quoted 5 lines
drivers/iommu/iommu.c +2
-0@@ -2813,6 +2811,9 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) {const struct iommu_ops *ops =dev_iommu_ops (dev); +if (WARN_ON (!list_empty (list))) +return ;
I don't understand why the input list *must* be empty. This interface has already been exported, so please update the comment to explain this new requirement.
drivers/iommu/iommu.c void iommu_get_resv_regions(struct device *dev, struct list_head *list) · line 2819
Quoted 4 lines
drivers/iommu/iommu.c +1
-0+if (ops->get_resv_regions) ops->get_resv_regions (dev, list); }
Best regards,
baolu
Hi,
Thanks for reviewing,
Quoted 41 lines · Baolu Lu
Quoted 40 lines · Dawei Li
In
iommu_get_resv_regions(), param list is an argument supplied by caller, into which callee is supposed to insert resv regions.In other words, this 'list' argument is expected to be an empty list, so make an explicit annotation on it.
Signed-off-by Dawei Li <[email protected]>--- drivers/iommu/iommu.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/iommu/iommu.c +2
-4@@ -813,7 +813,7 @@ int iommu_get_group_resv_regions(struct iommu_group *group,mutex_lock (&group->mutex);for_each_group_device (group, device) { -struct list_head dev_resv_regions; +LIST_HEAD (dev_resv_regions);/* * Non-API groups still expose reserved_regions in sysfs, @@ -822,7 +822,6 @@ int iommu_get_group_resv_regions(struct iommu_group *group,if (!device->dev->iommu)break ; -INIT_LIST_HEAD (&dev_resv_regions);iommu_get_resv_regions (device->dev, &dev_resv_regions); ret =iommu_insert_device_resv_regions (&dev_resv_regions, head);iommu_put_resv_regions (device->dev, &dev_resv_regions);@@ -1061,12 +1060,11 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain, struct device *dev) {struct iommu_resv_region *entry; -struct list_head mappings;unsigned long pg_size; +LIST_HEAD (mappings);int ret =0 ; pg_size = domain->pgsize_bitmap ?1UL <<__ffs (domain->pgsize_bitmap) :0 ; -INIT_LIST_HEAD (&mappings);if (WARN_ON_ONCE (iommu_is_dma_domain (domain) && !pg_size))
Quoted 8 lines
drivers/iommu/iommu.c void iommu_get_resv_regions(struct device *dev, struct list_head *list) · line 2814
Quoted 6 lines
drivers/iommu/iommu.c +2
-0return -EINVAL ;@@ -2813,6 +2811,9 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) {const struct iommu_ops *ops =dev_iommu_ops (dev); +if (WARN_ON (!list_empty (list))) +return ;I don't understand why the input list *must* be empty. This interface
Because @list is an output-only argument, which is supposed to be filled by caller(inserting elements into it). If it's not empty, it's an inputing argument, in which case caller will take existing node (in @list) into account, and insert new nodes before/after them.
Please lemme put it another way, if list argment is not empty:
Before calling:
list: head->A
After calling
list: head->A->B->C
It will confuse caller cuz it can't tell whether A is a valid returned by callee.
Quoted 10 lines
has already been exported, so please update the comment to explain this new requirement.
drivers/iommu/iommu.c void iommu_get_resv_regions(struct device *dev, struct list_head *list) · line 2819
Quoted 4 lines
drivers/iommu/iommu.c +1
-0+if (ops->get_resv_regions) ops->get_resv_regions (dev, list); }Best regards,
baolu
Quoted 55 lines · Dawei Li
Quoted 40 lines · Baolu Lu
Quoted 39 lines · Dawei Li
In
iommu_get_resv_regions(), param list is an argument supplied by caller, into which callee is supposed to insert resv regions.In other words, this 'list' argument is expected to be an empty list, so make an explicit annotation on it.
Signed-off-by Dawei Li<[email protected]>--- drivers/iommu/iommu.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/iommu/iommu.c +2
-4@@ -813,7 +813,7 @@ int iommu_get_group_resv_regions(struct iommu_group *group,mutex_lock (&group->mutex);for_each_group_device (group, device) { -struct list_head dev_resv_regions; +LIST_HEAD (dev_resv_regions);/* * Non-API groups still expose reserved_regions in sysfs, @@ -822,7 +822,6 @@ int iommu_get_group_resv_regions(struct iommu_group *group,if (!device->dev->iommu)break ; -INIT_LIST_HEAD (&dev_resv_regions);iommu_get_resv_regions (device->dev, &dev_resv_regions); ret =iommu_insert_device_resv_regions (&dev_resv_regions, head);iommu_put_resv_regions (device->dev, &dev_resv_regions);@@ -1061,12 +1060,11 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain, struct device *dev) {struct iommu_resv_region *entry; -struct list_head mappings;unsigned long pg_size; +LIST_HEAD (mappings);int ret =0 ; pg_size = domain->pgsize_bitmap ?1UL <<__ffs (domain->pgsize_bitmap) :0 ; -INIT_LIST_HEAD (&mappings);Quoted 8 lines
drivers/iommu/iommu.c void iommu_get_resv_regions(struct device *dev, struct list_head *list) · line 2814
Quoted 7 lines
drivers/iommu/iommu.c +2
-0if (WARN_ON_ONCE (iommu_is_dma_domain (domain) && !pg_size))return -EINVAL ;@@ -2813,6 +2811,9 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) {const struct iommu_ops *ops =dev_iommu_ops (dev); +if (WARN_ON (!list_empty (list))) +return ;I don't understand why the input list*must* be empty. This interface
Because @list is an output-only argument, which is supposed to be filled by caller(inserting elements into it). If it's not empty, it's an inputing argument, in which case caller will take existing node (in @list) into account, and insert new nodes before/after them.
Please lemme put it another way, if list argment is not empty:
Quoted 8 lines
Before calling:
list:head->AAfter calling
list:head->A->B->CIt will confuse caller cuz it can't tell whether A is a valid returned by callee.
I see. Thank you for the explanation.
Best regards,
baolu
Robin Murphy
reviewerQuoted 53 lines · Dawei Li
Hi,
Thanks for reviewing,Quoted 49 lines · Baolu Lu
Quoted 42 lines · Dawei Li
In
iommu_get_resv_regions(), param list is an argument supplied by caller, into which callee is supposed to insert resv regions.In other words, this 'list' argument is expected to be an empty list, so make an explicit annotation on it.
Signed-off-by Dawei Li <[email protected]>--- drivers/iommu/iommu.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/iommu/iommu.c +2
-4@@ -813,7 +813,7 @@ int iommu_get_group_resv_regions(struct iommu_group *group,mutex_lock (&group->mutex);for_each_group_device (group, device) { -struct list_head dev_resv_regions; +LIST_HEAD (dev_resv_regions);/* * Non-API groups still expose reserved_regions in sysfs, @@ -822,7 +822,6 @@ int iommu_get_group_resv_regions(struct iommu_group *group,if (!device->dev->iommu)break ; -INIT_LIST_HEAD (&dev_resv_regions);iommu_get_resv_regions (device->dev, &dev_resv_regions); ret =iommu_insert_device_resv_regions (&dev_resv_regions, head);iommu_put_resv_regions (device->dev, &dev_resv_regions);@@ -1061,12 +1060,11 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain, struct device *dev) {struct iommu_resv_region *entry; -struct list_head mappings;unsigned long pg_size; +LIST_HEAD (mappings);int ret =0 ; pg_size = domain->pgsize_bitmap ?1UL <<__ffs (domain->pgsize_bitmap) :0 ; -INIT_LIST_HEAD (&mappings);if (WARN_ON_ONCE (iommu_is_dma_domain (domain) && !pg_size))return -EINVAL ;drivers/iommu/iommu.c void iommu_get_resv_regions(struct device *dev, struct list_head *list) · line 2814
Quoted 4 lines
drivers/iommu/iommu.c +2
-0@@ -2813,6 +2811,9 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) {const struct iommu_ops *ops =dev_iommu_ops (dev); +if (WARN_ON (!list_empty (list))) +return ;I don't understand why the input list *must* be empty. This interface
Yeah, the commit message really doesn't make much sense :(
Quoted 6 lines
Because @list is an output-only argument, which is supposed to be filled by caller(inserting elements into it). If it's not empty, it's an inputing argument, in which case caller will take existing node (in @list) into account, and insert new nodes before/after them.
Please lemme put it another way, if list argment is not empty:
Quoted 8 lines
Before calling:
list:head->AAfter calling
list:head->A->B->CIt will confuse caller cuz it can't tell whether A is a valid returned by callee.
If a caller would be confused by appending to a non-empty list then that caller should avoid passing a non-empty list. But that's not the API's problem; in general, appending to non-empty lists is absolutely a valid thing to do, it's kind of the point of using a list rather than, say, returning an array. It seems entirely reasonable that a caller might want to collect the reserved regions for multiple groups into a single list for its own convenience, and we have absolutely no reason to disallow that.
Note also that your arbitrary input vs. output argument rule fundamentally couldn't work for this API, since actual implementations of ops->get_resv_regions already *do* build up the list by passing it around multiple different helper APIs internally (look at the call path through arm_smmu_get_resv_regions(), for instance).
Thanks,
Robin.
Quoted 10 lines
Quoted 10 lines
has already been exported, so please update the comment to explain this new requirement.
drivers/iommu/iommu.c void iommu_get_resv_regions(struct device *dev, struct list_head *list) · line 2819
Quoted 4 lines
drivers/iommu/iommu.c +1
-0+if (ops->get_resv_regions) ops->get_resv_regions (dev, list); }Best regards,
baolu