All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sven Schnelle" <svens@linux.ibm.com>
Cc: linux-s390@vger.kernel.org, "Heiko Carstens" <hca@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>
Subject: Re: [PATCH 1/3] iommu: sva: allow SVA without PASID
Date: Fri, 04 Dec 2026 15:12:16 +0000	[thread overview]
Message-ID: <sashiko-outbox-250066@kernel.org> (raw)
In-Reply-To: <20261204144529.960326-2-svens@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] iommu: unlocked dev->iommu dereference races with
  iommu_init_device()
- [Medium] iommu/sva: IOMMU_SVA stays under IOMMU_SUPPORT without
  selecting IOMMU_API
- [Low] iommu/sva: misleading kernel-doc in
  iommu_sva_bind_device_no_iommu()
--

commit 8cca46e12185238c9aeda86bc1c45d0a7bb8e5f9
Author: Arnd Bergmann <arnd@arndb.de>

iommu: sva: allow SVA without PASID

This commit adds iommu_sva_bind_device_no_iommu() so devices without an
IOMMU can install an iommu_mm_data object and allocate a global PASID on
an mm_struct, and updates iommu_alloc_mm_data() and
iommu_alloc_global_pasid() to handle a NULL dev->iommu.

> diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c
> index 9827cb8043635..b444f3483f6df 100644
> --- a/drivers/iommu/iommu-sva.c
> +++ b/drivers/iommu/iommu-sva.c

[ ... ]

> @@ -156,6 +156,35 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
>  }
>  EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
>  
> +/**
> + * iommu_sva_bind_device_no_iommu() - Bind a process address space to a device without iommu
> + * @dev: the device
> + * @mm: the mm to bind, caller must hold a reference to mm_users
> + *
> + * Allow the use of shared virtual addressing on devices that transparently
> + * use the CPU's page table instead of an IOMMU. A global PASID is allocated
> + * in case the same process uses another device with an IOMMU later.
> + *
> + * The reference is held until the mm_struct is destroyed.

[Severity: Low]
Is "The reference is held until the mm_struct is destroyed" accurate?
iommu_sva_bind_device_no_iommu() takes no reference on mm or dev and
allocates no iommu_sva handle; only mm->iommu_mm and its global PASID
remain allocated until mm_pasid_drop() runs in __mmdrop().  Describing
the lifetime of mm->iommu_mm and that no explicit unbind is needed would
avoid misleading callers.

[ ... ]

> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index b486b8bbd1fcd..1470bb9c1ca89 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -3827,17 +3827,24 @@ EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
>  ioasid_t iommu_alloc_global_pasid(struct device *dev)
>  {
>  	int ret;
> +	unsigned int max_pasid;
>  
> -	/* max_pasids == 0 means that the device does not support PASID */
> -	if (!dev->iommu->max_pasids)
> +	if (!dev->iommu) {
> +		/* no IOMMU but capable of enqcmd/st64bv0: pick any PASID */
> +		max_pasid = IOMMU_PASID_INVALID - 1;
> +	} else if (dev->iommu->max_pasids) {
> +		/*
> +		 * max_pasids is set up by vendor driver based on number of
> +		 * PASID bits supported but the IDA allocation is inclusive.
> +		 */
> +		max_pasid = dev->iommu->max_pasids - 1;
> +	} else {
> +		/* max_pasids == 0 means that the device does not support PASID */
>  		return IOMMU_PASID_INVALID;
> +	}

[Severity: Medium]
Can reading and dereferencing dev->iommu here without
iommu_probe_device_lock race with iommu_init_device()?

Previously, iommu_alloc_mm_data() and iommu_alloc_global_pasid() were
only reached when dev->iommu_group was non-NULL, so
__iommu_probe_device() returned early without touching dev->iommu.  When
iommu_sva_bind_device_no_iommu() runs with dev->iommu_group == NULL, a
concurrent iommu_device_register() scan via bus_iommu_probe() calls
iommu_init_device(dev), which transiently allocates dev->iommu with
max_pasids == 0 and then frees it with kfree() in dev_iommu_free() under
iommu_probe_device_lock:

CPU0                                    CPU1
iommu_sva_bind_device_no_iommu()        iommu_device_register()
  mutex_lock(&iommu_sva_lock)             ... -> __iommu_probe_device()
  iommu_alloc_mm_data()                     lock(&iommu_probe_device_lock)
    iommu_alloc_global_pasid()              iommu_init_device()
                                              dev_iommu_get(dev)
                                                dev->iommu = param
      if (!dev->iommu) /* non-NULL */
                                              dev_iommu_free(dev)
                                                dev->iommu = NULL
                                                kfree(param)
      else if (dev->iommu->max_pasids)
               <-- use-after-free

Depending on whether the compiler caches or reloads dev->iommu, this can
cause a slab use-after-free read, a NULL pointer dereference, or a
spurious -ENOSPC/-EOVERFLOW if transient max_pasids == 0 is observed
before kfree().  iommu_alloc_mm_data() has the same unlocked dev->iommu
dereference, and iommu_sva_bind_device_no_iommu() also checks
dev->iommu_group without iommu_probe_device_lock.

[ ... ]

> diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> index ef5043752a365..131595da295f7 100644
> --- a/include/linux/iommu.h
> +++ b/include/linux/iommu.h
> @@ -1663,6 +1663,8 @@ static inline u32 mm_get_enqcmd_pasid(struct mm_struct *mm)
>  void mm_pasid_drop(struct mm_struct *mm);
>  struct iommu_sva *iommu_sva_bind_device(struct device *dev,
>  					struct mm_struct *mm);
> +int iommu_sva_bind_device_no_iommu(struct device *dev,
> +				   struct mm_struct *mm);

[Severity: Medium]
Does selecting IOMMU_SVA from a no-IOMMU driver break the build when
CONFIG_IOMMU_SUPPORT=n?

In drivers/iommu/Kconfig, config IOMMU_SVA stays nested inside
if IOMMU_SUPPORT and selects IOMMU_MM_DATA without selecting IOMMU_API.
When a no-IOMMU driver (such as commit "drivers: firmware: add ARM on
s390 accelerator driver") selects IOMMU_SVA with CONFIG_IOMMU_SUPPORT=n
and nothing else enabling CONFIG_IOMMU_API, Kconfig warns about the
unmet IOMMU_SUPPORT dependency and enables CONFIG_IOMMU_SVA and
CONFIG_IOMMU_MM_DATA while CONFIG_IOMMU_API stays disabled.

In that configuration, drivers/iommu/Makefile builds iommu-sva.o without
iommu.o, while struct iommu_mm_data, struct dev_iommu, and the real
iommu_alloc_global_pasid() remain inside #ifdef CONFIG_IOMMU_API in
include/linux/iommu.h, causing compilation failures in
mm_get_enqcmd_pasid() and iommu-sva.c.  Moving config IOMMU_SVA outside
if IOMMU_SUPPORT and adding select IOMMU_API (or factoring global PASID
and iommu_mm_data out of CONFIG_IOMMU_API) would prevent both the
warning and the build failure.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204144529.960326-1-svens@linux.ibm.com?part=1

  reply	other threads:[~2026-12-04 15:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 14:45 [PATCH 0/3] Add arm on s390 accelerator driver Sven Schnelle
2026-12-04 14:45 ` [PATCH 1/3] iommu: sva: allow SVA without PASID Sven Schnelle
2026-12-04 15:12   ` sashiko-bot [this message]
2026-12-04 14:45 ` [PATCH 2/3] dt-bindings: firmware: platform-accel: add yaml documentation Sven Schnelle
2026-12-04 15:22   ` sashiko-bot
2026-12-05 13:18   ` Krzysztof Kozlowski
2026-12-04 14:45 ` [PATCH 3/3] drivers: firmware: add ARM on s390 accelerator driver Sven Schnelle
2026-12-04 15:28   ` Arnd Bergmann
2026-12-04 15:45   ` sashiko-bot
2026-12-05  9:47   ` Uwe Kleine-König

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=sashiko-outbox-250066@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=linux-s390@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=svens@linux.ibm.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 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.