Skip to content

Commit 668958f

Browse files
shemmingertmonjalo
authored andcommitted
eal: fix data race in multi-process support
If DPDK is built with thread sanitizer it reports a race in setting of multiprocess file descriptor. The fix is to use atomic operations when updating mp_fd. Build: $ meson -Db_sanitize=address build $ ninja -C build Simple example: $ .build/app/dpdk-testpmd -l 1-3 --no-huge EAL: Detected CPU lcores: 16 EAL: Detected NUMA nodes: 1 EAL: Static memory layout is selected, amount of reserved memory can be adjusted with -m or --socket-mem EAL: Detected static linkage of DPDK EAL: Multi-process socket /run/user/1000/dpdk/rte/mp_socket EAL: Selected IOVA mode 'VA' testpmd: No probed ethernet devices testpmd: create a new mbuf pool <mb_pool_0>: n=163456, size=2176, socket=0 testpmd: preferred mempool ops selected: ring_mp_mc EAL: Error - exiting with code: 1 Cause: Creation of mbuf pool for socket 0 failed: Cannot allocate memory ================== WARNING: ThreadSanitizer: data race (pid=87245) Write of size 4 at 0x558e04d8ff70 by main thread: #0 rte_mp_channel_cleanup <null> (dpdk-testpmd+0x1e7d30c) #1 rte_eal_cleanup <null> (dpdk-testpmd+0x1e85929) #2 rte_exit <null> (dpdk-testpmd+0x1e5bc0a) #3 mbuf_pool_create.cold <null> (dpdk-testpmd+0x274011) #4 main <null> (dpdk-testpmd+0x5cc15d) Previous read of size 4 at 0x558e04d8ff70 by thread T2: #0 mp_handle <null> (dpdk-testpmd+0x1e7c439) #1 ctrl_thread_init <null> (dpdk-testpmd+0x1e6ee1e) As if synchronized via sleep: #0 nanosleep libsanitizer/tsan/tsan_interceptors_posix.cpp:366 #1 get_tsc_freq <null> (dpdk-testpmd+0x1e92ff9) #2 set_tsc_freq <null> (dpdk-testpmd+0x1e6f2fc) #3 rte_eal_timer_init <null> (dpdk-testpmd+0x1e931a4) #4 rte_eal_init.cold <null> (dpdk-testpmd+0x29e578) DPDK#5 main <null> (dpdk-testpmd+0x5cbc45) Location is global 'mp_fd' of size 4 at 0x558e04d8ff70 (dpdk-testpmd+0x000003122f70) Thread T2 'rte_mp_handle' (tid=87248, running) created by main thread at: #0 pthread_create libsanitizer/tsan/tsan_interceptors_posix.cpp:969 #1 rte_ctrl_thread_create <null> (dpdk-testpmd+0x1e6efd0) #2 rte_mp_channel_init.cold <null> (dpdk-testpmd+0x29cb7c) #3 rte_eal_init <null> (dpdk-testpmd+0x1e8662e) #4 main <null> (dpdk-testpmd+0x5cbc45) SUMMARY: ThreadSanitizer: data race (app/dpdk-testpmd+0x1e7d30c) in rte_mp_channel_cleanup ================== ThreadSanitizer: reported 1 warnings Fixes: bacaa27 ("eal: add channel for multi-process communication") Cc: [email protected] Signed-off-by: Stephen Hemminger <[email protected]> Acked-by: Anatoly Burakov <[email protected]> Reviewed-by: Chengwen Feng <[email protected]>
1 parent db354bd commit 668958f

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

lib/eal/common/eal_common_proc.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ rte_mp_action_unregister(const char *name)
260260
}
261261

262262
static int
263-
read_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
263+
read_msg(int fd, struct mp_msg_internal *m, struct sockaddr_un *s)
264264
{
265265
int msglen;
266266
struct iovec iov;
@@ -281,7 +281,7 @@ read_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
281281
msgh.msg_controllen = sizeof(control);
282282

283283
retry:
284-
msglen = recvmsg(mp_fd, &msgh, 0);
284+
msglen = recvmsg(fd, &msgh, 0);
285285

286286
/* zero length message means socket was closed */
287287
if (msglen == 0)
@@ -390,11 +390,12 @@ mp_handle(void *arg __rte_unused)
390390
{
391391
struct mp_msg_internal msg;
392392
struct sockaddr_un sa;
393+
int fd;
393394

394-
while (mp_fd >= 0) {
395+
while ((fd = __atomic_load_n(&mp_fd, __ATOMIC_RELAXED)) >= 0) {
395396
int ret;
396397

397-
ret = read_msg(&msg, &sa);
398+
ret = read_msg(fd, &msg, &sa);
398399
if (ret <= 0)
399400
break;
400401

@@ -638,9 +639,8 @@ rte_mp_channel_init(void)
638639
NULL, mp_handle, NULL) < 0) {
639640
RTE_LOG(ERR, EAL, "failed to create mp thread: %s\n",
640641
strerror(errno));
641-
close(mp_fd);
642642
close(dir_fd);
643-
mp_fd = -1;
643+
close(__atomic_exchange_n(&mp_fd, -1, __ATOMIC_RELAXED));
644644
return -1;
645645
}
646646

@@ -656,11 +656,10 @@ rte_mp_channel_cleanup(void)
656656
{
657657
int fd;
658658

659-
if (mp_fd < 0)
659+
fd = __atomic_exchange_n(&mp_fd, -1, __ATOMIC_RELAXED);
660+
if (fd < 0)
660661
return;
661662

662-
fd = mp_fd;
663-
mp_fd = -1;
664663
pthread_cancel(mp_handle_tid);
665664
pthread_join(mp_handle_tid, NULL);
666665
close_socket_fd(fd);

0 commit comments

Comments
 (0)