Blame SOURCES/v0.9.7-backport-MR-1315-Static-call-fixes.patch

734205
From 358743e6d8748510f4c9a71511d7ceea7c72f7aa Mon Sep 17 00:00:00 2001
734205
From: Josh Poimboeuf <jpoimboe@redhat.com>
734205
Date: Mon, 21 Nov 2022 19:23:07 -0800
734205
Subject: [PATCH] v0.9.7 backport: MR!1315 ("Static call fixes")
734205
734205
commit 87ad96760a3af0db294d44865dfa1703f57f5595
734205
Author: Josh Poimboeuf <jpoimboe@redhat.com>
734205
Date:   Mon Nov 21 19:23:07 2022 -0800
734205
734205
    create-diff-object: fix s390 special_section initializer spacing
734205
734205
    Align the s390 special_section initializers to improve readability and
734205
    for consistency with the rest.
734205
734205
    Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
734205
734205
commit 56bd8c4d0da1634f8549e7269f77a53e9d936a57
734205
Author: Josh Poimboeuf <jpoimboe@redhat.com>
734205
Date:   Mon Nov 21 19:27:23 2022 -0800
734205
734205
    create-diff-object: refactor jump label filtering
734205
734205
    Convert the hard-coded should_keep_jump_label() to a proper callback,
734205
    since static calls will need a similar filter.
734205
734205
    Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
734205
734205
commit f83218ad12a2d9e20d99d379c78974a576aa558c
734205
Author: Josh Poimboeuf <jpoimboe@redhat.com>
734205
Date:   Mon Nov 21 19:29:53 2022 -0800
734205
734205
    create-diff-object: detect unsupported static calls
734205
734205
    Similar to jump labels, static calls aren't supported when the static
734205
    call key was originally defined in a module rather than in vmlinux.
734205
    Detect those cases and either remove them (in the case of tracepoints)
734205
    or error out.
734205
734205
    Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
734205
734205
commit ab2397c03e31f0f697aa8bf943d70b4e5a7def54
734205
Author: Josh Poimboeuf <jpoimboe@redhat.com>
734205
Date:   Mon Nov 21 19:41:30 2022 -0800
734205
734205
    kpatch-macros: add KPATCH_STATIC_CALL()
734205
734205
    Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
734205
734205
commit 92c178b6a30a827c48db46ff4238501ec406a28e
734205
Author: Josh Poimboeuf <jpoimboe@redhat.com>
734205
Date:   Tue Nov 22 12:53:09 2022 -0800
734205
734205
    create-diff-object: use errx() instead of err()
734205
734205
    Otherwise on recent distros it appends the errno to the error message,
734205
    like:
734205
734205
      create-diff-object: ERROR: x86.o: kpatch_regenerate_special_section: 2633: Found 1 unsupported static call(s) in the patched code. Use KPATCH_STATIC_CALL() instead.: Success
734205
734205
    which is not what we want in most cases.
734205
734205
    Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
734205
734205
Signed-off-by: Yannick Cote <ycote@redhat.com>
734205
---
734205
 kmod/patch/kpatch-macros.h        |  11 +
734205
 kpatch-build/create-diff-object.c | 328 ++++++++++++++++++------------
734205
 kpatch-build/log.h                |   2 +-
734205
 3 files changed, 211 insertions(+), 130 deletions(-)
734205
734205
diff --git a/kmod/patch/kpatch-macros.h b/kmod/patch/kpatch-macros.h
734205
index 8e09702ea001..b797838849ca 100644
734205
--- a/kmod/patch/kpatch-macros.h
734205
+++ b/kmod/patch/kpatch-macros.h
734205
@@ -141,4 +141,15 @@ struct kpatch_post_unpatch_callback {
734205
 		printk(_fmt, ## __VA_ARGS__); \
734205
 })
734205
 
734205
+/*
734205
+ * KPATCH_STATIC_CALL macro
734205
+ *
734205
+ * Replace usages of static_call() with this macro, when create-diff-object
734205
+ * recommends it due to the original static call key living in a module.
734205
+ *
734205
+ * This converts the static call to a regular indirect call.
734205
+ */
734205
+#define KPATCH_STATIC_CALL(name) \
734205
+	((typeof(STATIC_CALL_TRAMP(name))*)(STATIC_CALL_KEY(name).func))
734205
+
734205
 #endif /* __KPATCH_MACROS_H_ */
734205
diff --git a/kpatch-build/create-diff-object.c b/kpatch-build/create-diff-object.c
734205
index 7106b67cfd25..ddaa9b44f11e 100644
734205
--- a/kpatch-build/create-diff-object.c
734205
+++ b/kpatch-build/create-diff-object.c
734205
@@ -56,7 +56,7 @@
734205
 #define DIFF_FATAL(format, ...) \
734205
 ({ \
734205
 	fprintf(stderr, "ERROR: %s: " format "\n", childobj, ##__VA_ARGS__); \
734205
-	err(EXIT_STATUS_DIFF_FATAL, "unreconcilable difference"); \
734205
+	errx(EXIT_STATUS_DIFF_FATAL, "unreconcilable difference"); \
734205
 })
734205
 
734205
 char *childobj;
734205
@@ -71,6 +71,8 @@ enum loglevel loglevel = NORMAL;
734205
 
734205
 bool KLP_ARCH;
734205
 
734205
+int jump_label_errors, static_call_errors;
734205
+
734205
 /*******************
734205
  * Data structures
734205
  * ****************/
734205
@@ -78,6 +80,9 @@ struct special_section {
734205
 	char *name;
734205
 	enum architecture arch;
734205
 	int (*group_size)(struct kpatch_elf *kelf, int offset);
734205
+	bool (*group_filter)(struct lookup_table *lookup,
734205
+			     struct section *relasec, unsigned int offset,
734205
+			     unsigned int size);
734205
 };
734205
 
734205
 /*************
734205
@@ -2215,6 +2220,169 @@ static int fixup_group_size(struct kpatch_elf *kelf, int offset)
734205
 	return (int)(rela->addend - offset);
734205
 }
734205
 
734205
+static bool jump_table_group_filter(struct lookup_table *lookup,
734205
+				    struct section *relasec,
734205
+				    unsigned int group_offset,
734205
+				    unsigned int group_size)
734205
+{
734205
+	struct rela *code = NULL, *key = NULL, *rela;
734205
+	bool tracepoint = false, dynamic_debug = false;
734205
+	struct lookup_result symbol;
734205
+	int i = 0;
734205
+
734205
+	/*
734205
+	 * Here we hard-code knowledge about the contents of the jump_entry
734205
+	 * struct.  It has three fields: code, target, and key.  Each field has
734205
+	 * a relocation associated with it.
734205
+	 */
734205
+	list_for_each_entry(rela, &relasec->relas, list) {
734205
+		if (rela->offset >= group_offset &&
734205
+		    rela->offset < group_offset + group_size) {
734205
+			if (i == 0)
734205
+				code = rela;
734205
+			else if (i == 2)
734205
+				key = rela;
734205
+			i++;
734205
+		}
734205
+	}
734205
+
734205
+	if (i != 3 || !key || !code)
734205
+		ERROR("BUG: __jump_table has an unexpected format");
734205
+
734205
+	if (!strncmp(key->sym->name, "__tracepoint_", 13))
734205
+		tracepoint = true;
734205
+
734205
+	if (is_dynamic_debug_symbol(key->sym))
734205
+		dynamic_debug = true;
734205
+
734205
+	if (KLP_ARCH) {
734205
+		/*
734205
+		 * On older kernels (with .klp.arch support), jump labels
734205
+		 * aren't supported at all.  Error out when they occur in a
734205
+		 * replacement function, with the exception of tracepoints and
734205
+		 * dynamic debug printks.  An inert tracepoint or printk is
734205
+		 * harmless enough, but a broken jump label can cause
734205
+		 * unexpected behavior.
734205
+		 */
734205
+		if (tracepoint || dynamic_debug)
734205
+			return false;
734205
+
734205
+		/*
734205
+		 * This will be upgraded to an error after all jump labels have
734205
+		 * been reported.
734205
+		 */
734205
+		log_normal("Found a jump label at %s()+0x%lx, using key %s.  Jump labels aren't supported with this kernel.  Use static_key_enabled() instead.\n",
734205
+			   code->sym->name, code->addend, key->sym->name);
734205
+		jump_label_errors++;
734205
+		return false;
734205
+	}
734205
+
734205
+	/*
734205
+	 * On newer (5.8+) kernels, jump labels are supported in the case where
734205
+	 * the corresponding static key lives in vmlinux.  That's because such
734205
+	 * kernels apply vmlinux-specific .klp.rela sections at the same time
734205
+	 * (in the klp module load) as normal relas, before jump label init.
734205
+	 * On the other hand, jump labels based on static keys which are
734205
+	 * defined in modules aren't supported, because late module patching
734205
+	 * can result in the klp relas getting applied *after* the klp module's
734205
+	 * jump label init.
734205
+	 */
734205
+
734205
+	if (lookup_symbol(lookup, key->sym, &symbol) &&
734205
+	    strcmp(symbol.objname, "vmlinux")) {
734205
+
734205
+		/* The static key lives in a module -- not supported */
734205
+
734205
+		/* Inert tracepoints and dynamic debug printks are harmless */
734205
+		if (tracepoint || dynamic_debug)
734205
+			return false;
734205
+
734205
+		/*
734205
+		 * This will be upgraded to an error after all jump label
734205
+		 * errors have been reported.
734205
+		 */
734205
+		log_normal("Found a jump label at %s()+0x%lx, using key %s, which is defined in a module.  Use static_key_enabled() instead.\n",
734205
+			   code->sym->name, code->addend, key->sym->name);
734205
+		jump_label_errors++;
734205
+		return false;
734205
+	}
734205
+
734205
+	/* The static key lives in vmlinux or the patch module itself */
734205
+
734205
+	/*
734205
+	 * If the jump label key lives in the '__dyndbg' section, make sure
734205
+	 * the section gets included, because we don't use klp relocs for
734205
+	 * dynamic debug symbols.  For an example of such a key, see
734205
+	 * DYNAMIC_DEBUG_BRANCH().
734205
+	 */
734205
+	if (dynamic_debug)
734205
+		kpatch_include_symbol(key->sym);
734205
+
734205
+	return true;
734205
+}
734205
+
734205
+static bool static_call_sites_group_filter(struct lookup_table *lookup,
734205
+					   struct section *relasec,
734205
+					   unsigned int group_offset,
734205
+					   unsigned int group_size)
734205
+{
734205
+	struct rela *code = NULL, *key = NULL, *rela;
734205
+	bool tracepoint = false;
734205
+	struct lookup_result symbol;
734205
+	int i = 0;
734205
+
734205
+	/*
734205
+	 * Here we hard-code knowledge about the contents of the jump_entry
734205
+	 * struct.  It has three fields: code, target, and key.  Each field has
734205
+	 * a relocation associated with it.
734205
+	 */
734205
+	list_for_each_entry(rela, &relasec->relas, list) {
734205
+		if (rela->offset >= group_offset &&
734205
+		    rela->offset < group_offset + group_size) {
734205
+			if (i == 0)
734205
+				code = rela;
734205
+			else if (i == 1)
734205
+				key = rela;
734205
+			i++;
734205
+		}
734205
+	}
734205
+
734205
+	if (i != 2 || !key || !code)
734205
+		ERROR("BUG: .static_call_sites has an unexpected format");
734205
+
734205
+	if (!strncmp(key->sym->name, "__SCK__tp_func_", 15))
734205
+		tracepoint = true;
734205
+
734205
+	/*
734205
+	 * Static calls are only supported in the case where the corresponding
734205
+	 * static call key lives in vmlinux (see explanation in
734205
+	 * jump_table_group_filter).
734205
+	 */
734205
+
734205
+	if (lookup_symbol(lookup, key->sym, &symbol) &&
734205
+	    strcmp(symbol.objname, "vmlinux")) {
734205
+
734205
+		/* The key lives in a module -- not supported */
734205
+
734205
+		/* Inert tracepoints are harmless */
734205
+		if (tracepoint)
734205
+			return false;
734205
+
734205
+		/*
734205
+		 * This will be upgraded to an error after all static call
734205
+		 * errors have been reported.
734205
+		 */
734205
+		log_normal("Found a static call at %s()+0x%lx, using key %s, which is defined in a module.  Use KPATCH_STATIC_CALL() instead.\n",
734205
+			   code->sym->name, code->addend, key->sym->name);
734205
+		static_call_errors++;
734205
+		return false;
734205
+	}
734205
+
734205
+	/* The key lives in vmlinux or the patch module itself */
734205
+	return true;
734205
+}
734205
+
734205
+
734205
 static struct special_section special_sections[] = {
734205
 	{
734205
 		.name		= "__bug_table",
734205
@@ -2235,6 +2403,7 @@ static struct special_section special_sections[] = {
734205
 		.name		= "__jump_table",
734205
 		.arch		= X86_64 | PPC64 | S390,
734205
 		.group_size	= jump_table_group_size,
734205
+		.group_filter	= jump_table_group_filter,
734205
 	},
734205
 	{
734205
 		.name		= ".printk_index",
734205
@@ -2260,6 +2429,7 @@ static struct special_section special_sections[] = {
734205
 		.name		= ".static_call_sites",
734205
 		.arch		= X86_64,
734205
 		.group_size	= static_call_sites_group_size,
734205
+		.group_filter	= static_call_sites_group_filter,
734205
 	},
734205
 	{
734205
 		.name		= ".retpoline_sites",
734205
@@ -2297,138 +2467,36 @@ static struct special_section special_sections[] = {
734205
 		.group_size	= fixup_barrier_nospec_group_size,
734205
 	},
734205
 	{
734205
-		.name = ".s390_return_mem",
734205
-		.arch = S390,
734205
-		.group_size = s390_expolines_group_size,
734205
+		.name		= ".s390_return_mem",
734205
+		.arch		= S390,
734205
+		.group_size	= s390_expolines_group_size,
734205
 	},
734205
 	{
734205
-		.name = ".s390_return_reg",
734205
-		.arch = S390,
734205
-		.group_size = s390_expolines_group_size,
734205
+		.name		= ".s390_return_reg",
734205
+		.arch		= S390,
734205
+		.group_size	= s390_expolines_group_size,
734205
 	},
734205
 	{
734205
-		.name = ".s390_indirect_call",
734205
-		.arch = S390,
734205
-		.group_size = s390_expolines_group_size,
734205
+		.name		= ".s390_indirect_call",
734205
+		.arch		= S390,
734205
+		.group_size	= s390_expolines_group_size,
734205
 	},
734205
 	{
734205
-		.name = ".s390_indirect_branches",
734205
-		.arch = S390,
734205
-		.group_size = s390_expolines_group_size,
734205
+		.name		= ".s390_indirect_branches",
734205
+		.arch		= S390,
734205
+		.group_size	= s390_expolines_group_size,
734205
 	},
734205
 	{
734205
-		.name = ".s390_indirect_jump",
734205
-		.arch = S390,
734205
-		.group_size = s390_expolines_group_size,
734205
+		.name		= ".s390_indirect_jump",
734205
+		.arch		= S390,
734205
+		.group_size	= s390_expolines_group_size,
734205
 	},
734205
 	{},
734205
 };
734205
 
734205
-static bool should_keep_jump_label(struct lookup_table *lookup,
734205
-				   struct section *relasec,
734205
-				   unsigned int group_offset,
734205
-				   unsigned int group_size,
734205
-				   int *jump_labels_found)
734205
-{
734205
-	struct rela *code = NULL, *key = NULL, *rela;
734205
-	bool tracepoint = false, dynamic_debug = false;
734205
-	struct lookup_result symbol;
734205
-	int i = 0;
734205
-
734205
-	/*
734205
-	 * Here we hard-code knowledge about the contents of the jump_entry
734205
-	 * struct.  It has three fields: code, target, and key.  Each field has
734205
-	 * a relocation associated with it.
734205
-	 */
734205
-	list_for_each_entry(rela, &relasec->relas, list) {
734205
-		if (rela->offset >= group_offset &&
734205
-		    rela->offset < group_offset + group_size) {
734205
-			if (i == 0)
734205
-				code = rela;
734205
-			else if (i == 2)
734205
-				key = rela;
734205
-			i++;
734205
-		}
734205
-	}
734205
-
734205
-	if (i != 3 || !key || !code)
734205
-		ERROR("BUG: __jump_table has an unexpected format");
734205
-
734205
-	if (!strncmp(key->sym->name, "__tracepoint_", 13))
734205
-		tracepoint = true;
734205
-
734205
-	if (is_dynamic_debug_symbol(key->sym))
734205
-		dynamic_debug = true;
734205
-
734205
-	if (KLP_ARCH) {
734205
-		/*
734205
-		 * On older kernels (with .klp.arch support), jump labels
734205
-		 * aren't supported at all.  Error out when they occur in a
734205
-		 * replacement function, with the exception of tracepoints and
734205
-		 * dynamic debug printks.  An inert tracepoint or printk is
734205
-		 * harmless enough, but a broken jump label can cause
734205
-		 * unexpected behavior.
734205
-		 */
734205
-		if (tracepoint || dynamic_debug)
734205
-			return false;
734205
-
734205
-		/*
734205
-		 * This will be upgraded to an error after all jump labels have
734205
-		 * been reported.
734205
-		 */
734205
-		log_normal("Found a jump label at %s()+0x%lx, using key %s.  Jump labels aren't supported with this kernel.  Use static_key_enabled() instead.\n",
734205
-			   code->sym->name, code->addend, key->sym->name);
734205
-		(*jump_labels_found)++;
734205
-		return false;
734205
-	}
734205
-
734205
-	/*
734205
-	 * On newer (5.8+) kernels, jump labels are supported in the case where
734205
-	 * the corresponding static key lives in vmlinux.  That's because such
734205
-	 * kernels apply vmlinux-specific .klp.rela sections at the same time
734205
-	 * (in the klp module load) as normal relas, before jump label init.
734205
-	 * On the other hand, jump labels based on static keys which are
734205
-	 * defined in modules aren't supported, because late module patching
734205
-	 * can result in the klp relas getting applied *after* the klp module's
734205
-	 * jump label init.
734205
-	 */
734205
-
734205
-	if (lookup_symbol(lookup, key->sym, &symbol) &&
734205
-	    strcmp(symbol.objname, "vmlinux")) {
734205
-
734205
-		/* The static key lives in a module -- not supported */
734205
-
734205
-		/* Inert tracepoints and dynamic debug printks are harmless */
734205
-		if (tracepoint || dynamic_debug)
734205
-			return false;
734205
-
734205
-		/*
734205
-		 * This will be upgraded to an error after all jump labels have
734205
-		 * been reported.
734205
-		 */
734205
-		log_normal("Found a jump label at %s()+0x%lx, using key %s, which is defined in a module.  Use static_key_enabled() instead.\n",
734205
-			   code->sym->name, code->addend, key->sym->name);
734205
-		(*jump_labels_found)++;
734205
-		return false;
734205
-	}
734205
-
734205
-	/* The static key lives in vmlinux or the patch module itself */
734205
-
734205
-	/*
734205
-	 * If the jump label key lives in the '__dyndbg' section, make sure
734205
-	 * the section gets included, because we don't use klp relocs for
734205
-	 * dynamic debug symbols.  For an example of such a key, see
734205
-	 * DYNAMIC_DEBUG_BRANCH().
734205
-	 */
734205
-	if (dynamic_debug)
734205
-		kpatch_include_symbol(key->sym);
734205
-
734205
-	return true;
734205
-}
734205
-
734205
 static bool should_keep_rela_group(struct lookup_table *lookup,
734205
 				   struct section *relasec, unsigned int offset,
734205
-				   unsigned int size, int *jump_labels_found)
734205
+				   unsigned int size)
734205
 {
734205
 	struct rela *rela;
734205
 	bool found = false;
734205
@@ -2448,10 +2516,6 @@ static bool should_keep_rela_group(struct lookup_table *lookup,
734205
 	if (!found)
734205
 		return false;
734205
 
734205
-	if (!strcmp(relasec->name, ".rela__jump_table"))
734205
-		return should_keep_jump_label(lookup, relasec, offset, size,
734205
-					      jump_labels_found);
734205
-
734205
 	return true;
734205
 }
734205
 
734205
@@ -2488,7 +2552,6 @@ static void kpatch_regenerate_special_section(struct kpatch_elf *kelf,
734205
 	struct rela *rela, *safe;
734205
 	char *src, *dest;
734205
 	unsigned int group_size, src_offset, dest_offset;
734205
-	int jump_labels_found = 0;
734205
 
734205
 	LIST_HEAD(newrelas);
734205
 
734205
@@ -2523,8 +2586,11 @@ static void kpatch_regenerate_special_section(struct kpatch_elf *kelf,
734205
 		if (src_offset + group_size > relasec->base->sh.sh_size)
734205
 			group_size = (unsigned int)(relasec->base->sh.sh_size - src_offset);
734205
 
734205
-		if (!should_keep_rela_group(lookup, relasec, src_offset, group_size,
734205
-					    &jump_labels_found))
734205
+		if (!should_keep_rela_group(lookup, relasec, src_offset, group_size))
734205
+			continue;
734205
+
734205
+		if (special->group_filter &&
734205
+		    !special->group_filter(lookup, relasec, src_offset, group_size))
734205
 			continue;
734205
 
734205
 		/*
734205
@@ -2557,9 +2623,13 @@ static void kpatch_regenerate_special_section(struct kpatch_elf *kelf,
734205
 		dest_offset += group_size;
734205
 	}
734205
 
734205
-	if (jump_labels_found)
734205
-		ERROR("Found %d jump label(s) in the patched code. Jump labels aren't currently supported. Use static_key_enabled() instead.",
734205
-		      jump_labels_found);
734205
+	if (jump_label_errors)
734205
+		ERROR("Found %d unsupported jump label(s) in the patched code. Use static_key_enabled() instead.",
734205
+		      jump_label_errors);
734205
+
734205
+	if (static_call_errors)
734205
+		ERROR("Found %d unsupported static call(s) in the patched code. Use KPATCH_STATIC_CALL() instead.",
734205
+		      static_call_errors);
734205
 
734205
 	if (!dest_offset) {
734205
 		/* no changed or global functions referenced */
734205
diff --git a/kpatch-build/log.h b/kpatch-build/log.h
734205
index eefa0fce7b08..dbdc212713e1 100644
734205
--- a/kpatch-build/log.h
734205
+++ b/kpatch-build/log.h
734205
@@ -9,7 +9,7 @@ extern enum loglevel loglevel;
734205
 extern char *childobj;
734205
 
734205
 #define ERROR(format, ...) \
734205
-	err(EXIT_STATUS_ERROR, "ERROR: %s: %s: %d: " format, childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__)
734205
+	errx(EXIT_STATUS_ERROR, "ERROR: %s: %s: %d: " format, childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__)
734205
 
734205
 #define log_debug(format, ...) log(DEBUG, format, ##__VA_ARGS__)
734205
 #define log_normal(format, ...) log(NORMAL, "%s: " format, childobj, ##__VA_ARGS__)
734205
-- 
734205
2.38.1
734205