Skip to contentSkip to conversation

eal: fix core_index for non-EAL registered threads

PATCH Applied Maxime Peim 2 participants 6 messages Apr 24 – Jun 11

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal: fix core_index for non-EAL registered threads

Threads registered via rte_thread_register() are assigned a valid lcore_id by eal_lcore_non_eal_allocate(), but their core_index in lcore_config is left at -1. This value was set during rte_eal_cpu_init() for lcores with ROLE_OFF (undetected CPUs) and is never updated when the lcore is later allocated to a non-EAL thread.

As a result, rte_lcore_index() returns -1 for registered non-EAL threads. Libraries that use rte_lcore_index() to select per-lcore caches fall back to a shared global path when it returns -1, causing severe contention under concurrent access from multiple registered threads.

A concrete example is the mlx5 indexed memory pool (mlx5_ipool), which uses rte_lcore_index() in mlx5_ipool_malloc_cache() to select a per-core cache slot. When core_index is -1, all registered threads are funneled into a single shared slot protected by a spinlock. In testing with VPP (which registers worker threads via rte_thread_register()), this caused async flow rule insertion throughput to drop from ~6.4M rules/sec to ~1.2M rules/sec with 4 workers -- a 5x regression attributable entirely to spinlock contention in the ipool allocator.

Fix by setting core_index to the next sequential index (cfg->lcore_count) in eal_lcore_non_eal_allocate() before incrementing the count. Also reset core_index back to -1 on the error rollback path and in eal_lcore_non_eal_release() for correctness.

Fixes 5c307ba2a5b1 ("eal: register non-EAL threads as lcores")
---
 lib/eal/common/eal_common_lcore.c | 3 +++
 1 file changed, 3 insertions(+)
lib/eal/common/eal_common_lcore.c +3 -0
index 39411f9370..ae085d73e4 100644
@@ -378,6 +378,7 @@ eal_lcore_non_eal_allocate(void)
 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
 		if (cfg->lcore_role[lcore_id] != ROLE_OFF)
 			continue;
+		lcore_config[lcore_id].core_index = cfg->lcore_count;
 		cfg->lcore_role[lcore_id] = ROLE_NON_EAL;
 		cfg->lcore_count++;
 		break;
@@ -399,6 +400,7 @@ eal_lcore_non_eal_allocate(void)
 		}
 		EAL_LOG(DEBUG, "Initialization refused for lcore %u.",
 			lcore_id);
+		lcore_config[lcore_id].core_index = -1;
 		cfg->lcore_role[lcore_id] = ROLE_OFF;
 		cfg->lcore_count--;
 		lcore_id = RTE_MAX_LCORE;
@@ -420,6 +422,7 @@ eal_lcore_non_eal_release(unsigned int lcore_id)
 		goto out;
 	TAILQ_FOREACH(callback, &lcore_callbacks, next)
 		callback_uninit(callback, lcore_id);
+	lcore_config[lcore_id].core_index = -1;
 	cfg->lcore_role[lcore_id] = ROLE_OFF;
 	cfg->lcore_count--;
 out:
Signature · 1 line
2.43.0
#1 lore raw

Hello,

Quoted 22 lines · Maxime Peim

Threads registered via rte_thread_register() are assigned a valid lcore_id by eal_lcore_non_eal_allocate(), but their core_index in lcore_config is left at -1. This value was set during rte_eal_cpu_init() for lcores with ROLE_OFF (undetected CPUs) and is never updated when the lcore is later allocated to a non-EAL thread.

As a result, rte_lcore_index() returns -1 for registered non-EAL threads. Libraries that use rte_lcore_index() to select per-lcore caches fall back to a shared global path when it returns -1, causing severe contention under concurrent access from multiple registered threads.

A concrete example is the mlx5 indexed memory pool (mlx5_ipool), which uses rte_lcore_index() in mlx5_ipool_malloc_cache() to select a per-core cache slot. When core_index is -1, all registered threads are funneled into a single shared slot protected by a spinlock. In testing with VPP (which registers worker threads via rte_thread_register()), this caused async flow rule insertion throughput to drop from ~6.4M rules/sec to ~1.2M rules/sec with 4 workers -- a 5x regression attributable entirely to spinlock contention in the ipool allocator.

Quoted 7 lines

Fix by setting core_index to the next sequential index (cfg->lcore_count) in eal_lcore_non_eal_allocate() before incrementing the count. Also reset core_index back to -1 on the error rollback path and in eal_lcore_non_eal_release() for correctness.

Fixes 5c307ba2a5b1 ("eal: register non-EAL threads as lcores")

Thanks for the fix Maxime, it looks correct though I am a bit skeptical about usage of this API with dynamic thread allocation.

In the net/mlx5 context, for example, I expect no memory saving from using the lcore "index": mlx5 is allocating an array with RTE_MAX_LCORE+1 entries.
Using rte_lcore_id() would probably be good enough. Dariusz, Slava, any opinion?

Signature · 1 line
David Marchand
Acked-by on v1 1/1 Applied
#2 lore raw
In reply to Maxime Peim
Quoted 22 lines · Maxime Peim

Threads registered via rte_thread_register() are assigned a valid lcore_id by eal_lcore_non_eal_allocate(), but their core_index in lcore_config is left at -1. This value was set during rte_eal_cpu_init() for lcores with ROLE_OFF (undetected CPUs) and is never updated when the lcore is later allocated to a non-EAL thread.

As a result, rte_lcore_index() returns -1 for registered non-EAL threads. Libraries that use rte_lcore_index() to select per-lcore caches fall back to a shared global path when it returns -1, causing severe contention under concurrent access from multiple registered threads.

A concrete example is the mlx5 indexed memory pool (mlx5_ipool), which uses rte_lcore_index() in mlx5_ipool_malloc_cache() to select a per-core cache slot. When core_index is -1, all registered threads are funneled into a single shared slot protected by a spinlock. In testing with VPP (which registers worker threads via rte_thread_register()), this caused async flow rule insertion throughput to drop from ~6.4M rules/sec to ~1.2M rules/sec with 4 workers -- a 5x regression attributable entirely to spinlock contention in the ipool allocator.

Quoted 6 lines

Fix by setting core_index to the next sequential index (cfg->lcore_count) in eal_lcore_non_eal_allocate() before incrementing the count. Also reset core_index back to -1 on the error rollback path and in eal_lcore_non_eal_release() for correctness.

Fixes 5c307ba2a5b1 ("eal: register non-EAL threads as lcores")
Quoted 1 line

Applied, thanks.

Signature · 1 line
David Marchand
#3 lore raw
Quoted 29 lines · David Marchand
Quoted 27 lines · Maxime Peim

Threads registered via rte_thread_register() are assigned a valid lcore_id by eal_lcore_non_eal_allocate(), but their core_index in lcore_config is left at -1. This value was set during rte_eal_cpu_init() for lcores with ROLE_OFF (undetected CPUs) and is never updated when the lcore is later allocated to a non-EAL thread.

As a result, rte_lcore_index() returns -1 for registered non-EAL threads. Libraries that use rte_lcore_index() to select per-lcore caches fall back to a shared global path when it returns -1, causing severe contention under concurrent access from multiple registered threads.

A concrete example is the mlx5 indexed memory pool (mlx5_ipool), which uses rte_lcore_index() in mlx5_ipool_malloc_cache() to select a per-core cache slot. When core_index is -1, all registered threads are funneled into a single shared slot protected by a spinlock. In testing with VPP (which registers worker threads via rte_thread_register()), this caused async flow rule insertion throughput to drop from ~6.4M rules/sec to ~1.2M rules/sec with 4 workers -- a 5x regression attributable entirely to spinlock contention in the ipool allocator.

Fix by setting core_index to the next sequential index (cfg->lcore_count) in eal_lcore_non_eal_allocate() before incrementing the count. Also reset core_index back to -1 on the error rollback path and in eal_lcore_non_eal_release() for correctness.

Quoted 6 lines
Quoted 1 line
Fixes 5c307ba2a5b1 ("eal: register non-EAL threads as lcores")
Quoted 1 line

Hum, I did not push the change.
Re-reading this code, we have an issue if some external thread unregisters in the middle.

What do you think of the additional hunk:

$ git diff
(quoted diff) +0 -0

b/lib/eal/common/eal_common_lcore.c
index ae085d73e4..6f53f20d90 100644

lib/eal/common/eal_common_lcore.c +5 -2
@@ -372,13 +372,16 @@ eal_lcore_non_eal_allocate(void)
        struct rte_config *cfg = rte_eal_get_configuration();
        struct lcore_callback *callback;
        struct lcore_callback *prev;
+       unsigned int index = 0;
        unsigned int lcore_id;
 
        rte_rwlock_write_lock(&lcore_lock);
        for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
-               if (cfg->lcore_role[lcore_id] != ROLE_OFF)
+               if (cfg->lcore_role[lcore_id] != ROLE_OFF) {
+                       index++;
                        continue;
-               lcore_config[lcore_id].core_index = cfg->lcore_count;
+               }
+               lcore_config[lcore_id].core_index = index;
                cfg->lcore_role[lcore_id] = ROLE_NON_EAL;
                cfg->lcore_count++;
                break;
Signature · 1 line
David Marchand
#4 lore raw

Hi David

I am not sure it works either, if the lcore are manual set with a gap:
--lcores=0,7 (from eal_parse_lcores):
- lcore 0 will get core_index = 0
- lcore 7 will get core_index = 1

When calling rte_thread_register we will hit lcore=1 as first not-assigned lcore and set core_index=1 as well.

It seems like a solution should be to have a bitmap of the currently used core_index stored in the global config.

Please let me know what you think about that.

Maxime Peim

Quoted 80 lines · David Marchand
Quoted 6 lines · David Marchand
Quoted 4 lines · Maxime Peim

Threads registered via rte_thread_register() are assigned a valid lcore_id by eal_lcore_non_eal_allocate(), but their core_index in lcore_config is left at -1. This value was set during

rte_eal_cpu_init()

Quoted 1 line
Quoted 1 line

for lcores with ROLE_OFF (undetected CPUs) and is never updated when

the

Quoted 10 lines
Quoted 10 lines

lcore is later allocated to a non-EAL thread.

As a result, rte_lcore_index() returns -1 for registered non-EAL threads. Libraries that use rte_lcore_index() to select per-lcore caches fall back to a shared global path when it returns -1, causing severe contention under concurrent access from multiple registered threads.

A concrete example is the mlx5 indexed memory pool (mlx5_ipool), which uses rte_lcore_index() in mlx5_ipool_malloc_cache() to select a

per-core

Quoted 8 lines
Quoted 8 lines

cache slot. When core_index is -1, all registered threads are funneled into a single shared slot protected by a spinlock. In testing with VPP (which registers worker threads via rte_thread_register()), this caused async flow rule insertion throughput to drop from ~6.4M rules/sec to ~1.2M rules/sec with 4 workers -- a 5x regression attributable entirely to spinlock contention in the ipool allocator.

Fix by setting core_index to the next sequential index

(cfg->lcore_count)

Quoted 1 line
Quoted 1 line

in eal_lcore_non_eal_allocate() before incrementing the count. Also

reset

Quoted 9 lines
Quoted 4 lines

core_index back to -1 on the error rollback path and in eal_lcore_non_eal_release() for correctness.

Fixes 5c307ba2a5b1 ("eal: register non-EAL threads as lcores")
Quoted 1 line

Hum, I did not push the change.
Re-reading this code, we have an issue if some external thread unregisters in the middle.

What do you think of the additional hunk:

$ git diff
lib/eal/common/eal_common_lcore.c +5 -3
index ae085d73e4..6f53f20d90 100644
@@ -372,13 +372,16 @@ eal_lcore_non_eal_allocate(void)
        struct rte_config *cfg = rte_eal_get_configuration();
        struct lcore_callback *callback;
        struct lcore_callback *prev;
+       unsigned int index = 0;
        unsigned int lcore_id;
 
        rte_rwlock_write_lock(&lcore_lock);
        for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
-               if (cfg->lcore_role[lcore_id] != ROLE_OFF)
+               if (cfg->lcore_role[lcore_id] != ROLE_OFF) {
+                       index++;
                        continue;
-               lcore_config[lcore_id].core_index = cfg->lcore_count;
+               }
+               lcore_config[lcore_id].core_index = index;
                cfg->lcore_role[lcore_id] = ROLE_NON_EAL;
                cfg->lcore_count++;
                break;
 
 
--

David Marchand

#5 lore raw
Quoted 6 lines · Maxime Peim

I am not sure it works either, if the lcore are manual set with a gap: --lcores=0,7 (from eal_parse_lcores):
- lcore 0 will get core_index = 0
- lcore 7 will get core_index = 1

When calling rte_thread_register we will hit lcore=1 as first not-assigned lcore and set core_index=1 as well.

Ah ah yes...

Quoted 1 line

It seems like a solution should be to have a bitmap of the currently used core_index stored in the global config.

Yes, it seems the right way to handle this.
We have a rte_bitmap API but it is probably simpler to go with a rte_bitset.

Signature · 1 line
David Marchand