msm: kgsl: Add context reference counting to KGSL events

Add reference counting to all KGSL events since they are dependent
on the context.  This avoids a race condition where the context
could be destroyed while the events are being processed during cleanup.

CRs-fixed: 438134
Change-Id: Ic2cd484d441e4c392ab63b0a0131accaceafc711
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
diff --git a/drivers/gpu/msm/kgsl.c b/drivers/gpu/msm/kgsl.c
index 8ebca50..2cf9d89 100644
--- a/drivers/gpu/msm/kgsl.c
+++ b/drivers/gpu/msm/kgsl.c
@@ -99,6 +99,10 @@
 	event->func = cb;
 	event->owner = owner;
 
+	/* inc refcount to avoid race conditions in cleanup */
+	if (context)
+		kgsl_context_get(context);
+
 	/*
 	 * Add the event in order to the list.  Order is by context id
 	 * first and then by timestamp for that context.
@@ -153,6 +157,7 @@
 		if (event->func)
 			event->func(device, event->priv, id, cur);
 
+		kgsl_context_put(context);
 		list_del(&event->list);
 		kfree(event);
 	}
@@ -187,6 +192,9 @@
 		if (event->func)
 			event->func(device, event->priv, id, cur);
 
+		if (event->context)
+			kgsl_context_put(event->context);
+
 		list_del(&event->list);
 		kfree(event);
 	}
@@ -445,6 +453,9 @@
 		if (event->func)
 			event->func(device, event->priv, id, ts_processed);
 
+		if (event->context)
+			kgsl_context_put(event->context);
+
 		list_del(&event->list);
 		kfree(event);
 	}