Skip to contentSkip to conversation

gcov: add Clang support

PATCH Superseded Tri Vo 3 participants 12 messages Jan 22, 2019 – Mar 12, 2019

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Tri Vo

author
#0 lore raw

v3 cover gcov: add Clang support

This patch series adds Clang supoprt for gcov.

Patch 1 refactors existing code in preparation for Clang support. Patch 2 implements necessary LLVM runtime hooks and gcov kernel interfaces. Patch 3 updates documentation.

Greg Hackmann (2):
gcov: Clang: move common GCC code into gcc_base.c
gcov: Clang support

Tri Vo (1):
gcov: docs: add a note on GCC vs Clang differences

 Documentation/dev-tools/gcov.rst |  18 +-
 kernel/gcov/Kconfig              |   3 +-
 kernel/gcov/Makefile             |   5 +-
 kernel/gcov/base.c               |  84 +----
 kernel/gcov/clang.c              | 555 +++++++++++++++++++++++++++++++
 kernel/gcov/gcc_base.c           |  86 +++++
 kernel/gcov/gcov.h               |   3 +
 7 files changed, 665 insertions(+), 89 deletions(-)
 create mode 100644 kernel/gcov/clang.c
 create mode 100644 kernel/gcov/gcc_base.c

v2:
- Reorganized config dependencies, as per Masahiro.

v3:
- Squashed patches 2-4 of v2, as per Nick, Masahiro, and Peter. Addressed comments by Peter:
- Moved __gcov_exit() to gcc_base.c
- Added missing header to gcc_base.c
- Removed unnecessary boundary checks in gcov_info_add().
- Changed counters' allocation to use vmalloc().
- Added check for failed allocation of filename.
- Changed list_for_each_entry_safe to list_for_each_entry when traversing without modifying.
- Updated Documentation/dev-tools/gcov.rst

--
2.20.1.321.g9e740568ce-goog

author
#1 lore raw

PATCH v3 1/3 gcov: Clang: move common GCC code into gcc_base.c

From: Greg Hackmann <[email protected]>

base.c contains a few callbacks specific to GCC's gcov implementation. Move these into their own module in preparation for Clang support.

---
 kernel/gcov/Makefile   |  4 +-
 kernel/gcov/base.c     | 84 +----------------------------------------
 kernel/gcov/gcc_base.c | 86 ++++++++++++++++++++++++++++++++++++++++++
 kernel/gcov/gcov.h     |  3 ++
 4 files changed, 93 insertions(+), 84 deletions(-)
 create mode 100644 kernel/gcov/gcc_base.c
kernel/gcov/Makefile +2 -2
index ff06d64df397..45431ed679d1 100644
@@ -2,5 +2,5 @@
 ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
 
 obj-y := base.o fs.o
-obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_3_4.o
-obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_4_7.o
+obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_base.o gcc_3_4.o
+obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_base.o gcc_4_7.o
kernel/gcov/base.c +2 -82

90 lines not on this page · raw message

kernel/gcov/gcc_base.c new file +86 -0

86 lines not on this page · raw message

kernel/gcov/gcov.h +3 -0
index de118ad4a024..0ecf1d664ec3 100644
@@ -83,4 +83,7 @@ struct gcov_link {
 };
 extern const struct gcov_link gcov_link[];
 
+extern int gcov_events_enabled;
+extern struct mutex gcov_lock;
+
 #endif /* GCOV_H */
Signature · 1 line
2.20.1.321.g9e740568ce-goog
maintainer Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] gcov: Clang: move common GCC code into gcc_base.c

Quoted 5 lines · Tri Vo

From: Greg Hackmann <[email protected]>

base.c contains a few callbacks specific to GCC's gcov implementation. Move these into their own module in preparation for Clang support.

Quoted 7 lines

---

Looks good!

Signature · 2 lines
Peter Oberparleiter
Linux on Z Development - IBM Germany
author
#3 lore raw

PATCH v3 2/3 gcov: Clang support

In reply to Tri Vo

From: Greg Hackmann <[email protected]>

LLVM uses profiling data that's deliberately similar to GCC, but has a very different way of exporting that data. LLVM calls llvm_gcov_init() once per module, and provides a couple of callbacks that we can use to ask for more data.

We care about the "writeout" callback, which in turn calls back into compiler-rt/this module to dump all the gathered coverage data to disk:

llvm_gcda_start_file()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
[... repeats for each function ...]
llvm_gcda_summary_info()
llvm_gcda_end_file()

This design is much more stateless and unstructured than gcc's, and is intended to run at process exit. This forces us to keep some local state about which module we're dealing with at the moment. On the other hand, it also means we don't depend as much on how LLVM represents profiling data internally.

See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more details on how this works, particularly GCOVProfiler::emitProfileArcs(), GCOVProfiler::insertCounterWriteout(), and GCOVProfiler::insertFlush().

---
 kernel/gcov/Kconfig  |   3 +-
 kernel/gcov/Makefile |   1 +
 kernel/gcov/clang.c  | 555 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 558 insertions(+), 1 deletion(-)
 create mode 100644 kernel/gcov/clang.c
kernel/gcov/Kconfig +2 -1
index 1e3823fa799b..f71c1adcff31 100644
@@ -53,6 +53,7 @@ config GCOV_PROFILE_ALL
 choice
 	prompt "Specify GCOV format"
 	depends on GCOV_KERNEL
+	depends on CC_IS_GCC
 	---help---
 	The gcov format is usually determined by the GCC version, and the
 	default is chosen according to your GCC version. However, there are
@@ -62,7 +63,7 @@ choice
 
 config GCOV_FORMAT_3_4
 	bool "GCC 3.4 format"
-	depends on CC_IS_GCC && GCC_VERSION < 40700
+	depends on GCC_VERSION < 40700
 	---help---
 	Select this option to use the format defined by GCC 3.4.
 
kernel/gcov/Makefile +1 -0
index 45431ed679d1..d66a74b0f100 100644
@@ -4,3 +4,4 @@ ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
 obj-y := base.o fs.o
 obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_base.o gcc_3_4.o
 obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_base.o gcc_4_7.o
+obj-$(CONFIG_CC_IS_CLANG) += clang.o
kernel/gcov/clang.c new file +555 -0

555 lines not on this page · raw message

Signature · 1 line
2.20.1.321.g9e740568ce-goog
maintainer Reviewed-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] gcov: Clang support

Quoted 46 lines · Tri Vo

From: Greg Hackmann <[email protected]>

LLVM uses profiling data that's deliberately similar to GCC, but has a very different way of exporting that data. LLVM calls llvm_gcov_init() once per module, and provides a couple of callbacks that we can use to ask for more data.

We care about the "writeout" callback, which in turn calls back into compiler-rt/this module to dump all the gathered coverage data to disk:

llvm_gcda_start_file()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
[... repeats for each function ...]
llvm_gcda_summary_info()
llvm_gcda_end_file()

This design is much more stateless and unstructured than gcc's, and is intended to run at process exit. This forces us to keep some local state about which module we're dealing with at the moment. On the other hand, it also means we don't depend as much on how LLVM represents profiling data internally.

See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more details on how this works, particularly GCOVProfiler::emitProfileArcs(), GCOVProfiler::insertCounterWriteout(), and GCOVProfiler::insertFlush().

---
 kernel/gcov/Kconfig  |   3 +-
 kernel/gcov/Makefile |   1 +
 kernel/gcov/clang.c  | 555 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 558 insertions(+), 1 deletion(-)
 create mode 100644 kernel/gcov/clang.c

[...]

Quoted 6 lines
kernel/gcov/clang.c new file +0 -0
new file mode 100644
index 000000000000..f9acdee3d578
@@ -0,0 +1,555 @@

[...]

kernel/gcov/clang.c line 56

Quoted 10 lines
kernel/gcov/clang.c new file +10 -0
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/printk.h>
+#include <linux/ratelimit.h>
+#include <linux/seq_file.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>
+#include "gcov.h"
+
+#define GCOV_TAG_FUNCTION_LENGTH	3

This define seems to be unused and should be removed.

[...]

kernel/gcov/clang.c line 216

Quoted 9 lines
kernel/gcov/clang.c new file +9 -0
+/**
+ * gcov_info_unlink - unlink/remove profiling data set from the list
+ * @prev: previous profiling data set
+ * @info: profiling data set
+ */
+void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
+{
+	if (info)
+		list_del(&info->head);

The if-clause can be removed since @info is always non-NULL.

[...]

kernel/gcov/clang.c line 274

Quoted 4 lines
kernel/gcov/clang.c new file +4 -0
+static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
+{
+	size_t cv_size; /* counter values size */
+

This empty line should be removed.

[...]

Rest looks good! With these minor changes applied:

Signature · 2 lines
Peter Oberparleiter
Linux on Z Development - IBM Germany
author
#5 lore raw

PATCH v3 3/3 gcov: docs: add a note on GCC vs Clang differences

In reply to Tri Vo

Document some things of note to gcov users:
1. GCC gcov and Clang llvm-cov tools are not compatible.
2. The use of GCC vs Clang is transparent at build-time.

---
 Documentation/dev-tools/gcov.rst | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
Documentation/dev-tools/gcov.rst +14 -4
index 69a7d90c320a..46aae52a41d0 100644
@@ -34,10 +34,6 @@ Configure the kernel with::
         CONFIG_DEBUG_FS=y
         CONFIG_GCOV_KERNEL=y
 
-select the gcc's gcov format, default is autodetect based on gcc version::
-
-        CONFIG_GCOV_FORMAT_AUTODETECT=y
-
 and to get coverage data for the entire kernel::
 
         CONFIG_GCOV_PROFILE_ALL=y
@@ -169,6 +165,20 @@ b) gcov is run on the BUILD machine
       [user@build] gcov -o /tmp/coverage/tmp/out/init main.c
 
 
+Note on compilers
+-----------------
+
+GCC and LLVM gcov tools are not necessarily compatible. Use gcov_ to work with
+GCC-generated .gcno and .gcda files, and use llvm-cov_ for Clang.
+
+.. _gcov: http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
+.. _llvm-cov: https://llvm.org/docs/CommandGuide/llvm-cov.html
+
+Build differences between GCC and Clang gcov are handled by Kconfig. It
+automatically selects the appropriate gcov format depending on the detected
+toolchain.
+
+
 Troubleshooting
 ---------------
 
Signature · 1 line
2.20.1.321.g9e740568ce-goog
Reviewed-by on v3 3/3
#6 lore raw

Re: [PATCH v3 3/3] gcov: docs: add a note on GCC vs Clang differences

Quoted 6 lines · Tri Vo

Document some things of note to gcov users:
1. GCC gcov and Clang llvm-cov tools are not compatible.
2. The use of GCC vs Clang is transparent at build-time.

Thanks for adding this note (I made the mistake myself when testing out these patches that the coverage format is slightly different). And good find removing the documentation on the now obsolete config.

Quoted 43 lines
---
 Documentation/dev-tools/gcov.rst | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
Documentation/dev-tools/gcov.rst +14 -5
index 69a7d90c320a..46aae52a41d0 100644
@@ -34,10 +34,6 @@ Configure the kernel with::
         CONFIG_DEBUG_FS=y
         CONFIG_GCOV_KERNEL=y
 
-select the gcc's gcov format, default is autodetect based on gcc version::
-
-        CONFIG_GCOV_FORMAT_AUTODETECT=y
-
 and to get coverage data for the entire kernel::
 
         CONFIG_GCOV_PROFILE_ALL=y
@@ -169,6 +165,20 @@ b) gcov is run on the BUILD machine
       [user@build] gcov -o /tmp/coverage/tmp/out/init main.c
 
 
+Note on compilers
+-----------------
+
+GCC and LLVM gcov tools are not necessarily compatible. Use gcov_ to work with
+GCC-generated .gcno and .gcda files, and use llvm-cov_ for Clang.
+
+.. _gcov: http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
+.. _llvm-cov: https://llvm.org/docs/CommandGuide/llvm-cov.html
+
+Build differences between GCC and Clang gcov are handled by Kconfig. It
+automatically selects the appropriate gcov format depending on the detected
+toolchain.
+
+
 Troubleshooting
 ---------------
 
--

2.20.1.321.g9e740568ce-goog

Signature · 2 lines
Thanks,
~Nick Desaulniers
maintainer Reviewed-by on v3 3/3
#7 lore raw

Re: [PATCH v3 3/3] gcov: docs: add a note on GCC vs Clang differences

In reply to Tri Vo
Quoted 3 lines · Tri Vo

Document some things of note to gcov users:
1. GCC gcov and Clang llvm-cov tools are not compatible.
2. The use of GCC vs Clang is transparent at build-time.

For completeness sake please also add something like the following to the commit message:

Also adjust the documentation to account for the removal of config symbol CONFIG_GCOV_FORMAT_AUTODETECT by commit 6a61b70b43c9 ("gcov: remove CONFIG_GCOV_FORMAT_AUTODETECT").

Quoted 5 lines
---
 Documentation/dev-tools/gcov.rst | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
Signature · 2 lines
Peter Oberparleiter
Linux on Z Development - IBM Germany
#8 lore raw
In reply to Tri Vo
Quoted 40 lines · Tri Vo

This patch series adds Clang supoprt for gcov.

Patch 1 refactors existing code in preparation for Clang support. Patch 2 implements necessary LLVM runtime hooks and gcov kernel interfaces. Patch 3 updates documentation.

Greg Hackmann (2):
gcov: Clang: move common GCC code into gcc_base.c
gcov: Clang support

Tri Vo (1):
gcov: docs: add a note on GCC vs Clang differences

 Documentation/dev-tools/gcov.rst |  18 +-
 kernel/gcov/Kconfig              |   3 +-
 kernel/gcov/Makefile             |   5 +-
 kernel/gcov/base.c               |  84 +----
 kernel/gcov/clang.c              | 555 +++++++++++++++++++++++++++++++
 kernel/gcov/gcc_base.c           |  86 +++++
 kernel/gcov/gcov.h               |   3 +
 7 files changed, 665 insertions(+), 89 deletions(-)
 create mode 100644 kernel/gcov/clang.c
 create mode 100644 kernel/gcov/gcc_base.c

v2:
- Reorganized config dependencies, as per Masahiro.

v3:
- Squashed patches 2-4 of v2, as per Nick, Masahiro, and Peter. Addressed comments by Peter:
- Moved __gcov_exit() to gcc_base.c
- Added missing header to gcc_base.c
- Removed unnecessary boundary checks in gcov_info_add().
- Changed counters' allocation to use vmalloc().
- Added check for failed allocation of filename.
- Changed list_for_each_entry_safe to list_for_each_entry when traversing without modifying.
- Updated Documentation/dev-tools/gcov.rst

Quoted 3 lines

--
2.20.1.321.g9e740568ce-goog

Bumping this thread for review.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#9 lore raw
Quoted 46 lines · Nick Desaulniers
Quoted 40 lines · Tri Vo

This patch series adds Clang supoprt for gcov.

Patch 1 refactors existing code in preparation for Clang support. Patch 2 implements necessary LLVM runtime hooks and gcov kernel interfaces. Patch 3 updates documentation.

Greg Hackmann (2):
gcov: Clang: move common GCC code into gcc_base.c
gcov: Clang support

Tri Vo (1):
gcov: docs: add a note on GCC vs Clang differences

 Documentation/dev-tools/gcov.rst |  18 +-
 kernel/gcov/Kconfig              |   3 +-
 kernel/gcov/Makefile             |   5 +-
 kernel/gcov/base.c               |  84 +----
 kernel/gcov/clang.c              | 555 +++++++++++++++++++++++++++++++
 kernel/gcov/gcc_base.c           |  86 +++++
 kernel/gcov/gcov.h               |   3 +
 7 files changed, 665 insertions(+), 89 deletions(-)
 create mode 100644 kernel/gcov/clang.c
 create mode 100644 kernel/gcov/gcc_base.c

v2:
- Reorganized config dependencies, as per Masahiro.

v3:
- Squashed patches 2-4 of v2, as per Nick, Masahiro, and Peter. Addressed comments by Peter:
- Moved __gcov_exit() to gcc_base.c
- Added missing header to gcc_base.c
- Removed unnecessary boundary checks in gcov_info_add().
- Changed counters' allocation to use vmalloc().
- Added check for failed allocation of filename.
- Changed list_for_each_entry_safe to list_for_each_entry when traversing without modifying.
- Updated Documentation/dev-tools/gcov.rst

Quoted 3 lines

--
2.20.1.321.g9e740568ce-goog

Quoted 5 lines

Bumping this thread for review.

--
Thanks,
~Nick Desaulniers

Bumping for review; KernelCI is ready to enable clang builds. This is the final build error for arm64 allyesconfig + clang.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#10 lore raw
In reply to Tri Vo
Quoted 5 lines · Tri Vo

This patch series adds Clang supoprt for gcov.

Patch 1 refactors existing code in preparation for Clang support. Patch 2 implements necessary LLVM runtime hooks and gcov kernel interfaces. Patch 3 updates documentation.

Thanks for the updates! I've provided suggestions for some minor improvements in my other review e-mails.

With those changes applied, the patch set is in my opinion ready for inclusion into the mainline kernel. My suggestion would be to re-post the resulting version while putting Andrew Morton on CC as gcov changes are typically integrated via his tree.

Also I've successfully re-tested this patch set version on s390 using GCC 7.3.0 to ensure that the existing GCC support is still working. Unfortunately I wasn't able to test the Clang version due to some compile problems on s390 (unrelated to this patch set).

If you haven't done so, I would like to suggest to run the following tests with the Clang gcov-kernel version that should catch problems in some corner cases:

1. Unload a module, then use llvm-cov on the associated coverage file.

Expectation: correct llvm-cov output including coverage of module exit code.

2. Unload a module, modify its source, re-compile it and load it again, then use llvm-cov on the associated coverage file.

Expectation: kernel message "discarding saved data", correct llvm-cov output with no coverage of module exit code.

3. Unload a module, then reset all coverage data by writing to /sys/kernel/debug/gcov/reset.

Expectation: all coverage files associated with the module are removed from debugfs.

Signature · 2 lines
Peter Oberparleiter
Linux on Z Development - IBM Germany
author
#11 lore raw
Quoted 33 lines · Peter Oberparleiter
Quoted 5 lines · Tri Vo

This patch series adds Clang supoprt for gcov.

Patch 1 refactors existing code in preparation for Clang support. Patch 2 implements necessary LLVM runtime hooks and gcov kernel interfaces. Patch 3 updates documentation.

Thanks for the updates! I've provided suggestions for some minor improvements in my other review e-mails.

With those changes applied, the patch set is in my opinion ready for inclusion into the mainline kernel. My suggestion would be to re-post the resulting version while putting Andrew Morton on CC as gcov changes are typically integrated via his tree.

Also I've successfully re-tested this patch set version on s390 using GCC 7.3.0 to ensure that the existing GCC support is still working. Unfortunately I wasn't able to test the Clang version due to some compile problems on s390 (unrelated to this patch set).

If you haven't done so, I would like to suggest to run the following tests with the Clang gcov-kernel version that should catch problems in some corner cases:

1. Unload a module, then use llvm-cov on the associated coverage file.

Expectation: correct llvm-cov output including coverage of module exit code.

2. Unload a module, modify its source, re-compile it and load it again, then use llvm-cov on the associated coverage file.

Quoted 8 lines

Expectation: kernel message "discarding saved data", correct llvm-cov output with no coverage of module exit code.

3. Unload a module, then reset all coverage data by writing to /sys/kernel/debug/gcov/reset.

Expectation: all coverage files associated with the module are removed from debugfs.

Thanks for the suggested test cases! The current patchset doesn't seem to handle module loading/unloading correctly. I'll fix that in a follow-up.