Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
  3#include "vmlinux.h"
  4#include <bpf/bpf_helpers.h>
  5#include <bpf/bpf_tracing.h>
  6#include <bpf/bpf_core_read.h>
  7#include "bpf_misc.h"
  8
  9char _license[] SEC("license") = "GPL";
 10
 11static long stack[256];
 12
 13/*
 14 * KPROBE contexts
 15 */
 16
 17__weak int kprobe_typedef_ctx_subprog(bpf_user_pt_regs_t *ctx)
 18{
 19	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
 20}
 21
 22SEC("?kprobe")
 23__success
 24int kprobe_typedef_ctx(void *ctx)
 25{
 26	return kprobe_typedef_ctx_subprog(ctx);
 27}
 28
 29/* s390x defines:
 30 *
 31 * typedef user_pt_regs bpf_user_pt_regs_t;
 32 * typedef struct { ... } user_pt_regs;
 33 *
 34 * And so "canonical" underlying struct type is anonymous.
 35 * So on s390x only valid ways to have PTR_TO_CTX argument in global subprogs
 36 * are:
 37 *   - bpf_user_pt_regs_t *ctx (typedef);
 38 *   - struct bpf_user_pt_regs_t *ctx (backwards compatible struct hack);
 39 *   - void *ctx __arg_ctx (arg:ctx tag)
 40 *
 41 * Other architectures also allow using underlying struct types (e.g.,
 42 * `struct pt_regs *ctx` for x86-64)
 43 */
 44#ifndef bpf_target_s390
 45
 46#define pt_regs_struct_t typeof(*(__PT_REGS_CAST((struct pt_regs *)NULL)))
 47
 48__weak int kprobe_struct_ctx_subprog(pt_regs_struct_t *ctx)
 49{
 50	return bpf_get_stack((void *)ctx, &stack, sizeof(stack), 0);
 51}
 52
 53SEC("?kprobe")
 54__success
 55int kprobe_resolved_ctx(void *ctx)
 56{
 57	return kprobe_struct_ctx_subprog(ctx);
 58}
 59
 60#endif
 61
 62/* this is current hack to make this work on old kernels */
 63struct bpf_user_pt_regs_t {};
 64
 65__weak int kprobe_workaround_ctx_subprog(struct bpf_user_pt_regs_t *ctx)
 66{
 67	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
 68}
 69
 70SEC("?kprobe")
 71__success
 72int kprobe_workaround_ctx(void *ctx)
 73{
 74	return kprobe_workaround_ctx_subprog(ctx);
 75}
 76
 77/*
 78 * RAW_TRACEPOINT contexts
 79 */
 80
 81__weak int raw_tp_ctx_subprog(struct bpf_raw_tracepoint_args *ctx)
 82{
 83	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
 84}
 85
 86SEC("?raw_tp")
 87__success
 88int raw_tp_ctx(void *ctx)
 89{
 90	return raw_tp_ctx_subprog(ctx);
 91}
 92
 93/*
 94 * RAW_TRACEPOINT_WRITABLE contexts
 95 */
 96
 97__weak int raw_tp_writable_ctx_subprog(struct bpf_raw_tracepoint_args *ctx)
 98{
 99	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
100}
101
102SEC("?raw_tp")
103__success
104int raw_tp_writable_ctx(void *ctx)
105{
106	return raw_tp_writable_ctx_subprog(ctx);
107}
108
109/*
110 * PERF_EVENT contexts
111 */
112
113__weak int perf_event_ctx_subprog(struct bpf_perf_event_data *ctx)
114{
115	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
116}
117
118SEC("?perf_event")
119__success
120int perf_event_ctx(void *ctx)
121{
122	return perf_event_ctx_subprog(ctx);
123}
124
125/* this global subprog can be now called from many types of entry progs, each
126 * with different context type
127 */
128__weak int subprog_ctx_tag(void *ctx __arg_ctx)
129{
130	return bpf_get_stack(ctx, stack, sizeof(stack), 0);
131}
132
133struct my_struct { int x; };
134
135__weak int subprog_multi_ctx_tags(void *ctx1 __arg_ctx,
136				  struct my_struct *mem,
137				  void *ctx2 __arg_ctx)
138{
139	if (!mem)
140		return 0;
141
142	return bpf_get_stack(ctx1, stack, sizeof(stack), 0) +
143	       mem->x +
144	       bpf_get_stack(ctx2, stack, sizeof(stack), 0);
145}
146
147SEC("?raw_tp")
148__success __log_level(2)
149int arg_tag_ctx_raw_tp(void *ctx)
150{
151	struct my_struct x = { .x = 123 };
152
153	return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
154}
155
156SEC("?perf_event")
157__success __log_level(2)
158int arg_tag_ctx_perf(void *ctx)
159{
160	struct my_struct x = { .x = 123 };
161
162	return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
163}
164
165SEC("?kprobe")
166__success __log_level(2)
167int arg_tag_ctx_kprobe(void *ctx)
168{
169	struct my_struct x = { .x = 123 };
170
171	return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
172}
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
  3#include "vmlinux.h"
  4#include <bpf/bpf_helpers.h>
  5#include <bpf/bpf_tracing.h>
  6#include <bpf/bpf_core_read.h>
  7#include "bpf_misc.h"
  8
  9char _license[] SEC("license") = "GPL";
 10
 11static long stack[256];
 12
 13/*
 14 * KPROBE contexts
 15 */
 16
 17__weak int kprobe_typedef_ctx_subprog(bpf_user_pt_regs_t *ctx)
 18{
 19	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
 20}
 21
 22SEC("?kprobe")
 23__success
 24int kprobe_typedef_ctx(void *ctx)
 25{
 26	return kprobe_typedef_ctx_subprog(ctx);
 27}
 28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 29#define pt_regs_struct_t typeof(*(__PT_REGS_CAST((struct pt_regs *)NULL)))
 30
 31__weak int kprobe_struct_ctx_subprog(pt_regs_struct_t *ctx)
 32{
 33	return bpf_get_stack((void *)ctx, &stack, sizeof(stack), 0);
 34}
 35
 36SEC("?kprobe")
 37__success
 38int kprobe_resolved_ctx(void *ctx)
 39{
 40	return kprobe_struct_ctx_subprog(ctx);
 41}
 
 
 42
 43/* this is current hack to make this work on old kernels */
 44struct bpf_user_pt_regs_t {};
 45
 46__weak int kprobe_workaround_ctx_subprog(struct bpf_user_pt_regs_t *ctx)
 47{
 48	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
 49}
 50
 51SEC("?kprobe")
 52__success
 53int kprobe_workaround_ctx(void *ctx)
 54{
 55	return kprobe_workaround_ctx_subprog(ctx);
 56}
 57
 58/*
 59 * RAW_TRACEPOINT contexts
 60 */
 61
 62__weak int raw_tp_ctx_subprog(struct bpf_raw_tracepoint_args *ctx)
 63{
 64	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
 65}
 66
 67SEC("?raw_tp")
 68__success
 69int raw_tp_ctx(void *ctx)
 70{
 71	return raw_tp_ctx_subprog(ctx);
 72}
 73
 74/*
 75 * RAW_TRACEPOINT_WRITABLE contexts
 76 */
 77
 78__weak int raw_tp_writable_ctx_subprog(struct bpf_raw_tracepoint_args *ctx)
 79{
 80	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
 81}
 82
 83SEC("?raw_tp")
 84__success
 85int raw_tp_writable_ctx(void *ctx)
 86{
 87	return raw_tp_writable_ctx_subprog(ctx);
 88}
 89
 90/*
 91 * PERF_EVENT contexts
 92 */
 93
 94__weak int perf_event_ctx_subprog(struct bpf_perf_event_data *ctx)
 95{
 96	return bpf_get_stack(ctx, &stack, sizeof(stack), 0);
 97}
 98
 99SEC("?perf_event")
100__success
101int perf_event_ctx(void *ctx)
102{
103	return perf_event_ctx_subprog(ctx);
104}
105
106/* this global subprog can be now called from many types of entry progs, each
107 * with different context type
108 */
109__weak int subprog_ctx_tag(void *ctx __arg_ctx)
110{
111	return bpf_get_stack(ctx, stack, sizeof(stack), 0);
112}
113
114struct my_struct { int x; };
115
116__weak int subprog_multi_ctx_tags(void *ctx1 __arg_ctx,
117				  struct my_struct *mem,
118				  void *ctx2 __arg_ctx)
119{
120	if (!mem)
121		return 0;
122
123	return bpf_get_stack(ctx1, stack, sizeof(stack), 0) +
124	       mem->x +
125	       bpf_get_stack(ctx2, stack, sizeof(stack), 0);
126}
127
128SEC("?raw_tp")
129__success __log_level(2)
130int arg_tag_ctx_raw_tp(void *ctx)
131{
132	struct my_struct x = { .x = 123 };
133
134	return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
135}
136
137SEC("?perf_event")
138__success __log_level(2)
139int arg_tag_ctx_perf(void *ctx)
140{
141	struct my_struct x = { .x = 123 };
142
143	return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
144}
145
146SEC("?kprobe")
147__success __log_level(2)
148int arg_tag_ctx_kprobe(void *ctx)
149{
150	struct my_struct x = { .x = 123 };
151
152	return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
153}