mm: migrate: hugetlb: putback destination hugepage to active list
[linux/fpc-iii.git] / kernel / bpf / helpers.c
blob1447ec09421ebc4905c922f4f4fa2b65b52b474a
1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 #include <linux/bpf.h>
13 #include <linux/rcupdate.h>
14 #include <linux/random.h>
15 #include <linux/smp.h>
16 #include <linux/ktime.h>
17 #include <linux/sched.h>
18 #include <linux/uidgid.h>
20 /* If kernel subsystem is allowing eBPF programs to call this function,
21 * inside its own verifier_ops->get_func_proto() callback it should return
22 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
24 * Different map implementations will rely on rcu in map methods
25 * lookup/update/delete, therefore eBPF programs must run under rcu lock
26 * if program is allowed to access maps, so check rcu_read_lock_held in
27 * all three functions.
29 static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
31 /* verifier checked that R1 contains a valid pointer to bpf_map
32 * and R2 points to a program stack and map->key_size bytes were
33 * initialized
35 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
36 void *key = (void *) (unsigned long) r2;
37 void *value;
39 WARN_ON_ONCE(!rcu_read_lock_held());
41 value = map->ops->map_lookup_elem(map, key);
43 /* lookup() returns either pointer to element value or NULL
44 * which is the meaning of PTR_TO_MAP_VALUE_OR_NULL type
46 return (unsigned long) value;
49 const struct bpf_func_proto bpf_map_lookup_elem_proto = {
50 .func = bpf_map_lookup_elem,
51 .gpl_only = false,
52 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
53 .arg1_type = ARG_CONST_MAP_PTR,
54 .arg2_type = ARG_PTR_TO_MAP_KEY,
57 static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
59 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
60 void *key = (void *) (unsigned long) r2;
61 void *value = (void *) (unsigned long) r3;
63 WARN_ON_ONCE(!rcu_read_lock_held());
65 return map->ops->map_update_elem(map, key, value, r4);
68 const struct bpf_func_proto bpf_map_update_elem_proto = {
69 .func = bpf_map_update_elem,
70 .gpl_only = false,
71 .ret_type = RET_INTEGER,
72 .arg1_type = ARG_CONST_MAP_PTR,
73 .arg2_type = ARG_PTR_TO_MAP_KEY,
74 .arg3_type = ARG_PTR_TO_MAP_VALUE,
75 .arg4_type = ARG_ANYTHING,
78 static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
80 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
81 void *key = (void *) (unsigned long) r2;
83 WARN_ON_ONCE(!rcu_read_lock_held());
85 return map->ops->map_delete_elem(map, key);
88 const struct bpf_func_proto bpf_map_delete_elem_proto = {
89 .func = bpf_map_delete_elem,
90 .gpl_only = false,
91 .ret_type = RET_INTEGER,
92 .arg1_type = ARG_CONST_MAP_PTR,
93 .arg2_type = ARG_PTR_TO_MAP_KEY,
96 static u64 bpf_get_prandom_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
98 return prandom_u32();
101 const struct bpf_func_proto bpf_get_prandom_u32_proto = {
102 .func = bpf_get_prandom_u32,
103 .gpl_only = false,
104 .ret_type = RET_INTEGER,
107 static u64 bpf_get_smp_processor_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
109 return raw_smp_processor_id();
112 const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
113 .func = bpf_get_smp_processor_id,
114 .gpl_only = false,
115 .ret_type = RET_INTEGER,
118 static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
120 /* NMI safe access to clock monotonic */
121 return ktime_get_mono_fast_ns();
124 const struct bpf_func_proto bpf_ktime_get_ns_proto = {
125 .func = bpf_ktime_get_ns,
126 .gpl_only = true,
127 .ret_type = RET_INTEGER,
130 static u64 bpf_get_current_pid_tgid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
132 struct task_struct *task = current;
134 if (!task)
135 return -EINVAL;
137 return (u64) task->tgid << 32 | task->pid;
140 const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
141 .func = bpf_get_current_pid_tgid,
142 .gpl_only = false,
143 .ret_type = RET_INTEGER,
146 static u64 bpf_get_current_uid_gid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
148 struct task_struct *task = current;
149 kuid_t uid;
150 kgid_t gid;
152 if (!task)
153 return -EINVAL;
155 current_uid_gid(&uid, &gid);
156 return (u64) from_kgid(&init_user_ns, gid) << 32 |
157 from_kuid(&init_user_ns, uid);
160 const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
161 .func = bpf_get_current_uid_gid,
162 .gpl_only = false,
163 .ret_type = RET_INTEGER,
166 static u64 bpf_get_current_comm(u64 r1, u64 size, u64 r3, u64 r4, u64 r5)
168 struct task_struct *task = current;
169 char *buf = (char *) (long) r1;
171 if (!task)
172 return -EINVAL;
174 memcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));
175 return 0;
178 const struct bpf_func_proto bpf_get_current_comm_proto = {
179 .func = bpf_get_current_comm,
180 .gpl_only = false,
181 .ret_type = RET_INTEGER,
182 .arg1_type = ARG_PTR_TO_STACK,
183 .arg2_type = ARG_CONST_STACK_SIZE,