Skip to contentSkip to conversation

Kernel Access to Ftrace instances.

RFC In review Divya Indi 2 participants 6 messages Jun 5, 2019 – Jun 12, 2019 Tracing

lore
Share & tools
lore

Divya Indi

author
#0 lore raw

Hi,
Please Review the patches that follow. These include -

[PATCH 1/3] tracing: Relevant changes for kernel access to Ftrace instances. [PATCH 2/3] tracing: Adding additional NULL checks.
[PATCH 3/3] tracing: Add 2 new funcs. for kernel access to Ftrace instances.

Let me know if you have any concerns or questions.

A sample module demonstrating the use of the above functions will follow soon.

Thanks,
Divya

author
#1 lore raw

PATCH 1/3 tracing: Relevant changes for kernel access to Ftrace instances.

For commit (f45d122): tracing: Kernel access to Ftrace instances. We need the following additional changes to ensure other kernel components can use these functions -
1) Remove static keyword for newly exported fn - ftrace_set_clr_event.
2) Add the req functions to header file include/linux/trace_events.h.

---
 include/linux/trace_events.h | 6 ++++++
 kernel/trace/trace_events.c  | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)
include/linux/trace_events.h +6 -0
index 8a62731..d7b7d85 100644
@@ -539,6 +539,12 @@ extern int trace_define_field(struct trace_event_call *call, const char *type,
 
 #define is_signed_type(type)	(((type)(-1)) < (type)1)
 
+void trace_printk_init_buffers(void);
+int trace_array_printk(struct trace_array *tr, unsigned long ip,
+		const char *fmt, ...);
+struct trace_array *trace_array_create(const char *name);
+int trace_array_destroy(struct trace_array *tr);
+int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
 int trace_set_clr_event(const char *system, const char *event, int set);
 
 /*
kernel/trace/trace_events.c +1 -1
index 0ce3db6..b6b4618 100644
@@ -795,7 +795,7 @@ static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
 	return ret;
 }
 
-static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
+int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
 {
 	char *event = NULL, *sub = NULL, *match;
 	int ret;
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH 2/3 tracing: Adding additional NULL checks.

Now that we have exported certain functions providing access to Ftrace instances from other kernel components, we are adding some additional NULL checks to ensure safe usage by the users.

---
 kernel/trace/trace.c        | 3 +++
 kernel/trace/trace_events.c | 2 ++
 2 files changed, 5 insertions(+)
kernel/trace/trace.c +3 -0
index 1c80521..a60dc13 100644
@@ -3205,6 +3205,9 @@ int trace_array_printk(struct trace_array *tr,
 	if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
 		return 0;
 
+	if (!tr)
+		return -EINVAL;
+
 	va_start(ap, fmt);
 	ret = trace_array_vprintk(tr, ip, fmt, ap);
 	va_end(ap);
kernel/trace/trace_events.c +2 -0
index b6b4618..445b059 100644
@@ -800,6 +800,8 @@ int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
 	char *event = NULL, *sub = NULL, *match;
 	int ret;
 
+	if (!tr)
+		return -ENODEV;
 	/*
 	 * The buf format can be <subsystem>:<event-name>
 	 *  *:<event-name> means any event by that name.
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH 3/3 tracing: Add 2 new funcs. for kernel access to Ftrace instances.

Adding 2 new functions -
1) trace_array_lookup : Look up and return a trace array, given its name.
2) trace_array_set_clr_event : Enable/disable event recording to the given trace array.

---
 include/linux/trace_events.h |  3 +++
 kernel/trace/trace.c         | 11 +++++++++++
 kernel/trace/trace_events.c  | 22 ++++++++++++++++++++++
 3 files changed, 36 insertions(+)
include/linux/trace_events.h +3 -0
index d7b7d85..0cc99a8 100644
@@ -545,7 +545,10 @@ int trace_array_printk(struct trace_array *tr, unsigned long ip,
 struct trace_array *trace_array_create(const char *name);
 int trace_array_destroy(struct trace_array *tr);
 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
+struct trace_array *trace_array_lookup(const char *name);
 int trace_set_clr_event(const char *system, const char *event, int set);
+int trace_array_set_clr_event(struct trace_array *tr, const char *system,
+		const char *event, int set);
 
 /*
  * The double __builtin_constant_p is because gcc will give us an error
kernel/trace/trace.c +11 -0
index a60dc13..1d171fd 100644
@@ -8465,6 +8465,17 @@ static int instance_rmdir(const char *name)
 	return ret;
 }
 
+struct trace_array *trace_array_lookup(const char *name)
+{
+	struct trace_array *tr;
+	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
+		if (tr->name && strcmp(tr->name, name) == 0)
+			return tr;
+	}
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(trace_array_lookup);
+
 static __init void create_trace_instances(struct dentry *d_tracer)
 {
 	trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
kernel/trace/trace_events.c +22 -0
index 445b059..c126d2c 100644
@@ -859,6 +859,28 @@ int trace_set_clr_event(const char *system, const char *event, int set)
 }
 EXPORT_SYMBOL_GPL(trace_set_clr_event);
 
+/**
+ * trace_array_set_clr_event - enable or disable an event for a trace array
+ * @system: system name to match (NULL for any system)
+ * @event: event name to match (NULL for all events, within system)
+ * @set: 1 to enable, 0 to disable
+ *
+ * This is a way for other parts of the kernel to enable or disable
+ * event recording to instances.
+ *
+ * Returns 0 on success, -EINVAL if the parameters do not match any
+ * registered events.
+ */
+int trace_array_set_clr_event(struct trace_array *tr, const char *system,
+		const char *event, int set)
+{
+	if (!tr)
+		return -ENODEV;
+
+	return __ftrace_set_clr_event(tr, NULL, system, event, set);
+}
+EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
+
 /* 128 should be much more than enough */
 #define EVENT_BUF_SIZE		127
 
Signature · 1 line
1.8.3.1
maintainer
#4 lore raw

Re: [PATCH 3/3] tracing: Add 2 new funcs. for kernel access to Ftrace instances.

Quoted 33 lines · On Tue, 4 Jun 2019 17:42:05 -0700 Divya Indi <[email protected]> wrote:

Adding 2 new functions -
1) trace_array_lookup : Look up and return a trace array, given its name.
2) trace_array_set_clr_event : Enable/disable event recording to the given trace array.

---
 include/linux/trace_events.h |  3 +++
 kernel/trace/trace.c         | 11 +++++++++++
 kernel/trace/trace_events.c  | 22 ++++++++++++++++++++++
 3 files changed, 36 insertions(+)
include/linux/trace_events.h +3 -0
index d7b7d85..0cc99a8 100644
@@ -545,7 +545,10 @@ int trace_array_printk(struct trace_array *tr, unsigned long ip,
 struct trace_array *trace_array_create(const char *name);
 int trace_array_destroy(struct trace_array *tr);
 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
+struct trace_array *trace_array_lookup(const char *name);
 int trace_set_clr_event(const char *system, const char *event, int set);
+int trace_array_set_clr_event(struct trace_array *tr, const char *system,
+		const char *event, int set);
 
 /*
  * The double __builtin_constant_p is because gcc will give us an error
kernel/trace/trace.c +0 -0
index a60dc13..1d171fd 100644

kernel/trace/trace.c static int instance_rmdir(const char *name) · line 8471

Quoted 7 lines
kernel/trace/trace.c +4 -0
@@ -8465,6 +8465,17 @@ static int instance_rmdir(const char *name)
 	return ret;
 }
 
+struct trace_array *trace_array_lookup(const char *name)
+{
+	struct trace_array *tr;
+	list_for_each_entry(tr, &ftrace_trace_arrays, list) {

Accessing the ftrace_trace_arrays requires taking the trace_types_lock. It should also probably increment the ref counter too, and then trace_array_put() needs to be called.

This prevents the trace array from being freed while something has access to it.

-- Steve

Quoted 43 lines
kernel/trace/trace.c +7 -0
+		if (tr->name && strcmp(tr->name, name) == 0)
+			return tr;
+	}
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(trace_array_lookup);
+
 static __init void create_trace_instances(struct dentry *d_tracer)
 {
 	trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
kernel/trace/trace_events.c +22 -0
index 445b059..c126d2c 100644
@@ -859,6 +859,28 @@ int trace_set_clr_event(const char *system, const char *event, int set)
 }
 EXPORT_SYMBOL_GPL(trace_set_clr_event);
 
+/**
+ * trace_array_set_clr_event - enable or disable an event for a trace array
+ * @system: system name to match (NULL for any system)
+ * @event: event name to match (NULL for all events, within system)
+ * @set: 1 to enable, 0 to disable
+ *
+ * This is a way for other parts of the kernel to enable or disable
+ * event recording to instances.
+ *
+ * Returns 0 on success, -EINVAL if the parameters do not match any
+ * registered events.
+ */
+int trace_array_set_clr_event(struct trace_array *tr, const char *system,
+		const char *event, int set)
+{
+	if (!tr)
+		return -ENODEV;
+
+	return __ftrace_set_clr_event(tr, NULL, system, event, set);
+}
+EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
+
 /* 128 should be much more than enough */
 #define EVENT_BUF_SIZE		127
 
author
#5 lore raw

Re: [PATCH 3/3] tracing: Add 2 new funcs. for kernel access to Ftrace instances.

Hi Steven,

Thanks for taking the time to review. Please find my comments inline.

Quoted 47 lines · Steven Rostedt
Quoted 33 lines · On Tue, 4 Jun 2019 17:42:05 -0700 Divya Indi <[email protected]> wrote:

Adding 2 new functions -
1) trace_array_lookup : Look up and return a trace array, given its name.
2) trace_array_set_clr_event : Enable/disable event recording to the given trace array.

---
  include/linux/trace_events.h |  3 +++
  kernel/trace/trace.c         | 11 +++++++++++
  kernel/trace/trace_events.c  | 22 ++++++++++++++++++++++
  3 files changed, 36 insertions(+)
include/linux/trace_events.h +3 -0
index d7b7d85..0cc99a8 100644
@@ -545,7 +545,10 @@ int trace_array_printk(struct trace_array *tr, unsigned long ip,
  struct trace_array *trace_array_create(const char *name);
  int trace_array_destroy(struct trace_array *tr);
  int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
+struct trace_array *trace_array_lookup(const char *name);
  int trace_set_clr_event(const char *system, const char *event, int set);
+int trace_array_set_clr_event(struct trace_array *tr, const char *system,
+		const char *event, int set);
 
  /*
   * The double __builtin_constant_p is because gcc will give us an error
kernel/trace/trace.c +0 -0
index a60dc13..1d171fd 100644

kernel/trace/trace.c static int instance_rmdir(const char *name) · line 8471

Quoted 7 lines
kernel/trace/trace.c +4 -0
@@ -8465,6 +8465,17 @@ static int instance_rmdir(const char *name)
  	return ret;
  }
 
+struct trace_array *trace_array_lookup(const char *name)
+{
+	struct trace_array *tr;
+	list_for_each_entry(tr, &ftrace_trace_arrays, list) {

Accessing the ftrace_trace_arrays requires taking the trace_types_lock. It should also probably increment the ref counter too, and then trace_array_put() needs to be called.

Quoted 4 lines

This prevents the trace array from being freed while something has access to it.

-- Steve

Agree - Noted!

Also, adding a similar change for trace_array_create which also returns a ptr to a newly created trace_array so will face the same issue.

Since trace_array_lookup and trace_array_create will be accompanied by a trace_array_destroy once the use of the trace_array is done, decrementing the reference ctr here.

Sending a v2 to address this.

Thanks,

Divya

Quoted 44 lines
Quoted 44 lines
kernel/trace/trace.c +7 -0
+		if (tr->name && strcmp(tr->name, name) == 0)
+			return tr;
+	}
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(trace_array_lookup);
+
  static __init void create_trace_instances(struct dentry *d_tracer)
  {
  	trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
kernel/trace/trace_events.c +22 -0
index 445b059..c126d2c 100644
@@ -859,6 +859,28 @@ int trace_set_clr_event(const char *system, const char *event, int set)
  }
  EXPORT_SYMBOL_GPL(trace_set_clr_event);
 
+/**
+ * trace_array_set_clr_event - enable or disable an event for a trace array
+ * @system: system name to match (NULL for any system)
+ * @event: event name to match (NULL for all events, within system)
+ * @set: 1 to enable, 0 to disable
+ *
+ * This is a way for other parts of the kernel to enable or disable
+ * event recording to instances.
+ *
+ * Returns 0 on success, -EINVAL if the parameters do not match any
+ * registered events.
+ */
+int trace_array_set_clr_event(struct trace_array *tr, const char *system,
+		const char *event, int set)
+{
+	if (!tr)
+		return -ENODEV;
+
+	return __ftrace_set_clr_event(tr, NULL, system, event, set);
+}
+EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
+
  /* 128 should be much more than enough */
  #define EVENT_BUF_SIZE		127