Re: [PATCH v3 16/18] perf sched: Fixes for thread safety analysis

From: Namhyung Kim
Date: Fri Aug 26 2022 - 16:48:27 EST


On Fri, Aug 26, 2022 at 12:36 PM Adrian Hunter <adrian.hunter@xxxxxxxxx> wrote:
>
> On 26/08/22 21:26, Namhyung Kim wrote:
> > On Fri, Aug 26, 2022 at 10:48 AM Ian Rogers <irogers@xxxxxxxxxx> wrote:

> >> In the previous code the threads were blocked on mutexes that were
> >> stack allocated and the stack memory went away. You are correct to say
> >> that to those locks I added an init and destroy call. The lifetime of
> >> the mutex was wrong previously and remains wrong in this change.
> >
> > I think you fixed the lifetime issue with sched->thread_funcs_exit here.
> > All you need to do is calling pthread_join() after the mutex_unlock, no?
>
> Like this maybe:

Yeah, but at this point we might want to factor it out as a function like
destroy_tasks().

Thanks,
Namhyung

>
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index b483ff0d432e..8090c1218855 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -3326,6 +3326,13 @@ static int perf_sched__replay(struct perf_sched *sched)
> sched->thread_funcs_exit = true;
> mutex_unlock(&sched->start_work_mutex);
> mutex_unlock(&sched->work_done_wait_mutex);
> + /* Get rid of threads so they won't be upset by mutex destruction */
> + for (i = 0; i < sched->nr_tasks; i++) {
> + int err = pthread_join(sched->tasks[i]->thread, NULL);
> +
> + if (err)
> + pr_err("pthread_join() failed for task nr %lu, error %d\n", i, err);
> + }
> return 0;
> }