Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 07 Apr 2012 14:32:04 -0700
From:      Julian Elischer <julian@freebsd.org>
To:        Konstantin Belousov <kostikbel@gmail.com>
Cc:        current@freebsd.org, drivers@freebsd.org
Subject:   Re: device_attach(9) and driver initialization
Message-ID:  <4F80B254.1010606@freebsd.org>
In-Reply-To: <20120407125056.GS2358@deviant.kiev.zoral.com.ua>
References:  <20120407125056.GS2358@deviant.kiev.zoral.com.ua>

next in thread | previous in thread | raw e-mail | index | archive | help
On 4/7/12 5:50 AM, Konstantin Belousov wrote:
> Hello,
> there seems to be a problem with device attach sequence offered by newbus.
> Basically, when device attach method is executing, device is not fully
> initialized yet. Also the device state in the newbus part of the world
> is DS_ALIVE. There is definitely no shattering news in the statements,
> but drivers that e.g. create devfs node to communicate with consumers
> are prone to a race.
>
> If /dev node is created inside device attach method, then usermode
> can start calling cdevsw methods before device fully initialized itself.
> Even more, if device tries to use newbus helpers in cdevsw methods,
> like device_busy(9), then panic occurs "called for unatteched device".
> I get reports from users about this issues, to it is not something
> that only could happen.
Yes, we hit that (the tasting fro geom making requests as soona s we added
the devfs entries came in before we were quite set up) with the fusion-IO
driver but it was "solvable" by moving the device creation to be the very
last thing after all the setup was done. We were prepared to handle 
requests by then.

We don't seem to have hit the device_busy(etc) problem, but your solution
sounds reasonable.

> I propose to add DEVICE_AFTER_ATTACH() driver method, to be called
DEVICE_ATTACH_LATE() or DEVICE_ATTACH_DONE()
> from newbus right after device attach finished and newbus considers
> the device fully initialized. Driver then could create devfs node
> in the after_attach method instead of attach. Please see the patch below.
>
> diff --git a/sys/kern/device_if.m b/sys/kern/device_if.m
> index eb720eb..9db74e2 100644
> --- a/sys/kern/device_if.m
> +++ b/sys/kern/device_if.m
> @@ -43,6 +43,10 @@ INTERFACE device;
>   # Default implementations of some methods.
>   #
>   CODE {
> +	static void null_after_attach(device_t dev)
> +	{
> +	}
> +
>   	static int null_shutdown(device_t dev)
>   	{
>   	    return 0;
> @@ -199,6 +203,21 @@ METHOD int attach {
>   };
>
>   /**
> + * @brief Notify the driver that device is in attached state
> + *
> + * Called after driver is successfully attached to the device and
> + * corresponding device_t is fully operational. Driver now may expose
> + * the device to the consumers, e.g. create devfs nodes.
> + *
> + * @param dev		the device to probe
> + *
> + * @see DEVICE_ATTACH()
> + */
> +METHOD void after_attach {
> +	device_t dev;
> +} DEFAULT null_after_attach;
> +
> +/**
>    * @brief Detach a driver from a device.
>    *
>    * This can be called if the user is replacing the
> diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c
> index d485b9f..6d849cb 100644
> --- a/sys/kern/subr_bus.c
> +++ b/sys/kern/subr_bus.c
> @@ -2743,6 +2743,7 @@ device_attach(device_t dev)
>   	dev->state = DS_ATTACHED;
>   	dev->flags&= ~DF_DONENOMATCH;
>   	devadded(dev);
> +	DEVICE_AFTER_ATTACH(dev);
>   	return (0);
>   }
>




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4F80B254.1010606>