blob: d37d89390a330e8c080b0fe44d176283cb2d7e40 [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;
Girish1c682402024-01-31 21:03:51 +0000477 int64_t clientId = clientInfo.id;
Girish0ac5c212023-11-23 09:14:03 +0000478 std::scoped_lock lock{mLock};
479 if (!mProcessInfo->isPidTrusted(callingPid)) {
480 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
481 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
482 callingPid, actualCallingPid);
483 callingPid = actualCallingPid;
484 }
485 const MediaResourceParcel *secureCodec = NULL;
486 const MediaResourceParcel *nonSecureCodec = NULL;
487 const MediaResourceParcel *graphicMemory = NULL;
488 const MediaResourceParcel *drmSession = NULL;
489 for (size_t i = 0; i < resources.size(); ++i) {
490 switch (resources[i].type) {
491 case MediaResource::Type::kSecureCodec:
492 secureCodec = &resources[i];
493 break;
494 case MediaResource::Type::kNonSecureCodec:
495 nonSecureCodec = &resources[i];
496 break;
497 case MediaResource::Type::kGraphicMemory:
498 graphicMemory = &resources[i];
499 break;
500 case MediaResource::Type::kDrmSession:
501 drmSession = &resources[i];
502 break;
503 default:
504 break;
505 }
506 }
507
508 // first pass to handle secure/non-secure codec conflict
509 if (secureCodec != NULL) {
510 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
511 .subType = secureCodec->subType};
Girish1c682402024-01-31 21:03:51 +0000512 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, &mediaResource};
Girish0ac5c212023-11-23 09:14:03 +0000513 if (!mSupportsMultipleSecureCodecs) {
514 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
515 return false;
516 }
517 }
518 if (!mSupportsSecureWithNonSecureCodec) {
519 mediaResource.type = MediaResource::Type::kNonSecureCodec;
520 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
521 return false;
522 }
523 }
524 }
525 if (nonSecureCodec != NULL) {
526 if (!mSupportsSecureWithNonSecureCodec) {
527 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
528 .subType = nonSecureCodec->subType};
Girish1c682402024-01-31 21:03:51 +0000529 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, &mediaResource};
Girish0ac5c212023-11-23 09:14:03 +0000530 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
531 return false;
532 }
533 }
534 }
535
536 if (drmSession != NULL) {
Girish1c682402024-01-31 21:03:51 +0000537 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, drmSession};
Girish0ac5c212023-11-23 09:14:03 +0000538 getClientForResource_l(resourceRequestInfo, targetClients);
539 if (targetClients.size() == 0) {
540 return false;
541 }
542 }
543
544 if (targetClients.size() == 0 && graphicMemory != nullptr) {
545 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish1c682402024-01-31 21:03:51 +0000546 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, graphicMemory};
Girish0ac5c212023-11-23 09:14:03 +0000547 getClientForResource_l(resourceRequestInfo, targetClients);
548 }
549
550 if (targetClients.size() == 0) {
551 // if we are here, run the third pass to free one codec with the same type.
552 if (secureCodec != nullptr) {
Girish1c682402024-01-31 21:03:51 +0000553 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, secureCodec};
Girish0ac5c212023-11-23 09:14:03 +0000554 getClientForResource_l(resourceRequestInfo, targetClients);
555 }
556 if (nonSecureCodec != nullptr) {
Girish1c682402024-01-31 21:03:51 +0000557 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, nonSecureCodec};
Girish0ac5c212023-11-23 09:14:03 +0000558 getClientForResource_l(resourceRequestInfo, targetClients);
559 }
560 }
561
562 if (targetClients.size() == 0) {
563 // if we are here, run the fourth pass to free one codec with the different type.
564 if (secureCodec != nullptr) {
565 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish1c682402024-01-31 21:03:51 +0000566 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, &temp};
Girish0ac5c212023-11-23 09:14:03 +0000567 getClientForResource_l(resourceRequestInfo, targetClients);
568 }
569 if (nonSecureCodec != nullptr) {
570 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish1c682402024-01-31 21:03:51 +0000571 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, &temp};
Girish0ac5c212023-11-23 09:14:03 +0000572 getClientForResource_l(resourceRequestInfo, targetClients);
573 }
574 }
575
576 return !targetClients.empty();
577}
578
Girish9128e242022-11-23 20:52:29 +0000579Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100580 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000581 std::string clientName = clientInfo.name;
582 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Girish88a83502023-11-23 11:23:07 +0000583 clientInfo.pid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700584 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700585 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700586
Girish0ac5c212023-11-23 09:14:03 +0000587 // Check if there are any resources to be reclaimed before processing.
588 if (resources.empty()) {
Girishe7b338f2024-02-08 22:03:51 +0000589 // Invalid reclaim request. So no need to log.
Girish0ac5c212023-11-23 09:14:03 +0000590 return Status::ok();
591 }
592
Girish56fda312023-10-12 21:32:35 +0000593 std::vector<ClientInfo> targetClients;
Girishe7b338f2024-02-08 22:03:51 +0000594 if (getTargetClients(clientInfo, resources, targetClients)) {
595 // Reclaim all the target clients.
596 *_aidl_return = reclaimUnconditionallyFrom(targetClients);
597 } else {
598 // No clients to reclaim from.
Girish0ac5c212023-11-23 09:14:03 +0000599 ALOGI("%s: There aren't any clients to reclaim from", __func__);
Girishe7b338f2024-02-08 22:03:51 +0000600 // We need to log this failed reclaim as "no clients to reclaim from".
601 targetClients.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700602 }
603
Girish9128e242022-11-23 20:52:29 +0000604 // Log Reclaim Pushed Atom to statsd
Girish56fda312023-10-12 21:32:35 +0000605 pushReclaimAtom(clientInfo, targetClients, *_aidl_return);
Girish9128e242022-11-23 20:52:29 +0000606
Wonsik Kim271429d2020-10-01 10:12:56 -0700607 return Status::ok();
608}
609
Girish9128e242022-11-23 20:52:29 +0000610void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000611 const std::vector<ClientInfo>& targetClients,
612 bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000613 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000614 int requesterPriority = -1;
615 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000616 std::vector<int> priorities;
617 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000618
Girish56fda312023-10-12 21:32:35 +0000619 for (const ClientInfo& targetClient : targetClients) {
Girish9128e242022-11-23 20:52:29 +0000620 int targetPriority = -1;
Girish56fda312023-10-12 21:32:35 +0000621 getPriority_l(targetClient.mPid, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000622 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000623 }
Girish56fda312023-10-12 21:32:35 +0000624 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, targetClients, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000625}
626
Girishe8305272023-12-18 19:17:58 +0000627std::shared_ptr<IResourceManagerClient> ResourceManagerService::getClient_l(
Girishab17b0f2023-11-20 06:00:44 +0000628 int pid, const int64_t& clientId) const {
629 std::map<int, ResourceInfos>::const_iterator found = mMap.find(pid);
630 if (found == mMap.end()) {
631 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
632 return nullptr;
633 }
634
635 const ResourceInfos& infos = found->second;
636 ResourceInfos::const_iterator foundClient = infos.find(clientId);
637 if (foundClient == infos.end()) {
638 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
639 return nullptr;
640 }
641
642 return foundClient->second.client;
643}
644
Girishe8305272023-12-18 19:17:58 +0000645bool ResourceManagerService::removeClient_l(int pid, const int64_t& clientId) {
Girishab17b0f2023-11-20 06:00:44 +0000646 std::map<int, ResourceInfos>::iterator found = mMap.find(pid);
647 if (found == mMap.end()) {
648 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
649 return false;
650 }
651
652 ResourceInfos& infos = found->second;
653 ResourceInfos::iterator foundClient = infos.find(clientId);
654 if (foundClient == infos.end()) {
655 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
656 return false;
657 }
658
659 infos.erase(foundClient);
660 return true;
661}
662
Brian Lindahl64ee9452022-01-14 13:31:16 +0100663bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000664 const std::vector<ClientInfo>& targetClients) {
665 if (targetClients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700666 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700667 }
668
Girishab17b0f2023-11-20 06:00:44 +0000669 int64_t failedClientId = -1;
670 int32_t failedClientPid = -1;
Girish56fda312023-10-12 21:32:35 +0000671 for (const ClientInfo& targetClient : targetClients) {
Girishe8305272023-12-18 19:17:58 +0000672 std::shared_ptr<IResourceManagerClient> client = nullptr;
673 {
674 std::scoped_lock lock{mLock};
675 client = getClient_l(targetClient.mPid, targetClient.mClientId);
676 }
Girishab17b0f2023-11-20 06:00:44 +0000677 if (client == nullptr) {
Girish56fda312023-10-12 21:32:35 +0000678 // skip already released clients.
679 continue;
680 }
Girishab17b0f2023-11-20 06:00:44 +0000681 String8 log = String8::format("reclaimResource from client %p", client.get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700682 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700683 bool success;
Girishab17b0f2023-11-20 06:00:44 +0000684 Status status = client->reclaimResource(&success);
Chong Zhang181e6952019-10-09 13:23:39 -0700685 if (!status.isOk() || !success) {
Girishab17b0f2023-11-20 06:00:44 +0000686 failedClientId = targetClient.mClientId;
687 failedClientPid = targetClient.mPid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700688 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700689 }
690 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700691
Girishab17b0f2023-11-20 06:00:44 +0000692 if (failedClientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700693 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700694 }
695
Ronghua Wu67e7f542015-03-13 10:47:08 -0700696 {
Girish434b4d82023-07-11 23:24:54 +0000697 std::scoped_lock lock{mLock};
Girishe8305272023-12-18 19:17:58 +0000698 bool found = removeClient_l(failedClientPid, failedClientId);
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200699 if (found) {
700 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
701 } else {
702 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700703 }
704 }
705
Wonsik Kim271429d2020-10-01 10:12:56 -0700706 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700707}
708
Girish6a6044d2023-11-22 21:23:14 +0000709bool ResourceManagerService::overridePid_l(int32_t originalPid, int32_t newPid) {
710 mOverridePidMap.erase(originalPid);
711 if (newPid != -1) {
712 mOverridePidMap.emplace(originalPid, newPid);
713 return true;
714 }
715
716 return false;
717}
718
Brian Lindahl64ee9452022-01-14 13:31:16 +0100719Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800720 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
721 originalPid, newPid);
722 mServiceLog->add(log);
723
724 // allow if this is called from the same process or the process has
725 // permission.
726 if ((AIBinder_getCallingPid() != getpid()) &&
727 (checkCallingPermission(String16(
728 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
729 ALOGE(
730 "Permission Denial: can't access overridePid method from pid=%d, "
731 "self pid=%d\n",
732 AIBinder_getCallingPid(), getpid());
733 return Status::fromServiceSpecificError(PERMISSION_DENIED);
734 }
735
736 {
Girish434b4d82023-07-11 23:24:54 +0000737 std::scoped_lock lock{mLock};
Girish6a6044d2023-11-22 21:23:14 +0000738 if (overridePid_l(originalPid, newPid)) {
Girish1f002cf2023-02-17 00:36:29 +0000739 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800740 }
741 }
742
743 return Status::ok();
744}
745
Girish6a6044d2023-11-22 21:23:14 +0000746bool ResourceManagerService::overrideProcessInfo_l(
747 const std::shared_ptr<IResourceManagerClient>& client,
748 int pid,
749 int procState,
750 int oomScore) {
751 removeProcessInfoOverride_l(pid);
752
753 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
754 // Override value is rejected by ProcessInfo.
755 return false;
756 }
757
758 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
759 .uid = 0,
760 .id = 0,
761 .name = "<unknown client>"};
762 auto deathNotifier = DeathNotifier::Create(
763 client, ref<ResourceManagerService>(), clientInfo, true);
764
765 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
766 return true;
767}
768
Chong Zhang97d367b2020-09-16 12:53:14 -0700769Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100770 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700771 int oomScore) {
772 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
773 pid, procState, oomScore);
774 mServiceLog->add(log);
775
776 // Only allow the override if the caller already can access process state and oom scores.
777 int callingPid = AIBinder_getCallingPid();
778 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
779 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
780 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
781 return Status::fromServiceSpecificError(PERMISSION_DENIED);
782 }
783
784 if (client == nullptr) {
785 return Status::fromServiceSpecificError(BAD_VALUE);
786 }
787
Girish434b4d82023-07-11 23:24:54 +0000788 std::scoped_lock lock{mLock};
Girish6a6044d2023-11-22 21:23:14 +0000789 if (!overrideProcessInfo_l(client, pid, procState, oomScore)) {
Chong Zhang97d367b2020-09-16 12:53:14 -0700790 // Override value is rejected by ProcessInfo.
791 return Status::fromServiceSpecificError(BAD_VALUE);
792 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700793 return Status::ok();
Girish6a6044d2023-11-22 21:23:14 +0000794
Chong Zhang97d367b2020-09-16 12:53:14 -0700795}
796
Chong Zhang97d367b2020-09-16 12:53:14 -0700797void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000798 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700799
800 removeProcessInfoOverride_l(pid);
801}
802
803void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
804 auto it = mProcessInfoOverrideMap.find(pid);
805 if (it == mProcessInfoOverrideMap.end()) {
806 return;
807 }
808
809 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700810 mProcessInfoOverrideMap.erase(pid);
811}
812
Girish9128e242022-11-23 20:52:29 +0000813Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
814 int32_t pid = clientInfo.pid;
815 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700816 String8 log = String8::format(
817 "markClientForPendingRemoval(pid %d, clientId %lld)",
818 pid, (long long) clientId);
819 mServiceLog->add(log);
820
Girish434b4d82023-07-11 23:24:54 +0000821 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100822 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800823 pid_t callingPid = IPCThreadState::self()->getCallingPid();
824 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
825 pid, callingPid);
826 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700827 }
Girish434b4d82023-07-11 23:24:54 +0000828 PidResourceInfosMap::iterator found = mMap.find(pid);
829 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700830 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
831 pid, (long long)clientId);
832 return Status::ok();
833 }
Girish434b4d82023-07-11 23:24:54 +0000834 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700835
Girish434b4d82023-07-11 23:24:54 +0000836 ResourceInfos::iterator foundClient = infos.find(clientId);
837 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700838 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
839 return Status::ok();
840 }
841
Girish434b4d82023-07-11 23:24:54 +0000842 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700843 info.pendingRemoval = true;
844 return Status::ok();
845}
846
Wonsik Kim271429d2020-10-01 10:12:56 -0700847Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
848 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
849 mServiceLog->add(log);
850
Girish56fda312023-10-12 21:32:35 +0000851 std::vector<ClientInfo> targetClients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700852 {
Girish434b4d82023-07-11 23:24:54 +0000853 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100854 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800855 pid_t callingPid = IPCThreadState::self()->getCallingPid();
856 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
857 pid, callingPid);
858 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700859 }
860
861 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
862 MediaResource::Type::kNonSecureCodec,
863 MediaResource::Type::kGraphicMemory,
864 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100865 switch (type) {
866 // Codec resources are segregated by audio, video and image domains.
867 case MediaResource::Type::kSecureCodec:
868 case MediaResource::Type::kNonSecureCodec:
Girisha5a2d672023-09-20 18:40:20 +0000869 for (MediaResource::SubType subType : {MediaResource::SubType::kHwAudioCodec,
870 MediaResource::SubType::kSwAudioCodec,
871 MediaResource::SubType::kHwVideoCodec,
872 MediaResource::SubType::kSwVideoCodec,
873 MediaResource::SubType::kHwImageCodec,
874 MediaResource::SubType::kSwImageCodec}) {
Girishab17b0f2023-11-20 06:00:44 +0000875 ClientInfo clientInfo;
876 if (getBiggestClientPendingRemoval_l(pid, type, subType, clientInfo)) {
877 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100878 continue;
879 }
880 }
881 break;
882 // Non-codec resources are shared by audio, video and image codecs (no subtype).
883 default:
Girishab17b0f2023-11-20 06:00:44 +0000884 ClientInfo clientInfo;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100885 if (getBiggestClientPendingRemoval_l(pid, type,
Girishab17b0f2023-11-20 06:00:44 +0000886 MediaResource::SubType::kUnspecifiedSubType, clientInfo)) {
887 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100888 }
889 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700890 }
891 }
892 }
893
Girish56fda312023-10-12 21:32:35 +0000894 if (!targetClients.empty()) {
895 reclaimUnconditionallyFrom(targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700896 }
897 return Status::ok();
898}
899
Girish6a6044d2023-11-22 21:23:14 +0000900bool ResourceManagerService::getPriority_l(int pid, int* priority) const {
Henry Fang32762922020-01-28 18:40:39 -0800901 int newPid = pid;
902
Girish6a6044d2023-11-22 21:23:14 +0000903 std::map<int, int>::const_iterator found = mOverridePidMap.find(pid);
904 if (found != mOverridePidMap.end()) {
905 newPid = found->second;
Henry Fang32762922020-01-28 18:40:39 -0800906 ALOGD("getPriority_l: use override pid %d instead original pid %d",
907 newPid, pid);
908 }
909
910 return mProcessInfo->getPriority(newPid, priority);
911}
912
Girish56fda312023-10-12 21:32:35 +0000913bool ResourceManagerService::getAllClients_l(
914 const ResourceRequestInfo& resourceRequestInfo,
915 std::vector<ClientInfo>& clientsInfo) {
916 MediaResource::Type type = resourceRequestInfo.mResource->type;
917 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Girish9128e242022-11-23 20:52:29 +0000918
Girish434b4d82023-07-11 23:24:54 +0000919 for (auto& [pid, infos] : mMap) {
920 for (const auto& [id, info] : infos) {
Girish1c682402024-01-31 21:03:51 +0000921 if (pid == resourceRequestInfo.mCallingPid && id == resourceRequestInfo.mClientId) {
922 ALOGI("%s: Skip the client[%jd] for which the resource request is made",
923 __func__, id);
924 continue;
925 }
Girish434b4d82023-07-11 23:24:54 +0000926 if (hasResourceType(type, subType, info.resources)) {
Girish56fda312023-10-12 21:32:35 +0000927 if (!isCallingPriorityHigher_l(resourceRequestInfo.mCallingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700928 // some higher/equal priority process owns the resource,
929 // this request can't be fulfilled.
Girish56fda312023-10-12 21:32:35 +0000930 ALOGE("%s: can't reclaim resource %s from pid %d",
931 __func__, asString(type), pid);
932 clientsInfo.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700933 return false;
934 }
Girishab17b0f2023-11-20 06:00:44 +0000935 clientsInfo.emplace_back(pid, info.uid, info.clientId);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700936 }
937 }
938 }
Girish56fda312023-10-12 21:32:35 +0000939 if (clientsInfo.size() == 0) {
940 ALOGV("%s: didn't find any resource %s", __func__, asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700941 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700942 return true;
943}
944
Girishaff0ef22023-10-13 00:22:39 +0000945// Process priority (oom score) based reclaim:
946// - Find a process with lowest priority (than that of calling process).
947// - Find the bigegst client (with required resources) from that process.
Girish56fda312023-10-12 21:32:35 +0000948bool ResourceManagerService::getLowestPriorityBiggestClient_l(
949 const ResourceRequestInfo& resourceRequestInfo,
Girishab17b0f2023-11-20 06:00:44 +0000950 ClientInfo& clientInfo) {
Girish56fda312023-10-12 21:32:35 +0000951 int callingPid = resourceRequestInfo.mCallingPid;
952 MediaResource::Type type = resourceRequestInfo.mResource->type;
953 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700954 int lowestPriorityPid;
955 int lowestPriority;
956 int callingPriority;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700957
Henry Fang32762922020-01-28 18:40:39 -0800958 if (!getPriority_l(callingPid, &callingPriority)) {
Girishaff0ef22023-10-13 00:22:39 +0000959 ALOGE("%s: can't get process priority for pid %d", __func__, callingPid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700960 return false;
961 }
Brian Lindahl64ee9452022-01-14 13:31:16 +0100962 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700963 return false;
964 }
965 if (lowestPriority <= callingPriority) {
Girishaff0ef22023-10-13 00:22:39 +0000966 ALOGE("%s: lowest priority %d vs caller priority %d",
967 __func__, lowestPriority, callingPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700968 return false;
969 }
970
Girishab17b0f2023-11-20 06:00:44 +0000971 if (!getBiggestClient_l(lowestPriorityPid, type, subType, clientInfo)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700972 return false;
973 }
Girish9128e242022-11-23 20:52:29 +0000974
Girishaff0ef22023-10-13 00:22:39 +0000975 ALOGI("%s: CallingProcess(%d:%d) will reclaim from the lowestPriorityProcess(%d:%d)",
976 __func__, callingPid, callingPriority, lowestPriorityPid, lowestPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700977 return true;
978}
979
Brian Lindahl64ee9452022-01-14 13:31:16 +0100980bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
981 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700982 int pid = -1;
983 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +0000984 for (auto& [tempPid, infos] : mMap) {
985 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700986 // no client on this process.
987 continue;
988 }
Girish434b4d82023-07-11 23:24:54 +0000989 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700990 // doesn't have the requested resource type
991 continue;
992 }
Girish434b4d82023-07-11 23:24:54 +0000993 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -0800994 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700995 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
996 // TODO: remove this pid from mMap?
997 continue;
998 }
999 if (pid == -1 || tempPriority > priority) {
1000 // initial the value
1001 pid = tempPid;
1002 priority = tempPriority;
1003 }
1004 }
1005 if (pid != -1) {
1006 *lowestPriorityPid = pid;
1007 *lowestPriority = priority;
1008 }
1009 return (pid != -1);
1010}
1011
1012bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1013 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001014 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001015 return false;
1016 }
1017
1018 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001019 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001020 return false;
1021 }
1022
1023 return (callingPidPriority < priority);
1024}
1025
Brian Lindahl64ee9452022-01-14 13:31:16 +01001026bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +00001027 MediaResource::SubType subType, ClientInfo& clientInfo) {
1028 return getBiggestClient_l(pid, type, subType, clientInfo, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001029}
1030
1031bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +00001032 MediaResource::SubType subType, ClientInfo& clientInfo, bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001033 PidResourceInfosMap::iterator found = mMap.find(pid);
1034 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001035 ALOGE_IF(!pendingRemovalOnly,
1036 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001037 return false;
1038 }
1039
Girishab17b0f2023-11-20 06:00:44 +00001040 uid_t uid = -1;
1041 int64_t clientId = -1;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001042 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001043 const ResourceInfos& infos = found->second;
1044 for (const auto& [id, info] : infos) {
1045 const ResourceList& resources = info.resources;
1046 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001047 continue;
1048 }
Girishd11a03a2023-11-30 21:17:51 +00001049 for (const MediaResourceParcel& resource : resources.getResources()) {
Brian Lindahl64ee9452022-01-14 13:31:16 +01001050 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001051 if (resource.value > largestValue) {
1052 largestValue = resource.value;
Girishab17b0f2023-11-20 06:00:44 +00001053 clientId = info.clientId;
Girish434b4d82023-07-11 23:24:54 +00001054 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001055 }
1056 }
1057 }
1058 }
1059
Girishab17b0f2023-11-20 06:00:44 +00001060 if (clientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001061 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001062 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1063 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001064 return false;
1065 }
1066
Girishab17b0f2023-11-20 06:00:44 +00001067 clientInfo.mPid = pid;
1068 clientInfo.mUid = uid;
1069 clientInfo.mClientId = clientId;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001070 return true;
1071}
1072
Girish1f002cf2023-02-17 00:36:29 +00001073Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1074 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1075 return Status::ok();
1076}
1077
1078Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1079 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1080 return Status::ok();
1081}
1082
1083Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1084 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1085 return Status::ok();
1086}
1087
Girishde8eb592023-04-13 18:49:17 +00001088Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1089 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1090 return Status::ok();
1091}
1092
Girish1f002cf2023-02-17 00:36:29 +00001093long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1094 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1095}
1096
1097long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1098 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1099}
1100
Girish6a6044d2023-11-22 21:23:14 +00001101void ResourceManagerService::notifyClientReleased(const ClientInfoParcel& clientInfo) {
1102 mResourceManagerMetrics->notifyClientReleased(clientInfo);
1103}
1104
Ronghua Wu231c3d12015-03-11 15:10:32 -07001105} // namespace android