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 7e6b5f51c2c9a154a8386e2fbe69f7c3fe1c4a27
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 cgroup from a pidfd or cgroupfd
and charge system-heap dma-buf allocations to that cgroup's obj_cgroup.

> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 45e82cda7c441..adc0abcf80fae 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
[ ... ]
> @@ -3168,6 +3169,88 @@ struct obj_cgroup *get_obj_cgroup_from_folio(const 
> struct folio *folio)
[ ... ]
> +struct obj_cgroup *get_obj_cgroup_from_fd(int fd)
> +{
[ ... ]
> +     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();\n> +                 return 
> ERR_PTR(-ENOENT);
> +             }
> +             rcu_read_unlock();
> +     }

[Severity: Low]
Can this one-shot task_css() and css_tryget_online() lookup spuriously fail
with -ENOENT?

CPU0                                  CPU1
get_obj_cgroup_from_fd()
  rcu_read_lock()
  css = task_css(task, ...)
                                      cgroup_migrate_execute()
                                        css_set_move_task(task, ...)
                                      cgroup_destroy_locked()
                                        kill_css_finish()
  css_tryget_online(css)  <-- false
  return ERR_PTR(-ENOENT)

Also, pidfd_get_task() on a process pidfd returns the thread-group leader;
if that leader has exited (PF_EXITING) in a multi-threaded process whose
remaining threads migrated and whose old cgroup was offlined, the leader
stays associated with the offline css even though get_obj_cgroup_from_memcg()
can walk up to its online ancestor.  Using task_get_css(task, memory_cgrp_id)
before put_task_struct(task) avoids both issues:

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;
}

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/20261117-v2_20230123_tjmercier_google_com-v5-0-1ce975f2a...@redhat.com?part=2

Reply via email to