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 DB11641DB6; Thu, 2 Mar 2023 19:44:51 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 15491427F5; Thu, 2 Mar 2023 19:44:47 +0100 (CET) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id AC8E040E09; Thu, 2 Mar 2023 19:44:44 +0100 (CET) Received: by linux.microsoft.com (Postfix, from userid 1086) id EB4C020B9C3D; Thu, 2 Mar 2023 10:44:43 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com EB4C020B9C3D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1677782683; bh=xuZK/PT4vBYQyfE7YEavGvdJXFXwl7fUSKj7sgWKYWQ=; h=From:To:Cc:Subject:Date:From; b=JeLp7H0C9auqn8YS5CafRuWEIFHtVU+QwdQC3hmcKJKGD1EAAHFxseY5gMecKierO 9Vznx6nF+tYgoLjH0MxH6NZlil6ROi9JNfjrZPddtEytTimolUfteIoUurs7tFubIR GufApkhKKyOLX0VVokvkN6T3hd/J3lqcBBvU2yys= From: Tyler Retzlaff To: dev@dpdk.org Cc: david.marchand@redhat.com, Tyler Retzlaff , stable@dpdk.org Subject: [PATCH 1/2] eal: fix failure race and behavior of thread create Date: Thu, 2 Mar 2023 10:44:41 -0800 Message-Id: <1677782682-27200-1-git-send-email-roretzla@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org In rte_thread_create setting affinity after pthread_create may fail. Such a failure should result in the entire rte_thread_create failing but doesn't. Additionally if there is a failure to set affinity a race exists where the creating thread will free ctx and depending on scheduling of the new thread it may also free ctx (double free). Resolve both of the above issues by using the pthread_setaffinity_np prior to thread creation to set the affinity of the created thread. By doing this no failure paths exist after pthread_create returns successfully. Fixes: ce6e911d20f6 ("eal: add thread lifetime API") Cc: stable@dpdk.org Cc: roretzla@linux.microsoft.com Signed-off-by: Tyler Retzlaff --- lib/eal/unix/rte_thread.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/eal/unix/rte_thread.c b/lib/eal/unix/rte_thread.c index 37ebfcf..5bf633b 100644 --- a/lib/eal/unix/rte_thread.c +++ b/lib/eal/unix/rte_thread.c @@ -155,6 +155,17 @@ struct thread_routine_ctx { RTE_LOG(DEBUG, EAL, "pthread_attr_setschedparam failed\n"); goto cleanup; } + + if (CPU_COUNT(&thread_attr->cpuset) > 0) { + ret = pthread_attr_setaffinity_np(attrp, + sizeof(thread_attr->cpuset), + &thread_attr->cpuset); + if (ret != 0) { + RTE_LOG(DEBUG, EAL, + "pthread_attr_setaffinity_np failed\n"); + goto cleanup; + } + } } ret = pthread_create((pthread_t *)&thread_id->opaque_id, attrp, @@ -164,15 +175,6 @@ struct thread_routine_ctx { goto cleanup; } - if (thread_attr != NULL && CPU_COUNT(&thread_attr->cpuset) > 0) { - ret = rte_thread_set_affinity_by_id(*thread_id, - &thread_attr->cpuset); - if (ret != 0) { - RTE_LOG(DEBUG, EAL, "rte_thread_set_affinity_by_id failed\n"); - goto cleanup; - } - } - ctx = NULL; cleanup: free(ctx); -- 1.8.3.1