diff mbox series

[v13,07/10] iommu/arm-smmu-v3: Maintain a SID->device structure

Message ID 20210302092644.2553014-8-jean-philippe@linaro.org
State Superseded
Headers show
Series iommu: I/O page faults for SMMUv3 | expand

Commit Message

Jean-Philippe Brucker March 2, 2021, 9:26 a.m. UTC
When handling faults from the event or PRI queue, we need to find the
struct device associated with a SID. Add a rb_tree to keep track of
SIDs.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h |  13 +-
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 157 ++++++++++++++++----
 2 files changed, 140 insertions(+), 30 deletions(-)

Comments

Keqian Zhu March 2, 2021, 12:24 p.m. UTC | #1
Hi Jean,

Reviewed-by: Keqian Zhu <zhukeqian1@huawei.com>


On 2021/3/2 17:26, Jean-Philippe Brucker wrote:
> When handling faults from the event or PRI queue, we need to find the

> struct device associated with a SID. Add a rb_tree to keep track of

> SIDs.

> 

> Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> Reviewed-by: Eric Auger <eric.auger@redhat.com>

> Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>

> ---

>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h |  13 +-

>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 157 ++++++++++++++++----

>  2 files changed, 140 insertions(+), 30 deletions(-)

> 

> diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h

> index f985817c967a..7b15b7580c6e 100644

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

> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h

> @@ -639,6 +639,15 @@ struct arm_smmu_device {

>  

>  	/* IOMMU core code handle */

>  	struct iommu_device		iommu;

> +

> +	struct rb_root			streams;

> +	struct mutex			streams_mutex;

> +};

> +

> +struct arm_smmu_stream {

> +	u32				id;

> +	struct arm_smmu_master		*master;

> +	struct rb_node			node;

>  };

>  

>  /* SMMU private data for each master */

> @@ -647,8 +656,8 @@ struct arm_smmu_master {

>  	struct device			*dev;

>  	struct arm_smmu_domain		*domain;

>  	struct list_head		domain_head;

> -	u32				*sids;

> -	unsigned int			num_sids;

> +	struct arm_smmu_stream		*streams;

> +	unsigned int			num_streams;

>  	bool				ats_enabled;

>  	bool				sva_enabled;

>  	struct list_head		bonds;

> diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

> index 7edce914c45e..d148bb6d4289 100644

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

> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

> @@ -909,8 +909,8 @@ static void arm_smmu_sync_cd(struct arm_smmu_domain *smmu_domain,

>  

>  	spin_lock_irqsave(&smmu_domain->devices_lock, flags);

>  	list_for_each_entry(master, &smmu_domain->devices, domain_head) {

> -		for (i = 0; i < master->num_sids; i++) {

> -			cmd.cfgi.sid = master->sids[i];

> +		for (i = 0; i < master->num_streams; i++) {

> +			cmd.cfgi.sid = master->streams[i].id;

>  			arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd);

>  		}

>  	}

> @@ -1355,6 +1355,28 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)

>  	return 0;

>  }

>  

> +/* smmu->streams_mutex must be held */

> +__maybe_unused

> +static struct arm_smmu_master *

> +arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)

> +{

> +	struct rb_node *node;

> +	struct arm_smmu_stream *stream;

> +

> +	node = smmu->streams.rb_node;

> +	while (node) {

> +		stream = rb_entry(node, struct arm_smmu_stream, node);

> +		if (stream->id < sid)

> +			node = node->rb_right;

> +		else if (stream->id > sid)

> +			node = node->rb_left;

> +		else

> +			return stream->master;

> +	}

> +

> +	return NULL;

> +}

> +

>  /* IRQ and event handlers */

>  static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)

>  {

> @@ -1588,8 +1610,8 @@ static int arm_smmu_atc_inv_master(struct arm_smmu_master *master)

>  

>  	arm_smmu_atc_inv_to_cmd(0, 0, 0, &cmd);

>  

> -	for (i = 0; i < master->num_sids; i++) {

> -		cmd.atc.sid = master->sids[i];

> +	for (i = 0; i < master->num_streams; i++) {

> +		cmd.atc.sid = master->streams[i].id;

>  		arm_smmu_cmdq_issue_cmd(master->smmu, &cmd);

>  	}

>  

> @@ -1632,8 +1654,8 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid,

>  		if (!master->ats_enabled)

>  			continue;

>  

> -		for (i = 0; i < master->num_sids; i++) {

> -			cmd.atc.sid = master->sids[i];

> +		for (i = 0; i < master->num_streams; i++) {

> +			cmd.atc.sid = master->streams[i].id;

>  			arm_smmu_cmdq_batch_add(smmu_domain->smmu, &cmds, &cmd);

>  		}

>  	}

> @@ -2065,13 +2087,13 @@ static void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master)

>  	int i, j;

>  	struct arm_smmu_device *smmu = master->smmu;

>  

> -	for (i = 0; i < master->num_sids; ++i) {

> -		u32 sid = master->sids[i];

> +	for (i = 0; i < master->num_streams; ++i) {

> +		u32 sid = master->streams[i].id;

>  		__le64 *step = arm_smmu_get_step_for_sid(smmu, sid);

>  

>  		/* Bridged PCI devices may end up with duplicated IDs */

>  		for (j = 0; j < i; j++)

> -			if (master->sids[j] == sid)

> +			if (master->streams[j].id == sid)

>  				break;

>  		if (j < i)

>  			continue;

> @@ -2345,11 +2367,101 @@ static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)

>  	return sid < limit;

>  }

>  

> +static int arm_smmu_insert_master(struct arm_smmu_device *smmu,

> +				  struct arm_smmu_master *master)

> +{

> +	int i;

> +	int ret = 0;

> +	struct arm_smmu_stream *new_stream, *cur_stream;

> +	struct rb_node **new_node, *parent_node = NULL;

> +	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);

> +

> +	master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams),

> +				  GFP_KERNEL);

> +	if (!master->streams)

> +		return -ENOMEM;

> +	master->num_streams = fwspec->num_ids;

> +

> +	mutex_lock(&smmu->streams_mutex);

> +	for (i = 0; i < fwspec->num_ids; i++) {

> +		u32 sid = fwspec->ids[i];

> +

> +		new_stream = &master->streams[i];

> +		new_stream->id = sid;

> +		new_stream->master = master;

> +

> +		/*

> +		 * Check the SIDs are in range of the SMMU and our stream table

> +		 */

> +		if (!arm_smmu_sid_in_range(smmu, sid)) {

> +			ret = -ERANGE;

> +			break;

> +		}

> +

> +		/* Ensure l2 strtab is initialised */

> +		if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {

> +			ret = arm_smmu_init_l2_strtab(smmu, sid);

> +			if (ret)

> +				break;

> +		}

> +

> +		/* Insert into SID tree */

> +		new_node = &(smmu->streams.rb_node);

> +		while (*new_node) {

> +			cur_stream = rb_entry(*new_node, struct arm_smmu_stream,

> +					      node);

> +			parent_node = *new_node;

> +			if (cur_stream->id > new_stream->id) {

> +				new_node = &((*new_node)->rb_left);

> +			} else if (cur_stream->id < new_stream->id) {

> +				new_node = &((*new_node)->rb_right);

> +			} else {

> +				dev_warn(master->dev,

> +					 "stream %u already in tree\n",

> +					 cur_stream->id);

> +				ret = -EINVAL;

> +				break;

> +			}

> +		}

> +		if (ret)

> +			break;

> +

> +		rb_link_node(&new_stream->node, parent_node, new_node);

> +		rb_insert_color(&new_stream->node, &smmu->streams);

> +	}

> +

> +	if (ret) {

> +		for (i--; i >= 0; i--)

> +			rb_erase(&master->streams[i].node, &smmu->streams);

> +		kfree(master->streams);

> +	}

> +	mutex_unlock(&smmu->streams_mutex);

> +

> +	return ret;

> +}

> +

> +static void arm_smmu_remove_master(struct arm_smmu_master *master)

> +{

> +	int i;

> +	struct arm_smmu_device *smmu = master->smmu;

> +	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);

> +

> +	if (!smmu || !master->streams)

> +		return;

> +

> +	mutex_lock(&smmu->streams_mutex);

> +	for (i = 0; i < fwspec->num_ids; i++)

> +		rb_erase(&master->streams[i].node, &smmu->streams);

> +	mutex_unlock(&smmu->streams_mutex);

> +

> +	kfree(master->streams);

> +}

> +

>  static struct iommu_ops arm_smmu_ops;

>  

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

>  {

> -	int i, ret;

> +	int ret;

>  	struct arm_smmu_device *smmu;

>  	struct arm_smmu_master *master;

>  	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);

> @@ -2370,27 +2482,12 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev)

>  

>  	master->dev = dev;

>  	master->smmu = smmu;

> -	master->sids = fwspec->ids;

> -	master->num_sids = fwspec->num_ids;

>  	INIT_LIST_HEAD(&master->bonds);

>  	dev_iommu_priv_set(dev, master);

>  

> -	/* Check the SIDs are in range of the SMMU and our stream table */

> -	for (i = 0; i < master->num_sids; i++) {

> -		u32 sid = master->sids[i];

> -

> -		if (!arm_smmu_sid_in_range(smmu, sid)) {

> -			ret = -ERANGE;

> -			goto err_free_master;

> -		}

> -

> -		/* Ensure l2 strtab is initialised */

> -		if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {

> -			ret = arm_smmu_init_l2_strtab(smmu, sid);

> -			if (ret)

> -				goto err_free_master;

> -		}

> -	}

> +	ret = arm_smmu_insert_master(smmu, master);

> +	if (ret)

> +		goto err_free_master;

>  

>  	device_property_read_u32(dev, "pasid-num-bits", &master->ssid_bits);

>  	master->ssid_bits = min(smmu->ssid_bits, master->ssid_bits);

> @@ -2429,6 +2526,7 @@ static void arm_smmu_release_device(struct device *dev)

>  	WARN_ON(arm_smmu_master_sva_enabled(master));

>  	arm_smmu_detach_dev(master);

>  	arm_smmu_disable_pasid(master);

> +	arm_smmu_remove_master(master);

>  	kfree(master);

>  	iommu_fwspec_free(dev);

>  }

> @@ -2852,6 +2950,9 @@ static int arm_smmu_init_structures(struct arm_smmu_device *smmu)

>  {

>  	int ret;

>  

> +	mutex_init(&smmu->streams_mutex);

> +	smmu->streams = RB_ROOT;

> +

>  	ret = arm_smmu_init_queues(smmu);

>  	if (ret)

>  		return ret;

>
Will Deacon March 25, 2021, 5:48 p.m. UTC | #2
On Tue, Mar 02, 2021 at 10:26:43AM +0100, Jean-Philippe Brucker wrote:
> When handling faults from the event or PRI queue, we need to find the
> struct device associated with a SID. Add a rb_tree to keep track of
> SIDs.
> 
> Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Eric Auger <eric.auger@redhat.com>
> Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
> ---
>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h |  13 +-
>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 157 ++++++++++++++++----
>  2 files changed, 140 insertions(+), 30 deletions(-)
> 
> diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
> index f985817c967a..7b15b7580c6e 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
> @@ -639,6 +639,15 @@ struct arm_smmu_device {
>  
>  	/* IOMMU core code handle */
>  	struct iommu_device		iommu;
> +
> +	struct rb_root			streams;
> +	struct mutex			streams_mutex;
> +};
> +
> +struct arm_smmu_stream {
> +	u32				id;
> +	struct arm_smmu_master		*master;
> +	struct rb_node			node;
>  };
>  
>  /* SMMU private data for each master */
> @@ -647,8 +656,8 @@ struct arm_smmu_master {
>  	struct device			*dev;
>  	struct arm_smmu_domain		*domain;
>  	struct list_head		domain_head;
> -	u32				*sids;
> -	unsigned int			num_sids;
> +	struct arm_smmu_stream		*streams;
> +	unsigned int			num_streams;
>  	bool				ats_enabled;
>  	bool				sva_enabled;
>  	struct list_head		bonds;
> diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> index 7edce914c45e..d148bb6d4289 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -909,8 +909,8 @@ static void arm_smmu_sync_cd(struct arm_smmu_domain *smmu_domain,
>  
>  	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
>  	list_for_each_entry(master, &smmu_domain->devices, domain_head) {
> -		for (i = 0; i < master->num_sids; i++) {
> -			cmd.cfgi.sid = master->sids[i];
> +		for (i = 0; i < master->num_streams; i++) {
> +			cmd.cfgi.sid = master->streams[i].id;
>  			arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd);
>  		}
>  	}
> @@ -1355,6 +1355,28 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
>  	return 0;
>  }
>  
> +/* smmu->streams_mutex must be held */

Can you add a lockdep assertion for that?

> +__maybe_unused
> +static struct arm_smmu_master *
> +arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)
> +{
> +	struct rb_node *node;
> +	struct arm_smmu_stream *stream;
> +
> +	node = smmu->streams.rb_node;
> +	while (node) {
> +		stream = rb_entry(node, struct arm_smmu_stream, node);
> +		if (stream->id < sid)
> +			node = node->rb_right;
> +		else if (stream->id > sid)
> +			node = node->rb_left;
> +		else
> +			return stream->master;
> +	}
> +
> +	return NULL;
> +}

[...]

> +static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
> +				  struct arm_smmu_master *master)
> +{
> +	int i;
> +	int ret = 0;
> +	struct arm_smmu_stream *new_stream, *cur_stream;
> +	struct rb_node **new_node, *parent_node = NULL;
> +	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
> +
> +	master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams),
> +				  GFP_KERNEL);
> +	if (!master->streams)
> +		return -ENOMEM;
> +	master->num_streams = fwspec->num_ids;
> +
> +	mutex_lock(&smmu->streams_mutex);
> +	for (i = 0; i < fwspec->num_ids; i++) {
> +		u32 sid = fwspec->ids[i];
> +
> +		new_stream = &master->streams[i];
> +		new_stream->id = sid;
> +		new_stream->master = master;
> +
> +		/*
> +		 * Check the SIDs are in range of the SMMU and our stream table
> +		 */
> +		if (!arm_smmu_sid_in_range(smmu, sid)) {
> +			ret = -ERANGE;
> +			break;
> +		}
> +
> +		/* Ensure l2 strtab is initialised */
> +		if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
> +			ret = arm_smmu_init_l2_strtab(smmu, sid);
> +			if (ret)
> +				break;
> +		}
> +
> +		/* Insert into SID tree */
> +		new_node = &(smmu->streams.rb_node);
> +		while (*new_node) {
> +			cur_stream = rb_entry(*new_node, struct arm_smmu_stream,
> +					      node);
> +			parent_node = *new_node;
> +			if (cur_stream->id > new_stream->id) {
> +				new_node = &((*new_node)->rb_left);
> +			} else if (cur_stream->id < new_stream->id) {
> +				new_node = &((*new_node)->rb_right);
> +			} else {
> +				dev_warn(master->dev,
> +					 "stream %u already in tree\n",
> +					 cur_stream->id);
> +				ret = -EINVAL;
> +				break;
> +			}
> +		}
> +		if (ret)
> +			break;
> +
> +		rb_link_node(&new_stream->node, parent_node, new_node);
> +		rb_insert_color(&new_stream->node, &smmu->streams);
> +	}
> +
> +	if (ret) {
> +		for (i--; i >= 0; i--)

Is 'i--' really what you want for the initial value? Doesn't that correspond
to the ID you *didn't* add to the tree?

> +			rb_erase(&master->streams[i].node, &smmu->streams);
> +		kfree(master->streams);

Do you need to NULLify master->streams and/or reset master->num_streams
after this? Seems like they're left dangling.

Will
Jean-Philippe Brucker March 26, 2021, 9:49 a.m. UTC | #3
On Thu, Mar 25, 2021 at 05:48:07PM +0000, Will Deacon wrote:
> > +/* smmu->streams_mutex must be held */

> 

> Can you add a lockdep assertion for that?


Sure

> > +__maybe_unused

> > +static struct arm_smmu_master *

> > +arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)

> > +{

> > +	struct rb_node *node;

> > +	struct arm_smmu_stream *stream;

> > +

> > +	node = smmu->streams.rb_node;

> > +	while (node) {

> > +		stream = rb_entry(node, struct arm_smmu_stream, node);

> > +		if (stream->id < sid)

> > +			node = node->rb_right;

> > +		else if (stream->id > sid)

> > +			node = node->rb_left;

> > +		else

> > +			return stream->master;

> > +	}

> > +

> > +	return NULL;

> > +}

> 

> [...]

> 

> > +static int arm_smmu_insert_master(struct arm_smmu_device *smmu,

> > +				  struct arm_smmu_master *master)

> > +{

> > +	int i;

> > +	int ret = 0;

> > +	struct arm_smmu_stream *new_stream, *cur_stream;

> > +	struct rb_node **new_node, *parent_node = NULL;

> > +	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);

> > +

> > +	master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams),

> > +				  GFP_KERNEL);

> > +	if (!master->streams)

> > +		return -ENOMEM;

> > +	master->num_streams = fwspec->num_ids;

> > +

> > +	mutex_lock(&smmu->streams_mutex);

> > +	for (i = 0; i < fwspec->num_ids; i++) {

> > +		u32 sid = fwspec->ids[i];

> > +

> > +		new_stream = &master->streams[i];

> > +		new_stream->id = sid;

> > +		new_stream->master = master;

> > +

> > +		/*

> > +		 * Check the SIDs are in range of the SMMU and our stream table

> > +		 */

> > +		if (!arm_smmu_sid_in_range(smmu, sid)) {

> > +			ret = -ERANGE;

> > +			break;

> > +		}

> > +

> > +		/* Ensure l2 strtab is initialised */

> > +		if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {

> > +			ret = arm_smmu_init_l2_strtab(smmu, sid);

> > +			if (ret)

> > +				break;

> > +		}

> > +

> > +		/* Insert into SID tree */

> > +		new_node = &(smmu->streams.rb_node);

> > +		while (*new_node) {

> > +			cur_stream = rb_entry(*new_node, struct arm_smmu_stream,

> > +					      node);

> > +			parent_node = *new_node;

> > +			if (cur_stream->id > new_stream->id) {

> > +				new_node = &((*new_node)->rb_left);

> > +			} else if (cur_stream->id < new_stream->id) {

> > +				new_node = &((*new_node)->rb_right);

> > +			} else {

> > +				dev_warn(master->dev,

> > +					 "stream %u already in tree\n",

> > +					 cur_stream->id);

> > +				ret = -EINVAL;

> > +				break;

> > +			}

> > +		}

> > +		if (ret)

> > +			break;

> > +

> > +		rb_link_node(&new_stream->node, parent_node, new_node);

> > +		rb_insert_color(&new_stream->node, &smmu->streams);

> > +	}

> > +

> > +	if (ret) {

> > +		for (i--; i >= 0; i--)

> 

> Is 'i--' really what you want for the initial value? Doesn't that correspond

> to the ID you *didn't* add to the tree?


In case of error we break out of the loop, with i corresponding to the
stream that caused a fault but wasn't yet added to the tree. So i-- is
the last stream that was successfully added, or -1 in which case we don't
enter this for loop.

> > +			rb_erase(&master->streams[i].node, &smmu->streams);

> > +		kfree(master->streams);

> 

> Do you need to NULLify master->streams and/or reset master->num_streams

> after this? Seems like they're left dangling.


master is freed by arm_smmu_probe_device() when we return an error. Since
this function is unlikely to ever have another caller I didn't bother
cleaning up here

Thanks,
Jean
diff mbox series

Patch

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
index f985817c967a..7b15b7580c6e 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
@@ -639,6 +639,15 @@  struct arm_smmu_device {
 
 	/* IOMMU core code handle */
 	struct iommu_device		iommu;
+
+	struct rb_root			streams;
+	struct mutex			streams_mutex;
+};
+
+struct arm_smmu_stream {
+	u32				id;
+	struct arm_smmu_master		*master;
+	struct rb_node			node;
 };
 
 /* SMMU private data for each master */
@@ -647,8 +656,8 @@  struct arm_smmu_master {
 	struct device			*dev;
 	struct arm_smmu_domain		*domain;
 	struct list_head		domain_head;
-	u32				*sids;
-	unsigned int			num_sids;
+	struct arm_smmu_stream		*streams;
+	unsigned int			num_streams;
 	bool				ats_enabled;
 	bool				sva_enabled;
 	struct list_head		bonds;
diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index 7edce914c45e..d148bb6d4289 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -909,8 +909,8 @@  static void arm_smmu_sync_cd(struct arm_smmu_domain *smmu_domain,
 
 	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
 	list_for_each_entry(master, &smmu_domain->devices, domain_head) {
-		for (i = 0; i < master->num_sids; i++) {
-			cmd.cfgi.sid = master->sids[i];
+		for (i = 0; i < master->num_streams; i++) {
+			cmd.cfgi.sid = master->streams[i].id;
 			arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd);
 		}
 	}
@@ -1355,6 +1355,28 @@  static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
 	return 0;
 }
 
+/* smmu->streams_mutex must be held */
+__maybe_unused
+static struct arm_smmu_master *
+arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)
+{
+	struct rb_node *node;
+	struct arm_smmu_stream *stream;
+
+	node = smmu->streams.rb_node;
+	while (node) {
+		stream = rb_entry(node, struct arm_smmu_stream, node);
+		if (stream->id < sid)
+			node = node->rb_right;
+		else if (stream->id > sid)
+			node = node->rb_left;
+		else
+			return stream->master;
+	}
+
+	return NULL;
+}
+
 /* IRQ and event handlers */
 static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)
 {
@@ -1588,8 +1610,8 @@  static int arm_smmu_atc_inv_master(struct arm_smmu_master *master)
 
 	arm_smmu_atc_inv_to_cmd(0, 0, 0, &cmd);
 
-	for (i = 0; i < master->num_sids; i++) {
-		cmd.atc.sid = master->sids[i];
+	for (i = 0; i < master->num_streams; i++) {
+		cmd.atc.sid = master->streams[i].id;
 		arm_smmu_cmdq_issue_cmd(master->smmu, &cmd);
 	}
 
@@ -1632,8 +1654,8 @@  int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid,
 		if (!master->ats_enabled)
 			continue;
 
-		for (i = 0; i < master->num_sids; i++) {
-			cmd.atc.sid = master->sids[i];
+		for (i = 0; i < master->num_streams; i++) {
+			cmd.atc.sid = master->streams[i].id;
 			arm_smmu_cmdq_batch_add(smmu_domain->smmu, &cmds, &cmd);
 		}
 	}
@@ -2065,13 +2087,13 @@  static void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master)
 	int i, j;
 	struct arm_smmu_device *smmu = master->smmu;
 
-	for (i = 0; i < master->num_sids; ++i) {
-		u32 sid = master->sids[i];
+	for (i = 0; i < master->num_streams; ++i) {
+		u32 sid = master->streams[i].id;
 		__le64 *step = arm_smmu_get_step_for_sid(smmu, sid);
 
 		/* Bridged PCI devices may end up with duplicated IDs */
 		for (j = 0; j < i; j++)
-			if (master->sids[j] == sid)
+			if (master->streams[j].id == sid)
 				break;
 		if (j < i)
 			continue;
@@ -2345,11 +2367,101 @@  static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
 	return sid < limit;
 }
 
+static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
+				  struct arm_smmu_master *master)
+{
+	int i;
+	int ret = 0;
+	struct arm_smmu_stream *new_stream, *cur_stream;
+	struct rb_node **new_node, *parent_node = NULL;
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
+
+	master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams),
+				  GFP_KERNEL);
+	if (!master->streams)
+		return -ENOMEM;
+	master->num_streams = fwspec->num_ids;
+
+	mutex_lock(&smmu->streams_mutex);
+	for (i = 0; i < fwspec->num_ids; i++) {
+		u32 sid = fwspec->ids[i];
+
+		new_stream = &master->streams[i];
+		new_stream->id = sid;
+		new_stream->master = master;
+
+		/*
+		 * Check the SIDs are in range of the SMMU and our stream table
+		 */
+		if (!arm_smmu_sid_in_range(smmu, sid)) {
+			ret = -ERANGE;
+			break;
+		}
+
+		/* Ensure l2 strtab is initialised */
+		if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
+			ret = arm_smmu_init_l2_strtab(smmu, sid);
+			if (ret)
+				break;
+		}
+
+		/* Insert into SID tree */
+		new_node = &(smmu->streams.rb_node);
+		while (*new_node) {
+			cur_stream = rb_entry(*new_node, struct arm_smmu_stream,
+					      node);
+			parent_node = *new_node;
+			if (cur_stream->id > new_stream->id) {
+				new_node = &((*new_node)->rb_left);
+			} else if (cur_stream->id < new_stream->id) {
+				new_node = &((*new_node)->rb_right);
+			} else {
+				dev_warn(master->dev,
+					 "stream %u already in tree\n",
+					 cur_stream->id);
+				ret = -EINVAL;
+				break;
+			}
+		}
+		if (ret)
+			break;
+
+		rb_link_node(&new_stream->node, parent_node, new_node);
+		rb_insert_color(&new_stream->node, &smmu->streams);
+	}
+
+	if (ret) {
+		for (i--; i >= 0; i--)
+			rb_erase(&master->streams[i].node, &smmu->streams);
+		kfree(master->streams);
+	}
+	mutex_unlock(&smmu->streams_mutex);
+
+	return ret;
+}
+
+static void arm_smmu_remove_master(struct arm_smmu_master *master)
+{
+	int i;
+	struct arm_smmu_device *smmu = master->smmu;
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
+
+	if (!smmu || !master->streams)
+		return;
+
+	mutex_lock(&smmu->streams_mutex);
+	for (i = 0; i < fwspec->num_ids; i++)
+		rb_erase(&master->streams[i].node, &smmu->streams);
+	mutex_unlock(&smmu->streams_mutex);
+
+	kfree(master->streams);
+}
+
 static struct iommu_ops arm_smmu_ops;
 
 static struct iommu_device *arm_smmu_probe_device(struct device *dev)
 {
-	int i, ret;
+	int ret;
 	struct arm_smmu_device *smmu;
 	struct arm_smmu_master *master;
 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
@@ -2370,27 +2482,12 @@  static struct iommu_device *arm_smmu_probe_device(struct device *dev)
 
 	master->dev = dev;
 	master->smmu = smmu;
-	master->sids = fwspec->ids;
-	master->num_sids = fwspec->num_ids;
 	INIT_LIST_HEAD(&master->bonds);
 	dev_iommu_priv_set(dev, master);
 
-	/* Check the SIDs are in range of the SMMU and our stream table */
-	for (i = 0; i < master->num_sids; i++) {
-		u32 sid = master->sids[i];
-
-		if (!arm_smmu_sid_in_range(smmu, sid)) {
-			ret = -ERANGE;
-			goto err_free_master;
-		}
-
-		/* Ensure l2 strtab is initialised */
-		if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
-			ret = arm_smmu_init_l2_strtab(smmu, sid);
-			if (ret)
-				goto err_free_master;
-		}
-	}
+	ret = arm_smmu_insert_master(smmu, master);
+	if (ret)
+		goto err_free_master;
 
 	device_property_read_u32(dev, "pasid-num-bits", &master->ssid_bits);
 	master->ssid_bits = min(smmu->ssid_bits, master->ssid_bits);
@@ -2429,6 +2526,7 @@  static void arm_smmu_release_device(struct device *dev)
 	WARN_ON(arm_smmu_master_sva_enabled(master));
 	arm_smmu_detach_dev(master);
 	arm_smmu_disable_pasid(master);
+	arm_smmu_remove_master(master);
 	kfree(master);
 	iommu_fwspec_free(dev);
 }
@@ -2852,6 +2950,9 @@  static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
 {
 	int ret;
 
+	mutex_init(&smmu->streams_mutex);
+	smmu->streams = RB_ROOT;
+
 	ret = arm_smmu_init_queues(smmu);
 	if (ret)
 		return ret;