Linux IOMMU Development
 help / color / mirror / Atom feed
* Re: [PATCH] drm/tegra: Remove of_dma_configure() from host1x_device_add()
       [not found]               ` <20240202161540.GU1455070@nvidia.com>
@ 2024-02-07 13:05                 ` Jason Gunthorpe
  0 siblings, 0 replies; only message in thread
From: Jason Gunthorpe @ 2024-02-07 13:05 UTC (permalink / raw)
  To: Jon Hunter
  Cc: David Airlie, Daniel Vetter, dri-devel, linux-tegra, iommu,
	Mikko Perttunen, Thierry Reding, Diogo Ivo, patches

On Fri, Feb 02, 2024 at 12:15:40PM -0400, Jason Gunthorpe wrote:

> > Yes looks like a race of some sort. Adding a bit of debug also makes the
> > issue go away so difficult to see what is happening.
> 
> I'm wondering if it is racing with iommu driver probing? I looked but
> didn't notice anything obviously wrong there that would cause this
> though.

Oh there is at least one racy thing here..

The of_xlate hackjob is out of order and is racy if you have multiple instances:

struct tegra_smmu *tegra_smmu_probe(struct device *dev,
				    const struct tegra_smmu_soc *soc,
				    struct tegra_mc *mc)
{
	/*
	 * This is a bit of a hack. Ideally we'd want to simply return this
	 * value. However iommu_device_register() will attempt to add
	 * all devices to the IOMMU before we get that far. In order
	 * not to rely on global variables to track the IOMMU instance, we
	 * set it here so that it can be looked up from the .probe_device()
	 * callback via the IOMMU device's .drvdata field.
	 */
	mc->smmu = smmu;
         ^^^^^^^^^^^^^
           After this of_xlate and probe_device will succeed

        [...]
       	err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));               
	err = iommu_device_register(&smmu->iommu, &tegra_smmu_ops, dev);
        ^^^^^^^^^
        But the iommu instance is not fully initialized yet.

So:

static int tegra_smmu_of_xlate(struct device *dev,
			       struct of_phandle_args *args)
{
	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);

	dev_iommu_priv_set(dev, mc->smmu);
                            ^^^^^^^^^^^^
                             Gets the partially initialized iommu
			     instance


static struct iommu_device *tegra_smmu_probe_device(struct device *dev)
{
	smmu = dev_iommu_priv_get(dev);
	if (!smmu)
		return ERR_PTR(-ENODEV);
               ^^^^^^^^^^^^^
                  Allows the driver to bind to a partially setup instance

ie if you have multiple instances of tegra-smmu and you manage to do
concurrent probe where the iommu instance is probing concurrently with
the failing device_add flow then you can a situation like you have
described where the sysfs is not fully setup.

Is this making sense to you? Add a sleep after the mc->smmu store and
confirm with printing?

I think this is all an insane design. I fixed this race and removed
all this hackery in my fwspec removal series. There the iommu instance
only ever comes out of the locked list that iommu_device_register()
populates and drivers have a safe and simple flow.

Maybe just moving the store to mc->smmu later would improve it? I
didn't look closely..

Jason

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-02-07 13:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <0-v1-c76c50cd425e+15298-host1x_no_iommu_conf_jgg@nvidia.com>
     [not found] ` <583b9145-cbbe-4a03-8120-e2a66a6093c7@nvidia.com>
     [not found]   ` <20240131153312.GM1455070@nvidia.com>
     [not found]     ` <a1597113-3ec9-445c-90d1-62df97406fb2@nvidia.com>
     [not found]       ` <20240201200212.GQ1455070@nvidia.com>
     [not found]         ` <96bc6d37-1000-4651-9a26-a8446dd64803@nvidia.com>
     [not found]           ` <20240202143518.GS1455070@nvidia.com>
     [not found]             ` <361dcaf5-352e-4162-a952-c690783a2251@nvidia.com>
     [not found]               ` <20240202161540.GU1455070@nvidia.com>
2024-02-07 13:05                 ` [PATCH] drm/tegra: Remove of_dma_configure() from host1x_device_add() Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox