Skip to content

Commit e8e4ccd

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
bpf, x86: Use single ftrace_ops for direct calls
Using single ftrace_ops for direct calls update instead of allocating ftrace_ops object for each trampoline. With single ftrace_ops object we can use update_ftrace_direct_* api that allows multiple ip sites updates on single ftrace_ops object. Adding HAVE_SINGLE_FTRACE_DIRECT_OPS config option to be enabled on each arch that supports this. At the moment we can enable this only on x86 arch, because arm relies on ftrace_ops object representing just single trampoline image (stored in ftrace_ops::direct_call). Ach that do not support this will continue to use *_ftrace_direct api. Signed-off-by: Jiri Olsa <[email protected]>
1 parent c3dae69 commit e8e4ccd

File tree

4 files changed

+160
-17
lines changed

4 files changed

+160
-17
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ config X86
332332
select SCHED_SMT if SMP
333333
select ARCH_SUPPORTS_SCHED_CLUSTER if SMP
334334
select ARCH_SUPPORTS_SCHED_MC if SMP
335+
select HAVE_SINGLE_FTRACE_DIRECT_OPS if X86_64 && DYNAMIC_FTRACE_WITH_DIRECT_CALLS
335336

336337
config INSTRUCTION_DECODER
337338
def_bool y

kernel/bpf/trampoline.c

Lines changed: 150 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,40 @@ static DEFINE_MUTEX(trampoline_mutex);
3333
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
3434
static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex);
3535

36+
#ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS
37+
static struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip)
38+
{
39+
struct hlist_head *head_ip;
40+
struct bpf_trampoline *tr;
41+
42+
mutex_lock(&trampoline_mutex);
43+
head_ip = &trampoline_ip_table[hash_64(ip, TRAMPOLINE_HASH_BITS)];
44+
hlist_for_each_entry(tr, head_ip, hlist_ip) {
45+
if (tr->ip == ip)
46+
goto out;
47+
}
48+
tr = NULL;
49+
out:
50+
mutex_unlock(&trampoline_mutex);
51+
return tr;
52+
}
53+
#else
54+
static struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip)
55+
{
56+
return ops->private;
57+
}
58+
#endif /* CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */
59+
3660
static int bpf_tramp_ftrace_ops_func(struct ftrace_ops *ops, unsigned long ip,
3761
enum ftrace_ops_cmd cmd)
3862
{
39-
struct bpf_trampoline *tr = ops->private;
63+
struct bpf_trampoline *tr;
4064
int ret = 0;
4165

66+
tr = direct_ops_ip_lookup(ops, ip);
67+
if (!tr)
68+
return -EINVAL;
69+
4270
if (cmd == FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF) {
4371
/* This is called inside register_ftrace_direct_multi(), so
4472
* tr->mutex is already locked.
@@ -137,6 +165,122 @@ void bpf_image_ksym_del(struct bpf_ksym *ksym)
137165
PAGE_SIZE, true, ksym->name);
138166
}
139167

168+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
169+
#ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS
170+
/*
171+
* We have only single direct_ops which contains all the direct call
172+
* sites and is the only global ftrace_ops for all trampolines.
173+
*
174+
* We use 'update_ftrace_direct_*' api for attachment.
175+
*/
176+
struct ftrace_ops direct_ops = {
177+
.ops_func = bpf_tramp_ftrace_ops_func,
178+
};
179+
180+
static int direct_ops_alloc(struct bpf_trampoline *tr)
181+
{
182+
tr->fops = &direct_ops;
183+
return 0;
184+
}
185+
186+
static void direct_ops_free(struct bpf_trampoline *tr) { }
187+
188+
static struct ftrace_hash *hash_from(unsigned long ip, void *addr)
189+
{
190+
struct ftrace_hash *hash;
191+
192+
ip = ftrace_location(ip);
193+
if (!ip)
194+
return NULL;
195+
hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
196+
if (!hash)
197+
return NULL;
198+
if (!add_hash_entry_direct(hash, ip, (unsigned long) addr)) {
199+
free_ftrace_hash(hash);
200+
return NULL;
201+
}
202+
return hash;
203+
}
204+
205+
static int direct_ops_add(struct ftrace_ops *ops, unsigned long ip, void *addr)
206+
{
207+
struct ftrace_hash *hash = hash_from(ip, addr);
208+
int err = -ENOMEM;
209+
210+
if (hash)
211+
err = update_ftrace_direct_add(ops, hash);
212+
free_ftrace_hash(hash);
213+
return err;
214+
}
215+
216+
static int direct_ops_del(struct ftrace_ops *ops, unsigned long ip, void *addr)
217+
{
218+
struct ftrace_hash *hash = hash_from(ip, addr);
219+
int err = -ENOMEM;
220+
221+
if (hash)
222+
err = update_ftrace_direct_del(ops, hash);
223+
free_ftrace_hash(hash);
224+
return err;
225+
}
226+
227+
static int direct_ops_mod(struct ftrace_ops *ops, unsigned long ip, void *addr, bool lock_direct_mutex)
228+
{
229+
struct ftrace_hash *hash = hash_from(ip, addr);
230+
int err = -ENOMEM;
231+
232+
if (hash)
233+
err = update_ftrace_direct_mod(ops, hash, lock_direct_mutex);
234+
free_ftrace_hash(hash);
235+
return err;
236+
}
237+
#else
238+
/*
239+
* We allocate ftrace_ops object for each trampoline and it contains
240+
* call site specific for that trampoline.
241+
*
242+
* We use *_ftrace_direct api for attachment.
243+
*/
244+
static int direct_ops_alloc(struct bpf_trampoline *tr)
245+
{
246+
tr->fops = kzalloc(sizeof(struct ftrace_ops), GFP_KERNEL);
247+
if (!tr->fops)
248+
return -ENOMEM;
249+
tr->fops->private = tr;
250+
tr->fops->ops_func = bpf_tramp_ftrace_ops_func;
251+
return 0;
252+
}
253+
254+
static void direct_ops_free(struct bpf_trampoline *tr)
255+
{
256+
if (tr->fops) {
257+
ftrace_free_filter(tr->fops);
258+
kfree(tr->fops);
259+
}
260+
}
261+
262+
static int direct_ops_add(struct ftrace_ops *ops, unsigned long ip, void *addr)
263+
{
264+
ftrace_set_filter_ip(ops, (unsigned long)ip, 0, 1);
265+
return register_ftrace_direct(ops, (long)addr);
266+
}
267+
268+
static int direct_ops_del(struct ftrace_ops *ops, unsigned long ip, void *addr)
269+
{
270+
return unregister_ftrace_direct(ops, (long)addr, false);
271+
}
272+
273+
static int direct_ops_mod(struct ftrace_ops *ops, unsigned long ip, void *addr, bool lock_direct_mutex)
274+
{
275+
if (lock_direct_mutex)
276+
return modify_ftrace_direct(ops, (long)addr);
277+
return modify_ftrace_direct_nolock(ops, (long)addr);
278+
}
279+
#endif /* CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */
280+
#else
281+
static void direct_ops_free(struct bpf_trampoline *tr) { }
282+
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
283+
140284
static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)
141285
{
142286
struct bpf_trampoline *tr;
@@ -155,14 +299,11 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)
155299
if (!tr)
156300
goto out;
157301
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
158-
tr->fops = kzalloc(sizeof(struct ftrace_ops), GFP_KERNEL);
159-
if (!tr->fops) {
302+
if (direct_ops_alloc(tr)) {
160303
kfree(tr);
161304
tr = NULL;
162305
goto out;
163306
}
164-
tr->fops->private = tr;
165-
tr->fops->ops_func = bpf_tramp_ftrace_ops_func;
166307
#endif
167308

168309
tr->key = key;
@@ -187,7 +328,7 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
187328
int ret;
188329

189330
if (tr->func.ftrace_managed)
190-
ret = unregister_ftrace_direct(tr->fops, (long)old_addr, false);
331+
ret = direct_ops_del(tr->fops, tr->ip, old_addr);
191332
else
192333
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
193334

@@ -201,10 +342,7 @@ static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_ad
201342
int ret;
202343

203344
if (tr->func.ftrace_managed) {
204-
if (lock_direct_mutex)
205-
ret = modify_ftrace_direct(tr->fops, (long)new_addr);
206-
else
207-
ret = modify_ftrace_direct_nolock(tr->fops, (long)new_addr);
345+
ret = direct_ops_mod(tr->fops, tr->ip, new_addr, lock_direct_mutex);
208346
} else {
209347
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr);
210348
}
@@ -226,8 +364,7 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
226364
}
227365

228366
if (tr->func.ftrace_managed) {
229-
ftrace_set_filter_ip(tr->fops, (unsigned long)ip, 0, 1);
230-
ret = register_ftrace_direct(tr->fops, (long)new_addr);
367+
ret = direct_ops_add(tr->fops, tr->ip, new_addr);
231368
} else {
232369
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
233370
}
@@ -868,10 +1005,7 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
8681005
*/
8691006
hlist_del(&tr->hlist_key);
8701007
hlist_del(&tr->hlist_ip);
871-
if (tr->fops) {
872-
ftrace_free_filter(tr->fops);
873-
kfree(tr->fops);
874-
}
1008+
direct_ops_free(tr);
8751009
kfree(tr);
8761010
out:
8771011
mutex_unlock(&trampoline_mutex);

kernel/trace/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ config HAVE_DYNAMIC_FTRACE_WITH_REGS
5050
config HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
5151
bool
5252

53+
config HAVE_SINGLE_FTRACE_DIRECT_OPS
54+
bool
55+
5356
config HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS
5457
bool
5558

kernel/trace/ftrace.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2592,8 +2592,13 @@ unsigned long ftrace_find_rec_direct(unsigned long ip)
25922592
static void call_direct_funcs(unsigned long ip, unsigned long pip,
25932593
struct ftrace_ops *ops, struct ftrace_regs *fregs)
25942594
{
2595-
unsigned long addr = READ_ONCE(ops->direct_call);
2595+
unsigned long addr;
25962596

2597+
#ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS
2598+
addr = ftrace_find_rec_direct(ip);
2599+
#else
2600+
addr = READ_ONCE(ops->direct_call);
2601+
#endif
25972602
if (!addr)
25982603
return;
25992604

0 commit comments

Comments
 (0)