1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
5 #include <linux/anon_inodes.h>
6 #include <linux/filter.h>
8 #include <linux/rcupdate_trace.h>
10 struct bpf_iter_target_info {
11 struct list_head list;
12 const struct bpf_iter_reg *reg_info;
13 u32 btf_id; /* cached value */
16 struct bpf_iter_link {
18 struct bpf_iter_aux_info aux;
19 struct bpf_iter_target_info *tinfo;
22 struct bpf_iter_priv_data {
23 struct bpf_iter_target_info *tinfo;
24 const struct bpf_iter_seq_info *seq_info;
25 struct bpf_prog *prog;
29 u8 target_private[] __aligned(8);
32 static struct list_head targets = LIST_HEAD_INIT(targets);
33 static DEFINE_MUTEX(targets_mutex);
35 /* protect bpf_iter_link changes */
36 static DEFINE_MUTEX(link_mutex);
38 /* incremented on every opened seq_file */
39 static atomic64_t session_id;
41 static int prepare_seq_file(struct file *file, struct bpf_iter_link *link,
42 const struct bpf_iter_seq_info *seq_info);
44 static void bpf_iter_inc_seq_num(struct seq_file *seq)
46 struct bpf_iter_priv_data *iter_priv;
48 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
53 static void bpf_iter_dec_seq_num(struct seq_file *seq)
55 struct bpf_iter_priv_data *iter_priv;
57 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
62 static void bpf_iter_done_stop(struct seq_file *seq)
64 struct bpf_iter_priv_data *iter_priv;
66 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
68 iter_priv->done_stop = true;
71 static inline bool bpf_iter_target_support_resched(const struct bpf_iter_target_info *tinfo)
73 return tinfo->reg_info->feature & BPF_ITER_RESCHED;
76 static bool bpf_iter_support_resched(struct seq_file *seq)
78 struct bpf_iter_priv_data *iter_priv;
80 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
82 return bpf_iter_target_support_resched(iter_priv->tinfo);
85 /* maximum visited objects before bailing out */
86 #define MAX_ITER_OBJECTS 1000000
88 /* bpf_seq_read, a customized and simpler version for bpf iterator.
89 * The following are differences from seq_read():
90 * . fixed buffer size (PAGE_SIZE)
91 * . assuming NULL ->llseek()
92 * . stop() may call bpf program, handling potential overflow there
94 static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,
97 struct seq_file *seq = file->private_data;
98 size_t n, offs, copied = 0;
99 int err = 0, num_objs = 0;
103 mutex_lock(&seq->lock);
106 seq->size = PAGE_SIZE << 3;
107 seq->buf = kvmalloc(seq->size, GFP_KERNEL);
115 n = min(seq->count, size);
116 err = copy_to_user(buf, seq->buf + seq->from, n);
128 p = seq->op->start(seq, &seq->index);
133 seq->op->stop(seq, p);
138 err = seq->op->show(seq, p);
140 /* object is skipped, decrease seq_num, so next
141 * valid object can reuse the same seq_num.
143 bpf_iter_dec_seq_num(seq);
145 } else if (err < 0 || seq_has_overflowed(seq)) {
148 seq->op->stop(seq, p);
153 can_resched = bpf_iter_support_resched(seq);
155 loff_t pos = seq->index;
159 p = seq->op->next(seq, p, &seq->index);
160 if (pos == seq->index) {
161 pr_info_ratelimited("buggy seq_file .next function %ps "
162 "did not updated position index\n",
167 if (IS_ERR_OR_NULL(p))
170 /* got a valid next object, increase seq_num */
171 bpf_iter_inc_seq_num(seq);
173 if (seq->count >= size)
176 if (num_objs >= MAX_ITER_OBJECTS) {
179 seq->op->stop(seq, p);
185 err = seq->op->show(seq, p);
187 bpf_iter_dec_seq_num(seq);
189 } else if (err < 0 || seq_has_overflowed(seq)) {
194 seq->op->stop(seq, p);
206 seq->op->stop(seq, NULL);
210 /* bpf program called if !p */
211 seq->op->stop(seq, p);
213 if (!seq_has_overflowed(seq)) {
214 bpf_iter_done_stop(seq);
224 n = min(seq->count, size);
225 err = copy_to_user(buf, seq->buf, n);
238 mutex_unlock(&seq->lock);
242 static const struct bpf_iter_seq_info *
243 __get_seq_info(struct bpf_iter_link *link)
245 const struct bpf_iter_seq_info *seq_info;
248 seq_info = link->aux.map->ops->iter_seq_info;
253 return link->tinfo->reg_info->seq_info;
256 static int iter_open(struct inode *inode, struct file *file)
258 struct bpf_iter_link *link = inode->i_private;
260 return prepare_seq_file(file, link, __get_seq_info(link));
263 static int iter_release(struct inode *inode, struct file *file)
265 struct bpf_iter_priv_data *iter_priv;
266 struct seq_file *seq;
268 seq = file->private_data;
272 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
275 if (iter_priv->seq_info->fini_seq_private)
276 iter_priv->seq_info->fini_seq_private(seq->private);
278 bpf_prog_put(iter_priv->prog);
279 seq->private = iter_priv;
281 return seq_release_private(inode, file);
284 const struct file_operations bpf_iter_fops = {
287 .read = bpf_seq_read,
288 .release = iter_release,
291 /* The argument reg_info will be cached in bpf_iter_target_info.
292 * The common practice is to declare target reg_info as
293 * a const static variable and passed as an argument to
294 * bpf_iter_reg_target().
296 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info)
298 struct bpf_iter_target_info *tinfo;
300 tinfo = kzalloc(sizeof(*tinfo), GFP_KERNEL);
304 tinfo->reg_info = reg_info;
305 INIT_LIST_HEAD(&tinfo->list);
307 mutex_lock(&targets_mutex);
308 list_add(&tinfo->list, &targets);
309 mutex_unlock(&targets_mutex);
314 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info)
316 struct bpf_iter_target_info *tinfo;
319 mutex_lock(&targets_mutex);
320 list_for_each_entry(tinfo, &targets, list) {
321 if (reg_info == tinfo->reg_info) {
322 list_del(&tinfo->list);
328 mutex_unlock(&targets_mutex);
330 WARN_ON(found == false);
333 static void cache_btf_id(struct bpf_iter_target_info *tinfo,
334 struct bpf_prog *prog)
336 tinfo->btf_id = prog->aux->attach_btf_id;
339 bool bpf_iter_prog_supported(struct bpf_prog *prog)
341 const char *attach_fname = prog->aux->attach_func_name;
342 struct bpf_iter_target_info *tinfo = NULL, *iter;
343 u32 prog_btf_id = prog->aux->attach_btf_id;
344 const char *prefix = BPF_ITER_FUNC_PREFIX;
345 int prefix_len = strlen(prefix);
347 if (strncmp(attach_fname, prefix, prefix_len))
350 mutex_lock(&targets_mutex);
351 list_for_each_entry(iter, &targets, list) {
352 if (iter->btf_id && iter->btf_id == prog_btf_id) {
356 if (!strcmp(attach_fname + prefix_len, iter->reg_info->target)) {
357 cache_btf_id(iter, prog);
362 mutex_unlock(&targets_mutex);
365 prog->aux->ctx_arg_info_size = tinfo->reg_info->ctx_arg_info_size;
366 prog->aux->ctx_arg_info = tinfo->reg_info->ctx_arg_info;
369 return tinfo != NULL;
372 const struct bpf_func_proto *
373 bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
375 const struct bpf_iter_target_info *tinfo;
376 const struct bpf_func_proto *fn = NULL;
378 mutex_lock(&targets_mutex);
379 list_for_each_entry(tinfo, &targets, list) {
380 if (tinfo->btf_id == prog->aux->attach_btf_id) {
381 const struct bpf_iter_reg *reg_info;
383 reg_info = tinfo->reg_info;
384 if (reg_info->get_func_proto)
385 fn = reg_info->get_func_proto(func_id, prog);
389 mutex_unlock(&targets_mutex);
394 static void bpf_iter_link_release(struct bpf_link *link)
396 struct bpf_iter_link *iter_link =
397 container_of(link, struct bpf_iter_link, link);
399 if (iter_link->tinfo->reg_info->detach_target)
400 iter_link->tinfo->reg_info->detach_target(&iter_link->aux);
403 static void bpf_iter_link_dealloc(struct bpf_link *link)
405 struct bpf_iter_link *iter_link =
406 container_of(link, struct bpf_iter_link, link);
411 static int bpf_iter_link_replace(struct bpf_link *link,
412 struct bpf_prog *new_prog,
413 struct bpf_prog *old_prog)
417 mutex_lock(&link_mutex);
418 if (old_prog && link->prog != old_prog) {
423 if (link->prog->type != new_prog->type ||
424 link->prog->expected_attach_type != new_prog->expected_attach_type ||
425 link->prog->aux->attach_btf_id != new_prog->aux->attach_btf_id) {
430 old_prog = xchg(&link->prog, new_prog);
431 bpf_prog_put(old_prog);
434 mutex_unlock(&link_mutex);
438 static void bpf_iter_link_show_fdinfo(const struct bpf_link *link,
439 struct seq_file *seq)
441 struct bpf_iter_link *iter_link =
442 container_of(link, struct bpf_iter_link, link);
443 bpf_iter_show_fdinfo_t show_fdinfo;
446 "target_name:\t%s\n",
447 iter_link->tinfo->reg_info->target);
449 show_fdinfo = iter_link->tinfo->reg_info->show_fdinfo;
451 show_fdinfo(&iter_link->aux, seq);
454 static int bpf_iter_link_fill_link_info(const struct bpf_link *link,
455 struct bpf_link_info *info)
457 struct bpf_iter_link *iter_link =
458 container_of(link, struct bpf_iter_link, link);
459 char __user *ubuf = u64_to_user_ptr(info->iter.target_name);
460 bpf_iter_fill_link_info_t fill_link_info;
461 u32 ulen = info->iter.target_name_len;
462 const char *target_name;
468 target_name = iter_link->tinfo->reg_info->target;
469 target_len = strlen(target_name);
470 info->iter.target_name_len = target_len + 1;
473 if (ulen >= target_len + 1) {
474 if (copy_to_user(ubuf, target_name, target_len + 1))
479 if (copy_to_user(ubuf, target_name, ulen - 1))
481 if (put_user(zero, ubuf + ulen - 1))
487 fill_link_info = iter_link->tinfo->reg_info->fill_link_info;
489 return fill_link_info(&iter_link->aux, info);
494 static const struct bpf_link_ops bpf_iter_link_lops = {
495 .release = bpf_iter_link_release,
496 .dealloc = bpf_iter_link_dealloc,
497 .update_prog = bpf_iter_link_replace,
498 .show_fdinfo = bpf_iter_link_show_fdinfo,
499 .fill_link_info = bpf_iter_link_fill_link_info,
502 bool bpf_link_is_iter(struct bpf_link *link)
504 return link->ops == &bpf_iter_link_lops;
507 int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
508 struct bpf_prog *prog)
510 struct bpf_iter_target_info *tinfo = NULL, *iter;
511 struct bpf_link_primer link_primer;
512 union bpf_iter_link_info linfo;
513 struct bpf_iter_link *link;
514 u32 prog_btf_id, linfo_len;
518 if (attr->link_create.target_fd || attr->link_create.flags)
521 memset(&linfo, 0, sizeof(union bpf_iter_link_info));
523 ulinfo = make_bpfptr(attr->link_create.iter_info, uattr.is_kernel);
524 linfo_len = attr->link_create.iter_info_len;
525 if (bpfptr_is_null(ulinfo) ^ !linfo_len)
528 if (!bpfptr_is_null(ulinfo)) {
529 err = bpf_check_uarg_tail_zero(ulinfo, sizeof(linfo),
533 linfo_len = min_t(u32, linfo_len, sizeof(linfo));
534 if (copy_from_bpfptr(&linfo, ulinfo, linfo_len))
538 prog_btf_id = prog->aux->attach_btf_id;
539 mutex_lock(&targets_mutex);
540 list_for_each_entry(iter, &targets, list) {
541 if (iter->btf_id == prog_btf_id) {
546 mutex_unlock(&targets_mutex);
550 /* Only allow sleepable program for resched-able iterator */
551 if (prog->aux->sleepable && !bpf_iter_target_support_resched(tinfo))
554 link = kzalloc(sizeof(*link), GFP_USER | __GFP_NOWARN);
558 bpf_link_init(&link->link, BPF_LINK_TYPE_ITER, &bpf_iter_link_lops, prog);
561 err = bpf_link_prime(&link->link, &link_primer);
567 if (tinfo->reg_info->attach_target) {
568 err = tinfo->reg_info->attach_target(prog, &linfo, &link->aux);
570 bpf_link_cleanup(&link_primer);
575 return bpf_link_settle(&link_primer);
578 static void init_seq_meta(struct bpf_iter_priv_data *priv_data,
579 struct bpf_iter_target_info *tinfo,
580 const struct bpf_iter_seq_info *seq_info,
581 struct bpf_prog *prog)
583 priv_data->tinfo = tinfo;
584 priv_data->seq_info = seq_info;
585 priv_data->prog = prog;
586 priv_data->session_id = atomic64_inc_return(&session_id);
587 priv_data->seq_num = 0;
588 priv_data->done_stop = false;
591 static int prepare_seq_file(struct file *file, struct bpf_iter_link *link,
592 const struct bpf_iter_seq_info *seq_info)
594 struct bpf_iter_priv_data *priv_data;
595 struct bpf_iter_target_info *tinfo;
596 struct bpf_prog *prog;
597 u32 total_priv_dsize;
598 struct seq_file *seq;
601 mutex_lock(&link_mutex);
602 prog = link->link.prog;
604 mutex_unlock(&link_mutex);
607 total_priv_dsize = offsetof(struct bpf_iter_priv_data, target_private) +
608 seq_info->seq_priv_size;
609 priv_data = __seq_open_private(file, seq_info->seq_ops,
616 if (seq_info->init_seq_private) {
617 err = seq_info->init_seq_private(priv_data->target_private, &link->aux);
619 goto release_seq_file;
622 init_seq_meta(priv_data, tinfo, seq_info, prog);
623 seq = file->private_data;
624 seq->private = priv_data->target_private;
629 seq_release_private(file->f_inode, file);
630 file->private_data = NULL;
636 int bpf_iter_new_fd(struct bpf_link *link)
638 struct bpf_iter_link *iter_link;
643 if (link->ops != &bpf_iter_link_lops)
646 flags = O_RDONLY | O_CLOEXEC;
647 fd = get_unused_fd_flags(flags);
651 file = anon_inode_getfile("bpf_iter", &bpf_iter_fops, NULL, flags);
657 iter_link = container_of(link, struct bpf_iter_link, link);
658 err = prepare_seq_file(file, iter_link, __get_seq_info(iter_link));
662 fd_install(fd, file);
672 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop)
674 struct bpf_iter_priv_data *iter_priv;
675 struct seq_file *seq;
679 if (seq->file->f_op != &bpf_iter_fops)
682 seq_priv = seq->private;
683 iter_priv = container_of(seq_priv, struct bpf_iter_priv_data,
686 if (in_stop && iter_priv->done_stop)
689 meta->session_id = iter_priv->session_id;
690 meta->seq_num = iter_priv->seq_num;
692 return iter_priv->prog;
695 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx)
697 struct bpf_run_ctx run_ctx, *old_run_ctx;
700 if (prog->aux->sleepable) {
701 rcu_read_lock_trace();
704 old_run_ctx = bpf_set_run_ctx(&run_ctx);
705 ret = bpf_prog_run(prog, ctx);
706 bpf_reset_run_ctx(old_run_ctx);
708 rcu_read_unlock_trace();
712 old_run_ctx = bpf_set_run_ctx(&run_ctx);
713 ret = bpf_prog_run(prog, ctx);
714 bpf_reset_run_ctx(old_run_ctx);
719 /* bpf program can only return 0 or 1:
721 * 1 : retry the same object
722 * The bpf_iter_run_prog() return value
723 * will be seq_ops->show() return value.
725 return ret == 0 ? 0 : -EAGAIN;
728 BPF_CALL_4(bpf_for_each_map_elem, struct bpf_map *, map, void *, callback_fn,
729 void *, callback_ctx, u64, flags)
731 return map->ops->map_for_each_callback(map, callback_fn, callback_ctx, flags);
734 const struct bpf_func_proto bpf_for_each_map_elem_proto = {
735 .func = bpf_for_each_map_elem,
737 .ret_type = RET_INTEGER,
738 .arg1_type = ARG_CONST_MAP_PTR,
739 .arg2_type = ARG_PTR_TO_FUNC,
740 .arg3_type = ARG_PTR_TO_STACK_OR_NULL,
741 .arg4_type = ARG_ANYTHING,
744 BPF_CALL_4(bpf_loop, u32, nr_loops, void *, callback_fn, void *, callback_ctx,
747 bpf_callback_t callback = (bpf_callback_t)callback_fn;
751 /* Note: these safety checks are also verified when bpf_loop
752 * is inlined, be careful to modify this code in sync. See
753 * function verifier.c:inline_bpf_loop.
757 if (nr_loops > BPF_MAX_LOOPS)
760 for (i = 0; i < nr_loops; i++) {
761 ret = callback((u64)i, (u64)(long)callback_ctx, 0, 0, 0);
762 /* return value: 0 - continue, 1 - stop and return */
770 const struct bpf_func_proto bpf_loop_proto = {
773 .ret_type = RET_INTEGER,
774 .arg1_type = ARG_ANYTHING,
775 .arg2_type = ARG_PTR_TO_FUNC,
776 .arg3_type = ARG_PTR_TO_STACK_OR_NULL,
777 .arg4_type = ARG_ANYTHING,