Skip to contentSkip to conversation

tracing: creates 'tracing_is_disabled()'

PATCH Superseded Geyslan G. Bem 2 participants 6 messages Oct 18, 2013 – Nov 6, 2013 Tracing

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tracing: creates 'tracing_is_disabled()'

This patch creates the function 'tracing_is_disabled ", which can be used outside trace.c.

---
 kernel/trace/trace.c | 5 +++++
 kernel/trace/trace.h | 1 +
 2 files changed, 6 insertions(+)
kernel/trace/trace.c +5 -0
index 7974ba2..a120a73 100644
@@ -2964,6 +2964,11 @@ int tracing_open_generic(struct inode *inode, struct file *filp)
 	return 0;
 }
 
+inline bool tracing_is_disabled(void)
+{
+	return (tracing_disabled) ? true: false;
+}
+
 /*
  * Open and update trace_array ref count.
  * Must have the current trace_array passed to it.
kernel/trace/trace.h +1 -0
index 10c86fb..5deaa3b 100644
@@ -514,6 +514,7 @@ void tracing_reset_online_cpus(struct trace_buffer *buf);
 void tracing_reset_current(int cpu);
 void tracing_reset_all_online_cpus(void);
 int tracing_open_generic(struct inode *inode, struct file *filp);
+bool tracing_is_disabled(void);
 struct dentry *trace_create_file(const char *name,
 				 umode_t mode,
 				 struct dentry *parent,
Signature · 1 line
1.8.4
#1 lore raw

PATCH 2/2 tracing: fix referencing after memory freeing and refactors code

In 'system_tr_open()':
Fix possible 'dir' assignment after freeing it.

In both functions:
Restructures logic conditions testing 'tracing_is_disabled()' return before the others tests.
Centralizes the exiting in accordance to Coding Style, Chapter 7.

---
 kernel/trace/trace_events.c | 46 +++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
kernel/trace/trace_events.c +24 -22
index 368a4d5..0f56ebf 100644
@@ -1060,7 +1060,10 @@ static int subsystem_open(struct inode *inode, struct file *filp)
 	struct event_subsystem *system = NULL;
 	struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
 	struct trace_array *tr;
-	int ret;
+	int ret = -ENODEV;
+
+	if (tracing_is_disabled())
+		return ret;
 
 	/* Make sure the system still exists */
 	mutex_lock(&trace_types_lock);
@@ -1082,23 +1085,21 @@ static int subsystem_open(struct inode *inode, struct file *filp)
 	mutex_unlock(&trace_types_lock);
 
 	if (!system)
-		return -ENODEV;
+		return ret;
 
 	/* Some versions of gcc think dir can be uninitialized here */
 	WARN_ON(!dir);
 
 	/* Still need to increment the ref count of the system */
-	if (trace_array_get(tr) < 0) {
-		put_system(dir);
-		return -ENODEV;
-	}
+	ret = trace_array_get(tr);
+	if (ret)
+		goto err_get;
 
-	ret = tracing_open_generic(inode, filp);
-	if (ret < 0) {
-		trace_array_put(tr);
-		put_system(dir);
-	}
+	filp->private_data = dir;
+	return 0;
 
+err_get:
+	put_system(dir);
 	return ret;
 }
 
@@ -1106,28 +1107,29 @@ static int system_tr_open(struct inode *inode, struct file *filp)
 {
 	struct ftrace_subsystem_dir *dir;
 	struct trace_array *tr = inode->i_private;
-	int ret;
+	int ret = -ENODEV;
 
-	if (trace_array_get(tr) < 0)
-		return -ENODEV;
+	if (tracing_is_disabled())
+		return ret;
+
+	ret = trace_array_get(tr);
+	if (ret)
+		return ret;
 
 	/* Make a temporary dir that has no system but points to tr */
 	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
 	if (!dir) {
-		trace_array_put(tr);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto err_dir;
 	}
 
 	dir->tr = tr;
 
-	ret = tracing_open_generic(inode, filp);
-	if (ret < 0) {
-		trace_array_put(tr);
-		kfree(dir);
-	}
-
 	filp->private_data = dir;
+	return 0;
 
+err_dir:
+	trace_array_put(tr);
 	return ret;
 }
 
Signature · 1 line
1.8.4
maintainer
#2 lore raw

Re: [PATCH 2/2] tracing: fix referencing after memory freeing and refactors code

Sorry for the late review, I was busy getting ready for kernel summit and then traveling too much.

Quoted 49 lines · On Fri, 18 Oct 2013 17:59:42 -0300 "Geyslan G. Bem" <[email protected]> wrote:

In 'system_tr_open()':
Fix possible 'dir' assignment after freeing it.

In both functions:
Restructures logic conditions testing 'tracing_is_disabled()' return before the others tests.
Centralizes the exiting in accordance to Coding Style, Chapter 7.

---
 kernel/trace/trace_events.c | 46 +++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
kernel/trace/trace_events.c +8 -7
index 368a4d5..0f56ebf 100644
@@ -1060,7 +1060,10 @@ static int subsystem_open(struct inode *inode, struct file *filp)
 	struct event_subsystem *system = NULL;
 	struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
 	struct trace_array *tr;
-	int ret;
+	int ret = -ENODEV;
+
+	if (tracing_is_disabled())
+		return ret;
 
 	/* Make sure the system still exists */
 	mutex_lock(&trace_types_lock);
@@ -1082,23 +1085,21 @@ static int subsystem_open(struct inode *inode, struct file *filp)
 	mutex_unlock(&trace_types_lock);
 
 	if (!system)
-		return -ENODEV;
+		return ret;
 
 	/* Some versions of gcc think dir can be uninitialized here */
 	WARN_ON(!dir);
 
 	/* Still need to increment the ref count of the system */
-	if (trace_array_get(tr) < 0) {
-		put_system(dir);
-		return -ENODEV;
-	}
+	ret = trace_array_get(tr);
+	if (ret)
+		goto err_get;
 
-	ret = tracing_open_generic(inode, filp);

kernel/trace/trace_events.c static int subsystem_open(struct inode *inode, struct file *filp) · line 1103

Quoted 10 lines
kernel/trace/trace_events.c +4 -4
-	if (ret < 0) {
-		trace_array_put(tr);
-		put_system(dir);
-	}
+	filp->private_data = dir;
+	return 0;
 
+err_get:
+	put_system(dir);
 	return ret;

I don't see any improvement in the above code, except for the initial check of tracing_is_disabled(). Just add:

	if (tracing_is_disabled())
		return -ENODEV;

no need for all the fancy work with using ret and goto.

Quoted 28 lines
kernel/trace/trace_events.c +9 -5
 }
 
@@ -1106,28 +1107,29 @@ static int system_tr_open(struct inode *inode, struct file *filp)
 {
 	struct ftrace_subsystem_dir *dir;
 	struct trace_array *tr = inode->i_private;
-	int ret;
+	int ret = -ENODEV;
 
-	if (trace_array_get(tr) < 0)
-		return -ENODEV;
+	if (tracing_is_disabled())
+		return ret;
+
+	ret = trace_array_get(tr);
+	if (ret)
+		return ret;
 
 	/* Make a temporary dir that has no system but points to tr */
 	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
 	if (!dir) {
-		trace_array_put(tr);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto err_dir;
 	}
 
 	dir->tr = tr;

kernel/trace/trace_events.c static int system_tr_open(struct inode *inode, struct file *filp) · line 1130

Quoted 10 lines
kernel/trace/trace_events.c +1 -6
 
-	ret = tracing_open_generic(inode, filp);
-	if (ret < 0) {
-		trace_array_put(tr);
-		kfree(dir);
-	}
-
 	filp->private_data = dir;
+	return 0;
 

Again, we don't need the goto and err_dir. The simple fix is to finish the function with:

	ret = tracing_open_generic(inode, filp);
	if (ret < 0) {
		trace_array_put(tr);
		kfree(dir);
		return ret;
	}

	filp->private_data = dir;

	return 0;

-- Steve

Quoted 5 lines
kernel/trace/trace_events.c +2 -0
+err_dir:
+	trace_array_put(tr);
 	return ret;
 }
 
#3 lore raw

Re: [PATCH 2/2] tracing: fix referencing after memory freeing and refactors code

2013/11/6 Steven Rostedt <[email protected]>:

Quoted 3 lines

Sorry for the late review, I was busy getting ready for kernel summit and then traveling too much.

No problem.

Quoted 126 lines
Quoted 49 lines · On Fri, 18 Oct 2013 17:59:42 -0300 "Geyslan G. Bem" <[email protected]> wrote:

In 'system_tr_open()':
Fix possible 'dir' assignment after freeing it.

In both functions:
Restructures logic conditions testing 'tracing_is_disabled()' return before the others tests.
Centralizes the exiting in accordance to Coding Style, Chapter 7.

---
 kernel/trace/trace_events.c | 46 +++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
kernel/trace/trace_events.c +8 -7
index 368a4d5..0f56ebf 100644
@@ -1060,7 +1060,10 @@ static int subsystem_open(struct inode *inode, struct file *filp)
      struct event_subsystem *system = NULL;
      struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
      struct trace_array *tr;
-     int ret;
+     int ret = -ENODEV;
+
+     if (tracing_is_disabled())
+             return ret;
 
      /* Make sure the system still exists */
      mutex_lock(&trace_types_lock);
@@ -1082,23 +1085,21 @@ static int subsystem_open(struct inode *inode, struct file *filp)
      mutex_unlock(&trace_types_lock);
 
      if (!system)
-             return -ENODEV;
+             return ret;
 
      /* Some versions of gcc think dir can be uninitialized here */
      WARN_ON(!dir);
 
      /* Still need to increment the ref count of the system */
-     if (trace_array_get(tr) < 0) {
-             put_system(dir);
-             return -ENODEV;
-     }
+     ret = trace_array_get(tr);
+     if (ret)
+             goto err_get;
 
-     ret = tracing_open_generic(inode, filp);

kernel/trace/trace_events.c static int subsystem_open(struct inode *inode, struct file *filp) · line 1103

Quoted 10 lines
kernel/trace/trace_events.c +4 -4
-     if (ret < 0) {
-             trace_array_put(tr);
-             put_system(dir);
-     }
+     filp->private_data = dir;
+     return 0;
 
+err_get:
+     put_system(dir);
      return ret;

I don't see any improvement in the above code, except for the initial check of tracing_is_disabled(). Just add:

        if (tracing_is_disabled())
                return -ENODEV;

no need for all the fancy work with using ret and goto.

Quoted 28 lines
kernel/trace/trace_events.c +9 -5
 }
 
@@ -1106,28 +1107,29 @@ static int system_tr_open(struct inode *inode, struct file *filp)
 {
      struct ftrace_subsystem_dir *dir;
      struct trace_array *tr = inode->i_private;
-     int ret;
+     int ret = -ENODEV;
 
-     if (trace_array_get(tr) < 0)
-             return -ENODEV;
+     if (tracing_is_disabled())
+             return ret;
+
+     ret = trace_array_get(tr);
+     if (ret)
+             return ret;
 
      /* Make a temporary dir that has no system but points to tr */
      dir = kzalloc(sizeof(*dir), GFP_KERNEL);
      if (!dir) {
-             trace_array_put(tr);
-             return -ENOMEM;
+             ret = -ENOMEM;
+             goto err_dir;
      }
 
      dir->tr = tr;

kernel/trace/trace_events.c static int system_tr_open(struct inode *inode, struct file *filp) · line 1130

Quoted 10 lines
kernel/trace/trace_events.c +1 -6
 
-     ret = tracing_open_generic(inode, filp);
-     if (ret < 0) {
-             trace_array_put(tr);
-             kfree(dir);
-     }
-
      filp->private_data = dir;
+     return 0;
 

Again, we don't need the goto and err_dir. The simple fix is to finish the function with:

        ret = tracing_open_generic(inode, filp);
        if (ret < 0) {
                trace_array_put(tr);
                kfree(dir);
                return ret;
        }

        filp->private_data = dir;

        return 0;
Quoted 8 lines

-- Steve

Quoted 5 lines
kernel/trace/trace_events.c +2 -0
+err_dir:
+     trace_array_put(tr);
      return ret;
 }
 

Done:
[PATCH v2] tracing: fix referencing after memory freeing and refactors code

Signature · 4 lines
Regards,

Geyslan G. Bem
hackingbits.com
maintainer
#4 lore raw
In reply to Geyslan G. Bem
Quoted 14 lines · On Fri, 18 Oct 2013 17:59:41 -0300 "Geyslan G. Bem" <[email protected]> wrote:

This patch creates the function 'tracing_is_disabled ", which can be used outside trace.c.

---
 kernel/trace/trace.c | 5 +++++
 kernel/trace/trace.h | 1 +
 2 files changed, 6 insertions(+)
kernel/trace/trace.c +0 -0
index 7974ba2..a120a73 100644

kernel/trace/trace.c int tracing_open_generic(struct inode *inode, struct file *filp) · line 2967

Quoted 4 lines
kernel/trace/trace.c +1 -0
@@ -2964,6 +2964,11 @@ int tracing_open_generic(struct inode *inode, struct file *filp)
 	return 0;
 }
 
+inline bool tracing_is_disabled(void)

Nuke the "inline". Gcc and other compilers are smart enough to know to inline it on a -O2, that we don't need it. Especially, when it's used in another file.

-- Steve

Quoted 8 lines
kernel/trace/trace.c +4 -0
+{
+	return (tracing_disabled) ? true: false;
+}
+
 /*
  * Open and update trace_array ref count.
  * Must have the current trace_array passed to it.
kernel/trace/trace.h +0 -0
#5 lore raw

2013/10/18 Steven Rostedt <[email protected]>:

Quoted 20 lines
Quoted 17 lines · On Fri, 18 Oct 2013 17:59:41 -0300 "Geyslan G. Bem" <[email protected]> wrote:

This patch creates the function 'tracing_is_disabled ", which can be used outside trace.c.

---
 kernel/trace/trace.c | 5 +++++
 kernel/trace/trace.h | 1 +
 2 files changed, 6 insertions(+)
kernel/trace/trace.c +0 -0
index 7974ba2..a120a73 100644
@@ -2964,6 +2964,11 @@ int tracing_open_generic(struct inode *inode, struct file *filp)
      return 0;
 }
 
Quoted 7 lines

kernel/trace/trace.c int tracing_open_generic(struct inode *inode, struct file *filp) · line 2967

Quoted 1 line
kernel/trace/trace.c +1 -0
+inline bool tracing_is_disabled(void)

Nuke the "inline". Gcc and other compilers are smart enough to know to inline it on a -O2, that we don't need it. Especially, when it's used in another file.

-- Steve

Done. Version 3 sent.

Thanks.

Quoted 8 lines
Quoted 8 lines
kernel/trace/trace.c +4 -0
+{
+     return (tracing_disabled) ? true: false;
+}
+
 /*
  * Open and update trace_array ref count.
  * Must have the current trace_array passed to it.
kernel/trace/trace.h +0 -0