Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm: memcontrol: task_css() race with migration in
  get_obj_cgroup_from_fd()
--

commit 62e5304eba9bb9d84f6229eddf1a9273649483b8
Author: Albert Esteve <[email protected]>

dma-heap: charge dma-buf memory via explicit memcg

This commit adds a charge_fd field to struct dma_heap_allocation_data so
DMA_HEAP_IOCTL_ALLOC can resolve a target cgroupfd or pidfd via
get_obj_cgroup_from_fd() and charge system heap allocations to that
cgroup.

> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index e4a9af2fdaa3b..7826bc21bea9e 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
[ ... ]
> @@ -3171,6 +3172,88 @@ struct obj_cgroup *get_obj_cgroup_from_folio(const 
> struct folio *folio)
>       return objcg;
>  }
>  
[ ... ]
> +struct obj_cgroup *get_obj_cgroup_from_fd(int fd)
> +{
> +     struct cgroup_subsys_state *css;
> +     struct obj_cgroup *objcg;
> +     struct mem_cgroup *memcg;
> +     struct task_struct *task;
> +     unsigned int pidfd_flags;
> +
> +     if (mem_cgroup_disabled())
> +             return ERR_PTR(-EOPNOTSUPP);
> +
> +     CLASS(fd_raw, f)(fd);
> +     if (fd_empty(f))
> +             return ERR_PTR(-EBADF);
> +
> +     css = css_tryget_online_from_dir(fd_file(f)->f_path.dentry,
> +                                      &memory_cgrp_subsys);
> +     if (!IS_ERR(css) && !cgroup_on_dfl(css->cgroup)) {
> +             css_put(css);
> +             css = ERR_PTR(-EBADF);
> +     }
> +
> +     if (IS_ERR(css)) {
> +             if (PTR_ERR(css) != -EBADF)
> +                     return ERR_CAST(css);
> +
> +             /* fd is a pidfd */
> +             task = pidfd_get_task(fd, &pidfd_flags);
> +             if (IS_ERR(task))
> +                     return ERR_CAST(task);
> +
> +             rcu_read_lock();
> +             css = task_css(task, memory_cgrp_id);
> +             put_task_struct(task);
> +             if (!css || !css_tryget_online(css)) {
> +                     rcu_read_unlock();
> +                     return ERR_PTR(-ENOENT);
> +             }
> +             rcu_read_unlock();
> +     }

[Severity: Low]
Can the pidfd lookup in get_obj_cgroup_from_fd() spuriously fail with
-ENOENT when the target task is concurrently migrated or has an exited
thread-group leader?

Between task_css() and css_tryget_online(), the task can be migrated to
another cgroup and its previous cgroup offlined, causing the one-shot
css_tryget_online() on the stale css to fail.  Also, pidfd_get_task() on
a process pidfd returns the thread-group leader, which in a
multi-threaded process may already have PF_EXITING set and remain
attached to an offline css even while its obj_cgroup has been reparented
to an online ancestor that get_obj_cgroup_from_memcg() walks up to:

include/linux/cgroup.h:task_get_css() {
        rcu_read_lock();
        while (true) {
                css = task_css(task, subsys_id);
                /*
                 * Can't use css_tryget_online() here.  A task which has
                 * PF_EXITING set may stay associated with an offline css.
                 * If such task calls this function, css_tryget_online()
                 * will keep failing.
                 */
                if (likely(css_tryget(css)))
                        break;
                cpu_relax();
        }
        rcu_read_unlock();
        return css;
}

Using task_get_css(task, memory_cgrp_id) before put_task_struct(task)
would avoid both failure modes.

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/20261124-v2_20230123_tjmercier_google_com-v6-0-dce12fa03...@redhat.com?part=2

Reply via email to