Something went wrong. Try again.
Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420#!/bin/bash# SPDX-License-Identifier: GPL-2.0# Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
# Shell functions for the rest of the scripts.
export LC_ALL=C
MAX_RETRIES=600RETRY_INTERVAL=".1" # secondsSYSFS_KERNEL_DIR="/sys/kernel"SYSFS_KLP_DIR="$SYSFS_KERNEL_DIR/livepatch"SYSFS_DEBUG_DIR="$SYSFS_KERNEL_DIR/debug"SYSFS_KPROBES_DIR="$SYSFS_DEBUG_DIR/kprobes"if [[ -e /sys/kernel/tracing/trace ]]; then SYSFS_TRACING_DIR="$SYSFS_KERNEL_DIR/tracing"else SYSFS_TRACING_DIR="$SYSFS_DEBUG_DIR/tracing"fi
# Kselftest framework requirement - SKIP code is 4ksft_skip=4
# log(msg) - write message to kernel log# msg - insightful wordsfunction log() { echo "$1" > /dev/kmsg}
# skip(msg) - testing can't proceed# msg - explanationfunction skip() { log "SKIP: $1" echo "SKIP: $1" >&2 exit $ksft_skip}
# root testfunction is_root() { uid=$(id -u) if [ $uid -ne 0 ]; then echo "skip all tests: must be run as root" >&2 exit $ksft_skip fi}
# Check if we can compile the modules before loading themfunction has_kdir() { if [ -z "$KDIR" ]; then KDIR="/lib/modules/$(uname -r)/build" fi
if [ ! -d "$KDIR" ]; then echo "skip all tests: KDIR ($KDIR) not available to compile modules." exit $ksft_skip fi}
# die(msg) - game over, man# msg - dying wordsfunction die() { log "ERROR: $1" echo "ERROR: $1" >&2 exit 1}
function push_config() { DYNAMIC_DEBUG=$(grep '^kernel/livepatch' "$SYSFS_DEBUG_DIR/dynamic_debug/control" | \ awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}') FTRACE_ENABLED=$(sysctl --values kernel.ftrace_enabled) KPROBE_ENABLED=$(cat "$SYSFS_KPROBES_DIR/enabled") TRACING_ON=$(cat "$SYSFS_TRACING_DIR/tracing_on") CURRENT_TRACER=$(cat "$SYSFS_TRACING_DIR/current_tracer") FTRACE_FILTER=$(cat "$SYSFS_TRACING_DIR/set_ftrace_filter")}
function pop_config() { if [[ -n "$DYNAMIC_DEBUG" ]]; then echo -n "$DYNAMIC_DEBUG" > "$SYSFS_DEBUG_DIR/dynamic_debug/control" fi if [[ -n "$FTRACE_ENABLED" ]]; then sysctl kernel.ftrace_enabled="$FTRACE_ENABLED" &> /dev/null fi if [[ -n "$KPROBE_ENABLED" ]]; then echo "$KPROBE_ENABLED" > "$SYSFS_KPROBES_DIR/enabled" fi if [[ -n "$TRACING_ON" ]]; then echo "$TRACING_ON" > "$SYSFS_TRACING_DIR/tracing_on" fi if [[ -n "$CURRENT_TRACER" ]]; then echo "$CURRENT_TRACER" > "$SYSFS_TRACING_DIR/current_tracer" fi if [[ -n "$FTRACE_FILTER" ]]; then echo "$FTRACE_FILTER" \ | sed -e "/#### all functions enabled ####/d" \ > "$SYSFS_TRACING_DIR/set_ftrace_filter" fi}
function set_dynamic_debug() { cat <<-EOF > "$SYSFS_DEBUG_DIR/dynamic_debug/control" file kernel/livepatch/* +p func klp_try_switch_task -p EOF}
function set_ftrace_enabled() { local can_fail=0 if [[ "$1" == "--fail" ]] ; then can_fail=1 shift fi
local err=$(sysctl -q kernel.ftrace_enabled="$1" 2>&1) local result=$(sysctl --values kernel.ftrace_enabled)
if [[ "$result" != "$1" ]] ; then if [[ $can_fail -eq 1 ]] ; then echo "livepatch: $err" | sed 's#/proc/sys/kernel/#kernel.#' > /dev/kmsg return fi
skip "failed to set kernel.ftrace_enabled = $1" fi
echo "livepatch: kernel.ftrace_enabled = $result" > /dev/kmsg}
function cleanup() { pop_config}
# setup_config - save the current config and set a script exit trap that# restores the original config. Setup the dynamic debug# for verbose livepatching output and turn on# the ftrace_enabled sysctl.function setup_config() { is_root has_kdir push_config set_dynamic_debug set_ftrace_enabled 1 trap cleanup EXIT INT TERM HUP}
# loop_until(cmd) - loop a command until it is successful or $MAX_RETRIES,# sleep $RETRY_INTERVAL between attempts# cmd - command and its arguments to runfunction loop_until() { local cmd="$*" local i=0 while true; do eval "$cmd" && return 0 [[ $((i++)) -eq $MAX_RETRIES ]] && return 1 sleep $RETRY_INTERVAL done}
function is_livepatch_mod() { local mod="$1"
if [[ ! -f "test_modules/$mod.ko" ]]; then die "Can't find \"test_modules/$mod.ko\", try \"make\"" fi
if [[ $(modinfo "test_modules/$mod.ko" | awk '/^livepatch:/{print $NF}') == "Y" ]]; then return 0 fi
return 1}
function __load_mod() { local mod="$1"; shift
local msg="% insmod test_modules/$mod.ko $*" log "${msg%% }" ret=$(insmod "test_modules/$mod.ko" "$@" 2>&1) if [[ "$ret" != "" ]]; then die "$ret" fi
# Wait for module in sysfs ... loop_until '[[ -e "/sys/module/$mod" ]]' || die "failed to load module $mod"}
# load_mod(modname, params) - load a kernel module# modname - module name to load# params - module parameters to pass to insmodfunction load_mod() { local mod="$1"; shift
is_livepatch_mod "$mod" && die "use load_lp() to load the livepatch module $mod"
__load_mod "$mod" "$@"}
# load_lp_nowait(modname, params) - load a kernel module with a livepatch# but do not wait on until the transition finishes# modname - module name to load# params - module parameters to pass to insmodfunction load_lp_nowait() { local mod="$1"; shift
is_livepatch_mod "$mod" || die "module $mod is not a livepatch"
__load_mod "$mod" "$@"
# Wait for livepatch in sysfs ... loop_until '[[ -e "$SYSFS_KLP_DIR/$mod" ]]' || die "failed to load module $mod (sysfs)"}
# load_lp(modname, params) - load a kernel module with a livepatch# modname - module name to load# params - module parameters to pass to insmodfunction load_lp() { local mod="$1"; shift
load_lp_nowait "$mod" "$@"
# Wait until the transition finishes ... loop_until 'grep -q '^0$' $SYSFS_KLP_DIR/$mod/transition' || die "failed to complete transition"}
# load_failing_mod(modname, params) - load a kernel module, expect to fail# modname - module name to load# params - module parameters to pass to insmodfunction load_failing_mod() { local mod="$1"; shift
local msg="% insmod test_modules/$mod.ko $*" log "${msg%% }" ret=$(insmod "test_modules/$mod.ko" "$@" 2>&1) if [[ "$ret" == "" ]]; then die "$mod unexpectedly loaded" fi log "$ret"}
# unload_mod(modname) - unload a kernel module# modname - module name to unloadfunction unload_mod() { local mod="$1"
# Wait for module reference count to clear ... loop_until '[[ $(cat "/sys/module/$mod/refcnt") == "0" ]]' || die "failed to unload module $mod (refcnt)"
log "% rmmod $mod" ret=$(rmmod "$mod" 2>&1) if [[ "$ret" != "" ]]; then die "$ret" fi
# Wait for module in sysfs ... loop_until '[[ ! -e "/sys/module/$mod" ]]' || die "failed to unload module $mod (/sys/module)"}
# unload_lp(modname) - unload a kernel module with a livepatch# modname - module name to unloadfunction unload_lp() { unload_mod "$1"}
# disable_lp(modname) - disable a livepatch# modname - module name to unloadfunction disable_lp() { local mod="$1"
log "% echo 0 > $SYSFS_KLP_DIR/$mod/enabled" echo 0 > "$SYSFS_KLP_DIR/$mod/enabled"
# Wait until the transition finishes and the livepatch gets # removed from sysfs... loop_until '[[ ! -e "$SYSFS_KLP_DIR/$mod" ]]' || die "failed to disable livepatch $mod"}
# set_pre_patch_ret(modname, pre_patch_ret)# modname - module name to set# pre_patch_ret - new pre_patch_ret valuefunction set_pre_patch_ret { local mod="$1"; shift local ret="$1"
log "% echo $ret > /sys/module/$mod/parameters/pre_patch_ret" echo "$ret" > /sys/module/"$mod"/parameters/pre_patch_ret
# Wait for sysfs value to hold ... loop_until '[[ $(cat "/sys/module/$mod/parameters/pre_patch_ret") == "$ret" ]]' || die "failed to set pre_patch_ret parameter for $mod module"}
function start_test { local test="$1"
# Dump something unique into the dmesg log, then stash the entry # in LAST_DMESG. The check_result() function will use it to # find new kernel messages since the test started. local last_dmesg_msg="livepatch kselftest timestamp: $(date --rfc-3339=ns)" log "$last_dmesg_msg" loop_until 'dmesg | grep -q "$last_dmesg_msg"' || die "buffer busy? can't find canary dmesg message: $last_dmesg_msg" LAST_DMESG=$(dmesg | grep "$last_dmesg_msg")
echo -n "TEST: $test ... " log "===== TEST: $test ====="}
# check_result() - verify dmesg output# TODO - better filter, out of order msgs, etc?function check_result { local expect="$*" local result
# Test results include any new dmesg entry since LAST_DMESG, then: # - include lines matching keywords # - exclude lines matching keywords # - filter out dmesg timestamp prefixes result=$(dmesg | awk -v last_dmesg="$LAST_DMESG" 'p; $0 == last_dmesg { p=1 }' | \ grep -e 'livepatch:' -e 'test_klp' | \ grep -v '\(tainting\|taints\) kernel' | \ sed 's/^\[[ 0-9.]*\] //' | \ sed 's/^\[[ ]*[CT][0-9]*\] //')
if [[ "$expect" == "$result" ]] ; then echo "ok" elif [[ "$result" == "" ]] ; then echo -e "not ok\n\nbuffer overrun? can't find canary dmesg entry: $LAST_DMESG\n" die "livepatch kselftest(s) failed" else echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n" die "livepatch kselftest(s) failed" fi}
# does_sysfs_exist(modname, attr) - check sysfs attribute existence# modname - livepatch module creating the sysfs interface# attr - attribute name to be checkedfunction does_sysfs_exist() { local mod="$1"; shift local attr="$1"; shift
[[ -f "$SYSFS_KLP_DIR/$mod/$attr" ]]}
# check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs# path permissions# modname - livepatch module creating the sysfs interface# rel_path - relative path of the sysfs interface# expected_rights - expected access rightsfunction check_sysfs_rights() { local mod="$1"; shift local rel_path="$1"; shift local expected_rights="$1"; shift
local path="$SYSFS_KLP_DIR/$mod/$rel_path" local rights=$(/bin/stat --format '%A' "$path") if test "$rights" != "$expected_rights" ; then die "Unexpected access rights of $path: $expected_rights vs. $rights" fi}
# check_sysfs_value(modname, rel_path, expected_value) - check sysfs value# modname - livepatch module creating the sysfs interface# rel_path - relative path of the sysfs interface# expected_value - expected value read from the filefunction check_sysfs_value() { local mod="$1"; shift local rel_path="$1"; shift local expected_value="$1"; shift
local path="$SYSFS_KLP_DIR/$mod/$rel_path" local value=`cat $path` if test "$value" != "$expected_value" ; then die "Unexpected value in $path: $expected_value vs. $value" fi}
# cleanup_tracing() - stop and clean up function tracingfunction cleanup_tracing() { echo 0 > "$SYSFS_TRACING_DIR/tracing_on" echo "" > "$SYSFS_TRACING_DIR/set_ftrace_filter" echo "nop" > "$SYSFS_TRACING_DIR/current_tracer" echo "" > "$SYSFS_TRACING_DIR/trace"}
# trace_function(function) - start tracing of a function# function - to be traced functionfunction trace_function() { local function="$1"; shift
cleanup_tracing
echo "function" > "$SYSFS_TRACING_DIR/current_tracer" echo "$function" > "$SYSFS_TRACING_DIR/set_ftrace_filter" echo 1 > "$SYSFS_TRACING_DIR/tracing_on"}
# check_traced_functions(functions...) - check whether each function appeared in the trace log# functions - list of functions to be checkedfunction check_traced_functions() { local function
for function in "$@"; do if ! grep -Fwq "$function" "$SYSFS_TRACING_DIR/trace" ; then die "Function ($function) did not appear in the trace" fi done
cleanup_tracing}