Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Steven Moreland | c4dd210 | 2017-02-23 13:57:21 -0800 | [diff] [blame] | 17 | #define LOG_TAG "hw-ProcessState" |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 18 | |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 19 | #include <hwbinder/ProcessState.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 20 | |
Steven Moreland | 5e67788 | 2018-02-23 14:59:21 -0800 | [diff] [blame] | 21 | #include <cutils/atomic.h> |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 22 | #include <hwbinder/BpHwBinder.h> |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 23 | #include <hwbinder/IPCThreadState.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | #include <utils/String8.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 26 | #include <utils/threads.h> |
| 27 | |
Steven Moreland | 48adadd | 2019-09-05 17:04:38 -0700 | [diff] [blame] | 28 | #include "binder_kernel.h" |
Martijn Coenen | e01f4f2 | 2016-05-12 12:33:28 +0200 | [diff] [blame] | 29 | #include <hwbinder/Static.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 30 | |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <unistd.h> |
| 36 | #include <sys/ioctl.h> |
| 37 | #include <sys/mman.h> |
| 38 | #include <sys/stat.h> |
Philip Cuadra | 788e005 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 39 | #include <sys/types.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 40 | |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 41 | #define DEFAULT_BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2) |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 42 | #define DEFAULT_MAX_BINDER_THREADS 0 |
Hang Lu | d1388da | 2021-03-24 14:30:06 +0800 | [diff] [blame] | 43 | #define DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION 1 |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 44 | |
Philip Cuadra | 788e005 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 45 | // ------------------------------------------------------------------------- |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 46 | |
| 47 | namespace android { |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 48 | namespace hardware { |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 49 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 50 | class PoolThread : public Thread |
| 51 | { |
| 52 | public: |
Chih-Hung Hsieh | 1f555e9 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 53 | explicit PoolThread(bool isMain) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 54 | : mIsMain(isMain) |
| 55 | { |
| 56 | } |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 57 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 58 | protected: |
| 59 | virtual bool threadLoop() |
| 60 | { |
| 61 | IPCThreadState::self()->joinThreadPool(mIsMain); |
| 62 | return false; |
| 63 | } |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 64 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 65 | const bool mIsMain; |
| 66 | }; |
| 67 | |
| 68 | sp<ProcessState> ProcessState::self() |
| 69 | { |
Steven Moreland | f050d28 | 2020-07-21 02:42:12 +0000 | [diff] [blame] | 70 | return init(DEFAULT_BINDER_VM_SIZE, false /*requireMmapSize*/); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 73 | sp<ProcessState> ProcessState::selfOrNull() { |
Steven Moreland | f050d28 | 2020-07-21 02:42:12 +0000 | [diff] [blame] | 74 | return init(0, false /*requireMmapSize*/); |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Steven Moreland | f050d28 | 2020-07-21 02:42:12 +0000 | [diff] [blame] | 77 | sp<ProcessState> ProcessState::initWithMmapSize(size_t mmapSize) { |
| 78 | return init(mmapSize, true /*requireMmapSize*/); |
| 79 | } |
| 80 | |
| 81 | sp<ProcessState> ProcessState::init(size_t mmapSize, bool requireMmapSize) { |
| 82 | [[clang::no_destroy]] static sp<ProcessState> gProcess; |
| 83 | [[clang::no_destroy]] static std::mutex gProcessMutex; |
| 84 | |
| 85 | if (mmapSize == 0) { |
| 86 | std::lock_guard<std::mutex> l(gProcessMutex); |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 87 | return gProcess; |
| 88 | } |
| 89 | |
Steven Moreland | f050d28 | 2020-07-21 02:42:12 +0000 | [diff] [blame] | 90 | [[clang::no_destroy]] static std::once_flag gProcessOnce; |
| 91 | std::call_once(gProcessOnce, [&](){ |
| 92 | std::lock_guard<std::mutex> l(gProcessMutex); |
| 93 | gProcess = new ProcessState(mmapSize); |
| 94 | }); |
| 95 | |
| 96 | if (requireMmapSize) { |
| 97 | LOG_ALWAYS_FATAL_IF(mmapSize != gProcess->getMmapSize(), |
| 98 | "ProcessState already initialized with a different mmap size."); |
| 99 | } |
| 100 | |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 101 | return gProcess; |
| 102 | } |
| 103 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 104 | void ProcessState::startThreadPool() |
| 105 | { |
| 106 | AutoMutex _l(mLock); |
| 107 | if (!mThreadPoolStarted) { |
| 108 | mThreadPoolStarted = true; |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 109 | if (mSpawnThreadOnStart) { |
| 110 | spawnPooledThread(true); |
| 111 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 115 | sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 116 | { |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 117 | return getStrongProxyForHandle(0); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 120 | void ProcessState::becomeContextManager() |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 121 | { |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 122 | AutoMutex _l(mLock); |
Jeff Brown | c0f143d | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 123 | |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 124 | flat_binder_object obj { |
| 125 | .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX, |
| 126 | }; |
Steven Moreland | c149dca | 2019-01-09 18:01:02 -0800 | [diff] [blame] | 127 | |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 128 | status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj); |
Steven Moreland | c149dca | 2019-01-09 18:01:02 -0800 | [diff] [blame] | 129 | |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 130 | // fallback to original method |
| 131 | if (result != 0) { |
| 132 | android_errorWriteLog(0x534e4554, "121035042"); |
Steven Moreland | c149dca | 2019-01-09 18:01:02 -0800 | [diff] [blame] | 133 | |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 134 | int unused = 0; |
| 135 | result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 136 | } |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 137 | |
| 138 | if (result == -1) { |
| 139 | ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno)); |
| 140 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 143 | // Get references to userspace objects held by the kernel binder driver |
| 144 | // Writes up to count elements into buf, and returns the total number |
| 145 | // of references the kernel has, which may be larger than count. |
| 146 | // buf may be NULL if count is 0. The pointers returned by this method |
| 147 | // should only be used for debugging and not dereferenced, they may |
| 148 | // already be invalid. |
| 149 | ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) { |
| 150 | binder_node_debug_info info = {}; |
| 151 | |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 152 | uintptr_t* end = buf ? buf + buf_count : nullptr; |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 153 | size_t count = 0; |
| 154 | |
| 155 | do { |
| 156 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info); |
| 157 | if (result < 0) { |
| 158 | return -1; |
| 159 | } |
| 160 | if (info.ptr != 0) { |
| 161 | if (buf && buf < end) *buf++ = info.ptr; |
| 162 | count++; |
| 163 | if (buf && buf < end) *buf++ = info.cookie; |
| 164 | count++; |
| 165 | } |
| 166 | } while (info.ptr != 0); |
| 167 | |
| 168 | return count; |
| 169 | } |
| 170 | |
Martijn Coenen | 320e1d3 | 2018-09-07 10:41:33 +0200 | [diff] [blame] | 171 | // Queries the driver for the current strong reference count of the node |
| 172 | // that the handle points to. Can only be used by the servicemanager. |
| 173 | // |
| 174 | // Returns -1 in case of failure, otherwise the strong reference count. |
| 175 | ssize_t ProcessState::getStrongRefCountForNodeByHandle(int32_t handle) { |
| 176 | binder_node_info_for_ref info; |
| 177 | memset(&info, 0, sizeof(binder_node_info_for_ref)); |
| 178 | |
| 179 | info.handle = handle; |
| 180 | |
| 181 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info); |
| 182 | |
| 183 | if (result != OK) { |
Daniel Norman | c0af11a | 2019-06-14 11:22:03 -0700 | [diff] [blame] | 184 | static bool logged = false; |
| 185 | if (!logged) { |
| 186 | ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF."); |
| 187 | logged = true; |
| 188 | } |
Martijn Coenen | 320e1d3 | 2018-09-07 10:41:33 +0200 | [diff] [blame] | 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | return info.strong_count; |
| 193 | } |
| 194 | |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 195 | size_t ProcessState::getMmapSize() { |
| 196 | return mMmapSize; |
| 197 | } |
| 198 | |
Steven Moreland | 1460300 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 199 | void ProcessState::setCallRestriction(CallRestriction restriction) { |
Steven Moreland | d9bdb65 | 2019-09-17 15:42:45 -0700 | [diff] [blame] | 200 | LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr, |
| 201 | "Call restrictions must be set before the threadpool is started."); |
Steven Moreland | 1460300 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 202 | |
| 203 | mCallRestriction = restriction; |
| 204 | } |
| 205 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 206 | ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle) |
| 207 | { |
| 208 | const size_t N=mHandleToObject.size(); |
| 209 | if (N <= (size_t)handle) { |
| 210 | handle_entry e; |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 211 | e.binder = nullptr; |
| 212 | e.refs = nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 213 | status_t err = mHandleToObject.insertAt(e, N, handle+1-N); |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 214 | if (err < NO_ERROR) return nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 215 | } |
| 216 | return &mHandleToObject.editItemAt(handle); |
| 217 | } |
| 218 | |
| 219 | sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) |
| 220 | { |
| 221 | sp<IBinder> result; |
| 222 | |
| 223 | AutoMutex _l(mLock); |
| 224 | |
| 225 | handle_entry* e = lookupHandleLocked(handle); |
| 226 | |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 227 | if (e != nullptr) { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 228 | // We need to create a new BpHwBinder if there isn't currently one, OR we |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 229 | // are unable to acquire a weak reference on this current one. See comment |
| 230 | // in getWeakProxyForHandle() for more info about this. |
| 231 | IBinder* b = e->binder; |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 232 | if (b == nullptr || !e->refs->attemptIncWeak(this)) { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 233 | b = new BpHwBinder(handle); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 234 | e->binder = b; |
| 235 | if (b) e->refs = b->getWeakRefs(); |
| 236 | result = b; |
| 237 | } else { |
| 238 | // This little bit of nastyness is to allow us to add a primary |
| 239 | // reference to the remote proxy when this team doesn't have one |
| 240 | // but another team is sending the handle to us. |
| 241 | result.force_set(b); |
| 242 | e->refs->decWeak(this); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return result; |
| 247 | } |
| 248 | |
| 249 | wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle) |
| 250 | { |
| 251 | wp<IBinder> result; |
| 252 | |
| 253 | AutoMutex _l(mLock); |
| 254 | |
| 255 | handle_entry* e = lookupHandleLocked(handle); |
| 256 | |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 257 | if (e != nullptr) { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 258 | // We need to create a new BpHwBinder if there isn't currently one, OR we |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 259 | // are unable to acquire a weak reference on this current one. The |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 260 | // attemptIncWeak() is safe because we know the BpHwBinder destructor will always |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 261 | // call expungeHandle(), which acquires the same lock we are holding now. |
| 262 | // We need to do this because there is a race condition between someone |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 263 | // releasing a reference on this BpHwBinder, and a new reference on its handle |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 264 | // arriving from the driver. |
| 265 | IBinder* b = e->binder; |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 266 | if (b == nullptr || !e->refs->attemptIncWeak(this)) { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 267 | b = new BpHwBinder(handle); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 268 | result = b; |
| 269 | e->binder = b; |
| 270 | if (b) e->refs = b->getWeakRefs(); |
| 271 | } else { |
| 272 | result = b; |
| 273 | e->refs->decWeak(this); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return result; |
| 278 | } |
| 279 | |
| 280 | void ProcessState::expungeHandle(int32_t handle, IBinder* binder) |
| 281 | { |
| 282 | AutoMutex _l(mLock); |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 283 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 284 | handle_entry* e = lookupHandleLocked(handle); |
| 285 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 286 | // This handle may have already been replaced with a new BpHwBinder |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 287 | // (if someone failed the AttemptIncWeak() above); we don't want |
| 288 | // to overwrite it. |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 289 | if (e && e->binder == binder) e->binder = nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 292 | String8 ProcessState::makeBinderThreadName() { |
| 293 | int32_t s = android_atomic_add(1, &mThreadPoolSeq); |
Philip Cuadra | 788e005 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 294 | pid_t pid = getpid(); |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 295 | String8 name; |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 296 | name.appendFormat("HwBinder:%d_%X", pid, s); |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 297 | return name; |
| 298 | } |
| 299 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 300 | void ProcessState::spawnPooledThread(bool isMain) |
| 301 | { |
| 302 | if (mThreadPoolStarted) { |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 303 | String8 name = makeBinderThreadName(); |
| 304 | ALOGV("Spawning new pooled thread, name=%s\n", name.string()); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 305 | sp<Thread> t = new PoolThread(isMain); |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 306 | t->run(name.string()); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 310 | status_t ProcessState::setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool) { |
Steven Moreland | 8e047f7 | 2020-02-18 18:26:43 -0800 | [diff] [blame] | 311 | LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads, |
| 312 | "Binder threadpool cannot be shrunk after starting"); |
| 313 | |
Steven Moreland | 2aa3707 | 2018-10-25 16:32:17 -0700 | [diff] [blame] | 314 | // if the caller joins the pool, then there will be one thread which is impossible. |
| 315 | LOG_ALWAYS_FATAL_IF(maxThreads == 0 && callerJoinsPool, |
| 316 | "Binder threadpool must have a minimum of one thread if caller joins pool."); |
| 317 | |
| 318 | size_t threadsToAllocate = maxThreads; |
| 319 | |
| 320 | // If the caller is going to join the pool it will contribute one thread to the threadpool. |
| 321 | // This is part of the API's contract. |
| 322 | if (callerJoinsPool) threadsToAllocate--; |
| 323 | |
| 324 | // If we can, spawn one thread from userspace when the threadpool is started. This ensures |
| 325 | // that there is always a thread available to start more threads as soon as the threadpool |
| 326 | // is started. |
| 327 | bool spawnThreadOnStart = threadsToAllocate > 0; |
| 328 | if (spawnThreadOnStart) threadsToAllocate--; |
| 329 | |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 330 | // the BINDER_SET_MAX_THREADS ioctl really tells the kernel how many threads |
| 331 | // it's allowed to spawn, *in addition* to any threads we may have already |
Steven Moreland | 2aa3707 | 2018-10-25 16:32:17 -0700 | [diff] [blame] | 332 | // spawned locally. |
| 333 | size_t kernelMaxThreads = threadsToAllocate; |
| 334 | |
| 335 | AutoMutex _l(mLock); |
| 336 | if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &kernelMaxThreads) == -1) { |
| 337 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno)); |
| 338 | return -errno; |
Mathias Agopian | 8297f50 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 339 | } |
Steven Moreland | 2aa3707 | 2018-10-25 16:32:17 -0700 | [diff] [blame] | 340 | |
| 341 | mMaxThreads = maxThreads; |
| 342 | mSpawnThreadOnStart = spawnThreadOnStart; |
| 343 | |
| 344 | return NO_ERROR; |
Mathias Agopian | 8297f50 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Hang Lu | d1388da | 2021-03-24 14:30:06 +0800 | [diff] [blame] | 347 | status_t ProcessState::enableOnewaySpamDetection(bool enable) { |
| 348 | uint32_t enableDetection = enable ? 1 : 0; |
| 349 | if (ioctl(mDriverFD, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enableDetection) == -1) { |
Martijn Coenen | 4486bab | 2021-04-22 14:35:58 +0200 | [diff] [blame] | 350 | ALOGI("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno)); |
Hang Lu | d1388da | 2021-03-24 14:30:06 +0800 | [diff] [blame] | 351 | return -errno; |
| 352 | } |
| 353 | return NO_ERROR; |
| 354 | } |
| 355 | |
Martijn Coenen | 420d4bb | 2017-10-24 11:43:55 +0200 | [diff] [blame] | 356 | size_t ProcessState::getMaxThreads() { |
| 357 | return mMaxThreads; |
| 358 | } |
| 359 | |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 360 | void ProcessState::giveThreadPoolName() { |
| 361 | androidSetThreadName( makeBinderThreadName().string() ); |
| 362 | } |
| 363 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 364 | static int open_driver() |
| 365 | { |
Martijn Coenen | a660cbc | 2016-05-12 11:29:23 +0200 | [diff] [blame] | 366 | int fd = open("/dev/hwbinder", O_RDWR | O_CLOEXEC); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 367 | if (fd >= 0) { |
Jin Wei | 023b468 | 2012-10-18 17:00:48 +0800 | [diff] [blame] | 368 | int vers = 0; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 369 | status_t result = ioctl(fd, BINDER_VERSION, &vers); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 370 | if (result == -1) { |
Steve Block | eafc621 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 371 | ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 372 | close(fd); |
| 373 | fd = -1; |
| 374 | } |
| 375 | if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) { |
Greg Hartman | d521774 | 2017-01-03 16:56:05 -0800 | [diff] [blame] | 376 | ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)!", vers, BINDER_CURRENT_PROTOCOL_VERSION); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 377 | close(fd); |
| 378 | fd = -1; |
| 379 | } |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 380 | size_t maxThreads = DEFAULT_MAX_BINDER_THREADS; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 381 | result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); |
| 382 | if (result == -1) { |
Steve Block | eafc621 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 383 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 384 | } |
Hang Lu | d1388da | 2021-03-24 14:30:06 +0800 | [diff] [blame] | 385 | uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION; |
| 386 | result = ioctl(fd, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable); |
| 387 | if (result == -1) { |
Steven Moreland | 0e4c9bc | 2021-08-12 16:27:40 -0700 | [diff] [blame] | 388 | ALOGV("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno)); |
Hang Lu | d1388da | 2021-03-24 14:30:06 +0800 | [diff] [blame] | 389 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 390 | } else { |
Iliyan Malchev | e3256a6 | 2016-09-19 16:00:32 -0700 | [diff] [blame] | 391 | ALOGW("Opening '/dev/hwbinder' failed: %s\n", strerror(errno)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 392 | } |
| 393 | return fd; |
| 394 | } |
| 395 | |
Steven Moreland | f050d28 | 2020-07-21 02:42:12 +0000 | [diff] [blame] | 396 | ProcessState::ProcessState(size_t mmapSize) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 397 | : mDriverFD(open_driver()) |
| 398 | , mVMStart(MAP_FAILED) |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 399 | , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER) |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 400 | , mExecutingThreadsCount(0) |
| 401 | , mMaxThreads(DEFAULT_MAX_BINDER_THREADS) |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 402 | , mStarvationStartTimeMs(0) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 403 | , mThreadPoolStarted(false) |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 404 | , mSpawnThreadOnStart(true) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 405 | , mThreadPoolSeq(1) |
Steven Moreland | f050d28 | 2020-07-21 02:42:12 +0000 | [diff] [blame] | 406 | , mMmapSize(mmapSize) |
Steven Moreland | 1460300 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 407 | , mCallRestriction(CallRestriction::NONE) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 408 | { |
| 409 | if (mDriverFD >= 0) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 410 | // mmap the binder, providing a chunk of virtual address space to receive transactions. |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 411 | mVMStart = mmap(nullptr, mMmapSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 412 | if (mVMStart == MAP_FAILED) { |
| 413 | // *sigh* |
Jiyong Park | 1268c28 | 2019-01-28 16:22:49 +0900 | [diff] [blame] | 414 | ALOGE("Mmapping /dev/hwbinder failed: %s\n", strerror(errno)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 415 | close(mDriverFD); |
| 416 | mDriverFD = -1; |
| 417 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 418 | } |
Sergii Piatakov | b2cded4 | 2019-11-08 17:59:31 +0200 | [diff] [blame] | 419 | |
Steven Moreland | 065201c | 2019-12-11 11:16:08 -0800 | [diff] [blame] | 420 | #ifdef __ANDROID__ |
Sergii Piatakov | b2cded4 | 2019-11-08 17:59:31 +0200 | [diff] [blame] | 421 | LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating."); |
Steven Moreland | 065201c | 2019-12-11 11:16:08 -0800 | [diff] [blame] | 422 | #endif |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | ProcessState::~ProcessState() |
| 426 | { |
zhongjie | 8e8a025 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 427 | if (mDriverFD >= 0) { |
| 428 | if (mVMStart != MAP_FAILED) { |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 429 | munmap(mVMStart, mMmapSize); |
zhongjie | 8e8a025 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 430 | } |
| 431 | close(mDriverFD); |
| 432 | } |
| 433 | mDriverFD = -1; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 434 | } |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 435 | |
Steven Moreland | 7173a4c | 2019-09-26 15:55:02 -0700 | [diff] [blame] | 436 | } // namespace hardware |
| 437 | } // namespace android |