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

edgetpu: hold mmap_lock around pin_user_pages
mmap_lock must be held around call to pin_user_pages to protect against
concurrent address space modifications.
Bug: 283787360
edgetpu: usage-stats remove unnecessary out of memory log
edgetpu: return -EIO for operations on device with bad firmware state
GitOrigin-RevId: 2bde5e1f63d19540e003164600bd173b644dec57
Change-Id: I8ea1e112b54f76b9199d991501643aaf57985781
diff --git a/drivers/edgetpu/edgetpu-core.c b/drivers/edgetpu/edgetpu-core.c
index 6973bdb..db806bf 100644
--- a/drivers/edgetpu/edgetpu-core.c
+++ b/drivers/edgetpu/edgetpu-core.c
@@ -380,11 +380,10 @@
 {
 	switch (etdev->state) {
 	case ETDEV_STATE_BAD:
-		return -ENODEV;
+	case ETDEV_STATE_NOFW:
+		return -EIO;
 	case ETDEV_STATE_FWLOADING:
 		return -EAGAIN;
-	case ETDEV_STATE_NOFW:
-		return -EINVAL;
 	default:
 		break;
 	}
diff --git a/drivers/edgetpu/edgetpu-device-group.c b/drivers/edgetpu/edgetpu-device-group.c
index 9b85679..c410ebc 100644
--- a/drivers/edgetpu/edgetpu-device-group.c
+++ b/drivers/edgetpu/edgetpu-device-group.c
@@ -1258,8 +1258,18 @@
 		kvfree(pages);
 		return ERR_PTR(-ENOMEM);
 	}
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 8, 0)
+	down_read(&current->mm->mmap_sem);
+#else
+	mmap_read_lock(current->mm);
+#endif
 	ret = pin_user_pages(host_addr & PAGE_MASK, num_pages, foll_flags,
 			     pages, vmas);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 8, 0)
+	up_read(&current->mm->mmap_sem);
+#else
+	mmap_read_unlock(current->mm);
+#endif
 	kvfree(vmas);
 	if (ret < 0) {
 		etdev_dbg(etdev, "pin_user_pages failed %u:%pK-%u: %d",
diff --git a/drivers/edgetpu/edgetpu-kci.c b/drivers/edgetpu/edgetpu-kci.c
index 313e9f8..b3c3d4c 100644
--- a/drivers/edgetpu/edgetpu-kci.c
+++ b/drivers/edgetpu/edgetpu-kci.c
@@ -43,11 +43,14 @@
 #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)                                                        \
+#define RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, opstring)                                              \
 	do {                                                                                       \
 		int ret = edgetpu_get_state_errno_locked(kci->mailbox->etdev);                     \
-		if (ret)                                                                           \
+		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 u32 edgetpu_kci_queue_element_size(enum mailbox_queue_type type)
@@ -805,7 +808,7 @@
 
 	if (!kci)
 		return -ENODEV;
-	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci);
+	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, "join group");
 	return edgetpu_kci_send_cmd_with_data(kci, &cmd, &detail, sizeof(detail));
 }
 
@@ -817,7 +820,7 @@
 
 	if (!kci)
 		return -ENODEV;
-	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci);
+	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, "leave group");
 	return edgetpu_kci_send_cmd(kci, &cmd);
 }
 
@@ -1046,7 +1049,7 @@
 
 	if (!kci)
 		return -ENODEV;
-	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci);
+	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, "open device");
 	if (vcid < 0)
 		return edgetpu_kci_send_cmd(kci, &cmd);
 	return edgetpu_kci_send_cmd_with_data(kci, &cmd, &detail, sizeof(detail));
@@ -1063,7 +1066,7 @@
 
 	if (!kci)
 		return -ENODEV;
-	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci);
+	RETURN_ERRNO_IF_ETDEV_NOT_GOOD(kci, "close device");
 	return edgetpu_kci_send_cmd(kci, &cmd);
 }
 
diff --git a/drivers/edgetpu/edgetpu-usage-stats.c b/drivers/edgetpu/edgetpu-usage-stats.c
index 9934ca6..ba9d673 100644
--- a/drivers/edgetpu/edgetpu-usage-stats.c
+++ b/drivers/edgetpu/edgetpu-usage-stats.c
@@ -959,11 +959,8 @@
 
 	ustats = devm_kzalloc(etdev->dev, sizeof(*etdev->usage_stats),
 			      GFP_KERNEL);
-	if (!ustats) {
-		etdev_warn(etdev,
-			   "failed to allocate memory for usage stats\n");
+	if (!ustats)
 		return;
-	}
 
 	hash_init(ustats->uid_hash_table);
 	mutex_init(&ustats->usage_stats_lock);