blob: 3a79fa08f803277f2846c3dcc7c95cadb927b500 [file] [log] [blame]
Ronghua Wu231c3d12015-03-11 15:10:32 -07001/*
2**
3** Copyright 2015, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
19#define LOG_TAG "ResourceManagerService"
20#include <utils/Log.h>
21
Chong Zhangfdd512a2019-11-22 11:03:14 -080022#include <android/binder_manager.h>
23#include <android/binder_process.h>
Chong Zhangc7303e82020-12-02 16:38:52 -080024#include <binder/IPCThreadState.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070025#include <binder/IServiceManager.h>
Chong Zhangee33d642019-08-08 14:26:43 -070026#include <cutils/sched_policy.h>
Chong Zhang181e6952019-10-09 13:23:39 -070027#include <media/MediaResourcePolicy.h>
Girish1f002cf2023-02-17 00:36:29 +000028#include <media/stagefright/foundation/ABase.h>
Chong Zhangee33d642019-08-08 14:26:43 -070029#include <mediautils/BatteryNotifier.h>
Girish1f002cf2023-02-17 00:36:29 +000030#include <mediautils/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070031#include <mediautils/SchedulingPolicyService.h>
Girish1484e5d2023-11-20 06:00:44 +000032#include <com_android_media_codec_flags.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070033
Girish1f002cf2023-02-17 00:36:29 +000034#include "ResourceManagerMetrics.h"
Girish1484e5d2023-11-20 06:00:44 +000035#include "ResourceManagerServiceNew.h"
Chong Zhanga9d45c72020-09-09 12:41:17 -070036#include "ResourceObserverService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070037#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070038
Girish1484e5d2023-11-20 06:00:44 +000039namespace CodecFeatureFlags = com::android::media::codec::flags;
40
Ronghua Wu231c3d12015-03-11 15:10:32 -070041namespace android {
42
Girish6a6044d2023-11-22 21:23:14 +000043void ResourceManagerService::getResourceDump(std::string& resourceLog) const {
44 PidResourceInfosMap mapCopy;
45 std::map<int, int> overridePidMapCopy;
46 {
47 std::scoped_lock lock{mLock};
48 mapCopy = mMap; // Shadow copy, real copy will happen on write.
49 overridePidMapCopy = mOverridePidMap;
50 }
51
52 const size_t SIZE = 256;
53 char buffer[SIZE];
54 resourceLog.append(" Processes:\n");
55 for (const auto& [pid, infos] : mapCopy) {
56 snprintf(buffer, SIZE, " Pid: %d\n", pid);
57 resourceLog.append(buffer);
58 int priority = 0;
59 if (getPriority_l(pid, &priority)) {
60 snprintf(buffer, SIZE, " Priority: %d\n", priority);
61 } else {
62 snprintf(buffer, SIZE, " Priority: <unknown>\n");
63 }
64 resourceLog.append(buffer);
65
66 for (const auto& [infoKey, info] : infos) {
67 resourceLog.append(" Client:\n");
68 snprintf(buffer, SIZE, " Id: %lld\n", (long long)info.clientId);
69 resourceLog.append(buffer);
70
71 std::string clientName = info.name;
72 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
73 resourceLog.append(buffer);
74
75 const ResourceList& resources = info.resources;
76 resourceLog.append(" Resources:\n");
Girishd11a03a2023-11-30 21:17:51 +000077 resourceLog.append(resources.toString());
Dongwon Kangfe508d32015-12-15 14:22:05 +090078 }
79 }
Girish6a6044d2023-11-22 21:23:14 +000080
81 resourceLog.append(" Process Pid override:\n");
82 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
83 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
84 it->first, it->second);
85 resourceLog.append(buffer);
86 }
Dongwon Kangfe508d32015-12-15 14:22:05 +090087}
88
Brian Lindahl64ee9452022-01-14 13:31:16 +010089binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -070090 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -070091
dcashman014e91e2015-09-11 09:33:01 -070092 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
93 result.format("Permission Denial: "
94 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -080095 AIBinder_getCallingPid(),
96 AIBinder_getCallingUid());
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +000097 write(fd, result.c_str(), result.size());
dcashman014e91e2015-09-11 09:33:01 -070098 return PERMISSION_DENIED;
99 }
100
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700101 bool supportsMultipleSecureCodecs;
102 bool supportsSecureWithNonSecureCodec;
103 String8 serviceLog;
104 {
Girish434b4d82023-07-11 23:24:54 +0000105 std::scoped_lock lock{mLock};
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700106 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
107 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
108 serviceLog = mServiceLog->toString(" " /* linePrefix */);
109 }
110
Girish6a6044d2023-11-22 21:23:14 +0000111 // Get all the resource (and overload pid) logs
112 std::string resourceLog;
113 getResourceDump(resourceLog);
114
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700115 const size_t SIZE = 256;
116 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700117 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
118 result.append(buffer);
119 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700120 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700121 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700122 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
123 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700124 result.append(buffer);
125
Girish6a6044d2023-11-22 21:23:14 +0000126 result.append(resourceLog.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700127
Ronghua Wu022ed722015-05-11 15:15:09 -0700128 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700129 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700130
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000131 write(fd, result.c_str(), result.size());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700132 return OK;
133}
134
Brian Lindahl64ee9452022-01-14 13:31:16 +0100135struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800136 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700137
Chong Zhangdd726802019-08-21 17:24:13 -0700138 virtual void noteStartVideo(int uid) override {
139 BatteryNotifier::getInstance().noteStartVideo(uid);
140 }
141 virtual void noteStopVideo(int uid) override {
142 BatteryNotifier::getInstance().noteStopVideo(uid);
143 }
144 virtual void noteResetVideo() override {
145 BatteryNotifier::getInstance().noteResetVideo();
146 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800147 virtual bool requestCpusetBoost(bool enable) override {
148 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700149 }
150
151protected:
152 virtual ~SystemCallbackImpl() {}
153
154private:
155 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800156 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700157};
158
159ResourceManagerService::ResourceManagerService()
160 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
161
Brian Lindahl64ee9452022-01-14 13:31:16 +0100162ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700163 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700164 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700165 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700166 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700167 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700168 mSupportsSecureWithNonSecureCodec(true),
Girishab17b0f2023-11-20 06:00:44 +0000169 mCpuBoostCount(0) {
Chong Zhangdd726802019-08-21 17:24:13 -0700170 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000171 // Create ResourceManagerMetrics that handles all the metrics.
172 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700173}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700174
Chong Zhangfdd512a2019-11-22 11:03:14 -0800175//static
176void ResourceManagerService::instantiate() {
Girishab17b0f2023-11-20 06:00:44 +0000177 std::shared_ptr<ResourceManagerService> service = Create();
Chong Zhangfdd512a2019-11-22 11:03:14 -0800178 binder_status_t status =
Charles Chen5b097ef2023-03-15 01:21:22 +0000179 AServiceManager_addServiceWithFlags(
Charles Chene55549f2023-03-15 01:21:22 +0000180 service->asBinder().get(), getServiceName(),
181 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800182 if (status != STATUS_OK) {
183 return;
184 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700185
186 std::shared_ptr<ResourceObserverService> observerService =
187 ResourceObserverService::instantiate();
188
189 if (observerService != nullptr) {
190 service->setObserverService(observerService);
191 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800192 // TODO: mediaserver main() is already starting the thread pool,
193 // move this to mediaserver main() when other services in mediaserver
194 // are converted to ndk-platform aidl.
195 //ABinderProcess_startThreadPool();
196}
197
Girishab17b0f2023-11-20 06:00:44 +0000198std::shared_ptr<ResourceManagerService> ResourceManagerService::Create() {
199 return Create(new ProcessInfo(), new SystemCallbackImpl());
200}
201
202std::shared_ptr<ResourceManagerService> ResourceManagerService::Create(
203 const sp<ProcessInfoInterface>& processInfo,
204 const sp<SystemCallbackInterface>& systemResource) {
Girish6a6044d2023-11-22 21:23:14 +0000205 std::shared_ptr<ResourceManagerService> service = nullptr;
Girish1484e5d2023-11-20 06:00:44 +0000206 // If codec importance feature is on, create the refactored implementation.
207 if (CodecFeatureFlags::codec_importance()) {
Girish6a6044d2023-11-22 21:23:14 +0000208 service = ::ndk::SharedRefBase::make<ResourceManagerServiceNew>(processInfo,
209 systemResource);
210 } else {
211 service = ::ndk::SharedRefBase::make<ResourceManagerService>(processInfo,
212 systemResource);
Girish1484e5d2023-11-20 06:00:44 +0000213 }
Girish6a6044d2023-11-22 21:23:14 +0000214
215 if (service != nullptr) {
216 service->init();
217 }
218
219 return service;
Girishab17b0f2023-11-20 06:00:44 +0000220}
221
Girish1484e5d2023-11-20 06:00:44 +0000222// TEST only function.
223std::shared_ptr<ResourceManagerService> ResourceManagerService::CreateNew(
224 const sp<ProcessInfoInterface>& processInfo,
225 const sp<SystemCallbackInterface>& systemResource) {
Girish6a6044d2023-11-22 21:23:14 +0000226 std::shared_ptr<ResourceManagerService> service =
227 ::ndk::SharedRefBase::make<ResourceManagerServiceNew>(processInfo, systemResource);
228 service->init();
229 return service;
Girish1484e5d2023-11-20 06:00:44 +0000230}
231
Girish6a6044d2023-11-22 21:23:14 +0000232void ResourceManagerService::init() {}
233
Ronghua Wu231c3d12015-03-11 15:10:32 -0700234ResourceManagerService::~ResourceManagerService() {}
235
Chong Zhanga9d45c72020-09-09 12:41:17 -0700236void ResourceManagerService::setObserverService(
237 const std::shared_ptr<ResourceObserverService>& observerService) {
238 mObserverService = observerService;
239}
240
Chong Zhang181e6952019-10-09 13:23:39 -0700241Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000242 String8 log = String8::format("config(%s)", getString(policies).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700243 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700244
Girish434b4d82023-07-11 23:24:54 +0000245 std::scoped_lock lock{mLock};
Ronghua Wu231c3d12015-03-11 15:10:32 -0700246 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700247 const std::string &type = policies[i].type;
248 const std::string &value = policies[i].value;
249 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700250 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700251 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700252 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700253 }
254 }
Chong Zhang181e6952019-10-09 13:23:39 -0700255 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700256}
257
Girishab17b0f2023-11-20 06:00:44 +0000258void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700259 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700260 if (resource.type == MediaResource::Type::kCpuBoost
261 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700262 // Request it on every new instance of kCpuBoost, as the media.codec
263 // could have died, if we only do it the first time subsequent instances
264 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800265 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700266 ALOGW("couldn't request cpuset boost");
267 }
268 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700269 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000270 && (resource.subType == MediaResource::SubType::kHwVideoCodec
271 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000272 mSystemCB->noteStartVideo(uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700273 }
274}
275
Girishab17b0f2023-11-20 06:00:44 +0000276void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhang181e6952019-10-09 13:23:39 -0700277 if (resource.type == MediaResource::Type::kCpuBoost
278 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700279 && mCpuBoostCount > 0) {
280 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800281 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700282 }
Chong Zhang181e6952019-10-09 13:23:39 -0700283 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000284 && (resource.subType == MediaResource::SubType::kHwVideoCodec
285 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000286 mSystemCB->noteStopVideo(uid);
Robert Shihc3af31b2019-09-20 21:45:01 -0700287 }
288}
289
Girish9128e242022-11-23 20:52:29 +0000290Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800291 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700292 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000293 int32_t pid = clientInfo.pid;
294 int32_t uid = clientInfo.uid;
295 int64_t clientId = clientInfo.id;
Girish9128e242022-11-23 20:52:29 +0000296 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000297 pid, uid, (long long) clientId, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700298 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700299
Girish434b4d82023-07-11 23:24:54 +0000300 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100301 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800302 pid_t callingPid = IPCThreadState::self()->getCallingPid();
303 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100304 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
305 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800306 pid = callingPid;
307 uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800308 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700309 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish27365ed2023-10-11 20:20:55 +0000310 ResourceInfo& info = getResourceInfoForEdit(clientInfo, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700311 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700312
313 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700314 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700315
316 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
317 ALOGW("Ignoring request to remove negative value of non-drm resource");
318 continue;
319 }
Girishd11a03a2023-11-30 21:17:51 +0000320 bool isNewEntry = false;
321 if (!info.resources.add(res, &isNewEntry)) {
322 continue;
323 }
324 if (isNewEntry) {
Girishab17b0f2023-11-20 06:00:44 +0000325 onFirstAdded(res, info.uid);
Chong Zhang79d2b282018-04-17 14:14:31 -0700326 }
Girishd11a03a2023-11-30 21:17:51 +0000327
Chong Zhanga9d45c72020-09-09 12:41:17 -0700328 // Add it to the list of added resources for observers.
Girishd11a03a2023-11-30 21:17:51 +0000329 resourceAdded.add(res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700330 }
Girishf1d166c2023-07-20 22:35:29 +0000331 if (info.deathNotifier == nullptr && client != nullptr) {
Girishab17b0f2023-11-20 06:00:44 +0000332 info.deathNotifier = DeathNotifier::Create(
333 client, ref<ResourceManagerService>(), clientInfo);
Wonsik Kim3e378962017-01-05 17:00:02 +0900334 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700335 if (mObserverService != nullptr && !resourceAdded.empty()) {
336 mObserverService->onResourceAdded(uid, pid, resourceAdded);
337 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900338 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000339
Chong Zhang181e6952019-10-09 13:23:39 -0700340 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700341}
342
Girish9128e242022-11-23 20:52:29 +0000343Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700344 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000345 int32_t pid = clientInfo.pid;
346 int32_t uid = clientInfo.uid;
347 int64_t clientId = clientInfo.id;
348 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000349 pid, uid, (long long) clientId, getString(resources).c_str());
Chong Zhangfb092d32019-08-12 09:45:44 -0700350 mServiceLog->add(log);
351
Girish434b4d82023-07-11 23:24:54 +0000352 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100353 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800354 pid_t callingPid = IPCThreadState::self()->getCallingPid();
355 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
356 pid, callingPid);
357 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700358 }
Girish434b4d82023-07-11 23:24:54 +0000359 PidResourceInfosMap::iterator found = mMap.find(pid);
360 if (found == mMap.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700361 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700362 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700363 }
Girish434b4d82023-07-11 23:24:54 +0000364 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700365
Girish434b4d82023-07-11 23:24:54 +0000366 ResourceInfos::iterator foundClient = infos.find(clientId);
367 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700368 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700369 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700370 }
371
Girish434b4d82023-07-11 23:24:54 +0000372 ResourceInfo& info = foundClient->second;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700373 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700374 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700375 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700376
377 if (res.value < 0) {
378 ALOGW("Ignoring request to remove negative value of resource");
379 continue;
380 }
Girishd11a03a2023-11-30 21:17:51 +0000381
382 long removedEntryValue = -1;
383 if (info.resources.remove(res, &removedEntryValue)) {
Chong Zhanga9d45c72020-09-09 12:41:17 -0700384 MediaResourceParcel actualRemoved = res;
Girishd11a03a2023-11-30 21:17:51 +0000385 if (removedEntryValue != -1) {
Girishab17b0f2023-11-20 06:00:44 +0000386 onLastRemoved(res, info.uid);
Girishd11a03a2023-11-30 21:17:51 +0000387 actualRemoved.value = removedEntryValue;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700388 }
389
390 // Add it to the list of removed resources for observers.
Girishd11a03a2023-11-30 21:17:51 +0000391 resourceRemoved.add(actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700392 }
393 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700394 if (mObserverService != nullptr && !resourceRemoved.empty()) {
395 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
396 }
Chong Zhang181e6952019-10-09 13:23:39 -0700397 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700398}
399
Girish9128e242022-11-23 20:52:29 +0000400Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
401 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700402 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900403}
404
Girish9128e242022-11-23 20:52:29 +0000405Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
406 int32_t pid = clientInfo.pid;
407 int32_t uid = clientInfo.uid;
408 int64_t clientId = clientInfo.id;
409 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
410 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700411 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700412
Girish434b4d82023-07-11 23:24:54 +0000413 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100414 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800415 pid_t callingPid = IPCThreadState::self()->getCallingPid();
416 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
417 pid, callingPid);
418 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800419 }
Girish434b4d82023-07-11 23:24:54 +0000420 PidResourceInfosMap::iterator found = mMap.find(pid);
421 if (found == mMap.end()) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700422 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700423 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700424 }
Girish434b4d82023-07-11 23:24:54 +0000425 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700426
Girish434b4d82023-07-11 23:24:54 +0000427 ResourceInfos::iterator foundClient = infos.find(clientId);
428 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700429 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700430 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700431 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700432
Girish434b4d82023-07-11 23:24:54 +0000433 const ResourceInfo& info = foundClient->second;
Girishd11a03a2023-11-30 21:17:51 +0000434 for (const MediaResourceParcel& res : info.resources.getResources()) {
435 onLastRemoved(res, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700436 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700437
Girish1f002cf2023-02-17 00:36:29 +0000438 // Since this client has been removed, update the metrics collector.
439 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000440
Chong Zhanga9d45c72020-09-09 12:41:17 -0700441 if (mObserverService != nullptr && !info.resources.empty()) {
442 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
443 }
444
Girish434b4d82023-07-11 23:24:54 +0000445 infos.erase(foundClient);
Chong Zhang181e6952019-10-09 13:23:39 -0700446 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700447}
448
Girish56fda312023-10-12 21:32:35 +0000449void ResourceManagerService::getClientForResource_l(
450 const ResourceRequestInfo& resourceRequestInfo,
451 std::vector<ClientInfo>& clientsInfo) {
Girishaff0ef22023-10-13 00:22:39 +0000452 int callingPid = resourceRequestInfo.mCallingPid;
Girish56fda312023-10-12 21:32:35 +0000453 const MediaResourceParcel* res = resourceRequestInfo.mResource;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700454 if (res == NULL) {
455 return;
456 }
Girishaff0ef22023-10-13 00:22:39 +0000457
458 // Before looking into other processes, check if we have clients marked for
459 // pending removal in the same process.
Girishab17b0f2023-11-20 06:00:44 +0000460 ClientInfo clientInfo;
461 if (getBiggestClientPendingRemoval_l(callingPid, res->type, res->subType, clientInfo)) {
462 clientsInfo.emplace_back(clientInfo);
Girishaff0ef22023-10-13 00:22:39 +0000463 return;
464 }
465
466 // Now find client(s) from a lowest priority process that has needed resources.
Girish56fda312023-10-12 21:32:35 +0000467 if (getLowestPriorityBiggestClient_l(resourceRequestInfo, clientInfo)) {
468 clientsInfo.push_back(clientInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700469 }
470}
471
Girish0ac5c212023-11-23 09:14:03 +0000472bool ResourceManagerService::getTargetClients(
Girish88a83502023-11-23 11:23:07 +0000473 const ClientInfoParcel& clientInfo,
Girish0ac5c212023-11-23 09:14:03 +0000474 const std::vector<MediaResourceParcel>& resources,
475 std::vector<ClientInfo>& targetClients) {
Girish88a83502023-11-23 11:23:07 +0000476 int32_t callingPid = clientInfo.pid;
Girish0ac5c212023-11-23 09:14:03 +0000477 std::scoped_lock lock{mLock};
478 if (!mProcessInfo->isPidTrusted(callingPid)) {
479 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
480 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
481 callingPid, actualCallingPid);
482 callingPid = actualCallingPid;
483 }
484 const MediaResourceParcel *secureCodec = NULL;
485 const MediaResourceParcel *nonSecureCodec = NULL;
486 const MediaResourceParcel *graphicMemory = NULL;
487 const MediaResourceParcel *drmSession = NULL;
488 for (size_t i = 0; i < resources.size(); ++i) {
489 switch (resources[i].type) {
490 case MediaResource::Type::kSecureCodec:
491 secureCodec = &resources[i];
492 break;
493 case MediaResource::Type::kNonSecureCodec:
494 nonSecureCodec = &resources[i];
495 break;
496 case MediaResource::Type::kGraphicMemory:
497 graphicMemory = &resources[i];
498 break;
499 case MediaResource::Type::kDrmSession:
500 drmSession = &resources[i];
501 break;
502 default:
503 break;
504 }
505 }
506
507 // first pass to handle secure/non-secure codec conflict
508 if (secureCodec != NULL) {
509 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
510 .subType = secureCodec->subType};
511 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
512 if (!mSupportsMultipleSecureCodecs) {
513 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
514 return false;
515 }
516 }
517 if (!mSupportsSecureWithNonSecureCodec) {
518 mediaResource.type = MediaResource::Type::kNonSecureCodec;
519 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
520 return false;
521 }
522 }
523 }
524 if (nonSecureCodec != NULL) {
525 if (!mSupportsSecureWithNonSecureCodec) {
526 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
527 .subType = nonSecureCodec->subType};
528 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
529 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
530 return false;
531 }
532 }
533 }
534
535 if (drmSession != NULL) {
536 ResourceRequestInfo resourceRequestInfo{callingPid, drmSession};
537 getClientForResource_l(resourceRequestInfo, targetClients);
538 if (targetClients.size() == 0) {
539 return false;
540 }
541 }
542
543 if (targetClients.size() == 0 && graphicMemory != nullptr) {
544 // if no secure/non-secure codec conflict, run second pass to handle other resources.
545 ResourceRequestInfo resourceRequestInfo{callingPid, graphicMemory};
546 getClientForResource_l(resourceRequestInfo, targetClients);
547 }
548
549 if (targetClients.size() == 0) {
550 // if we are here, run the third pass to free one codec with the same type.
551 if (secureCodec != nullptr) {
552 ResourceRequestInfo resourceRequestInfo{callingPid, secureCodec};
553 getClientForResource_l(resourceRequestInfo, targetClients);
554 }
555 if (nonSecureCodec != nullptr) {
556 ResourceRequestInfo resourceRequestInfo{callingPid, nonSecureCodec};
557 getClientForResource_l(resourceRequestInfo, targetClients);
558 }
559 }
560
561 if (targetClients.size() == 0) {
562 // if we are here, run the fourth pass to free one codec with the different type.
563 if (secureCodec != nullptr) {
564 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
565 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
566 getClientForResource_l(resourceRequestInfo, targetClients);
567 }
568 if (nonSecureCodec != nullptr) {
569 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
570 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
571 getClientForResource_l(resourceRequestInfo, targetClients);
572 }
573 }
574
575 return !targetClients.empty();
576}
577
Girish9128e242022-11-23 20:52:29 +0000578Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100579 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000580 std::string clientName = clientInfo.name;
581 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Girish88a83502023-11-23 11:23:07 +0000582 clientInfo.pid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700583 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700584 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700585
Girish0ac5c212023-11-23 09:14:03 +0000586 // Check if there are any resources to be reclaimed before processing.
587 if (resources.empty()) {
588 return Status::ok();
589 }
590
Girish56fda312023-10-12 21:32:35 +0000591 std::vector<ClientInfo> targetClients;
Girish88a83502023-11-23 11:23:07 +0000592 if (!getTargetClients(clientInfo, resources, targetClients)) {
Girish0ac5c212023-11-23 09:14:03 +0000593 // Nothing to reclaim from.
594 ALOGI("%s: There aren't any clients to reclaim from", __func__);
595 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700596 }
597
Girish56fda312023-10-12 21:32:35 +0000598 *_aidl_return = reclaimUnconditionallyFrom(targetClients);
Girish9128e242022-11-23 20:52:29 +0000599
600 // Log Reclaim Pushed Atom to statsd
Girish56fda312023-10-12 21:32:35 +0000601 pushReclaimAtom(clientInfo, targetClients, *_aidl_return);
Girish9128e242022-11-23 20:52:29 +0000602
Wonsik Kim271429d2020-10-01 10:12:56 -0700603 return Status::ok();
604}
605
Girish9128e242022-11-23 20:52:29 +0000606void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000607 const std::vector<ClientInfo>& targetClients,
608 bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000609 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000610 int requesterPriority = -1;
611 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000612 std::vector<int> priorities;
613 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000614
Girish56fda312023-10-12 21:32:35 +0000615 for (const ClientInfo& targetClient : targetClients) {
Girish9128e242022-11-23 20:52:29 +0000616 int targetPriority = -1;
Girish56fda312023-10-12 21:32:35 +0000617 getPriority_l(targetClient.mPid, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000618 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000619 }
Girish56fda312023-10-12 21:32:35 +0000620 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, targetClients, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000621}
622
Girishab17b0f2023-11-20 06:00:44 +0000623std::shared_ptr<IResourceManagerClient> ResourceManagerService::getClient(
624 int pid, const int64_t& clientId) const {
625 std::map<int, ResourceInfos>::const_iterator found = mMap.find(pid);
626 if (found == mMap.end()) {
627 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
628 return nullptr;
629 }
630
631 const ResourceInfos& infos = found->second;
632 ResourceInfos::const_iterator foundClient = infos.find(clientId);
633 if (foundClient == infos.end()) {
634 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
635 return nullptr;
636 }
637
638 return foundClient->second.client;
639}
640
641bool ResourceManagerService::removeClient(int pid, const int64_t& clientId) {
642 std::map<int, ResourceInfos>::iterator found = mMap.find(pid);
643 if (found == mMap.end()) {
644 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
645 return false;
646 }
647
648 ResourceInfos& infos = found->second;
649 ResourceInfos::iterator foundClient = infos.find(clientId);
650 if (foundClient == infos.end()) {
651 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
652 return false;
653 }
654
655 infos.erase(foundClient);
656 return true;
657}
658
Brian Lindahl64ee9452022-01-14 13:31:16 +0100659bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000660 const std::vector<ClientInfo>& targetClients) {
661 if (targetClients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700662 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700663 }
664
Girishab17b0f2023-11-20 06:00:44 +0000665 int64_t failedClientId = -1;
666 int32_t failedClientPid = -1;
Girish56fda312023-10-12 21:32:35 +0000667 for (const ClientInfo& targetClient : targetClients) {
Girishab17b0f2023-11-20 06:00:44 +0000668 std::shared_ptr<IResourceManagerClient> client = getClient(
669 targetClient.mPid, targetClient.mClientId);
670 if (client == nullptr) {
Girish56fda312023-10-12 21:32:35 +0000671 // skip already released clients.
672 continue;
673 }
Girishab17b0f2023-11-20 06:00:44 +0000674 String8 log = String8::format("reclaimResource from client %p", client.get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700675 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700676 bool success;
Girishab17b0f2023-11-20 06:00:44 +0000677 Status status = client->reclaimResource(&success);
Chong Zhang181e6952019-10-09 13:23:39 -0700678 if (!status.isOk() || !success) {
Girishab17b0f2023-11-20 06:00:44 +0000679 failedClientId = targetClient.mClientId;
680 failedClientPid = targetClient.mPid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700681 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700682 }
683 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700684
Girishab17b0f2023-11-20 06:00:44 +0000685 if (failedClientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700686 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700687 }
688
Ronghua Wu67e7f542015-03-13 10:47:08 -0700689 {
Girish434b4d82023-07-11 23:24:54 +0000690 std::scoped_lock lock{mLock};
Girishab17b0f2023-11-20 06:00:44 +0000691 bool found = removeClient(failedClientPid, failedClientId);
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200692 if (found) {
693 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
694 } else {
695 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700696 }
697 }
698
Wonsik Kim271429d2020-10-01 10:12:56 -0700699 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700700}
701
Girish6a6044d2023-11-22 21:23:14 +0000702bool ResourceManagerService::overridePid_l(int32_t originalPid, int32_t newPid) {
703 mOverridePidMap.erase(originalPid);
704 if (newPid != -1) {
705 mOverridePidMap.emplace(originalPid, newPid);
706 return true;
707 }
708
709 return false;
710}
711
Brian Lindahl64ee9452022-01-14 13:31:16 +0100712Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800713 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
714 originalPid, newPid);
715 mServiceLog->add(log);
716
717 // allow if this is called from the same process or the process has
718 // permission.
719 if ((AIBinder_getCallingPid() != getpid()) &&
720 (checkCallingPermission(String16(
721 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
722 ALOGE(
723 "Permission Denial: can't access overridePid method from pid=%d, "
724 "self pid=%d\n",
725 AIBinder_getCallingPid(), getpid());
726 return Status::fromServiceSpecificError(PERMISSION_DENIED);
727 }
728
729 {
Girish434b4d82023-07-11 23:24:54 +0000730 std::scoped_lock lock{mLock};
Girish6a6044d2023-11-22 21:23:14 +0000731 if (overridePid_l(originalPid, newPid)) {
Girish1f002cf2023-02-17 00:36:29 +0000732 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800733 }
734 }
735
736 return Status::ok();
737}
738
Girish6a6044d2023-11-22 21:23:14 +0000739bool ResourceManagerService::overrideProcessInfo_l(
740 const std::shared_ptr<IResourceManagerClient>& client,
741 int pid,
742 int procState,
743 int oomScore) {
744 removeProcessInfoOverride_l(pid);
745
746 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
747 // Override value is rejected by ProcessInfo.
748 return false;
749 }
750
751 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
752 .uid = 0,
753 .id = 0,
754 .name = "<unknown client>"};
755 auto deathNotifier = DeathNotifier::Create(
756 client, ref<ResourceManagerService>(), clientInfo, true);
757
758 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
759 return true;
760}
761
Chong Zhang97d367b2020-09-16 12:53:14 -0700762Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100763 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700764 int oomScore) {
765 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
766 pid, procState, oomScore);
767 mServiceLog->add(log);
768
769 // Only allow the override if the caller already can access process state and oom scores.
770 int callingPid = AIBinder_getCallingPid();
771 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
772 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
773 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
774 return Status::fromServiceSpecificError(PERMISSION_DENIED);
775 }
776
777 if (client == nullptr) {
778 return Status::fromServiceSpecificError(BAD_VALUE);
779 }
780
Girish434b4d82023-07-11 23:24:54 +0000781 std::scoped_lock lock{mLock};
Girish6a6044d2023-11-22 21:23:14 +0000782 if (!overrideProcessInfo_l(client, pid, procState, oomScore)) {
Chong Zhang97d367b2020-09-16 12:53:14 -0700783 // Override value is rejected by ProcessInfo.
784 return Status::fromServiceSpecificError(BAD_VALUE);
785 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700786 return Status::ok();
Girish6a6044d2023-11-22 21:23:14 +0000787
Chong Zhang97d367b2020-09-16 12:53:14 -0700788}
789
Chong Zhang97d367b2020-09-16 12:53:14 -0700790void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000791 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700792
793 removeProcessInfoOverride_l(pid);
794}
795
796void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
797 auto it = mProcessInfoOverrideMap.find(pid);
798 if (it == mProcessInfoOverrideMap.end()) {
799 return;
800 }
801
802 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700803 mProcessInfoOverrideMap.erase(pid);
804}
805
Girish9128e242022-11-23 20:52:29 +0000806Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
807 int32_t pid = clientInfo.pid;
808 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700809 String8 log = String8::format(
810 "markClientForPendingRemoval(pid %d, clientId %lld)",
811 pid, (long long) clientId);
812 mServiceLog->add(log);
813
Girish434b4d82023-07-11 23:24:54 +0000814 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100815 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800816 pid_t callingPid = IPCThreadState::self()->getCallingPid();
817 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
818 pid, callingPid);
819 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700820 }
Girish434b4d82023-07-11 23:24:54 +0000821 PidResourceInfosMap::iterator found = mMap.find(pid);
822 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700823 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
824 pid, (long long)clientId);
825 return Status::ok();
826 }
Girish434b4d82023-07-11 23:24:54 +0000827 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700828
Girish434b4d82023-07-11 23:24:54 +0000829 ResourceInfos::iterator foundClient = infos.find(clientId);
830 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700831 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
832 return Status::ok();
833 }
834
Girish434b4d82023-07-11 23:24:54 +0000835 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700836 info.pendingRemoval = true;
837 return Status::ok();
838}
839
Wonsik Kim271429d2020-10-01 10:12:56 -0700840Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
841 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
842 mServiceLog->add(log);
843
Girish56fda312023-10-12 21:32:35 +0000844 std::vector<ClientInfo> targetClients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700845 {
Girish434b4d82023-07-11 23:24:54 +0000846 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100847 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800848 pid_t callingPid = IPCThreadState::self()->getCallingPid();
849 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
850 pid, callingPid);
851 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700852 }
853
854 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
855 MediaResource::Type::kNonSecureCodec,
856 MediaResource::Type::kGraphicMemory,
857 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100858 switch (type) {
859 // Codec resources are segregated by audio, video and image domains.
860 case MediaResource::Type::kSecureCodec:
861 case MediaResource::Type::kNonSecureCodec:
Girisha5a2d672023-09-20 18:40:20 +0000862 for (MediaResource::SubType subType : {MediaResource::SubType::kHwAudioCodec,
863 MediaResource::SubType::kSwAudioCodec,
864 MediaResource::SubType::kHwVideoCodec,
865 MediaResource::SubType::kSwVideoCodec,
866 MediaResource::SubType::kHwImageCodec,
867 MediaResource::SubType::kSwImageCodec}) {
Girishab17b0f2023-11-20 06:00:44 +0000868 ClientInfo clientInfo;
869 if (getBiggestClientPendingRemoval_l(pid, type, subType, clientInfo)) {
870 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100871 continue;
872 }
873 }
874 break;
875 // Non-codec resources are shared by audio, video and image codecs (no subtype).
876 default:
Girishab17b0f2023-11-20 06:00:44 +0000877 ClientInfo clientInfo;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100878 if (getBiggestClientPendingRemoval_l(pid, type,
Girishab17b0f2023-11-20 06:00:44 +0000879 MediaResource::SubType::kUnspecifiedSubType, clientInfo)) {
880 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100881 }
882 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700883 }
884 }
885 }
886
Girish56fda312023-10-12 21:32:35 +0000887 if (!targetClients.empty()) {
888 reclaimUnconditionallyFrom(targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700889 }
890 return Status::ok();
891}
892
Girish6a6044d2023-11-22 21:23:14 +0000893bool ResourceManagerService::getPriority_l(int pid, int* priority) const {
Henry Fang32762922020-01-28 18:40:39 -0800894 int newPid = pid;
895
Girish6a6044d2023-11-22 21:23:14 +0000896 std::map<int, int>::const_iterator found = mOverridePidMap.find(pid);
897 if (found != mOverridePidMap.end()) {
898 newPid = found->second;
Henry Fang32762922020-01-28 18:40:39 -0800899 ALOGD("getPriority_l: use override pid %d instead original pid %d",
900 newPid, pid);
901 }
902
903 return mProcessInfo->getPriority(newPid, priority);
904}
905
Girish56fda312023-10-12 21:32:35 +0000906bool ResourceManagerService::getAllClients_l(
907 const ResourceRequestInfo& resourceRequestInfo,
908 std::vector<ClientInfo>& clientsInfo) {
909 MediaResource::Type type = resourceRequestInfo.mResource->type;
910 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Girish9128e242022-11-23 20:52:29 +0000911
Girish434b4d82023-07-11 23:24:54 +0000912 for (auto& [pid, infos] : mMap) {
913 for (const auto& [id, info] : infos) {
914 if (hasResourceType(type, subType, info.resources)) {
Girish56fda312023-10-12 21:32:35 +0000915 if (!isCallingPriorityHigher_l(resourceRequestInfo.mCallingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700916 // some higher/equal priority process owns the resource,
917 // this request can't be fulfilled.
Girish56fda312023-10-12 21:32:35 +0000918 ALOGE("%s: can't reclaim resource %s from pid %d",
919 __func__, asString(type), pid);
920 clientsInfo.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700921 return false;
922 }
Girishab17b0f2023-11-20 06:00:44 +0000923 clientsInfo.emplace_back(pid, info.uid, info.clientId);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700924 }
925 }
926 }
Girish56fda312023-10-12 21:32:35 +0000927 if (clientsInfo.size() == 0) {
928 ALOGV("%s: didn't find any resource %s", __func__, asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700929 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700930 return true;
931}
932
Girishaff0ef22023-10-13 00:22:39 +0000933// Process priority (oom score) based reclaim:
934// - Find a process with lowest priority (than that of calling process).
935// - Find the bigegst client (with required resources) from that process.
Girish56fda312023-10-12 21:32:35 +0000936bool ResourceManagerService::getLowestPriorityBiggestClient_l(
937 const ResourceRequestInfo& resourceRequestInfo,
Girishab17b0f2023-11-20 06:00:44 +0000938 ClientInfo& clientInfo) {
Girish56fda312023-10-12 21:32:35 +0000939 int callingPid = resourceRequestInfo.mCallingPid;
940 MediaResource::Type type = resourceRequestInfo.mResource->type;
941 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700942 int lowestPriorityPid;
943 int lowestPriority;
944 int callingPriority;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700945
Henry Fang32762922020-01-28 18:40:39 -0800946 if (!getPriority_l(callingPid, &callingPriority)) {
Girishaff0ef22023-10-13 00:22:39 +0000947 ALOGE("%s: can't get process priority for pid %d", __func__, callingPid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700948 return false;
949 }
Brian Lindahl64ee9452022-01-14 13:31:16 +0100950 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700951 return false;
952 }
953 if (lowestPriority <= callingPriority) {
Girishaff0ef22023-10-13 00:22:39 +0000954 ALOGE("%s: lowest priority %d vs caller priority %d",
955 __func__, lowestPriority, callingPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700956 return false;
957 }
958
Girishab17b0f2023-11-20 06:00:44 +0000959 if (!getBiggestClient_l(lowestPriorityPid, type, subType, clientInfo)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700960 return false;
961 }
Girish9128e242022-11-23 20:52:29 +0000962
Girishaff0ef22023-10-13 00:22:39 +0000963 ALOGI("%s: CallingProcess(%d:%d) will reclaim from the lowestPriorityProcess(%d:%d)",
964 __func__, callingPid, callingPriority, lowestPriorityPid, lowestPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700965 return true;
966}
967
Brian Lindahl64ee9452022-01-14 13:31:16 +0100968bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
969 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700970 int pid = -1;
971 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +0000972 for (auto& [tempPid, infos] : mMap) {
973 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700974 // no client on this process.
975 continue;
976 }
Girish434b4d82023-07-11 23:24:54 +0000977 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700978 // doesn't have the requested resource type
979 continue;
980 }
Girish434b4d82023-07-11 23:24:54 +0000981 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -0800982 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700983 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
984 // TODO: remove this pid from mMap?
985 continue;
986 }
987 if (pid == -1 || tempPriority > priority) {
988 // initial the value
989 pid = tempPid;
990 priority = tempPriority;
991 }
992 }
993 if (pid != -1) {
994 *lowestPriorityPid = pid;
995 *lowestPriority = priority;
996 }
997 return (pid != -1);
998}
999
1000bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1001 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001002 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001003 return false;
1004 }
1005
1006 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001007 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001008 return false;
1009 }
1010
1011 return (callingPidPriority < priority);
1012}
1013
Brian Lindahl64ee9452022-01-14 13:31:16 +01001014bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +00001015 MediaResource::SubType subType, ClientInfo& clientInfo) {
1016 return getBiggestClient_l(pid, type, subType, clientInfo, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001017}
1018
1019bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +00001020 MediaResource::SubType subType, ClientInfo& clientInfo, bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001021 PidResourceInfosMap::iterator found = mMap.find(pid);
1022 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001023 ALOGE_IF(!pendingRemovalOnly,
1024 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001025 return false;
1026 }
1027
Girishab17b0f2023-11-20 06:00:44 +00001028 uid_t uid = -1;
1029 int64_t clientId = -1;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001030 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001031 const ResourceInfos& infos = found->second;
1032 for (const auto& [id, info] : infos) {
1033 const ResourceList& resources = info.resources;
1034 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001035 continue;
1036 }
Girishd11a03a2023-11-30 21:17:51 +00001037 for (const MediaResourceParcel& resource : resources.getResources()) {
Brian Lindahl64ee9452022-01-14 13:31:16 +01001038 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001039 if (resource.value > largestValue) {
1040 largestValue = resource.value;
Girishab17b0f2023-11-20 06:00:44 +00001041 clientId = info.clientId;
Girish434b4d82023-07-11 23:24:54 +00001042 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001043 }
1044 }
1045 }
1046 }
1047
Girishab17b0f2023-11-20 06:00:44 +00001048 if (clientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001049 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001050 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1051 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001052 return false;
1053 }
1054
Girishab17b0f2023-11-20 06:00:44 +00001055 clientInfo.mPid = pid;
1056 clientInfo.mUid = uid;
1057 clientInfo.mClientId = clientId;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001058 return true;
1059}
1060
Girish1f002cf2023-02-17 00:36:29 +00001061Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1062 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1063 return Status::ok();
1064}
1065
1066Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1067 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1068 return Status::ok();
1069}
1070
1071Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1072 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1073 return Status::ok();
1074}
1075
Girishde8eb592023-04-13 18:49:17 +00001076Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1077 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1078 return Status::ok();
1079}
1080
Girish1f002cf2023-02-17 00:36:29 +00001081long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1082 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1083}
1084
1085long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1086 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1087}
1088
Girish6a6044d2023-11-22 21:23:14 +00001089void ResourceManagerService::notifyClientReleased(const ClientInfoParcel& clientInfo) {
1090 mResourceManagerMetrics->notifyClientReleased(clientInfo);
1091}
1092
Ronghua Wu231c3d12015-03-11 15:10:32 -07001093} // namespace android