From e62f52c9d549a5d13298f55cdb2dcd90468e673e Mon Sep 17 00:00:00 2001 From: Vyacheslav Cherkashin Date: Wed, 4 Oct 2017 17:59:03 +0300 Subject: [PATCH] KI: introduce script and README for integration in to kernel Change-Id: I14b84ab040a371c3bbb630ec8a8179246186fe48 Signed-off-by: Vyacheslav Cherkashin --- kernel_integration/README.md | 25 ++ ...epare-kernel-for-swap-modules-integration.patch | 112 +++++++ ...Prepare-kernel-for-swap-hooks-integration.patch | 40 +++ kernel_integration/sync_swap_to_kernel.sh | 344 +++++++++++++++++++++ 4 files changed, 521 insertions(+) create mode 100644 kernel_integration/README.md create mode 100644 kernel_integration/patches/0001-Prepare-kernel-for-swap-modules-integration.patch create mode 100644 kernel_integration/patches/0002-Prepare-kernel-for-swap-hooks-integration.patch create mode 100755 kernel_integration/sync_swap_to_kernel.sh diff --git a/kernel_integration/README.md b/kernel_integration/README.md new file mode 100644 index 0000000..dd15f7a --- /dev/null +++ b/kernel_integration/README.md @@ -0,0 +1,25 @@ +# How-to integrate swap-modules to kernel. + + +## Two cases are possible: +### 1. swap-modules to kernel initial integration, do the following steps: + * #### prepare kernel for swap-modules integration: + + ```sh + $ cd + $ git am /kernel_integration/patches/*.patch + ``` + In case of patch conflicts you have to solve it manually + + * #### sync swap-modules to kernel: + + ```sh + $ ./sync_swap_to_kernel.sh + ``` + + +### 2. swap-modules changes synchronization to kernel, do the following step: + +```sh +$ ./sync_swap_to_kernel.sh +``` diff --git a/kernel_integration/patches/0001-Prepare-kernel-for-swap-modules-integration.patch b/kernel_integration/patches/0001-Prepare-kernel-for-swap-modules-integration.patch new file mode 100644 index 0000000..a583520 --- /dev/null +++ b/kernel_integration/patches/0001-Prepare-kernel-for-swap-modules-integration.patch @@ -0,0 +1,112 @@ +From 4300c8d2fbd666ae42c73a3bf62f1754a7e83786 Mon Sep 17 00:00:00 2001 +From: Your Name +Date: Wed, 4 Oct 2017 14:30:52 +0300 +Subject: [PATCH 1/9] Prepare kernel for swap-modules integration + +--- + arch/arm/Kconfig.debug | 1 + + arch/arm64/Kconfig.debug | 1 + + arch/x86/Kconfig.debug | 1 + + kernel/Makefile | 1 + + kernel/swap/Kconfig | 12 ++++++++++++ + kernel/swap/Makefile | 5 +++++ + kernel/swap/modules/Makefile | 5 +++++ + kernel/swap/modules/empty.c | 0 + 8 files changed, 26 insertions(+) + create mode 100644 kernel/swap/Kconfig + create mode 100644 kernel/swap/Makefile + create mode 100644 kernel/swap/modules/Makefile + create mode 100644 kernel/swap/modules/empty.c + +diff --git a/arch/arm/Kconfig.debug b/arch/arm/Kconfig.debug +index 439d17d..c8ea9da 100644 +--- a/arch/arm/Kconfig.debug ++++ b/arch/arm/Kconfig.debug +@@ -1,5 +1,6 @@ + menu "Kernel hacking" + ++source "kernel/swap/Kconfig" + source "lib/Kconfig.debug" + + config ARM_PTDUMP +diff --git a/arch/arm64/Kconfig.debug b/arch/arm64/Kconfig.debug +index 5fdd6dc..0aeac7f 100644 +--- a/arch/arm64/Kconfig.debug ++++ b/arch/arm64/Kconfig.debug +@@ -1,5 +1,6 @@ + menu "Kernel hacking" + ++source "kernel/swap/Kconfig" + source "lib/Kconfig.debug" + + config FRAME_POINTER +diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug +index 61bd2ad..b7e8d6a 100644 +--- a/arch/x86/Kconfig.debug ++++ b/arch/x86/Kconfig.debug +@@ -3,6 +3,7 @@ menu "Kernel hacking" + config TRACE_IRQFLAGS_SUPPORT + def_bool y + ++source "kernel/swap/Kconfig" + source "lib/Kconfig.debug" + + config STRICT_DEVMEM +diff --git a/kernel/Makefile b/kernel/Makefile +index 17ea6d4..61c1552 100644 +--- a/kernel/Makefile ++++ b/kernel/Makefile +@@ -89,6 +89,7 @@ obj-$(CONFIG_CPU_PM) += cpu_pm.o + obj-$(CONFIG_BPF) += bpf/ + + obj-$(CONFIG_PERF_EVENTS) += events/ ++obj-$(CONFIG_SWAP_DA) += swap/ + + obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o + obj-$(CONFIG_PADATA) += padata.o +diff --git a/kernel/swap/Kconfig b/kernel/swap/Kconfig +new file mode 100644 +index 0000000..fb06289 +--- /dev/null ++++ b/kernel/swap/Kconfig +@@ -0,0 +1,12 @@ ++# ++# General SWAP options ++# ++ ++menuconfig SWAP_DA ++ bool "System Wide Analysis of Performance" ++ select KALLSYMS ++ select DEBUG_FS ++ help ++ SWAP is a profiling tool. ++ ++ If unsure, say Y. +diff --git a/kernel/swap/Makefile b/kernel/swap/Makefile +new file mode 100644 +index 0000000..377288f +--- /dev/null ++++ b/kernel/swap/Makefile +@@ -0,0 +1,5 @@ ++# ++# Makefile for the SWAP ++# ++ ++obj-y += modules/ +diff --git a/kernel/swap/modules/Makefile b/kernel/swap/modules/Makefile +new file mode 100644 +index 0000000..95799a9 +--- /dev/null ++++ b/kernel/swap/modules/Makefile +@@ -0,0 +1,5 @@ ++# ++# Makefile for the SWAP modules ++# ++ ++obj-y += empty.o +diff --git a/kernel/swap/modules/empty.c b/kernel/swap/modules/empty.c +new file mode 100644 +index 0000000..e69de29 +-- +2.1.4 + diff --git a/kernel_integration/patches/0002-Prepare-kernel-for-swap-hooks-integration.patch b/kernel_integration/patches/0002-Prepare-kernel-for-swap-hooks-integration.patch new file mode 100644 index 0000000..c9c2625 --- /dev/null +++ b/kernel_integration/patches/0002-Prepare-kernel-for-swap-hooks-integration.patch @@ -0,0 +1,40 @@ +From 91deb09957548909c65981810bf19fe837041275 Mon Sep 17 00:00:00 2001 +From: Your Name +Date: Wed, 4 Oct 2017 20:28:56 +0300 +Subject: [PATCH 2/9] Prepare kernel for swap-hooks integration + +--- + kernel/swap/Makefile | 1 + + kernel/swap/hooks/Makefile | 5 +++++ + kernel/swap/hooks/empty.c | 0 + 3 files changed, 6 insertions(+) + create mode 100644 kernel/swap/hooks/Makefile + create mode 100644 kernel/swap/hooks/empty.c + +diff --git a/kernel/swap/Makefile b/kernel/swap/Makefile +index 377288f..7315477 100644 +--- a/kernel/swap/Makefile ++++ b/kernel/swap/Makefile +@@ -2,4 +2,5 @@ + # Makefile for the SWAP + # + ++obj-y += hooks/ + obj-y += modules/ +diff --git a/kernel/swap/hooks/Makefile b/kernel/swap/hooks/Makefile +new file mode 100644 +index 0000000..34f305f +--- /dev/null ++++ b/kernel/swap/hooks/Makefile +@@ -0,0 +1,5 @@ ++# ++# Makefile for hooks ++# ++ ++obj-y += empty.o +diff --git a/kernel/swap/hooks/empty.c b/kernel/swap/hooks/empty.c +new file mode 100644 +index 0000000..e69de29 +-- +2.1.4 + diff --git a/kernel_integration/sync_swap_to_kernel.sh b/kernel_integration/sync_swap_to_kernel.sh new file mode 100755 index 0000000..83f250b --- /dev/null +++ b/kernel_integration/sync_swap_to_kernel.sh @@ -0,0 +1,344 @@ +#!/bin/bash + + +get_tmp() { + mktemp /tmp/sync_swap_to_kernel_hohoho.XXXXXXXXXX +} + +rm_tmp() { + rm -f /tmp/sync_swap_to_kernel_hohoho.* +} + + + +CALL() { + cmd=$@ + + $cmd + if [ $? != 0 ] ; then + echo "### ERROR: Cannot call >$cmd<" >&2 + rm_tmp + exit 1 + fi + + return 0 +} + + +rm_val() { + file=$1 + val=$2 + + tmp=$(get_tmp) + cpf=$(get_tmp) + + CALL cp $file $cpf + while true; do + max_a=$(cat $cpf | wc -l) + n1=$(cat $cpf | grep -n "^$val[[:space:]+:=]" | head -1 | cut -f1 -d:) + s=$(cat $cpf | grep -A$max_a "$val" | grep -nv '\$' | head -1 | cut -f1 -d:) + + if [ "$n1" == "" ] ; then + break + fi + + n2=$(($n1 + $s - 1)) + + cat $cpf | sed "$n1,${n2}d" > $tmp + mv $tmp $cpf + done + + cat $cpf +} + +rm_null_val() { + file=$1 + val=$2 + + rm_null_val_tmp=$(get_tmp) + + while true; do + n=$(cat $file | grep -n "^$val[[:space:]+:=]" | grep "=[[:space:]]*$" | head -1 | cut -f1 -d:) + if [ "$n" == "" ] ; then + break + fi + + cat $file | sed $n,${n}d > $rm_null_val_tmp + mv $rm_null_val_tmp $file + done + + cat $file +} + +gen_ccflags() { + echo "ccflags-y := -Ikernel/swap/modules" + + echo "" + echo "ifeq (\$(CONFIG_ARM), y)" + echo -e " link = arm" + echo "endif" + + echo "" + echo "ifeq (\$(CONFIG_ARM64), y)" + echo -e " link = arm64" + echo "endif" + + echo "" + echo "ifeq (\$(CONFIG_X86), y)" + echo -e " link = x86" + echo "endif" + + echo "" + echo -e "ccflags-y += -Ikernel/swap/modules/kprobe/arch/\$(link) \\" + echo -e " -Ikernel/swap/modules/uprobe/arch/\$(link)" +} + +kb_to_make() { + kbuild=$1 + + kb_tmp1=$(get_tmp) + kb_tmp2=$(get_tmp) + kb_tmp3=$(get_tmp) + + # remove KBUILD_EXTRA_SYMBOLS + rm_val $kbuild KBUILD_EXTRA_SYMBOLS > $kb_tmp1 + + # replace 'obj-m' > 'obj-y' + cat $kb_tmp1 | sed -e 's/obj-m/obj-y/g' > $kb_tmp2 + + # remove '$(extra_cflags)' + cat $kb_tmp2 | sed -e 's/$(extra_cflags)//g' > $kb_tmp3 + + # remove 'EXTRA_CFLAGS = ' + rm_null_val $kb_tmp3 EXTRA_CFLAGS > $kb_tmp1 + + # add include path + gen_ccflags > $kb_tmp3 + + cat $kb_tmp1 >> $kb_tmp3 + + # replace 'EXTRA_CFLAGS' > 'ccflags-y' + cat $kb_tmp3 | sed -e 's/^EXTRA_CFLAGS/ccflags-y /g' +} + +check_dir() { + black_dirs="\ +modules/kprobe/arch/asm-mips \ +modules/ks_manager \ +" + + dir=$1 + + for d in $black_dirs; do + if [ "$dir" == "$d" ]; then + return 1 + fi + done + + return 0 +} + +check_file() { + black_files="\ +modules/build.sh \ +modules/build.config.example \ +modules/deploy.sh \ +modules/pack.sh \ +" + + file=$1 + + for ff in $black_files; do + if [ "$file" == "$ff" ]; then + return 1 + fi + done + + return 0 +} + +cp_main_kb() { + in=$1 + out=$2 + cp_tmp=$(get_tmp) + + echo "#" > $out + echo "# Makefile for the SWAP modules" >> $out + echo "#" >> $out + + # remove EXTRA_CFLAGS, extra_cflags, ks_manager + cat $in | sed -e /EXTRA_CFLAGS/d | sed -e /extra_cflags/d | sed -e /ks_manager/d > $cp_tmp + + # replace 'obj-m :=' > 'obj-y +=' + cat $cp_tmp | sed -e 's|obj-m :=|obj-y +=|g' >> $out +} + +rm_lvc() { + kswap=$1 + mf=$2 + + VERSION=$(cat $mf | grep "^VERSION" | cut -d= -f2) + PATCHLEVEL=$(cat $mf | grep "^PATCHLEVEL" | cut -d= -f2) + SUBLEVEL=$(cat $mf | grep "^SUBLEVEL" | cut -d= -f2) + lvc=$(($VERSION * 65536 + $PATCHLEVEL * 256 + SUBLEVEL)) + + tmp_ver=$(get_tmp) + + cd $kswap + + grep -Irn "KERNEL_VERSION([^)]*)" | grep -v "*/" | grep -o "KERNEL_VERSION([^)]*)" | sort -u | sed 's|KERNEL_VERSION(||;s/)\|,/ /g' | awk '{print "s|KERNEL_VERSION(" $1 ", " $2 ", " $3 ")\\()*[[:blank:]]*\\(/\\*[^*]*\\*/\\)\\?\\)$|" $1*65536+$2*256+$3 "\\1|"}' > $tmp_ver + find . -regex '.*/.*\.\(c\|h\)$' -exec sed -f $tmp_ver -i {} \; + find . -regex '.*/.*\.\(c\|h\)$' -exec unifdef -m -DLINUX_VERSION_CODE=$lvc {} \; +} + +update_modules() +{ + echo "### UPDATE MODULES ###" + + # check kernel path + if [ ! -f $KERNEL_DIR/.git/config ]; then + echo "Don't true path to kernel $KERNEL_DIR" + exit 1 + fi + + # check Makefile + kmakefile=$KERNEL_DIR/Makefile + if [ ! -f $kmakefile ]; then + echo "Makefile not found" + exit 1 + fi + + # remove modules from kernel + kmodules_dir=$KERNEL_DIR/kernel/swap/modules/ + CALL git rm -r "$kmodules_dir" > /dev/null + + CALL cd $SWAP_DIR + files=$(CALL git ls-files modules/) + for f in $files; do + sf=$SWAP_DIR/$f + kf=$kmodules_dir/$(echo $f | sed 's|^modules/||') + file_name=$(basename $f) + dir_name=$(dirname $f) + + # skip files + check_file $f + if [ $? != 0 ] ; then + echo "skip file: $f" + continue + fi + + # skip directories + check_dir $dir_name + if [ $? != 0 ] ; then + echo "skip directory: $dir_name" + continue + fi + + # modification Kbuild + if [ "$file_name" == "Kbuild" ]; then + dir_name=$(dirname $f) + kf=$(echo $kf | sed 's/Kbuild/Makefile/') + + # check main Kbuild + if [ "$dir_name" == "modules" ]; then + cp_main_kb $sf $kf + continue + fi + + # make Kbuild from Makefile + CALL mkdir -p $(dirname $kf) + kb_to_make $sf > $kf + + continue + fi + + # other files + CALL mkdir -p $(dirname $kf) + CALL cp $sf $kf + done + + rm_lvc $kmodules_dir $kmakefile + + CALL cd $KERNEL_DIR + CALL git add kernel/swap/modules/. +} + +update_hooks() { + echo "### UPDATE HOOKS ###" + + # remove old hooks + CALL cd $KERNEL_DIR + CALL git rm -r $KERNEL_DIR/include/swap/ > /dev/null + CALL git rm -r $KERNEL_DIR/kernel/swap/hooks/ > /dev/null + CALL git rm $KERNEL_DIR/kernel/swap/Makefile > /dev/null + CALL git rm $KERNEL_DIR/kernel/swap/Kconfig > /dev/null + + # copy new hooks + CALL cd $SWAP_DIR + files=$(CALL git ls-files kernel_integration/hooks/) + for f in $files; do + kf=$KERNEL_DIR/$(echo $f | sed 's|^kernel_integration/hooks/||') + sf=$SWAP_DIR/$f + + CALL mkdir -p $(dirname $kf) + CALL cp "$sf" "$kf" + done + + # add new hooks + CALL cd $KERNEL_DIR + CALL git add kernel/swap/Kconfig + CALL git add kernel/swap/Makefile + CALL git add kernel/swap/hooks/. + CALL git add include/swap/. +} + +make_commit() { + echo "### MAKE COMMIT ###" + author="$(git config --global user.name) <$(git config --global user.email)>" + + CALL cd $SWAP_DIR + comm=$(git log -1 | head -1 | cut -f2 -d' ') + msg="SWAP: sync\n\ntizen.org/kernel/swap-modules\ncommit $comm\n\nSigned-off-by: $author" + + CALL cd $KERNEL_DIR + if [ "$(git diff --cached)" == "" ]; then + echo "" + echo "### no change ###" + return 1 + fi + + echo "### create commit ###" + git commit --author="$(echo -e $author)" -m"$(echo -e $msg)" + + return 0 +} + +show_commit() { + echo "### commit message ###" + + CALL cd $KERNEL_DIR + git log -1 +} + +main() { + if [ "$#" -lt 2 ] ; then + echo "Usage: $0 " + exit 1 + fi + + # set global variables + SWAP_DIR=$(readlink -f $1) + KERNEL_DIR=$(readlink -f $2) + + update_modules + update_hooks + make_commit + if [ $? != 0 ]; then + return 1 + fi + + show_commit +} + +main $@ +rm_tmp -- 2.7.4