kprobes: Reduce registration and optimization lookup costs
Subsystems (7)
Jiri Olsa
reviewercover kprobes: Reduce registration and optimization lookup costs
hi,
this series reduces repeated lookup work when registering and preparing large numbers of kprobes, particularly on x86 with optimized probes.
The kernel already provides interfaces for registering large numbers of kprobes. Faster registration will become more important for our planned inline function tracing support, which will use kprobes.
The changes address two kinds of lookup:
- add indexes sorted by code address for core static-call and jump-label reservation checks. The original tables remain sorted by key for updates. Build the indexes during late init and retain linear scans if allocation fails. Module reservation checks remain linear.
- add an address-ordered rbtree of registered kprobes and iterators for ranges and monotonically increasing addresses. Convert instruction recovery and optimization checks to reuse these iterators instead of repeatedly looking up individual addresses.
The series also determines x86 jump-label instruction sizes directly from their opcode or NOP encoding, avoiding instruction decoding.
The final samples patch is included only to demonstrate the testing and is not intended for merging. It adds 50k functions to the kernel, along with sample module code that registers kprobes on them, simulating kprobes on inline kernel functions.
There's overall speedup about 10x for registering the kprobe, please check the perf stats below. Plus there's one more low hanging fruit leftover I plan to send later for bug entry lookup that will add bit more speed up.
The test below measures perf stat for the probe_many_example.ko module to attach kprobes to 20,000 functions.
Performance counter stats for:
'insmod ./samples/kprobes/kprobe_many_example.ko nr_functions=20000':
Before:
50,908,260,992 cycles:k
53,886,723,615 instructions:k
10.072721766 seconds time elapsedAfter:
5,122,978,035 cycles:k
5,620,945,319 instructions:k
1.027289983 seconds time elapsedthanks,
jirka
---
Jiri Olsa (13):
static_call: Index code text reservation lookups
jump_label: Index text reservation lookups
jump_label/x86: Avoid decoding instructions for jump entry size
kprobes: Maintain an rbtree of registered probes
kprobes: Add an iterator for address ranges
kprobes: Add an address iterator for probe lookup
kprobes: Use kprobe iterator in get_optimized_kprobe x86/kprobes: Use kprobe iterator in __recover_optprobed_insn x86/kprobes: Use kprobe iterator in arch_check_optimized_kprobe x86/kprobes: Use kprobe address iterator in can_optimize x86/kprobes: Use kprobe address iterator in can_probe x86/kprobes: Use kprobe address iterator in in copy_optimized_instructions samples/kprobes: Add batch probe example
arch/x86/kernel/jump_label.c | 14 +++++++---- arch/x86/kernel/kprobes/common.h | 7 +++--- arch/x86/kernel/kprobes/core.c | 26 +++++++++++-------- arch/x86/kernel/kprobes/opt.c | 32 ++++++++++++++--------- include/linux/kprobes.h | 25 ++++++++++++++++++ kernel/Makefile | 3 +++ kernel/jump_label.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- kernel/kprobes.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ kernel/kprobes_many.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ kernel/static_call_inline.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- samples/Kconfig | 8 ++++++ samples/kprobes/Makefile | 1 + samples/kprobes/kprobe_many_example.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 598 insertions(+), 42 deletions(-) create mode 100644 kernel/kprobes_many.c create mode 100644 samples/kprobes/kprobe_many_example.c
Jiri Olsa
reviewerPATCH 1/13 static_call: Index code text reservation lookups
Text reservation checks scan every static call site. Build an address-sorted index for core text sites and use binary search to find the starting point near the requested range.
Module indexing is intentionally unsupported. Module reservation checks continue to use a linear scan, avoiding per-module index allocation and teardown complexity.
--- kernel/static_call_inline.c | 114 +++++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 2 deletions(-)
kernel/static_call_inline.c +112 -2
@@ -15,6 +15,18 @@ extern struct static_call_site __start_static_call_sites[],extern struct static_call_tramp_key __start_static_call_tramp_key[], __stop_static_call_tramp_key[]; +struct static_call_site_index { +struct static_call_site *base; +unsigned int nr; +u32 entries[]; +}; + +/* +* Index static call sites by code address for text reservation lookups. +* The site table itself is sorted by static-call-key address. +*/ +static struct static_call_site_index *static_call_site_index; +int static_call_initialized;/*
@@ -41,7 +53,7 @@ static void static_call_unlock(void)mutex_unlock (&static_call_mutex); } -static inline void *static_call_addr (struct static_call_site *site) +static inline void *static_call_addr (const struct static_call_site *site) {return (void *)((long )site->addr + (long )&site->addr); }
@@ -301,6 +313,79 @@ static int addr_conflict(struct static_call_site *site, void *start, void *end)return 0 ; } +static int static_call_site_index_cmp (const void *a,const void *b,const void *priv) +{ +const struct static_call_site *base = priv; +unsigned int ia = *(u32 *)a; +unsigned int ib = *(u32 *)b; +unsigned long ca = (unsigned long )static_call_addr (base + ia); +unsigned long cb = (unsigned long )static_call_addr (base + ib); + +return cmp_int (ca, cb); +} + +static struct static_call_site_index * +static_call_site_index_alloc (struct static_call_site *start, +struct static_call_site *stop) +{ +struct static_call_site_index *index; +unsigned int nr = stop - start; +unsigned int i; + +if (!nr) +return NULL ; + + index =kvmalloc_flex (*index, entries, nr,GFP_KERNEL ); +if (!index) +return NULL ; + + index->nr = nr; + index->base = start; +for (i =0 ; i < nr; i++) + index->entries[i] = i; + +sort_r (index->entries, nr,sizeof (index->entries[0 ]), + static_call_site_index_cmp,NULL , start); + +return index; +} + +static int +static_call_site_index_text_reserved (struct static_call_site_index *index, +void *start,void *end,bool init) +{ +unsigned int low =0 , high = index->nr, i; + +/* Find the first site after @start. */ +while (low < high) { +unsigned int mid = low + (high - low) /2 ; +void *addr =static_call_addr (index->base + index->entries[mid]); + +if (addr <= start) + low = mid +1 ; +else + high = mid; + } + +/* The preceding site may overlap @start. */ +if (low) + low--; + +for (i = low; i < index->nr; i++) { +struct static_call_site *site = index->base + index->entries[i]; +void *addr =static_call_addr (site); + +/* Later sites cannot overlap once this address is past @end. */ +if (addr > end) +break ; +if ((init || !static_call_is_init (site)) && +addr_conflict (site, start, end)) +return 1 ; + } + +return 0 ; +} +static int __static_call_text_reserved (struct static_call_site *iter_start,struct static_call_site *iter_stop,void *start,void *end,bool init)
@@ -483,7 +568,15 @@ static inline int __static_call_mod_text_reserved(void *start, void *end)int static_call_text_reserved (void *start,void *end) {bool init = system_state <SYSTEM_RUNNING ; -int ret =__static_call_text_reserved (__start_static_call_sites, +struct static_call_site_index *index; +int ret; + +/* Pairs with the release in static_call_site_index_build(). */ + index =smp_load_acquire (&static_call_site_index); +if (index) + ret =static_call_site_index_text_reserved (index, start, end, init); +else + ret =__static_call_text_reserved (__start_static_call_sites, __stop_static_call_sites, start, end, init);if (ret)
@@ -522,6 +615,23 @@ int __init static_call_init(void) }early_initcall (static_call_init); +static int __init static_call_site_index_build (void ) +{ +struct static_call_site_index *index; + +/* +* Build the index in an initcall so normal memory allocation is +* available, static_call_init() can run before that. +*/ + index =static_call_site_index_alloc (__start_static_call_sites, + __stop_static_call_sites); + +/* Publish the initialized index to static_call_text_reserved(). */ +smp_store_release (&static_call_site_index, index); +return 0 ; +} +late_initcall (static_call_site_index_build); +#ifdef CONFIG_STATIC_CALL_SELFTEST static int func_a (int x)
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 2/13 jump_label: Index text reservation lookups
In reply to Jiri OlsaText reservation checks scan every jump entry. Build an index sorted by code address for core entries and use binary search to find the starting point near the requested range. Keep the original table sorted by static-key address for updates.
Module reservation checks continue to use a linear scan, avoiding per-module index allocation and teardown complexity.
--- kernel/jump_label.c | 115 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-)
kernel/jump_label.c +114 -1
@@ -8,6 +8,7 @@*/ #include <linux/memory.h> #include <linux/uaccess.h> +#include <linux/cache.h> #include <linux/module.h> #include <linux/list.h> #include <linux/slab.h>
@@ -22,6 +23,18 @@/* mutex to protect coming/going of the jump_label table */ static DEFINE_MUTEX (jump_label_mutex); +struct jump_entry_index { +struct jump_entry *base; +unsigned int nr; +u32 entries[]; +}; + +/* +* Index jump entries by code address for text reservation lookups. The jump +* table itself is sorted by static-key address, then by code address per key. +*/ +static struct jump_entry_index *jump_entry_index; +void jump_label_lock (void ) {mutex_lock (&jump_label_mutex);
@@ -381,6 +394,81 @@ static int addr_conflict(struct jump_entry *entry, void *start, void *end)return 0 ; } +static int jump_entry_index_cmp (const void *a,const void *b,const void *priv) +{ +const struct jump_entry *base = priv; +unsigned int ia = *(u32 *)a; +unsigned int ib = *(u32 *)b; +unsigned long ca =jump_entry_code (base + ia); +unsigned long cb =jump_entry_code (base + ib); + +if (ca < cb) +return -1 ; +if (ca > cb) +return 1 ; +return 0 ; +} + +static struct jump_entry_index * +jump_entry_index_alloc (struct jump_entry *start,struct jump_entry *stop) +{ +unsigned int i, nr = stop - start; +struct jump_entry_index *index; + +if (!nr) +return NULL ; + + index =kvmalloc_flex (*index, entries, nr,GFP_KERNEL ); +if (!index) +return NULL ; + + index->nr = nr; + index->base = start; +for (i =0 ; i < nr; i++) + index->entries[i] = i; + +sort_r (index->entries, nr,sizeof (index->entries[0 ]), + jump_entry_index_cmp,NULL , start); +return index; +} + +static int jump_entry_index_text_reserved (struct jump_entry_index *index, +void *start,void *end,bool init) +{ +unsigned long start_addr = (unsigned long )start; +unsigned long end_addr = (unsigned long )end; +unsigned int low =0 , high = index->nr, i; + +/* Find the first entry after @start. */ +while (low < high) { +unsigned int mid = low + (high - low) /2 ; +unsigned long addr =jump_entry_code (index->base + index->entries[mid]); + +if (addr <= start_addr) + low = mid +1 ; +else + high = mid; + } + +/* The preceding entry may start before @start and overlap it. */ +if (low) + low--; + +for (i = low; i < index->nr; i++) { +struct jump_entry *entry = index->base + index->entries[i]; +unsigned long addr =jump_entry_code (entry); + +/* Later entries cannot overlap once this address is past @end. */ +if (addr > end_addr) +break ; +if ((init || !jump_entry_is_init (entry)) && +addr_conflict (entry, start, end)) +return 1 ; + } + +return 0 ; +} +static int __jump_label_text_reserved (struct jump_entry *iter_start,struct jump_entry *iter_stop,void *start,void *end,bool init) {
@@ -869,8 +957,16 @@ early_initcall(jump_label_init_module);*/ int jump_label_text_reserved (void *start,void *end) { +struct jump_entry_index *index;bool init = system_state <SYSTEM_RUNNING ; -int ret =__jump_label_text_reserved (__start___jump_table, +int ret; + +/* Pairs with the release in jump_entry_index_build(). */ + index =smp_load_acquire (&jump_entry_index); +if (index) + ret =jump_entry_index_text_reserved (index, start, end, init); +else + ret =__jump_label_text_reserved (__start___jump_table, __stop___jump_table, start, end, init);if (ret)
@@ -909,6 +1005,23 @@ static void jump_label_update(struct static_key *key)__jump_label_update (key, entry, stop, init); } +static int __init jump_entry_index_build (void ) +{ +struct jump_entry_index *index; + +/* +* Build the index in an initcall so normal memory allocation is +* available; jump_label_init() runs before that. +*/ + index =jump_entry_index_alloc (__start___jump_table, __stop___jump_table); + +/* Publish the initialized index to jump_label_text_reserved(). */ +smp_store_release (&jump_entry_index, index); + +return 0 ; +} +late_initcall (jump_entry_index_build); +#ifdef CONFIG_STATIC_KEYS_SELFTEST static DEFINE_STATIC_KEY_TRUE (sk_true);static DEFINE_STATIC_KEY_FALSE (sk_false);
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 3/13 jump_label/x86: Avoid decoding instructions for jump entry size
In reply to Jiri OlsaDetermine the jump entry size from its opcode or NOP encoding instead of decoding the instruction. This reduces kprobe registration time when checking jump label entries.
--- arch/x86/kernel/jump_label.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
arch/x86/kernel/jump_label.c +9 -5
@@ -15,16 +15,20 @@#include <asm/kprobes.h> #include <asm/alternative.h> #include <asm/text-patching.h> -#include <asm/insn.h> int arch_jump_entry_size (struct jump_entry *entry) { -struct insn insn = {}; +const u8 *code = (void *)jump_entry_code (entry); -insn_decode_kernel (&insn, (void *)jump_entry_code (entry)); -BUG_ON (insn.length !=2 && insn.length !=5 ); +if (code[0 ] ==JMP8_INSN_OPCODE || + !memcmp (code, x86_nops[JMP8_INSN_SIZE ],JMP8_INSN_SIZE )) +return JMP8_INSN_SIZE ; -return insn.length; +if (code[0 ] ==JMP32_INSN_OPCODE || + !memcmp (code, x86_nops[JMP32_INSN_SIZE ],JMP32_INSN_SIZE )) +return JMP32_INSN_SIZE ; + +BUG (); }struct jump_label_patch {
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 4/13 kprobes: Maintain an rbtree of registered probes
In reply to Jiri OlsaAdd an address-ordered rbtree for registered probes. Keep it synchronized with the hash table when probes are added, removed, or replaced by aggregator probes.
--- include/linux/kprobes.h | 2 ++ kernel/kprobes.c | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)
include/linux/kprobes.h +2 -0
@@ -28,6 +28,7 @@#include <linux/ftrace.h> #include <linux/objpool.h> #include <linux/rethook.h> +#include <linux/rbtree.h> #include <asm/kprobes.h> #ifdef CONFIG_KPROBES
@@ -58,6 +59,7 @@ typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,struct kprobe {struct hlist_node hlist; +struct rb_node rb_node;/* list of kprobes for multi-handler support */ struct list_head list;
kernel/kprobes.c +49 -0
114 lines not on this page · raw message
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 5/13 kprobes: Add an iterator for address ranges
In reply to Jiri OlsaAdd an iterator that walks registered probes in address order within a specified range. Use the kprobe rbtree to find the first probe at or after the start address, then advance through the tree.
Require kprobe_mutex to be held while iterating and provide a for_each_kprobe() helper.
--- include/linux/kprobes.h | 13 +++++++++++++ kernel/kprobes.c | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)
include/linux/kprobes.h +13 -0
@@ -95,6 +95,11 @@ struct kprobe {u32 flags; }; +struct kprobe_iter { +struct kprobe *next; +unsigned long end; +}; +/* Kprobe status flags */ #define KPROBE_FLAG_GONE 1/* breakpoint has already gone */ #define KPROBE_FLAG_DISABLED 2/* probe is temporarily disabled */
@@ -386,6 +391,14 @@ static inline void kprobe_ftrace_kill(void) {}/* Get the kprobe at this addr (if any) - called with preemption disabled */ struct kprobe *get_kprobe (void *addr); +struct kprobe *kprobe_iter_start (struct kprobe_iter *iter, +unsigned long start,unsigned long end); +struct kprobe *kprobe_iter_next (struct kprobe_iter *iter); + +#define for_each_kprobe (iter, p, start, end) \ + for ((p) = kprobe_iter_start(&(iter), (start), (end)); \ + (p); (p) = kprobe_iter_next(&(iter))) +/* kprobe_running() will just return the current_kprobe on this CPU */ static inline struct kprobe *kprobe_running (void ) {
kernel/kprobes.c +42 -0
48 lines not on this page · raw message
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 6/13 kprobes: Add an address iterator for probe lookup
In reply to Jiri OlsaAdd kprobe_addr_iter_start() and kprobe_addr_iter_next() to look up probes at nondecreasing addresses while retaining the next candidate in the range iterator.
Provide for_each_addr_kprobe() for callers that advance the address in the loop body. Return NULL for addresses without a registered probe and require kprobe_mutex to remain held during iteration.
--- include/linux/kprobes.h | 10 ++++++++++ kernel/kprobes.c | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+)
include/linux/kprobes.h +10 -0
@@ -394,11 +394,21 @@ struct kprobe *get_kprobe(void *addr);struct kprobe *kprobe_iter_start (struct kprobe_iter *iter,unsigned long start,unsigned long end);struct kprobe *kprobe_iter_next (struct kprobe_iter *iter); +struct kprobe *kprobe_addr_iter_start (struct kprobe_iter *iter, +unsigned long start,unsigned long end); +struct kprobe *kprobe_addr_iter_next (struct kprobe_iter *iter, +unsigned long addr);#define for_each_kprobe (iter, p, start, end) \ for ((p) = kprobe_iter_start(&(iter), (start), (end)); \ (p); (p) = kprobe_iter_next(&(iter))) +/* The caller holds kprobe_mutex and initializes and advances addr. */ +#define for_each_addr_kprobe (iter, kp, addr, end) \ + for ((kp) = kprobe_addr_iter_start(&(iter), (addr), (end)); \ + (addr) <= (end); \ + (kp) = kprobe_addr_iter_next(&(iter), (addr))) +/* kprobe_running() will just return the current_kprobe on this CPU */ static inline struct kprobe *kprobe_running (void ) {
kernel/kprobes.c +25 -0
@@ -477,6 +477,31 @@ struct kprobe *kprobe_iter_next(struct kprobe_iter *iter)return kp; } +/* Address iteration retains the candidate in iter->next until addr passes it. */ +struct kprobe *kprobe_addr_iter_start (struct kprobe_iter *iter, +unsigned long start,unsigned long end) +{ + iter->next =kprobe_iter_start (iter, start, end); +return kprobe_addr_iter_next (iter, start); +} + +/* Addresses must be nondecreasing; return NULL at addresses without a probe. */ +struct kprobe *kprobe_addr_iter_next (struct kprobe_iter *iter, +unsigned long addr) +{ +struct kprobe *kp = iter->next; + +lockdep_assert_held (&kprobe_mutex); +while (kp && (unsigned long )kp->addr < addr) { +kprobe_iter_next (iter); + kp = iter->next; + } +if (!kp || (unsigned long )kp->addr > iter->end) +return NULL ; + +return (unsigned long )kp->addr == addr ? kp :NULL ; +} +static int aggr_pre_handler (struct kprobe *p,struct pt_regs *regs);/* Return true if 'p' is an aggregator */
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 7/13 kprobes: Use kprobe iterator in get_optimized_kprobe
In reply to Jiri OlsaUse the kprobe tree iterator to find optimized probes whose instruction ranges may contain the given address. This replaces repeated hash lookups for each preceding instruction address.
--- kernel/kprobes.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
kernel/kprobes.c +10 -8
@@ -591,23 +591,25 @@ bool kprobe_disarmed(struct kprobe *p)*/ static struct kprobe *get_optimized_kprobe (kprobe_opcode_t *addr) { -int i; -struct kprobe *p; +unsigned long offset =MAX_OPTIMIZED_LENGTH /sizeof (kprobe_opcode_t) -1 ; +unsigned long addr_val = (unsigned long )addr; +struct kprobe *p, *candidate =NULL ;struct optimized_kprobe *op; +struct kprobe_iter iter; -/* Don't check i == 0, since that is a breakpoint case. */ -for (i =1 ; i <MAX_OPTIMIZED_LENGTH /sizeof (kprobe_opcode_t); i++) { - p =get_kprobe (addr - i); +/* Don't check addr itself, since that is a breakpoint case. */ +for_each_kprobe (iter, p, addr_val - offset, addr_val -1 ) {/* A disabled probe can have prepared, but inactive, optinsns. */ -if (!p || !kprobe_optready (p) ||kprobe_disarmed (p)) +if (!kprobe_optready (p) ||kprobe_disarmed (p))continue ; +/* The iterator walks low to high; keep the nearest matching probe. */ op = container_of(p, struct optimized_kprobe, kp);if (arch_within_optimized_kprobe (op, addr)) -return p; + candidate = p; } -return NULL ; +return candidate; }#define OPTPROBE_GEN_MAX 2
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 8/13 x86/kprobes: Use kprobe iterator in __recover_optprobed_insn
In reply to Jiri OlsaUse the kprobe range iterator to find an optimized probe covering the address being recovered. This replaces repeated hash lookups across the JMP32 instruction range.
--- arch/x86/kernel/kprobes/opt.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kernel/kprobes/opt.c +2 -3
@@ -37,12 +37,11 @@unsigned long __recover_optprobed_insn (kprobe_opcode_t *buf,unsigned long addr) {struct optimized_kprobe *op; +struct kprobe_iter iter;struct kprobe *kp;long offs; -int i; -for (i =0 ; i <JMP32_INSN_SIZE ; i++) { - kp =get_kprobe ((void *)addr - i); +for_each_kprobe (iter, kp, addr - (JMP32_INSN_SIZE -1 ), addr) {/* This function only handles jump-optimized kprobe */ if (kp &&kprobe_optimized (kp)) { op = container_of(kp, struct optimized_kprobe, kp);
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 9/13 x86/kprobes: Use kprobe iterator in arch_check_optimized_kprobe
In reply to Jiri Olsaarch_check_optimized_kprobe() checks each byte of the optimized range with a separate get_kprobe() lookup. Iterate over registered probes in the range instead, retaining the check for probes that are still armed.
--- arch/x86/kernel/kprobes/opt.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kernel/kprobes/opt.c +5 -4
@@ -334,12 +334,13 @@ static int can_optimize(unsigned long paddr)/* Check optimized_kprobe can actually be optimized. */ int arch_check_optimized_kprobe (struct optimized_kprobe *op) { -int i; +unsigned long start = (unsigned long )op->kp.addr; +unsigned long end = start + op->optinsn.size -1 ; +struct kprobe_iter iter;struct kprobe *p; -for (i =1 ; i < op->optinsn.size; i++) { - p =get_kprobe (op->kp.addr + i); -if (p && !kprobe_disarmed (p)) +for_each_kprobe (iter, p, start +1 , end) { +if (!kprobe_disarmed (p))return -EEXIST ; }
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 10/13 x86/kprobes: Use kprobe address iterator in can_optimize
In reply to Jiri Olsacan_optimize() already iterates over kprobes in the function. Pass the iterator's kprobe to recover_probed_instruction() so it doesn't need to look up the kprobe again for each address. Keep the lookup for callers that pass NULL.
--- arch/x86/kernel/kprobes/common.h | 4 ++-- arch/x86/kernel/kprobes/core.c | 16 ++++++++-------- arch/x86/kernel/kprobes/opt.c | 9 ++++++--- 3 files changed, 16 insertions(+), 13 deletions(-)
arch/x86/kernel/kprobes/common.h +2 -2
@@ -79,9 +79,9 @@/* Ensure if the instruction can be boostable */ extern bool can_boost (struct insn *insn,void *orig_addr); -/* Recover instruction if given address is probed */ +/* Recover instruction; @kp is NULL if the caller needs a kprobe lookup. */ extern unsigned long recover_probed_instruction (kprobe_opcode_t *buf, -unsigned long addr); +unsigned long addr,struct kprobe **kp);/* * Copy an instruction and adjust the displacement if the instruction * uses the %rip-relative addressing mode.
arch/x86/kernel/kprobes/core.c +8 -8
@@ -201,12 +201,11 @@ bool can_boost(struct insn *insn, void *addr) }static unsigned long -__recover_probed_insn (kprobe_opcode_t *buf,unsigned long addr) +__recover_probed_insn (kprobe_opcode_t *buf,unsigned long addr,struct kprobe **kpp) { -struct kprobe *kp; +struct kprobe *kp = kpp ? *kpp :get_kprobe ((void *)addr);bool faddr; - kp =get_kprobe ((void *)addr); faddr =ftrace_location (addr) == addr;/* * Use the current code if it is not modified by Kprobe
@@ -251,7 +250,8 @@ __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)* for preventing to release referencing kprobes. * Returns zero if the instruction can not get recovered (or access failed). */ -unsigned long recover_probed_instruction (kprobe_opcode_t *buf,unsigned long addr) +unsigned long recover_probed_instruction (kprobe_opcode_t *buf,unsigned long addr, +struct kprobe **kp) {unsigned long __addr;
@@ -259,7 +259,7 @@ unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addif (__addr != addr)return __addr; -return __recover_probed_insn (buf, addr); +return __recover_probed_insn (buf, addr, kp); }/* Check if insn is INT or UD */
@@ -304,7 +304,7 @@ static bool can_probe(unsigned long paddr)* relative-jump. Since the relative-jump itself is * normally used, we just go through if there is no kprobe. */ - __addr =recover_probed_instruction (buf, addr); + __addr =recover_probed_instruction (buf, addr,NULL );if (!__addr)return false;
@@ -327,7 +327,7 @@ static bool can_probe(unsigned long paddr)if (addr != paddr)return false; - __addr =recover_probed_instruction (buf, addr); + __addr =recover_probed_instruction (buf, addr,NULL );if (!__addr)return false;
@@ -394,7 +394,7 @@ kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offseint __copy_instruction (u8 *dest,u8 *src,u8 *real,struct insn *insn) {kprobe_opcode_t buf[MAX_INSN_SIZE ]; -unsigned long recovered_insn =recover_probed_instruction (buf, (unsigned long )src); +unsigned long recovered_insn =recover_probed_instruction (buf, (unsigned long )src,NULL );int ret;if (!recovered_insn || !insn)
arch/x86/kernel/kprobes/opt.c +6 -3
@@ -257,7 +257,9 @@ static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)/* Decode whole function to ensure any instructions don't jump into target */ static int can_optimize (unsigned long paddr) { -unsigned long addr, size =0 , offset =0 ; +unsigned long addr, end, size =0 , offset =0 ; +struct kprobe_iter iter; +struct kprobe *kp;struct insn insn;kprobe_opcode_t buf[MAX_INSN_SIZE ];
@@ -279,7 +281,8 @@ static int can_optimize(unsigned long paddr)/* Decode instructions */ addr = paddr - offset; -while (addr < paddr - offset + size) {/* Decode until function end */ + end = paddr - offset + size -1 ; +for_each_addr_kprobe (iter, kp, addr, end) {/* Decode until function end */ unsigned long recovered_insn;int ret;
@@ -289,7 +292,7 @@ static int can_optimize(unsigned long paddr)* we can't optimize kprobe in this function. */ return 0 ; - recovered_insn =recover_probed_instruction (buf, addr); + recovered_insn =recover_probed_instruction (buf, addr, &kp);if (!recovered_insn)return 0 ;
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 11/13 x86/kprobes: Use kprobe address iterator in can_probe
In reply to Jiri OlsaWalk the instructions before the target with for_each_addr_kprobe() and pass each matching kprobe to recover_probed_instruction(). Reuse the iterator’s kprobe for the target instruction as well.
--- arch/x86/kernel/kprobes/core.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kernel/kprobes/core.c +7 -3
@@ -287,6 +287,8 @@ static inline bool is_exception_insn(struct insn *insn)static bool can_probe (unsigned long paddr) {unsigned long addr, __addr, offset =0 ; +struct kprobe_iter iter; +struct kprobe *kp;struct insn insn;kprobe_opcode_t buf[MAX_INSN_SIZE ];
@@ -295,7 +297,9 @@ static bool can_probe(unsigned long paddr)/* Decode instructions */ addr = paddr - offset; -while (addr < paddr) { +for_each_addr_kprobe (iter, kp, addr, paddr) { +if (addr == paddr) +break ;/* * Check if the instruction has been modified by another * kprobe, in which case we replace the breakpoint by the
@@ -304,7 +308,7 @@ static bool can_probe(unsigned long paddr)* relative-jump. Since the relative-jump itself is * normally used, we just go through if there is no kprobe. */ - __addr =recover_probed_instruction (buf, addr,NULL ); + __addr =recover_probed_instruction (buf, addr, &kp);if (!__addr)return false;
@@ -327,7 +331,7 @@ static bool can_probe(unsigned long paddr)if (addr != paddr)return false; - __addr =recover_probed_instruction (buf, addr,NULL ); + __addr =recover_probed_instruction (buf, addr, &kp);if (!__addr)return false;
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 12/13 x86/kprobes: Use kprobe address iterator in in copy_optimized_instructions
In reply to Jiri Olsacopy_optimized_instructions() copies whole instructions until it covers the five-byte jump. Pass the iterator's kprobe to instruction recovery at each instruction start, avoiding a separate lookup. Other __copy_instruction() callers retain the lookup behavior by passing NULL.
--- arch/x86/kernel/kprobes/common.h | 3 ++- arch/x86/kernel/kprobes/core.c | 6 +++--- arch/x86/kernel/kprobes/opt.c | 9 +++++++-- 3 files changed, 12 insertions(+), 6 deletions(-)
arch/x86/kernel/kprobes/common.h +2 -1
@@ -86,7 +86,8 @@ extern unsigned long recover_probed_instruction(kprobe_opcode_t *buf,* Copy an instruction and adjust the displacement if the instruction * uses the %rip-relative addressing mode. */ -extern int __copy_instruction (u8 *dest,u8 *src,u8 *real,struct insn *insn); +extern int __copy_instruction (u8 *dest,u8 *src,u8 *real,struct insn *insn, +struct kprobe **kp);/* Generate a relative-jump/call instruction */ extern void synthesize_reljump (void *dest,void *from,void *to);
arch/x86/kernel/kprobes/core.c +3 -3
@@ -395,10 +395,10 @@ kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offse* instruction, displacement must be adjust by @real, not @dest. * This returns the length of copied instruction, or 0 if it has an error. */ -int __copy_instruction (u8 *dest,u8 *src,u8 *real,struct insn *insn) +int __copy_instruction (u8 *dest,u8 *src,u8 *real,struct insn *insn,struct kprobe **kp) {kprobe_opcode_t buf[MAX_INSN_SIZE ]; -unsigned long recovered_insn =recover_probed_instruction (buf, (unsigned long )src,NULL ); +unsigned long recovered_insn =recover_probed_instruction (buf, (unsigned long )src, kp);int ret;if (!recovered_insn || !insn)
@@ -735,7 +735,7 @@ static int arch_copy_kprobe(struct kprobe *p)int ret, len;/* Copy an instruction with recovering if other optprobe modifies it.*/ - len =__copy_instruction (buf, p->addr, p->ainsn.insn, &insn); + len =__copy_instruction (buf, p->addr, p->ainsn.insn, &insn,NULL );if (!len)return -EINVAL ;
arch/x86/kernel/kprobes/opt.c +7 -2
@@ -201,14 +201,19 @@ NOKPROBE_SYMBOL(optimized_callback);static int copy_optimized_instructions (u8 *dest,u8 *src,u8 *real) { +struct kprobe_iter iter; +unsigned long addr; +struct kprobe *kp;struct insn insn;int len =0 , ret; -while (len <JMP32_INSN_SIZE ) { - ret =__copy_instruction (dest + len, src + len, real + len, &insn); + addr = (unsigned long )src; +for_each_addr_kprobe (iter, kp, addr, (unsigned long )src +JMP32_INSN_SIZE -1 ) { + ret =__copy_instruction (dest + len, src + len, real + len, &insn, &kp);if (!ret || !can_boost (&insn, src + len))return -EINVAL ; len += ret; + addr += ret; }/* Check whether the address range is reserved */ if (ftrace_text_reserved(src, src + len - 1) ||
Signature · 1 line
2.43.0
Jiri Olsa
reviewerPATCH 13/13 samples/kprobes: Add batch probe example
In reply to Jiri OlsaAdd a sample module that registers kprobes on a batch of generated functions and calls each function to trigger the probes.
--- kernel/Makefile | 3 + kernel/kprobes_many.c | 81 ++++++++++++++++++++++++ samples/Kconfig | 8 +++ samples/kprobes/Makefile | 1 + samples/kprobes/kprobe_many_example.c | 91 +++++++++++++++++++++++++++ 5 files changed, 184 insertions(+) create mode 100644 kernel/kprobes_many.c create mode 100644 samples/kprobes/kprobe_many_example.c
kernel/Makefile +3 -0
@@ -99,6 +99,9 @@ obj-$(CONFIG_AUDITSYSCALL) += auditsc.o audit_watch.o audit_fsnotify.o audit_tre obj-$(CONFIG_GCOV_KERNEL) += gcov/ obj-$(CONFIG_KCOV) += kcov.o obj-$(CONFIG_KPROBES) += kprobes.o +ifneq ($(CONFIG_SAMPLE_KPROBES_MANY),) +obj-y += kprobes_many.o +endif obj-$(CONFIG_FAIL_FUNCTION) += fail_function.o obj-$(CONFIG_KGDB) += debug/ obj-$(CONFIG_DETECT_HUNG_TASK) += hung_task.o
kernel/kprobes_many.c new file +81 -0
81 lines not on this page · raw message
samples/Kconfig +8 -0
@@ -80,6 +80,14 @@ config SAMPLE_KRETPROBES default m depends on SAMPLE_KPROBES && KRETPROBES +config SAMPLE_KPROBES_MANY + tristate "Build batch kprobe example -- x86 loadable module only" + depends on SAMPLE_KPROBES && X86 && m + help + This builds a sample module that registers kprobes in the middle of + 50000 generated x86 kernel functions using the batch kprobe + interface. + config SAMPLE_HW_BREAKPOINT tristate "Build kernel hardware breakpoint examples -- loadable module only" depends on HAVE_HW_BREAKPOINT && m
samples/kprobes/Makefile +1 -0
@@ -4,3 +4,4 @@ obj-$(CONFIG_SAMPLE_KPROBES) += kprobe_example.o obj-$(CONFIG_SAMPLE_KRETPROBES) += kretprobe_example.o +obj-$(CONFIG_SAMPLE_KPROBES_MANY) += kprobe_many_example.o
samples/kprobes/kprobe_many_example.c new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
2.43.0
Masami Hiramatsu (Google)
maintainerRe: [PATCH 04/13] kprobes: Maintain an rbtree of registered probes
In reply to Jiri OlsaQuoted 3 lines · Jiri Olsa
Add an address-ordered rbtree for registered probes. Keep it synchronized with the hash table when probes are added, removed, or replaced by aggregator probes.
Could you tell me why don't you use hlist (get_kprobe) instead? I would NOT like 2 different lists which is basically doing the same thing.
Thanks,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH 04/13] kprobes: Maintain an rbtree of registered probes
Quoted 8 lines · Masami Hiramatsu (Google)
Quoted 3 lines · Jiri Olsa
Add an address-ordered rbtree for registered probes. Keep it synchronized with the hash table when probes are added, removed, or replaced by aggregator probes.
Could you tell me why don't you use hlist (
get_kprobe) instead? I would NOT like 2 different lists which is basically doing the same thing.
The speedup comes from finding the first relevant kprobe, then iterating over the remaining probes in address order. The hash table supports exact-address lookups, but it doesn’t provide that ordering.
I agree that maintaining both indexes is less convenient. What do you think about centralizing their updates as in the change below? untested
jirka
---
kernel/kprobes.c +33 -21
@@ -435,6 +435,30 @@ static void kprobe_tree_replace(struct kprobe *old, struct kprobe *new)RB_CLEAR_NODE (&old->rb_node); } +static void add_kprobe (struct kprobe *p) +{ +lockdep_assert_held (&kprobe_mutex); +INIT_HLIST_NODE (&p->hlist); +hlist_add_head_rcu (&p->hlist, + &kprobe_table[hash_ptr (p->addr,KPROBE_HASH_BITS )]); +kprobe_tree_insert (p); +} + +static void remove_kprobe (struct kprobe *p) +{ +lockdep_assert_held (&kprobe_mutex); +hlist_del_init_rcu (&p->hlist); +kprobe_tree_erase (p); +} + +static void replace_kprobe (struct kprobe *old,struct kprobe *new) +{ +lockdep_assert_held (&kprobe_mutex); +INIT_HLIST_NODE (&new->hlist); +hlist_replace_rcu (&old->hlist, &new->hlist); +kprobe_tree_replace (old, new); +} +struct kprobe *kprobe_iter_start (struct kprobe_iter *iter,unsigned long start,unsigned long end) {
@@ -751,8 +775,7 @@ static void do_unoptimize_kprobes(struct list_head *unopt_list,* for synchronization, these probes are reclaimed. * (reclaiming is done by do_free_cleaned_kprobes().) */ -hlist_del_init_rcu (&op->kp.hlist); -kprobe_tree_erase (&op->kp); +remove_kprobe (&op->kp); }else {/* * Keep on cooling_list until the quiescence period
@@ -831,8 +854,7 @@ static void optprobe_finalize_generation(struct optprobe_generation *gen)* Unhash it now and move to cur_gen's freeing list so * it will be reclaimed after the next quiescence period. */ -hlist_del_init_rcu (&op->kp.hlist); -kprobe_tree_erase (&op->kp); +remove_kprobe (&op->kp);list_move (&op->list, &optprobe_gens[optprobe_cur_gen].freeing_list);kick_kprobe_optimizer (); }else {
@@ -1116,8 +1138,7 @@ static void unoptimize_kprobe(struct kprobe *p, bool force)if (kprobe_unused (&op->kp)) {if (!kprobe_gone (&op->kp))arch_disarm_kprobe (&op->kp); -hlist_del_init_rcu (&op->kp.hlist); -kprobe_tree_erase (&op->kp); +remove_kprobe (&op->kp);list_move (&op->list, &cur->freeing_list); }else {list_move (&op->list, &cur->cooling_list);
@@ -1132,8 +1153,7 @@ static void unoptimize_kprobe(struct kprobe *p, bool force)*/ if (!kprobe_gone (&op->kp))arch_disarm_kprobe (&op->kp); -hlist_del_init_rcu (&op->kp.hlist); -kprobe_tree_erase (&op->kp); +remove_kprobe (&op->kp);list_move (&op->list, &cur->freeing_list);kick_kprobe_optimizer (); }
@@ -1188,8 +1208,7 @@ static void kill_optimized_kprobe(struct kprobe *p)if (!list_empty (&op->list)) {if (kprobe_unused (p)) {if (optprobe_queued_unopt (op) ||optprobe_queued_cooling (op)) { -hlist_del_init_rcu (&op->kp.hlist); -kprobe_tree_erase (&op->kp); +remove_kprobe (&op->kp);list_move (&op->list, &optprobe_gens[optprobe_cur_gen].freeing_list);kick_kprobe_optimizer ();
@@ -1709,11 +1728,9 @@ static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p) ap->post_handler = aggr_post_handler;INIT_LIST_HEAD (&ap->list); -INIT_HLIST_NODE (&ap->hlist);list_add_rcu (&p->list, &ap->list); -hlist_replace_rcu (&p->hlist, &ap->hlist); -kprobe_tree_replace (p, ap); +replace_kprobe (p, ap); }/*
@@ -2052,16 +2069,12 @@ static int __register_kprobe(struct kprobe *p)return ret; } -INIT_HLIST_NODE (&p->hlist); -hlist_add_head_rcu (&p->hlist, - &kprobe_table[hash_ptr (p->addr,KPROBE_HASH_BITS )]); -kprobe_tree_insert (p); +add_kprobe (p);if (!kprobes_all_disarmed && !kprobe_disabled (p)) { ret =arm_kprobe (p);if (ret) { -hlist_del_rcu (&p->hlist); -kprobe_tree_erase (p); +remove_kprobe (p);synchronize_rcu (); } }
@@ -2191,8 +2204,7 @@ static int __unregister_kprobe_top(struct kprobe *p)* !disarmed could be happen if the probe is under delayed * unoptimizing. */ -hlist_del_rcu (&ap->hlist); -kprobe_tree_erase (ap); +remove_kprobe (ap);return 0 ; }
Masami Hiramatsu (Google)
maintainerRe: [PATCH 04/13] kprobes: Maintain an rbtree of registered probes
Quoted 10 lines · Jiri Olsa
Quoted 8 lines · Masami Hiramatsu (Google)
Quoted 3 lines · Jiri Olsa
Add an address-ordered rbtree for registered probes. Keep it synchronized with the hash table when probes are added, removed, or replaced by aggregator probes.
Could you tell me why don't you use hlist (
get_kprobe) instead? I would NOT like 2 different lists which is basically doing the same thing.
Quoted 6 lines
The speedup comes from finding the first relevant kprobe, then iterating over the remaining probes in address order. The hash table supports exact-address lookups, but it doesn’t provide that ordering.
I agree that maintaining both indexes is less convenient. What do you think about centralizing their updates as in the change below? untested
If you can replace the hash table with other faster design, it is OK to me. But I would like to avoid this simple-adding another lookup table for the rare usage case (because of the maintenance cost, and duplicating a list makes it difficult to handle cases where discrepancies arise between the two lists.)
Can you change the size of the hashtable and measure how the performance is changed? I need more options.
Thanks,
Quoted 4 lines
jirka
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
[email protected]
AI reviewRe: [PATCH 04/13] kprobes: Maintain an rbtree of registered probes
In reply to Jiri OlsaThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] kprobes: double rb_erase() in kprobe_tree_erase() via freeing_list It also noticed 2 related…
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] kprobes: double rb_erase() in kprobe_tree_erase() via freeing_list
It also noticed 2 related pre-existing issues this patch does not introduce:
- [Critical] kprobes: missing cooling delay on forced unoptimization in unoptimize_kprobe() [1]
- [Critical] kprobes: premature optinsn slot free in kill_optimized_kprobe() [2]
--
commit 8d3f55a56b4caad208426449cba53c9ca99b1316
Author: Jiri Olsa <[email protected]>
kprobes: Maintain an rbtree of registered probes
This commit adds an address-ordered rbtree for registered kprobes and keeps it synchronized with kprobe_table across probe insertion, removal, and aggregator replacement.
Quoted 4 lines
kernel/kprobes.c +0
-0
[ ... ]
Quoted 4 lines
kernel/kprobes.c +0
-0@@ -395,6 +396,45 @@ struct kprobe *get_kprobe(void *addr) }NOKPROBE_SYMBOL (get_kprobe);
[ ... ]
Quoted 6 lines
kernel/kprobes.c +6
-0+static void kprobe_tree_erase (struct kprobe *p) +{ +lockdep_assert_held (&kprobe_mutex); +rb_erase (&p->rb_node, &kprobe_tree); +RB_CLEAR_NODE (&p->rb_node); +}
[ ... ]
Quoted 9 lines
kernel/kprobes.c +1
-0@@ -643,6 +683,7 @@ static void do_unoptimize_kprobes(struct list_head *unopt_list,* (reclaiming is done by do_free_cleaned_kprobes().) */ hlist_del_init_rcu (&op->kp.hlist); +kprobe_tree_erase (&op->kp); }else {/* * Keep on cooling_list until the quiescence period
kernel/kprobes.c static void optprobe_finalize_generation(struct optprobe_generation *gen) · line 769
Quoted 7 lines
kernel/kprobes.c +1
-0@@ -722,6 +763,7 @@ static void optprobe_finalize_generation(struct optprobe_generation *gen)* it will be reclaimed after the next quiescence period. */ hlist_del_init_rcu (&op->kp.hlist); +kprobe_tree_erase (&op->kp);list_move (&op->list, &optprobe_gens[optprobe_cur_gen].freeing_list);kick_kprobe_optimizer (); }else {
[Severity: High]
Can this regression call rb_erase() twice on &op->kp.rb_node and corrupt kprobe_tree?
When an unused optimized_kprobe is queued onto freeing_list (for example in optprobe_finalize_generation(), unoptimize_kprobe(), or kill_optimized_kprobe()), it calls hlist_del_init_rcu(&op->kp.hlist) and kprobe_tree_erase(&op->kp). When optprobe_dispatch_generation() later dispatches that generation, do_unoptimize_kprobes() walks freeing_list and calls both functions again on the same probe.
Unlike hlist_del_init_rcu(), which checks !hlist_unhashed(), kprobe_tree_erase() does not check !RB_EMPTY_NODE(&p->rb_node) before calling rb_erase(). Because the first erase leaves stale child pointers in rb_node and sets its parent to itself, the second rb_erase() links live kprobe_tree nodes to the about-to-be-freed op->kp.rb_node.
Quoted 9 lines
kernel/kprobes.c +1
-0@@ -1006,6 +1048,7 @@ static void unoptimize_kprobe(struct kprobe *p, bool force)if (!kprobe_gone (&op->kp))arch_disarm_kprobe (&op->kp);hlist_del_init_rcu (&op->kp.hlist); +kprobe_tree_erase (&op->kp);list_move (&op->list, &cur->freeing_list); }else {list_move (&op->list, &cur->cooling_list);
kernel/kprobes.c static void unoptimize_kprobe(struct kprobe *p, bool force) · line 1070
Quoted 7 lines
kernel/kprobes.c +1
-0@@ -1021,6 +1064,7 @@ static void unoptimize_kprobe(struct kprobe *p, bool force)if (!kprobe_gone (&op->kp))arch_disarm_kprobe (&op->kp);hlist_del_init_rcu (&op->kp.hlist); +kprobe_tree_erase (&op->kp);list_move (&op->list, &cur->freeing_list);kick_kprobe_optimizer (); }
[Severity: Critical]
This problem wasn't introduced by this patch, but in unoptimize_kprobe() [1], can forced unoptimization of an already-optimized probe free the probe and its detour trampoline before preempted tasks exit it?
kernel/kprobes.c:unoptimize_kprobe() {
...
if (!kprobe_optimized(p))
return;
/* Optimized kprobe case */
if (force) {
/* Forcibly update the code: this is a special case */
force_unoptimize_kprobe(op);
} else {
list_add(&op->list, &cur->unoptimizing_list);
kick_kprobe_optimizer();
}
}Once a probe finishes optimizing, KPROBE_FLAG_OPTIMIZED is set and op->list is empty. When unoptimize_kprobe(p, true) runs (such as from add_new_kprobe() or disarm_all_kprobes()), force_unoptimize_kprobe(op) patches the jump back to a breakpoint and clears KPROBE_FLAG_OPTIMIZED, leaving op->list empty instead of queueing op onto cur->cooling_list.
A subsequent unregister_kprobe() sees kprobe_disarmed(ap) evaluate to true in __unregister_kprobe_top(), unhashes and erases ap immediately, and waits only for synchronize_rcu() before freeing struct optimized_kprobe and its trampoline slot without waiting for a Tasks RCU grace period. If a task was preempted inside the detour trampoline before the jump was patched, it triggers a use-after-free when it resumes.
kernel/kprobes.c static void kill_optimized_kprobe(struct kprobe *p) · line 1126
Quoted 8 lines
kernel/kprobes.c +1
-0@@ -1076,6 +1120,7 @@ static void kill_optimized_kprobe(struct kprobe *p)if (kprobe_unused (p)) {if (optprobe_queued_unopt (op) ||optprobe_queued_cooling (op)) {hlist_del_init_rcu (&op->kp.hlist); +kprobe_tree_erase (&op->kp);list_move (&op->list, &optprobe_gens[optprobe_cur_gen].freeing_list);kick_kprobe_optimizer ();
[Severity: Critical]
This problem wasn't introduced by this patch, but in kill_optimized_kprobe() [2], can killing an aggregator probe that still has child probes free its optinsn slot while tasks are still preempted inside the trampoline?
kernel/kprobes.c:kill_optimized_kprobe() {
...
if (!list_empty(&op->list)) {
if (kprobe_unused(p)) {
...
} else {
list_del_init(&op->list);
}
}
op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
if (!optprobe_queued_freeing(op) && !optprobe_queued_unopt(op) &&
!optprobe_queued_cooling(op))
arch_remove_optimized_kprobe(op);
}When kill_kprobe() is called on an aggregator probe that is queued on unoptimizing_list or cooling_list and still has child probes (!kprobe_unused(p)), the else branch runs list_del_init(&op->list). Because optprobe_queued_unopt() and optprobe_queued_cooling() check queue membership by walking those lists, detaching op->list first makes both checks return false. arch_remove_optimized_kprobe() then frees the detour buffer immediately without waiting for the pending Tasks RCU grace period.
[1] https://sashiko.dev/#/bug/linux-f9b316b8-64c0-4c88-8de2-81f70a772f7b
[2] https://sashiko.dev/#/bug/linux-cc62e94b-a020-483a-84cb-52080d1d23a0
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH 09/13] x86/kprobes: Use kprobe iterator in arch_check_optimized_kprobe
In reply to Jiri OlsaThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Medium] kprobes: sibling re-optimization…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Medium] kprobes: sibling re-optimization failure in __disarm_kprobe()
[1]
--
commit 8174a2b0aeefac5511bfa58a9428f79328aae112
Author: Jiri Olsa <[email protected]>
x86/kprobes: Use kprobe iterator in arch_check_optimized_kprobe
This commit replaces the byte-by-byte get_kprobe() lookups in arch_check_optimized_kprobe() with for_each_kprobe() over the optimized instruction range.
Quoted 11 lines
arch/x86/kernel/kprobes/opt.c +2
-1@@ -334,12 +334,13 @@ static int can_optimize(unsigned long paddr)/* Check optimized_kprobe can actually be optimized. */ int arch_check_optimized_kprobe (struct optimized_kprobe *op) { -int i; +unsigned long start = (unsigned long )op->kp.addr; +unsigned long end = start + op->optinsn.size -1 ;
arch/x86/kernel/kprobes/opt.c static int can_optimize(unsigned long paddr) · line 345
Quoted 10 lines
arch/x86/kernel/kprobes/opt.c +3
-3+struct kprobe_iter iter;struct kprobe *p; -for (i =1 ; i < op->optinsn.size; i++) { - p =get_kprobe (op->kp.addr + i); -if (p && !kprobe_disarmed (p)) +for_each_kprobe (iter, p, start +1 , end) { +if (!kprobe_disarmed (p))return -EEXIST ; }
[Severity: Medium]
This problem wasn't introduced by this patch, but can kprobe_disarmed(p) here reject sibling re-optimization during __disable_kprobe() [1]?
In __disable_kprobe(), disarm_kprobe(orig_p, true) runs before KPROBE_FLAG_DISABLED is set on orig_p->flags:
kernel/kprobes.c:__disable_kprobe() {
...
if (!kprobes_all_disarmed && !kprobe_disabled(orig_p)) {
ret = disarm_kprobe(orig_p, true);
if (ret) {
p->flags &= ~KPROBE_FLAG_DISABLED;
return ERR_PTR(ret);
}
}
orig_p->flags |= KPROBE_FLAG_DISABLED;
...
}When __disarm_kprobe() disarms a probe that overlapped the jump range of a sibling optprobe, it calls optimize_kprobe() -> arch_check_optimized_kprobe() to re-optimize that sibling. Because orig_p is still registered and lacks KPROBE_FLAG_DISABLED at that point, kprobe_disarmed(orig_p) returns false and arch_check_optimized_kprobe() fails with -EEXIST, leaving the sibling probe permanently unoptimized.
[1] https://sashiko.dev/#/bug/linux-504cacc5-65e4-4f67-bdd9-fa0c7244bee6
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
[email protected]
AI reviewRe: [PATCH 13/13] samples/kprobes: Add batch probe example
In reply to Jiri OlsaThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] samples/kprobes: missing ANNOTATE_NOCFI_SYM() on kprobe_many_call_function() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] samples/kprobes: missing ANNOTATE_NOCFI_SYM() on kprobe_many_call_function()
--
commit 0bb6f0bd88ba358a50c5bd0da72b9a9137c6b243
Author: Jiri Olsa <[email protected]>
samples/kprobes: Add batch probe example
This commit adds 50,000 generated x86 functions and a sample module that registers kprobes on them in a batch and calls each function to verify the probes.
Quoted 5 lines
samples/kprobes/kprobe_many_example.c new file +0
-0
[ ... ]
samples/kprobes/kprobe_many_example.c line 39
Quoted 9 lines
samples/kprobes/kprobe_many_example.c new file +9
-0+/* The generated assembly functions have no CFI type metadata. */ +static noinline __nocfi void +kprobe_many_call_function (char *functions,unsigned int index) +{ +void (*function)(void ); + + function = (void (*)(void ))(functions + index *KPROBE_MANY_FUNC_SIZE ); +function (); +}
[Severity: Low]
Does kprobe_many_call_function() needANNOTATE_NOCFI_SYM(kprobe_many_call_function)?
Because __nocfi omits the kCFI ud2 sequence before the indirect call, building with CONFIG_MITIGATION_RETPOLINE=y, CONFIG_CALL_PADDING=y and CONFIG_CFI=y causes objtool's validate_retpoline() to emit a "no-cfi indirect call!" warning (breaking the build when CONFIG_OBJTOOL_WERROR=y is enabled) unless the function symbol is annotated with ANNOTATE_NOCFI_SYM().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
kernel test robot
build botRe: [PATCH 13/13] samples/kprobes: Add batch probe example
In reply to Jiri Olsakernel test robot noticed the following build warnings:
Hi Jiri,
kernel test robot noticed the following build warnings:
[auto build test WARNING on trace/for-next]
[also build test WARNING on linus/master v7.4-rc2 next-20261109] [cannot apply to tip/x86/core]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jiri-Olsa/static_call-Index-code-text-reservation-lookups/20261109-204904 base: https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next patch link: https://lore.kernel.org/r/20261109195827.1091410-1-jolsa%40kernel.org patch subject: [PATCH 13/13] samples/kprobes: Add batch probe example config: x86_64-allmodconfig (https://download.01.org/0day-ci/archive/20261111/[email protected]/config) compiler: clang version 22.1.8 (https://github.com/llvm/llvm-project ca7933e47d3a3451d81e72ac174dcb5aa28b59d1) rustc: rustc 1.96.0 (ac68faa20 2026-05-25) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261111/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> samples/kprobes/kprobe_many_example.o: warning: objtool: kprobe_many_call_function+0x1f: no-cfi indirect call!
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 04/13] kprobes: Maintain an rbtree of registered probes
In reply to Masami Hiramatsu (Google)Quoted 18 lines · Masami Hiramatsu (Google)
Quoted 10 lines · Jiri Olsa
Quoted 8 lines · Masami Hiramatsu (Google)
Quoted 3 lines · Jiri Olsa
Add an address-ordered rbtree for registered probes. Keep it synchronized with the hash table when probes are added, removed, or replaced by aggregator probes.
Could you tell me why don't you use hlist (
get_kprobe) instead? I would NOT like 2 different lists which is basically doing the same thing.Quoted 6 lines
The speedup comes from finding the first relevant kprobe, then iterating over the remaining probes in address order. The hash table supports exact-address lookups, but it doesn’t provide that ordering.
I agree that maintaining both indexes is less convenient. What do you think about centralizing their updates as in the change below? untested
Quoted 8 lines
If you can replace the hash table with other faster design, it is OK to me. But I would like to avoid this simple-adding another lookup table for the rare usage case (because of the maintenance cost, and duplicating a list makes it difficult to handle cases where discrepancies arise between the two lists.)
Can you change the size of the hashtable and measure how the performance is changed? I need more options.
yep, I'll check on that
jirka
Peter Zijlstra
maintainerRe: [PATCH 01/13] static_call: Index code text reservation lookups
In reply to Jiri OlsaQuoted 7 lines · Jiri Olsa
Text reservation checks scan every static call site. Build an address-sorted index for core text sites and use binary search to find the starting point near the requested range.
Module indexing is intentionally unsupported. Module reservation checks continue to use a linear scan, avoiding per-module index allocation and teardown complexity.
So this changelog leaves me wondering why, and at what cost. Why do I want to have this extra array in my kernel and such.
Peter Zijlstra
maintainerRe: [PATCH 02/13] jump_label: Index text reservation lookups
In reply to Jiri OlsaQuoted 9 lines · Jiri Olsa
Text reservation checks scan every jump entry. Build an index sorted by code address for core entries and use binary search to find the starting point near the requested range. Keep the original table sorted by static-key address for updates.
Module reservation checks continue to use a linear scan, avoiding per-module index allocation and teardown complexity.
Signed-off-by Jiri Olsa <[email protected]>
This is the *exact* same code as the last patch; seriously? You couldn't share?
Peter Zijlstra
maintainerRe: [PATCH 01/13] static_call: Index code text reservation lookups
In reply to Jiri OlsaQuoted 62 lines · Jiri Olsa
kernel/static_call_inline.c +62
-0+static int static_call_site_index_cmp (const void *a,const void *b,const void *priv) +{ +const struct static_call_site *base = priv; +unsigned int ia = *(u32 *)a; +unsigned int ib = *(u32 *)b; +unsigned long ca = (unsigned long )static_call_addr (base + ia); +unsigned long cb = (unsigned long )static_call_addr (base + ib); + +return cmp_int (ca, cb); +} + +static struct static_call_site_index * +static_call_site_index_alloc (struct static_call_site *start, +struct static_call_site *stop) +{ +struct static_call_site_index *index; +unsigned int nr = stop - start; +unsigned int i; + +if (!nr) +return NULL ; + + index =kvmalloc_flex (*index, entries, nr,GFP_KERNEL ); +if (!index) +return NULL ; + + index->nr = nr; + index->base = start; +for (i =0 ; i < nr; i++) + index->entries[i] = i; + +sort_r (index->entries, nr,sizeof (index->entries[0 ]), + static_call_site_index_cmp,NULL , start); + +return index; +} + +static int +static_call_site_index_text_reserved (struct static_call_site_index *index, +void *start,void *end,bool init) +{ +unsigned int low =0 , high = index->nr, i; + +/* Find the first site after @start. */ +while (low < high) { +unsigned int mid = low + (high - low) /2 ; +void *addr =static_call_addr (index->base + index->entries[mid]); + +if (addr <= start) + low = mid +1 ; +else + high = mid; + } + +/* The preceding site may overlap @start. */ +if (low) + low--; + +for (i = low; i < index->nr; i++) { +struct static_call_site *site = index->base + index->entries[i]; +void *addr =static_call_addr (site); +
kernel/static_call_inline.c static int addr_conflict(struct static_call_site *site, void *start, void *end) · line 387
Quoted 10 lines
kernel/static_call_inline.c +10
-0+/* Later sites cannot overlap once this address is past @end. */ +if (addr > end) +break ; +if ((init || !static_call_is_init (site)) && +addr_conflict (site, start, end)) +return 1 ; + } + +return 0 ; +}
FWIW, we do have bsearch() as well.
Peter Zijlstra
maintainerRe: [PATCH 03/13] jump_label/x86: Avoid decoding instructions for jump entry size
In reply to Jiri OlsaQuoted 4 lines · Jiri Olsa
int
arch_jump_entry_size(struct jump_entry *entry){arch/x86/kernel/jump_label.c +1
-0+const u8 *code = (void *)jump_entry_code (entry);
arch/x86/kernel/jump_label.c line 32
Quoted 10 lines
arch/x86/kernel/jump_label.c +8
-0+if (code[0 ] ==JMP8_INSN_OPCODE || + !memcmp (code, x86_nops[JMP8_INSN_SIZE ],JMP8_INSN_SIZE )) +return JMP8_INSN_SIZE ; +if (code[0 ] ==JMP32_INSN_OPCODE || + !memcmp (code, x86_nops[JMP32_INSN_SIZE ],JMP32_INSN_SIZE )) +return JMP32_INSN_SIZE ; + +BUG (); }
Does this actually generate sane code and checks code[0] first for both values before it goes and does memcmp() ? The naive translation would suggest it does not.
Peter Zijlstra
maintainerRe: [PATCH 04/13] kprobes: Maintain an rbtree of registered probes
In reply to Jiri OlsaQuoted 14 lines · Jiri Olsa
kernel/kprobes.c +14
-0+static void kprobe_tree_insert (struct kprobe *p) +{ +struct rb_node **link = &kprobe_tree.rb_node, *parent =NULL ; +unsigned long addr = (unsigned long )p->addr; + +lockdep_assert_held (&kprobe_mutex); +while (*link) { + struct kprobe *kp = rb_entry(*link, struct kprobe, rb_node); +unsigned long kp_addr = (unsigned long )kp->addr; + + parent = *link; +if (addr < kp_addr) + link = &(*link)->rb_left; +else if (addr > kp_addr)
kernel/kprobes.c struct kprobe *get_kprobe(void *addr) · line 422
Quoted 10 lines
kernel/kprobes.c +10
-0+ link = &(*link)->rb_right; + else { +WARN_ON_ONCE (1 ); +return ; + } + } + +rb_link_node (&p->rb_node, parent, link); +rb_insert_color (&p->rb_node, &kprobe_tree); +}
You probably want to use rb_add() here.
Peter Zijlstra
maintainerRe: [PATCH 01/13] static_call: Index code text reservation lookups
In reply to Jiri OlsaQuoted 96 lines · Jiri Olsa
kernel/static_call_inline.c +75
-1@@ -15,6 +15,18 @@ extern struct static_call_site __start_static_call_sites[],extern struct static_call_tramp_key __start_static_call_tramp_key[], __stop_static_call_tramp_key[]; +struct static_call_site_index { +struct static_call_site *base; +unsigned int nr; +u32 entries[]; +}; + +/* +* Index static call sites by code address for text reservation lookups. +* The site table itself is sorted by static-call-key address. +*/ +static struct static_call_site_index *static_call_site_index; +int static_call_initialized;/* @@ -41,7 +53,7 @@ static void static_call_unlock(void)mutex_unlock (&static_call_mutex); } -static inline void *static_call_addr (struct static_call_site *site) +static inline void *static_call_addr (const struct static_call_site *site) {return (void *)((long )site->addr + (long )&site->addr); }@@ -301,6 +313,79 @@ static int addr_conflict(struct static_call_site *site, void *start, void *end)return 0 ; } +static int static_call_site_index_cmp (const void *a,const void *b,const void *priv) +{ +const struct static_call_site *base = priv; +unsigned int ia = *(u32 *)a; +unsigned int ib = *(u32 *)b; +unsigned long ca = (unsigned long )static_call_addr (base + ia); +unsigned long cb = (unsigned long )static_call_addr (base + ib); + +return cmp_int (ca, cb); +} + +static struct static_call_site_index * +static_call_site_index_alloc (struct static_call_site *start, +struct static_call_site *stop) +{ +struct static_call_site_index *index; +unsigned int nr = stop - start; +unsigned int i; + +if (!nr) +return NULL ; + + index =kvmalloc_flex (*index, entries, nr,GFP_KERNEL ); +if (!index) +return NULL ; + + index->nr = nr; + index->base = start; +for (i =0 ; i < nr; i++) + index->entries[i] = i; + +sort_r (index->entries, nr,sizeof (index->entries[0 ]), + static_call_site_index_cmp,NULL , start); + +return index; +} + +static int +static_call_site_index_text_reserved (struct static_call_site_index *index, +void *start,void *end,bool init) +{ +unsigned int low =0 , high = index->nr, i; + +/* Find the first site after @start. */ +while (low < high) { +unsigned int mid = low + (high - low) /2 ; +void *addr =static_call_addr (index->base + index->entries[mid]); + +if (addr <= start) + low = mid +1 ; +else + high = mid; + } + +/* The preceding site may overlap @start. */ +if (low) + low--; + +for (i = low; i < index->nr; i++) { +struct static_call_site *site = index->base + index->entries[i]; +void *addr =static_call_addr (site); +
kernel/static_call_inline.c static int addr_conflict(struct static_call_site *site, void *start, void *end) · line 387
Quoted 10 lines
kernel/static_call_inline.c +10
-0+/* Later sites cannot overlap once this address is past @end. */ +if (addr > end) +break ; +if ((init || !static_call_is_init (site)) && +addr_conflict (site, start, end)) +return 1 ; + } + +return 0 ; +}
Looking at this in a little more detail; I'm confused.
You're literally adding an index, so that you can find the static_call_size, while all you really need is static_call_addr().
That is, you could have just created an array of relocated site->addr and sorted that, that would safe you a ton of indirection all around.
That is:
index->entries[i] = (long)static_call_addr(start + i) - (long)&index->entries[i];
That will make your lookup take at least half the amount of loads.
Also, you really should support modules, there is no point being fast until you load a module.
Peter Zijlstra
maintainerRe: [PATCH 01/13] static_call: Index code text reservation lookups
Quoted 4 lines · Peter Zijlstra
Looking at this in a little more detail; I'm confused.
You're literally adding an index, so that you can find the
static_call_size, while all you really need isstatic_call_addr().
/size/site/ obviously :-)
Quoted 6 lines
That is, you could have just created an array of relocated
site->addrand sorted that, that would safe you a ton of indirection all around.That is:
index->entries[i] = (long)static_call_addr(start + i) - (long)&index->entries[i];
Should be:
index->entries[i] = (long)static_call_addr(start + i) - (long)index->entries;
They have to all be relative to one location in order for sorting to make sense.
Quoted 5 lines
That will make your lookup take at least half the amount of loads.
Also, you really should support modules, there is no point being fast until you load a module.
Re: [PATCH 01/13] static_call: Index code text reservation lookups
Quoted 13 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Looking at this in a little more detail; I'm confused.
You're literally adding an index, so that you can find the
static_call_size, while all you really need isstatic_call_addr()./size/site/ obviously :-)
Quoted 5 lines
That is, you could have just created an array of relocated
site->addrand sorted that, that would safe you a ton of indirection all around.That is:
Quoted 10 lines
Quoted 1 line
index->entries[i] = (long)static_call_addr(start + i) - (long)&index->entries[i];Should be:
index->entries[i] = (long)static_call_addr(start + i) - (long)index->entries;
They have to all be relative to one location in order for sorting to make sense.
Quoted 1 line
That will make your lookup take at least half the amount of loads.
hum, but we still need the site’s init flag for static_call_is_init
I could store site pointer directly instead of the index and see how much faster it is
Quoted 4 lines
Quoted 4 lines
Also, you really should support modules, there is no point being fast until you load a module.
I had that, but decided to remove it because my follow up changes did not support modules as well.. I will include it
jirka
Re: [PATCH 01/13] static_call: Index code text reservation lookups
In reply to Peter ZijlstraQuoted 68 lines · Peter Zijlstra
Quoted 66 lines · Jiri Olsa
kernel/static_call_inline.c +66
-0+static int static_call_site_index_cmp (const void *a,const void *b,const void *priv) +{ +const struct static_call_site *base = priv; +unsigned int ia = *(u32 *)a; +unsigned int ib = *(u32 *)b; +unsigned long ca = (unsigned long )static_call_addr (base + ia); +unsigned long cb = (unsigned long )static_call_addr (base + ib); + +return cmp_int (ca, cb); +} + +static struct static_call_site_index * +static_call_site_index_alloc (struct static_call_site *start, +struct static_call_site *stop) +{ +struct static_call_site_index *index; +unsigned int nr = stop - start; +unsigned int i; + +if (!nr) +return NULL ; + + index =kvmalloc_flex (*index, entries, nr,GFP_KERNEL ); +if (!index) +return NULL ; + + index->nr = nr; + index->base = start; +for (i =0 ; i < nr; i++) + index->entries[i] = i; + +sort_r (index->entries, nr,sizeof (index->entries[0 ]), + static_call_site_index_cmp,NULL , start); + +return index; +} + +static int +static_call_site_index_text_reserved (struct static_call_site_index *index, +void *start,void *end,bool init) +{ +unsigned int low =0 , high = index->nr, i; + +/* Find the first site after @start. */ +while (low < high) { +unsigned int mid = low + (high - low) /2 ; +void *addr =static_call_addr (index->base + index->entries[mid]); + +if (addr <= start) + low = mid +1 ; +else + high = mid; + } + +/* The preceding site may overlap @start. */ +if (low) + low--; + +for (i = low; i < index->nr; i++) { +struct static_call_site *site = index->base + index->entries[i]; +void *addr =static_call_addr (site); + +/* Later sites cannot overlap once this address is past @end. */ +if (addr > end) +break ; + if ((init || !static_call_is_init(site)) &&
Quoted 8 lines
kernel/static_call_inline.c static int addr_conflict(struct static_call_site *site, void *start, void *end) · line 387
Quoted 6 lines
kernel/static_call_inline.c +6
-0+ addr_conflict(site, start, end)) +return 1 ; + } + +return 0 ; +}FWIW, we do have
bsearch()as well.
yes, but bsearch() returns exact match, but we need to find first entry after start even when no entry matches it... I'm not sure how to do that with bsearch
jirka
Re: [PATCH 01/13] static_call: Index code text reservation lookups
In reply to Peter ZijlstraQuoted 11 lines · Peter Zijlstra
Quoted 7 lines · Jiri Olsa
Text reservation checks scan every static call site. Build an address-sorted index for core text sites and use binary search to find the starting point near the requested range.
Module indexing is intentionally unsupported. Module reservation checks continue to use a linear scan, avoiding per-module index allocation and teardown complexity.
So this changelog leaves me wondering why, and at what cost. Why do I want to have this extra array in my kernel and such.
right, I'll include benchmark numbers for each change
jirka
Re: [PATCH 02/13] jump_label: Index text reservation lookups
In reply to Peter ZijlstraQuoted 6 lines · Peter Zijlstra
Quoted 5 lines · Jiri Olsa
Text reservation checks scan every jump entry. Build an index sorted by code address for core entries and use binary search to find the starting point near the requested range. Keep the original table sorted by static-key address for updates.
Quoted 7 lines
Quoted 4 lines
Module reservation checks continue to use a linear scan, avoiding per-module index allocation and teardown complexity.
Signed-off-by Jiri Olsa <[email protected]>This is the *exact* same code as the last patch; seriously? You couldn't share?
I think we could share the search for the lower bound, but then the overlap check is different
perhaps if we keep the index generic we could share the allocation and setup, I'll check
jirka
Re: [PATCH 03/13] jump_label/x86: Avoid decoding instructions for jump entry size
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
Quoted 8 lines · Jiri Olsa
int
arch_jump_entry_size(struct jump_entry *entry){arch/x86/kernel/jump_label.c +4
-0+const u8 *code = (void *)jump_entry_code (entry); +if (code[0 ] ==JMP8_INSN_OPCODE || + !memcmp (code, x86_nops[JMP8_INSN_SIZE ],JMP8_INSN_SIZE )) +return JMP8_INSN_SIZE ;
Quoted 11 lines
arch/x86/kernel/jump_label.c line 32
Quoted 6 lines
arch/x86/kernel/jump_label.c +5
-0+if (code[0 ] ==JMP32_INSN_OPCODE || + !memcmp (code, x86_nops[JMP32_INSN_SIZE ],JMP32_INSN_SIZE )) +return JMP32_INSN_SIZE ; + +BUG (); }Does this actually generate sane code and checks code[0] first for both values before it goes and does
memcmp()? The naive translation would suggest it does not.
nope.. you're right, I'll make it explicit
thanks,
jirka