[PATCH rfcv2 5/8] iommu/arm-smmu-v3: Pre-allocate a per-master invalidation array

Jason Gunthorpe jgg at nvidia.com
Wed Sep 24 14:32:30 PDT 2025


On Mon, Sep 08, 2025 at 04:26:59PM -0700, Nicolin Chen wrote:
> When a master is attached from an old domain to a new domain, it needs to
> build an invalidation array to delete and add the array entries from/onto
> the invalidation arrays of those two domains, passed via the to_merge and
> to_unref arguments into arm_smmu_invs_merge/unref() respectively.
> 
> Since the master->num_streams might differ across masters, a memory would
> have to be allocated when building an to_merge/to_unref array which might
> fail with -ENOMEM.
> 
> On the other hand, an attachment to arm_smmu_blocked_domain must not fail
> so it's the best to avoid any memory allocation in that path.
> 
> Pre-allocate a fixed size invalidation array for every master. This array
> will be used as a scratch to fill dynamically when building a to_merge or
> to_unref invs array.

Mention the sorting here too, though maybe that hunk should be moved
to the next patch.

> +	/* Base case has 1 ASID or 1~2 VMIDs. ATS case adds num_ids */
> +	if (!ats_supported)
> +		master->build_invs = arm_smmu_invs_alloc(2);
> +	else
> +		master->build_invs = arm_smmu_invs_alloc(2 + fwspec->num_ids);
> +	if (IS_ERR(master->build_invs)) {
> +		kfree(master->streams);
> +		return PTR_ERR(master->build_invs);
> +	}
> +
> +	/* Put the ids into order for a sorted to_merge or to_unref array */
> +	sort_nonatomic(fwspec->ids, fwspec->num_ids, sizeof(fwspec->ids[0]),
> +		       arm_smmu_ids_cmp, NULL);

The sort could be moved under the above !ats_supported, a little more
insurance in case something is inspecting the ids.

I searched around and only found tegra_dev_iommu_get_stream_id() which
is fine with the sort due to num_ids ==1

Otherwise looks OK

Reviewed-by: Jason Gunthorpe <jgg at nvidia.com>

Jason



More information about the linux-arm-kernel mailing list