fix: cgroupv2_subtree_control_recursive(): test before writing
cgroupv2_subtree_control_recursive() writes to subtree_control
files in the cgroup tree, starting at the root cgroup. As the
root cgroup is managed by SystemD, its control files belong to the
root user.
This new check ensures that we only attempt to write to subtree_control
if the desired controller is not already active (e.g., because we're
using SystemD Delegation mechanisms), thus better enabling cgroup
manipulations for non-root users.
With upstream commit 7476a636d3100 ("cgroup/cpuset: Show invalid
partition reason string"), doesn't return error but needs to be re-read
again. This oddity is seen in kernel >= 6.5, fix it by introducing some
checks for cpuset_exclusive oddity.
-----------------------------------------------------------------
Test Results:
Run Date: Apr 12 10:20:07
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
--------------------------------------------
setup 0.00
038-cgxget-cpuset_settings.py 24.75
teardown 0.00
--------------------------------------------
Total Run Time 24.75
Fixes: https://github.com/libcgroup/libcgroup/issues/423 Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Starting Linux Kernel version 6.1, the pids controller added an new
interface file pids.peak. It was introduced by upstream commit 5251c6c436ed ("cgroup: add pids.peak interface for pids controller").
Append a new expected output with the interface 'pids.peak' to cgroup v1
and v2 valid output.
-----------------------------------------------------------------
Test Results:
Run Date: Apr 04 10:11:31
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
--------------------------------------------
setup 0.00
013-cgget-multiple_g_flags.py 0.12
teardown 0.00
--------------------------------------------
Total Run Time 0.12
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Starting Linux Kernel version 6.0, the cpu controller cpu.stat (v2)
additionally displays force_idle statistics. It was introduced by
upstream commit 1fcf54deb767 ("sched/core: add forced idle accounting
for cgroups"). Append a new expected output with the field
'core_sched.force_idle_usec' to cgroup v2 valid output.
-----------------------------------------------------------------
Test Results:
Run Date: Apr 03 15:49:31
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
--------------------------------------------
setup 0.00
013-cgget-multiple_g_flags.py 0.12
teardown 0.00
--------------------------------------------
Total Run Time 0.12
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Fri, 29 Mar 2024 07:18:59 +0000 (12:48 +0530)]
ftests/010: add cpu controller format (v2)
Starting Linux Kernel version 6.0, the cpu controller cpu.stat (v2)
additionally displays force_idle statistics. It was introduced by
upstream commit 1fcf54deb767 ("sched/core: add forced idle accounting
for cgroups"). Append a new expected output with the field
'core_sched.force_idle_usec' to cgroup v2 valid output.
-----------------------------------------------------------------
Test Results:
Run Date: Apr 03 15:48:51
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
--------------------------------------------------------
setup 0.00
010-cgget-g_flag_controller_and_cgroup.py 0.09
teardown 0.00
--------------------------------------------------------
Total Run Time 0.09
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Thu, 28 Mar 2024 06:07:09 +0000 (11:37 +0530)]
ftests/009: add cpu controller format (v2)
Starting Linux Kernel version 6.0, the cpu controller cpu.stat (v2)
additionally displays force_idle statistics. It was introduced by
upstream commit 1fcf54deb767 ("sched/core: add forced idle accounting
for cgroups"). Append a new expected output with the field
'core_sched.force_idle_usec' to cgroup v2 valid output.
-----------------------------------------------------------------
Test Results:
Run Date: Apr 03 15:36:32
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
--------------------------------------------------
setup 0.00
009-cgget-g_flag_controller_only.py 0.10
teardown 0.00
--------------------------------------------------
Total Run Time 0.10
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Adriaan Schmidt [Tue, 27 Feb 2024 12:37:17 +0000 (13:37 +0100)]
feat: add cgroup_get_threads()
In the same way `cgroup_get_procs()` reads the cgroup.procs
file of a cgroup, the new function reads cgroup.threads, which
is useful to interact with threaded cgroups.
Kamalesh Babulal [Wed, 13 Mar 2024 10:04:25 +0000 (10:04 +0000)]
ftests/092: add test to attach threads to cgroup (v1)
Add test to attach/move all thread of a tid to cgroup using
cgroup_attach_thread_tid() in cgroup v1.
-----------------------------------------------------------------
Test Results:
Run Date: Mar 13 09:52:58
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
---------------------------------------------------
setup 0.00
092-sudo-cgroup_attach_thread_tid.py 0.09
teardown 0.00
---------------------------------------------------
Total Run Time 0.09
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Mon, 11 Mar 2024 03:37:19 +0000 (09:07 +0530)]
src/python: add attach_threads option to attach()
Add support to move all threads by writing a tid into cgroups
cgroup.procs for the cgroup v1 controller, when attach_threads option in
the attach() is set, by default its false. When set, it calls
cgroup_attach_thread_tid().
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Add helper cgroup_v1_build_cgroup_procs_path(), that constructs the path
to write tid into cgroup.procs, that will allow to move all of the tasks
sharing the same pid (thread leader). This helper, will be called by
cgroup_attach_task_tid() based on the move_threads function argument.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Introduce a new helper cgroup_attach_task_tid(), which will be used to
move threads into a cgroup for legacy/hybrid controllers running setup
in cgroup v1 mode. The core of the helper is craved out of
cgroup_attach_task_pid(), now both cgroup_attach_task_pid() and
cgroup_attach_task() calls cgroup_attach_task_tid() in turn.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Add support for cgroup hybrid mode with controllers in cgroup v2.
-----------------------------------------------------------------
Test Results:
Run Date: Nov 01 10:40:32
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
-----------------------------------------------------------
setup 0.00
081-pybindings-cgrp_get_curr_ctrl_path-v1.py 2.09
teardown 0.00
-----------------------------------------------------------
Total Run Time 2.09
Suggested-by: Tom Hromatka <tom.hromatka@oracle.com> Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
ftests/089: enable controller for hybrid parent cgroup
Enable the cpuset controller for the hybrid parent cgroup, in case of
the cgroup hybrid setup mode with controller enabled.
-----------------------------------------------------------------
Test Results:
Run Date: Nov 01 12:50:20
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
------------------------------------------
setup 0.00
089-cgset-recursive_flag.py 0.23
teardown 0.00
------------------------------------------
Total Run Time 0.23
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Tom Hromatka [Wed, 6 Dec 2023 15:37:44 +0000 (15:37 +0000)]
ftests/systemd: add method to check if systemd support
Add a method to check if libcgroup is compiled with --enable-systemd
support. The newly introduced is_systemd_enabled(), calls systemd C
function is_systemd_enabled(), that return true if configured with
--enable-systemd, false otherwise.
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Tom Hromatka [Thu, 7 Dec 2023 04:19:07 +0000 (04:19 +0000)]
python: compile systemd support by default
Compile systemd support file by default, there are two versions of
systemd functions available now, those get called based on the
--enable-systemd configuration, hence remove the #ifdef WITH_SYSTEMD
guards from bindings.
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Mon, 27 Nov 2023 14:37:33 +0000 (20:07 +0530)]
include/Makefile: install systemd.h by default
Install systemd.h header file by default, as we have stub and defined
versions of the systemd functions for both non-systemd and systemd
enabled configurations. This will help packagers to ship package
without systemd support (--enable-systemd=no).
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Sat, 25 Nov 2023 14:32:34 +0000 (20:02 +0530)]
src/Makefile: compile systemd support by default
Compile systemd support file by default, now we have stub and defined
versions of the systemd functions for both non-systemd and systemd
enabled configurations. Pass linker flag WITH_SYSTEMD only when
--enable-systemd is set, that will call the defined function, otherwise
call the stub functions.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Fri, 13 Oct 2023 12:02:24 +0000 (17:32 +0530)]
ftests: add test to exercise cgset recursive set option
Add a test to recursive set settings of the controller(s) in a cgroup
and its descendants using cgset '-R' flag.
-----------------------------------------------------------------
Test Results:
Run Date: Oct 28 15:25:57
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
------------------------------------------
setup 0.00
089-cgset-recursive_flag.py 0.13
teardown 0.00
------------------------------------------
Total Run Time 0.13
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Wed, 11 Oct 2023 11:29:38 +0000 (16:59 +0530)]
tools/cgset: add support for cgroup.subtree_control
The cgroup.subtree_control settings are special, in comparison to other
controller settings. It can both enable and disable the controllers in
the single argument, depending on the argument the cgroup hierarchy walk
is either pre-order or post-order.
tools/cgset: add -R option to recursively set variables
Add -R option to recursively set variable(s) passed to cgroups under
<cgroup_path>. This will help users to set a controller setting for
all the cgroups under a cgroup hierarchy, instead of passing the
cgroups multiple times on the command line.
Without the patch
------------------
./cgset -r cpu.shares=256 foo
./cgget -r cpu.shares foo
foo/ch1 foo/ch2
foo:
cpu.shares: 256
foo/ch1:
cpu.shares: 1024
foo/ch2:
cpu.shares: 1024
With the patch
--------------
./cgset -R -r cpu.shares=512 foo
./cgget -r cpu.shares foo foo/ch1 foo/ch2
foo:
cpu.shares: 512
foo/ch1:
cpu.shares: 512
foo/ch2:
cpu.shares: 512
[check for ECGEOF recommend by Tom Hromatka] Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Move the cgroup controller value setting logic to a helper function,
cgroup_set_cgroup_values() and also introduce program_name variable, to
be used instead of argv[0], when printing the info()/err() messages.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Tue, 17 Oct 2023 10:26:37 +0000 (15:56 +0530)]
ftests: add test to cgcreate of non default controllers
Add test to create cgroups with non default controllers (hugetlb, misc)
using the cgcreate.
-----------------------------------------------------------------
Test Results:
Run Date: Oct 20 05:21:48
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
---------------------------------------------------------
setup 0.00
091-cgcreate-non-default-controllers-v2.py 0.04
teardown 0.00
---------------------------------------------------------
Total Run Time 0.04
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Mon, 16 Oct 2023 08:43:56 +0000 (14:13 +0530)]
gunit/012: populate root_cgroup.controllers file
To enable a controller in a cgroup, its parent's subtree_control file is
read to check if the controller is enabled, if not enabled, parent's
cgroup.controllers is checked for the supported controllers, before
enabling it in the cgroup.subtree_control.
Populate controllers in the root_cgroup.controllers file, to help with
the check for supported controllers, without this
cgroupv2_subtree_control_recursive() will fail, when it is checked for
controller existence.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Sun, 15 Oct 2023 08:43:56 +0000 (14:13 +0530)]
api.c: enable controller in the root_cgroup.subtree_file
systemd by default only enables cpu, cpuset, io, memory, and pids
controller the root_cgroup.subtree_control. Trying to enable other
controllers (hugetlb and misc) in a nested cgroups create scenario,
will fail because they are not, enabled the controller in the
root_cgroup.subtree_control file.
$ sudo cgcreate -ghugetlb:foo/bar
Error: Failed to delete foo/bar: No such file or directory
cgcreate: can't create cgroup foo/bar: No such file or directory
Fix this by enabling the controller in the root_cgroup.subtree_control
file unconditionally for all controllers, if not already enabled, while
calling cgroupv2_subtree_control_recursive() to enable the controller
in the given cgroup and its descendants. Checking and enabling every
controller unconditionally should work in the future, in case systemd
disables some other controllers too.
Fixes: https://github.com/libcgroup/libcgroup/issues/405 Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Fix unused variable and sign comparison warnings across the test case:
017-API_fuzz_test.cpp: In member function ‘virtual void APIArgsTest_API_cgroup_add_controller_Test::TestBody()’:
017-API_fuzz_test.cpp:190:6: warning: unused variable ‘ret’ [-Wunused-variable]
int ret;
^~~
In file included from 017-API_fuzz_test.cpp:11:
../../googletest/googletest/include/gtest/gtest.h: In instantiation of ‘testing::AssertionResult testing::internal::CmpHelperEQ(const char*, const char*, const T1&, const T2&) [with T1 = long unsigned int; T2 = int]’:
../../googletest/googletest/include/gtest/gtest.h:1421:23: required from ‘static testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare(const char*, const char*, const T1&, const T2&) [with T1 = long unsigned int; T2 = int; bool lhs_is_null_literal = false]’
017-API_fuzz_test.cpp:480:2: required from here
../../googletest/googletest/include/gtest/gtest.h:1392:11: warning: comparison of integer expressions of different signedness: ‘const long unsigned int’ and ‘const int’ [-Wsign-compare]
if (lhs == rhs) {
~~~~^~~~~~
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
015-cgroupv2_controller_enabled.cpp: In member function ‘void CgroupV2ControllerEnabled::InitMountTable()’:
015-cgroupv2_controller_enabled.cpp:69:7: warning: unused variable ‘ret’ [-Wunused-variable]
int ret, i;
^~~
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Fix unused variable warnings and signed comparison warning across the
test case:
013-cgroup_build_tasks_procs_path.cpp: In member function ‘virtual void BuildTasksProcPathTest_BuildTasksProcPathTest_CgV2_Test::TestBody()’:
013-cgroup_build_tasks_procs_path.cpp:112:27: warning: unused variable ‘ctrlr’ [-Wunused-variable]
struct cgroup_controller ctrlr = {0};
^~~~~
013-cgroup_build_tasks_procs_path.cpp: In member function ‘virtual void BuildTasksProcPathTest_BuildTasksProcPathTest_CgV1WithNs_Test::TestBody()’:
013-cgroup_build_tasks_procs_path.cpp:126:27: warning: unused variable ‘ctrlr’ [-Wunused-variable]
struct cgroup_controller ctrlr = {0};
^~~~~
013-cgroup_build_tasks_procs_path.cpp: In member function ‘virtual void BuildTasksProcPathTest_BuildTasksProcPathTest_CgV2WithNs_Test::TestBody()’:
013-cgroup_build_tasks_procs_path.cpp:140:27: warning: unused variable ‘ctrlr’ [-Wunused-variable]
struct cgroup_controller ctrlr = {0};
^~~~~
In file included from 013-cgroup_build_tasks_procs_path.cpp:9:
../../googletest/googletest/include/gtest/gtest.h: In instantiation of ‘testing::AssertionResult testing::internal::CmpHelperLT(const char*, const char*, const T1&, const T2&) [with T1 = int; T2 = long unsigned int]’:
013-cgroup_build_tasks_procs_path.cpp:53:4: required from here
../../googletest/googletest/include/gtest/gtest.h:1526:28: warning: comparison of integer expressions of different signedness: ‘const int’ and ‘const long unsigned int’ [-Wsign-compare]
../../googletest/googletest/include/gtest/gtest.h:1510:7:
if (val1 op val2) {\
~~~~~~~~~~~~
../../googletest/googletest/include/gtest/gtest.h:1526:28:
GTEST_IMPL_CMP_HELPER_(LT, <);
../../googletest/googletest/include/gtest/gtest.h:1510:12: note: in definition of macro ‘GTEST_IMPL_CMP_HELPER_’
if (val1 op val2) {\
^~
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
011-cgroupv2_subtree_control.cpp: In member function ‘virtual void SubtreeControlTest::SetUp()’:
011-cgroupv2_subtree_control.cpp:24:12: warning: unused variable ‘i’ [-Wunused-variable]
int ret, i;
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
In file included from 001-path.cpp:9:
../../googletest/googletest/include/gtest/gtest.h: In instantiation of ‘testing::AssertionResult testing::internal::CmpHelperLT(const char*, const char*, const T1&, const T2&) [with T1 = int; T2 = long unsigned int]’:
001-path.cpp:53:4: required from here
../../googletest/googletest/include/gtest/gtest.h:1526:28: warning: comparison of integer expressions of different signedness: ‘const int’ and ‘const long unsigned int’ [-Wsign-compare]
../../googletest/googletest/include/gtest/gtest.h:1510:7:
if (val1 op val2) {\
~~~~~~~~~~~~
../../googletest/googletest/include/gtest/gtest.h:1526:28: GTEST_IMPL_CMP_HELPER_(LT, <);
../../googletest/googletest/include/gtest/gtest.h:1510:12: note: in
definition of macro ‘GTEST_IMPL_CMP_HELPER_’
if (val1 op val2) {\
^~
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Wed, 23 Aug 2023 11:05:12 +0000 (11:05 +0000)]
ftests: Add a test to stress create systemd scope via cgcreate
Add a test to stress creation of systemd scope via the cgcreate command
line tool, by passing invalid slice and scope names.
-----------------------------------------------------------------
Test Results:
Run Date: Aug 23 11:03:00
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
-------------------------------------------------
setup 0.00
997-sudo-cgcreate_systemd_scope.py 0.13
teardown 0.00
-------------------------------------------------
Total Run Time 0.13
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Wed, 30 Aug 2023 06:00:31 +0000 (11:30 +0530)]
add FILE_PATH_CHANGES to ignore list of patch review
Add FILE_PATH_CHANGES to the list of warnings, that is very specific to
Linux Kernel, which is triggered when a file is added, moved, or deleted
to the source tree. Let's ignore it and this patch also removes an extra
new line in the .checkpatch.conf.
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Sun, 20 Aug 2023 05:42:47 +0000 (11:12 +0530)]
ftests: update test to check for ctrl name in scope name
Update the test case to check for controller names in the scope name.
-----------------------------------------------------------------
Test Results:
Run Date: Aug 18 07:57:32
Passed: 1 test(s)
Skipped: 0 test(s)
Failed: 0 test(s)
-----------------------------------------------------------------
Timing Results:
Test Time (sec)
-------------------------------------------------
setup 0.00
078-sudo-cgcreate_systemd_scope.py 5.28
teardown 0.00
-------------------------------------------------
Total Run Time 5.28
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Kamalesh Babulal [Tue, 22 Aug 2023 06:34:13 +0000 (12:04 +0530)]
src/systemd: check for ctrl name in scope name
systemd will silently prefix a '_' to the scope name and create, and
delegate it under the slice. If it matches with any of the original
cgroup and pseudo BPF-base systemd controllers. i.e.,
this implicit rename will cause confusion to the users, who would not
see any errors during creation but operate on non-delegated cgroup scope
created by libcgroup internally. Disallow such systemd scope names.
Reported-by: Tom Hromatka <tom.hromatka@oracle.com> Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com> Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>