From: Nicolin Chen <nicolinc@nvidia.com>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: <iommu@lists.linux.dev>,
Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
"Lu Baolu" <baolu.lu@linux.intel.com>,
Kevin Tian <kevin.tian@intel.com>, <patches@lists.linux.dev>,
Pranjal Shrivastava <praan@google.com>
Subject: Re: [PATCH] iommufd: Fix refcounting race during mmap
Date: Wed, 17 Sep 2025 11:34:15 -0700 [thread overview]
Message-ID: <aMr/JyYjV97r+vqx@Asurada-Nvidia> (raw)
In-Reply-To: <0-v1-e6faace50971+3cc-iommufd_mmap_fix_jgg@nvidia.com>
On Tue, Sep 16, 2025 at 01:10:07PM -0300, Jason Gunthorpe wrote:
> The owner object of the imap can be destroyed while the imap remains in
> the mtree. So access to the imap pointer without holding locks is racy
> with destruction.
>
> The imap is safe to access outside the lock once a users refcount is
> obtained, the owner object cannot start destruction until users is 0.
>
> Thus the users refcount should not be obtained at the end of
> iommufd_fops_mmap() but instead inside the mtree lock held around the
> mtree_load(). Move the refcount there and use refcount_inc_not_zero() as
> we can have a 0 refcount inside the mtree during destruction races.
>
> Cc: stable@vger.kernel.org
> Fixes: 56e9a0d8e53f ("iommufd: Add mmap interface")
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Nicolin Chen <nicolinc@nvidia.com>
> ---
> drivers/iommu/iommufd/main.c | 20 ++++++++++++++------
> 1 file changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
> index 15af7ced0501d6..109de747e8b3ed 100644
> --- a/drivers/iommu/iommufd/main.c
> +++ b/drivers/iommu/iommufd/main.c
> @@ -551,15 +551,22 @@ static int iommufd_fops_mmap(struct file *filp, struct vm_area_struct *vma)
> return -EPERM;
>
> /* vma->vm_pgoff carries a page-shifted start position to an immap */
> + mtree_lock(&ictx->mt_mmap);
> immap = mtree_load(&ictx->mt_mmap, vma->vm_pgoff << PAGE_SHIFT);
Nit: should we put the comment line closer to mtree_load()?
next prev parent reply other threads:[~2025-09-17 18:34 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-16 16:10 [PATCH] iommufd: Fix refcounting race during mmap Jason Gunthorpe
2025-09-17 18:34 ` Nicolin Chen [this message]
2025-09-18 14:44 ` Jason Gunthorpe
2025-09-19 8:13 ` Tian, Kevin
2025-09-19 13:42 ` Jason Gunthorpe
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=aMr/JyYjV97r+vqx@Asurada-Nvidia \
--to=nicolinc@nvidia.com \
--cc=baolu.lu@linux.intel.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=kevin.tian@intel.com \
--cc=lorenzo.stoakes@oracle.com \
--cc=patches@lists.linux.dev \
--cc=praan@google.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;
as well as URLs for NNTP newsgroup(s).