iommu/arm-smmu-v3: Two vsmmu impl_ops cleanups
Subsystems (4)
v3 cover iommu/arm-smmu-v3: Two vsmmu impl_ops cleanups
Since the vsmmu and cmdqv patches were accepeted via the iommufd tree, this is based on Jason's for-next tree.
Per request from Will following the accepted latest vcmdq series to clean up the impl_ops:
https://lore.kernel.org/linux-iommu/aHE4Y-fbucm-j-yi@willie-the-truck/
"
It would be nice to avoid adding data members to the ops structure, if at all possible. The easiest thing would probably be to add a function for getting the vsmmu size and then pushing the two checks against 'vsmmu_type' down into the impl_ops callbacks so that:
1. If the type is IOMMU_VIOMMU_TYPE_ARM_SMMUV3, we don't bother with the impl_ops at all in arm_vsmmu_init() and arm_smmu_get_viommu_size()
2. Otherwise, we pass the type into the impl_ops and they can check it
Of course, that can be a patch on top of the series as there's no point respinning the whole just for this.
"
Add two clean up patches with the first one doing 1 and 2 to prioritize IOMMU_VIOMMU_TYPE_ARM_SMMUV3 always, and the second one dropping static vsmmu_type and vsmmu_size data members.
Changelog
v3:
* Add Acked-by from Will
* Use logical "!=" instead of arithmetic "^"
v2:
https://lore.kernel.org/all/[email protected]/
* Add Acked-by from Will
* Move get_viommu_size and vsmmu_init validation to arm_smmu_impl_probe()
v1:
https://lore.kernel.org/all/[email protected]/
Nicolin Chen (2):
iommu/arm-smmu-v3: Do not bother impl_ops if
IOMMU_VIOMMU_TYPE_ARM_SMMUV3
iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
.../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 20 +++++++++---------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 14 +++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 +-- .../iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 14 +++++++++++-- 4 files changed, 36 insertions(+), 15 deletions(-)
base-commit: ab6bc44159d8f0c4ee757e0ce041fa9033e0ead8
Signature · 1 line
2.43.0
PATCH v3 1/2 iommu/arm-smmu-v3: Do not bother impl_ops if IOMMU_VIOMMU_TYPE_ARM_SMMUV3
When viommu type is IOMMU_VIOMMU_TYPE_ARM_SMMUV3, always return or init the standard struct arm_vsmmu, instead of going through impl_ops that must have its own viommu type than the standard IOMMU_VIOMMU_TYPE_ARM_SMMUV3.
Given that arm_vsmmu_init() is called after arm_smmu_get_viommu_size(), any unsupported viommu->type must be a corruption. And it must be a driver bug that its vsmmu_size and vsmmu_init ops aren't paired. Warn these two cases.
--- .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 24 ++++++++++--------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 14 +++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +13 -11
@@ -420,14 +420,13 @@ size_t arm_smmu_get_viommu_size(struct device *dev, !(smmu->features & ARM_SMMU_FEAT_S2FWB))return 0 ; -if (smmu->impl_ops && smmu->impl_ops->vsmmu_size && - viommu_type == smmu->impl_ops->vsmmu_type) -return smmu->impl_ops->vsmmu_size; +if (viommu_type ==IOMMU_VIOMMU_TYPE_ARM_SMMUV3 ) + return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core); -if (viommu_type !=IOMMU_VIOMMU_TYPE_ARM_SMMUV3 ) +if (!smmu->impl_ops || !smmu->impl_ops->vsmmu_size || + viommu_type != smmu->impl_ops->vsmmu_type)return 0 ; - - return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core); +return smmu->impl_ops->vsmmu_size; } int arm_vsmmu_init(struct iommufd_viommu *viommu,
@@ -447,12 +446,15 @@ int arm_vsmmu_init(struct iommufd_viommu *viommu,/* FIXME Move VMID allocation from the S2 domain allocation to here */ vsmmu->vmid = s2_parent->s2_cfg.vmid; -if (smmu->impl_ops && smmu->impl_ops->vsmmu_init && - viommu->type == smmu->impl_ops->vsmmu_type) -return smmu->impl_ops->vsmmu_init (vsmmu, user_data); +if (viommu->type ==IOMMU_VIOMMU_TYPE_ARM_SMMUV3 ) { + viommu->ops = &arm_vsmmu_ops; +return 0 ; + } - viommu->ops = &arm_vsmmu_ops; -return 0 ; +/* Unsupported type was rejected in arm_smmu_get_viommu_size() */ +if (WARN_ON (viommu->type != smmu->impl_ops->vsmmu_type)) +return -EOPNOTSUPP ; +return smmu->impl_ops->vsmmu_init (vsmmu, user_data); }int arm_vmaster_report_event (struct arm_smmu_vmaster *vmaster,u64 *evt)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +14 -0
@@ -4703,6 +4703,7 @@ static void arm_smmu_impl_remove(void *data)static struct arm_smmu_device *arm_smmu_impl_probe (struct arm_smmu_device *smmu) {struct arm_smmu_device *new_smmu =ERR_PTR (-ENODEV ); +const struct arm_smmu_impl_ops *ops;int ret;if (smmu->impl_dev && (smmu->options &ARM_SMMU_OPT_TEGRA241_CMDQV ))
@@ -4713,11 +4714,24 @@ static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu)if (IS_ERR (new_smmu))return new_smmu; + ops = new_smmu->impl_ops; +if (ops) { +/* vsmmu_size and vsmmu_init ops must be paired */ +if (WARN_ON (!ops->vsmmu_size != !ops->vsmmu_init)) { + ret = -EINVAL ; + goto err_remove; + } + } + ret =devm_add_action_or_reset (new_smmu->dev, arm_smmu_impl_remove, new_smmu);if (ret)return ERR_PTR (ret);return new_smmu; + +err_remove: +arm_smmu_impl_remove (new_smmu); +return ERR_PTR (ret); }static int arm_smmu_device_probe (struct platform_device *pdev)
Signature · 1 line
2.43.0
Re: [PATCH v3 1/2] iommu/arm-smmu-v3: Do not bother impl_ops if IOMMU_VIOMMU_TYPE_ARM_SMMUV3
Quoted 11 lines · Nicolin Chen
When viommu type is
IOMMU_VIOMMU_TYPE_ARM_SMMUV3, always return or init the standard structarm_vsmmu, instead of going throughimpl_opsthat must have its own viommu type than the standardIOMMU_VIOMMU_TYPE_ARM_SMMUV3.Given that
arm_vsmmu_init()is called afterarm_smmu_get_viommu_size(), any unsupportedviommu->typemust be a corruption. And it must be a driver bug that itsvsmmu_sizeandvsmmu_initops aren't paired. Warn these two cases.Suggested-by Will Deacon <[email protected]>Acked-by Will Deacon <[email protected]>Signed-off-by Nicolin Chen <[email protected]>
Quoted 91 lines
--- .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 24 ++++++++++--------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 14 +++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +13
-11@@ -420,14 +420,13 @@ size_t arm_smmu_get_viommu_size(struct device *dev, !(smmu->features & ARM_SMMU_FEAT_S2FWB))return 0 ; -if (smmu->impl_ops && smmu->impl_ops->vsmmu_size && - viommu_type == smmu->impl_ops->vsmmu_type) -return smmu->impl_ops->vsmmu_size; +if (viommu_type ==IOMMU_VIOMMU_TYPE_ARM_SMMUV3 ) + return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core); -if (viommu_type !=IOMMU_VIOMMU_TYPE_ARM_SMMUV3 ) +if (!smmu->impl_ops || !smmu->impl_ops->vsmmu_size || + viommu_type != smmu->impl_ops->vsmmu_type)return 0 ; - - return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core); +return smmu->impl_ops->vsmmu_size; } int arm_vsmmu_init(struct iommufd_viommu *viommu,@@ -447,12 +446,15 @@ int arm_vsmmu_init(struct iommufd_viommu *viommu,/* FIXME Move VMID allocation from the S2 domain allocation to here */ vsmmu->vmid = s2_parent->s2_cfg.vmid; -if (smmu->impl_ops && smmu->impl_ops->vsmmu_init && - viommu->type == smmu->impl_ops->vsmmu_type) -return smmu->impl_ops->vsmmu_init (vsmmu, user_data); +if (viommu->type ==IOMMU_VIOMMU_TYPE_ARM_SMMUV3 ) { + viommu->ops = &arm_vsmmu_ops; +return 0 ; + } - viommu->ops = &arm_vsmmu_ops; -return 0 ; +/* Unsupported type was rejected in arm_smmu_get_viommu_size() */ +if (WARN_ON (viommu->type != smmu->impl_ops->vsmmu_type)) +return -EOPNOTSUPP ; +return smmu->impl_ops->vsmmu_init (vsmmu, user_data); }int arm_vmaster_report_event (struct arm_smmu_vmaster *vmaster,u64 *evt)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +14
-0@@ -4703,6 +4703,7 @@ static void arm_smmu_impl_remove(void *data)static struct arm_smmu_device *arm_smmu_impl_probe (struct arm_smmu_device *smmu) {struct arm_smmu_device *new_smmu =ERR_PTR (-ENODEV ); +const struct arm_smmu_impl_ops *ops;int ret;if (smmu->impl_dev && (smmu->options &ARM_SMMU_OPT_TEGRA241_CMDQV ))@@ -4713,11 +4714,24 @@ static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu)if (IS_ERR (new_smmu))return new_smmu; + ops = new_smmu->impl_ops; +if (ops) { +/* vsmmu_size and vsmmu_init ops must be paired */ +if (WARN_ON (!ops->vsmmu_size != !ops->vsmmu_init)) { + ret = -EINVAL ; + goto err_remove; + } + } + ret =devm_add_action_or_reset (new_smmu->dev, arm_smmu_impl_remove, new_smmu);if (ret)return ERR_PTR (ret);return new_smmu; + +err_remove: +arm_smmu_impl_remove (new_smmu); +return ERR_PTR (ret); }static int arm_smmu_device_probe (struct platform_device *pdev)Signature · 1 line
2.43.0
PATCH v3 2/2 iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
In reply to Nicolin ChenIt's more flexible to have a get_viommu_size op. Replace static vsmmu_size and vsmmu_type with that.
--- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 8 ++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 ++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 +-- drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 14 ++++++++++++-- 4 files changed, 17 insertions(+), 12 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +2 -6
@@ -423,10 +423,9 @@ size_t arm_smmu_get_viommu_size(struct device *dev,if (viommu_type ==IOMMU_VIOMMU_TYPE_ARM_SMMUV3 ) return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core); -if (!smmu->impl_ops || !smmu->impl_ops->vsmmu_size || - viommu_type != smmu->impl_ops->vsmmu_type) +if (!smmu->impl_ops || !smmu->impl_ops->get_viommu_size)return 0 ; -return smmu->impl_ops->vsmmu_size; +return smmu->impl_ops->get_viommu_size (viommu_type); } int arm_vsmmu_init(struct iommufd_viommu *viommu,
@@ -451,9 +450,6 @@ int arm_vsmmu_init(struct iommufd_viommu *viommu,return 0 ; } -/* Unsupported type was rejected in arm_smmu_get_viommu_size() */ -if (WARN_ON (viommu->type != smmu->impl_ops->vsmmu_type)) -return -EOPNOTSUPP ;return smmu->impl_ops->vsmmu_init (vsmmu, user_data); }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -2
@@ -4716,8 +4716,8 @@ static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu) ops = new_smmu->impl_ops;if (ops) { -/* vsmmu_size and vsmmu_init ops must be paired */ -if (WARN_ON (!ops->vsmmu_size != !ops->vsmmu_init)) { +/* get_viommu_size and vsmmu_init ops must be paired */ +if (WARN_ON (!ops->get_viommu_size != !ops->vsmmu_init)) { ret = -EINVAL ; goto err_remove; }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -2
@@ -728,8 +728,7 @@ struct arm_smmu_impl_ops {*/ void *(*hw_info)(struct arm_smmu_device *smmu,u32 *length,enum iommu_hw_info_type *type); -const size_t vsmmu_size; -const enum iommu_viommu_type vsmmu_type; +size_t (*get_viommu_size)(enum iommu_viommu_type viommu_type);int (*vsmmu_init)(struct arm_vsmmu *vsmmu,const struct iommu_user_data *user_data); };
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +12 -2
@@ -832,6 +832,13 @@ static void *tegra241_cmdqv_hw_info(struct arm_smmu_device *smmu, u32 *length,return info; } +static size_t tegra241_cmdqv_get_vintf_size (enum iommu_viommu_type viommu_type) +{ +if (viommu_type !=IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV ) +return 0 ; + return VIOMMU_STRUCT_SIZE(struct tegra241_vintf, vsmmu.core); +} +static struct arm_smmu_impl_ops tegra241_cmdqv_impl_ops = {/* For in-kernel use */ .get_secondary_cmdq = tegra241_cmdqv_get_cmdq,
@@ -839,8 +846,7 @@ static struct arm_smmu_impl_ops tegra241_cmdqv_impl_ops = { .device_remove = tegra241_cmdqv_remove, /* For user-space use */ .hw_info = tegra241_cmdqv_hw_info, - .vsmmu_size = VIOMMU_STRUCT_SIZE(struct tegra241_vintf, vsmmu.core), - .vsmmu_type = IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV, + .get_viommu_size = tegra241_cmdqv_get_vintf_size, .vsmmu_init = tegra241_cmdqv_init_vintf_user, };
@@ -1273,6 +1279,10 @@ tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu,phys_addr_t page0_base;int ret; +/* Unsupported type was rejected in tegra241_cmdqv_get_vintf_size() */ +if (WARN_ON (vsmmu->core.type !=IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV )) +return -EOPNOTSUPP ; +if (!user_data)return -EINVAL ;
Signature · 1 line
2.43.0
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 98 lines · Nicolin Chen
It's more flexible to have a
get_viommu_sizeop. Replace staticvsmmu_sizeandvsmmu_typewith that.Suggested-by Will Deacon <[email protected]>Acked-by Will Deacon <[email protected]>Signed-off-by Nicolin Chen <[email protected]>--- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 8 ++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 ++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 +-- drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 14 ++++++++++++-- 4 files changed, 17 insertions(+), 12 deletions(-)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +2
-6@@ -423,10 +423,9 @@ size_t arm_smmu_get_viommu_size(struct device *dev,if (viommu_type ==IOMMU_VIOMMU_TYPE_ARM_SMMUV3 ) return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core); -if (!smmu->impl_ops || !smmu->impl_ops->vsmmu_size || - viommu_type != smmu->impl_ops->vsmmu_type) +if (!smmu->impl_ops || !smmu->impl_ops->get_viommu_size)return 0 ; -return smmu->impl_ops->vsmmu_size; +return smmu->impl_ops->get_viommu_size (viommu_type); } int arm_vsmmu_init(struct iommufd_viommu *viommu,@@ -451,9 +450,6 @@ int arm_vsmmu_init(struct iommufd_viommu *viommu,return 0 ; } -/* Unsupported type was rejected in arm_smmu_get_viommu_size() */ -if (WARN_ON (viommu->type != smmu->impl_ops->vsmmu_type)) -return -EOPNOTSUPP ;return smmu->impl_ops->vsmmu_init (vsmmu, user_data); }drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-2@@ -4716,8 +4716,8 @@ static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu) ops = new_smmu->impl_ops;if (ops) { -/* vsmmu_size and vsmmu_init ops must be paired */ -if (WARN_ON (!ops->vsmmu_size != !ops->vsmmu_init)) { +/* get_viommu_size and vsmmu_init ops must be paired */ +if (WARN_ON (!ops->get_viommu_size != !ops->vsmmu_init)) { ret = -EINVAL ; goto err_remove; }drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-2@@ -728,8 +728,7 @@ struct arm_smmu_impl_ops {*/ void *(*hw_info)(struct arm_smmu_device *smmu,u32 *length,enum iommu_hw_info_type *type); -const size_t vsmmu_size; -const enum iommu_viommu_type vsmmu_type; +size_t (*get_viommu_size)(enum iommu_viommu_type viommu_type);int (*vsmmu_init)(struct arm_vsmmu *vsmmu,const struct iommu_user_data *user_data); };drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +8
-2@@ -832,6 +832,13 @@ static void *tegra241_cmdqv_hw_info(struct arm_smmu_device *smmu, u32 *length,return info; } +static size_t tegra241_cmdqv_get_vintf_size (enum iommu_viommu_type viommu_type) +{ +if (viommu_type !=IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV ) +return 0 ; + return VIOMMU_STRUCT_SIZE(struct tegra241_vintf, vsmmu.core); +} +static struct arm_smmu_impl_ops tegra241_cmdqv_impl_ops = {/* For in-kernel use */ .get_secondary_cmdq = tegra241_cmdqv_get_cmdq,@@ -839,8 +846,7 @@ static struct arm_smmu_impl_ops tegra241_cmdqv_impl_ops = { .device_remove = tegra241_cmdqv_remove, /* For user-space use */ .hw_info = tegra241_cmdqv_hw_info, - .vsmmu_size = VIOMMU_STRUCT_SIZE(struct tegra241_vintf, vsmmu.core), - .vsmmu_type = IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV, + .get_viommu_size = tegra241_cmdqv_get_vintf_size, .vsmmu_init = tegra241_cmdqv_init_vintf_user, };
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu, · line 1285
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +4
-0@@ -1273,6 +1279,10 @@ tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu,phys_addr_t page0_base;int ret; +/* Unsupported type was rejected in tegra241_cmdqv_get_vintf_size() */ +if (WARN_ON (vsmmu->core.type !=IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV )) +return -EOPNOTSUPP ; +
Nit: I don't think we'd expect a call to this if the vintf_size returned 0? I see that in iommufd_viommu_alloc_ioctl, we already have a check:
viommu_size = ops->get_viommu_size(idev->dev, cmd->type);
if (!viommu_size) {
rc = -EOPNOTSUPP;
goto out_put_idev;
}And call ops->viommu_init only when the above isn't met. Thus, if we still end up calling ops->viommu_init, shouldn't we BUG_ON() it? I'd rather have the core code handle such things (since the driver is simply implementing the ops) and BUG_ON() something that's terribly wrong..
I can't see any ops->viommu_init being called elsewhere atm, let me know if there's a different path that I missed..
Apart from the above nit,
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +0
-0if (!user_data)return -EINVAL ;Signature · 1 line
2.43.0
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 5 lines · Pranjal Shrivastava
Quoted 4 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +0
-0@@ -1273,6 +1279,10 @@ tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu,phys_addr_t page0_base;int ret;
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu, · line 1285
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +4
-0+/* Unsupported type was rejected in tegra241_cmdqv_get_vintf_size() */ +if (WARN_ON (vsmmu->core.type !=IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV )) +return -EOPNOTSUPP ; +Nit: I don't think we'd expect a call to this if the
vintf_sizereturned 0? I see that iniommufd_viommu_alloc_ioctl, we already have a check:
It's added in the previous patch where I explained that this is to detect data corruption. When something like that happens, it would be often illogical.
Quoted 5 lines
And call
ops->viommu_initonly when the above isn't met. Thus, if we still end up callingops->viommu_init, shouldn't weBUG_ON()it? I'd rather have the core code handle such things (since the driver is simply implementing the ops) andBUG_ON()something that's terribly wrong..
BUG_ON is discouraged following the coding style:
https://docs.kernel.org/process/coding-style.html#use-warn-rather-than-bug
Quoted 2 lines
I can't see any
ops->viommu_initbeing called elsewhere atm, let me know if there's a different path that I missed..
I see it as a precaution that should never get triggered. But in case that it happens, I don't want it to proceed further wasting precious HW resource given that this function allocates a VINTF.
Nicolin
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 14 lines · Nicolin Chen
Quoted 5 lines · Pranjal Shrivastava
Quoted 4 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +0
-0@@ -1273,6 +1279,10 @@ tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu,phys_addr_t page0_base;int ret;Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu, · line 1285
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +4
-0+/* Unsupported type was rejected in tegra241_cmdqv_get_vintf_size() */ +if (WARN_ON (vsmmu->core.type !=IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV )) +return -EOPNOTSUPP ; +Nit: I don't think we'd expect a call to this if the
vintf_sizereturned 0? I see that iniommufd_viommu_alloc_ioctl, we already have a check:
Quoted 4 lines
It's added in the previous patch where I explained that this is to detect data corruption. When something like that happens, it would be often illogical.
Right.. I got mis-led by the comment, my point is that if an "unsupported type" was rejected in _get_vintf_size, we wouldn't be here calling viommu_init since we error out based on the check in iommufd_viommu_alloc_ioctl.. but yes, if there was some data corruption that changed the viommu type between these calls, I guess it makes sense to check and error out here.
Quoted 9 lines
Quoted 5 lines
And call
ops->viommu_initonly when the above isn't met. Thus, if we still end up callingops->viommu_init, shouldn't weBUG_ON()it? I'd rather have the core code handle such things (since the driver is simply implementing the ops) andBUG_ON()something that's terribly wrong..
BUG_ONis discouraged following the coding style:
https://docs.kernel.org/process/coding-style.html#use-warn-rather-than-bug
Noted. Thanks.
Quoted 7 lines
Quoted 2 lines
I can't see any
ops->viommu_initbeing called elsewhere atm, let me know if there's a different path that I missed..I see it as a precaution that should never get triggered. But in case that it happens, I don't want it to proceed further wasting precious HW resource given that this function allocates a VINTF.
Agreed.
Quoted 1 line
Nicolin
Praan
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 8 lines · Pranjal Shrivastava
Quoted 7 lines · Nicolin Chen
Quoted 6 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu, · line 1282
Quoted 5 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +1
-0@@ -1273,6 +1279,10 @@ tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu,phys_addr_t page0_base;int ret; +/* Unsupported type was rejected in tegra241_cmdqv_get_vintf_size() */
Sorry, if this wasn't clear in the previous comment. I meant this comment must be updated, the "unsupported type" wasn't rejected in vintf_size, rather the type got corrupted which brought us here. Had the vintf_size rejected it, we wouldn't be calling the init op.
Thanks,
Praan
Quoted 44 lines
Quoted 11 lines
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu, · line 1285
Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +3
-0+if (WARN_ON (vsmmu->core.type !=IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV )) +return -EOPNOTSUPP ; +Nit: I don't think we'd expect a call to this if the
vintf_sizereturned 0? I see that iniommufd_viommu_alloc_ioctl, we already have a check:It's added in the previous patch where I explained that this is to detect data corruption. When something like that happens, it would be often illogical.
Right.. I got mis-led by the comment, my point is that if an "unsupported type" was rejected in
_get_vintf_size, we wouldn't be here callingviommu_initsince we error out based on the check iniommufd_viommu_alloc_ioctl.. but yes, if there was some data corruption that changed the viommu type between these calls, I guess it makes sense to check and error out here.Quoted 9 lines
Quoted 5 lines
And call
ops->viommu_initonly when the above isn't met. Thus, if we still end up callingops->viommu_init, shouldn't weBUG_ON()it? I'd rather have the core code handle such things (since the driver is simply implementing the ops) andBUG_ON()something that's terribly wrong..
BUG_ONis discouraged following the coding style:
https://docs.kernel.org/process/coding-style.html#use-warn-rather-than-bugNoted. Thanks.
Quoted 7 lines
Quoted 2 lines
I can't see any
ops->viommu_initbeing called elsewhere atm, let me know if there's a different path that I missed..I see it as a precaution that should never get triggered. But in case that it happens, I don't want it to proceed further wasting precious HW resource given that this function allocates a VINTF.
Agreed.
Quoted 1 line
Nicolin
Praan
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 8 lines · Pranjal Shrivastava
Quoted 7 lines · Pranjal Shrivastava
Quoted 6 lines · Nicolin Chen
Quoted 5 lines · Pranjal Shrivastava
Quoted 4 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +0
-0@@ -1273,6 +1279,10 @@ tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu,phys_addr_t page0_base;int ret;
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu, · line 1282
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +1
-0+/* Unsupported type was rejected in tegra241_cmdqv_get_vintf_size() */ Sorry, if this wasn't clear in the previous comment. I meant this comment must be updated, the "unsupported type" wasn't rejected in
vintf_size, rather the type got corrupted which brought us here.
Any unsupported type would be indeed rejected by the init op callback. There is nothing wrong with that statement.
It indicates that we shouldn't see an unsupported type here, unless some serious kernel bug like data corruption happens, which is implied by the WARN_ON itself.
Quoted 2 lines
Had the
vintf_sizerejected it, we wouldn't be calling the init op.
A data corruption could happen any time, not related to the init op. A concurrent buggy thread can overwrite the vIOMMU object when a write access to its adjacent memory overflows.
Nicolin
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 9 lines · Nicolin Chen
Quoted 8 lines · Pranjal Shrivastava
Quoted 7 lines · Pranjal Shrivastava
Quoted 6 lines · Nicolin Chen
Quoted 5 lines · Pranjal Shrivastava
Quoted 4 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +0
-0@@ -1273,6 +1279,10 @@ tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu,phys_addr_t page0_base;int ret;
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu, · line 1282
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +1
-0+/* Unsupported type was rejected in tegra241_cmdqv_get_vintf_size() */ Sorry, if this wasn't clear in the previous comment. I meant this comment must be updated, the "unsupported type" wasn't rejected in
vintf_size, rather the type got corrupted which brought us here.Any unsupported type would be indeed rejected by the init op callback. There is nothing wrong with that statement.
The comment mentioned tegra241_cmdqv_get_vintf_size() which isn't the init op. The statement:
"unsupported type would be indeed rejected by the init op"
isn't the same as:
"Unsupported type was rejected in tegra241_cmdqv_get_vintf_size()"
Quoted 5 lines
It indicates that we shouldn't see an unsupported type here, unless some serious kernel bug like data corruption happens, which is implied by the
WARN_ONitself.
Quoted 7 lines
Quoted 2 lines
Had the
vintf_sizerejected it, we wouldn't be calling the init op.A data corruption could happen any time, not related to the init op. A concurrent buggy thread can overwrite the vIOMMU object when a write access to its adjacent memory overflows.
I'm agreeing with all of it, it's just that the comment says something was rejected in by the size op, which raises confusion as to why we're in the init op. The init op rejecting something due to data corruption is a different thing..
I totally get the point about data corruption, i.e.:
size op -> returned something valid
<data corruption>
init op -> rejecting corrupted type
Wheras I was just trying to understand a case where as per the comment:
"Unsupported type was rejected in tegra241_cmdqv_get_vintf_size()", i.e. ->size op returned 0, yet we ended up calling the init op
I guess I should've been more clear. Sorry for all the confusion.
Quoted 1 line
Nicolin
Praan
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 8 lines · Pranjal Shrivastava
On Thu, Jul 24, 2025 at 02:49:28PM -0700, Nicolin Chen wrote:
I'm agreeing with all of it, it's just that the comment says something was rejected in by the size op, which raises confusion as to why we're in the init op. The init op rejecting something due to data corruption is a different thing..I totally get the point about data corruption, i.e.:
Quoted 7 lines
size op -> returned something valid
<data corruption>
init op -> rejecting corrupted typeWheras I was just trying to understand a case where as per the comment:
"Unsupported type was rejected integra241_cmdqv_get_vintf_size()", i.e. ->size op returned 0, yet we ended up calling the init op
Is the updated one in v4 fine to you?
/*
* Unsupported type should be rejected by tegra241_cmdqv_get_vintf_size.
* Seeing one here indicates a kernel bug or some data corruption. */
Nicolin
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 17 lines · Nicolin Chen
Quoted 8 lines · Pranjal Shrivastava
On Thu, Jul 24, 2025 at 02:49:28PM -0700, Nicolin Chen wrote:
I'm agreeing with all of it, it's just that the comment says something was rejected in by the size op, which raises confusion as to why we're in the init op. The init op rejecting something due to data corruption is a different thing..I totally get the point about data corruption, i.e.:
Quoted 7 lines
size op -> returned something valid
<data corruption>
init op -> rejecting corrupted typeWheras I was just trying to understand a case where as per the comment:
"Unsupported type was rejected integra241_cmdqv_get_vintf_size()", i.e. ->size op returned 0, yet we ended up calling the init op
Quoted 6 lines
Is the updated one in v4 fine to you?
/*
* Unsupported type should be rejected bytegra241_cmdqv_get_vintf_size.
* Seeing one here indicates a kernel bug or some data corruption. */
Yes, v4 looks fine.. but I was just trying to understand if the comment was wrong, didn't necessarily need a re-spin just for that comment :) Thanks for accommodating it though.
Quoted 2 lines
Nicolin
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
In reply to Nicolin ChenHi Nicolin,
Quoted 22 lines · Nicolin Chen
Quoted 8 lines · Pranjal Shrivastava
Quoted 7 lines · Pranjal Shrivastava
Quoted 6 lines · Nicolin Chen
Quoted 5 lines · Pranjal Shrivastava
Quoted 4 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +0
-0@@ -1273,6 +1279,10 @@ tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu,phys_addr_t page0_base;int ret;Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_init_vintf_user(struct arm_vsmmu *vsmmu, · line 1282
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +1
-0+/* Unsupported type was rejected in tegra241_cmdqv_get_vintf_size() */ Sorry, if this wasn't clear in the previous comment. I meant this comment must be updated, the "unsupported type" wasn't rejected in
vintf_size, rather the type got corrupted which brought us here.Any unsupported type would be indeed rejected by the init op callback. There is nothing wrong with that statement.
It indicates that we shouldn't see an unsupported type here, unless some serious kernel bug like data corruption happens, which is implied by the
WARN_ONitself.
Quoted 6 lines
Quoted 2 lines
Had the
vintf_sizerejected it, we wouldn't be calling the init op.A data corruption could happen any time, not related to the init op. A concurrent buggy thread can overwrite the vIOMMU object when a write access to its adjacent memory overflows.
Can you please elaborate on that, as memory corruption can happen any time event after the next check and there is no way to defend against that?
Thanks,
Mostafa
Quoted 2 lines
Nicolin
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 5 lines · Mostafa Saleh
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines · Pranjal Shrivastava
Quoted 1 line · Nicolin Chen
Had the
vintf_sizerejected it, we wouldn't be calling the init op.
Quoted 7 lines
Quoted 3 lines
A data corruption could happen any time, not related to the init op. A concurrent buggy thread can overwrite the vIOMMU object when a write access to its adjacent memory overflows.
Can you please elaborate on that, as memory corruption can happen any time event after the next check and there is no way to defend against that?
That narrative is under a condition (in the context) "when there is a kernel bug corrupting data" :)
E.g. some new lines of code allocates a wrong size of memory and writes above the size. If that memory is near this vIOMMU object it might overwrite to this vIOMMU object that this function gets.
This certainly won't happen if everything is sane.
Nicolin
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 14 lines · Nicolin Chen
Quoted 5 lines · Mostafa Saleh
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines · Pranjal Shrivastava
Quoted 1 line · Nicolin Chen
Had the
vintf_sizerejected it, we wouldn't be calling the init op.Quoted 7 lines
Quoted 3 lines
A data corruption could happen any time, not related to the init op. A concurrent buggy thread can overwrite the vIOMMU object when a write access to its adjacent memory overflows.
Can you please elaborate on that, as memory corruption can happen any time event after the next check and there is no way to defend against that?
Quoted 8 lines
That narrative is under a condition (in the context) "when there is a kernel bug corrupting data" :)
E.g. some new lines of code allocates a wrong size of memory and writes above the size. If that memory is near this vIOMMU object it might overwrite to this vIOMMU object that this function gets.
This certainly won't happen if everything is sane.
I see, but I don't think we should do anything about that, there are 100s of structs in the kernel, we can't add checks everywhere, and I don't see anything special about this path to add an assertion, this kind of defensive programming isn't really helpful. We just need to review any new code properly :)
Thanks,
Mostafa
Quoted 2 lines
Nicolin
Re: [PATCH v3 2/2] iommu/arm-smmu-v3: Replace vsmmu_size/type with get_viommu_size
Quoted 22 lines · Mostafa Saleh
Quoted 21 lines · Nicolin Chen
Quoted 5 lines · Mostafa Saleh
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines · Pranjal Shrivastava
Quoted 1 line · Nicolin Chen
Had the
vintf_sizerejected it, we wouldn't be calling the init op.Quoted 7 lines
Quoted 3 lines
A data corruption could happen any time, not related to the init op. A concurrent buggy thread can overwrite the vIOMMU object when a write access to its adjacent memory overflows.
Can you please elaborate on that, as memory corruption can happen any time event after the next check and there is no way to defend against that?
That narrative is under a condition (in the context) "when there is a kernel bug corrupting data" :)
E.g. some new lines of code allocates a wrong size of memory and writes above the size. If that memory is near this vIOMMU object it might overwrite to this vIOMMU object that this function gets.
Quoted 7 lines
Quoted 1 line
This certainly won't happen if everything is sane.
I see, but I don't think we should do anything about that, there are 100s of structs in the kernel, we can't add checks everywhere, and I don't see anything special about this path to add an assertion, this kind of defensive programming isn't really helpful. We just need to review any new code properly :)
It could help for debugging purpose when writing new lines of code. Kernel has quite a lot of WARN_ONs fencing something that shouldn't happen.
With that being said, I admit that this particular line is a bit of overreacting. Removing it doesn't have too big impact, as something else would likely crash when such a corruption does happen.
Nicolin