From mboxrd@z Thu Jan 1 00:00:00 1970 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 60230ECAAA6 for ; Fri, 26 Aug 2022 17:41:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344891AbiHZRly (ORCPT ); Fri, 26 Aug 2022 13:41:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344726AbiHZRlt (ORCPT ); Fri, 26 Aug 2022 13:41:49 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD11475385; Fri, 26 Aug 2022 10:41:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661535706; x=1693071706; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=y+5UHSCKqDo8+5mfChXR80HnmMPwWmD45j5P+hWg1wk=; b=V91dgmRq0VFwA5+fbwzwuTpKbf04M7deayiSZ2ySYHNx0MRBaKLz/Vsc Yrr8MllZsb5in6qYm83nLQStQjlmn/z7iwoNApFY2eAwflqc1k9ysjMGv eD0cjIzMEjZCt3oJReNwwO8K2LkOUADu3D+QXd4vfH0e8xBBq2iYgbjma ToM2L7VU2ml/H4D5HbHI0QA5u7Ek+ofuvbPwoKfgr6McQN+oKIQoOtb1M X4XZR9ORjXyMNQVaJZL8cAhfQ4EzetBSL/Uwj2IADSK7PK8yGb1Rb2lRR th6nI5UQ747OguGudPnnC+/yLHG7ubwMGgFJ1PpnudjwXwz/PTxWw8JPm Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10451"; a="295833336" X-IronPort-AV: E=Sophos;i="5.93,265,1654585200"; d="scan'208";a="295833336" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2022 10:41:31 -0700 X-IronPort-AV: E=Sophos;i="5.93,265,1654585200"; d="scan'208";a="640142949" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO [10.0.2.15]) ([10.252.50.209]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2022 10:41:19 -0700 Message-ID: Date: Fri, 26 Aug 2022 20:41:12 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0 Thunderbird/91.11.0 Subject: Re: [PATCH v3 16/18] perf sched: Fixes for thread safety analysis Content-Language: en-US To: Ian Rogers 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 References: <20220824153901.488576-1-irogers@google.com> <20220824153901.488576-17-irogers@google.com> From: Adrian Hunter Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-perf-users@vger.kernel.org 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. > > Thanks, > Ian > >>> return 0; >>> } >>> >>