From nobody Tue Apr 7 16:29:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71FEBECAAA6 for ; Fri, 26 Aug 2022 16:47:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344835AbiHZQrM (ORCPT ); Fri, 26 Aug 2022 12:47:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344740AbiHZQqX (ORCPT ); Fri, 26 Aug 2022 12:46:23 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D93803340D for ; Fri, 26 Aug 2022 09:45:28 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-33dc390f26cso33356427b3.9 for ; Fri, 26 Aug 2022 09:45:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=qVZGUrjNaf+uaIklV22hg50RBgn+7V45ToWO7LHu6z0=; b=KHJFy5APhi23hCB5yhBJbIt9QnW9DPxdVtwh9gL5fbHIYRHK3POGYS46+q2HkyOStC 76K6TkDIM4sJYciYCZapgV+g78ZVpk+J4MwS4SdpMuSUyUj1+4fLMR5afhfyFuWXTpmU ZDI89zMH1X54UbxfpgRHoJ8NKFjReW0C17bEAFMiD2YuZo9/MlfTIgj6OoCs0J/6Qs9v Pvk6mvDm/iNVPGZ6P+HTAHuE75ABfGTMz4NSGJZyWLpb+bOxLLiomAK2i4UP5IPDIORg 2nD82jqta63p0XiFw5/8qAyBlH+7+B9IyCPQLmSFDJElrJtkcY+fpxPVGMM09tEFQx7F DJ1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=qVZGUrjNaf+uaIklV22hg50RBgn+7V45ToWO7LHu6z0=; b=PK6Qftd1dDj4uI6o1OSfe0RaOzR4Bb75lGm9Vxz889AtL4hXo/2unRNAy93lAMqn02 UFRjRfmRHzleY0av+nHRgU9AnJnSi3dQzgH/+bIzJTPAAvYi2vm+Ut+EfyJ8WEMkjxJ9 Hi9OLbTpY7dWIEApae6f7zacVmFxbDk4aroWRLMrfuj7WUIEdo1FPOH4r8tDTe7FEE8n i7RkqXdHa9U+7Srznr8NcMlZPl7ZsfrgkEgbHpjEYGH3T7VkJ40RTDtdPpwjZvc7oSOu cHxHvey0wksYAGIg/F45nXlSbMu49+f+DljvLx5zzJycq4Ws1AfKKeYxyAYyoGx4V22B 4YSQ== X-Gm-Message-State: ACgBeo1tJ2RLeMZZxd74NIcGgS4w6PL0cOQ+FPdFDcoDi9PMvZNBGizx E/BVYArUYjzaGNJ3BNrQCxpzCt2eQv5v X-Google-Smtp-Source: AA6agR6J0Hznz0vPOdzwS6MMpPQI4D+kQEceU90qKk0iVhgT86mCXFGZ9ZRvq5vU8WGX7k2j0+P8PXMbYM+2 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:ccb1:c46b:7044:2508]) (user=irogers job=sendgmr) by 2002:a25:b901:0:b0:682:66a2:f714 with SMTP id x1-20020a25b901000000b0068266a2f714mr428844ybj.577.1661532327823; Fri, 26 Aug 2022 09:45:27 -0700 (PDT) Date: Fri, 26 Aug 2022 09:42:40 -0700 In-Reply-To: <20220826164242.43412-1-irogers@google.com> Message-Id: <20220826164242.43412-17-irogers@google.com> Mime-Version: 1.0 References: <20220826164242.43412-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Subject: [PATCH v4 16/18] perf sched: Fixes for thread safety analysis From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , Nathan Chancellor , Nick Desaulniers , Tom Rix , Weiguo Li , Athira Rajeev , Thomas Richter , Ravi Bangoria , Dario Petrillo , Hewenliang , yaowenbin , Wenyu Liu , Song Liu , Andrii Nakryiko , Dave Marchevsky , Leo Yan , Kim Phillips , Pavithra Gurushankar , Alexandre Truong , Quentin Monnet , William Cohen , Andres Freund , Adrian Hunter , "=?UTF-8?q?Martin=20Li=C5=A1ka?=" , Colin Ian King , James Clark , Fangrui Song , Stephane Eranian , Kajol Jain , Alexey Bayduraev , Riccardo Mancini , Andi Kleen , Masami Hiramatsu , Zechuan Chen , Jason Wang , Christophe JAILLET , Remi Bernon , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add annotations to describe lock behavior. Add unlocks so that mutexes aren't conditionally held on exit from perf_sched__replay. Add an exit variable so that thread_func can terminate, rather than leaving the threads blocked on mutexes. Signed-off-by: Ian Rogers --- tools/perf/builtin-sched.c | 46 ++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 7e4006d6b8bc..b483ff0d432e 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -246,6 +246,7 @@ struct perf_sched { const char *time_str; struct perf_time_interval ptime; struct perf_time_interval hist_time; + volatile bool thread_funcs_exit; }; =20 /* per thread run time data */ @@ -633,31 +634,34 @@ static void *thread_func(void *ctx) prctl(PR_SET_NAME, comm2); if (fd < 0) return NULL; -again: - ret =3D sem_post(&this_task->ready_for_work); - BUG_ON(ret); - mutex_lock(&sched->start_work_mutex); - mutex_unlock(&sched->start_work_mutex); =20 - cpu_usage_0 =3D get_cpu_usage_nsec_self(fd); + while (!sched->thread_funcs_exit) { + ret =3D sem_post(&this_task->ready_for_work); + BUG_ON(ret); + mutex_lock(&sched->start_work_mutex); + mutex_unlock(&sched->start_work_mutex); =20 - for (i =3D 0; i < this_task->nr_events; i++) { - this_task->curr_event =3D i; - perf_sched__process_event(sched, this_task->atoms[i]); - } + cpu_usage_0 =3D get_cpu_usage_nsec_self(fd); =20 - cpu_usage_1 =3D get_cpu_usage_nsec_self(fd); - this_task->cpu_usage =3D cpu_usage_1 - cpu_usage_0; - ret =3D sem_post(&this_task->work_done_sem); - BUG_ON(ret); + for (i =3D 0; i < this_task->nr_events; i++) { + this_task->curr_event =3D i; + perf_sched__process_event(sched, this_task->atoms[i]); + } =20 - mutex_lock(&sched->work_done_wait_mutex); - mutex_unlock(&sched->work_done_wait_mutex); + cpu_usage_1 =3D get_cpu_usage_nsec_self(fd); + this_task->cpu_usage =3D cpu_usage_1 - cpu_usage_0; + ret =3D sem_post(&this_task->work_done_sem); + BUG_ON(ret); =20 - goto again; + mutex_lock(&sched->work_done_wait_mutex); + mutex_unlock(&sched->work_done_wait_mutex); + } + return NULL; } =20 static void create_tasks(struct perf_sched *sched) + EXCLUSIVE_LOCK_FUNCTION(sched->start_work_mutex) + EXCLUSIVE_LOCK_FUNCTION(sched->work_done_wait_mutex) { struct task_desc *task; pthread_attr_t attr; @@ -687,6 +691,8 @@ static void create_tasks(struct perf_sched *sched) } =20 static void wait_for_tasks(struct perf_sched *sched) + EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) + EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) { u64 cpu_usage_0, cpu_usage_1; struct task_desc *task; @@ -738,6 +744,8 @@ static void wait_for_tasks(struct perf_sched *sched) } =20 static void run_one_test(struct perf_sched *sched) + EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) + EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) { u64 T0, T1, delta, avg_delta, fluct; =20 @@ -3309,11 +3317,15 @@ static int perf_sched__replay(struct perf_sched *sc= hed) print_task_traces(sched); add_cross_task_wakeups(sched); =20 + sched->thread_funcs_exit =3D false; create_tasks(sched); printf("------------------------------------------------------------\n"); for (i =3D 0; i < sched->replay_repeat; i++) run_one_test(sched); =20 + sched->thread_funcs_exit =3D true; + mutex_unlock(&sched->start_work_mutex); + mutex_unlock(&sched->work_done_wait_mutex); return 0; } =20 --=20 2.37.2.672.g94769d06f0-goog