Skip to content

Commit d56b63c

Browse files
Benjamin TissoiresAlexei Starovoitov
authored andcommitted
bpf: add support for bpf_wq user type
Mostly a copy/paste from the bpf_timer API, without the initialization and free, as they will be done in a separate patch. Signed-off-by: Benjamin Tissoires <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent fc22d94 commit d56b63c

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

include/linux/bpf.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ struct bpf_map_ops {
185185

186186
enum {
187187
/* Support at most 10 fields in a BTF type */
188-
BTF_FIELDS_MAX = 10,
188+
BTF_FIELDS_MAX = 11,
189189
};
190190

191191
enum btf_field_type {
@@ -202,6 +202,7 @@ enum btf_field_type {
202202
BPF_GRAPH_NODE = BPF_RB_NODE | BPF_LIST_NODE,
203203
BPF_GRAPH_ROOT = BPF_RB_ROOT | BPF_LIST_HEAD,
204204
BPF_REFCOUNT = (1 << 9),
205+
BPF_WORKQUEUE = (1 << 10),
205206
};
206207

207208
typedef void (*btf_dtor_kfunc_t)(void *);
@@ -238,6 +239,7 @@ struct btf_record {
238239
u32 field_mask;
239240
int spin_lock_off;
240241
int timer_off;
242+
int wq_off;
241243
int refcount_off;
242244
struct btf_field fields[];
243245
};
@@ -312,6 +314,8 @@ static inline const char *btf_field_type_name(enum btf_field_type type)
312314
return "bpf_spin_lock";
313315
case BPF_TIMER:
314316
return "bpf_timer";
317+
case BPF_WORKQUEUE:
318+
return "bpf_wq";
315319
case BPF_KPTR_UNREF:
316320
case BPF_KPTR_REF:
317321
return "kptr";
@@ -340,6 +344,8 @@ static inline u32 btf_field_type_size(enum btf_field_type type)
340344
return sizeof(struct bpf_spin_lock);
341345
case BPF_TIMER:
342346
return sizeof(struct bpf_timer);
347+
case BPF_WORKQUEUE:
348+
return sizeof(struct bpf_wq);
343349
case BPF_KPTR_UNREF:
344350
case BPF_KPTR_REF:
345351
case BPF_KPTR_PERCPU:
@@ -367,6 +373,8 @@ static inline u32 btf_field_type_align(enum btf_field_type type)
367373
return __alignof__(struct bpf_spin_lock);
368374
case BPF_TIMER:
369375
return __alignof__(struct bpf_timer);
376+
case BPF_WORKQUEUE:
377+
return __alignof__(struct bpf_wq);
370378
case BPF_KPTR_UNREF:
371379
case BPF_KPTR_REF:
372380
case BPF_KPTR_PERCPU:
@@ -406,6 +414,7 @@ static inline void bpf_obj_init_field(const struct btf_field *field, void *addr)
406414
/* RB_ROOT_CACHED 0-inits, no need to do anything after memset */
407415
case BPF_SPIN_LOCK:
408416
case BPF_TIMER:
417+
case BPF_WORKQUEUE:
409418
case BPF_KPTR_UNREF:
410419
case BPF_KPTR_REF:
411420
case BPF_KPTR_PERCPU:

include/uapi/linux/bpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7306,6 +7306,10 @@ struct bpf_timer {
73067306
__u64 __opaque[2];
73077307
} __attribute__((aligned(8)));
73087308

7309+
struct bpf_wq {
7310+
__u64 __opaque[2];
7311+
} __attribute__((aligned(8)));
7312+
73097313
struct bpf_dynptr {
73107314
__u64 __opaque[2];
73117315
} __attribute__((aligned(8)));

kernel/bpf/btf.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,6 +3464,15 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask,
34643464
goto end;
34653465
}
34663466
}
3467+
if (field_mask & BPF_WORKQUEUE) {
3468+
if (!strcmp(name, "bpf_wq")) {
3469+
if (*seen_mask & BPF_WORKQUEUE)
3470+
return -E2BIG;
3471+
*seen_mask |= BPF_WORKQUEUE;
3472+
type = BPF_WORKQUEUE;
3473+
goto end;
3474+
}
3475+
}
34673476
field_mask_test_name(BPF_LIST_HEAD, "bpf_list_head");
34683477
field_mask_test_name(BPF_LIST_NODE, "bpf_list_node");
34693478
field_mask_test_name(BPF_RB_ROOT, "bpf_rb_root");
@@ -3515,6 +3524,7 @@ static int btf_find_struct_field(const struct btf *btf,
35153524
switch (field_type) {
35163525
case BPF_SPIN_LOCK:
35173526
case BPF_TIMER:
3527+
case BPF_WORKQUEUE:
35183528
case BPF_LIST_NODE:
35193529
case BPF_RB_NODE:
35203530
case BPF_REFCOUNT:
@@ -3582,6 +3592,7 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t,
35823592
switch (field_type) {
35833593
case BPF_SPIN_LOCK:
35843594
case BPF_TIMER:
3595+
case BPF_WORKQUEUE:
35853596
case BPF_LIST_NODE:
35863597
case BPF_RB_NODE:
35873598
case BPF_REFCOUNT:
@@ -3816,6 +3827,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
38163827

38173828
rec->spin_lock_off = -EINVAL;
38183829
rec->timer_off = -EINVAL;
3830+
rec->wq_off = -EINVAL;
38193831
rec->refcount_off = -EINVAL;
38203832
for (i = 0; i < cnt; i++) {
38213833
field_type_size = btf_field_type_size(info_arr[i].type);
@@ -3846,6 +3858,11 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
38463858
/* Cache offset for faster lookup at runtime */
38473859
rec->timer_off = rec->fields[i].offset;
38483860
break;
3861+
case BPF_WORKQUEUE:
3862+
WARN_ON_ONCE(rec->wq_off >= 0);
3863+
/* Cache offset for faster lookup at runtime */
3864+
rec->wq_off = rec->fields[i].offset;
3865+
break;
38493866
case BPF_REFCOUNT:
38503867
WARN_ON_ONCE(rec->refcount_off >= 0);
38513868
/* Cache offset for faster lookup at runtime */

kernel/bpf/syscall.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ void btf_record_free(struct btf_record *rec)
559559
case BPF_SPIN_LOCK:
560560
case BPF_TIMER:
561561
case BPF_REFCOUNT:
562+
case BPF_WORKQUEUE:
562563
/* Nothing to release */
563564
break;
564565
default:
@@ -608,6 +609,7 @@ struct btf_record *btf_record_dup(const struct btf_record *rec)
608609
case BPF_SPIN_LOCK:
609610
case BPF_TIMER:
610611
case BPF_REFCOUNT:
612+
case BPF_WORKQUEUE:
611613
/* Nothing to acquire */
612614
break;
613615
default:
@@ -679,6 +681,8 @@ void bpf_obj_free_fields(const struct btf_record *rec, void *obj)
679681
case BPF_TIMER:
680682
bpf_timer_cancel_and_free(field_ptr);
681683
break;
684+
case BPF_WORKQUEUE:
685+
break;
682686
case BPF_KPTR_UNREF:
683687
WRITE_ONCE(*(u64 *)field_ptr, 0);
684688
break;
@@ -1085,7 +1089,7 @@ static int map_check_btf(struct bpf_map *map, struct bpf_token *token,
10851089

10861090
map->record = btf_parse_fields(btf, value_type,
10871091
BPF_SPIN_LOCK | BPF_TIMER | BPF_KPTR | BPF_LIST_HEAD |
1088-
BPF_RB_ROOT | BPF_REFCOUNT,
1092+
BPF_RB_ROOT | BPF_REFCOUNT | BPF_WORKQUEUE,
10891093
map->value_size);
10901094
if (!IS_ERR_OR_NULL(map->record)) {
10911095
int i;

kernel/bpf/verifier.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,8 @@ static void mark_ptr_not_null_reg(struct bpf_reg_state *reg)
18381838
*/
18391839
if (btf_record_has_field(map->inner_map_meta->record, BPF_TIMER))
18401840
reg->map_uid = reg->id;
1841+
if (btf_record_has_field(map->inner_map_meta->record, BPF_WORKQUEUE))
1842+
reg->map_uid = reg->id;
18411843
} else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
18421844
reg->type = PTR_TO_XDP_SOCK;
18431845
} else if (map->map_type == BPF_MAP_TYPE_SOCKMAP ||
@@ -18141,6 +18143,13 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,
1814118143
}
1814218144
}
1814318145

18146+
if (btf_record_has_field(map->record, BPF_WORKQUEUE)) {
18147+
if (is_tracing_prog_type(prog_type)) {
18148+
verbose(env, "tracing progs cannot use bpf_wq yet\n");
18149+
return -EINVAL;
18150+
}
18151+
}
18152+
1814418153
if ((bpf_prog_is_offloaded(prog->aux) || bpf_map_is_offloaded(map)) &&
1814518154
!bpf_offload_prog_map_match(prog, map)) {
1814618155
verbose(env, "offload device mismatch between prog and map\n");

0 commit comments

Comments
 (0)