common/mlx5: fix multi-process mempool registration
cover common/mlx5: fix multi-process mempool registration
Dmitry Kozlyuk (2):
mempool: make event callbacks process-private common/mlx5: fix multi-process mempool registration
drivers/common/mlx5/mlx5_common.c | 15 ++++---- drivers/common/mlx5/mlx5_common_mr.c | 2 +- drivers/common/mlx5/mlx5_common_mr.h | 2 +- lib/mempool/rte_mempool.c | 54 ++++++++-------------------- lib/mempool/rte_mempool.h | 2 ++ 5 files changed, 27 insertions(+), 48 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 mempool: make event callbacks process-private
Callbacks for mempool events were registered in a process-shared tailq. This was inherently incorrect because the same function
may be loaded to a different address in each process.
Make the tailq process-private.
Use the EAL tailq lock to reduce the number of different locks this module operates.
--- lib/mempool/rte_mempool.c | 54 ++++++++++----------------------------- lib/mempool/rte_mempool.h | 2 ++ 2 files changed, 16 insertions(+), 40 deletions(-)
lib/mempool/rte_mempool.c +14 -40
@@ -36,12 +36,10 @@ static struct rte_tailq_elem rte_mempool_tailq = { };EAL_REGISTER_TAILQ (rte_mempool_tailq) -TAILQ_HEAD (mempool_callback_list, rte_tailq_entry); +TAILQ_HEAD (mempool_callback_tailq, mempool_callback_data); -static struct rte_tailq_elem callback_tailq = { - .name ="RTE_MEMPOOL_CALLBACK" , -}; -EAL_REGISTER_TAILQ (callback_tailq) +static struct mempool_callback_tailq callback_tailq = +TAILQ_HEAD_INITIALIZER (callback_tailq);/* Invoke all registered mempool event callbacks. */ static void
@@ -1372,6 +1370,7 @@ void rte_mempool_walk(void (*func)(struct rte_mempool *, void *), }struct mempool_callback_data { +TAILQ_ENTRY (mempool_callback_data) callbacks;rte_mempool_event_callback *func;void *user_data; };
@@ -1380,14 +1379,11 @@ static void mempool_event_callback_invoke(enum rte_mempool_event event, struct rte_mempool *mp) { - struct mempool_callback_list *list; -struct rte_tailq_entry *te; + struct mempool_callback_data *cb;void *tmp_te;rte_mcfg_tailq_read_lock (); - list =RTE_TAILQ_CAST (callback_tailq.head, mempool_callback_list); - RTE_TAILQ_FOREACH_SAFE(te, list, next, tmp_te) { -struct mempool_callback_data *cb = te->data; + RTE_TAILQ_FOREACH_SAFE(cb, &callback_tailq, callbacks, tmp_te) {rte_mcfg_tailq_read_unlock (); cb->func (event, mp, cb->user_data);rte_mcfg_tailq_read_lock ();
@@ -1399,10 +1395,7 @@ intrte_mempool_event_callback_register (rte_mempool_event_callback *func, void *user_data) { -struct mempool_callback_list *list; -struct rte_tailq_entry *te =NULL ;struct mempool_callback_data *cb; -void *tmp_te;int ret;if (func ==NULL ) {
@@ -1411,36 +1404,23 @@ rte_mempool_event_callback_register(rte_mempool_event_callback *func, }rte_mcfg_tailq_write_lock (); - list =RTE_TAILQ_CAST (callback_tailq.head, mempool_callback_list); - RTE_TAILQ_FOREACH_SAFE(te, list, next, tmp_te) { - cb = te->data; + TAILQ_FOREACH(cb, &callback_tailq, callbacks) {if (cb->func == func && cb->user_data == user_data) { ret = -EEXIST ; goto exit; } } - te =rte_zmalloc ("mempool_cb_tail_entry" ,sizeof (*te),0 ); -if (te ==NULL ) { -RTE_LOG (ERR ,MEMPOOL , -"Cannot allocate event callback tailq entry!\n" ); - ret = -ENOMEM ; - goto exit; - } - - cb =rte_malloc ("mempool_cb_data" ,sizeof (*cb),0 ); + cb =calloc (1 ,sizeof (*cb));if (cb ==NULL ) { -RTE_LOG (ERR ,MEMPOOL , -"Cannot allocate event callback!\n" ); -rte_free (te); +RTE_LOG (ERR ,MEMPOOL ,"Cannot allocate event callback!\n" ); ret = -ENOMEM ; goto exit; } cb->func = func; cb->user_data = user_data; - te->data = cb; -TAILQ_INSERT_TAIL (list, te, next); +TAILQ_INSERT_TAIL (&callback_tailq, cb, callbacks); ret =0 ; exit:
@@ -1453,27 +1433,21 @@ intrte_mempool_event_callback_unregister (rte_mempool_event_callback *func, void *user_data) { -struct mempool_callback_list *list; -struct rte_tailq_entry *te =NULL ;struct mempool_callback_data *cb;int ret = -ENOENT ;rte_mcfg_tailq_write_lock (); - list =RTE_TAILQ_CAST (callback_tailq.head, mempool_callback_list); - TAILQ_FOREACH(te, list, next) { - cb = te->data; + TAILQ_FOREACH(cb, &callback_tailq, callbacks) {if (cb->func == func && cb->user_data == user_data) { -TAILQ_REMOVE (list, te, next); +TAILQ_REMOVE (&callback_tailq, cb, callbacks); ret =0 ;break ; } }rte_mcfg_tailq_write_unlock (); -if (ret ==0 ) { -rte_free (te); -rte_free (cb); - } +if (ret ==0 ) +free (cb); rte_errno = -ret;return ret; }
lib/mempool/rte_mempool.h +2 -0
@@ -1847,6 +1847,8 @@ typedef void (rte_mempool_event_callback)(* Register a callback function invoked on mempool life cycle event. * The function will be invoked in the process * that performs an action which triggers the callback. +* Registration is process-private, +* i.e. each process must manage callbacks on its own if needed. * * @param func * Callback function.
Signature · 1 line
2.25.1
RE: [PATCH 1/2] mempool: make event callbacks process-private
Quoted 15 lines
-----Original Message-----
From: Dmitry Kozlyuk <[email protected]>
Sent: Monday, August 8, 2022 12:43
To: [email protected]
Cc: Olivier Matz <[email protected]>; Andrew Rybchenko <[email protected]>; [email protected]
Subject: [PATCH 1/2] mempool: make event callbacks process-privateCallbacks for mempool events were registered in a process-shared tailq. This was inherently incorrect because the same function may be loaded to a different address in each process.
Make the tailq process-private.
Use the EAL tailq lock to reduce the number of different locks this module operates.
Quoted 4 lines
Fixes da2b9cb25e5f ("mempool: add event callbacks")Signed-off-by Dmitry Kozlyuk <[email protected]>
Andrew Rybchenko
Reviewed-byRe: [PATCH 1/2] mempool: make event callbacks process-private
Quoted 15 lines · Slava Ovsiienko
Quoted 15 lines
-----Original Message-----
From: Dmitry Kozlyuk <[email protected]>
Sent: Monday, August 8, 2022 12:43
To: [email protected]
Cc: Olivier Matz <[email protected]>; Andrew Rybchenko <[email protected]>; [email protected]
Subject: [PATCH 1/2] mempool: make event callbacks process-privateCallbacks for mempool events were registered in a process-shared tailq. This was inherently incorrect because the same function may be loaded to a different address in each process.
Make the tailq process-private.
Use the EAL tailq lock to reduce the number of different locks this module operates.
Quoted 5 lines
Quoted 4 lines
Fixes da2b9cb25e5f ("mempool: add event callbacks")Signed-off-by Dmitry Kozlyuk <[email protected]>
RE: [PATCH 1/2] mempool: make event callbacks process-private
In reply to Dmitry KozlyukQuoted 14 lines
From: Dmitry Kozlyuk <[email protected]>
Sent: Monday, August 8, 2022 12:43 PM
To: [email protected]
Cc: Olivier Matz <[email protected]>; Andrew Rybchenko <[email protected]>; [email protected]
Subject: [PATCH 1/2] mempool: make event callbacks process-privateCallbacks for mempool events were registered in a process-shared tailq. This was inherently incorrect because the same function
may be loaded to a different address in each process.
Make the tailq process-private.
Use the EAL tailq lock to reduce the number of different locks this module operates.
Quoted 4 lines
Fixes da2b9cb25e5f ("mempool: add event callbacks")Signed-off-by Dmitry Kozlyuk <[email protected]>
Kind reminder to the mempool maintainers.
PATCH 2/2 common/mlx5: fix multi-process mempool registration
In reply to Dmitry KozlyukThe mp_cb_registered flag shared between all processes was used to ensure that for any IB device (MLX5 common device) mempool event callback was registered only once
and mempools that had been existing before the device start were traversed only once to register them.
Since mempool callback registrations have become process-private, callback registration must be done by every process.
The flag can no longer reflect the state for any single process. Replace it with a registration counter to track
when no more callbacks are registered for the device in any process. It is sufficient to only register pre-existing mempools in the primary process because it is the one that starts the device.
--- drivers/common/mlx5/mlx5_common.c | 15 +++++++++------ drivers/common/mlx5/mlx5_common_mr.c | 2 +- drivers/common/mlx5/mlx5_common_mr.h | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-)
drivers/common/mlx5/mlx5_common.c +9 -6
@@ -583,18 +583,17 @@ mlx5_dev_mempool_subscribe(struct mlx5_common_device *cdev)if (!cdev->config.mr_mempool_reg_en)return 0 ;rte_rwlock_write_lock (&cdev->mr_scache.mprwlock); -if (cdev->mr_scache.mp_cb_registered) - goto exit;/* Callback for this device may be already registered. */ ret =rte_mempool_event_callback_register (mlx5_dev_mempool_event_cb, cdev);if (ret !=0 && rte_errno !=EEXIST ) goto exit; +__atomic_add_fetch (&cdev->mr_scache.mempool_cb_reg_n,1 , + __ATOMIC_ACQUIRE);/* Register mempools only once for this device. */ -if (ret ==0 ) +if (rte_eal_process_type () ==RTE_PROC_PRIMARY )rte_mempool_walk (mlx5_dev_mempool_register_cb, cdev); ret =0 ; - cdev->mr_scache.mp_cb_registered =1 ; exit:rte_rwlock_write_unlock (&cdev->mr_scache.mprwlock);return ret;
@@ -603,10 +602,14 @@ mlx5_dev_mempool_subscribe(struct mlx5_common_device *cdev)static void mlx5_dev_mempool_unsubscribe (struct mlx5_common_device *cdev) { +uint32_t mempool_cb_reg_n;int ret; -if (!cdev->mr_scache.mp_cb_registered || - !cdev->config.mr_mempool_reg_en) +if (!cdev->config.mr_mempool_reg_en) +return ; + mempool_cb_reg_n =__atomic_sub_fetch (&cdev->mr_scache.mempool_cb_reg_n, +1 , __ATOMIC_RELEASE); +if (mempool_cb_reg_n >0 )return ;/* Stop watching for mempool events and unregister all mempools. */ ret = rte_mempool_event_callback_unregister(mlx5_dev_mempool_event_cb,
drivers/common/mlx5/mlx5_common_mr.c +1 -1
@@ -1138,7 +1138,7 @@ mlx5_mr_create_cache(struct mlx5_mr_share_cache *share_cache, int socket) &share_cache->dereg_mr_cb);rte_rwlock_init (&share_cache->rwlock);rte_rwlock_init (&share_cache->mprwlock); - share_cache->mp_cb_registered =0 ; + share_cache->mempool_cb_reg_n =0 ;/* Initialize B-tree and allocate memory for global MR cache table. */ return mlx5_mr_btree_init (&share_cache->cache,MLX5_MR_BTREE_CACHE_N *2 , socket);
drivers/common/mlx5/mlx5_common_mr.h +1 -1
@@ -81,7 +81,7 @@ struct mlx5_mr_share_cache {uint32_t dev_gen;/* Generation number to flush local caches. */ rte_rwlock_t rwlock;/* MR cache Lock. */ rte_rwlock_t mprwlock;/* Mempool Registration Lock. */ -uint8_t mp_cb_registered;/* Mempool are Registered. */ +uint32_t mempool_cb_reg_n;/* Mempool event callabck registrants. */ struct mlx5_mr_btree cache;/* Global MR cache table. */ struct mlx5_mr_list mr_list;/* Registered MR list. */ struct mlx5_mr_list mr_free_list;/* Freed MR list. */
Signature · 1 line
2.25.1
Quoted 20 lines
-----Original Message-----
From: Dmitry Kozlyuk <[email protected]>
Sent: Monday, August 8, 2022 12:43
To: [email protected]
Cc: Matan Azrad <[email protected]>; Slava Ovsiienko <[email protected]>; [email protected]
Subject: [PATCH 2/2] common/mlx5: fix multi-process mempool registrationThe
mp_cb_registeredflag shared between all processes was used to ensure that for any IB device (MLX5 common device) mempool event callback was registered only once and mempools that had been existing before the device start were traversed only once to register them.
Since mempool callback registrations have become process-private, callback registration must be done by every process.
The flag can no longer reflect the state for any single process. Replace it with a registration counter to track when no more callbacks are registered for the device in any process.
It is sufficient to only register pre-existing mempools in the primary process because it is the one that starts the device.
Quoted 4 lines
Fixes 690b2a88c2f7 ("common/mlx5: add mempool registration facilities")Signed-off-by Dmitry Kozlyuk <[email protected]>
Thomas Monjalon
Applied08/08/2022 11:42, Dmitry Kozlyuk:
Quoted 3 lines
Dmitry Kozlyuk (2):
mempool: make event callbacks process-private common/mlx5: fix multi-process mempool registration
Applied, thanks.