Commit graph

114 commits

Author SHA1 Message Date
Suren Baghdasaryan
911109c414 libprocessgroup: Prevent SetProcessProfiles from using cached fd
Because we cache file descriptors associated with cgroup "tasks" file it
should not be used with SetProcessProfiles API which operates on entire
processes rather than tasks. Change SetProcessProfiles API to prevent
cache fd usage, modify ExecuteForProcess to not attempt to use cached
fd. Also fix unconditional calls to EnableResourceCaching from
ExecuteForTask which should be called only when SetTaskProfiles is used
with use_fd_cache set to true.

Bug: 149524788
Change-Id: I880efaf8217a4dd7ccfbb4fb167b2295cefc057a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2020-02-14 01:42:11 +00:00
Quentin Perret
20b2f1e0ab task_profiles.json: Fix uclamp attribute
task_profiles.json currently uses attributes of a defunct version of
uclamp, which was used for testing.

Let's use the attributes that actually got merged upstream instead.

Bug: 143710295
Change-Id: Ic6a14f52e14efeefc91f6e6bf580c15e2eded82e
Signed-off-by: Quentin Perret <qperret@google.com>
2020-02-04 13:10:31 +00: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
8438595f21 libprocessgroup: Prevent aggregate profiles from referencing stale ones
Currently when vendor overrides a profile the profile object is being
replaced with a new one. However the old profile might have been
referenced by an aggregate profile and with such profile replacement
the aggregate profile is left referencing a stale object. Fix this by
replacing the content of the old profile with the content from the new
one instead of replacing the object itself.

Bug: 148311066
Test: override profiles referenced in aggregate profile and verify
Test: correct replacement
Change-Id: Iabddbf3580455e5263fedad6665cf52fb323e50a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2020-01-27 21:07:27 +00:00
Quentin Perret
64811b7b13 libprocessgroup: ensure schedboost_enabled is true with uclamp
In the current state, schedboost_enabled() is true if and only if
schedtune is in use. As a result, all tests conditioned by
schedboost_enabled() will be skipped on devices using uclamp since it is
and extension of the CPU controller.

Fix this by making schedboost_enabled() return true if either schedtune
or the CPU controller is enabled.

Bug: 44953631
Change-Id: Idaadf252c9cf411a176180ab8988d559ca8a1332
Signed-off-by: Quentin Perret <qperret@google.com>
2020-01-27 11:04:03 +00:00
Tom Cherry
d89ed132a0 init: always kill oneshot services' process groups.
~2007 a change was added that would allow oneshot services to
daemonize by not killing their process group.  This was a hack at the
time, and should certainly not be needed now.  I've resisted removing
the behavior however, as it hadn't caused any issues.

Recently, it was detected that the cgroups that these processes belong
to, would exist forever and therefore leak memory.  Instead of simply
removing the cgroups when empty, this provides a good opportunity to
do the right thing and fix this behavior once and for all.

The new (correct) behavior only happens for devices with vendor images
built for Android R or later.  Init will log a warning to dmesg when
it detects this difference in behavior has occurred.

Bug: 144545923
Test: boot CF/Coral and see no difference in behavior.
Test: boot CF with a service that daemonizes and see the warning.
Change-Id: I333a2e25a541ec0114ac50ab8ae7f1ea3f055447
2019-11-19 15:13:17 -08:00
Marco Ballesio
726a68cb40 Add support for freezer cgroup
Adds the freezer cgroup to process groups

Change-Id: Ib7a8dbe776ff156ff3827b9a659365384f3e6ac8
Bug: 143308662
Test: manual - frozen processes aren't scheduled for execution until
unfrozen
2019-11-13 20:21:07 +00:00
Haibo Huang
cf8a5b0e0e Fix build with new googletest
The new googletest requires prefix to be non-empty.

Exempt-From-Owner-Approval: fix test
Change-Id: Ic039cb657b455919e429a3ec2107893b1734e8a1
2019-11-10 04:08:25 +00:00
Wei Wang
ee2f260223 sched_policy: add get_cpuset/sched_policy_profile_name
Expose API to return name of task profile for cpuset/sched policy so
that libprocessgroup clients using SetTaskProfiles directly don't have
to maintain the mapping. This reduces the risk of inconsistency and saves
memory.

Bug: 139521784
Test: atest libcutils_test:SchedPolicy
Change-Id: I414312a038613913fb6a827bdcefceb3dec21264
2019-10-14 20:03:06 -07:00
Rick Yiu
0b211fa8b3 libprocessgroup: add support to set aggregate profiles
To support setting multiple profiles with one call. The json format
is as below example.

  "AggregateProfiles": [
    ...
    {
      "Name": "SCHED_SP_BACKGROUND",
      "Profiles": [ "HighEnergySaving", "LowIoPriority", "TimerSlackHigh" ]
    },
    ...
  }

Bug: 139521784
Test: SetProfile works as expected
Change-Id: Ibe14ed57d5169cafcbcbbdb054df3ed171a2f6a2
2019-09-30 08:35:28 +00:00
Tom Cherry
73f5b65ed4 Add surenb@ to libprocessgroup OWNERS
Test: n/a

Change-Id: Ia671384e4202f618d0ae08bc33f0ca8bbd9e53c1
2019-09-17 08:50:04 -07:00
Wei Wang
73e355409a Let blkio cgroup follow sched_policy
This reverts commit e8678cf883.

With b/131214885 resolved, we should reland the change for bg sched group.
If a thread explicitly calls set_sched_policy to set a thread to background, it should not be latency sensitive.

This would help background I/O impacting foreground I/O e.g. b/65727988

Test: build and boot
Bug: 140209629
Change-Id: I218db084fb733cad57af8a5eae926818a6a2cdb8
2019-08-29 03:51:32 +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
Riddle Hsu
a6abd821d5 Allow to drop existing fd cache of cgroup path
A process can give up the permission to set cgroup. If we still
keep the fd that was cached before losing the permission, when
the process sets scheduling group, it will write to the cached
fd without checking if is accessible and lead to sepolicy denied.

Bug: 123043091
Test: Build and boot.
Test: A new process from zygote set cgroup and drop fd cache, and
      then specializes to app domain. There is no sepolicy denied
      when the process creates new thread.
      (android::thread_data_t::trampoline)

Change-Id: I285ee91424ea965ea9c670fc0f6662948e3e2ce5
2019-06-18 15:53:53 +08:00
Treehugger Robot
ad6f656be2 Merge "Remove ACgroupController_getFlags to fix API breakage" 2019-06-18 01:26:31 +00:00
Steven Moreland
b495b0d403 libprocessgroup: move native_bridge_support
To avoid merge conflict.

Bug: 135293492
Bug: 135298384
Test: N/A
Change-Id: I034b989c7e0f8a477b29ef7ca40bba8f2dc929e5
2019-06-17 12:48:06 -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
mtk16036
53f79e6861 race condition in libprocessgroup
while enable fdsan (file descriptor sanitizer),
fdsan report use-after-close error after boot complete (sedom).

Because, in SetCgroupAction::EnableResourceCaching() currently has a data race against all the
use fd_ functions like SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) etc.

ThreadA                                     | ThreadB
-------------------------------------------------------------------------------------------------
in SetCgroupAction::EnableResourceCaching() | in SetCgroupAction::ExecuteForProcess(...)
-------------------------------------------------------------------------------------------------
                                            | in SetCgroupAction::AddTidToCgroup(int tid, int fd)
-------------------------------------------------------------------------------------------------
fd_ = std::move(fd); /*modified fd_ value*/ |
-------------------------------------------------------------------------------------------------
                                            | write(fd)  /* crash here, fd is closed by ThreadA*/
-------------------------------------------------------------------------------------------------

So, add mutex lock to protect fd_ data race.

Bug: 134120826
Test: auto test, run the adb reboot test 100 times and no fdsan error report on libprocessgroup
Change-Id: Iccf2f705e030f79324f1164509e715dc5be825de
2019-06-11 10:52:25 -07: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
dimitry
49c1f6efea Enable native_bridge_support for libprocessgroup
Android build system added support for building translated binaries
used on natively bridged targets (arm on x86 for example).

However in order to avoid building unnecessary binaries and libraries
for such architectures most modules do not support native bridge by default.

All needed modules have to explicitly indicate if they may be used as part
of translated binary build.

This change enabled native bridge support for libprocessgroup modules
needed by libutils greylisted library (available for apps targeting
Android version < N).

Bug: http://b/77159578
Test: make
Change-Id: I8d6f33e95e9f7424f5fa12f6fbabafa1d6c37413
2019-05-16 14:19:09 +02:00
Vic Yang
d167cb2958 Static link libjsoncpp in libprocessgroup
The majority of libjsoncpp use is due to libprocessgroup.  Static
linking reduces relocations required at runtime as well as compacts the
pages dirtied by relocations.  On a 32-bit system, where this matters
the most, this reduces 8KB of dirty pages per libprocessgroup load.
Overall, the dirty page reduction on 32-bit cuttlefish is ~500KB.

Bug: 132275636
Test: Boot on cuttlefish.  Calculate total amount of dirty memory used
      for libprocessgroup and libjsoncpp.
Change-Id: I1135bb45a3764f96a4a3a47c98fbcdee3913c988
(cherry picked from commit b562e68503)
2019-05-08 20:29:44 -07:00
Suren Baghdasaryan
3bdf315eae libprocessgroup: limit libprocessgroup's VNDK API surface
Limit libprocessgroup VNDK API to the minimum set required for task
profiles usage. This API allows vendors to use cgroups without accessing
cgroup files directly, therefore allowing Android to change cgroup
arrangement details without breaking vendor code.

Bug: 131098932
Test: build and boot
Change-Id: I92463dfb44a108a133bafd2fe52237b6b1d50a69
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-04-22 20:13:13 +00:00
Suren Baghdasaryan
8a315d2a3e Re-enable file descriptor caching and add option to skip caching
This reverts commit bee9f5718b
"libprocessgroup: Disable file descriptor caching temporarily" and adds
option to use SetTaskProfiles and SetProcessProfiles without file caching.
This option is used from JNI to avoid access denials because cached files
are not whitelisted for JNI usage.

Bug: 123868658
Bug: 123043091
Test: boot using svelte target
Change-Id: I76b9d6af8a1dd4464cb3cf3e6dc327980efdf361
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-04-20 00:11:21 +00:00
Treehugger Robot
5c421695f0 Merge "Let blkio cgroup follow cpuset cgroup only" 2019-04-17 01:51:33 +00:00
Rick Yiu
e8678cf883 Let blkio cgroup follow cpuset cgroup only
Some app may have different cgroup settings in cpuset and schedtune for its
threads, so let blkio follow cpuset only, which represents the app's current
state more accurately. Otherwise, if that thread is doing IO, then its
performance will be affected because its blkio group is in lower priority
group as schedtune.

ex: an app is now in top-app, but some thread of it set schedtune group to
background, and blkio follows schedtune because it is called later.

Main thread:
6:schedtune:/top-app
5:memory:/
4:cpuset:/top-app
3:cpuacct:/uid_1000/pid_8766
2:cpu:/
1:blkio:/
0::/

Some thread:
6:schedtune:/background
5:memory:/
4:cpuset:/top-app
3:cpuacct:/uid_1000/pid_8766
2:cpu:/
1:blkio:/background
0::/

Bug: 124727032
Test: blkio has same settings with cpuset
Change-Id: I9a140c7d9d93e1dd43c34c8cf066f4a62e2bf604
2019-04-16 22:20:58 +00:00
Treehugger Robot
cc5e2dc2e8 Merge "CgroupSetupCgroups: don't leak fd." 2019-04-04 17:27:27 +00:00
Yifan Hong
5ece395328 libcgrouprc: version script: linux only
mac builds doesn't support version script.
This fixes the build.

Test: pass

Change-Id: Ia3d06e4eff05b41982b3dfbb946efa41358b2c76
2019-04-03 14:44:46 -07:00
Yifan Hong
8df75fc304 CgroupSetupCgroups: don't leak fd.
- If file is unlinked, the mmapped region is still there.
- If file is truncated, a SIGBUS will be seen, and holding
  the fd doesn't help.

Test: boots (sanity)
Bug: 123664216
Change-Id: I6683804bc795fab6798891a4471e5fe58fbffe13
2019-04-03 12:13:26 -07:00
Yifan Hong
9d7b89abea CgroupSetupCgroups -> CgroupSetup
Test: builds
Bug: 123664216
Change-Id: I47c46ca9ba5c1fbf3f9f7a1b185dc48b058b1e32
Merged-In: I47c46ca9ba5c1fbf3f9f7a1b185dc48b058b1e32
2019-04-02 22:31:57 -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
d8ce1fb1d1 libprocessgroup_setup: use libcgrouprc_format
Use CgroupController definition from libcgrouprc_format, not
libprocessgroup, because the wire format will be removed
from libprocessgroup later.

Bug: 123664216
Test: builds
Change-Id: If5e2301a1b65c3f6a51a1661cfeeed4e299f634e
Merged-In: If5e2301a1b65c3f6a51a1661cfeeed4e299f634e
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
Yifan Hong
098323ba5b libprocessgroup: Add libcgrouprc
This module is an LL-NDK library that can be loaded by
modules that link to libprocessgroup (which is in VNDK).
This module defines APIs that reads cgroups.rc file
programatically. Internally, it uses libcgrouprc_format to
do so.

Test: builds
Bug: 123664216

Change-Id: I9c13c0528461758154e23cbab3a94ade7fb351ee
Merged-In: I9c13c0528461758154e23cbab3a94ade7fb351ee
2019-04-02 22:31:57 -07:00
Yifan Hong
c4b3ab4edc libprocessgroup: Add libcgrouprc_format
This module defines the wire format of the mmap()ed cgroup.rc
file.

Test: builds
Bug: 123664216

Change-Id: Iaf6199f759a6264590b13ca7ba6d7f576c3ed56a
Merged-In: Iaf6199f759a6264590b13ca7ba6d7f576c3ed56a
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
Elliott Hughes
be87d447b4 libprocessgroup: add -Wexit-time-destructors.
Exit time destructors already caused trouble (see
dba6d44376), and without this warning,
it's too easy to reintroduce issues.

Bug: 129023204
Test: builds
Change-Id: If5fb4d0c11c74ba4971bb218059c6d37edeaf4df
2019-03-26 18:50:22 -07: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
Yifan Hong
830f837231 Merge changes from topic "libprocessgroup"
* changes:
  libprocessgroup: Add VTS tests for vendor JSON files
  libprocessgroup_proto_test split into multiple files
2019-03-18 16:53:47 +00:00
Yifan Hong
e8e2756f93 libprocessgroup: Add VTS tests for vendor JSON files
Test: vts_processgroup_validate_test
Bug: 123664216
Change-Id: I5518e34cde6fc30195fc99133f86cb23ae3be92f
2019-03-15 16:28:42 -07:00
Yifan Hong
28a44eb6f6 libprocessgroup_proto_test split into multiple files
... so that they can be reused later.

Also, removed test to strull and inline static constexpr variable.

Bug: 123664216
Test: libprocessgroup_proto_test
Change-Id: Ia6b93c6689d7531e6f597d1387434aed1b3efb0a
2019-03-15 12:34:42 -07:00
Elliott Hughes
08b4d32139 libprocessgroup: PLOG already includes strerror(errno).
Test: builds
Change-Id: Ic1fe6b46144f7dbeb7ffba5534d94b4dae192d5c
2019-03-14 20:06:36 -07: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
Treehugger Robot
8dcd839158 Merge "Reduce log spam from libprocessgroup" 2019-03-01 22:11:15 +00:00
Suren Baghdasaryan
e681df44eb libprocessgroup: convert Json::Value to const refs
Convert Json::Value to const refs to prevent creation of a null member
if specified element does not exist. Fix variable naming in the parser
while we are at it.

Change-Id: I7dabfd1c73983b465984dd6c411c9fe48743a348
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-03-01 00:29:49 +00: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
Rick Yiu
e9d149fd7e Unify some text format in task_profiles.json
Bug: 111307099
Test: cgroup function is normal
Change-Id: Id992b875720ff67a5d1a2b052072ee7ea7b16a9a
2019-02-27 21:08:19 +08:00
Treehugger Robot
d5345f58fd Merge "Add blkio cgroup to libprocessgroup" 2019-02-27 13:04:00 +00:00
Wei Wang
1770566de6 Fix string reference usage
Bug: 124774415
Test: Build
Change-Id: Ic0e5ee4f3375c2fa21fbc2ce3796b7368e9e289c
2019-02-27 06:04:13 +00:00