Skip to content

Commit

Permalink
kprobes: Introduce kprobe cache to reduce cache misshits
Browse files Browse the repository at this point in the history
Introduce kprobe cache to reduce cache misshits for
massive multiple kprobes.
For stress testing kprobes, we need to activate kprobes
as many as possible. This situation causes cache miss
hit storm on kprobe hash-list. kprobe hashlist is already
enlarged to 512 entries and this is still small for 40k
kprobes.

For example, when registering 40k probes on the hlist and
enabling 20k probes, perf tools shows still a lot of
cache-misses are on the get_kprobe.
  ----
  Samples: 633  of event 'cache-misses', Event count (approx.): 3414776
  +  68.13%  [k] get_kprobe
  +   4.38%  [k] ftrace_lookup_ip
  +   2.54%  [k] kprobe_ftrace_handler
  ----

Also, I found that the most of the kprobes are not hit.
In that case, to reduce cache-misses, we can reduce the
random memory access by introducing a per-cpu cache which
caches the address of frequently used kprobe data structure
and its probe address.

With kpcache enabled, the get_kprobe_cached goes down to
around 4-5% of cache-misses with 20k probes.
  ----
  Samples: 729  of event 'cache-misses', Event count (approx.): 690125
  +  14.49%  [k] ftrace_lookup_ip
  +   5.61%  [k] kprobe_trace_func
  +   5.17%  [k] kprobe_ftrace_handler
  +   4.62%  [k] get_kprobe_cached
  ----

Of course this reduces the enabling time too.

Without this fix (just enlarge hash table):
(2934 sec, 1 min intervals for each 2000 probes enabled)

  ----
  Enabling trace events: start at 1393921862
  0 1393921864 a2mp_chan_alloc_skb_cb_38581
  ...
  19999 1393924928 nfs4_open_confirm_done_11785
  ----

With this fix:
(2025 sec, 1 min intervals for each 2000 probes enabled)
  ----
  Enabling trace events: start at 1393912623
  0 1393912625 a2mp_chan_alloc_skb_cb_38800
  ....
  19999 1393914648 nfs2_xdr_dec_readlinkres_11628
  ----

This patch implements a simple per-cpu 4way/512entry cache
for kprobes hlist. All get_kprobe on hot-path uses the cache
and if the cache miss-hit, it searches kprobes on the hlist
and inserts the found kprobes to the cache entry.
When removing kprobes, it clears cache entries by using IPI,
because it is per-cpu cache.

Note that this consumes some amount of memory (34KB per cpu)
compared with previous one (4KB total) only for kprobes.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
  • Loading branch information
mhiramat committed Dec 13, 2018
1 parent f4e6760 commit 5aa3f1f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 4 deletions.
2 changes: 1 addition & 1 deletion arch/x86/kernel/kprobes/core.c
Expand Up @@ -669,7 +669,7 @@ int kprobe_int3_handler(struct pt_regs *regs)
*/

kcb = get_kprobe_ctlblk();
p = get_kprobe(addr);
p = get_kprobe_cached(addr);

if (p) {
if (kprobe_running()) {
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kernel/kprobes/ftrace.c
Expand Up @@ -33,7 +33,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
struct kprobe_ctlblk *kcb;

/* Preempt is disabled by ftrace */
p = get_kprobe((kprobe_opcode_t *)ip);
p = get_kprobe_cached((kprobe_opcode_t *)ip);
if (unlikely(!p) || kprobe_disabled(p))
return;

Expand Down
1 change: 1 addition & 0 deletions include/linux/kprobes.h
Expand Up @@ -337,6 +337,7 @@ extern int arch_prepare_kprobe_ftrace(struct kprobe *p);

int arch_check_ftrace_location(struct kprobe *p);
struct kprobe *get_kprobe(void *addr);
struct kprobe *get_kprobe_cached(void *addr);
void kretprobe_hash_lock(struct task_struct *tsk,
struct hlist_head **head, unsigned long *flags);
void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags);
Expand Down
116 changes: 114 additions & 2 deletions kernel/kprobes.c
Expand Up @@ -89,6 +89,97 @@ static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
/* Blacklist -- list of struct kprobe_blacklist_entry */
static LIST_HEAD(kprobe_blacklist);

/* Kprobe cache -- software defined 4 way set associative cache */
#define KPCACHE_BITS 2
#define KPCACHE_SIZE (1 << KPCACHE_BITS)
#define KPCACHE_INDEX(i) ((i) & (KPCACHE_SIZE - 1))

struct kprobe_cache_entry {
unsigned long addr;
struct kprobe *kp;
};

struct kprobe_cache {
struct kprobe_cache_entry table[KPROBE_TABLE_SIZE][KPCACHE_SIZE];
int index[KPROBE_TABLE_SIZE];
};

static DEFINE_PER_CPU(struct kprobe_cache, kpcache);

static inline
struct kprobe *kpcache_get(unsigned long hash, unsigned long addr)
{
struct kprobe_cache *cache = this_cpu_ptr(&kpcache);
struct kprobe_cache_entry *ent = &cache->table[hash][0];
struct kprobe *ret = NULL;
int i, idx;

retry:
idx = READ_ONCE(cache->index[hash]);
for (i = 0; i < KPCACHE_SIZE; i++)
if (ent[i].addr == addr) {
/* Save the current value */
ret = READ_ONCE(ent[i].kp);
break;
}
barrier();
/* Retry if the cache is updated */
if (unlikely(idx != READ_ONCE(cache->index[hash])))
goto retry;

return ret;
}

static inline void kpcache_set(unsigned long hash, unsigned long addr,
struct kprobe *kp)
{
struct kprobe_cache *cache = this_cpu_ptr(&kpcache);
struct kprobe_cache_entry *ent = &cache->table[hash][0];
int i = KPCACHE_INDEX(cache->index[hash]++);

/*
* Setting must be done in this order for avoiding interruption;
* (1)invalidate entry, (2)set the value, and (3)enable entry.
*/
ent[i].addr = 0;
barrier();
ent[i].kp = kp;
barrier();
ent[i].addr = addr;
}

static void kpcache_invalidate_this_cpu(void *addr)
{
unsigned long hash = hash_ptr(addr, KPROBE_HASH_BITS);
struct kprobe_cache *cache = this_cpu_ptr(&kpcache);
struct kprobe_cache_entry *ent = &cache->table[hash][0];
int i;

for (i = 0; i < KPCACHE_SIZE; i++)
if (ent[i].addr == (unsigned long)addr) {
ent[i].addr = 0;
/* Disturb the round-robin for better utilization */
i += KPCACHE_SIZE - KPCACHE_INDEX(cache->index[hash]);
cache->index[hash] += i; /* Update cache index */
}
}

/* This must be called after ensuring the kprobe is removed from hlist */
static void kpcache_invalidate(unsigned long addr)
{
on_each_cpu(kpcache_invalidate_this_cpu, (void *)addr, 1);
/*
* Here is a trick. Normally, we need another synchronize_rcu() here,
* but all kpcache users are called from arch-dependent kprobe handler
* which runs as an interrupt handler. Since kpcache invalidation is
* done in IPI handler on each processor, kprobe handlers have left
* from the cached kprobe at this point. Note that kpcache_invalidate()
* waits for finishing IPIs, all kprobes before running IPI have done,
* and the kprobes after the IPI can not access invalideted data.
* So it is already safe to release them beyond this point.
*/
}

#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
/*
* kprobe->ainsn.insn points to the copy of the instruction to be
Expand Down Expand Up @@ -340,6 +431,24 @@ struct kprobe *get_kprobe(void *addr)
{
return xa_load(&kprobe_xarray, (unsigned long)addr);
}
NOKPROBE_SYMBOL(get_kprobe);

/* This must be called under preempt disabled */
struct kprobe *get_kprobe_cached(void *addr)
{
unsigned long hash = hash_ptr(addr, KPROBE_HASH_BITS);
struct kprobe *p;

p = kpcache_get(hash, (unsigned long)addr);
if (likely(p))
return p;

p = get_kprobe(addr);
if (likely(p))
kpcache_set(hash, (unsigned long)addr, p);
return p;
}
NOKPROBE_SYMBOL(get_kprobe_cached);

static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);

Expand Down Expand Up @@ -547,6 +656,7 @@ static void do_free_cleaned_kprobes(void)
*/
continue;
}
kpcache_invalidate((unsigned long)op->kp.addr);
free_aggr_kprobe(&op->kp);
}
}
Expand Down Expand Up @@ -1711,13 +1821,15 @@ static void __unregister_kprobe_bottom(struct kprobe *p)
{
struct kprobe *ap;

if (list_empty(&p->list))
if (list_empty(&p->list)) {
/* This is an independent kprobe */
kpcache_invalidate((unsigned long)p->addr);
arch_remove_kprobe(p);
else if (list_is_singular(&p->list)) {
} else if (list_is_singular(&p->list)) {
/* This is the last child of an aggrprobe */
ap = list_entry(p->list.next, struct kprobe, list);
list_del(&p->list);
kpcache_invalidate((unsigned long)p->addr);
free_aggr_kprobe(ap);
}
/* Otherwise, do nothing. */
Expand Down

0 comments on commit 5aa3f1f

Please sign in to comment.