diff options
Diffstat (limited to 'kernel/bpf/inode.c')
-rw-r--r-- | kernel/bpf/inode.c | 156 |
1 files changed, 153 insertions, 3 deletions
diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c index bf6da59ae0d0..a41343009ccc 100644 --- a/kernel/bpf/inode.c +++ b/kernel/bpf/inode.c | |||
@@ -150,8 +150,154 @@ static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) | |||
150 | return 0; | 150 | return 0; |
151 | } | 151 | } |
152 | 152 | ||
153 | struct map_iter { | ||
154 | void *key; | ||
155 | bool done; | ||
156 | }; | ||
157 | |||
158 | static struct map_iter *map_iter(struct seq_file *m) | ||
159 | { | ||
160 | return m->private; | ||
161 | } | ||
162 | |||
163 | static struct bpf_map *seq_file_to_map(struct seq_file *m) | ||
164 | { | ||
165 | return file_inode(m->file)->i_private; | ||
166 | } | ||
167 | |||
168 | static void map_iter_free(struct map_iter *iter) | ||
169 | { | ||
170 | if (iter) { | ||
171 | kfree(iter->key); | ||
172 | kfree(iter); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | static struct map_iter *map_iter_alloc(struct bpf_map *map) | ||
177 | { | ||
178 | struct map_iter *iter; | ||
179 | |||
180 | iter = kzalloc(sizeof(*iter), GFP_KERNEL | __GFP_NOWARN); | ||
181 | if (!iter) | ||
182 | goto error; | ||
183 | |||
184 | iter->key = kzalloc(map->key_size, GFP_KERNEL | __GFP_NOWARN); | ||
185 | if (!iter->key) | ||
186 | goto error; | ||
187 | |||
188 | return iter; | ||
189 | |||
190 | error: | ||
191 | map_iter_free(iter); | ||
192 | return NULL; | ||
193 | } | ||
194 | |||
195 | static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos) | ||
196 | { | ||
197 | struct bpf_map *map = seq_file_to_map(m); | ||
198 | void *key = map_iter(m)->key; | ||
199 | |||
200 | if (map_iter(m)->done) | ||
201 | return NULL; | ||
202 | |||
203 | if (unlikely(v == SEQ_START_TOKEN)) | ||
204 | goto done; | ||
205 | |||
206 | if (map->ops->map_get_next_key(map, key, key)) { | ||
207 | map_iter(m)->done = true; | ||
208 | return NULL; | ||
209 | } | ||
210 | |||
211 | done: | ||
212 | ++(*pos); | ||
213 | return key; | ||
214 | } | ||
215 | |||
216 | static void *map_seq_start(struct seq_file *m, loff_t *pos) | ||
217 | { | ||
218 | if (map_iter(m)->done) | ||
219 | return NULL; | ||
220 | |||
221 | return *pos ? map_iter(m)->key : SEQ_START_TOKEN; | ||
222 | } | ||
223 | |||
224 | static void map_seq_stop(struct seq_file *m, void *v) | ||
225 | { | ||
226 | } | ||
227 | |||
228 | static int map_seq_show(struct seq_file *m, void *v) | ||
229 | { | ||
230 | struct bpf_map *map = seq_file_to_map(m); | ||
231 | void *key = map_iter(m)->key; | ||
232 | |||
233 | if (unlikely(v == SEQ_START_TOKEN)) { | ||
234 | seq_puts(m, "# WARNING!! The output is for debug purpose only\n"); | ||
235 | seq_puts(m, "# WARNING!! The output format will change\n"); | ||
236 | } else { | ||
237 | map->ops->map_seq_show_elem(map, key, m); | ||
238 | } | ||
239 | |||
240 | return 0; | ||
241 | } | ||
242 | |||
243 | static const struct seq_operations bpffs_map_seq_ops = { | ||
244 | .start = map_seq_start, | ||
245 | .next = map_seq_next, | ||
246 | .show = map_seq_show, | ||
247 | .stop = map_seq_stop, | ||
248 | }; | ||
249 | |||
250 | static int bpffs_map_open(struct inode *inode, struct file *file) | ||
251 | { | ||
252 | struct bpf_map *map = inode->i_private; | ||
253 | struct map_iter *iter; | ||
254 | struct seq_file *m; | ||
255 | int err; | ||
256 | |||
257 | iter = map_iter_alloc(map); | ||
258 | if (!iter) | ||
259 | return -ENOMEM; | ||
260 | |||
261 | err = seq_open(file, &bpffs_map_seq_ops); | ||
262 | if (err) { | ||
263 | map_iter_free(iter); | ||
264 | return err; | ||
265 | } | ||
266 | |||
267 | m = file->private_data; | ||
268 | m->private = iter; | ||
269 | |||
270 | return 0; | ||
271 | } | ||
272 | |||
273 | static int bpffs_map_release(struct inode *inode, struct file *file) | ||
274 | { | ||
275 | struct seq_file *m = file->private_data; | ||
276 | |||
277 | map_iter_free(map_iter(m)); | ||
278 | |||
279 | return seq_release(inode, file); | ||
280 | } | ||
281 | |||
282 | /* bpffs_map_fops should only implement the basic | ||
283 | * read operation for a BPF map. The purpose is to | ||
284 | * provide a simple user intuitive way to do | ||
285 | * "cat bpffs/pathto/a-pinned-map". | ||
286 | * | ||
287 | * Other operations (e.g. write, lookup...) should be realized by | ||
288 | * the userspace tools (e.g. bpftool) through the | ||
289 | * BPF_OBJ_GET_INFO_BY_FD and the map's lookup/update | ||
290 | * interface. | ||
291 | */ | ||
292 | static const struct file_operations bpffs_map_fops = { | ||
293 | .open = bpffs_map_open, | ||
294 | .read = seq_read, | ||
295 | .release = bpffs_map_release, | ||
296 | }; | ||
297 | |||
153 | static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw, | 298 | static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw, |
154 | const struct inode_operations *iops) | 299 | const struct inode_operations *iops, |
300 | const struct file_operations *fops) | ||
155 | { | 301 | { |
156 | struct inode *dir = dentry->d_parent->d_inode; | 302 | struct inode *dir = dentry->d_parent->d_inode; |
157 | struct inode *inode = bpf_get_inode(dir->i_sb, dir, mode); | 303 | struct inode *inode = bpf_get_inode(dir->i_sb, dir, mode); |
@@ -159,6 +305,7 @@ static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw, | |||
159 | return PTR_ERR(inode); | 305 | return PTR_ERR(inode); |
160 | 306 | ||
161 | inode->i_op = iops; | 307 | inode->i_op = iops; |
308 | inode->i_fop = fops; | ||
162 | inode->i_private = raw; | 309 | inode->i_private = raw; |
163 | 310 | ||
164 | bpf_dentry_finalize(dentry, inode, dir); | 311 | bpf_dentry_finalize(dentry, inode, dir); |
@@ -167,12 +314,15 @@ static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw, | |||
167 | 314 | ||
168 | static int bpf_mkprog(struct dentry *dentry, umode_t mode, void *arg) | 315 | static int bpf_mkprog(struct dentry *dentry, umode_t mode, void *arg) |
169 | { | 316 | { |
170 | return bpf_mkobj_ops(dentry, mode, arg, &bpf_prog_iops); | 317 | return bpf_mkobj_ops(dentry, mode, arg, &bpf_prog_iops, NULL); |
171 | } | 318 | } |
172 | 319 | ||
173 | static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg) | 320 | static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg) |
174 | { | 321 | { |
175 | return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops); | 322 | struct bpf_map *map = arg; |
323 | |||
324 | return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops, | ||
325 | map->btf ? &bpffs_map_fops : NULL); | ||
176 | } | 326 | } |
177 | 327 | ||
178 | static struct dentry * | 328 | static struct dentry * |