From: Yu Watanabe Date: Mon, 16 Sep 2024 00:43:18 +0000 (+0900) Subject: bpf-link: introduce bpf_ring_buffer_free() and friends X-Git-Tag: v257-rc1~421^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=46718d344fdaaaf523d854a0c728197e7406a55a;p=thirdparty%2Fsystemd.git bpf-link: introduce bpf_ring_buffer_free() and friends Then, replace rb_free() in networkd. Follow-up for 6d9ef22acdeac4b429efb75164341233955484af. --- diff --git a/src/network/networkd-sysctl.c b/src/network/networkd-sysctl.c index 667c4408dd9..2b9e860c818 100644 --- a/src/network/networkd-sysctl.c +++ b/src/network/networkd-sysctl.c @@ -34,13 +34,7 @@ static struct sysctl_monitor_bpf* sysctl_monitor_bpf_free(struct sysctl_monitor_ return NULL; } -static struct ring_buffer* rb_free(struct ring_buffer *rb) { - sym_ring_buffer__free(rb); - return NULL; -} - DEFINE_TRIVIAL_CLEANUP_FUNC(struct sysctl_monitor_bpf *, sysctl_monitor_bpf_free); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct ring_buffer *, rb_free); static int sysctl_event_handler(void *ctx, void *data, size_t data_sz) { struct sysctl_write_event *we = ASSERT_PTR(data); @@ -99,7 +93,7 @@ static int on_ringbuf_io(sd_event_source *s, int fd, uint32_t revents, void *use int sysctl_add_monitor(Manager *manager) { _cleanup_(sysctl_monitor_bpf_freep) struct sysctl_monitor_bpf *obj = NULL; _cleanup_(bpf_link_freep) struct bpf_link *sysctl_link = NULL; - _cleanup_(rb_freep) struct ring_buffer *sysctl_buffer = NULL; + _cleanup_(bpf_ring_buffer_freep) struct ring_buffer *sysctl_buffer = NULL; _cleanup_close_ int cgroup_fd = -EBADF, rootcg = -EBADF; _cleanup_free_ char *cgroup = NULL; int idx = 0, r; @@ -163,7 +157,7 @@ void sysctl_remove_monitor(Manager *manager) { assert(manager); manager->sysctl_event_source = sd_event_source_disable_unref(manager->sysctl_event_source); - manager->sysctl_buffer = rb_free(manager->sysctl_buffer); + manager->sysctl_buffer = bpf_ring_buffer_free(manager->sysctl_buffer); manager->sysctl_link = bpf_link_free(manager->sysctl_link); manager->sysctl_skel = sysctl_monitor_bpf_free(manager->sysctl_skel); manager->cgroup_fd = safe_close(manager->cgroup_fd); diff --git a/src/nsresourced/nsresourced-manager.c b/src/nsresourced/nsresourced-manager.c index d87da586f7e..43a00d6cefd 100644 --- a/src/nsresourced/nsresourced-manager.c +++ b/src/nsresourced/nsresourced-manager.c @@ -6,6 +6,7 @@ #include "sd-daemon.h" #include "bpf-dlopen.h" +#include "bpf-link.h" #include "build-path.h" #include "common-signal.h" #include "env-util.h" @@ -141,8 +142,7 @@ Manager* manager_free(Manager *m) { #if HAVE_VMLINUX_H sd_event_source_disable_unref(m->userns_restrict_bpf_ring_buffer_event_source); - if (m->userns_restrict_bpf_ring_buffer) - sym_ring_buffer__free(m->userns_restrict_bpf_ring_buffer); + bpf_ring_buffer_free(m->userns_restrict_bpf_ring_buffer); userns_restrict_bpf_free(m->userns_restrict_bpf); #endif diff --git a/src/shared/bpf-link.c b/src/shared/bpf-link.c index 77f6a4ee9ac..c83ebd48040 100644 --- a/src/shared/bpf-link.c +++ b/src/shared/bpf-link.c @@ -31,7 +31,7 @@ int bpf_serialize_link(FILE *f, FDSet *fds, const char *key, struct bpf_link *li return serialize_fd(f, fds, key, sym_bpf_link__fd(link)); } -struct bpf_link *bpf_link_free(struct bpf_link *link) { +struct bpf_link* bpf_link_free(struct bpf_link *link) { /* If libbpf wasn't dlopen()ed, sym_bpf_link__destroy might be unresolved (NULL), so let's not try to * call it if link is NULL. link might also be a non-null "error pointer", but such a value can only * originate from a call to libbpf, but that means that libbpf is available, and we can let @@ -41,3 +41,10 @@ struct bpf_link *bpf_link_free(struct bpf_link *link) { return NULL; } + +struct ring_buffer* bpf_ring_buffer_free(struct ring_buffer *rb) { + if (rb) /* See the comment in bpf_link_free(). */ + sym_ring_buffer__free(rb); + + return NULL; +} diff --git a/src/shared/bpf-link.h b/src/shared/bpf-link.h index 38aa080862d..c66bf38eaeb 100644 --- a/src/shared/bpf-link.h +++ b/src/shared/bpf-link.h @@ -12,5 +12,8 @@ bool bpf_can_link_program(struct bpf_program *prog); int bpf_serialize_link(FILE *f, FDSet *fds, const char *key, struct bpf_link *link); -struct bpf_link *bpf_link_free(struct bpf_link *p); +struct bpf_link* bpf_link_free(struct bpf_link *p); DEFINE_TRIVIAL_CLEANUP_FUNC(struct bpf_link *, bpf_link_free); + +struct ring_buffer* bpf_ring_buffer_free(struct ring_buffer *rb); +DEFINE_TRIVIAL_CLEANUP_FUNC(struct ring_buffer *, bpf_ring_buffer_free);