diff options
author | 2023-10-26 14:53:49 -0700 | |
---|---|---|
committer | 2023-10-31 05:43:46 +0000 | |
commit | 66ee192e35245f43110d123a06975574e61d7e8d (patch) | |
tree | de802fd55a1acb37b73a72eee89c08345c03aa0b | |
parent | 9ddc976d043c90a01ff71154c06d651e84104a73 (diff) |
Binder: migrate off libutils Mutex
Bug: 302723053
Test: mma
Change-Id: I5b81d5c0b6c7fef7f2216d4010adfa9cf72f9876
-rw-r--r-- | libs/binder/Binder.cpp | 22 | ||||
-rw-r--r-- | libs/binder/BpBinder.cpp | 34 | ||||
-rw-r--r-- | libs/binder/IServiceManager.cpp | 2 | ||||
-rw-r--r-- | libs/binder/MemoryDealer.cpp | 10 | ||||
-rw-r--r-- | libs/binder/ProcessState.cpp | 8 | ||||
-rw-r--r-- | libs/binder/Utils.h | 13 | ||||
-rw-r--r-- | libs/binder/include/binder/BpBinder.h | 6 | ||||
-rw-r--r-- | libs/binder/include/binder/ProcessState.h | 5 |
8 files changed, 57 insertions, 43 deletions
diff --git a/libs/binder/Binder.cpp b/libs/binder/Binder.cpp index f22e90a03f..a01f447c9c 100644 --- a/libs/binder/Binder.cpp +++ b/libs/binder/Binder.cpp @@ -271,7 +271,7 @@ public: bool mInheritRt = false; // for below objects - Mutex mLock; + RpcMutex mLock; std::set<sp<RpcServerLink>> mRpcServerLinks; BpBinder::ObjectManager mObjects; @@ -307,7 +307,7 @@ status_t BBinder::startRecordingTransactions(const Parcel& data) { return PERMISSION_DENIED; } Extras* e = getOrCreateExtras(); - AutoMutex lock(e->mLock); + RpcMutexUniqueLock lock(e->mLock); if (mRecordingOn) { LOG(INFO) << "Could not start Binder recording. Another is already in progress."; return INVALID_OPERATION; @@ -337,7 +337,7 @@ status_t BBinder::stopRecordingTransactions() { return PERMISSION_DENIED; } Extras* e = getOrCreateExtras(); - AutoMutex lock(e->mLock); + RpcMutexUniqueLock lock(e->mLock); if (mRecordingOn) { e->mRecordingFd.reset(); mRecordingOn = false; @@ -405,7 +405,7 @@ status_t BBinder::transact( if (kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION) [[unlikely]] { Extras* e = mExtras.load(std::memory_order_acquire); - AutoMutex lock(e->mLock); + RpcMutexUniqueLock lock(e->mLock); if (mRecordingOn) { Parcel emptyReply; timespec ts; @@ -452,7 +452,7 @@ void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCoo Extras* e = getOrCreateExtras(); LOG_ALWAYS_FATAL_IF(!e, "no memory"); - AutoMutex _l(e->mLock); + RpcMutexUniqueLock _l(e->mLock); return e->mObjects.attach(objectID, object, cleanupCookie, func); } @@ -461,7 +461,7 @@ void* BBinder::findObject(const void* objectID) const Extras* e = mExtras.load(std::memory_order_acquire); if (!e) return nullptr; - AutoMutex _l(e->mLock); + RpcMutexUniqueLock _l(e->mLock); return e->mObjects.find(objectID); } @@ -469,7 +469,7 @@ void* BBinder::detachObject(const void* objectID) { Extras* e = mExtras.load(std::memory_order_acquire); if (!e) return nullptr; - AutoMutex _l(e->mLock); + RpcMutexUniqueLock _l(e->mLock); return e->mObjects.detach(objectID); } @@ -477,7 +477,7 @@ void BBinder::withLock(const std::function<void()>& doWithLock) { Extras* e = getOrCreateExtras(); LOG_ALWAYS_FATAL_IF(!e, "no memory"); - AutoMutex _l(e->mLock); + RpcMutexUniqueLock _l(e->mLock); doWithLock(); } @@ -485,7 +485,7 @@ sp<IBinder> BBinder::lookupOrCreateWeak(const void* objectID, object_make_func m const void* makeArgs) { Extras* e = getOrCreateExtras(); LOG_ALWAYS_FATAL_IF(!e, "no memory"); - AutoMutex _l(e->mLock); + RpcMutexUniqueLock _l(e->mLock); return e->mObjects.lookupOrCreateWeak(objectID, make, makeArgs); } @@ -692,7 +692,7 @@ status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd, auto weakThis = wp<BBinder>::fromExisting(this); Extras* e = getOrCreateExtras(); - AutoMutex _l(e->mLock); + RpcMutexUniqueLock _l(e->mLock); auto rpcServer = RpcServer::make(); LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null"); auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis); @@ -716,7 +716,7 @@ status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd, void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) { Extras* e = mExtras.load(std::memory_order_acquire); if (!e) return; - AutoMutex _l(e->mLock); + RpcMutexUniqueLock _l(e->mLock); (void)e->mRpcServerLinks.erase(link); } diff --git a/libs/binder/BpBinder.cpp b/libs/binder/BpBinder.cpp index 3bc4f929e7..eb8b79f501 100644 --- a/libs/binder/BpBinder.cpp +++ b/libs/binder/BpBinder.cpp @@ -38,7 +38,7 @@ namespace android { // --------------------------------------------------------------------------- -Mutex BpBinder::sTrackingLock; +RpcMutex BpBinder::sTrackingLock; std::unordered_map<int32_t, uint32_t> BpBinder::sTrackingMap; std::unordered_map<int32_t, uint32_t> BpBinder::sLastLimitCallbackMap; int BpBinder::sNumTrackedUids = 0; @@ -163,7 +163,7 @@ sp<BpBinder> BpBinder::create(int32_t handle) { int32_t trackedUid = -1; if (sCountByUidEnabled) { trackedUid = IPCThreadState::self()->getCallingUid(); - AutoMutex _l(sTrackingLock); + RpcMutexUniqueLock _l(sTrackingLock); uint32_t trackedValue = sTrackingMap[trackedUid]; if (trackedValue & LIMIT_REACHED_MASK) [[unlikely]] { if (sBinderProxyThrottleCreate) { @@ -276,7 +276,7 @@ std::optional<int32_t> BpBinder::getDebugBinderHandle() const { } bool BpBinder::isDescriptorCached() const { - Mutex::Autolock _l(mLock); + RpcMutexUniqueLock _l(mLock); return mDescriptorCache.c_str() != kDescriptorUninit.c_str(); } @@ -292,7 +292,7 @@ const String16& BpBinder::getInterfaceDescriptor() const status_t err = thiz->transact(INTERFACE_TRANSACTION, data, &reply); if (err == NO_ERROR) { String16 res(reply.readString16()); - Mutex::Autolock _l(mLock); + RpcMutexUniqueLock _l(mLock); // mDescriptorCache could have been assigned while the lock was // released. if (mDescriptorCache.c_str() == kDescriptorUninit.c_str()) mDescriptorCache = res; @@ -385,7 +385,7 @@ status_t BpBinder::transact( status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags); } if (data.dataSize() > LOG_TRANSACTIONS_OVER_SIZE) { - Mutex::Autolock _l(mLock); + RpcMutexUniqueLock _l(mLock); ALOGW("Large outgoing transaction of %zu bytes, interface descriptor %s, code %d", data.dataSize(), String8(mDescriptorCache).c_str(), code); } @@ -431,7 +431,7 @@ status_t BpBinder::linkToDeath( "linkToDeath(): recipient must be non-NULL"); { - AutoMutex _l(mLock); + RpcMutexUniqueLock _l(mLock); if (!mObitsSent) { if (!mObituaries) { @@ -467,7 +467,7 @@ status_t BpBinder::unlinkToDeath( return INVALID_OPERATION; } - AutoMutex _l(mLock); + RpcMutexUniqueLock _l(mLock); if (mObitsSent) { return DEAD_OBJECT; @@ -555,30 +555,30 @@ void BpBinder::reportOneDeath(const Obituary& obit) void* BpBinder::attachObject(const void* objectID, void* object, void* cleanupCookie, object_cleanup_func func) { - AutoMutex _l(mLock); + RpcMutexUniqueLock _l(mLock); ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects); return mObjects.attach(objectID, object, cleanupCookie, func); } void* BpBinder::findObject(const void* objectID) const { - AutoMutex _l(mLock); + RpcMutexUniqueLock _l(mLock); return mObjects.find(objectID); } void* BpBinder::detachObject(const void* objectID) { - AutoMutex _l(mLock); + RpcMutexUniqueLock _l(mLock); return mObjects.detach(objectID); } void BpBinder::withLock(const std::function<void()>& doWithLock) { - AutoMutex _l(mLock); + RpcMutexUniqueLock _l(mLock); doWithLock(); } sp<IBinder> BpBinder::lookupOrCreateWeak(const void* objectID, object_make_func make, const void* makeArgs) { - AutoMutex _l(mLock); + RpcMutexUniqueLock _l(mLock); return mObjects.lookupOrCreateWeak(objectID, make, makeArgs); } @@ -602,7 +602,7 @@ BpBinder::~BpBinder() { IPCThreadState* ipc = IPCThreadState::self(); if (mTrackedUid >= 0) { - AutoMutex _l(sTrackingLock); + RpcMutexUniqueLock _l(sTrackingLock); uint32_t trackedValue = sTrackingMap[mTrackedUid]; if ((trackedValue & COUNTING_VALUE_MASK) == 0) [[unlikely]] { ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this, @@ -702,7 +702,7 @@ bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) uint32_t BpBinder::getBinderProxyCount(uint32_t uid) { - AutoMutex _l(sTrackingLock); + RpcMutexUniqueLock _l(sTrackingLock); auto it = sTrackingMap.find(uid); if (it != sTrackingMap.end()) { return it->second & COUNTING_VALUE_MASK; @@ -717,7 +717,7 @@ uint32_t BpBinder::getBinderProxyCount() void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts) { - AutoMutex _l(sTrackingLock); + RpcMutexUniqueLock _l(sTrackingLock); uids.setCapacity(sTrackingMap.size()); counts.setCapacity(sTrackingMap.size()); for (const auto& it : sTrackingMap) { @@ -731,12 +731,12 @@ void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); } void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); } void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) { - AutoMutex _l(sTrackingLock); + RpcMutexUniqueLock _l(sTrackingLock); sLimitCallback = cb; } void BpBinder::setBinderProxyCountWatermarks(int high, int low) { - AutoMutex _l(sTrackingLock); + RpcMutexUniqueLock _l(sTrackingLock); sBinderProxyCountHighWatermark = high; sBinderProxyCountLowWatermark = low; } diff --git a/libs/binder/IServiceManager.cpp b/libs/binder/IServiceManager.cpp index 6034f2b4ca..fe566fccb2 100644 --- a/libs/binder/IServiceManager.cpp +++ b/libs/binder/IServiceManager.cpp @@ -200,7 +200,7 @@ bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t } bool checkPermission(const String16& permission, pid_t pid, uid_t uid, bool logPermissionFailure) { - static Mutex gPermissionControllerLock; + static std::mutex gPermissionControllerLock; static sp<IPermissionController> gPermissionController; sp<IPermissionController> pc; diff --git a/libs/binder/MemoryDealer.cpp b/libs/binder/MemoryDealer.cpp index 5b1cb7ea56..95bdbb4b65 100644 --- a/libs/binder/MemoryDealer.cpp +++ b/libs/binder/MemoryDealer.cpp @@ -155,7 +155,7 @@ private: void dump_l(String8& res, const char* what) const; static const int kMemoryAlign; - mutable Mutex mLock; + mutable std::mutex mLock; LinkedList<chunk_t> mList; size_t mHeapSize; }; @@ -305,14 +305,14 @@ size_t SimpleBestFitAllocator::size() const size_t SimpleBestFitAllocator::allocate(size_t size, uint32_t flags) { - Mutex::Autolock _l(mLock); + std::unique_lock<std::mutex> _l(mLock); ssize_t offset = alloc(size, flags); return offset; } status_t SimpleBestFitAllocator::deallocate(size_t offset) { - Mutex::Autolock _l(mLock); + std::unique_lock<std::mutex> _l(mLock); chunk_t const * const freed = dealloc(offset); if (freed) { return NO_ERROR; @@ -420,7 +420,7 @@ SimpleBestFitAllocator::chunk_t* SimpleBestFitAllocator::dealloc(size_t start) void SimpleBestFitAllocator::dump(const char* what) const { - Mutex::Autolock _l(mLock); + std::unique_lock<std::mutex> _l(mLock); dump_l(what); } @@ -434,7 +434,7 @@ void SimpleBestFitAllocator::dump_l(const char* what) const void SimpleBestFitAllocator::dump(String8& result, const char* what) const { - Mutex::Autolock _l(mLock); + std::unique_lock<std::mutex> _l(mLock); dump_l(result, what); } diff --git a/libs/binder/ProcessState.cpp b/libs/binder/ProcessState.cpp index 8ec4af9945..58203c1373 100644 --- a/libs/binder/ProcessState.cpp +++ b/libs/binder/ProcessState.cpp @@ -189,7 +189,7 @@ void ProcessState::childPostFork() { void ProcessState::startThreadPool() { - AutoMutex _l(mLock); + std::unique_lock<std::mutex> _l(mLock); if (!mThreadPoolStarted) { if (mMaxThreads == 0) { // see also getThreadPoolMaxTotalThreadCount @@ -203,7 +203,7 @@ void ProcessState::startThreadPool() bool ProcessState::becomeContextManager() { - AutoMutex _l(mLock); + std::unique_lock<std::mutex> _l(mLock); flat_binder_object obj { .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX, @@ -310,7 +310,7 @@ sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) { sp<IBinder> result; - AutoMutex _l(mLock); + std::unique_lock<std::mutex> _l(mLock); if (handle == 0 && the_context_object != nullptr) return the_context_object; @@ -374,7 +374,7 @@ sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) void ProcessState::expungeHandle(int32_t handle, IBinder* binder) { - AutoMutex _l(mLock); + std::unique_lock<std::mutex> _l(mLock); handle_entry* e = lookupHandleLocked(handle); diff --git a/libs/binder/Utils.h b/libs/binder/Utils.h index dd632c0b26..8942c31c48 100644 --- a/libs/binder/Utils.h +++ b/libs/binder/Utils.h @@ -22,6 +22,19 @@ #include <log/log.h> #include <utils/Errors.h> +/* TEMP_FAILURE_RETRY is not available on macOS and Trusty. */ +#ifndef TEMP_FAILURE_RETRY +/* Used to retry syscalls that can return EINTR. */ +#define TEMP_FAILURE_RETRY(exp) \ + ({ \ + __typeof__(exp) _rc; \ + do { \ + _rc = (exp); \ + } while (_rc == -1 && errno == EINTR); \ + _rc; \ + }) +#endif + #define TEST_AND_RETURN(value, expr) \ do { \ if (!(expr)) { \ diff --git a/libs/binder/include/binder/BpBinder.h b/libs/binder/include/binder/BpBinder.h index 28fb9f1bbc..d78ea0d6b1 100644 --- a/libs/binder/include/binder/BpBinder.h +++ b/libs/binder/include/binder/BpBinder.h @@ -18,7 +18,7 @@ #include <android-base/unique_fd.h> #include <binder/IBinder.h> -#include <utils/Mutex.h> +#include <binder/RpcThreads.h> #include <map> #include <optional> @@ -193,7 +193,7 @@ private: void reportOneDeath(const Obituary& obit); bool isDescriptorCached() const; - mutable Mutex mLock; + mutable RpcMutex mLock; volatile int32_t mAlive; volatile int32_t mObitsSent; Vector<Obituary>* mObituaries; @@ -201,7 +201,7 @@ private: mutable String16 mDescriptorCache; int32_t mTrackedUid; - static Mutex sTrackingLock; + static RpcMutex sTrackingLock; static std::unordered_map<int32_t,uint32_t> sTrackingMap; static int sNumTrackedUids; static std::atomic_bool sCountByUidEnabled; diff --git a/libs/binder/include/binder/ProcessState.h b/libs/binder/include/binder/ProcessState.h index 9dc370b412..363b5e115e 100644 --- a/libs/binder/include/binder/ProcessState.h +++ b/libs/binder/include/binder/ProcessState.h @@ -18,12 +18,13 @@ #include <binder/IBinder.h> #include <utils/KeyedVector.h> -#include <utils/Mutex.h> #include <utils/String16.h> #include <utils/String8.h> #include <pthread.h> +#include <mutex> + // --------------------------------------------------------------------------- namespace android { @@ -178,7 +179,7 @@ private: // Time when thread pool was emptied int64_t mStarvationStartTimeMs; - mutable Mutex mLock; // protects everything below. + mutable std::mutex mLock; // protects everything below. Vector<handle_entry> mHandleToObject; |