Commit graph

354 commits

Author SHA1 Message Date
Treehugger Robot
b45c8ad05b Merge changes Ie74b9c27,If2224475 into main
* changes:
  libprocessgroup: Check validity of uid and pid arguments for createProcessGroup
  Revert "libprocessgroup: Check validity of uid and pid arguments"
2024-04-18 19:09:18 +00:00
T.J. Mercier
7d9d171e4e libprocessgroup: Check validity of uid and pid arguments for createProcessGroup
killProcessGroup already checks for validity of the uid and pid input
arguments. Also add this checking to createProcessGroup.

Bug: 333261173
Change-Id: Ie74b9c2716978cb5eb434af12e27945e29af876e
2024-04-18 16:18:22 +00:00
T.J. Mercier
29e30f25b4 Revert "libprocessgroup: Check validity of uid and pid arguments"
This reverts commit 5a3c3f7ccf.

Don't SIGABRT for invalid requests.

Bug: 333261173
Change-Id: If22244753b43529ed17c315b3732af371827e774
2024-04-18 16:17:48 +00:00
T.J. Mercier
1cfa2c4111 Add build flag to split the cgroup v2 hierarchy into apps/system
This flag adds "apps" and "system" cgroups underneath the v2 hierarchy
root. Cgroups with UIDs < 10000 (AID_APP_START) will be placed
under "system" and others will be placed under "apps". UIDs under 10000
are reserved for core Android subsystems. This allows us to
apply different cgroup controls collectively to system processes and
normal applications.

Bug: 327480673
Change-Id: I40837dee27a59691f81fef48e66a86c5eacda892
2024-04-08 21:15:48 +00:00
T.J. Mercier
f8901767e6 Add build flag to force memcg to the v2 cgroup hierarchy
This flag prevents memcg from being mounted as a v1 hierarchy, even if
cgroups.json specifies it should be mounted as v1. It will activate
memcg in the v2 hierarchy even if cgroups.json does not specify that it
should be activated in the v2 hierarchy.

The cgroup_disable=memory kernel command line argument will still
prevent memcg from being activated, as this forced memcg controller
is marked as optional.

Bug: 327480673
Change-Id: Iad2491dd0c1576156ee2346928d041d85af890f0
2024-04-08 21:15:48 +00:00
T.J. Mercier
d1e048f956 Use ConvertUid{Pid}ToPath for all path generation
Consolidate into a single implementation.

Test: m
Change-Id: I0fc52db2d4b2973a74bad24c0a5f77384a559cee
2024-04-08 21:15:48 +00:00
T.J. Mercier
44eb705480 Fix unused params and remove unneeded cflags
We already get -Wall and -Werror from the build system, and we do not
want/need -Wexit-time-destructors since it prevents local statics with
non-trivial destructors.

Test: m
Change-Id: I8283bf223404d6c253861d3888c1b720c099386e
2024-04-08 21:15:48 +00:00
Treehugger Robot
ff239bb0fc Merge "Add test mapping" into main 2024-02-08 18:30:25 +00:00
T.J. Mercier
c902df07b0 Add test mapping
Bug: 315371696
Change-Id: If8448e703cc5accde11bcdccf7585ef6779f59b0
2024-02-08 16:57:43 +00:00
T.J. Mercier
96b0065ebf Merge changes I6e95870e,Ibcef6b73,I302ce3c2 into main
* changes:
  libprocessgroup: Use pid_t consistently for TIDs
  libprocessgroup: Use pid_t consistently for PIDs
  libprocessgroup: Use uid_t consistently for UIDs
2024-01-29 21:28:12 +00:00
Treehugger Robot
761db93167 Merge changes from topic "b314861914" into main
* changes:
  Revert "Migrate the blkio controller to the v2 cgroup hierarchy"
  Revert "task_profiles.json: Set io.prio.class"
2024-01-26 15:38:44 +00:00
Randall Huang
d0ce4ca33c Revert "Migrate the blkio controller to the v2 cgroup hierarchy"
This reverts commit f99ca8aeb1.

Reason for revert: b/2768906

Change-Id: I9ce259a2c96e9f8dd8c7598204c35922b56f7a0a
2024-01-26 03:54:47 +00:00
Randall Huang
9e38fe5299 Revert "task_profiles.json: Set io.prio.class"
This reverts commit 9bba87244d.

Reason for revert: b/314861914

Change-Id: I566c4fa98e4d1bd8701086c2b5e34802ee0a89fb
2024-01-26 03:51:54 +00:00
T.J. Mercier
1c007996b3 libprocessgroup: Use pid_t consistently for TIDs
Test: m
Change-Id: I6e95870e6c5855bfe03be3be8f7a8884147bc15d
2024-01-25 16:35:30 +00:00
T.J. Mercier
d6fb2259f6 libprocessgroup: Use pid_t consistently for PIDs
Test: m
Change-Id: Ibcef6b73a8fd3088c88b822a6b33d005595488b2
2024-01-24 23:42:39 +00:00
T.J. Mercier
a757eb856e libprocessgroup: Use uid_t consistently for UIDs
Found with -Wsign-conversion.

Test: m
Change-Id: I302ce3c2ffdd533fe45e98c5c7edbb442db46ae4
2024-01-24 22:48:25 +00:00
T.J. Mercier
a103630b5c libprocessgroup: Use cgroup.kill
By using cgroup.kill we don't need to read cgroup.procs at all for
SIGKILLs, which is more efficient and should help reduce CPU contention
and cgroup lock contention. Fallback to cgroup.procs if we encounter an
error trying to use cgroup.kill, but if cgroup.kill fails it's likely
that cgroup.procs will too.

Bug: 239829790
Test: atest StagedRollbackTest:com.android.tests.rollback.host.StagedRollbackTest#testNativeWatchdogTriggersRebootlessApexRollback
Change-Id: I9da67efd00af49b7b4b502fb742c1095d5c7b9e9
2023-12-14 21:52:13 +00:00
T.J. Mercier
4928b6ef57 Reapply "libprocessgroup: Poll on cgroup.events"
This reverts commit a72332f953.

This change was originally reverted due to a bug in the child commit
which caused process group kills not to occur when using cgroup.kill.
Now that has been fixed, bring back this change with the fixed child.

Bug: 301871933
Change-Id: Ia6c74d9b67a8c88aec4812ac4655646934e0d189
2023-12-14 21:49:14 +00:00
T.J. Mercier
b73d41c50b Merge changes from topic "revert-2819069-LYZVLDOFQA" into main
* changes:
  Revert "libprocessgroup: Poll on cgroup.events"
  Revert "libprocessgroup: Use cgroup.kill"
2023-12-08 01:50:41 +00:00
Android Culprit Assistant
a72332f953 Revert "libprocessgroup: Poll on cgroup.events"
This revert was created by Android Culprit Assistant. The culprit was identified in the following culprit search session (http://go/aca-get/91da3c52-9b76-498b-bdbd-a9de7d7ff53b).

Change-Id: I996c595bee9acc15aedaf0a912f67fa027f33cd0
2023-12-08 01:48:25 +00:00
Android Culprit Assistant
c0ce178640 Revert "libprocessgroup: Use cgroup.kill"
This revert was created by Android Culprit Assistant. The culprit was identified in the following culprit search session (http://go/aca-get/91da3c52-9b76-498b-bdbd-a9de7d7ff53b).

Change-Id: I459265b9c9117d6006c1223947a202505d24c08f
2023-12-08 01:48:30 +00:00
Treehugger Robot
1b44b8c6d6 Merge changes from topic "cgroup.events" into main
* changes:
  libprocessgroup: Use cgroup.kill
  libprocessgroup: Poll on cgroup.events
2023-12-07 19:40:11 +00:00
T.J. Mercier
38b8bb1e4a libprocessgroup: Use cgroup.kill
By using cgroup.kill we don't need to read cgroup.procs at all for
SIGKILLs, which is more efficient and should help reduce CPU contention
and cgroup lock contention. Fallback to cgroup.procs if we encounter an
error trying to use cgroup.kill, but if cgroup.kill fails it's likely
that cgroup.procs will too.

Bug: 239829790
Change-Id: I44706faccfb7c4611b512a3642b913f06d30c1dc
2023-12-07 00:12:07 +00:00
T.J. Mercier
3b5bb3a364 libprocessgroup: Poll on cgroup.events
In killProcessGroup we currently read cgroup.procs to find processes to
kill, send them kill signals until cgroup.procs is empty, then remove
the cgroup directory. The cgroup cannot be removed until all processes
are dead, otherwise we'll get an EBUSY error from the kernel.

There is a race in the kernel where cgroup.procs can read empty even
though the cgroup is pinned by processes which are still exiting, and
can't be removed yet. [1]

Let's use the populated field of cgroup.events instead of an empty
cgroup.procs file to determine when the cgroup is removable. In
addition to functioning like we expect, this is more efficient because
we can poll on cgroup.events instead of retrying kills and rereading
cgroup.procs every 5ms which should help reduce CPU contention and
cgroup lock contention.

It's still possible that it takes longer for a cgroup to become
unpopulated than our timeout allows, in which case we will fail to
remove the cgroup and leak kernel memory. But this change should help
reduce the probability of that happening.

[1] https://lore.kernel.org/all/CABdmKX3SOXpcK85a7cx3iXrwUj=i1yXqEz9i9zNkx8mB=ZXQ8A@mail.gmail.com/

Bug: 301871933
Change-Id: If7dcfb331f47e06994c9ac85ed08bbcce18cdad7
2023-12-07 00:12:00 +00:00
Bart Van Assche
9bba87244d task_profiles.json: Set io.prio.class
Provide an additional way to request the block layer to prioritize
foreground I/O over background I/O. This patch prepares for tests with
the mq-deadline I/O scheduler. While CFQ and BFQ support a "weight"
cgroup attribute, the mq-deadline scheduler does not. The mq-deadline
I/O scheduler only supports the I/O priority class for differentiating
I/O priorities.

The io.prio.class attribute is declared optional since this attribute
only exists if CONFIG_BLK_CGROUP_IOPRIO != n in the kernel
configuration.

Bug: 186902601
Change-Id: Iee1004cd0996e32245aff2b51772ef40178e024f
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-11-30 10:00:44 -08:00
Bart Van Assche
a43b96c157 libprocessgroup: Stop services that have been migrated to another v2 cgroup
CL 2828279 doesn't do what it's description says that it does. Making
Service::Stop() work for processes that have been migrated to another v2
cgroup requires changing DoKillProcessGroupOnce(). Hence this CL that
removes the early return statements from DoKillProcessGroupOnce().

Bug: 308900853
Change-Id: Ib798555feeb95a786a619c3d7013c7d5829b01ad
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-11-27 14:13:46 -08:00
Kelvin Zhang
06d80246c8 Merge "Make RemoveCgroup idempotent" into main 2023-11-21 21:05:43 +00:00
Bart Van Assche
3048812055 Revert "libprocessgroup: Add a function to remove only empty process groups"
Revert this CL because it tests whether or not cgroup.procs files are
empty with the stat() system call and because the cgroup filesystem
always reports st_size == 0. Rename RemoveUidCgroups() into
RemoveEmptyUidCgroups().

Change-Id: I4de6f16c814c4b47d8d74c8045f0c1ee71975ac0
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-11-17 14:28:30 -08:00
Bart Van Assche
f99ca8aeb1 Migrate the blkio controller to the v2 cgroup hierarchy
There are multiple use cases in Android for which background writes need
to be controlled via the cgroup mechanism. The cgroup mechanism can only
control background writes if both the blkio and memcg controllers are
mounted in the v2 cgroup hierarchy. Hence this patch that migrates the
blkio controller from the v1 to the v2 cgroup hierarchy.

The changes compared to the previous version of this CL are as follows:
- The JoinCgroup actions for the "io" controller have been left out
  since these caused processes to be migrated to the v2 root cgroup.
- The BfqWeight / CfqGroupIdle / CfqWeight settings have been included
  in this CL instead of applying these settings as a separate CL.

Change-Id: I67e06ce3462bb1c1345dba78f8d3d655b6519c74
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-11-17 11:37:24 -08:00
Bart Van Assche
2953a9273e libprocessgroup: Reject JoinCgroup actions for v2 controllers
A JoinCgroup action for a v2 cgroup controller migrates a process or task
from the uid_%d/pid_%d cgroup into another cgroup, e.g. the root cgroup.
This may make services unkillable because Service::Stop() only stops a
service if the uid_%d/pid_%d cgroup still exists when Service::Stop() is
called.

Bug: 309674654
Change-Id: I20b797afdf596125ff5a6ed41cb33fe59b84ac88
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-11-17 11:37:24 -08:00
Bart Van Assche
df98534e19 libprocessgroup: Make a log message more detailed
For log messages like the following it is not possible to derive why
this message has been logged:

E libprocessgroup: AddTidToCgroup failed to write '3949'; fd=55: Operation not supported on transport endpoint

Hence include the cgroup path and the tid type in the log message.

Change-Id: I057711fe576b82f6454456b7284186ddeece33c3
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-11-17 10:11:20 -08:00
Kelvin Zhang
459edb0bd0 Make RemoveCgroup idempotent
When attempting to remove a cgroup, a ENOENT means this cgroup is
already removed. Treat such errno as success for idempotency.

Test: th
Bug: 308900853
Change-Id: I6ef3c25f03d185194205b3845784d284fdc4d444
2023-11-15 08:39:41 -08:00
T.J. Mercier
9431b63a7c libprocessgroup: Internal linkage for removeAllProcessGroupsInternal
This function is internal and shouldn't be visible externally.

Bug: 301871933
Change-Id: Ie8261795fa8b010c47da667f21e61949d7a9ae00
2023-11-07 13:52:36 +00:00
T.J. Mercier
4d0d285926 libprocessgroup: Convert incorrect PLOGs to LOGs
PLOG depends on errno being set to a useful value, otherwise it will
print a meaningless error string. A few PLOG call sites occur where
either errno is not set at all, or it is set only some of the time where
there are already PLOG calls closer to where the error occurs. Convert
these PLOG calls to LOG.

Bug: 301871933
Change-Id: Ifa6bd2401f9dd9b84b2506e886336e89bac81bb1
2023-11-03 18:34:56 +00:00
T.J. Mercier
a99e7d84cb libprocessgroup: Use correct language for cgroups
Refer to cgroups and the cgroup hierarchy by their correct names.

Bug: 301871933
Change-Id: Iebb3eac9363018e923773292786d01977c8763ae
2023-11-03 18:34:46 +00:00
T.J. Mercier
599d979126 libprocessgroup: Remove max_processes from KillProcessGroup API
The max_processes calculation is incorrect for KillProcessGroup because
the set of processes in cgroup.procs can differ between the multiple
reads in the implementation. Luckily the exact value isn't very
important because it's just logged. Remove max_processes from the API
and remove the warning about the new behavior in Android 11.

Note that we still always LOG(INFO) that any cgroup is being killed.

Bug: 301871933
Change-Id: I8e449f5089d4a48dbc1797b6d979539e87026f43
2023-10-31 16:31:44 +00:00
T.J. Mercier
af5de39039 Merge "libprocessgroup: Don't sleep after last cgroup removal attempt" into main 2023-10-07 00:34:39 +00:00
T.J. Mercier
b02f94490d libprocessgroup: Don't sleep after last cgroup removal attempt
Currently we sleep for 5ms before decrementing retries for the last
time. This is a waste of time, so bail out of the loop if the last
rmdir attempt fails.

Change-Id: Ia20840d27592b4eb3d9762647b19c111ff94209f
2023-10-06 23:11:09 +00:00
Xin Li
4484eb1bfd Merge Android U (ab/10368041)
Bug: 291102124
Merged-In: I849958d9e9de3be48e32f57acc7b3526e184482c
Change-Id: Ic765b54aa981b6370b5dff6b4cf046bbe9fbdbaf
2023-08-25 13:54:47 -07:00
Suren Baghdasaryan
3483798fd9 libprocessgroup: optimize SetAttributeAction::ExecuteForProcess performance
Current implementation of SetAttributeAction::ExecuteForProcess reuses
SetAttributeAction::ExecuteForTask while not utilizing available uid/pid
information. This results in a call to GetPathForTask() which is an
expensive function due to it reading and parsing /proc/$pid/cgroups.
This can be avoided if we utilize available uid/pid info and the fact
that cgroup v2 attributes share the cgroup v2 hierarchy as process
groups, which use a known path template.

Bug: 292636609
Change-Id: I02e3046bd85d0dfebc68ab444f1796bb54cc69c7
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2023-07-27 21:55:48 +00:00
Suren Baghdasaryan
3507846872 libprocessgroup: fix reset of file_v2_name
ProfileAttribute::Reset does not reset file_v2_name, fix that. Also
provide ProfileAttribute::file_name() to consolidate the code.

Bug: 292636609
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: I5b33ca47b4fa5cabf582c8804bd13f72f6e58411
2023-07-27 21:55:40 +00:00
T.J. Mercier
aa6158b410 libprocessgroup: UIDs in linux are unsigned
We missed two incorrect specifiers in the previous commit with this same
title.

We use the %d format specificier for uid_t, which maps to
__kernel_uid32_t, which is unsigned. [1] This is undefined behavior
which can lead to paths with negative UIDs when erroneously large
values are passed for uid:

E libprocessgroup: No such cgroup attribute: /sys/fs/cgroup/uid_-89846/cgroup.freeze

Fix it with %u.

[1] https://cs.android.com/search?q=typedef.*__kernel_uid32_t&ss=android%2Fplatform%2Fsuperproject%2Fmain

Change-Id: Ica04b03526bd2e156f026a2797fe9912b259cd9f
2023-07-26 22:12:44 +00:00
T.J. Mercier
bf2bebd8e7 libprocessgroup: UIDs in linux are unsigned
We use the %d format specificier for uid_t, which maps to
__kernel_uid32_t, which is unsigned. [1] This is undefined behavior
which can lead to paths with negative UIDs when erroneously large
values are passed for uid:

E libprocessgroup: No such cgroup attribute: /sys/fs/cgroup/uid_-89846/cgroup.freeze

Fix it with %u.

[1] https://cs.android.com/search?q=typedef.*__kernel_uid32_t&ss=android%2Fplatform%2Fsuperproject%2Fmain

Change-Id: Ibb52ba2503e30e2f20770b7d23629167e38d076a
2023-07-21 00:39:57 +00:00
T.J. Mercier
9c8c748c11 libprocessgroup: Remove isolated UID cgroup directories
Global UID level cgroup removal was eliminated because of a race
between app launch and app killing using the same directory name. [1]

However isolated app UIDs are assigned sequentially, and are
basically never reused until we wrap around the large range of
isolated UIDs. This leaves thousands of isolated cgroup directories
unused, which consumes kernel memory and increases memory reclaim
overhead. Remove this subset of UID level cgroup directories when
killing process groups.

[1] d0464b0c01

Test: 50 cycle ACT leaves 1000 fewer empty isolated cgroups
Bug: 290953668
Change-Id: If7d2a7b8eec14561a72208049b74ff785ca961bd
2023-07-12 18:29:21 +00:00
Bart Van Assche
07f639d4d9 Revert "libprocessgroup: Add I/O scheduler attributes to task_profiles.json" am: 9fe0400080
Original change: https://googleplex-android-review.googlesource.com/c/platform/system/core/+/23221374

Change-Id: I557ccd30751d0d6af0924c32259aa94354f3ef0e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2023-05-16 16:35:22 +00:00
Bart Van Assche
de4ac00254 Revert "Updating Attributes on task_profiles.json" am: f4a3d72ee8
Original change: https://googleplex-android-review.googlesource.com/c/platform/system/core/+/23221373

Change-Id: I0af83a1250ee7f774c8f88958700b061da6bfdb3
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2023-05-16 16:35:21 +00:00
Bart Van Assche
e8a7a44037 Revert "task_profiles.json: Convert tabs into spaces" am: 878406e7f1
Original change: https://googleplex-android-review.googlesource.com/c/platform/system/core/+/23221372

Change-Id: I111a8d79047776938fec4cbafa545a711802c8d2
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2023-05-16 16:35:19 +00:00
Bart Van Assche
9fe0400080 Revert "libprocessgroup: Add I/O scheduler attributes to task_profiles.json"
This reverts commit 9c0fcbb0f7.

Bug: 261857030
Ignore-AOSP-First: this change is for the U branch only.
Change-Id: Id4bd3494b33dd6bc0644406d599c9bfd597c7435
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-05-12 16:35:06 -07:00
Bart Van Assche
f4a3d72ee8 Revert "Updating Attributes on task_profiles.json"
This reverts commit 92153fb955.

Bug: 261857030
Ignore-AOSP-First: this change is for the U branch only.
Change-Id: I99417707f0d0b8c7dca3927b6ce9d52436621f4e
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-05-12 16:35:01 -07:00
Bart Van Assche
878406e7f1 Revert "task_profiles.json: Convert tabs into spaces"
This reverts commit 6ad747ac2d.

Bug: 261857030
Ignore-AOSP-First: this change is for the U branch only.
Change-Id: I93447b71146e6e9297ef49026d90dc4c35b91244
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2023-05-12 16:34:02 -07:00