Linux Audio

Check our new training course

Loading...
v4.17
 
  1/* Copyright (c) 2017 Facebook
  2 *
  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.
  6 */
  7#include <linux/slab.h>
  8#include <linux/bpf.h>
  9
 10#include "map_in_map.h"
 11
 12struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
 13{
 14	struct bpf_map *inner_map, *inner_map_meta;
 
 15	struct fd f;
 16
 17	f = fdget(inner_map_ufd);
 18	inner_map = __bpf_map_get(f);
 19	if (IS_ERR(inner_map))
 20		return inner_map;
 21
 22	/* prog_array->owner_prog_type and owner_jited
 23	 * is a runtime binding.  Doing static check alone
 24	 * in the verifier is not enough.
 25	 */
 26	if (inner_map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
 
 
 27		fdput(f);
 28		return ERR_PTR(-ENOTSUPP);
 29	}
 30
 31	/* Does not support >1 level map-in-map */
 32	if (inner_map->inner_map_meta) {
 33		fdput(f);
 34		return ERR_PTR(-EINVAL);
 35	}
 36
 37	inner_map_meta = kzalloc(sizeof(*inner_map_meta), GFP_USER);
 
 
 
 
 
 38	if (!inner_map_meta) {
 39		fdput(f);
 40		return ERR_PTR(-ENOMEM);
 41	}
 42
 43	inner_map_meta->map_type = inner_map->map_type;
 44	inner_map_meta->key_size = inner_map->key_size;
 45	inner_map_meta->value_size = inner_map->value_size;
 46	inner_map_meta->map_flags = inner_map->map_flags;
 47	inner_map_meta->ops = inner_map->ops;
 48	inner_map_meta->max_entries = inner_map->max_entries;
 
 
 
 
 
 
 
 
 
 49
 50	fdput(f);
 51	return inner_map_meta;
 52}
 53
 54void bpf_map_meta_free(struct bpf_map *map_meta)
 55{
 56	kfree(map_meta);
 57}
 58
 59bool bpf_map_meta_equal(const struct bpf_map *meta0,
 60			const struct bpf_map *meta1)
 61{
 62	/* No need to compare ops because it is covered by map_type */
 63	return meta0->map_type == meta1->map_type &&
 64		meta0->key_size == meta1->key_size &&
 65		meta0->value_size == meta1->value_size &&
 66		meta0->map_flags == meta1->map_flags &&
 67		meta0->max_entries == meta1->max_entries;
 68}
 69
 70void *bpf_map_fd_get_ptr(struct bpf_map *map,
 71			 struct file *map_file /* not used */,
 72			 int ufd)
 73{
 74	struct bpf_map *inner_map;
 75	struct fd f;
 76
 77	f = fdget(ufd);
 78	inner_map = __bpf_map_get(f);
 79	if (IS_ERR(inner_map))
 80		return inner_map;
 81
 82	if (bpf_map_meta_equal(map->inner_map_meta, inner_map))
 83		inner_map = bpf_map_inc(inner_map, false);
 
 84	else
 85		inner_map = ERR_PTR(-EINVAL);
 86
 87	fdput(f);
 88	return inner_map;
 89}
 90
 91void bpf_map_fd_put_ptr(void *ptr)
 92{
 93	/* ptr->ops->map_free() has to go through one
 94	 * rcu grace period by itself.
 95	 */
 96	bpf_map_put(ptr);
 97}
 98
 99u32 bpf_map_fd_sys_lookup_elem(void *ptr)
100{
101	return ((struct bpf_map *)ptr)->id;
102}
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* Copyright (c) 2017 Facebook
 
 
 
 
  3 */
  4#include <linux/slab.h>
  5#include <linux/bpf.h>
  6
  7#include "map_in_map.h"
  8
  9struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
 10{
 11	struct bpf_map *inner_map, *inner_map_meta;
 12	u32 inner_map_meta_size;
 13	struct fd f;
 14
 15	f = fdget(inner_map_ufd);
 16	inner_map = __bpf_map_get(f);
 17	if (IS_ERR(inner_map))
 18		return inner_map;
 19
 20	/* Does not support >1 level map-in-map */
 21	if (inner_map->inner_map_meta) {
 22		fdput(f);
 23		return ERR_PTR(-EINVAL);
 24	}
 25
 26	if (!inner_map->ops->map_meta_equal) {
 27		fdput(f);
 28		return ERR_PTR(-ENOTSUPP);
 29	}
 30
 31	if (map_value_has_spin_lock(inner_map)) {
 
 32		fdput(f);
 33		return ERR_PTR(-ENOTSUPP);
 34	}
 35
 36	inner_map_meta_size = sizeof(*inner_map_meta);
 37	/* In some cases verifier needs to access beyond just base map. */
 38	if (inner_map->ops == &array_map_ops)
 39		inner_map_meta_size = sizeof(struct bpf_array);
 40
 41	inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
 42	if (!inner_map_meta) {
 43		fdput(f);
 44		return ERR_PTR(-ENOMEM);
 45	}
 46
 47	inner_map_meta->map_type = inner_map->map_type;
 48	inner_map_meta->key_size = inner_map->key_size;
 49	inner_map_meta->value_size = inner_map->value_size;
 50	inner_map_meta->map_flags = inner_map->map_flags;
 
 51	inner_map_meta->max_entries = inner_map->max_entries;
 52	inner_map_meta->spin_lock_off = inner_map->spin_lock_off;
 53
 54	/* Misc members not needed in bpf_map_meta_equal() check. */
 55	inner_map_meta->ops = inner_map->ops;
 56	if (inner_map->ops == &array_map_ops) {
 57		inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
 58		container_of(inner_map_meta, struct bpf_array, map)->index_mask =
 59		     container_of(inner_map, struct bpf_array, map)->index_mask;
 60	}
 61
 62	fdput(f);
 63	return inner_map_meta;
 64}
 65
 66void bpf_map_meta_free(struct bpf_map *map_meta)
 67{
 68	kfree(map_meta);
 69}
 70
 71bool bpf_map_meta_equal(const struct bpf_map *meta0,
 72			const struct bpf_map *meta1)
 73{
 74	/* No need to compare ops because it is covered by map_type */
 75	return meta0->map_type == meta1->map_type &&
 76		meta0->key_size == meta1->key_size &&
 77		meta0->value_size == meta1->value_size &&
 78		meta0->map_flags == meta1->map_flags;
 
 79}
 80
 81void *bpf_map_fd_get_ptr(struct bpf_map *map,
 82			 struct file *map_file /* not used */,
 83			 int ufd)
 84{
 85	struct bpf_map *inner_map, *inner_map_meta;
 86	struct fd f;
 87
 88	f = fdget(ufd);
 89	inner_map = __bpf_map_get(f);
 90	if (IS_ERR(inner_map))
 91		return inner_map;
 92
 93	inner_map_meta = map->inner_map_meta;
 94	if (inner_map_meta->ops->map_meta_equal(inner_map_meta, inner_map))
 95		bpf_map_inc(inner_map);
 96	else
 97		inner_map = ERR_PTR(-EINVAL);
 98
 99	fdput(f);
100	return inner_map;
101}
102
103void bpf_map_fd_put_ptr(void *ptr)
104{
105	/* ptr->ops->map_free() has to go through one
106	 * rcu grace period by itself.
107	 */
108	bpf_map_put(ptr);
109}
110
111u32 bpf_map_fd_sys_lookup_elem(void *ptr)
112{
113	return ((struct bpf_map *)ptr)->id;
114}