blob: aeec7e1741882b69a560d1b0edc8256b9b9e1067 [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 Songfd4f12b2020-05-09 10:59:04 -070070/* bpf_seq_read, a customized and simpler version for bpf iterator.
71 * no_llseek is assumed for this file.
72 * The following are differences from seq_read():
73 * . fixed buffer size (PAGE_SIZE)
74 * . assuming no_llseek
75 * . stop() may call bpf program, handling potential overflow there
76 */
77static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,
78 loff_t *ppos)
79{
80 struct seq_file *seq = file->private_data;
81 size_t n, offs, copied = 0;
82 int err = 0;
83 void *p;
84
85 mutex_lock(&seq->lock);
86
87 if (!seq->buf) {
88 seq->size = PAGE_SIZE;
89 seq->buf = kmalloc(seq->size, GFP_KERNEL);
90 if (!seq->buf) {
91 err = -ENOMEM;
92 goto done;
93 }
94 }
95
96 if (seq->count) {
97 n = min(seq->count, size);
98 err = copy_to_user(buf, seq->buf + seq->from, n);
99 if (err) {
100 err = -EFAULT;
101 goto done;
102 }
103 seq->count -= n;
104 seq->from += n;
105 copied = n;
106 goto done;
107 }
108
109 seq->from = 0;
110 p = seq->op->start(seq, &seq->index);
111 if (!p)
112 goto stop;
113 if (IS_ERR(p)) {
114 err = PTR_ERR(p);
115 seq->op->stop(seq, p);
116 seq->count = 0;
117 goto done;
118 }
119
120 err = seq->op->show(seq, p);
121 if (err > 0) {
Yonghong Songe5158d92020-05-09 10:59:07 -0700122 /* object is skipped, decrease seq_num, so next
123 * valid object can reuse the same seq_num.
124 */
125 bpf_iter_dec_seq_num(seq);
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700126 seq->count = 0;
127 } else if (err < 0 || seq_has_overflowed(seq)) {
128 if (!err)
129 err = -E2BIG;
130 seq->op->stop(seq, p);
131 seq->count = 0;
132 goto done;
133 }
134
135 while (1) {
136 loff_t pos = seq->index;
137
138 offs = seq->count;
139 p = seq->op->next(seq, p, &seq->index);
140 if (pos == seq->index) {
141 pr_info_ratelimited("buggy seq_file .next function %ps "
142 "did not updated position index\n",
143 seq->op->next);
144 seq->index++;
145 }
146
147 if (IS_ERR_OR_NULL(p))
148 break;
149
Yonghong Songe5158d92020-05-09 10:59:07 -0700150 /* got a valid next object, increase seq_num */
151 bpf_iter_inc_seq_num(seq);
152
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700153 if (seq->count >= size)
154 break;
155
156 err = seq->op->show(seq, p);
157 if (err > 0) {
Yonghong Songe5158d92020-05-09 10:59:07 -0700158 bpf_iter_dec_seq_num(seq);
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700159 seq->count = offs;
160 } else if (err < 0 || seq_has_overflowed(seq)) {
161 seq->count = offs;
162 if (offs == 0) {
163 if (!err)
164 err = -E2BIG;
165 seq->op->stop(seq, p);
166 goto done;
167 }
168 break;
169 }
170 }
171stop:
172 offs = seq->count;
173 /* bpf program called if !p */
174 seq->op->stop(seq, p);
Yonghong Songe5158d92020-05-09 10:59:07 -0700175 if (!p) {
176 if (!seq_has_overflowed(seq)) {
177 bpf_iter_done_stop(seq);
178 } else {
179 seq->count = offs;
180 if (offs == 0) {
181 err = -E2BIG;
182 goto done;
183 }
Yonghong Songfd4f12b2020-05-09 10:59:04 -0700184 }
185 }
186
187 n = min(seq->count, size);
188 err = copy_to_user(buf, seq->buf, n);
189 if (err) {
190 err = -EFAULT;
191 goto done;
192 }
193 copied = n;
194 seq->count -= n;
195 seq->from = n;
196done:
197 if (!copied)
198 copied = err;
199 else
200 *ppos += copied;
201 mutex_unlock(&seq->lock);
202 return copied;
203}
204
Yonghong Songa5cbe052020-07-23 11:41:12 -0700205static const struct bpf_iter_seq_info *
206__get_seq_info(struct bpf_iter_link *link)
207{
208 const struct bpf_iter_seq_info *seq_info;
209
210 if (link->aux.map) {
211 seq_info = link->aux.map->ops->iter_seq_info;
212 if (seq_info)
213 return seq_info;
214 }
215
216 return link->tinfo->reg_info->seq_info;
217}
218
Yonghong Song367ec3e2020-05-09 10:59:06 -0700219static int iter_open(struct inode *inode, struct file *file)
220{
221 struct bpf_iter_link *link = inode->i_private;
222
Yonghong Songa5cbe052020-07-23 11:41:12 -0700223 return prepare_seq_file(file, link, __get_seq_info(link));
Yonghong Song367ec3e2020-05-09 10:59:06 -0700224}
225
Yonghong Songac51d992020-05-09 10:59:05 -0700226static int iter_release(struct inode *inode, struct file *file)
227{
228 struct bpf_iter_priv_data *iter_priv;
229 struct seq_file *seq;
230
231 seq = file->private_data;
232 if (!seq)
233 return 0;
234
235 iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
236 target_private);
237
Yonghong Songa5cbe052020-07-23 11:41:12 -0700238 if (iter_priv->seq_info->fini_seq_private)
239 iter_priv->seq_info->fini_seq_private(seq->private);
Yonghong Songac51d992020-05-09 10:59:05 -0700240
241 bpf_prog_put(iter_priv->prog);
242 seq->private = iter_priv;
243
244 return seq_release_private(inode, file);
245}
246
Yonghong Song367ec3e2020-05-09 10:59:06 -0700247const struct file_operations bpf_iter_fops = {
248 .open = iter_open,
Yonghong Songac51d992020-05-09 10:59:05 -0700249 .llseek = no_llseek,
250 .read = bpf_seq_read,
251 .release = iter_release,
252};
253
Yonghong Song15172a42020-05-13 11:02:19 -0700254/* The argument reg_info will be cached in bpf_iter_target_info.
255 * The common practice is to declare target reg_info as
256 * a const static variable and passed as an argument to
257 * bpf_iter_reg_target().
258 */
259int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info)
Yonghong Songae243452020-05-09 10:58:59 -0700260{
261 struct bpf_iter_target_info *tinfo;
262
263 tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
264 if (!tinfo)
265 return -ENOMEM;
266
Yonghong Song15172a42020-05-13 11:02:19 -0700267 tinfo->reg_info = reg_info;
Yonghong Songae243452020-05-09 10:58:59 -0700268 INIT_LIST_HEAD(&tinfo->list);
269
270 mutex_lock(&targets_mutex);
271 list_add(&tinfo->list, &targets);
272 mutex_unlock(&targets_mutex);
273
274 return 0;
275}
276
Yonghong Songab2ee4f2020-05-13 11:02:20 -0700277void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info)
Yonghong Songae243452020-05-09 10:58:59 -0700278{
279 struct bpf_iter_target_info *tinfo;
280 bool found = false;
281
282 mutex_lock(&targets_mutex);
283 list_for_each_entry(tinfo, &targets, list) {
Yonghong Songab2ee4f2020-05-13 11:02:20 -0700284 if (reg_info == tinfo->reg_info) {
Yonghong Songae243452020-05-09 10:58:59 -0700285 list_del(&tinfo->list);
286 kfree(tinfo);
287 found = true;
288 break;
289 }
290 }
291 mutex_unlock(&targets_mutex);
292
293 WARN_ON(found == false);
294}
Yonghong Song15d83c42020-05-09 10:59:00 -0700295
296static void cache_btf_id(struct bpf_iter_target_info *tinfo,
297 struct bpf_prog *prog)
298{
299 tinfo->btf_id = prog->aux->attach_btf_id;
300}
301
302bool bpf_iter_prog_supported(struct bpf_prog *prog)
303{
304 const char *attach_fname = prog->aux->attach_func_name;
305 u32 prog_btf_id = prog->aux->attach_btf_id;
306 const char *prefix = BPF_ITER_FUNC_PREFIX;
307 struct bpf_iter_target_info *tinfo;
308 int prefix_len = strlen(prefix);
309 bool supported = false;
310
311 if (strncmp(attach_fname, prefix, prefix_len))
312 return false;
313
314 mutex_lock(&targets_mutex);
315 list_for_each_entry(tinfo, &targets, list) {
316 if (tinfo->btf_id && tinfo->btf_id == prog_btf_id) {
317 supported = true;
318 break;
319 }
Yonghong Song15172a42020-05-13 11:02:19 -0700320 if (!strcmp(attach_fname + prefix_len, tinfo->reg_info->target)) {
Yonghong Song15d83c42020-05-09 10:59:00 -0700321 cache_btf_id(tinfo, prog);
322 supported = true;
323 break;
324 }
325 }
326 mutex_unlock(&targets_mutex);
327
Yonghong Song3c32cc12020-05-13 11:02:21 -0700328 if (supported) {
329 prog->aux->ctx_arg_info_size = tinfo->reg_info->ctx_arg_info_size;
330 prog->aux->ctx_arg_info = tinfo->reg_info->ctx_arg_info;
331 }
332
Yonghong Song15d83c42020-05-09 10:59:00 -0700333 return supported;
334}
Yonghong Songde4e05c2020-05-09 10:59:01 -0700335
336static void bpf_iter_link_release(struct bpf_link *link)
337{
Yonghong Songa5cbe052020-07-23 11:41:12 -0700338 struct bpf_iter_link *iter_link =
339 container_of(link, struct bpf_iter_link, link);
340
Yonghong Song5e7b3022020-08-04 22:50:56 -0700341 if (iter_link->tinfo->reg_info->detach_target)
342 iter_link->tinfo->reg_info->detach_target(&iter_link->aux);
Yonghong Songde4e05c2020-05-09 10:59:01 -0700343}
344
345static void bpf_iter_link_dealloc(struct bpf_link *link)
346{
347 struct bpf_iter_link *iter_link =
348 container_of(link, struct bpf_iter_link, link);
349
350 kfree(iter_link);
351}
352
Yonghong Song2057c922020-05-09 10:59:02 -0700353static int bpf_iter_link_replace(struct bpf_link *link,
354 struct bpf_prog *new_prog,
355 struct bpf_prog *old_prog)
356{
357 int ret = 0;
358
359 mutex_lock(&link_mutex);
360 if (old_prog && link->prog != old_prog) {
361 ret = -EPERM;
362 goto out_unlock;
363 }
364
365 if (link->prog->type != new_prog->type ||
366 link->prog->expected_attach_type != new_prog->expected_attach_type ||
367 link->prog->aux->attach_btf_id != new_prog->aux->attach_btf_id) {
368 ret = -EINVAL;
369 goto out_unlock;
370 }
371
372 old_prog = xchg(&link->prog, new_prog);
373 bpf_prog_put(old_prog);
374
375out_unlock:
376 mutex_unlock(&link_mutex);
377 return ret;
378}
379
Yonghong Song6b0a2492020-08-21 11:44:18 -0700380static void bpf_iter_link_show_fdinfo(const struct bpf_link *link,
381 struct seq_file *seq)
382{
383 struct bpf_iter_link *iter_link =
384 container_of(link, struct bpf_iter_link, link);
385 bpf_iter_show_fdinfo_t show_fdinfo;
386
387 seq_printf(seq,
388 "target_name:\t%s\n",
389 iter_link->tinfo->reg_info->target);
390
391 show_fdinfo = iter_link->tinfo->reg_info->show_fdinfo;
392 if (show_fdinfo)
393 show_fdinfo(&iter_link->aux, seq);
394}
395
396static int bpf_iter_link_fill_link_info(const struct bpf_link *link,
397 struct bpf_link_info *info)
398{
399 struct bpf_iter_link *iter_link =
400 container_of(link, struct bpf_iter_link, link);
401 char __user *ubuf = u64_to_user_ptr(info->iter.target_name);
402 bpf_iter_fill_link_info_t fill_link_info;
403 u32 ulen = info->iter.target_name_len;
404 const char *target_name;
405 u32 target_len;
406
407 if (!ulen ^ !ubuf)
408 return -EINVAL;
409
410 target_name = iter_link->tinfo->reg_info->target;
411 target_len = strlen(target_name);
412 info->iter.target_name_len = target_len + 1;
413
414 if (ubuf) {
415 if (ulen >= target_len + 1) {
416 if (copy_to_user(ubuf, target_name, target_len + 1))
417 return -EFAULT;
418 } else {
419 char zero = '\0';
420
421 if (copy_to_user(ubuf, target_name, ulen - 1))
422 return -EFAULT;
423 if (put_user(zero, ubuf + ulen - 1))
424 return -EFAULT;
425 return -ENOSPC;
426 }
427 }
428
429 fill_link_info = iter_link->tinfo->reg_info->fill_link_info;
430 if (fill_link_info)
431 return fill_link_info(&iter_link->aux, info);
432
433 return 0;
434}
435
Yonghong Songde4e05c2020-05-09 10:59:01 -0700436static const struct bpf_link_ops bpf_iter_link_lops = {
437 .release = bpf_iter_link_release,
438 .dealloc = bpf_iter_link_dealloc,
Yonghong Song2057c922020-05-09 10:59:02 -0700439 .update_prog = bpf_iter_link_replace,
Yonghong Song6b0a2492020-08-21 11:44:18 -0700440 .show_fdinfo = bpf_iter_link_show_fdinfo,
441 .fill_link_info = bpf_iter_link_fill_link_info,
Yonghong Songde4e05c2020-05-09 10:59:01 -0700442};
443
Yonghong Song367ec3e2020-05-09 10:59:06 -0700444bool bpf_link_is_iter(struct bpf_link *link)
445{
446 return link->ops == &bpf_iter_link_lops;
447}
448
Yonghong Songde4e05c2020-05-09 10:59:01 -0700449int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
450{
Yonghong Song5e7b3022020-08-04 22:50:56 -0700451 union bpf_iter_link_info __user *ulinfo;
Yonghong Songde4e05c2020-05-09 10:59:01 -0700452 struct bpf_link_primer link_primer;
453 struct bpf_iter_target_info *tinfo;
Yonghong Song5e7b3022020-08-04 22:50:56 -0700454 union bpf_iter_link_info linfo;
Yonghong Songde4e05c2020-05-09 10:59:01 -0700455 struct bpf_iter_link *link;
Yonghong Song5e7b3022020-08-04 22:50:56 -0700456 u32 prog_btf_id, linfo_len;
Yonghong Songde4e05c2020-05-09 10:59:01 -0700457 bool existed = false;
Yonghong Songde4e05c2020-05-09 10:59:01 -0700458 int err;
459
Yonghong Song5e7b3022020-08-04 22:50:56 -0700460 if (attr->link_create.target_fd || attr->link_create.flags)
461 return -EINVAL;
462
463 memset(&linfo, 0, sizeof(union bpf_iter_link_info));
464
465 ulinfo = u64_to_user_ptr(attr->link_create.iter_info);
466 linfo_len = attr->link_create.iter_info_len;
467 if (!ulinfo ^ !linfo_len)
468 return -EINVAL;
469
470 if (ulinfo) {
471 err = bpf_check_uarg_tail_zero(ulinfo, sizeof(linfo),
472 linfo_len);
473 if (err)
474 return err;
475 linfo_len = min_t(u32, linfo_len, sizeof(linfo));
476 if (copy_from_user(&linfo, ulinfo, linfo_len))
477 return -EFAULT;
478 }
479
Yonghong Songde4e05c2020-05-09 10:59:01 -0700480 prog_btf_id = prog->aux->attach_btf_id;
481 mutex_lock(&targets_mutex);
482 list_for_each_entry(tinfo, &targets, list) {
483 if (tinfo->btf_id == prog_btf_id) {
484 existed = true;
485 break;
486 }
487 }
488 mutex_unlock(&targets_mutex);
489 if (!existed)
490 return -ENOENT;
491
492 link = kzalloc(sizeof(*link), GFP_USER | __GFP_NOWARN);
493 if (!link)
494 return -ENOMEM;
495
496 bpf_link_init(&link->link, BPF_LINK_TYPE_ITER, &bpf_iter_link_lops, prog);
497 link->tinfo = tinfo;
498
499 err = bpf_link_prime(&link->link, &link_primer);
500 if (err) {
501 kfree(link);
502 return err;
503 }
504
Yonghong Song5e7b3022020-08-04 22:50:56 -0700505 if (tinfo->reg_info->attach_target) {
506 err = tinfo->reg_info->attach_target(prog, &linfo, &link->aux);
Yonghong Songa5cbe052020-07-23 11:41:12 -0700507 if (err) {
Yonghong Song5e7b3022020-08-04 22:50:56 -0700508 bpf_link_cleanup(&link_primer);
509 return err;
Yonghong Songa5cbe052020-07-23 11:41:12 -0700510 }
Yonghong Songa5cbe052020-07-23 11:41:12 -0700511 }
512
Yonghong Songde4e05c2020-05-09 10:59:01 -0700513 return bpf_link_settle(&link_primer);
514}
Yonghong Songac51d992020-05-09 10:59:05 -0700515
516static void init_seq_meta(struct bpf_iter_priv_data *priv_data,
517 struct bpf_iter_target_info *tinfo,
Yonghong Songa5cbe052020-07-23 11:41:12 -0700518 const struct bpf_iter_seq_info *seq_info,
Yonghong Songac51d992020-05-09 10:59:05 -0700519 struct bpf_prog *prog)
520{
521 priv_data->tinfo = tinfo;
Yonghong Songa5cbe052020-07-23 11:41:12 -0700522 priv_data->seq_info = seq_info;
Yonghong Songac51d992020-05-09 10:59:05 -0700523 priv_data->prog = prog;
524 priv_data->session_id = atomic64_inc_return(&session_id);
525 priv_data->seq_num = 0;
526 priv_data->done_stop = false;
527}
528
Yonghong Songa5cbe052020-07-23 11:41:12 -0700529static int prepare_seq_file(struct file *file, struct bpf_iter_link *link,
530 const struct bpf_iter_seq_info *seq_info)
Yonghong Songac51d992020-05-09 10:59:05 -0700531{
532 struct bpf_iter_priv_data *priv_data;
533 struct bpf_iter_target_info *tinfo;
534 struct bpf_prog *prog;
535 u32 total_priv_dsize;
536 struct seq_file *seq;
537 int err = 0;
538
539 mutex_lock(&link_mutex);
540 prog = link->link.prog;
541 bpf_prog_inc(prog);
542 mutex_unlock(&link_mutex);
543
544 tinfo = link->tinfo;
545 total_priv_dsize = offsetof(struct bpf_iter_priv_data, target_private) +
Yonghong Songa5cbe052020-07-23 11:41:12 -0700546 seq_info->seq_priv_size;
547 priv_data = __seq_open_private(file, seq_info->seq_ops,
Yonghong Song15172a42020-05-13 11:02:19 -0700548 total_priv_dsize);
Yonghong Songac51d992020-05-09 10:59:05 -0700549 if (!priv_data) {
550 err = -ENOMEM;
551 goto release_prog;
552 }
553
Yonghong Songa5cbe052020-07-23 11:41:12 -0700554 if (seq_info->init_seq_private) {
555 err = seq_info->init_seq_private(priv_data->target_private, &link->aux);
Yonghong Songac51d992020-05-09 10:59:05 -0700556 if (err)
557 goto release_seq_file;
558 }
559
Yonghong Songa5cbe052020-07-23 11:41:12 -0700560 init_seq_meta(priv_data, tinfo, seq_info, prog);
Yonghong Songac51d992020-05-09 10:59:05 -0700561 seq = file->private_data;
562 seq->private = priv_data->target_private;
563
564 return 0;
565
566release_seq_file:
567 seq_release_private(file->f_inode, file);
568 file->private_data = NULL;
569release_prog:
570 bpf_prog_put(prog);
571 return err;
572}
573
574int bpf_iter_new_fd(struct bpf_link *link)
575{
Yonghong Songa5cbe052020-07-23 11:41:12 -0700576 struct bpf_iter_link *iter_link;
Yonghong Songac51d992020-05-09 10:59:05 -0700577 struct file *file;
578 unsigned int flags;
579 int err, fd;
580
581 if (link->ops != &bpf_iter_link_lops)
582 return -EINVAL;
583
584 flags = O_RDONLY | O_CLOEXEC;
585 fd = get_unused_fd_flags(flags);
586 if (fd < 0)
587 return fd;
588
589 file = anon_inode_getfile("bpf_iter", &bpf_iter_fops, NULL, flags);
590 if (IS_ERR(file)) {
591 err = PTR_ERR(file);
592 goto free_fd;
593 }
594
Yonghong Songa5cbe052020-07-23 11:41:12 -0700595 iter_link = container_of(link, struct bpf_iter_link, link);
596 err = prepare_seq_file(file, iter_link, __get_seq_info(iter_link));
Yonghong Songac51d992020-05-09 10:59:05 -0700597 if (err)
598 goto free_file;
599
600 fd_install(fd, file);
601 return fd;
602
603free_file:
604 fput(file);
605free_fd:
606 put_unused_fd(fd);
607 return err;
608}
Yonghong Songe5158d92020-05-09 10:59:07 -0700609
610struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop)
611{
612 struct bpf_iter_priv_data *iter_priv;
613 struct seq_file *seq;
614 void *seq_priv;
615
616 seq = meta->seq;
617 if (seq->file->f_op != &bpf_iter_fops)
618 return NULL;
619
620 seq_priv = seq->private;
621 iter_priv = container_of(seq_priv, struct bpf_iter_priv_data,
622 target_private);
623
624 if (in_stop && iter_priv->done_stop)
625 return NULL;
626
627 meta->session_id = iter_priv->session_id;
628 meta->seq_num = iter_priv->seq_num;
629
630 return iter_priv->prog;
631}
632
633int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx)
634{
635 int ret;
636
637 rcu_read_lock();
638 migrate_disable();
639 ret = BPF_PROG_RUN(prog, ctx);
640 migrate_enable();
641 rcu_read_unlock();
642
Yonghong Song2e3ed682020-05-13 11:02:18 -0700643 /* bpf program can only return 0 or 1:
644 * 0 : okay
645 * 1 : retry the same object
646 * The bpf_iter_run_prog() return value
647 * will be seq_ops->show() return value.
648 */
Yonghong Songe5158d92020-05-09 10:59:07 -0700649 return ret == 0 ? 0 : -EAGAIN;
650}