All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Oberparleiter <oberpar@linux.ibm.com>
To: akpm@linux-foundation.org
Cc: torvalds@linux-foundation.org, oberpar@linux.ibm.com,
	linux-kernel@vger.kernel.org
Subject: [PATCH] gcov: fail build on gcov_info size mismatch
Date: Thu, 11 Mar 2021 14:03:28 +0100	[thread overview]
Message-ID: <20210311130328.2859337-1-oberpar@linux.ibm.com> (raw)

gcov kernel profiling works by emulating parts of GCC's libgcov in the
kernel, including a definition of struct gcov_info used by profiling
code to handle coverage data. The original definition of this data type
is not available outside of GCC's source tree, and when it changes with
new GCC versions the result may be hard-to-debug kernel failures [1].

This patch adds a compile-time check to ensure that the kernel's version
of struct gcov_info has the same length as the one used by GCC as
determined by looking at GCC's assembler output. This check should help
reduce the number of run-time failures when using gcov kernel support
with new GCC versions that include updates to struct gcov_info.

Tested with various GCC versions between 4.9 and 10, and also Clang 11.

[1] https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1891288

Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
---
 kernel/gcov/Makefile       |  9 +++++++++
 kernel/gcov/gcc_4_7.c      |  3 +++
 kernel/gcov/geninfosize.sh | 19 +++++++++++++++++++
 3 files changed, 31 insertions(+)
 create mode 100755 kernel/gcov/geninfosize.sh

diff --git a/kernel/gcov/Makefile b/kernel/gcov/Makefile
index 16f8ecc7d882..1cbeb0de944e 100644
--- a/kernel/gcov/Makefile
+++ b/kernel/gcov/Makefile
@@ -1,6 +1,15 @@
 # SPDX-License-Identifier: GPL-2.0
 ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
 
+ifeq ($(CONFIG_CC_IS_GCC),y)
+  GCOV_INFO_SIZE := $(shell $(srctree)/$(src)/geninfosize.sh)
+  ifneq ($(GCOV_INFO_SIZE),)
+    ccflags-y += -DGCC_GCOV_INFO_SIZE=$(GCOV_INFO_SIZE)
+  else
+    $(error Could not determine size of GCC's struct gcov_info)
+  endif
+endif
+
 obj-y := base.o fs.o
 obj-$(CONFIG_CC_IS_GCC) += gcc_base.o gcc_4_7.o
 obj-$(CONFIG_CC_IS_CLANG) += clang.o
diff --git a/kernel/gcov/gcc_4_7.c b/kernel/gcov/gcc_4_7.c
index c53408a00d0b..fce003cfc790 100644
--- a/kernel/gcov/gcc_4_7.c
+++ b/kernel/gcov/gcc_4_7.c
@@ -94,6 +94,9 @@ struct gcov_info {
 	struct gcov_fn_info **functions;
 };
 
+_Static_assert(sizeof(struct gcov_info) == GCC_GCOV_INFO_SIZE,
+	       "struct gcov_info must be updated");
+
 /**
  * gcov_info_filename - return info filename
  * @info: profiling data set
diff --git a/kernel/gcov/geninfosize.sh b/kernel/gcov/geninfosize.sh
new file mode 100755
index 000000000000..c05f1374fcb3
--- /dev/null
+++ b/kernel/gcov/geninfosize.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# Determine size of GCC's internal struct gcov_info. CC must be set correctly.
+#
+# Note: GCC adds an instance of its built-in version of struct gcov_info under
+#       the label .LPBX0 to assembler output when compiling with --coverage
+#
+
+echo "void fn(void) {}" | $CC -S --coverage -x c - -o - | \
+while read a b c ; do
+	# .size	.LPBX0, 112
+	if [ "$a" = ".size" -a "$b" = ".LPBX0," ] ; then
+		echo "$c"
+		break
+	fi
+done
+
+exit 0
-- 
2.25.1


             reply	other threads:[~2021-03-11 13:08 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-11 13:03 Peter Oberparleiter [this message]
2021-03-11 18:38 ` [PATCH] gcov: fail build on gcov_info size mismatch Linus Torvalds
2021-03-12 17:46   ` Peter Oberparleiter
2021-03-11 19:33 ` kernel test robot
2021-03-11 19:33   ` kernel test robot
2021-03-11 20:02   ` Linus Torvalds
2021-03-11 20:02     ` Linus Torvalds
2021-03-12  3:49     ` [kbuild-all] " Rong Chen
2021-03-12  3:49       ` Rong Chen
2021-03-12 17:52       ` [kbuild-all] " Linus Torvalds
2021-03-12 17:52         ` Linus Torvalds
2021-03-15  2:31         ` [kbuild-all] " Rong Chen
2021-03-15  2:31           ` Rong Chen
2021-03-15 19:12           ` [kbuild-all] " Linus Torvalds
2021-03-15 19:12             ` Linus Torvalds
2021-03-15 20:32             ` [kbuild-all] " Jamie Heilman
2021-03-15 20:32               ` Jamie Heilman
2021-03-15 20:57               ` [kbuild-all] " Linus Torvalds
2021-03-15 20:57                 ` Linus Torvalds
2021-03-15 23:09                 ` [kbuild-all] " Herbert Xu
2021-03-15 23:09                   ` Herbert Xu
2021-03-15 23:22                   ` [kbuild-all] " Linus Torvalds
2021-03-15 23:22                     ` Linus Torvalds
2021-03-16  9:51         ` [kbuild-all] " Herbert Xu
2021-03-16  9:51           ` Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210311130328.2859337-1-oberpar@linux.ibm.com \
    --to=oberpar@linux.ibm.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.