2016-12-22 09:41:03

by Marcin Nowakowski

[permalink] [raw]
Subject: [PATCH RESEND 0/2] kbuild: dead code elimination: ftrace fixes

Enabling dead code & data elimination currently breaks ftrace operation,
as the __mcount_loc section is removed (as it is not referenced directly
anywhere in the code).
Moreover, there are a lot of entries missing in the __mcount_loc section
as the recordmcount tool doesn't currently properly handle the section
names as created by the use of -ffunction-sections.

The following 2 patches fix that behaviour.
They are based on next-20161208.

Marcin Nowakowski (2):
kbuild: keep __mcount_loc table through dead code elimination
recordmcount: fix mcount recording with -ffunction-sections

include/asm-generic/vmlinux.lds.h | 2 +-
scripts/Makefile | 5 +++++
scripts/recordmcount.c | 26 +++++++++++++++++++++++++-
3 files changed, 31 insertions(+), 2 deletions(-)

--
2.7.4


2016-12-22 09:10:11

by Nicholas Piggin

[permalink] [raw]
Subject: Re: [PATCH RESEND 0/2] kbuild: dead code elimination: ftrace fixes

On Thu, 22 Dec 2016 09:51:45 +0100
Marcin Nowakowski <[email protected]> wrote:

> Enabling dead code & data elimination currently breaks ftrace operation,
> as the __mcount_loc section is removed (as it is not referenced directly
> anywhere in the code).
> Moreover, there are a lot of entries missing in the __mcount_loc section
> as the recordmcount tool doesn't currently properly handle the section
> names as created by the use of -ffunction-sections.

Thanks for keeping on top of these. I didn't have any objections, so
if you would send them to Michal Marek for merge and cc linux-kbuild
that would be appreciated.

Thanks,
Nick

2016-12-22 09:15:18

by Marcin Nowakowski

[permalink] [raw]
Subject: Re: [PATCH RESEND 0/2] kbuild: dead code elimination: ftrace fixes

Hi Nick,

On 22.12.2016 10:08, Nicholas Piggin wrote:
> On Thu, 22 Dec 2016 09:51:45 +0100
> Marcin Nowakowski <[email protected]> wrote:
>
>> Enabling dead code & data elimination currently breaks ftrace operation,
>> as the __mcount_loc section is removed (as it is not referenced directly
>> anywhere in the code).
>> Moreover, there are a lot of entries missing in the __mcount_loc section
>> as the recordmcount tool doesn't currently properly handle the section
>> names as created by the use of -ffunction-sections.
>
> Thanks for keeping on top of these. I didn't have any objections, so
> if you would send them to Michal Marek for merge and cc linux-kbuild
> that would be appreciated.

I had previously sent them to Michal and cc:kbuild, but Michal has not
picked them up yet hence I've sent them to lkml now.
It's not clear to me if Michal is the person responsible for
recordmcount changes, but I initially posted this to kbuild as this is
where all of your dead code & data elimination patches were going through.

Michal - do you plan to pick up these changes or would they need to be
acked/merged by somebody else?

thanks,
Marcin

2016-12-22 09:40:03

by Marcin Nowakowski

[permalink] [raw]
Subject: [PATCH 2/2] recordmcount: fix mcount recording with -ffunction-sections

When CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is enabled, each function is
added to its own new section called .text.functionname.
As described in vmlinux.lds.h, "those enabling
LD_DEAD_CODE_DATA_ELIMINATION must ensure they don't have conflicting
section names, and must pull in .text.[0-9a-zA-Z_]*", add a similar
pattern matching to recordmcount script for section detection.

To simplify the basic C implementation, replace the regex with a string
match on the prefix and a single character match following the '.text.'
prefix.

Signed-off-by: Marcin Nowakowski <[email protected]>
Acked-by: Nicholas Piggin <[email protected]>
---
scripts/Makefile | 5 +++++
scripts/recordmcount.c | 26 +++++++++++++++++++++++++-
2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/scripts/Makefile b/scripts/Makefile
index 1d80897..9b88250 100644
--- a/scripts/Makefile
+++ b/scripts/Makefile
@@ -23,6 +23,11 @@ hostprogs-$(CONFIG_SYSTEM_EXTRA_CERTIFICATE) += insert-sys-cert

HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include
HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
+
+ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
+HOSTCFLAGS_recordmcount.o = -DMCOUNT_INCLUDE_FUNCTION_SECTIONS
+endif
+
HOSTLOADLIBES_sign-file = -lcrypto
HOSTLOADLIBES_extract-cert = -lcrypto

diff --git a/scripts/recordmcount.c b/scripts/recordmcount.c
index aeb3422..bb517ef 100644
--- a/scripts/recordmcount.c
+++ b/scripts/recordmcount.c
@@ -32,6 +32,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <ctype.h>

/*
* glibc synced up and added the metag number but didn't add the relocations.
@@ -408,9 +409,31 @@ static uint32_t (*w)(uint32_t);
static uint32_t (*w2)(uint16_t);

/* Names of the sections that could contain calls to mcount. */
+#ifdef MCOUNT_INCLUDE_FUNCTION_SECTIONS
+static int
+is_mcounted_function_section_name(char const *const txtname)
+{
+ const size_t len = strlen(".text.");
+
+ return strncmp(".text.", txtname, len) == 0 &&
+ strlen(txtname) > len &&
+ (isalnum(txtname[len]) || txtname[len] == '_');
+}
+#else
+static int
+is_mcounted_function_section_name(char const *const txtname)
+{
+ return 0;
+}
+#endif
+
+
static int
is_mcounted_section_name(char const *const txtname)
{
+ int ffunc_section =
+ is_mcounted_function_section_name(txtname);
+
return strcmp(".text", txtname) == 0 ||
strcmp(".ref.text", txtname) == 0 ||
strcmp(".sched.text", txtname) == 0 ||
@@ -419,7 +442,8 @@ is_mcounted_section_name(char const *const txtname)
strcmp(".softirqentry.text", txtname) == 0 ||
strcmp(".kprobes.text", txtname) == 0 ||
strcmp(".cpuidle.text", txtname) == 0 ||
- strcmp(".text.unlikely", txtname) == 0;
+ strcmp(".text.unlikely", txtname) == 0 ||
+ ffunc_section;
}

/* 32 bit and 64 bit are very similar */
--
2.7.4

2016-12-22 09:41:04

by Marcin Nowakowski

[permalink] [raw]
Subject: [PATCH 1/2] kbuild: keep __mcount_loc table through dead code elimination

When CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is enabled we must ensure
__mcount_loc is preserved, as otherwise there are no valid entries for
ftrace to work with.

Fixes: 4b89b7f7aad5 ('kbuild: keep data tables through dead code elimination')

Signed-off-by: Marcin Nowakowski <[email protected]>
Acked-by: Nicholas Piggin <[email protected]>
---
include/asm-generic/vmlinux.lds.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 0968d13..f492aa4 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -89,7 +89,7 @@
#ifdef CONFIG_FTRACE_MCOUNT_RECORD
#define MCOUNT_REC() . = ALIGN(8); \
VMLINUX_SYMBOL(__start_mcount_loc) = .; \
- *(__mcount_loc) \
+ KEEP(*(__mcount_loc)) \
VMLINUX_SYMBOL(__stop_mcount_loc) = .;
#else
#define MCOUNT_REC()
--
2.7.4