[Copybara Auto Merge] Merge branch pro into android13-gs-pixel-5.10-udc

edgetpu: replace use of macros that modify control flow
Roll-up the following commits from darwinn-2.0:
8f5061cc6 edgetpu: replace group leader check macro that modifies control flow
8e1fc53e2 edgetpu: kci replace use of macro that modifies control flow
Bug: 286406228
GitOrigin-RevId: 60b1b7ad3d0448758f8442765858723bdfadd300
Change-Id: Ie479cfad75bf19c91472c91b8c3a434b15c4960f
diff --git a/drivers/edgetpu/edgetpu-fs.c b/drivers/edgetpu/edgetpu-fs.c
index bef8d35..db55484 100644
--- a/drivers/edgetpu/edgetpu-fs.c
+++ b/drivers/edgetpu/edgetpu-fs.c
@@ -60,18 +60,19 @@
 #define LOCK(client) mutex_lock(&client->group_lock)
 #define UNLOCK(client) mutex_unlock(&client->group_lock)
 /*
- * Locks @client->group_lock and assigns @client->group to @grp.
- * Returns -EINVAL if @client is not the leader of the group.
+ * Locks @client->group_lock and checks whether @client is the leader of a group.
+ * If @client is not the leader of a group, unlocks group_lock and returns false.
+ * If @client is the leader of a group, returns true with group_lock held.
  */
-#define LOCK_RETURN_IF_NOT_LEADER(client, grp)                                 \
-	do {                                                                   \
-		LOCK(client);                                                  \
-		grp = client->group;                                           \
-		if (!grp || !edgetpu_device_group_is_leader(grp, client)) {    \
-			UNLOCK(client);                                        \
-			return -EINVAL;                                        \
-		}                                                              \
-	} while (0)
+static inline bool lock_check_group_leader(struct edgetpu_client *client)
+{
+	LOCK(client);
+	if (!client->group || !edgetpu_device_group_is_leader(client->group, client)) {
+		UNLOCK(client);
+		return false;
+	}
+	return true;
+}
 
 bool is_edgetpu_file(struct file *file)
 {
@@ -139,16 +140,15 @@
 static int edgetpu_ioctl_set_eventfd(struct edgetpu_client *client,
 				     struct edgetpu_event_register __user *argp)
 {
-	struct edgetpu_device_group *group;
 	int ret;
 	struct edgetpu_event_register eventreg;
 
 	if (copy_from_user(&eventreg, argp, sizeof(eventreg)))
 		return -EFAULT;
 
-	LOCK_RETURN_IF_NOT_LEADER(client, group);
-	ret = edgetpu_group_set_eventfd(group, eventreg.event_id,
-					eventreg.eventfd);
+	if (!lock_check_group_leader(client))
+		return -EINVAL;
+	ret = edgetpu_group_set_eventfd(client->group, eventreg.event_id, eventreg.eventfd);
 	UNLOCK(client);
 	return ret;
 }
@@ -156,10 +156,9 @@
 static int edgetpu_ioctl_unset_eventfd(struct edgetpu_client *client,
 				       uint event_id)
 {
-	struct edgetpu_device_group *group;
-
-	LOCK_RETURN_IF_NOT_LEADER(client, group);
-	edgetpu_group_unset_eventfd(group, event_id);
+	if (!lock_check_group_leader(client))
+		return -EINVAL;
+	edgetpu_group_unset_eventfd(client->group, event_id);
 	UNLOCK(client);
 	return 0;
 }
@@ -311,9 +310,10 @@
 
 	trace_edgetpu_map_buffer_start(&ibuf);
 
-	LOCK_RETURN_IF_NOT_LEADER(client, group);
+	if (!lock_check_group_leader(client))
+		return -EINVAL;
 	/* to prevent group being released when we perform map/unmap later */
-	group = edgetpu_device_group_get(group);
+	group = edgetpu_device_group_get(client->group);
 	/*
 	 * Don't hold @client->group_lock on purpose since
 	 * 1. We don't care whether @client still belongs to @group.
@@ -343,15 +343,15 @@
 				      struct edgetpu_map_ioctl __user *argp)
 {
 	struct edgetpu_map_ioctl ibuf;
-	struct edgetpu_device_group *group;
 	int ret;
 
 	if (copy_from_user(&ibuf, argp, sizeof(ibuf)))
 		return -EFAULT;
 
-	LOCK_RETURN_IF_NOT_LEADER(client, group);
-	ret = edgetpu_device_group_unmap(group, ibuf.die_index,
-					 ibuf.device_address, ibuf.flags);
+	if (!lock_check_group_leader(client))
+		return -EINVAL;
+	ret = edgetpu_device_group_unmap(client->group, ibuf.die_index, ibuf.device_address,
+					 ibuf.flags);
 	UNLOCK(client);
 	return ret;
 }
@@ -369,14 +369,14 @@
 static int edgetpu_ioctl_sync_buffer(struct edgetpu_client *client,
 				     struct edgetpu_sync_ioctl __user *argp)
 {
-	struct edgetpu_device_group *group;
 	int ret;
 	struct edgetpu_sync_ioctl ibuf;
 
 	if (copy_from_user(&ibuf, argp, sizeof(ibuf)))
 		return -EFAULT;
-	LOCK_RETURN_IF_NOT_LEADER(client, group);
-	ret = edgetpu_device_group_sync_buffer(group, &ibuf);
+	if (!lock_check_group_leader(client))
+		return -EINVAL;
+	ret = edgetpu_device_group_sync_buffer(client->group, &ibuf);
 	UNLOCK(client);
 	return ret;
 }
@@ -394,9 +394,10 @@
 
 	trace_edgetpu_map_dmabuf_start(&ibuf);
 
-	LOCK_RETURN_IF_NOT_LEADER(client, group);
+	if (!lock_check_group_leader(client))
+		return -EINVAL;
 	/* to prevent group being released when we perform unmap on fault */
-	group = edgetpu_device_group_get(group);
+	group = edgetpu_device_group_get(client->group);
 	ret = edgetpu_map_dmabuf(group, &ibuf);
 	UNLOCK(client);
 	if (ret)
@@ -419,14 +420,14 @@
 edgetpu_ioctl_unmap_dmabuf(struct edgetpu_client *client,
 			   struct edgetpu_map_dmabuf_ioctl __user *argp)
 {
-	struct edgetpu_device_group *group;
 	int ret;
 	struct edgetpu_map_dmabuf_ioctl ibuf;
 
 	if (copy_from_user(&ibuf, argp, sizeof(ibuf)))
 		return -EFAULT;
-	LOCK_RETURN_IF_NOT_LEADER(client, group);
-	ret = edgetpu_unmap_dmabuf(group, ibuf.die_index, ibuf.device_address);
+	if (!lock_check_group_leader(client))
+		return -EINVAL;
+	ret = edgetpu_unmap_dmabuf(client->group, ibuf.die_index, ibuf.device_address);
 	UNLOCK(client);
 	return ret;
 }
@@ -476,9 +477,10 @@
 	if (copy_from_user(&ibuf, argp, sizeof(ibuf)))
 		return -EFAULT;
 
-	LOCK_RETURN_IF_NOT_LEADER(client, group);
+	if (!lock_check_group_leader(client))
+		return -EINVAL;
 	/* to prevent group being released when we perform unmap on fault */
-	group = edgetpu_device_group_get(group);
+	group = edgetpu_device_group_get(client->group);
 	ret = edgetpu_map_bulk_dmabuf(group, &ibuf);
 	UNLOCK(client);
 	if (ret)
@@ -497,14 +499,14 @@
 	struct edgetpu_client *client,
 	struct edgetpu_map_bulk_dmabuf_ioctl __user *argp)
 {
-	struct edgetpu_device_group *group;
 	int ret;
 	struct edgetpu_map_bulk_dmabuf_ioctl ibuf;
 
 	if (copy_from_user(&ibuf, argp, sizeof(ibuf)))
 		return -EFAULT;
-	LOCK_RETURN_IF_NOT_LEADER(client, group);
-	ret = edgetpu_unmap_bulk_dmabuf(group, ibuf.device_address);
+	if (!lock_check_group_leader(client))
+		return -EINVAL;
+	ret = edgetpu_unmap_bulk_dmabuf(client->group, ibuf.device_address);
 	UNLOCK(client);
 	return ret;
 }
diff --git a/drivers/edgetpu/edgetpu-kci.c b/drivers/edgetpu/edgetpu-kci.c
index b3c3d4c..aea57f3 100644
--- a/drivers/edgetpu/edgetpu-kci.c
+++ b/drivers/edgetpu/edgetpu-kci.c
@@ -42,16 +42,15 @@
 #define KCI_TIMEOUT	(1000)
 #endif
 
-/* A macro for KCIs to leave early when the device state is known to be bad. */
-#define RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, opstring)                                              \
-	do {                                                                                       \
-		int ret = edgetpu_get_state_errno_locked(kci->mailbox->etdev);                     \
-		if (ret) {                                                                         \
-			etdev_err(kci->mailbox->etdev, "%s failed: device state %u (%d)",          \
-				  opstring, kci->mailbox->etdev->state, ret);                      \
-			return ret;                                                                \
-		}                                                                                  \
-	} while (0)
+static inline int check_etdev_state(struct edgetpu_kci *kci, char *opstring)
+{
+	int ret = edgetpu_get_state_errno_locked(kci->mailbox->etdev);
+
+	if (ret)
+		etdev_err(kci->mailbox->etdev, "%s failed: device state %u (%d)",
+			  opstring, kci->mailbox->etdev->state, ret);
+	return ret;
+}
 
 static inline u32 edgetpu_kci_queue_element_size(enum mailbox_queue_type type)
 {
@@ -805,10 +804,13 @@
 		.n_dies = n_dies,
 		.vid = vid,
 	};
+	int ret;
 
 	if (!kci)
 		return -ENODEV;
-	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, "join group");
+	ret = check_etdev_state(kci, "join group");
+	if (ret)
+		return ret;
 	return edgetpu_kci_send_cmd_with_data(kci, &cmd, &detail, sizeof(detail));
 }
 
@@ -817,10 +819,13 @@
 	struct edgetpu_command_element cmd = {
 		.code = KCI_CODE_LEAVE_GROUP,
 	};
+	int ret;
 
 	if (!kci)
 		return -ENODEV;
-	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, "leave group");
+	ret = check_etdev_state(kci, "leave group");
+	if (ret)
+		return ret;
 	return edgetpu_kci_send_cmd(kci, &cmd);
 }
 
@@ -1046,10 +1051,13 @@
 			.flags = mailbox_map,
 		},
 	};
+	int ret;
 
 	if (!kci)
 		return -ENODEV;
-	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, "open device");
+	ret = check_etdev_state(kci, "open device");
+	if (ret)
+		return ret;
 	if (vcid < 0)
 		return edgetpu_kci_send_cmd(kci, &cmd);
 	return edgetpu_kci_send_cmd_with_data(kci, &cmd, &detail, sizeof(detail));
@@ -1063,10 +1071,13 @@
 			.flags = mailbox_map,
 		},
 	};
+	int ret;
 
 	if (!kci)
 		return -ENODEV;
-	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, "close device");
+	ret = check_etdev_state(kci, "close device");
+	if (ret)
+		return ret;
 	return edgetpu_kci_send_cmd(kci, &cmd);
 }