Skip to contentSkip to conversation

lib/smp_processor_id: Don't use cpumask_equal()

PATCH Applied Waiman Long 5 participants 6 messages Oct 3, 2019 – Oct 9, 2019 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lib/smp_processor_id: Don't use cpumask_equal()

The check_preemption_disabled() function uses cpumask_equal() to see if the task is bounded to the current CPU only. cpumask_equal() calls memcmp() to do the comparison. As x86 doesn't have __HAVE_ARCH_MEMCMP, the slow memcmp() function in lib/string.c is used.

On a RT kernel that call check_preemption_disabled() very frequently, below is the perf-record output of a certain microbenchmark:

42.75% 2.45% testpmd [kernel.kallsyms] [k] check_preemption_disabled 40.01% 39.97% testpmd [kernel.kallsyms] [k] memcmp

We should avoid calling memcmp() in performance critical path. So the cpumask_equal() call is now replaced with an equivalent simpler check.

---
 lib/smp_processor_id.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/smp_processor_id.c +1 -1
index 60ba93fc42ce..bd9571653288 100644
@@ -23,7 +23,7 @@ unsigned int check_preemption_disabled(const char *what1, const char *what2)
 	 * Kernel threads bound to a single CPU can safely use
 	 * smp_processor_id():
 	 */
-	if (cpumask_equal(current->cpus_ptr, cpumask_of(this_cpu)))
+	if (current->nr_cpus_allowed == 1)
 		goto out;
 
 	/*
Signature · 1 line
2.18.1
Acked-by on v2 1/1
#1 lore raw
Quoted 5 lines · Waiman Long

The check_preemption_disabled() function uses cpumask_equal() to see if the task is bounded to the current CPU only. cpumask_equal() calls memcmp() to do the comparison. As x86 doesn't have __HAVE_ARCH_MEMCMP, the slow memcmp() function in lib/string.c is used.

Quoted 8 lines

On a RT kernel that call check_preemption_disabled() very frequently, below is the perf-record output of a certain microbenchmark:

42.75% 2.45% testpmd [kernel.kallsyms] [k] check_preemption_disabled 40.01% 39.97% testpmd [kernel.kallsyms] [k] memcmp

We should avoid calling memcmp() in performance critical path. So the cpumask_equal() call is now replaced with an equivalent simpler check.

using a simple integer comparison is still more efficient than what __HAVE_ARCH_MEMCMP can offer.

Quoted 1 line

Sebastian

#2 lore raw
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Waiman Long

The check_preemption_disabled() function uses cpumask_equal() to see if the task is bounded to the current CPU only. cpumask_equal() calls memcmp() to do the comparison. As x86 doesn't have __HAVE_ARCH_MEMCMP, the slow memcmp() function in lib/string.c is used.

On a RT kernel that call check_preemption_disabled() very frequently, below is the perf-record output of a certain microbenchmark:

Quoted 7 lines
Quoted 5 lines

42.75% 2.45% testpmd [kernel.kallsyms] [k] check_preemption_disabled 40.01% 39.97% testpmd [kernel.kallsyms] [k] memcmp

We should avoid calling memcmp() in performance critical path. So the cpumask_equal() call is now replaced with an equivalent simpler check.

using a simple integer comparison is still more efficient than what __HAVE_ARCH_MEMCMP can offer.

You are right. My main point is to try to avoid using cpumask_equal() in performance critical path irrespective of this patch.

Quoted 4 lines
Quoted 1 line

Sebastian

Cheers,
Longman

Reviewed-by on v2 1/1
#3 lore raw
In reply to Waiman Long

Hi,

Quoted 24 lines · Waiman Long

The check_preemption_disabled() function uses cpumask_equal() to see if the task is bounded to the current CPU only. cpumask_equal() calls memcmp() to do the comparison. As x86 doesn't have __HAVE_ARCH_MEMCMP, the slow memcmp() function in lib/string.c is used.

On a RT kernel that call check_preemption_disabled() very frequently, below is the perf-record output of a certain microbenchmark:

42.75% 2.45% testpmd [kernel.kallsyms] [k] check_preemption_disabled 40.01% 39.97% testpmd [kernel.kallsyms] [k] memcmp

We should avoid calling memcmp() in performance critical path. So the cpumask_equal() call is now replaced with an equivalent simpler check.

---
 lib/smp_processor_id.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/smp_processor_id.c +0 -0
index 60ba93fc42ce..bd9571653288 100644

lib/smp_processor_id.c unsigned int check_preemption_disabled(const char *what1, const char *what2) · line 27

Quoted 6 lines
lib/smp_processor_id.c +1 -1
@@ -23,7 +23,7 @@ unsigned int check_preemption_disabled(const char *what1, const char *what2)
 	 * Kernel threads bound to a single CPU can safely use
 	 * smp_processor_id():
 	 */
-	if (cpumask_equal(current->cpus_ptr, cpumask_of(this_cpu)))
+	if (current->nr_cpus_allowed == 1)
 		goto out;

Makes sense to me.

Thanks,

Juri

#4 lore raw
In reply to Waiman Long
Quoted 8 lines · Waiman Long

The check_preemption_disabled() function uses cpumask_equal() to see if the task is bounded to the current CPU only. cpumask_equal() calls memcmp() to do the comparison. As x86 doesn't have __HAVE_ARCH_MEMCMP, the slow memcmp() function in lib/string.c is used.

On a RT kernel that call check_preemption_disabled() very frequently, below is the perf-record output of a certain microbenchmark:

Quoted 7 lines

42.75% 2.45% testpmd [kernel.kallsyms] [k] check_preemption_disabled 40.01% 39.97% testpmd [kernel.kallsyms] [k] memcmp

We should avoid calling memcmp() in performance critical path. So the cpumask_equal() call is now replaced with an equivalent simpler check.

Thanks!

merge bot Applied
#5 lore raw
In reply to Waiman Long
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     e950cca3f3c40902a052a78a36b3fac1f8a62d19
Gitweb:        https://git.kernel.org/tip/e950cca3f3c40902a052a78a36b3fac1f8a62d19
Author:        Waiman Long <[email protected]>
AuthorDate:    Thu, 03 Oct 2019 16:36:08 -04:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 09 Oct 2019 12:46:10 +02:00

lib/smp_processor_id: Don't use cpumask_equal()

The check_preemption_disabled() function uses cpumask_equal() to see if the task is bounded to the current CPU only. cpumask_equal() calls memcmp() to do the comparison. As x86 doesn't have __HAVE_ARCH_MEMCMP, the slow memcmp() function in lib/string.c is used.

On a RT kernel that call check_preemption_disabled() very frequently, below is the perf-record output of a certain microbenchmark:

42.75% 2.45% testpmd [kernel.kallsyms] [k] check_preemption_disabled 40.01% 39.97% testpmd [kernel.kallsyms] [k] memcmp

We should avoid calling memcmp() in performance critical path. So the cpumask_equal() call is now replaced with an equivalent simpler check.

---
 lib/smp_processor_id.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/smp_processor_id.c +1 -1
index 60ba93f..bd95716 100644
@@ -23,7 +23,7 @@ unsigned int check_preemption_disabled(const char *what1, const char *what2)
 	 * Kernel threads bound to a single CPU can safely use
 	 * smp_processor_id():
 	 */
-	if (cpumask_equal(current->cpus_ptr, cpumask_of(this_cpu)))
+	if (current->nr_cpus_allowed == 1)
 		goto out;
 
 	/*