Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] drm/vkms: const discarded locking config->lock in
  vkms_connector_detect()
- [High] drm/vkms: self-deadlock on config->lock in
  connector_status_store()
- [Medium] drm/vkms: unlocked RCU list walk and UAF in
  vkms_connector_detect()
- [Medium] drm/vkms: races from dropping config->lock in
  device_enabled_store()
- [Medium] drm/vkms: RCU lockdep warning in vkms_config_for_each_*
  during vkms_create()
- [Low] drm/vkms: commit message and vkms_config_show() comment mismatch
  with code
--

commit e3fcd80e0d55e7be8d3c20f129777ead0dbe15ab
Author: Louis Chauvet <[email protected]>

drm/vkms: Lock the config dump against concurrent configfs changes

This commit moves the configuration lock into struct vkms_config, converts
the configuration lists to RCU-protected lists, and updates configfs,
debugfs, and connector detection to synchronize configuration access.

> diff --git a/drivers/gpu/drm/vkms/vkms_config.c 
> b/drivers/gpu/drm/vkms/vkms_config.c
> index f9e30a4e5a3f8..6699ddae71260 100644
> --- a/drivers/gpu/drm/vkms/vkms_config.c
> +++ b/drivers/gpu/drm/vkms/vkms_config.c
[ ... ]
> @@ -348,28 +350,38 @@ static int vkms_config_show(struct seq_file *m, void 
> *data)
>       struct vkms_config_encoder *encoder_cfg;
>       struct vkms_config_connector *connector_cfg;
>  
> -     dev_name = vkms_config_get_device_name((struct vkms_config 
> *)vkmsdev->config);
> -     seq_printf(m, "dev_name=%s\n", dev_name);
> -
> -     vkms_config_for_each_plane(vkmsdev->config, plane_cfg) {
> -             seq_puts(m, "plane:\n");
> -             seq_printf(m, "\ttype=%d\n",
> -                        vkms_config_plane_get_type(plane_cfg));
> -     }
> +     /*
> +      * The configuration lists are mutated by the configfs under the
> +      * configuration lock. Walk them under RCU instead: mutations use
> +      * list_{add,del}_rcu and free the objects after a grace period,
> +      * so this reader holds no lock and cannot block the configfs
> +      * paths (the disable path removes this file while holding the
> +      * lock, and the debugfs removal waits for this reader to drain).
> +      */
> +     scoped_guard(rcu) {

[Severity: Low]
This isn't a bug, but both the commit message and this comment appear out of
sync with the code.  The commit message says "Move the configuration lock
into struct vkms_config and run the dump under it", whereas
vkms_config_show() uses scoped_guard(rcu).  In addition, this comment says
the disable path removes the debugfs file while holding the lock, but
device_enabled_store() drops dev->config->lock before calling vkms_destroy().

> diff --git a/drivers/gpu/drm/vkms/vkms_config.h 
> b/drivers/gpu/drm/vkms/vkms_config.h
> index 56805b0c9b145..125273f67e7d7 100644
> --- a/drivers/gpu/drm/vkms/vkms_config.h
> +++ b/drivers/gpu/drm/vkms/vkms_config.h
[ ... ]
> @@ -126,7 +139,9 @@ struct vkms_config_connector {
>   * @plane_cfg: &struct vkms_config_plane pointer used as cursor
>   */
>  #define vkms_config_for_each_plane(config, plane_cfg) \
> -     list_for_each_entry((plane_cfg), &(config)->planes, link)
> +     list_for_each_entry_rcu((plane_cfg), &(config)->planes, link, \
> +                                   lockdep_is_held(&(config)->lock) || \
> +                                   !(config)->dev)

[Severity: Medium]
Does this lockdep condition trigger an RCU_LOCKDEP_WARN splat during
vkms_create() when CONFIG_PROVE_RCU_LIST=y is enabled?

In vkms_create(), config->dev is assigned before calling vkms_modeset_init():

drivers/gpu/drm/vkms/vkms_drv.c:vkms_create() {
        ...
        vkms_device->faux_dev = fdev;
        vkms_device->config = config;
        config->dev = vkms_device;
        ...
        ret = vkms_modeset_init(vkms_device);
}

Neither vkms_init() nor device_enabled_store() holds config->lock or
rcu_read_lock() across vkms_create(), so config->dev is non-NULL when
vkms_modeset_init() -> vkms_output_init() and vkms_config_is_valid() iterate
the configuration lists with 
vkms_config_for_each_{plane,crtc,encoder,connector}().

> diff --git a/drivers/gpu/drm/vkms/vkms_configfs.c 
> b/drivers/gpu/drm/vkms/vkms_configfs.c
> index af097967ca6fc..6bb24f5835db1 100644
> --- a/drivers/gpu/drm/vkms/vkms_configfs.c
> +++ b/drivers/gpu/drm/vkms/vkms_configfs.c
[ ... ]
> @@ -548,7 +546,7 @@ static ssize_t connector_status_store(struct config_item 
> *item,
>           status != connector_status_unknown)
>               return -EINVAL;
>  
> -     scoped_guard(mutex, &connector->dev->lock) {
> +     scoped_guard(mutex, &connector->dev->config->lock) {
>               vkms_config_connector_set_status(connector->config, status);
>  
>               if (connector->dev->enabled)
>                       
> vkms_trigger_connector_hotplug(connector->dev->config->dev);
>       }

[Severity: High]
Does calling vkms_trigger_connector_hotplug() while holding
connector->dev->config->lock self-deadlock on config->lock when a connector's
configfs status is written on an enabled device?

connector_status_store()
  mutex_lock(&connector->dev->config->lock)
  vkms_trigger_connector_hotplug()
    drm_kms_helper_hotplug_event()
      ...
        drm_helper_probe_single_connector_modes()
          vkms_connector_detect()
            mutex_lock(&vkmsdev->config->lock)  <-- deadlocks

This also inverts the lock order against normal DRM connector probing (such
as drm_mode_getconnector()), which acquires dev->mode_config.mutex before
calling vkms_connector_detect() -> mutex_lock(&vkmsdev->config->lock).

[ ... ]

> @@ -711,21 +709,34 @@ static ssize_t device_enabled_store(struct config_item 
> *item, const char *page,
>       if (kstrtobool(page, &enabled))
>               return -EINVAL;
>  
> -     scoped_guard(mutex, &dev->lock) {
> -             if (!dev->enabled && enabled) {
> -                     if (!vkms_config_is_valid(dev->config))
> -                             return -EINVAL;
> +     scoped_guard(mutex, &dev->config->lock) {
> +             if (dev->enabled == enabled)
> +                     return (ssize_t)count;
>  
> -                     ret = vkms_create(dev->config);
> -                     if (ret)
> -                             return ret;
> -             } else if (dev->enabled && !enabled) {
> -                     vkms_destroy(dev->config);
> -             }
> +             if (!dev->enabled && !vkms_config_is_valid(dev->config))
> +                     return -EINVAL;
>  
>               dev->enabled = enabled;
>       }
>  
> +     /*
> +      * Do not hold config->lock while creating/destroying the device:
> +      * vkms_destroy() unregisters the DRM device, which removes the
> +      * vkms_config debugfs file and waits for its readers to drain, and
> +      * vkms_connector_detect() takes config->lock to read the connector
> +      * status.
> +      */
> +     if (enabled) {
> +             ret = vkms_create(dev->config);
> +             if (ret) {
> +                     scoped_guard(mutex, &dev->config->lock)
> +                             dev->enabled = false;
> +                     return ret;
> +             }
> +     } else {
> +             vkms_destroy(dev->config);
> +     }

[Severity: Medium]
Can dropping dev->config->lock after setting dev->enabled leave vkms_create()
and vkms_destroy() open to concurrent configfs operations?

CPU0                              CPU1
device_enabled_store(1)
  mutex_lock(&config->lock)
  dev->enabled = true
  mutex_unlock(&config->lock)
                                  connector_status_store()
                                    mutex_lock(&config->lock)
                                    if (connector->dev->enabled)
                                      vkms_trigger_connector_hotplug(
                                        config->dev)
                                        <-- NULL pointer dereference
  vkms_create(dev->config)
    config->dev = vkms_device

Because config->lock is released before vkms_create() or vkms_destroy() runs:
- a second write to enabled on another file descriptor can run vkms_destroy()
  concurrently with vkms_create() (or mutate the configuration after
  dev->enabled is cleared to false while vkms_destroy() is still running);
- configfs unlink (*_drop_link) and rmdir (*_release) callbacks do not check
  dev->enabled, so they can acquire config->lock after vkms_config_is_valid()
  returns and detach or free planes, CRTCs, encoders, or connectors while
  vkms_create() -> vkms_output_init() is initializing the device.

> diff --git a/drivers/gpu/drm/vkms/vkms_connector.c 
> b/drivers/gpu/drm/vkms/vkms_connector.c
> index 57138027966f5..080a0240d5e92 100644
> --- a/drivers/gpu/drm/vkms/vkms_connector.c
> +++ b/drivers/gpu/drm/vkms/vkms_connector.c
> @@ -27,8 +27,10 @@ static enum drm_connector_status 
> vkms_connector_detect(struct drm_connector *con
>       status = connector->status;
>  
>       vkms_config_for_each_connector(vkmsdev->config, connector_cfg) {
> -             if (connector_cfg->connector == vkms_connector)
> -                     status = 
> vkms_config_connector_get_status(connector_cfg);
> +             if (connector_cfg->connector == vkms_connector) {
> +                     scoped_guard(mutex, &vkmsdev->config->lock)
> +                             status = 
> vkms_config_connector_get_status(connector_cfg);
> +             }
>       }

[Severity: High]
Does passing &vkmsdev->config->lock to scoped_guard(mutex, ...) fail to build
under CONFIG_WERROR=y?  In struct vkms_device, config is declared as
const struct vkms_config *, so &vkmsdev->config->lock discards the const
qualifier (-Werror=discarded-qualifiers).  This is rewritten in a later patch
in the series, commit "drm/vkms: Introduce back pointer to connector", but
breaks bisection at this commit.

[Severity: Medium]
Can walking vkmsdev->config->connectors here without rcu_read_lock() or
holding config->lock across the loop trigger an RCU lockdep warning and race
with connector_release()?

CPU0                              CPU1
vkms_connector_detect()
  vkms_config_for_each_connector()
    if (connector_cfg->connector == ...)
                                  connector_release()
                                    mutex_lock(&config->lock)
                                    vkms_config_destroy_connector()
                                      list_del_rcu(&connector_cfg->link)
                                      kfree_rcu(connector_cfg, rcu)
                                    mutex_unlock(&config->lock)
                                  /* RCU grace period elapses */
      mutex_lock(&config->lock)
      vkms_config_connector_get_status(
        connector_cfg)            <-- use-after-free

Because vkmsdev->config->dev is non-NULL and neither rcu_read_lock() nor
config->lock is held across vkms_config_for_each_connector(), every call to
vkms_connector_detect() triggers RCU_LOCKDEP_WARN under
CONFIG_PROVE_RCU_LIST=y, and connector_cfg can be freed while CPU0 is
preempted or blocked on config->lock.  Later patches in the series (commit
"drm/vkms: Reference-count vkms_config and vkms_config_connector" and commit
"drm/vkms: Introduce back pointer to connector") replace this traversal with
a reference-counted back-pointer.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to