Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 26 | #include <cutils/sched_policy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 27 | #include <dirent.h> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 28 | #include <media/MediaResourcePolicy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 29 | #include <media/stagefright/ProcessInfo.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 30 | #include <mediautils/BatteryNotifier.h> |
| 31 | #include <mediautils/SchedulingPolicyService.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/time.h> |
| 36 | #include <unistd.h> |
| 37 | |
Steven Moreland | 0a0ff0b | 2021-04-02 00:06:01 +0000 | [diff] [blame] | 38 | #include "IMediaResourceMonitor.h" |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 39 | #include "ResourceManagerService.h" |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 40 | #include "ResourceObserverService.h" |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 41 | #include "ServiceLog.h" |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 42 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 45 | //static |
| 46 | std::mutex ResourceManagerService::sCookieLock; |
| 47 | //static |
| 48 | uintptr_t ResourceManagerService::sCookieCounter = 0; |
| 49 | //static |
| 50 | std::map<uintptr_t, sp<DeathNotifier> > ResourceManagerService::sCookieToDeathNotifierMap; |
| 51 | |
| 52 | class DeathNotifier : public RefBase { |
| 53 | public: |
| 54 | DeathNotifier(const std::shared_ptr<ResourceManagerService> &service, |
| 55 | int pid, int64_t clientId); |
| 56 | |
| 57 | virtual ~DeathNotifier() {} |
| 58 | |
| 59 | // Implement death recipient |
| 60 | static void BinderDiedCallback(void* cookie); |
| 61 | virtual void binderDied(); |
| 62 | |
| 63 | protected: |
| 64 | std::weak_ptr<ResourceManagerService> mService; |
| 65 | int mPid; |
| 66 | int64_t mClientId; |
| 67 | }; |
| 68 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 69 | DeathNotifier::DeathNotifier(const std::shared_ptr<ResourceManagerService> &service, |
| 70 | int pid, int64_t clientId) |
| 71 | : mService(service), mPid(pid), mClientId(clientId) {} |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 72 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 73 | //static |
| 74 | void DeathNotifier::BinderDiedCallback(void* cookie) { |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 75 | sp<DeathNotifier> notifier; |
| 76 | { |
| 77 | std::scoped_lock lock{ResourceManagerService::sCookieLock}; |
| 78 | auto it = ResourceManagerService::sCookieToDeathNotifierMap.find( |
| 79 | reinterpret_cast<uintptr_t>(cookie)); |
| 80 | if (it == ResourceManagerService::sCookieToDeathNotifierMap.end()) { |
| 81 | return; |
| 82 | } |
| 83 | notifier = it->second; |
| 84 | } |
| 85 | if (notifier.get() != nullptr) { |
| 86 | notifier->binderDied(); |
| 87 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 88 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 89 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 90 | void DeathNotifier::binderDied() { |
| 91 | // Don't check for pid validity since we know it's already dead. |
| 92 | std::shared_ptr<ResourceManagerService> service = mService.lock(); |
| 93 | if (service == nullptr) { |
| 94 | ALOGW("ResourceManagerService is dead as well."); |
| 95 | return; |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 96 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 97 | |
| 98 | service->overridePid(mPid, -1); |
Henry Fang | b35141c | 2020-06-29 13:11:39 -0700 | [diff] [blame] | 99 | // thiz is freed in the call below, so it must be last call referring thiz |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 100 | service->removeResource(mPid, mClientId, false /*checkValid*/); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 101 | } |
Henry Fang | b35141c | 2020-06-29 13:11:39 -0700 | [diff] [blame] | 102 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 103 | class OverrideProcessInfoDeathNotifier : public DeathNotifier { |
| 104 | public: |
| 105 | OverrideProcessInfoDeathNotifier(const std::shared_ptr<ResourceManagerService> &service, |
| 106 | int pid) : DeathNotifier(service, pid, 0) {} |
| 107 | |
| 108 | virtual ~OverrideProcessInfoDeathNotifier() {} |
| 109 | |
| 110 | virtual void binderDied(); |
| 111 | }; |
| 112 | |
| 113 | void OverrideProcessInfoDeathNotifier::binderDied() { |
| 114 | // Don't check for pid validity since we know it's already dead. |
| 115 | std::shared_ptr<ResourceManagerService> service = mService.lock(); |
| 116 | if (service == nullptr) { |
| 117 | ALOGW("ResourceManagerService is dead as well."); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | service->removeProcessInfoOverride(mPid); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 122 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 123 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 124 | template <typename T> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 125 | static String8 getString(const std::vector<T> &items) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 126 | String8 itemsStr; |
| 127 | for (size_t i = 0; i < items.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 128 | itemsStr.appendFormat("%s ", toString(items[i]).string()); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 129 | } |
| 130 | return itemsStr; |
| 131 | } |
| 132 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 133 | static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) { |
| 134 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 135 | if (it->second.type == type) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
Chih-Hung Hsieh | 51873d8 | 2016-08-09 14:18:51 -0700 | [diff] [blame] | 142 | static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 143 | for (size_t i = 0; i < infos.size(); ++i) { |
| 144 | if (hasResourceType(type, infos[i].resources)) { |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | static ResourceInfos& getResourceInfosForEdit( |
| 152 | int pid, |
| 153 | PidResourceInfosMap& map) { |
| 154 | ssize_t index = map.indexOfKey(pid); |
| 155 | if (index < 0) { |
| 156 | // new pid |
| 157 | ResourceInfos infosForPid; |
| 158 | map.add(pid, infosForPid); |
| 159 | } |
| 160 | |
| 161 | return map.editValueFor(pid); |
| 162 | } |
| 163 | |
| 164 | static ResourceInfo& getResourceInfoForEdit( |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 165 | uid_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 166 | int64_t clientId, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 167 | const std::shared_ptr<IResourceManagerClient>& client, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 168 | ResourceInfos& infos) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 169 | ssize_t index = infos.indexOfKey(clientId); |
| 170 | |
| 171 | if (index < 0) { |
| 172 | ResourceInfo info; |
| 173 | info.uid = uid; |
| 174 | info.clientId = clientId; |
| 175 | info.client = client; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 176 | info.cookie = 0; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 177 | info.pendingRemoval = false; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 178 | |
| 179 | index = infos.add(clientId, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 180 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 181 | |
| 182 | return infos.editValueAt(index); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 185 | static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) { |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 186 | static const char* const kServiceName = "media_resource_monitor"; |
Dongwon Kang | 2642c84 | 2016-03-23 18:07:29 -0700 | [diff] [blame] | 187 | sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName)); |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 188 | if (binder != NULL) { |
| 189 | sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder); |
| 190 | for (size_t i = 0; i < resources.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 191 | if (resources[i].subType == MediaResource::SubType::kAudioCodec) { |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 192 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 193 | } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) { |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 194 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC); |
| 195 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 200 | binder_status_t ResourceManagerService::dump( |
| 201 | int fd, const char** /*args*/, uint32_t /*numArgs*/) { |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 202 | String8 result; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 203 | |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 204 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 205 | result.format("Permission Denial: " |
| 206 | "can't dump ResourceManagerService from pid=%d, uid=%d\n", |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 207 | AIBinder_getCallingPid(), |
| 208 | AIBinder_getCallingUid()); |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 209 | write(fd, result.string(), result.size()); |
| 210 | return PERMISSION_DENIED; |
| 211 | } |
| 212 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 213 | PidResourceInfosMap mapCopy; |
| 214 | bool supportsMultipleSecureCodecs; |
| 215 | bool supportsSecureWithNonSecureCodec; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 216 | std::map<int, int> overridePidMapCopy; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 217 | String8 serviceLog; |
| 218 | { |
| 219 | Mutex::Autolock lock(mLock); |
| 220 | mapCopy = mMap; // Shadow copy, real copy will happen on write. |
| 221 | supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs; |
| 222 | supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec; |
| 223 | serviceLog = mServiceLog->toString(" " /* linePrefix */); |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 224 | overridePidMapCopy = mOverridePidMap; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | const size_t SIZE = 256; |
| 228 | char buffer[SIZE]; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 229 | snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this); |
| 230 | result.append(buffer); |
| 231 | result.append(" Policies:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 232 | snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 233 | result.append(buffer); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 234 | snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n", |
| 235 | supportsSecureWithNonSecureCodec); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 236 | result.append(buffer); |
| 237 | |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 238 | result.append(" Processes:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 239 | for (size_t i = 0; i < mapCopy.size(); ++i) { |
| 240 | snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i)); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 241 | result.append(buffer); |
| 242 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 243 | const ResourceInfos &infos = mapCopy.valueAt(i); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 244 | for (size_t j = 0; j < infos.size(); ++j) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 245 | result.append(" Client:\n"); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 246 | snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId); |
| 247 | result.append(buffer); |
| 248 | |
Arun Johnson | aabe014 | 2022-07-18 19:16:04 +0000 | [diff] [blame^] | 249 | std::string clientName = "<unknown client>"; |
| 250 | if (infos[j].client != nullptr) { |
| 251 | Status status = infos[j].client->getName(&clientName); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 252 | } |
| 253 | snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 254 | result.append(buffer); |
| 255 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 256 | const ResourceList &resources = infos[j].resources; |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 257 | result.append(" Resources:\n"); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 258 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 259 | snprintf(buffer, SIZE, " %s\n", toString(it->second).string()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 260 | result.append(buffer); |
| 261 | } |
| 262 | } |
| 263 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 264 | result.append(" Process Pid override:\n"); |
| 265 | for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) { |
| 266 | snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n", |
| 267 | it->first, it->second); |
| 268 | result.append(buffer); |
| 269 | } |
Ronghua Wu | 022ed72 | 2015-05-11 15:15:09 -0700 | [diff] [blame] | 270 | result.append(" Events logs (most recent at top):\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 271 | result.append(serviceLog); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 272 | |
| 273 | write(fd, result.string(), result.size()); |
| 274 | return OK; |
| 275 | } |
| 276 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 277 | struct SystemCallbackImpl : |
| 278 | public ResourceManagerService::SystemCallbackInterface { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 279 | SystemCallbackImpl() : mClientToken(new BBinder()) {} |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 280 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 281 | virtual void noteStartVideo(int uid) override { |
| 282 | BatteryNotifier::getInstance().noteStartVideo(uid); |
| 283 | } |
| 284 | virtual void noteStopVideo(int uid) override { |
| 285 | BatteryNotifier::getInstance().noteStopVideo(uid); |
| 286 | } |
| 287 | virtual void noteResetVideo() override { |
| 288 | BatteryNotifier::getInstance().noteResetVideo(); |
| 289 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 290 | virtual bool requestCpusetBoost(bool enable) override { |
| 291 | return android::requestCpusetBoost(enable, mClientToken); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | protected: |
| 295 | virtual ~SystemCallbackImpl() {} |
| 296 | |
| 297 | private: |
| 298 | DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 299 | sp<IBinder> mClientToken; |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | ResourceManagerService::ResourceManagerService() |
| 303 | : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {} |
| 304 | |
| 305 | ResourceManagerService::ResourceManagerService( |
| 306 | const sp<ProcessInfoInterface> &processInfo, |
| 307 | const sp<SystemCallbackInterface> &systemResource) |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 308 | : mProcessInfo(processInfo), |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 309 | mSystemCB(systemResource), |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 310 | mServiceLog(new ServiceLog()), |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 311 | mSupportsMultipleSecureCodecs(true), |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 312 | mSupportsSecureWithNonSecureCodec(true), |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 313 | mCpuBoostCount(0), |
| 314 | mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 315 | mSystemCB->noteResetVideo(); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 316 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 317 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 318 | //static |
| 319 | void ResourceManagerService::instantiate() { |
| 320 | std::shared_ptr<ResourceManagerService> service = |
| 321 | ::ndk::SharedRefBase::make<ResourceManagerService>(); |
| 322 | binder_status_t status = |
| 323 | AServiceManager_addService(service->asBinder().get(), getServiceName()); |
| 324 | if (status != STATUS_OK) { |
| 325 | return; |
| 326 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 327 | |
| 328 | std::shared_ptr<ResourceObserverService> observerService = |
| 329 | ResourceObserverService::instantiate(); |
| 330 | |
| 331 | if (observerService != nullptr) { |
| 332 | service->setObserverService(observerService); |
| 333 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 334 | // TODO: mediaserver main() is already starting the thread pool, |
| 335 | // move this to mediaserver main() when other services in mediaserver |
| 336 | // are converted to ndk-platform aidl. |
| 337 | //ABinderProcess_startThreadPool(); |
| 338 | } |
| 339 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 340 | ResourceManagerService::~ResourceManagerService() {} |
| 341 | |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 342 | void ResourceManagerService::setObserverService( |
| 343 | const std::shared_ptr<ResourceObserverService>& observerService) { |
| 344 | mObserverService = observerService; |
| 345 | } |
| 346 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 347 | Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 348 | String8 log = String8::format("config(%s)", getString(policies).string()); |
| 349 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 350 | |
| 351 | Mutex::Autolock lock(mLock); |
| 352 | for (size_t i = 0; i < policies.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 353 | const std::string &type = policies[i].type; |
| 354 | const std::string &value = policies[i].value; |
| 355 | if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 356 | mSupportsMultipleSecureCodecs = (value == "true"); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 357 | } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 358 | mSupportsSecureWithNonSecureCodec = (value == "true"); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 361 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 364 | void ResourceManagerService::onFirstAdded( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 365 | const MediaResourceParcel& resource, const ResourceInfo& clientInfo) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 366 | // first time added |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 367 | if (resource.type == MediaResource::Type::kCpuBoost |
| 368 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 369 | // Request it on every new instance of kCpuBoost, as the media.codec |
| 370 | // could have died, if we only do it the first time subsequent instances |
| 371 | // never gets the boost. |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 372 | if (mSystemCB->requestCpusetBoost(true) != OK) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 373 | ALOGW("couldn't request cpuset boost"); |
| 374 | } |
| 375 | mCpuBoostCount++; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 376 | } else if (resource.type == MediaResource::Type::kBattery |
| 377 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 378 | mSystemCB->noteStartVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | void ResourceManagerService::onLastRemoved( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 383 | const MediaResourceParcel& resource, const ResourceInfo& clientInfo) { |
| 384 | if (resource.type == MediaResource::Type::kCpuBoost |
| 385 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 386 | && mCpuBoostCount > 0) { |
| 387 | if (--mCpuBoostCount == 0) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 388 | mSystemCB->requestCpusetBoost(false); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 389 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 390 | } else if (resource.type == MediaResource::Type::kBattery |
| 391 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 392 | mSystemCB->noteStopVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 396 | void ResourceManagerService::mergeResources( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 397 | MediaResourceParcel& r1, const MediaResourceParcel& r2) { |
| 398 | // The resource entry on record is maintained to be in [0,INT64_MAX]. |
| 399 | // Clamp if merging in the new resource value causes it to go out of bound. |
| 400 | // Note that the new resource value could be negative, eg.DrmSession, the |
| 401 | // value goes lower when the session is used more often. During reclaim |
| 402 | // the session with the highest value (lowest usage) would be closed. |
| 403 | if (r2.value < INT64_MAX - r1.value) { |
| 404 | r1.value += r2.value; |
| 405 | if (r1.value < 0) { |
| 406 | r1.value = 0; |
| 407 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 408 | } else { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 409 | r1.value = INT64_MAX; |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 413 | Status ResourceManagerService::addResource( |
| 414 | int32_t pid, |
| 415 | int32_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 416 | int64_t clientId, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 417 | const std::shared_ptr<IResourceManagerClient>& client, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 418 | const std::vector<MediaResourceParcel>& resources) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 419 | String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 420 | pid, (long long) clientId, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 421 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 422 | |
| 423 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 424 | if (!mProcessInfo->isValidPid(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 425 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 426 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 427 | ALOGW("%s called with untrusted pid %d, using calling pid %d, uid %d", __FUNCTION__, |
| 428 | pid, callingPid, callingUid); |
| 429 | pid = callingPid; |
| 430 | uid = callingUid; |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 431 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 432 | ResourceInfos& infos = getResourceInfosForEdit(pid, mMap); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 433 | ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 434 | ResourceList resourceAdded; |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 435 | |
| 436 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 437 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 438 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 439 | |
| 440 | if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) { |
| 441 | ALOGW("Ignoring request to remove negative value of non-drm resource"); |
| 442 | continue; |
| 443 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 444 | if (info.resources.find(resType) == info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 445 | if (res.value <= 0) { |
| 446 | // We can't init a new entry with negative value, although it's allowed |
| 447 | // to merge in negative values after the initial add. |
| 448 | ALOGW("Ignoring request to add new resource entry with value <= 0"); |
| 449 | continue; |
| 450 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 451 | onFirstAdded(res, info); |
| 452 | info.resources[resType] = res; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 453 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 454 | mergeResources(info.resources[resType], res); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 455 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 456 | // Add it to the list of added resources for observers. |
| 457 | auto it = resourceAdded.find(resType); |
| 458 | if (it == resourceAdded.end()) { |
| 459 | resourceAdded[resType] = res; |
| 460 | } else { |
| 461 | mergeResources(it->second, res); |
| 462 | } |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 463 | } |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 464 | if (info.cookie == 0 && client != nullptr) { |
| 465 | info.cookie = addCookieAndLink_l(client->asBinder(), |
| 466 | new DeathNotifier(ref<ResourceManagerService>(), pid, clientId)); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 467 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 468 | if (mObserverService != nullptr && !resourceAdded.empty()) { |
| 469 | mObserverService->onResourceAdded(uid, pid, resourceAdded); |
| 470 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 471 | notifyResourceGranted(pid, resources); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 472 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 475 | Status ResourceManagerService::removeResource( |
| 476 | int32_t pid, int64_t clientId, |
| 477 | const std::vector<MediaResourceParcel>& resources) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 478 | String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)", |
| 479 | pid, (long long) clientId, getString(resources).string()); |
| 480 | mServiceLog->add(log); |
| 481 | |
| 482 | Mutex::Autolock lock(mLock); |
| 483 | if (!mProcessInfo->isValidPid(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 484 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 485 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 486 | pid, callingPid); |
| 487 | pid = callingPid; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 488 | } |
| 489 | ssize_t index = mMap.indexOfKey(pid); |
| 490 | if (index < 0) { |
| 491 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 492 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 493 | } |
| 494 | ResourceInfos &infos = mMap.editValueAt(index); |
| 495 | |
| 496 | index = infos.indexOfKey(clientId); |
| 497 | if (index < 0) { |
| 498 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 499 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | ResourceInfo &info = infos.editValueAt(index); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 503 | ResourceList resourceRemoved; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 504 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 505 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 506 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 507 | |
| 508 | if (res.value < 0) { |
| 509 | ALOGW("Ignoring request to remove negative value of resource"); |
| 510 | continue; |
| 511 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 512 | // ignore if we don't have it |
| 513 | if (info.resources.find(resType) != info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 514 | MediaResourceParcel &resource = info.resources[resType]; |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 515 | MediaResourceParcel actualRemoved = res; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 516 | if (resource.value > res.value) { |
| 517 | resource.value -= res.value; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 518 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 519 | onLastRemoved(res, info); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 520 | actualRemoved.value = resource.value; |
Chong Zhang | 102b3e3 | 2020-11-02 12:21:50 -0800 | [diff] [blame] | 521 | info.resources.erase(resType); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | // Add it to the list of removed resources for observers. |
| 525 | auto it = resourceRemoved.find(resType); |
| 526 | if (it == resourceRemoved.end()) { |
| 527 | resourceRemoved[resType] = actualRemoved; |
| 528 | } else { |
| 529 | mergeResources(it->second, actualRemoved); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 533 | if (mObserverService != nullptr && !resourceRemoved.empty()) { |
| 534 | mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved); |
| 535 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 536 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 539 | Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 540 | removeResource(pid, clientId, true /*checkValid*/); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 541 | return Status::ok(); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 542 | } |
| 543 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 544 | Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) { |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 545 | String8 log = String8::format( |
| 546 | "removeResource(pid %d, clientId %lld)", |
| 547 | pid, (long long) clientId); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 548 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 549 | |
| 550 | Mutex::Autolock lock(mLock); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 551 | if (checkValid && !mProcessInfo->isValidPid(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 552 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 553 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 554 | pid, callingPid); |
| 555 | pid = callingPid; |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 556 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 557 | ssize_t index = mMap.indexOfKey(pid); |
| 558 | if (index < 0) { |
| 559 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 560 | return Status::ok(); |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 561 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 562 | ResourceInfos &infos = mMap.editValueAt(index); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 563 | |
| 564 | index = infos.indexOfKey(clientId); |
| 565 | if (index < 0) { |
| 566 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 567 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 568 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 569 | |
| 570 | const ResourceInfo &info = infos[index]; |
| 571 | for (auto it = info.resources.begin(); it != info.resources.end(); it++) { |
| 572 | onLastRemoved(it->second, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 573 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 574 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 575 | removeCookieAndUnlink_l(info.client->asBinder(), info.cookie); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 576 | |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 577 | if (mObserverService != nullptr && !info.resources.empty()) { |
| 578 | mObserverService->onResourceRemoved(info.uid, pid, info.resources); |
| 579 | } |
| 580 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 581 | infos.removeItemsAt(index); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 582 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 585 | void ResourceManagerService::getClientForResource_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 586 | int callingPid, const MediaResourceParcel *res, |
| 587 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 588 | if (res == NULL) { |
| 589 | return; |
| 590 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 591 | std::shared_ptr<IResourceManagerClient> client; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 592 | if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 593 | clients->push_back(client); |
| 594 | } |
| 595 | } |
| 596 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 597 | Status ResourceManagerService::reclaimResource( |
| 598 | int32_t callingPid, |
| 599 | const std::vector<MediaResourceParcel>& resources, |
| 600 | bool* _aidl_return) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 601 | String8 log = String8::format("reclaimResource(callingPid %d, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 602 | callingPid, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 603 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 604 | *_aidl_return = false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 605 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 606 | Vector<std::shared_ptr<IResourceManagerClient>> clients; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 607 | { |
| 608 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 609 | if (!mProcessInfo->isValidPid(callingPid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 610 | pid_t actualCallingPid = IPCThreadState::self()->getCallingPid(); |
| 611 | ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__, |
| 612 | callingPid, actualCallingPid); |
| 613 | callingPid = actualCallingPid; |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 614 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 615 | const MediaResourceParcel *secureCodec = NULL; |
| 616 | const MediaResourceParcel *nonSecureCodec = NULL; |
| 617 | const MediaResourceParcel *graphicMemory = NULL; |
| 618 | const MediaResourceParcel *drmSession = NULL; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 619 | for (size_t i = 0; i < resources.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 620 | MediaResource::Type type = resources[i].type; |
| 621 | if (resources[i].type == MediaResource::Type::kSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 622 | secureCodec = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 623 | } else if (type == MediaResource::Type::kNonSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 624 | nonSecureCodec = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 625 | } else if (type == MediaResource::Type::kGraphicMemory) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 626 | graphicMemory = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 627 | } else if (type == MediaResource::Type::kDrmSession) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 628 | drmSession = &resources[i]; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
| 632 | // first pass to handle secure/non-secure codec conflict |
| 633 | if (secureCodec != NULL) { |
| 634 | if (!mSupportsMultipleSecureCodecs) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 635 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) { |
| 636 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | if (!mSupportsSecureWithNonSecureCodec) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 640 | if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) { |
| 641 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | } |
| 645 | if (nonSecureCodec != NULL) { |
| 646 | if (!mSupportsSecureWithNonSecureCodec) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 647 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) { |
| 648 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 652 | if (drmSession != NULL) { |
| 653 | getClientForResource_l(callingPid, drmSession, &clients); |
| 654 | if (clients.size() == 0) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 655 | return Status::ok(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 656 | } |
| 657 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 658 | |
| 659 | if (clients.size() == 0) { |
| 660 | // if no secure/non-secure codec conflict, run second pass to handle other resources. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 661 | getClientForResource_l(callingPid, graphicMemory, &clients); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 662 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 663 | |
| 664 | if (clients.size() == 0) { |
| 665 | // if we are here, run the third pass to free one codec with the same type. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 666 | getClientForResource_l(callingPid, secureCodec, &clients); |
| 667 | getClientForResource_l(callingPid, nonSecureCodec, &clients); |
| 668 | } |
| 669 | |
| 670 | if (clients.size() == 0) { |
| 671 | // if we are here, run the fourth pass to free one codec with the different type. |
| 672 | if (secureCodec != NULL) { |
Wonsik Kim | b363901 | 2022-03-16 13:57:41 -0700 | [diff] [blame] | 673 | MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 674 | getClientForResource_l(callingPid, &temp, &clients); |
| 675 | } |
| 676 | if (nonSecureCodec != NULL) { |
Wonsik Kim | b363901 | 2022-03-16 13:57:41 -0700 | [diff] [blame] | 677 | MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 678 | getClientForResource_l(callingPid, &temp, &clients); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 683 | *_aidl_return = reclaimInternal(clients); |
| 684 | return Status::ok(); |
| 685 | } |
| 686 | |
| 687 | bool ResourceManagerService::reclaimInternal( |
| 688 | const Vector<std::shared_ptr<IResourceManagerClient>> &clients) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 689 | if (clients.size() == 0) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 690 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 693 | std::shared_ptr<IResourceManagerClient> failedClient; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 694 | for (size_t i = 0; i < clients.size(); ++i) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 695 | String8 log = String8::format("reclaimResource from client %p", clients[i].get()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 696 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 697 | bool success; |
| 698 | Status status = clients[i]->reclaimResource(&success); |
| 699 | if (!status.isOk() || !success) { |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 700 | failedClient = clients[i]; |
| 701 | break; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 702 | } |
| 703 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 704 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 705 | if (failedClient == NULL) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 706 | return true; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 709 | { |
| 710 | Mutex::Autolock lock(mLock); |
| 711 | bool found = false; |
| 712 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 713 | ResourceInfos &infos = mMap.editValueAt(i); |
| 714 | for (size_t j = 0; j < infos.size();) { |
| 715 | if (infos[j].client == failedClient) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 716 | j = infos.removeItemsAt(j); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 717 | found = true; |
| 718 | } else { |
| 719 | ++j; |
| 720 | } |
| 721 | } |
| 722 | if (found) { |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | if (!found) { |
| 727 | ALOGV("didn't find failed client"); |
| 728 | } |
| 729 | } |
| 730 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 731 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 734 | Status ResourceManagerService::overridePid( |
| 735 | int originalPid, |
| 736 | int newPid) { |
| 737 | String8 log = String8::format("overridePid(originalPid %d, newPid %d)", |
| 738 | originalPid, newPid); |
| 739 | mServiceLog->add(log); |
| 740 | |
| 741 | // allow if this is called from the same process or the process has |
| 742 | // permission. |
| 743 | if ((AIBinder_getCallingPid() != getpid()) && |
| 744 | (checkCallingPermission(String16( |
| 745 | "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) { |
| 746 | ALOGE( |
| 747 | "Permission Denial: can't access overridePid method from pid=%d, " |
| 748 | "self pid=%d\n", |
| 749 | AIBinder_getCallingPid(), getpid()); |
| 750 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 751 | } |
| 752 | |
| 753 | { |
| 754 | Mutex::Autolock lock(mLock); |
| 755 | mOverridePidMap.erase(originalPid); |
| 756 | if (newPid != -1) { |
| 757 | mOverridePidMap.emplace(originalPid, newPid); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | return Status::ok(); |
| 762 | } |
| 763 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 764 | Status ResourceManagerService::overrideProcessInfo( |
| 765 | const std::shared_ptr<IResourceManagerClient>& client, |
| 766 | int pid, |
| 767 | int procState, |
| 768 | int oomScore) { |
| 769 | String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)", |
| 770 | pid, procState, oomScore); |
| 771 | mServiceLog->add(log); |
| 772 | |
| 773 | // Only allow the override if the caller already can access process state and oom scores. |
| 774 | int callingPid = AIBinder_getCallingPid(); |
| 775 | if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16( |
| 776 | "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) { |
| 777 | ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid); |
| 778 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 779 | } |
| 780 | |
| 781 | if (client == nullptr) { |
| 782 | return Status::fromServiceSpecificError(BAD_VALUE); |
| 783 | } |
| 784 | |
| 785 | Mutex::Autolock lock(mLock); |
| 786 | removeProcessInfoOverride_l(pid); |
| 787 | |
| 788 | if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) { |
| 789 | // Override value is rejected by ProcessInfo. |
| 790 | return Status::fromServiceSpecificError(BAD_VALUE); |
| 791 | } |
| 792 | |
| 793 | uintptr_t cookie = addCookieAndLink_l(client->asBinder(), |
| 794 | new OverrideProcessInfoDeathNotifier(ref<ResourceManagerService>(), pid)); |
| 795 | |
| 796 | mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{cookie, client}); |
| 797 | |
| 798 | return Status::ok(); |
| 799 | } |
| 800 | |
| 801 | uintptr_t ResourceManagerService::addCookieAndLink_l( |
| 802 | ::ndk::SpAIBinder binder, const sp<DeathNotifier>& notifier) { |
| 803 | std::scoped_lock lock{sCookieLock}; |
| 804 | |
| 805 | uintptr_t cookie; |
| 806 | // Need to skip cookie 0 (if it wraps around). ResourceInfo has cookie initialized to 0 |
| 807 | // indicating the death notifier is not created yet. |
| 808 | while ((cookie = ++sCookieCounter) == 0); |
| 809 | AIBinder_linkToDeath(binder.get(), mDeathRecipient.get(), (void*)cookie); |
| 810 | sCookieToDeathNotifierMap.emplace(cookie, notifier); |
| 811 | |
| 812 | return cookie; |
| 813 | } |
| 814 | |
| 815 | void ResourceManagerService::removeCookieAndUnlink_l( |
| 816 | ::ndk::SpAIBinder binder, uintptr_t cookie) { |
| 817 | std::scoped_lock lock{sCookieLock}; |
| 818 | AIBinder_unlinkToDeath(binder.get(), mDeathRecipient.get(), (void*)cookie); |
| 819 | sCookieToDeathNotifierMap.erase(cookie); |
| 820 | } |
| 821 | |
| 822 | void ResourceManagerService::removeProcessInfoOverride(int pid) { |
| 823 | Mutex::Autolock lock(mLock); |
| 824 | |
| 825 | removeProcessInfoOverride_l(pid); |
| 826 | } |
| 827 | |
| 828 | void ResourceManagerService::removeProcessInfoOverride_l(int pid) { |
| 829 | auto it = mProcessInfoOverrideMap.find(pid); |
| 830 | if (it == mProcessInfoOverrideMap.end()) { |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | mProcessInfo->removeProcessInfoOverride(pid); |
| 835 | |
| 836 | removeCookieAndUnlink_l(it->second.client->asBinder(), it->second.cookie); |
| 837 | |
| 838 | mProcessInfoOverrideMap.erase(pid); |
| 839 | } |
| 840 | |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 841 | Status ResourceManagerService::markClientForPendingRemoval(int32_t pid, int64_t clientId) { |
| 842 | String8 log = String8::format( |
| 843 | "markClientForPendingRemoval(pid %d, clientId %lld)", |
| 844 | pid, (long long) clientId); |
| 845 | mServiceLog->add(log); |
| 846 | |
| 847 | Mutex::Autolock lock(mLock); |
| 848 | if (!mProcessInfo->isValidPid(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 849 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 850 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 851 | pid, callingPid); |
| 852 | pid = callingPid; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 853 | } |
| 854 | ssize_t index = mMap.indexOfKey(pid); |
| 855 | if (index < 0) { |
| 856 | ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld", |
| 857 | pid, (long long)clientId); |
| 858 | return Status::ok(); |
| 859 | } |
| 860 | ResourceInfos &infos = mMap.editValueAt(index); |
| 861 | |
| 862 | index = infos.indexOfKey(clientId); |
| 863 | if (index < 0) { |
| 864 | ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId); |
| 865 | return Status::ok(); |
| 866 | } |
| 867 | |
| 868 | ResourceInfo &info = infos.editValueAt(index); |
| 869 | info.pendingRemoval = true; |
| 870 | return Status::ok(); |
| 871 | } |
| 872 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 873 | Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) { |
| 874 | String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid); |
| 875 | mServiceLog->add(log); |
| 876 | |
| 877 | Vector<std::shared_ptr<IResourceManagerClient>> clients; |
| 878 | { |
| 879 | Mutex::Autolock lock(mLock); |
| 880 | if (!mProcessInfo->isValidPid(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 881 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 882 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 883 | pid, callingPid); |
| 884 | pid = callingPid; |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | for (MediaResource::Type type : {MediaResource::Type::kSecureCodec, |
| 888 | MediaResource::Type::kNonSecureCodec, |
| 889 | MediaResource::Type::kGraphicMemory, |
| 890 | MediaResource::Type::kDrmSession}) { |
| 891 | std::shared_ptr<IResourceManagerClient> client; |
| 892 | if (getBiggestClient_l(pid, type, &client, true /* pendingRemovalOnly */)) { |
| 893 | clients.add(client); |
| 894 | break; |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | if (!clients.empty()) { |
| 900 | reclaimInternal(clients); |
| 901 | } |
| 902 | return Status::ok(); |
| 903 | } |
| 904 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 905 | bool ResourceManagerService::getPriority_l(int pid, int* priority) { |
| 906 | int newPid = pid; |
| 907 | |
| 908 | if (mOverridePidMap.find(pid) != mOverridePidMap.end()) { |
| 909 | newPid = mOverridePidMap[pid]; |
| 910 | ALOGD("getPriority_l: use override pid %d instead original pid %d", |
| 911 | newPid, pid); |
| 912 | } |
| 913 | |
| 914 | return mProcessInfo->getPriority(newPid, priority); |
| 915 | } |
| 916 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 917 | bool ResourceManagerService::getAllClients_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 918 | int callingPid, MediaResource::Type type, |
| 919 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
| 920 | Vector<std::shared_ptr<IResourceManagerClient>> temp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 921 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 922 | ResourceInfos &infos = mMap.editValueAt(i); |
| 923 | for (size_t j = 0; j < infos.size(); ++j) { |
| 924 | if (hasResourceType(type, infos[j].resources)) { |
| 925 | if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) { |
| 926 | // some higher/equal priority process owns the resource, |
| 927 | // this request can't be fulfilled. |
| 928 | ALOGE("getAllClients_l: can't reclaim resource %s from pid %d", |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 929 | asString(type), mMap.keyAt(i)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 930 | return false; |
| 931 | } |
| 932 | temp.push_back(infos[j].client); |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | if (temp.size() == 0) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 937 | ALOGV("getAllClients_l: didn't find any resource %s", asString(type)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 938 | return true; |
| 939 | } |
| 940 | clients->appendVector(temp); |
| 941 | return true; |
| 942 | } |
| 943 | |
| 944 | bool ResourceManagerService::getLowestPriorityBiggestClient_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 945 | int callingPid, MediaResource::Type type, |
| 946 | std::shared_ptr<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 947 | int lowestPriorityPid; |
| 948 | int lowestPriority; |
| 949 | int callingPriority; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 950 | |
| 951 | // Before looking into other processes, check if we have clients marked for |
| 952 | // pending removal in the same process. |
| 953 | if (getBiggestClient_l(callingPid, type, client, true /* pendingRemovalOnly */)) { |
| 954 | return true; |
| 955 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 956 | if (!getPriority_l(callingPid, &callingPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 957 | ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d", |
| 958 | callingPid); |
| 959 | return false; |
| 960 | } |
| 961 | if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) { |
| 962 | return false; |
| 963 | } |
| 964 | if (lowestPriority <= callingPriority) { |
| 965 | ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d", |
| 966 | lowestPriority, callingPriority); |
| 967 | return false; |
| 968 | } |
| 969 | |
| 970 | if (!getBiggestClient_l(lowestPriorityPid, type, client)) { |
| 971 | return false; |
| 972 | } |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | bool ResourceManagerService::getLowestPriorityPid_l( |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 977 | MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 978 | int pid = -1; |
| 979 | int priority = -1; |
| 980 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 981 | if (mMap.valueAt(i).size() == 0) { |
| 982 | // no client on this process. |
| 983 | continue; |
| 984 | } |
| 985 | if (!hasResourceType(type, mMap.valueAt(i))) { |
| 986 | // doesn't have the requested resource type |
| 987 | continue; |
| 988 | } |
| 989 | int tempPid = mMap.keyAt(i); |
| 990 | int tempPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 991 | if (!getPriority_l(tempPid, &tempPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 992 | ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid); |
| 993 | // TODO: remove this pid from mMap? |
| 994 | continue; |
| 995 | } |
| 996 | if (pid == -1 || tempPriority > priority) { |
| 997 | // initial the value |
| 998 | pid = tempPid; |
| 999 | priority = tempPriority; |
| 1000 | } |
| 1001 | } |
| 1002 | if (pid != -1) { |
| 1003 | *lowestPriorityPid = pid; |
| 1004 | *lowestPriority = priority; |
| 1005 | } |
| 1006 | return (pid != -1); |
| 1007 | } |
| 1008 | |
| 1009 | bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) { |
| 1010 | int callingPidPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1011 | if (!getPriority_l(callingPid, &callingPidPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1012 | return false; |
| 1013 | } |
| 1014 | |
| 1015 | int priority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1016 | if (!getPriority_l(pid, &priority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | return (callingPidPriority < priority); |
| 1021 | } |
| 1022 | |
| 1023 | bool ResourceManagerService::getBiggestClient_l( |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1024 | int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client, |
| 1025 | bool pendingRemovalOnly) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1026 | ssize_t index = mMap.indexOfKey(pid); |
| 1027 | if (index < 0) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1028 | ALOGE_IF(!pendingRemovalOnly, |
| 1029 | "getBiggestClient_l: can't find resource info for pid %d", pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1030 | return false; |
| 1031 | } |
| 1032 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 1033 | std::shared_ptr<IResourceManagerClient> clientTemp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1034 | uint64_t largestValue = 0; |
| 1035 | const ResourceInfos &infos = mMap.valueAt(index); |
| 1036 | for (size_t i = 0; i < infos.size(); ++i) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 1037 | const ResourceList &resources = infos[i].resources; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1038 | if (pendingRemovalOnly && !infos[i].pendingRemoval) { |
| 1039 | continue; |
| 1040 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 1041 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 1042 | const MediaResourceParcel &resource = it->second; |
| 1043 | if (resource.type == type) { |
| 1044 | if (resource.value > largestValue) { |
| 1045 | largestValue = resource.value; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1046 | clientTemp = infos[i].client; |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | if (clientTemp == NULL) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1053 | ALOGE_IF(!pendingRemovalOnly, |
| 1054 | "getBiggestClient_l: can't find resource type %s for pid %d", |
| 1055 | asString(type), pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1056 | return false; |
| 1057 | } |
| 1058 | |
| 1059 | *client = clientTemp; |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1063 | } // namespace android |