Re: [RFC PATCH v6 10/12] livepatch/selftests: add __asm__ symbol renaming examples

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Feb 16, 2022 at 11:39:38AM -0500, Joe Lawrence wrote:
> GCC can rename symbols like static data and optimized functions, adding
> a suffix that includes illegal C characters.  Extend the klp-convert
> examples to demonstrate how to use __asm__ renaming from C code to
> create klp-relocations to such renamed symbols.
> 
> Signed-off-by: Joe Lawrence <joe.lawrence@xxxxxxxxxx>
> ---
>  lib/livepatch/test_klp_convert.h                    | 2 ++
>  lib/livepatch/test_klp_convert1.c                   | 8 ++++++++
>  lib/livepatch/test_klp_convert_mod_a.c              | 6 ++++++
>  lib/livepatch/test_klp_convert_mod_b.c              | 6 ++++++
>  tools/testing/selftests/livepatch/test-livepatch.sh | 4 ++++
>  5 files changed, 26 insertions(+)
> 
> diff --git a/lib/livepatch/test_klp_convert.h b/lib/livepatch/test_klp_convert.h
> index 5d97bc546d6e..42befbfd63cb 100644
> --- a/lib/livepatch/test_klp_convert.h
> +++ b/lib/livepatch/test_klp_convert.h
> @@ -10,5 +10,7 @@ extern char driver_name[];
>  extern char homonym_string[];
>  extern const char *get_homonym_string(void);
>  extern const char *test_klp_get_driver_name(void);
> +extern char klp_string_a[] __asm__("klp_string.12345");
> +extern char klp_string_b[] __asm__("klp_string.67890");
>  
>  #endif
> diff --git a/lib/livepatch/test_klp_convert1.c b/lib/livepatch/test_klp_convert1.c
> index cd2d3c638258..06926cf1c609 100644
> --- a/lib/livepatch/test_klp_convert1.c
> +++ b/lib/livepatch/test_klp_convert1.c
> @@ -25,6 +25,12 @@ void print_homonym_string(void)
>  	pr_info("get_homonym_string(), 1: %s\n", get_homonym_string());
>  }
>  
> +void print_static_strings(void)
> +{
> +	pr_info("klp_string.12345 = %s\n", klp_string_a);
> +	pr_info("klp_string.67890 = %s\n", klp_string_b);
> +}
> +
>  /* provide a sysfs handle to invoke debug functions */
>  static int print_debug;
>  static int print_debug_set(const char *val, const struct kernel_param *kp)
> @@ -32,6 +38,7 @@ static int print_debug_set(const char *val, const struct kernel_param *kp)
>  	print_saved_command_line();
>  	print_driver_name();
>  	print_homonym_string();
> +	print_static_strings();
>  
>  	return 0;
>  }
> @@ -67,6 +74,7 @@ KLP_MODULE_RELOC(test_klp_convert_mod) test_klp_convert_mod_relocs_a[] = {
>  	KLP_SYMPOS(homonym_string, 1),
>  	KLP_SYMPOS(get_homonym_string, 1),
>  	KLP_SYMPOS(test_klp_get_driver_name, 0),
> +	KLP_SYMPOS(klp_string_b, 1),
>  };
>  
>  static struct klp_func funcs[] = {
> diff --git a/lib/livepatch/test_klp_convert_mod_a.c b/lib/livepatch/test_klp_convert_mod_a.c
> index ae5e911fbb9b..9af0fcab0c8d 100644
> --- a/lib/livepatch/test_klp_convert_mod_a.c
> +++ b/lib/livepatch/test_klp_convert_mod_a.c
> @@ -20,6 +20,12 @@ __used static const char *get_homonym_string(void)
>  	return homonym_string;
>  }
>  
> +__used static void static_string_function(void)
> +{
> +	__used static char klp_string[] __asm__("klp_string.12345") =
> +		__FILE__ " static string";
> +}
> +
>  MODULE_LICENSE("GPL");
>  MODULE_AUTHOR("Joe Lawrence <joe.lawrence@xxxxxxxxxx>");
>  MODULE_DESCRIPTION("Livepatch test: klp-convert module");
> diff --git a/lib/livepatch/test_klp_convert_mod_b.c b/lib/livepatch/test_klp_convert_mod_b.c
> index 5eca8a4cae38..0a68e898fe03 100644
> --- a/lib/livepatch/test_klp_convert_mod_b.c
> +++ b/lib/livepatch/test_klp_convert_mod_b.c
> @@ -11,3 +11,9 @@ __used static const char *get_homonym_string(void)
>  {
>  	return homonym_string;
>  }
> +
> +__used static void static_string_function(void)
> +{
> +	__used static char klp_string[] __asm__("klp_string.67890") =
> +		__FILE__ " static string";
> +}
> diff --git a/tools/testing/selftests/livepatch/test-livepatch.sh b/tools/testing/selftests/livepatch/test-livepatch.sh
> index bcb8b468b80a..ec3b6c919b01 100755
> --- a/tools/testing/selftests/livepatch/test-livepatch.sh
> +++ b/tools/testing/selftests/livepatch/test-livepatch.sh
> @@ -200,6 +200,8 @@ $MOD_KLP_CONVERT1: driver_name, 0: $MOD_KLP_CONVERT_MOD
>  $MOD_KLP_CONVERT1: test_klp_get_driver_name(), 0: $MOD_KLP_CONVERT_MOD
>  $MOD_KLP_CONVERT1: homonym_string, 1: homonym string A
>  $MOD_KLP_CONVERT1: get_homonym_string(), 1: homonym string A
> +test_klp_convert1: klp_string.12345 = lib/livepatch/test_klp_convert_mod_a.c static string
> +test_klp_convert1: klp_string.67890 = lib/livepatch/test_klp_convert_mod_b.c static string
>  % echo 0 > /sys/kernel/livepatch/$MOD_KLP_CONVERT1/enabled
>  livepatch: '$MOD_KLP_CONVERT1': initializing unpatching transition
>  livepatch: '$MOD_KLP_CONVERT1': starting unpatching transition
> @@ -265,6 +267,8 @@ $MOD_KLP_CONVERT1: driver_name, 0: $MOD_KLP_CONVERT_MOD
>  $MOD_KLP_CONVERT1: test_klp_get_driver_name(), 0: $MOD_KLP_CONVERT_MOD
>  $MOD_KLP_CONVERT1: homonym_string, 1: homonym string A
>  $MOD_KLP_CONVERT1: get_homonym_string(), 1: homonym string A
> +test_klp_convert1: klp_string.12345 = lib/livepatch/test_klp_convert_mod_a.c static string
> +test_klp_convert1: klp_string.67890 = lib/livepatch/test_klp_convert_mod_b.c static string
>  % echo 0 > /sys/kernel/livepatch/$MOD_KLP_CONVERT1/enabled
>  livepatch: '$MOD_KLP_CONVERT1': initializing unpatching transition
>  livepatch: '$MOD_KLP_CONVERT1': starting unpatching transition
> -- 
> 2.26.3
> 

I'm not sure how portable it is using __asm__("new.name"), but it seems
to work with versions of GCC that I've tried.  Other tools like
kpatch-build can manipulate such symbols directly, but for anything that
needs to feed the C compiler, some kind of workaround is needed to
create klp-relocations for them.

FWIW, the trick works through the KLP_SYMPOS macro annotations as well,
for example:

  void pfn_valid_part_0(void) __asm__("pfn_valid.part.0");
  KLP_MODULE_RELOC(kvm) kvm_main_relocs[] = {
  	KLP_SYMPOS(pfn_valid_part_0, 0),
  };

-- Joe




[Index of Archives]     [Linux&nblp;USB Development]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite Secrets]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux