Skip to contentSkip to conversation

libbpf: fix perf_buffer__free() API for sparse allocs

PATCH In review Eelco Chaudron 3 participants 4 messages May 27, 2020 – May 28, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: fix perf_buffer__free() API for sparse allocs

In case the cpu_bufs are sparsely allocated they are not all free'ed. These changes will fix this.

---
 tools/lib/bpf/libbpf.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +4 -1
index 5d60de6fd818..74d967619dcf 100644
@@ -8137,9 +8137,12 @@ void perf_buffer__free(struct perf_buffer *pb)
 	if (!pb)
 		return;
 	if (pb->cpu_bufs) {
-		for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) {
+		for (i = 0; i < pb->cpu_cnt; i++) {
 			struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
 
+			if (!cpu_buf)
+				continue;
+
 			bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
 			perf_buffer__free_cpu_buf(pb, cpu_buf);
 		}
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Eelco Chaudron

In case the cpu_bufs are sparsely allocated they are not all free'ed. These changes will fix this.

---

Thanks a lot!

You forgot:

Fixes fb84b8224655 ("libbpf: add perf buffer API")
Quoted 22 lines
 tools/lib/bpf/libbpf.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +4 -1
index 5d60de6fd818..74d967619dcf 100644
@@ -8137,9 +8137,12 @@ void perf_buffer__free(struct perf_buffer *pb)
        if (!pb)
                return;
        if (pb->cpu_bufs) {
-               for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) {
+               for (i = 0; i < pb->cpu_cnt; i++) {
                        struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
 
+                       if (!cpu_buf)
+                               continue;
+
                        bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
                        perf_buffer__free_cpu_buf(pb, cpu_buf);
                }
#2 lore raw
Quoted 5 lines · Andrii Nakryiko
Quoted 4 lines · Eelco Chaudron

In case the cpu_bufs are sparsely allocated they are not all free'ed. These changes will fix this.

Quoted 8 lines
Quoted 2 lines

---

Thanks a lot!

You forgot:

Fixes fb84b8224655 ("libbpf: add perf buffer API")

Thanks, I forgot that :( Daniel do you want me to send a v2, or will you add it when you apply it?

Quoted 27 lines
Quoted 25 lines
 tools/lib/bpf/libbpf.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +4 -1
index 5d60de6fd818..74d967619dcf 100644
@@ -8137,9 +8137,12 @@ void perf_buffer__free(struct perf_buffer *pb)
        if (!pb)
                return;
        if (pb->cpu_bufs) {
-               for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) 
 {
+               for (i = 0; i < pb->cpu_cnt; i++) {
                        struct perf_cpu_buf *cpu_buf = 
 pb->cpu_bufs[i];
 
+                       if (!cpu_buf)
+                               continue;
+
                        bpf_map_delete_elem(pb->map_fd, 
&cpu_buf->map_key);
                        perf_buffer__free_cpu_buf(pb, cpu_buf);
                }
maintainer
#3 lore raw
Quoted 10 lines · Eelco Chaudron
Quoted 9 lines · Andrii Nakryiko
Quoted 6 lines · Eelco Chaudron

In case the cpu_bufs are sparsely allocated they are not all free'ed. These changes will fix this.

---

Quoted 8 lines
Quoted 5 lines

Thanks a lot!

You forgot:

Fixes fb84b8224655 ("libbpf: add perf buffer API")

Thanks, I forgot that :( Daniel do you want me to send a v2, or will you add it when you apply it?

No worries, added it. Applied to bpf-next, thanks!