Skip to contentSkip to conversation

tracing: fix referencing after memory freeing and refactors code

PATCH Superseded Geyslan G. Bem 3 participants 12 messages Oct 18, 2013 – Oct 19, 2013 Tracing

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

Restructures function logic conditions testing 'tracing_open_generic' return before the others. It avoids: unnecessary trace_array_get and kzalloc when tracing is disabled; and fix the possible 'dir' assignment after freeing it.

Centralizes the exiting, ensuring the 'trace_array_put' on error, in accordance to Coding Style, Chapter 7.

---
 kernel/trace/trace_events.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
kernel/trace/trace_events.c +12 -10
index 368a4d5..8232362 100644
@@ -1108,26 +1108,28 @@ static int system_tr_open(struct inode *inode, struct file *filp)
 	struct trace_array *tr = inode->i_private;
 	int ret;
 
-	if (trace_array_get(tr) < 0)
-		return -ENODEV;
+	ret = tracing_open_generic(inode, filp);
+	if (ret)
+		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
#1 lore raw
Quoted 20 lines · On Thu, 17 Oct 2013 22:44:56 -0300 "Geyslan G. Bem" <[email protected]> wrote:

Restructures function logic conditions testing 'tracing_open_generic' return before the others. It avoids: unnecessary trace_array_get and kzalloc when tracing is disabled; and fix the possible 'dir' assignment after freeing it.

Centralizes the exiting, ensuring the 'trace_array_put' on error, in accordance to Coding Style, Chapter 7.

---
 kernel/trace/trace_events.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
kernel/trace/trace_events.c +0 -0
index 368a4d5..8232362 100644
@@ -1108,26 +1108,28 @@ static int system_tr_open(struct inode *inode, struct file *filp)
 	struct trace_array *tr = inode->i_private;
 	int ret;

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

Quoted 10 lines
kernel/trace/trace_events.c +7 -2
 
-	if (trace_array_get(tr) < 0)
-		return -ENODEV;
+	ret = tracing_open_generic(inode, filp);
+	if (ret)
+		return ret;
+
+	ret = trace_array_get(tr);
+	if (ret)
+		return ret;

Hmm,

I'm thinking of just nuking the tracing_open_generic() here. The only thing it does here is the tracing_disabled check. The assignment of inode->i_private to filp->private_data is pointless as it just reassigns it anyway.

We could add a tracing_is_disabled() function to test instead.

-- Steve

Quoted 26 lines
kernel/trace/trace_events.c +5 -8
 	/* 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;
 }
 
#2 lore raw

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

Quoted 36 lines
Quoted 20 lines · On Thu, 17 Oct 2013 22:44:56 -0300 "Geyslan G. Bem" <[email protected]> wrote:

Restructures function logic conditions testing 'tracing_open_generic' return before the others. It avoids: unnecessary trace_array_get and kzalloc when tracing is disabled; and fix the possible 'dir' assignment after freeing it.

Centralizes the exiting, ensuring the 'trace_array_put' on error, in accordance to Coding Style, Chapter 7.

---
 kernel/trace/trace_events.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
kernel/trace/trace_events.c +0 -0
index 368a4d5..8232362 100644
@@ -1108,26 +1108,28 @@ static int system_tr_open(struct inode *inode, struct file *filp)
      struct trace_array *tr = inode->i_private;
      int ret;

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

Quoted 10 lines
kernel/trace/trace_events.c +7 -2
 
-     if (trace_array_get(tr) < 0)
-             return -ENODEV;
+     ret = tracing_open_generic(inode, filp);
+     if (ret)
+             return ret;
+
+     ret = trace_array_get(tr);
+     if (ret)
+             return ret;

Hmm,

Quoted 6 lines

I'm thinking of just nuking the tracing_open_generic() here. The only thing it does here is the tracing_disabled check. The assignment of inode->i_private to filp->private_data is pointless as it just reassigns it anyway.

We could add a tracing_is_disabled() function to test instead.

Nice, I can do it.

Questions:

1267 static const struct file_operations ftrace_enable_fops = {
1268 .open = tracing_open_generic,
...
1286 static const struct file_operations ftrace_event_filter_fops = {
1287 .open = tracing_open_generic,
...
1317 static const struct file_operations ftrace_show_header_fops = {
1318 .open = tracing_open_generic,

Are that structures in same case? Their 'open' can be replaced to the new 'tracing_is_disabled()?

I think that 'subsystem_open()' can be also refactored to use the about to rise 'tracing_is_disabled()'. Am I right?
1096 ret = tracing_open_generic(inode, filp);

Regards,
-- Geyslan

Quoted 30 lines

-- Steve

Quoted 26 lines
kernel/trace/trace_events.c +5 -8
      /* 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;
 }
 
#3 lore raw

2013/10/18 Geyslan Gregório Bem <[email protected]>:

Quoted 42 lines

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

Quoted 41 lines
Quoted 20 lines · On Thu, 17 Oct 2013 22:44:56 -0300 "Geyslan G. Bem" <[email protected]> wrote:

Restructures function logic conditions testing 'tracing_open_generic' return before the others. It avoids: unnecessary trace_array_get and kzalloc when tracing is disabled; and fix the possible 'dir' assignment after freeing it.

Centralizes the exiting, ensuring the 'trace_array_put' on error, in accordance to Coding Style, Chapter 7.

---
 kernel/trace/trace_events.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
kernel/trace/trace_events.c +0 -0
index 368a4d5..8232362 100644
@@ -1108,26 +1108,28 @@ static int system_tr_open(struct inode *inode, struct file *filp)
      struct trace_array *tr = inode->i_private;
      int ret;

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

Quoted 10 lines
kernel/trace/trace_events.c +7 -2
 
-     if (trace_array_get(tr) < 0)
-             return -ENODEV;
+     ret = tracing_open_generic(inode, filp);
+     if (ret)
+             return ret;
+
+     ret = trace_array_get(tr);
+     if (ret)
+             return ret;

Hmm,

I'm thinking of just nuking the tracing_open_generic() here. The only thing it does here is the tracing_disabled check. The assignment of inode->i_private to filp->private_data is pointless as it just reassigns it anyway.

Quoted 5 lines
Quoted 1 line

We could add a tracing_is_disabled() function to test instead.

Nice, I can do it.

Questions:

I realized that here not:

Quoted 9 lines

1267 static const struct file_operations ftrace_enable_fops = {
1268 .open = tracing_open_generic,
...
1286 static const struct file_operations ftrace_event_filter_fops = {
1287 .open = tracing_open_generic,
...
1317 static const struct file_operations ftrace_show_header_fops = {
1318 .open = tracing_open_generic,

Tell me about this other case:

Quoted 7 lines

Are that structures in same case? Their 'open' can be replaced to the new 'tracing_is_disabled()?

I think that 'subsystem_open()' can be also refactored to use the about to rise 'tracing_is_disabled()'. Am I right?
1096 ret = tracing_open_generic(inode, filp);

Regards,
-- Geyslan

Quoted 31 lines
Quoted 31 lines

-- Steve

Quoted 26 lines
kernel/trace/trace_events.c +5 -8
      /* 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;
 }
 
maintainer
#4 lore raw
Quoted 6 lines · On Fri, 18 Oct 2013 08:02:32 -0300 Geyslan Gregório Bem <[email protected]>…
Quoted 6 lines
Quoted 6 lines

I'm thinking of just nuking the tracing_open_generic() here. The only thing it does here is the tracing_disabled check. The assignment of inode->i_private to filp->private_data is pointless as it just reassigns it anyway.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

We could add a tracing_is_disabled() function to test instead.

Nice, I can do it.

Questions:

I realized that here not:

Right, tracing_is_disabled() should not be called as a fops method.

Quoted 10 lines
Quoted 9 lines

1267 static const struct file_operations ftrace_enable_fops = {
1268 .open = tracing_open_generic,
...
1286 static const struct file_operations ftrace_event_filter_fops = {
1287 .open = tracing_open_generic,
...
1317 static const struct file_operations ftrace_show_header_fops = {
1318 .open = tracing_open_generic,

Quoted 8 lines

Tell me about this other case:

Quoted 7 lines

Are that structures in same case? Their 'open' can be replaced to the new 'tracing_is_disabled()?

I think that 'subsystem_open()' can be also refactored to use the about to rise 'tracing_is_disabled()'. Am I right?
1096 ret = tracing_open_generic(inode, filp);

Hmm, no, it needs the assignment too. But perhaps we could just open code it. The tracing_open_generic() is more for fops then to be a helper for functions that don't use it for their .open routine.

Thus, for subsystem_open() you could have at the start:

	if (tracing_is_disabled())
		return -ENODEV;

Then have:

- ret = tracing_open_generic(inode, filp);
- if (ret < 0) {
- trace_array_put(tr);
- put_system(dir);
- }
+ filp->private_data = dir;

-- Steve

Quoted 3 lines

Regards,
-- Geyslan

#5 lore raw
In reply to Steven Rostedt
Quoted 6 lines · Steven Rostedt
Quoted 2 lines · On Thu, 17 Oct 2013 22:44:56 -0300 "Geyslan G. Bem" <[email protected]> wrote:

and fix the possible 'dir'
assignment after freeing it.

This should be safe afaics, nobody will use it anyway. Even subsystem_release() won't be called if .open() fails. But I agree this doesn't look good.

Quoted 3 lines

I'm thinking of just nuking the tracing_open_generic() here. The only thing it does here is the tracing_disabled check. The assignment of inode->i_private to filp->private_data is pointless

The same for ftrace_enable_fops() and ftrace_event_filter_fops() at least. The users of event_file_data() do not use ->private_data.

OTOH, say, trace_format_open() doesn't check tracing_disabled, so

Quoted 1 line

We could add a tracing_is_disabled() function to test instead.

perhaps it can have more callers.

Oleg.

#6 lore raw

Steven, et all, sorry for off-topic...

You probably know that kernel/trace/ is not trivial ;) and the fact that cscope doesn't shows the callers in kernel/trace/trace_events.c doesn't really help.

Fixed by the patch below, but I am not sure it is fine to uglify the code to help the buggy tools. Is there any other way?

Oleg.
---

kernel/trace/trace_events.c +3 -0
index 368a4d5..087fff1 100644
@@ -68,6 +68,9 @@ static int system_refcount_dec(struct event_subsystem *system)
 		struct ftrace_event_file *___n;				\
 		list_for_each_entry_safe(file, ___n, &tr->events, list)
 
+/* Yes. Twice to not confuse csope */
+#define while_for_each_event_file()		\
+	}
 #define while_for_each_event_file()		\
 	}
 
#7 lore raw
In reply to Oleg Nesterov

2013/10/19 Oleg Nesterov <[email protected]>:

Quoted 11 lines · Steven Rostedt
Quoted 6 lines
Quoted 2 lines · On Thu, 17 Oct 2013 22:44:56 -0300 "Geyslan G. Bem" <[email protected]> wrote:

and fix the possible 'dir'
assignment after freeing it.

This should be safe afaics, nobody will use it anyway. Even subsystem_release() won't be called if .open() fails. But I agree this doesn't look good.

Right.

Quoted 8 lines
Quoted 4 lines

I'm thinking of just nuking the tracing_open_generic() here. The only thing it does here is the tracing_disabled check. The assignment of inode->i_private to filp->private_data is pointless

The same for ftrace_enable_fops() and ftrace_event_filter_fops() at least. The users of event_file_data() do not use ->private_data.

Aren't "ftrace_enable_fops" and "ftrace_event_filter_fops" structures? About event_file_data() I think that the callers uses the private_data. So, we have to analyze better.

Quoted 6 lines

OTOH, say, trace_format_open() doesn't check tracing_disabled, so

Quoted 1 line

We could add a tracing_is_disabled() function to test instead.

perhaps it can have more callers.

static int trace_format_open(struct inode *inode, struct file *file)

{
    struct seq_file *m;
    int ret;

    ret = seq_open(file, &trace_format_seq_ops);
    if (ret < 0)
        return ret;

    m = file->private_data;
    m->private = file;

    return 0;
}

I really got confused here. The 'm' assignments are, to me, pointless.

Quoted 2 lines

Oleg.

#8 lore raw
Quoted 8 lines · Geyslan Gregório Bem

2013/10/19 Oleg Nesterov <[email protected]>:

Quoted 6 lines · Steven Rostedt
Quoted 4 lines

I'm thinking of just nuking the tracing_open_generic() here. The only thing it does here is the tracing_disabled check. The assignment of inode->i_private to filp->private_data is pointless

Quoted 5 lines
Quoted 3 lines

The same for ftrace_enable_fops() and ftrace_event_filter_fops() at least. The users of event_file_data() do not use ->private_data.

Aren't "ftrace_enable_fops" and "ftrace_event_filter_fops" structures?

I meant, their ->open() methods.

Quoted 2 lines

About event_file_data() I think that the callers uses the private_data. So, we have to analyze better.

No, event_file_data() uses ->i_private, filp->private_data is not used. And it can't be used, it can point to the already destroyed/freed data.

but, as for seq_open() users,

Quoted 15 lines

static int trace_format_open(struct inode *inode, struct file *file)

{
    struct seq_file *m;
    int ret;

    ret = seq_open(file, &trace_format_seq_ops);
    if (ret < 0)
        return ret;

    m = file->private_data;
    m->private = file;

    return 0;
}
Quoted 1 line

I really got confused here. The 'm' assignments are, to me, pointless.

I confused too... Why do you think it is pointless?

Just in case, not that after seq_open() ->private_data points to seq_file but it is still "void *". And in this case ->private_data has nothing to do with ->private_data set by tracing_open_generic().

Oleg.

#9 lore raw

2013/10/19 Oleg Nesterov <[email protected]>:

Quoted 9 lines · Geyslan Gregório Bem
Quoted 8 lines

2013/10/19 Oleg Nesterov <[email protected]>:

Quoted 6 lines · Steven Rostedt
Quoted 4 lines

I'm thinking of just nuking the tracing_open_generic() here. The only thing it does here is the tracing_disabled check. The assignment of inode->i_private to filp->private_data is pointless

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

The same for ftrace_enable_fops() and ftrace_event_filter_fops() at least. The users of event_file_data() do not use ->private_data.

Aren't "ftrace_enable_fops" and "ftrace_event_filter_fops" structures?

I meant, their ->open() methods.

I see.

Quoted 6 lines
Quoted 3 lines

About event_file_data() I think that the callers uses the private_data. So, we have to analyze better.

No, event_file_data() uses ->i_private, filp->private_data is not used. And it can't be used, it can point to the already destroyed/freed data.

Ok. I got it.

Quoted 18 lines

but, as for seq_open() users,

Quoted 15 lines

static int trace_format_open(struct inode *inode, struct file *file)

{
    struct seq_file *m;
    int ret;

    ret = seq_open(file, &trace_format_seq_ops);
    if (ret < 0)
        return ret;

    m = file->private_data;
    m->private = file;

    return 0;
}
Quoted 8 lines
Quoted 1 line

I really got confused here. The 'm' assignments are, to me, pointless.

I confused too... Why do you think it is pointless?

Just in case, not that after seq_open() ->private_data points to seq_file but it is still "void *". And in this case ->private_data has nothing to do with ->private_data set by tracing_open_generic().

My bad. I realized it now.

Quoted 2 lines

Oleg.

Let's wait Steve's reply about further use of tracing_is_disabled().

Regards.
--Geyslan

maintainer
#10 lore raw
Quoted 1 line · On Sat, 19 Oct 2013 11:41:10 -0300 Geyslan Gregório Bem <[email protected]>…

Let's wait Steve's reply about further use of tracing_is_disabled().

Might want to ping me later. This weekend I'm taking my daughter to colleges, and early Monday morning I'm leaving to Edinburgh (arriving on Tuesday).

-- Steve

#11 lore raw

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

Quoted 11 lines
Quoted 1 line · On Sat, 19 Oct 2013 11:41:10 -0300 Geyslan Gregório Bem <[email protected]>…

Let's wait Steve's reply about further use of tracing_is_disabled().

Might want to ping me later. This weekend I'm taking my daughter to colleges, and early Monday morning I'm leaving to Edinburgh (arriving on Tuesday).

-- Steve

There is no problem, weekends are sacred to me. It's family time.

o/
--Geyslan