From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id B86FBA0350; Thu, 25 Jun 2020 02:38:56 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 79CB85F69; Thu, 25 Jun 2020 02:38:55 +0200 (CEST) Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by dpdk.org (Postfix) with ESMTP id D2C4D5F2F for ; Thu, 25 Jun 2020 02:38:53 +0200 (CEST) IronPort-SDR: 6V3F6HjtO87aCIw8/NK9/nNtwT98u/OBU86WyeCkBOn8FAhhq7yPoZPJLwmTrrGpKKucvl6CW4 wS+/ytElv7zQ== X-IronPort-AV: E=McAfee;i="6000,8403,9662"; a="146184088" X-IronPort-AV: E=Sophos;i="5.75,277,1589266800"; d="scan'208";a="146184088" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jun 2020 17:38:52 -0700 IronPort-SDR: H9IRsNFQvr+oShHW+bDYZ5XTqgwqGqP7WhSRl/CQMrXhkFDUkWCZbNo7LzflDR242S9OQsfsyJ Bxmx871W4kYA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,277,1589266800"; d="scan'208";a="294278501" Received: from orsmsx101.amr.corp.intel.com ([10.22.225.128]) by orsmga007.jf.intel.com with ESMTP; 24 Jun 2020 17:38:52 -0700 Received: from [10.166.30.253] (10.166.30.253) by ORSMSX101.amr.corp.intel.com (10.22.225.128) with Microsoft SMTP Server (TLS) id 14.3.439.0; Wed, 24 Jun 2020 17:38:52 -0700 To: Tasnim Bashar , CC: , , , , , , , , References: <20200602020052.4540-1-tbashar@mellanox.com> <20200624231041.5304-1-tbashar@mellanox.com> From: Ranjit Menon Message-ID: <9bdf1ba4-01ee-63ff-34ba-5c46a571c33f@intel.com> Date: Wed, 24 Jun 2020 17:38:50 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.8.1 MIME-Version: 1.0 In-Reply-To: <20200624231041.5304-1-tbashar@mellanox.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Originating-IP: [10.166.30.253] Subject: Re: [dpdk-dev] [PATCH v4] eal/windows: fix thread handle X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Hi, Tasnim... On 6/24/2020 4:10 PM, Tasnim Bashar wrote: > Casting thread ID to handle is not accurate way to get thread handle. > Need to use OpenThread function to get thread handle from thread ID. > > pthread_setaffinity_np and pthread_getaffinity_np functions > for Windows are affected because of it. > > Signed-off-by: Tasnim Bashar > --- > v3: WA to remove warning(-Wmaybe-uninitialized) > v4: Directly used function name instead of #define > --- > lib/librte_eal/windows/include/pthread.h | 84 +++++++++++++++----- > lib/librte_eal/windows/include/rte_windows.h | 1 + > 2 files changed, 67 insertions(+), 18 deletions(-) > > diff --git a/lib/librte_eal/windows/include/pthread.h b/lib/librte_eal/windows/include/pthread.h > index e2274cf4e9..2553c0890a 100644 > --- a/lib/librte_eal/windows/include/pthread.h > +++ b/lib/librte_eal/windows/include/pthread.h > @@ -6,6 +6,7 @@ > #define _PTHREAD_H_ > > #include > +#include > > /** > * This file is required to support the common code in eal_common_proc.c, > @@ -16,8 +17,8 @@ > extern "C" { > #endif > > -#include > #include > +#include > > #define PTHREAD_BARRIER_SERIAL_THREAD TRUE > > @@ -40,37 +41,84 @@ typedef SYNCHRONIZATION_BARRIER pthread_barrier_t; > /* pthread function overrides */ > #define pthread_self() \ > ((pthread_t)GetCurrentThreadId()) > -#define pthread_setaffinity_np(thread, size, cpuset) \ > - eal_set_thread_affinity_mask(thread, (unsigned long *) cpuset) > -#define pthread_getaffinity_np(thread, size, cpuset) \ > - eal_get_thread_affinity_mask(thread, (unsigned long *) cpuset) > -#define pthread_create(threadid, threadattr, threadfunc, args) \ > - eal_create_thread(threadid, threadattr, threadfunc, args) > > static inline int > -eal_set_thread_affinity_mask(pthread_t threadid, unsigned long *cpuset) > +pthread_setaffinity_np(pthread_t threadid, size_t cpuset_size, > + rte_cpuset_t *cpuset) > { > - SetThreadAffinityMask((HANDLE) threadid, *cpuset); > - return 0; > + DWORD_PTR ret; > + HANDLE thread_handle; > + > + if (cpuset == NULL || cpuset_size == 0) > + return -1; > + > + thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadid); > + if (thread_handle == NULL) { > + RTE_LOG_WIN32_ERR("OpenThread()"); > + return -1; > + } > + > + ret = SetThreadAffinityMask(thread_handle, *cpuset->_bits); > + if (ret == 0) { > + RTE_LOG_WIN32_ERR("SetThreadAffinityMask()"); > + goto close_handle; > + } > + > +close_handle: > + if (CloseHandle(thread_handle) == 0) { > + RTE_LOG_WIN32_ERR("CloseHandle()"); > + return -1; > + } > + return (ret == 0) ? -1 : 0; > } > > static inline int > -eal_get_thread_affinity_mask(pthread_t threadid, unsigned long *cpuset) > +pthread_getaffinity_np(pthread_t threadid, size_t cpuset_size, > + rte_cpuset_t *cpuset) > { > /* Workaround for the lack of a GetThreadAffinityMask() > *API in Windows > */ > - /* obtain previous mask by setting dummy mask */ > - DWORD dwprevaffinitymask = > - SetThreadAffinityMask((HANDLE) threadid, 0x1); > + DWORD_PTR prev_affinity_mask; > + HANDLE thread_handle; > + DWORD_PTR ret; Initialize ret to 0 here, otherwise... (see below) > + > + if (cpuset == NULL || cpuset_size == 0) > + return -1; > + > + thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadid); > + if (thread_handle == NULL) { > + RTE_LOG_WIN32_ERR("OpenThread()"); > + return -1; > + } > + > + /* obtain previous mask by setting dummy mask */ > + prev_affinity_mask = SetThreadAffinityMask(thread_handle, 0x1); > + if (prev_affinity_mask == 0) { > + RTE_LOG_WIN32_ERR("SetThreadAffinityMask()"); > + goto close_handle; ...after this jump, ret is uninitialized and will produce random results at the check at the end of the function. > + } > + > /* set it back! */ > - SetThreadAffinityMask((HANDLE) threadid, dwprevaffinitymask); > - *cpuset = dwprevaffinitymask; > - return 0; > + ret = SetThreadAffinityMask(thread_handle, prev_affinity_mask); > + if (ret == 0) { > + RTE_LOG_WIN32_ERR("SetThreadAffinityMask()"); > + goto close_handle; > + } > + > + memset(cpuset, 0, cpuset_size); > + *cpuset->_bits = prev_affinity_mask; > + > +close_handle: > + if (CloseHandle(thread_handle) == 0) { > + RTE_LOG_WIN32_ERR("SetThreadAffinityMask()"); > + return -1; > + } > + return (ret == 0) ? -1 : 0; > } > > static inline int > -eal_create_thread(void *threadid, const void *threadattr, void *threadfunc, > +pthread_create(void *threadid, const void *threadattr, void *threadfunc, > void *args) > { > RTE_SET_USED(threadattr); > diff --git a/lib/librte_eal/windows/include/rte_windows.h b/lib/librte_eal/windows/include/rte_windows.h > index 899ed7d874..d013b50241 100644 > --- a/lib/librte_eal/windows/include/rte_windows.h > +++ b/lib/librte_eal/windows/include/rte_windows.h > @@ -31,6 +31,7 @@ > #define INITGUID > #endif > #include > +#include > > /** > * Log GetLastError() with context, usually a Win32 API function and arguments. ranjit m.