xref: /linux-6.15/kernel/bpf/stackmap.c (revision b992f01e)
125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d5a3b1f6SAlexei Starovoitov /* Copyright (c) 2016 Facebook
3d5a3b1f6SAlexei Starovoitov  */
4d5a3b1f6SAlexei Starovoitov #include <linux/bpf.h>
5d5a3b1f6SAlexei Starovoitov #include <linux/jhash.h>
6d5a3b1f6SAlexei Starovoitov #include <linux/filter.h>
77b04d6d6SSong Liu #include <linux/kernel.h>
8d5a3b1f6SAlexei Starovoitov #include <linux/stacktrace.h>
9d5a3b1f6SAlexei Starovoitov #include <linux/perf_event.h>
10c9a0f3b8SJiri Olsa #include <linux/btf_ids.h>
11bd7525daSJiri Olsa #include <linux/buildid.h>
12557c0c6eSAlexei Starovoitov #include "percpu_freelist.h"
137c7e3d31SSong Liu #include "mmap_unlock_work.h"
14d5a3b1f6SAlexei Starovoitov 
156e71b04aSChenbo Feng #define STACK_CREATE_FLAG_MASK					\
16615755a7SSong Liu 	(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY |	\
17615755a7SSong Liu 	 BPF_F_STACK_BUILD_ID)
186e71b04aSChenbo Feng 
19d5a3b1f6SAlexei Starovoitov struct stack_map_bucket {
20557c0c6eSAlexei Starovoitov 	struct pcpu_freelist_node fnode;
21d5a3b1f6SAlexei Starovoitov 	u32 hash;
22d5a3b1f6SAlexei Starovoitov 	u32 nr;
23615755a7SSong Liu 	u64 data[];
24d5a3b1f6SAlexei Starovoitov };
25d5a3b1f6SAlexei Starovoitov 
26d5a3b1f6SAlexei Starovoitov struct bpf_stack_map {
27d5a3b1f6SAlexei Starovoitov 	struct bpf_map map;
28557c0c6eSAlexei Starovoitov 	void *elems;
29557c0c6eSAlexei Starovoitov 	struct pcpu_freelist freelist;
30d5a3b1f6SAlexei Starovoitov 	u32 n_buckets;
31557c0c6eSAlexei Starovoitov 	struct stack_map_bucket *buckets[];
32d5a3b1f6SAlexei Starovoitov };
33d5a3b1f6SAlexei Starovoitov 
34615755a7SSong Liu static inline bool stack_map_use_build_id(struct bpf_map *map)
35615755a7SSong Liu {
36615755a7SSong Liu 	return (map->map_flags & BPF_F_STACK_BUILD_ID);
37615755a7SSong Liu }
38615755a7SSong Liu 
39615755a7SSong Liu static inline int stack_map_data_size(struct bpf_map *map)
40615755a7SSong Liu {
41615755a7SSong Liu 	return stack_map_use_build_id(map) ?
42615755a7SSong Liu 		sizeof(struct bpf_stack_build_id) : sizeof(u64);
43615755a7SSong Liu }
44615755a7SSong Liu 
45557c0c6eSAlexei Starovoitov static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
46557c0c6eSAlexei Starovoitov {
4730e29a9aSTatsuhiko Yasumatsu 	u64 elem_size = sizeof(struct stack_map_bucket) +
4830e29a9aSTatsuhiko Yasumatsu 			(u64)smap->map.value_size;
49557c0c6eSAlexei Starovoitov 	int err;
50557c0c6eSAlexei Starovoitov 
5196eabe7aSMartin KaFai Lau 	smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
5296eabe7aSMartin KaFai Lau 					 smap->map.numa_node);
53557c0c6eSAlexei Starovoitov 	if (!smap->elems)
54557c0c6eSAlexei Starovoitov 		return -ENOMEM;
55557c0c6eSAlexei Starovoitov 
56557c0c6eSAlexei Starovoitov 	err = pcpu_freelist_init(&smap->freelist);
57557c0c6eSAlexei Starovoitov 	if (err)
58557c0c6eSAlexei Starovoitov 		goto free_elems;
59557c0c6eSAlexei Starovoitov 
60557c0c6eSAlexei Starovoitov 	pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
61557c0c6eSAlexei Starovoitov 			       smap->map.max_entries);
62557c0c6eSAlexei Starovoitov 	return 0;
63557c0c6eSAlexei Starovoitov 
64557c0c6eSAlexei Starovoitov free_elems:
65d407bd25SDaniel Borkmann 	bpf_map_area_free(smap->elems);
66557c0c6eSAlexei Starovoitov 	return err;
67557c0c6eSAlexei Starovoitov }
68557c0c6eSAlexei Starovoitov 
69d5a3b1f6SAlexei Starovoitov /* Called from syscall */
70d5a3b1f6SAlexei Starovoitov static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
71d5a3b1f6SAlexei Starovoitov {
72d5a3b1f6SAlexei Starovoitov 	u32 value_size = attr->value_size;
73d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap;
74d5a3b1f6SAlexei Starovoitov 	u64 cost, n_buckets;
75d5a3b1f6SAlexei Starovoitov 	int err;
76d5a3b1f6SAlexei Starovoitov 
772c78ee89SAlexei Starovoitov 	if (!bpf_capable())
78d5a3b1f6SAlexei Starovoitov 		return ERR_PTR(-EPERM);
79d5a3b1f6SAlexei Starovoitov 
806e71b04aSChenbo Feng 	if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
81823707b6SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
82823707b6SAlexei Starovoitov 
83d5a3b1f6SAlexei Starovoitov 	/* check sanity of attributes */
84d5a3b1f6SAlexei Starovoitov 	if (attr->max_entries == 0 || attr->key_size != 4 ||
85615755a7SSong Liu 	    value_size < 8 || value_size % 8)
86615755a7SSong Liu 		return ERR_PTR(-EINVAL);
87615755a7SSong Liu 
88615755a7SSong Liu 	BUILD_BUG_ON(sizeof(struct bpf_stack_build_id) % sizeof(u64));
89615755a7SSong Liu 	if (attr->map_flags & BPF_F_STACK_BUILD_ID) {
90615755a7SSong Liu 		if (value_size % sizeof(struct bpf_stack_build_id) ||
91615755a7SSong Liu 		    value_size / sizeof(struct bpf_stack_build_id)
92615755a7SSong Liu 		    > sysctl_perf_event_max_stack)
93615755a7SSong Liu 			return ERR_PTR(-EINVAL);
94615755a7SSong Liu 	} else if (value_size / 8 > sysctl_perf_event_max_stack)
95d5a3b1f6SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
96d5a3b1f6SAlexei Starovoitov 
97d5a3b1f6SAlexei Starovoitov 	/* hash table size must be power of 2 */
98d5a3b1f6SAlexei Starovoitov 	n_buckets = roundup_pow_of_two(attr->max_entries);
996183f4d3SBui Quang Minh 	if (!n_buckets)
1006183f4d3SBui Quang Minh 		return ERR_PTR(-E2BIG);
101d5a3b1f6SAlexei Starovoitov 
102d5a3b1f6SAlexei Starovoitov 	cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
103d5a3b1f6SAlexei Starovoitov 	cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
104b936ca64SRoman Gushchin 	smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
10537086810SRoman Gushchin 	if (!smap)
106b936ca64SRoman Gushchin 		return ERR_PTR(-ENOMEM);
107d5a3b1f6SAlexei Starovoitov 
108bd475643SJakub Kicinski 	bpf_map_init_from_attr(&smap->map, attr);
109d5a3b1f6SAlexei Starovoitov 	smap->map.value_size = value_size;
110d5a3b1f6SAlexei Starovoitov 	smap->n_buckets = n_buckets;
111557c0c6eSAlexei Starovoitov 
11297c79a38SArnaldo Carvalho de Melo 	err = get_callchain_buffers(sysctl_perf_event_max_stack);
113d5a3b1f6SAlexei Starovoitov 	if (err)
11437086810SRoman Gushchin 		goto free_smap;
115d5a3b1f6SAlexei Starovoitov 
116557c0c6eSAlexei Starovoitov 	err = prealloc_elems_and_freelist(smap);
117557c0c6eSAlexei Starovoitov 	if (err)
118557c0c6eSAlexei Starovoitov 		goto put_buffers;
119557c0c6eSAlexei Starovoitov 
120d5a3b1f6SAlexei Starovoitov 	return &smap->map;
121d5a3b1f6SAlexei Starovoitov 
122557c0c6eSAlexei Starovoitov put_buffers:
123557c0c6eSAlexei Starovoitov 	put_callchain_buffers();
12437086810SRoman Gushchin free_smap:
125d407bd25SDaniel Borkmann 	bpf_map_area_free(smap);
126d5a3b1f6SAlexei Starovoitov 	return ERR_PTR(err);
127d5a3b1f6SAlexei Starovoitov }
128d5a3b1f6SAlexei Starovoitov 
1295f412632SYonghong Song static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
130615755a7SSong Liu 					  u64 *ips, u32 trace_nr, bool user)
131615755a7SSong Liu {
132615755a7SSong Liu 	int i;
1337c7e3d31SSong Liu 	struct mmap_unlock_irq_work *work = NULL;
1347c7e3d31SSong Liu 	bool irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
135615755a7SSong Liu 	struct vm_area_struct *vma;
136bae77c5eSSong Liu 
1377c7e3d31SSong Liu 	/* If the irq_work is in use, fall back to report ips. Same
1387c7e3d31SSong Liu 	 * fallback is used for kernel stack (!user) on a stackmap with
1397c7e3d31SSong Liu 	 * build_id.
140615755a7SSong Liu 	 */
141bae77c5eSSong Liu 	if (!user || !current || !current->mm || irq_work_busy ||
1422f1aaf3eSYonghong Song 	    !mmap_read_trylock(current->mm)) {
143615755a7SSong Liu 		/* cannot access current->mm, fall back to ips */
144615755a7SSong Liu 		for (i = 0; i < trace_nr; i++) {
145615755a7SSong Liu 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
146615755a7SSong Liu 			id_offs[i].ip = ips[i];
147bd7525daSJiri Olsa 			memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
148615755a7SSong Liu 		}
149615755a7SSong Liu 		return;
150615755a7SSong Liu 	}
151615755a7SSong Liu 
152615755a7SSong Liu 	for (i = 0; i < trace_nr; i++) {
153615755a7SSong Liu 		vma = find_vma(current->mm, ips[i]);
154921f88fcSJiri Olsa 		if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) {
155615755a7SSong Liu 			/* per entry fall back to ips */
156615755a7SSong Liu 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
157615755a7SSong Liu 			id_offs[i].ip = ips[i];
158bd7525daSJiri Olsa 			memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
159615755a7SSong Liu 			continue;
160615755a7SSong Liu 		}
161615755a7SSong Liu 		id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
162615755a7SSong Liu 			- vma->vm_start;
163615755a7SSong Liu 		id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
164615755a7SSong Liu 	}
1657c7e3d31SSong Liu 	bpf_mmap_unlock_mm(work, current->mm);
166615755a7SSong Liu }
167615755a7SSong Liu 
168fa28dcb8SSong Liu static struct perf_callchain_entry *
169fa28dcb8SSong Liu get_callchain_entry_for_task(struct task_struct *task, u32 init_nr)
170fa28dcb8SSong Liu {
171046cc3ddSSong Liu #ifdef CONFIG_STACKTRACE
172fa28dcb8SSong Liu 	struct perf_callchain_entry *entry;
173fa28dcb8SSong Liu 	int rctx;
174fa28dcb8SSong Liu 
175fa28dcb8SSong Liu 	entry = get_callchain_entry(&rctx);
176fa28dcb8SSong Liu 
177fa28dcb8SSong Liu 	if (!entry)
178fa28dcb8SSong Liu 		return NULL;
179fa28dcb8SSong Liu 
180fa28dcb8SSong Liu 	entry->nr = init_nr +
181fa28dcb8SSong Liu 		stack_trace_save_tsk(task, (unsigned long *)(entry->ip + init_nr),
182fa28dcb8SSong Liu 				     sysctl_perf_event_max_stack - init_nr, 0);
183fa28dcb8SSong Liu 
184fa28dcb8SSong Liu 	/* stack_trace_save_tsk() works on unsigned long array, while
185fa28dcb8SSong Liu 	 * perf_callchain_entry uses u64 array. For 32-bit systems, it is
186fa28dcb8SSong Liu 	 * necessary to fix this mismatch.
187fa28dcb8SSong Liu 	 */
188fa28dcb8SSong Liu 	if (__BITS_PER_LONG != 64) {
189fa28dcb8SSong Liu 		unsigned long *from = (unsigned long *) entry->ip;
190fa28dcb8SSong Liu 		u64 *to = entry->ip;
191fa28dcb8SSong Liu 		int i;
192fa28dcb8SSong Liu 
193fa28dcb8SSong Liu 		/* copy data from the end to avoid using extra buffer */
194fa28dcb8SSong Liu 		for (i = entry->nr - 1; i >= (int)init_nr; i--)
195fa28dcb8SSong Liu 			to[i] = (u64)(from[i]);
196fa28dcb8SSong Liu 	}
197fa28dcb8SSong Liu 
198fa28dcb8SSong Liu 	put_callchain_entry(rctx);
199fa28dcb8SSong Liu 
200fa28dcb8SSong Liu 	return entry;
201046cc3ddSSong Liu #else /* CONFIG_STACKTRACE */
202046cc3ddSSong Liu 	return NULL;
203046cc3ddSSong Liu #endif
204fa28dcb8SSong Liu }
205fa28dcb8SSong Liu 
2067b04d6d6SSong Liu static long __bpf_get_stackid(struct bpf_map *map,
2077b04d6d6SSong Liu 			      struct perf_callchain_entry *trace, u64 flags)
208d5a3b1f6SAlexei Starovoitov {
209d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
210d5a3b1f6SAlexei Starovoitov 	struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
211615755a7SSong Liu 	u32 max_depth = map->value_size / stack_map_data_size(map);
212c5dfd78eSArnaldo Carvalho de Melo 	/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
213c5dfd78eSArnaldo Carvalho de Melo 	u32 init_nr = sysctl_perf_event_max_stack - max_depth;
214d5a3b1f6SAlexei Starovoitov 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
215d5a3b1f6SAlexei Starovoitov 	u32 hash, id, trace_nr, trace_len;
216d5a3b1f6SAlexei Starovoitov 	bool user = flags & BPF_F_USER_STACK;
217d5a3b1f6SAlexei Starovoitov 	u64 *ips;
218615755a7SSong Liu 	bool hash_matches;
219d5a3b1f6SAlexei Starovoitov 
220d5a3b1f6SAlexei Starovoitov 	/* get_perf_callchain() guarantees that trace->nr >= init_nr
221c5dfd78eSArnaldo Carvalho de Melo 	 * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
222d5a3b1f6SAlexei Starovoitov 	 */
223d5a3b1f6SAlexei Starovoitov 	trace_nr = trace->nr - init_nr;
224d5a3b1f6SAlexei Starovoitov 
225d5a3b1f6SAlexei Starovoitov 	if (trace_nr <= skip)
226d5a3b1f6SAlexei Starovoitov 		/* skipping more than usable stack trace */
227d5a3b1f6SAlexei Starovoitov 		return -EFAULT;
228d5a3b1f6SAlexei Starovoitov 
229d5a3b1f6SAlexei Starovoitov 	trace_nr -= skip;
230d5a3b1f6SAlexei Starovoitov 	trace_len = trace_nr * sizeof(u64);
231d5a3b1f6SAlexei Starovoitov 	ips = trace->ip + skip + init_nr;
232d5a3b1f6SAlexei Starovoitov 	hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
233d5a3b1f6SAlexei Starovoitov 	id = hash & (smap->n_buckets - 1);
234557c0c6eSAlexei Starovoitov 	bucket = READ_ONCE(smap->buckets[id]);
235d5a3b1f6SAlexei Starovoitov 
236615755a7SSong Liu 	hash_matches = bucket && bucket->hash == hash;
237615755a7SSong Liu 	/* fast cmp */
238615755a7SSong Liu 	if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
239d5a3b1f6SAlexei Starovoitov 		return id;
240615755a7SSong Liu 
241615755a7SSong Liu 	if (stack_map_use_build_id(map)) {
242615755a7SSong Liu 		/* for build_id+offset, pop a bucket before slow cmp */
243615755a7SSong Liu 		new_bucket = (struct stack_map_bucket *)
244615755a7SSong Liu 			pcpu_freelist_pop(&smap->freelist);
245615755a7SSong Liu 		if (unlikely(!new_bucket))
246615755a7SSong Liu 			return -ENOMEM;
2475f412632SYonghong Song 		new_bucket->nr = trace_nr;
2485f412632SYonghong Song 		stack_map_get_build_id_offset(
2495f412632SYonghong Song 			(struct bpf_stack_build_id *)new_bucket->data,
2505f412632SYonghong Song 			ips, trace_nr, user);
251615755a7SSong Liu 		trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
252615755a7SSong Liu 		if (hash_matches && bucket->nr == trace_nr &&
253615755a7SSong Liu 		    memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
254615755a7SSong Liu 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
255d5a3b1f6SAlexei Starovoitov 			return id;
256d5a3b1f6SAlexei Starovoitov 		}
257615755a7SSong Liu 		if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
258615755a7SSong Liu 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
259615755a7SSong Liu 			return -EEXIST;
260615755a7SSong Liu 		}
261615755a7SSong Liu 	} else {
262615755a7SSong Liu 		if (hash_matches && bucket->nr == trace_nr &&
263615755a7SSong Liu 		    memcmp(bucket->data, ips, trace_len) == 0)
264615755a7SSong Liu 			return id;
265d5a3b1f6SAlexei Starovoitov 		if (bucket && !(flags & BPF_F_REUSE_STACKID))
266d5a3b1f6SAlexei Starovoitov 			return -EEXIST;
267d5a3b1f6SAlexei Starovoitov 
268557c0c6eSAlexei Starovoitov 		new_bucket = (struct stack_map_bucket *)
269557c0c6eSAlexei Starovoitov 			pcpu_freelist_pop(&smap->freelist);
270d5a3b1f6SAlexei Starovoitov 		if (unlikely(!new_bucket))
271d5a3b1f6SAlexei Starovoitov 			return -ENOMEM;
272615755a7SSong Liu 		memcpy(new_bucket->data, ips, trace_len);
273615755a7SSong Liu 	}
274d5a3b1f6SAlexei Starovoitov 
275d5a3b1f6SAlexei Starovoitov 	new_bucket->hash = hash;
276d5a3b1f6SAlexei Starovoitov 	new_bucket->nr = trace_nr;
277d5a3b1f6SAlexei Starovoitov 
278d5a3b1f6SAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], new_bucket);
279d5a3b1f6SAlexei Starovoitov 	if (old_bucket)
280557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
281d5a3b1f6SAlexei Starovoitov 	return id;
282d5a3b1f6SAlexei Starovoitov }
283d5a3b1f6SAlexei Starovoitov 
2847b04d6d6SSong Liu BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
2857b04d6d6SSong Liu 	   u64, flags)
2867b04d6d6SSong Liu {
2877b04d6d6SSong Liu 	u32 max_depth = map->value_size / stack_map_data_size(map);
2887b04d6d6SSong Liu 	/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
2897b04d6d6SSong Liu 	u32 init_nr = sysctl_perf_event_max_stack - max_depth;
2907b04d6d6SSong Liu 	bool user = flags & BPF_F_USER_STACK;
2917b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
2927b04d6d6SSong Liu 	bool kernel = !user;
2937b04d6d6SSong Liu 
2947b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
2957b04d6d6SSong Liu 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
2967b04d6d6SSong Liu 		return -EINVAL;
2977b04d6d6SSong Liu 
2987b04d6d6SSong Liu 	trace = get_perf_callchain(regs, init_nr, kernel, user,
2997b04d6d6SSong Liu 				   sysctl_perf_event_max_stack, false, false);
3007b04d6d6SSong Liu 
3017b04d6d6SSong Liu 	if (unlikely(!trace))
3027b04d6d6SSong Liu 		/* couldn't fetch the stack trace */
3037b04d6d6SSong Liu 		return -EFAULT;
3047b04d6d6SSong Liu 
3057b04d6d6SSong Liu 	return __bpf_get_stackid(map, trace, flags);
3067b04d6d6SSong Liu }
3077b04d6d6SSong Liu 
308d5a3b1f6SAlexei Starovoitov const struct bpf_func_proto bpf_get_stackid_proto = {
309d5a3b1f6SAlexei Starovoitov 	.func		= bpf_get_stackid,
310d5a3b1f6SAlexei Starovoitov 	.gpl_only	= true,
311d5a3b1f6SAlexei Starovoitov 	.ret_type	= RET_INTEGER,
312d5a3b1f6SAlexei Starovoitov 	.arg1_type	= ARG_PTR_TO_CTX,
313d5a3b1f6SAlexei Starovoitov 	.arg2_type	= ARG_CONST_MAP_PTR,
314d5a3b1f6SAlexei Starovoitov 	.arg3_type	= ARG_ANYTHING,
315d5a3b1f6SAlexei Starovoitov };
316d5a3b1f6SAlexei Starovoitov 
3177b04d6d6SSong Liu static __u64 count_kernel_ip(struct perf_callchain_entry *trace)
3187b04d6d6SSong Liu {
3197b04d6d6SSong Liu 	__u64 nr_kernel = 0;
3207b04d6d6SSong Liu 
3217b04d6d6SSong Liu 	while (nr_kernel < trace->nr) {
3227b04d6d6SSong Liu 		if (trace->ip[nr_kernel] == PERF_CONTEXT_USER)
3237b04d6d6SSong Liu 			break;
3247b04d6d6SSong Liu 		nr_kernel++;
3257b04d6d6SSong Liu 	}
3267b04d6d6SSong Liu 	return nr_kernel;
3277b04d6d6SSong Liu }
3287b04d6d6SSong Liu 
3297b04d6d6SSong Liu BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
3307b04d6d6SSong Liu 	   struct bpf_map *, map, u64, flags)
3317b04d6d6SSong Liu {
3327b04d6d6SSong Liu 	struct perf_event *event = ctx->event;
3337b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
3347b04d6d6SSong Liu 	bool kernel, user;
3357b04d6d6SSong Liu 	__u64 nr_kernel;
3367b04d6d6SSong Liu 	int ret;
3377b04d6d6SSong Liu 
3387b04d6d6SSong Liu 	/* perf_sample_data doesn't have callchain, use bpf_get_stackid */
3397b04d6d6SSong Liu 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
3407b04d6d6SSong Liu 		return bpf_get_stackid((unsigned long)(ctx->regs),
3417b04d6d6SSong Liu 				       (unsigned long) map, flags, 0, 0);
3427b04d6d6SSong Liu 
3437b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
3447b04d6d6SSong Liu 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
3457b04d6d6SSong Liu 		return -EINVAL;
3467b04d6d6SSong Liu 
3477b04d6d6SSong Liu 	user = flags & BPF_F_USER_STACK;
3487b04d6d6SSong Liu 	kernel = !user;
3497b04d6d6SSong Liu 
3507b04d6d6SSong Liu 	trace = ctx->data->callchain;
3517b04d6d6SSong Liu 	if (unlikely(!trace))
3527b04d6d6SSong Liu 		return -EFAULT;
3537b04d6d6SSong Liu 
3547b04d6d6SSong Liu 	nr_kernel = count_kernel_ip(trace);
3557b04d6d6SSong Liu 
3567b04d6d6SSong Liu 	if (kernel) {
3577b04d6d6SSong Liu 		__u64 nr = trace->nr;
3587b04d6d6SSong Liu 
3597b04d6d6SSong Liu 		trace->nr = nr_kernel;
3607b04d6d6SSong Liu 		ret = __bpf_get_stackid(map, trace, flags);
3617b04d6d6SSong Liu 
3627b04d6d6SSong Liu 		/* restore nr */
3637b04d6d6SSong Liu 		trace->nr = nr;
3647b04d6d6SSong Liu 	} else { /* user */
3657b04d6d6SSong Liu 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
3667b04d6d6SSong Liu 
3677b04d6d6SSong Liu 		skip += nr_kernel;
3687b04d6d6SSong Liu 		if (skip > BPF_F_SKIP_FIELD_MASK)
3697b04d6d6SSong Liu 			return -EFAULT;
3707b04d6d6SSong Liu 
3717b04d6d6SSong Liu 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
3727b04d6d6SSong Liu 		ret = __bpf_get_stackid(map, trace, flags);
3737b04d6d6SSong Liu 	}
3747b04d6d6SSong Liu 	return ret;
3757b04d6d6SSong Liu }
3767b04d6d6SSong Liu 
3777b04d6d6SSong Liu const struct bpf_func_proto bpf_get_stackid_proto_pe = {
3787b04d6d6SSong Liu 	.func		= bpf_get_stackid_pe,
3797b04d6d6SSong Liu 	.gpl_only	= false,
3807b04d6d6SSong Liu 	.ret_type	= RET_INTEGER,
3817b04d6d6SSong Liu 	.arg1_type	= ARG_PTR_TO_CTX,
3827b04d6d6SSong Liu 	.arg2_type	= ARG_CONST_MAP_PTR,
3837b04d6d6SSong Liu 	.arg3_type	= ARG_ANYTHING,
3847b04d6d6SSong Liu };
3857b04d6d6SSong Liu 
386fa28dcb8SSong Liu static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
3877b04d6d6SSong Liu 			    struct perf_callchain_entry *trace_in,
388fa28dcb8SSong Liu 			    void *buf, u32 size, u64 flags)
389c195651eSYonghong Song {
390c195651eSYonghong Song 	u32 init_nr, trace_nr, copy_len, elem_size, num_elem;
391c195651eSYonghong Song 	bool user_build_id = flags & BPF_F_USER_BUILD_ID;
392c195651eSYonghong Song 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
393c195651eSYonghong Song 	bool user = flags & BPF_F_USER_STACK;
394c195651eSYonghong Song 	struct perf_callchain_entry *trace;
395c195651eSYonghong Song 	bool kernel = !user;
396c195651eSYonghong Song 	int err = -EINVAL;
397c195651eSYonghong Song 	u64 *ips;
398c195651eSYonghong Song 
399c195651eSYonghong Song 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
400c195651eSYonghong Song 			       BPF_F_USER_BUILD_ID)))
401c195651eSYonghong Song 		goto clear;
402c195651eSYonghong Song 	if (kernel && user_build_id)
403c195651eSYonghong Song 		goto clear;
404c195651eSYonghong Song 
405c195651eSYonghong Song 	elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id)
406c195651eSYonghong Song 					    : sizeof(u64);
407c195651eSYonghong Song 	if (unlikely(size % elem_size))
408c195651eSYonghong Song 		goto clear;
409c195651eSYonghong Song 
410fa28dcb8SSong Liu 	/* cannot get valid user stack for task without user_mode regs */
411fa28dcb8SSong Liu 	if (task && user && !user_mode(regs))
412fa28dcb8SSong Liu 		goto err_fault;
413fa28dcb8SSong Liu 
414c195651eSYonghong Song 	num_elem = size / elem_size;
415c195651eSYonghong Song 	if (sysctl_perf_event_max_stack < num_elem)
416c195651eSYonghong Song 		init_nr = 0;
417c195651eSYonghong Song 	else
418c195651eSYonghong Song 		init_nr = sysctl_perf_event_max_stack - num_elem;
419fa28dcb8SSong Liu 
4207b04d6d6SSong Liu 	if (trace_in)
4217b04d6d6SSong Liu 		trace = trace_in;
4227b04d6d6SSong Liu 	else if (kernel && task)
423fa28dcb8SSong Liu 		trace = get_callchain_entry_for_task(task, init_nr);
424fa28dcb8SSong Liu 	else
425c195651eSYonghong Song 		trace = get_perf_callchain(regs, init_nr, kernel, user,
426fa28dcb8SSong Liu 					   sysctl_perf_event_max_stack,
427fa28dcb8SSong Liu 					   false, false);
428c195651eSYonghong Song 	if (unlikely(!trace))
429c195651eSYonghong Song 		goto err_fault;
430c195651eSYonghong Song 
431c195651eSYonghong Song 	trace_nr = trace->nr - init_nr;
432c195651eSYonghong Song 	if (trace_nr < skip)
433c195651eSYonghong Song 		goto err_fault;
434c195651eSYonghong Song 
435c195651eSYonghong Song 	trace_nr -= skip;
436c195651eSYonghong Song 	trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
437c195651eSYonghong Song 	copy_len = trace_nr * elem_size;
438c195651eSYonghong Song 	ips = trace->ip + skip + init_nr;
439c195651eSYonghong Song 	if (user && user_build_id)
440c195651eSYonghong Song 		stack_map_get_build_id_offset(buf, ips, trace_nr, user);
441c195651eSYonghong Song 	else
442c195651eSYonghong Song 		memcpy(buf, ips, copy_len);
443c195651eSYonghong Song 
444c195651eSYonghong Song 	if (size > copy_len)
445c195651eSYonghong Song 		memset(buf + copy_len, 0, size - copy_len);
446c195651eSYonghong Song 	return copy_len;
447c195651eSYonghong Song 
448c195651eSYonghong Song err_fault:
449c195651eSYonghong Song 	err = -EFAULT;
450c195651eSYonghong Song clear:
451c195651eSYonghong Song 	memset(buf, 0, size);
452c195651eSYonghong Song 	return err;
453c195651eSYonghong Song }
454c195651eSYonghong Song 
455fa28dcb8SSong Liu BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
456fa28dcb8SSong Liu 	   u64, flags)
457fa28dcb8SSong Liu {
4587b04d6d6SSong Liu 	return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
459fa28dcb8SSong Liu }
460fa28dcb8SSong Liu 
461c195651eSYonghong Song const struct bpf_func_proto bpf_get_stack_proto = {
462c195651eSYonghong Song 	.func		= bpf_get_stack,
463c195651eSYonghong Song 	.gpl_only	= true,
464c195651eSYonghong Song 	.ret_type	= RET_INTEGER,
465c195651eSYonghong Song 	.arg1_type	= ARG_PTR_TO_CTX,
466c195651eSYonghong Song 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
467c195651eSYonghong Song 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
468c195651eSYonghong Song 	.arg4_type	= ARG_ANYTHING,
469c195651eSYonghong Song };
470c195651eSYonghong Song 
471fa28dcb8SSong Liu BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
472fa28dcb8SSong Liu 	   u32, size, u64, flags)
473fa28dcb8SSong Liu {
47406ab134cSDave Marchevsky 	struct pt_regs *regs;
475*b992f01eSNaveen N. Rao 	long res = -EINVAL;
476fa28dcb8SSong Liu 
47706ab134cSDave Marchevsky 	if (!try_get_task_stack(task))
47806ab134cSDave Marchevsky 		return -EFAULT;
47906ab134cSDave Marchevsky 
48006ab134cSDave Marchevsky 	regs = task_pt_regs(task);
481*b992f01eSNaveen N. Rao 	if (regs)
48206ab134cSDave Marchevsky 		res = __bpf_get_stack(regs, task, NULL, buf, size, flags);
48306ab134cSDave Marchevsky 	put_task_stack(task);
48406ab134cSDave Marchevsky 
48506ab134cSDave Marchevsky 	return res;
486fa28dcb8SSong Liu }
487fa28dcb8SSong Liu 
488fa28dcb8SSong Liu const struct bpf_func_proto bpf_get_task_stack_proto = {
489fa28dcb8SSong Liu 	.func		= bpf_get_task_stack,
490fa28dcb8SSong Liu 	.gpl_only	= false,
491fa28dcb8SSong Liu 	.ret_type	= RET_INTEGER,
492fa28dcb8SSong Liu 	.arg1_type	= ARG_PTR_TO_BTF_ID,
493d19ddb47SSong Liu 	.arg1_btf_id	= &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
494fa28dcb8SSong Liu 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
495fa28dcb8SSong Liu 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
496fa28dcb8SSong Liu 	.arg4_type	= ARG_ANYTHING,
497fa28dcb8SSong Liu };
498fa28dcb8SSong Liu 
4997b04d6d6SSong Liu BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
5007b04d6d6SSong Liu 	   void *, buf, u32, size, u64, flags)
5017b04d6d6SSong Liu {
5022b9b305fSSong Liu 	struct pt_regs *regs = (struct pt_regs *)(ctx->regs);
5037b04d6d6SSong Liu 	struct perf_event *event = ctx->event;
5047b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
5057b04d6d6SSong Liu 	bool kernel, user;
5067b04d6d6SSong Liu 	int err = -EINVAL;
5077b04d6d6SSong Liu 	__u64 nr_kernel;
5087b04d6d6SSong Liu 
5097b04d6d6SSong Liu 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
5102b9b305fSSong Liu 		return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
5117b04d6d6SSong Liu 
5127b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
5137b04d6d6SSong Liu 			       BPF_F_USER_BUILD_ID)))
5147b04d6d6SSong Liu 		goto clear;
5157b04d6d6SSong Liu 
5167b04d6d6SSong Liu 	user = flags & BPF_F_USER_STACK;
5177b04d6d6SSong Liu 	kernel = !user;
5187b04d6d6SSong Liu 
5197b04d6d6SSong Liu 	err = -EFAULT;
5207b04d6d6SSong Liu 	trace = ctx->data->callchain;
5217b04d6d6SSong Liu 	if (unlikely(!trace))
5227b04d6d6SSong Liu 		goto clear;
5237b04d6d6SSong Liu 
5247b04d6d6SSong Liu 	nr_kernel = count_kernel_ip(trace);
5257b04d6d6SSong Liu 
5267b04d6d6SSong Liu 	if (kernel) {
5277b04d6d6SSong Liu 		__u64 nr = trace->nr;
5287b04d6d6SSong Liu 
5297b04d6d6SSong Liu 		trace->nr = nr_kernel;
5302b9b305fSSong Liu 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
5317b04d6d6SSong Liu 
5327b04d6d6SSong Liu 		/* restore nr */
5337b04d6d6SSong Liu 		trace->nr = nr;
5347b04d6d6SSong Liu 	} else { /* user */
5357b04d6d6SSong Liu 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
5367b04d6d6SSong Liu 
5377b04d6d6SSong Liu 		skip += nr_kernel;
5387b04d6d6SSong Liu 		if (skip > BPF_F_SKIP_FIELD_MASK)
5397b04d6d6SSong Liu 			goto clear;
5407b04d6d6SSong Liu 
5417b04d6d6SSong Liu 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
5422b9b305fSSong Liu 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
5437b04d6d6SSong Liu 	}
5447b04d6d6SSong Liu 	return err;
5457b04d6d6SSong Liu 
5467b04d6d6SSong Liu clear:
5477b04d6d6SSong Liu 	memset(buf, 0, size);
5487b04d6d6SSong Liu 	return err;
5497b04d6d6SSong Liu 
5507b04d6d6SSong Liu }
5517b04d6d6SSong Liu 
5527b04d6d6SSong Liu const struct bpf_func_proto bpf_get_stack_proto_pe = {
5537b04d6d6SSong Liu 	.func		= bpf_get_stack_pe,
5547b04d6d6SSong Liu 	.gpl_only	= true,
5557b04d6d6SSong Liu 	.ret_type	= RET_INTEGER,
5567b04d6d6SSong Liu 	.arg1_type	= ARG_PTR_TO_CTX,
5577b04d6d6SSong Liu 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
5587b04d6d6SSong Liu 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
5597b04d6d6SSong Liu 	.arg4_type	= ARG_ANYTHING,
5607b04d6d6SSong Liu };
5617b04d6d6SSong Liu 
562557c0c6eSAlexei Starovoitov /* Called from eBPF program */
563d5a3b1f6SAlexei Starovoitov static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
564d5a3b1f6SAlexei Starovoitov {
5653b4a63f6SPrashant Bhole 	return ERR_PTR(-EOPNOTSUPP);
566557c0c6eSAlexei Starovoitov }
567557c0c6eSAlexei Starovoitov 
568557c0c6eSAlexei Starovoitov /* Called from syscall */
569557c0c6eSAlexei Starovoitov int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
570557c0c6eSAlexei Starovoitov {
571d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
572557c0c6eSAlexei Starovoitov 	struct stack_map_bucket *bucket, *old_bucket;
573557c0c6eSAlexei Starovoitov 	u32 id = *(u32 *)key, trace_len;
574d5a3b1f6SAlexei Starovoitov 
575d5a3b1f6SAlexei Starovoitov 	if (unlikely(id >= smap->n_buckets))
576557c0c6eSAlexei Starovoitov 		return -ENOENT;
577557c0c6eSAlexei Starovoitov 
578557c0c6eSAlexei Starovoitov 	bucket = xchg(&smap->buckets[id], NULL);
579557c0c6eSAlexei Starovoitov 	if (!bucket)
580557c0c6eSAlexei Starovoitov 		return -ENOENT;
581557c0c6eSAlexei Starovoitov 
582615755a7SSong Liu 	trace_len = bucket->nr * stack_map_data_size(map);
583615755a7SSong Liu 	memcpy(value, bucket->data, trace_len);
584557c0c6eSAlexei Starovoitov 	memset(value + trace_len, 0, map->value_size - trace_len);
585557c0c6eSAlexei Starovoitov 
586557c0c6eSAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], bucket);
587557c0c6eSAlexei Starovoitov 	if (old_bucket)
588557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
589557c0c6eSAlexei Starovoitov 	return 0;
590d5a3b1f6SAlexei Starovoitov }
591d5a3b1f6SAlexei Starovoitov 
59216f07c55SYonghong Song static int stack_map_get_next_key(struct bpf_map *map, void *key,
59316f07c55SYonghong Song 				  void *next_key)
594d5a3b1f6SAlexei Starovoitov {
59516f07c55SYonghong Song 	struct bpf_stack_map *smap = container_of(map,
59616f07c55SYonghong Song 						  struct bpf_stack_map, map);
59716f07c55SYonghong Song 	u32 id;
59816f07c55SYonghong Song 
59916f07c55SYonghong Song 	WARN_ON_ONCE(!rcu_read_lock_held());
60016f07c55SYonghong Song 
60116f07c55SYonghong Song 	if (!key) {
60216f07c55SYonghong Song 		id = 0;
60316f07c55SYonghong Song 	} else {
60416f07c55SYonghong Song 		id = *(u32 *)key;
60516f07c55SYonghong Song 		if (id >= smap->n_buckets || !smap->buckets[id])
60616f07c55SYonghong Song 			id = 0;
60716f07c55SYonghong Song 		else
60816f07c55SYonghong Song 			id++;
60916f07c55SYonghong Song 	}
61016f07c55SYonghong Song 
61116f07c55SYonghong Song 	while (id < smap->n_buckets && !smap->buckets[id])
61216f07c55SYonghong Song 		id++;
61316f07c55SYonghong Song 
61416f07c55SYonghong Song 	if (id >= smap->n_buckets)
61516f07c55SYonghong Song 		return -ENOENT;
61616f07c55SYonghong Song 
61716f07c55SYonghong Song 	*(u32 *)next_key = id;
61816f07c55SYonghong Song 	return 0;
619d5a3b1f6SAlexei Starovoitov }
620d5a3b1f6SAlexei Starovoitov 
621d5a3b1f6SAlexei Starovoitov static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
622d5a3b1f6SAlexei Starovoitov 				 u64 map_flags)
623d5a3b1f6SAlexei Starovoitov {
624d5a3b1f6SAlexei Starovoitov 	return -EINVAL;
625d5a3b1f6SAlexei Starovoitov }
626d5a3b1f6SAlexei Starovoitov 
627d5a3b1f6SAlexei Starovoitov /* Called from syscall or from eBPF program */
628d5a3b1f6SAlexei Starovoitov static int stack_map_delete_elem(struct bpf_map *map, void *key)
629d5a3b1f6SAlexei Starovoitov {
630d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
631d5a3b1f6SAlexei Starovoitov 	struct stack_map_bucket *old_bucket;
632d5a3b1f6SAlexei Starovoitov 	u32 id = *(u32 *)key;
633d5a3b1f6SAlexei Starovoitov 
634d5a3b1f6SAlexei Starovoitov 	if (unlikely(id >= smap->n_buckets))
635d5a3b1f6SAlexei Starovoitov 		return -E2BIG;
636d5a3b1f6SAlexei Starovoitov 
637d5a3b1f6SAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], NULL);
638d5a3b1f6SAlexei Starovoitov 	if (old_bucket) {
639557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
640d5a3b1f6SAlexei Starovoitov 		return 0;
641d5a3b1f6SAlexei Starovoitov 	} else {
642d5a3b1f6SAlexei Starovoitov 		return -ENOENT;
643d5a3b1f6SAlexei Starovoitov 	}
644d5a3b1f6SAlexei Starovoitov }
645d5a3b1f6SAlexei Starovoitov 
646d5a3b1f6SAlexei Starovoitov /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
647d5a3b1f6SAlexei Starovoitov static void stack_map_free(struct bpf_map *map)
648d5a3b1f6SAlexei Starovoitov {
649d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
650d5a3b1f6SAlexei Starovoitov 
651d407bd25SDaniel Borkmann 	bpf_map_area_free(smap->elems);
652557c0c6eSAlexei Starovoitov 	pcpu_freelist_destroy(&smap->freelist);
653d407bd25SDaniel Borkmann 	bpf_map_area_free(smap);
654d5a3b1f6SAlexei Starovoitov 	put_callchain_buffers();
655d5a3b1f6SAlexei Starovoitov }
656d5a3b1f6SAlexei Starovoitov 
6572872e9acSAndrey Ignatov static int stack_trace_map_btf_id;
65814499160SMauricio Vasquez B const struct bpf_map_ops stack_trace_map_ops = {
659f4d05259SMartin KaFai Lau 	.map_meta_equal = bpf_map_meta_equal,
660d5a3b1f6SAlexei Starovoitov 	.map_alloc = stack_map_alloc,
661d5a3b1f6SAlexei Starovoitov 	.map_free = stack_map_free,
662d5a3b1f6SAlexei Starovoitov 	.map_get_next_key = stack_map_get_next_key,
663d5a3b1f6SAlexei Starovoitov 	.map_lookup_elem = stack_map_lookup_elem,
664d5a3b1f6SAlexei Starovoitov 	.map_update_elem = stack_map_update_elem,
665d5a3b1f6SAlexei Starovoitov 	.map_delete_elem = stack_map_delete_elem,
666e8d2bec0SDaniel Borkmann 	.map_check_btf = map_check_no_btf,
6672872e9acSAndrey Ignatov 	.map_btf_name = "bpf_stack_map",
6682872e9acSAndrey Ignatov 	.map_btf_id = &stack_trace_map_btf_id,
669d5a3b1f6SAlexei Starovoitov };
670