最近在6.12上开放了一个内核模块,在7.0上编译的时候,有很多报错。
因为内核的接口一直在变。需要使用内核版本宏来控制。
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 16, 0)
int cfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
int cfs_fileattr_set(struct mnt_idmap *_idmap, struct dentry *dentry, struct file_kattr *fa);
#else
int cfs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
int cfs_fileattr_set(struct mnt_idmap *_idmap, struct dentry *dentry, struct fileattr *fa);
#endif
linux/verision.h是一个编译生成的头文件。位于build目录的。
include/generated/uapi/linux/version.h
$ cat ../qemu/linux-6.12.37/include/generated/uapi/linux/version.h
#define LINUX_VERSION_CODE 396325
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c)))
#define LINUX_VERSION_MAJOR 6
#define LINUX_VERSION_PATCHLEVEL 12
#define LINUX_VERSION_SUBLEVEL 37
怎么确定一个提交引入的内核版本呢。
首先找到这个提交,所在的文件,比如
include/uapi/linux/fcntl.h删除了一个宏定义。
查看下面的日志,
$ git log -p --first-parent Makefile include/uapi/linux/fcntl.h
注意--first-parent非常重要,只看主分支,不看merge 开发者的分支。
commit c6d64479d6093a5c3d709d4cc992a5344877cc3c Merge: 9fb2cfa4635a 6c056ae4b275 Author: Linus Torvalds <torvalds@linux-foundation.org> Date: Mon Nov 18 14:54:10 2024 -0800 Merge tag 'pull-statx' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs Pull statx updates from Al Viro: "Sanitize struct filename and lookup flags handling in statx and friends" * tag 'pull-statx' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: libfs: kill empty_dir_getattr() fs: Simplify getattr interface function checking AT_GETATTR_NOSEC flag fs/stat.c: switch to CLASS(fd_raw) kill getname_statx_lookup_flags() io_statx_prep(): use getname_uflags() diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h index 87e2dec79fea..a40833bf2855 100644 --- a/include/uapi/linux/fcntl.h +++ b/include/uapi/linux/fcntl.h @@ -154,8 +154,4 @@ usable with open_by_handle_at(2). */ #define AT_HANDLE_MNT_ID_UNIQUE 0x001 /* Return the u64 unique mount ID. */ -#if defined(__KERNEL__) -#define AT_GETATTR_NOSEC 0x80000000 -#endif -
找到那个提交,然后使用。--oneline
$ git log --oneline --first-parent Makefile include/uapi/linux/fcntl.h
...
ffd294d346d1 Linux 6.13
5bc55a333a2f Linux 6.13-rc7
9d89551994a4 Linux 6.13-rc6
fc033cf25e61 Linux 6.13-rc5
4bbf9020becb Linux 6.13-rc4
78d4f34e2115 Linux 6.13-rc3
fac04efc5c79 Linux 6.13-rc2
40384c840ea1 Linux 6.13-rc1
6a34dfa15d6e Merge tag 'kbuild-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild
b5361254c902 Merge tag 'modules-6.13-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/modules/linux
798bb342e041 Merge tag 'rust-6.13' of https://github.com/Rust-for-Linux/linux
1675db5c42b7 Merge tag 'vfs-6.13.exportfs' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
c6d64479d609 Merge tag 'pull-statx' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
adc218676eef Linux 6.12
2d5404caa8c7 (tag: v6.12-rc7) Linux 6.12-rc7
看前后的Makefile版本:
可以看到是6.12版本发布后的合入。此时已经是6.13的合入窗口了,属于6.13版本引入的特性。
但是看合入的那个Makefile,仍然是6.12,因为6.13-rc1还未发布。
$ git show c6d64479d609:Makefile
# SPDX-License-Identifier: GPL-2.0
VERSION = 6
PATCHLEVEL = 12
SUBLEVEL = 0
EXTRAVERSION =
NAME = Baby Opossum Posse