Skip to content

Commit 7ac88eb

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
bpf: Remove bpf_image tree
Now that we have all the objects (bpf_prog, bpf_trampoline, bpf_dispatcher) linked in bpf_tree, there's no need to have separate bpf_image tree for images. Reverting the bpf_image tree together with struct bpf_image, because it's no longer needed. Also removing bpf_image_alloc function and adding the original bpf_jit_alloc_exec_page interface instead. The kernel_text_address function can now rely only on is_bpf_text_address, because it checks the bpf_tree that contains all the objects. Keeping bpf_image_ksym_add and bpf_image_ksym_del because they are useful wrappers with perf's ksymbol interface calls. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200312195610.346362-13-jolsa@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 517b75e commit 7ac88eb

4 files changed

Lines changed: 13 additions & 84 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,8 @@ void bpf_trampoline_put(struct bpf_trampoline *tr);
583583
#define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
584584
void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
585585
struct bpf_prog *to);
586-
struct bpf_image {
587-
struct latch_tree_node tnode;
588-
unsigned char data[];
589-
};
590-
#define BPF_IMAGE_SIZE (PAGE_SIZE - sizeof(struct bpf_image))
591-
bool is_bpf_image_address(unsigned long address);
592-
void *bpf_image_alloc(void);
593586
/* Called only from JIT-enabled code, so there's no need for stubs. */
587+
void *bpf_jit_alloc_exec_page(void);
594588
void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
595589
void bpf_image_ksym_del(struct bpf_ksym *ksym);
596590
void bpf_ksym_add(struct bpf_ksym *ksym);

‎kernel/bpf/dispatcher.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static void bpf_dispatcher_update(struct bpf_dispatcher *d, int prev_num_progs)
113113
noff = 0;
114114
} else {
115115
old = d->image + d->image_off;
116-
noff = d->image_off ^ (BPF_IMAGE_SIZE / 2);
116+
noff = d->image_off ^ (PAGE_SIZE / 2);
117117
}
118118

119119
new = d->num_progs ? d->image + noff : NULL;
@@ -140,7 +140,7 @@ void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
140140

141141
mutex_lock(&d->mutex);
142142
if (!d->image) {
143-
d->image = bpf_image_alloc();
143+
d->image = bpf_jit_alloc_exec_page();
144144
if (!d->image)
145145
goto out;
146146
bpf_image_ksym_add(d->image, &d->ksym);

‎kernel/bpf/trampoline.c‎

Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ const struct bpf_prog_ops bpf_extension_prog_ops = {
1818
#define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
1919

2020
static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
21-
static struct latch_tree_root image_tree __cacheline_aligned;
2221

23-
/* serializes access to trampoline_table and image_tree */
22+
/* serializes access to trampoline_table */
2423
static DEFINE_MUTEX(trampoline_mutex);
2524

26-
static void *bpf_jit_alloc_exec_page(void)
25+
void *bpf_jit_alloc_exec_page(void)
2726
{
2827
void *image;
2928

@@ -39,78 +38,20 @@ static void *bpf_jit_alloc_exec_page(void)
3938
return image;
4039
}
4140

42-
static __always_inline bool image_tree_less(struct latch_tree_node *a,
43-
struct latch_tree_node *b)
44-
{
45-
struct bpf_image *ia = container_of(a, struct bpf_image, tnode);
46-
struct bpf_image *ib = container_of(b, struct bpf_image, tnode);
47-
48-
return ia < ib;
49-
}
50-
51-
static __always_inline int image_tree_comp(void *addr, struct latch_tree_node *n)
52-
{
53-
void *image = container_of(n, struct bpf_image, tnode);
54-
55-
if (addr < image)
56-
return -1;
57-
if (addr >= image + PAGE_SIZE)
58-
return 1;
59-
60-
return 0;
61-
}
62-
63-
static const struct latch_tree_ops image_tree_ops = {
64-
.less = image_tree_less,
65-
.comp = image_tree_comp,
66-
};
67-
68-
static void *__bpf_image_alloc(bool lock)
69-
{
70-
struct bpf_image *image;
71-
72-
image = bpf_jit_alloc_exec_page();
73-
if (!image)
74-
return NULL;
75-
76-
if (lock)
77-
mutex_lock(&trampoline_mutex);
78-
latch_tree_insert(&image->tnode, &image_tree, &image_tree_ops);
79-
if (lock)
80-
mutex_unlock(&trampoline_mutex);
81-
return image->data;
82-
}
83-
84-
void *bpf_image_alloc(void)
85-
{
86-
return __bpf_image_alloc(true);
87-
}
88-
89-
bool is_bpf_image_address(unsigned long addr)
90-
{
91-
bool ret;
92-
93-
rcu_read_lock();
94-
ret = latch_tree_find((void *) addr, &image_tree, &image_tree_ops) != NULL;
95-
rcu_read_unlock();
96-
97-
return ret;
98-
}
99-
10041
void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
10142
{
10243
ksym->start = (unsigned long) data;
103-
ksym->end = ksym->start + BPF_IMAGE_SIZE;
44+
ksym->end = ksym->start + PAGE_SIZE;
10445
bpf_ksym_add(ksym);
10546
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
106-
BPF_IMAGE_SIZE, false, ksym->name);
47+
PAGE_SIZE, false, ksym->name);
10748
}
10849

10950
void bpf_image_ksym_del(struct bpf_ksym *ksym)
11051
{
11152
bpf_ksym_del(ksym);
11253
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
113-
BPF_IMAGE_SIZE, true, ksym->name);
54+
PAGE_SIZE, true, ksym->name);
11455
}
11556

11657
static void bpf_trampoline_ksym_add(struct bpf_trampoline *tr)
@@ -141,7 +82,7 @@ struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
14182
goto out;
14283

14384
/* is_root was checked earlier. No need for bpf_jit_charge_modmem() */
144-
image = __bpf_image_alloc(false);
85+
image = bpf_jit_alloc_exec_page();
14586
if (!image) {
14687
kfree(tr);
14788
tr = NULL;
@@ -243,8 +184,8 @@ bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total)
243184

244185
static int bpf_trampoline_update(struct bpf_trampoline *tr)
245186
{
246-
void *old_image = tr->image + ((tr->selector + 1) & 1) * BPF_IMAGE_SIZE/2;
247-
void *new_image = tr->image + (tr->selector & 1) * BPF_IMAGE_SIZE/2;
187+
void *old_image = tr->image + ((tr->selector + 1) & 1) * PAGE_SIZE/2;
188+
void *new_image = tr->image + (tr->selector & 1) * PAGE_SIZE/2;
248189
struct bpf_tramp_progs *tprogs;
249190
u32 flags = BPF_TRAMP_F_RESTORE_REGS;
250191
int err, total;
@@ -272,7 +213,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr)
272213

273214
synchronize_rcu_tasks();
274215

275-
err = arch_prepare_bpf_trampoline(new_image, new_image + BPF_IMAGE_SIZE / 2,
216+
err = arch_prepare_bpf_trampoline(new_image, new_image + PAGE_SIZE / 2,
276217
&tr->func.model, flags, tprogs,
277218
tr->func.addr);
278219
if (err < 0)
@@ -383,8 +324,6 @@ int bpf_trampoline_unlink_prog(struct bpf_prog *prog)
383324

384325
void bpf_trampoline_put(struct bpf_trampoline *tr)
385326
{
386-
struct bpf_image *image;
387-
388327
if (!tr)
389328
return;
390329
mutex_lock(&trampoline_mutex);
@@ -396,11 +335,9 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
396335
if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT])))
397336
goto out;
398337
bpf_image_ksym_del(&tr->ksym);
399-
image = container_of(tr->image, struct bpf_image, data);
400-
latch_tree_erase(&image->tnode, &image_tree, &image_tree_ops);
401338
/* wait for tasks to get out of trampoline before freeing it */
402339
synchronize_rcu_tasks();
403-
bpf_jit_free_exec(image);
340+
bpf_jit_free_exec(tr->image);
404341
hlist_del(&tr->hlist);
405342
kfree(tr);
406343
out:

‎kernel/extable.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ int kernel_text_address(unsigned long addr)
149149
goto out;
150150
if (is_bpf_text_address(addr))
151151
goto out;
152-
if (is_bpf_image_address(addr))
153-
goto out;
154152
ret = 0;
155153
out:
156154
if (no_rcu)

0 commit comments

Comments
 (0)