Commit Graph

2341 Commits

Author SHA1 Message Date
Artem Savkov
99542e864e create-diff-object: rename arguments in most correlate/compare functions
While theoretically most of these functions can work both ways right now
all calls follow the same pattern: first argument is orig element and
second is patched element. Rename the arguments so that these functions
are used in the same fashion going forward. This allows us to cut some
corners such as removing the elseif statement in kpatch_correlate_symbol().

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
22e16619e0 create-diff-object: base->orig renames
Rename "base" to "orig" when referencing object files and their contents
to be consistent with temporary directory structure.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
e2b50d7b66 create-diff-object: make locals_match() and maybe_discarded_sym() return bool
Change the return type of locals_match() and maybe_discarded_sym() to
better reflect their usage.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
720768767d Switch to per-file lookup table pointers.
So far create-diff-object worked only with objectfiles built from a
single source file. To support object-files built from multiple sources
such as linked vmlinux.o, we call locals_match() for each of STT_FILE
symbols and store the pointer to the beginning of appropriate symbol
block in lookup table in each symbol.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
db442d1405 Make lookup_symbol() accept struct symbol as an argument
At the moment lookup_symbol() takes symbol name as an argument which
might not be enough in some cases (e.g. for objectfiles built from
multiple source files). Make it accept full symbol structure.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
a132000868 test/integration/rhel-8.0: disable unneeded test
gcc-static-local-var-4.patch is disabled on this distribution, disable
the test as well as it will always fail during 'slow' integration test
runs.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
edf6613870
Merge pull request #1207 from jpoimboe/config-printk-index
Support CONFIG_PRINTK_INDEX
2021-08-17 09:36:43 +02:00
Josh Poimboeuf
56471ffc7c kpatch-build: Support CONFIG_PRINTK_INDEX, part 2
For each printk() call site, CONFIG_PRINTK_INDEX makes a static local
struct named `_entry`, and then adds a pointer to it in the
`.printk_index` section.

When regenerating the `.printk_index` section for the patch module, we
only need to include those entries which are referenced by included
functions.  Luckily this is a common pattern already used by several
other "special" sections.  Add `.printk_index` to the special section
handling logic.

Fixes: #1206

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-08-11 08:47:04 -07:00
Josh Poimboeuf
6cf50a6fca create-diff-object: Support CONFIG_PRINTK_INDEX, part 1
CONFIG_PRINTK_INDEX creates a static local struct variable named
`_entry` for every call site to printk().  The initializer for that
struct assigns the `__LINE__` macro to one of its fields.

Similarly to the WARN macro's usage [1] of `__LINE__`, it causes
problems because it results in the line number getting directly embedded
in the struct.  If a line is added or removed higher up in the source
file, the `_entry` struct changes accordingly due to a change in the
printk() call site line number.

`_entry` is similar to other "special" static locals, in that we don't
need to correlate the patched version with the original version.  We can
instead just ignore any changes to it.

Any substantial (non-line-number) change to the `_entry` struct would be
a second-order (dependent) effect of a first-order code change, which
would be detected using other means.  In that case the patched version
of `_entry` will be included, due to being referenced by the changed
function.

Fixes: #1206

[1] See kpatch_line_macro_change_only()

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-08-11 08:46:32 -07:00
Josh Poimboeuf
ea0470baa7 create-diff-object: change kpatch_line_macro_change_only() return type to bool
Make kpatch_line_macro_change_only()'s usage more clear by changing its
return type to bool.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-08-10 13:56:58 -07:00
Joe Lawrence
a19c4ed296 testing: pass KPATCH_BUILD_OPTS to kpatch-test
Update the test/integration/Makefile to pass a KPATCH_BUILD_OPTS
variable to kpatch-test.  This allows the user better control over the
kpatch build process, for example, building non-atomic replace .ko files
on kernels that do support atomic-replace:

  % make integration KPATCH_BUILD_OPTS="--non-replace"

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-08-09 17:58:02 -04:00
Joe Lawrence
a02668a0f7 kmod: remove redundant -mcmodel=large on ppc64le
In PR #1205, Kamalesh reports:

  ... I see that the -mcmodel=large flag is being passed twice with
  KBUILD_CFLAGS_MODULE set:

  gcc -Wp,-MMD,/root/.kpatch/tmp/patch/.livepatch-meminfo.mod.o.d ............ -mcmodel=medium .... -I/root/kpatch/kmod/patch -mcmodel=large -fplugin=/root/kpatch/kpatch-build/gcc-plugins/ppc64le-plugin.so ... -DMODULE -mno-save-toc-indirect -mcmodel=large -mcmodel=large -DKBUILD_BASENAME='"livepatch_meminfo.mod"' -DKBUILD_MODNAME='"livepatch_meminfo"' -D__KBUILD_MODNAME=kmod_livepatch_meminfo -c -o /root/.kpatch/tmp/patch/livepatch-meminfo.mod.o /root/.kpatch/tmp/patch/livepatch-meminfo.mod.c.

  I loaded the module built without the KBUILD_CFLAGS_MODULE +=
  -mcmodel=large flag and seems to okay. I guess, we can remove the arch
  specific flag from the Makefile.

Suggested-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-08-09 17:58:02 -04:00
Joe Lawrence
624e5e3b82 kmod: pass CFLAGS_MODULE to module build
The kmod/patch/Makefile defines KBUILD_CFLAGS_MODULE, but it seems that
kbuild doesn't honor it as environment variable.  This is noticed when
attempting to use the kpatch-build --non-replace option: the flag is
added to KBUILD_CFLAGS_MODULE, yet the kernel module build ignores it.

At the same time, the kernel docs suggest passing CFLAGS_MODULE [1], not
KBUILD_CFLAGS_MODULE, from the commandline.  Setup KPATCH_MAKE to pass
these options through that variable.

[1] https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt

Fixes: c14e6e9118 ("kpatch-build: Add PPC64le livepatch support")
Fixes: 17dcebf077 ("kpatch-build: enable klp with replace option by default")
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-08-09 17:58:02 -04:00
Joe Lawrence
58c816ebd4
Merge pull request #1199 from joe-lawrence/kpatch-build-extraversion
kpatch-build: set EXTRAVERSION and not localversion for RH kernels
2021-07-20 11:22:44 -04:00
Artem Savkov
948fb639c6
Merge pull request #1200 from sm00th/secsym
Make sure section symbols exist
2021-07-19 09:00:17 +02:00
Artem Savkov
ae574f6975 Bump unittest objs submodule
Update unittest objects submodule to point to a version that contains
objectfiles to test #1193.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-07-13 17:42:36 +02:00
Artem Savkov
5622e3cc3d Make sure section symbols exist
Binutils recently became much more aggressive about removing unused
section symbols. Since we can not rely on those being available anymore
add additional checks before using them.

Fixes: #1193

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-07-13 17:40:58 +02:00
Artem Savkov
59aabd154e
Merge pull request #1186 from gwelymernans/secsym-pr
create-diff-object: Check that the section has a secsym
2021-07-13 17:40:01 +02:00
Joe Lawrence
a26b2af2a3 kpatch-build: set EXTRAVERSION and not localversion for RH kernels
There are some Red Hat kernel NVR combinations like
"kernel-5.13.0-0.rc4.33.el9.x86_64" that don't work well with our srpm
localversion strategy and end up botching the utsrelease.h file... which
allows for kpatch builds, but the module loader rightly rejects the
vermagic mismatch.

An ordinary rpmbuild sets up the kernel Makefile with:

  # make sure EXTRAVERSION says what we want it to say
  # Trim the release if this is a CI build, since KERNELVERSION is limited to 64 characters
  ShortRel=$(perl -e "print \"%{release}\" =~ s/\.pr\.[0-9A-Fa-f]{32}//r")
  perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -${ShortRel}.%{_target_cpu}${Variant:++${Variant}}/" Makefile

The simplest fix is just adding the version string to the kernel
Makefile EXTRAVERSION as rpmbuild would do (minus the perl voodoo).

Fixes: #1196
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-07-02 16:34:27 -04:00
Artem Savkov
cecf544e8e
Merge pull request #1190 from liu-song-6/kpatch-rmmod-before-insmod
kpatch: rmmod module of the same name before loading a module
2021-06-21 11:03:20 +02:00
Artem Savkov
0331438d7e
Merge pull request #1183 from liu-song-6/replace
kpatch-build: enable option -R|--replace to build replace klp
2021-06-21 11:02:49 +02:00
Song Liu
df1e98f2c7 kpatch: rmmod module of the same name before loading a module
With klp->replace, when a patch is replaced, it is no longer an active
patch, but the module is still loaded with a refcount of 0. If the user
tries to load the patch again, insmod will fail with EEXIT. To avoid
such errors, run a proactive rmmod before loading the module. This is a
no-op if the module is not loaded or is actually in use.

Also, update module_ref_count() to only succeed with refcnt > 1.

Signed-off-by: Song Liu <song@kernel.org>
2021-06-18 12:09:16 -07:00
Joe Lawrence
10abc1bbc3
Merge pull request #1192 from xiejingf/upstream_master
kpatch: use /sys/kernel/kpatch/ to check whether core module is loaded
2021-06-17 16:58:28 -04:00
tim.xjf
39f695bbb2 kpatch: use /sys/kernel/kpatch/ to check whether core module is loaded
Fixes #1187

checking if kpatch_register or klp_enable_patch exists in /proc/kallsyms
might not be reliable when module loading or unloading occurs at the same time.
The kernel implementation about /proc/kallsyms is not guranteed to be consistent.

Signed-off-by: xiejingfeng <xiejingfeng@linux.alibaba.com>
2021-06-12 00:43:45 +08:00
Joe Lawrence
6d90e31172
Merge pull request #1189 from theyoyojo/fixes_1188
kpatch: Sync signal subcmd usage output with manpage
2021-06-08 11:29:08 -04:00
Joel Savitz
17539e71e7 kpatch: Sync signal subcmd usage output with manpage
Fixes: 1188

Signed-off-by: Joel Savitz <jsavitz@redhat.com>
2021-06-07 17:40:30 -04:00
Bill Wendling
ba3defa060 create-diff-object: Check that the section has a secsym
A STT_SECTION symbol is not needed if if it is not used as a relocation
target. Therefore, a section, in this case a debug section, may not have
a secsym associated with it.

Signed-off-by: Bill Wendling <morbo@google.com>
2021-06-03 01:50:16 -07:00
Artem Savkov
cbbdae2a3d
Merge pull request #1185 from jpoimboe/irc-libera
README.md: Migrate #kpatch IRC channel to libera.chat
2021-05-27 09:21:56 +02:00
Josh Poimboeuf
87ca62d8de README.md: Migrate #kpatch IRC channel to libera.chat
Move the #kpatch channel to libera.chat due to the freenode implosion.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-05-26 15:40:26 -05:00
Song Liu
4b68be868f kpatch-test: disable replace flag when the kernel doesn't support it
For redhat kernel < 4.18.0-193.el8 or non-redhat kernel version < 5.1,
add -R to $KPATCHBUILD_OPTS.

Signed-off-by: Song Liu <song@kernel.org>
2021-05-25 15:22:27 -07:00
Song Liu
8487a0ca0a manpages: update kpatch-build with -R | --non-replace option
Signed-off-by: Song Liu <song@kernel.org>
2021-05-25 15:21:44 -07:00
Song Liu
69e69d59e8 doc: kpatch-build uses "replace" flag by default
Add documentation about kpatch-build enables livepatch "replace" flag by
default, and provides -R|--non-replace option to disable the flag.

Signed-off-by: Song Liu <song@kernel.org>
2021-05-25 15:19:16 -07:00
Song Liu
17dcebf077 kpatch-build: enable klp with replace option by default
Since 5.1 kernel, klp_patch supports a "replace" option, which does atomic
replace of cumulative patches. Enable building such patch by default. If
replace behavior is not desired, the user can use -R|--non-replace option
to disable it.

Signed-off-by: Song Liu <song@kernel.org>
2021-05-25 15:18:36 -07:00
Joe Lawrence
97b69bee75
Merge pull request #1172 from jpoimboe/out-of-range-rela-fix
A couple of fixes for the out-of-range relocation check
2021-05-05 12:35:26 -04:00
Yannick Cote
e499f09942
Merge pull request #1175 from yhcote/master
Bump to v0.9.3
2021-04-20 14:45:58 -04:00
Yannick Cote
35b771712e Bump to v0.9.3
Interesting changes since v0.9.2:

- Initial support for clang compiler
- Add support for rhel-8.4
- rhel-8.4: workaround pahole and extended ELF sections
- rhel-8.4: drop klp.arch support
- Kpatch command waits for module to fully unload
- Kpatch command informs user when signal subcommand is unnecessary
- kpatch-build skips ppc64le vdso files

Signed-off-by: Yannick Cote <ycote@redhat.com>
2021-04-20 14:34:00 -04:00
Joe Lawrence
c819391c26
Merge pull request #1174 from joe-lawrence/rhel-8.4-support
Additional rhel 8.4 support
2021-04-20 14:19:57 -04:00
Joe Lawrence
fed5786932 integration: add rhel-8.4
Rebased to kernel-4.18.0-304.el8.

Note: since RHEL-8.4 dropped klp.arch support, we can now re-enable
those tests that reference static keys defined in vmlinux.

Also, adjust for adjust for ppc64le inlining:

Building gcc-static-local-var-4.patch on ppc64le results in test
failure, as the kpatch .ko now contains a 'free_ioctx' symbol (the test
expects to NOT see one).

From the build log:

  aio.o: changed function: free_ioctx
  aio.o: new function: put_aio_ring_file      << now un-inlined?
  aio.o: changed function: aio_free_ring
  aio.o: changed function: ioctx_alloc
  aio.o: changed function: aio_prep_rw
  aio.o: changed function: aio_read_events
  aio.o: new function: kpatch_aio_foo         << expected new function

and a source code change to free_ioctx():

  % diff -upr \
       <(objdump -D -j .text.free_ioctx ~/.kpatch/tmp/orig/fs/aio.o) \
       <(objdump -D -j .text.free_ioctx ~/.kpatch/tmp/patched/fs/aio.o)

  --- /dev/fd/63  2020-10-26 14:28:18.086236019 -0400
  +++ /dev/fd/62  2020-10-26 14:28:18.086236019 -0400
  @@ -1,5 +1,5 @@

  -/root/.kpatch/tmp/orig/fs/aio.o:     file format elf64-powerpcle
  +/root/.kpatch/tmp/patched/fs/aio.o:     file format elf64-powerpcle

   Disassembly of section .text.free_ioctx:
  @@ -53,7 +53,7 @@ Disassembly of section .text.free_ioctx:
     b0:  00 00 82 3c     addis   r4,r2,0
     b4:  00 00 84 e8     ld      r4,0(r4)
     b8:  78 fb e6 7f     mr      r6,r31
  -  bc:  e0 00 63 38     addi    r3,r3,224
  +  bc:  38 00 63 38     addi    r3,r3,56
     c0:  01 00 00 48     bl      c0 <free_ioctx+0xb8>
     c4:  00 00 00 60     nop
     c8:  70 ff ff 4b     b       38 <free_ioctx+0x30>

Marking put_aio_ring_file() as __always_inline keeps the r3 / 224
offset value, leaving free_ioctx() unchanged.  Since it's no longer
included in the resulting .ko, gcc-static-local-var-4.test will pass
once again.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-04-20 11:40:59 -04:00
Joe Lawrence
d9f49e850a kpatch-build: drop klp.arch support in RHEL-8.4
RHEL-8.4 backported the "livepatch,module: Remove .klp.arch and
module_disable_ro()" patchset, so update kpatch-build accordingly.

Link: https://lore.kernel.org/live-patching/nycvar.YFH.7.76.2005080016330.25812@cbobk.fhfr.pm/T/#t
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-04-20 11:17:56 -04:00
Josh Poimboeuf
81f9ca4833 create-diff-object: Fix out-of-range relocation check
Improve the relocation check for the case where the referenced symbol
isn't at the beginning of the section.

Note that the check still isn't perfect, as many relocations have a
negative addend.  But it's still a lot better than nothing.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-04-13 14:04:54 -05:00
Josh Poimboeuf
fa5a95cafd create-diff-object: Fix out-of-range relocation error message
Showing sec+addend isn't valid, show sym+addend instead.

Before:

  create-diff-object: ERROR: sys.o: kpatch_check_relocations: 2550: out-of-range relocation .rodata.__kpatch_do_sys_uname.str1.1+139 in .rela.text.__kpatch_do_sys_uname

After:

  create-diff-object: ERROR: sys.o: kpatch_check_relocations: 2550: out-of-range relocation .LC7+139 in .rela.text.__kpatch_do_sys_uname

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-04-13 13:58:59 -05:00
Joe Lawrence
453fb1b97a
Merge pull request #1167 from jpoimboe/static-call
kpatch-build: add support for static calls
2021-03-24 13:47:59 -04:00
Joe Lawrence
a9d435ae8e
Merge pull request #1163 from joe-lawrence/rhel-8.4-no-klp-arch
kpatch-build: drop klp.arch support in RHEL-8.4
2021-03-24 13:47:41 -04:00
Joe Lawrence
509bfab25f
Merge pull request #1168 from sm00th/actions
github actions: unit tests
2021-03-24 13:47:19 -04:00
Josh Poimboeuf
7be75549ba kpatch-build: add support for static calls
Add the new .static_call_sites special section.

Fixes #1165.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-03-24 10:37:44 -05:00
Artem Savkov
5abe5f666b github actions: unit tests
Add unit tests through github actions. This is exactly the same as our
travis tests but using github actions instead.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-03-23 08:47:29 +01:00
Joe Lawrence
6d466fa76f kpatch-build: drop klp.arch support in RHEL-8.4
RHEL-8.4 backported the "livepatch,module: Remove .klp.arch and
module_disable_ro()" patchset [1], so update kpatch-build accordingly.

[1] https://lore.kernel.org/live-patching/nycvar.YFH.7.76.2005080016330.25812@cbobk.fhfr.pm/T/#t

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-03-18 21:58:57 -04:00
Joe Lawrence
f9d2ec1d77 kpatch-build: fix kernel_is_rhel() for rhel-8 z-stream kernels
During review of PR #1163, Josh reported:

  When trying this out, I think I found that kernel_is_rhel() is broken
  for z-stream kernels. It expects a "." after the el8, like ".el8.",
  but [rhel-8] z-stream kernels have the minor version appended to the
  major version like ".el8_3".

Tweak the regex pattern in kernel_is_rhel() to account for such z-stream
kernel versions.  At the same time, add .el9 to the regex in preparation
of rhel-9.

Reported-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-03-18 21:58:01 -04:00
Artem Savkov
6321c9ab5e
Merge pull request #1162 from sm00th/cold-noid
support for .cold functions with no id suffix
2021-03-09 10:26:33 +01:00
Artem Savkov
d84c734245 Bump unittest objfile submodule
Update unittest submodule pointer to include unittests for this case.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-03-05 09:20:40 +01:00