linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] PCI: rcar: Fix of_find_matching_node() reference leak
@ 2022-06-22  2:57 Bjorn Helgaas
  2022-06-22  2:57 ` [PATCH 1/2] PCI: rcar: Add dev struct for of_device_get_match_data() Bjorn Helgaas
  2022-06-22  2:57 ` [PATCH 2/2] PCI: rcar: Resolve of_find_matching_node() reference leak Bjorn Helgaas
  0 siblings, 2 replies; 5+ messages in thread
From: Bjorn Helgaas @ 2022-06-22  2:57 UTC (permalink / raw)
  To: Marek Vasut, Yoshihiro Shimoda
  Cc: Liang He, Geert Uytterhoeven, Lorenzo Pieralisi, Rob Herring,
	Krzysztof Wilczyński, linux-pci, linux-renesas-soc,
	linux-kernel, Bjorn Helgaas

From: Bjorn Helgaas <bhelgaas@google.com>

Alternate proposal for Liang's patch [1].

This is a bulkier fix, but removes the redundant
rcar_pcie_abort_handler_of_match[] table.

It also simplifies the unusual device_initcall()/builtin_platform_driver() 
construct, which seems unnecessary as far as I can tell.

Compile-tested only.

[1] https://lore.kernel.org/r/20220621070145.4080147-1-windhl@126.com

Bjorn Helgaas (2):
  PCI: rcar: Add dev struct for of_device_get_match_data()
  PCI: rcar: Resolve of_find_matching_node() reference leak

 drivers/pci/controller/pcie-rcar-host.c | 113 +++++++++++++-----------
 1 file changed, 60 insertions(+), 53 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] PCI: rcar: Add dev struct for of_device_get_match_data()
  2022-06-22  2:57 [PATCH 0/2] PCI: rcar: Fix of_find_matching_node() reference leak Bjorn Helgaas
@ 2022-06-22  2:57 ` Bjorn Helgaas
  2022-06-22  2:57 ` [PATCH 2/2] PCI: rcar: Resolve of_find_matching_node() reference leak Bjorn Helgaas
  1 sibling, 0 replies; 5+ messages in thread
From: Bjorn Helgaas @ 2022-06-22  2:57 UTC (permalink / raw)
  To: Marek Vasut, Yoshihiro Shimoda
  Cc: Liang He, Geert Uytterhoeven, Lorenzo Pieralisi, Rob Herring,
	Krzysztof Wilczyński, linux-pci, linux-renesas-soc,
	linux-kernel, Bjorn Helgaas, Geert Uytterhoeven

From: Bjorn Helgaas <bhelgaas@google.com>

Add a struct rcar_variant to hold details about differences between the
different R-Car variants we support.  No functional change intended.

The benefit is that we can find all this information with the existing
single of_device_get_match_data() call.  A subsequent commit will remove
an of_find_matching_node() call by extending this structure.

Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: Liang He <windhl@126.com>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
---
 drivers/pci/controller/pcie-rcar-host.c | 53 +++++++++++++++----------
 1 file changed, 33 insertions(+), 20 deletions(-)

diff --git a/drivers/pci/controller/pcie-rcar-host.c b/drivers/pci/controller/pcie-rcar-host.c
index 997c4df6a1e7..ccf13aafa1e5 100644
--- a/drivers/pci/controller/pcie-rcar-host.c
+++ b/drivers/pci/controller/pcie-rcar-host.c
@@ -56,13 +56,19 @@ static void __iomem *pcie_base;
 static struct device *pcie_dev;
 #endif
 
+struct rcar_pcie_host;
+
+struct rcar_variant {
+	int	(*phy_init_fn)(struct rcar_pcie_host *host);
+};
+
 /* Structure representing the PCIe interface */
 struct rcar_pcie_host {
-	struct rcar_pcie	pcie;
-	struct phy		*phy;
-	struct clk		*bus_clk;
-	struct			rcar_msi msi;
-	int			(*phy_init_fn)(struct rcar_pcie_host *host);
+	struct rcar_pcie	  pcie;
+	struct phy		  *phy;
+	struct clk		  *bus_clk;
+	struct rcar_msi		  msi;
+	const struct rcar_variant *variant;
 };
 
 static DEFINE_SPINLOCK(pmsr_lock);
@@ -958,19 +964,26 @@ static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
 	return err;
 }
 
+static const struct rcar_variant rcar_h1_data = {
+	.phy_init_fn = rcar_pcie_phy_init_h1,
+};
+
+static const struct rcar_variant rcar_gen2_data = {
+	.phy_init_fn = rcar_pcie_phy_init_gen2,
+};
+
+static const struct rcar_variant rcar_gen3_data = {
+	.phy_init_fn = rcar_pcie_phy_init_gen3,
+};
+
 static const struct of_device_id rcar_pcie_of_match[] = {
-	{ .compatible = "renesas,pcie-r8a7779",
-	  .data = rcar_pcie_phy_init_h1 },
-	{ .compatible = "renesas,pcie-r8a7790",
-	  .data = rcar_pcie_phy_init_gen2 },
-	{ .compatible = "renesas,pcie-r8a7791",
-	  .data = rcar_pcie_phy_init_gen2 },
-	{ .compatible = "renesas,pcie-rcar-gen2",
-	  .data = rcar_pcie_phy_init_gen2 },
-	{ .compatible = "renesas,pcie-r8a7795",
-	  .data = rcar_pcie_phy_init_gen3 },
-	{ .compatible = "renesas,pcie-rcar-gen3",
-	  .data = rcar_pcie_phy_init_gen3 },
+	{ .compatible = "renesas,pcie-r8a7779",   .data = &rcar_h1_data },
+	{ .compatible = "renesas,pcie-r8a7790",   .data = &rcar_gen2_data },
+	{ .compatible = "renesas,pcie-r8a7791",   .data = &rcar_gen2_data },
+	{ .compatible = "renesas,pcie-rcar-gen2", .data = &rcar_gen2_data },
+
+	{ .compatible = "renesas,pcie-r8a7795",   .data = &rcar_gen3_data },
+	{ .compatible = "renesas,pcie-rcar-gen3", .data = &rcar_gen3_data },
 	{},
 };
 
@@ -1015,8 +1028,8 @@ static int rcar_pcie_probe(struct platform_device *pdev)
 	if (err)
 		goto err_clk_disable;
 
-	host->phy_init_fn = of_device_get_match_data(dev);
-	err = host->phy_init_fn(host);
+	host->variant = of_device_get_match_data(dev);
+	err = host->variant->phy_init_fn(host);
 	if (err) {
 		dev_err(dev, "failed to init PCIe PHY\n");
 		goto err_clk_disable;
@@ -1084,7 +1097,7 @@ static int __maybe_unused rcar_pcie_resume(struct device *dev)
 		return 0;
 
 	/* Failure to get a link might just be that no cards are inserted */
-	err = host->phy_init_fn(host);
+	err = host->variant->phy_init_fn(host);
 	if (err) {
 		dev_info(dev, "PCIe link down\n");
 		return 0;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] PCI: rcar: Resolve of_find_matching_node() reference leak
  2022-06-22  2:57 [PATCH 0/2] PCI: rcar: Fix of_find_matching_node() reference leak Bjorn Helgaas
  2022-06-22  2:57 ` [PATCH 1/2] PCI: rcar: Add dev struct for of_device_get_match_data() Bjorn Helgaas
@ 2022-06-22  2:57 ` Bjorn Helgaas
  2022-06-22  7:45   ` Geert Uytterhoeven
  1 sibling, 1 reply; 5+ messages in thread
From: Bjorn Helgaas @ 2022-06-22  2:57 UTC (permalink / raw)
  To: Marek Vasut, Yoshihiro Shimoda
  Cc: Liang He, Geert Uytterhoeven, Lorenzo Pieralisi, Rob Herring,
	Krzysztof Wilczyński, linux-pci, linux-renesas-soc,
	linux-kernel, Bjorn Helgaas, Geert Uytterhoeven

From: Bjorn Helgaas <bhelgaas@google.com>

Previously, rcar_pcie_init() used of_find_matching_node() to search the
entire device tree for compatible strings for which we need to install an
abort handler.  If we found one, we got a device_node with refcount
incremented, but we discarded the pointer and never released that
reference.

Extend the struct rcar_variant to indicate whether each variant requires an
abort handler.  Install the handler in rcar_pcie_probe() when needed.

Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: Liang He <windhl@126.com>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
---
 drivers/pci/controller/pcie-rcar-host.c | 60 +++++++++++--------------
 1 file changed, 27 insertions(+), 33 deletions(-)

diff --git a/drivers/pci/controller/pcie-rcar-host.c b/drivers/pci/controller/pcie-rcar-host.c
index ccf13aafa1e5..139a0274b9e0 100644
--- a/drivers/pci/controller/pcie-rcar-host.c
+++ b/drivers/pci/controller/pcie-rcar-host.c
@@ -60,6 +60,7 @@ struct rcar_pcie_host;
 
 struct rcar_variant {
 	int	(*phy_init_fn)(struct rcar_pcie_host *host);
+	bool	hook_aborts;
 };
 
 /* Structure representing the PCIe interface */
@@ -964,12 +965,35 @@ static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
 	return err;
 }
 
+#ifdef CONFIG_ARM
+static int rcar_pcie_aarch32_abort_handler(unsigned long addr,
+		unsigned int fsr, struct pt_regs *regs)
+{
+	return !fixup_exception(regs);
+}
+#endif
+
+static void rcar_pcie_hook_aborts(void)
+{
+#ifdef CONFIG_ARM
+#ifdef CONFIG_ARM_LPAE
+	hook_fault_code(17, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
+			"asynchronous external abort");
+#else
+	hook_fault_code(22, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
+			"imprecise external abort");
+#endif
+#endif
+}
+
 static const struct rcar_variant rcar_h1_data = {
 	.phy_init_fn = rcar_pcie_phy_init_h1,
+	.hook_aborts = true,
 };
 
 static const struct rcar_variant rcar_gen2_data = {
 	.phy_init_fn = rcar_pcie_phy_init_gen2,
+	.hook_aborts = true,
 };
 
 static const struct rcar_variant rcar_gen3_data = {
@@ -1035,6 +1059,9 @@ static int rcar_pcie_probe(struct platform_device *pdev)
 		goto err_clk_disable;
 	}
 
+	if (host->variant->hook_aborts)
+		rcar_pcie_hook_aborts();
+
 	/* Failure to get a link might just be that no cards are inserted */
 	if (rcar_pcie_hw_init(pcie)) {
 		dev_info(dev, "PCIe link down\n");
@@ -1153,37 +1180,4 @@ static struct platform_driver rcar_pcie_driver = {
 	},
 	.probe = rcar_pcie_probe,
 };
-
-#ifdef CONFIG_ARM
-static int rcar_pcie_aarch32_abort_handler(unsigned long addr,
-		unsigned int fsr, struct pt_regs *regs)
-{
-	return !fixup_exception(regs);
-}
-
-static const struct of_device_id rcar_pcie_abort_handler_of_match[] __initconst = {
-	{ .compatible = "renesas,pcie-r8a7779" },
-	{ .compatible = "renesas,pcie-r8a7790" },
-	{ .compatible = "renesas,pcie-r8a7791" },
-	{ .compatible = "renesas,pcie-rcar-gen2" },
-	{},
-};
-
-static int __init rcar_pcie_init(void)
-{
-	if (of_find_matching_node(NULL, rcar_pcie_abort_handler_of_match)) {
-#ifdef CONFIG_ARM_LPAE
-		hook_fault_code(17, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
-				"asynchronous external abort");
-#else
-		hook_fault_code(22, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
-				"imprecise external abort");
-#endif
-	}
-
-	return platform_driver_register(&rcar_pcie_driver);
-}
-device_initcall(rcar_pcie_init);
-#else
 builtin_platform_driver(rcar_pcie_driver);
-#endif
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] PCI: rcar: Resolve of_find_matching_node() reference leak
  2022-06-22  2:57 ` [PATCH 2/2] PCI: rcar: Resolve of_find_matching_node() reference leak Bjorn Helgaas
@ 2022-06-22  7:45   ` Geert Uytterhoeven
  2022-06-22 11:05     ` Bjorn Helgaas
  0 siblings, 1 reply; 5+ messages in thread
From: Geert Uytterhoeven @ 2022-06-22  7:45 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: Marek Vasut, Yoshihiro Shimoda, Liang He, Lorenzo Pieralisi,
	Rob Herring, Krzysztof Wilczyński, linux-pci, Linux-Renesas,
	Linux Kernel Mailing List, Bjorn Helgaas

Hi Bjorn,

On Wed, Jun 22, 2022 at 4:57 AM Bjorn Helgaas <helgaas@kernel.org> wrote:
> From: Bjorn Helgaas <bhelgaas@google.com>
>
> Previously, rcar_pcie_init() used of_find_matching_node() to search the
> entire device tree for compatible strings for which we need to install an
> abort handler.  If we found one, we got a device_node with refcount
> incremented, but we discarded the pointer and never released that
> reference.
>
> Extend the struct rcar_variant to indicate whether each variant requires an
> abort handler.  Install the handler in rcar_pcie_probe() when needed.
>
> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
> Cc: Liang He <windhl@126.com>
> Cc: Geert Uytterhoeven <geert@linux-m68k.org>

Thanks for your patch!

> --- a/drivers/pci/controller/pcie-rcar-host.c
> +++ b/drivers/pci/controller/pcie-rcar-host.c

> @@ -964,12 +965,35 @@ static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
>         return err;
>  }
>
> +#ifdef CONFIG_ARM
> +static int rcar_pcie_aarch32_abort_handler(unsigned long addr,
> +               unsigned int fsr, struct pt_regs *regs)
> +{
> +       return !fixup_exception(regs);
> +}
> +#endif
> +
> +static void rcar_pcie_hook_aborts(void)
> +{
> +#ifdef CONFIG_ARM
> +#ifdef CONFIG_ARM_LPAE
> +       hook_fault_code(17, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
> +                       "asynchronous external abort");
> +#else
> +       hook_fault_code(22, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
> +                       "imprecise external abort");
> +#endif
> +#endif
> +}
> +
>  static const struct rcar_variant rcar_h1_data = {
>         .phy_init_fn = rcar_pcie_phy_init_h1,
> +       .hook_aborts = true,
>  };
>
>  static const struct rcar_variant rcar_gen2_data = {
>         .phy_init_fn = rcar_pcie_phy_init_gen2,
> +       .hook_aborts = true,
>  };
>
>  static const struct rcar_variant rcar_gen3_data = {
> @@ -1035,6 +1059,9 @@ static int rcar_pcie_probe(struct platform_device *pdev)
>                 goto err_clk_disable;
>         }
>
> +       if (host->variant->hook_aborts)
> +               rcar_pcie_hook_aborts();

I was quite sure there was a good reason why this was not done in
.probe() before...

And indeed, the original submission[1] did have a comment explaining
that:

    + /*
    + * Since probe() can be deferred we need to make sure that
    + * hook_fault_code is not called after __init memory is freed
    + * by kernel and since rcar_pcie_abort_handler() is a no-op,
    + * we can install the handler here without risking it
    + * accessing some uninitialized driver state.
    + */

No idea why it was removed in v2 and later, but the point is:
hook_fault_code() is __init, so you cannot call it from a deferred
probe.
And you should have got a section mismatch warning ;-)

[1] https://lore.kernel.org/all/20200912211853.15321-1-marek.vasut@gmail.com/

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] PCI: rcar: Resolve of_find_matching_node() reference leak
  2022-06-22  7:45   ` Geert Uytterhoeven
@ 2022-06-22 11:05     ` Bjorn Helgaas
  0 siblings, 0 replies; 5+ messages in thread
From: Bjorn Helgaas @ 2022-06-22 11:05 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Marek Vasut, Yoshihiro Shimoda, Liang He, Lorenzo Pieralisi,
	Rob Herring, Krzysztof Wilczyński, linux-pci, Linux-Renesas,
	Linux Kernel Mailing List, Bjorn Helgaas

On Wed, Jun 22, 2022 at 09:45:49AM +0200, Geert Uytterhoeven wrote:
> Hi Bjorn,
> 
> On Wed, Jun 22, 2022 at 4:57 AM Bjorn Helgaas <helgaas@kernel.org> wrote:
> > From: Bjorn Helgaas <bhelgaas@google.com>
> >
> > Previously, rcar_pcie_init() used of_find_matching_node() to search the
> > entire device tree for compatible strings for which we need to install an
> > abort handler.  If we found one, we got a device_node with refcount
> > incremented, but we discarded the pointer and never released that
> > reference.
> >
> > Extend the struct rcar_variant to indicate whether each variant requires an
> > abort handler.  Install the handler in rcar_pcie_probe() when needed.
> >
> > Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
> > Cc: Liang He <windhl@126.com>
> > Cc: Geert Uytterhoeven <geert@linux-m68k.org>
> 
> Thanks for your patch!
> 
> > --- a/drivers/pci/controller/pcie-rcar-host.c
> > +++ b/drivers/pci/controller/pcie-rcar-host.c
> 
> > @@ -964,12 +965,35 @@ static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
> >         return err;
> >  }
> >
> > +#ifdef CONFIG_ARM
> > +static int rcar_pcie_aarch32_abort_handler(unsigned long addr,
> > +               unsigned int fsr, struct pt_regs *regs)
> > +{
> > +       return !fixup_exception(regs);
> > +}
> > +#endif
> > +
> > +static void rcar_pcie_hook_aborts(void)
> > +{
> > +#ifdef CONFIG_ARM
> > +#ifdef CONFIG_ARM_LPAE
> > +       hook_fault_code(17, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
> > +                       "asynchronous external abort");
> > +#else
> > +       hook_fault_code(22, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
> > +                       "imprecise external abort");
> > +#endif
> > +#endif
> > +}
> > +
> >  static const struct rcar_variant rcar_h1_data = {
> >         .phy_init_fn = rcar_pcie_phy_init_h1,
> > +       .hook_aborts = true,
> >  };
> >
> >  static const struct rcar_variant rcar_gen2_data = {
> >         .phy_init_fn = rcar_pcie_phy_init_gen2,
> > +       .hook_aborts = true,
> >  };
> >
> >  static const struct rcar_variant rcar_gen3_data = {
> > @@ -1035,6 +1059,9 @@ static int rcar_pcie_probe(struct platform_device *pdev)
> >                 goto err_clk_disable;
> >         }
> >
> > +       if (host->variant->hook_aborts)
> > +               rcar_pcie_hook_aborts();
> 
> I was quite sure there was a good reason why this was not done in
> .probe() before...
> 
> And indeed, the original submission[1] did have a comment explaining
> that:
> 
>     + /*
>     + * Since probe() can be deferred we need to make sure that
>     + * hook_fault_code is not called after __init memory is freed
>     + * by kernel and since rcar_pcie_abort_handler() is a no-op,
>     + * we can install the handler here without risking it
>     + * accessing some uninitialized driver state.
>     + */
> 
> No idea why it was removed in v2 and later, but the point is:
> hook_fault_code() is __init, so you cannot call it from a deferred
> probe.
> And you should have got a section mismatch warning ;-)

Oooh, thanks for that!  I missed the builtin_platform_driver_probe()
vs builtin_platform_driver() difference that explains why doing this
at probe-time works for pci-ixp4xx.c but not here.

Bjorn

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-06-22 11:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-22  2:57 [PATCH 0/2] PCI: rcar: Fix of_find_matching_node() reference leak Bjorn Helgaas
2022-06-22  2:57 ` [PATCH 1/2] PCI: rcar: Add dev struct for of_device_get_match_data() Bjorn Helgaas
2022-06-22  2:57 ` [PATCH 2/2] PCI: rcar: Resolve of_find_matching_node() reference leak Bjorn Helgaas
2022-06-22  7:45   ` Geert Uytterhoeven
2022-06-22 11:05     ` Bjorn Helgaas

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).