mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: mingming cao <mmc@linux.ibm.com>
To: Johan Hovold <johan@kernel.org>,
	Geoff Levand <geoff@infradead.org>,
	Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>,
	Christophe Leroy <chleroy@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 RESEND 2/3] powerpc/pseries: switch to dynamic ibmebus root device
Date: Mon, 12 Oct 2026 14:10:33 -0700	[thread overview]
Message-ID: <85244b39-017b-4978-8a4e-ff8e8dc89ffb@linux.ibm.com> (raw)
In-Reply-To: <20260812142016.55136-3-johan@kernel.org>

On 8/12/26 7:20 AM, Johan Hovold wrote:
> Driver core expects devices to be dynamically allocated and will, for
> example, complain loudly if a device that lacks a release function is
> ever freed.
> 
> Use root_device_register() to allocate and register the root device
> instead of open coding using a static device.
> 
> Also add the missing sanity check when registering ibmebus devices to
> avoid use-after-free if the bus failed to register (which would
> previously have triggered a bunch of use-after-free warnings).
> 
> Signed-off-by: Johan Hovold <johan@kernel.org>
> ---
>   arch/powerpc/platforms/pseries/ibmebus.c | 39 ++++++++++++++----------
>   1 file changed, 23 insertions(+), 16 deletions(-)
> 
> diff --git a/arch/powerpc/platforms/pseries/ibmebus.c b/arch/powerpc/platforms/pseries/ibmebus.c
> index 2d0f991da2c8..48076fe6ab30 100644
> --- a/arch/powerpc/platforms/pseries/ibmebus.c
> +++ b/arch/powerpc/platforms/pseries/ibmebus.c
> @@ -52,9 +52,7 @@
>   #include <asm/ibmebus.h>
>   #include <asm/machdep.h>
>   
> -static struct device ibmebus_bus_device = { /* fake "parent" device */
> -	.init_name = "ibmebus",
> -};
> +static struct device *ibmebus_bus_device;	/* fake "parent" device */
>   
>   const struct bus_type ibmebus_bus_type;
>   
> @@ -172,7 +170,10 @@ static int ibmebus_create_device(struct device_node *dn)
>   	struct platform_device *dev;
>   	int ret;
>   
> -	dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
> +	if (!ibmebus_bus_device)
> +		return -ENOENT;
> +
> +	dev = of_device_alloc(dn, NULL, ibmebus_bus_device);
>   	if (!dev)
>   		return -ENOMEM;
>   
> @@ -448,6 +449,7 @@ EXPORT_SYMBOL(ibmebus_bus_type);
>   
>   static int __init ibmebus_bus_init(void)
>   {
> +	struct device *root;
>   	int err;
>   
>   	printk(KERN_INFO "IBM eBus Device Driver\n");
> @@ -459,23 +461,28 @@ static int __init ibmebus_bus_init(void)
>   		return err;
>   	}
>   
> -	err = device_register(&ibmebus_bus_device);
> -	if (err) {
> -		printk(KERN_WARNING "%s: device_register returned %i\n",
> +	root = root_device_register("ibmebus");
> +	if (IS_ERR(root)) {
> +		err = PTR_ERR(root);
> +		printk(KERN_WARNING "%s: root_device_register returned %i\n",
>   		       __func__, err);
> -		put_device(&ibmebus_bus_device);
> -		bus_unregister(&ibmebus_bus_type);
> -
> -		return err;
> +		goto err_deregister_bus;
>   	}
>   
> +	ibmebus_bus_device = root;
> +
>   	err = ibmebus_create_devices(ibmebus_matches);
> -	if (err) {
> -		device_unregister(&ibmebus_bus_device);
> -		bus_unregister(&ibmebus_bus_type);
> -		return err;
> -	}
> +	if (err)
> +		goto err_deregister_root;
>   
>   	return 0;
> +
> +err_deregister_root:
> +	ibmebus_bus_device = NULL;
> +	root_device_unregister(root);
> +err_deregister_bus:
> +	bus_unregister(&ibmebus_bus_type);
> +
> +	return err;
>   }
>   machine_postcore_initcall(pseries, ibmebus_bus_init);

I went through the init error paths and they look right: the global is
only set once the root device exists and is cleared before the root is
unregistered, so the new NULL check in ibmebus_create_device() stays
valid after a failed init.

Reviewed-by: Mingming Cao <mmc@linux.ibm.com>

  reply	other threads:[~2026-10-12 21:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-12 14:20 [PATCH v2 RESEND 0/3] powerpc: switch to dynamic root devices Johan Hovold
2026-08-12 14:20 ` [PATCH v2 RESEND 1/3] powerpc/ps3: switch to dynamic system bus root device Johan Hovold
2026-08-12 14:20 ` [PATCH v2 RESEND 2/3] powerpc/pseries: switch to dynamic ibmebus " Johan Hovold
2026-10-12 21:10   ` mingming cao [this message]
2026-08-12 14:20 ` [PATCH v2 RESEND 3/3] powerpc/pseries: clean up ibmebus printks Johan Hovold

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=85244b39-017b-4978-8a4e-ff8e8dc89ffb@linux.ibm.com \
    --to=mmc@linux.ibm.com \
    --cc=chleroy@kernel.org \
    --cc=geoff@infradead.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=npiggin@gmail.com \
    /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

all inboxes | Powered by JetHome®