kABI简介
kABI: Kernel Application Binary Interface,是内核为用户空间程序提供的稳定的二进制接口。kABI兼容,即内核与驱动的二进制兼容。如果驱动用到的内核接口都是兼容的,那么驱动就可以不用重新编译就可以在新版本安装使用。当然,kABI兼容也会带来架构腐化、维护时间长、维护成本高等问题。
kABI白名单
kABI白名单是内核提供的对外接口的子集。kernel研发过程中各个版本之间kABI白名单内的接口保持不变,那么只使用白名单内的内核函数的驱动就无需重新编译,即可在新版本内核中使用。
如何创建kABI白名单
拟出需兼容的驱动列表,通过modprobe等工具得到驱动所设计的函数,筛选出其中的内核函数。
$ modprobe --dump-modversions megaraid_sas.ko
0x79d6acc4 module_layout
0x6bc3fbc0 __unregister_chrdev
0x2d3385d3 system_wq
0x99b8fd62 kmalloc_caches # 内核函数
...
再从驱动编译版本内核的Modules.symvers筛选出函数信息即可得到kABI白名单Module.kabi
$ cat Module.symvers | grep kmalloc_caches
0x00000000 kmalloc_caches vmlinux EXPORT_SYMBOL
检查kABI兼容性
此处使用CentOS 7.x的开源工具check-kabi,进行kABI兼容性检查。
#!/usr/bin/python
#
# check-kabi - Red Hat kABI reference checking tool
#
# We use this script to check against reference Module.kabi files.
#
# Author: Jon Masters <jcm@redhat.com>
# Copyright (C) 2007-2009 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU# General Public License (GPL).
# Changelog:
#
# 2009/08/15 - Updated for use in RHEL6.
# 2007/06/13 - Initial rewrite in python by Jon Masters.
__author__ = "Jon Masters <jcm@redhat.com>"
__version__ = "2.0"
__date__ = "2009/08/15"
__copyright__ = "Copyright (C) 2007-2009 Red Hat, Inc"
__license__ = "GPL"
import getopt
import os
import re
import string
import sys
true = 1
false = 0
def load_symvers(symvers,filename):
"""Load a Module.symvers file."""
symvers_file = open(filename,"r")
while true:
in_line = symvers_file.readline()
if in_line == "":
break
if in_line == "\n":
continue
checksum,symbol,directory,type = string.split(in_line)
symvers[symbol] = in_line[0:-1]
def load_kabi(kabi,filename):
"""Load a Module.kabi file."""
kabi_file = open(filename,"r")
while true:
in_line = kabi_file.readline()
if in_line == "":
break
if in_line == "\n":
continue
checksum,symbol,directory,type = string.split(in_line)
kabi[symbol] = in_line[0:-1]
def check_kabi(symvers,kabi):
"""Check Module.kabi and Module.symvers files."""
fail=0
warn=0
lost=0
changed_symbols=[]
moved_symbols=[]
losted_symbols=[]
for symbol in kabi:
abi_hash,abi_sym,abi_dir,abi_type = string.split(kabi[symbol])
if symvers.has_key(symbol):
sym_hash,sym_sym,sym_dir,sym_type = string.split(symvers[symbol])
if abi_hash != sym_hash:
fail=1
changed_symbols.append(symbol)
if abi_dir != sym_dir:
warn=1
moved_symbols.append(symbol)
else:
lost=1
losted_symbols.append(symbol)
if fail:
print "*** ERROR - ABI BREAKAGE WAS DETECTED ***"
print ""
print "The following symbols have been changed (this will cause an ABI breakage):"
print "new kabi:"
for symbol in changed_symbols:
print symvers[symbol]
print "old kabi:"
for symbol in changed_symbols:
print kabi[symbol]
print ""
if lost:
print "*** ERROR - ABI BREAKAGE WAS DETECTED ***"
print ""
print "The following symbols have been losted (this will cause an ABI breakage):"
print "old kabi:"
for symbol in losted_symbols:
print kabi[symbol]
print ""
if warn:
print "*** WARNING - ABI SYMBOLS MOVED ***"
print ""
print "The following symbols moved (typically caused by moving a symbol from being"
print "provided by the kernel vmlinux out to a loadable module):"
print "new kabi:"
for symbol in moved_symbols:
print symvers[symbol]
print "old kabi"
for symbol in moved_symbols:
print kabi[symbol]
print ""
"""Halt the build, if we got errors and/or warnings. In either case,
double-checkig is required to avoid introducing / concealing
KABI inconsistencies."""
if fail or warn or lost:
sys.exit(1)
sys.exit(0)
def usage():
print """
check-kabi: check Module.kabi and Module.symvers files.
check-kabi [ -k Module.kabi ] [ -s Module.symvers ]
"""
if __name__ == "__main__":
symvers_file = ""
kabi_file = ""
opts, args = getopt.getopt(sys.argv[1:], 'hk:s:')
for o, v in opts:
if o == "-s":
symvers_file = v
if o == "-h":
usage()
sys.exit(0)
if o == "-k":
kabi_file = v
if (symvers_file == "") or (kabi_file == ""):
usage()
sys.exit(1)
symvers={}
kabi={}
load_symvers(symvers,symvers_file)
load_kabi(kabi,kabi_file)
check_kabi(symvers,kabi)
依据kABI兼容性实现原理,此python脚本将当前符号表中函数信息与kABI白名单中的函数信息进行对比。如果相关函数发生变化,将输出对应信息且需要相对应的kabi fix进行修复;无打印信息则表示kABI验证通过。check-kabi脚本使用方法如下。
python2 check-kabi -k Module.kabi -s Module.symvers