Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux 内核模块编译错误 #define asm_inline asm __inline #2857

Closed
yinntian opened this issue Sep 22, 2022 · 7 comments
Closed

Linux 内核模块编译错误 #define asm_inline asm __inline #2857

yinntian opened this issue Sep 22, 2022 · 7 comments
Labels

Comments

@yinntian
Copy link
Contributor

Xmake 版本

xmake v2.7.1+202209191332

操作系统版本和架构

Linux wl-kernel 6.0.0-rc5 #1 SMP PREEMPT_DYNAMIC Sat Sep 17 14:58:17 CST 2022 x86_64 x86_64 x86_64 GNU/Linux

描述问题

使用copy_to_user函数后编译失败

#define asm_inline asm __inline

是不是缺少了编译选项导致宏错误

make编译没问题

期待的结果

编译通过

工程配置

xmake.lua

target("hello")
    add_rules("platform.linux.driver")
    add_files("src/*.c")
--     set_values("linux.driver.linux-headers", "/usr/src/linux-headers-6.0.0-rc5")
    set_values("linux.driver.linux-headers", "/usr/src/linux-headers-5.15.0-47-generic") 

main.c

#include <linux/module.h>     /* Needed by all modules */
#include <linux/init.h>       /* Needed for the macros */
#include <linux/fs.h>

static int __init module_load(void) {
    printk(KERN_DEBUG "Hello.\n");
    copy_to_user(NULL, NULL, 0);
    return 0;
}

附加信息和错误日志

xmake -vD

luna@wl-kernel:~/master/project/te$ xmake -vD
[ 25%]: cache compiling.release src/main.c
/usr/bin/gcc -c -DKBUILD_MODNAME=\"hello\" -isystem /usr/lib/gcc/x86_64-linux-gnu/11/include -I/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include -I/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include/generated -I/usr/src/linux-headers-5.15.0-47-generic/include -I/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include/uapi -I/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include/generated/uapi -I/usr/src/linux-headers-5.15.0-47-generic/include/uapi -I/usr/src/linux-headers-5.15.0-47-generic/include/generated/uapi -include /usr/src/linux-headers-5.15.0-47-generic/include/linux/compiler-version.h -include /usr/src/linux-headers-5.15.0-47-generic/include/linux/kconfig.h -I/usr/src/linux-headers-5.15.0-47-generic/ubuntu/include -include /usr/src/linux-headers-5.15.0-47-generic/include/linux/compiler_types.h -D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -DCONFIG_X86_X32_ABI -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -mfunction-return=thunk-extern -fno-jump-tables -mharden-sls=all -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -fno-allow-store-data-races -Wframe-larger-than=1024 -fstack-protector-strong -Wimplicit-fallthrough=5 -Wno-main -Wno-unused-but-set-variable -Wno-unused-const-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-stack-clash-protection -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fsanitize=bounds -fsanitize=shift -fsanitize=bool -fsanitize=enum -DMODULE -DKBUILD_BASENAME=\"main\" -o build/.objs/hello/linux/x86_64/release/src/main.c.o src/main.c
error: @programdir/modules/private/async/runjobs.lua:256: @programdir/modules/private/action/build/object.lua:91: @programdir/modules/core/tools/gcc.lua:676: src/main.c: In function ‘module_load’:
src/main.c:7:5: warning: ignoring return value of ‘copy_to_user’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    7 |     copy_to_user(NULL, NULL, 0);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from <command-line>:
/usr/src/linux-headers-5.15.0-47-generic/include/linux/thread_info.h: In function ‘copy_overflow’:
/usr/src/linux-headers-5.15.0-47-generic/include/linux/compiler_types.h:258:20: warning: ‘asm’ operand 2 probably does not match constraints
  258 | #define asm_inline asm __inline
      |                    ^~~
/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include/asm/bug.h:27:9: note: in expansion of macro ‘asm_inline’
   27 |         asm_inline volatile("1:\t" ins "\n"                             \
      |         ^~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include/asm/bug.h:82:9: note: in expansion of macro ‘_BUG_FLAGS’
   82 |         _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE);            \
      |         ^~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/include/asm-generic/bug.h:100:17: note: in expansion of macro ‘__WARN_FLAGS’
  100 |                 __WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
      |                 ^~~~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/include/asm-generic/bug.h:132:17: note: in expansion of macro ‘__WARN_printf’
  132 |                 __WARN_printf(TAINT_WARN, format);                      \
      |                 ^~~~~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/include/linux/thread_info.h:200:9: note: in expansion of macro ‘WARN’
  200 |         WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
      |         ^~~~
/usr/src/linux-headers-5.15.0-47-generic/include/linux/compiler_types.h:258:20: error: impossible constraint in ‘asm’
  258 | #define asm_inline asm __inline
      |                    ^~~
/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include/asm/bug.h:27:9: note: in expansion of macro ‘asm_inline’
   27 |         asm_inline volatile("1:\t" ins "\n"                             \
      |         ^~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include/asm/bug.h:82:9: note: in expansion of macro ‘_BUG_FLAGS’
   82 |         _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE);            \
      |         ^~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/include/asm-generic/bug.h:100:17: note: in expansion of macro ‘__WARN_FLAGS’
  100 |                 __WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
      |                 ^~~~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/include/asm-generic/bug.h:132:17: note: in expansion of macro ‘__WARN_printf’
  132 |                 __WARN_printf(TAINT_WARN, format);                      \
      |                 ^~~~~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/include/linux/thread_info.h:200:9: note: in expansion of macro ‘WARN’
  200 |         WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
      |         ^~~~
/usr/src/linux-headers-5.15.0-47-generic/include/linux/thread_info.h: In function ‘module_load’:
/usr/src/linux-headers-5.15.0-47-generic/include/linux/compiler_types.h:258:20: warning: ‘asm’ operand 2 probably does not match constraints
  258 | #define asm_inline asm __inline
      |                    ^~~
/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include/asm/bug.h:27:9: note: in expansion of macro ‘asm_inline’
   27 |         asm_inline volatile("1:\t" ins "\n"                             \
      |         ^~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/arch/x86/include/asm/bug.h:82:9: note: in expansion of macro ‘_BUG_FLAGS’
   82 |         _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE);            \
      |         ^~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/include/asm-generic/bug.h:106:17: note: in expansion of macro ‘__WARN_FLAGS’
  106 |                 __WARN_FLAGS(BUGFLAG_ONCE |                     \
      |                 ^~~~~~~~~~~~
/usr/src/linux-headers-5.15.0-47-generic/include/linux/thread_info.h:216:13: note: in expansion of macro ‘WARN_ON_ONCE’
  216 |         if (WARN_ON_ONCE(bytes > INT_MAX))
      |             ^~~~~~~~~~~~
stack traceback:
    [C]: in function 'error'
    [@programdir/core/base/os.lua:890]:
    [@programdir/modules/core/tools/gcc.lua:676]: in function 'catch'
    [@programdir/core/sandbox/modules/try.lua:123]: in function 'try'
    [@programdir/modules/core/tools/gcc.lua:617]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:280]:
    [@programdir/modules/private/action/build/object.lua:91]: in function 'script'
    [@programdir/modules/private/action/build/object.lua:104]: in function 'build_object'
    [@programdir/modules/private/action/build/object.lua:129]: in function 'jobfunc'
    [@programdir/modules/private/async/runjobs.lua:232]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:280]: in function 'trycall'
    [@programdir/core/sandbox/modules/try.lua:117]: in function 'try'
    [@programdir/modules/private/async/runjobs.lua:218]: in function 'cotask'
    [@programdir/core/base/scheduler.lua:388]:

stack traceback:
        [C]: in function 'error'
        @programdir/core/base/os.lua:890: in function 'base/os.raiselevel'
        (...tail calls...)
        @programdir/modules/private/async/runjobs.lua:256: in field 'catch'
        @programdir/core/sandbox/modules/try.lua:123: in global 'try'
        @programdir/modules/private/async/runjobs.lua:218: in upvalue 'cotask'
        @programdir/core/base/scheduler.lua:388: in function <@programdir/core/base/scheduler.lua:385>

xmake f -c -vD

luna@wl-kernel:~/master/project/te$ xmake f -c -vD
checking for platform ... linux
checking for architecture ... x86_64
checking for gcc ... /usr/bin/gcc
checkinfo: cannot runv(dmd --version), No such file or directory
checking for dmd ... no
checkinfo: cannot runv(ldc2 --version), No such file or directory
checking for ldc2 ... no
checkinfo: cannot runv(gdc --version), No such file or directory
checking for gdc ... no
checkinfo: cannot runv(zig version), No such file or directory
checking for zig ... no
checkinfo: cannot runv(zig version), No such file or directory
checking for zig ... no
checking for gcc ... /usr/bin/gcc
checking for the c compiler (cc) ... gcc
checking for make ... /usr/bin/make
configure
{
    host = linux
    ndk_stdcxx = true
    mode = release
    arch = x86_64
    plat = linux
    clean = true
    buildir = build
    ccache = true
    kind = static
}
@yinntian yinntian added the bug label Sep 22, 2022
@waruqi
Copy link
Member

waruqi commented Sep 22, 2022

我这里可以 没遇到什么问题,你可以跟 make 的 cflags 自己对下,然后手动复制命令执行,排查下 flags

@yinntian
Copy link
Contributor Author

还是这个问题,我发现使用xmake提供的包可以成功编译。
随后我从xmake-repo中提取命令手动构建内核,发现了个问题,这个错误可能和内核版本有关。

下面3个版本可以成功编译

  • 5.16.9
  • 5.10.46
  • 5.10.144

下面2个版本编译失败

  • 6.0-rc5
  • 5.15.69

下面代码是从xmake-repo中提取出来的

wget ...
tar ...
make allyesconfig
make modules_prepare

内核编译信息
kernel-build-5.10.144.txt
kernel-build-5.15.69.txt
xmake -vD信息
module-build-5.10.144.txt
module-build-5.15.69.txt

@waruqi
Copy link
Member

waruqi commented Sep 23, 2022

diff 了下 flags,出错的 flags 里面,多了一个 -Werror

而出错提示里 也跟 -Werror 有关

/home/luna/linux/kernel/linux-5.15.69/include/linux/thread_info.h: In function ‘copy_overflow’:
/home/luna/linux/kernel/linux-5.15.69/include/linux/compiler_types.h:270:20: error: ‘asm’ operand 2 probably does not match constraints [-Werror]
270 | #define asm_inline asm __inline
| ^~~

应该是不同 kernel 版本配置的 flags 差异导致,有的开了 -Werror, 感觉跟 xmake 也没啥关系,是 kernel 的问题。

image

@yinntian
Copy link
Contributor Author

感觉和-Werror关系不大,去掉后问题依旧,而且问题开头的错误日志中,flags里并没有-Werror。
我又测试了几个版本,5.16.10编译通过,5.16.11编译不通过,从错误日志中看,这两个的flags是一致的。

尝试使用make编译,均通过

#Makefile
obj-m += hello.o

hello-y := \
	src/main.o \


all:
	make -C /home/luna/linux/kernel/linux-5.16.11 M=$(PWD) modules

clean:
	make -C /home/luna/linux/kernel/linux-5.16.11 M=$(PWD) clean

module-build-5.16.10.txt
module-build-5.16.11.txt

@waruqi
Copy link
Member

waruqi commented Sep 26, 2022

从错误日志中看,这两个的flags是一致的。

要么是 kernel 自身 headers 导致的问题,要么是这两都缺失一些 flags 或者有不正确的 flags,但是因为某些原因,只有一个版本暴露了出来

尝试使用make编译,均通过

如果直接 make 可以,你可以跟 make 的 flags 做 diff 对比,如果 flags 还是完全一致,那跟 xmake 真没啥关系。。你可以自己复制 xmake -vD 里面的 command line 跟 make flags 挨个对,挨个试,很容易查出来问题,比每次贴 logs 让我猜快多了。。我这没法复现问题

@yinntian
Copy link
Contributor Author

翻了一下编译日志,发现多了几个flag
经测试,开启编译优化(-O)后编译通过,问题解决

#make
-D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -DCONFIG_X86_X32_ABI -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining -Wframe-larger-than=2048 -fstack-protector-strong -Werror -Wimplicit-fallthrough=5 -Wno-main -Wno-unused-but-set-variable -Wno-unused-const-variable -fno-stack-clash-protection -fzero-call-used-regs=used-gpr -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -fno-inline-functions-called-once -falign-functions=64 -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -Wno-alloc-size-larger-than -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned    -fsanitize=kernel-address -fasan-shadow-offset=0xdffffc0000000000   --param asan-globals=1   --param asan-instrumentation-with-call-threshold=0   --param asan-instrument-allocas=1   --param asan-stack=1  -fsanitize=bounds -fsanitize=shift -fsanitize=integer-divide-by-zero -fsanitize=bool -fsanitize=enum  -fsanitize-coverage=trace-pc -fsanitize-coverage=trace-cmp  -DMODULE  -DKBUILD_BASENAME='"main"' -DKBUILD_MODNAME='"hello"' -D__KBUILD_MODNAME=kmod_hello -c -o /home/luna/linux/test/src/main.o /home/luna/linux/test/src/main.c  ; ./tools/objtool/objtool orc generate  --module  --no-fp  --no-unreachable  --retpoline  --uaccess  /home/luna/linux/test/src/main.o
#xmake
-D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -DCONFIG_X86_X32_ABI -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -fno-allow-store-data-races -fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining -Wframe-larger-than=2048 -fstack-protector-strong -Werror -Wimplicit-fallthrough=5 -Wno-main -Wno-unused-but-set-variable -Wno-unused-const-variable -fno-stack-clash-protection -fzero-call-used-regs=used-gpr -mrecord-mcount -mfentry -DCC_USING_FENTRY -fno-inline-functions-called-once -falign-functions=64 -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -Wno-alloc-size-larger-than -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fsanitize=kernel-address -fasan-shadow-offset=0xdffffc0000000000 -fsanitize=bounds -fsanitize=shift -fsanitize=integer-divide-by-zero -fsanitize=bool -fsanitize=enum -fsanitize-coverage=trace-pc -fsanitize-coverage=trace-cmp -DMODULE -DKBUILD_BASENAME=\"main\" -o build/.objs/hello/linux/x86_64/release/src/main.c.o src/main.c

@jiandongg
Copy link

DavidingPlus added a commit to DavidingPlus/linux-kernel-learning that referenced this issue Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants