From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id D4DDBA0579 for ; Tue, 6 Apr 2021 18:17:50 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CB371141258; Tue, 6 Apr 2021 18:17:50 +0200 (CEST) Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) by mails.dpdk.org (Postfix) with ESMTP id 969C814124E; Tue, 6 Apr 2021 18:17:48 +0200 (CEST) Received: by mail-qk1-f181.google.com with SMTP id q3so15453243qkq.12; Tue, 06 Apr 2021 09:17:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uEhBOxK3PZBQH4NDfEu+/iRyJs7huXicnSw85Gdlxd0=; b=oy6Do68f38JnoMDiE9NWTtU8V+2giHaxVzeueU9MFDdKqcUlCzSQGUmioBCji/secl 3Rm6jPCl9B5huZAJe5GW3Grrwbth5dfxz332+XGHfCHfOvuOrgf4hg8fyn7eMq7Il7kL DCgUNmP5EwKfVw1L331OcYuSkfDBgOtuVhy1b1n3aHFl2ZgEQHYWFx6ovVYEcsVfsDv9 8JHcsFtX1zk0f/l+06UfW7qcK29SYNYrtVaiDBkV5Fy4qf/xcx3PS7gqVPgrF5/Dw7O/ uUUocPFHx9UZ84TpOBGKKDI1CBwWln19Vn2bZN0djXGxpyjAG5ES4evIvmUUQA1b0i2C hFXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uEhBOxK3PZBQH4NDfEu+/iRyJs7huXicnSw85Gdlxd0=; b=qwKTNbc4e1DiZkjXCNJipVsxfEsk2Fu5mXsALVStBu/6i5KrC3wthvzh/jeoOpqzcY MDNmzerMwTsWQx1P4y6uR+RB5gv+ZT6g9yCa90ylhM/4qCBAEhyxnZ/cQBSHbdzabnn4 /48sLCf2EghlBDSf3G4KXUtiyLTgF2fbB5j3gPELPOrFtEXdhmso/Kdj5lK63tp6EzSc 3gOI6k+Pkkoyxp/FnYRvdO742jv06l+Prym1xdI6oLWoLgQyaY7SfEkFqpFctGB3jONZ 0p7q8XxFuauJd5NdX+i5wz8JMgzez+xQ5L5ltB7HuTfFtrhUlee7AKVR/JoR65J8277H KGUA== X-Gm-Message-State: AOAM533UoUfHFeIyOhjcmt9nHmv+ecp5m0LJtVdYqxuOpQuUOfuCnIC+ N1X2TzvhEeAlYAJsog6s+vU= X-Google-Smtp-Source: ABdhPJyGwgxM2p5BMG217UzBpHMBiL9UKpcn4RqgiT8ZU+/u/QNbRvkWJbV1uqJDz7XdRV+hfSMPKQ== X-Received: by 2002:a37:c441:: with SMTP id h1mr30544528qkm.123.1617725868015; Tue, 06 Apr 2021 09:17:48 -0700 (PDT) Received: from localhost.localdomain (bras-base-hullpq2034w-grc-18-74-15-213-92.dsl.bell.ca. [74.15.213.92]) by smtp.gmail.com with ESMTPSA id h9sm14147037qtk.6.2021.04.06.09.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Apr 2021 09:17:47 -0700 (PDT) From: Luc Pelletier To: olivier.matz@6wind.com, jianfeng.tan@intel.com, Honnappa.Nagarahalli@arm.com Cc: dev@dpdk.org, Luc Pelletier , stable@dpdk.org Date: Tue, 6 Apr 2021 12:15:35 -0400 Message-Id: <20210406161534.103272-1-lucp.at.work@gmail.com> In-Reply-To: <20210324130422.92357-1-lucp.at.work@gmail.com> References: <20210324130422.92357-1-lucp.at.work@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-stable] [PATCH v3] eal: fix race in ctrl thread creation X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org Sender: "stable" The creation of control threads used a pthread barrier for synchronization. This patch fixes a race condition where the pthread barrier could get destroyed while one of the threads has not yet returned from the pthread_barrier_wait function, which could result in undefined behaviour. The barrier has been completely removed in favour of a reference count on the control thread parameters struct. Fixes: 3a0d465d4c53 ("eal: fix use-after-free on control thread creation") Cc: jianfeng.tan@intel.com Cc: stable@dpdk.org Signed-off-by: Luc Pelletier --- Hi Olivier, Hi Honnappa, Thanks for your input Honnappa. I've made the changes to completely remove the barrier. However, I didn't move the call to pthread_setaffinity_np to the control thread; I think we want to report the result of that function to the caller of rte_ctrl_thread_create and doing so from ctrl_thread_init would be a lot trickier. Olivier, what do you think of these changes? lib/librte_eal/common/eal_common_thread.c | 35 ++++++++--------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/lib/librte_eal/common/eal_common_thread.c b/lib/librte_eal/common/eal_common_thread.c index 73a055902..2421066f9 100644 --- a/lib/librte_eal/common/eal_common_thread.c +++ b/lib/librte_eal/common/eal_common_thread.c @@ -169,12 +169,11 @@ __rte_thread_uninit(void) struct rte_thread_ctrl_params { void *(*start_routine)(void *); void *arg; - pthread_barrier_t configured; + unsigned int refcnt; }; static void *ctrl_thread_init(void *arg) { - int ret; struct internal_config *internal_conf = eal_get_internal_configuration(); rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset; @@ -184,11 +183,8 @@ static void *ctrl_thread_init(void *arg) __rte_thread_init(rte_lcore_id(), cpuset); - ret = pthread_barrier_wait(¶ms->configured); - if (ret == PTHREAD_BARRIER_SERIAL_THREAD) { - pthread_barrier_destroy(¶ms->configured); + if (__atomic_sub_fetch(¶ms->refcnt, 1, __ATOMIC_ACQ_REL) == 0) free(params); - } return start_routine(routine_arg); } @@ -210,14 +206,11 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name, params->start_routine = start_routine; params->arg = arg; - - pthread_barrier_init(¶ms->configured, NULL, 2); + params->refcnt = 2; ret = pthread_create(thread, attr, ctrl_thread_init, (void *)params); - if (ret != 0) { - free(params); - return -ret; - } + if (ret != 0) + goto fail; if (name != NULL) { ret = rte_thread_setname(*thread, name); @@ -228,24 +221,20 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name, ret = pthread_setaffinity_np(*thread, sizeof(*cpuset), cpuset); if (ret) - goto fail; + goto fail_cancel; - ret = pthread_barrier_wait(¶ms->configured); - if (ret == PTHREAD_BARRIER_SERIAL_THREAD) { - pthread_barrier_destroy(¶ms->configured); + if (__atomic_sub_fetch(¶ms->refcnt, 1, __ATOMIC_ACQ_REL) == 0) free(params); - } return 0; -fail: - if (PTHREAD_BARRIER_SERIAL_THREAD == - pthread_barrier_wait(¶ms->configured)) { - pthread_barrier_destroy(¶ms->configured); - free(params); - } +fail_cancel: pthread_cancel(*thread); pthread_join(*thread, NULL); + +fail: + free(params); + return -ret; } -- 2.25.1