1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
3 */
4 #include <linux/bpf.h>
5 #include <linux/jhash.h>
6 #include <linux/filter.h>
7 #include <linux/kernel.h>
8 #include <linux/stacktrace.h>
9 #include <linux/perf_event.h>
10 #include <linux/elf.h>
11 #include <linux/pagemap.h>
12 #include <linux/irq_work.h>
13 #include <linux/btf_ids.h>
14 #include "percpu_freelist.h"
15
16 #define STACK_CREATE_FLAG_MASK \
17 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY | \
18 BPF_F_STACK_BUILD_ID)
19
20 struct stack_map_bucket {
21 struct pcpu_freelist_node fnode;
22 u32 hash;
23 u32 nr;
24 u64 data[];
25 };
26
27 struct bpf_stack_map {
28 struct bpf_map map;
29 void *elems;
30 struct pcpu_freelist freelist;
31 u32 n_buckets;
32 struct stack_map_bucket *buckets[];
33 };
34
35 /* irq_work to run up_read() for build_id lookup in nmi context */
36 struct stack_map_irq_work {
37 struct irq_work irq_work;
38 struct mm_struct *mm;
39 };
40
do_up_read(struct irq_work * entry)41 static void do_up_read(struct irq_work *entry)
42 {
43 struct stack_map_irq_work *work;
44
45 if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
46 return;
47
48 work = container_of(entry, struct stack_map_irq_work, irq_work);
49 mmap_read_unlock_non_owner(work->mm);
50 }
51
52 static DEFINE_PER_CPU(struct stack_map_irq_work, up_read_work);
53
stack_map_use_build_id(struct bpf_map * map)54 static inline bool stack_map_use_build_id(struct bpf_map *map)
55 {
56 return (map->map_flags & BPF_F_STACK_BUILD_ID);
57 }
58
stack_map_data_size(struct bpf_map * map)59 static inline int stack_map_data_size(struct bpf_map *map)
60 {
61 return stack_map_use_build_id(map) ?
62 sizeof(struct bpf_stack_build_id) : sizeof(u64);
63 }
64
prealloc_elems_and_freelist(struct bpf_stack_map * smap)65 static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
66 {
67 u64 elem_size = sizeof(struct stack_map_bucket) +
68 (u64)smap->map.value_size;
69 int err;
70
71 smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
72 smap->map.numa_node);
73 if (!smap->elems)
74 return -ENOMEM;
75
76 err = pcpu_freelist_init(&smap->freelist);
77 if (err)
78 goto free_elems;
79
80 pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
81 smap->map.max_entries);
82 return 0;
83
84 free_elems:
85 bpf_map_area_free(smap->elems);
86 return err;
87 }
88
89 /* Called from syscall */
stack_map_alloc(union bpf_attr * attr)90 static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
91 {
92 u32 value_size = attr->value_size;
93 struct bpf_stack_map *smap;
94 struct bpf_map_memory mem;
95 u64 cost, n_buckets;
96 int err;
97
98 if (!bpf_capable())
99 return ERR_PTR(-EPERM);
100
101 if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
102 return ERR_PTR(-EINVAL);
103
104 /* check sanity of attributes */
105 if (attr->max_entries == 0 || attr->key_size != 4 ||
106 value_size < 8 || value_size % 8)
107 return ERR_PTR(-EINVAL);
108
109 BUILD_BUG_ON(sizeof(struct bpf_stack_build_id) % sizeof(u64));
110 if (attr->map_flags & BPF_F_STACK_BUILD_ID) {
111 if (value_size % sizeof(struct bpf_stack_build_id) ||
112 value_size / sizeof(struct bpf_stack_build_id)
113 > sysctl_perf_event_max_stack)
114 return ERR_PTR(-EINVAL);
115 } else if (value_size / 8 > sysctl_perf_event_max_stack)
116 return ERR_PTR(-EINVAL);
117
118 /* hash table size must be power of 2 */
119 n_buckets = roundup_pow_of_two(attr->max_entries);
120 if (!n_buckets)
121 return ERR_PTR(-E2BIG);
122
123 cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
124 cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
125 err = bpf_map_charge_init(&mem, cost);
126 if (err)
127 return ERR_PTR(err);
128
129 smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
130 if (!smap) {
131 bpf_map_charge_finish(&mem);
132 return ERR_PTR(-ENOMEM);
133 }
134
135 bpf_map_init_from_attr(&smap->map, attr);
136 smap->map.value_size = value_size;
137 smap->n_buckets = n_buckets;
138
139 err = get_callchain_buffers(sysctl_perf_event_max_stack);
140 if (err)
141 goto free_charge;
142
143 err = prealloc_elems_and_freelist(smap);
144 if (err)
145 goto put_buffers;
146
147 bpf_map_charge_move(&smap->map.memory, &mem);
148
149 return &smap->map;
150
151 put_buffers:
152 put_callchain_buffers();
153 free_charge:
154 bpf_map_charge_finish(&mem);
155 bpf_map_area_free(smap);
156 return ERR_PTR(err);
157 }
158
159 #define BPF_BUILD_ID 3
160 /*
161 * Parse build id from the note segment. This logic can be shared between
162 * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
163 * identical.
164 */
stack_map_parse_build_id(void * page_addr,unsigned char * build_id,void * note_start,Elf32_Word note_size)165 static inline int stack_map_parse_build_id(void *page_addr,
166 unsigned char *build_id,
167 void *note_start,
168 Elf32_Word note_size)
169 {
170 Elf32_Word note_offs = 0, new_offs;
171
172 /* check for overflow */
173 if (note_start < page_addr || note_start + note_size < note_start)
174 return -EINVAL;
175
176 /* only supports note that fits in the first page */
177 if (note_start + note_size > page_addr + PAGE_SIZE)
178 return -EINVAL;
179
180 while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
181 Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
182
183 if (nhdr->n_type == BPF_BUILD_ID &&
184 nhdr->n_namesz == sizeof("GNU") &&
185 nhdr->n_descsz > 0 &&
186 nhdr->n_descsz <= BPF_BUILD_ID_SIZE) {
187 memcpy(build_id,
188 note_start + note_offs +
189 ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr),
190 nhdr->n_descsz);
191 memset(build_id + nhdr->n_descsz, 0,
192 BPF_BUILD_ID_SIZE - nhdr->n_descsz);
193 return 0;
194 }
195 new_offs = note_offs + sizeof(Elf32_Nhdr) +
196 ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4);
197 if (new_offs <= note_offs) /* overflow */
198 break;
199 note_offs = new_offs;
200 }
201 return -EINVAL;
202 }
203
204 /* Parse build ID from 32-bit ELF */
stack_map_get_build_id_32(void * page_addr,unsigned char * build_id)205 static int stack_map_get_build_id_32(void *page_addr,
206 unsigned char *build_id)
207 {
208 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
209 Elf32_Phdr *phdr;
210 int i;
211
212 /* only supports phdr that fits in one page */
213 if (ehdr->e_phnum >
214 (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
215 return -EINVAL;
216
217 phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
218
219 for (i = 0; i < ehdr->e_phnum; ++i) {
220 if (phdr[i].p_type == PT_NOTE &&
221 !stack_map_parse_build_id(page_addr, build_id,
222 page_addr + phdr[i].p_offset,
223 phdr[i].p_filesz))
224 return 0;
225 }
226 return -EINVAL;
227 }
228
229 /* Parse build ID from 64-bit ELF */
stack_map_get_build_id_64(void * page_addr,unsigned char * build_id)230 static int stack_map_get_build_id_64(void *page_addr,
231 unsigned char *build_id)
232 {
233 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
234 Elf64_Phdr *phdr;
235 int i;
236
237 /* only supports phdr that fits in one page */
238 if (ehdr->e_phnum >
239 (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
240 return -EINVAL;
241
242 phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
243
244 for (i = 0; i < ehdr->e_phnum; ++i) {
245 if (phdr[i].p_type == PT_NOTE &&
246 !stack_map_parse_build_id(page_addr, build_id,
247 page_addr + phdr[i].p_offset,
248 phdr[i].p_filesz))
249 return 0;
250 }
251 return -EINVAL;
252 }
253
254 /* Parse build ID of ELF file mapped to vma */
stack_map_get_build_id(struct vm_area_struct * vma,unsigned char * build_id)255 static int stack_map_get_build_id(struct vm_area_struct *vma,
256 unsigned char *build_id)
257 {
258 Elf32_Ehdr *ehdr;
259 struct page *page;
260 void *page_addr;
261 int ret;
262
263 /* only works for page backed storage */
264 if (!vma->vm_file)
265 return -EINVAL;
266
267 page = find_get_page(vma->vm_file->f_mapping, 0);
268 if (!page)
269 return -EFAULT; /* page not mapped */
270
271 ret = -EINVAL;
272 page_addr = kmap_atomic(page);
273 ehdr = (Elf32_Ehdr *)page_addr;
274
275 /* compare magic x7f "ELF" */
276 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
277 goto out;
278
279 /* only support executable file and shared object file */
280 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
281 goto out;
282
283 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
284 ret = stack_map_get_build_id_32(page_addr, build_id);
285 else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
286 ret = stack_map_get_build_id_64(page_addr, build_id);
287 out:
288 kunmap_atomic(page_addr);
289 put_page(page);
290 return ret;
291 }
292
stack_map_get_build_id_offset(struct bpf_stack_build_id * id_offs,u64 * ips,u32 trace_nr,bool user)293 static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
294 u64 *ips, u32 trace_nr, bool user)
295 {
296 int i;
297 struct vm_area_struct *vma;
298 bool irq_work_busy = false;
299 struct stack_map_irq_work *work = NULL;
300
301 if (irqs_disabled()) {
302 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
303 work = this_cpu_ptr(&up_read_work);
304 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY) {
305 /* cannot queue more up_read, fallback */
306 irq_work_busy = true;
307 }
308 } else {
309 /*
310 * PREEMPT_RT does not allow to trylock mmap sem in
311 * interrupt disabled context. Force the fallback code.
312 */
313 irq_work_busy = true;
314 }
315 }
316
317 /*
318 * We cannot do up_read() when the irq is disabled, because of
319 * risk to deadlock with rq_lock. To do build_id lookup when the
320 * irqs are disabled, we need to run up_read() in irq_work. We use
321 * a percpu variable to do the irq_work. If the irq_work is
322 * already used by another lookup, we fall back to report ips.
323 *
324 * Same fallback is used for kernel stack (!user) on a stackmap
325 * with build_id.
326 */
327 if (!user || !current || !current->mm || irq_work_busy ||
328 !mmap_read_trylock_non_owner(current->mm)) {
329 /* cannot access current->mm, fall back to ips */
330 for (i = 0; i < trace_nr; i++) {
331 id_offs[i].status = BPF_STACK_BUILD_ID_IP;
332 id_offs[i].ip = ips[i];
333 memset(id_offs[i].build_id, 0, BPF_BUILD_ID_SIZE);
334 }
335 return;
336 }
337
338 for (i = 0; i < trace_nr; i++) {
339 vma = find_vma(current->mm, ips[i]);
340 if (!vma || stack_map_get_build_id(vma, id_offs[i].build_id)) {
341 /* per entry fall back to ips */
342 id_offs[i].status = BPF_STACK_BUILD_ID_IP;
343 id_offs[i].ip = ips[i];
344 memset(id_offs[i].build_id, 0, BPF_BUILD_ID_SIZE);
345 continue;
346 }
347 id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
348 - vma->vm_start;
349 id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
350 }
351
352 if (!work) {
353 mmap_read_unlock_non_owner(current->mm);
354 } else {
355 work->mm = current->mm;
356 irq_work_queue(&work->irq_work);
357 }
358 }
359
360 static struct perf_callchain_entry *
get_callchain_entry_for_task(struct task_struct * task,u32 init_nr)361 get_callchain_entry_for_task(struct task_struct *task, u32 init_nr)
362 {
363 #ifdef CONFIG_STACKTRACE
364 struct perf_callchain_entry *entry;
365 int rctx;
366
367 entry = get_callchain_entry(&rctx);
368
369 if (!entry)
370 return NULL;
371
372 entry->nr = init_nr +
373 stack_trace_save_tsk(task, (unsigned long *)(entry->ip + init_nr),
374 sysctl_perf_event_max_stack - init_nr, 0);
375
376 /* stack_trace_save_tsk() works on unsigned long array, while
377 * perf_callchain_entry uses u64 array. For 32-bit systems, it is
378 * necessary to fix this mismatch.
379 */
380 if (__BITS_PER_LONG != 64) {
381 unsigned long *from = (unsigned long *) entry->ip;
382 u64 *to = entry->ip;
383 int i;
384
385 /* copy data from the end to avoid using extra buffer */
386 for (i = entry->nr - 1; i >= (int)init_nr; i--)
387 to[i] = (u64)(from[i]);
388 }
389
390 put_callchain_entry(rctx);
391
392 return entry;
393 #else /* CONFIG_STACKTRACE */
394 return NULL;
395 #endif
396 }
397
__bpf_get_stackid(struct bpf_map * map,struct perf_callchain_entry * trace,u64 flags)398 static long __bpf_get_stackid(struct bpf_map *map,
399 struct perf_callchain_entry *trace, u64 flags)
400 {
401 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
402 struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
403 u32 max_depth = map->value_size / stack_map_data_size(map);
404 /* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
405 u32 init_nr = sysctl_perf_event_max_stack - max_depth;
406 u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
407 u32 hash, id, trace_nr, trace_len;
408 bool user = flags & BPF_F_USER_STACK;
409 u64 *ips;
410 bool hash_matches;
411
412 /* get_perf_callchain() guarantees that trace->nr >= init_nr
413 * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
414 */
415 trace_nr = trace->nr - init_nr;
416
417 if (trace_nr <= skip)
418 /* skipping more than usable stack trace */
419 return -EFAULT;
420
421 trace_nr -= skip;
422 trace_len = trace_nr * sizeof(u64);
423 ips = trace->ip + skip + init_nr;
424 hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
425 id = hash & (smap->n_buckets - 1);
426 bucket = READ_ONCE(smap->buckets[id]);
427
428 hash_matches = bucket && bucket->hash == hash;
429 /* fast cmp */
430 if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
431 return id;
432
433 if (stack_map_use_build_id(map)) {
434 /* for build_id+offset, pop a bucket before slow cmp */
435 new_bucket = (struct stack_map_bucket *)
436 pcpu_freelist_pop(&smap->freelist);
437 if (unlikely(!new_bucket))
438 return -ENOMEM;
439 new_bucket->nr = trace_nr;
440 stack_map_get_build_id_offset(
441 (struct bpf_stack_build_id *)new_bucket->data,
442 ips, trace_nr, user);
443 trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
444 if (hash_matches && bucket->nr == trace_nr &&
445 memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
446 pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
447 return id;
448 }
449 if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
450 pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
451 return -EEXIST;
452 }
453 } else {
454 if (hash_matches && bucket->nr == trace_nr &&
455 memcmp(bucket->data, ips, trace_len) == 0)
456 return id;
457 if (bucket && !(flags & BPF_F_REUSE_STACKID))
458 return -EEXIST;
459
460 new_bucket = (struct stack_map_bucket *)
461 pcpu_freelist_pop(&smap->freelist);
462 if (unlikely(!new_bucket))
463 return -ENOMEM;
464 memcpy(new_bucket->data, ips, trace_len);
465 }
466
467 new_bucket->hash = hash;
468 new_bucket->nr = trace_nr;
469
470 old_bucket = xchg(&smap->buckets[id], new_bucket);
471 if (old_bucket)
472 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
473 return id;
474 }
475
BPF_CALL_3(bpf_get_stackid,struct pt_regs *,regs,struct bpf_map *,map,u64,flags)476 BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
477 u64, flags)
478 {
479 u32 max_depth = map->value_size / stack_map_data_size(map);
480 /* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
481 u32 init_nr = sysctl_perf_event_max_stack - max_depth;
482 bool user = flags & BPF_F_USER_STACK;
483 struct perf_callchain_entry *trace;
484 bool kernel = !user;
485
486 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
487 BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
488 return -EINVAL;
489
490 trace = get_perf_callchain(regs, init_nr, kernel, user,
491 sysctl_perf_event_max_stack, false, false);
492
493 if (unlikely(!trace))
494 /* couldn't fetch the stack trace */
495 return -EFAULT;
496
497 return __bpf_get_stackid(map, trace, flags);
498 }
499
500 const struct bpf_func_proto bpf_get_stackid_proto = {
501 .func = bpf_get_stackid,
502 .gpl_only = true,
503 .ret_type = RET_INTEGER,
504 .arg1_type = ARG_PTR_TO_CTX,
505 .arg2_type = ARG_CONST_MAP_PTR,
506 .arg3_type = ARG_ANYTHING,
507 };
508
count_kernel_ip(struct perf_callchain_entry * trace)509 static __u64 count_kernel_ip(struct perf_callchain_entry *trace)
510 {
511 __u64 nr_kernel = 0;
512
513 while (nr_kernel < trace->nr) {
514 if (trace->ip[nr_kernel] == PERF_CONTEXT_USER)
515 break;
516 nr_kernel++;
517 }
518 return nr_kernel;
519 }
520
BPF_CALL_3(bpf_get_stackid_pe,struct bpf_perf_event_data_kern *,ctx,struct bpf_map *,map,u64,flags)521 BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
522 struct bpf_map *, map, u64, flags)
523 {
524 struct perf_event *event = ctx->event;
525 struct perf_callchain_entry *trace;
526 bool kernel, user;
527 __u64 nr_kernel;
528 int ret;
529
530 /* perf_sample_data doesn't have callchain, use bpf_get_stackid */
531 if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
532 return bpf_get_stackid((unsigned long)(ctx->regs),
533 (unsigned long) map, flags, 0, 0);
534
535 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
536 BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
537 return -EINVAL;
538
539 user = flags & BPF_F_USER_STACK;
540 kernel = !user;
541
542 trace = ctx->data->callchain;
543 if (unlikely(!trace))
544 return -EFAULT;
545
546 nr_kernel = count_kernel_ip(trace);
547
548 if (kernel) {
549 __u64 nr = trace->nr;
550
551 trace->nr = nr_kernel;
552 ret = __bpf_get_stackid(map, trace, flags);
553
554 /* restore nr */
555 trace->nr = nr;
556 } else { /* user */
557 u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
558
559 skip += nr_kernel;
560 if (skip > BPF_F_SKIP_FIELD_MASK)
561 return -EFAULT;
562
563 flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
564 ret = __bpf_get_stackid(map, trace, flags);
565 }
566 return ret;
567 }
568
569 const struct bpf_func_proto bpf_get_stackid_proto_pe = {
570 .func = bpf_get_stackid_pe,
571 .gpl_only = false,
572 .ret_type = RET_INTEGER,
573 .arg1_type = ARG_PTR_TO_CTX,
574 .arg2_type = ARG_CONST_MAP_PTR,
575 .arg3_type = ARG_ANYTHING,
576 };
577
__bpf_get_stack(struct pt_regs * regs,struct task_struct * task,struct perf_callchain_entry * trace_in,void * buf,u32 size,u64 flags)578 static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
579 struct perf_callchain_entry *trace_in,
580 void *buf, u32 size, u64 flags)
581 {
582 u32 init_nr, trace_nr, copy_len, elem_size, num_elem;
583 bool user_build_id = flags & BPF_F_USER_BUILD_ID;
584 u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
585 bool user = flags & BPF_F_USER_STACK;
586 struct perf_callchain_entry *trace;
587 bool kernel = !user;
588 int err = -EINVAL;
589 u64 *ips;
590
591 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
592 BPF_F_USER_BUILD_ID)))
593 goto clear;
594 if (kernel && user_build_id)
595 goto clear;
596
597 elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id)
598 : sizeof(u64);
599 if (unlikely(size % elem_size))
600 goto clear;
601
602 /* cannot get valid user stack for task without user_mode regs */
603 if (task && user && !user_mode(regs))
604 goto err_fault;
605
606 num_elem = size / elem_size;
607 if (sysctl_perf_event_max_stack < num_elem)
608 init_nr = 0;
609 else
610 init_nr = sysctl_perf_event_max_stack - num_elem;
611
612 if (trace_in)
613 trace = trace_in;
614 else if (kernel && task)
615 trace = get_callchain_entry_for_task(task, init_nr);
616 else
617 trace = get_perf_callchain(regs, init_nr, kernel, user,
618 sysctl_perf_event_max_stack,
619 false, false);
620 if (unlikely(!trace))
621 goto err_fault;
622
623 trace_nr = trace->nr - init_nr;
624 if (trace_nr < skip)
625 goto err_fault;
626
627 trace_nr -= skip;
628 trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
629 copy_len = trace_nr * elem_size;
630 ips = trace->ip + skip + init_nr;
631 if (user && user_build_id)
632 stack_map_get_build_id_offset(buf, ips, trace_nr, user);
633 else
634 memcpy(buf, ips, copy_len);
635
636 if (size > copy_len)
637 memset(buf + copy_len, 0, size - copy_len);
638 return copy_len;
639
640 err_fault:
641 err = -EFAULT;
642 clear:
643 memset(buf, 0, size);
644 return err;
645 }
646
BPF_CALL_4(bpf_get_stack,struct pt_regs *,regs,void *,buf,u32,size,u64,flags)647 BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
648 u64, flags)
649 {
650 return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
651 }
652
653 const struct bpf_func_proto bpf_get_stack_proto = {
654 .func = bpf_get_stack,
655 .gpl_only = true,
656 .ret_type = RET_INTEGER,
657 .arg1_type = ARG_PTR_TO_CTX,
658 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
659 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
660 .arg4_type = ARG_ANYTHING,
661 };
662
BPF_CALL_4(bpf_get_task_stack,struct task_struct *,task,void *,buf,u32,size,u64,flags)663 BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
664 u32, size, u64, flags)
665 {
666 struct pt_regs *regs;
667 long res = -EINVAL;
668
669 if (!try_get_task_stack(task))
670 return -EFAULT;
671
672 regs = task_pt_regs(task);
673 if (regs)
674 res = __bpf_get_stack(regs, task, NULL, buf, size, flags);
675 put_task_stack(task);
676
677 return res;
678 }
679
680 BTF_ID_LIST_SINGLE(bpf_get_task_stack_btf_ids, struct, task_struct)
681
682 const struct bpf_func_proto bpf_get_task_stack_proto = {
683 .func = bpf_get_task_stack,
684 .gpl_only = false,
685 .ret_type = RET_INTEGER,
686 .arg1_type = ARG_PTR_TO_BTF_ID,
687 .arg1_btf_id = &bpf_get_task_stack_btf_ids[0],
688 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
689 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
690 .arg4_type = ARG_ANYTHING,
691 };
692
BPF_CALL_4(bpf_get_stack_pe,struct bpf_perf_event_data_kern *,ctx,void *,buf,u32,size,u64,flags)693 BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
694 void *, buf, u32, size, u64, flags)
695 {
696 struct pt_regs *regs = (struct pt_regs *)(ctx->regs);
697 struct perf_event *event = ctx->event;
698 struct perf_callchain_entry *trace;
699 bool kernel, user;
700 int err = -EINVAL;
701 __u64 nr_kernel;
702
703 if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
704 return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
705
706 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
707 BPF_F_USER_BUILD_ID)))
708 goto clear;
709
710 user = flags & BPF_F_USER_STACK;
711 kernel = !user;
712
713 err = -EFAULT;
714 trace = ctx->data->callchain;
715 if (unlikely(!trace))
716 goto clear;
717
718 nr_kernel = count_kernel_ip(trace);
719
720 if (kernel) {
721 __u64 nr = trace->nr;
722
723 trace->nr = nr_kernel;
724 err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
725
726 /* restore nr */
727 trace->nr = nr;
728 } else { /* user */
729 u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
730
731 skip += nr_kernel;
732 if (skip > BPF_F_SKIP_FIELD_MASK)
733 goto clear;
734
735 flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
736 err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
737 }
738 return err;
739
740 clear:
741 memset(buf, 0, size);
742 return err;
743
744 }
745
746 const struct bpf_func_proto bpf_get_stack_proto_pe = {
747 .func = bpf_get_stack_pe,
748 .gpl_only = true,
749 .ret_type = RET_INTEGER,
750 .arg1_type = ARG_PTR_TO_CTX,
751 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
752 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
753 .arg4_type = ARG_ANYTHING,
754 };
755
756 /* Called from eBPF program */
stack_map_lookup_elem(struct bpf_map * map,void * key)757 static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
758 {
759 return ERR_PTR(-EOPNOTSUPP);
760 }
761
762 /* Called from syscall */
bpf_stackmap_copy(struct bpf_map * map,void * key,void * value)763 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
764 {
765 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
766 struct stack_map_bucket *bucket, *old_bucket;
767 u32 id = *(u32 *)key, trace_len;
768
769 if (unlikely(id >= smap->n_buckets))
770 return -ENOENT;
771
772 bucket = xchg(&smap->buckets[id], NULL);
773 if (!bucket)
774 return -ENOENT;
775
776 trace_len = bucket->nr * stack_map_data_size(map);
777 memcpy(value, bucket->data, trace_len);
778 memset(value + trace_len, 0, map->value_size - trace_len);
779
780 old_bucket = xchg(&smap->buckets[id], bucket);
781 if (old_bucket)
782 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
783 return 0;
784 }
785
stack_map_get_next_key(struct bpf_map * map,void * key,void * next_key)786 static int stack_map_get_next_key(struct bpf_map *map, void *key,
787 void *next_key)
788 {
789 struct bpf_stack_map *smap = container_of(map,
790 struct bpf_stack_map, map);
791 u32 id;
792
793 WARN_ON_ONCE(!rcu_read_lock_held());
794
795 if (!key) {
796 id = 0;
797 } else {
798 id = *(u32 *)key;
799 if (id >= smap->n_buckets || !smap->buckets[id])
800 id = 0;
801 else
802 id++;
803 }
804
805 while (id < smap->n_buckets && !smap->buckets[id])
806 id++;
807
808 if (id >= smap->n_buckets)
809 return -ENOENT;
810
811 *(u32 *)next_key = id;
812 return 0;
813 }
814
stack_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)815 static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
816 u64 map_flags)
817 {
818 return -EINVAL;
819 }
820
821 /* Called from syscall or from eBPF program */
stack_map_delete_elem(struct bpf_map * map,void * key)822 static int stack_map_delete_elem(struct bpf_map *map, void *key)
823 {
824 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
825 struct stack_map_bucket *old_bucket;
826 u32 id = *(u32 *)key;
827
828 if (unlikely(id >= smap->n_buckets))
829 return -E2BIG;
830
831 old_bucket = xchg(&smap->buckets[id], NULL);
832 if (old_bucket) {
833 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
834 return 0;
835 } else {
836 return -ENOENT;
837 }
838 }
839
840 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
stack_map_free(struct bpf_map * map)841 static void stack_map_free(struct bpf_map *map)
842 {
843 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
844
845 bpf_map_area_free(smap->elems);
846 pcpu_freelist_destroy(&smap->freelist);
847 bpf_map_area_free(smap);
848 put_callchain_buffers();
849 }
850
851 static int stack_trace_map_btf_id;
852 const struct bpf_map_ops stack_trace_map_ops = {
853 .map_meta_equal = bpf_map_meta_equal,
854 .map_alloc = stack_map_alloc,
855 .map_free = stack_map_free,
856 .map_get_next_key = stack_map_get_next_key,
857 .map_lookup_elem = stack_map_lookup_elem,
858 .map_update_elem = stack_map_update_elem,
859 .map_delete_elem = stack_map_delete_elem,
860 .map_check_btf = map_check_no_btf,
861 .map_btf_name = "bpf_stack_map",
862 .map_btf_id = &stack_trace_map_btf_id,
863 };
864
stack_map_init(void)865 static int __init stack_map_init(void)
866 {
867 int cpu;
868 struct stack_map_irq_work *work;
869
870 for_each_possible_cpu(cpu) {
871 work = per_cpu_ptr(&up_read_work, cpu);
872 init_irq_work(&work->irq_work, do_up_read);
873 }
874 return 0;
875 }
876 subsys_initcall(stack_map_init);
877