Commit Graph

59 Commits

Author SHA1 Message Date
Josh Poimboeuf 6ee8803978 kmod/core: kpatch_unregister return error if !kpmod->enabled
It's probably cleaner to just return -EINVAL if the kpmod isn't enabled,
instead of warning and continuing, which would be dangerous.

I think the reason I had it WARN before is because this condition
shouldn't be possible given the source for the patch module.  But the
core module can't necessarily assume that it's our trustworthy patch
module code on the other side.
2014-05-02 23:02:29 -05:00
Josh Poimboeuf 4bde6c3bbf kmod/core: fix kpatch_num_registered decrement
Oops, kpatch_unregister doesn't decrement kpatch_num_registered whenever
it's > 1.
2014-05-02 22:55:32 -05:00
Josh Poimboeuf de40ff3e54 kmod/core: check for needed kernel config options
Yes, this is ugly.  There's a much cleaner way to do this for an in-tree
module, but this is the only way I know how to do it here.
2014-05-02 16:11:55 -05:00
Josh Poimboeuf 65810a47d0 kpatch replace
Allow the user to atomically replace all existing modules with a new
"kpatch replace" command.  This provides a safe way to do atomic
upgrades for cumulative patch module updates.
2014-05-02 15:35:00 -05:00
Josh Poimboeuf 411fd570f2 kmod/core: update a few comments
- update the file description comment to be a little more accurate and
  concise
- s/trampoline/ftrace handler/
2014-05-01 12:36:28 -05:00
Josh Poimboeuf b5de5a8b25 kmod/core: checkpatch and sparse fixes
- checkpatch doesn't like the FSF address since it's subject to change
- checkpatch doesn't like strings split by line
- whitespace fix
- sparse suggested to change some variables and functions to static
2014-05-01 12:31:33 -05:00
Josh Poimboeuf 968845f1bd kmod/core: make WARN messages more informative 2014-04-30 13:42:22 -05:00
Josh Poimboeuf ac22230761 kmod/core: make func->op an enum 2014-04-30 13:26:29 -05:00
Josh Poimboeuf 2f34cf9a89 kmod/core: NMI synchronization improvements
This is an attempt to both simplify and improve the correctness of the
NMI synchronization code.

There's a race in kpatch_ftrace_handler() between the kpatch_get_func()
and kpatch_finish_status() calls which could result in func being NULL.
The retry was supposed to fix this.  However, this race would still be a
problem in the repatching case (if the function had already been
previously patched), in which case func would not be NULL, but could
instead point to the previously patched version of the function.  In
this case it wouldn't retry and it would be possible for the previous
version of the function to run.

The fix is to use a memory barrier between accesses of the func hash and
the status variable, and then just call kpatch_get_func() *after*
accessing the status variable.  For OP_PATCH, if status is SUCCESS, then
func is guaranteed to point to the new function.  If status is FAILURE,
func might point to the new function, in which case we can use
get_prev_func to get the previous version of the function.

I also made some pretty big changes to try to simplify the design so
that there are less moving parts and so that it's hopefully easier to
understand.  I moved the OP field into the kpatch_func struct.  This
allows us to merge the two global state variables (status + op) into a
single global state variable (state), which helps make the code quite a
bit simpler.  I turned it into a proper state machine and documented the
meaning of each state in the comments.

Moving the OP field to the kpatch_func struct also paves the way for an
upcoming pull request which will allow patch modules to be atomically
replaced ("kpatch load --replace <module>").
2014-04-29 23:36:53 -05:00
Josh Poimboeuf 87d852afa2 kmod/core: fail more gracefully in kpatch_unregister
In kpatch_unregister(), if kpatch_remove_patch succeeds but one of the
subsequent ftrace unregistering calls fails, it returns an error and
fails to module_put() the patch module, even though the patch has been
removed.  This causes the patch module to get stuck in a weird place
where its patch has been unregistered but the patch module can't ever be
removed.

These errors aren't serious and wouldn't cause any real problems if they
did somehow fail, so instead just WARN if they fail.
2014-04-29 23:36:53 -05:00
Josh Poimboeuf 0858e8b054 add include linux/module.h to kpatch.h
Needed for the module and kobject structs.
2014-04-28 13:24:04 -05:00
Josh Poimboeuf b7a2862f90 safe kpatch unload
Currently the patch module calls kpatch_unregister in the patch module
exit path.  If the activeness safety check fails in kpatch_unregister,
it's too late for the patch module to stop exiting, so all it can do is
panic.

Prevent this scenario by requiring the user to disable the patch module
via sysfs before allowing the module to be unloaded.  The sysfs write
will fail if the activeness safety check fails.  An rmmod will fail if
the patch is still enabled.

Also add support for this new unloading model in "kpatch unload".
2014-04-25 23:05:26 -05:00
Jincheng Miao 6c2d6444b1 Adding preempt_mask.h to core.c
When compiling core.c, it may report error like:
"error: implicit declaration of function ‘in_nmi’"

Adding header file in_nmi defined could avoid this.

Signed-off-by: Jincheng Miao <jincheng.miao@gmail.com>
2014-04-25 12:08:04 +08:00
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 3b489e7d29 support CONFIG_MODVERSIONS=y
When CONFIG_MODVERSIONS is enabled, loading of the patch module fails
with "no symbol version for kpatch_register".  When building the patch
module, we need to point it to the core module's Module.symvers file.

This also works when CONFIG_MODVERSIONS is disabled, since
Module.symvers is created regardless.
2014-03-27 16:36:43 -07:00
Josh Poimboeuf d4ab5fab2e always rebuild core module
If the kernel updates, make won't detect that the core module needs
rebuilding.  So always rebuild it.
2014-03-26 21:28:37 -07:00
Seth Jennings 5932f0d083 add uninstall target
Make it easy to clean up an install

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-03-20 11:29:25 -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 ff44a0af26 core/kmod: use kmalloc instead of kzalloc
There's no need to zero out the kpatch funcs array.  The addr fields are
initialized by the patch module, the mod field is intialized by the core
module, and the node struct doesn't need to be initialized because its
fields are overwritten by hash_add.
2014-03-18 16:57:51 -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 c3ad933132 install uname fix 2014-02-18 09:23:17 -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
Seth Jennings 6ab1273021 major rewrite to per-section function/data model
Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-02-11 10:25:48 -06:00