Fix Deadlock Issue On AppFuseBridge

There are two locks used by AppFuseBridge.
First is it's object lock, and the second is a mutex lock in app fuse library.
There are two oppsite routines to get those locks.

  (Thread A) Got Java lock -> Try to get Native lock
  (Thread B)        Got Native lock -> Try to get Java lock

Bug : https://issuetracker.google.com/issues/145707568
Signed-off-by: hyeeun.jun@samsung.com <hyeeun.jun@samsung.com>

The order must be followed to obtain two locks.
If not, the dead lock will be caused.
Therefore we change the routine to get the mutex lock first, and the object lock later.
This commit is contained in:
hyeeun.jun@samsung.com
2020-01-29 17:09:06 +09:00
committed by Hyeeun Jun
parent f8af692214
commit 2e28bb2372
2 changed files with 30 additions and 0 deletions

View File

@@ -56,6 +56,15 @@ public class AppFuseBridge implements Runnable {
public ParcelFileDescriptor addBridge(MountScope mountScope)
throws FuseUnavailableMountException, NativeDaemonConnectorException {
/*
** Dead Lock between Java lock (AppFuseBridge.java) and Native lock (FuseBridgeLoop.cc)
**
** (Thread A) Got Java lock (addBrdige) -> Try to get Native lock (native_add_brdige)
** (Thread B) Got Native lock (FuseBrdigeLoop.start) -> Try to get Java lock (onClosed)
**
** Guarantee the lock order (native lock -> java lock) when adding Bridge.
*/
native_lock();
try {
synchronized (this) {
Preconditions.checkArgument(mScopes.indexOfKey(mountScope.mountId) < 0);
@@ -73,6 +82,7 @@ public class AppFuseBridge implements Runnable {
return result;
}
} finally {
native_unlock();
IoUtils.closeQuietly(mountScope);
}
}
@@ -159,4 +169,6 @@ public class AppFuseBridge implements Runnable {
private native void native_delete(long loop);
private native void native_start_loop(long loop);
private native int native_add_bridge(long loop, int mountId, int deviceId);
private native void native_lock();
private native void native_unlock();
}

View File

@@ -123,6 +123,14 @@ jint com_android_server_storage_AppFuseBridge_add_bridge(
return proxyFd[1].release();
}
void com_android_server_storage_AppFuseBridge_lock(JNIEnv* env, jobject self) {
fuse::FuseBridgeLoop::Lock();
}
void com_android_server_storage_AppFuseBridge_unlock(JNIEnv* env, jobject self) {
fuse::FuseBridgeLoop::Unlock();
}
const JNINativeMethod methods[] = {
{
"native_new",
@@ -143,6 +151,16 @@ const JNINativeMethod methods[] = {
"native_add_bridge",
"(JII)I",
reinterpret_cast<void*>(com_android_server_storage_AppFuseBridge_add_bridge)
},
{
"native_lock",
"()V",
reinterpret_cast<void*>(com_android_server_storage_AppFuseBridge_lock)
},
{
"native_unlock",
"()V",
reinterpret_cast<void*>(com_android_server_storage_AppFuseBridge_unlock)
}
};