Skip to contentSkip to conversation

livepatch-build-tools: fixes for handling .cold and .hot sections

PATCH In review Roger Pau Monne 2 participants 9 messages Jan 16, 2025 – Jan 21, 2025

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 68a71f75ac7b..84ca14d3e397 100644
@@ -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
#2 lore raw

PATCH 2/7 create-diff-object: add symbol relations

In reply to Roger Pau Monne

From: 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
index 5ff9ef6ca8e9..7f3a82ffdb29 100644
@@ -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
index fed360a9aa68..b041d94d9723 100644
@@ -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
#3 lore raw

PATCH 3/7 create-diff-object: propagate child symbol changes

In reply to Roger Pau Monne

From: 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
index b041d94d9723..dd5466bff6ce 100644
@@ -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
#4 lore raw

PATCH 4/7 create-diff-object: allow changing subsections

In reply to Roger Pau Monne

From: 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
index dd5466bff6ce..3189d3e8451c 100644
@@ -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
#5 lore raw

PATCH 5/7 create-diff-object: add .text.hot to the list of bundleable functions

In reply to Roger Pau Monne

From: 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
index 84ca14d3e397..b46fcf5cb6ca 100644
@@ -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
#6 lore raw

PATCH 6/7 create-diff-object: propagate ignore.functions to children

In reply to Roger Pau Monne

From: 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
index 3189d3e8451c..6060a73555ed 100644
@@ -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
#7 lore raw

PATCH 7/7 create-build-diff: support for .cold functions with no id suffix

In reply to Roger Pau Monne

From: 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.

Fixes #1160
---
 common.c             | 2 +-
 create-diff-object.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
common.c +1 -1
index b46fcf5cb6ca..67b9fcdb0ada 100644
@@ -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
index 6060a73555ed..19590fc0fce1 100644
@@ -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
Reviewed-by on the v1 series
#8 lore raw
In reply to Roger Pau Monne
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 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

For all patches,

Thanks