From nobody Wed Nov 27 16:36:49 2024 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F97413B2A5; Tue, 8 Oct 2024 21:19:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=96.67.55.147 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728422347; cv=none; b=pmKpyN+VHfFsdPhLkFF5wozB8v2x42SZM8ycTMJtaqGWyqJPTU1hnr28zGzB1m/Hi3irM4B8V+JXipPxOo2FSiJfDU9E+kzrD1x3EfjiRL78hkp6W5ZaG95VfEFGOC7Bn1e9wQ+f/Q8Mdvl1gCrvPkBqQWmza9QoSnxJx2i+Gvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728422347; c=relaxed/simple; bh=FnVKqoqYuhkZnUwPaYSIAQwKE9UC7Dsy2iOnEm1mWDk=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=dvMxGIBWUzbSLVnXsjoilqvFu1iLrlyqFculPRCj8doA9ww+e6HB9dDobcUCRioT+PrzJZ13SFWR8+TmNQ4edArB1cQ5vljLfNZIlnIyTNgJ5RhhsBpwaZ/hbPykg37nEtJFd5ly9iTHbakxLml+S/KFa7aUr1jkL4U5cJzOcu0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=surriel.com; spf=pass smtp.mailfrom=shelob.surriel.com; arc=none smtp.client-ip=96.67.55.147 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shelob.surriel.com Received: from [2601:18c:9101:a8b6:6e0b:84ff:fee2:98bb] (helo=imladris.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1syHQd-0000000080F-3jkY; Tue, 08 Oct 2024 17:07:35 -0400 Date: Tue, 8 Oct 2024 17:07:35 -0400 From: Rik van Riel To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@meta.com, Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko Subject: [PATCH] bpf: use kvzmalloc to allocate BPF verifier environment Message-ID: <20241008170735.16766766@imladris.surriel.com> X-Mailer: Claws Mail 4.3.0 (GTK 3.24.41; x86_64-redhat-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: riel@surriel.com Content-Type: text/plain; charset="utf-8" The kzmalloc call in bpf_check can fail when memory is very fragmented, which in turn can lead to an OOM kill. Use kvzmalloc to fall back to vmalloc when memory is too fragmented to allocate an order 3 sized bpf verifier environment. Admittedly this is not a very common case, and only happens on systems where memory has already been squeezed close to the limit, but this does not seem like much of a hot path, and it's a simple enough fix. Signed-off-by: Rik van Riel Reviewed-by: Shakeel Butt --- kernel/bpf/verifier.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index dd86282ccaa4..0c41646a8793 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -22298,7 +22298,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_att= r *attr, bpfptr_t uattr, __u3 /* 'struct bpf_verifier_env' can be global, but since it's not small, * allocate/free it every time bpf_check() is called */ - env =3D kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); + env =3D kvzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); if (!env) return -ENOMEM; =20 @@ -22534,6 +22534,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_att= r *attr, bpfptr_t uattr, __u3 mutex_unlock(&bpf_verifier_lock); vfree(env->insn_aux_data); err_free_env: - kfree(env); + kvfree(env); return ret; } --=20 2.45.2