Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1525479ybl; Tue, 3 Dec 2019 08:28:45 -0800 (PST) X-Google-Smtp-Source: APXvYqzT8oYA0kYF+VYvljNkoEUa8eCCU/1Amd9zWSjKKmrgD/rBTX4nPze6DZOhTzvsjQWNFVJT X-Received: by 2002:a05:6808:356:: with SMTP id j22mr4390212oie.130.1575390525635; Tue, 03 Dec 2019 08:28:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575390525; cv=none; d=google.com; s=arc-20160816; b=r1JllxhEKWIBqm6O9o1r8f4HUUGvtdshjcQnBbU2xFW+oFx+qlkAEgl+BEdPiFCf/H eRKW9UqBFHaTUYoKNHxT3eAP1Pac9H/aiP1oxeZNRj0yIoKwXKX5tWo0xpbo7R7oWUem JUXPudcdm00taj6tQjutM4cUoLtNA3RiLe0i5YwpICjIXkG2Y2sSDZRNqEw3/U6KPKCo IYE0murBvy8nzMtpswAA+bBMNY3W+LokE2Xd1Wdsl5oAv+/Ua/gsVc7vuyEozZrrKe6f zkkjAg6lY2d0on0MgbLpDk1TDGOLmyzR2EiEemdGQL7fULsKcc9k7yDq7gt2lkSuUxJw u4Jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=lqObdLCOyG5b7rNpo5rr0Mvl7bO6lP5iGdeOJv12XeI=; b=y4FMh3g5x4KIdKuRvac7D07HYEzj5lX/8NxoiYGFevcWjw2f4roBuJT3Ozv5x0xV19 lr8Nq5ps++KXBfeDmnepHvlx0mAvbpojjLsCu4mpoiRNfFgVC0epR1Ftk6LlEAzOVvdS uaPaSFzT+GTYUNa7dzTa1b5HK7UKnfF6J/ZOgHgeD6VocbE9w+edLFeXdf8aFRIqTLSY Hpzz5urVC/lJrxeR4W+gUxcs6d72LrEO5un7jrDX8TCMuqJBYA/5dA3JldTjHl3OdLlN UBNy0+YozFfe7u2UYuYSC+8F4MBOJVY6JQTTA+M06zH+NirIrm01tx2YNCReylB2B23M 1zmA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w7si1561875oie.196.2019.12.03.08.28.33; Tue, 03 Dec 2019 08:28:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727313AbfLCQ1d (ORCPT + 99 others); Tue, 3 Dec 2019 11:27:33 -0500 Received: from mail.kernel.org ([198.145.29.99]:60150 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726968AbfLCQ1d (ORCPT ); Tue, 3 Dec 2019 11:27:33 -0500 Received: from gandalf.local.home (cpe-66-24-58-225.stny.res.rr.com [66.24.58.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 166BE2068E; Tue, 3 Dec 2019 16:27:32 +0000 (UTC) Date: Tue, 3 Dec 2019 11:27:30 -0500 From: Steven Rostedt To: Prateek Sood Cc: mingo@redhat.com, linux-kernel@vger.kernel.org, kaushalk@codeaurora.org Subject: Re: [PATCH] trace: circular dependency for trace_types_lock and event_mutex Message-ID: <20191203112730.77b334f6@gandalf.local.home> In-Reply-To: <0101016ecc80a1fa-27cc3e87-c16d-4cd6-b3c6-9c893010fdef-000000@us-west-2.amazonses.com> References: <0101016ecc80a1fa-27cc3e87-c16d-4cd6-b3c6-9c893010fdef-000000@us-west-2.amazonses.com> X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Prateek, Thanks for the patch. Note, I think a better subject is: tracing: Fix lock inversion caused by trace_event_enable_tgid_record() On Tue, 3 Dec 2019 16:03:32 +0000 Prateek Sood wrote: > Task T2 Task T3 > trace_options_core_write() subsystem_open() > > mutex_lock(trace_types_lock) mutex_lock(event_mutex) > > set_tracer_flag() > > trace_event_enable_tgid_record() mutex_lock(trace_types_lock) > > mutex_lock(event_mutex) > > This gives a circular dependency deadlock between trace_types_lock and > event_mutex. To fix this invert the usage of trace_types_lock and event_mutex > in trace_options_core_write(). This keeps the sequence of lock usage consistent. > > Change-Id: I3752a77c59555852c2241f7775ec4a1960c15c15 What's this Change-Id? Something internal? I'll be adding: Link: http://lkml.kernel.org/r/0101016ecc80a1fa-27cc3e87-c16d-4cd6-b3c6-9c893010fdef-000000@us-west-2.amazonses.com I'll test this out, and probably even add a stable tag. -- Steve > Signed-off-by: Prateek Sood > --- > kernel/trace/trace.c | 6 ++++++ > kernel/trace/trace_events.c | 8 ++++---- > kernel/trace/trace_irqsoff.c | 4 ++++ > kernel/trace/trace_sched_wakeup.c | 4 ++++ > 4 files changed, 18 insertions(+), 4 deletions(-) > > diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c > index 6a0ee91..2c09aa1 100644 > --- a/kernel/trace/trace.c > +++ b/kernel/trace/trace.c > @@ -4590,6 +4590,8 @@ int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set) > > int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) > { > + lockdep_assert_held(&event_mutex); > + > /* do nothing if flag is already set */ > if (!!(tr->trace_flags & mask) == !!enabled) > return 0; > @@ -4657,6 +4659,7 @@ static int trace_set_options(struct trace_array *tr, char *option) > > cmp += len; > > + mutex_lock(&event_mutex); > mutex_lock(&trace_types_lock); > > ret = match_string(trace_options, -1, cmp); > @@ -4667,6 +4670,7 @@ static int trace_set_options(struct trace_array *tr, char *option) > ret = set_tracer_flag(tr, 1 << ret, !neg); > > mutex_unlock(&trace_types_lock); > + mutex_unlock(&event_mutex); > > /* > * If the first trailing whitespace is replaced with '\0' by strstrip, > @@ -7972,9 +7976,11 @@ static void get_tr_index(void *data, struct trace_array **ptr, > if (val != 0 && val != 1) > return -EINVAL; > > + mutex_lock(&event_mutex); > mutex_lock(&trace_types_lock); > ret = set_tracer_flag(tr, 1 << index, val); > mutex_unlock(&trace_types_lock); > + mutex_unlock(&event_mutex); > > if (ret < 0) > return ret; > diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c > index fba87d1..995061b 100644 > --- a/kernel/trace/trace_events.c > +++ b/kernel/trace/trace_events.c > @@ -320,7 +320,8 @@ void trace_event_enable_cmd_record(bool enable) > struct trace_event_file *file; > struct trace_array *tr; > > - mutex_lock(&event_mutex); > + lockdep_assert_held(&event_mutex); > + > do_for_each_event_file(tr, file) { > > if (!(file->flags & EVENT_FILE_FL_ENABLED)) > @@ -334,7 +335,6 @@ void trace_event_enable_cmd_record(bool enable) > clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); > } > } while_for_each_event_file(); > - mutex_unlock(&event_mutex); > } > > void trace_event_enable_tgid_record(bool enable) > @@ -342,7 +342,8 @@ void trace_event_enable_tgid_record(bool enable) > struct trace_event_file *file; > struct trace_array *tr; > > - mutex_lock(&event_mutex); > + lockdep_assert_held(&event_mutex); > + > do_for_each_event_file(tr, file) { > if (!(file->flags & EVENT_FILE_FL_ENABLED)) > continue; > @@ -356,7 +357,6 @@ void trace_event_enable_tgid_record(bool enable) > &file->flags); > } > } while_for_each_event_file(); > - mutex_unlock(&event_mutex); > } > > static int __ftrace_event_enable_disable(struct trace_event_file *file, > diff --git a/kernel/trace/trace_irqsoff.c b/kernel/trace/trace_irqsoff.c > index a745b0c..887cdb5 100644 > --- a/kernel/trace/trace_irqsoff.c > +++ b/kernel/trace/trace_irqsoff.c > @@ -560,8 +560,10 @@ static int __irqsoff_tracer_init(struct trace_array *tr) > save_flags = tr->trace_flags; > > /* non overwrite screws up the latency tracers */ > + mutex_lock(&event_mutex); > set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1); > set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1); > + mutex_unlock(&event_mutex); > > tr->max_latency = 0; > irqsoff_trace = tr; > @@ -586,8 +588,10 @@ static void __irqsoff_tracer_reset(struct trace_array *tr) > > stop_irqsoff_tracer(tr, is_graph(tr)); > > + mutex_lock(&event_mutex); > set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag); > set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag); > + mutex_unlock(&event_mutex); > ftrace_reset_array_ops(tr); > > irqsoff_busy = false; > diff --git a/kernel/trace/trace_sched_wakeup.c b/kernel/trace/trace_sched_wakeup.c > index 5e43b96..0bc67d1 100644 > --- a/kernel/trace/trace_sched_wakeup.c > +++ b/kernel/trace/trace_sched_wakeup.c > @@ -671,8 +671,10 @@ static int __wakeup_tracer_init(struct trace_array *tr) > save_flags = tr->trace_flags; > > /* non overwrite screws up the latency tracers */ > + mutex_lock(&event_mutex); > set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1); > set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1); > + mutex_unlock(&event_mutex); > > tr->max_latency = 0; > wakeup_trace = tr; > @@ -722,8 +724,10 @@ static void wakeup_tracer_reset(struct trace_array *tr) > /* make sure we put back any tasks we are tracing */ > wakeup_reset(tr); > > + mutex_lock(&event_mutex); > set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag); > set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag); > + mutex_unlock(&event_mutex); > ftrace_reset_array_ops(tr); > wakeup_busy = false; > }