Commit Graph

31 Commits

Author SHA1 Message Date
Josh Poimboeuf
fe6ace4fc7 kmod/core: error handling review fixes
Some fixes from the code review for better readability.
2014-04-24 14:22:51 -05:00
Josh Poimboeuf
f3f39c0587 fix activeness safety check when unpatching
When unpatching, the activeness safety logic should check for the new
function on the stack, not the old one.

Fixes #64.
2014-04-23 14:37:35 -05:00
Josh Poimboeuf
48cc3a409e kmod/core: move a couple of functions
Move kpatch_get_*_func a little higher in the file where they will be
needed for the next commit.
2014-04-23 14:37:35 -05:00
Josh Poimboeuf
2984b53d21 kmod: add new kpatch_module struct
Put funcs, num_funcs, and mod in their own struct called kpatch_module.
This allows us to keep patch module specific variables in one place (and
we'll have more of these variables soon).
2014-04-23 14:36:15 -05:00
Josh Poimboeuf
ff28767295 kmod: error handling cleanup
Cleanup the error handling a little bit and make the flow a little
clearer.
2014-04-23 14:36:15 -05:00
Masami Hiramatsu
42e0779c0c kmod/core: Support live patching on NMI handlers
Support live patching on NMI handlers. This adds checks for
possible inconsistency of live patching on NMI handlers.
The inconsistency problem means that any concurrent execution
of old function and new function, which can lead unexpected results.

Current kpatch checks possible inconsistency problem with
stop_machine, which can cover only threads and normal interrupts.
However, beacuse NMI can not stop with it, stop_machine is not
enough for live patching on NMI handlers or sub-functions which are
invoked in the NMI context.

To check for possible inconsistency of live patching on those
functions, add an atomic flag to count patching target functions
invoked in NMI context while updating kpatch hash table. If the
flag is set by the target functions in NMI, we can not ensure
there is no concurrent execution on it.

This fixes the issue #65.

Changes from v5:
 - Fix to add a NULL check in kpatch_get_committed_func().

Changes from v4:
 - Change kpatch_operation to atomic_t.
 - Use smp_rmb/wmb barriers between kpatch_operation and kpatch_status.
 - Check in_nmi() first and if true, access kpatch_operation.

Changes from v3:
 - Fix kpatch_apply/remove_patch to return 0 if succeeded.

Changes from v2:
 - Clean up kpatch_get_committed_func as same style of kpatch_get_func.
 - Rename opr to op in kpatch_ftrace_handler.
 - Consolidate in_nmi() and kpatch_operation check into one condition.
 - Fix UNPATCH/PATCH mistype in kpatch_register.

Changes from v1:
 - Rename inconsistent_flag to kpatch_status.
 - Introduce new enums and helper functions for kpatch_status.
 - Use hash_del_rcu instead of hlist_del_rcu.
 - Rename get_committed_func to kpatch_get_committed_func.
 - Use ACCESS_ONCE for kpatch_operation to prevent compiler optimization.
 - Fix to remove (!func || func->updating) condition from NMI check.
 - Add more precise comments.
 - Fix setting order of kpatch_status and kpatch_operation.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
2014-04-23 10:58:45 +09:00
Masami Hiramatsu
79ca5dbfa7 kmod/core: Handle registering error and unroll it
Handle registering error to unroll the ftrace filter.
This also introduces get_kpatch_func() and
kpatch_remove_funcs_from_filter() for holding up
redundant loops.

Changes from v2:
 - Rebased on the latest kpatch.

Changes from v1:
 - Rename get_kpatch_func to kpatch_get_func.
 - Fix function definition style issue.
 - Do not jump to a label in "if" block.
 - Rollback the ftrace user counter if we hit an error.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
2014-04-23 10:58:45 +09:00
Josh Poimboeuf
991b25b067 kmod/core: require fentry
Only compile the core module if the compiler supports -mfentry.
2014-04-15 14:40:03 -05:00
Josh Poimboeuf
e6cad4f0b1 kmod/core: use pr_err instead of printk 2014-04-15 14:21:19 -05:00
Josh Poimboeuf
56645d346d kmod/core: taint kernel with TAINT_USER
For now, taint with TAINT_USER when loading a patch module so that the
user can always detect when a kpatch module has been previously loaded.
Eventually we will want a dedicated TAINT_KPATCH flag in the kernel.
2014-04-15 13:34:01 -05:00
Josh Poimboeuf
2d7be5f48c kmod/core: add "notrace" to ftrace handler
The ftrace handler needs the notrace annotation so that ftrace won't
trace it and get into a recursive loop.
2014-03-19 19:16:12 -05:00
Josh Poimboeuf
29227a0fbd kmod/core: improve performance for cumulative patching
When multiple patch modules patch the same function, there's no need to
patch all the intermediate functions.  Just hook them all into the
original function and use the ftrace handler to find the newest one.

Also use a mutex in the register/unregister functions to protect changes
to kpatch_num_registered, kpatch_func_hash and calls to the ftrace
functions by other register/unregister invocations.
2014-03-19 10:05:07 -05:00
Josh Poimboeuf
37a756af58 kmod/core: protect kpatch_[un]register with mutex
Use a mutex in the register/unregister functions to protect changes to
kpatch_num_registered, kpatch_func_hash and calls to the ftrace
functions by other register/unregister invocations.
2014-03-19 10:05:07 -05:00
Josh Poimboeuf
bfad3b1880 kmod/core: update API and use hash table
My apologies for the size of this commit.  I combined these two features
(updating API and using a hash table) into a single commit because their
implementations are tightly coupled and I didn't want to have to add
support for the old kpatch_funcs array with the new API just for the
sake of splitting up the commit :-)

- Update the core module API to get a more clear separation between core
  module and patch module.  This is cleaner and will help our case for
  getting the core module merged upstream into the kernel.
- Convert the old kpatch_funcs array into a hash table.  This is so much
  nicer performance-wise and everything-else-wise than that ugly old
  array.
- Do the incremental patching in stop machine.  This ensures that the
  funcs hash is up to date and we don't miss anything.
- Disable preemption in the ftrace handler when accessing the func hash.
  That way we don't get conflicts with the stop_machine handler updating
  the hash.
2014-03-18 13:34:15 -05:00
Josh Poimboeuf
c164649d4e kmod/core: rename some variables
For the sake of consistency and readability, rename some variables.
Also change func->old_addr_end to func->old_size.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf
3fb9fd48ff kmod/core: move kpatch_remove_patch
Just move kpatch_remove_patch to a more logical location next to
kpatch_apply_patch.  No functional changes.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf
5f243c9db1 kmod/core: remove obsolete kpatch_rela struct 2014-03-14 20:23:12 -05:00
Josh Poimboeuf
b81e2d607b kmod/core: print module load/unload messages
Print the loading/unloading messages after they have successfully
completed.  Using the KERN_NOTICE log level which corresponds to a
"normal but significant condition."
2014-03-14 20:23:12 -05:00
Josh Poimboeuf
e7dde72ed9 kmod/core: remove unused old_func_name field
Not needed, we can always add it again later if needed (for sanity
checks, maybe)
2014-03-14 20:23:12 -05:00
Josh Poimboeuf
15e1b83cfe kmod/core: handle kmalloc errors and fix leak 2014-03-14 20:23:12 -05:00
Josh Poimboeuf
aea0932655 kmod/core: remove sync_core comment
No need to call sync_core() since we aren't directly modifying any code.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf
aa5346d13b kmod/core: remove comments about preemption
Preemption shouldn't cause a problem with determining activeness safety.
Even if a thread is preempted, it'll be on the backtrace.

We may need to disable preemption when reading the kpatch_funcs array,
but I'm removing that comment for now because the kpatch_funcs array
will soon be replaced by a much better data structure, and we'll deal
with proper synchronization then.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf
57f45c89e6 kmod/core: remove ftrace hacks
These hacks were from a previous implementation and are no longer
needed.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf
99dd4b53fb kmod/core: update module comments 2014-03-14 20:23:12 -05:00
Josh Poimboeuf
344050d8d3 kmod/core: remove assembler code
Long ago, the kpatch_trampoline required being written in assembler, but
that's no longer needed now that it integrates nicely with ftrace.

Move it to a C function and rename it kpatch_ftrace_handler.
2014-03-14 20:23:12 -05:00
Seth Jennings
2419410a52 fix KPATCH_BUILD path
use the build symlink in /lib/modules for a more portable
path to the build env for the kernel.

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-03-05 11:31:15 -06:00
Josh Poimboeuf
330a08dd0d add GPLv2 headers to source files 2014-03-04 21:34:19 -06:00
Josh Poimboeuf
8085d0402b build and install core module with make
Build and install the kpatch core module with make and make install,
rather than building it every time with kpatch build.

The only downside to this approach is that the user has to make and make
install kpatch every time they get a new kernel.  But this is only
temporary, until the kpatch module is delivered in an RPM.
2014-02-18 07:44:33 -06:00
Josh Poimboeuf
62de820aab proper makefile support
- setup the makefiles to support "make" and "make install", which builds
  the kpatch-build tools and installs everything in /usr/local.
- update kpatch-build to support new paths
- add "kpatch build" wrapper around kpatch-build
2014-02-13 16:02:10 -06:00
Josh Poimboeuf
72b1ee7916 use consistent naming for core and patch modules 2014-02-13 11:00:12 -06:00
Josh Poimboeuf
4f27b9ae31 functional reorganization
Organize the files functionally:
- kmod/core: core kmod source
- kmod/patch: patch kmod source
- kpatch: kpatch script
- kpatch-build: kpatch build script and supporting tools
- contrib: distro-related files
2014-02-13 11:00:06 -06:00