From: Jason Gunthorpe <jgg@nvidia.com>
To: Baolu Lu <baolu.lu@linux.intel.com>
Cc: iommu@lists.linux.dev, Joerg Roedel <joro@8bytes.org>,
llvm@lists.linux.dev, Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Miguel Ojeda <ojeda@kernel.org>,
Robin Murphy <robin.murphy@arm.com>, Tom Rix <trix@redhat.com>,
Will Deacon <will@kernel.org>, Kevin Tian <kevin.tian@intel.com>,
Nicolin Chen <nicolinc@nvidia.com>
Subject: Re: [PATCH v2 12/14] iommu: Consolidate the default_domain setup to one function
Date: Thu, 30 Mar 2023 12:36:22 -0300 [thread overview]
Message-ID: <ZCWsdjM/21IPvjKi@nvidia.com> (raw)
In-Reply-To: <19197c52-139e-c3c5-2771-42323d38c045@linux.intel.com>
On Thu, Mar 30, 2023 at 08:37:16PM +0800, Baolu Lu wrote:
> On 2023/3/30 7:40, Jason Gunthorpe wrote:
> > +/**
> > + * iommu_setup_default_domain - Set the default_domain for the group
> > + * @group: Group to change
> > + * @target_type: Domain type to set as the default_domain
> > + *
> > + * Allocate a default domain and set it as the current domain on the group. If
> > + * the group already has a default domain it will be changed to the target_type.
> > + * When target_type is 0 the default domain is selected based on driver and
> > + * system preferences.
> > + */
> > +static int iommu_setup_default_domain(struct iommu_group *group,
> > + int target_type)
> > +{
> > + struct group_device *gdev;
> > + struct iommu_domain *dom;
> > + struct bus_type *bus =
> > + list_first_entry(&group->devices, struct group_device, list)
> > + ->dev->bus;
> > + int ret;
> > +
> > + lockdep_assert_held(&group->mutex);
> > +
> > + target_type = iommu_get_default_domain_type(group, target_type);
> > + if (target_type < 0)
> > + return -EINVAL;
> > +
> > + if (group->default_domain && group->default_domain->type == target_type)
> > + return 0;
> > +
> > + dom = __iommu_domain_alloc(bus, target_type);
> > + if (!dom && target_type != IOMMU_DOMAIN_DMA) {
> > + dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
> > + if (dom)
> > + pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
> > + target_type, group->name);
> > + }
>
> The background of the code above is that some ARM IOMMU drivers only
> support DMA mapping domain and do not support identity domain.
> Therefore, during boot, if the allocation of identity domain fails, a
> DMA mapping domain is used instead.
Er, this is doing two things then because it also allows DMA_FQ to
degrade to just DMA..
I changed it like this:
dom = __iommu_domain_alloc(bus, req_type);
if (!dom && !target_type &&
(req_type == IOMMU_DOMAIN_IDENTITY ||
req_type == IOMMU_DOMAIN_DMA_FQ)) {
dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
So the auto selection only happens if the target_type is not automatic.
> However, this does not apply to use cases that change the default domain
> through sysfs. In such cases, it seems that we should directly return
> failure (-ENODEV) and tell the user that the iommu driver does not
> support identity domain.
And with the fix below it will return ENODEV rather than autoselect DMA.
> > + /*
> > + * There are still some drivers which don't support default domains, so
> > + * we ignore the failure and leave group->default_domain NULL.
> > + *
> > + * We assume that the iommu driver starts up the device in
> > + * 'set_platform_dma_ops' mode if it does not support default domains.
> > + */
> > + if (!dom) {
> > + ret = 0;
> > + goto out_set;
> > + }
>
> Should we call set_platform_dma_ops here?
It could be done and should be harmless, but the driver is supposed to
start up in that mode so don't need to explicitly enter it when
plugging the device.. I kept things as-is
> The existing default domain
> (if exists) will be freed below. But the iommu driver doesn't know about
> this. It probably will create a UAF case?
This is a bug:
if (!dom) {
/* Once in default_domain mode we never leave */
if (group->default_domain)
return -ENODEV;
So default_domain is either NULL forever or set to something forever.
Jason
next prev parent reply other threads:[~2023-03-30 15:36 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-29 23:40 [PATCH v2 00/14] Consolidate the error handling around device attachment Jason Gunthorpe
2023-03-29 23:40 ` [PATCH v2 01/14] iommu: Replace iommu_group_device_count() with list_count_nodes() Jason Gunthorpe
2023-03-30 6:22 ` Baolu Lu
2023-04-04 9:15 ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 02/14] iommu: Add for_each_group_device() Jason Gunthorpe
2023-03-29 23:52 ` Miguel Ojeda
2023-03-30 14:28 ` Jason Gunthorpe
2023-05-09 13:12 ` Miguel Ojeda
2023-05-10 1:01 ` Jason Gunthorpe
2023-03-30 6:23 ` Baolu Lu
2023-04-04 9:16 ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 03/14] iommu: Make __iommu_group_set_domain() handle error unwind Jason Gunthorpe
2023-03-30 6:23 ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 04/14] iommu: Use __iommu_group_set_domain() for __iommu_attach_group() Jason Gunthorpe
2023-03-30 6:23 ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 05/14] iommu: Use __iommu_group_set_domain() in iommu_change_dev_def_domain() Jason Gunthorpe
2023-03-30 6:24 ` Baolu Lu
2023-04-04 9:16 ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 06/14] iommu: Replace __iommu_group_dma_first_attach() with set_domain Jason Gunthorpe
2023-03-30 6:24 ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 07/14] iommu: Make iommu_group_do_dma_first_attach() simpler Jason Gunthorpe
2023-03-30 6:42 ` Baolu Lu
2023-03-30 14:41 ` Jason Gunthorpe
2023-03-31 2:21 ` Baolu Lu
2023-04-04 9:17 ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 08/14] iommu: Make iommu_group_do_dma_first_attach() work with owned groups Jason Gunthorpe
2023-03-30 6:45 ` Baolu Lu
2023-03-30 15:54 ` Robin Murphy
2023-03-30 16:49 ` Jason Gunthorpe
2023-04-04 9:21 ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 09/14] iommu: Fix iommu_probe_device() to attach the right domain Jason Gunthorpe
2023-03-30 7:33 ` Baolu Lu
2023-04-04 9:25 ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 10/14] iommu: Remove the assignment of group->domain during default domain alloc Jason Gunthorpe
2023-03-30 7:33 ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 11/14] iommu: Consolidate the code to calculate the target default domain type Jason Gunthorpe
2023-03-30 11:51 ` Baolu Lu
2023-04-04 9:39 ` Tian, Kevin
2023-04-04 18:51 ` Jason Gunthorpe
2023-03-29 23:40 ` [PATCH v2 12/14] iommu: Consolidate the default_domain setup to one function Jason Gunthorpe
2023-03-30 12:37 ` Baolu Lu
2023-03-30 14:29 ` Robin Murphy
2023-03-30 14:45 ` Jason Gunthorpe
2023-03-30 15:42 ` Jason Gunthorpe
2023-04-04 11:29 ` Robin Murphy
2023-03-30 15:36 ` Jason Gunthorpe [this message]
2023-03-30 18:23 ` Robin Murphy
2023-03-30 19:01 ` Jason Gunthorpe
2023-03-29 23:40 ` [PATCH v2 13/14] iommu: Remove __iommu_group_for_each_dev() Jason Gunthorpe
2023-03-30 12:40 ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 14/14] iommu: Tidy the control flow in iommu_group_store_type() Jason Gunthorpe
2023-03-30 12:45 ` Baolu Lu
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=ZCWsdjM/21IPvjKi@nvidia.com \
--to=jgg@nvidia.com \
--cc=baolu.lu@linux.intel.com \
--cc=iommu@lists.linux.dev \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=llvm@lists.linux.dev \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nicolinc@nvidia.com \
--cc=ojeda@kernel.org \
--cc=robin.murphy@arm.com \
--cc=trix@redhat.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 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).