Commit graph

27 commits

Author SHA1 Message Date
T.J. Mercier
1c007996b3 libprocessgroup: Use pid_t consistently for TIDs
Test: m
Change-Id: I6e95870e6c5855bfe03be3be8f7a8884147bc15d
2024-01-25 16:35:30 +00:00
Bart Van Assche
b0947e008a libprocessgroup: Proceed if activation of an optional controller fails
Not all Android kernels support all the cgroup controllers mentioned in
task_profiles.json and/or cgroups.json. Support such kernels by ignoring
certain cgroup activation failures.

Bug: 213617178
Change-Id: I90c0bd959f8a6484c4f2fbc895845e073527271e
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-02-10 15:02:44 -08:00
Bart Van Assche
17c84b9ffb libprocessgroup: Introduce a local variable in CgroupMap::ActivateControllers()
This CL prepares for introducing an additional flag test.

Bug: 213617178
Change-Id: Ia74c1990792b5839f76498de2cac0008ed92040f
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-02-09 13:26:17 -08:00
Bart Van Assche
0e8e4f87fd Fix the cgroup v2 thread path
The name for the file with thread IDs is "tasks" in the v1 hierarchy and
"cgroup.threads" in the v2 hierarchy.

References:
* https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/cgroups.html
* https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html

Bug: 213617178
Fixes: 82b72a5667 ("libprocessgroup: Add support for task profiles")
Change-Id: I87d67edeb12803cb83486be8f1b1b56a3f275a9d
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-01-09 14:40:51 -08:00
Bart Van Assche
8923d72252 Micro-optimize CgroupMap::ActivateControllers()
The C++ string concatenation operator is inefficient because it always
allocates a new string, even in cases where std::string::apppend() does
not allocate a new string. Hence this patch that replaces string
concatenation with a call to std::string::append().

Bug: 213617178
Test: Booted Android in Cuttlefish.
Change-Id: I79bdb89e957d3cfb40e48ef00c3e5576b4f533a5
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-02-15 00:09:34 +00:00
Suren Baghdasaryan
9e3ace52bb libprocessgroup: Add CgroupGetControllerFromPath API function
Add new CgroupGetControllerFromPath function to retrieve the name of the
cgroup using a file path. If the file does not belong to any cgroup, the
function returns false.

Bug: 191283136
Test: build and boot
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: Ic17a474cb25a80a3339b33ed8bc27b07af053abb
2021-11-08 21:24:33 +00:00
Suren Baghdasaryan
25ad3f9b86 libprocessgroup: Activate controllers in cgroup v2 hierarchy at intermediate levels
When creating uid/pid hierarchy, cgroup.subtree_control should be set at
every level of that hierarchy except for the leaf level.

Bug: 195149205
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: Iedc2e859715b31db62158c85016708f252db2b70
2021-08-02 20:02:51 +00:00
Jiyong Park
ab8a7d2a03 Cgroup APIs are guarded
The following API was added for the API level 30 and beyond. Currently,
its existence is tested using the null check which is done regardless of
the min sdk version of the compilation unit. (which in turn required us
to mark the API symbol weak regardless of the min sdk version.)

* AGroupController_getFlags

Now, we have a better way of testing the API availability;
__builtin_available. The null check is replaced with the call to the
compiler-provided macro which determines if the code is running in a
version of OS where the API is known to exist.

Bug: 150860940
Bug: 134795810
Test: m

Change-Id: I5d17fc877c553c402a62485b61af574a54658a76
2021-01-12 20:50:27 +09:00
Marco Ballesio
f16f9417b0 libprocessgroup: support for cgroup v2 hierarchy
for a first implementation the cgroup v2 freezer controller will be used in a
way similar to cgroup v1, that is a single child group will hold all frozen
processes. Some adjustments are needed for the new structure.

- Add support for cgroup v2 syntax under procfs.
- Separate creation of a directory with ownership/mode changes to allow changes
    after mounting the cgroup kernfs root.
- Allow the creation of sub-groups under a cgroup v2 hierarchy.

Bug: 154548692
Test: manually verified that a proper cgroup v2 hierarchy is created and
accessible

Change-Id: I9af59e8214acaead3f520a94c95e75394c0df948
2020-06-10 09:14:00 -07:00
Suren Baghdasaryan
cee468fb79 Revive ACgroupController_getFlags LLNDK function to detect missing cgroups
ACgroupController_getFlags was reverted due to LLNDK breakage, however
it allows detection of cgroups that failed to mount in a more efficient
way. Revive the function as a weakly linked symbol to allow for it to
be missing in case older LLNDK library is being used with the new
system software. This effectively reverts the commit
aa1d54f0cc "Remove ACgroupController_getFlags to fix API breakage"'
except it declares ACgroupController_getFlags function as weak and
targets it for API level 30. If LLNKD library does not contain
ACgroupController_getFlags the behavior falls back to the current
way of identifying cgroups that failed to mount.

Test: build and verify correct operation with a missing cgroup
Change-Id: I9158ef53aba97972d41d71dd3396ac43796a7004
Merged-In: I9158ef53aba97972d41d71dd3396ac43796a7004
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2020-01-29 17:24:06 +00:00
Suren Baghdasaryan
25eb1bfadf Fix IsUsable to check for each controller separately
CgroupController::IsUsable is using a global static variable to store the
existence of the controller. That means the first controller existence
check would affect all other controllers. Fix this by making this variable
to be a member of CgroupController class so that each controller can check
for its existence independently of other controllers.

Fixes: aa1d54f0cc ("Remove ACgroupController_getFlags to fix API breakage")
Bug: 136020193
Test: adb shell cat /proc/$pid/task/*/cgroup" prints "cpuset:/top-app"
Test: for new launched activity process
Change-Id: I4741a9126ea494122d5b2b1a0c4d7252bff6025c
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-06-26 11:35:11 -07:00
Suren Baghdasaryan
338260a467 Remove ACgroupController_getFlags to fix API breakage
Since ACgroupController_getFlags was introduced after LLNDK freeze it
causes media crashes due to missing symbol. Remove the new function and
re-implement cgroup controller detection to not require flags field but
instead check for its existence on the first access.

Bug: 135049992
Test: libcutils_test with and without CONFIG_CPUSETS enabled
Change-Id: I0220d6a926884dc22a7424d7d0a980c379c6f4eb
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-06-17 16:46:23 +00:00
Suren Baghdasaryan
fa7a05fe5f libprocessgroup: add flags to indicate when a controller failed to mount
Controllers listed in cgroups.json file might fail to mount if kernel is
not configured to support them. We need a way to indicate whether a
controller was successfully mounted and is usable to avoid logging errors
and warnings when a controller that failed to mount is being used. Add
flags bitmask to cgrouprc controller descriptor and use a bit to indicate
that controller is successfully mounted. Modify cpusets_enabled() and
schedboost_enabled() functions to use this bit and report the actual
availability of the controller.

Bug: 124080437
Test: libcutils_test with cpuset and schedtune controllers disabled
Change-Id: I770cc39fe50465146e3205aacf77dc3c56923c5d
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-05-21 10:03:21 -07:00
Yifan Hong
53e0deb7b2 libprocessgroup: use libcgrouprc to read cgroup.rc
Use the LL-NDK library to read cgroup.rc.

As a side-effect, the CgroupController class is changed
to a wrapper of ACgroupController* from the library. The
class itself essentially becomes the pointer, and can be
trivially copied. Modify all client code for this change.

Test: builds and boots
Bug: 123664216

Change-Id: I635d9885db62e82f37421f6d20f59c84a6f4cbb2
Merged-In: I635d9885db62e82f37421f6d20f59c84a6f4cbb2
2019-04-02 22:31:57 -07:00
Yifan Hong
6f9ce2e548 libprocessgroup: Move CgroupSetupCgroups() to libprocessgroup_setup
Only init uses SetupCgroups. This functionality is
moved from libprocessgroup to its own library, and only
init links to it.

Also, merge CgroupSetupCgroups() with CgroupMap::SetupCgroups()
because the former is just an alias of the latter, and
CgroupMap does not belong to libcgrouprc_setup.

Test: boots
Bug: 123664216
Change-Id: I941dc0c415e2b22ae663d43e30dc7a464687325e
Merged-In: I941dc0c415e2b22ae663d43e30dc7a464687325e
2019-04-02 22:31:57 -07:00
Suren Baghdasaryan
5b53573671 libprocessgroup: restrict SetupCgroups to one-time usage and only by init
SetupCgroups is called by init process during early-init stage and is not
supposed to be called again by anyone else. Ensure that the caller is the
init process, make sure cgroup.rc file is written only one time, keep the
file descriptor to cgroup.rc file open by the init process to ensure all
its further mappings stay valid even if the file is deleted.

Bug: 124774415
Test: build, run, verify no errors or warning in the logcat

Change-Id: Ib8822cf0112db7744e28d442182d54dcf06f46f2
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-03-28 01:25:22 +00:00
Suren Baghdasaryan
c8ac3b9152 Revert "libprocessgroup: make sure SetupCgroups is called once and only by init"
This reverts commit 798e805715.

Reason for revert: causes SELinux policy denials when vold calls KillProcessesWithOpenFiles.

Bug: 129298168
Change-Id: Ie00bfc2b743757209b40b55d2ee038d7d20c73ff
2019-03-26 02:49:13 +00:00
Suren Baghdasaryan
798e805715 libprocessgroup: make sure SetupCgroups is called once and only by init
SetupCgroups is called by init process during early-init stage and is not
supposed to be called again by anyone else. Ensure that the caller is the
init process, make sure cgroup.rc file is written only one time, keep the
file descriptor to cgroup.rc file open by the init process to ensure all
its further mappings stay valid even if the file is deleted.

Bug: 124774415
Test: build, run, verify no errors or warning in the logcat

Change-Id: I70ccec551fc07c380333566f618b969667dcf783
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-03-25 16:22:10 +00:00
Peter Collingbourne
dba6d44376 libprocessgroup: Deliberately leak TaskProfiles and CgroupMap instances.
This avoids a race between destruction on process exit and concurrent
access from another thread.

Bug: 129023204
Change-Id: Iecafa67a78e2cce53687156e7f91547b4a23ec13
2019-03-21 17:00:48 +00:00
Wei Wang
d71d30179c Set cg_file_data_ to null when initialization failed
Cannot really do calls if initialization failed.
Also fix LOG usage.

But: 124774415
Test: Build
Change-Id: Ifd7db90cfee51ba5830e1d4db8f8d19b5bc9feba
2019-03-08 10:18:06 -08:00
Suren Baghdasaryan
05da67c390 Add vendor cgroups and task_profiles support
Vendors should be able to specify additional cgroups and task profiles
without changing system files. Add support for /vendor/etc/cgroups.json
and /vendor/etc/task_profiles.json files which will augment cgroups and
task profiles specified in /etc/cgroups.json and /etc/task_profiles.json
system files.

Bug: 124960615
Change-Id: I548c2e866b65c19856a3617d75b75dcd06f7d5b7
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-03-01 00:29:37 +00:00
Suren Baghdasaryan
d032a92c04 Fix cgroup support for recovery mode
In recovery mode a separate set of cgroups should be mounted. Add a
cgroups.recovery.json file to describe cgroups mounted during recovery.
Change cgroups descriptor parset to allow cgroups and cgroups2 sections
to be omitted.

Bug: 124270406
Test: "adb reboot sideload; adb devices" shows 'sideload' as expected

Change-Id: Ic32bd2b4612ec8390064f87d4655d1bd1183d741
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-02-14 17:08:40 +00:00
Suren Baghdasaryan
3a22225ac6 Merge "libprocessgroup: Fix file mode parsing that requires octal values" 2019-02-07 04:54:28 +00:00
Suren Baghdasaryan
e3ad888011 libprocessgroup: Fix cgroup directory ownership setup
When cgroup directory is created its user and ownership gets set, however
because previous code was storing getpwnam() return value the second
invocation would effectively override the previous ones result. Fix this
by copying necessary results. Also change getpwnam() to getgrnam() when
group name is converted into gid. getpwnam() works because of the way
Android uses these ids, however more generally this is incorrect and
getgrnam() should be used instead.

Bug: 111307099
Test: verified user/group membership of the cgroup directories
Change-Id: I78668bc1a36a74f53d8e9825e2d06e3e09501e7a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-02-06 14:28:23 -08:00
Suren Baghdasaryan
c760231891 libprocessgroup: Fix file mode parsing that requires octal values
JSON supports only decimal values and therefore file mode which should
use octal values has to be represented as strings. Fix the implicit
octal-to-decimal conversion and convert from string to octal in the code.

Bug: 111307099
Test: confirmed by verifying correct file permissions

Change-Id: I3ef9de0aea259f93bf74efeffca72d37d4740e15
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-02-06 21:01:41 +00:00
Suren Baghdasaryan
ff25a5f584 Disable SetupCgroup for non-Android targets
Non-android targets should not mount cgroups described in cgroup map
file. When used on non-Android targets SetupCgroup will fail. When
SetupCgroup is called via SetupCgroups a warning will be generated for
each cgroup that fails to mount.

Bug: 111307099
Change-Id: I213a5f9b02f312ba1dd7dc91c89b67334fb939b9
Merged-In: I213a5f9b02f312ba1dd7dc91c89b67334fb939b9
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-02-03 17:55:59 +00:00
Suren Baghdasaryan
82b72a5667 libprocessgroup: Add support for task profiles
Abstract usage of cgroups into task profiles that allows for changes
in cgroup hierarchy and version without affecting framework codebase.
Rework current processgroup and sched_policy API function implementations
to use task profiles instead of hardcoded paths and attributes.
Mount cgroups using information from cgroups.json rather than from init.rc

Exempt-From-Owner-Approval: already approved in internal master

Bug: 111307099
Test: builds, boots

Change-Id: If5532d6dc570add825cebd5b5148e00c7d688e32
Merged-In: If5532d6dc570add825cebd5b5148e00c7d688e32
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-02-03 16:48:35 +00:00