Skip to content
Snippets Groups Projects
  1. Jul 01, 2022
  2. Dec 23, 2021
    • David Vernet's avatar
      Documentation: livepatch: Add livepatch API page · e368cd72
      David Vernet authored
      
      The livepatch subsystem has several exported functions and objects with
      kerneldoc comments. Though the livepatch documentation contains handwritten
      descriptions of all of these exported functions, they are currently not
      pulled into the docs build using the kernel-doc directive.
      
      In order to allow readers of the documentation to see the full kerneldoc
      comments in the generated documentation files, this change adds a new
      Documentation/livepatch/api.rst page which contains kernel-doc directives
      to link the kerneldoc comments directly in the documentation.  With this,
      all of the hand-written descriptions of the APIs now cross-reference the
      kerneldoc comments on the new Livepatching APIs page, and running
      ./scripts/find-unused-docs.sh on kernel/livepatch no longer shows any files
      as missing documentation.
      
      Note that all of the handwritten API descriptions were left alone with the
      exception of Documentation/livepatch/system-state.rst, which was updated to
      allow the cross-referencing to work correctly. The file now follows the
      cross-referencing formatting guidance specified in
      Documentation/doc-guide/kernel-doc.rst. Furthermore, some comments around
      klp_shadow_free_all() were updated to say <_, id> rather than <*, id> to
      match the rest of the file, and to prevent the docs build from emitting an
      "Inline emphasis start-string without end string" error.
      
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Link: https://lore.kernel.org/r/20211221145743.4098360-1-void@manifault.com
      e368cd72
  3. Mar 09, 2021
  4. Jan 26, 2021
  5. May 07, 2020
    • Peter Zijlstra's avatar
      livepatch: Remove .klp.arch · 1d05334d
      Peter Zijlstra authored
      
      After the previous patch, vmlinux-specific KLP relocations are now
      applied early during KLP module load.  This means that .klp.arch
      sections are no longer needed for *vmlinux-specific* KLP relocations.
      
      One might think they're still needed for *module-specific* KLP
      relocations.  If a to-be-patched module is loaded *after* its
      corresponding KLP module is loaded, any corresponding KLP relocations
      will be delayed until the to-be-patched module is loaded.  If any
      special sections (.parainstructions, for example) rely on those
      relocations, their initializations (apply_paravirt) need to be done
      afterwards.  Thus the apparent need for arch_klp_init_object_loaded()
      and its corresponding .klp.arch sections -- it allows some of the
      special section initializations to be done at a later time.
      
      But... if you look closer, that dependency between the special sections
      and the module-specific KLP relocations doesn't actually exist in
      reality.  Looking at the contents of the .altinstructions and
      .parainstructions sections, there's not a realistic scenario in which a
      KLP module's .altinstructions or .parainstructions section needs to
      access a symbol in a to-be-patched module.  It might need to access a
      local symbol or even a vmlinux symbol; but not another module's symbol.
      When a special section needs to reference a local or vmlinux symbol, a
      normal rela can be used instead of a KLP rela.
      
      Since the special section initializations don't actually have any real
      dependency on module-specific KLP relocations, .klp.arch and
      arch_klp_init_object_loaded() no longer have a reason to exist.  So
      remove them.
      
      As Peter said much more succinctly:
      
        So the reason for .klp.arch was that .klp.rela.* stuff would overwrite
        paravirt instructions. If that happens you're doing it wrong. Those
        RELAs are core kernel, not module, and thus should've happened in
        .rela.* sections at patch-module loading time.
      
        Reverting this removes the two apply_{paravirt,alternatives}() calls
        from the late patching path, and means we don't have to worry about
        them when removing module_disable_ro().
      
      [ jpoimboe: Rewrote patch description.  Tweaked klp_init_object_loaded()
      	    error path. ]
      
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Acked-by: default avatarJoe Lawrence <joe.lawrence@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      1d05334d
  6. Nov 01, 2019
  7. Jul 15, 2019
    • Mauro Carvalho Chehab's avatar
      docs: add some directories to the main documentation index · 113094f7
      Mauro Carvalho Chehab authored
      
      The contents of those directories were orphaned at the documentation
      body.
      
      While those directories could likely be moved to be inside some guide,
      I'm opting to just adding their indexes to the main one, removing the
      :orphan: and adding the SPDX header.
      
      For the drivers, the rationale is that the documentation contains
      a mix of Kernelspace, uAPI and admin-guide. So, better to keep them on
      separate directories, as we've be doing with similar subsystem-specific
      docs that were not split yet.
      
      For the others, well... I'm too lazy to do the move. Also, it
      seems to make sense to keep at least some of those at the main
      dir (like kbuild, for example). In any case, a latter patch
      could do the move.
      
      Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+samsung@kernel.org>
      Acked-by: default avatarBartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
      113094f7
  8. May 07, 2019
  9. Jan 16, 2019
    • Miroslav Benes's avatar
      livepatch: Remove signal sysfs attribute · 0b3d5279
      Miroslav Benes authored
      
      The fake signal is send automatically now. We can rely on it completely
      and remove the sysfs attribute.
      
      Signed-off-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      0b3d5279
    • Miroslav Benes's avatar
      livepatch: Send a fake signal periodically · cba82dea
      Miroslav Benes authored
      
      An administrator may send a fake signal to all remaining blocking tasks
      of a running transition by writing to
      /sys/kernel/livepatch/<patch>/signal attribute. Let's do it
      automatically after 15 seconds. The timeout is chosen deliberately. It
      gives the tasks enough time to transition themselves.
      
      Theoretically, sending it once should be more than enough. However,
      every task must get outside of a patched function to be successfully
      transitioned. It could prove not to be simple and resending could be
      helpful in that case.
      
      A new workqueue job could be a cleaner solution to achieve it, but it
      could also introduce deadlocks and cause more headaches with
      synchronization and cancelling.
      
      [jkosina@suse.cz: removed added newline]
      Signed-off-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      cba82dea
  10. Jan 11, 2019
    • Joe Lawrence's avatar
      selftests/livepatch: introduce tests · a2818ee4
      Joe Lawrence authored
      
      Add a few livepatch modules and simple target modules that the included
      regression suite can run tests against:
      
        - basic livepatching (multiple patches, atomic replace)
        - pre/post (un)patch callbacks
        - shadow variable API
      
      Signed-off-by: default avatarJoe Lawrence <joe.lawrence@redhat.com>
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Tested-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Tested-by: default avatarAlice Ferrazzi <alice.ferrazzi@gmail.com>
      Acked-by: default avatarJoe Lawrence <joe.lawrence@redhat.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      a2818ee4
    • Petr Mladek's avatar
      livepatch: Remove ordering (stacking) of the livepatches · d67a5372
      Petr Mladek authored
      
      The atomic replace and cumulative patches were introduced as a more secure
      way to handle dependent patches. They simplify the logic:
      
        + Any new cumulative patch is supposed to take over shadow variables
          and changes made by callbacks from previous livepatches.
      
        + All replaced patches are discarded and the modules can be unloaded.
          As a result, there is only one scenario when a cumulative livepatch
          gets disabled.
      
      The different handling of "normal" and cumulative patches might cause
      confusion. It would make sense to keep only one mode. On the other hand,
      it would be rude to enforce using the cumulative livepatches even for
      trivial and independent (hot) fixes.
      
      However, the stack of patches is not really necessary any longer.
      The patch ordering was never clearly visible via the sysfs interface.
      Also the "normal" patches need a lot of caution anyway.
      
      Note that the list of enabled patches is still necessary but the ordering
      is not longer enforced.
      
      Otherwise, the code is ready to disable livepatches in an random order.
      Namely, klp_check_stack_func() always looks for the function from
      the livepatch that is being disabled. klp_func structures are just
      removed from the related func_stack. Finally, the ftrace handlers
      is removed only when the func_stack becomes empty.
      
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      d67a5372
    • Petr Mladek's avatar
      livepatch: Atomic replace and cumulative patches documentation · c4e6874f
      Petr Mladek authored
      
      User documentation for the atomic replace feature. It makes it easier
      to maintain livepatches using so-called cumulative patches.
      
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: default avatarJoe Lawrence <joe.lawrence@redhat.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      c4e6874f
    • Jason Baron's avatar
      livepatch: Add atomic replace · e1452b60
      Jason Baron authored
      
      Sometimes we would like to revert a particular fix. Currently, this
      is not easy because we want to keep all other fixes active and we
      could revert only the last applied patch.
      
      One solution would be to apply new patch that implemented all
      the reverted functions like in the original code. It would work
      as expected but there will be unnecessary redirections. In addition,
      it would also require knowing which functions need to be reverted at
      build time.
      
      Another problem is when there are many patches that touch the same
      functions. There might be dependencies between patches that are
      not enforced on the kernel side. Also it might be pretty hard to
      actually prepare the patch and ensure compatibility with the other
      patches.
      
      Atomic replace && cumulative patches:
      
      A better solution would be to create cumulative patch and say that
      it replaces all older ones.
      
      This patch adds a new "replace" flag to struct klp_patch. When it is
      enabled, a set of 'nop' klp_func will be dynamically created for all
      functions that are already being patched but that will no longer be
      modified by the new patch. They are used as a new target during
      the patch transition.
      
      The idea is to handle Nops' structures like the static ones. When
      the dynamic structures are allocated, we initialize all values that
      are normally statically defined.
      
      The only exception is "new_func" in struct klp_func. It has to point
      to the original function and the address is known only when the object
      (module) is loaded. Note that we really need to set it. The address is
      used, for example, in klp_check_stack_func().
      
      Nevertheless we still need to distinguish the dynamically allocated
      structures in some operations. For this, we add "nop" flag into
      struct klp_func and "dynamic" flag into struct klp_object. They
      need special handling in the following situations:
      
        + The structures are added into the lists of objects and functions
          immediately. In fact, the lists were created for this purpose.
      
        + The address of the original function is known only when the patched
          object (module) is loaded. Therefore it is copied later in
          klp_init_object_loaded().
      
        + The ftrace handler must not set PC to func->new_func. It would cause
          infinite loop because the address points back to the beginning of
          the original function.
      
        + The various free() functions must free the structure itself.
      
      Note that other ways to detect the dynamic structures are not considered
      safe. For example, even the statically defined struct klp_object might
      include empty funcs array. It might be there just to run some callbacks.
      
      Also note that the safe iterator must be used in the free() functions.
      Otherwise already freed structures might get accessed.
      
      Special callbacks handling:
      
      The callbacks from the replaced patches are _not_ called by intention.
      It would be pretty hard to define a reasonable semantic and implement it.
      
      It might even be counter-productive. The new patch is cumulative. It is
      supposed to include most of the changes from older patches. In most cases,
      it will not want to call pre_unpatch() post_unpatch() callbacks from
      the replaced patches. It would disable/break things for no good reasons.
      Also it should be easier to handle various scenarios in a single script
      in the new patch than think about interactions caused by running many
      scripts from older patches. Not to say that the old scripts even would
      not expect to be called in this situation.
      
      Removing replaced patches:
      
      One nice effect of the cumulative patches is that the code from the
      older patches is no longer used. Therefore the replaced patches can
      be removed. It has several advantages:
      
        + Nops' structs will no longer be necessary and might be removed.
          This would save memory, restore performance (no ftrace handler),
          allow clear view on what is really patched.
      
        + Disabling the patch will cause using the original code everywhere.
          Therefore the livepatch callbacks could handle only one scenario.
          Note that the complication is already complex enough when the patch
          gets enabled. It is currently solved by calling callbacks only from
          the new cumulative patch.
      
        + The state is clean in both the sysfs interface and lsmod. The modules
          with the replaced livepatches might even get removed from the system.
      
      Some people actually expected this behavior from the beginning. After all
      a cumulative patch is supposed to "completely" replace an existing one.
      It is like when a new version of an application replaces an older one.
      
      This patch does the first step. It removes the replaced patches from
      the list of patches. It is safe. The consistency model ensures that
      they are no longer used. By other words, each process works only with
      the structures from klp_transition_patch.
      
      The removal is done by a special function. It combines actions done by
      __disable_patch() and klp_complete_transition(). But it is a fast
      track without all the transaction-related stuff.
      
      Signed-off-by: default avatarJason Baron <jbaron@akamai.com>
      [pmladek@suse.com: Split, reuse existing code, simplified]
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Cc: Josh Poimboeuf <jpoimboe@redhat.com>
      Cc: Jessica Yu <jeyu@kernel.org>
      Cc: Jiri Kosina <jikos@kernel.org>
      Cc: Miroslav Benes <mbenes@suse.cz>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      e1452b60
    • Petr Mladek's avatar
      livepatch: Simplify API by removing registration step · 958ef1e3
      Petr Mladek authored
      
      The possibility to re-enable a registered patch was useful for immediate
      patches where the livepatch module had to stay until the system reboot.
      The improved consistency model allows to achieve the same result by
      unloading and loading the livepatch module again.
      
      Also we are going to add a feature called atomic replace. It will allow
      to create a patch that would replace all already registered patches.
      The aim is to handle dependent patches more securely. It will obsolete
      the stack of patches that helped to handle the dependencies so far.
      Then it might be unclear when a cumulative patch re-enabling is safe.
      
      It would be complicated to support the many modes. Instead we could
      actually make the API and code easier to understand.
      
      Therefore, remove the two step public API. All the checks and init calls
      are moved from klp_register_patch() to klp_enabled_patch(). Also the patch
      is automatically freed, including the sysfs interface when the transition
      to the disabled state is completed.
      
      As a result, there is never a disabled patch on the top of the stack.
      Therefore we do not need to check the stack in __klp_enable_patch().
      And we could simplify the check in __klp_disable_patch().
      
      Also the API and logic is much easier. It is enough to call
      klp_enable_patch() in module_init() call. The patch can be disabled
      by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module
      can be removed once the transition finishes and sysfs interface is freed.
      
      The only problem is how to free the structures and kobjects safely.
      The operation is triggered from the sysfs interface. We could not put
      the related kobject from there because it would cause lock inversion
      between klp_mutex and kernfs locks, see kn->count lockdep map.
      
      Therefore, offload the free task to a workqueue. It is perfectly fine:
      
        + The patch can no longer be used in the livepatch operations.
      
        + The module could not be removed until the free operation finishes
          and module_put() is called.
      
        + The operation is asynchronous already when the first
          klp_try_complete_transition() fails and another call
          is queued with a delay.
      
      Suggested-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      958ef1e3
  11. May 24, 2018
  12. Apr 17, 2018
    • Petr Mladek's avatar
      livepatch: Allow to call a custom callback when freeing shadow variables · 3b2c77d0
      Petr Mladek authored
      
      We might need to do some actions before the shadow variable is freed.
      For example, we might need to remove it from a list or free some data
      that it points to.
      
      This is already possible now. The user can get the shadow variable
      by klp_shadow_get(), do the necessary actions, and then call
      klp_shadow_free().
      
      This patch allows to do it a more elegant way. The user could implement
      the needed actions in a callback that is passed to klp_shadow_free()
      as a parameter. The callback usually does reverse operations to
      the constructor callback that can be called by klp_shadow_*alloc().
      
      It is especially useful for klp_shadow_free_all(). There we need to do
      these extra actions for each found shadow variable with the given ID.
      
      Note that the memory used by the shadow variable itself is still released
      later by rcu callback. It is needed to protect internal structures that
      keep all shadow variables. But the destructor is called immediately.
      The shadow variable must not be access anyway after klp_shadow_free()
      is called. The user is responsible to protect this any suitable way.
      
      Be aware that the destructor is called under klp_shadow_lock. It is
      the same as for the contructor in klp_shadow_alloc().
      
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      3b2c77d0
    • Petr Mladek's avatar
      livepatch: Initialize shadow variables safely by a custom callback · e91c2518
      Petr Mladek authored
      
      The existing API allows to pass a sample data to initialize the shadow
      data. It works well when the data are position independent. But it fails
      miserably when we need to set a pointer to the shadow structure itself.
      
      Unfortunately, we might need to initialize the pointer surprisingly
      often because of struct list_head. It is even worse because the list
      might be hidden in other common structures, for example, struct mutex,
      struct wait_queue_head.
      
      For example, this was needed to fix races in ALSA sequencer. It required
      to add mutex into struct snd_seq_client. See commit b3defb79
      ("ALSA: seq: Make ioctls race-free") and commit d15d662e
      ("ALSA: seq: Fix racy pool initializations")
      
      This patch makes the API more safe. A custom constructor function and data
      are passed to klp_shadow_*alloc() functions instead of the sample data.
      
      Note that ctor_data are no longer a template for shadow->data. It might
      point to any data that might be necessary when the constructor is called.
      
      Also note that the constructor is called under klp_shadow_lock. It is
      an internal spin_lock that synchronizes alloc() vs. get() operations,
      see klp_shadow_get_or_alloc(). On one hand, this adds a risk of ABBA
      deadlocks. On the other hand, it allows to do some operations safely.
      For example, we could add the new structure into an existing list.
      This must be done only once when the structure is allocated.
      
      Reported-by: default avatarNicolai Stange <nstange@suse.de>
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      e91c2518
  13. Jan 11, 2018
    • Miroslav Benes's avatar
      livepatch: Remove immediate feature · d0807da7
      Miroslav Benes authored
      
      Immediate flag has been used to disable per-task consistency and patch
      all tasks immediately. It could be useful if the patch doesn't change any
      function or data semantics.
      
      However, it causes problems on its own. The consistency problem is
      currently broken with respect to immediate patches.
      
      func            a
      patches         1i
                      2i
                      3
      
      When the patch 3 is applied, only 2i function is checked (by stack
      checking facility). There might be a task sleeping in 1i though. Such
      task is migrated to 3, because we do not check 1i in
      klp_check_stack_func() at all.
      
      Coming atomic replace feature would be easier to implement and more
      reliable without immediate.
      
      Thus, remove immediate feature completely and save us from the problems.
      
      Note that force feature has the similar problem. However it is
      considered as a last resort. If used, administrator should not apply any
      new live patches and should plan for reboot into an updated kernel.
      
      The architectures would now need to provide HAVE_RELIABLE_STACKTRACE to
      fully support livepatch.
      
      Signed-off-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      d0807da7
  14. Dec 07, 2017
    • Miroslav Benes's avatar
      livepatch: force transition to finish · c99a2be7
      Miroslav Benes authored
      
      If a task sleeps in a set of patched functions uninterruptedly, it could
      block the whole transition indefinitely.  Thus it may be useful to clear
      its TIF_PATCH_PENDING to allow the process to finish.
      
      Admin can do that now by writing to force sysfs attribute in livepatch
      sysfs directory. TIF_PATCH_PENDING is then cleared for all tasks and the
      transition can finish successfully.
      
      Important note! Administrator should not use this feature without a
      clearance from a patch distributor. It must be checked that by doing so
      the consistency model guarantees are not violated. Removal (rmmod) of
      patch modules is permanently disabled when the feature is used. It
      cannot be guaranteed there is no task sleeping in such module.
      
      Signed-off-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      c99a2be7
  15. Dec 04, 2017
    • Miroslav Benes's avatar
      livepatch: send a fake signal to all blocking tasks · 43347d56
      Miroslav Benes authored
      
      Live patching consistency model is of LEAVE_PATCHED_SET and
      SWITCH_THREAD. This means that all tasks in the system have to be marked
      one by one as safe to call a new patched function. Safe means when a
      task is not (sleeping) in a set of patched functions. That is, no
      patched function is on the task's stack. Another clearly safe place is
      the boundary between kernel and userspace. The patching waits for all
      tasks to get outside of the patched set or to cross the boundary. The
      transition is completed afterwards.
      
      The problem is that a task can block the transition for quite a long
      time, if not forever. It could sleep in a set of patched functions, for
      example.  Luckily we can force the task to leave the set by sending it a
      fake signal, that is a signal with no data in signal pending structures
      (no handler, no sign of proper signal delivered). Suspend/freezer use
      this to freeze the tasks as well. The task gets TIF_SIGPENDING set and
      is woken up (if it has been sleeping in the kernel before) or kicked by
      rescheduling IPI (if it was running on other CPU). This causes the task
      to go to kernel/userspace boundary where the signal would be handled and
      the task would be marked as safe in terms of live patching.
      
      There are tasks which are not affected by this technique though. The
      fake signal is not sent to kthreads. They should be handled differently.
      They can be woken up so they leave the patched set and their
      TIF_PATCH_PENDING can be cleared thanks to stack checking.
      
      For the sake of completeness, if the task is in TASK_RUNNING state but
      not currently running on some CPU it doesn't get the IPI, but it would
      eventually handle the signal anyway. Second, if the task runs in the
      kernel (in TASK_RUNNING state) it gets the IPI, but the signal is not
      handled on return from the interrupt. It would be handled on return to
      the userspace in the future when the fake signal is sent again. Stack
      checking deals with these cases in a better way.
      
      If the task was sleeping in a syscall it would be woken by our fake
      signal, it would check if TIF_SIGPENDING is set (by calling
      signal_pending() predicate) and return ERESTART* or EINTR. Syscalls with
      ERESTART* return values are restarted in case of the fake signal (see
      do_signal()). EINTR is propagated back to the userspace program. This
      could disturb the program, but...
      
      * each process dealing with signals should react accordingly to EINTR
        return values.
      * syscalls returning EINTR happen to be quite common situation in the
        system even if no fake signal is sent.
      * freezer sends the fake signal and does not deal with EINTR anyhow.
        Thus EINTR values are returned when the system is resumed.
      
      The very safe marking is done in architectures' "entry" on syscall and
      interrupt/exception exit paths, and in a stack checking functions of
      livepatch.  TIF_PATCH_PENDING is cleared and the next
      recalc_sigpending() drops TIF_SIGPENDING. In connection with this, also
      call klp_update_patch_state() before do_signal(), so that
      recalc_sigpending() in dequeue_signal() can clear TIF_PATCH_PENDING
      immediately and thus prevent a double call of do_signal().
      
      Note that the fake signal is not sent to stopped/traced tasks. Such task
      prevents the patching to finish till it continues again (is not traced
      anymore).
      
      Last, sending the fake signal is not automatic. It is done only when
      admin requests it by writing 1 to signal sysfs attribute in livepatch
      sysfs directory.
      
      Signed-off-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Cc: Oleg Nesterov <oleg@redhat.com>
      Cc: Michael Ellerman <mpe@ellerman.id.au>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Cc: Ingo Molnar <mingo@redhat.com>
      Cc: "H. Peter Anvin" <hpa@zytor.com>
      Cc: Andy Lutomirski <luto@kernel.org>
      Cc: linuxppc-dev@lists.ozlabs.org
      Cc: x86@kernel.org
      Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      43347d56
  16. Oct 19, 2017
    • Joe Lawrence's avatar
      livepatch: add (un)patch callbacks · 93862e38
      Joe Lawrence authored
      
      Provide livepatch modules a klp_object (un)patching notification
      mechanism.  Pre and post-(un)patch callbacks allow livepatch modules to
      setup or synchronize changes that would be difficult to support in only
      patched-or-unpatched code contexts.
      
      Callbacks can be registered for target module or vmlinux klp_objects,
      but each implementation is klp_object specific.
      
        - Pre-(un)patch callbacks run before any (un)patching transition
          starts.
      
        - Post-(un)patch callbacks run once an object has been (un)patched and
          the klp_patch fully transitioned to its target state.
      
      Example use cases include modification of global data and registration
      of newly available services/handlers.
      
      See Documentation/livepatch/callbacks.txt for details and
      samples/livepatch/ for examples.
      
      Signed-off-by: default avatarJoe Lawrence <joe.lawrence@redhat.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      93862e38
  17. Oct 02, 2017
  18. Sep 14, 2017
    • Joe Lawrence's avatar
      livepatch: introduce shadow variable API · 439e7271
      Joe Lawrence authored
      
      Add exported API for livepatch modules:
      
        klp_shadow_get()
        klp_shadow_alloc()
        klp_shadow_get_or_alloc()
        klp_shadow_free()
        klp_shadow_free_all()
      
      that implement "shadow" variables, which allow callers to associate new
      shadow fields to existing data structures.  This is intended to be used
      by livepatch modules seeking to emulate additions to data structure
      definitions.
      
      See Documentation/livepatch/shadow-vars.txt for a summary of the new
      shadow variable API, including a few common use cases.
      
      See samples/livepatch/livepatch-shadow-* for example modules that
      demonstrate shadow variables.
      
      [jkosina@suse.cz: fix __klp_shadow_get_or_alloc() comment as spotted by
       Josh]
      Signed-off-by: default avatarJoe Lawrence <joe.lawrence@redhat.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      439e7271
  19. Mar 08, 2017
    • Josh Poimboeuf's avatar
      livepatch: allow removal of a disabled patch · 3ec24776
      Josh Poimboeuf authored
      Currently we do not allow patch module to unload since there is no
      method to determine if a task is still running in the patched code.
      
      The consistency model gives us the way because when the unpatching
      finishes we know that all tasks were marked as safe to call an original
      function. Thus every new call to the function calls the original code
      and at the same time no task can be somewhere in the patched code,
      because it had to leave that code to be marked as safe.
      
      We can safely let the patch module go after that.
      
      Completion is used for synchronization between module removal and sysfs
      infrastructure in a similar way to commit 942e4431 ("module: Fix
      mod->mkobj.kobj potentially freed too early").
      
      Note that we still do not allow the removal for immediate model, that is
      no consistency model. The module refcount may increase in this case if
      somebody disables and enables the patch several times. This should not
      cause any harm.
      
      With this change a call to try_module_get() is moved to
      __klp_enable_patch from klp_register_patch to make module reference
      counting symmetric (module_put() is in a patch disable path) and to
      allow to take a new reference to a disabled module when being enabled.
      
      Finally, we need to be very careful about possible races between
      klp_unregister_patch(), kobject_put() functions and operations
      on the related sysfs files.
      
      kobject_put(&patch->kobj) must be called without klp_mutex. Otherwise,
      it might be blocked by enabled_store() that needs the mutex as well.
      In addition, enabled_store() must check if the patch was not
      unregisted in the meantime.
      
      There is no need to do the same for other kobject_put() callsites
      at the moment. Their sysfs operations neither take the lock nor
      they access any data that might be freed in the meantime.
      
      There was an attempt to use kobjects the right way and prevent these
      races by design. But it made the patch definition more complicated
      and opened another can of worms. See
      https://lkml.kernel.org/r/1464018848-4303-1-git-send-email-pmladek@suse.com
      
      
      
      [Thanks to Petr Mladek for improving the commit message.]
      
      Signed-off-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      3ec24776
    • Josh Poimboeuf's avatar
      livepatch: change to a per-task consistency model · d83a7cb3
      Josh Poimboeuf authored
      Change livepatch to use a basic per-task consistency model.  This is the
      foundation which will eventually enable us to patch those ~10% of
      security patches which change function or data semantics.  This is the
      biggest remaining piece needed to make livepatch more generally useful.
      
      This code stems from the design proposal made by Vojtech [1] in November
      2014.  It's a hybrid of kGraft and kpatch: it uses kGraft's per-task
      consistency and syscall barrier switching combined with kpatch's stack
      trace switching.  There are also a number of fallback options which make
      it quite flexible.
      
      Patches are applied on a per-task basis, when the task is deemed safe to
      switch over.  When a patch is enabled, livepatch enters into a
      transition state where tasks are converging to the patched state.
      Usually this transition state can complete in a few seconds.  The same
      sequence occurs when a patch is disabled, except the tasks converge from
      the patched state to the unpatched state.
      
      An interrupt handler inherits the patched state of the task it
      interrupts.  The same is true for forked tasks: the child inherits the
      patched state of the parent.
      
      Livepatch uses several complementary approaches to determine when it's
      safe to patch tasks:
      
      1. The first and most effective approach is stack checking of sleeping
         tasks.  If no affected functions are on the stack of a given task,
         the task is patched.  In most cases this will patch most or all of
         the tasks on the first try.  Otherwise it'll keep trying
         periodically.  This option is only available if the architecture has
         reliable stacks (HAVE_RELIABLE_STACKTRACE).
      
      2. The second approach, if needed, is kernel exit switching.  A
         task is switched when it returns to user space from a system call, a
         user space IRQ, or a signal.  It's useful in the following cases:
      
         a) Patching I/O-bound user tasks which are sleeping on an affected
            function.  In this case you have to send SIGSTOP and SIGCONT to
            force it to exit the kernel and be patched.
         b) Patching CPU-bound user tasks.  If the task is highly CPU-bound
            then it will get patched the next time it gets interrupted by an
            IRQ.
         c) In the future it could be useful for applying patches for
            architectures which don't yet have HAVE_RELIABLE_STACKTRACE.  In
            this case you would have to signal most of the tasks on the
            system.  However this isn't supported yet because there's
            currently no way to patch kthreads without
            HAVE_RELIABLE_STACKTRACE.
      
      3. For idle "swapper" tasks, since they don't ever exit the kernel, they
         instead have a klp_update_patch_state() call in the idle loop which
         allows them to be patched before the CPU enters the idle state.
      
         (Note there's not yet such an approach for kthreads.)
      
      All the above approaches may be skipped by setting the 'immediate' flag
      in the 'klp_patch' struct, which will disable per-task consistency and
      patch all tasks immediately.  This can be useful if the patch doesn't
      change any function or data semantics.  Note that, even with this flag
      set, it's possible that some tasks may still be running with an old
      version of the function, until that function returns.
      
      There's also an 'immediate' flag in the 'klp_func' struct which allows
      you to specify that certain functions in the patch can be applied
      without per-task consistency.  This might be useful if you want to patch
      a common function like schedule(), and the function change doesn't need
      consistency but the rest of the patch does.
      
      For architectures which don't have HAVE_RELIABLE_STACKTRACE, the user
      must set patch->immediate which causes all tasks to be patched
      immediately.  This option should be used with care, only when the patch
      doesn't change any function or data semantics.
      
      In the future, architectures which don't have HAVE_RELIABLE_STACKTRACE
      may be allowed to use per-task consistency if we can come up with
      another way to patch kthreads.
      
      The /sys/kernel/livepatch/<patch>/transition file shows whether a patch
      is in transition.  Only a single patch (the topmost patch on the stack)
      can be in transition at a given time.  A patch can remain in transition
      indefinitely, if any of the tasks are stuck in the initial patch state.
      
      A transition can be reversed and effectively canceled by writing the
      opposite value to the /sys/kernel/livepatch/<patch>/enabled file while
      the transition is in progress.  Then all the tasks will attempt to
      converge back to the original patch state.
      
      [1] https://lkml.kernel.org/r/20141107140458.GA21774@suse.cz
      
      
      
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: Ingo Molnar <mingo@kernel.org>        # for the scheduler changes
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      d83a7cb3
  20. Jan 26, 2017
  21. Jan 11, 2017
    • Miroslav Benes's avatar
      livepatch: doc: remove the limitation for schedule() patching · 372e2db7
      Miroslav Benes authored
      
      The Limitations section of the documentation describes the impossibility
      to livepatch anything that is inlined to __schedule() function. This had
      been true till 4.9 kernel came. Thanks to commit 0100301b
      ("sched/x86: Rewrite the switch_to() code") from Brian Gerst there is
      __switch_to_asm function now (implemented in assembly) called properly
      from context_switch(). RIP is thus saved on the stack and a task would
      return to proper version of __schedule() et al. functions.
      
      Of course __switch_to_asm() is not patchable for the reason described in
      the section. But there is no __fentry__ call and I cannot imagine a
      reason to do it anyway.
      
      Therefore, remove the paragraphs from the section.
      
      Signed-off-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      372e2db7
  22. Dec 09, 2016
  23. Aug 18, 2016
  24. Apr 27, 2016
    • Petr Mladek's avatar
      livepatch: Add some basic livepatch documentation · 5e4e3844
      Petr Mladek authored
      
      livepatch framework deserves some documentation, definitely.
      This is an attempt to provide some basic info. I hope that
      it will be useful for both LivePatch producers and also
      potential developers of the framework itself.
      
      [jkosina@suse.cz:
       - incorporated feedback (grammar fixes) from
         Chris J Arges <chris.j.arges@canonical.com>
       - s/LivePatch/livepatch in changelog as pointed out by
         Josh Poimboeuf <jpoimboe@redhat.com>
       - incorporated part of feedback (grammar fixes / reformulations) from
         Balbir Singh <bsingharora@gmail.com>
      ]
      
      Acked-by: default avatarJessica Yu <jeyu@redhat.com>
      Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      5e4e3844
  25. Apr 01, 2016
Loading