Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/vkms: sleeping in RCU read-side critical section in
  vkms_conn_get_modes()
- [Medium] drm/vkms: pointer signedness mismatch in
  vkms_config_connector_get_edid()
- [Low] drm/vkms: direct __rcu pointer dereference in
  vkms_config_connector_release()
- [Low] drm/vkms: inaccurate kernel-doc for edid_enabled in
  vkms_config_connector
--

commit 2562fe9d046d1f1711056464ad1f96e4ee4649be
Author: Louis Chauvet <[email protected]>

drm/vkms: Introduce config for connector EDID

This commit adds RCU-protected EDID configuration to vkms_config_connector
and updates vkms_conn_get_modes() to read the configured EDID when enabled.

> diff --git a/drivers/gpu/drm/vkms/vkms_config.c 
> b/drivers/gpu/drm/vkms/vkms_config.c
> index fa4ca256b4b91..173e88122bcb1 100644
> --- a/drivers/gpu/drm/vkms/vkms_config.c
> +++ b/drivers/gpu/drm/vkms/vkms_config.c
[ ... ]
> @@ -1070,6 +1084,8 @@ static void vkms_config_connector_release(struct kref 
> *ref)
>               /* The config is dying and is draining us: just unlink */
>               list_del_init(&connector_cfg->link);
>       }
> +     if (connector_cfg->edid)
> +             call_rcu(&connector_cfg->edid->rcu, vkms_config_edid_free);
>       kfree_rcu(connector_cfg, rcu);
>  }

[Severity: Low]
connector_cfg->edid is annotated with __rcu, so dereferencing
&connector_cfg->edid->rcu directly without rcu_dereference_protected()
triggers a Sparse noderef warning, unlike vkms_config_connector_set_edid()
below.  Also, because vkms_config_edid_free() is a module function queued
via call_rcu(), unloading the module immediately after vkms_exit() drops
default_config could race with an in-flight callback unless vkms_exit()
calls rcu_barrier() or struct vkms_config_edid embeds the data as a
flexible array freed with kfree_rcu().

[ ... ]
> diff --git a/drivers/gpu/drm/vkms/vkms_config.h 
> b/drivers/gpu/drm/vkms/vkms_config.h
> index 341e0638b5087..ef8de0316b94c 100644
> --- a/drivers/gpu/drm/vkms/vkms_config.h
> +++ b/drivers/gpu/drm/vkms/vkms_config.h
> @@ -145,6 +145,22 @@ struct vkms_config_encoder {
>       struct drm_encoder *encoder;
>  };
>  
> +/**
> + * struct vkms_config_edid - RCU-protected EDID data
> + *
> + * @edid: Pointer to the EDID data buffer
> + * @edid_len: Length of the EDID data
> + * @rcu: RCU head for safe freeing
> + *
> + * This struct ensures EDID data and its length are always accessed
> + * coherently by readers without requiring locks.
> + */
> +struct vkms_config_edid {
> +     const u8 *edid;
> +     unsigned int edid_len;
> +     struct rcu_head rcu;
> +};
> +
>  /**
>   * struct vkms_config_connector
>   *
> @@ -157,6 +173,8 @@ struct vkms_config_encoder {
>   *                         are the sames as ones accepted by
>   *                         drm_mode_create_hdmi_colorspace_property() and
>   *                         drm_mode_create_dp_colorspace_property()
> + * @edid_enabled: If true, create the EDID property
> + * @edid: RCU-protected EDID data. Readers must use rcu_read_lock() to 
> access.

[Severity: Low]
This isn't a bug, but the kernel-doc for @edid_enabled says it controls
creating the EDID property, whereas the EDID property is always attached
and edid_enabled instead controls whether vkms_conn_get_modes() exposes
the configured EDID.

[ ... ]
> +static inline const u8 *
> +vkms_config_connector_get_edid(const struct vkms_config_connector 
> *connector_cfg, int *len)
> +{
> +     const struct vkms_config_edid *edid = 
> rcu_dereference(connector_cfg->edid);
> +
> +     if (edid) {
> +             *len = edid->edid_len;
> +             return edid->edid;
> +     }
> +
> +     *len = 0;
> +     return NULL;
> +}
> +
> +int vkms_config_connector_set_edid(struct vkms_config_connector 
> *connector_cfg,
> +                                const u8 *edid, unsigned int len);

[Severity: Medium]
Should the len parameter of vkms_config_connector_get_edid() be
unsigned int * instead of int *?

struct vkms_config_edid.edid_len and vkms_config_connector_set_edid() both
use unsigned int, and vkms_connector_read_block() passes &edid_len of type
unsigned int *, which triggers a -Wpointer-sign compiler warning (and a
build failure under CONFIG_WERROR=y).

> diff --git a/drivers/gpu/drm/vkms/vkms_connector.c 
> b/drivers/gpu/drm/vkms/vkms_connector.c
> index 552d767c3976e..71a0ae15be653 100644
> --- a/drivers/gpu/drm/vkms/vkms_connector.c
> +++ b/drivers/gpu/drm/vkms/vkms_connector.c
> @@ -43,13 +43,48 @@ static const struct drm_connector_funcs 
> vkms_connector_funcs = {
>       .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
>  };
>  
> +static int vkms_connector_read_block(void *context, u8 *buf, unsigned int 
> block, size_t len)
> +{
> +     struct vkms_config_connector *config = context;
> +     unsigned int edid_len;
> +     const u8 *edid = vkms_config_connector_get_edid(config, &edid_len);
> +
> +     if (block * len + len > edid_len)
> +             return 1;
> +     memcpy(buf, &edid[block * len], len);
> +     return 0;
> +}
> +
>  static int vkms_conn_get_modes(struct drm_connector *connector)
>  {
> -     int count;
> +     struct vkms_connector *vkms_connector = 
> drm_connector_to_vkms_connector(connector);
> +     const struct drm_edid *drm_edid = NULL;
> +     int count = 0;
> +     struct vkms_config_connector *cfg = 
> vkms_config_connector_get(vkms_connector->cfg);
> +
> +     if (vkms_config_connector_get_edid_enabled(cfg)) {
> +             /*
> +              * Hold rcu_read_lock() for the entire duration so that
> +              * the EDID data accessed via the callback remains valid.
> +              */
> +             rcu_read_lock();
> +             drm_edid = drm_edid_read_custom(connector,
> +                                             vkms_connector_read_block, cfg);
> +
> +             drm_edid_connector_update(connector, drm_edid);
> +
> +             count = drm_edid_connector_add_modes(connector);
> +
> +             drm_edid_free(drm_edid);
> +             rcu_read_unlock();

[Severity: High]
Does holding rcu_read_lock() across drm_edid_read_custom(),
drm_edid_connector_update(), and drm_edid_connector_add_modes() sleep in
an RCU read-side critical section?

All three helpers perform GFP_KERNEL allocations and acquire sleeping
mutexes: for example, drm_edid_read_custom() -> _drm_do_get_edid() takes
mutex_lock(&connector->edid_override_mutex) in drm_edid_override_get() and
allocates the EDID buffer with kmalloc(..., GFP_KERNEL).  Also, because
vkms_connector_read_block() calls vkms_config_connector_get_edid() on each
128-byte block, a concurrent vkms_config_connector_set_edid() can swap the
RCU pointer between block 0 and extension blocks, causing a torn read.
Instead of drm_edid_read_custom(), could this snapshot the buffer under
rcu_read_lock() or pass the snapshot to drm_edid_alloc() outside the RCU
read-side critical section?

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

Reply via email to