blob: b7aef5b3416d5de0eb1128f4217d696e7355eb69 [file] [log] [blame]
Yonghong Songae243452020-05-09 10:58:59 -07001// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2020 Facebook */
3
4#include <linux/fs.h>
Yonghong Songac51d992020-05-09 10:59:05 -07005#include <linux/anon_inodes.h>
Yonghong Songae243452020-05-09 10:58:59 -07006#include <linux/filter.h>
7#include <linux/bpf.h>
8
9struct bpf_iter_target_info {
10 struct list_head list;
Yonghong Song15172a42020-05-13 11:02:19 -070011 const struct bpf_iter_reg *reg_info;
Yonghong Song15d83c42020-05-09 10:59:00 -070012 u32 btf_id; /* cached value */
Yonghong Songae243452020-05-09 10:58:59 -070013};
14
Yonghong Songde4e05c2020-05-09 10:59:01 -070015struct bpf_iter_link {
16 struct bpf_link link;
Yonghong Songa5cbe052020-07-23 11:41:12 -070017 struct bpf_iter_aux_info aux;
Yonghong Songde4e05c2020-05-09 10:59:01 -070018 struct bpf_iter_target_info *tinfo;
19};
20
Yonghong Songac51d992020-05-09 10:59:05 -070021struct bpf_iter_priv_data {
22 struct bpf_iter_target_info *tinfo;
Yonghong Songa5cbe052020-07-23 11:41:12 -070023 const struct bpf_iter_seq_info *seq_info;
Yonghong Songac51d992020-05-09 10:59:05 -070024 struct bpf_prog *prog;
25 u64 session_id;
26 u64 seq_num;
27 bool done_stop;
28 u8 target_private[] __aligned(8);
29};
30
Yonghong Songae243452020-05-09 10:58:59 -070031static struct list_head targets = LIST_HEAD_INIT(targets);
32static DEFINE_MUTEX(targets_mutex);
33
Yonghong Song2057c922020-05-09 10:59:02 -070034/* protect bpf_iter_link changes */
35static DEFINE_MUTEX(link_mutex);
36
Yonghong Songac51d992020-05-09 10:59:05 -070037/* incremented on every opened seq_file */
38static atomic64_t session_id;
39
Yonghong Songa5cbe052020-07-23 11:41:12 -070040static int prepare_seq_file(struct file *file, struct bpf_iter_link *link,
41 const struct bpf_iter_seq_info *seq_info);
Yonghong Song367ec3e2020-05-09 10:59:06 -070042
Yonghong Songe5158d92020-05-09 10:59:07 -070043static void bpf_iter_inc_seq_num(struct seq_file *seq)
44{
45 struct bpf_iter_priv_data *iter_priv;
46
47 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
48 target_private);
49 iter_priv->seq_num++;
50}
51
52static void bpf_iter_dec_seq_num(struct seq_file *seq)
53{
54 struct bpf_iter_priv_data *iter_priv;
55
56 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
57 target_private);
58 iter_priv->seq_num--;
59}
60
61static void bpf_iter_done_stop(struct seq_file *seq)
62{
63 struct bpf_iter_priv_data *iter_priv;
64
65 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
66 target_private);
67 iter_priv->done_stop = true;
68}
69
Yonghong Songcf83b2d2020-10-27 23:10:54 -070070static bool bpf_iter_support_resched(struct seq_file *seq)
71{
72 struct bpf_iter_priv_data *iter_priv;
73
74 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
75 target_private);
76 return iter_priv->tinfo->reg_info->feature & BPF_ITER_RESCHED;
77}
78
Yonghong Songe6796542020-08-18 15:23:09 -070079/* maximum visited objects before bailing out */
80#define MAX_ITER_OBJECTS 1000000
81
Yonghong Songfd4f12b2020-05-09 10:59:04 -070082/* bpf_seq_read, a customized and simpler version for bpf iterator.
83 * no_llseek is assumed for this file.
84 * The following are differences from seq_read():
85 * . fixed buffer size (PAGE_SIZE)
86 * . assuming no_llseek
87 * . stop() may call bpf program, handling potential overflow there
88 */
89static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,
90 loff_t *ppos)
91{
92 struct seq_file *seq = file->private_data;
93 size_t n, offs, copied = 0;
Yonghong Songe6796542020-08-18 15:23:09 -070094 int err = 0, num_objs = 0;
Yonghong Songcf83b2d2020-10-27 23:10:54 -070095 bool can_resched;
Yonghong Songfd4f12b2020-05-09 10:59:04 -070096 void *p;
97
98 mutex_lock(&seq->lock);
99
100 if (!seq->buf) {
Alan Maguireaf653202020-09-28 12:31:07 +0100101 seq->size = PAGE_SIZE << 3;
102 seq->buf = kvmalloc(seq->size, GFP_KERNEL);
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700103 if (!seq->buf) {
104 err = -ENOMEM;
105 goto done;
106 }
107 }
108
109 if (seq->count) {
110 n = min(seq->count, size);
111 err = copy_to_user(buf, seq->buf + seq->from, n);
112 if (err) {
113 err = -EFAULT;
114 goto done;
115 }
116 seq->count -= n;
117 seq->from += n;
118 copied = n;
119 goto done;
120 }
121
122 seq->from = 0;
123 p = seq->op->start(seq, &seq->index);
124 if (!p)
125 goto stop;
126 if (IS_ERR(p)) {
127 err = PTR_ERR(p);
128 seq->op->stop(seq, p);
129 seq->count = 0;
130 goto done;
131 }
132
133 err = seq->op->show(seq, p);
134 if (err > 0) {
Yonghong Songe5158d92020-05-09 10:59:07 -0700135 /* object is skipped, decrease seq_num, so next
136 * valid object can reuse the same seq_num.
137 */
138 bpf_iter_dec_seq_num(seq);
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700139 seq->count = 0;
140 } else if (err < 0 || seq_has_overflowed(seq)) {
141 if (!err)
142 err = -E2BIG;
143 seq->op->stop(seq, p);
144 seq->count = 0;
145 goto done;
146 }
147
Yonghong Songcf83b2d2020-10-27 23:10:54 -0700148 can_resched = bpf_iter_support_resched(seq);
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700149 while (1) {
150 loff_t pos = seq->index;
151
Yonghong Songe6796542020-08-18 15:23:09 -0700152 num_objs++;
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700153 offs = seq->count;
154 p = seq->op->next(seq, p, &seq->index);
155 if (pos == seq->index) {
156 pr_info_ratelimited("buggy seq_file .next function %ps "
157 "did not updated position index\n",
158 seq->op->next);
159 seq->index++;
160 }
161
162 if (IS_ERR_OR_NULL(p))
163 break;
164
Yonghong Songe5158d92020-05-09 10:59:07 -0700165 /* got a valid next object, increase seq_num */
166 bpf_iter_inc_seq_num(seq);
167
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700168 if (seq->count >= size)
169 break;
170
Yonghong Songe6796542020-08-18 15:23:09 -0700171 if (num_objs >= MAX_ITER_OBJECTS) {
172 if (offs == 0) {
173 err = -EAGAIN;
174 seq->op->stop(seq, p);
175 goto done;
176 }
177 break;
178 }
179
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700180 err = seq->op->show(seq, p);
181 if (err > 0) {
Yonghong Songe5158d92020-05-09 10:59:07 -0700182 bpf_iter_dec_seq_num(seq);
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700183 seq->count = offs;
184 } else if (err < 0 || seq_has_overflowed(seq)) {
185 seq->count = offs;
186 if (offs == 0) {
187 if (!err)
188 err = -E2BIG;
189 seq->op->stop(seq, p);
190 goto done;
191 }
192 break;
193 }
Yonghong Songcf83b2d2020-10-27 23:10:54 -0700194
195 if (can_resched)
196 cond_resched();
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700197 }
198stop:
199 offs = seq->count;
200 /* bpf program called if !p */
201 seq->op->stop(seq, p);
Yonghong Songe5158d92020-05-09 10:59:07 -0700202 if (!p) {
203 if (!seq_has_overflowed(seq)) {
204 bpf_iter_done_stop(seq);
205 } else {
206 seq->count = offs;
207 if (offs == 0) {
208 err = -E2BIG;
209 goto done;
210 }
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700211 }
212 }
213
214 n = min(seq->count, size);
215 err = copy_to_user(buf, seq->buf, n);
216 if (err) {
217 err = -EFAULT;
218 goto done;
219 }
220 copied = n;
221 seq->count -= n;
222 seq->from = n;
223done:
224 if (!copied)
225 copied = err;
226 else
227 *ppos += copied;
228 mutex_unlock(&seq->lock);
229 return copied;
230}
231
Yonghong Songa5cbe052020-07-23 11:41:12 -0700232static const struct bpf_iter_seq_info *
233__get_seq_info(struct bpf_iter_link *link)
234{
235 const struct bpf_iter_seq_info *seq_info;
236
237 if (link->aux.map) {
238 seq_info = link->aux.map->ops->iter_seq_info;
239 if (seq_info)
240 return seq_info;
241 }
242
243 return link->tinfo->reg_info->seq_info;
244}
245
Yonghong Song367ec3e2020-05-09 10:59:06 -0700246static int iter_open(struct inode *inode, struct file *file)
247{
248 struct bpf_iter_link *link = inode->i_private;
249
Yonghong Songa5cbe052020-07-23 11:41:12 -0700250 return prepare_seq_file(file, link, __get_seq_info(link));
Yonghong Song367ec3e2020-05-09 10:59:06 -0700251}
252
Yonghong Songac51d992020-05-09 10:59:05 -0700253static int iter_release(struct inode *inode, struct file *file)
254{
255 struct bpf_iter_priv_data *iter_priv;
256 struct seq_file *seq;
257
258 seq = file->private_data;
259 if (!seq)
260 return 0;
261
262 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
263 target_private);
264
Yonghong Songa5cbe052020-07-23 11:41:12 -0700265 if (iter_priv->seq_info->fini_seq_private)
266 iter_priv->seq_info->fini_seq_private(seq->private);
Yonghong Songac51d992020-05-09 10:59:05 -0700267
268 bpf_prog_put(iter_priv->prog);
269 seq->private = iter_priv;
270
271 return seq_release_private(inode, file);
272}
273
Yonghong Song367ec3e2020-05-09 10:59:06 -0700274const struct file_operations bpf_iter_fops = {
275 .open = iter_open,
Yonghong Songac51d992020-05-09 10:59:05 -0700276 .llseek = no_llseek,
277 .read = bpf_seq_read,
278 .release = iter_release,
279};
280
Yonghong Song15172a42020-05-13 11:02:19 -0700281/* The argument reg_info will be cached in bpf_iter_target_info.
282 * The common practice is to declare target reg_info as
283 * a const static variable and passed as an argument to
284 * bpf_iter_reg_target().
285 */
286int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info)
Yonghong Songae243452020-05-09 10:58:59 -0700287{
288 struct bpf_iter_target_info *tinfo;
289
Yonghong Song17d8bed2021-02-11 16:59:26 -0800290 tinfo = kzalloc(sizeof(*tinfo), GFP_KERNEL);
Yonghong Songae243452020-05-09 10:58:59 -0700291 if (!tinfo)
292 return -ENOMEM;
293
Yonghong Song15172a42020-05-13 11:02:19 -0700294 tinfo->reg_info = reg_info;
Yonghong Songae243452020-05-09 10:58:59 -0700295 INIT_LIST_HEAD(&tinfo->list);
296
297 mutex_lock(&targets_mutex);
298 list_add(&tinfo->list, &targets);
299 mutex_unlock(&targets_mutex);
300
301 return 0;
302}
303
Yonghong Songab2ee4f2020-05-13 11:02:20 -0700304void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info)
Yonghong Songae243452020-05-09 10:58:59 -0700305{
306 struct bpf_iter_target_info *tinfo;
307 bool found = false;
308
309 mutex_lock(&targets_mutex);
310 list_for_each_entry(tinfo, &targets, list) {
Yonghong Songab2ee4f2020-05-13 11:02:20 -0700311 if (reg_info == tinfo->reg_info) {
Yonghong Songae243452020-05-09 10:58:59 -0700312 list_del(&tinfo->list);
313 kfree(tinfo);
314 found = true;
315 break;
316 }
317 }
318 mutex_unlock(&targets_mutex);
319
320 WARN_ON(found == false);
321}
Yonghong Song15d83c42020-05-09 10:59:00 -0700322
323static void cache_btf_id(struct bpf_iter_target_info *tinfo,
324 struct bpf_prog *prog)
325{
326 tinfo->btf_id = prog->aux->attach_btf_id;
327}
328
329bool bpf_iter_prog_supported(struct bpf_prog *prog)
330{
331 const char *attach_fname = prog->aux->attach_func_name;
332 u32 prog_btf_id = prog->aux->attach_btf_id;
333 const char *prefix = BPF_ITER_FUNC_PREFIX;
334 struct bpf_iter_target_info *tinfo;
335 int prefix_len = strlen(prefix);
336 bool supported = false;
337
338 if (strncmp(attach_fname, prefix, prefix_len))
339 return false;
340
341 mutex_lock(&targets_mutex);
342 list_for_each_entry(tinfo, &targets, list) {
343 if (tinfo->btf_id && tinfo->btf_id == prog_btf_id) {
344 supported = true;
345 break;
346 }
Yonghong Song15172a42020-05-13 11:02:19 -0700347 if (!strcmp(attach_fname + prefix_len, tinfo->reg_info->target)) {
Yonghong Song15d83c42020-05-09 10:59:00 -0700348 cache_btf_id(tinfo, prog);
349 supported = true;
350 break;
351 }
352 }
353 mutex_unlock(&targets_mutex);
354
Yonghong Song3c32cc12020-05-13 11:02:21 -0700355 if (supported) {
356 prog->aux->ctx_arg_info_size = tinfo->reg_info->ctx_arg_info_size;
357 prog->aux->ctx_arg_info = tinfo->reg_info->ctx_arg_info;
358 }
359
Yonghong Song15d83c42020-05-09 10:59:00 -0700360 return supported;
361}
Yonghong Songde4e05c2020-05-09 10:59:01 -0700362
Martin KaFai Lau3cee6fb2021-07-01 13:06:19 -0700363const struct bpf_func_proto *
364bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
365{
366 const struct bpf_iter_target_info *tinfo;
367 const struct bpf_func_proto *fn = NULL;
368
369 mutex_lock(&targets_mutex);
370 list_for_each_entry(tinfo, &targets, list) {
371 if (tinfo->btf_id == prog->aux->attach_btf_id) {
372 const struct bpf_iter_reg *reg_info;
373
374 reg_info = tinfo->reg_info;
375 if (reg_info->get_func_proto)
376 fn = reg_info->get_func_proto(func_id, prog);
377 break;
378 }
379 }
380 mutex_unlock(&targets_mutex);
381
382 return fn;
383}
384
Yonghong Songde4e05c2020-05-09 10:59:01 -0700385static void bpf_iter_link_release(struct bpf_link *link)
386{
Yonghong Songa5cbe052020-07-23 11:41:12 -0700387 struct bpf_iter_link *iter_link =
388 container_of(link, struct bpf_iter_link, link);
389
Yonghong Song5e7b3022020-08-04 22:50:56 -0700390 if (iter_link->tinfo->reg_info->detach_target)
391 iter_link->tinfo->reg_info->detach_target(&iter_link->aux);
Yonghong Songde4e05c2020-05-09 10:59:01 -0700392}
393
394static void bpf_iter_link_dealloc(struct bpf_link *link)
395{
396 struct bpf_iter_link *iter_link =
397 container_of(link, struct bpf_iter_link, link);
398
399 kfree(iter_link);
400}
401
Yonghong Song2057c922020-05-09 10:59:02 -0700402static int bpf_iter_link_replace(struct bpf_link *link,
403 struct bpf_prog *new_prog,
404 struct bpf_prog *old_prog)
405{
406 int ret = 0;
407
408 mutex_lock(&link_mutex);
409 if (old_prog && link->prog != old_prog) {
410 ret = -EPERM;
411 goto out_unlock;
412 }
413
414 if (link->prog->type != new_prog->type ||
415 link->prog->expected_attach_type != new_prog->expected_attach_type ||
416 link->prog->aux->attach_btf_id != new_prog->aux->attach_btf_id) {
417 ret = -EINVAL;
418 goto out_unlock;
419 }
420
421 old_prog = xchg(&link->prog, new_prog);
422 bpf_prog_put(old_prog);
423
424out_unlock:
425 mutex_unlock(&link_mutex);
426 return ret;
427}
428
Yonghong Song6b0a2492020-08-21 11:44:18 -0700429static void bpf_iter_link_show_fdinfo(const struct bpf_link *link,
430 struct seq_file *seq)
431{
432 struct bpf_iter_link *iter_link =
433 container_of(link, struct bpf_iter_link, link);
434 bpf_iter_show_fdinfo_t show_fdinfo;
435
436 seq_printf(seq,
437 "target_name:\t%s\n",
438 iter_link->tinfo->reg_info->target);
439
440 show_fdinfo = iter_link->tinfo->reg_info->show_fdinfo;
441 if (show_fdinfo)
442 show_fdinfo(&iter_link->aux, seq);
443}
444
445static int bpf_iter_link_fill_link_info(const struct bpf_link *link,
446 struct bpf_link_info *info)
447{
448 struct bpf_iter_link *iter_link =
449 container_of(link, struct bpf_iter_link, link);
450 char __user *ubuf = u64_to_user_ptr(info->iter.target_name);
451 bpf_iter_fill_link_info_t fill_link_info;
452 u32 ulen = info->iter.target_name_len;
453 const char *target_name;
454 u32 target_len;
455
456 if (!ulen ^ !ubuf)
457 return -EINVAL;
458
459 target_name = iter_link->tinfo->reg_info->target;
460 target_len = strlen(target_name);
461 info->iter.target_name_len = target_len + 1;
462
463 if (ubuf) {
464 if (ulen >= target_len + 1) {
465 if (copy_to_user(ubuf, target_name, target_len + 1))
466 return -EFAULT;
467 } else {
468 char zero = '\0';
469
470 if (copy_to_user(ubuf, target_name, ulen - 1))
471 return -EFAULT;
472 if (put_user(zero, ubuf + ulen - 1))
473 return -EFAULT;
474 return -ENOSPC;
475 }
476 }
477
478 fill_link_info = iter_link->tinfo->reg_info->fill_link_info;
479 if (fill_link_info)
480 return fill_link_info(&iter_link->aux, info);
481
482 return 0;
483}
484
Yonghong Songde4e05c2020-05-09 10:59:01 -0700485static const struct bpf_link_ops bpf_iter_link_lops = {
486 .release = bpf_iter_link_release,
487 .dealloc = bpf_iter_link_dealloc,
Yonghong Song2057c922020-05-09 10:59:02 -0700488 .update_prog = bpf_iter_link_replace,
Yonghong Song6b0a2492020-08-21 11:44:18 -0700489 .show_fdinfo = bpf_iter_link_show_fdinfo,
490 .fill_link_info = bpf_iter_link_fill_link_info,
Yonghong Songde4e05c2020-05-09 10:59:01 -0700491};
492
Yonghong Song367ec3e2020-05-09 10:59:06 -0700493bool bpf_link_is_iter(struct bpf_link *link)
494{
495 return link->ops == &bpf_iter_link_lops;
496}
497
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -0700498int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
499 struct bpf_prog *prog)
Yonghong Songde4e05c2020-05-09 10:59:01 -0700500{
501 struct bpf_link_primer link_primer;
502 struct bpf_iter_target_info *tinfo;
Yonghong Song5e7b3022020-08-04 22:50:56 -0700503 union bpf_iter_link_info linfo;
Yonghong Songde4e05c2020-05-09 10:59:01 -0700504 struct bpf_iter_link *link;
Yonghong Song5e7b3022020-08-04 22:50:56 -0700505 u32 prog_btf_id, linfo_len;
Yonghong Songde4e05c2020-05-09 10:59:01 -0700506 bool existed = false;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -0700507 bpfptr_t ulinfo;
Yonghong Songde4e05c2020-05-09 10:59:01 -0700508 int err;
509
Yonghong Song5e7b3022020-08-04 22:50:56 -0700510 if (attr->link_create.target_fd || attr->link_create.flags)
511 return -EINVAL;
512
513 memset(&linfo, 0, sizeof(union bpf_iter_link_info));
514
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -0700515 ulinfo = make_bpfptr(attr->link_create.iter_info, uattr.is_kernel);
Yonghong Song5e7b3022020-08-04 22:50:56 -0700516 linfo_len = attr->link_create.iter_info_len;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -0700517 if (bpfptr_is_null(ulinfo) ^ !linfo_len)
Yonghong Song5e7b3022020-08-04 22:50:56 -0700518 return -EINVAL;
519
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -0700520 if (!bpfptr_is_null(ulinfo)) {
Yonghong Song5e7b3022020-08-04 22:50:56 -0700521 err = bpf_check_uarg_tail_zero(ulinfo, sizeof(linfo),
522 linfo_len);
523 if (err)
524 return err;
525 linfo_len = min_t(u32, linfo_len, sizeof(linfo));
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -0700526 if (copy_from_bpfptr(&linfo, ulinfo, linfo_len))
Yonghong Song5e7b3022020-08-04 22:50:56 -0700527 return -EFAULT;
528 }
529
Yonghong Songde4e05c2020-05-09 10:59:01 -0700530 prog_btf_id = prog->aux->attach_btf_id;
531 mutex_lock(&targets_mutex);
532 list_for_each_entry(tinfo, &targets, list) {
533 if (tinfo->btf_id == prog_btf_id) {
534 existed = true;
535 break;
536 }
537 }
538 mutex_unlock(&targets_mutex);
539 if (!existed)
540 return -ENOENT;
541
542 link = kzalloc(sizeof(*link), GFP_USER | __GFP_NOWARN);
543 if (!link)
544 return -ENOMEM;
545
546 bpf_link_init(&link->link, BPF_LINK_TYPE_ITER, &bpf_iter_link_lops, prog);
547 link->tinfo = tinfo;
548
549 err = bpf_link_prime(&link->link, &link_primer);
550 if (err) {
551 kfree(link);
552 return err;
553 }
554
Yonghong Song5e7b3022020-08-04 22:50:56 -0700555 if (tinfo->reg_info->attach_target) {
556 err = tinfo->reg_info->attach_target(prog, &linfo, &link->aux);
Yonghong Songa5cbe052020-07-23 11:41:12 -0700557 if (err) {
Yonghong Song5e7b3022020-08-04 22:50:56 -0700558 bpf_link_cleanup(&link_primer);
559 return err;
Yonghong Songa5cbe052020-07-23 11:41:12 -0700560 }
Yonghong Songa5cbe052020-07-23 11:41:12 -0700561 }
562
Yonghong Songde4e05c2020-05-09 10:59:01 -0700563 return bpf_link_settle(&link_primer);
564}
Yonghong Songac51d992020-05-09 10:59:05 -0700565
566static void init_seq_meta(struct bpf_iter_priv_data *priv_data,
567 struct bpf_iter_target_info *tinfo,
Yonghong Songa5cbe052020-07-23 11:41:12 -0700568 const struct bpf_iter_seq_info *seq_info,
Yonghong Songac51d992020-05-09 10:59:05 -0700569 struct bpf_prog *prog)
570{
571 priv_data->tinfo = tinfo;
Yonghong Songa5cbe052020-07-23 11:41:12 -0700572 priv_data->seq_info = seq_info;
Yonghong Songac51d992020-05-09 10:59:05 -0700573 priv_data->prog = prog;
574 priv_data->session_id = atomic64_inc_return(&session_id);
575 priv_data->seq_num = 0;
576 priv_data->done_stop = false;
577}
578
Yonghong Songa5cbe052020-07-23 11:41:12 -0700579static int prepare_seq_file(struct file *file, struct bpf_iter_link *link,
580 const struct bpf_iter_seq_info *seq_info)
Yonghong Songac51d992020-05-09 10:59:05 -0700581{
582 struct bpf_iter_priv_data *priv_data;
583 struct bpf_iter_target_info *tinfo;
584 struct bpf_prog *prog;
585 u32 total_priv_dsize;
586 struct seq_file *seq;
587 int err = 0;
588
589 mutex_lock(&link_mutex);
590 prog = link->link.prog;
591 bpf_prog_inc(prog);
592 mutex_unlock(&link_mutex);
593
594 tinfo = link->tinfo;
595 total_priv_dsize = offsetof(struct bpf_iter_priv_data, target_private) +
Yonghong Songa5cbe052020-07-23 11:41:12 -0700596 seq_info->seq_priv_size;
597 priv_data = __seq_open_private(file, seq_info->seq_ops,
Yonghong Song15172a42020-05-13 11:02:19 -0700598 total_priv_dsize);
Yonghong Songac51d992020-05-09 10:59:05 -0700599 if (!priv_data) {
600 err = -ENOMEM;
601 goto release_prog;
602 }
603
Yonghong Songa5cbe052020-07-23 11:41:12 -0700604 if (seq_info->init_seq_private) {
605 err = seq_info->init_seq_private(priv_data->target_private, &link->aux);
Yonghong Songac51d992020-05-09 10:59:05 -0700606 if (err)
607 goto release_seq_file;
608 }
609
Yonghong Songa5cbe052020-07-23 11:41:12 -0700610 init_seq_meta(priv_data, tinfo, seq_info, prog);
Yonghong Songac51d992020-05-09 10:59:05 -0700611 seq = file->private_data;
612 seq->private = priv_data->target_private;
613
614 return 0;
615
616release_seq_file:
617 seq_release_private(file->f_inode, file);
618 file->private_data = NULL;
619release_prog:
620 bpf_prog_put(prog);
621 return err;
622}
623
624int bpf_iter_new_fd(struct bpf_link *link)
625{
Yonghong Songa5cbe052020-07-23 11:41:12 -0700626 struct bpf_iter_link *iter_link;
Yonghong Songac51d992020-05-09 10:59:05 -0700627 struct file *file;
628 unsigned int flags;
629 int err, fd;
630
631 if (link->ops != &bpf_iter_link_lops)
632 return -EINVAL;
633
634 flags = O_RDONLY | O_CLOEXEC;
635 fd = get_unused_fd_flags(flags);
636 if (fd < 0)
637 return fd;
638
639 file = anon_inode_getfile("bpf_iter", &bpf_iter_fops, NULL, flags);
640 if (IS_ERR(file)) {
641 err = PTR_ERR(file);
642 goto free_fd;
643 }
644
Yonghong Songa5cbe052020-07-23 11:41:12 -0700645 iter_link = container_of(link, struct bpf_iter_link, link);
646 err = prepare_seq_file(file, iter_link, __get_seq_info(iter_link));
Yonghong Songac51d992020-05-09 10:59:05 -0700647 if (err)
648 goto free_file;
649
650 fd_install(fd, file);
651 return fd;
652
653free_file:
654 fput(file);
655free_fd:
656 put_unused_fd(fd);
657 return err;
658}
Yonghong Songe5158d92020-05-09 10:59:07 -0700659
660struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop)
661{
662 struct bpf_iter_priv_data *iter_priv;
663 struct seq_file *seq;
664 void *seq_priv;
665
666 seq = meta->seq;
667 if (seq->file->f_op != &bpf_iter_fops)
668 return NULL;
669
670 seq_priv = seq->private;
671 iter_priv = container_of(seq_priv, struct bpf_iter_priv_data,
672 target_private);
673
674 if (in_stop && iter_priv->done_stop)
675 return NULL;
676
677 meta->session_id = iter_priv->session_id;
678 meta->seq_num = iter_priv->seq_num;
679
680 return iter_priv->prog;
681}
682
683int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx)
684{
685 int ret;
686
687 rcu_read_lock();
688 migrate_disable();
Andrii Nakryikofb7dd8b2021-08-15 00:05:54 -0700689 ret = bpf_prog_run(prog, ctx);
Yonghong Songe5158d92020-05-09 10:59:07 -0700690 migrate_enable();
691 rcu_read_unlock();
692
Yonghong Song2e3ed682020-05-13 11:02:18 -0700693 /* bpf program can only return 0 or 1:
694 * 0 : okay
695 * 1 : retry the same object
696 * The bpf_iter_run_prog() return value
697 * will be seq_ops->show() return value.
698 */
Yonghong Songe5158d92020-05-09 10:59:07 -0700699 return ret == 0 ? 0 : -EAGAIN;
700}
Yonghong Song69c087b2021-02-26 12:49:25 -0800701
702BPF_CALL_4(bpf_for_each_map_elem, struct bpf_map *, map, void *, callback_fn,
703 void *, callback_ctx, u64, flags)
704{
705 return map->ops->map_for_each_callback(map, callback_fn, callback_ctx, flags);
706}
707
708const struct bpf_func_proto bpf_for_each_map_elem_proto = {
709 .func = bpf_for_each_map_elem,
710 .gpl_only = false,
711 .ret_type = RET_INTEGER,
712 .arg1_type = ARG_CONST_MAP_PTR,
713 .arg2_type = ARG_PTR_TO_FUNC,
714 .arg3_type = ARG_PTR_TO_STACK_OR_NULL,
715 .arg4_type = ARG_ANYTHING,
716};
Joanne Koonge6f2dd02021-11-29 19:06:19 -0800717
718/* maximum number of loops */
719#define MAX_LOOPS BIT(23)
720
721BPF_CALL_4(bpf_loop, u32, nr_loops, void *, callback_fn, void *, callback_ctx,
722 u64, flags)
723{
724 bpf_callback_t callback = (bpf_callback_t)callback_fn;
725 u64 ret;
726 u32 i;
727
728 if (flags)
729 return -EINVAL;
730 if (nr_loops > MAX_LOOPS)
731 return -E2BIG;
732
733 for (i = 0; i < nr_loops; i++) {
734 ret = callback((u64)i, (u64)(long)callback_ctx, 0, 0, 0);
735 /* return value: 0 - continue, 1 - stop and return */
736 if (ret)
737 return i + 1;
738 }
739
740 return i;
741}
742
743const struct bpf_func_proto bpf_loop_proto = {
744 .func = bpf_loop,
745 .gpl_only = false,
746 .ret_type = RET_INTEGER,
747 .arg1_type = ARG_ANYTHING,
748 .arg2_type = ARG_PTR_TO_FUNC,
749 .arg3_type = ARG_PTR_TO_STACK_OR_NULL,
750 .arg4_type = ARG_ANYTHING,
751};