1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Facebook
8 #include <linux/ptrace.h>
9 #include <linux/sched.h>
10 #include <linux/types.h>
11 #include <bpf/bpf_helpers.h>
13 typedef uint32_t pid_t;
14 struct task_struct {};
16 #define TASK_COMM_LEN 16
17 #define PERF_MAX_STACK_DEPTH 127
19 #define STROBE_TYPE_INVALID 0
20 #define STROBE_TYPE_INT 1
21 #define STROBE_TYPE_STR 2
22 #define STROBE_TYPE_MAP 3
24 #define STACK_TABLE_EPOCH_SHIFT 20
25 #define STROBE_MAX_STR_LEN 1
26 #define STROBE_MAX_CFGS 32
27 #define READ_MAP_VAR_PAYLOAD_CAP \
28 ((1 + STROBE_MAX_MAP_ENTRIES * 2) * STROBE_MAX_STR_LEN)
29 #define STROBE_MAX_PAYLOAD \
30 (STROBE_MAX_STRS * STROBE_MAX_STR_LEN + \
31 STROBE_MAX_MAPS * READ_MAP_VAR_PAYLOAD_CAP)
33 struct strobe_value_header {
35 * meaning depends on type:
36 * 1. int: 0, if value not set, 1 otherwise
37 * 2. str: 1 always, whether value is set or not is determined by ptr
38 * 3. map: 1 always, pointer points to additional struct with number
39 * of entries (up to STROBE_MAX_MAP_ENTRIES)
43 * _reserved might be used for some future fields/flags, but we always
44 * want to keep strobe_value_header to be 8 bytes, so BPF can read 16
45 * bytes in one go and get both header and value
51 * strobe_value_generic is used from BPF probe only, but needs to be a union
52 * of strobe_value_int/strobe_value_str/strobe_value_map
54 struct strobe_value_generic {
55 struct strobe_value_header header;
62 struct strobe_value_int {
63 struct strobe_value_header header;
67 struct strobe_value_str {
68 struct strobe_value_header header;
72 struct strobe_value_map {
73 struct strobe_value_header header;
74 const struct strobe_map_raw* value;
77 struct strobe_map_entry {
83 * Map of C-string key/value pairs with fixed maximum capacity. Each map has
84 * corresponding int64 ID, which application can use (or ignore) in whatever
85 * way appropriate. Map is "write-only", there is no way to get data out of
86 * map. Map is intended to be used to provide metadata for profilers and is
87 * not to be used for internal in-app communication. All methods are
90 struct strobe_map_raw {
92 * general purpose unique ID that's up to application to decide
93 * whether and how to use; for request metadata use case id is unique
94 * request ID that's used to match metadata with stack traces on
95 * Strobelight backend side
98 /* number of used entries in map */
101 * having volatile doesn't change anything on BPF side, but clang
102 * emits warnings for passing `volatile const char *` into
103 * bpf_probe_read_user_str that expects just `const char *`
107 * key/value entries, each consisting of 2 pointers to key and value
110 struct strobe_map_entry entries[STROBE_MAX_MAP_ENTRIES];
113 /* Following values define supported values of TLS mode */
114 #define TLS_NOT_SET -1
115 #define TLS_LOCAL_EXEC 0
116 #define TLS_IMM_EXEC 1
117 #define TLS_GENERAL_DYN 2
120 * structure that universally represents TLS location (both for static
121 * executables and shared libraries)
123 struct strobe_value_loc {
125 * tls_mode defines what TLS mode was used for particular metavariable:
126 * - -1 (TLS_NOT_SET) - no metavariable;
127 * - 0 (TLS_LOCAL_EXEC) - Local Executable mode;
128 * - 1 (TLS_IMM_EXEC) - Immediate Executable mode;
129 * - 2 (TLS_GENERAL_DYN) - General Dynamic mode;
130 * Local Dynamic mode is not yet supported, because never seen in
131 * practice. Mode defines how offset field is interpreted. See
132 * calc_location() in below for details.
136 * TLS_LOCAL_EXEC: offset from thread pointer (fs:0 for x86-64,
137 * tpidr_el0 for aarch64).
138 * TLS_IMM_EXEC: absolute address of GOT entry containing offset
139 * from thread pointer;
140 * TLS_GENERAL_DYN: absolute address of double GOT entry
141 * containing tls_index_t struct;
146 struct strobemeta_cfg {
147 int64_t req_meta_idx;
148 struct strobe_value_loc int_locs[STROBE_MAX_INTS];
149 struct strobe_value_loc str_locs[STROBE_MAX_STRS];
150 struct strobe_value_loc map_locs[STROBE_MAX_MAPS];
153 struct strobe_map_descr {
157 * cnt <0 - map value isn't set;
158 * 0 - map has id set, but no key/value entries
162 * both key_lens[i] and val_lens[i] should be >0 for present key/value
165 uint16_t key_lens[STROBE_MAX_MAP_ENTRIES];
166 uint16_t val_lens[STROBE_MAX_MAP_ENTRIES];
169 struct strobemeta_payload {
170 /* req_id has valid request ID, if req_meta_valid == 1 */
172 uint8_t req_meta_valid;
174 * mask has Nth bit set to 1, if Nth metavar was present and
177 uint64_t int_vals_set_mask;
178 int64_t int_vals[STROBE_MAX_INTS];
179 /* len is >0 for present values */
180 uint16_t str_lens[STROBE_MAX_STRS];
181 /* if map_descrs[i].cnt == -1, metavar is not present/set */
182 struct strobe_map_descr map_descrs[STROBE_MAX_MAPS];
184 * payload has compactly packed values of str and map variables in the
185 * form: strval1\0strval2\0map1key1\0map1val1\0map2key1\0map2val1\0
186 * (and so on); str_lens[i], key_lens[i] and val_lens[i] determines
189 char payload[STROBE_MAX_PAYLOAD];
192 struct strobelight_bpf_sample {
194 char comm[TASK_COMM_LEN];
199 struct strobemeta_payload metadata;
201 * makes it possible to pass (<real payload size> + 1) as data size to
202 * perf_submit() to avoid perf_submit's paranoia about passing zero as
203 * size, as it deduces that <real payload size> might be
204 * **theoretically** zero
206 char dummy_safeguard;
210 __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
211 __uint(max_entries, 32);
212 __uint(key_size, sizeof(int));
213 __uint(value_size, sizeof(int));
214 } samples SEC(".maps");
217 __uint(type, BPF_MAP_TYPE_STACK_TRACE);
218 __uint(max_entries, 16);
219 __uint(key_size, sizeof(uint32_t));
220 __uint(value_size, sizeof(uint64_t) * PERF_MAX_STACK_DEPTH);
221 } stacks_0 SEC(".maps");
224 __uint(type, BPF_MAP_TYPE_STACK_TRACE);
225 __uint(max_entries, 16);
226 __uint(key_size, sizeof(uint32_t));
227 __uint(value_size, sizeof(uint64_t) * PERF_MAX_STACK_DEPTH);
228 } stacks_1 SEC(".maps");
231 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
232 __uint(max_entries, 1);
233 __type(key, uint32_t);
234 __type(value, struct strobelight_bpf_sample);
235 } sample_heap SEC(".maps");
238 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
239 __uint(max_entries, STROBE_MAX_CFGS);
241 __type(value, struct strobemeta_cfg);
242 } strobemeta_cfgs SEC(".maps");
244 /* Type for the dtv. */
245 /* https://github.com/lattera/glibc/blob/master/nptl/sysdeps/x86_64/tls.h#L34 */
254 /* Partial definition for tcbhead_t */
255 /* https://github.com/bminor/glibc/blob/master/sysdeps/x86_64/nptl/tls.h#L42 */
262 * TLS module/offset information for shared library case.
263 * For x86-64, this is mapped onto two entries in GOT.
264 * For aarch64, this is pointed to by second GOT entry.
276 static void *calc_location(struct strobe_value_loc *loc, void *tls_base)
280 * - -1 (TLS_NOT_SET), if no metavar is present;
281 * - 0 (TLS_LOCAL_EXEC), if metavar uses Local Executable mode of TLS
282 * (offset from fs:0 for x86-64 or tpidr_el0 for aarch64);
283 * - 1 (TLS_IMM_EXEC), if metavar uses Immediate Executable mode of TLS;
284 * - 2 (TLS_GENERAL_DYN), if metavar uses General Dynamic mode of TLS;
285 * This schema allows to use something like:
286 * (tls_mode + 1) * (tls_base + offset)
287 * to get NULL for "no metavar" location, or correct pointer for local
288 * executable mode without doing extra ifs.
290 if (loc->tls_mode <= TLS_LOCAL_EXEC) {
291 /* static executable is simple, we just have offset from
293 void *addr = tls_base + loc->offset;
294 /* multiply by (tls_mode + 1) to get NULL, if we have no
295 * metavar in this slot */
296 return (void *)((loc->tls_mode + 1) * (int64_t)addr);
299 * Other modes are more complicated, we need to jump through few hoops.
301 * For immediate executable mode (currently supported only for aarch64):
302 * - loc->offset is pointing to a GOT entry containing fixed offset
303 * relative to tls_base;
305 * For general dynamic mode:
306 * - loc->offset is pointing to a beginning of double GOT entries;
307 * - (for aarch64 only) second entry points to tls_index_t struct;
308 * - (for x86-64 only) two GOT entries are already tls_index_t;
309 * - tls_index_t->module is used to find start of TLS section in
310 * which variable resides;
311 * - tls_index_t->offset provides offset within that TLS section,
312 * pointing to value of variable.
314 struct tls_index tls_index;
318 bpf_probe_read_user(&tls_index, sizeof(struct tls_index),
319 (void *)loc->offset);
320 /* valid module index is always positive */
321 if (tls_index.module > 0) {
322 /* dtv = ((struct tcbhead *)tls_base)->dtv[tls_index.module] */
323 bpf_probe_read_user(&dtv, sizeof(dtv),
324 &((struct tcbhead *)tls_base)->dtv);
325 dtv += tls_index.module;
329 bpf_probe_read_user(&tls_ptr, sizeof(void *), dtv);
330 /* if pointer has (void *)-1 value, then TLS wasn't initialized yet */
331 return tls_ptr && tls_ptr != (void *)-1
332 ? tls_ptr + tls_index.offset
341 static void read_int_var(struct strobemeta_cfg *cfg,
342 size_t idx, void *tls_base,
343 struct strobe_value_generic *value,
344 struct strobemeta_payload *data)
346 void *location = calc_location(&cfg->int_locs[idx], tls_base);
350 bpf_probe_read_user(value, sizeof(struct strobe_value_generic), location);
351 data->int_vals[idx] = value->val;
352 if (value->header.len)
353 data->int_vals_set_mask |= (1 << idx);
356 static __always_inline uint64_t read_str_var(struct strobemeta_cfg *cfg,
357 size_t idx, void *tls_base,
358 struct strobe_value_generic *value,
359 struct strobemeta_payload *data,
365 data->str_lens[idx] = 0;
366 location = calc_location(&cfg->str_locs[idx], tls_base);
370 bpf_probe_read_user(value, sizeof(struct strobe_value_generic), location);
371 len = bpf_probe_read_user_str(&data->payload[off], STROBE_MAX_STR_LEN, value->ptr);
373 * if bpf_probe_read_user_str returns error (<0), due to casting to
374 * unsinged int, it will become big number, so next check is
375 * sufficient to check for errors AND prove to BPF verifier, that
376 * bpf_probe_read_user_str won't return anything bigger than
379 if (len > STROBE_MAX_STR_LEN)
382 data->str_lens[idx] = len;
386 static __always_inline uint64_t read_map_var(struct strobemeta_cfg *cfg,
387 size_t idx, void *tls_base,
388 struct strobe_value_generic *value,
389 struct strobemeta_payload *data,
392 struct strobe_map_descr* descr = &data->map_descrs[idx];
393 struct strobe_map_raw map;
397 descr->tag_len = 0; /* presume no tag is set */
398 descr->cnt = -1; /* presume no value is set */
400 location = calc_location(&cfg->map_locs[idx], tls_base);
404 bpf_probe_read_user(value, sizeof(struct strobe_value_generic), location);
405 if (bpf_probe_read_user(&map, sizeof(struct strobe_map_raw), value->ptr))
409 descr->cnt = map.cnt;
410 if (cfg->req_meta_idx == idx) {
411 data->req_id = map.id;
412 data->req_meta_valid = 1;
415 len = bpf_probe_read_user_str(&data->payload[off], STROBE_MAX_STR_LEN, map.tag);
416 if (len <= STROBE_MAX_STR_LEN) {
417 descr->tag_len = len;
422 #pragma clang loop unroll(disable)
426 for (int i = 0; i < STROBE_MAX_MAP_ENTRIES; ++i) {
430 descr->key_lens[i] = 0;
431 len = bpf_probe_read_user_str(&data->payload[off], STROBE_MAX_STR_LEN,
433 if (len <= STROBE_MAX_STR_LEN) {
434 descr->key_lens[i] = len;
437 descr->val_lens[i] = 0;
438 len = bpf_probe_read_user_str(&data->payload[off], STROBE_MAX_STR_LEN,
440 if (len <= STROBE_MAX_STR_LEN) {
441 descr->val_lens[i] = len;
456 struct read_var_ctx {
457 struct strobemeta_payload *data;
459 struct strobemeta_cfg *cfg;
461 /* value gets mutated */
462 struct strobe_value_generic *value;
466 static int read_var_callback(__u64 index, struct read_var_ctx *ctx)
468 /* lose precision info for ctx->payload_off, verifier won't track
469 * double xor, barrier_var() is needed to force clang keep both xors.
471 ctx->payload_off ^= index;
472 barrier_var(ctx->payload_off);
473 ctx->payload_off ^= index;
476 if (index >= STROBE_MAX_INTS)
478 read_int_var(ctx->cfg, index, ctx->tls_base, ctx->value, ctx->data);
481 if (index >= STROBE_MAX_MAPS)
483 if (ctx->payload_off > sizeof(ctx->data->payload) - READ_MAP_VAR_PAYLOAD_CAP)
485 ctx->payload_off = read_map_var(ctx->cfg, index, ctx->tls_base,
486 ctx->value, ctx->data, ctx->payload_off);
489 if (index >= STROBE_MAX_STRS)
491 if (ctx->payload_off > sizeof(ctx->data->payload) - STROBE_MAX_STR_LEN)
493 ctx->payload_off = read_str_var(ctx->cfg, index, ctx->tls_base,
494 ctx->value, ctx->data, ctx->payload_off);
499 #endif /* USE_BPF_LOOP */
502 * read_strobe_meta returns NULL, if no metadata was read; otherwise returns
503 * pointer to *right after* payload ends
510 static void *read_strobe_meta(struct task_struct *task,
511 struct strobemeta_payload *data)
513 pid_t pid = bpf_get_current_pid_tgid() >> 32;
514 struct strobe_value_generic value = {0};
515 struct strobemeta_cfg *cfg;
519 cfg = bpf_map_lookup_elem(&strobemeta_cfgs, &pid);
523 data->int_vals_set_mask = 0;
524 data->req_meta_valid = 0;
527 * we don't have struct task_struct definition, it should be:
528 * tls_base = (void *)task->thread.fsbase;
530 tls_base = (void *)task;
533 struct read_var_ctx ctx = {
535 .tls_base = tls_base,
542 ctx.type = READ_INT_VAR;
543 err = bpf_loop(STROBE_MAX_INTS, read_var_callback, &ctx, 0);
544 if (err != STROBE_MAX_INTS)
547 ctx.type = READ_STR_VAR;
548 err = bpf_loop(STROBE_MAX_STRS, read_var_callback, &ctx, 0);
549 if (err != STROBE_MAX_STRS)
552 ctx.type = READ_MAP_VAR;
553 err = bpf_loop(STROBE_MAX_MAPS, read_var_callback, &ctx, 0);
554 if (err != STROBE_MAX_MAPS)
557 payload_off = ctx.payload_off;
558 /* this should not really happen, here only to satisfy verifer */
559 if (payload_off > sizeof(data->payload))
560 payload_off = sizeof(data->payload);
563 #pragma clang loop unroll(disable)
566 #endif /* NO_UNROLL */
567 for (int i = 0; i < STROBE_MAX_INTS; ++i) {
568 read_int_var(cfg, i, tls_base, &value, data);
571 #pragma clang loop unroll(disable)
574 #endif /* NO_UNROLL */
575 for (int i = 0; i < STROBE_MAX_STRS; ++i) {
576 payload_off = read_str_var(cfg, i, tls_base, &value, data, payload_off);
579 #pragma clang loop unroll(disable)
582 #endif /* NO_UNROLL */
583 for (int i = 0; i < STROBE_MAX_MAPS; ++i) {
584 payload_off = read_map_var(cfg, i, tls_base, &value, data, payload_off);
586 #endif /* USE_BPF_LOOP */
589 * return pointer right after end of payload, so it's possible to
590 * calculate exact amount of useful data that needs to be sent
592 return &data->payload[payload_off];
595 SEC("raw_tracepoint/kfree_skb")
596 int on_event(struct pt_regs *ctx) {
597 pid_t pid = bpf_get_current_pid_tgid() >> 32;
598 struct strobelight_bpf_sample* sample;
599 struct task_struct *task;
604 sample = bpf_map_lookup_elem(&sample_heap, &zero);
606 return 0; /* this will never happen */
609 bpf_get_current_comm(&sample->comm, TASK_COMM_LEN);
610 ktime_ns = bpf_ktime_get_ns();
611 sample->ktime = ktime_ns;
613 task = (struct task_struct *)bpf_get_current_task();
614 sample_end = read_strobe_meta(task, &sample->metadata);
615 sample->has_meta = sample_end != NULL;
616 sample_end = sample_end ? : &sample->metadata;
618 if ((ktime_ns >> STACK_TABLE_EPOCH_SHIFT) & 1) {
619 sample->kernel_stack_id = bpf_get_stackid(ctx, &stacks_1, 0);
620 sample->user_stack_id = bpf_get_stackid(ctx, &stacks_1, BPF_F_USER_STACK);
622 sample->kernel_stack_id = bpf_get_stackid(ctx, &stacks_0, 0);
623 sample->user_stack_id = bpf_get_stackid(ctx, &stacks_0, BPF_F_USER_STACK);
626 uint64_t sample_size = sample_end - (void *)sample;
627 /* should always be true */
628 if (sample_size < sizeof(struct strobelight_bpf_sample))
629 bpf_perf_event_output(ctx, &samples, 0, sample, 1 + sample_size);
633 char _license[] SEC("license") = "GPL";