From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3E14DC54FB3 for ; Thu, 29 May 2025 04:19:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=MdWHT1perMY/ilaILk5jhZmkWKssh6zUyVvG2waTWcY=; b=jlfZyyOxBzGuTbDXMSgrFUbBK5 Y0W4FFvEPSviMC71l9UZc04h8idjXbQ+1ZieqxH0qeY26QoWJTzM3GKogEt91qGmgZxVuD8WNxpKe ES0wADGci9Qp+5xO9JDgCwynba1aedNh6uRHLCHCcGSZoU7KrgLzy2muxPMdSZMWNDhqulZq6ezzs 2HyZQYNt5KwA4tgZxL4TE+Yxu0qLOQkzb3uITT5q1q6o2bkw+VGh6nDD4ANC9ZtcF5uBSzsIn0uAM u5a4bUEYg/94+I0F16dCYSa1ydJlru0BckShtvktt6Cww6UpZTNDnrvRWAG9DqdqBD8MyNwZssWMH s8GiW4qw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1uKUjw-0000000EtWR-43Ko; Thu, 29 May 2025 04:19:36 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1uKUju-0000000EtVn-3yNM for kexec@lists.infradead.org; Thu, 29 May 2025 04:19:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1748492374; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MdWHT1perMY/ilaILk5jhZmkWKssh6zUyVvG2waTWcY=; b=WMdaEYO7XFsoP1MAeQNsQiddo07BWsRmGHXYYZ5iZqrK2WfvcVjOoGo+dSu5LKITDTXObd +vnqn86CyGkxKZe9TwwgW9r7HoFAfDobRvnGD1h/0+lSHFBaF1t9o/0jOnEKaOid9/GCKO 2aRIBho+b74evyeLbFKmypQlDGUqvgU= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-444-Tv4T9c1KMTWX4BB937_ruQ-1; Thu, 29 May 2025 00:19:31 -0400 X-MC-Unique: Tv4T9c1KMTWX4BB937_ruQ-1 X-Mimecast-MFC-AGG-ID: Tv4T9c1KMTWX4BB937_ruQ_1748492368 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 50B6F1956089; Thu, 29 May 2025 04:19:28 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.72.112.18]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 70AB8180047F; Thu, 29 May 2025 04:19:15 +0000 (UTC) From: Pingfan Liu To: bpf@vger.kernel.org Cc: Pingfan Liu , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , Jeremy Linton , Catalin Marinas , Will Deacon , Ard Biesheuvel , Simon Horman , Gerd Hoffmann , Vitaly Kuznetsov , Philipp Rudo , Viktor Malik , Jan Hendrik Farr , Baoquan He , Dave Young , Andrew Morton , kexec@lists.infradead.org, KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Subject: [PATCHv3 4/9] bpf: Introduce decompressor kfunc Date: Thu, 29 May 2025 12:17:39 +0800 Message-ID: <20250529041744.16458-5-piliu@redhat.com> In-Reply-To: <20250529041744.16458-1-piliu@redhat.com> References: <20250529041744.16458-1-piliu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250528_211935_058264_030EFC31 X-CRM114-Status: GOOD ( 16.79 ) X-BeenThere: kexec@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kexec" Errors-To: kexec-bounces+kexec=archiver.kernel.org@lists.infradead.org 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 Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: John Fastabend Cc: Andrii Nakryiko Cc: Martin KaFai Lau Cc: Eduard Zingerman Cc: Song Liu Cc: Yonghong Song Cc: KP Singh Cc: Stanislav Fomichev Cc: Hao Luo Cc: Jiri Olsa 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 #include #include +#include #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