LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Rusty Russell <rusty@rustcorp.com.au>
Subject: [RFC][PATCH 07/17 v2] tracing: Allow for modules to export their trace enums as well
Date: Wed, 01 Apr 2015 21:56:55 -0400	[thread overview]
Message-ID: <20150402020941.100913661@goodmis.org> (raw)
In-Reply-To: <20150402015648.249824760@goodmis.org>

[-- Attachment #1: 0007-tracing-Allow-for-modules-to-export-their-trace-enum.patch --]
[-- Type: text/plain, Size: 7335 bytes --]

From: "Steven Rostedt (Red Hat)" <rostedt@goodmis.org>

Update the infrastructure such that modules that declare TRACE_DEFINE_ENUM()
will have those enums shown in the enum_map files in the tracing directory.

Link: http://lkml.kernel.org/r/87vbhjp74q.fsf@rustcorp.com.au

Acked-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 include/linux/module.h      |   2 +
 kernel/module.c             |   3 +
 kernel/trace/trace.c        | 149 +++++++++++++++++++++++++++++++++++++++++---
 kernel/trace/trace_events.c |   2 +-
 4 files changed, 145 insertions(+), 11 deletions(-)

diff --git a/include/linux/module.h b/include/linux/module.h
index 42999fe2dbd0..53dc41dd5c62 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -338,6 +338,8 @@ struct module {
 #ifdef CONFIG_EVENT_TRACING
 	struct ftrace_event_call **trace_events;
 	unsigned int num_trace_events;
+	struct trace_enum_map **trace_enums;
+	unsigned int num_trace_enums;
 #endif
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
 	unsigned int num_ftrace_callsites;
diff --git a/kernel/module.c b/kernel/module.c
index b3d634ed06c9..d8f8ab271c2b 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2753,6 +2753,9 @@ static int find_module_sections(struct module *mod, struct load_info *info)
 	mod->trace_events = section_objs(info, "_ftrace_events",
 					 sizeof(*mod->trace_events),
 					 &mod->num_trace_events);
+	mod->trace_enums = section_objs(info, "_ftrace_enum_map",
+					sizeof(*mod->trace_enums),
+					&mod->num_trace_enums);
 #endif
 #ifdef CONFIG_TRACING
 	mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index c64f8d848aa1..c9a714a42b7b 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -124,7 +124,36 @@ enum ftrace_dump_mode ftrace_dump_on_oops;
 int __disable_trace_on_warning;
 
 /* Map of enums to their values */
-static struct trace_enum_map *trace_enum_maps;
+struct trace_enum_map_head {
+	struct module			*mod;
+	unsigned long			length;
+};
+
+union trace_enum_map_item;
+
+struct trace_enum_map_tail {
+	/*
+	 * "end" is first and points to NULL as it must be different
+	 * than "mod" or "enum_string"
+	 */
+	const char			*end;	/* points to NULL */
+	union trace_enum_map_item	*next;
+};
+
+/*
+ * The trace_enum_maps are saved in an array with two extra elements,
+ * one at the beginning, and one at the end. The beginning item contains
+ * the count of the saved maps (head.length), and the module they
+ * belong to if not built in (head.mod). The ending item contains a
+ * pointer to the next array of saved enum_map items.
+ */
+union trace_enum_map_item {
+	struct trace_enum_map		map;
+	struct trace_enum_map_head	head;
+	struct trace_enum_map_tail	tail;
+};
+
+static union trace_enum_map_item *trace_enum_maps;
 
 static int tracing_set_tracer(struct trace_array *tr, const char *buf);
 
@@ -3911,32 +3940,62 @@ static const struct file_operations tracing_saved_cmdlines_size_fops = {
 	.write		= tracing_saved_cmdlines_size_write,
 };
 
-static void
-trace_insert_enum_map(struct trace_enum_map **start, struct trace_enum_map **stop)
+static inline union trace_enum_map_item *
+trace_enum_jmp_to_tail(union trace_enum_map_item *ptr)
+{
+	/* Return tail of array given the head */
+	return ptr + ptr->head.length + 1;
+}
+
+static void trace_insert_enum_map(struct module *mod,
+				  struct trace_enum_map **start, int len)
 {
+	struct trace_enum_map **stop;
 	struct trace_enum_map **map;
-	struct trace_enum_map *map_array;
-	int len = stop - start;
+	union trace_enum_map_item *map_array;
+	union trace_enum_map_item *ptr;
 
 	if (len <= 0)
 		return;
 
-	map_array = kmalloc(sizeof(*map_array) * (len + 1), GFP_KERNEL);
+	stop = start + len;
+
+	/*
+	 * The trace_enum_maps contains the map plus a head and tail item,
+	 * where the head holds the module and length of array, and the
+	 * tail holds a pointer to the next list.
+	 */
+	map_array = kmalloc(sizeof(*map_array) * (len + 2), GFP_KERNEL);
 	if (!map_array) {
 		pr_warning("Unable to allocate trace enum mapping\n");
 		return;
 	}
 
-	trace_enum_maps = map_array;
+	if (!trace_enum_maps)
+		trace_enum_maps = map_array;
+	else {
+		ptr = trace_enum_maps;
+		for (;;) {
+			ptr = trace_enum_jmp_to_tail(ptr);
+			if (!ptr->tail.next)
+				break;
+			ptr = ptr->tail.next;
+
+		}
+		ptr->tail.next = map_array;
+	}
+	map_array->head.mod = mod;
+	map_array->head.length = len;
+	map_array++;
 
 	for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
-		*map_array = **map;
+		map_array->map = **map;
 		map_array++;
 	}
 	memset(map_array, 0, sizeof(*map_array));
 
 	/* Pass in the start of the array (-len) */
-	trace_event_enum_update(&map_array[-len], len);
+	trace_event_enum_update(&map_array[-len].map, len);
 }
 
 static ssize_t
@@ -6578,9 +6637,75 @@ extern struct trace_enum_map *__stop_ftrace_enum_maps[];
 
 static void __init trace_enum_init(void)
 {
-	trace_insert_enum_map(__start_ftrace_enum_maps, __stop_ftrace_enum_maps);
+	int len;
+
+	len = __stop_ftrace_enum_maps - __start_ftrace_enum_maps;
+	trace_insert_enum_map(NULL, __start_ftrace_enum_maps, len);
+}
+
+#ifdef CONFIG_MODULES
+static void trace_module_add_enums(struct module *mod)
+{
+	if (!mod->num_trace_enums)
+		return;
+
+	/*
+	 * Modules with bad taint do not have events created, do
+	 * not bother with enums either.
+	 */
+	if (trace_module_has_bad_taint(mod))
+		return;
+
+	trace_insert_enum_map(mod, mod->trace_enums, mod->num_trace_enums);
+}
+
+static void trace_module_remove_enums(struct module *mod)
+{
+	union trace_enum_map_item *map;
+	union trace_enum_map_item **last = &trace_enum_maps;
+
+	if (!mod->num_trace_enums)
+		return;
+
+	map = trace_enum_maps;
+
+	while (map) {
+		if (map->head.mod == mod)
+			break;
+		map = trace_enum_jmp_to_tail(map);
+		last = &map->tail.next;
+		map = map->tail.next;
+	}
+	if (!map)
+		return;
+
+	*last = trace_enum_jmp_to_tail(map)->tail.next;
+	kfree(map);
+}
+
+static int trace_module_notify(struct notifier_block *self,
+			       unsigned long val, void *data)
+{
+	struct module *mod = data;
+
+	switch (val) {
+	case MODULE_STATE_COMING:
+		trace_module_add_enums(mod);
+		break;
+	case MODULE_STATE_GOING:
+		trace_module_remove_enums(mod);
+		break;
+	}
+
+	return 0;
 }
 
+static struct notifier_block trace_module_nb = {
+	.notifier_call = trace_module_notify,
+	.priority = 0,
+};
+#endif
+
 static __init int tracer_init_debugfs(void)
 {
 	struct dentry *d_tracer;
@@ -6607,6 +6732,10 @@ static __init int tracer_init_debugfs(void)
 
 	trace_enum_init();
 
+#ifdef CONFIG_MODULES
+	register_module_notifier(&trace_module_nb);
+#endif
+
 #ifdef CONFIG_DYNAMIC_FTRACE
 	trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
 			&ftrace_update_tot_cnt, &tracing_dyn_info_fops);
diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index 6b7fd0bf5d28..d5571d88d48d 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -2011,7 +2011,7 @@ static int trace_module_notify(struct notifier_block *self,
 
 static struct notifier_block trace_module_nb = {
 	.notifier_call = trace_module_notify,
-	.priority = 0,
+	.priority = 1, /* higher than trace.c module notify */
 };
 #endif /* CONFIG_MODULES */
 
-- 
2.1.4



  parent reply	other threads:[~2015-04-02  2:09 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-02  1:56 [RFC][PATCH 00/17 v2] tracing: Use TRACE_DEFINE_ENUM() to show enum values Steven Rostedt
2015-04-02  1:56 ` [RFC][PATCH 01/17 v2] tracing: Add TRACE_SYSTEM_VAR to intel-sst Steven Rostedt
     [not found]   ` <CADRr18NOdH3rqLeK3aJBaQ5ZfQSPDYiLdkDNshzSvUwctR8EEQ@mail.gmail.com>
2015-04-02 13:32     ` Steven Rostedt
2015-04-02 13:33       ` Steven Rostedt
2015-04-02 13:40         ` Mark Brown
2015-04-02 13:49           ` Steven Rostedt
2015-04-02 13:59             ` Mark Brown
2015-04-02 14:03               ` Steven Rostedt
2015-04-02 14:28                 ` Mark Brown
2015-04-02  1:56 ` [RFC][PATCH 02/17 v2] tracing: Add TRACE_SYSTEM_VAR to kvm-s390 Steven Rostedt
2015-04-02  9:15   ` Cornelia Huck
2015-04-02  1:56 ` [RFC][PATCH 03/17 v2] tracing: Add TRACE_SYSTEM_VAR to xhci-hcd Steven Rostedt
2015-04-02  1:56 ` [RFC][PATCH 04/17 v2] tracing: Give system name a pointer Steven Rostedt
2015-04-02  1:56 ` [RFC][PATCH 05/17 v2] tracing: Update trace-event-sample with TRACE_SYSTEM_VAR documentation Steven Rostedt
2015-04-02  1:56 ` [RFC][PATCH 06/17 v2] tracing: Add TRACE_DEFINE_ENUM() macro to map enums to their values Steven Rostedt
2015-04-02  7:47   ` Namhyung Kim
2015-04-02 13:27     ` Steven Rostedt
2015-04-02 13:57       ` [RFC][PATCH 06/17 v3] " Steven Rostedt
2015-04-02 16:25         ` Steven Rostedt
2015-04-02  1:56 ` Steven Rostedt [this message]
2015-04-02  1:56 ` [RFC][PATCH 08/17 v2] tracing/samples: Update the trace-event-sample.h with TRACE_DEFINE_ENUM() Steven Rostedt
2015-04-02  1:56 ` [RFC][PATCH 09/17 v2] tracing: Show the mapped enums in enum_map file Steven Rostedt
2015-04-02  2:58   ` Steven Rostedt
2015-04-02  1:56 ` [RFC][PATCH 10/17 v2] x86/tlb/trace: Export enums in used by tlb_flush tracepoint Steven Rostedt
2015-04-02  1:56 ` [RFC][PATCH 11/17 v2] net/9p/tracing: Export enums in tracepoints to userspace Steven Rostedt
2015-04-02  1:57 ` [RFC][PATCH 12/17 v2] f2fs: Export the enums in the " Steven Rostedt
2015-04-02  1:57 ` [RFC][PATCH 13/17 v2] irq/tracing: Export enums in tracepoints to user space Steven Rostedt
2015-04-02  1:57 ` [RFC][PATCH 14/17 v2] mm: tracing: " Steven Rostedt
2015-04-02  1:57 ` [RFC][PATCH 15/17 v2] SUNRPC: " Steven Rostedt
2015-04-02  1:57 ` [RFC][PATCH 16/17 v2] v4l: Export enums used by " Steven Rostedt
2015-04-02  1:57 ` [RFC][PATCH 17/17 v2] writeback: Export enums used by tracepoint " Steven Rostedt
2015-04-07  0:44 ` [RFC][PATCH 00/17 v2] tracing: Use TRACE_DEFINE_ENUM() to show enum values Masami Hiramatsu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150402020941.100913661@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=rusty@rustcorp.com.au \
    --subject='Re: [RFC][PATCH 07/17 v2] tracing: Allow for modules to export their trace enums as well' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).