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.
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.
readme: small clarification about contributing
Merge pull request #70 from jpoimboe/readme-contributions

readme: small clarification about contributing
Merge pull request #69 from jpoimboe/core-cumulative-performance

kmod/core: improve performance for cumulative patching
error on symbol ambiguity in link-vmlinux-syms

Like in add-patches-section, we should continue to
search the vmlinux symbol table to see if there
is a duplicate global symbol.  This normally would
not be possible, however, because create-diff-object
modifies unchanged local functions to be global
as a hack so they can be linked, there is a chance
that these symbols could collide with an existing
global symbol.  We should detect this case and error
out.

Hopefully we can figure out a way to avoid this
situation altogether.  But for now, this is a
protection against improper linking.

Signed-off-by: Seth Jennings <sjenning@redhat.com>
Merge pull request #71 from spartacus06/fixes

error on symbol ambiguity in link-vmlinux-syms
add-patches-section: detect no patched functions
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.
Merge pull request #72 from jpoimboe/no-patched-functions

add-patches-section: detect no patched functions
Merge pull request #73 from jpoimboe/core-notrace

kmod/core: add "notrace" to ftrace handler
add uninstall target

Make it easy to clean up an install

Signed-off-by: Seth Jennings <sjenning@redhat.com>
Merge pull request #74 from spartacus06/uninstall

add uninstall target
order local FUNC syms before SECTION syms

ld drops FUNC syms that appear after the last SECTION
sym in the symbol table.

Make sure we order the FUNC syms before the SECTION syms.

Signed-off-by: Seth Jennings <sjenning@redhat.com>
refactor copying symbols to output

Create a new function kpatch_copy_symbols() that copies symbols from
one kelf to another if the "select" function to return true for the
symbol.

Signed-off-by: Seth Jennings <sjenning@redhat.com>
Merge pull request #75 from spartacus06/symorder

order local FUNC syms before SECTION syms
kmod/core: Handle registering error and unroll it

Handle registering error to unroll the ftrace filter.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu@gmail.com>
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.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu@gmail.com>