Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp661332rwe; Fri, 26 Aug 2022 11:48:17 -0700 (PDT) X-Google-Smtp-Source: AA6agR4NLhDZ36aPlVtUOf47EhT2OBk9HthL8x0wy43AnvjSLvgl12VK+SavCshdc6NRCESaCUfw X-Received: by 2002:a17:906:eecb:b0:73c:5c85:142b with SMTP id wu11-20020a170906eecb00b0073c5c85142bmr6269828ejb.433.1661539697084; Fri, 26 Aug 2022 11:48:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661539697; cv=none; d=google.com; s=arc-20160816; b=dD9Ag3o48Pai64UjEtyaFE4B1bmvRf+C500tilBiszlgs6rxPUQ1Jx9iIxZbD/fCuJ W2nnv57o/76/rFiK9iH75BXdEXrtC8jVTkFlqP2mhQ+kDjsH1zKQhwbhA1oyVvb3PZ91 hV3NgG2P4ZLNUoCEJz5OIe1z/aHEIxEniRaD1gDAZe/NxGMkXLP0R55jjmCp6W2+YaQe ygmhPTFbUdTWCQiBneam2Q6gRNkb6fpI1PvCG9j0CX3i8gSSUY+5zTTUV0wgKMGtHOao E8VsEoHaE6pzL0YPMJyu5MUxhGM1ND2x88S12xXPxY7lqBBO5rV21syrHuLU1PNjviE1 aiWg== 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; bh=Nn+QtQucyfx6hcyPZtuaj6djQCVOs1Ze3pe5mHGUDSg=; b=m74Yq3nvZEoPEQzTNDg7sLmLdbauqKZtjRT8+fc0d48o0HaGOO+TUPfdYpXT58j9VP U7i1pDIrgGHSqU58HU6ES9gnVXZ4UeRwRLie/JZvPvuR9lftT6sL2JpOtU8VPIRA3jcK /fQapn8sN2IVwjzMf/wvww6odchgCCy5tqH+stkeo5tN/u8aIJid7cnNbSSGicFXypkH YEFOmLXAQhjkq5Pq0WMguCSWYiLqreIVcDTQFAC0vB4uVPWRhnh68tkKEOM5MOP7UrJb F31NvVK6YG4VYNX2DR9CQRb7VhYcT7eNxv69/O8emxvJ8AvQ5MO9LYWeGpinQAp+5pt7 l6WA== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hc30-20020a170907169e00b00741362a9695si1131236ejc.13.2022.08.26.11.47.49; Fri, 26 Aug 2022 11:48:17 -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; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239758AbiHZS1B (ORCPT + 99 others); Fri, 26 Aug 2022 14:27:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229676AbiHZS0z (ORCPT ); Fri, 26 Aug 2022 14:26:55 -0400 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9508ED2B35; Fri, 26 Aug 2022 11:26:54 -0700 (PDT) Received: by mail-ot1-f47.google.com with SMTP id 6-20020a9d0106000000b0063963134d04so1551205otu.3; Fri, 26 Aug 2022 11:26:54 -0700 (PDT) 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=Nn+QtQucyfx6hcyPZtuaj6djQCVOs1Ze3pe5mHGUDSg=; b=bFgjdebfTpIvwzclDqBA2KmKVAIRLPKkJ/LVr/EV1JSXcFZAxdNWwYEIkE31uEu44g uHC6WjnCRo6i6OQPexTu1F8JtEaf34Bt5gbfPZagxuNv94AXsewo6CLt8rjZPxX/JoYW ruUHhrPZ2gLHVB3zui1qaNWovmEE7gxY3tKQXIRR5hlqsutKm4UMvdX/cC0zx0VohRqF aZshjRXpNuVRSKOgXsfFA7+XQudvGgJSDFAI2kkGLZHXLqDdbPonydQrNSiwsS4Gd7Yz uWCo2f9PmyBEeeLldtFV/mCvJ9tLyM/99rNLrGa88Bm1EFpRAh+NenE2D1p+SZ4yEYOF q8tw== X-Gm-Message-State: ACgBeo1A1ff6putp3PSfnjFSYQgGZD6KV6fPo+n6rMRnHyI5MJB9ziKe pIjjh3j+FT3Nou0Voaufoi8MQleVo2tR0nrNF58= X-Received: by 2002:a9d:6f18:0:b0:638:b4aa:a546 with SMTP id n24-20020a9d6f18000000b00638b4aaa546mr1811694otq.124.1661538413803; Fri, 26 Aug 2022 11:26:53 -0700 (PDT) MIME-Version: 1.0 References: <20220824153901.488576-1-irogers@google.com> <20220824153901.488576-17-irogers@google.com> In-Reply-To: From: Namhyung Kim Date: Fri, 26 Aug 2022 11:26:42 -0700 Message-ID: Subject: Re: [PATCH v3 16/18] perf sched: Fixes for thread safety analysis To: Ian Rogers Cc: Adrian Hunter , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , 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 , linux-perf-users , bpf , llvm@lists.linux.dev Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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:48 AM Ian Rogers wrote: > > 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. 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? Thanks, Namhyung