尝试将进程添加到 cgroup v2 时 EBUSY

尝试将进程添加到 cgroup v2 时 EBUSY

我正在玩cgroups v2Fedora 35内核5.15.5-200.fc35.x86_64)。

我能够测试cpu和控制器cpusetpids没有任何问题。但对于memoryhugetlb或者io我有麻烦了。以下是如何为memory控制器重现此问题的步骤列表(所有命令均以 root 用户身份执行):

  1. 确保在根 cgroup 中启用内存控制器:

    # cat cgroup.subtree_control
    cpu io memory hugetlb pids
    
  2. 创建新的cgroup:

    # mkdir example
    # cd example/
    
  3. 启用内存控制器:

    # echo "+memory" > cgroup.subtree_control 
    
  4. 将进程添加到控制组:

    # echo $$ > cgroup.procs 
    bash: echo: write error: Device or resource busy
    

当我尝试首先将进程添加到控制器cgroup然后启用控制器时,最后一步会发生相同的错误。

我究竟做错了什么?

答案1

这是因为 cgroups v2“无内部进程”规则:

   The "no internal processes" rule is in fact more subtle than
   stated above.  More precisely, the rule is that a (nonroot)
   cgroup can't both (1) have member processes, and (2) distribute
   resources into child cgroups—that is, have a nonempty
   cgroup.subtree_control file.  Thus, it is possible for a cgroup
   to have both member processes and child cgroups, but before
   controllers can be enabled for that cgroup, the member processes
   must be moved out of the cgroup (e.g., perhaps into the child
   cgroups).

   With the Linux 4.14 addition of "thread mode" (described below),
   the "no internal processes" rule has been relaxed in some cases.

来源:cgroups(7)


另请参阅内核源代码:

https://elixir.bootlin.com/linux/v5.19/source/kernel/cgroup/cgroup.c#L2586

**
 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
 * @dst_cgrp: destination cgroup to test
 *
 * On the default hierarchy, except for the mixable, (possible) thread root
 * and threaded cgroups, subtree_control must be zero for migration
 * destination cgroups with tasks so that child cgroups don't compete
 * against tasks.
 */
int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
{
    // [..]

    /* apply no-internal-process constraint */
    if (dst_cgrp->subtree_control)
        return -EBUSY;

    return 0;
}

例如,这有效:

# cd /sys/fs/cgroup
/sys/fs/cgroup # rmdir deleteme
/sys/fs/cgroup # mkdir deleteme
/sys/fs/cgroup # cd deleteme 
/sys/fs/cgroup/deleteme # mkdir leaf
/sys/fs/cgroup/deleteme # echo '+memory' > cgroup.subtree_control
/sys/fs/cgroup/deleteme # echo $$ > leaf/cgroup.procs
/sys/fs/cgroup/deleteme #

相关内容