* [patch 1/6] x86, ftrace, hw-branch-tracer: support hotplug cpus
@ 2009-01-19 9:26 Markus Metzger
2009-02-24 19:36 ` Ingo Molnar
0 siblings, 1 reply; 4+ messages in thread
From: Markus Metzger @ 2009-01-19 9:26 UTC (permalink / raw)
To: linux-kernel, mingo, tglx, hpa
Cc: markus.t.metzger, markus.t.metzger, srostedt
Support hotplug cpus.
Reported-by: Andi Kleen <ak@linux.intel.com>
Signed-off-by: Markus Metzger <markus.t.metzger@intel.com>
---
Index: ftrace/kernel/trace/trace_hw_branches.c
===================================================================
--- ftrace.orig/kernel/trace/trace_hw_branches.c 2009-01-16 15:52:35.000000000 +0100
+++ ftrace/kernel/trace/trace_hw_branches.c 2009-01-16 15:53:40.000000000 +0100
@@ -1,7 +1,8 @@
/*
* h/w branch tracer for x86 based on bts
*
- * Copyright (C) 2008 Markus Metzger <markus.t.metzger@gmail.com>
+ * Copyright (C) 2008-2009 Intel Corporation.
+ * Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009
*
*/
@@ -10,6 +11,9 @@
#include <linux/debugfs.h>
#include <linux/ftrace.h>
#include <linux/kallsyms.h>
+#include <linux/mutex.h>
+#include <linux/cpu.h>
+#include <linux/smp.h>
#include <asm/ds.h>
@@ -18,13 +22,31 @@
#define SIZEOF_BTS (1 << 13)
+/* The tracer mutex protects the below per-cpu tracer array.
+ It needs to be held to:
+ - start tracing on all cpus
+ - stop tracing on all cpus
+ - start tracing on a single hotplug cpu
+ - stop tracing on a single hotplug cpu
+ - read the trace from all cpus
+ - read the trace from a single cpu
+*/
+static DEFINE_MUTEX(bts_tracer_mutex);
static DEFINE_PER_CPU(struct bts_tracer *, tracer);
static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);
#define this_tracer per_cpu(tracer, smp_processor_id())
#define this_buffer per_cpu(buffer, smp_processor_id())
+static int __read_mostly trace_hw_branches_enabled;
+
+/*
+ * Start tracing on the current cpu.
+ * The argument is ignored.
+ *
+ * pre: bts_tracer_mutex must be locked.
+ */
static void bts_trace_start_cpu(void *arg)
{
if (this_tracer)
@@ -42,14 +64,20 @@
static void bts_trace_start(struct trace_array *tr)
{
- int cpu;
+ mutex_lock(&bts_tracer_mutex);
- tracing_reset_online_cpus(tr);
+ on_each_cpu(bts_trace_start_cpu, NULL, 1);
+ trace_hw_branches_enabled = 1;
- for_each_cpu_mask(cpu, cpu_possible_map)
- smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
+ mutex_unlock(&bts_tracer_mutex);
}
+/*
+ * Start tracing on the current cpu.
+ * The argument is ignored.
+ *
+ * pre: bts_tracer_mutex must be locked.
+ */
static void bts_trace_stop_cpu(void *arg)
{
if (this_tracer) {
@@ -60,20 +88,58 @@
static void bts_trace_stop(struct trace_array *tr)
{
- int cpu;
+ mutex_lock(&bts_tracer_mutex);
+
+ trace_hw_branches_enabled = 0;
+ on_each_cpu(bts_trace_stop_cpu, NULL, 1);
- for_each_cpu_mask(cpu, cpu_possible_map)
+ mutex_unlock(&bts_tracer_mutex);
+}
+
+static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
+ unsigned long action, void *hcpu)
+{
+ unsigned int cpu = (unsigned long)hcpu;
+
+ mutex_lock(&bts_tracer_mutex);
+
+ if (!trace_hw_branches_enabled)
+ goto out;
+
+ switch (action) {
+ case CPU_ONLINE:
+ case CPU_DOWN_FAILED:
+ smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
+ break;
+ case CPU_DOWN_PREPARE:
smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
+ break;
+ }
+
+ out:
+ mutex_unlock(&bts_tracer_mutex);
+ return NOTIFY_DONE;
}
+static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
+ .notifier_call = bts_hotcpu_handler
+};
+
static int bts_trace_init(struct trace_array *tr)
{
+ register_hotcpu_notifier(&bts_hotcpu_notifier);
tracing_reset_online_cpus(tr);
bts_trace_start(tr);
return 0;
}
+static void bts_trace_reset(struct trace_array *tr)
+{
+ bts_trace_stop(tr);
+ unregister_hotcpu_notifier(&bts_hotcpu_notifier);
+}
+
static void bts_trace_print_header(struct seq_file *m)
{
seq_puts(m,
@@ -107,18 +173,34 @@
{
struct ring_buffer_event *event;
struct hw_branch_entry *entry;
- unsigned long irq;
+ unsigned long irq1, irq2;
+ int cpu;
- event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), &irq);
- if (!event)
+ if (unlikely(!tr))
+ return;
+
+ if (unlikely(!trace_hw_branches_enabled))
return;
+
+ local_irq_save(irq1);
+ cpu = raw_smp_processor_id();
+ if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
+ goto out;
+
+ event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), &irq2);
+ if (!event)
+ goto out;
entry = ring_buffer_event_data(event);
tracing_generic_entry_update(&entry->ent, 0, from);
entry->ent.type = TRACE_HW_BRANCHES;
- entry->ent.cpu = smp_processor_id();
+ entry->ent.cpu = cpu;
entry->from = from;
entry->to = to;
- ring_buffer_unlock_commit(tr->buffer, event, irq);
+ ring_buffer_unlock_commit(tr->buffer, event, irq2);
+
+ out:
+ atomic_dec(&tr->data[cpu]->disabled);
+ local_irq_restore(irq1);
}
static void trace_bts_at(struct trace_array *tr,
@@ -142,6 +224,11 @@
}
}
+/*
+ * Collect the trace on the current cpu and write it into the ftrace buffer.
+ *
+ * pre: bts_tracer_mutex must be locked
+ */
static void trace_bts_cpu(void *arg)
{
struct trace_array *tr = (struct trace_array *) arg;
@@ -151,6 +238,9 @@
if (!this_tracer)
return;
+ if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
+ return;
+
ds_suspend_bts(this_tracer);
trace = ds_read_bts(this_tracer);
if (!trace)
@@ -170,17 +260,18 @@
static void trace_bts_prepare(struct trace_iterator *iter)
{
- int cpu;
+ mutex_lock(&bts_tracer_mutex);
+
+ on_each_cpu(trace_bts_cpu, iter->tr, 1);
- for_each_cpu_mask(cpu, cpu_possible_map)
- smp_call_function_single(cpu, trace_bts_cpu, iter->tr, 1);
+ mutex_unlock(&bts_tracer_mutex);
}
struct tracer bts_tracer __read_mostly =
{
.name = "hw-branch-tracer",
.init = bts_trace_init,
- .reset = bts_trace_stop,
+ .reset = bts_trace_reset,
.print_header = bts_trace_print_header,
.print_line = bts_trace_print_line,
.start = bts_trace_start,
---------------------------------------------------------------------
Intel GmbH
Dornacher Strasse 1
85622 Feldkirchen/Muenchen Germany
Sitz der Gesellschaft: Feldkirchen bei Muenchen
Geschaeftsfuehrer: Douglas Lusk, Peter Gleissner, Hannes Schwaderer
Registergericht: Muenchen HRB 47456 Ust.-IdNr.
VAT Registration No.: DE129385895
Citibank Frankfurt (BLZ 502 109 00) 600119052
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [patch 1/6] x86, ftrace, hw-branch-tracer: support hotplug cpus
2009-01-19 9:26 [patch 1/6] x86, ftrace, hw-branch-tracer: support hotplug cpus Markus Metzger
@ 2009-02-24 19:36 ` Ingo Molnar
2009-02-24 21:46 ` Nathan Lynch
0 siblings, 1 reply; 4+ messages in thread
From: Ingo Molnar @ 2009-02-24 19:36 UTC (permalink / raw)
To: Markus Metzger; +Cc: linux-kernel, tglx, hpa, markus.t.metzger, srostedt
* Markus Metzger <markus.t.metzger@intel.com> wrote:
> Support hotplug cpus.
> +static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
> + unsigned long action, void *hcpu)
> +{
> + unsigned int cpu = (unsigned long)hcpu;
> +
> + mutex_lock(&bts_tracer_mutex);
This is buggy: CPU hotplug handlers must not sleep (they are
called with irqs disabled), while this code does a mutex_lock()
which can sleep.
Ingo
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [patch 1/6] x86, ftrace, hw-branch-tracer: support hotplug cpus
2009-02-24 19:36 ` Ingo Molnar
@ 2009-02-24 21:46 ` Nathan Lynch
2009-02-25 9:01 ` Metzger, Markus T
0 siblings, 1 reply; 4+ messages in thread
From: Nathan Lynch @ 2009-02-24 21:46 UTC (permalink / raw)
To: Ingo Molnar
Cc: Markus Metzger, linux-kernel, tglx, hpa, markus.t.metzger, srostedt
On Tue, 24 Feb 2009 20:36:22 +0100
Ingo Molnar <mingo@elte.hu> wrote:
>
> * Markus Metzger <markus.t.metzger@intel.com> wrote:
>
> > Support hotplug cpus.
>
> > +static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
> > + unsigned long action, void *hcpu)
> > +{
> > + unsigned int cpu = (unsigned long)hcpu;
> > +
> > + mutex_lock(&bts_tracer_mutex);
>
> This is buggy: CPU hotplug handlers must not sleep (they are
> called with irqs disabled)
I don't think that is the case; the cpu notifier chains are "raw", not
atomic, and the cpu hotplug core doesn't disable irqs while processing
the chains afaict. And there are several current examples of cpu
hotplug callbacks performing sleeping operations (slab and slub,
page_alloc, probably many others).
^ permalink raw reply [flat|nested] 4+ messages in thread* RE: [patch 1/6] x86, ftrace, hw-branch-tracer: support hotplug cpus
2009-02-24 21:46 ` Nathan Lynch
@ 2009-02-25 9:01 ` Metzger, Markus T
0 siblings, 0 replies; 4+ messages in thread
From: Metzger, Markus T @ 2009-02-25 9:01 UTC (permalink / raw)
To: Nathan Lynch, Ingo Molnar
Cc: linux-kernel, tglx, hpa, markus.t.metzger, srostedt
>-----Original Message-----
>From: Nathan Lynch [mailto:ntl@pobox.com]
>Sent: Tuesday, February 24, 2009 10:46 PM
>To: Ingo Molnar
>Cc: Metzger, Markus T; linux-kernel@vger.kernel.org; tglx@linutronix.de; hpa@zytor.com;
>markus.t.metzger@gmail.com; srostedt@redhat.com
>Subject: Re: [patch 1/6] x86, ftrace, hw-branch-tracer: support hotplug cpus
>
>On Tue, 24 Feb 2009 20:36:22 +0100
>Ingo Molnar <mingo@elte.hu> wrote:
>
>>
>> * Markus Metzger <markus.t.metzger@intel.com> wrote:
>>
>> > Support hotplug cpus.
>>
>> > +static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
>> > + unsigned long action, void *hcpu)
>> > +{
>> > + unsigned int cpu = (unsigned long)hcpu;
>> > +
>> > + mutex_lock(&bts_tracer_mutex);
>>
>> This is buggy: CPU hotplug handlers must not sleep (they are
>> called with irqs disabled)
>
>I don't think that is the case; the cpu notifier chains are "raw", not
>atomic, and the cpu hotplug core doesn't disable irqs while processing
>the chains afaict. And there are several current examples of cpu
>hotplug callbacks performing sleeping operations (slab and slub,
>page_alloc, probably many others).
The hotplug code uses mutexes around raw_notifier_call_chain() calls in
all but one case:
For CPU_STARTING_FROZEN, the caller has interrupts disabled.
I guess I could check the notification reason before grabbing the lock
in the handler.
regards,
markus.
---------------------------------------------------------------------
Intel GmbH
Dornacher Strasse 1
85622 Feldkirchen/Muenchen Germany
Sitz der Gesellschaft: Feldkirchen bei Muenchen
Geschaeftsfuehrer: Douglas Lusk, Peter Gleissner, Hannes Schwaderer
Registergericht: Muenchen HRB 47456 Ust.-IdNr.
VAT Registration No.: DE129385895
Citibank Frankfurt (BLZ 502 109 00) 600119052
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2009-02-25 9:02 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-19 9:26 [patch 1/6] x86, ftrace, hw-branch-tracer: support hotplug cpus Markus Metzger
2009-02-24 19:36 ` Ingo Molnar
2009-02-24 21:46 ` Nathan Lynch
2009-02-25 9:01 ` Metzger, Markus T
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome