From: Hector Martin <marcan@marcan.st>
To: Jason Gunthorpe <jgg@nvidia.com>,
acpica-devel@lists.linux.dev,
Alyssa Rosenzweig <alyssa@rosenzweig.io>,
Albert Ou <aou@eecs.berkeley.edu>,
asahi@lists.linux.dev, Catalin Marinas <catalin.marinas@arm.com>,
Dexuan Cui <decui@microsoft.com>,
devicetree@vger.kernel.org, David Woodhouse <dwmw2@infradead.org>,
Frank Rowand <frowand.list@gmail.com>,
Hanjun Guo <guohanjun@huawei.com>,
Haiyang Zhang <haiyangz@microsoft.com>,
iommu@lists.linux.dev,
Jean-Philippe Brucker <jean-philippe@linaro.org>,
Jonathan Hunter <jonathanh@nvidia.com>,
Joerg Roedel <joro@8bytes.org>,
"K. Y. Srinivasan" <kys@microsoft.com>,
Len Brown <lenb@kernel.org>,
linux-acpi@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-hyperv@vger.kernel.org, linux-mips@vger.kernel.org,
linux-riscv@lists.infradead.org,
linux-snps-arc@lists.infradead.org, linux-tegra@vger.kernel.org,
Russell King <linux@armlinux.org.uk>,
Lorenzo Pieralisi <lpieralisi@kernel.org>,
Marek Szyprowski <m.szyprowski@samsung.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
patches@lists.linux.dev, Paul Walmsley <paul.walmsley@sifive.com>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Robert Moore <robert.moore@intel.com>,
Rob Herring <robh+dt@kernel.org>,
Robin Murphy <robin.murphy@arm.com>,
Sudeep Holla <sudeep.holla@arm.com>,
Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>,
Sven Peter <sven@svenpeter.dev>,
Thierry Reding <thierry.reding@gmail.com>,
Thomas Bogendoerfer <tsbogend@alpha.franken.de>,
Krishna Reddy <vdumpa@nvidia.com>,
Vineet Gupta <vgupta@kernel.org>,
virtualization@lists.linux.dev, Wei Liu <wei.liu@kernel.org>,
Will Deacon <will@kernel.org>
Cc: "André Draszik" <andre.draszik@linaro.org>,
"Lu Baolu" <baolu.lu@linux.intel.com>,
"Christoph Hellwig" <hch@lst.de>,
"Jerry Snitselaar" <jsnitsel@redhat.com>,
"Moritz Fischer" <mdf@kernel.org>,
"Zhenhua Huang" <quic_zhenhuah@quicinc.com>,
"Rafael J. Wysocki" <rafael.j.wysocki@intel.com>,
"Rob Herring" <robh@kernel.org>
Subject: Re: [PATCH v2 06/17] iommu: Add iommu_fwspec_alloc/dealloc()
Date: Sun, 19 Nov 2023 17:10:30 +0900 [thread overview]
Message-ID: <20a7ef6d-a8ca-4bd8-ad7e-11856db617a2@marcan.st> (raw)
In-Reply-To: <6-v2-36a0088ecaa7+22c6e-iommu_fwspec_jgg@nvidia.com>
On 2023/11/15 23:05, Jason Gunthorpe wrote:
> Allow fwspec to exist independently from the dev->iommu by providing
> functions to allow allocating and freeing the raw struct iommu_fwspec.
>
> Reflow the existing paths to call the new alloc/dealloc functions.
>
> Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
> drivers/iommu/iommu.c | 82 ++++++++++++++++++++++++++++++++-----------
> include/linux/iommu.h | 11 +++++-
> 2 files changed, 72 insertions(+), 21 deletions(-)
>
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index 18a82a20934d53..86bbb9e75c7e03 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -361,10 +361,8 @@ static void dev_iommu_free(struct device *dev)
> struct dev_iommu *param = dev->iommu;
>
> dev->iommu = NULL;
> - if (param->fwspec) {
> - fwnode_handle_put(param->fwspec->iommu_fwnode);
> - kfree(param->fwspec);
> - }
> + if (param->fwspec)
> + iommu_fwspec_dealloc(param->fwspec);
> kfree(param);
> }
>
> @@ -2920,10 +2918,61 @@ const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
> return ops;
> }
>
> +static int iommu_fwspec_assign_iommu(struct iommu_fwspec *fwspec,
> + struct device *dev,
> + struct fwnode_handle *iommu_fwnode)
> +{
> + const struct iommu_ops *ops;
> +
> + if (fwspec->iommu_fwnode) {
> + /*
> + * fwspec->iommu_fwnode is the first iommu's fwnode. In the rare
> + * case of multiple iommus for one device they must point to the
> + * same driver, checked via same ops.
> + */
> + ops = iommu_ops_from_fwnode(iommu_fwnode);
This carries over a related bug from the original code: If a device has
two IOMMUs and the first one probes but the second one defers, ops will
be NULL here and the check will fail with EINVAL.
Adding a check for that case here fixes it:
if (!ops)
return driver_deferred_probe_check_state(dev);
With that, for the whole series:
Tested-by: Hector Martin <marcan@marcan.st>
I can't specifically test for the probe races the series intends to fix
though, since that bug we only hit extremely rarely. I'm just testing
that nothing breaks.
> + if (fwspec->ops != ops)
> + return -EINVAL;
> + return 0;
> + }
> +
> + if (!fwspec->ops) {
> + ops = iommu_ops_from_fwnode(iommu_fwnode);
> + if (!ops)
> + return driver_deferred_probe_check_state(dev);
> + fwspec->ops = ops;
> + }
> +
> + of_node_get(to_of_node(iommu_fwnode));
> + fwspec->iommu_fwnode = iommu_fwnode;
> + return 0;
> +}
> +
> +struct iommu_fwspec *iommu_fwspec_alloc(void)
> +{
> + struct iommu_fwspec *fwspec;
> +
> + fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
> + if (!fwspec)
> + return ERR_PTR(-ENOMEM);
> + return fwspec;
> +}
> +
> +void iommu_fwspec_dealloc(struct iommu_fwspec *fwspec)
> +{
> + if (!fwspec)
> + return;
> +
> + if (fwspec->iommu_fwnode)
> + fwnode_handle_put(fwspec->iommu_fwnode);
> + kfree(fwspec);
> +}
> +
> int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
> const struct iommu_ops *ops)
> {
> struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
> + int ret;
>
> if (fwspec)
> return ops == fwspec->ops ? 0 : -EINVAL;
> @@ -2931,29 +2980,22 @@ int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
> if (!dev_iommu_get(dev))
> return -ENOMEM;
>
> - fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
> - if (!fwspec)
> - return -ENOMEM;
> + fwspec = iommu_fwspec_alloc();
> + if (IS_ERR(fwspec))
> + return PTR_ERR(fwspec);
>
> - of_node_get(to_of_node(iommu_fwnode));
> - fwspec->iommu_fwnode = iommu_fwnode;
> fwspec->ops = ops;
> + ret = iommu_fwspec_assign_iommu(fwspec, dev, iommu_fwnode);
> + if (ret) {
> + iommu_fwspec_dealloc(fwspec);
> + return ret;
> + }
> +
> dev_iommu_fwspec_set(dev, fwspec);
> return 0;
> }
> EXPORT_SYMBOL_GPL(iommu_fwspec_init);
>
> -void iommu_fwspec_free(struct device *dev)
> -{
> - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
> -
> - if (fwspec) {
> - fwnode_handle_put(fwspec->iommu_fwnode);
> - kfree(fwspec);
> - dev_iommu_fwspec_set(dev, NULL);
> - }
> -}
> -EXPORT_SYMBOL_GPL(iommu_fwspec_free);
>
> int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
> {
> diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> index e98a4ca8f536b7..c7c68cb59aa4dc 100644
> --- a/include/linux/iommu.h
> +++ b/include/linux/iommu.h
> @@ -813,9 +813,18 @@ struct iommu_sva {
> struct iommu_domain *domain;
> };
>
> +struct iommu_fwspec *iommu_fwspec_alloc(void);
> +void iommu_fwspec_dealloc(struct iommu_fwspec *fwspec);
> +
> int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
> const struct iommu_ops *ops);
> -void iommu_fwspec_free(struct device *dev);
> +static inline void iommu_fwspec_free(struct device *dev)
> +{
> + if (!dev->iommu)
> + return;
> + iommu_fwspec_dealloc(dev->iommu->fwspec);
> + dev->iommu->fwspec = NULL;
> +}
> int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids);
> const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode);
>
- Hector
next prev parent reply other threads:[~2023-11-19 8:10 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-15 14:05 [PATCH v2 00/17] Solve iommu probe races around iommu_fwspec Jason Gunthorpe
2023-11-15 14:05 ` [PATCH v2 01/17] iommu: Remove struct iommu_ops *iommu from arch_setup_dma_ops() Jason Gunthorpe
2023-11-15 14:05 ` [PATCH v2 02/17] iommmu/of: Do not return struct iommu_ops from of_iommu_configure() Jason Gunthorpe
2023-11-15 14:05 ` [PATCH v2 03/17] iommu/of: Use -ENODEV consistently in of_iommu_configure() Jason Gunthorpe
2023-11-15 14:42 ` Jerry Snitselaar
2023-11-15 14:05 ` [PATCH v2 04/17] acpi: Do not return struct iommu_ops from acpi_iommu_configure_id() Jason Gunthorpe
2023-11-15 14:45 ` Jerry Snitselaar
2023-11-15 14:05 ` [PATCH v2 05/17] iommu: Make iommu_fwspec->ids a distinct allocation Jason Gunthorpe
2023-11-15 14:05 ` [PATCH v2 06/17] iommu: Add iommu_fwspec_alloc/dealloc() Jason Gunthorpe
2023-11-19 8:10 ` Hector Martin [this message]
2023-11-19 9:19 ` Hector Martin
2023-11-19 14:13 ` Jason Gunthorpe
2023-11-21 6:47 ` Hector Martin
2023-11-21 16:00 ` Jason Gunthorpe
2023-11-23 9:08 ` Hector Martin
2023-11-15 14:05 ` [PATCH v2 07/17] iommu: Add iommu_probe_device_fwspec() Jason Gunthorpe
2023-11-15 14:05 ` [PATCH v2 08/17] iommu/of: Do not use dev->iommu within of_iommu_configure() Jason Gunthorpe
2023-11-15 14:06 ` [PATCH v2 09/17] iommu: Add iommu_fwspec_append_ids() Jason Gunthorpe
2023-11-15 14:06 ` [PATCH v2 10/17] acpi: Do not use dev->iommu within acpi_iommu_configure() Jason Gunthorpe
2023-11-15 14:06 ` [PATCH v2 11/17] iommu: Hold iommu_probe_device_lock while calling ops->of_xlate Jason Gunthorpe
2023-11-15 14:06 ` [PATCH v2 12/17] iommu: Make iommu_ops_from_fwnode() static Jason Gunthorpe
2023-11-15 15:09 ` Jerry Snitselaar
2023-11-16 14:36 ` Moritz Fischer
2023-11-15 14:06 ` [PATCH v2 13/17] iommu: Remove dev_iommu_fwspec_set() Jason Gunthorpe
2023-11-15 14:06 ` [PATCH v2 14/17] iommu: Remove pointless iommu_fwspec_free() Jason Gunthorpe
2023-11-15 14:06 ` [PATCH v2 15/17] iommu: Add ops->of_xlate_fwspec() Jason Gunthorpe
2023-11-15 14:06 ` [PATCH v2 16/17] iommu: Mark dev_iommu_get() with lockdep Jason Gunthorpe
2023-11-15 14:06 ` [PATCH v2 17/17] iommu: Mark dev_iommu_priv_set() with a lockdep Jason Gunthorpe
2023-11-15 14:54 ` [PATCH v2 00/17] Solve iommu probe races around iommu_fwspec Jerry Snitselaar
2023-11-15 15:22 ` Robin Murphy
2023-11-15 15:36 ` Jason Gunthorpe
2023-11-15 20:23 ` Robin Murphy
2023-11-16 4:17 ` Jason Gunthorpe
2023-11-21 16:06 ` Robin Murphy
2023-11-21 17:55 ` 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=20a7ef6d-a8ca-4bd8-ad7e-11856db617a2@marcan.st \
--to=marcan@marcan.st \
--cc=acpica-devel@lists.linux.dev \
--cc=alyssa@rosenzweig.io \
--cc=andre.draszik@linaro.org \
--cc=aou@eecs.berkeley.edu \
--cc=asahi@lists.linux.dev \
--cc=baolu.lu@linux.intel.com \
--cc=catalin.marinas@arm.com \
--cc=decui@microsoft.com \
--cc=devicetree@vger.kernel.org \
--cc=dwmw2@infradead.org \
--cc=frowand.list@gmail.com \
--cc=guohanjun@huawei.com \
--cc=haiyangz@microsoft.com \
--cc=hch@lst.de \
--cc=iommu@lists.linux.dev \
--cc=jean-philippe@linaro.org \
--cc=jgg@nvidia.com \
--cc=jonathanh@nvidia.com \
--cc=joro@8bytes.org \
--cc=jsnitsel@redhat.com \
--cc=kys@microsoft.com \
--cc=lenb@kernel.org \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux-snps-arc@lists.infradead.org \
--cc=linux-tegra@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=lpieralisi@kernel.org \
--cc=m.szyprowski@samsung.com \
--cc=mdf@kernel.org \
--cc=palmer@dabbelt.com \
--cc=patches@lists.linux.dev \
--cc=paul.walmsley@sifive.com \
--cc=quic_zhenhuah@quicinc.com \
--cc=rafael.j.wysocki@intel.com \
--cc=rafael@kernel.org \
--cc=robert.moore@intel.com \
--cc=robh+dt@kernel.org \
--cc=robh@kernel.org \
--cc=robin.murphy@arm.com \
--cc=sudeep.holla@arm.com \
--cc=suravee.suthikulpanit@amd.com \
--cc=sven@svenpeter.dev \
--cc=thierry.reding@gmail.com \
--cc=tsbogend@alpha.franken.de \
--cc=vdumpa@nvidia.com \
--cc=vgupta@kernel.org \
--cc=virtualization@lists.linux.dev \
--cc=wei.liu@kernel.org \
--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).