Merge cherrypicks of ['partner-android-review.googlesource.com/3018607'] into sparse-12796948-L61900030008455193.
SPARSE_CHANGE: Iffb2b7d83890cad6b2c8dfac4a45355a1284e8c0


Change-Id: Ia712bb078ecfb6d7c438c7312b79b6e579f07b04
Signed-off-by: Coastguard Worker <android-build-coastguard-worker@google.com>
diff --git a/mali_kbase/csf/mali_kbase_csf.c b/mali_kbase/csf/mali_kbase_csf.c
index 734b43b..e1fde23 100644
--- a/mali_kbase/csf/mali_kbase_csf.c
+++ b/mali_kbase/csf/mali_kbase_csf.c
@@ -1728,8 +1728,6 @@
 	int gr;
 	bool reported = false;
 	struct base_gpu_queue_group_error err_payload;
-	int err;
-	struct kbase_device *kbdev;
 
 	if (WARN_ON(!kctx))
 		return;
@@ -1737,14 +1735,6 @@
 	if (WARN_ON(!fault))
 		return;
 
-	kbdev = kctx->kbdev;
-	err = kbase_reset_gpu_try_prevent(kbdev);
-	/* Regardless of whether reset failed or is currently happening, exit
-	 * early
-	 */
-	if (err)
-		return;
-
 	err_payload =
 		(struct base_gpu_queue_group_error){ .error_type = BASE_GPU_QUEUE_GROUP_ERROR_FATAL,
 						     .payload = { .fatal_group = {
@@ -1752,7 +1742,7 @@
 									  .status = fault->status,
 								  } } };
 
-	rt_mutex_lock(&kctx->csf.lock);
+	lockdep_assert_held(&kctx->csf.lock);
 
 	for (gr = 0; gr < MAX_QUEUE_GROUP_NUM; gr++) {
 		struct kbase_queue_group *const group = kctx->csf.queue_groups[gr];
@@ -1773,12 +1763,8 @@
 		}
 	}
 
-	rt_mutex_unlock(&kctx->csf.lock);
-
 	if (reported)
 		kbase_event_wakeup_sync(kctx);
-
-	kbase_reset_gpu_allow(kbdev);
 }
 
 void kbase_csf_ctx_term(struct kbase_context *kctx)
@@ -3380,7 +3366,9 @@
 	for (as = 0; as < kbdev->nr_hw_address_spaces; as++) {
 		unsigned long flags;
 		struct kbase_context *kctx;
-		struct kbase_fault fault;
+		struct kbase_fault fault = (struct kbase_fault){
+			.status = GPU_EXCEPTION_TYPE_SW_FAULT_1,
+		};
 
 		if (as == MCU_AS_NR)
 			continue;
@@ -3397,10 +3385,13 @@
 			spin_unlock_irqrestore(&kbdev->hwaccess_lock, flags);
 			continue;
 		}
-		fault = (struct kbase_fault){
-			.status = GPU_EXCEPTION_TYPE_SW_FAULT_1,
-		};
-		kbase_csf_ctx_handle_fault(kctx, &fault, true);
+		if (!kbase_reset_gpu_try_prevent(kbdev)) {
+			rt_mutex_lock(&kctx->csf.lock);
+			kbase_csf_ctx_handle_fault(kctx, &fault, true);
+			rt_mutex_unlock(&kctx->csf.lock);
+
+			kbase_reset_gpu_allow(kbdev);
+		}
 		kbase_ctx_sched_release_ctx_lock(kctx);
 	}
 	if (kbase_prepare_to_reset_gpu(kbdev, RESET_FLAGS_HWC_UNRECOVERABLE_ERROR))
diff --git a/mali_kbase/mmu/backend/mali_kbase_mmu_csf.c b/mali_kbase/mmu/backend/mali_kbase_mmu_csf.c
index e86a095..9d67142 100644
--- a/mali_kbase/mmu/backend/mali_kbase_mmu_csf.c
+++ b/mali_kbase/mmu/backend/mali_kbase_mmu_csf.c
@@ -145,6 +145,7 @@
 	unsigned int as_no = as->number;
 	unsigned long flags;
 	const uintptr_t fault_addr = fault->addr;
+	int err;
 
 	/* terminal fault, print info about the fault */
 	if (kbdev->gpu_props.gpu_id.product_model < GPU_ID_MODEL_MAKE(14, 0)) {
@@ -167,19 +168,41 @@
 			kctx->pid);
 	}
 
+	err = kbase_reset_gpu_try_prevent(kbdev);
+	if (!err) {
+		/* Switching to UNMAPPED mode will make the firmware recovered from a faulty
+		 * state and become responsive. Just after switching to UNMAPPED mode, if this
+		 * worker thread gets prempted then it wouldn't yet complete terminating affected
+		 * CSG groups and notifying user space of the fault. During the preemption period
+		 * if other thread tries to create or terminate a CSG group for the affected
+		 * context it could end up with a problem racing on this faulty context between
+		 * this worker thread and other thread.
+		 *
+		 * Holding 'csf.lock' in this worker thread before switching UNMAPPED mode will
+		 * hold other threads until the fault handling is done by this worker thread, which
+		 * will prevent the racing problem.
+		 */
+		rt_mutex_lock(&kctx->csf.lock);
+	}
+
 	/* AS transaction begin */
 	spin_lock_irqsave(&kbdev->hwaccess_lock, flags);
 	kbase_mmu_disable(kctx);
 	kbase_ctx_flag_set(kctx, KCTX_AS_DISABLED_ON_FAULT);
 	spin_unlock_irqrestore(&kbdev->hwaccess_lock, flags);
 
-	/* Switching to UNMAPPED mode above would have enabled the firmware to
-	 * recover from the fault (if the memory access was made by firmware)
-	 * and it can then respond to CSG termination requests to be sent now.
-	 * All GPU command queue groups associated with the context would be
-	 * affected as they use the same GPU address space.
-	 */
-	kbase_csf_ctx_handle_fault(kctx, fault, false);
+	if (!err) {
+		/* Switching to UNMAPPED mode above would have enabled the firmware to
+		 * recover from the fault (if the memory access was made by firmware)
+		 * and it can then respond to CSG termination requests to be sent now.
+		 * All GPU command queue groups associated with the context would be
+		 * affected as they use the same GPU address space.
+		 */
+		kbase_csf_ctx_handle_fault(kctx, fault, false);
+		rt_mutex_unlock(&kctx->csf.lock);
+
+		kbase_reset_gpu_allow(kbdev);
+	}
 
 	/* Now clear the GPU fault */
 	spin_lock_irqsave(&kbdev->hwaccess_lock, flags);
@@ -197,6 +220,7 @@
 {
 	unsigned long flags;
 	struct kbase_device *kbdev = kctx->kbdev;
+	int err;
 
 	/* Make sure the context was active */
 	if (WARN_ON(atomic_read(&kctx->refcount) <= 0))
@@ -233,6 +257,23 @@
 		}
 	}
 
+	err = kbase_reset_gpu_try_prevent(kbdev);
+	if (!err) {
+		/* Switching to UNMAPPED mode will make the firmware recovered from a faulty
+		 * state and become responsive. Just after switching to UNMAPPED mode, if this
+		 * worker thread gets prempted then it wouldn't yet complete terminating affected
+		 * CSG groups and notifying user space of the fault. During the preemption period
+		 * if other thread tries to create or terminate a CSG group for the affected
+		 * context it could end up with a problem racing on this faulty context between
+		 * this worker thread and other thread.
+		 *
+		 * Holding 'csf.lock' in this worker thread before switching UNMAPPED mode will
+		 * hold other threads until the fault handling is done by this worker thread, which
+		 * will prevent the racing problem.
+		 */
+		rt_mutex_lock(&kctx->csf.lock);
+	}
+
 	/* AS transaction begin */
 
 	/* switch to UNMAPPED mode,
@@ -255,13 +296,18 @@
 
 	/* AS transaction end */
 
-	/* Switching to UNMAPPED mode above would have enabled the firmware to
-	 * recover from the fault (if the memory access was made by firmware)
-	 * and it can then respond to CSG termination requests to be sent now.
-	 * All GPU command queue groups associated with the context would be
-	 * affected as they use the same GPU address space.
-	 */
-	kbase_csf_ctx_handle_fault(kctx, fault, false);
+	if (!err) {
+		/* Switching to UNMAPPED mode above would have enabled the firmware to
+		 * recover from the fault (if the memory access was made by firmware)
+		 * and it can then respond to CSG termination requests to be sent now.
+		 * All GPU command queue groups associated with the context would be
+		 * affected as they use the same GPU address space.
+		 */
+		kbase_csf_ctx_handle_fault(kctx, fault, false);
+		rt_mutex_unlock(&kctx->csf.lock);
+
+		kbase_reset_gpu_allow(kbdev);
+	}
 
 	/* Clear down the fault */
 	kbase_mmu_hw_clear_fault(kbdev, as, KBASE_MMU_FAULT_TYPE_PAGE_UNEXPECTED);
@@ -474,9 +520,19 @@
 		 status, kbase_gpu_exception_name(GPU_FAULTSTATUS_EXCEPTION_TYPE_GET(status)),
 		 as_nr, (void *)phys_addr, as_valid ? "true" : "false",
 		 status & GPU_FAULTSTATUS_ADDRESS_VALID_MASK ? "true" : "false");
-
 	kctx = kbase_ctx_sched_as_to_ctx(kbdev, as_nr);
-	kbase_csf_ctx_handle_fault(kctx, fault, false);
+	if (!kctx) {
+		atomic_dec(&kbdev->faults_pending);
+		return;
+	}
+	if (!kbase_reset_gpu_try_prevent(kbdev)) {
+		rt_mutex_lock(&kctx->csf.lock);
+		kbase_csf_ctx_handle_fault(kctx, fault, false);
+		rt_mutex_unlock(&kctx->csf.lock);
+
+		kbase_reset_gpu_allow(kbdev);
+	}
+
 	kbase_ctx_sched_release_ctx_lock(kctx);
 
 	/* A work for GPU fault is complete.