[PATCH v5 2/6] eal: add thread lifetime management
Tyler Retzlaff
roretzla at linux.microsoft.com
Wed Mar 1 21:34:04 CET 2023
On Wed, Mar 01, 2023 at 09:11:43AM +0100, David Marchand wrote:
> Hello Tyler,
>
> On Wed, Oct 5, 2022 at 7:07 PM Tyler Retzlaff
> <roretzla at linux.microsoft.com> wrote:
> > diff --git a/lib/eal/unix/rte_thread.c b/lib/eal/unix/rte_thread.c
> > index 9126595..d4c1a7f 100644
> > --- a/lib/eal/unix/rte_thread.c
> > +++ b/lib/eal/unix/rte_thread.c
> > @@ -16,6 +16,11 @@ struct eal_tls_key {
> > pthread_key_t thread_index;
> > };
> >
> > +struct thread_routine_ctx {
> > + rte_thread_func thread_func;
> > + void *routine_args;
> > +};
> > +
> > static int
> > thread_map_priority_to_os_value(enum rte_thread_priority eal_pri, int *os_pri,
> > int *pol)
> > @@ -75,6 +80,136 @@ struct eal_tls_key {
> > return 0;
> > }
> >
> > +static void *
> > +thread_func_wrapper(void *arg)
> > +{
> > + struct thread_routine_ctx ctx = *(struct thread_routine_ctx *)arg;
> > +
> > + free(arg);
> > +
> > + return (void *)(uintptr_t)ctx.thread_func(ctx.routine_args);
> > +}
> > +
> > +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;
> > + pthread_attr_t attr;
> > + pthread_attr_t *attrp = NULL;
> > + struct thread_routine_ctx *ctx;
> > + struct sched_param param = {
> > + .sched_priority = 0,
> > + };
> > + int policy = SCHED_OTHER;
> > +
> > + 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;
> > +
> > + if (thread_attr != NULL) {
> > + ret = pthread_attr_init(&attr);
> > + if (ret != 0) {
> > + RTE_LOG(DEBUG, EAL, "pthread_attr_init failed\n");
> > + goto cleanup;
> > + }
> > +
> > + attrp = &attr;
> > +
> > + /*
> > + * Set the inherit scheduler parameter to explicit,
> > + * otherwise the priority attribute is ignored.
> > + */
> > + ret = pthread_attr_setinheritsched(attrp,
> > + PTHREAD_EXPLICIT_SCHED);
> > + if (ret != 0) {
> > + RTE_LOG(DEBUG, EAL, "pthread_attr_setinheritsched failed\n");
> > + goto cleanup;
> > + }
> > +
> > +
> > + if (thread_attr->priority ==
> > + RTE_THREAD_PRIORITY_REALTIME_CRITICAL) {
> > + ret = ENOTSUP;
> > + goto cleanup;
> > + }
> > + ret = thread_map_priority_to_os_value(thread_attr->priority,
> > + ¶m.sched_priority, &policy);
> > + if (ret != 0)
> > + goto cleanup;
> > +
> > + ret = pthread_attr_setschedpolicy(attrp, policy);
> > + if (ret != 0) {
> > + RTE_LOG(DEBUG, EAL, "pthread_attr_setschedpolicy failed\n");
> > + goto cleanup;
> > + }
> > +
> > + ret = pthread_attr_setschedparam(attrp, ¶m);
> > + if (ret != 0) {
> > + RTE_LOG(DEBUG, EAL, "pthread_attr_setschedparam failed\n");
> > + goto cleanup;
> > + }
> > + }
> > +
> > + ret = pthread_create((pthread_t *)&thread_id->opaque_id, attrp,
> > + thread_func_wrapper, ctx);
> > + if (ret != 0) {
> > + RTE_LOG(DEBUG, EAL, "pthread_create failed\n");
> > + 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);
> > + if (attrp != NULL)
> > + pthread_attr_destroy(&attr);
> > +
> > + return ret;
> > +}
>
> I am looking back at potential races in our code while reviewing the
> ctrl thread creation fix, and I stopped at this patch.
Thank you for doing this, this series I actually did not start I only
took over so the review is welcome.
>
> What happens if rte_thread_set_affinity_by_id() fails?
> A new thread got started, but I don't see it being terminated in the
> cleanup phase.
>
>
> In this same situation, we may have a small race for a double free on
> ctx since thread_func_wrapper() may have already been called from the
> new thread.
I will review this and get back with fixes if necessary.
>
>
> I have the same concerns with the windows implementation.
I will check it too.
>
>
> --
> David Marchand
More information about the dev
mailing list