Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp642565rwe; Fri, 26 Aug 2022 11:22:48 -0700 (PDT) X-Google-Smtp-Source: AA6agR5SCMqtey/xMTtpK7ID/X8ls1sZZ/icHpKENGWqLDF7WoW1/1wOl87slKd0fFxQaYROTpZZ X-Received: by 2002:aa7:dcc8:0:b0:447:e8b3:b4f3 with SMTP id w8-20020aa7dcc8000000b00447e8b3b4f3mr3307485edu.374.1661538167844; Fri, 26 Aug 2022 11:22:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661538167; cv=none; d=google.com; s=arc-20160816; b=pa3wFbGEYK2HkRLMyQ/Kew6xip5FwuFhGGR6le7sQKx8sCT4U35fShbnSHvlmA6kkZ XkSOy2giA/PK1k8MWA2jkyxJt3NLUSoLFm0fA5rUx/gIaPA4gTq1IxsRSGVi228IKRq3 u6EWnGZb+8hE6dTdplNfl5bjuj1mNUoF2rY8WJ+zaSWUVnsS0bsMbmfcCF8FC8If4LjY nc+cTjLvxNZOfFVguvxMROfKDK1OrMbDO8ro+t1lJ8aQ7nIj+CI/UdbC/lm+kDvLAOXJ E2MXH/p/ZC6oX9KCC4JFN0S9oiiN45u63DumPHyEJxLEkZTJvgd9A2vISym8t59qDJdZ +uZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=9JHPKBEhnzj2Xmii0IJwAZm9bDxPzFxlEKG6x6ug6zk=; b=qcL0yzpGm1nDCYewl487bo7F/ZZ143nYCidKTKrmG2njEPyyo/D6e1zHVOoPy8Jy8J YYWhWWbSaaz6MCr1ZZrAgSf4QjHlBRtRy8tXcLw7Yai8Y3tcEM0uTC4V1Xx96EMb52re gkj74EdjENEfR83bfc8n1TtJJhywzS0v/nbyK1hvF5QK2z/iMYdS1tl+6oOcl4RQQeWT +Jhc/H/PxCQegEruv5KAZk6tXeXPjvqMKhJuoK9sY6AHTcd1e9HqqnGkTQQGxtlqSAha vHNYHiKDJut1j5f+7Q5g7RNK+fsXLIcbUbiWzvz1bFvANCz+dK7LPvzfqj4+vmbxN7cC e+0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YeShUHZA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e25-20020a1709067e1900b0073d6d78e986si1468321ejr.470.2022.08.26.11.22.22; Fri, 26 Aug 2022 11:22:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YeShUHZA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344613AbiHZRsR (ORCPT + 99 others); Fri, 26 Aug 2022 13:48:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242827AbiHZRsQ (ORCPT ); Fri, 26 Aug 2022 13:48:16 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CA49DF4F8 for ; Fri, 26 Aug 2022 10:48:14 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id bs25so2647163wrb.2 for ; Fri, 26 Aug 2022 10:48:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=9JHPKBEhnzj2Xmii0IJwAZm9bDxPzFxlEKG6x6ug6zk=; b=YeShUHZAjaMfVm4ZJoT0KtWHtHVQF69k9j5LM9cpMqgaMBQIQk8RISAZ3c9Tqdk1K3 +u9YAnO+b7e0rlD9Hwu0FZ8cxVnXQkNbxbgHITNxqiu6DnykqjCEQzRAu1VWZ3pWMwXO qV8dmBSMKtVzj95smb3JbC4Qxz6oW0AAHo4U4mvjUaCX+F/oNdn28VcnJqXUvoYxsHeL LDVP5o6aCWrz15+TGMnlnF5B9lUezo3kaYrDNBfi+zhipDC3vY6zC/AYRaBblBKFZwCA JCYLynPHBYuVw2A7/hRiyX8i8tRylbPycdSMmfMib/nALqD33gt64nTQzJDHFay+hhCo fdEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=9JHPKBEhnzj2Xmii0IJwAZm9bDxPzFxlEKG6x6ug6zk=; b=svEUEZKh/0vVVD+kTN5fFU8dSCqupU9i5JS8JrpIyvFlMU4/virgFk9S+g3GH1ku3e JaM44lmYisximoRNLSP9gnOEDWE28m9aRwU27EIigRFGuQcFgwhzLsDPU1byO42u539v 4DSfqxbQJnUmyZizHC8VeI4s/ERfswv5pmxvD8oeXFB5tUV0pqGQP12ZroJUIENMzrwf FoamxbrFOSefmsSWJp9wvdlYJbsIRkJkXaK6z129SFs3Cm8O+uHUN0oL0/kQbelQSoLz G9zvRGfchNnDHLfik3nOWMh76q3ZkY3cwyVSAVstNeF4plMx2vIPTWbYHu2ohr+7MiXK DvmA== X-Gm-Message-State: ACgBeo0HppbteQu1y6GJqBoxfX2v9vHMrjYE4lXvFrH5hy13Kl2/p5fk FF4oMmeCWQ3kcnf3MLIrT4FhEB8pJQrSEhbo0zeBmA== X-Received: by 2002:adf:e28d:0:b0:21e:4c3b:b446 with SMTP id v13-20020adfe28d000000b0021e4c3bb446mr431200wri.300.1661536092846; Fri, 26 Aug 2022 10:48:12 -0700 (PDT) MIME-Version: 1.0 References: <20220824153901.488576-1-irogers@google.com> <20220824153901.488576-17-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Fri, 26 Aug 2022 10:48:00 -0700 Message-ID: Subject: Re: [PATCH v3 16/18] perf sched: Fixes for thread safety analysis To: Adrian Hunter Cc: 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_Almeida?= , 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 , =?UTF-8?Q?Martin_Li=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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 26, 2022 at 10:41 AM Adrian Hunter wrote: > > On 26/08/22 19:06, Ian Rogers wrote: > > On Fri, Aug 26, 2022 at 5:12 AM Adrian Hunter wrote: > >> > >> On 24/08/22 18:38, Ian Rogers wrote: > >>> 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; > >>> }; > >>> > >>> /* 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 = sem_post(&this_task->ready_for_work); > >>> - BUG_ON(ret); > >>> - mutex_lock(&sched->start_work_mutex); > >>> - mutex_unlock(&sched->start_work_mutex); > >>> > >>> - cpu_usage_0 = get_cpu_usage_nsec_self(fd); > >>> + while (!sched->thread_funcs_exit) { > >>> + ret = sem_post(&this_task->ready_for_work); > >>> + BUG_ON(ret); > >>> + mutex_lock(&sched->start_work_mutex); > >>> + mutex_unlock(&sched->start_work_mutex); > >>> > >>> - for (i = 0; i < this_task->nr_events; i++) { > >>> - this_task->curr_event = i; > >>> - perf_sched__process_event(sched, this_task->atoms[i]); > >>> - } > >>> + cpu_usage_0 = get_cpu_usage_nsec_self(fd); > >>> > >>> - cpu_usage_1 = get_cpu_usage_nsec_self(fd); > >>> - this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; > >>> - ret = sem_post(&this_task->work_done_sem); > >>> - BUG_ON(ret); > >>> + for (i = 0; i < this_task->nr_events; i++) { > >>> + this_task->curr_event = i; > >>> + perf_sched__process_event(sched, this_task->atoms[i]); > >>> + } > >>> > >>> - mutex_lock(&sched->work_done_wait_mutex); > >>> - mutex_unlock(&sched->work_done_wait_mutex); > >>> + cpu_usage_1 = get_cpu_usage_nsec_self(fd); > >>> + this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; > >>> + ret = sem_post(&this_task->work_done_sem); > >>> + BUG_ON(ret); > >>> > >>> - goto again; > >>> + mutex_lock(&sched->work_done_wait_mutex); > >>> + mutex_unlock(&sched->work_done_wait_mutex); > >>> + } > >>> + return NULL; > >>> } > >>> > >>> 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) > >>> } > >>> > >>> 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) > >>> } > >>> > >>> 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; > >>> > >>> @@ -3309,11 +3317,15 @@ static int perf_sched__replay(struct perf_sched *sched) > >>> print_task_traces(sched); > >>> add_cross_task_wakeups(sched); > >>> > >>> + sched->thread_funcs_exit = false; > >>> create_tasks(sched); > >>> printf("------------------------------------------------------------\n"); > >>> for (i = 0; i < sched->replay_repeat; i++) > >>> run_one_test(sched); > >>> > >>> + sched->thread_funcs_exit = true; > >>> + mutex_unlock(&sched->start_work_mutex); > >>> + mutex_unlock(&sched->work_done_wait_mutex); > >> > >> I think you still need to wait for the threads to exit before > >> destroying the mutexes. > > > > This is a pre-existing issue and beyond the scope of this patch set. > > You added the mutex_destroy functions in patch 8, so it is still > fallout from that. 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. Thanks, Ian > > > > Thanks, > > Ian > > > >>> return 0; > >>> } > >>> > >> >