Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] drm/ras: repeated node_id lookup in
  drm_ras_nl_get_error_counter_dumpit()
- [Medium] drm/ras: premature node put in get_node_error_counter() and
  threshold helper
- [Low] drm/ras: separate declaration and initialization of __free()
  node variables
--

commit d22066c85bafafc21ec35b35656510aa805c320f
Author: Rodrigo Vivi <[email protected]>

drm/ras: Keep nodes alive while netlink operations use them

This commit adds reference counting and a release completion to struct
drm_ras_node so that drm_ras_node_unregister() waits for active netlink
operations to finish using a node before returning.

> diff --git a/drivers/gpu/drm/drm_ras.c b/drivers/gpu/drm/drm_ras.c
> index f561604e7e967..550ca7d54cd12 100644
> --- a/drivers/gpu/drm/drm_ras.c
> +++ b/drivers/gpu/drm/drm_ras.c
[ ... ]
> @@ -178,9 +208,9 @@ int drm_ras_nl_list_nodes_dumpit(struct sk_buff *skb,
>  static int get_node_error_counter(u32 node_id, u32 error_id,
>                                 const char **name, u32 *value)
>  {
> -     struct drm_ras_node *node;
> +     struct drm_ras_node *node __free(drm_ras_node_put) = NULL;
>  
> -     node = xa_load(&drm_ras_xa, node_id);
> +     node = drm_ras_node_get(node_id);
>       if (!node || !node->query_error_counter)
>               return -ENOENT;
>  
> @@ -193,9 +223,9 @@ static int get_node_error_counter(u32 node_id, u32 
> error_id,
>  
>  static int get_node_error_threshold(u32 node_id, u32 error_id, const char 
> **name, u32 *threshold)
>  {
> -     struct drm_ras_node *node;
> +     struct drm_ras_node *node __free(drm_ras_node_put) = NULL;
>  
> -     node = xa_load(&drm_ras_xa, node_id);
> +     node = drm_ras_node_get(node_id);
>       if (!node)
>               return -ENOENT;

[Severity: Medium]
Does dropping the node reference on return from get_node_error_counter()
and get_node_error_threshold() leave *name unprotected in
doit_reply_value() and doit_reply_threshold()?

drivers/gpu/drm/drm_ras.c:doit_reply_value() {
        ...
        ret = get_node_error_counter(node_id, error_id,
                                     &error_name, &value);
        if (ret)
                goto cancel_msg;

        ret = msg_reply_value(msg, error_id, error_name, value);
        ...
}

CPU0                                  CPU1
doit_reply_value()
  get_node_error_counter()
    node = drm_ras_node_get(...)
    node->query_error_counter(...,
                              &error_name, ...)
    drm_ras_node_put(node)
                                      drm_ras_node_unregister(node)
                                        xa_erase(&drm_ras_xa, node->id)
                                        drm_ras_node_put(node)
                                        wait_for_completion(&node->released)
                                      /* driver frees counter names / unloads */
  msg_reply_value(..., error_name, ...)
    nla_put_string(..., error_name)   <-- use-after-free

Because the __free(drm_ras_node_put) reference lives inside the helper,
drm_ras_node_unregister() can finish waiting before msg_reply_value() or
msg_reply_threshold() copies error_name into the skb.

> @@ -210,9 +240,9 @@ static int get_node_error_threshold(u32 node_id, u32 
> error_id, const char **name
>  
>  static int set_node_error_threshold(u32 node_id, u32 error_id, u32 threshold)
>  {
> -     struct drm_ras_node *node;
> +     struct drm_ras_node *node __free(drm_ras_node_put) = NULL;
>  
> -     node = xa_load(&drm_ras_xa, node_id);
> +     node = drm_ras_node_get(node_id);
>       if (!node)
>               return -ENOENT;

[Severity: Low]
This isn't a bug, but cleanup.h style prefers defining and initializing
__free() variables in one statement ("struct drm_ras_node *node
__free(drm_ras_node_put) = drm_ras_node_get(node_id);") rather than
initializing them to NULL and assigning later, here and in the other
functions touched by this patch.

[ ... ]

> @@ -451,7 +482,7 @@ int drm_ras_nl_get_error_counter_dumpit(struct sk_buff 
> *skb,
>  {
>       const struct genl_info *info = genl_info_dump(cb);
>       struct drm_ras_ctx *ctx = (void *)cb->ctx;
> -     struct drm_ras_node *node;
> +     struct drm_ras_node *node __free(drm_ras_node_put) = NULL;
>       struct nlattr *hdr;
>       const char *error_name;
>       u32 node_id, error_id, value;
> @@ -462,7 +493,7 @@ int drm_ras_nl_get_error_counter_dumpit(struct sk_buff 
> *skb,
>  
>       node_id = 
> nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
>  
> -     node = xa_load(&drm_ras_xa, node_id);
> +     node = drm_ras_node_get(node_id);
>       if (!node)
>               return -ENOENT;

[Severity: Medium]
Can the dump loop in drm_ras_nl_get_error_counter_dumpit() silently
truncate the dump or switch to a different node if
drm_ras_node_unregister() runs mid-dump?

drivers/gpu/drm/drm_ras.c:drm_ras_nl_get_error_counter_dumpit() {
        ...
        for (error_id = max(node->error_counter_range.first, ctx->restart);
             error_id <= node->error_counter_range.last;
             error_id++) {
                ret = get_node_error_counter(node_id, error_id,
                                             &error_name, &value);
                /*
                 * For non-contiguous range, driver return -ENOENT as indication
                 * to skip this ID when listing all errors.
                 */
                if (ret == -ENOENT) {
                        ret = 0;
                        continue;
                }
        ...
}

drm_ras_nl_get_error_counter_dumpit() already holds a reference on node,
but each loop iteration passes node_id to get_node_error_counter(), which
acquires xa_lock(&drm_ras_xa) and looks up node_id in drm_ras_xa again.

If drm_ras_node_unregister() runs concurrently, it calls xa_erase() before
waiting on node->released.  Subsequent iterations of the loop then get
-ENOENT from drm_ras_node_get(node_id) and treat it as a non-contiguous
error ID to skip, returning a truncated dump with status 0 (or querying a
newly registered node if xa_alloc() reuses node_id while the old node is
still waiting in drm_ras_node_unregister()).  Passing the already pinned
node pointer to a helper would avoid both the race and the repeated
xa_lock / kref cycles in the loop.

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

Reply via email to