Re: [PATCH v2 1/2] gcov: fix clang-11+ support

2021-03-15 Thread Nathan Chancellor
On Fri, Mar 12, 2021 at 02:41:31PM -0800, Nick Desaulniers wrote:
> LLVM changed the expected function signatures for llvm_gcda_start_file()
> and llvm_gcda_emit_function() in the clang-11 release. Users of clang-11
> or newer may have noticed their kernels failing to boot due to a panic
> when enabling CONFIG_GCOV_KERNEL=y +CONFIG_GCOV_PROFILE_ALL=y.  Fix up
> the function signatures so calling these functions doesn't panic the
> kernel.
> 
> Link: https://reviews.llvm.org/rGcdd683b516d147925212724b09ec6fb792a40041
> Link: https://reviews.llvm.org/rG13a633b438b6500ecad9e4f936ebadf3411d0f44
> Cc: sta...@vger.kernel.org # 5.4
> Reported-by: Prasad Sodagudi 
> Suggested-by: Nathan Chancellor 
> Reviewed-by: Fangrui Song 
> Signed-off-by: Nick Desaulniers 
> Tested-by: Nathan Chancellor 

Reviewed-by: Nathan Chancellor 

> ---
> Changes V1 -> V2:
> * Use CONFIG_CLANG_VERSION instead of __clang_major__.
> * Pick up and retain Suggested-by, Tested-by, and Reviewed-by tags.
> * Drop note from commit message about `git blame`; I did what was
>   sugguested in V1, but it still looks to git like I wrote those
>   functions. Oh well.
> 
>  kernel/gcov/clang.c | 69 +
>  1 file changed, 69 insertions(+)
> 
> diff --git a/kernel/gcov/clang.c b/kernel/gcov/clang.c
> index c94b820a1b62..8743150db2ac 100644
> --- a/kernel/gcov/clang.c
> +++ b/kernel/gcov/clang.c
> @@ -75,7 +75,9 @@ struct gcov_fn_info {
>  
>   u32 num_counters;
>   u64 *counters;
> +#if CONFIG_CLANG_VERSION < 11
>   const char *function_name;
> +#endif
>  };
>  
>  static struct gcov_info *current_info;
> @@ -105,6 +107,7 @@ void llvm_gcov_init(llvm_gcov_callback writeout, 
> llvm_gcov_callback flush)
>  }
>  EXPORT_SYMBOL(llvm_gcov_init);
>  
> +#if CONFIG_CLANG_VERSION < 11
>  void llvm_gcda_start_file(const char *orig_filename, const char version[4],
>   u32 checksum)
>  {
> @@ -113,7 +116,17 @@ void llvm_gcda_start_file(const char *orig_filename, 
> const char version[4],
>   current_info->checksum = checksum;
>  }
>  EXPORT_SYMBOL(llvm_gcda_start_file);
> +#else
> +void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 
> checksum)
> +{
> + current_info->filename = orig_filename;
> + current_info->version = version;
> + current_info->checksum = checksum;
> +}
> +EXPORT_SYMBOL(llvm_gcda_start_file);
> +#endif
>  
> +#if CONFIG_CLANG_VERSION < 11
>  void llvm_gcda_emit_function(u32 ident, const char *function_name,
>   u32 func_checksum, u8 use_extra_checksum, u32 cfg_checksum)
>  {
> @@ -133,6 +146,24 @@ void llvm_gcda_emit_function(u32 ident, const char 
> *function_name,
>   list_add_tail(>head, _info->functions);
>  }
>  EXPORT_SYMBOL(llvm_gcda_emit_function);
> +#else
> +void llvm_gcda_emit_function(u32 ident, u32 func_checksum,
> + u8 use_extra_checksum, u32 cfg_checksum)
> +{
> + struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
> +
> + if (!info)
> + return;
> +
> + INIT_LIST_HEAD(>head);
> + info->ident = ident;
> + info->checksum = func_checksum;
> + info->use_extra_checksum = use_extra_checksum;
> + info->cfg_checksum = cfg_checksum;
> + list_add_tail(>head, _info->functions);
> +}
> +EXPORT_SYMBOL(llvm_gcda_emit_function);
> +#endif
>  
>  void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
>  {
> @@ -295,6 +326,7 @@ void gcov_info_add(struct gcov_info *dst, struct 
> gcov_info *src)
>   }
>  }
>  
> +#if CONFIG_CLANG_VERSION < 11
>  static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
>  {
>   size_t cv_size; /* counter values size */
> @@ -322,6 +354,28 @@ static struct gcov_fn_info *gcov_fn_info_dup(struct 
> gcov_fn_info *fn)
>   kfree(fn_dup);
>   return NULL;
>  }
> +#else
> +static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
> +{
> + size_t cv_size; /* counter values size */
> + struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
> + GFP_KERNEL);
> + if (!fn_dup)
> + return NULL;
> + INIT_LIST_HEAD(_dup->head);
> +
> + cv_size = fn->num_counters * sizeof(fn->counters[0]);
> + fn_dup->counters = vmalloc(cv_size);
> + if (!fn_dup->counters) {
> + kfree(fn_dup);
> + return NULL;
> + }
> +
> + memcpy(fn_dup->counters, fn->counters, cv_size);
> +
> + return fn_dup;
> +}
> +#endif
>  
>  /**
>   * gcov_info_dup - duplicate profiling data set
> @@ -362,6 +416,7 @@ struct gcov_info *gcov_info_dup(struct gcov_info *info)
>   * gcov_info_free - release memory for profiling data set duplicate
>   * @info: profiling data set duplicate to free
>   */
> +#if CONFIG_CLANG_VERSION < 11
>  void gcov_info_free(struct gcov_info *info)
>  {
>   struct gcov_fn_info *fn, *tmp;
> @@ -375,6 +430,20 @@ void gcov_info_free(struct gcov_info *info)
>   kfree(info->filename);
>   kfree(info);

Re: [PATCH v2 1/2] gcov: fix clang-11+ support

2021-03-15 Thread Peter Oberparleiter
On 12.03.2021 23:41, Nick Desaulniers wrote:
> LLVM changed the expected function signatures for llvm_gcda_start_file()
> and llvm_gcda_emit_function() in the clang-11 release. Users of clang-11
> or newer may have noticed their kernels failing to boot due to a panic
> when enabling CONFIG_GCOV_KERNEL=y +CONFIG_GCOV_PROFILE_ALL=y.  Fix up
> the function signatures so calling these functions doesn't panic the
> kernel.
> 
> Link: https://reviews.llvm.org/rGcdd683b516d147925212724b09ec6fb792a40041
> Link: https://reviews.llvm.org/rG13a633b438b6500ecad9e4f936ebadf3411d0f44
> Cc: sta...@vger.kernel.org # 5.4
> Reported-by: Prasad Sodagudi 
> Suggested-by: Nathan Chancellor 
> Reviewed-by: Fangrui Song 
> Signed-off-by: Nick Desaulniers 
> Tested-by: Nathan Chancellor 

Looks good to me (minus the code duplication - but that's IMO acceptable
since it's cleaned up again with patch 2).

Acked-by: Peter Oberparleiter 

That said, I'm currently thinking of adding a compile time check that
performs a dry-run gcov_info => gcda conversion in user space to detect
these kind of issues before kernels fail unpredictably [1]. I'm
confident that this could work for the GCC gcov kernel code, not sure
about the Clang version though. But if it's possible I guess it would
make sense to extend this to include the Clang code as well.

Note that this check wouldn't work for cross-compiles since the build
machine must be able to run code for the target machine.

[1]
https://lore.kernel.org/lkml/1c7a49e7-0e27-561b-a2f9-d42a83dc4...@linux.ibm.com/


Regards,
  Peter

-- 
Peter Oberparleiter
Linux on Z Development - IBM Germany


[PATCH v2 1/2] gcov: fix clang-11+ support

2021-03-12 Thread Nick Desaulniers
LLVM changed the expected function signatures for llvm_gcda_start_file()
and llvm_gcda_emit_function() in the clang-11 release. Users of clang-11
or newer may have noticed their kernels failing to boot due to a panic
when enabling CONFIG_GCOV_KERNEL=y +CONFIG_GCOV_PROFILE_ALL=y.  Fix up
the function signatures so calling these functions doesn't panic the
kernel.

Link: https://reviews.llvm.org/rGcdd683b516d147925212724b09ec6fb792a40041
Link: https://reviews.llvm.org/rG13a633b438b6500ecad9e4f936ebadf3411d0f44
Cc: sta...@vger.kernel.org # 5.4
Reported-by: Prasad Sodagudi 
Suggested-by: Nathan Chancellor 
Reviewed-by: Fangrui Song 
Signed-off-by: Nick Desaulniers 
Tested-by: Nathan Chancellor 
---
Changes V1 -> V2:
* Use CONFIG_CLANG_VERSION instead of __clang_major__.
* Pick up and retain Suggested-by, Tested-by, and Reviewed-by tags.
* Drop note from commit message about `git blame`; I did what was
  sugguested in V1, but it still looks to git like I wrote those
  functions. Oh well.

 kernel/gcov/clang.c | 69 +
 1 file changed, 69 insertions(+)

diff --git a/kernel/gcov/clang.c b/kernel/gcov/clang.c
index c94b820a1b62..8743150db2ac 100644
--- a/kernel/gcov/clang.c
+++ b/kernel/gcov/clang.c
@@ -75,7 +75,9 @@ struct gcov_fn_info {
 
u32 num_counters;
u64 *counters;
+#if CONFIG_CLANG_VERSION < 11
const char *function_name;
+#endif
 };
 
 static struct gcov_info *current_info;
@@ -105,6 +107,7 @@ void llvm_gcov_init(llvm_gcov_callback writeout, 
llvm_gcov_callback flush)
 }
 EXPORT_SYMBOL(llvm_gcov_init);
 
+#if CONFIG_CLANG_VERSION < 11
 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
u32 checksum)
 {
@@ -113,7 +116,17 @@ void llvm_gcda_start_file(const char *orig_filename, const 
char version[4],
current_info->checksum = checksum;
 }
 EXPORT_SYMBOL(llvm_gcda_start_file);
+#else
+void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 checksum)
+{
+   current_info->filename = orig_filename;
+   current_info->version = version;
+   current_info->checksum = checksum;
+}
+EXPORT_SYMBOL(llvm_gcda_start_file);
+#endif
 
+#if CONFIG_CLANG_VERSION < 11
 void llvm_gcda_emit_function(u32 ident, const char *function_name,
u32 func_checksum, u8 use_extra_checksum, u32 cfg_checksum)
 {
@@ -133,6 +146,24 @@ void llvm_gcda_emit_function(u32 ident, const char 
*function_name,
list_add_tail(>head, _info->functions);
 }
 EXPORT_SYMBOL(llvm_gcda_emit_function);
+#else
+void llvm_gcda_emit_function(u32 ident, u32 func_checksum,
+   u8 use_extra_checksum, u32 cfg_checksum)
+{
+   struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
+
+   if (!info)
+   return;
+
+   INIT_LIST_HEAD(>head);
+   info->ident = ident;
+   info->checksum = func_checksum;
+   info->use_extra_checksum = use_extra_checksum;
+   info->cfg_checksum = cfg_checksum;
+   list_add_tail(>head, _info->functions);
+}
+EXPORT_SYMBOL(llvm_gcda_emit_function);
+#endif
 
 void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
 {
@@ -295,6 +326,7 @@ void gcov_info_add(struct gcov_info *dst, struct gcov_info 
*src)
}
 }
 
+#if CONFIG_CLANG_VERSION < 11
 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
 {
size_t cv_size; /* counter values size */
@@ -322,6 +354,28 @@ static struct gcov_fn_info *gcov_fn_info_dup(struct 
gcov_fn_info *fn)
kfree(fn_dup);
return NULL;
 }
+#else
+static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
+{
+   size_t cv_size; /* counter values size */
+   struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
+   GFP_KERNEL);
+   if (!fn_dup)
+   return NULL;
+   INIT_LIST_HEAD(_dup->head);
+
+   cv_size = fn->num_counters * sizeof(fn->counters[0]);
+   fn_dup->counters = vmalloc(cv_size);
+   if (!fn_dup->counters) {
+   kfree(fn_dup);
+   return NULL;
+   }
+
+   memcpy(fn_dup->counters, fn->counters, cv_size);
+
+   return fn_dup;
+}
+#endif
 
 /**
  * gcov_info_dup - duplicate profiling data set
@@ -362,6 +416,7 @@ struct gcov_info *gcov_info_dup(struct gcov_info *info)
  * gcov_info_free - release memory for profiling data set duplicate
  * @info: profiling data set duplicate to free
  */
+#if CONFIG_CLANG_VERSION < 11
 void gcov_info_free(struct gcov_info *info)
 {
struct gcov_fn_info *fn, *tmp;
@@ -375,6 +430,20 @@ void gcov_info_free(struct gcov_info *info)
kfree(info->filename);
kfree(info);
 }
+#else
+void gcov_info_free(struct gcov_info *info)
+{
+   struct gcov_fn_info *fn, *tmp;
+
+   list_for_each_entry_safe(fn, tmp, >functions, head) {
+   vfree(fn->counters);
+   list_del(>head);
+   kfree(fn);
+   }
+   kfree(info->filename);
+