summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWhi copybara merger <whitechapel-automerger@google.com>2023-06-08 17:50:24 +0000
committerTodd Poynor <toddpoynor@google.com>2023-06-15 00:52:32 +0000
commitab8f463868d570869db65ad12c74563502978489 (patch)
treed6cb29ea1d04707794749ef51349274d732774f2
parent77305054ef96576b24605b3c303570cd2e34b07b (diff)
downloadjaneiro-android-gs-tangorpro-5.10-u-beta5.3.tar.gz
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
-rw-r--r--drivers/edgetpu/edgetpu-fs.c78
-rw-r--r--drivers/edgetpu/edgetpu-kci.c39
2 files changed, 65 insertions, 52 deletions
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 @@ static struct dentry *edgetpu_debugfs_dir;
#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_fs_release(struct inode *inode, struct file *file)
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_set_eventfd(struct edgetpu_client *client,
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 @@ static int edgetpu_ioctl_map_buffer(struct edgetpu_client *client,
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 @@ static int edgetpu_ioctl_unmap_buffer(struct edgetpu_client *client,
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 @@ edgetpu_ioctl_allocate_device_buffer(struct edgetpu_client *client, u64 size)
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 @@ edgetpu_ioctl_map_dmabuf(struct edgetpu_client *client,
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 @@ static int
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 @@ edgetpu_ioctl_map_bulk_dmabuf(struct edgetpu_client *client,
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 @@ static int edgetpu_ioctl_unmap_bulk_dmabuf(
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 @@ int edgetpu_kci_join_group(struct edgetpu_kci *kci, u8 n_dies, u8 vid)
.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 @@ int edgetpu_kci_leave_group(struct edgetpu_kci *kci)
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 @@ int edgetpu_kci_open_device(struct edgetpu_kci *kci, u32 mailbox_map, u32 client
.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 @@ int edgetpu_kci_close_device(struct edgetpu_kci *kci, u32 mailbox_map)
.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);
}