drm/panfrost: Remove set but not used variable 'bo'
[linux/fpc-iii.git] / kernel / bpf / helpers.c
blobd8b7b110a1c5e397ffb2af11705a255d80587736
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 */
4 #include <linux/bpf.h>
5 #include <linux/rcupdate.h>
6 #include <linux/random.h>
7 #include <linux/smp.h>
8 #include <linux/topology.h>
9 #include <linux/ktime.h>
10 #include <linux/sched.h>
11 #include <linux/uidgid.h>
12 #include <linux/filter.h>
13 #include <linux/ctype.h>
14 #include <linux/jiffies.h>
16 #include "../../lib/kstrtox.h"
18 /* If kernel subsystem is allowing eBPF programs to call this function,
19 * inside its own verifier_ops->get_func_proto() callback it should return
20 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
22 * Different map implementations will rely on rcu in map methods
23 * lookup/update/delete, therefore eBPF programs must run under rcu lock
24 * if program is allowed to access maps, so check rcu_read_lock_held in
25 * all three functions.
27 BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
29 WARN_ON_ONCE(!rcu_read_lock_held());
30 return (unsigned long) map->ops->map_lookup_elem(map, key);
33 const struct bpf_func_proto bpf_map_lookup_elem_proto = {
34 .func = bpf_map_lookup_elem,
35 .gpl_only = false,
36 .pkt_access = true,
37 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
38 .arg1_type = ARG_CONST_MAP_PTR,
39 .arg2_type = ARG_PTR_TO_MAP_KEY,
42 BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
43 void *, value, u64, flags)
45 WARN_ON_ONCE(!rcu_read_lock_held());
46 return map->ops->map_update_elem(map, key, value, flags);
49 const struct bpf_func_proto bpf_map_update_elem_proto = {
50 .func = bpf_map_update_elem,
51 .gpl_only = false,
52 .pkt_access = true,
53 .ret_type = RET_INTEGER,
54 .arg1_type = ARG_CONST_MAP_PTR,
55 .arg2_type = ARG_PTR_TO_MAP_KEY,
56 .arg3_type = ARG_PTR_TO_MAP_VALUE,
57 .arg4_type = ARG_ANYTHING,
60 BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
62 WARN_ON_ONCE(!rcu_read_lock_held());
63 return map->ops->map_delete_elem(map, key);
66 const struct bpf_func_proto bpf_map_delete_elem_proto = {
67 .func = bpf_map_delete_elem,
68 .gpl_only = false,
69 .pkt_access = true,
70 .ret_type = RET_INTEGER,
71 .arg1_type = ARG_CONST_MAP_PTR,
72 .arg2_type = ARG_PTR_TO_MAP_KEY,
75 BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags)
77 return map->ops->map_push_elem(map, value, flags);
80 const struct bpf_func_proto bpf_map_push_elem_proto = {
81 .func = bpf_map_push_elem,
82 .gpl_only = false,
83 .pkt_access = true,
84 .ret_type = RET_INTEGER,
85 .arg1_type = ARG_CONST_MAP_PTR,
86 .arg2_type = ARG_PTR_TO_MAP_VALUE,
87 .arg3_type = ARG_ANYTHING,
90 BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
92 return map->ops->map_pop_elem(map, value);
95 const struct bpf_func_proto bpf_map_pop_elem_proto = {
96 .func = bpf_map_pop_elem,
97 .gpl_only = false,
98 .ret_type = RET_INTEGER,
99 .arg1_type = ARG_CONST_MAP_PTR,
100 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
103 BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
105 return map->ops->map_peek_elem(map, value);
108 const struct bpf_func_proto bpf_map_peek_elem_proto = {
109 .func = bpf_map_pop_elem,
110 .gpl_only = false,
111 .ret_type = RET_INTEGER,
112 .arg1_type = ARG_CONST_MAP_PTR,
113 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
116 const struct bpf_func_proto bpf_get_prandom_u32_proto = {
117 .func = bpf_user_rnd_u32,
118 .gpl_only = false,
119 .ret_type = RET_INTEGER,
122 BPF_CALL_0(bpf_get_smp_processor_id)
124 return smp_processor_id();
127 const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
128 .func = bpf_get_smp_processor_id,
129 .gpl_only = false,
130 .ret_type = RET_INTEGER,
133 BPF_CALL_0(bpf_get_numa_node_id)
135 return numa_node_id();
138 const struct bpf_func_proto bpf_get_numa_node_id_proto = {
139 .func = bpf_get_numa_node_id,
140 .gpl_only = false,
141 .ret_type = RET_INTEGER,
144 BPF_CALL_0(bpf_ktime_get_ns)
146 /* NMI safe access to clock monotonic */
147 return ktime_get_mono_fast_ns();
150 const struct bpf_func_proto bpf_ktime_get_ns_proto = {
151 .func = bpf_ktime_get_ns,
152 .gpl_only = true,
153 .ret_type = RET_INTEGER,
156 BPF_CALL_0(bpf_get_current_pid_tgid)
158 struct task_struct *task = current;
160 if (unlikely(!task))
161 return -EINVAL;
163 return (u64) task->tgid << 32 | task->pid;
166 const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
167 .func = bpf_get_current_pid_tgid,
168 .gpl_only = false,
169 .ret_type = RET_INTEGER,
172 BPF_CALL_0(bpf_get_current_uid_gid)
174 struct task_struct *task = current;
175 kuid_t uid;
176 kgid_t gid;
178 if (unlikely(!task))
179 return -EINVAL;
181 current_uid_gid(&uid, &gid);
182 return (u64) from_kgid(&init_user_ns, gid) << 32 |
183 from_kuid(&init_user_ns, uid);
186 const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
187 .func = bpf_get_current_uid_gid,
188 .gpl_only = false,
189 .ret_type = RET_INTEGER,
192 BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
194 struct task_struct *task = current;
196 if (unlikely(!task))
197 goto err_clear;
199 strncpy(buf, task->comm, size);
201 /* Verifier guarantees that size > 0. For task->comm exceeding
202 * size, guarantee that buf is %NUL-terminated. Unconditionally
203 * done here to save the size test.
205 buf[size - 1] = 0;
206 return 0;
207 err_clear:
208 memset(buf, 0, size);
209 return -EINVAL;
212 const struct bpf_func_proto bpf_get_current_comm_proto = {
213 .func = bpf_get_current_comm,
214 .gpl_only = false,
215 .ret_type = RET_INTEGER,
216 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
217 .arg2_type = ARG_CONST_SIZE,
220 #if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK)
222 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
224 arch_spinlock_t *l = (void *)lock;
225 union {
226 __u32 val;
227 arch_spinlock_t lock;
228 } u = { .lock = __ARCH_SPIN_LOCK_UNLOCKED };
230 compiletime_assert(u.val == 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0");
231 BUILD_BUG_ON(sizeof(*l) != sizeof(__u32));
232 BUILD_BUG_ON(sizeof(*lock) != sizeof(__u32));
233 arch_spin_lock(l);
236 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
238 arch_spinlock_t *l = (void *)lock;
240 arch_spin_unlock(l);
243 #else
245 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
247 atomic_t *l = (void *)lock;
249 BUILD_BUG_ON(sizeof(*l) != sizeof(*lock));
250 do {
251 atomic_cond_read_relaxed(l, !VAL);
252 } while (atomic_xchg(l, 1));
255 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
257 atomic_t *l = (void *)lock;
259 atomic_set_release(l, 0);
262 #endif
264 static DEFINE_PER_CPU(unsigned long, irqsave_flags);
266 notrace BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock)
268 unsigned long flags;
270 local_irq_save(flags);
271 __bpf_spin_lock(lock);
272 __this_cpu_write(irqsave_flags, flags);
273 return 0;
276 const struct bpf_func_proto bpf_spin_lock_proto = {
277 .func = bpf_spin_lock,
278 .gpl_only = false,
279 .ret_type = RET_VOID,
280 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
283 notrace BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock)
285 unsigned long flags;
287 flags = __this_cpu_read(irqsave_flags);
288 __bpf_spin_unlock(lock);
289 local_irq_restore(flags);
290 return 0;
293 const struct bpf_func_proto bpf_spin_unlock_proto = {
294 .func = bpf_spin_unlock,
295 .gpl_only = false,
296 .ret_type = RET_VOID,
297 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
300 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
301 bool lock_src)
303 struct bpf_spin_lock *lock;
305 if (lock_src)
306 lock = src + map->spin_lock_off;
307 else
308 lock = dst + map->spin_lock_off;
309 preempt_disable();
310 ____bpf_spin_lock(lock);
311 copy_map_value(map, dst, src);
312 ____bpf_spin_unlock(lock);
313 preempt_enable();
316 BPF_CALL_0(bpf_jiffies64)
318 return get_jiffies_64();
321 const struct bpf_func_proto bpf_jiffies64_proto = {
322 .func = bpf_jiffies64,
323 .gpl_only = false,
324 .ret_type = RET_INTEGER,
327 #ifdef CONFIG_CGROUPS
328 BPF_CALL_0(bpf_get_current_cgroup_id)
330 struct cgroup *cgrp = task_dfl_cgroup(current);
332 return cgroup_id(cgrp);
335 const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
336 .func = bpf_get_current_cgroup_id,
337 .gpl_only = false,
338 .ret_type = RET_INTEGER,
341 #ifdef CONFIG_CGROUP_BPF
342 DECLARE_PER_CPU(struct bpf_cgroup_storage*,
343 bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
345 BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
347 /* flags argument is not used now,
348 * but provides an ability to extend the API.
349 * verifier checks that its value is correct.
351 enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
352 struct bpf_cgroup_storage *storage;
353 void *ptr;
355 storage = this_cpu_read(bpf_cgroup_storage[stype]);
357 if (stype == BPF_CGROUP_STORAGE_SHARED)
358 ptr = &READ_ONCE(storage->buf)->data[0];
359 else
360 ptr = this_cpu_ptr(storage->percpu_buf);
362 return (unsigned long)ptr;
365 const struct bpf_func_proto bpf_get_local_storage_proto = {
366 .func = bpf_get_local_storage,
367 .gpl_only = false,
368 .ret_type = RET_PTR_TO_MAP_VALUE,
369 .arg1_type = ARG_CONST_MAP_PTR,
370 .arg2_type = ARG_ANYTHING,
372 #endif
374 #define BPF_STRTOX_BASE_MASK 0x1F
376 static int __bpf_strtoull(const char *buf, size_t buf_len, u64 flags,
377 unsigned long long *res, bool *is_negative)
379 unsigned int base = flags & BPF_STRTOX_BASE_MASK;
380 const char *cur_buf = buf;
381 size_t cur_len = buf_len;
382 unsigned int consumed;
383 size_t val_len;
384 char str[64];
386 if (!buf || !buf_len || !res || !is_negative)
387 return -EINVAL;
389 if (base != 0 && base != 8 && base != 10 && base != 16)
390 return -EINVAL;
392 if (flags & ~BPF_STRTOX_BASE_MASK)
393 return -EINVAL;
395 while (cur_buf < buf + buf_len && isspace(*cur_buf))
396 ++cur_buf;
398 *is_negative = (cur_buf < buf + buf_len && *cur_buf == '-');
399 if (*is_negative)
400 ++cur_buf;
402 consumed = cur_buf - buf;
403 cur_len -= consumed;
404 if (!cur_len)
405 return -EINVAL;
407 cur_len = min(cur_len, sizeof(str) - 1);
408 memcpy(str, cur_buf, cur_len);
409 str[cur_len] = '\0';
410 cur_buf = str;
412 cur_buf = _parse_integer_fixup_radix(cur_buf, &base);
413 val_len = _parse_integer(cur_buf, base, res);
415 if (val_len & KSTRTOX_OVERFLOW)
416 return -ERANGE;
418 if (val_len == 0)
419 return -EINVAL;
421 cur_buf += val_len;
422 consumed += cur_buf - str;
424 return consumed;
427 static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags,
428 long long *res)
430 unsigned long long _res;
431 bool is_negative;
432 int err;
434 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
435 if (err < 0)
436 return err;
437 if (is_negative) {
438 if ((long long)-_res > 0)
439 return -ERANGE;
440 *res = -_res;
441 } else {
442 if ((long long)_res < 0)
443 return -ERANGE;
444 *res = _res;
446 return err;
449 BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,
450 long *, res)
452 long long _res;
453 int err;
455 err = __bpf_strtoll(buf, buf_len, flags, &_res);
456 if (err < 0)
457 return err;
458 if (_res != (long)_res)
459 return -ERANGE;
460 *res = _res;
461 return err;
464 const struct bpf_func_proto bpf_strtol_proto = {
465 .func = bpf_strtol,
466 .gpl_only = false,
467 .ret_type = RET_INTEGER,
468 .arg1_type = ARG_PTR_TO_MEM,
469 .arg2_type = ARG_CONST_SIZE,
470 .arg3_type = ARG_ANYTHING,
471 .arg4_type = ARG_PTR_TO_LONG,
474 BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,
475 unsigned long *, res)
477 unsigned long long _res;
478 bool is_negative;
479 int err;
481 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
482 if (err < 0)
483 return err;
484 if (is_negative)
485 return -EINVAL;
486 if (_res != (unsigned long)_res)
487 return -ERANGE;
488 *res = _res;
489 return err;
492 const struct bpf_func_proto bpf_strtoul_proto = {
493 .func = bpf_strtoul,
494 .gpl_only = false,
495 .ret_type = RET_INTEGER,
496 .arg1_type = ARG_PTR_TO_MEM,
497 .arg2_type = ARG_CONST_SIZE,
498 .arg3_type = ARG_ANYTHING,
499 .arg4_type = ARG_PTR_TO_LONG,
501 #endif