From: Mostafa Saleh <smostafa@google.com>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: iommu@lists.linux.dev, Joerg Roedel <joro@8bytes.org>,
linux-arm-kernel@lists.infradead.org,
Robin Murphy <robin.murphy@arm.com>,
Will Deacon <will@kernel.org>,
Michael Shavit <mshavit@google.com>,
Nicolin Chen <nicolinc@nvidia.com>,
patches@lists.linux.dev, Ryan Roberts <ryan.roberts@arm.com>
Subject: Re: [PATCH 7/7] iommu/arm-smmu-v3: Use the new rb tree helpers
Date: Tue, 4 Jun 2024 16:22:11 +0000 [thread overview]
Message-ID: <Zl8_M0_Vc6-_h7WH@google.com> (raw)
In-Reply-To: <7-v1-1b720dce51d1+4f44-smmuv3_tidy_jgg@nvidia.com>
Hi Jason,
On Mon, Jun 03, 2024 at 07:31:33PM -0300, Jason Gunthorpe wrote:
> Since v5.12 the rbtree has gained some simplifying helpers aimed at making
> rb tree users write less convoluted boiler plate code. Instead the caller
> provides a single comparison function and the helpers generate the prior
> open-coded stuff.
>
> Update smmu->streams to use rb_find_add() and rb_find().
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Mostafa Saleh <smostafa@google.com>
> ---
> drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 68 ++++++++++-----------
> 1 file changed, 31 insertions(+), 37 deletions(-)
>
> 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 7a6c9aac4cd450..25bae0b05a488c 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -1667,26 +1667,37 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
> return 0;
> }
>
> +static int arm_smmu_streams_cmp_key(const void *lhs, const struct rb_node *rhs)
> +{
> + struct arm_smmu_stream *stream_rhs =
> + rb_entry(rhs, struct arm_smmu_stream, node);
> + const u32 *sid_lhs = lhs;
> +
> + if (*sid_lhs < stream_rhs->id)
> + return -1;
> + if (*sid_lhs > stream_rhs->id)
> + return 1;
> + return 0;
> +}
> +
> +static int arm_smmu_streams_cmp_node(struct rb_node *lhs,
> + const struct rb_node *rhs)
> +{
> + return arm_smmu_streams_cmp_key(
> + &rb_entry(lhs, struct arm_smmu_stream, node)->id, rhs);
> +}
> +
> 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;
>
> lockdep_assert_held(&smmu->streams_mutex);
>
> - 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;
> + node = rb_find(&sid, &smmu->streams, arm_smmu_streams_cmp_key);
> + if (!node)
> + return NULL;
> + return rb_entry(node, struct arm_smmu_stream, node)->master;
> }
>
> /* IRQ and event handlers */
> @@ -2795,8 +2806,6 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
> {
> 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),
> @@ -2807,9 +2816,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
>
> mutex_lock(&smmu->streams_mutex);
> for (i = 0; i < fwspec->num_ids; i++) {
> + struct arm_smmu_stream *new_stream = &master->streams[i];
> u32 sid = fwspec->ids[i];
>
> - new_stream = &master->streams[i];
> new_stream->id = sid;
> new_stream->master = master;
>
> @@ -2818,28 +2827,13 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
> 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)
> + if (rb_find_add(&new_stream->node, &smmu->streams,
> + arm_smmu_streams_cmp_node)) {
> + dev_warn(master->dev, "stream %u already in tree\n",
> + sid);
> + ret = -EINVAL;
> break;
> -
> - rb_link_node(&new_stream->node, parent_node, new_node);
> - rb_insert_color(&new_stream->node, &smmu->streams);
> + }
> }
>
> if (ret) {
> --
> 2.45.2
>
WARNING: multiple messages have this Message-ID (diff)
From: Mostafa Saleh <smostafa@google.com>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: iommu@lists.linux.dev, Joerg Roedel <joro@8bytes.org>,
linux-arm-kernel@lists.infradead.org,
Robin Murphy <robin.murphy@arm.com>,
Will Deacon <will@kernel.org>,
Michael Shavit <mshavit@google.com>,
Nicolin Chen <nicolinc@nvidia.com>,
patches@lists.linux.dev, Ryan Roberts <ryan.roberts@arm.com>
Subject: Re: [PATCH 7/7] iommu/arm-smmu-v3: Use the new rb tree helpers
Date: Tue, 4 Jun 2024 16:22:11 +0000 [thread overview]
Message-ID: <Zl8_M0_Vc6-_h7WH@google.com> (raw)
In-Reply-To: <7-v1-1b720dce51d1+4f44-smmuv3_tidy_jgg@nvidia.com>
Hi Jason,
On Mon, Jun 03, 2024 at 07:31:33PM -0300, Jason Gunthorpe wrote:
> Since v5.12 the rbtree has gained some simplifying helpers aimed at making
> rb tree users write less convoluted boiler plate code. Instead the caller
> provides a single comparison function and the helpers generate the prior
> open-coded stuff.
>
> Update smmu->streams to use rb_find_add() and rb_find().
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Mostafa Saleh <smostafa@google.com>
> ---
> drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 68 ++++++++++-----------
> 1 file changed, 31 insertions(+), 37 deletions(-)
>
> 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 7a6c9aac4cd450..25bae0b05a488c 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -1667,26 +1667,37 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
> return 0;
> }
>
> +static int arm_smmu_streams_cmp_key(const void *lhs, const struct rb_node *rhs)
> +{
> + struct arm_smmu_stream *stream_rhs =
> + rb_entry(rhs, struct arm_smmu_stream, node);
> + const u32 *sid_lhs = lhs;
> +
> + if (*sid_lhs < stream_rhs->id)
> + return -1;
> + if (*sid_lhs > stream_rhs->id)
> + return 1;
> + return 0;
> +}
> +
> +static int arm_smmu_streams_cmp_node(struct rb_node *lhs,
> + const struct rb_node *rhs)
> +{
> + return arm_smmu_streams_cmp_key(
> + &rb_entry(lhs, struct arm_smmu_stream, node)->id, rhs);
> +}
> +
> 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;
>
> lockdep_assert_held(&smmu->streams_mutex);
>
> - 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;
> + node = rb_find(&sid, &smmu->streams, arm_smmu_streams_cmp_key);
> + if (!node)
> + return NULL;
> + return rb_entry(node, struct arm_smmu_stream, node)->master;
> }
>
> /* IRQ and event handlers */
> @@ -2795,8 +2806,6 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
> {
> 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),
> @@ -2807,9 +2816,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
>
> mutex_lock(&smmu->streams_mutex);
> for (i = 0; i < fwspec->num_ids; i++) {
> + struct arm_smmu_stream *new_stream = &master->streams[i];
> u32 sid = fwspec->ids[i];
>
> - new_stream = &master->streams[i];
> new_stream->id = sid;
> new_stream->master = master;
>
> @@ -2818,28 +2827,13 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
> 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)
> + if (rb_find_add(&new_stream->node, &smmu->streams,
> + arm_smmu_streams_cmp_node)) {
> + dev_warn(master->dev, "stream %u already in tree\n",
> + sid);
> + ret = -EINVAL;
> break;
> -
> - rb_link_node(&new_stream->node, parent_node, new_node);
> - rb_insert_color(&new_stream->node, &smmu->streams);
> + }
> }
>
> if (ret) {
> --
> 2.45.2
>
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2024-06-04 16:22 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-03 22:31 [PATCH 0/7] Tidy some minor things in the stream table/cd table area Jason Gunthorpe
2024-06-03 22:31 ` Jason Gunthorpe
2024-06-03 22:31 ` [PATCH 1/7] iommu/arm-smmu-v3: Split struct arm_smmu_strtab_cfg.strtab Jason Gunthorpe
2024-06-03 22:31 ` Jason Gunthorpe
2024-06-04 8:32 ` Nicolin Chen
2024-06-04 8:32 ` Nicolin Chen
2024-06-04 12:59 ` Jason Gunthorpe
2024-06-04 12:59 ` Jason Gunthorpe
2024-06-04 18:28 ` Nicolin Chen
2024-06-04 18:28 ` Nicolin Chen
2024-06-04 19:02 ` Jason Gunthorpe
2024-06-04 19:02 ` Jason Gunthorpe
2024-06-04 19:28 ` Nicolin Chen
2024-06-04 19:28 ` Nicolin Chen
2024-06-04 15:52 ` Mostafa Saleh
2024-06-04 15:52 ` Mostafa Saleh
2024-06-05 23:51 ` Jason Gunthorpe
2024-06-05 23:51 ` Jason Gunthorpe
2024-06-03 22:31 ` [PATCH 2/7] iommu/arm-smmu-v3: Do not zero the strtab twice Jason Gunthorpe
2024-06-03 22:31 ` Jason Gunthorpe
2024-06-04 15:56 ` Mostafa Saleh
2024-06-04 15:56 ` Mostafa Saleh
2024-06-05 21:22 ` Jason Gunthorpe
2024-06-05 21:22 ` Jason Gunthorpe
2024-06-03 22:31 ` [PATCH 3/7] iommu/arm-smmu-v3: Shrink the strtab l1_desc array Jason Gunthorpe
2024-06-03 22:31 ` Jason Gunthorpe
2024-06-04 16:01 ` Mostafa Saleh
2024-06-04 16:01 ` Mostafa Saleh
2024-06-03 22:31 ` [PATCH 4/7] iommu/arm-smmu-v3: Split struct arm_smmu_ctx_desc_cfg.cdtab Jason Gunthorpe
2024-06-03 22:31 ` Jason Gunthorpe
2024-06-04 16:07 ` Mostafa Saleh
2024-06-04 16:07 ` Mostafa Saleh
2024-06-06 23:59 ` Jason Gunthorpe
2024-06-06 23:59 ` Jason Gunthorpe
2024-06-03 22:31 ` [PATCH 5/7] iommu/arm-smmu-v3: Do not use devm for the cd table allocations Jason Gunthorpe
2024-06-03 22:31 ` Jason Gunthorpe
2024-06-03 22:31 ` [PATCH 6/7] iommu/arm-smmu-v3: Shrink the cdtab l1_desc array Jason Gunthorpe
2024-06-03 22:31 ` Jason Gunthorpe
2024-06-04 16:14 ` Mostafa Saleh
2024-06-04 16:14 ` Mostafa Saleh
2024-06-03 22:31 ` [PATCH 7/7] iommu/arm-smmu-v3: Use the new rb tree helpers Jason Gunthorpe
2024-06-03 22:31 ` Jason Gunthorpe
2024-06-04 16:22 ` Mostafa Saleh [this message]
2024-06-04 16:22 ` Mostafa Saleh
2024-06-03 22:41 ` [PATCH 0/7] Tidy some minor things in the stream table/cd table area Nicolin Chen
2024-06-03 22:41 ` Nicolin Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Zl8_M0_Vc6-_h7WH@google.com \
--to=smostafa@google.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=joro@8bytes.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=mshavit@google.com \
--cc=nicolinc@nvidia.com \
--cc=patches@lists.linux.dev \
--cc=robin.murphy@arm.com \
--cc=ryan.roberts@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.