From 2926ff354657c02ad4e1c453138f8670d031d7c4 Mon Sep 17 00:00:00 2001
From: gauravjindal <gaurav.jindal@spreadtrum.com>
Date: Tue, 2 Jun 2015 18:06:48 +0530
Subject: [PATCH 1/1] Save extra cpu cycles in getting slow/fast domain

In present code, it is compulsory to check if the cpu is in slowest/fastest domain before calling the functions;

hmp_slower_domain() and hmp_faster_domain() to get the relative slower/faster domains.

If the caller calls without checking, it will give invlaid address which can result in undefined behavior.

Also in the functions

hmp_select_slower_cpu(), hmp_select_faster_cpu() and  hmp_best_little_cpu(),

extra cpu cycles and instructions can be saved by changing the flow to get the relative slower/faster domain. As with this change the function
hmp_slower_domain() and hmp_faster_domain() will itself take care of the condition if the current cpu is in slowest or fastest domain.
So extra checks before getting the relative faster/slower domains can be avoided thus reducing function calls and prevent cpu cycles thus improving the performance.

Signed-off-by: gauravjindal <gaurav.jindal@spreadtrum.com>
---
 kernel/sched/fair.c |   19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 1baf641..f7c2356 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -3878,6 +3878,8 @@ static inline struct hmp_domain *hmp_slower_domain(int cpu)
 	struct list_head *pos;
 
 	pos = &hmp_cpu_domain(cpu)->hmp_domains;
+	if(list_is_last(pos, &hmp_domains))
+		return hmp_cpu_domain(cpu);
 	return list_entry(pos->next, struct hmp_domain, hmp_domains);
 }
 
@@ -3887,6 +3889,8 @@ static inline struct hmp_domain *hmp_faster_domain(int cpu)
 	struct list_head *pos;
 
 	pos = &hmp_cpu_domain(cpu)->hmp_domains;
+	if( pos == hmp_domains.next)
+		return hmp_cpu_domain(cpu);
 	return list_entry(pos->prev, struct hmp_domain, hmp_domains);
 }
 
@@ -3900,10 +3904,7 @@ static inline unsigned int hmp_select_faster_cpu(struct task_struct *tsk,
 	__always_unused int lowest_ratio;
 	struct hmp_domain *hmp;
 
-	if (hmp_cpu_is_fastest(cpu))
-		hmp = hmp_cpu_domain(cpu);
-	else
-		hmp = hmp_faster_domain(cpu);
+	hmp = hmp_faster_domain(cpu);
 
 	lowest_ratio = hmp_domain_min_load(hmp, &lowest_cpu,
 			tsk_cpus_allowed(tsk));
@@ -3922,10 +3923,7 @@ static inline unsigned int hmp_select_slower_cpu(struct task_struct *tsk,
 	struct hmp_domain *hmp;
 	__always_unused int lowest_ratio;
 
-	if (hmp_cpu_is_slowest(cpu))
-		hmp = hmp_cpu_domain(cpu);
-	else
-		hmp = hmp_slower_domain(cpu);
+	hmp = hmp_slower_domain(cpu);
 
 	lowest_ratio = hmp_domain_min_load(hmp, &lowest_cpu,
 			tsk_cpus_allowed(tsk));
@@ -3952,10 +3950,7 @@ static inline unsigned int hmp_best_little_cpu(struct task_struct *tsk,
 			tsk->se.avg.load_avg_ratio > ((NICE_0_LOAD * 90)/100))
 		return hmp_select_slower_cpu(tsk, cpu);
 
-	if (hmp_cpu_is_slowest(cpu))
-		hmp = hmp_cpu_domain(cpu);
-	else
-		hmp = hmp_slower_domain(cpu);
+	hmp = hmp_slower_domain(cpu);
 
 	/* respect affinity */
 	cpumask_and(&allowed_hmp_cpus, &hmp->cpus,
-- 
1.7.9.5

