44b7ce
From 909d7a656bb5e638bb199b8abd1c5b221b873793 Mon Sep 17 00:00:00 2001
44b7ce
From: Song Liu <songliubraving@fb.com>
44b7ce
Date: Wed, 13 Oct 2021 00:10:20 -0700
44b7ce
Subject: [PATCH 1/2] create-diff-object: compare section name with
44b7ce
 kpatch_section_function_name
44b7ce
44b7ce
Profile-Guided Optimization (PGO) uses profiling data to help the compiler
44b7ce
to evaluate the properties of a function, and thus adds different prefixes
44b7ce
to the section/function. For example, with -ffunction-sections, the
44b7ce
compiler will prefix some sectiones with .text.unlikely. However, if a
44b7ce
function changes from the version in the profiling data, the compiler may
44b7ce
ignore the profiling data. This often happens to the patched function
44b7ce
when kpatch-build builds the patch. As a result, the original function
44b7ce
and the patch function may have different prefix, i.e., one of them has
44b7ce
.text, the other has .text.unlikely.
44b7ce
44b7ce
To correlate these functions properly, use kpatch_section_function_name()
44b7ce
in kpatch_correlate_sections() to find matching sections.
44b7ce
44b7ce
Signed-off-by: Song Liu <songliubraving@fb.com>
44b7ce
---
44b7ce
 kpatch-build/create-diff-object.c | 7 +++++--
44b7ce
 1 file changed, 5 insertions(+), 2 deletions(-)
44b7ce
44b7ce
diff --git a/kpatch-build/create-diff-object.c b/kpatch-build/create-diff-object.c
44b7ce
index 826741d0..319ed68d 100644
44b7ce
--- a/kpatch-build/create-diff-object.c
44b7ce
+++ b/kpatch-build/create-diff-object.c
44b7ce
@@ -1001,6 +1001,8 @@ static void kpatch_correlate_section(struct section *sec_orig,
44b7ce
 		kpatch_correlate_symbol(sec_orig->sym, sec_patched->sym);
44b7ce
 }
44b7ce
 
44b7ce
+static char *kpatch_section_function_name(struct section *sec);
44b7ce
+
44b7ce
 static void kpatch_correlate_sections(struct list_head *seclist_orig,
44b7ce
 		struct list_head *seclist_patched)
44b7ce
 {
44b7ce
@@ -1010,8 +1012,9 @@ static void kpatch_correlate_sections(struct list_head *seclist_orig,
44b7ce
 		if (sec_orig->twin)
44b7ce
 			continue;
44b7ce
 		list_for_each_entry(sec_patched, seclist_patched, list) {
44b7ce
-			if (kpatch_mangled_strcmp(sec_orig->name, sec_patched->name) ||
44b7ce
-			    sec_patched->twin)
44b7ce
+			if (sec_patched->twin ||
44b7ce
+			    kpatch_mangled_strcmp(kpatch_section_function_name(sec_orig),
44b7ce
+						  kpatch_section_function_name(sec_patched)))
44b7ce
 				continue;
44b7ce
 
44b7ce
 			if (is_special_static(is_rela_section(sec_orig) ?
44b7ce
44b7ce
From 0223fa3ab3efc1bed18cff7acf470185e2e66406 Mon Sep 17 00:00:00 2001
ad57ea
From: Song Liu <songliubraving@fb.com>
ad57ea
Date: Wed, 13 Oct 2021 01:03:55 -0700
44b7ce
Subject: [PATCH 2/2] kpatch-build: add support for clang pgo
ad57ea
ad57ea
For clang with Profile-Guided Optimization (PGO), profile data is needed
ad57ea
to compile the livepatch properly. Add option -p|--profile-data, which
ad57ea
specifies the profile data file. This option is only valid with
ad57ea
CONFIG_CC_IS_CLANG and CONFIG_PGO_CLANG.
ad57ea
ad57ea
Signed-off-by: Song Liu <songliubraving@fb.com>
ad57ea
---
8b8236
 kpatch-build/kpatch-build | 20 +++++++++++++++++++-
8b8236
 1 file changed, 19 insertions(+), 1 deletion(-)
ad57ea
ad57ea
diff --git a/kpatch-build/kpatch-build b/kpatch-build/kpatch-build
8b8236
index 634095d5..1f3fc99b 100755
ad57ea
--- a/kpatch-build/kpatch-build
ad57ea
+++ b/kpatch-build/kpatch-build
8b8236
@@ -535,9 +535,10 @@ usage() {
ad57ea
 	echo "		--skip-cleanup          Skip post-build cleanup" >&2
ad57ea
 	echo "		--skip-compiler-check   Skip compiler version matching check" >&2
ad57ea
 	echo "		                        (not recommended)" >&2
ad57ea
+	echo "		-p, --profile-data      specify profile data for PGO (clang only)" >&2
ad57ea
 }
ad57ea
 
8b8236
-options="$(getopt -o ha:r:s:c:v:j:t:n:o:dR -l "help,archversion:,sourcerpm:,sourcedir:,config:,vmlinux:,jobs:,target:,name:,output:,oot-module:,oot-module-src:,debug,skip-gcc-check,skip-compiler-check,skip-cleanup,non-replace" -- "$@")" || die "getopt failed"
8b8236
+options="$(getopt -o ha:r:s:c:v:j:t:n:o:dRp: -l "help,archversion:,sourcerpm:,sourcedir:,config:,vmlinux:,jobs:,target:,name:,output:,oot-module:,oot-module-src:,debug,skip-gcc-check,skip-compiler-check,skip-cleanup,non-replace,profile-data" -- "$@")" || die "getopt failed"
ad57ea
 
ad57ea
 eval set -- "$options"
ad57ea
 
8b8236
@@ -619,6 +620,10 @@ while [[ $# -gt 0 ]]; do
ad57ea
 		echo "WARNING: Skipping compiler version matching check (not recommended)"
ad57ea
 		SKIPCOMPILERCHECK=1
ad57ea
 		;;
ad57ea
+	-p|--profile-data)
ad57ea
+		PROFILE_DATA="$(readlink -f "$2")"
ad57ea
+		shift
ad57ea
+		;;
ad57ea
 	*)
ad57ea
 		[[ "$1" = "--" ]] && shift && continue
ad57ea
 		[[ ! -f "$1" ]] && die "patch file '$1' not found"
8b8236
@@ -896,6 +901,16 @@ fi
ad57ea
 
8b8236
 if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
ad57ea
 	echo "WARNING: Clang support is experimental"
8b8236
+	if [[ -z "$PROFILE_DATA" ]] && [[ -n "$CONFIG_PGO_CLANG" ]]; then
ad57ea
+		die "Please specify profile-data for CONFIG_PGO_CLANG"
ad57ea
+	fi
8b8236
+	if [[ -n "$PROFILE_DATA" ]] && [[ -z "$CONFIG_PGO_CLANG" ]]; then
ad57ea
+		echo "WARNING profile-data specified w/o CONFIG_PGO_CLANG, ignore it"
ad57ea
+	fi
ad57ea
+else
ad57ea
+	if [[ -n "$PROFILE_DATA" ]]; then
ad57ea
+		die "Only supports profile-data with Clang"
ad57ea
+	fi
ad57ea
 fi
ad57ea
 
ad57ea
 if [[ "$SKIPCOMPILERCHECK" -eq 0 ]]; then
8b8236
@@ -943,6 +958,9 @@ declare -a MAKEVARS
8b8236
 if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
8b8236
 	MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${CLANG}")
ad57ea
 	MAKEVARS+=("HOSTCC=clang")
8b8236
+	if [[ -n "$CONFIG_PGO_CLANG" ]]; then
ad57ea
+		MAKEVARS+=("CFLAGS_PGO_CLANG=-fprofile-use=$PROFILE_DATA")
ad57ea
+	fi
ad57ea
 else
8b8236
 	MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${GCC}")
ad57ea
 fi