livepatch-build-tools: fixes for handling .cold and .hot sections
cover livepatch-build-tools: fixes for handling .cold and .hot sections
Hello,
Fixes picked from kpatch to deal with .cold and .hot sub-functions sections generated by gcc.
Thanks, Roger.
Artem Savkov (7):
create-diff-object: ignore .cold.* suffixes in is_bundleable()
create-diff-object: add symbol relations
create-diff-object: propagate child symbol changes
create-diff-object: allow changing subsections
create-diff-object: add .text.hot to the list of bundleable functions
create-diff-object: propagate ignore.functions to children
create-build-diff: support for .cold functions with no id suffix
common.c | 9 ++++- common.h | 2 ++ create-diff-object.c | 78 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 84 insertions(+), 5 deletions(-)
Signature · 1 line
2.46.0
PATCH 1/7 create-diff-object: ignore .cold.* suffixes in is_bundleable()
From: Artem Savkov <[email protected]>
While building a gcc-consprop patch from integration tests gcc8 would place a __timekeeping_inject_sleeptime.constprop.18.cold.27 symbol into .text.unlikely.__timekeeping_inject_sleeptime.constprop.18 section. Because section name doesn't have the '.cold.27' suffix this symbol fails is_bundleable() check while still being bundleable and later exits early in kpatch_rename_mangled_functions() without renaming the corresponding patched function. All of this results in a create-diff-object errror:
ERROR: timekeeping.o: symbol changed sections: __timekeeping_inject_sleeptime.constprop.18.cold.27 /home/asavkov/dev/kpatch/kpatch-build/create-diff-object: unreconcilable difference
Fix by ignoring .cold.* name suffix in is_bundleable() for.text.unlikely sections.
--- common.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
common.c +3 -1
@@ -126,7 +126,9 @@ static int is_bundleable(struct symbol *sym)if (sym->type ==STT_FUNC && !strncmp (sym->sec->name,".text.unlikely." ,15 ) && - !strcmp (sym->sec->name +15 , sym->name)) + (!strcmp (sym->sec->name +15 , sym->name) || + (strstr (sym->name,".cold." ) && + !strncmp (sym->sec->name +15 , sym->name,strlen (sym->sec->name) -15 ))))return 1 ; if (sym->type == STT_OBJECT &&
Signature · 1 line
2.46.0
PATCH 2/7 create-diff-object: add symbol relations
In reply to Roger Pau MonneFrom: Artem Savkov <[email protected]>
Add a function that would detect parent/child symbol relations. So far it only supports .cold.* symbols as children.
--- common.h | 2 ++ create-diff-object.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+)
common.h +2 -0
@@ -85,6 +85,8 @@ struct section {struct symbol {struct list_head list;struct symbol *twin; +struct symbol *parent; +struct symbol *child;struct section *sec;GElf_Sym sym;char *name;
create-diff-object.c +35 -0
@@ -327,6 +327,38 @@ static void kpatch_rename_mangled_functions(struct kpatch_elf *base, } } +/* +* During optimization gcc may move unlikely execution branches into *.cold +* subfunctions. kpatch_detect_child_functions detects such subfunctions and +* crossreferences them with their parent functions through parent/child +* pointers. +*/ +static void kpatch_detect_child_functions (struct kpatch_elf *kelf) +{ +struct symbol *sym; + +list_for_each_entry (sym, &kelf->symbols, list) { +char *coldstr; + + coldstr =strstr (sym->name,".cold." ); +if (coldstr !=NULL ) { +char *pname; + + pname =strndup (sym->name, coldstr - sym->name); +if (!pname) +ERROR ("strndup" ); + + sym->parent =find_symbol_by_name (&kelf->symbols, pname); +free (pname); + +if (!sym->parent) +ERROR ("failed to find parent function for %s" , sym->name); + + sym->parent->child = sym; + } + } +} +/* * This function detects whether the given symbol is a "special" static local * variable (for lack of a better term).
@@ -2329,6 +2361,9 @@ int main(int argc, char *argv[])log_debug ("Open patched\n" ); kelf_patched =kpatch_elf_open (arguments.args[1 ]); +kpatch_detect_child_functions (kelf_base); +kpatch_detect_child_functions (kelf_patched); +log_debug ("Compare elf headers\n" );kpatch_compare_elf_headers (kelf_base->elf, kelf_patched->elf);log_debug ("Check program headers of base\n" );
Signature · 1 line
2.46.0
PATCH 3/7 create-diff-object: propagate child symbol changes
In reply to Roger Pau MonneFrom: Artem Savkov <[email protected]>
Propagate child symbol changes to it's parent.
--- create-diff-object.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
create-diff-object.c +8 -2
@@ -821,8 +821,14 @@ static void kpatch_compare_sections(struct list_head *seclist)if (sec->base->sym && sec->base->sym->status !=CHANGED ) sec->base->sym->status = sec->status; }else { -if (sec->sym && sec->sym->status !=CHANGED ) - sec->sym->status = sec->status; +struct symbol *sym = sec->sym; + +if (sym && sym->status !=CHANGED ) + sym->status = sec->status; + +if (sym && sym->child && sym->status ==SAME && + sym->child->sec->status ==CHANGED ) + sym->status =CHANGED ; } } }
Signature · 1 line
2.46.0
PATCH 4/7 create-diff-object: allow changing subsections
In reply to Roger Pau MonneFrom: Artem Savkov <[email protected]>
gcc8 can place functions to .text.unlikely and .text.hot subsections during optimizations. Allow symbols to change subsections instead of failing.
--- create-diff-object.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
create-diff-object.c +27 -2
@@ -55,6 +55,13 @@#include "common.h" char *childobj; + +enum subsection { +SUBSECTION_NORMAL , +SUBSECTION_HOT , +SUBSECTION_UNLIKELY +}; +enum loglevel loglevel =NORMAL ;static void kpatch_compare_elf_headers (Elf *elf1,Elf *elf2)
@@ -833,6 +840,22 @@ static void kpatch_compare_sections(struct list_head *seclist) } } +static enum subsection kpatch_subsection_type (struct section *sec) +{ +if (!strncmp (sec->name,".text.unlikely." ,15 )) +return SUBSECTION_UNLIKELY ; + +if (!strncmp (sec->name,".text.hot." ,10 )) +return SUBSECTION_HOT ; + +return SUBSECTION_NORMAL ; +} + +static int kpatch_subsection_changed (struct section *sec1,struct section *sec2) +{ +return kpatch_subsection_type (sec1) !=kpatch_subsection_type (sec2); +} +static void kpatch_compare_correlated_symbol (struct symbol *sym) {struct symbol *sym1 = sym, *sym2 = sym->twin;
@@ -846,10 +869,12 @@ static void kpatch_compare_correlated_symbol(struct symbol *sym)/* * If two symbols are correlated but their sections are not, then the * symbol has changed sections. This is only allowed if the symbol is -* moving out of an ignored section. +* moving out of an ignored section, or moving between normal/hot/unlikely +* subsections. */ if (sym1->sec && sym2->sec && sym1->sec->twin != sym2->sec) { -if (sym2->sec->twin && sym2->sec->twin->ignore) +if ((sym2->sec->twin && sym2->sec->twin->ignore) || +kpatch_subsection_changed (sym1->sec, sym2->sec)) sym->status =CHANGED ;else DIFF_FATAL ("symbol changed sections: %s, %s, %s, %s" , sym1->name, sym2->name, sym1->sec->name, sym2->sec->name);
Signature · 1 line
2.46.0
PATCH 5/7 create-diff-object: add .text.hot to the list of bundleable functions
In reply to Roger Pau MonneFrom: Artem Savkov <[email protected]>
According to gcc8's man pages gcc can put functions into .text.unlikely or .text.hot subfunctions during optimization. Add ".text.hot" to the list of bundleable functions in is_bundleable().
--- common.c | 5 +++++ 1 file changed, 5 insertions(+)
common.c +5 -0
@@ -131,6 +131,11 @@ static int is_bundleable(struct symbol *sym) !strncmp(sym->sec->name + 15, sym->name, strlen(sym->sec->name) - 15))))return 1 ; +if (sym->type ==STT_FUNC && + !strncmp (sym->sec->name,".text.hot." ,10 ) && + !strcmp (sym->sec->name +10 , sym->name)) +return 1 ; +if (sym->type ==STT_OBJECT && !strncmp (sym->sec->name,".data." ,6 ) && !strcmp (sym->sec->name +6 , sym->name))
Signature · 1 line
2.46.0
PATCH 6/7 create-diff-object: propagate ignore.functions to children
In reply to Roger Pau MonneFrom: Artem Savkov <[email protected]>
Add child symbols to .kpatch.ignore.functions in case their parents are added to the list.
--- create-diff-object.c | 4 ++++ 1 file changed, 4 insertions(+)
create-diff-object.c +4 -0
@@ -936,6 +936,10 @@ static void kpatch_mark_ignored_functions_same(struct kpatch_elf *kelf)log_normal ("NOTICE: no change detected in function %s, unnecessary KPATCH_IGNORE_FUNCTION()?\n" , rela->sym->name); rela->sym->status =SAME ; rela->sym->sec->status =SAME ; + +if (rela->sym->child) + rela->sym->child->status =SAME ; +if (rela->sym->sec->secsym) rela->sym->sec->secsym->status =SAME ;if (rela->sym->sec->rela)
Signature · 1 line
2.46.0
PATCH 7/7 create-build-diff: support for .cold functions with no id suffix
In reply to Roger Pau MonneFrom: Artem Savkov <[email protected]>
create-build-diff expects .cold functions to be suffixed by an id, which is not always the case. Drop the trailing '.' when searching for cold functions.
--- common.c | 2 +- create-diff-object.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
common.c +1 -1
@@ -127,7 +127,7 @@ static int is_bundleable(struct symbol *sym)if (sym->type ==STT_FUNC && !strncmp (sym->sec->name,".text.unlikely." ,15 ) && (!strcmp (sym->sec->name +15 , sym->name) || - (strstr (sym->name,".cold." ) && + (strstr (sym->name,".cold" ) && !strncmp (sym->sec->name +15 , sym->name,strlen (sym->sec->name) -15 ))))return 1 ;
create-diff-object.c +1 -1
@@ -347,7 +347,7 @@ static void kpatch_detect_child_functions(struct kpatch_elf *kelf)list_for_each_entry (sym, &kelf->symbols, list) {char *coldstr; - coldstr =strstr (sym->name,".cold." ); + coldstr =strstr (sym->name,".cold" );if (coldstr !=NULL ) {char *pname;
Signature · 1 line
2.46.0
Quoted 8 lines · Roger Pau Monne
Hello,
Fixes picked from kpatch to deal with .cold and .hot sub-functions sections generated by gcc.
Thanks, Roger.
Quoted 9 lines
Artem Savkov (7):
create-diff-object: ignore .cold.* suffixes inis_bundleable()
create-diff-object: add symbol relations
create-diff-object: propagate child symbol changes
create-diff-object: allow changing subsections
create-diff-object: add .text.hot to the list of bundleable functions
create-diff-object: propagate ignore.functions to children
create-build-diff: support for .cold functions with no id suffix
For all patches,
Thanks