livepatch-klp-build: small fixups and enhancements
Subsystems (2)
Joe Lawrence
maintainerv2 cover livepatch-klp-build: small fixups and enhancements
Here is v2, a quick iteration on v1 posted lasted week. I expect some comments on verbosity and some code stylings, so fully expect to roll at least another version.
See also some per-patch inline diffstat area commentary.
v2:
- Update patch subject prefixes accordingly [Josh]
- Added a small objtool/klp patch. Test systems setup crazy long pathnames :D
- Removed patch ("limit parent .git directory search") as this version replaces the use of git apply --recount with patch and recountdiff. A side effect of this simplification was no longer needing this weird hack. [Josh]
- Updated the patch that handles input patches that add files to also support removing files, implement this by directly inspecting the .patch +++ and --- header lines via two file lists [Josh]
- Implement two short-circuiting updates: validate patches for steps 1 and 2, and allow the user to omit patches for steps 3 and 4. This combines the original 'fail-fast' patch and some related notes on the v1 thread. [Josh]
- Since v2 replaces git apply with patch and recountdiff, there is no need for a -z/--fuzz argument, it comes with GNU patch for free.
Joe Lawrence (5):
objtool/klp: Fix mkstemp() failure with long paths livepatch/klp-build: handle patches that add/remove files livepatch/klp-build: switch to GNU patch and recountdiff livepatch/klp-build: minor short-circuiting tweaks livepatch/klp-build: provide friendlier error messages
scripts/livepatch/klp-build | 92 ++++++++++++++++++++----------------- tools/objtool/elf.c | 10 +++- 2 files changed, 58 insertions(+), 44 deletions(-)
Signature · 1 line
2.52.0
Joe Lawrence
maintainerPATCH v2 1/5 objtool/klp: Fix mkstemp() failure with long paths
klp-build's elf_create_file() fails with EINVAL when the build directory path is long enough to truncate the "XXXXXX" suffix in the 256-byte tmp_name buffer. Increase the buffer to PATH_MAX and add a truncation check to ensure a valid template string for mkstemp().
--- tools/objtool/elf.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
Repro notes:
# Consider a looooong path like:
$ LONG_DIR=tests/gitlab.com/joe.lawrence/kernel-tests/-/archive/klp-build/kernel-tests-klp-build.tar.gz/general/kpatch/patch-upgrade/kpatch/kernel/BUILD/kernel-6.12.0-178.1964_2250006255.el10/linux-6.12.0-178.1964_2250006255.el10.x86_64
# Place the source repo within the long path $ mkdir -p /tmp/"$LONG_DIR" $ cd /tmp/"$LONG_DIR" $ git clone --depth=1 --branch=v6.19-rc4 git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git $ cd linux
# Grab a sample patch
$ wget https://raw.githubusercontent.com/dynup/kpatch/refs/heads/master/examples/cmdline-string.patch
# Basic config for livepatching ...
$ make -j$(nproc) defconfig
$ ./scripts/config --file .config \
--set-val CONFIG_FTRACE y \
--set-val CONFIG_KALLSYMS_ALL y \
--set-val CONFIG_FUNCTION_TRACER y \
--set-val CONFIG_DYNAMIC_FTRACE y \
--set-val CONFIG_DYNAMIC_DEBUG y \
--set-val CONFIG_LIVEPATCH y
$ make olddefconfig # BAD BUILD, mkstemp() unhappy:
$ ./scripts/livepatch/klp-build -T cmdline-string.patch
Validating patch(es)
Building original kernel
Copying original object files
Fixing patch(es)
Building patched kernel
Copying patched object files
Diffing objects
vmlinux.o: changed function: cmdline_proc_show
vmlinux.o: error: objtool [elf.c:1233]: elf_create_file: can't create tmp file failed: Invalid argument
error: klp-build: objtool klp diff failed
error: klp-build: line 657: '( cd "$ORIG_DIR"; "${cmd[@]}" > >(tee -a "$log") 2> >(tee -a "$log" | "${filter[@]}" 1>&2) || die "objtool klp diff failed" )' # GOOD BUILD, with PATH_MAX buffer for mkstemp():
$ ./scripts/livepatch/klp-build -S 3 -T cmdline-string.patch Diffing objects vmlinux.o: changed function: cmdline_proc_show Building patch module: livepatch-cmdline-string.ko SUCCESS
tools/objtool/elf.c +8 -2
@@ -15,6 +15,7 @@#include <stdlib.h> #include <string.h> #include <unistd.h> +#include <limits.h> #include <errno.h> #include <libgen.h> #include <ctype.h>
@@ -1192,6 +1193,7 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)char *dir, *base, *tmp_name;struct symbol *sym;struct elf *elf; +int path_len;elf_version (EV_CURRENT );
@@ -1219,13 +1221,17 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) base =basename (base); - tmp_name =malloc (256 ); + tmp_name =malloc (PATH_MAX );if (!tmp_name) {ERROR_GLIBC ("malloc" );return NULL ; } -snprintf (tmp_name,256 ,"%s/%s.XXXXXX" , dir, base); + path_len =snprintf (tmp_name,PATH_MAX ,"%s/%s.XXXXXX" , dir, base); +if (path_len >=PATH_MAX ) { +ERROR_GLIBC ("snprintf" ); +return NULL ; + } elf->fd =mkstemp (tmp_name);if (elf->fd ==-1 ) {
Signature · 1 line
2.52.0
Josh Poimboeuf
maintainerRe: [PATCH v2 1/5] objtool/klp: Fix mkstemp() failure with long paths
tools/objtool/elf.c struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) · line 1224
Quoted 6 lines · Joe Lawrence
tools/objtool/elf.c +1
-1@@ -1219,13 +1221,17 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) base =basename (base); - tmp_name =malloc (256 ); + tmp_name =malloc (PATH_MAX );
The allocation size can be more precise with something like
tmp_name = malloc(strlen(name) + 8);
Also, I'm scratching my head at the existing code and wondering why we are splitting out the dirname() and the basename() just to paste them back together again?? Can you simplify that while you're at it?
tools/objtool/elf.c struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) · line 1234
Quoted 11 lines
tools/objtool/elf.c +5
-1if (!tmp_name) {ERROR_GLIBC ("malloc" );return NULL ; } -snprintf (tmp_name,256 ,"%s/%s.XXXXXX" , dir, base); + path_len =snprintf (tmp_name,PATH_MAX ,"%s/%s.XXXXXX" , dir, base); +if (path_len >=PATH_MAX ) { +ERROR_GLIBC ("snprintf" ); +return NULL ; + }
Checking for all the snprintf() cases can be a pain so we have a snprintf_check() for a streamlined error checking experience.
Signature · 1 line
Josh
Joe Lawrence
maintainerRe: [PATCH v2 1/5] objtool/klp: Fix mkstemp() failure with long paths
Quoted 21 lines · Josh Poimboeuf
tools/objtool/elf.c struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) · line 1224
Quoted 6 lines · Joe Lawrence
tools/objtool/elf.c +1
-1@@ -1219,13 +1221,17 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) base =basename (base); - tmp_name =malloc (256 ); + tmp_name =malloc (PATH_MAX );The allocation size can be more precise with something like
tmp_name = malloc(strlen(name) + 8);Also, I'm scratching my head at the existing code and wondering why we are splitting out the
dirname()and thebasename()just to paste them back together again?? Can you simplify that while you're at it?Quoted 5 lines
tools/objtool/elf.c +0
-0if (!tmp_name) {ERROR_GLIBC ("malloc" );return NULL ; }
Quoted 10 lines
tools/objtool/elf.c struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) · line 1234
Quoted 6 lines
tools/objtool/elf.c +5
-1-snprintf (tmp_name,256 ,"%s/%s.XXXXXX" , dir, base); + path_len =snprintf (tmp_name,PATH_MAX ,"%s/%s.XXXXXX" , dir, base); +if (path_len >=PATH_MAX ) { +ERROR_GLIBC ("snprintf" ); +return NULL ; + }Checking for all the
snprintf()cases can be a pain so we have asnprintf_check()for a streamlined error checking experience.
Ah, completely missed that, thanks for pointing out. I'll incorporate both suggestions into v3.
--
Joe
Joe Lawrence
maintainerPATCH v2 2/5 livepatch/klp-build: handle patches that add/remove files
In reply to Joe LawrenceThe klp-build script prepares a clean patch by populating two temporary directories ('a' and 'b') with source files and diffing the result. However, this process currently fails when a patch introduces a new source file as the script attempts to copy files that do not yet exist in the original source tree. Likewise, there is a similar limitation when a patch removes a source file and the script tries to copy files that no longer exist.
Refactor the file-gathering logic to distinguish between original input files and patched output files:
- Split get_patch_files() into get_patch_input_files() and get_patch_output_files() to identify which files exist before and after patch application.
- Filter out "/dev/null" from both to handle file creation/deletion
- Update refresh_patch() to only copy existing input files to the 'a' directory and the resulting output files to the 'b' directory.
This allows klp-build to successfully process patches that add or remove source files.
--- scripts/livepatch/klp-build | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-)
Lightly tested with patches that added or removed a source file, as generated by git diff, git format-patch, and diff -Nupr.
scripts/livepatch/klp-build +27 -7
@@ -299,15 +299,33 @@ set_kernelversion() { sed -i "2i echo $localversion; exit 0" scripts/setlocalversion } -get_patch_files() { +get_patch_input_files() { + local patch="$1" + + grep0 -E '^--- ' "$patch" \ + | gawk '{print $2}' \ + | grep -v '^/dev/null$' \ + | sed 's|^[^/]*/||' \ + | sort -u +} + +get_patch_output_files() { local patch="$1" - grep0 -E '^(--- |\+\+\+ )' "$patch" \ + grep0 -E '^\+\+\+ ' "$patch" \ | gawk '{print $2}' \ + | grep -v '^/dev/null$' \ | sed 's|^[^/]*/||' \ | sort -u } +get_patch_files() { + local patch="$1" + + { get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \ + | sort -u +} + # Make sure git re-stats the changed files git_refresh() { local patch="$1"
@@ -315,7 +333,7 @@ git_refresh() { [[ ! -e "$SRC/.git" ]] && return - get_patch_files "$patch" | mapfile -t files + get_patch_input_files "$patch" | mapfile -t files ( cd "$SRC"
@@ -429,21 +447,23 @@ do_init() { refresh_patch() { local patch="$1" local tmpdir="$PATCH_TMP_DIR" - local files=() + local input_files=() + local output_files=() rm -rf "$tmpdir" mkdir -p "$tmpdir/a" mkdir -p "$tmpdir/b" # Get all source files affected by the patch - get_patch_files "$patch" | mapfile -t files + get_patch_input_files "$patch" | mapfile -t input_files + get_patch_output_files "$patch" | mapfile -t output_files # Copy orig source files to 'a' - ( cd "$SRC" && echo "${files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" ) + ( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" ) # Copy patched source files to 'b' apply_patch "$patch" --recount - ( cd "$SRC" && echo "${files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" ) + ( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" ) revert_patch "$patch" --recount # Diff 'a' and 'b' to make a clean patch
Signature · 1 line
2.52.0
Josh Poimboeuf
maintainerRe: [PATCH v2 2/5] livepatch/klp-build: handle patches that add/remove files
Quoted 34 lines · Joe Lawrence
The klp-build script prepares a clean patch by populating two temporary directories ('a' and 'b') with source files and diffing the result. However, this process currently fails when a patch introduces a new source file as the script attempts to copy files that do not yet exist in the original source tree. Likewise, there is a similar limitation when a patch removes a source file and the script tries to copy files that no longer exist.
Refactor the file-gathering logic to distinguish between original input files and patched output files:
- Split
get_patch_files()intoget_patch_input_files()andget_patch_output_files()to identify which files exist before and after patch application.
- Filter out "/dev/null" from both to handle file creation/deletion
- Updaterefresh_patch()to only copy existing input files to the 'a' directory and the resulting output files to the 'b' directory.This allows klp-build to successfully process patches that add or remove source files.
Signed-off-by Joe Lawrence <[email protected]>--- scripts/livepatch/klp-build | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-)Lightly tested with patches that added or removed a source file, as generated by
git diff,git format-patch, anddiff -Nupr.scripts/livepatch/klp-build +0
-0
scripts/livepatch/klp-build set_kernelversion() { · line 307
Quoted 10 lines
scripts/livepatch/klp-build +6
-1@@ -299,15 +299,33 @@ set_kernelversion() { sed -i "2i echo $localversion; exit 0" scripts/setlocalversion } -get_patch_files() { +get_patch_input_files() { + local patch="$1" + + grep0 -E '^--- ' "$patch" \ + | gawk '{print $2}' \ + | grep -v '^/dev/null$' \
Because pipefail is enabled, the grep0 helper should be used instead of grep, otherwise a failed match can propagate to an error. Maybe we need a "make check" or something which enforces that and runs shellcheck.
Signature · 1 line
Josh
Joe Lawrence
maintainerRe: [PATCH v2 2/5] livepatch/klp-build: handle patches that add/remove files
Quoted 38 lines · Josh Poimboeuf
Quoted 37 lines · Joe Lawrence
The klp-build script prepares a clean patch by populating two temporary directories ('a' and 'b') with source files and diffing the result. However, this process currently fails when a patch introduces a new source file as the script attempts to copy files that do not yet exist in the original source tree. Likewise, there is a similar limitation when a patch removes a source file and the script tries to copy files that no longer exist.
Refactor the file-gathering logic to distinguish between original input files and patched output files:
- Split
get_patch_files()intoget_patch_input_files()andget_patch_output_files()to identify which files exist before and after patch application.
- Filter out "/dev/null" from both to handle file creation/deletion
- Updaterefresh_patch()to only copy existing input files to the 'a' directory and the resulting output files to the 'b' directory.This allows klp-build to successfully process patches that add or remove source files.
Signed-off-by Joe Lawrence <[email protected]>--- scripts/livepatch/klp-build | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-)Lightly tested with patches that added or removed a source file, as generated by
git diff,git format-patch, anddiff -Nupr.scripts/livepatch/klp-build +0
-0@@ -299,15 +299,33 @@ set_kernelversion() { sed -i "2i echo $localversion; exit 0" scripts/setlocalversion }
Quoted 12 lines
scripts/livepatch/klp-build set_kernelversion() { · line 307
Quoted 7 lines
scripts/livepatch/klp-build +6
-1-get_patch_files() { +get_patch_input_files() { + local patch="$1" + + grep0 -E '^--- ' "$patch" \ + | gawk '{print $2}' \ + | grep -v '^/dev/null$' \Because pipefail is enabled, the grep0 helper should be used instead of grep, otherwise a failed match can propagate to an error. Maybe we need a "make check" or something which enforces that and runs shellcheck.
Good catch. So your idea is to drop a Makefile in scripts/livepatch with a check target that runs shellcheck and then a klp-build specific check for any non-grep0 grep? (like grep -w 'grep' klp-build). If so, any other things to should check for?
--
Joe
Josh Poimboeuf
maintainerRe: [PATCH v2 2/5] livepatch/klp-build: handle patches that add/remove files
Quoted 52 lines · Joe Lawrence
Quoted 38 lines · Josh Poimboeuf
Quoted 37 lines · Joe Lawrence
The klp-build script prepares a clean patch by populating two temporary directories ('a' and 'b') with source files and diffing the result. However, this process currently fails when a patch introduces a new source file as the script attempts to copy files that do not yet exist in the original source tree. Likewise, there is a similar limitation when a patch removes a source file and the script tries to copy files that no longer exist.
Refactor the file-gathering logic to distinguish between original input files and patched output files:
- Split
get_patch_files()intoget_patch_input_files()andget_patch_output_files()to identify which files exist before and after patch application.
- Filter out "/dev/null" from both to handle file creation/deletion
- Updaterefresh_patch()to only copy existing input files to the 'a' directory and the resulting output files to the 'b' directory.This allows klp-build to successfully process patches that add or remove source files.
Signed-off-by Joe Lawrence <[email protected]>--- scripts/livepatch/klp-build | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-)Lightly tested with patches that added or removed a source file, as generated by
git diff,git format-patch, anddiff -Nupr.scripts/livepatch/klp-build +0
-0@@ -299,15 +299,33 @@ set_kernelversion() { sed -i "2i echo $localversion; exit 0" scripts/setlocalversion }Quoted 12 lines
scripts/livepatch/klp-build set_kernelversion() { · line 307
Quoted 7 lines
scripts/livepatch/klp-build +6
-1-get_patch_files() { +get_patch_input_files() { + local patch="$1" + + grep0 -E '^--- ' "$patch" \ + | gawk '{print $2}' \ + | grep -v '^/dev/null$' \Because pipefail is enabled, the grep0 helper should be used instead of grep, otherwise a failed match can propagate to an error. Maybe we need a "make check" or something which enforces that and runs shellcheck.
Quoted 4 lines
Good catch. So your idea is to drop a
Makefileinscripts/livepatchwith a check target that runs shellcheck and then a klp-build specific check for any non-grep0 grep? (likegrep -w 'grep' klp-build). If so, any other things to should check for?
That's all I can think of, hopefully shellcheck would catch most of the other footguns.
Signature · 1 line
Josh
Joe Lawrence
maintainerRe: [PATCH v2 2/5] livepatch/klp-build: handle patches that add/remove files
Quoted 40 lines · Josh Poimboeuf
Quoted 39 lines · Joe Lawrence
Quoted 38 lines · Josh Poimboeuf
Quoted 37 lines · Joe Lawrence
The klp-build script prepares a clean patch by populating two temporary directories ('a' and 'b') with source files and diffing the result. However, this process currently fails when a patch introduces a new source file as the script attempts to copy files that do not yet exist in the original source tree. Likewise, there is a similar limitation when a patch removes a source file and the script tries to copy files that no longer exist.
Refactor the file-gathering logic to distinguish between original input files and patched output files:
- Split
get_patch_files()intoget_patch_input_files()andget_patch_output_files()to identify which files exist before and after patch application.
- Filter out "/dev/null" from both to handle file creation/deletion
- Updaterefresh_patch()to only copy existing input files to the 'a' directory and the resulting output files to the 'b' directory.This allows klp-build to successfully process patches that add or remove source files.
Signed-off-by Joe Lawrence <[email protected]>--- scripts/livepatch/klp-build | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-)Lightly tested with patches that added or removed a source file, as generated by
git diff,git format-patch, anddiff -Nupr.scripts/livepatch/klp-build +0
-0@@ -299,15 +299,33 @@ set_kernelversion() { sed -i "2i echo $localversion; exit 0" scripts/setlocalversion }
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
scripts/livepatch/klp-build +3
-1-get_patch_files() { +get_patch_input_files() { + local patch="$1" +Quoted 8 lines
Quoted 8 lines
scripts/livepatch/klp-build set_kernelversion() { · line 307
Quoted 3 lines
scripts/livepatch/klp-build +3
-0+ grep0 -E '^--- ' "$patch" \ + | gawk '{print $2}' \ + | grep -v '^/dev/null$' \Because pipefail is enabled, the grep0 helper should be used instead of grep, otherwise a failed match can propagate to an error. Maybe we need a "make check" or something which enforces that and runs shellcheck.
How about defining our own grep in the script that intercepts the call and throws an error:
grep() {
echo "error: $SCRIPT: use grep0 or 'command grep' instead of bare grep" >&2
exit 1
}That seems easier than trying to externally parse the script to figure out what's a command, comment, word-match, legit grep, etc.
--
Joe
Josh Poimboeuf
maintainerRe: [PATCH v2 2/5] livepatch/klp-build: handle patches that add/remove files
Quoted 57 lines · Joe Lawrence
Quoted 40 lines · Josh Poimboeuf
Quoted 39 lines · Joe Lawrence
Quoted 38 lines · Josh Poimboeuf
Quoted 37 lines · Joe Lawrence
The klp-build script prepares a clean patch by populating two temporary directories ('a' and 'b') with source files and diffing the result. However, this process currently fails when a patch introduces a new source file as the script attempts to copy files that do not yet exist in the original source tree. Likewise, there is a similar limitation when a patch removes a source file and the script tries to copy files that no longer exist.
Refactor the file-gathering logic to distinguish between original input files and patched output files:
- Split
get_patch_files()intoget_patch_input_files()andget_patch_output_files()to identify which files exist before and after patch application.
- Filter out "/dev/null" from both to handle file creation/deletion
- Updaterefresh_patch()to only copy existing input files to the 'a' directory and the resulting output files to the 'b' directory.This allows klp-build to successfully process patches that add or remove source files.
Signed-off-by Joe Lawrence <[email protected]>--- scripts/livepatch/klp-build | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-)Lightly tested with patches that added or removed a source file, as generated by
git diff,git format-patch, anddiff -Nupr.scripts/livepatch/klp-build +0
-0@@ -299,15 +299,33 @@ set_kernelversion() { sed -i "2i echo $localversion; exit 0" scripts/setlocalversion }Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
scripts/livepatch/klp-build +3
-1-get_patch_files() { +get_patch_input_files() { + local patch="$1" +Quoted 8 lines
Quoted 8 lines
scripts/livepatch/klp-build set_kernelversion() { · line 307
Quoted 3 lines
scripts/livepatch/klp-build +3
-0+ grep0 -E '^--- ' "$patch" \ + | gawk '{print $2}' \ + | grep -v '^/dev/null$' \Because pipefail is enabled, the grep0 helper should be used instead of grep, otherwise a failed match can propagate to an error. Maybe we need a "make check" or something which enforces that and runs shellcheck.
How about defining our own grep in the script that intercepts the call and throws an error:
Quoted 7 lines
grep() { echo "error: $SCRIPT: use grep0 or 'command grep' instead of bare grep" >&2 exit 1 }That seems easier than trying to externally parse the script to figure out what's a command, comment, word-match, legit grep, etc.
Ack, sounds good.
Signature · 1 line
Josh
Joe Lawrence
maintainerPATCH v2 3/5 livepatch/klp-build: switch to GNU patch and recountdiff
In reply to Joe LawrenceThe klp-build script is currently very strict with input patches, requiring them to apply cleanly via git apply --recount. This prevents the use of patches with minor contextual fuzz relative to the target kernel sources.
To allow users to reuse a patch across similar kernel streams, switch to using GNU patch and patchutils for intermediate patch manipulation. Update the logic for applying, reverting, and regenerating patches:
- Use 'patch -p1' for better handling of context fuzz.
- Use 'recountdiff' to update line counts after FIX_PATCH_LINES.
- Drop git_refresh() and related git-specific logic.
--- scripts/livepatch/klp-build | 50 +++++++++++-------------------------- 1 file changed, 14 insertions(+), 36 deletions(-)
I think this does simplify things, but:
- introduces a dependency on patchutil's recountdiff
- requires goofy epoch timestamp filtering as diff -N doesn't use the git diff /dev/null, but a localized beginining of time epoch that may be 1969 or 1970 depending on the local timezone
- can be *really* chatty, for example:
Validating patch(es)
patching file fs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line). Fixing patch(es)
patching file fs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line). patching file fs/proc/cmdline.c
patching file fs/proc/cmdline.c
Building patched kernel
Copying patched object files
Diffing objects
vmlinux.o: changed function: override_release vmlinux.o: changed function: cmdline_proc_show Building patch module: livepatch-cmdline-string.ko SUCCESS
My initial thought was that I'd only be interested in knowing about patch offset/fuzz during the validation phase. And in the interest of clarifying some of the output messages, it would be nice to know the patch it was referring to, so how about a follow up patch pretty-formatting with some indentation like:
Validating patch(es)
cmdline-string.patch
patching file fs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line).
Fixing patch(es)
Building patched kernel
Copying patched object files
Diffing objects
vmlinux.o: changed function: override_release vmlinux.o: changed function: cmdline_proc_show Building patch module: livepatch-cmdline-string.ko SUCCESS
That said, Song suggested using --silent across the board, so maybe tie that into the existing --verbose option?
scripts/livepatch/klp-build +14 -36
@@ -96,7 +96,7 @@ restore_files() { cleanup() { set +o nounset - revert_patches "--recount" + revert_patches restore_files [[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR" return 0
@@ -285,7 +285,7 @@ set_module_name() { } # Hardcode the value printed by the localversion script to prevent patch -# application from appending it with '+' due to a dirty git working tree. +# application from appending it with '+' due to a dirty working tree. set_kernelversion() { local file="$SRC/scripts/setlocalversion" local localversion
@@ -303,8 +303,8 @@ get_patch_input_files() { local patch="$1" grep0 -E '^--- ' "$patch" \ + | grep -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ | gawk '{print $2}' \ - | grep -v '^/dev/null$' \ | sed 's|^[^/]*/||' \ | sort -u }
@@ -313,8 +313,8 @@ get_patch_output_files() { local patch="$1" grep0 -E '^\+\+\+ ' "$patch" \ + | grep -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ | gawk '{print $2}' \ - | grep -v '^/dev/null$' \ | sed 's|^[^/]*/||' \ | sort -u }
@@ -326,21 +326,6 @@ get_patch_files() { | sort -u } -# Make sure git re-stats the changed files -git_refresh() { - local patch="$1" - local files=() - - [[ ! -e "$SRC/.git" ]] && return - - get_patch_input_files "$patch" | mapfile -t files - - ( - cd "$SRC" - git update-index -q --refresh -- "${files[@]}" - ) -} - check_unsupported_patches() { local patch
@@ -361,18 +346,14 @@ check_unsupported_patches() { apply_patch() { local patch="$1" - shift - local extra_args=("$@") [[ ! -f "$patch" ]] && die "$patch doesn't exist" ( cd "$SRC" - - # The sed strips the version signature from 'git format-patch', - # otherwise 'git apply --recount' warns. - sed -n '/^-- /q;p' "$patch" | - git apply "${extra_args[@]}" + # The sed strips the version signature from 'git format-patch'. + sed -n '/^-- /q;p' "$patch" | \ + patch -p1 --no-backup-if-mismatch -r /dev/null ) APPLIED_PATCHES+=("$patch")
@@ -380,17 +361,13 @@ apply_patch() { revert_patch() { local patch="$1" - shift - local extra_args=("$@") local tmp=() ( cd "$SRC" - - sed -n '/^-- /q;p' "$patch" | - git apply --reverse "${extra_args[@]}" + sed -n '/^-- /q;p' "$patch" | \ + patch -p1 -R --silent --no-backup-if-mismatch -r /dev/null ) - git_refresh "$patch" for p in "${APPLIED_PATCHES[@]}"; do [[ "$p" == "$patch" ]] && continue
@@ -437,6 +414,7 @@ do_init() { APPLIED_PATCHES=() [[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines" + command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)" validate_config set_module_name
@@ -462,12 +440,12 @@ refresh_patch() { ( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" ) # Copy patched source files to 'b' - apply_patch "$patch" --recount + apply_patch "$patch" ( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" ) - revert_patch "$patch" --recount + revert_patch "$patch" # Diff 'a' and 'b' to make a clean patch - ( cd "$tmpdir" && git diff --no-index --no-prefix a b > "$patch" ) || true + ( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true } # Copy the patches to a temporary directory, fix their lines so as not to
@@ -490,7 +468,7 @@ fix_patches() { cp -f "$old_patch" "$tmp_patch" refresh_patch "$tmp_patch" - "$FIX_PATCH_LINES" "$tmp_patch" > "$new_patch" + "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch" refresh_patch "$new_patch" PATCHES[i]="$new_patch"
Signature · 1 line
2.52.0
Joe Lawrence
maintainerRe: [PATCH v2 3/5] livepatch/klp-build: switch to GNU patch and recountdiff
Quoted 9 lines · Joe Lawrence
The klp-build script is currently very strict with input patches, requiring them to apply cleanly via
git apply --recount. This prevents the use of patches with minor contextual fuzz relative to the target kernel sources.To allow users to reuse a patch across similar kernel streams, switch to using GNU patch and patchutils for intermediate patch manipulation. Update the logic for applying, reverting, and regenerating patches:
Quoted 9 lines
- Use 'patch -p1' for better handling of context fuzz.
- Use 'recountdiff' to update line counts afterFIX_PATCH_LINES.
- Dropgit_refresh()and related git-specific logic.Signed-off-by Joe Lawrence <[email protected]>--- scripts/livepatch/klp-build | 50 +++++++++++-------------------------- 1 file changed, 14 insertions(+), 36 deletions(-)
Forgot to mention this was tested as per Josh's suggestion (slightly modified*):
$ find . -type f -name '*.c' ! -path "./lib/*" -print0 | xargs -0 sed -i '1iasm("nop");'
$ git checkout tools arch/x86/lib/inat.c arch/x86/lib/insn.c kernel/configs.c
$ git diff > /tmp/oneline.patch$ ./scripts/livepatch/klp-build /tmp/oneline.patch ... error: klp-build: no changes detected
* modified to exclude lib/ to dodge unsupported patch complaints, avoid symlinks (GNU patch does not like), and kernel/configs.o which objtool couldn't find its .discard.sym_checksum section
--
Joe
Josh Poimboeuf
maintainerRe: [PATCH v2 3/5] livepatch/klp-build: switch to GNU patch and recountdiff
In reply to Joe LawrenceQuoted 3 lines · Joe Lawrence
I think this does simplify things, but:
- introduces a dependency on patchutil's recountdiff
I was wondering if we could instead just update fix-patch-lines to fix the line numbers/counts, but I get the feeling that would require rewriting the whole script and may not be worth the complexity. That script is nice and simple and robust at the moment.
Quoted 40 lines
- requires goofy epoch timestamp filtering as
diff -Ndoesn't use thegit diff/dev/null, but a localized beginining of time epoch that may be 1969 or 1970 depending on the local timezone
- can be *really* chatty, for example:Validating patch(es)
patching filefs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line). Fixing patch(es)
patching filefs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line). patching filefs/proc/cmdline.c
patching filefs/proc/cmdline.c
Building patched kernel
Copying patched object files
Diffing objects
vmlinux.o: changed function:override_releasevmlinux.o: changed function:cmdline_proc_showBuilding patch module: livepatch-cmdline-string.ko SUCCESSMy initial thought was that I'd only be interested in knowing about patch offset/fuzz during the validation phase. And in the interest of clarifying some of the output messages, it would be nice to know the patch it was referring to, so how about a follow up patch pretty-formatting with some indentation like:
Validating patch(es)
cmdline-string.patch
patching filefs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line).
Fixing patch(es)
Building patched kernel
Copying patched object files
Diffing objects
vmlinux.o: changed function:override_releasevmlinux.o: changed function:cmdline_proc_showBuilding patch module: livepatch-cmdline-string.ko SUCCESS
Quoted 2 lines
That said, Song suggested using --silent across the board, so maybe tie that into the existing --verbose option?
Hm. Currently we go to considerable effort to make klp-build's output as concise as possible, which is good. On the other hand, it might be important to know the patch has fuzz.
I'm thinking I would agree that maybe it should be verbose when validating patches and silent elsewhere. And the pretty formatting is a nice upgrade to that.
We might also consider indenting the outputs of the other steps. For example:
Building patched kernel
vmlinux.o: some warning
Copying patched object files
Diffing objects
vmlinux.o: changed function: override_release vmlinux.o: changed function: cmdline_proc_show
Or alternatively, print the step names in ASCII bold or something.
Quoted 7 lines
apply_patch(){scripts/livepatch/klp-build +0
-2local patch="$1" - shift - local extra_args=("$@") [[ ! -f "$patch" ]] && die "$patch doesn't exist"
scripts/livepatch/klp-build check_unsupported_patches() { · line 356
Quoted 10 lines
scripts/livepatch/klp-build +3
-5( cd "$SRC" - - # The sed strips the version signature from 'git format-patch', - # otherwise 'git apply --recount' warns. - sed -n '/^-- /q;p' "$patch" | - git apply "${extra_args[@]}" + # The sed strips the version signature from 'git format-patch'. + sed -n '/^-- /q;p' "$patch" | \ + patch -p1 --no-backup-if-mismatch -r /dev/null
Is this still needed now that we don't use git apply --recount?
scripts/livepatch/klp-build fix_patches() { · line 472
Quoted 7 lines
scripts/livepatch/klp-build +1
-1@@ -490,7 +468,7 @@ fix_patches() { cp -f "$old_patch" "$tmp_patch" refresh_patch "$tmp_patch" - "$FIX_PATCH_LINES" "$tmp_patch" > "$new_patch" + "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch" refresh_patch "$new_patch"
Do we still need to refresh after the recountdiff?
Signature · 1 line
Josh
Joe Lawrence
maintainerRe: [PATCH v2 3/5] livepatch/klp-build: switch to GNU patch and recountdiff
Quoted 10 lines · Josh Poimboeuf
Quoted 3 lines · Joe Lawrence
I think this does simplify things, but:
- introduces a dependency on patchutil's recountdiff
I was wondering if we could instead just update fix-patch-lines to fix the line numbers/counts, but I get the feeling that would require rewriting the whole script and may not be worth the complexity. That script is nice and simple and robust at the moment.
Ok, I'll take a look at fix-patch-lines and see how complicated it might turn out to incorporate a recount.
Quoted 40 lines
Quoted 40 lines
- requires goofy epoch timestamp filtering as
diff -Ndoesn't use thegit diff/dev/null, but a localized beginining of time epoch that may be 1969 or 1970 depending on the local timezone
- can be *really* chatty, for example:Validating patch(es)
patching filefs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line). Fixing patch(es)
patching filefs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line). patching filefs/proc/cmdline.c
patching filefs/proc/cmdline.c
Building patched kernel
Copying patched object files
Diffing objects
vmlinux.o: changed function:override_releasevmlinux.o: changed function:cmdline_proc_showBuilding patch module: livepatch-cmdline-string.ko SUCCESSMy initial thought was that I'd only be interested in knowing about patch offset/fuzz during the validation phase. And in the interest of clarifying some of the output messages, it would be nice to know the patch it was referring to, so how about a follow up patch pretty-formatting with some indentation like:
Validating patch(es)
cmdline-string.patch
patching filefs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line).
Fixing patch(es)
Building patched kernel
Copying patched object files
Diffing objects
vmlinux.o: changed function:override_releasevmlinux.o: changed function:cmdline_proc_showBuilding patch module: livepatch-cmdline-string.ko SUCCESS
Quoted 7 lines
Quoted 2 lines
That said, Song suggested using --silent across the board, so maybe tie that into the existing --verbose option?
Hm. Currently we go to considerable effort to make klp-build's output as concise as possible, which is good. On the other hand, it might be important to know the patch has fuzz.
To keep it succinct, the script could check for offset/fuzz and only report it, including the "patching file ..." part, if there is any.
Quoted 4 lines
I'm thinking I would agree that maybe it should be verbose when validating patches and silent elsewhere. And the pretty formatting is a nice upgrade to that.
In the past I've used a little function like:
indent() {
local num="${1:-0}"
sed "s/^/$(printf '%*s' "$num" '')/"
}so I could just pipe in echo or command output like: ./cmd | indent 2. Good enough or maybe you have one?
Quoted 12 lines
We might also consider indenting the outputs of the other steps. For example:
Building patched kernel
vmlinux.o: some warning
Copying patched object files
Diffing objects
vmlinux.o: changed function:override_releasevmlinux.o: changed function:cmdline_proc_showOr alternatively, print the step names in ASCII bold or something.
While I do kinda like the recent color coded output from the compilers, I don't know if I'm ready for a full-color livepatch build experience :D
I wouldn't be against it, but my vote leans towards the indentation since it leaves prettier log files, even if the color codes are filtered out. Then again, the color scheme bikeshedding we could look forward to!
Quoted 7 lines
Quoted 7 lines
apply_patch(){scripts/livepatch/klp-build +0
-2local patch="$1" - shift - local extra_args=("$@") [[ ! -f "$patch" ]] && die "$patch doesn't exist"
Quoted 13 lines
scripts/livepatch/klp-build check_unsupported_patches() { · line 356
Quoted 10 lines
scripts/livepatch/klp-build +3
-5( cd "$SRC" - - # The sed strips the version signature from 'git format-patch', - # otherwise 'git apply --recount' warns. - sed -n '/^-- /q;p' "$patch" | - git apply "${extra_args[@]}" + # The sed strips the version signature from 'git format-patch'. + sed -n '/^-- /q;p' "$patch" | \ + patch -p1 --no-backup-if-mismatch -r /dev/nullIs this still needed now that we don't use git apply --recount?
I'll double check. I recall having difficulties with recountdiff when taking these out, but can't reproduce that by hand at the moment.
Quoted 10 lines
scripts/livepatch/klp-build fix_patches() { · line 472
Quoted 7 lines
scripts/livepatch/klp-build +1
-1@@ -490,7 +468,7 @@ fix_patches() { cp -f "$old_patch" "$tmp_patch" refresh_patch "$tmp_patch" - "$FIX_PATCH_LINES" "$tmp_patch" > "$new_patch" + "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch" refresh_patch "$new_patch"Do we still need to refresh after the recountdiff?
Yeah I think it's redundant as long as recountdiff doesn't emit something weird, but if it did, it's probably already screwed up the patch.
--
Joe
Josh Poimboeuf
maintainerRe: [PATCH v2 3/5] livepatch/klp-build: switch to GNU patch and recountdiff
Quoted 24 lines · Joe Lawrence
Quoted 23 lines · Josh Poimboeuf
Quoted 21 lines · Joe Lawrence
My initial thought was that I'd only be interested in knowing about patch offset/fuzz during the validation phase. And in the interest of clarifying some of the output messages, it would be nice to know the patch it was referring to, so how about a follow up patch pretty-formatting with some indentation like:
Validating patch(es)
cmdline-string.patch
patching filefs/proc/cmdline.c
Hunk #1 succeeded at 7 (offset 1 line).
Fixing patch(es)
Building patched kernel
Copying patched object files
Diffing objects
vmlinux.o: changed function:override_releasevmlinux.o: changed function:cmdline_proc_showBuilding patch module: livepatch-cmdline-string.ko SUCCESSThat said, Song suggested using --silent across the board, so maybe tie that into the existing --verbose option?
Quoted 7 lines
Quoted 4 lines
Hm. Currently we go to considerable effort to make klp-build's output as concise as possible, which is good. On the other hand, it might be important to know the patch has fuzz.
To keep it succinct, the script could check for offset/fuzz and only report it, including the "patching file ..." part, if there is any.
Maybe? Only if it's not too complicated.
Quoted 7 lines
Quoted 4 lines
I'm thinking I would agree that maybe it should be verbose when validating patches and silent elsewhere. And the pretty formatting is a nice upgrade to that.
In the past I've used a little function like:
Quoted 7 lines
indent() { local num="${1:-0}" sed "s/^/$(printf '%*s' "$num" '')/" }so I could just pipe in echo or command output like:
./cmd | indent 2. Good enough or maybe you have one?
Sounds good, it probably needs a "return true" at the end of the function.
Quoted 13 lines
Quoted 12 lines
We might also consider indenting the outputs of the other steps. For example:
Building patched kernel
vmlinux.o: some warning
Copying patched object files
Diffing objects
vmlinux.o: changed function:override_releasevmlinux.o: changed function:cmdline_proc_showOr alternatively, print the step names in ASCII bold or something.
Quoted 7 lines
While I do kinda like the recent color coded output from the compilers, I don't know if I'm ready for a full-color livepatch build experience :D
I wouldn't be against it, but my vote leans towards the indentation since it leaves prettier log files, even if the color codes are filtered out. Then again, the color scheme bikeshedding we could look forward to!
:-)
Signature · 1 line
Josh
Joe Lawrence
maintainerPATCH v2 4/5 livepatch/klp-build: minor short-circuiting tweaks
In reply to Joe LawrenceUpdate SHORT_CIRCUIT behavior to better handle patch validation and argument processing in later klp-build steps.
Perform patch validation for both step 1 (building original kernel) and step 2 (building patched kernel) to ensure patches are verified before any compilation occurs.
Additionally, allow the user to omit input patches when skipping past step 2, while noting that any specified patches will be ignored in that case if they were provided.
--- scripts/livepatch/klp-build | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
scripts/livepatch/klp-build +13 -4
@@ -214,12 +214,18 @@ process_args() { done if [[ $# -eq 0 ]]; then - usage - exit 1 + if (( SHORT_CIRCUIT <= 2 )); then + usage + exit 1 + fi + else + if (( SHORT_CIRCUIT >= 3 )); then + status "note: patch arguments ignored at step $SHORT_CIRCUIT" + fi fi + PATCHES=("$@") KEEP_TMP="$keep_tmp" - PATCHES=("$@") } # temporarily disable xtrace for especially verbose code
@@ -801,9 +807,12 @@ build_patch_module() { process_args "$@" do_init -if (( SHORT_CIRCUIT <= 1 )); then +if (( SHORT_CIRCUIT <= 2 )); then status "Validating patch(es)" validate_patches +fi + +if (( SHORT_CIRCUIT <= 1 )); then status "Building original kernel" clean_kernel build_kernel
Signature · 1 line
2.52.0
Josh Poimboeuf
maintainerRe: [PATCH v2 4/5] livepatch/klp-build: minor short-circuiting tweaks
Quoted 21 lines · Joe Lawrence
Update
SHORT_CIRCUITbehavior to better handle patch validation and argument processing in later klp-build steps.Perform patch validation for both step 1 (building original kernel) and step 2 (building patched kernel) to ensure patches are verified before any compilation occurs.
Additionally, allow the user to omit input patches when skipping past step 2, while noting that any specified patches will be ignored in that case if they were provided.
Signed-off-by Joe Lawrence <[email protected]>--- scripts/livepatch/klp-build | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)scripts/livepatch/klp-build +0
-0
scripts/livepatch/klp-build process_args() { · line 220
Quoted 9 lines
scripts/livepatch/klp-build +4
-2@@ -214,12 +214,18 @@ process_args() { done if [[ $# -eq 0 ]]; then - usage - exit 1 + if (( SHORT_CIRCUIT <= 2 )); then + usage + exit 1 + fi
Ack
scripts/livepatch/klp-build process_args() { · line 224
Quoted 4 lines
scripts/livepatch/klp-build +4
-0+ else + if (( SHORT_CIRCUIT >= 3 )); then + status "note: patch arguments ignored at step $SHORT_CIRCUIT" + fi
Personally I don't care to see this status, but maybe I'm biased from writing the --short-circuit feature and not being confused by this :-)
Signature · 1 line
Josh
Joe Lawrence
maintainerRe: [PATCH v2 4/5] livepatch/klp-build: minor short-circuiting tweaks
Quoted 34 lines · Josh Poimboeuf
Quoted 21 lines · Joe Lawrence
Update
SHORT_CIRCUITbehavior to better handle patch validation and argument processing in later klp-build steps.Perform patch validation for both step 1 (building original kernel) and step 2 (building patched kernel) to ensure patches are verified before any compilation occurs.
Additionally, allow the user to omit input patches when skipping past step 2, while noting that any specified patches will be ignored in that case if they were provided.
Signed-off-by Joe Lawrence <[email protected]>--- scripts/livepatch/klp-build | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)scripts/livepatch/klp-build +0
-0scripts/livepatch/klp-build process_args() { · line 220
Quoted 9 lines
scripts/livepatch/klp-build +4
-2@@ -214,12 +214,18 @@ process_args() { done if [[ $# -eq 0 ]]; then - usage - exit 1 + if (( SHORT_CIRCUIT <= 2 )); then + usage + exit 1 + fiAck
Quoted 8 lines
scripts/livepatch/klp-build process_args() { · line 224
Quoted 4 lines
scripts/livepatch/klp-build +4
-0+ else + if (( SHORT_CIRCUIT >= 3 )); then + status "note: patch arguments ignored at step $SHORT_CIRCUIT" + fiPersonally I don't care to see this status, but maybe I'm biased from writing the --short-circuit feature and not being confused by this :-)
Alrighty, I'll drop this part unless somebody asks for it.
--
Joe
Joe Lawrence
maintainerPATCH v2 5/5 livepatch/klp-build: provide friendlier error messages
In reply to Joe LawrenceProvide a little bit more context behind some of the klp-build failure modes clarify which of the user-provided patches is unsupported, doesn't apply, and which kernel build failed.
--- scripts/livepatch/klp-build | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
scripts/livepatch/klp-build +6 -5
@@ -343,7 +343,7 @@ check_unsupported_patches() { for file in "${files[@]}"; do case "$file" in lib/*|*.S) - die "unsupported patch to $file" + die "$patch unsupported patch to $file" ;; esac done
@@ -359,7 +359,7 @@ apply_patch() { cd "$SRC" # The sed strips the version signature from 'git format-patch'. sed -n '/^-- /q;p' "$patch" | \ - patch -p1 --no-backup-if-mismatch -r /dev/null + patch -p1 --no-backup-if-mismatch -r /dev/null || die "$patch doesn't apply" ) APPLIED_PATCHES+=("$patch")
@@ -500,6 +500,7 @@ clean_kernel() { } build_kernel() { + local build="$1" local log="$TMP_DIR/build.log" local objtool_args=() local cmd=()
@@ -538,7 +539,7 @@ build_kernel() { "${cmd[@]}" \ 1> >(tee -a "$log") \ 2> >(tee -a "$log" | grep0 -v "modpost.*undefined!" >&2) - ) + ) || die "$build kernel build failed" } find_objects() {
@@ -815,7 +816,7 @@ fi if (( SHORT_CIRCUIT <= 1 )); then status "Building original kernel" clean_kernel - build_kernel + build_kernel "original" status "Copying original object files" copy_orig_objects fi
@@ -825,7 +826,7 @@ if (( SHORT_CIRCUIT <= 2 )); then fix_patches apply_patches status "Building patched kernel" - build_kernel + build_kernel "patched" revert_patches status "Copying patched object files" copy_patched_objects
Signature · 1 line
2.52.0