Re: [PATCH v1] driver core: Allow device link operations inside sync_state()

From: Rafael J. Wysocki
Date: Thu Nov 14 2019 - 04:15:51 EST


On Wed, Nov 13, 2019 at 3:36 AM Saravana Kannan <saravanak@xxxxxxxxxx> wrote:
>
> Some sync_state() implementations might need to call APIs that in turn
> make calls to device link APIs. So, do the sync_state() callbacks
> without holding the device link lock.
>
> Signed-off-by: Saravana Kannan <saravanak@xxxxxxxxxx>
> ---
> drivers/base/core.c | 63 +++++++++++++++++++++++++++++++++++++++------
> 1 file changed, 55 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/base/core.c b/drivers/base/core.c
> index e6d3e6d485da..d396b0597c10 100644
> --- a/drivers/base/core.c
> +++ b/drivers/base/core.c
> @@ -48,6 +48,8 @@ early_param("sysfs.deprecated", sysfs_deprecated_setup);
> static LIST_HEAD(wait_for_suppliers);
> static DEFINE_MUTEX(wfs_lock);
> static LIST_HEAD(deferred_sync);
> +static LIST_HEAD(sync_list);
> +static DEFINE_MUTEX(sync_lock);
> static unsigned int defer_sync_state_count = 1;
>
> #ifdef CONFIG_SRCU
> @@ -695,7 +697,23 @@ int device_links_check_suppliers(struct device *dev)
> return ret;
> }
>
> -static void __device_links_supplier_sync_state(struct device *dev)
> +/** __device_links_queue_sync_state - Queue a device for sync_state() callback
> + * @dev: Device to call sync_state() on
> + *
> + * Queues a device for a sync_state() callback when the device links write lock
> + * isn't held. This allows the sync_state() execution flow to use device links
> + * APIs. The caller must ensure this function is called with
> + * device_links_write_lock() held.
> + *
> + * This function does a get_device() to make sure the device is not freed while
> + * on this list.
> + *
> + * So the caller must also ensure that device_links_flush_sync_list() is called
> + * as soon as the caller releases device_links_write_lock(). This is necessary
> + * to make sure the sync_state() is called in a timely fashion and the
> + * put_device() is called on this device.
> + */
> +static void __device_links_queue_sync_state(struct device *dev)
> {
> struct device_link *link;
>
> @@ -709,12 +727,35 @@ static void __device_links_supplier_sync_state(struct device *dev)
> return;
> }
>
> - if (dev->bus->sync_state)
> - dev->bus->sync_state(dev);
> - else if (dev->driver && dev->driver->sync_state)
> - dev->driver->sync_state(dev);
> -
> dev->state_synced = true;

BTW, this should go into device_links_flush_sync_list() too, shouldn't it?