From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 87C70A00C3;
	Tue, 21 Jun 2022 23:28:25 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 1EF5F4069C;
	Tue, 21 Jun 2022 23:28:25 +0200 (CEST)
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
 by mails.dpdk.org (Postfix) with ESMTP id D761D40151
 for <dev@dpdk.org>; Tue, 21 Jun 2022 23:28:23 +0200 (CEST)
Received: by linux.microsoft.com (Postfix, from userid 1086)
 id 2296A20C6345; Tue, 21 Jun 2022 14:28:23 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2296A20C6345
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
 s=default; t=1655846903;
 bh=LdzGOxQUHrmz//tgUtOE3ue0YIkZxvmkxoFXji9frzk=;
 h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
 b=FFT0JOJk8z33l0aEsXw8PIVVWufPWGYot3mCyEEzsFjfcr63Bh0iuIPACX6q5K+4b
 Eo3PJlFIJHoLbbmwCnW+svkM7r5XY77POLmDB8hmCf9aRlNUH6Vs3C1Fl/Aww4sI43
 hV7Uw8Xu8/k4fEMKqlMY9LlaC7Xlj6ae2MV1u++o=
Date: Tue, 21 Jun 2022 14:28:23 -0700
From: Tyler Retzlaff <roretzla@linux.microsoft.com>
To: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
Cc: dev@dpdk.org, thomas@monjalon.net, anatoly.burakov@intel.com,
 Narcisa Vasile <navasile@microsoft.com>
Subject: Re: [PATCH v2 2/6] eal: add thread lifetime management
Message-ID: <20220621212823.GF18214@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net>
References: <1654783134-13303-1-git-send-email-roretzla@linux.microsoft.com>
 <1655250438-18044-1-git-send-email-roretzla@linux.microsoft.com>
 <1655250438-18044-3-git-send-email-roretzla@linux.microsoft.com>
 <20220618155908.70e822af@sovereign>
 <20220621185103.GC18214@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net>
 <20220621224421.244a772f@sovereign>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20220621224421.244a772f@sovereign>
User-Agent: Mutt/1.5.21 (2010-09-15)
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

On Tue, Jun 21, 2022 at 10:44:21PM +0300, Dmitry Kozlyuk wrote:
> 2022-06-21 11:51 (UTC-0700), Tyler Retzlaff:
> > > > +int
> > > > +rte_thread_join(rte_thread_t thread_id, unsigned long *value_ptr)
> > > > +{
> > > > +	int ret = 0;
> > > > +	void *res = NULL;
> > > > +	void **pres = NULL;
> > > > +
> > > > +	if (value_ptr != NULL)
> > > > +		pres = &res;
> > > > +
> > > > +	ret = pthread_join((pthread_t)thread_id.opaque_id, pres);
> > > > +	if (ret != 0) {
> > > > +		RTE_LOG(DEBUG, EAL, "pthread_join failed\n");
> > > > +		return ret;
> > > > +	}
> > > > +
> > > > +	if (value_ptr != NULL && *pres != NULL)
> > > > +		*value_ptr = *(unsigned long *)(*pres);
> > > > +
> > > > +	return 0;
> > > > +}  
> > > 
> > > What makes *pres == NULL special?  
> > 
> > it's not clear what you mean, can you explain? maybe there is some
> > context i am missing from the original patch series?
> 
> There's no previous context.
> After ptread_join(), *pres holds the return value of the thread routine.
> You only assign *value_ptr if value_ptr is not NULL (obviously correct)
> and if *pres != NULL, that is, if the thread returned a non-NULL value.
> But this value is opaque, why do you filter NULL?

i don't think it is opaque here? unsigned long * value_ptr says we have
to store an integer. which leads to a discussion of what should get
stored at the value_ptr location if pthread_join() itself returns no
result but the caller of rte_thread_join() requests the result.

> Perhaps you meant if (pres != NULL), no dereference?

that i think is just a repeat of a test checking if the caller of
rte_thread_join is interested in the result?
i.e. value_ptr != NULL -> pres != NULL

both pres and *pres are dereferenced so it seems to track that prior to
those dereferences they have to be validated as being non-NULL.
i don't see how we could avoid dereferencing **pres to satisfy the
calling contract when the result is requested.

now if value_ptr was unsigned long ** i guess i'd understand. i could
always be reading the code wrong. but thinking about further there is
another problem with this in that we really don't know what is being
aliased in *pres when using the pthread implementation, since pthread
could be returning a pointer to something narrow or with unknown layout
where later dereferencing it as something wider or in this case
specifically as unsigned long * would have horrible consequences.

i think this ends up semi-related to your other comment about what the
result type from rte_thread_func is, we can discuss offline and post
details back to the list.

> 
> > > > +int
> > > > +rte_thread_create(rte_thread_t *thread_id,
> > > > +		  const rte_thread_attr_t *thread_attr,
> > > > +		  rte_thread_func thread_func, void *args)
> > > > +{
> > > > +	int ret = 0;
> > > > +	DWORD tid;
> > > > +	HANDLE thread_handle = NULL;
> > > > +	GROUP_AFFINITY thread_affinity;
> > > > +	struct thread_routine_ctx *ctx = NULL;
> > > > +
> > > > +	ctx = calloc(1, sizeof(*ctx));
> > > > +	if (ctx == NULL) {
> > > > +		RTE_LOG(DEBUG, EAL, "Insufficient memory for thread context allocations\n");
> > > > +		ret = ENOMEM;
> > > > +		goto cleanup;
> > > > +	}
> > > > +	ctx->routine_args = args;
> > > > +	ctx->thread_func = thread_func;
> > > > +
> > > > +	thread_handle = CreateThread(NULL, 0, thread_func_wrapper, ctx,
> > > > +		CREATE_SUSPENDED, &tid);
> > > > +	if (thread_handle == NULL) {
> > > > +		ret = thread_log_last_error("CreateThread()");
> > > > +		free(ctx);
> > > > +		goto cleanup;  
> > > 
> > > Missing `free(ctx)` from other error paths below.  
> > 
> > beyond this point free(ctx) will happen in thread_func_wrapper. i will
> > add a comment to make it clear.
> 
> Not if you exit before ResumeThread()
> and thread_func_wrapper() will never execute to call free().

yes, you are right i forgot that this thread is created suspended.