stackmap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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/btf_ids.h>
  11. #include <linux/buildid.h>
  12. #include "percpu_freelist.h"
  13. #include "mmap_unlock_work.h"
  14. #define STACK_CREATE_FLAG_MASK \
  15. (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY | \
  16. BPF_F_STACK_BUILD_ID)
  17. struct stack_map_bucket {
  18. struct pcpu_freelist_node fnode;
  19. u32 hash;
  20. u32 nr;
  21. u64 data[];
  22. };
  23. struct bpf_stack_map {
  24. struct bpf_map map;
  25. void *elems;
  26. struct pcpu_freelist freelist;
  27. u32 n_buckets;
  28. struct stack_map_bucket *buckets[] __counted_by(n_buckets);
  29. };
  30. static inline bool stack_map_use_build_id(struct bpf_map *map)
  31. {
  32. return (map->map_flags & BPF_F_STACK_BUILD_ID);
  33. }
  34. static inline int stack_map_data_size(struct bpf_map *map)
  35. {
  36. return stack_map_use_build_id(map) ?
  37. sizeof(struct bpf_stack_build_id) : sizeof(u64);
  38. }
  39. static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
  40. {
  41. u64 elem_size = sizeof(struct stack_map_bucket) +
  42. (u64)smap->map.value_size;
  43. int err;
  44. smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
  45. smap->map.numa_node);
  46. if (!smap->elems)
  47. return -ENOMEM;
  48. err = pcpu_freelist_init(&smap->freelist);
  49. if (err)
  50. goto free_elems;
  51. pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
  52. smap->map.max_entries);
  53. return 0;
  54. free_elems:
  55. bpf_map_area_free(smap->elems);
  56. return err;
  57. }
  58. /* Called from syscall */
  59. static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
  60. {
  61. u32 value_size = attr->value_size;
  62. struct bpf_stack_map *smap;
  63. u64 cost, n_buckets;
  64. int err;
  65. if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
  66. return ERR_PTR(-EINVAL);
  67. /* check sanity of attributes */
  68. if (attr->max_entries == 0 || attr->key_size != 4 ||
  69. value_size < 8 || value_size % 8)
  70. return ERR_PTR(-EINVAL);
  71. BUILD_BUG_ON(sizeof(struct bpf_stack_build_id) % sizeof(u64));
  72. if (attr->map_flags & BPF_F_STACK_BUILD_ID) {
  73. if (value_size % sizeof(struct bpf_stack_build_id) ||
  74. value_size / sizeof(struct bpf_stack_build_id)
  75. > sysctl_perf_event_max_stack)
  76. return ERR_PTR(-EINVAL);
  77. } else if (value_size / 8 > sysctl_perf_event_max_stack)
  78. return ERR_PTR(-EINVAL);
  79. /* hash table size must be power of 2; roundup_pow_of_two() can overflow
  80. * into UB on 32-bit arches, so check that first
  81. */
  82. if (attr->max_entries > 1UL << 31)
  83. return ERR_PTR(-E2BIG);
  84. n_buckets = roundup_pow_of_two(attr->max_entries);
  85. cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
  86. smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
  87. if (!smap)
  88. return ERR_PTR(-ENOMEM);
  89. bpf_map_init_from_attr(&smap->map, attr);
  90. smap->n_buckets = n_buckets;
  91. err = get_callchain_buffers(sysctl_perf_event_max_stack);
  92. if (err)
  93. goto free_smap;
  94. err = prealloc_elems_and_freelist(smap);
  95. if (err)
  96. goto put_buffers;
  97. return &smap->map;
  98. put_buffers:
  99. put_callchain_buffers();
  100. free_smap:
  101. bpf_map_area_free(smap);
  102. return ERR_PTR(err);
  103. }
  104. static int fetch_build_id(struct vm_area_struct *vma, unsigned char *build_id, bool may_fault)
  105. {
  106. return may_fault ? build_id_parse(vma, build_id, NULL)
  107. : build_id_parse_nofault(vma, build_id, NULL);
  108. }
  109. /*
  110. * Expects all id_offs[i].ip values to be set to correct initial IPs.
  111. * They will be subsequently:
  112. * - either adjusted in place to a file offset, if build ID fetching
  113. * succeeds; in this case id_offs[i].build_id is set to correct build ID,
  114. * and id_offs[i].status is set to BPF_STACK_BUILD_ID_VALID;
  115. * - or IP will be kept intact, if build ID fetching failed; in this case
  116. * id_offs[i].build_id is zeroed out and id_offs[i].status is set to
  117. * BPF_STACK_BUILD_ID_IP.
  118. */
  119. static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
  120. u32 trace_nr, bool user, bool may_fault)
  121. {
  122. int i;
  123. struct mmap_unlock_irq_work *work = NULL;
  124. bool irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
  125. struct vm_area_struct *vma, *prev_vma = NULL;
  126. const char *prev_build_id;
  127. /* If the irq_work is in use, fall back to report ips. Same
  128. * fallback is used for kernel stack (!user) on a stackmap with
  129. * build_id.
  130. */
  131. if (!user || !current || !current->mm || irq_work_busy ||
  132. !mmap_read_trylock(current->mm)) {
  133. /* cannot access current->mm, fall back to ips */
  134. for (i = 0; i < trace_nr; i++) {
  135. id_offs[i].status = BPF_STACK_BUILD_ID_IP;
  136. memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
  137. }
  138. return;
  139. }
  140. for (i = 0; i < trace_nr; i++) {
  141. u64 ip = READ_ONCE(id_offs[i].ip);
  142. if (range_in_vma(prev_vma, ip, ip)) {
  143. vma = prev_vma;
  144. memcpy(id_offs[i].build_id, prev_build_id, BUILD_ID_SIZE_MAX);
  145. goto build_id_valid;
  146. }
  147. vma = find_vma(current->mm, ip);
  148. if (!vma || fetch_build_id(vma, id_offs[i].build_id, may_fault)) {
  149. /* per entry fall back to ips */
  150. id_offs[i].status = BPF_STACK_BUILD_ID_IP;
  151. memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
  152. continue;
  153. }
  154. build_id_valid:
  155. id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ip - vma->vm_start;
  156. id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
  157. prev_vma = vma;
  158. prev_build_id = id_offs[i].build_id;
  159. }
  160. bpf_mmap_unlock_mm(work, current->mm);
  161. }
  162. static struct perf_callchain_entry *
  163. get_callchain_entry_for_task(struct task_struct *task, u32 max_depth)
  164. {
  165. #ifdef CONFIG_STACKTRACE
  166. struct perf_callchain_entry *entry;
  167. int rctx;
  168. entry = get_callchain_entry(&rctx);
  169. if (!entry)
  170. return NULL;
  171. entry->nr = stack_trace_save_tsk(task, (unsigned long *)entry->ip,
  172. max_depth, 0);
  173. /* stack_trace_save_tsk() works on unsigned long array, while
  174. * perf_callchain_entry uses u64 array. For 32-bit systems, it is
  175. * necessary to fix this mismatch.
  176. */
  177. if (__BITS_PER_LONG != 64) {
  178. unsigned long *from = (unsigned long *) entry->ip;
  179. u64 *to = entry->ip;
  180. int i;
  181. /* copy data from the end to avoid using extra buffer */
  182. for (i = entry->nr - 1; i >= 0; i--)
  183. to[i] = (u64)(from[i]);
  184. }
  185. put_callchain_entry(rctx);
  186. return entry;
  187. #else /* CONFIG_STACKTRACE */
  188. return NULL;
  189. #endif
  190. }
  191. static long __bpf_get_stackid(struct bpf_map *map,
  192. struct perf_callchain_entry *trace, u64 flags)
  193. {
  194. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  195. struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
  196. u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
  197. u32 hash, id, trace_nr, trace_len, i;
  198. bool user = flags & BPF_F_USER_STACK;
  199. u64 *ips;
  200. bool hash_matches;
  201. if (trace->nr <= skip)
  202. /* skipping more than usable stack trace */
  203. return -EFAULT;
  204. trace_nr = trace->nr - skip;
  205. trace_len = trace_nr * sizeof(u64);
  206. ips = trace->ip + skip;
  207. hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
  208. id = hash & (smap->n_buckets - 1);
  209. bucket = READ_ONCE(smap->buckets[id]);
  210. hash_matches = bucket && bucket->hash == hash;
  211. /* fast cmp */
  212. if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
  213. return id;
  214. if (stack_map_use_build_id(map)) {
  215. struct bpf_stack_build_id *id_offs;
  216. /* for build_id+offset, pop a bucket before slow cmp */
  217. new_bucket = (struct stack_map_bucket *)
  218. pcpu_freelist_pop(&smap->freelist);
  219. if (unlikely(!new_bucket))
  220. return -ENOMEM;
  221. new_bucket->nr = trace_nr;
  222. id_offs = (struct bpf_stack_build_id *)new_bucket->data;
  223. for (i = 0; i < trace_nr; i++)
  224. id_offs[i].ip = ips[i];
  225. stack_map_get_build_id_offset(id_offs, trace_nr, user, false /* !may_fault */);
  226. trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
  227. if (hash_matches && bucket->nr == trace_nr &&
  228. memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
  229. pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
  230. return id;
  231. }
  232. if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
  233. pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
  234. return -EEXIST;
  235. }
  236. } else {
  237. if (hash_matches && bucket->nr == trace_nr &&
  238. memcmp(bucket->data, ips, trace_len) == 0)
  239. return id;
  240. if (bucket && !(flags & BPF_F_REUSE_STACKID))
  241. return -EEXIST;
  242. new_bucket = (struct stack_map_bucket *)
  243. pcpu_freelist_pop(&smap->freelist);
  244. if (unlikely(!new_bucket))
  245. return -ENOMEM;
  246. memcpy(new_bucket->data, ips, trace_len);
  247. }
  248. new_bucket->hash = hash;
  249. new_bucket->nr = trace_nr;
  250. old_bucket = xchg(&smap->buckets[id], new_bucket);
  251. if (old_bucket)
  252. pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
  253. return id;
  254. }
  255. BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
  256. u64, flags)
  257. {
  258. u32 max_depth = map->value_size / stack_map_data_size(map);
  259. u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
  260. bool user = flags & BPF_F_USER_STACK;
  261. struct perf_callchain_entry *trace;
  262. bool kernel = !user;
  263. if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
  264. BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
  265. return -EINVAL;
  266. max_depth += skip;
  267. if (max_depth > sysctl_perf_event_max_stack)
  268. max_depth = sysctl_perf_event_max_stack;
  269. trace = get_perf_callchain(regs, 0, kernel, user, max_depth,
  270. false, false);
  271. if (unlikely(!trace))
  272. /* couldn't fetch the stack trace */
  273. return -EFAULT;
  274. return __bpf_get_stackid(map, trace, flags);
  275. }
  276. const struct bpf_func_proto bpf_get_stackid_proto = {
  277. .func = bpf_get_stackid,
  278. .gpl_only = true,
  279. .ret_type = RET_INTEGER,
  280. .arg1_type = ARG_PTR_TO_CTX,
  281. .arg2_type = ARG_CONST_MAP_PTR,
  282. .arg3_type = ARG_ANYTHING,
  283. };
  284. static __u64 count_kernel_ip(struct perf_callchain_entry *trace)
  285. {
  286. __u64 nr_kernel = 0;
  287. while (nr_kernel < trace->nr) {
  288. if (trace->ip[nr_kernel] == PERF_CONTEXT_USER)
  289. break;
  290. nr_kernel++;
  291. }
  292. return nr_kernel;
  293. }
  294. BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
  295. struct bpf_map *, map, u64, flags)
  296. {
  297. struct perf_event *event = ctx->event;
  298. struct perf_callchain_entry *trace;
  299. bool kernel, user;
  300. __u64 nr_kernel;
  301. int ret;
  302. /* perf_sample_data doesn't have callchain, use bpf_get_stackid */
  303. if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN))
  304. return bpf_get_stackid((unsigned long)(ctx->regs),
  305. (unsigned long) map, flags, 0, 0);
  306. if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
  307. BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
  308. return -EINVAL;
  309. user = flags & BPF_F_USER_STACK;
  310. kernel = !user;
  311. trace = ctx->data->callchain;
  312. if (unlikely(!trace))
  313. return -EFAULT;
  314. nr_kernel = count_kernel_ip(trace);
  315. if (kernel) {
  316. __u64 nr = trace->nr;
  317. trace->nr = nr_kernel;
  318. ret = __bpf_get_stackid(map, trace, flags);
  319. /* restore nr */
  320. trace->nr = nr;
  321. } else { /* user */
  322. u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
  323. skip += nr_kernel;
  324. if (skip > BPF_F_SKIP_FIELD_MASK)
  325. return -EFAULT;
  326. flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
  327. ret = __bpf_get_stackid(map, trace, flags);
  328. }
  329. return ret;
  330. }
  331. const struct bpf_func_proto bpf_get_stackid_proto_pe = {
  332. .func = bpf_get_stackid_pe,
  333. .gpl_only = false,
  334. .ret_type = RET_INTEGER,
  335. .arg1_type = ARG_PTR_TO_CTX,
  336. .arg2_type = ARG_CONST_MAP_PTR,
  337. .arg3_type = ARG_ANYTHING,
  338. };
  339. static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
  340. struct perf_callchain_entry *trace_in,
  341. void *buf, u32 size, u64 flags, bool may_fault)
  342. {
  343. u32 trace_nr, copy_len, elem_size, num_elem, max_depth;
  344. bool user_build_id = flags & BPF_F_USER_BUILD_ID;
  345. bool crosstask = task && task != current;
  346. u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
  347. bool user = flags & BPF_F_USER_STACK;
  348. struct perf_callchain_entry *trace;
  349. bool kernel = !user;
  350. int err = -EINVAL;
  351. u64 *ips;
  352. if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
  353. BPF_F_USER_BUILD_ID)))
  354. goto clear;
  355. if (kernel && user_build_id)
  356. goto clear;
  357. elem_size = user_build_id ? sizeof(struct bpf_stack_build_id) : sizeof(u64);
  358. if (unlikely(size % elem_size))
  359. goto clear;
  360. /* cannot get valid user stack for task without user_mode regs */
  361. if (task && user && !user_mode(regs))
  362. goto err_fault;
  363. /* get_perf_callchain does not support crosstask user stack walking
  364. * but returns an empty stack instead of NULL.
  365. */
  366. if (crosstask && user) {
  367. err = -EOPNOTSUPP;
  368. goto clear;
  369. }
  370. num_elem = size / elem_size;
  371. max_depth = num_elem + skip;
  372. if (sysctl_perf_event_max_stack < max_depth)
  373. max_depth = sysctl_perf_event_max_stack;
  374. if (may_fault)
  375. rcu_read_lock(); /* need RCU for perf's callchain below */
  376. if (trace_in)
  377. trace = trace_in;
  378. else if (kernel && task)
  379. trace = get_callchain_entry_for_task(task, max_depth);
  380. else
  381. trace = get_perf_callchain(regs, 0, kernel, user, max_depth,
  382. crosstask, false);
  383. if (unlikely(!trace) || trace->nr < skip) {
  384. if (may_fault)
  385. rcu_read_unlock();
  386. goto err_fault;
  387. }
  388. trace_nr = trace->nr - skip;
  389. trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
  390. copy_len = trace_nr * elem_size;
  391. ips = trace->ip + skip;
  392. if (user_build_id) {
  393. struct bpf_stack_build_id *id_offs = buf;
  394. u32 i;
  395. for (i = 0; i < trace_nr; i++)
  396. id_offs[i].ip = ips[i];
  397. } else {
  398. memcpy(buf, ips, copy_len);
  399. }
  400. /* trace/ips should not be dereferenced after this point */
  401. if (may_fault)
  402. rcu_read_unlock();
  403. if (user_build_id)
  404. stack_map_get_build_id_offset(buf, trace_nr, user, may_fault);
  405. if (size > copy_len)
  406. memset(buf + copy_len, 0, size - copy_len);
  407. return copy_len;
  408. err_fault:
  409. err = -EFAULT;
  410. clear:
  411. memset(buf, 0, size);
  412. return err;
  413. }
  414. BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
  415. u64, flags)
  416. {
  417. return __bpf_get_stack(regs, NULL, NULL, buf, size, flags, false /* !may_fault */);
  418. }
  419. const struct bpf_func_proto bpf_get_stack_proto = {
  420. .func = bpf_get_stack,
  421. .gpl_only = true,
  422. .ret_type = RET_INTEGER,
  423. .arg1_type = ARG_PTR_TO_CTX,
  424. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  425. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  426. .arg4_type = ARG_ANYTHING,
  427. };
  428. BPF_CALL_4(bpf_get_stack_sleepable, struct pt_regs *, regs, void *, buf, u32, size,
  429. u64, flags)
  430. {
  431. return __bpf_get_stack(regs, NULL, NULL, buf, size, flags, true /* may_fault */);
  432. }
  433. const struct bpf_func_proto bpf_get_stack_sleepable_proto = {
  434. .func = bpf_get_stack_sleepable,
  435. .gpl_only = true,
  436. .ret_type = RET_INTEGER,
  437. .arg1_type = ARG_PTR_TO_CTX,
  438. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  439. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  440. .arg4_type = ARG_ANYTHING,
  441. };
  442. static long __bpf_get_task_stack(struct task_struct *task, void *buf, u32 size,
  443. u64 flags, bool may_fault)
  444. {
  445. struct pt_regs *regs;
  446. long res = -EINVAL;
  447. if (!try_get_task_stack(task))
  448. return -EFAULT;
  449. regs = task_pt_regs(task);
  450. if (regs)
  451. res = __bpf_get_stack(regs, task, NULL, buf, size, flags, may_fault);
  452. put_task_stack(task);
  453. return res;
  454. }
  455. BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
  456. u32, size, u64, flags)
  457. {
  458. return __bpf_get_task_stack(task, buf, size, flags, false /* !may_fault */);
  459. }
  460. const struct bpf_func_proto bpf_get_task_stack_proto = {
  461. .func = bpf_get_task_stack,
  462. .gpl_only = false,
  463. .ret_type = RET_INTEGER,
  464. .arg1_type = ARG_PTR_TO_BTF_ID,
  465. .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  466. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  467. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  468. .arg4_type = ARG_ANYTHING,
  469. };
  470. BPF_CALL_4(bpf_get_task_stack_sleepable, struct task_struct *, task, void *, buf,
  471. u32, size, u64, flags)
  472. {
  473. return __bpf_get_task_stack(task, buf, size, flags, true /* !may_fault */);
  474. }
  475. const struct bpf_func_proto bpf_get_task_stack_sleepable_proto = {
  476. .func = bpf_get_task_stack_sleepable,
  477. .gpl_only = false,
  478. .ret_type = RET_INTEGER,
  479. .arg1_type = ARG_PTR_TO_BTF_ID,
  480. .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  481. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  482. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  483. .arg4_type = ARG_ANYTHING,
  484. };
  485. BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
  486. void *, buf, u32, size, u64, flags)
  487. {
  488. struct pt_regs *regs = (struct pt_regs *)(ctx->regs);
  489. struct perf_event *event = ctx->event;
  490. struct perf_callchain_entry *trace;
  491. bool kernel, user;
  492. int err = -EINVAL;
  493. __u64 nr_kernel;
  494. if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN))
  495. return __bpf_get_stack(regs, NULL, NULL, buf, size, flags, false /* !may_fault */);
  496. if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
  497. BPF_F_USER_BUILD_ID)))
  498. goto clear;
  499. user = flags & BPF_F_USER_STACK;
  500. kernel = !user;
  501. err = -EFAULT;
  502. trace = ctx->data->callchain;
  503. if (unlikely(!trace))
  504. goto clear;
  505. nr_kernel = count_kernel_ip(trace);
  506. if (kernel) {
  507. __u64 nr = trace->nr;
  508. trace->nr = nr_kernel;
  509. err = __bpf_get_stack(regs, NULL, trace, buf, size, flags, false /* !may_fault */);
  510. /* restore nr */
  511. trace->nr = nr;
  512. } else { /* user */
  513. u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
  514. skip += nr_kernel;
  515. if (skip > BPF_F_SKIP_FIELD_MASK)
  516. goto clear;
  517. flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
  518. err = __bpf_get_stack(regs, NULL, trace, buf, size, flags, false /* !may_fault */);
  519. }
  520. return err;
  521. clear:
  522. memset(buf, 0, size);
  523. return err;
  524. }
  525. const struct bpf_func_proto bpf_get_stack_proto_pe = {
  526. .func = bpf_get_stack_pe,
  527. .gpl_only = true,
  528. .ret_type = RET_INTEGER,
  529. .arg1_type = ARG_PTR_TO_CTX,
  530. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  531. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  532. .arg4_type = ARG_ANYTHING,
  533. };
  534. /* Called from eBPF program */
  535. static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
  536. {
  537. return ERR_PTR(-EOPNOTSUPP);
  538. }
  539. /* Called from syscall */
  540. int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
  541. {
  542. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  543. struct stack_map_bucket *bucket, *old_bucket;
  544. u32 id = *(u32 *)key, trace_len;
  545. if (unlikely(id >= smap->n_buckets))
  546. return -ENOENT;
  547. bucket = xchg(&smap->buckets[id], NULL);
  548. if (!bucket)
  549. return -ENOENT;
  550. trace_len = bucket->nr * stack_map_data_size(map);
  551. memcpy(value, bucket->data, trace_len);
  552. memset(value + trace_len, 0, map->value_size - trace_len);
  553. old_bucket = xchg(&smap->buckets[id], bucket);
  554. if (old_bucket)
  555. pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
  556. return 0;
  557. }
  558. static int stack_map_get_next_key(struct bpf_map *map, void *key,
  559. void *next_key)
  560. {
  561. struct bpf_stack_map *smap = container_of(map,
  562. struct bpf_stack_map, map);
  563. u32 id;
  564. WARN_ON_ONCE(!rcu_read_lock_held());
  565. if (!key) {
  566. id = 0;
  567. } else {
  568. id = *(u32 *)key;
  569. if (id >= smap->n_buckets || !smap->buckets[id])
  570. id = 0;
  571. else
  572. id++;
  573. }
  574. while (id < smap->n_buckets && !smap->buckets[id])
  575. id++;
  576. if (id >= smap->n_buckets)
  577. return -ENOENT;
  578. *(u32 *)next_key = id;
  579. return 0;
  580. }
  581. static long stack_map_update_elem(struct bpf_map *map, void *key, void *value,
  582. u64 map_flags)
  583. {
  584. return -EINVAL;
  585. }
  586. /* Called from syscall or from eBPF program */
  587. static long stack_map_delete_elem(struct bpf_map *map, void *key)
  588. {
  589. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  590. struct stack_map_bucket *old_bucket;
  591. u32 id = *(u32 *)key;
  592. if (unlikely(id >= smap->n_buckets))
  593. return -E2BIG;
  594. old_bucket = xchg(&smap->buckets[id], NULL);
  595. if (old_bucket) {
  596. pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
  597. return 0;
  598. } else {
  599. return -ENOENT;
  600. }
  601. }
  602. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  603. static void stack_map_free(struct bpf_map *map)
  604. {
  605. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  606. bpf_map_area_free(smap->elems);
  607. pcpu_freelist_destroy(&smap->freelist);
  608. bpf_map_area_free(smap);
  609. put_callchain_buffers();
  610. }
  611. static u64 stack_map_mem_usage(const struct bpf_map *map)
  612. {
  613. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  614. u64 value_size = map->value_size;
  615. u64 n_buckets = smap->n_buckets;
  616. u64 enties = map->max_entries;
  617. u64 usage = sizeof(*smap);
  618. usage += n_buckets * sizeof(struct stack_map_bucket *);
  619. usage += enties * (sizeof(struct stack_map_bucket) + value_size);
  620. return usage;
  621. }
  622. BTF_ID_LIST_SINGLE(stack_trace_map_btf_ids, struct, bpf_stack_map)
  623. const struct bpf_map_ops stack_trace_map_ops = {
  624. .map_meta_equal = bpf_map_meta_equal,
  625. .map_alloc = stack_map_alloc,
  626. .map_free = stack_map_free,
  627. .map_get_next_key = stack_map_get_next_key,
  628. .map_lookup_elem = stack_map_lookup_elem,
  629. .map_update_elem = stack_map_update_elem,
  630. .map_delete_elem = stack_map_delete_elem,
  631. .map_check_btf = map_check_no_btf,
  632. .map_mem_usage = stack_map_mem_usage,
  633. .map_btf_id = &stack_trace_map_btf_ids[0],
  634. };