lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <d7b6b7de88a374a79c7ed707c74f12797b50cc58.camel@redhat.com>
Date: Wed, 19 Nov 2025 11:19:14 -0500
From: Radu Rendec <rrendec@...hat.com>
To: Marc Zyngier <maz@...nel.org>
Cc: Bjorn Helgaas <bhelgaas@...gle.com>, Manivannan Sadhasivam
 <mani@...nel.org>,  Will Deacon <will@...nel.org>, Rob Herring
 <robh@...nel.org>, Krzysztof Wilczyński	
 <kwilczynski@...nel.org>, Lorenzo Pieralisi <lpieralisi@...nel.org>, 
	linux-pci@...r.kernel.org, linux-arm-kernel@...ts.infradead.org, 
	linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/2] PCI: host-common: Do not set drvdata in
 pci_host_common_init()

On Wed, 2025-11-19 at 12:01 +0000, Marc Zyngier wrote:
> On Tue, 18 Nov 2025 22:12:43 +0000,
> Radu Rendec <rrendec@...hat.com> wrote:
> > 
> > Currently pci_host_common_init() uses the platform device's drvdata to
> > store the pointer to the allocated struct pci_host_bridge. This makes
> > sense for drivers that use pci_host_common_{probe,remove}() directly as
> > the platform probe/remove functions, but leaves no option for more
> > complex drivers to store a pointer to their own private data.
> > 
> > Change pci_host_common_init() to return the pointer to the allocated
> > struct pci_host_bridge, and move the platform_set_drvdata() call to
> > pci_host_common_probe(). This way, drivers that implement their own
> > probe function can still use pci_host_common_init() but store their own
> > pointer in the platform device's drvdata.
> > 
> > For symmetry, move the release code to a new function that takes a
> > pointer to struct pci_host_bridge, and make pci_host_common_release() a
> > wrapper to it that extracts the pointer from the platform device's
> > drvdata. This way, drivers that store their own private pointer in the
> > platform device's drvdata can still use the library release code.
> > 
> > No functional change to the existing users of pci-host-common is
> > intended, with the exception of the pcie-apple driver, which is modified
> > in a subsequent patch.
> 
> This paragraph doesn't belong here. Maybe as a note, but not in the
> commit message.

Ack. And thanks for reviewing! More comments below.

> > 
> > Signed-off-by: Radu Rendec <rrendec@...hat.com>
> > ---
> >  drivers/pci/controller/pci-host-common.c | 36 ++++++++++++++++--------
> >  drivers/pci/controller/pci-host-common.h |  6 ++--
> >  2 files changed, 29 insertions(+), 13 deletions(-)
> > 
> > diff --git a/drivers/pci/controller/pci-host-common.c b/drivers/pci/controller/pci-host-common.c
> > index 810d1c8de24e9..86002195c93ac 100644
> > --- a/drivers/pci/controller/pci-host-common.c
> > +++ b/drivers/pci/controller/pci-host-common.c
> > @@ -52,25 +52,24 @@ struct pci_config_window *pci_host_common_ecam_create(struct device *dev,
> >  }
> >  EXPORT_SYMBOL_GPL(pci_host_common_ecam_create);
> >  
> > -int pci_host_common_init(struct platform_device *pdev,
> > - const struct pci_ecam_ops *ops)
> > +struct pci_host_bridge *pci_host_common_init(struct platform_device *pdev,
> > +      const struct pci_ecam_ops *ops)
> >  {
> >   struct device *dev = &pdev->dev;
> >   struct pci_host_bridge *bridge;
> >   struct pci_config_window *cfg;
> > + int rc;
> >  
> >   bridge = devm_pci_alloc_host_bridge(dev, 0);
> >   if (!bridge)
> > - return -ENOMEM;
> > + return ERR_PTR(-ENOMEM);
> >  
> >   of_pci_check_probe_only();
> >  
> > - platform_set_drvdata(pdev, bridge);
> > -
> >   /* Parse and map our Configuration Space windows */
> >   cfg = pci_host_common_ecam_create(dev, bridge, ops);
> >   if (IS_ERR(cfg))
> > - return PTR_ERR(cfg);
> > + return (struct pci_host_bridge *)cfg;
> >  
> >   bridge->sysdata = cfg;
> >   bridge->ops = (struct pci_ops *)&ops->pci_ops;
> > @@ -78,31 +77,46 @@ int pci_host_common_init(struct platform_device *pdev,
> >   bridge->disable_device = ops->disable_device;
> >   bridge->msi_domain = true;
> >  
> > - return pci_host_probe(bridge);
> > + rc = pci_host_probe(bridge);
> > + if (rc)
> > + return ERR_PTR(rc);
> > +
> > + return bridge;
> >  }
> >  EXPORT_SYMBOL_GPL(pci_host_common_init);
> >  
> >  int pci_host_common_probe(struct platform_device *pdev)
> >  {
> >   const struct pci_ecam_ops *ops;
> > + struct pci_host_bridge *bridge;
> >  
> >   ops = of_device_get_match_data(&pdev->dev);
> >   if (!ops)
> >   return -ENODEV;
> >  
> > - return pci_host_common_init(pdev, ops);
> > + bridge = pci_host_common_init(pdev, ops);
> > + if (IS_ERR(bridge))
> > + return PTR_ERR(bridge);
> > +
> > + platform_set_drvdata(pdev, bridge);
> 
> Congratulations, you just broke pcie-microchip-host.c again.
> 
> Yes, I did that myself in afc0a570bb613871 ("PCI: host-generic:
> Extract an ECAM bridge creation helper from pci_host_common_probe()"),
> and it was fixed by Geert in bdb32a0f67806 ("PCI: host-generic: Set
> driver_data before calling gen_pci_init()").

Right. Which means there is an assumption (outside of
pci_host_common_probe() and pci_host_common_remove()) that the device's
drvdata is a pointer to the bridge. This is what I had feared but it
wasn't obvious and I failed to check thoroughly. Sorry!

> > +
> > + return 0;
> >  }
> >  EXPORT_SYMBOL_GPL(pci_host_common_probe);
> >  
> > -void pci_host_common_remove(struct platform_device *pdev)
> > +void pci_host_common_release(struct pci_host_bridge *bridge)
> >  {
> > - struct pci_host_bridge *bridge = platform_get_drvdata(pdev);
> > -
> >   pci_lock_rescan_remove();
> >   pci_stop_root_bus(bridge->bus);
> >   pci_remove_root_bus(bridge->bus);
> >   pci_unlock_rescan_remove();
> >  }
> > +EXPORT_SYMBOL_GPL(pci_host_common_release);
> 
> Even with the pcie-apple.c driver change, this is never called. I'd
> refrain from adding an export until we actually have an identified
> user.

That's fair. I just wanted to provide it for symmetry. But it's not
even needed if we change the approach (see below).

> > +
> > +void pci_host_common_remove(struct platform_device *pdev)
> > +{
> > + pci_host_common_release(platform_get_drvdata(pdev));
> > +}
> >  EXPORT_SYMBOL_GPL(pci_host_common_remove);
> >  
> >  MODULE_DESCRIPTION("Common library for PCI host controller drivers");
> > diff --git a/drivers/pci/controller/pci-host-common.h b/drivers/pci/controller/pci-host-common.h
> > index 51c35ec0cf37d..018e593bafe47 100644
> > --- a/drivers/pci/controller/pci-host-common.h
> > +++ b/drivers/pci/controller/pci-host-common.h
> > @@ -11,11 +11,13 @@
> >  #define _PCI_HOST_COMMON_H
> >  
> >  struct pci_ecam_ops;
> > +struct pci_host_bridge;
> >  
> >  int pci_host_common_probe(struct platform_device *pdev);
> > -int pci_host_common_init(struct platform_device *pdev,
> > - const struct pci_ecam_ops *ops);
> > +struct pci_host_bridge *pci_host_common_init(struct platform_device *pdev,
> > +      const struct pci_ecam_ops *ops);
> >  void pci_host_common_remove(struct platform_device *pdev);
> > +void pci_host_common_release(struct pci_host_bridge *bridge);
> >  
> >  struct pci_config_window *pci_host_common_ecam_create(struct device *dev,
> >   struct pci_host_bridge *bridge, const struct pci_ecam_ops *ops);
> 
> My concern with this is two-fold:
> 
> - it is yet another obscure API change with odd side effects,
>   requiring to track and understand the per-driver flow of information
>   (and the apple pcie driver is a prime example of how hard this is)
> 
> - we can't directly associate the PCIe port data with the bridge like
>   must drivers do, because the bridge allocation is done outside of
>   the calling driver, and there is no link back to the bridge from
>   pci_config_window.
> 
> I'd rather that last point was addressed so that we could make the
> Apple driver behave similarly to other drivers, and let it use the
> bridge private data for its PCIe port information.

Using the bridge private data to store the driver's private data was my
first thought too. In fact, in the first version of my (local) changes
I added a "size" parameter to pci_host_common_init(), to pass it down
to devm_pci_alloc_host_bridge() as the priv size.

But there's another problem with this approach: pci_host_common_init()
also calls pci_host_common_ecam_create() -> pci_ecam_create() ->
ops->init(), so init() would be called with the bridge private area
allocated but not populated.

I don't see an elegant solution to this. The two options that crossed
my mind are these:
 * Add yet another parameter to pci_host_common_init(), a void *, so it
   takes a void *priv and a size_t size. The size would be passed down
   to devm_pci_alloc_host_bridge(), then `size` bytes would be copied
   from `priv` to the bridge private data area. What I don't like about
   this is the extra memcpy() and the fact that the calling driver
   would have to prepare an "offline" copy of its private data (likely
   as a local variable) and pass it to pci_host_common_init().
 * Pass just the void *priv to pci_host_common_init(). By convention,
   the bridge private data would be used to store that void *priv
   itself, which is a pointer to the driver's private data. So, the 2nd
   parameter to devm_pci_alloc_host_bridge() would be sizeof(void *).
   The downside is that there's an extra pointer indirection.

In any case, your other concern about linking back to the bridge from
pci_config_window would be addressed because pci_config_window has a
pointer to the device (the `parent` field), which (in this scenario, by
convention) has the bridge pointer stored as drvdata.

If any of the options above looks appealing, or if you have a better
idea, please let me know, and I can prepare a v2.

-- 
Thanks,
Radu


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ