From: Jason Gunthorpe <jgg@nvidia.com>
To: Vasant Hegde <vasant.hegde@amd.com>
Cc: iommu@lists.linux.dev, joro@8bytes.org,
suravee.suthikulpanit@amd.com,
Daniel Marcovitch <dmarcovitch@nvidia.com>
Subject: Re: [PATCH 2/3] iommu/amd/iommu_v2: Fix pasid_state->wq race
Date: Tue, 24 Jan 2023 11:30:53 -0400 [thread overview]
Message-ID: <Y8/5rTjCZjN9I0ak@nvidia.com> (raw)
In-Reply-To: <20230124104355.119166-3-vasant.hegde@amd.com>
On Tue, Jan 24, 2023 at 10:43:54AM +0000, Vasant Hegde wrote:
> From: Daniel Marcovitch <dmarcovitch@nvidia.com>
>
> pasid_state->wq is used to wait for all outstanding pri handling prior
> to pasid unbind.
>
> ppr handler calls the wake_up API on the pasid_state->wq once
> pasid_state refcount has reached 0. Since refcount is initialized to 1,
> this will only occur during pasid unbind.
>
> However, unbind_pasid may reach refcount check at this point of ppr_handler
> execution. Since refcount is 0, it will not wait on wait_queue - but
> rather proceed with pasid_state deallocation, including wait_queue.
>
> This can cause unallocated memory access.
>
> Fixed by adding spinlock to ensure atomicity between refcount check
> and event queue handling. Moved wait_queue APIs to
> wake_up_locked / wake_event_interruptible_locked accordingly.
>
> Signed-off-by: Daniel Marcovitch <dmarcovitch@nvidia.com>
> Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
> ---
> drivers/iommu/amd/iommu_v2.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iommu/amd/iommu_v2.c b/drivers/iommu/amd/iommu_v2.c
> index 858748baae0b..44fed4ca576f 100644
> --- a/drivers/iommu/amd/iommu_v2.c
> +++ b/drivers/iommu/amd/iommu_v2.c
> @@ -256,14 +256,18 @@ static void free_pasid_state(struct pasid_state *pasid_state)
>
> static void put_pasid_state(struct pasid_state *pasid_state)
> {
> + spin_lock(&pasid_state->wq.lock);
> if (refcount_dec_and_test(&pasid_state->count))
> - wake_up(&pasid_state->wq);
> + wake_up_locked(&pasid_state->wq);
> + spin_unlock(&pasid_state->wq.lock);
> }
>
> static void put_pasid_state_wait(struct pasid_state *pasid_state)
> {
> + spin_lock(&pasid_state->wq.lock);
> if (!refcount_dec_and_test(&pasid_state->count))
> - wait_event(pasid_state->wq, !refcount_read(&pasid_state->count));
> + wait_event_interruptible_locked(pasid_state->wq, !refcount_read(&pasid_state->count));
> + spin_unlock(&pasid_state->wq.lock);
You can't call wait_event_interruptible_locked() while holding a
spinlock
What this is really trying to make is one of those open coded rwlocks
that can be passed into a WQ.
struct refcount count
struct completion comp
init:
refcount_set(count, 1)
get:
if !refcount_inc_not_zero(count)
// Concurrent destruction, can't get the pasid state!
put:
if refcount_dec_and_test(count)
complete(&device->comp)
destroy:
put() // put back the refcount_set()
wait_for_completion()
// Now count == 0
clear_pasid_state()
free()
Jason
next prev parent reply other threads:[~2023-01-24 15:30 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-24 10:43 [PATCH 0/3] iommu/amd/iommu_v2: Fix refcount related issues Vasant Hegde
2023-01-24 10:43 ` [PATCH 1/3] iommu/amd/iommu_v2: Fix pasid_state refcount dec hit 0 warning on pasid unbind Vasant Hegde
2023-01-24 15:23 ` Jason Gunthorpe
2023-01-27 4:44 ` Vasant Hegde
2023-01-27 12:58 ` Jason Gunthorpe
2023-01-24 10:43 ` [PATCH 2/3] iommu/amd/iommu_v2: Fix pasid_state->wq race Vasant Hegde
2023-01-24 15:30 ` Jason Gunthorpe [this message]
2023-01-24 10:43 ` [PATCH 3/3] iommu/amd/iommu_v2: Prevent scheduling new ppr notifier during unbind_pasid Vasant Hegde
2023-01-24 15:33 ` Jason Gunthorpe
2023-01-27 4:58 ` Vasant Hegde
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=Y8/5rTjCZjN9I0ak@nvidia.com \
--to=jgg@nvidia.com \
--cc=dmarcovitch@nvidia.com \
--cc=iommu@lists.linux.dev \
--cc=joro@8bytes.org \
--cc=suravee.suthikulpanit@amd.com \
--cc=vasant.hegde@amd.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox