Skip to content
Snippets Groups Projects
  1. Dec 01, 2022
    • Dmitry Safonov's avatar
      jump_label: Prevent key->enabled int overflow · eb8c5072
      Dmitry Safonov authored
      
      1. With CONFIG_JUMP_LABEL=n static_key_slow_inc() doesn't have any
         protection against key->enabled refcounter overflow.
      2. With CONFIG_JUMP_LABEL=y static_key_slow_inc_cpuslocked()
         still may turn the refcounter negative as (v + 1) may overflow.
      
      key->enabled is indeed a ref-counter as it's documented in multiple
      places: top comment in jump_label.h, Documentation/staging/static-keys.rst,
      etc.
      
      As -1 is reserved for static key that's in process of being enabled,
      functions would break with negative key->enabled refcount:
      - for CONFIG_JUMP_LABEL=n negative return of static_key_count()
        breaks static_key_false(), static_key_true()
      - the ref counter may become 0 from negative side by too many
        static_key_slow_inc() calls and lead to use-after-free issues.
      
      These flaws result in that some users have to introduce an additional
      mutex and prevent the reference counter from overflowing themselves,
      see bpf_enable_runtime_stats() checking the counter against INT_MAX / 2.
      
      Prevent the reference counter overflow by checking if (v + 1) > 0.
      Change functions API to return whether the increment was successful.
      
      Signed-off-by: default avatarDmitry Safonov <dima@arista.com>
      Acked-by: default avatarJakub Kicinski <kuba@kernel.org>
      Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
      eb8c5072
  2. Oct 27, 2022
  3. Jun 24, 2022
  4. Jul 05, 2021
  5. May 12, 2021
  6. Mar 19, 2021
  7. Dec 18, 2020
  8. Oct 16, 2020
  9. Sep 01, 2020
  10. Aug 29, 2019
    • Andrew Murray's avatar
      jump_label: Don't warn on __exit jump entries · 8f35eaa5
      Andrew Murray authored
      
      On architectures that discard .exit.* sections at runtime, a
      warning is printed for each jump label that is used within an
      in-kernel __exit annotated function:
      
      can't patch jump_label at ehci_hcd_cleanup+0x8/0x3c
      WARNING: CPU: 0 PID: 1 at kernel/jump_label.c:410 __jump_label_update+0x12c/0x138
      
      As these functions will never get executed (they are free'd along
      with the rest of initmem) - we do not need to patch them and should
      not display any warnings.
      
      The warning is displayed because the test required to satisfy
      jump_entry_is_init is based on init_section_contains (__init_begin to
      __init_end) whereas the test in __jump_label_update is based on
      init_kernel_text (_sinittext to _einittext) via kernel_text_address).
      
      Fixes: 19483677 ("jump_label: Annotate entries that operate on __init code earlier")
      Signed-off-by: default avatarAndrew Murray <andrew.murray@arm.com>
      Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Acked-by: default avatarMark Rutland <mark.rutland@arm.com>
      Signed-off-by: default avatarWill Deacon <will@kernel.org>
      8f35eaa5
  11. Jun 17, 2019
    • Daniel Bristot de Oliveira's avatar
      jump_label: Batch updates if arch supports it · c2ba8a15
      Daniel Bristot de Oliveira authored and Ingo Molnar's avatar Ingo Molnar committed
      
      If the architecture supports the batching of jump label updates, use it!
      
      An easy way to see the benefits of this patch is switching the
      schedstats on and off. For instance:
      
      -------------------------- %< ----------------------------
        #!/bin/sh
        while [ true ]; do
            sysctl -w kernel.sched_schedstats=1
            sleep 2
            sysctl -w kernel.sched_schedstats=0
            sleep 2
        done
      -------------------------- >% ----------------------------
      
      while watching the IPI count:
      
      -------------------------- %< ----------------------------
        # watch -n1 "cat /proc/interrupts | grep Function"
      -------------------------- >% ----------------------------
      
      With the current mode, it is possible to see +- 168 IPIs each 2 seconds,
      while with this patch the number of IPIs goes to 3 each 2 seconds.
      
      Regarding the performance impact of this patch set, I made two measurements:
      
          The time to update a key (the task that is causing the change)
          The time to run the int3 handler (the side effect on a thread that
                                            hits the code being changed)
      
      The schedstats static key was chosen as the key to being switched on and off.
      The reason being is that it is used in more than 56 places, in a hot path. The
      change in the schedstats static key will be done with the following command:
      
      while [ true ]; do
          sysctl -w kernel.sched_schedstats=1
          usleep 500000
          sysctl -w kernel.sched_schedstats=0
          usleep 500000
      done
      
      In this way, they key will be updated twice per second. To force the hit of the
      int3 handler, the system will also run a kernel compilation with two jobs per
      CPU. The test machine is a two nodes/24 CPUs box with an Intel Xeon processor
      @2.27GHz.
      
      Regarding the update part, on average, the regular kernel takes 57 ms to update
      the schedstats key, while the kernel with the batch updates takes just 1.4 ms
      on average. Although it seems to be too good to be true, it makes sense: the
      schedstats key is used in 56 places, so it was expected that it would take
      around 56 times to update the keys with the current implementation, as the
      IPIs are the most expensive part of the update.
      
      Regarding the int3 handler, the non-batch handler takes 45 ns on average, while
      the batch version takes around 180 ns. At first glance, it seems to be a high
      value. But it is not, considering that it is doing 56 updates, rather than one!
      It is taking four times more, only. This gain is possible because the patch
      uses a binary search in the vector: log2(56)=5.8. So, it was expected to have
      an overhead within four times.
      
      (voice of tv propaganda) But, that is not all! As the int3 handler keeps on for
      a shorter period (because the update part is on for a shorter time), the number
      of hits in the int3 handler decreased by 10%.
      
      The question then is: Is it worth paying the price of "135 ns" more in the int3
      handler?
      
      Considering that, in this test case, we are saving the handling of 53 IPIs,
      that takes more than these 135 ns, it seems to be a meager price to be paid.
      Moreover, the test case was forcing the hit of the int3, in practice, it
      does not take that often. While the IPI takes place on all CPUs, hitting
      the int3 handler or not!
      
      For instance, in an isolated CPU with a process running in user-space
      (nohz_full use-case), the chances of hitting the int3 handler is barely zero,
      while there is no way to avoid the IPIs. By bounding the IPIs, we are improving
      a lot this scenario.
      
      Signed-off-by: default avatarDaniel Bristot de Oliveira <bristot@redhat.com>
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Cc: Borislav Petkov <bp@alien8.de>
      Cc: Chris von Recklinghausen <crecklin@redhat.com>
      Cc: Clark Williams <williams@redhat.com>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: H. Peter Anvin <hpa@zytor.com>
      Cc: Jason Baron <jbaron@akamai.com>
      Cc: Jiri Kosina <jkosina@suse.cz>
      Cc: Josh Poimboeuf <jpoimboe@redhat.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Marcelo Tosatti <mtosatti@redhat.com>
      Cc: Masami Hiramatsu <mhiramat@kernel.org>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Scott Wood <swood@redhat.com>
      Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Link: https://lkml.kernel.org/r/acc891dbc2dbc9fd616dd680529a2337b1d1274c.1560325897.git.bristot@redhat.com
      
      
      Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
      c2ba8a15
    • Daniel Bristot de Oliveira's avatar
      jump_label: Sort entries of the same key by the code · 0f133021
      Daniel Bristot de Oliveira authored and Ingo Molnar's avatar Ingo Molnar committed
      
      In the batching mode, all the entries of a given key are updated at once.
      During the update of a key, a hit in the int3 handler will check if the
      hitting code address belongs to one of these keys.
      
      To optimize the search of a given code in the vector of entries being
      updated, a binary search is used. The binary search relies on the order
      of the entries of a key by its code. Hence the keys need to be sorted
      by the code too, so sort the entries of a given key by the code.
      
      Signed-off-by: default avatarDaniel Bristot de Oliveira <bristot@redhat.com>
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Cc: Borislav Petkov <bp@alien8.de>
      Cc: Chris von Recklinghausen <crecklin@redhat.com>
      Cc: Clark Williams <williams@redhat.com>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: H. Peter Anvin <hpa@zytor.com>
      Cc: Jason Baron <jbaron@akamai.com>
      Cc: Jiri Kosina <jkosina@suse.cz>
      Cc: Josh Poimboeuf <jpoimboe@redhat.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Marcelo Tosatti <mtosatti@redhat.com>
      Cc: Masami Hiramatsu <mhiramat@kernel.org>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Scott Wood <swood@redhat.com>
      Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Link: https://lkml.kernel.org/r/f57ae83e0592418ba269866bb7ade570fc8632e0.1560325897.git.bristot@redhat.com
      
      
      Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
      0f133021
    • Daniel Bristot de Oliveira's avatar
      jump_label: Add a jump_label_can_update() helper · e1aacb3f
      Daniel Bristot de Oliveira authored and Ingo Molnar's avatar Ingo Molnar committed
      
      Move the check if a jump_entry is valid to a function. No functional
      change.
      
      Signed-off-by: default avatarDaniel Bristot de Oliveira <bristot@redhat.com>
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Cc: Borislav Petkov <bp@alien8.de>
      Cc: Chris von Recklinghausen <crecklin@redhat.com>
      Cc: Clark Williams <williams@redhat.com>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: H. Peter Anvin <hpa@zytor.com>
      Cc: Jason Baron <jbaron@akamai.com>
      Cc: Jiri Kosina <jkosina@suse.cz>
      Cc: Josh Poimboeuf <jpoimboe@redhat.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Marcelo Tosatti <mtosatti@redhat.com>
      Cc: Masami Hiramatsu <mhiramat@kernel.org>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Scott Wood <swood@redhat.com>
      Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Link: https://lkml.kernel.org/r/56b69bd3f8e644ed64f2dbde7c088030b8cbe76b.1560325897.git.bristot@redhat.com
      
      
      Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
      e1aacb3f
  12. May 21, 2019
  13. Apr 29, 2019
  14. Apr 03, 2019
  15. Jan 06, 2019
    • Masahiro Yamada's avatar
      jump_label: move 'asm goto' support test to Kconfig · e9666d10
      Masahiro Yamada authored
      
      Currently, CONFIG_JUMP_LABEL just means "I _want_ to use jump label".
      
      The jump label is controlled by HAVE_JUMP_LABEL, which is defined
      like this:
      
        #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
        # define HAVE_JUMP_LABEL
        #endif
      
      We can improve this by testing 'asm goto' support in Kconfig, then
      make JUMP_LABEL depend on CC_HAS_ASM_GOTO.
      
      Ugly #ifdef HAVE_JUMP_LABEL will go away, and CONFIG_JUMP_LABEL will
      match to the real kernel capability.
      
      Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
      Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)
      Tested-by: default avatarSedat Dilek <sedat.dilek@gmail.com>
      e9666d10
  16. Oct 02, 2018
  17. Sep 27, 2018
  18. Sep 10, 2018
  19. Mar 20, 2018
    • Josh Poimboeuf's avatar
      jump_label: Disable jump labels in __exit code · 578ae447
      Josh Poimboeuf authored and Ingo Molnar's avatar Ingo Molnar committed
      
      With the following commit:
      
        33352244 ("jump_label: Explicitly disable jump labels in __init code")
      
      ... we explicitly disabled jump labels in __init code, so they could be
      detected and not warned about in the following commit:
      
        dc1dd184 ("jump_label: Warn on failed jump_label patching attempt")
      
      In-kernel __exit code has the same issue.  It's never used, so it's
      freed along with the rest of initmem.  But jump label entries in __exit
      code aren't explicitly disabled, so we get the following warning when
      enabling pr_debug() in __exit code:
      
        can't patch jump_label at dmi_sysfs_exit+0x0/0x2d
        WARNING: CPU: 0 PID: 22572 at kernel/jump_label.c:376 __jump_label_update+0x9d/0xb0
      
      Fix the warning by disabling all jump labels in initmem (which includes
      both __init and __exit code).
      
      Reported-and-tested-by: default avatarLi Wang <liwang@redhat.com>
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Cc: Borislav Petkov <bp@suse.de>
      Cc: Jason Baron <jbaron@akamai.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Fixes: dc1dd184 ("jump_label: Warn on failed jump_label patching attempt")
      Link: http://lkml.kernel.org/r/7121e6e595374f06616c505b6e690e275c0054d1.1521483452.git.jpoimboe@redhat.com
      
      
      Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
      578ae447
  20. Mar 14, 2018
  21. Feb 21, 2018
  22. Jan 24, 2018
  23. Nov 14, 2017
  24. Oct 19, 2017
    • Borislav Petkov's avatar
      locking/static_keys: Improve uninitialized key warning · 5cdda511
      Borislav Petkov authored and Ingo Molnar's avatar Ingo Molnar committed
      
      Right now it says:
      
        static_key_disable_cpuslocked used before call to jump_label_init
        ------------[ cut here ]------------
        WARNING: CPU: 0 PID: 0 at kernel/jump_label.c:161 static_key_disable_cpuslocked+0x68/0x70
        Modules linked in:
        CPU: 0 PID: 0 Comm: swapper Not tainted 4.14.0-rc5+ #1
        Hardware name: SGI.COM C2112-4GP3/X10DRT-P-Series, BIOS 2.0a 05/09/2016
        task: ffffffff81c0e480 task.stack: ffffffff81c00000
        RIP: 0010:static_key_disable_cpuslocked+0x68/0x70
        RSP: 0000:ffffffff81c03ef0 EFLAGS: 00010096 ORIG_RAX: 0000000000000000
        RAX: 0000000000000041 RBX: ffffffff81c32680 RCX: ffffffff81c5cbf8
        RDX: 0000000000000001 RSI: 0000000000000092 RDI: 0000000000000002
        RBP: ffff88807fffd240 R08: 726f666562206465 R09: 0000000000000136
        R10: 0000000000000000 R11: 696e695f6c656261 R12: ffffffff82158900
        R13: ffffffff8215f760 R14: 0000000000000001 R15: 0000000000000008
        FS:  0000000000000000(0000) GS:ffff883f7f400000(0000) knlGS:0000000000000000
        CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
        CR2: ffff88807ffff000 CR3: 0000000001c09000 CR4: 00000000000606b0
        Call Trace:
         static_key_disable+0x16/0x20
         start_kernel+0x15a/0x45d
         ? load_ucode_intel_bsp+0x11/0x2d
         secondary_startup_64+0xa5/0xb0
        Code: 48 c7 c7 a0 15 cf 81 e9 47 53 4b 00 48 89 df e8 5f fc ff ff eb e8 48 c7 c6 \
      	c0 97 83 81 48 c7 c7 d0 ff a2 81 31 c0 e8 c5 9d f5 ff <0f> ff eb a7 0f ff eb \
      	b0 e8 eb a2 4b 00 53 48 89 fb e8 42 0e f0
      
      but it doesn't tell me which key it is. So dump the key's name too:
      
        static_key_disable_cpuslocked(): static key 'virt_spin_lock_key' used before call to jump_label_init()
      
      And that makes pinpointing which key is causing that a lot easier.
      
       include/linux/jump_label.h           |   14 +++++++-------
       include/linux/jump_label_ratelimit.h |    6 +++---
       kernel/jump_label.c                  |   14 +++++++-------
       3 files changed, 17 insertions(+), 17 deletions(-)
      
      Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
      Reviewed-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
      Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
      Cc: Hannes Frederic Sowa <hannes@stressinduktion.org>
      Cc: Jason Baron <jbaron@akamai.com>
      Cc: Juergen Gross <jgross@suse.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Paolo Bonzini <pbonzini@redhat.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Link: http://lkml.kernel.org/r/20171018152428.ffjgak4o25f7ept6@pd.tnic
      
      
      Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
      5cdda511
  25. Aug 10, 2017
Loading