From 74d85fe93b95ee04ccd914b1949e967e039e4cfb Mon Sep 17 00:00:00 2001 From: Giuseppe Ognibene Date: Thu, 26 Feb 2026 16:14:54 +0100 Subject: [PATCH] refactor: apply `const` to bpf variables Signed-off-by: Giuseppe Ognibene --- bpf/generictracer/k_tracer.c | 8 ++++---- bpf/gotracer/go_nethttp.c | 8 ++++---- bpf/gotracer/go_redis.c | 4 ++-- bpf/gotracer/go_runtime.c | 6 +++--- bpf/gpuevent/cuda.c | 8 ++++---- bpf/logenricher/logenricher.c | 2 +- bpf/netolly/flows.c | 6 +++--- bpf/netolly/flows_common.h | 2 +- bpf/netolly/flows_sock.c | 6 +++--- bpf/pid/pid.h | 14 +++++++------- bpf/watcher/watcher.c | 2 +- 11 files changed, 33 insertions(+), 33 deletions(-) diff --git a/bpf/generictracer/k_tracer.c b/bpf/generictracer/k_tracer.c index 298718bb92..e7d84b52f8 100644 --- a/bpf/generictracer/k_tracer.c +++ b/bpf/generictracer/k_tracer.c @@ -130,7 +130,7 @@ int BPF_KRETPROBE(obi_kretprobe_sys_accept4, s32 fd) { // TODO: try to merge with store_accept_fd_info() above bpf_map_update_elem(&fd_to_connection, &key, &info.p_conn.conn, BPF_ANY); - u64 accept_time = bpf_ktime_get_ns(); + const u64 accept_time = bpf_ktime_get_ns(); bpf_map_update_elem(&accepted_connections, &info.p_conn.conn, &accept_time, BPF_ANY); } else { @@ -1002,7 +1002,7 @@ int obi_socket__http_filter(struct __sk_buff *skb) { //bpf_d_printk("http buf=[%s] [%s]", buf, __FUNCTION__); //d_print_http_connection_info(&conn); if (packet_type == PACKET_TYPE_REQUEST) { - u64 cookie = bpf_get_socket_cookie(skb); + const u64 cookie = bpf_get_socket_cookie(skb); //bpf_dbg_printk("cookie=%llx, len=%d, buf=[%s]", cookie, len, buf); //dbg_print_http_connection_info(&conn); @@ -1170,8 +1170,8 @@ int obi_handle_buf_with_args(void *ctx) { (info) ? still_reading(info) : 0); if (info && !info->submitted) { - u8 reading = still_reading(info); - u8 responding = still_responding(info); + const u8 reading = still_reading(info); + const u8 responding = still_responding(info); // Still reading checks if we are processing buffers of a HTTP request // that has started, but we haven't seen a response yet. if (reading || responding) { diff --git a/bpf/gotracer/go_nethttp.c b/bpf/gotracer/go_nethttp.c index 10c211e968..f30c2dc8c0 100644 --- a/bpf/gotracer/go_nethttp.c +++ b/bpf/gotracer/go_nethttp.c @@ -258,7 +258,7 @@ int obi_uprobe_ginGetValueRet(struct pt_regs *ctx) { bpf_map_lookup_elem(&ongoing_http_server_requests, &g_key); off_table_t *ot = get_offsets_table(); - u64 fullpath_off = go_offset_of(ot, (go_offset){.v = _gin_fullpath_pos}); + const u64 fullpath_off = go_offset_of(ot, (go_offset){.v = _gin_fullpath_pos}); bpf_dbg_printk("goroutine_addr=%lx, invocation=%llx, fullpath_off=%d", goroutine_addr, @@ -273,7 +273,7 @@ int obi_uprobe_ginGetValueRet(struct pt_regs *ctx) { // registers if (fullpath_off == _gin_fullpath_off_pre_17) { void *ptr = GO_PARAM8(ctx); - u64 len = (u64)GO_PARAM9(ctx); + const u64 len = (u64)GO_PARAM9(ctx); if (ptr) { bpf_dbg_printk("pre gin 1.7.0 fullPath from: %llx", ptr); @@ -282,7 +282,7 @@ int obi_uprobe_ginGetValueRet(struct pt_regs *ctx) { } } else { void *ptr = GO_PARAM6(ctx); - u64 len = (u64)GO_PARAM7(ctx); + const u64 len = (u64)GO_PARAM7(ctx); if (ptr) { bpf_dbg_printk("post gin 1.7.0 fullPath from: %llx", ptr); @@ -950,7 +950,7 @@ int obi_uprobe_http2ResponseWriterStateWriteHeader(struct pt_regs *ctx) { bpf_dbg_printk("=== uprobe/http2ResponseWriterStateWriteHeader ==="); void *goroutine_addr = GOROUTINE_PTR(ctx); - u64 status = (u64)GO_PARAM2(ctx); + const u64 status = (u64)GO_PARAM2(ctx); bpf_dbg_printk("goroutine_addr=%lx, status=%d", goroutine_addr, status); go_addr_key_t g_key = {}; go_addr_key_from_id(&g_key, goroutine_addr); diff --git a/bpf/gotracer/go_redis.c b/bpf/gotracer/go_redis.c index c6e70062b1..71f3ded91e 100644 --- a/bpf/gotracer/go_redis.c +++ b/bpf/gotracer/go_redis.c @@ -130,7 +130,7 @@ int obi_uprobe_redis_with_writer(struct pt_regs *ctx) { &conn_ptr, sizeof(conn_ptr), (void *)(tcp_conn_ptr + 8)); // find conn bpf_dbg_printk("conn_ptr=%llx", conn_ptr); if (conn_ptr) { - u8 ok = get_conn_info(conn_ptr, &req->conn); + const u8 ok = get_conn_info(conn_ptr, &req->conn); if (!ok) { __builtin_memset(&req->conn, 0, sizeof(connection_info_t)); } @@ -161,7 +161,7 @@ int obi_uprobe_redis_with_writer_ret(struct pt_regs *ctx) { if (bw) { bpf_dbg_printk("Found bw: %llx", bw); - u64 io_writer_buf_ptr_pos = + const u64 io_writer_buf_ptr_pos = go_offset_of(ot, (go_offset){.v = _io_writer_buf_ptr_pos}); void *buf = 0; diff --git a/bpf/gotracer/go_runtime.c b/bpf/gotracer/go_runtime.c index 078b520474..e78a919b85 100644 --- a/bpf/gotracer/go_runtime.c +++ b/bpf/gotracer/go_runtime.c @@ -159,7 +159,7 @@ enum offsets : u8 { SEC("uprobe/runtime.mstart1") int obi_uprobe_runtime_mstart1(struct pt_regs *ctx) { - u64 pid_tgid = bpf_get_current_pid_tgid(); + const u64 pid_tgid = bpf_get_current_pid_tgid(); void *g = (void *)GOROUTINE_PTR(ctx); void *m = NULL; @@ -190,7 +190,7 @@ int obi_uprobe_runtime_mexit(struct pt_regs *ctx) { // gp *g, oldval, newval uint32 SEC("uprobe/runtime.casgstatus") int obi_uprobe_runtime_casgstatus(struct pt_regs *ctx) { - u64 pid_tgid = bpf_get_current_pid_tgid(); + const u64 pid_tgid = bpf_get_current_pid_tgid(); void *g = (void *)GO_PARAM1(ctx); void *m = NULL; @@ -219,7 +219,7 @@ int obi_uprobe_runtime_casgstatus(struct pt_regs *ctx) { }; grpc_srv_func_invocation_t *invocation; - u32 newval = (u32)(uintptr_t)GO_PARAM3(ctx); + const u32 newval = (u32)(uintptr_t)GO_PARAM3(ctx); switch (newval) { case g_running: case g_syscall: diff --git a/bpf/gpuevent/cuda.c b/bpf/gpuevent/cuda.c index 9d70c4f955..bd8f7a552e 100644 --- a/bpf/gpuevent/cuda.c +++ b/bpf/gpuevent/cuda.c @@ -34,7 +34,7 @@ enum { SEC("uprobe/cudaLaunchKernel") int BPF_KPROBE(obi_cuda_launch, u64 func_off, u64 grid_xy, u64 grid_z, u64 block_xy, u64 block_z) { (void)ctx; - u64 id = bpf_get_current_pid_tgid(); + const u64 id = bpf_get_current_pid_tgid(); if (!valid_pid(id)) { return 0; @@ -68,7 +68,7 @@ int BPF_KPROBE(obi_cuda_malloc, void **devPtr, size_t size) { (void)ctx; (void)devPtr; - u64 id = bpf_get_current_pid_tgid(); + const u64 id = bpf_get_current_pid_tgid(); if (!valid_pid(id)) { return 0; @@ -96,7 +96,7 @@ int BPF_KPROBE(obi_cuda_memcpy, void *dst, void *src, size_t size, u8 kind) { (void)dst; (void)src; - u64 id = bpf_get_current_pid_tgid(); + const u64 id = bpf_get_current_pid_tgid(); if (!valid_pid(id)) { return 0; @@ -122,7 +122,7 @@ int BPF_KPROBE(obi_cuda_memcpy, void *dst, void *src, size_t size, u8 kind) { SEC("uprobe/cudaGraphLaunch") int BPF_KPROBE(obi_graph_launch) { (void)ctx; - u64 id = bpf_get_current_pid_tgid(); + const u64 id = bpf_get_current_pid_tgid(); if (!valid_pid(id)) { return 0; diff --git a/bpf/logenricher/logenricher.c b/bpf/logenricher/logenricher.c index c70729cf2b..8bc6d92f4d 100644 --- a/bpf/logenricher/logenricher.c +++ b/bpf/logenricher/logenricher.c @@ -133,7 +133,7 @@ int BPF_KPROBE(obi_kprobe_tty_write, struct kiocb *iocb, struct iov_iter *from) struct tty_file_private *tfp = (struct tty_file_private *)BPF_CORE_READ(iocb, ki_filp, private_data); struct tty_struct *tty = BPF_CORE_READ(tfp, tty); - bool is_master = tty_driver_is_pty(tty) && tty_driver_is_master(tty); + const bool is_master = tty_driver_is_pty(tty) && tty_driver_is_master(tty); struct tty_dev master = {}; struct tty_dev slave = {}; diff --git a/bpf/netolly/flows.c b/bpf/netolly/flows.c index 522d3e8f6c..a5c20a93b5 100644 --- a/bpf/netolly/flows.c +++ b/bpf/netolly/flows.c @@ -165,7 +165,7 @@ static inline int flow_monitor(struct __sk_buff *skb) { } id.if_index = skb->ifindex; - u64 current_time = bpf_ktime_get_ns(); + const u64 current_time = bpf_ktime_get_ns(); // TODO: we need to add spinlock here when we deprecate versions prior to 5.1, or provide // a spinlocked alternative version and use it selectively https://lwn.net/Articles/779120/ @@ -181,7 +181,7 @@ static inline int flow_monitor(struct __sk_buff *skb) { } aggregate_flow->flags |= flags; - long ret = bpf_map_update_elem(&aggregated_flows, &id, aggregate_flow, BPF_ANY); + const long ret = bpf_map_update_elem(&aggregated_flows, &id, aggregate_flow, BPF_ANY); if (trace_messages && ret != 0) { // usually error -16 (-EBUSY) is printed here. // In this case, the flow is dropped, as submitting it to the ringbuffer would cause @@ -234,7 +234,7 @@ static inline int flow_monitor(struct __sk_buff *skb) { // even if we know that the entry is new, another CPU might be concurrently inserting a flow // so we need to specify BPF_ANY - long ret = bpf_map_update_elem(&aggregated_flows, &id, &new_flow, BPF_ANY); + const long ret = bpf_map_update_elem(&aggregated_flows, &id, &new_flow, BPF_ANY); if (ret != 0) { // usually error -16 (-EBUSY) or -7 (E2BIG) is printed here. // In this case, we send the single-packet flow via ringbuffer as in the worst case we can have diff --git a/bpf/netolly/flows_common.h b/bpf/netolly/flows_common.h index 938e40c611..ee5e5c43f1 100644 --- a/bpf/netolly/flows_common.h +++ b/bpf/netolly/flows_common.h @@ -131,7 +131,7 @@ static inline u8 get_connection_initiator(flow_id *id, u16 flags) { conn_initiator_key initiator_key; // from the initiator_key with sorted ip/ports, know the index of the // endpoint that that initiated the connection, which might be the low or the high address - u8 low_is_src = fill_conn_initiator_key(id, &initiator_key); + const u8 low_is_src = fill_conn_initiator_key(id, &initiator_key); u8 *initiator = (u8 *)bpf_map_lookup_elem(&conn_initiators, &initiator_key); u8 initiator_index = INITIATOR_UNKNOWN; if (initiator == NULL) { diff --git a/bpf/netolly/flows_sock.c b/bpf/netolly/flows_sock.c index 49ae179758..890e520db0 100644 --- a/bpf/netolly/flows_sock.c +++ b/bpf/netolly/flows_sock.c @@ -203,7 +203,7 @@ int obi_socket__filter(struct __sk_buff *skb) { return TC_ACT_UNSPEC; } - u64 current_time = bpf_ktime_get_ns(); + const u64 current_time = bpf_ktime_get_ns(); // TODO: we need to add spinlock here when we deprecate versions prior to 5.1, or provide // a spinlocked alternative version and use it selectively https://lwn.net/Articles/779120/ @@ -219,7 +219,7 @@ int obi_socket__filter(struct __sk_buff *skb) { } aggregate_flow->flags |= flags; - long ret = bpf_map_update_elem(&aggregated_flows, &id, aggregate_flow, BPF_ANY); + const long ret = bpf_map_update_elem(&aggregated_flows, &id, aggregate_flow, BPF_ANY); if (trace_messages && ret != 0) { // usually error -16 (-EBUSY) is printed here. // In this case, the flow is dropped, as submitting it to the ringbuffer would cause @@ -271,7 +271,7 @@ int obi_socket__filter(struct __sk_buff *skb) { // even if we know that the entry is new, another CPU might be concurrently inserting a flow // so we need to specify BPF_ANY - long ret = bpf_map_update_elem(&aggregated_flows, &id, &new_flow, BPF_ANY); + const long ret = bpf_map_update_elem(&aggregated_flows, &id, &new_flow, BPF_ANY); if (ret != 0) { // usually error -16 (-EBUSY) or -7 (E2BIG) is printed here. // In this case, we send the single-packet flow via ringbuffer as in the worst case we can have diff --git a/bpf/pid/pid.h b/bpf/pid/pid.h index eff3b95f36..d85dc11f51 100644 --- a/bpf/pid/pid.h +++ b/bpf/pid/pid.h @@ -22,12 +22,12 @@ enum { k_prime_hash = 192053 }; // closest prime to k_max_concurrent_pids * 64 static __always_inline u8 pid_matches(pid_data_t *p) { // combine the namespace id and the pid into one single u64 - u64 k = (((u64)p->ns) << 32) | p->pid; + const u64 k = (((u64)p->ns) << 32) | p->pid; // divide with prime number lower than max pids * 64, modulo with primes gives good hash functions - u32 h = (u32)(k % k_prime_hash); - u32 segment = h / 64; // divide by the segment size (8 bytes) to find the segment - u32 bit = h & 63; // lowest 64 bits gives us the placement inside the segment + const u32 h = (u32)(k % k_prime_hash); + const u32 segment = h / 64; // divide by the segment size (8 bytes) to find the segment + const u32 bit = h & 63; // lowest 64 bits gives us the placement inside the segment u64 *v = bpf_map_lookup_elem(&valid_pids, &segment); if (!v) { @@ -40,7 +40,7 @@ static __always_inline u8 pid_matches(pid_data_t *p) { } static __always_inline u32 valid_pid(u64 id) { - u32 a_pid = id >> 32; + const u32 a_pid = id >> 32; // accept all PIDs if debugging OTEL_EBPF_BPF_PID_FILTER_OFF option is set if (!filter_pids) { return a_pid; @@ -63,7 +63,7 @@ static __always_inline u32 valid_pid(u64 id) { if (a_pid != 0) { pid_data_t p_key = {.pid = a_pid, .ns = pid_ns_id}; - u8 found_ns_pid = pid_matches(&p_key); + const u8 found_ns_pid = pid_matches(&p_key); if (found_ns_pid) { bpf_map_update_elem(&pid_cache, &a_pid, &a_pid, BPF_ANY); @@ -71,7 +71,7 @@ static __always_inline u32 valid_pid(u64 id) { } else if (ns_ppid != 0) { pid_data_t pp_key = {.pid = ns_ppid, .ns = pid_ns_id}; - u8 found_ns_ppid = pid_matches(&pp_key); + const u8 found_ns_ppid = pid_matches(&pp_key); if (found_ns_ppid) { bpf_map_update_elem(&pid_cache, &a_pid, &a_pid, BPF_ANY); diff --git a/bpf/watcher/watcher.c b/bpf/watcher/watcher.c index 3403c0e813..855e44d8db 100644 --- a/bpf/watcher/watcher.c +++ b/bpf/watcher/watcher.c @@ -37,7 +37,7 @@ int obi_kprobe_sys_bind(struct pt_regs *ctx) { return 0; } - u16 port = get_sockaddr_port_user(addr); + const u16 port = get_sockaddr_port_user(addr); if (!port) { return 0;