Search code examples
ubuntuwindows-subsystem-for-linuxcgroups

How to enable cgroup v2 in WSL2?


The easiest way to get access to cgroup v2 capable system having only a Windows machine is to spawn WSL2 instance hosting Ubuntu 22.04. Unfortunately, there is an issue. Removal of v1 controllers doesn't result in that controllers added in v2 hierarchy.

By default, WSL2 has both cgroup v1 and cgroup v2 hierarchies enabled, with all controllers sitting in v1:

$ mount -l | grep cgroup
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,relatime,mode=755)
cgroup2 on /sys/fs/cgroup/unified type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu type cgroup (rw,nosuid,nodev,noexec,relatime,cpu)
cgroup on /sys/fs/cgroup/cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_prio)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)

I tried removing v1 controllers with $ umount /sys/fs/cgroup/*. This seems removes cgroup v1 controllers from $ mount | grep cgroup list. But then nothing is being added to v2 (/sys/fs/cgroup/unified).

If I understand the cgroup v2 official documentation correctly, a controller may be moved to cgroup v2 only when no more processes are handled by that controller.

How do I enable controllers like "cpu" and "memory" to cgroup v2?


Solution

  • Since Linux v5.0, kernel boot option cgroup_no_v1=<list_of_controllers_to_disable> can be used to disable cgroup v1 hierarchies. As result your machine should start as cgroup v2 only.

    For WSL, Microsoft documentation says we have to create %UserProfile%\.wslconfig file with the following content:

    [wsl2]
    kernelCommandLine = cgroup_no_v1=all
    

    That's it.
    Make sure you reboot your WSL with > wsl.exe --shutdown command.
    You can see enabled controllers you got in <cgroup_fs_mount_point>/cgroup.controllers file.

    Extra tweaks:

    1. By default, cgroup2 will likely be mounted at /sys/fs/cgroup/unified. Some apps might not like it (docker in particular). Move it to the conventional place with:
    $ mount --move /sys/fs/cgroup/unified /sys/fs/cgroup
    
    1. You can achieve similar effect by editing /etc/fstab file:
    cgroup2 /sys/fs/cgroup cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0
    

    The receipt #2 had one side-effect where the original cgroup mount point at /sys/fs/cgroup/unified kept "dangling". You could see that by running $ mount | grep cgroup2 or looking /proc/self/mountinfo file. The upside of #2 was that docker service was starting without any issues in this case. Hence, if you can tolerate dangling mounts, the /etc/fstab method is preferred.