Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] drm/vkms: lock ordering inversion in connector_enabled_store()
  and release
- [High] drm/vkms: hot-attach before validation in
  connector_possible_encoders_allow_link()
- [High] drm/vkms: vkms_config_is_valid() in connector_enabled_store()
  blocks disable/hot-add
- [Medium] drm/vkms: static connector with enabled=0 bypasses -EBUSY
  check on encoder link
- [Medium] drm/vkms: missing connector_is_enabled() check on EDID store
  hotplug trigger
- [Medium] drm/vkms: race and dangling connector pointer between device
  enable and dynamic configfs
--

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

drm/vkms: Introduce configfs for dynamic connector creation

This commit adds dynamic and enabled configfs attributes to VKMS
connectors to support creating, removing, and attaching encoders to
dynamic connectors at runtime.

> diff --git a/drivers/gpu/drm/vkms/vkms_configfs.c 
> b/drivers/gpu/drm/vkms/vkms_configfs.c
> index 1822978879ff3..0681722e98e8d 100644
> --- a/drivers/gpu/drm/vkms/vkms_configfs.c
> +++ b/drivers/gpu/drm/vkms/vkms_configfs.c
[ ... ]
> @@ -1281,6 +1287,106 @@ static ssize_t connector_edid_store(struct 
> config_item *item,
>                   connector_status_disconnected)
>                       
> vkms_trigger_connector_hotplug(connector->dev->config->dev);
>       }

[Severity: Medium]
Should connector_edid_store() and connector_edid_enabled_store() also
check connector_is_enabled() before calling
vkms_trigger_connector_hotplug(), as connector_status_store() does?

Because a newly created dynamic connector defaults to
connector_status_connected, configuring its EDID attributes while it is
still disabled triggers a hotplug event on the device before the connector
has been hot-added.

> +     return count;
> +}
> +
> +static ssize_t connector_enabled_show(struct config_item *item, char *page)
> +{
> +     struct vkms_configfs_connector *connector;
> +     bool enabled;
> +
> +     connector = connector_item_to_vkms_configfs_connector(item);
> +
> +     scoped_guard(mutex, &connector->dev->config->lock)
> +             enabled = vkms_config_connector_is_enabled(connector->config);
> +
> +     return sprintf(page, "%d\n", enabled);
> +}
> +
> +static ssize_t connector_enabled_store(struct config_item *item,
> +                                    const char *page, size_t count)
> +{
> +     struct vkms_configfs_connector *connector;
> +     struct vkms_config_connector *connector_cfg;
> +     bool enabled, was_enabled;
> +
> +     connector = connector_item_to_vkms_configfs_connector(item);
> +     connector_cfg = connector->config;
> +
> +     if (kstrtobool(page, &enabled))
> +             return -EINVAL;
> +     scoped_guard(mutex, &connector->dev->config->lock) {
> +             if (!connector->dev->enabled) {
> +                     vkms_config_connector_set_enabled(connector_cfg, 
> enabled);
> +             } else {
> +                     // Only dynamic connector can be enabled/disabled at 
> runtime
> +                     if (!connector_cfg->dynamic)
> +                             return -EBUSY;
> +
> +                     was_enabled = 
> vkms_config_connector_is_enabled(connector_cfg);
> +                     vkms_config_connector_set_enabled(connector_cfg, 
> enabled);
> +
> +                     // Resulting configuration is invalid (missing encoder 
> for example)
> +                     // Early return to avoid drm core issue
> +                     if (!vkms_config_is_valid(connector->dev->config)) {
> +                             vkms_config_connector_set_enabled(connector_cfg,
> +                                                               was_enabled);
> +                             return -EINVAL;
> +                     }

[Severity: High]
Does running vkms_config_is_valid() across the whole device here prevent
enabling or disabling a dynamic connector whenever any disabled dynamic
connector has no encoder linked?

valid_connector_possible_encoders() requires every connector in
config->connectors to have at least one possible encoder, regardless of
whether it is enabled:

drivers/gpu/drm/vkms/vkms_config.c:valid_connector_possible_encoders() {
        ...
        vkms_config_for_each_connector(config, connector_cfg) {
                if (xa_empty(&connector_cfg->possible_encoders)) {
                        drm_info(dev,
                                 "All connectors must have at least one 
possible encoder\n");
                        return false;
                }
        }
        ...
}

As a result, creating a new connector directory while the device is
enabled (which starts with dynamic=true, enabled=false and an empty
possible_encoders xarray) causes vkms_config_is_valid() to fail for all
connectors until that disabled connector is also given an encoder.
Similarly, if a user unlinks the encoder from a dynamic connector and then
writes 0 to enabled to remove it, connector_enabled_store() rejects the
disable with -EINVAL.

> +
> +                     if (!was_enabled && enabled) {
> +                             struct vkms_connector *conn;
> +
> +                             // Adding the connector
> +                             conn = 
> vkms_connector_hot_add(connector->dev->config->dev,
> +                                                           connector_cfg);
> +                             if (IS_ERR(conn)) {
> +                                     
> vkms_config_connector_set_enabled(connector_cfg, was_enabled);
> +                                     return PTR_ERR(conn);
> +                             }
> +                             connector_cfg->connector = conn;
> +                     } else if (was_enabled && !enabled) {
> +                             
> vkms_connector_hot_remove(connector_cfg->connector);
> +                     }
> +             }
> +     }
> +     return count;
> +}

[Severity: High]
Can calling vkms_connector_hot_add() and vkms_connector_hot_remove() under
connector->dev->config->lock in connector_enabled_store() (and in
connector_release() below) cause an ABBA deadlock with connector probing?

Both vkms_connector_hot_add() and vkms_connector_hot_remove() acquire
mode_config.mutex while config->lock is held.  During connector probing,
the DRM core holds mode_config.mutex and calls vkms_connector_detect(),
which takes config->lock:

CPU0                              CPU1
----                              ----
lock(&config->lock)
                                  lock(&dev->mode_config.mutex)
lock(&dev->mode_config.mutex)
                                  lock(&config->lock)

 *** DEADLOCK ***

CPU0: connector_enabled_store() -> vkms_connector_hot_add()
CPU1: drm_mode_getconnector() -> ... -> vkms_connector_detect()

[Severity: Medium]
Can connector_enabled_store(), connector_release(), or
connector_possible_encoders_allow_link() race with device_enabled_store()
and dereference an uninitialized or stale connector_cfg->connector or
config->dev pointer?

device_enabled_store() sets dev->enabled = true under config->lock and
then drops the lock before calling vkms_create().  In addition, when a
device is disabled, vkms_connector_dynamic_destroy() frees the dynamic
vkms_connector without clearing connector_cfg->connector.

CPU0                              CPU1
device_enabled_store()
  lock(&config->lock)
  dev->enabled = true
  unlock(&config->lock)
                                  connector_enabled_store()
                                    lock(&config->lock)
                                    if (connector->dev->enabled)
                                      vkms_connector_hot_remove(
                                        connector_cfg->connector)
                                        <-- NULL or use-after-free
  vkms_create()
    ...
    connector_cfg->connector = ...

[ ... ]

> @@ -1335,14 +1452,24 @@ static int 
> connector_possible_encoders_allow_link(struct config_item *src,
>               return -EINVAL;
>  
>       connector = 
> connector_possible_encoders_item_to_vkms_configfs_connector(src);
> +     connector_cfg = connector->config;
>       encoder = encoder_item_to_vkms_configfs_encoder(target);
>  
>       scoped_guard(mutex, &connector->dev->config->lock) {
> -             if (connector->dev->enabled)
> -                     return -EBUSY;
> +             if (connector->dev->enabled && connector_cfg->enabled) {
> +                     if (!connector_cfg->dynamic)
> +                             return -EBUSY;
> +
> +                     ret = 
> vkms_connector_hot_attach_encoder(connector->config->connector,
> +                                                             
> encoder->config->encoder);
> +                     if (ret)
> +                             return ret;
> +             }
>  
>               ret = vkms_config_connector_attach_encoder(connector->config,
>                                                          encoder->config);
> +             if (ret)
> +                     return ret;
>       }
>  
>       return ret;

[Severity: High]
Can calling vkms_connector_hot_attach_encoder() before
vkms_config_connector_attach_encoder() cause a NULL pointer dereference
when linking an encoder from another disabled VKMS device?

vkms_config_connector_attach_encoder() is what checks
connector_cfg->config != encoder_cfg->config and checks for duplicate or
failed xarray insertions.  Because vkms_connector_hot_attach_encoder()
runs first, linking an encoder that belongs to a different, disabled VKMS
device passes a NULL encoder->config->encoder to
drm_connector_attach_encoder() -> drm_encoder_mask().  Even for an encoder
on the same device, if vkms_config_connector_attach_encoder() fails with
-EEXIST or -ENOMEM, the encoder bit added to the live DRM connector by
vkms_connector_hot_attach_encoder() is not rolled back.

[Severity: Medium]
Should connector_possible_encoders_allow_link() check
connector_is_enabled(connector_cfg) instead of connector_cfg->enabled?

While the device is disabled, connector_enabled_store() allows setting
connector_cfg->enabled = false on a static connector.  When the device is
subsequently enabled, vkms_output_init() still registers all static
connectors, so checking connector_cfg->enabled here skips the -EBUSY check
and allows mutating connector_cfg->possible_encoders on a live static
connector.

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

Reply via email to