Skip to content

Commit e3a56b3

Browse files
nicolincwilldeacon
authored andcommitted
iommu/arm-smmu-v3: Pre-allocate a per-master invalidation array
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. Sort fwspec->ids in an ascending order to fit to the arm_smmu_invs_merge() function. Co-developed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Pranjal Shrivastava <praan@google.com> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Will Deacon <will@kernel.org>
1 parent 15a2a56 commit e3a56b3

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3789,26 +3789,57 @@ static int arm_smmu_init_sid_strtab(struct arm_smmu_device *smmu, u32 sid)
37893789
return 0;
37903790
}
37913791

3792+
static int arm_smmu_stream_id_cmp(const void *_l, const void *_r)
3793+
{
3794+
const typeof_member(struct arm_smmu_stream, id) *l = _l;
3795+
const typeof_member(struct arm_smmu_stream, id) *r = _r;
3796+
3797+
return cmp_int(*l, *r);
3798+
}
3799+
37923800
static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
37933801
struct arm_smmu_master *master)
37943802
{
37953803
int i;
37963804
int ret = 0;
37973805
struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
3806+
bool ats_supported = dev_is_pci(master->dev) &&
3807+
pci_ats_supported(to_pci_dev(master->dev));
37983808

37993809
master->streams = kzalloc_objs(*master->streams, fwspec->num_ids);
38003810
if (!master->streams)
38013811
return -ENOMEM;
38023812
master->num_streams = fwspec->num_ids;
38033813

3804-
mutex_lock(&smmu->streams_mutex);
3814+
if (!ats_supported) {
3815+
/* Base case has 1 ASID entry or maximum 2 VMID entries */
3816+
master->build_invs = arm_smmu_invs_alloc(2);
3817+
} else {
3818+
/* ATS case adds num_ids of entries, on top of the base case */
3819+
master->build_invs = arm_smmu_invs_alloc(2 + fwspec->num_ids);
3820+
}
3821+
if (!master->build_invs) {
3822+
kfree(master->streams);
3823+
return -ENOMEM;
3824+
}
3825+
38053826
for (i = 0; i < fwspec->num_ids; i++) {
38063827
struct arm_smmu_stream *new_stream = &master->streams[i];
3807-
struct rb_node *existing;
3808-
u32 sid = fwspec->ids[i];
38093828

3810-
new_stream->id = sid;
3829+
new_stream->id = fwspec->ids[i];
38113830
new_stream->master = master;
3831+
}
3832+
3833+
/* Put the ids into order for sorted to_merge/to_unref arrays */
3834+
sort_nonatomic(master->streams, master->num_streams,
3835+
sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
3836+
NULL);
3837+
3838+
mutex_lock(&smmu->streams_mutex);
3839+
for (i = 0; i < fwspec->num_ids; i++) {
3840+
struct arm_smmu_stream *new_stream = &master->streams[i];
3841+
struct rb_node *existing;
3842+
u32 sid = new_stream->id;
38123843

38133844
ret = arm_smmu_init_sid_strtab(smmu, sid);
38143845
if (ret)
@@ -3838,6 +3869,7 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
38383869
for (i--; i >= 0; i--)
38393870
rb_erase(&master->streams[i].node, &smmu->streams);
38403871
kfree(master->streams);
3872+
kfree(master->build_invs);
38413873
}
38423874
mutex_unlock(&smmu->streams_mutex);
38433875

@@ -3859,6 +3891,7 @@ static void arm_smmu_remove_master(struct arm_smmu_master *master)
38593891
mutex_unlock(&smmu->streams_mutex);
38603892

38613893
kfree(master->streams);
3894+
kfree(master->build_invs);
38623895
}
38633896

38643897
static struct iommu_device *arm_smmu_probe_device(struct device *dev)

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,14 @@ struct arm_smmu_master {
928928
struct arm_smmu_device *smmu;
929929
struct device *dev;
930930
struct arm_smmu_stream *streams;
931+
/*
932+
* Scratch memory for a to_merge or to_unref array to build a per-domain
933+
* invalidation array. It'll be pre-allocated with enough enries for all
934+
* possible build scenarios. It can be used by only one caller at a time
935+
* until the arm_smmu_invs_merge/unref() finishes. Must be locked by the
936+
* iommu_group mutex.
937+
*/
938+
struct arm_smmu_invs *build_invs;
931939
struct arm_smmu_vmaster *vmaster; /* use smmu->streams_mutex */
932940
/* Locked by the iommu core using the group mutex */
933941
struct arm_smmu_ctx_desc_cfg cd_table;

0 commit comments

Comments
 (0)