Blame view

kernel/bpf/queue_stack_maps.c 6.84 KB
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
1
2
3
4
5
6
7
8
9
  // SPDX-License-Identifier: GPL-2.0
  /*
   * queue_stack_maps.c: BPF queue and stack maps
   *
   * Copyright (c) 2018 Politecnico di Torino
   */
  #include <linux/bpf.h>
  #include <linux/list.h>
  #include <linux/slab.h>
813961de3   Alexei Starovoitov   bpf: fix integer ...
10
  #include <linux/capability.h>
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
11
12
13
  #include "percpu_freelist.h"
  
  #define QUEUE_STACK_CREATE_FLAG_MASK \
591fe9888   Daniel Borkmann   bpf: add program ...
14
  	(BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
15
16
17
18
19
20
  
  struct bpf_queue_stack {
  	struct bpf_map map;
  	raw_spinlock_t lock;
  	u32 head, tail;
  	u32 size; /* max_entries + 1 */
385bbf7b1   Gustavo A. R. Silva   bpf, libbpf: Repl...
21
  	char elements[] __aligned(8);
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  };
  
  static struct bpf_queue_stack *bpf_queue_stack(struct bpf_map *map)
  {
  	return container_of(map, struct bpf_queue_stack, map);
  }
  
  static bool queue_stack_map_is_empty(struct bpf_queue_stack *qs)
  {
  	return qs->head == qs->tail;
  }
  
  static bool queue_stack_map_is_full(struct bpf_queue_stack *qs)
  {
  	u32 head = qs->head + 1;
  
  	if (unlikely(head >= qs->size))
  		head = 0;
  
  	return head == qs->tail;
  }
  
  /* Called from syscall */
  static int queue_stack_map_alloc_check(union bpf_attr *attr)
  {
2c78ee898   Alexei Starovoitov   bpf: Implement CA...
47
  	if (!bpf_capable())
813961de3   Alexei Starovoitov   bpf: fix integer ...
48
  		return -EPERM;
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
49
50
  	/* check sanity of attributes */
  	if (attr->max_entries == 0 || attr->key_size != 0 ||
813961de3   Alexei Starovoitov   bpf: fix integer ...
51
  	    attr->value_size == 0 ||
591fe9888   Daniel Borkmann   bpf: add program ...
52
53
  	    attr->map_flags & ~QUEUE_STACK_CREATE_FLAG_MASK ||
  	    !bpf_map_flags_access_ok(attr->map_flags))
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  		return -EINVAL;
  
  	if (attr->value_size > KMALLOC_MAX_SIZE)
  		/* if value_size is bigger, the user space won't be able to
  		 * access the elements.
  		 */
  		return -E2BIG;
  
  	return 0;
  }
  
  static struct bpf_map *queue_stack_map_alloc(union bpf_attr *attr)
  {
  	int ret, numa_node = bpf_map_attr_numa_node(attr);
b936ca643   Roman Gushchin   bpf: rework memlo...
68
  	struct bpf_map_memory mem = {0};
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
69
  	struct bpf_queue_stack *qs;
813961de3   Alexei Starovoitov   bpf: fix integer ...
70
  	u64 size, queue_size, cost;
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
71

813961de3   Alexei Starovoitov   bpf: fix integer ...
72
73
  	size = (u64) attr->max_entries + 1;
  	cost = queue_size = sizeof(*qs) + size * attr->value_size;
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
74

b936ca643   Roman Gushchin   bpf: rework memlo...
75
  	ret = bpf_map_charge_init(&mem, cost);
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
76
77
78
79
  	if (ret < 0)
  		return ERR_PTR(ret);
  
  	qs = bpf_map_area_alloc(queue_size, numa_node);
b936ca643   Roman Gushchin   bpf: rework memlo...
80
81
  	if (!qs) {
  		bpf_map_charge_finish(&mem);
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
82
  		return ERR_PTR(-ENOMEM);
b936ca643   Roman Gushchin   bpf: rework memlo...
83
  	}
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
84
85
86
87
  
  	memset(qs, 0, sizeof(*qs));
  
  	bpf_map_init_from_attr(&qs->map, attr);
b936ca643   Roman Gushchin   bpf: rework memlo...
88
  	bpf_map_charge_move(&qs->map.memory, &mem);
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
89
90
91
92
93
94
95
96
97
98
99
  	qs->size = size;
  
  	raw_spin_lock_init(&qs->lock);
  
  	return &qs->map;
  }
  
  /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  static void queue_stack_map_free(struct bpf_map *map)
  {
  	struct bpf_queue_stack *qs = bpf_queue_stack(map);
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
100
101
102
103
104
105
106
107
108
109
110
111
112
  	bpf_map_area_free(qs);
  }
  
  static int __queue_map_get(struct bpf_map *map, void *value, bool delete)
  {
  	struct bpf_queue_stack *qs = bpf_queue_stack(map);
  	unsigned long flags;
  	int err = 0;
  	void *ptr;
  
  	raw_spin_lock_irqsave(&qs->lock, flags);
  
  	if (queue_stack_map_is_empty(qs)) {
d3f66e411   Daniel Borkmann   bpf: fix leaking ...
113
  		memset(value, 0, qs->map.value_size);
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  		err = -ENOENT;
  		goto out;
  	}
  
  	ptr = &qs->elements[qs->tail * qs->map.value_size];
  	memcpy(value, ptr, qs->map.value_size);
  
  	if (delete) {
  		if (unlikely(++qs->tail >= qs->size))
  			qs->tail = 0;
  	}
  
  out:
  	raw_spin_unlock_irqrestore(&qs->lock, flags);
  	return err;
  }
  
  
  static int __stack_map_get(struct bpf_map *map, void *value, bool delete)
  {
  	struct bpf_queue_stack *qs = bpf_queue_stack(map);
  	unsigned long flags;
  	int err = 0;
  	void *ptr;
  	u32 index;
  
  	raw_spin_lock_irqsave(&qs->lock, flags);
  
  	if (queue_stack_map_is_empty(qs)) {
d3f66e411   Daniel Borkmann   bpf: fix leaking ...
143
  		memset(value, 0, qs->map.value_size);
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  		err = -ENOENT;
  		goto out;
  	}
  
  	index = qs->head - 1;
  	if (unlikely(index >= qs->size))
  		index = qs->size - 1;
  
  	ptr = &qs->elements[index * qs->map.value_size];
  	memcpy(value, ptr, qs->map.value_size);
  
  	if (delete)
  		qs->head = index;
  
  out:
  	raw_spin_unlock_irqrestore(&qs->lock, flags);
  	return err;
  }
  
  /* Called from syscall or from eBPF program */
  static int queue_map_peek_elem(struct bpf_map *map, void *value)
  {
  	return __queue_map_get(map, value, false);
  }
  
  /* Called from syscall or from eBPF program */
  static int stack_map_peek_elem(struct bpf_map *map, void *value)
  {
  	return __stack_map_get(map, value, false);
  }
  
  /* Called from syscall or from eBPF program */
  static int queue_map_pop_elem(struct bpf_map *map, void *value)
  {
  	return __queue_map_get(map, value, true);
  }
  
  /* Called from syscall or from eBPF program */
  static int stack_map_pop_elem(struct bpf_map *map, void *value)
  {
  	return __stack_map_get(map, value, true);
  }
  
  /* Called from syscall or from eBPF program */
  static int queue_stack_map_push_elem(struct bpf_map *map, void *value,
  				     u64 flags)
  {
  	struct bpf_queue_stack *qs = bpf_queue_stack(map);
  	unsigned long irq_flags;
  	int err = 0;
  	void *dst;
  
  	/* BPF_EXIST is used to force making room for a new element in case the
  	 * map is full
  	 */
  	bool replace = (flags & BPF_EXIST);
  
  	/* Check supported flags for queue and stack maps */
  	if (flags & BPF_NOEXIST || flags > BPF_EXIST)
  		return -EINVAL;
  
  	raw_spin_lock_irqsave(&qs->lock, irq_flags);
  
  	if (queue_stack_map_is_full(qs)) {
  		if (!replace) {
  			err = -E2BIG;
  			goto out;
  		}
  		/* advance tail pointer to overwrite oldest element */
  		if (unlikely(++qs->tail >= qs->size))
  			qs->tail = 0;
  	}
  
  	dst = &qs->elements[qs->head * qs->map.value_size];
  	memcpy(dst, value, qs->map.value_size);
  
  	if (unlikely(++qs->head >= qs->size))
  		qs->head = 0;
  
  out:
  	raw_spin_unlock_irqrestore(&qs->lock, irq_flags);
  	return err;
  }
  
  /* Called from syscall or from eBPF program */
  static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
  {
  	return NULL;
  }
  
  /* Called from syscall or from eBPF program */
  static int queue_stack_map_update_elem(struct bpf_map *map, void *key,
  				       void *value, u64 flags)
  {
  	return -EINVAL;
  }
  
  /* Called from syscall or from eBPF program */
  static int queue_stack_map_delete_elem(struct bpf_map *map, void *key)
  {
  	return -EINVAL;
  }
  
  /* Called from syscall */
  static int queue_stack_map_get_next_key(struct bpf_map *map, void *key,
  					void *next_key)
  {
  	return -EINVAL;
  }
2872e9ac3   Andrey Ignatov   bpf: Set map_btf_...
253
  static int queue_map_btf_id;
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
254
  const struct bpf_map_ops queue_map_ops = {
f4d052592   Martin KaFai Lau   bpf: Add map_meta...
255
  	.map_meta_equal = bpf_map_meta_equal,
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
256
257
258
259
260
261
262
263
264
265
  	.map_alloc_check = queue_stack_map_alloc_check,
  	.map_alloc = queue_stack_map_alloc,
  	.map_free = queue_stack_map_free,
  	.map_lookup_elem = queue_stack_map_lookup_elem,
  	.map_update_elem = queue_stack_map_update_elem,
  	.map_delete_elem = queue_stack_map_delete_elem,
  	.map_push_elem = queue_stack_map_push_elem,
  	.map_pop_elem = queue_map_pop_elem,
  	.map_peek_elem = queue_map_peek_elem,
  	.map_get_next_key = queue_stack_map_get_next_key,
2872e9ac3   Andrey Ignatov   bpf: Set map_btf_...
266
267
  	.map_btf_name = "bpf_queue_stack",
  	.map_btf_id = &queue_map_btf_id,
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
268
  };
2872e9ac3   Andrey Ignatov   bpf: Set map_btf_...
269
  static int stack_map_btf_id;
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
270
  const struct bpf_map_ops stack_map_ops = {
f4d052592   Martin KaFai Lau   bpf: Add map_meta...
271
  	.map_meta_equal = bpf_map_meta_equal,
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
272
273
274
275
276
277
278
279
280
281
  	.map_alloc_check = queue_stack_map_alloc_check,
  	.map_alloc = queue_stack_map_alloc,
  	.map_free = queue_stack_map_free,
  	.map_lookup_elem = queue_stack_map_lookup_elem,
  	.map_update_elem = queue_stack_map_update_elem,
  	.map_delete_elem = queue_stack_map_delete_elem,
  	.map_push_elem = queue_stack_map_push_elem,
  	.map_pop_elem = stack_map_pop_elem,
  	.map_peek_elem = stack_map_peek_elem,
  	.map_get_next_key = queue_stack_map_get_next_key,
2872e9ac3   Andrey Ignatov   bpf: Set map_btf_...
282
283
  	.map_btf_name = "bpf_queue_stack",
  	.map_btf_id = &stack_map_btf_id,
f1a2e44a3   Mauricio Vasquez B   bpf: add queue an...
284
  };