Skip to content

Commit f2aa7e5

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
ftrace,bpf: Remove FTRACE_OPS_FL_JMP ftrace_ops flag
At the moment the we allow the jmp attach only for ftrace_ops that has FTRACE_OPS_FL_JMP set. This conflicts with following changes where we use single ftrace_ops object for all direct call sites, so all could be be attached via just call or jmp. We already limit the jmp attach support with config option and bit (LSB) set on the trampoline address. It turns out that's actually enough to limit the jmp attach for architecture and only for chosen addresses (with LSB bit set). Each user of register_ftrace_direct or modify_ftrace_direct can set the trampoline bit (LSB) to indicate it has to be attached by jmp. The bpf trampoline generation code uses trampoline flags to generate jmp-attach specific code and ftrace inner code uses the trampoline bit (LSB) to handle return from jmp attachment, so there's no harm to remove the FTRACE_OPS_FL_JMP bit. The fexit/fmodret performance stays the same (did not drop), current code: fentry : 77.904 ± 0.546M/s fexit : 62.430 ± 0.554M/s fmodret : 66.503 ± 0.902M/s with this change: fentry : 80.472 ± 0.061M/s fexit : 63.995 ± 0.127M/s fmodret : 67.362 ± 0.175M/s Fixes: 25e4e35 ("ftrace: Introduce FTRACE_OPS_FL_JMP") Signed-off-by: Jiri Olsa <[email protected]>
1 parent 037d2fc commit f2aa7e5

File tree

3 files changed

+14
-33
lines changed

3 files changed

+14
-33
lines changed

include/linux/ftrace.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ enum {
359359
FTRACE_OPS_FL_DIRECT = BIT(17),
360360
FTRACE_OPS_FL_SUBOP = BIT(18),
361361
FTRACE_OPS_FL_GRAPH = BIT(19),
362-
FTRACE_OPS_FL_JMP = BIT(20),
363362
};
364363

365364
#ifndef CONFIG_DYNAMIC_FTRACE_WITH_ARGS

kernel/bpf/trampoline.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,15 @@ static int modify_fentry(struct bpf_trampoline *tr, u32 orig_flags,
214214
int ret;
215215

216216
if (tr->func.ftrace_managed) {
217+
unsigned long addr = (unsigned long) new_addr;
218+
219+
if (bpf_trampoline_use_jmp(tr->flags))
220+
addr = ftrace_jmp_set(addr);
221+
217222
if (lock_direct_mutex)
218-
ret = modify_ftrace_direct(tr->fops, (long)new_addr);
223+
ret = modify_ftrace_direct(tr->fops, addr);
219224
else
220-
ret = modify_ftrace_direct_nolock(tr->fops, (long)new_addr);
225+
ret = modify_ftrace_direct_nolock(tr->fops, addr);
221226
} else {
222227
ret = bpf_trampoline_update_fentry(tr, orig_flags, old_addr,
223228
new_addr);
@@ -240,10 +245,15 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
240245
}
241246

242247
if (tr->func.ftrace_managed) {
248+
unsigned long addr = (unsigned long) new_addr;
249+
250+
if (bpf_trampoline_use_jmp(tr->flags))
251+
addr = ftrace_jmp_set(addr);
252+
243253
ret = ftrace_set_filter_ip(tr->fops, (unsigned long)ip, 0, 1);
244254
if (ret)
245255
return ret;
246-
ret = register_ftrace_direct(tr->fops, (long)new_addr);
256+
ret = register_ftrace_direct(tr->fops, addr);
247257
} else {
248258
ret = bpf_trampoline_update_fentry(tr, 0, NULL, new_addr);
249259
}
@@ -499,13 +509,6 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
499509
if (err)
500510
goto out_free;
501511

502-
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_JMP
503-
if (bpf_trampoline_use_jmp(tr->flags))
504-
tr->fops->flags |= FTRACE_OPS_FL_JMP;
505-
else
506-
tr->fops->flags &= ~FTRACE_OPS_FL_JMP;
507-
#endif
508-
509512
WARN_ON(tr->cur_image && total == 0);
510513
if (tr->cur_image)
511514
/* progs already running at this address */
@@ -533,15 +536,8 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
533536
tr->cur_image = im;
534537
out:
535538
/* If any error happens, restore previous flags */
536-
if (err) {
539+
if (err)
537540
tr->flags = orig_flags;
538-
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_JMP
539-
if (bpf_trampoline_use_jmp(tr->flags))
540-
tr->fops->flags |= FTRACE_OPS_FL_JMP;
541-
else
542-
tr->fops->flags &= ~FTRACE_OPS_FL_JMP;
543-
#endif
544-
}
545541
kfree(tlinks);
546542
return err;
547543

kernel/trace/ftrace.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6017,15 +6017,8 @@ int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
60176017
if (ftrace_hash_empty(hash))
60186018
return -EINVAL;
60196019

6020-
/* This is a "raw" address, and this should never happen. */
6021-
if (WARN_ON_ONCE(ftrace_is_jmp(addr)))
6022-
return -EINVAL;
6023-
60246020
mutex_lock(&direct_mutex);
60256021

6026-
if (ops->flags & FTRACE_OPS_FL_JMP)
6027-
addr = ftrace_jmp_set(addr);
6028-
60296022
/* Make sure requested entries are not already registered.. */
60306023
size = 1 << hash->size_bits;
60316024
for (i = 0; i < size; i++) {
@@ -6146,13 +6139,6 @@ __modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
61466139

61476140
lockdep_assert_held_once(&direct_mutex);
61486141

6149-
/* This is a "raw" address, and this should never happen. */
6150-
if (WARN_ON_ONCE(ftrace_is_jmp(addr)))
6151-
return -EINVAL;
6152-
6153-
if (ops->flags & FTRACE_OPS_FL_JMP)
6154-
addr = ftrace_jmp_set(addr);
6155-
61566142
/* Enable the tmp_ops to have the same functions as the direct ops */
61576143
ftrace_ops_init(&tmp_ops);
61586144
tmp_ops.func_hash = ops->func_hash;

0 commit comments

Comments
 (0)