Skip to contentSkip to conversation

binder: return pending info for frozen async txns

PATCH Superseded Li Li 2 participants 4 messages Nov 3, 2022 – Nov 10, 2022 Android drivers

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li Li

author
#0 lore raw

cover binder: return pending info for frozen async txns

From: Li Li <[email protected]>

User applications need to know if their binder transactions reach a frozen process or not. For sync binder calls, Linux kernel already has a dedicated return value BR_FROZEN_REPLY, indicating this sync binder transaction will be rejected (similar to BR_DEAD_REPLY) as the target process is frozen. But for async binder calls, the user space application doesn't have a way to know if the target process is frozen.

This patch add a new return value, BR_TRANSACTION_PENDING, to fix this issue. Similar to BR_TRANSACTION_COMPLETE, it means the async binder transaction has been put in the queue of the target process, but it's waiting for the target process to be unfrozen.

v1: checkpatch.pl --strict passed

Li Li (1):
binder: return pending info for frozen async txns

 drivers/android/binder.c            | 23 ++++++++++++++++++++---
 drivers/android/binder_internal.h   |  3 ++-
 include/uapi/linux/android/binder.h |  7 ++++++-
 3 files changed, 28 insertions(+), 5 deletions(-)
Signature · 1 line
2.38.1.431.g37b22c650d-goog
author
#1 lore raw

PATCH 1/1 binder: return pending info for frozen async txns

From: Li Li <[email protected]>

An async transaction to a frozen process will still be successsfully put in the queue. But this pending async transaction won't be processed until the target process is unfrozen at an unspecified time in the future. Pass this important information back to the user space caller by returning BR_TRANSACTION_PENDING.

---
 drivers/android/binder.c            | 23 ++++++++++++++++++++---
 drivers/android/binder_internal.h   |  3 ++-
 include/uapi/linux/android/binder.h |  7 ++++++-
 3 files changed, 28 insertions(+), 5 deletions(-)
drivers/android/binder.c +20 -3
index 880224ec6abb..a097b89f6a7a 100644
@@ -2728,7 +2728,10 @@ binder_find_outdated_transaction_ilocked(struct binder_transaction *t,
  *
  * Return:	0 if the transaction was successfully queued
  *		BR_DEAD_REPLY if the target process or thread is dead
- *		BR_FROZEN_REPLY if the target process or thread is frozen
+ *		BR_FROZEN_REPLY if the target process or thread is frozen and
+ *			the sync transaction was rejected
+ *		BR_TRANSACTION_PENDING if the target process is frozen and the
+ *			async transaction was successfully queued
  */
 static int binder_proc_transaction(struct binder_transaction *t,
 				    struct binder_proc *proc,
@@ -2807,6 +2810,9 @@ static int binder_proc_transaction(struct binder_transaction *t,
 		binder_stats_deleted(BINDER_STAT_TRANSACTION);
 	}
 
+	if (oneway && proc->is_frozen)
+		return BR_TRANSACTION_PENDING;
+
 	return 0;
 }
 
@@ -3607,9 +3613,16 @@ static void binder_transaction(struct binder_proc *proc,
 	} else {
 		BUG_ON(target_node == NULL);
 		BUG_ON(t->buffer->async_transaction != 1);
-		binder_enqueue_thread_work(thread, tcomplete);
 		return_error = binder_proc_transaction(t, target_proc, NULL);
-		if (return_error)
+		/*
+		 * Let the caller know its async transaction reaches a frozen
+		 * process and is put in a pending queue, waiting for the target
+		 * process to be unfrozen.
+		 */
+		if (return_error == BR_TRANSACTION_PENDING)
+			tcomplete->type = BINDER_WORK_TRANSACTION_PENDING;
+		binder_enqueue_thread_work(thread, tcomplete);
+		if (return_error && return_error != BR_TRANSACTION_PENDING)
 			goto err_dead_proc_or_thread;
 	}
 	if (target_thread)
@@ -4440,10 +4453,13 @@ static int binder_thread_read(struct binder_proc *proc,
 			binder_stat_br(proc, thread, cmd);
 		} break;
 		case BINDER_WORK_TRANSACTION_COMPLETE:
+		case BINDER_WORK_TRANSACTION_PENDING:
 		case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: {
 			if (proc->oneway_spam_detection_enabled &&
 				   w->type == BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT)
 				cmd = BR_ONEWAY_SPAM_SUSPECT;
+			else if (w->type == BINDER_WORK_TRANSACTION_PENDING)
+				cmd = BR_TRANSACTION_PENDING;
 			else
 				cmd = BR_TRANSACTION_COMPLETE;
 			binder_inner_proc_unlock(proc);
@@ -6170,6 +6186,7 @@ static const char * const binder_return_strings[] = {
 	"BR_FAILED_REPLY",
 	"BR_FROZEN_REPLY",
 	"BR_ONEWAY_SPAM_SUSPECT",
+	"BR_TRANSACTION_PENDING"
 };
 
 static const char * const binder_command_strings[] = {
drivers/android/binder_internal.h +2 -1
index abe19d88c6ec..6c51325a826f 100644
@@ -133,7 +133,7 @@ enum binder_stat_types {
 };
 
 struct binder_stats {
-	atomic_t br[_IOC_NR(BR_ONEWAY_SPAM_SUSPECT) + 1];
+	atomic_t br[_IOC_NR(BR_TRANSACTION_PENDING) + 1];
 	atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
 	atomic_t obj_created[BINDER_STAT_COUNT];
 	atomic_t obj_deleted[BINDER_STAT_COUNT];
@@ -152,6 +152,7 @@ struct binder_work {
 	enum binder_work_type {
 		BINDER_WORK_TRANSACTION = 1,
 		BINDER_WORK_TRANSACTION_COMPLETE,
+		BINDER_WORK_TRANSACTION_PENDING,
 		BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT,
 		BINDER_WORK_RETURN_ERROR,
 		BINDER_WORK_NODE,
include/uapi/linux/android/binder.h +6 -1
index e72e4de8f452..c21b3b3eb4e4 100644
@@ -450,7 +450,7 @@ enum binder_driver_return_protocol {
 
 	BR_FROZEN_REPLY = _IO('r', 18),
 	/*
-	 * The target of the last transaction (either a bcTRANSACTION or
+	 * The target of the last sync transaction (either a bcTRANSACTION or
 	 * a bcATTEMPT_ACQUIRE) is frozen.  No parameters.
 	 */
 
@@ -460,6 +460,11 @@ enum binder_driver_return_protocol {
 	 * asynchronous transaction makes the allocated async buffer size exceed
 	 * detection threshold.  No parameters.
 	 */
+
+	BR_TRANSACTION_PENDING = _IO('r', 20),
+	/*
+	 * The target of the last async transaction is frozen.  No parameters.
+	 */
 };
 
 enum binder_driver_command_protocol {
Signature · 1 line
2.38.1.431.g37b22c650d-goog
maintainer
#2 lore raw
Quoted 3 lines · Li Li

From: Li Li <[email protected]>

An async transaction to a frozen process will still be successsfully

nit: sucessfully typo

Quoted 29 lines

put in the queue. But this pending async transaction won't be processed until the target process is unfrozen at an unspecified time in the future. Pass this important information back to the user space caller by returning BR_TRANSACTION_PENDING.

---
 drivers/android/binder.c            | 23 ++++++++++++++++++++---
 drivers/android/binder_internal.h   |  3 ++-
 include/uapi/linux/android/binder.h |  7 ++++++-
 3 files changed, 28 insertions(+), 5 deletions(-)
drivers/android/binder.c +4 -1
index 880224ec6abb..a097b89f6a7a 100644
@@ -2728,7 +2728,10 @@ binder_find_outdated_transaction_ilocked(struct binder_transaction *t,
  *
  * Return:	0 if the transaction was successfully queued
  *		BR_DEAD_REPLY if the target process or thread is dead
- *		BR_FROZEN_REPLY if the target process or thread is frozen
+ *		BR_FROZEN_REPLY if the target process or thread is frozen and
+ *			the sync transaction was rejected
+ *		BR_TRANSACTION_PENDING if the target process is frozen and the
+ *			async transaction was successfully queued
  */
 static int binder_proc_transaction(struct binder_transaction *t,
 				    struct binder_proc *proc,

drivers/android/binder.c static int binder_proc_transaction(struct binder_transaction *t, · line 2813

Quoted 4 lines
drivers/android/binder.c +1 -0
@@ -2807,6 +2810,9 @@ static int binder_proc_transaction(struct binder_transaction *t,
 		binder_stats_deleted(BINDER_STAT_TRANSACTION);
 	}
 
+	if (oneway && proc->is_frozen)

Do you need the inner lock here for proc->is_frozen?

Quoted 6 lines
drivers/android/binder.c +2 -0
+		return BR_TRANSACTION_PENDING;
+
 	return 0;
 }
 

drivers/android/binder.c static void binder_transaction(struct binder_proc *proc, · line 3618

Quoted 8 lines
drivers/android/binder.c +2 -2
@@ -3607,9 +3613,16 @@ static void binder_transaction(struct binder_proc *proc,
 	} else {
 		BUG_ON(target_node == NULL);
 		BUG_ON(t->buffer->async_transaction != 1);
-		binder_enqueue_thread_work(thread, tcomplete);
 		return_error = binder_proc_transaction(t, target_proc, NULL);
-		if (return_error)
+		/*
+		 * Let the caller know its async transaction reaches a frozen

nit: I believe you meant s/its/when or similar?

drivers/android/binder.c static void binder_transaction(struct binder_proc *proc, · line 3624

Quoted 6 lines
drivers/android/binder.c +6 -0
+		 * process and is put in a pending queue, waiting for the target
+		 * process to be unfrozen.
+		 */
+		if (return_error == BR_TRANSACTION_PENDING)
+			tcomplete->type = BINDER_WORK_TRANSACTION_PENDING;
+		binder_enqueue_thread_work(thread, tcomplete);

I wonder if switching the order of queuing the tcomplete here and waking up the target thread inside binder_proc_transaction() will have any performance implications if this task gets scheduled out.

Quoted 75 lines
drivers/android/binder.c +5 -0
+		if (return_error && return_error != BR_TRANSACTION_PENDING)
 			goto err_dead_proc_or_thread;
 	}
 	if (target_thread)
@@ -4440,10 +4453,13 @@ static int binder_thread_read(struct binder_proc *proc,
 			binder_stat_br(proc, thread, cmd);
 		} break;
 		case BINDER_WORK_TRANSACTION_COMPLETE:
+		case BINDER_WORK_TRANSACTION_PENDING:
 		case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: {
 			if (proc->oneway_spam_detection_enabled &&
 				   w->type == BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT)
 				cmd = BR_ONEWAY_SPAM_SUSPECT;
+			else if (w->type == BINDER_WORK_TRANSACTION_PENDING)
+				cmd = BR_TRANSACTION_PENDING;
 			else
 				cmd = BR_TRANSACTION_COMPLETE;
 			binder_inner_proc_unlock(proc);
@@ -6170,6 +6186,7 @@ static const char * const binder_return_strings[] = {
 	"BR_FAILED_REPLY",
 	"BR_FROZEN_REPLY",
 	"BR_ONEWAY_SPAM_SUSPECT",
+	"BR_TRANSACTION_PENDING"
 };
 
 static const char * const binder_command_strings[] = {
drivers/android/binder_internal.h +2 -1
index abe19d88c6ec..6c51325a826f 100644
@@ -133,7 +133,7 @@ enum binder_stat_types {
 };
 
 struct binder_stats {
-	atomic_t br[_IOC_NR(BR_ONEWAY_SPAM_SUSPECT) + 1];
+	atomic_t br[_IOC_NR(BR_TRANSACTION_PENDING) + 1];
 	atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
 	atomic_t obj_created[BINDER_STAT_COUNT];
 	atomic_t obj_deleted[BINDER_STAT_COUNT];
@@ -152,6 +152,7 @@ struct binder_work {
 	enum binder_work_type {
 		BINDER_WORK_TRANSACTION = 1,
 		BINDER_WORK_TRANSACTION_COMPLETE,
+		BINDER_WORK_TRANSACTION_PENDING,
 		BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT,
 		BINDER_WORK_RETURN_ERROR,
 		BINDER_WORK_NODE,
include/uapi/linux/android/binder.h +6 -1
index e72e4de8f452..c21b3b3eb4e4 100644
@@ -450,7 +450,7 @@ enum binder_driver_return_protocol {
 
 	BR_FROZEN_REPLY = _IO('r', 18),
 	/*
-	 * The target of the last transaction (either a bcTRANSACTION or
+	 * The target of the last sync transaction (either a bcTRANSACTION or
 	 * a bcATTEMPT_ACQUIRE) is frozen.  No parameters.
 	 */
 
@@ -460,6 +460,11 @@ enum binder_driver_return_protocol {
 	 * asynchronous transaction makes the allocated async buffer size exceed
 	 * detection threshold.  No parameters.
 	 */
+
+	BR_TRANSACTION_PENDING = _IO('r', 20),
+	/*
+	 * The target of the last async transaction is frozen.  No parameters.
+	 */
 };
 
 enum binder_driver_command_protocol {
Signature · 1 line
2.38.1.431.g37b22c650d-goog
author
#3 lore raw
Quoted 7 lines · Carlos Llamas
Quoted 3 lines · Li Li

From: Li Li <[email protected]>

An async transaction to a frozen process will still be successsfully

nit: sucessfully typo

Nice catch! Will remove the extra 's' in v2.

Quoted 33 lines
Quoted 33 lines

put in the queue. But this pending async transaction won't be processed until the target process is unfrozen at an unspecified time in the future. Pass this important information back to the user space caller by returning BR_TRANSACTION_PENDING.

---
 drivers/android/binder.c            | 23 ++++++++++++++++++++---
 drivers/android/binder_internal.h   |  3 ++-
 include/uapi/linux/android/binder.h |  7 ++++++-
 3 files changed, 28 insertions(+), 5 deletions(-)
drivers/android/binder.c +4 -1
index 880224ec6abb..a097b89f6a7a 100644
@@ -2728,7 +2728,10 @@ binder_find_outdated_transaction_ilocked(struct binder_transaction *t,
  *
  * Return:   0 if the transaction was successfully queued
  *           BR_DEAD_REPLY if the target process or thread is dead
- *           BR_FROZEN_REPLY if the target process or thread is frozen
+ *           BR_FROZEN_REPLY if the target process or thread is frozen and
+ *                   the sync transaction was rejected
+ *           BR_TRANSACTION_PENDING if the target process is frozen and the
+ *                   async transaction was successfully queued
  */
 static int binder_proc_transaction(struct binder_transaction *t,
                                  struct binder_proc *proc,
@@ -2807,6 +2810,9 @@ static int binder_proc_transaction(struct binder_transaction *t,
              binder_stats_deleted(BINDER_STAT_TRANSACTION);
      }
 
Quoted 3 lines

drivers/android/binder.c static int binder_proc_transaction(struct binder_transaction *t, · line 2813

Quoted 1 line
drivers/android/binder.c +1 -0
+     if (oneway && proc->is_frozen)

Do you need the inner lock here for proc->is_frozen?

You're right. I'll add a new local variable and set it between the existing binder_inner_proc_lock() and binder_inner_proc_unlock().

Quoted 6 lines
Quoted 6 lines
drivers/android/binder.c +2 -0
+             return BR_TRANSACTION_PENDING;
+
      return 0;
 }
 
Quoted 11 lines

drivers/android/binder.c static void binder_transaction(struct binder_proc *proc, · line 3618

Quoted 9 lines
drivers/android/binder.c +2 -2
@@ -3607,9 +3613,16 @@ static void binder_transaction(struct binder_proc *proc,
      } else {
              BUG_ON(target_node == NULL);
              BUG_ON(t->buffer->async_transaction != 1);
-             binder_enqueue_thread_work(thread, tcomplete);
              return_error = binder_proc_transaction(t, target_proc, NULL);
-             if (return_error)
+             /*
+              * Let the caller know its async transaction reaches a frozen

nit: I believe you meant s/its/when or similar?

Will change it in v2. Thanks!

Quoted 11 lines

drivers/android/binder.c static void binder_transaction(struct binder_proc *proc, · line 3624

Quoted 7 lines
drivers/android/binder.c +6 -0
+              * process and is put in a pending queue, waiting for the target
+              * process to be unfrozen.
+              */
+             if (return_error == BR_TRANSACTION_PENDING)
+                     tcomplete->type = BINDER_WORK_TRANSACTION_PENDING;
+             binder_enqueue_thread_work(thread, tcomplete);

I wonder if switching the order of queuing the tcomplete here and waking up the target thread inside binder_proc_transaction() will have any performance implications if this task gets scheduled out.

Eventually the sender has to wait this whole function finish and then call another
ioctl to actually read tcomplete back. If this task gets cheduled out, it won't have
a chance to perform that reading operation even without this change. So there's no difference.

Quoted 76 lines
Quoted 76 lines
drivers/android/binder.c +5 -0
+             if (return_error && return_error != BR_TRANSACTION_PENDING)
                      goto err_dead_proc_or_thread;
      }
      if (target_thread)
@@ -4440,10 +4453,13 @@ static int binder_thread_read(struct binder_proc *proc,
                      binder_stat_br(proc, thread, cmd);
              } break;
              case BINDER_WORK_TRANSACTION_COMPLETE:
+             case BINDER_WORK_TRANSACTION_PENDING:
              case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: {
                      if (proc->oneway_spam_detection_enabled &&
                                 w->type == BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT)
                              cmd = BR_ONEWAY_SPAM_SUSPECT;
+                     else if (w->type == BINDER_WORK_TRANSACTION_PENDING)
+                             cmd = BR_TRANSACTION_PENDING;
                      else
                              cmd = BR_TRANSACTION_COMPLETE;
                      binder_inner_proc_unlock(proc);
@@ -6170,6 +6186,7 @@ static const char * const binder_return_strings[] = {
      "BR_FAILED_REPLY",
      "BR_FROZEN_REPLY",
      "BR_ONEWAY_SPAM_SUSPECT",
+     "BR_TRANSACTION_PENDING"
 };
 
 static const char * const binder_command_strings[] = {
drivers/android/binder_internal.h +2 -1
index abe19d88c6ec..6c51325a826f 100644
@@ -133,7 +133,7 @@ enum binder_stat_types {
 };
 
 struct binder_stats {
-     atomic_t br[_IOC_NR(BR_ONEWAY_SPAM_SUSPECT) + 1];
+     atomic_t br[_IOC_NR(BR_TRANSACTION_PENDING) + 1];
      atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
      atomic_t obj_created[BINDER_STAT_COUNT];
      atomic_t obj_deleted[BINDER_STAT_COUNT];
@@ -152,6 +152,7 @@ struct binder_work {
      enum binder_work_type {
              BINDER_WORK_TRANSACTION = 1,
              BINDER_WORK_TRANSACTION_COMPLETE,
+             BINDER_WORK_TRANSACTION_PENDING,
              BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT,
              BINDER_WORK_RETURN_ERROR,
              BINDER_WORK_NODE,
include/uapi/linux/android/binder.h +6 -2
index e72e4de8f452..c21b3b3eb4e4 100644
@@ -450,7 +450,7 @@ enum binder_driver_return_protocol {
 
      BR_FROZEN_REPLY = _IO('r', 18),
      /*
-      * The target of the last transaction (either a bcTRANSACTION or
+      * The target of the last sync transaction (either a bcTRANSACTION or
       * a bcATTEMPT_ACQUIRE) is frozen.  No parameters.
       */
 
@@ -460,6 +460,11 @@ enum binder_driver_return_protocol {
       * asynchronous transaction makes the allocated async buffer size exceed
       * detection threshold.  No parameters.
       */
+
+     BR_TRANSACTION_PENDING = _IO('r', 20),
+     /*
+      * The target of the last async transaction is frozen.  No parameters.
+      */
 };
 
 enum binder_driver_command_protocol {
--

2.38.1.431.g37b22c650d-goog