From: Pingfan Liu <piliu@redhat.com>
To: bpf@vger.kernel.org
Cc: Pingfan Liu <piliu@redhat.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jeremy Linton <jeremy.linton@arm.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>, Ard Biesheuvel <ardb@kernel.org>,
Simon Horman <horms@kernel.org>,
Gerd Hoffmann <kraxel@redhat.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Philipp Rudo <prudo@redhat.com>, Viktor Malik <vmalik@redhat.com>,
Jan Hendrik Farr <kernel@jfarr.cc>, Baoquan He <bhe@redhat.com>,
Dave Young <dyoung@redhat.com>,
Andrew Morton <akpm@linux-foundation.org>,
kexec@lists.infradead.org, KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>
Subject: [PATCHv3 4/9] bpf: Introduce decompressor kfunc
Date: Thu, 29 May 2025 12:17:39 +0800 [thread overview]
Message-ID: <20250529041744.16458-5-piliu@redhat.com> (raw)
In-Reply-To: <20250529041744.16458-1-piliu@redhat.com>
This commit bridges the gap between bpf-prog and the kernel
decompression routines. At present, only a global memory allocator is
used for the decompression. Later, if needed, the decompress_fn's
prototype can be changed to pass in a task related allocator.
Signed-off-by: Pingfan Liu <piliu@redhat.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Song Liu <song@kernel.org>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: KP Singh <kpsingh@kernel.org>
Cc: Stanislav Fomichev <sdf@fomichev.me>
Cc: Hao Luo <haoluo@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
To: bpf@vger.kernel.org
---
kernel/bpf/helpers.c | 111 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 111 insertions(+)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 1f4284e58400b..9748d6101d032 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -23,6 +23,7 @@
#include <linux/btf_ids.h>
#include <linux/bpf_mem_alloc.h>
#include <linux/kasan.h>
+#include <linux/decompress/generic.h>
#include "../../lib/kstrtox.h"
@@ -3194,12 +3195,122 @@ __bpf_kfunc void bpf_local_irq_restore(unsigned long *flags__irq_flag)
local_irq_restore(*flags__irq_flag);
}
+#define MAX_UNCOMPRESSED_BUF_SIZE (1 << 28)
+/*
+ * At present, one global allocator for decompression. Later if needed, changing the
+ * prototype of decompress_fn to introduce each task's allocator.
+ */
+static char *output_buf;
+static char *output_cur;
+static DEFINE_MUTEX(output_buf_mutex);
+
+/*
+ * Copy the partial decompressed content in [buf, buf + len) to dst.
+ * If the dst size is beyond the capacity, return -1 to indicate the
+ * decompress method that something is wrong.
+ */
+static long flush(void *buf, unsigned long len)
+{
+
+ if (output_cur - output_buf > MAX_UNCOMPRESSED_BUF_SIZE - len)
+ return -1;
+ memcpy(output_cur, buf, len);
+ output_cur += len;
+ return len;
+}
+
+__bpf_kfunc struct mem_range_result *bpf_decompress(char *image_gz_payload, int image_gz_sz)
+{
+ struct mem_cgroup *memcg, *old_memcg;
+ decompress_fn decompressor;
+ struct mem_range_result *range;
+ const char *name;
+ char *input_buf;
+ int ret;
+
+ memcg = get_mem_cgroup_from_current();
+ old_memcg = set_active_memcg(memcg);
+ range = kmalloc(sizeof(struct mem_range_result), GFP_KERNEL);
+ if (!range) {
+ pr_err("fail to allocate mem_range_result\n");
+ goto error;
+ }
+ kref_init(&range->ref);
+
+ input_buf = __vmalloc(image_gz_sz, GFP_KERNEL | __GFP_ACCOUNT);
+ if (!input_buf) {
+ kfree(range);
+ pr_err("fail to allocate input buffer\n");
+ goto error;
+ }
+
+ ret = copy_from_kernel_nofault(input_buf, image_gz_payload, image_gz_sz);
+ if (ret < 0) {
+ kfree(range);
+ vfree(input_buf);
+ pr_err("Error when copying from 0x%p, size:0x%x\n",
+ image_gz_payload, image_gz_sz);
+ goto error;
+ }
+
+ mutex_lock(&output_buf_mutex);
+ output_buf = __vmalloc(MAX_UNCOMPRESSED_BUF_SIZE, GFP_KERNEL | __GFP_ACCOUNT);
+ if (!output_buf) {
+ mutex_unlock(&output_buf_mutex);
+ kfree(range);
+ vfree(input_buf);
+ pr_err("fail to allocate output buffer\n");
+ goto error;
+ }
+ output_cur = output_buf;
+ decompressor = decompress_method(input_buf, image_gz_sz, &name);
+ if (!decompressor) {
+ kfree(range);
+ vfree(input_buf);
+ vfree(output_buf);
+ mutex_unlock(&output_buf_mutex);
+ pr_err("Can not find decompress method\n");
+ goto error;
+ }
+ ret = decompressor(input_buf, image_gz_sz, NULL, flush,
+ NULL, NULL, NULL);
+
+ vfree(input_buf);
+ /* Update the range map */
+ if (ret == 0) {
+ range->kmalloc = false;
+ range->buf = output_buf;
+ range->buf_sz = MAX_UNCOMPRESSED_BUF_SIZE;
+ range->data_sz = output_cur - output_buf;
+ output_buf = output_cur = NULL;
+ mutex_unlock(&output_buf_mutex);
+ range->status = 0;
+ /* Do not release the reference */
+ range->memcg = memcg;
+ set_active_memcg(old_memcg);
+ return range;
+ }
+
+ /* Decompression fails */
+ vfree(output_buf);
+ output_buf = output_cur = NULL;
+ mutex_unlock(&output_buf_mutex);
+ kfree(range);
+ pr_err("Decompress error\n");
+
+error:
+ set_active_memcg(old_memcg);
+ mem_cgroup_put(memcg);
+ return NULL;
+}
+
__bpf_kfunc_end_defs();
BTF_KFUNCS_START(generic_btf_ids)
#ifdef CONFIG_CRASH_DUMP
BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE)
#endif
+BTF_ID_FLAGS(func, bpf_decompress, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_SLEEPABLE)
BTF_ID_FLAGS(func, bpf_mem_range_result_put, KF_RELEASE)
BTF_ID_FLAGS(func, bpf_copy_to_kernel, KF_TRUSTED_ARGS | KF_SLEEPABLE)
BTF_ID_FLAGS(func, bpf_obj_new_impl, KF_ACQUIRE | KF_RET_NULL)
--
2.49.0
next prev parent reply other threads:[~2025-05-29 4:19 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-29 4:17 [PATCHv3 0/9] kexec: Use BPF lskel to enable kexec to load PE format boot image Pingfan Liu
2025-05-29 4:17 ` [PATCHv3 1/9] kexec_file: Make kexec_image_load_default global visible Pingfan Liu
2025-05-29 4:17 ` [PATCHv3 2/9] lib/decompress: Keep decompressor when CONFIG_KEXEC_PE_IMAGE Pingfan Liu
2025-05-29 4:17 ` [PATCHv3 3/9] bpf: Introduce bpf_copy_to_kernel() to buffer the content from bpf-prog Pingfan Liu
2025-05-29 11:48 ` kernel test robot
2025-06-25 18:10 ` Philipp Rudo
2025-05-29 4:17 ` Pingfan Liu [this message]
2025-05-29 12:31 ` [PATCHv3 4/9] bpf: Introduce decompressor kfunc kernel test robot
2025-05-29 4:17 ` [PATCHv3 5/9] kexec: Introduce kexec_pe_image to parse and load PE file Pingfan Liu
2025-06-25 18:09 ` Philipp Rudo
2025-06-30 13:45 ` Pingfan Liu
2025-07-02 9:17 ` Philipp Rudo
2025-07-03 1:17 ` Pingfan Liu
2025-05-29 4:17 ` [PATCHv3 6/9] kexec: Integrate with the introduced bpf kfuncs Pingfan Liu
2025-05-29 4:17 ` [PATCHv3 7/9] kexec: Introduce a bpf-prog lskel to parse PE file Pingfan Liu
2025-05-29 4:17 ` [PATCHv3 8/9] kexec: Integrate bpf light skeleton to load zboot image Pingfan Liu
2025-06-25 18:10 ` Philipp Rudo
2025-06-30 12:40 ` Pingfan Liu
2025-05-29 4:17 ` [PATCHv3 9/9] arm64/kexec: Add PE image format support Pingfan Liu
2025-05-29 15:34 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250529041744.16458-5-piliu@redhat.com \
--to=piliu@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ardb@kernel.org \
--cc=ast@kernel.org \
--cc=bhe@redhat.com \
--cc=bpf@vger.kernel.org \
--cc=catalin.marinas@arm.com \
--cc=daniel@iogearbox.net \
--cc=dyoung@redhat.com \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=horms@kernel.org \
--cc=jeremy.linton@arm.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kernel@jfarr.cc \
--cc=kexec@lists.infradead.org \
--cc=kpsingh@kernel.org \
--cc=kraxel@redhat.com \
--cc=martin.lau@linux.dev \
--cc=prudo@redhat.com \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=vkuznets@redhat.com \
--cc=vmalik@redhat.com \
--cc=will@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox