Yonghong Song | ae24345 | 2020-05-09 10:58:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* Copyright (c) 2020 Facebook */ |
| 3 | |
| 4 | #include <linux/fs.h> |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 5 | #include <linux/anon_inodes.h> |
Yonghong Song | ae24345 | 2020-05-09 10:58:59 -0700 | [diff] [blame] | 6 | #include <linux/filter.h> |
| 7 | #include <linux/bpf.h> |
| 8 | |
| 9 | struct bpf_iter_target_info { |
| 10 | struct list_head list; |
Yonghong Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame] | 11 | const struct bpf_iter_reg *reg_info; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 12 | u32 btf_id; /* cached value */ |
Yonghong Song | ae24345 | 2020-05-09 10:58:59 -0700 | [diff] [blame] | 13 | }; |
| 14 | |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 15 | struct bpf_iter_link { |
| 16 | struct bpf_link link; |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 17 | struct bpf_iter_aux_info aux; |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 18 | struct bpf_iter_target_info *tinfo; |
| 19 | }; |
| 20 | |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 21 | struct bpf_iter_priv_data { |
| 22 | struct bpf_iter_target_info *tinfo; |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 23 | const struct bpf_iter_seq_info *seq_info; |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 24 | 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 Song | ae24345 | 2020-05-09 10:58:59 -0700 | [diff] [blame] | 31 | static struct list_head targets = LIST_HEAD_INIT(targets); |
| 32 | static DEFINE_MUTEX(targets_mutex); |
| 33 | |
Yonghong Song | 2057c92 | 2020-05-09 10:59:02 -0700 | [diff] [blame] | 34 | /* protect bpf_iter_link changes */ |
| 35 | static DEFINE_MUTEX(link_mutex); |
| 36 | |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 37 | /* incremented on every opened seq_file */ |
| 38 | static atomic64_t session_id; |
| 39 | |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 40 | static int prepare_seq_file(struct file *file, struct bpf_iter_link *link, |
| 41 | const struct bpf_iter_seq_info *seq_info); |
Yonghong Song | 367ec3e | 2020-05-09 10:59:06 -0700 | [diff] [blame] | 42 | |
Yonghong Song | e5158d9 | 2020-05-09 10:59:07 -0700 | [diff] [blame] | 43 | static 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 | |
| 52 | static 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 | |
| 61 | static 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 Song | fd4f12b | 2020-05-09 10:59:04 -0700 | [diff] [blame] | 70 | /* 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 | */ |
| 77 | static 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 Song | e5158d9 | 2020-05-09 10:59:07 -0700 | [diff] [blame] | 122 | /* 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 Song | fd4f12b | 2020-05-09 10:59:04 -0700 | [diff] [blame] | 126 | 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 Song | e5158d9 | 2020-05-09 10:59:07 -0700 | [diff] [blame] | 150 | /* got a valid next object, increase seq_num */ |
| 151 | bpf_iter_inc_seq_num(seq); |
| 152 | |
Yonghong Song | fd4f12b | 2020-05-09 10:59:04 -0700 | [diff] [blame] | 153 | if (seq->count >= size) |
| 154 | break; |
| 155 | |
| 156 | err = seq->op->show(seq, p); |
| 157 | if (err > 0) { |
Yonghong Song | e5158d9 | 2020-05-09 10:59:07 -0700 | [diff] [blame] | 158 | bpf_iter_dec_seq_num(seq); |
Yonghong Song | fd4f12b | 2020-05-09 10:59:04 -0700 | [diff] [blame] | 159 | 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 | } |
| 171 | stop: |
| 172 | offs = seq->count; |
| 173 | /* bpf program called if !p */ |
| 174 | seq->op->stop(seq, p); |
Yonghong Song | e5158d9 | 2020-05-09 10:59:07 -0700 | [diff] [blame] | 175 | 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 Song | fd4f12b | 2020-05-09 10:59:04 -0700 | [diff] [blame] | 184 | } |
| 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; |
| 196 | done: |
| 197 | if (!copied) |
| 198 | copied = err; |
| 199 | else |
| 200 | *ppos += copied; |
| 201 | mutex_unlock(&seq->lock); |
| 202 | return copied; |
| 203 | } |
| 204 | |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 205 | static 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 Song | 367ec3e | 2020-05-09 10:59:06 -0700 | [diff] [blame] | 219 | static int iter_open(struct inode *inode, struct file *file) |
| 220 | { |
| 221 | struct bpf_iter_link *link = inode->i_private; |
| 222 | |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 223 | return prepare_seq_file(file, link, __get_seq_info(link)); |
Yonghong Song | 367ec3e | 2020-05-09 10:59:06 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 226 | static 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 Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 238 | if (iter_priv->seq_info->fini_seq_private) |
| 239 | iter_priv->seq_info->fini_seq_private(seq->private); |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 240 | |
| 241 | bpf_prog_put(iter_priv->prog); |
| 242 | seq->private = iter_priv; |
| 243 | |
| 244 | return seq_release_private(inode, file); |
| 245 | } |
| 246 | |
Yonghong Song | 367ec3e | 2020-05-09 10:59:06 -0700 | [diff] [blame] | 247 | const struct file_operations bpf_iter_fops = { |
| 248 | .open = iter_open, |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 249 | .llseek = no_llseek, |
| 250 | .read = bpf_seq_read, |
| 251 | .release = iter_release, |
| 252 | }; |
| 253 | |
Yonghong Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame] | 254 | /* 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 | */ |
| 259 | int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info) |
Yonghong Song | ae24345 | 2020-05-09 10:58:59 -0700 | [diff] [blame] | 260 | { |
| 261 | struct bpf_iter_target_info *tinfo; |
| 262 | |
| 263 | tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL); |
| 264 | if (!tinfo) |
| 265 | return -ENOMEM; |
| 266 | |
Yonghong Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame] | 267 | tinfo->reg_info = reg_info; |
Yonghong Song | ae24345 | 2020-05-09 10:58:59 -0700 | [diff] [blame] | 268 | 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 Song | ab2ee4f | 2020-05-13 11:02:20 -0700 | [diff] [blame] | 277 | void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info) |
Yonghong Song | ae24345 | 2020-05-09 10:58:59 -0700 | [diff] [blame] | 278 | { |
| 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 Song | ab2ee4f | 2020-05-13 11:02:20 -0700 | [diff] [blame] | 284 | if (reg_info == tinfo->reg_info) { |
Yonghong Song | ae24345 | 2020-05-09 10:58:59 -0700 | [diff] [blame] | 285 | 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 Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 295 | |
| 296 | static 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 | |
| 302 | bool 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 Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame] | 320 | if (!strcmp(attach_fname + prefix_len, tinfo->reg_info->target)) { |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 321 | cache_btf_id(tinfo, prog); |
| 322 | supported = true; |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | mutex_unlock(&targets_mutex); |
| 327 | |
Yonghong Song | 3c32cc1 | 2020-05-13 11:02:21 -0700 | [diff] [blame] | 328 | 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 Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 333 | return supported; |
| 334 | } |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 335 | |
| 336 | static void bpf_iter_link_release(struct bpf_link *link) |
| 337 | { |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 338 | struct bpf_iter_link *iter_link = |
| 339 | container_of(link, struct bpf_iter_link, link); |
| 340 | |
Yonghong Song | 5e7b302 | 2020-08-04 22:50:56 -0700 | [diff] [blame] | 341 | if (iter_link->tinfo->reg_info->detach_target) |
| 342 | iter_link->tinfo->reg_info->detach_target(&iter_link->aux); |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static 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 Song | 2057c92 | 2020-05-09 10:59:02 -0700 | [diff] [blame] | 353 | static 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 | |
| 375 | out_unlock: |
| 376 | mutex_unlock(&link_mutex); |
| 377 | return ret; |
| 378 | } |
| 379 | |
Yonghong Song | 6b0a249 | 2020-08-21 11:44:18 -0700 | [diff] [blame^] | 380 | static 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 | |
| 396 | static 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 Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 436 | static const struct bpf_link_ops bpf_iter_link_lops = { |
| 437 | .release = bpf_iter_link_release, |
| 438 | .dealloc = bpf_iter_link_dealloc, |
Yonghong Song | 2057c92 | 2020-05-09 10:59:02 -0700 | [diff] [blame] | 439 | .update_prog = bpf_iter_link_replace, |
Yonghong Song | 6b0a249 | 2020-08-21 11:44:18 -0700 | [diff] [blame^] | 440 | .show_fdinfo = bpf_iter_link_show_fdinfo, |
| 441 | .fill_link_info = bpf_iter_link_fill_link_info, |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 442 | }; |
| 443 | |
Yonghong Song | 367ec3e | 2020-05-09 10:59:06 -0700 | [diff] [blame] | 444 | bool bpf_link_is_iter(struct bpf_link *link) |
| 445 | { |
| 446 | return link->ops == &bpf_iter_link_lops; |
| 447 | } |
| 448 | |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 449 | int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) |
| 450 | { |
Yonghong Song | 5e7b302 | 2020-08-04 22:50:56 -0700 | [diff] [blame] | 451 | union bpf_iter_link_info __user *ulinfo; |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 452 | struct bpf_link_primer link_primer; |
| 453 | struct bpf_iter_target_info *tinfo; |
Yonghong Song | 5e7b302 | 2020-08-04 22:50:56 -0700 | [diff] [blame] | 454 | union bpf_iter_link_info linfo; |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 455 | struct bpf_iter_link *link; |
Yonghong Song | 5e7b302 | 2020-08-04 22:50:56 -0700 | [diff] [blame] | 456 | u32 prog_btf_id, linfo_len; |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 457 | bool existed = false; |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 458 | int err; |
| 459 | |
Yonghong Song | 5e7b302 | 2020-08-04 22:50:56 -0700 | [diff] [blame] | 460 | 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 Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 480 | 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 Song | 5e7b302 | 2020-08-04 22:50:56 -0700 | [diff] [blame] | 505 | if (tinfo->reg_info->attach_target) { |
| 506 | err = tinfo->reg_info->attach_target(prog, &linfo, &link->aux); |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 507 | if (err) { |
Yonghong Song | 5e7b302 | 2020-08-04 22:50:56 -0700 | [diff] [blame] | 508 | bpf_link_cleanup(&link_primer); |
| 509 | return err; |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 510 | } |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 513 | return bpf_link_settle(&link_primer); |
| 514 | } |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 515 | |
| 516 | static void init_seq_meta(struct bpf_iter_priv_data *priv_data, |
| 517 | struct bpf_iter_target_info *tinfo, |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 518 | const struct bpf_iter_seq_info *seq_info, |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 519 | struct bpf_prog *prog) |
| 520 | { |
| 521 | priv_data->tinfo = tinfo; |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 522 | priv_data->seq_info = seq_info; |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 523 | 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 Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 529 | static int prepare_seq_file(struct file *file, struct bpf_iter_link *link, |
| 530 | const struct bpf_iter_seq_info *seq_info) |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 531 | { |
| 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 Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 546 | seq_info->seq_priv_size; |
| 547 | priv_data = __seq_open_private(file, seq_info->seq_ops, |
Yonghong Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame] | 548 | total_priv_dsize); |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 549 | if (!priv_data) { |
| 550 | err = -ENOMEM; |
| 551 | goto release_prog; |
| 552 | } |
| 553 | |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 554 | if (seq_info->init_seq_private) { |
| 555 | err = seq_info->init_seq_private(priv_data->target_private, &link->aux); |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 556 | if (err) |
| 557 | goto release_seq_file; |
| 558 | } |
| 559 | |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 560 | init_seq_meta(priv_data, tinfo, seq_info, prog); |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 561 | seq = file->private_data; |
| 562 | seq->private = priv_data->target_private; |
| 563 | |
| 564 | return 0; |
| 565 | |
| 566 | release_seq_file: |
| 567 | seq_release_private(file->f_inode, file); |
| 568 | file->private_data = NULL; |
| 569 | release_prog: |
| 570 | bpf_prog_put(prog); |
| 571 | return err; |
| 572 | } |
| 573 | |
| 574 | int bpf_iter_new_fd(struct bpf_link *link) |
| 575 | { |
Yonghong Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 576 | struct bpf_iter_link *iter_link; |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 577 | 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 Song | a5cbe05 | 2020-07-23 11:41:12 -0700 | [diff] [blame] | 595 | iter_link = container_of(link, struct bpf_iter_link, link); |
| 596 | err = prepare_seq_file(file, iter_link, __get_seq_info(iter_link)); |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 597 | if (err) |
| 598 | goto free_file; |
| 599 | |
| 600 | fd_install(fd, file); |
| 601 | return fd; |
| 602 | |
| 603 | free_file: |
| 604 | fput(file); |
| 605 | free_fd: |
| 606 | put_unused_fd(fd); |
| 607 | return err; |
| 608 | } |
Yonghong Song | e5158d9 | 2020-05-09 10:59:07 -0700 | [diff] [blame] | 609 | |
| 610 | struct 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 | |
| 633 | int 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 Song | 2e3ed68 | 2020-05-13 11:02:18 -0700 | [diff] [blame] | 643 | /* 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 Song | e5158d9 | 2020-05-09 10:59:07 -0700 | [diff] [blame] | 649 | return ret == 0 ? 0 : -EAGAIN; |
| 650 | } |