Skip to contentSkip to conversation

gcov: add gcov profiling infrastructure

PATCH In review Peter Oberparleiter 3 participants 11 messages Feb 3, 2009 – Feb 26, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 3/4 gcov: add gcov profiling infrastructure

From: Peter Oberparleiter <[email protected]>

Enable the use of GCC's coverage testing tool gcov [1] with the Linux kernel. gcov may be useful for:

* debugging (has this code been reached at all?)
* test improvement (how do I change my test to cover these lines?)
* minimizing kernel configurations (do I need this option if the associated code is never run?)

The profiling patch incorporates the following changes:

* change kbuild to include profiling flags
* provide functions needed by profiling code
* present profiling data as files in debugfs

Note that on some architectures, enabling gcc's profiling option "-fprofile-arcs" for the entire kernel may trigger compile/link/ run-time problems, some of which are caused by toolchain bugs and others which require adjustment of architecture code.

For this reason profiling the entire kernel is initially restricted to those architectures for which it is known to work without changes. This restriction can be lifted once an architecture has been tested and found compatible with gcc's profiling. Profiling of single files or directories is still available on all platforms (see config help text).

[1] http://gcc.gnu.org/onlinedocs/gcc/Gcov.html

---

 Documentation/gcov.txt              |  246 ++++++++++++++
 Documentation/kernel-parameters.txt |    7 
 Makefile                            |   11 
 arch/Kconfig                        |    1 
 init/main.c                         |    8 
 kernel/Makefile                     |    1 
 kernel/gcov/Kconfig                 |   48 ++
 kernel/gcov/Makefile                |    3 
 kernel/gcov/base.c                  |  145 ++++++++
 kernel/gcov/fs.c                    |  620 ++++++++++++++++++++++++++++++++++++
 kernel/gcov/gcc_3_4.c               |  419 ++++++++++++++++++++++++
 kernel/gcov/gcov.h                  |  128 +++++++
 scripts/Makefile.lib                |    9 
 13 files changed, 1641 insertions(+), 5 deletions(-)
Documentation/gcov.txt new file +246 -0
Index: linux-2.6.29-rc3/Documentation/gcov.txt
===================================================================
@@ -0,0 +1,246 @@
+Using gcov with the Linux kernel
+================================
+
+1. Introduction
+2. Preparation
+3. Customization
+4. Files
+5. Modules
+6. Separated build and test machines
+7. Troubleshooting
+Appendix A: sample script: gather_on_build.sh
+Appendix B: sample script: gather_on_test.sh
+
+
+1. Introduction
+===============
+
+gcov profiling kernel support enables the use of GCC's coverage testing
+tool gcov [1] with the Linux kernel. Coverage data of a running kernel
+is exported in gcov-compatible format via the "gcov" debugfs directory.
+To get coverage data for a specific file, change to the kernel build
+directory and use gcov with the -o option as follows (requires root):
+
+# cd /tmp/linux-out
+# gcov -o /sys/kernel/debug/gcov/tmp/linux-out/kernel spinlock.c
+
+This will create source code files annotated with execution counts
+in the current directory. In addition, graphical gcov front-ends such
+as lcov [2] can be used to automate the process of collecting data
+for the entire kernel and provide coverage overviews in HTML format.
+
+Possible uses:
+
+* debugging (has this line been reached at all?)
+* test improvement (how do I change my test to cover these lines?)
+* minimizing kernel configurations (do I need this option if the
+  associated code is never run?)
+
+--
+
+[1] http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
+[2] http://ltp.sourceforge.net/coverage/lcov.php
+
+
+2. Preparation
+==============
+
+Configure the kernel with:
+
+        CONFIG_DEBUGFS=y
+        CONFIG_GCOV_KERNEL=y
+
+and to get coverage data for the entire kernel:
+
+        CONFIG_GCOV_PROFILE_ALL=y
+
+Note that kernels compiled with profiling flags will be significantly
+larger and run slower. Also CONFIG_GCOV_PROFILE_ALL may not be supported
+on all architectures.
+
+Profiling data will only become accessible once debugfs has been
+mounted:
+
+        mount -t debugfs none /sys/kernel/debug
+
+
+3. Customization
+================
+
+To enable profiling for specific files or directories, add a line
+similar to the following to the respective kernel Makefile:
+
+        For a single file (e.g. main.o):
+                GCOV_PROFILE_main.o := y
+
+        For all files in one directory:
+                GCOV_PROFILE := y
+
+To exclude files from being profiled even when CONFIG_GCOV_PROFILE_ALL
+is specified, use:
+
+                GCOV_PROFILE_main.o := n
+        and:
+                GCOV_PROFILE := n
+
+Only files which are linked to the main kernel image or are compiled as
+kernel modules are supported by this mechanism.
+
+
+4. Files
+========
+
+The gcov kernel support creates the following files in debugfs:
+
+        /sys/kernel/debug/gcov
+                Parent directory for all gcov-related files.
+
+        /sys/kernel/debug/gcov/reset
+                Global reset file: resets all coverage data to zero when
+                written to.
+
+        /sys/kernel/debug/gcov/path/to/compile/dir/file.gcda
+                The actual gcov data file as understood by the gcov
+                tool. Resets file coverage data to zero when written to.
+
+        /sys/kernel/debug/gcov/path/to/compile/dir/file.gcno
+                Symbolic link to a static data file required by the gcov
+                tool. This file is generated by gcc when compiling with
+                option -ftest-coverage.
+
+
+5. Modules
+==========
+
+Kernel modules may contain cleanup code which is only run during
+module unload time. The gcov mechanism provides a means to collect
+coverage data for such code by keeping a copy of the data associated
+with the unloaded module. This data remains available through debugfs.
+Once the module is loaded again, the associated coverage counters are
+initialized with the data from its previous instantiation.
+
+This behavior can be deactivated by specifying the gcov_persist kernel
+parameter:
+
+        gcov_persist=0
+
+At run-time, a user can also choose to discard data for an unloaded
+module by writing to its data file or the global reset file.
+
+
+6. Separated build and test machines
+====================================
+
+The gcov kernel profiling infrastructure is designed to work out-of-the
+box for setups where kernels are built and run on the same machine. In
+cases where the kernel runs on a separate machine, special preparations
+must be made, depending on where the gcov tool is used:
+
+a) gcov is run on the TEST machine
+
+The gcov tool version on the test machine must be compatible with the
+gcc version used for kernel build. Also the following files need to be
+copied from build to test machine:
+
+from the source tree:
+  - all C source files + headers
+
+from the build tree:
+  - all C source files + headers
+  - all .gcda and .gcno files
+  - all links to directories
+
+It is important to note that these files need to be placed into the
+exact same file system location on the test machine as on the build
+machine. If any of the path components is symbolic link, the actual
+directory needs to be used instead (due to make's CURDIR handling).
+
+b) gcov is run on the BUILD machine
+
+The following files need to be copied after each test case from test
+to build machine:
+
+from the gcov directory in sysfs:
+  - all .gcda files
+  - all links to .gcno files
+
+These files can be copied to any location on the build machine. gcov
+must then be called with the -o option pointing to that directory.
+
+Example directory setup on the build machine:
+
+  /tmp/linux:    kernel source tree
+  /tmp/out:      kernel build directory as specified by make O=
+  /tmp/coverage: location of the files copied from the test machine
+
+  [user@build] cd /tmp/out
+  [user@build] gcov -o /tmp/coverage/tmp/out/init main.c
+
+
+7. Troubleshooting
+==================
+
+Problem:  Compilation aborts during linker step.
+Cause:    Profiling flags are specified for source files which are not
+          linked to the main kernel or which are linked by a custom
+          linker procedure.
+Solution: Exclude affected source files from profiling by specifying
+          GCOV_PROFILE := n or GCOV_PROFILE_basename.o := n in the
+          corresponding Makefile.
+
+
+Appendix A: gather_on_build.sh
+==============================
+
+Sample script to gather coverage meta files on the build machine
+(see 6a):
+
+#!/bin/bash
+
+KSRC=$1
+KOBJ=$2
+DEST=$3
+
+if [ -z "$KSRC" ] || [ -z "$KOBJ" ] || [ -z "$DEST" ]; then
+  echo "Usage: $0 <ksrc directory> <kobj directory> <output.tar.gz>" >&2
+  exit 1
+fi
+
+KSRC=$(cd $KSRC; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
+KOBJ=$(cd $KOBJ; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
+
+find $KSRC $KOBJ \( -name '*.gcno' -o -name '*.[ch]' -o -type l \) -a \
+                 -perm /u+r,g+r | tar cfz $DEST -P -T -
+
+if [ $? -eq 0 ] ; then
+  echo "$DEST successfully created, copy to test system and unpack with:"
+  echo "  tar xfz $DEST -P"
+else
+  echo "Could not create file $DEST"
+fi
+
+
+Appendix B: gather_on_test.sh
+=============================
+
+Sample script to gather coverage data files on the test machine
+(see 6b):
+
+#!/bin/bash
+
+DEST=$1
+GCDA=/sys/kernel/debug/gcov
+
+if [ -z "$DEST" ] ; then
+  echo "Usage: $0 <output.tar.gz>" >&2
+  exit 1
+fi
+
+find $GCDA -name '*.gcno' -o -name '*.gcda' | tar cfz $DEST -T -
+
+if [ $? -eq 0 ] ; then
+  echo "$DEST successfully created, copy to build system and unpack with:"
+  echo "  tar xfz $DEST"
+else
+  echo "Could not create file $DEST"
+fi
Documentation/kernel-parameters.txt +7 -0
Index: linux-2.6.29-rc3/Documentation/kernel-parameters.txt
===================================================================
@@ -42,6 +42,7 @@ parameter is applicable:
 	EFI	EFI Partitioning (GPT) is enabled
 	EIDE	EIDE/ATAPI support is enabled.
 	FB	The frame buffer device is enabled.
+	GCOV	GCOV profiling is enabled.
 	HW	Appropriate hardware is enabled.
 	IA-64	IA-64 architecture is enabled.
 	IOSCHED	More than one I/O scheduler is enabled.
@@ -778,6 +779,12 @@ and is between 256 and 4096 characters. 
 			Format: off | on
 			default: on
 
+	gcov_persist=	[GCOV] When non-zero (default), profiling data for
+			kernel modules is saved and remains accessible via
+			debugfs, even when the module is unloaded/reloaded.
+			When zero, profiling data is discarded and associated
+			debugfs files are removed at module unload time.
+
 	gdth=		[HW,SCSI]
 			See header of drivers/scsi/gdth.c.
 
Makefile +6 -5
Index: linux-2.6.29-rc3/Makefile
===================================================================
@@ -333,6 +333,7 @@ AFLAGS_MODULE   = $(MODFLAGS)
 LDFLAGS_MODULE  =
 CFLAGS_KERNEL	=
 AFLAGS_KERNEL	=
+CFLAGS_GCOV	= -fprofile-arcs -ftest-coverage
 
 
 # Use LINUXINCLUDE when you must reference the include/ directory.
@@ -359,7 +360,7 @@ export CPP AR NM STRIP OBJCOPY OBJDUMP M
 export HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
 
 export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
-export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
+export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE CFLAGS_GCOV
 export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
 
 # When compiling out-of-tree modules, put MODVERDIR in the module
@@ -1202,8 +1203,8 @@ clean: archclean $(clean-dirs)
 		\( -name '*.[oas]' -o -name '*.ko' -o -name '.*.cmd' \
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
 		-o -name '*.symtypes' -o -name 'modules.order' \
-		-o -name 'Module.markers' -o -name '.tmp_*.o.*' \) \
-		-type f -print | xargs rm -f
+		-o -name 'Module.markers' -o -name '.tmp_*.o.*' \
+		-o -name '*.gcno' \) -type f -print | xargs rm -f
 
 # mrproper - Delete all generated files, including .config
 #
@@ -1405,8 +1406,8 @@ clean: $(clean-dirs)
 	$(call cmd,rmfiles)
 	@find $(KBUILD_EXTMOD) $(RCS_FIND_IGNORE) \
 		\( -name '*.[oas]' -o -name '*.ko' -o -name '.*.cmd' \
-		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \) \
-		-type f -print | xargs rm -f
+		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
+		-o -name '*.gcno' \) -type f -print | xargs rm -f
 
 help:
 	@echo  '  Building external modules.'
arch/Kconfig +1 -0
Index: linux-2.6.29-rc3/arch/Kconfig
===================================================================
@@ -106,3 +106,4 @@ config HAVE_CLK
 	  The <linux/clk.h> calls support software clock gating and
 	  thus are a key power management tool on many systems.
 
+source "kernel/gcov/Kconfig"
init/main.c +8 -0
Index: linux-2.6.29-rc3/init/main.c
===================================================================
@@ -75,6 +75,14 @@
 #include <asm/smp.h>
 #endif
 
+#ifdef CONFIG_GCOV_KERNEL
+
+#if (__GNUC__ < 3) || (__GNUC__ == 3) && (__GNUC_MINOR__ < 4)
+#error "GCOV profiling support for gcc versions below 3.4 not included"
+#endif /* __GNUC__ */
+
+#endif /* CONFIG_GCOV_KERNEL */
+
 static int kernel_init(void *);
 
 extern void init_IRQ(void);
kernel/Makefile +1 -0
Index: linux-2.6.29-rc3/kernel/Makefile
===================================================================
@@ -69,6 +69,7 @@ obj-$(CONFIG_STOP_MACHINE) += stop_machi
 obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
 obj-$(CONFIG_AUDIT) += audit.o auditfilter.o
 obj-$(CONFIG_AUDITSYSCALL) += auditsc.o
+obj-$(CONFIG_GCOV_KERNEL) += gcov/
 obj-$(CONFIG_AUDIT_TREE) += audit_tree.o
 obj-$(CONFIG_KPROBES) += kprobes.o
 obj-$(CONFIG_KGDB) += kgdb.o
kernel/gcov/Kconfig new file +48 -0

48 lines not on this page · raw message

kernel/gcov/Makefile new file +3 -0
Index: linux-2.6.29-rc3/kernel/gcov/Makefile
===================================================================
@@ -0,0 +1,3 @@
+EXTRA_CFLAGS := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
+
+obj-$(CONFIG_GCOV_KERNEL) := base.o fs.o gcc_3_4.o
kernel/gcov/base.c new file +145 -0

145 lines not on this page · raw message

kernel/gcov/fs.c new file +620 -0

620 lines not on this page · raw message

kernel/gcov/gcc_3_4.c new file +419 -0

419 lines not on this page · raw message

kernel/gcov/gcov.h new file +128 -0

128 lines not on this page · raw message

scripts/Makefile.lib +9 -0
Index: linux-2.6.29-rc3/scripts/Makefile.lib
===================================================================
@@ -111,6 +111,15 @@ _a_flags       = $(KBUILD_CPPFLAGS) $(KB
                  $(asflags-y) $(AFLAGS_$(basetarget).o)
 _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F))
 
+# Enable gcov profiling flags for a file, directory or for all files depending
+# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
+# (in this order)
+ifeq ($(CONFIG_GCOV_KERNEL),y)
+_c_flags += $(if $(patsubst n%,, \
+		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
+		$(CFLAGS_GCOV))
+endif
+
 # If building the kernel in a separate objtree expand all occurrences
 # of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
 
#1 lore raw
Quoted 34 lines · Peter Oberparleiter

From: Peter Oberparleiter <[email protected]>

Enable the use of GCC's coverage testing tool gcov [1] with the Linux kernel. gcov may be useful for:

* debugging (has this code been reached at all?)
* test improvement (how do I change my test to cover these lines?)
* minimizing kernel configurations (do I need this option if the associated code is never run?)

The profiling patch incorporates the following changes:

* change kbuild to include profiling flags
* provide functions needed by profiling code
* present profiling data as files in debugfs

Note that on some architectures, enabling gcc's profiling option "-fprofile-arcs" for the entire kernel may trigger compile/link/ run-time problems, some of which are caused by toolchain bugs and others which require adjustment of architecture code.

For this reason profiling the entire kernel is initially restricted to those architectures for which it is known to work without changes. This restriction can be lifted once an architecture has been tested and found compatible with gcc's profiling. Profiling of single files or directories is still available on all platforms (see config help text).

[1] http://gcc.gnu.org/onlinedocs/gcc/Gcov.html

---

Quoted 14 lines
 Documentation/gcov.txt              |  246 ++++++++++++++
 Documentation/kernel-parameters.txt |    7 
 Makefile                            |   11 
 arch/Kconfig                        |    1 
 init/main.c                         |    8 
 kernel/Makefile                     |    1 
 kernel/gcov/Kconfig                 |   48 ++
 kernel/gcov/Makefile                |    3 
 kernel/gcov/base.c                  |  145 ++++++++
 kernel/gcov/fs.c                    |  620 ++++++++++++++++++++++++++++++++++++
 kernel/gcov/gcc_3_4.c               |  419 ++++++++++++++++++++++++
 kernel/gcov/gcov.h                  |  128 +++++++
 scripts/Makefile.lib                |    9 
 13 files changed, 1641 insertions(+), 5 deletions(-)

Nice tool - with one general observation: i'd really prefer if this was properly integrated with ftrace.

In particular the per object file controls are nice - those might be useful to seed ftrace filters as well.

So there's good synergy possible and we should reach that synergy before this goes upstream instead of putting it into a separate debugfs position with incompatible flags and concepts. Especially since user-space tools will pick up the gcov data so this gets hardcoded compatibility-wise very quickly and changing it after the fact will be harder.

Yes, it's more work and it means extending both the ftrace code and the gcov code - but it's very much worth it.

Ingo

#2 lore raw
Quoted 6 lines · Ingo Molnar
Quoted 2 lines · Peter Oberparleiter

Enable the use of GCC's coverage testing tool gcov [1] with the Linux kernel. gcov may be useful for:

Nice tool - with one general observation: i'd really prefer if this was properly integrated with ftrace.

I've tried to read up on ftrace and here's what I understood ftrace and gcov have in common:

- works by inserting probes
- probes gather information when called
- data gathered is presented to user space

As for differences:

ftrace:
- traces function calls
- probing code is provided by the kernel
- presents textual data in a single file
- can enable/disable tracing as a whole or per function
- presents controls to choose a single tracer to be active

gcov:
- traces basic block transitions
- probing code is provided by gcc
- data is presented as binary file + symbolic link per compiled object file (including directory structure)
- probing code cannot be disabled, only data can be reset

Quoted 2 lines

In particular the per object file controls are nice - those might be useful to seed ftrace filters as well.

If I understand you correctly, you want to be able to enable/disable tracing per compiled object file, possibly also choose active tracers per file. The problem I see with this approach is that with gcov, controls for a file are created when the gcc generated constructor function for that file is called. They also have preset names (.gcda and .gcno files).

I don't think it's feasible to enable gcov compile flags for the entire kernel when only a non-gcov tracer needs to be enabled. So for that, a new mechanism is needed which registers all object files. In can imagine that such a mechanism may be installed using some kbuild magic, but then there's still no guarantee that controls are provided for the same set of files that are registered with the gcov tracer.

Quoted 7 lines

So there's good synergy possible and we should reach that synergy before this goes upstream instead of putting it into a separate debugfs position with incompatible flags and concepts. Especially since user-space tools will pick up the gcov data so this gets hardcoded compatibility-wise very quickly and changing it after the fact will be harder.

From what I've understood about the ftrace mechanism, its currently far

from the point where another tracing mechanism based on a substantially different design approach can be integrated without a major interface rework.

As far as user-space tools are concerned - the gcov-kernel support is designed to be used with the gcov tool (which has been around for some time already). The only other tool which I know of that uses the data provided by the gcov-kernel support is lcov (which is also an established tool). These tools are there already, so I don't see a need to hurry with merging gcov/ftrace and whatever other mechanism might fit (oprofile and kprobes come to mind).

To summarize my point of view: if it's only about moving the gcov directory from / to /tracing then that can easily be done. If it's about extending ftrace's control and presentation file structure as well as its API to support non-function call based tracers and per-object file control and presentation then that's a very long way to go and I would rather not delay the inclusion of the gcov kernel infrastructure that much.

#3 lore raw
In reply to Peter Oberparleiter

node->parent is always NULL, causing NULL-pointer dereference in remove_node.

Could you please include something like this?

---
 kernel/gcov/fs.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
kernel/gcov/fs.c +1 -0
index 0084492..6675c82 100644
@@ -374,6 +374,7 @@ static struct gcov_node *new_node(struct gcov_node *parent,
 		return NULL;
 	}
 	init_node(node, info, name);
+	node->parent = parent;
 	/* Differentiate between gcov data file nodes and directory nodes. */
 	if (info) {
 		node->dentry = debugfs_create_file(deskew(node->name), 0600,
Signature · 1 line
1.5.6.3
#4 lore raw
Quoted 2 lines · Li Wei

node->parent is always NULL, causing NULL-pointer dereference in remove_node.

You're right. Thanks for pointing this out - not sure how this escaped testing...

Quoted 1 line

Could you please include something like this?

Since a few RCs have been released since the last gcov-kernel patch post, I'll do a full re-post with a fix similar to the one you suggested included.

Regards,
Peter Oberparleiter

#7 lore raw
In reply to Peter Oberparleiter
Quoted 9 lines · Peter Oberparleiter
(quoted diff) +9 -0
+/* write() implementation for reset file. Reset all profiling data to zero
+ * and remove ghost nodes. */
+static ssize_t reset_write(struct file *file, const char __user *addr,
+			   size_t len, loff_t *pos)
+{
+	struct gcov_node *node;
+	struct gcov_node *r;
+
+	mutex_lock(&node_lock);

Review

Quoted 10 lines
(quoted diff) +10 -0
+	list_for_each_entry_safe(node, r, &all_head, all) {
+		if (node->info)
+			gcov_info_reset(node->info);
+		else
+			remove_node(node);
+	}
+	mutex_unlock(&node_lock);
+
+	return len;
+}

The remove_node above may have deleted the node that r points at. How about replacing all_head with a leaves_head that contains all leaf nodes, and iterate the latter instead?

---
 kernel/gcov/fs.c |   19 ++++++++++---------
 1 files changed, 10 insertions(+), 9 deletions(-)
kernel/gcov/fs.c +6 -6
index 6675c82..aab3be7 100644
@@ -29,7 +29,7 @@
  * struct gcov_node - represents a debugfs entry
  * @list: list head for child node list
  * @children: child nodes
- * @all: list head for list of all nodes
+ * @leaves: list head for list of all leaf nodes
  * @parent: parent node
  * @info: associated profiling data structure if not a directory
  * @ghost: when an object file containing profiling data is unloaded we keep a
@@ -47,7 +47,7 @@
 struct gcov_node {
 	struct list_head list;
 	struct list_head children;
-	struct list_head all;
+	struct list_head leaves;
 	struct gcov_node *parent;
 	struct gcov_info *info;
 	struct gcov_info *ghost;
@@ -60,7 +60,7 @@ static const char objtree[] = OBJTREE;
 static const char srctree[] = SRCTREE;
 static struct gcov_node root_node;
 static struct dentry *reset_dentry;
-static LIST_HEAD(all_head);
+static LIST_HEAD(leaves_head);
 static DEFINE_MUTEX(node_lock);
 
 /* If non-zero, keep copies of profiling data for unloaded modules. */
@@ -206,7 +206,7 @@ static struct gcov_node *get_node_by_name(const char *name)
 	struct gcov_node *node;
 	struct gcov_info *info;
 
-	list_for_each_entry(node, &all_head, all) {
+	list_for_each_entry(node, &leaves_head, leaves) {
 		info = get_node_info(node);
 		if (info && (strcmp(info->filename, name) == 0))
 			return node;
@@ -355,7 +355,7 @@ static void init_node(struct gcov_node *node, struct gcov_info *info,
 {
 	INIT_LIST_HEAD(&node->list);
 	INIT_LIST_HEAD(&node->children);
-	INIT_LIST_HEAD(&node->all);
+	INIT_LIST_HEAD(&node->leaves);
 	node->info = info;
 	if (name)
 		strcpy(node->name, name);
@@ -389,7 +389,8 @@ static struct gcov_node *new_node(struct gcov_node *parent,
 	if (info)
 		add_links(node, parent->dentry);
 	list_add(&node->list, &parent->children);
-	list_add(&node->all, &all_head);
+	list_add(&node->leaves, &leaves_head)From e3ffddfa9af66bf6f9df400b63ae88f2ac90562c Mon Sep 17 00:00:00 2001

From: Li Wei <[email protected]>
Date: Thu, 26 Feb 2009 17:53:37 +0800
Subject: [PATCH] gcov: fix panic in reset_write

remove_node may delete the next node in all_head. Replace the all_head with a list of all leaf nodes, and process leaves_head in reset_write. ;
+ list_del_init(&parent->leaves);

return node;
}
@@ -412,7 +413,7 @@ static void remove_links(struct gcov_node *node)
static void release_node(struct gcov_node *node)
{
list_del(&node->list);
- list_del(&node->all);
+ list_del(&node->leaves);
debugfs_remove(node->dentry);
remove_links(node);
if (node->ghost)
@@ -456,7 +457,7 @@ static ssize_t reset_write(struct file *file, const char __user *addr,
struct gcov_node *r;

mutex_lock(&node_lock);
- list_for_each_entry_safe(node, r, &all_head, all) {
+ list_for_each_entry_safe(node, r, &leaves_head, leaves) {
if (node->info)
gcov_info_reset(node->info);
else
@@ -480,7 +481,7 @@ static const struct file_operations gcov_reset_fops = {
.read = reset_read,
};

-/* Create a node for a given profiling data set and add it to all lists and +/* Create a node for a given profiling data set and add it to leaves lists and
* debugfs. Needs to be called with node_lock held. */
static void add_node(struct gcov_info *info)
{

Signature · 1 line
1.5.6.3
#5 lore raw
In reply to Peter Oberparleiter
Quoted 3 lines · Peter Oberparleiter

Since a few RCs have been released since the last gcov-kernel patch post, I'll do a full re-post with a fix similar to the one you suggested included.

Thanks! It would be great if ".."s in object pathnames could also be supported in the forth coming re-post.

I have some external modules which link with object files outside their directories. For example, a source tree looks like

module0/main.c
lib0/lib.c

and the Kbuild file has to use

main-y += ../lib0/lib.o

to link in lib.c. Current gcov-kernel does not support ".." in pathnames. Could ".."s be handled in add_node?

---
 kernel/gcov/fs.c |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
kernel/gcov/fs.c +9 -0
index aab3be7..2f92955 100644
@@ -500,6 +500,15 @@ static void add_node(struct gcov_info *info)
 		if (curr == next)
 			continue;
 		*next = 0;
+		if (!strcmp(curr, "."))
+			continue;
+		if (!strcmp(curr, "..")) {
+			if (parent != &root_node) {
+				parent = parent->parent;
+				continue;
+			} else
+				goto out_err;
+		}
 		node = get_child_by_name(parent, curr);
 		if (!node) {
 			node = new_node(parent, NULL, curr);
Signature · 1 line
1.5.6.3
#8 lore raw
In reply to Li Wei
Quoted 16 lines · Li Wei
Quoted 15 lines · Peter Oberparleiter
(quoted diff) +15 -0
+/* write() implementation for reset file. Reset all profiling data to zero
+ * and remove ghost nodes. */
+static ssize_t reset_write(struct file *file, const char __user *addr,
+			   size_t len, loff_t *pos)
+{
+	struct gcov_node *node;
+	struct gcov_node *r;
+
+	mutex_lock(&node_lock);
+	list_for_each_entry_safe(node, r, &all_head, all) {
+		if (node->info)
+			gcov_info_reset(node->info);
+		else
+			remove_node(node);
+	}
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+	mutex_unlock(&node_lock);
+
+	return len;
+}

The remove_node above may have deleted the node that r points at. How about replacing all_head with a leaves_head that contains all leaf nodes, and iterate the latter instead?

You're right again - with the fix for node->parent I'm running into kernel oopses because of this problem. But instead of creating and maintaining a new new leaf-nodes list, I'd prefer simply restarting the loop when remove_node has been called. This results in a bit more computing effort but doesn't increase data complexity any more than necessary. My proposed fix looks something like this (unless there are objections, I'll include this one in the re-post):

---
 kernel/gcov/fs.c |   21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/gcov/fs.c +14 -7
Index: linux-2.6.29-rc6/kernel/gcov/fs.c
===================================================================
@@ -453,15 +453,22 @@ static ssize_t reset_write(struct file *
 			   size_t len, loff_t *pos)
 {
 	struct gcov_node *node;
-	struct gcov_node *r;
+	int restart;
 
 	mutex_lock(&node_lock);
-	list_for_each_entry_safe(node, r, &all_head, all) {
-		if (node->info)
-			gcov_info_reset(node->info);
-		else
-			remove_node(node);
-	}
+	do {
+		restart = 0;
+		list_for_each_entry(node, &all_head, all) {
+			if (node->info)
+				gcov_info_reset(node->info);
+			else if (list_empty(&node->children)) {
+				remove_node(node);
+				/* Several nodes may have gone - restart. */
+				restart = 1;
+				break;
+			}
+		}
+	} while (restart);
 	mutex_unlock(&node_lock);
 
 	return len;
#9 lore raw
Quoted 2 lines · Peter Oberparleiter

necessary. My proposed fix looks something like this (unless there are objections, I'll include this one in the re-post):

I'd prefer a goto. But this looks nice too. Thanks! :)

Quoted 39 lines
---
 kernel/gcov/fs.c |   21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

Index: linux-2.6.29-rc6/kernel/gcov/fs.c
===================================================================

kernel/gcov/fs.c +14 -7
@@ -453,15 +453,22 @@ static ssize_t reset_write(struct file *
 			   size_t len, loff_t *pos)
 {
 	struct gcov_node *node;
-	struct gcov_node *r;
+	int restart;
 
 	mutex_lock(&node_lock);
-	list_for_each_entry_safe(node, r, &all_head, all) {
-		if (node->info)
-			gcov_info_reset(node->info);
-		else
-			remove_node(node);
-	}
+	do {
+		restart = 0;
+		list_for_each_entry(node, &all_head, all) {
+			if (node->info)
+				gcov_info_reset(node->info);
+			else if (list_empty(&node->children)) {
+				remove_node(node);
+				/* Several nodes may have gone - restart. */
+				restart = 1;
+				break;
+			}
+		}
+	} while (restart);
 	mutex_unlock(&node_lock);
 
 	return len;
#10 lore raw
Quoted 5 lines · Li Wei
Quoted 2 lines · Peter Oberparleiter

necessary. My proposed fix looks something like this (unless there are objections, I'll include this one in the re-post):

I'd prefer a goto. But this looks nice too. Thanks! :)

I usually try to prevent gotos that jump backwards, but on second thought I agree that in this case it makes the code more readable. Also a quick look confirms that similar constructs seem to be common practice within the kernel so I'll use the goto instead:

---
 kernel/gcov/fs.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/gcov/fs.c +6 -3
Index: linux-2.6.29-rc6/kernel/gcov/fs.c
===================================================================
@@ -453,14 +453,17 @@ static ssize_t reset_write(struct file *
 			   size_t len, loff_t *pos)
 {
 	struct gcov_node *node;
-	struct gcov_node *r;
 
 	mutex_lock(&node_lock);
-	list_for_each_entry_safe(node, r, &all_head, all) {
+restart:
+	list_for_each_entry(node, &all_head, all) {
 		if (node->info)
 			gcov_info_reset(node->info);
-		else
+		else if (list_empty(&node->children)) {
 			remove_node(node);
+			/* Several nodes may have gone - restart loop. */
+			goto restart;
+		}
 	}
 	mutex_unlock(&node_lock);
 
#6 lore raw
In reply to Li Wei
Quoted 7 lines · Li Wei
Quoted 3 lines · Peter Oberparleiter

Since a few RCs have been released since the last gcov-kernel patch post, I'll do a full re-post with a fix similar to the one you suggested included.

Thanks! It would be great if ".."s in object pathnames could also be supported in the forth coming re-post.

Sounds reasonable. I'll add a slightly modified patch:

---
 kernel/gcov/fs.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/gcov/fs.c +8 -0
Index: linux-2.6.29-rc6/kernel/gcov/fs.c
===================================================================
@@ -502,6 +502,14 @@ static void add_node(struct gcov_info *i
 		if (curr == next)
 			continue;
 		*next = 0;
+		if (strcmp(curr, ".") == 0)
+			continue;
+		if (strcmp(curr, "..") == 0) {
+			parent = parent->parent;
+			if (!parent)
+				goto out_err;
+			continue;
+		}
 		node = get_child_by_name(parent, curr);
 		if (!node) {
 			node = new_node(parent, NULL, curr);