Merge cherrypicks of ['partner-android-review.googlesource.com/2939861', 'partner-android-review.googlesource.com/2947570'] into sparse-12362889-L89400030006799064.
SPARSE_CHANGE: I698e0cc56d8e003f520df39743f52459af898fbf
SPARSE_CHANGE: I8246b1f4598777832d22da398a6ff822a9cc506e


Change-Id: I525c4453d314883554c35ee2db1304e28102f789
Signed-off-by: Coastguard Worker <[email protected]>
diff --git a/drivers/soc/google/vh/kernel/sched/sched_priv.h b/drivers/soc/google/vh/kernel/sched/sched_priv.h
index 54ee7e2..0f9d140 100644
--- a/drivers/soc/google/vh/kernel/sched/sched_priv.h
+++ b/drivers/soc/google/vh/kernel/sched/sched_priv.h
@@ -429,6 +429,37 @@
 	return get_vendor_task_struct(p)->is_binder_task;
 }
 
+static inline bool should_auto_prefer_idle(struct task_struct *p, int group)
+{
+	if (group == VG_TOPAPP) {
+		/*
+		 * Binder Task
+		 */
+		if (is_binder_task(p))
+			return true;
+		/*
+		 * Task of prio <= 120 and with possitive wake_q_count
+		 */
+		if (p->prio <= DEFAULT_PRIO && p->wake_q_count)
+			return true;
+		/*
+		 * Task of prio <= 120 and waked up by another process
+		 */
+		if (current) {
+			if (p->prio <= DEFAULT_PRIO && current->tgid != p->tgid)
+				return true;
+		}
+	} else if (group == VG_FOREGROUND) {
+		/*
+		 * Binder Task
+		 */
+		if (is_binder_task(p))
+			return true;
+	}
+
+	return false;
+}
+
 static inline bool get_prefer_idle(struct task_struct *p)
 {
 	struct vendor_task_struct *vp = get_vendor_task_struct(p);
@@ -440,8 +471,7 @@
 	if (get_uclamp_fork_reset(p, true) || vp->prefer_idle || vbinder->prefer_idle)
 		return true;
 	else if (vendor_sched_auto_prefer_idle)
-		return vp->group == VG_TOPAPP && ((p->prio <= DEFAULT_PRIO && p->wake_q_count) ||
-			is_binder_task(p));
+		return should_auto_prefer_idle(p, vp->group);
 	else if (vendor_sched_reduce_prefer_idle)
 		return (vg[vp->group].prefer_idle && p->prio <= DEFAULT_PRIO &&
 			uclamp_eff_value_pixel_mod(p, UCLAMP_MAX) == SCHED_CAPACITY_SCALE);