Skip to content

Commit ef256ee

Browse files
committed
eBPF tracer: fixed race condition
1 parent 02681c1 commit ef256ee

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

ebpftracer/ebpf.go

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

ebpftracer/ebpf/l7/l7.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct trace_event_raw_sys_exit_rw__stub {
7373

7474
static inline __attribute__((__always_inline__))
7575
int trace_enter_write(__u64 fd, char *buf, __u64 size) {
76-
__u32 pid = bpf_get_current_pid_tgid() >> 32;
76+
__u64 id = bpf_get_current_pid_tgid();
7777
struct l7_request req = {};
7878
req.partial = 0;
7979
if (is_http_request(buf)) {
@@ -93,7 +93,7 @@ int trace_enter_write(__u64 fd, char *buf, __u64 size) {
9393
}
9494
req.ns = bpf_ktime_get_ns();
9595
struct socket_key k = {};
96-
k.pid = pid;
96+
k.pid = id >> 32;
9797
k.fd = fd;
9898
bpf_map_update_elem(&active_l7_requests, &k, &req, BPF_ANY);
9999
return 0;
@@ -117,46 +117,54 @@ int trace_exit_read(struct trace_event_raw_sys_exit_rw__stub* ctx) {
117117
if (!args) {
118118
return 0;
119119
}
120+
char *buf;
121+
struct socket_key k = {};
122+
k.pid = id >> 32;
123+
k.fd = args->fd;
124+
buf = args->buf;
125+
120126
bpf_map_delete_elem(&active_reads, &id);
121127
if (ctx->ret <= 0) {
122128
return 0;
123129
}
124-
struct socket_key k = {};
125-
k.pid = id >> 32;
126-
k.fd = args->fd;
127130

128131
struct l7_request *req = bpf_map_lookup_elem(&active_l7_requests, &k);
129132
if (!req) {
130133
return 0;
131134
}
135+
struct l7_event e = {};
136+
e.protocol = req->protocol;
137+
e.fd = k.fd;
138+
e.pid = k.pid;
139+
__u64 ns = req->ns;
140+
__u8 partial = req->partial;
132141
bpf_map_delete_elem(&active_l7_requests, &k);
133142

134-
struct l7_event e = {};
135143
if (req->protocol == PROTOCOL_HTTP) {
136-
e.status = parse_http_status(args->buf);
144+
e.status = parse_http_status(buf);
137145
} else if (req->protocol == PROTOCOL_POSTGRES) {
138-
e.status = parse_postgres_status(args->buf, ctx->ret);
146+
e.status = parse_postgres_status(buf, ctx->ret);
139147
} else if (req->protocol == PROTOCOL_REDIS) {
140-
e.status = parse_redis_status(args->buf, ctx->ret);
148+
e.status = parse_redis_status(buf, ctx->ret);
141149
} else if (req->protocol == PROTOCOL_MEMCACHED) {
142-
e.status = parse_memcached_status(args->buf, ctx->ret);
150+
e.status = parse_memcached_status(buf, ctx->ret);
143151
} else if (req->protocol == PROTOCOL_MYSQL) {
144-
e.status = parse_mysql_status(args->buf, ctx->ret);
152+
e.status = parse_mysql_status(buf, ctx->ret);
145153
} else if (req->protocol == PROTOCOL_MONGO) {
146-
e.status = parse_mongo_status(args->buf, ctx->ret, req->partial);
154+
e.status = parse_mongo_status(buf, ctx->ret, partial);
147155
if (e.status == 1) {
148-
req->partial = 1;
149-
bpf_map_update_elem(&active_l7_requests, &k, req, BPF_ANY);
156+
struct l7_request r = {};
157+
r.partial = 1;
158+
r.protocol = e.protocol;
159+
r.ns = ns;
160+
bpf_map_update_elem(&active_l7_requests, &k, &r, BPF_ANY);
150161
return 0;
151162
}
152163
}
153164
if (e.status == 0) {
154165
return 0;
155166
}
156-
e.protocol = req->protocol;
157-
e.fd = k.fd;
158-
e.pid = k.pid;
159-
e.duration = bpf_ktime_get_ns() - req->ns;
167+
e.duration = bpf_ktime_get_ns() - ns;
160168
bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, &e, sizeof(e));
161169
return 0;
162170
}

ebpftracer/ebpf/tcp/state.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ int inet_sock_set_state(void *ctx)
7878
if (!fdp) {
7979
return 0;
8080
}
81-
bpf_map_delete_elem(&fd_by_pid_tgid, &id);
8281
struct sk_info i = {};
8382
i.pid = pid;
8483
i.fd = *fdp;
84+
bpf_map_delete_elem(&fd_by_pid_tgid, &id);
8585
bpf_map_update_elem(&sk_info, &args.skaddr, &i, BPF_ANY);
8686
return 0;
8787
}

0 commit comments

Comments
 (0)