qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: CLEMENT MATHIEU--DRIF <clement.mathieu--drif@eviden.com>
Cc: "qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
	"jasowang@redhat.com" <jasowang@redhat.com>,
	"zhenzhong.duan@intel.com" <zhenzhong.duan@intel.com>,
	"kevin.tian@intel.com" <kevin.tian@intel.com>,
	"yi.l.liu@intel.com" <yi.l.liu@intel.com>,
	"peterx@redhat.com" <peterx@redhat.com>
Subject: Re: [PATCH v5 2/2] intel_iommu: Take locks when looking for and creating address spaces
Date: Wed, 14 May 2025 07:48:58 -0400	[thread overview]
Message-ID: <20250514074821-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <20250430124750.240412-3-clement.mathieu--drif@eviden.com>

On Wed, Apr 30, 2025 at 12:48:06PM +0000, CLEMENT MATHIEU--DRIF wrote:
> vtd_find_add_as can be called by multiple threads which leads to a race
> condition. Taking the IOMMU lock ensures we avoid such a race.
> Moreover we also need to take the bql to avoid an assert to fail in
> memory_region_add_subregion_overlap when actually allocating a new
> address space.
> 
> Signed-off-by: Clement Mathieu--Drif <clement.mathieu--drif@eviden.com>
> ---
>  hw/i386/intel_iommu.c | 25 ++++++++++++++++++++++++-
>  1 file changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
> index dad1d9f300..144e25622a 100644
> --- a/hw/i386/intel_iommu.c
> +++ b/hw/i386/intel_iommu.c
> @@ -4205,9 +4205,30 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
>      VTDAddressSpace *vtd_dev_as;
>      char name[128];
>  
> +    vtd_iommu_lock(s);
>      vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
> +    vtd_iommu_unlock(s);
> +
>      if (!vtd_dev_as) {
> -        struct vtd_as_key *new_key = g_malloc(sizeof(*new_key));
> +        struct vtd_as_key *new_key;
> +        /* Slow path */
> +
> +        /*
> +        * memory_region_add_subregion_overlap requires the bql,
> +        * make sure we own it.
> +        */


not how comments should look

> +        BQL_LOCK_GUARD();
> +        vtd_iommu_lock(s);
> +
> +        /* Check again as we released the lock for a moment */
> +        vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
> +        if (vtd_dev_as) {
> +            vtd_iommu_unlock(s);
> +            return vtd_dev_as;
> +        }
> +
> +        /* Still nothing, allocate a new address space */
> +        new_key = g_malloc(sizeof(*new_key));
>  
>          new_key->bus = bus;
>          new_key->devfn = devfn;
> @@ -4298,6 +4319,8 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
>          vtd_switch_address_space(vtd_dev_as);
>  
>          g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
> +    

trailing whitespace here

> +        vtd_iommu_unlock(s);
>      }
>      return vtd_dev_as;
>  }



I fixed these up but pls take care, Clement.

> -- 
> 2.49.0



  reply	other threads:[~2025-05-14 11:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-30 12:48 [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
2025-04-30 12:48 ` [PATCH v5 1/2] intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically CLEMENT MATHIEU--DRIF
2025-04-30 12:48 ` [PATCH v5 2/2] intel_iommu: Take locks when looking for and creating address spaces CLEMENT MATHIEU--DRIF
2025-05-14 11:48   ` Michael S. Tsirkin [this message]
2025-05-14 16:29     ` CLEMENT MATHIEU--DRIF
2025-05-06  5:07 ` [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
2025-05-06  9:54 ` Duan, Zhenzhong

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=20250514074821-mutt-send-email-mst@kernel.org \
    --to=mst@redhat.com \
    --cc=clement.mathieu--drif@eviden.com \
    --cc=jasowang@redhat.com \
    --cc=kevin.tian@intel.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=yi.l.liu@intel.com \
    --cc=zhenzhong.duan@intel.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).