bcc icon indicating copy to clipboard operation
bcc copied to clipboard

asm __inline usage on newer kernels (5.10.x)

Open rostamn739 opened this issue 4 years ago • 5 comments

Trying to use newer kernel with headers (5.10)

OS: centos7 Kernel repo: elrepo

Error: left paren expected after asm, unable to parse asm __inline

Tried to compile bcc from source (using llvm-toolset-7.0 from scl), same thing

Quick steps to reproduce: install kernel-ml from elrepo, install kernel-ml-devel then install bcc-tools or use a source build. Then run /usr/share/bcc/tools/offcputime to get compiler error about asm __inline

rostamn739 avatar Jan 26 '21 11:01 rostamn739

I have not tried to reproduce the issue yet, could you paste the details of the error? Also, could you try the following patch to see whether it works for your or not?

diff --git a/src/cc/export/helpers.h b/src/cc/export/helpers.h
index 0447a956..a0388729 100644
--- a/src/cc/export/helpers.h
+++ b/src/cc/export/helpers.h
@@ -25,6 +25,11 @@ R"********(
 #define asm_inline asm
 #endif
 
+#ifdef __inline
+#undef __inline
+#define __inline
+#endif
+
 /* Before bpf_helpers.h is included, uapi bpf.h has been
  * included, which references linux/types.h. This may bring
  * in asm_volatile_goto definition if permitted based on

yonghong-song avatar Jan 27 '21 23:01 yonghong-song

I think I see the same problem on Debian Buster (Linux 5.10.0-0.bpo.3-amd64), also may be related: https://gitlab.com/gitlab-com/gl-infra/infrastructure/-/issues/12052

/usr/sbin/cachestat-bpfcc

In file included from /virtual/main.c:2:
In file included from include/uapi/linux/ptrace.h:143:
In file included from ./arch/x86/include/asm/ptrace.h:5:
./arch/x86/include/asm/segment.h:262:2: error: expected '(' after 'asm'
        alternative_io ("lsl %[seg],%[p]",
        ^
./arch/x86/include/asm/alternative.h:240:2: note: expanded from macro 'alternative_io'
        asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)   \
        ^
include/linux/compiler_types.h:245:24: note: expanded from macro 'asm_inline'
#define asm_inline asm __inline
                       ^
1 error generated.
Traceback (most recent call last):
  File "/usr/sbin/cachestat-bpfcc", line 95, in <module>
    b = BPF(text=bpf_text)
  File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 320, in __init__
    raise Exception("Failed to compile BPF text")
Exception: Failed to compile BPF text

frukto avatar Feb 25 '21 08:02 frukto

Does latest bcc (bcc trunk) still have the issue? It should have been fixed with the following patch

commit 2d1497cde1cc9835f759a707b42dea83bee378b8
Author: Ivan Babrou <[email protected]>
Date:   Thu Oct 10 20:24:21 2019 -0700

    Redefine asm_inline for Linux 5.4+, Fixes #2546 (#2547)
    
    Here's the upstream commit that introduced `asm_inline`:
    
    * https://github.com/torvalds/linux/commit/eb111869301e15b737315a46c913ae82bd19eb9d
    
    Without this patch BCC fails with the following:
    
    ```
    $ sudo /usr/share/bcc/tools/opensnoop
    In file included from /virtual/main.c:2:
    In file included from include/uapi/linux/ptrace.h:142:
    In file included from ./arch/x86/include/asm/ptrace.h:5:
    ./arch/x86/include/asm/segment.h:254:2: error: expected '(' after 'asm'
            alternative_io ("lsl %[seg],%[p]",
            ^
    ./arch/x86/include/asm/alternative.h:240:2: note: expanded from macro 'alternative_io'
            asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)   \
            ^
    include/linux/compiler_types.h:210:24: note: expanded from macro 'asm_inline'
                           ^

yonghong-song avatar Feb 25 '21 17:02 yonghong-song

I fixed this issue by undefining asm_inline

--- a/compiler_types.h +++ b/compiler_types.h @@ -242,7 +242,7 @@ struct ftrace_likely_data { #endif

#ifdef CONFIG_CC_HAS_ASM_INLINE +#define asm_inline asm -#define asm_inline asm __inline #else #define asm_inline asm #endif

dilfish avatar Aug 06 '21 11:08 dilfish

There is a workaround in samples/bpf/asm_goto_workaround.h.

/*
 * asm_inline is defined as asm __inline in "include/linux/compiler_types.h"
 * if supported by the kernel's CC (i.e CONFIG_CC_HAS_ASM_INLINE) which is not
 * supported by CLANG.
 */
#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
#endif

ESWZY avatar Apr 24 '22 16:04 ESWZY