Skip to contentSkip to conversation

tracing: trace_output.c, fix false positive compiler warning

tip:tracing/core Steven Rostedt 2 participants 3 messages May 6, 2009 1 message not in the archive Tracing

lore
Share & tools
lore

Steven Rostedt

author maintainer
#1 lore raw
In reply to a message not in the archive
Quoted 37 lines · tip-bot for Jaswinder Singh Rajput
Commit-ID:  48dd0fed90e2b1f1ba87401439b85942181c6df3
Gitweb:     http://git.kernel.org/tip/48dd0fed90e2b1f1ba87401439b85942181c6df3
Author:     Jaswinder Singh Rajput <[email protected]>
AuthorDate: Wed, 6 May 2009 15:45:45 +0530
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 6 May 2009 14:19:16 +0200

tracing: trace_output.c, fix false positive compiler warning

This compiler warning:

CC kernel/trace/trace_output.o
kernel/trace/trace_output.c: In function ?register_ftrace_event?:
kernel/trace/trace_output.c:544: warning: ?list? may be used uninitialized in this function

Is wrong as 'list' is always initialized - but GCC (4.3.2) does not recognize this relationship properly.

Work around the warning by initializing the variable to NULL.

[ Impact: fix false positive compiler warning ]

LKML-Reference: <new-submission>

---
 kernel/trace/trace_output.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/trace/trace_output.c +0 -0
index 5fc51f0..8bd9a2c 100644

kernel/trace/trace_output.c int register_ftrace_event(struct trace_event *event) · line 544

Quoted 5 lines
kernel/trace/trace_output.c +1 -1
@@ -541,7 +541,7 @@ int register_ftrace_event(struct trace_event *event)
 	INIT_LIST_HEAD(&event->list);
 
 	if (!event->type) {
-		struct list_head *list;
+		struct list_head *list = NULL;

Actually this is the wrong place to initialize. The correct place is in the function that is expected to.

kernel/trace/trace_output.c int register_ftrace_event(struct trace_event *event) · line 546

Quoted 3 lines
kernel/trace/trace_output.c +0 -0
 		if (next_event_type > FTRACE_MAX_EVENT) {
 

Could you test this patch instead:

tracing: quiet gcc compile warning

Some versions of gcc can not catch the fact that the list variable in register_ftrace_event is initialized. There's one place in the logic that is a bit complex. The trace_search_list function that initializes the list will not initialize it on error. But that's OK, because the caller checks for error and will not use the list variable if there is one. Some versions of gcc miss this.

[ Impact: quiet gcc from complaining about an unused variable ]

kernel/trace/trace_output.c +3 -1
index 5fc51f0..e949cf6 100644
@@ -506,8 +506,10 @@ static int trace_search_list(struct list_head **list)
 	}
 
 	/* Did we used up all 65 thousand events??? */
-	if ((last + 1) > FTRACE_MAX_EVENT)
+	if ((last + 1) > FTRACE_MAX_EVENT) {
+		*list = NULL;
 		return 0;
+	}
 
 	*list = &e->list;
 	return last + 1;
#2 lore raw
Quoted 42 lines · Steven Rostedt
Quoted 39 lines · tip-bot for Jaswinder Singh Rajput
Commit-ID:  48dd0fed90e2b1f1ba87401439b85942181c6df3
Gitweb:     http://git.kernel.org/tip/48dd0fed90e2b1f1ba87401439b85942181c6df3
Author:     Jaswinder Singh Rajput <[email protected]>
AuthorDate: Wed, 6 May 2009 15:45:45 +0530
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 6 May 2009 14:19:16 +0200

tracing: trace_output.c, fix false positive compiler warning

This compiler warning:

CC kernel/trace/trace_output.o
kernel/trace/trace_output.c: In function ?register_ftrace_event?:
kernel/trace/trace_output.c:544: warning: ?list? may be used uninitialized in this function

Is wrong as 'list' is always initialized - but GCC (4.3.2) does not recognize this relationship properly.

Work around the warning by initializing the variable to NULL.

[ Impact: fix false positive compiler warning ]

LKML-Reference: <new-submission>

---
 kernel/trace/trace_output.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/trace/trace_output.c +0 -0
index 5fc51f0..8bd9a2c 100644
@@ -541,7 +541,7 @@ int register_ftrace_event(struct trace_event *event)
 	INIT_LIST_HEAD(&event->list);
 
Quoted 6 lines

kernel/trace/trace_output.c int register_ftrace_event(struct trace_event *event) · line 544

Quoted 3 lines
kernel/trace/trace_output.c +1 -1
 	if (!event->type) {
-		struct list_head *list;
+		struct list_head *list = NULL;

Actually this is the wrong place to initialize. The correct place is in the function that is expected to.

does it really matter? It's far more robust to initialize at the definition site, because there we can be sure there's no side-effects. This one:

kernel/trace/trace_output.c static int trace_search_list(struct list_head **list) · line 512

Quoted 6 lines
kernel/trace/trace_output.c +3 -1
 	/* Did we used up all 65 thousand events??? */
-	if ((last + 1) > FTRACE_MAX_EVENT)
+	if ((last + 1) > FTRACE_MAX_EVENT) {
+		*list = NULL;
 		return 0;
+	}

Is correct too but needs a semantic check (and ongoing maintenance, etc.).

Ingo

author maintainer
#3 lore raw
Quoted 22 lines · Ingo Molnar
Quoted 11 lines
Quoted 11 lines
---
 kernel/trace/trace_output.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/trace/trace_output.c +0 -0
index 5fc51f0..8bd9a2c 100644
@@ -541,7 +541,7 @@ int register_ftrace_event(struct trace_event *event)
 	INIT_LIST_HEAD(&event->list);
 
Quoted 6 lines

kernel/trace/trace_output.c int register_ftrace_event(struct trace_event *event) · line 544

Quoted 3 lines
kernel/trace/trace_output.c +1 -1
 	if (!event->type) {
-		struct list_head *list;
+		struct list_head *list = NULL;

Actually this is the wrong place to initialize. The correct place is in the function that is expected to.

does it really matter? It's far more robust to initialize at the definition site, because there we can be sure there's no side-effects. This one:

Quoted 9 lines

kernel/trace/trace_output.c static int trace_search_list(struct list_head **list) · line 512

Quoted 6 lines
kernel/trace/trace_output.c +3 -1
 	/* Did we used up all 65 thousand events??? */
-	if ((last + 1) > FTRACE_MAX_EVENT)
+	if ((last + 1) > FTRACE_MAX_EVENT) {
+		*list = NULL;
 		return 0;
+	}

Is correct too but needs a semantic check (and ongoing maintenance, etc.).

Actually, to answer this, we need to look at the entire code. Just looking at the changes of a patch does not include the big picture.

The original code is:

static int trace_search_list(struct list_head **list)

{
        struct trace_event *e;
        int last = __TRACE_LAST_TYPE;

        if (list_empty(&ftrace_event_list)) {
                *list = &ftrace_event_list;
                return last + 1;
        }

        /*
         * We used up all possible max events,
         * lets see if somebody freed one.
         */
        list_for_each_entry(e, &ftrace_event_list, list) {
                if (e->type != last + 1)
                        break;
                last++;
        }

        /* Did we used up all 65 thousand events??? */
        if ((last + 1) > FTRACE_MAX_EVENT)
                return 0;

        *list = &e->list;
        return last + 1;
}

[...]

                struct list_head *list;

                if (next_event_type > FTRACE_MAX_EVENT) {

                        event->type = trace_search_list(&list);
                        if (!event->type)
                                goto out;

                } else {

                        event->type = next_event_type++;
                        list = &ftrace_event_list;
                }

                if (WARN_ON(ftrace_find_event(event->type)))
                        goto out;

                list_add_tail(&event->list, list);

The caller is:

	struct list_head *list;

	if () {
		event->type = trace_seach_list(&list);
	} else {
		[...]
		list = &ftrace_event_list;
	}

This code shows that list is initialized by either trace_seach_list() or set manually.

Thus, my change makes trace_search_list always initialize the list variable. Thus if trace_search_list() is used someplace else, it will not cause us this error again.

If gcc can not figure out that trace_search_list initializes the code (from the original code), the

	struct list_head *list = NULL;

will always be performed, because gcc thinks that's the only way to guarantee that it will be initialized.

My solution, gcc can easily see that trace_search_list will always initialize list, and will not set it needlessly to NULL.

-- Steve