From patchwork Thu Jan 16 05:51:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 13941303 X-Patchwork-Delegate: bpf@iogearbox.net Received: from 66-220-155-178.mail-mxout.facebook.com (66-220-155-178.mail-mxout.facebook.com [66.220.155.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E29D3141987 for ; Thu, 16 Jan 2025 05:51:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=66.220.155.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737006709; cv=none; b=tUEGKbHaF/BWxk9UoWgaCS2hJtBmn/BpIDY/p5oMbJD0pRVW0OBOBb9OrtZyE7waibGGkgQU+0zVOmBGfYFyEjioyPUopAfIrQzBwLnx29h0tKxHsfreR57QHL2GDl+QkANL/A/SrXCJZpdc1c+3Kxzs9P3+U/6ZDQbDiHvopi0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737006709; c=relaxed/simple; bh=M0MRPC4InUgpTCkcLNGTZh51OD1kaASqNwm7rdAmrAs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tsD8hrJp3+p/PSZeTV7UQ029qHEGUhXm5ogSQiqjIr87lndYzwp2D1lgNiMshRgrrIPuv9wLHjyXR1++2Vp+jYA9a+RHOyP64jQOOlfJS9PyWEPNiI2Y6mN3X8ujwtLSW/hzOmTBWWEh6eqp8K9Vl30vpI8sloJEYBZeXAqvnWY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev; spf=fail smtp.mailfrom=linux.dev; arc=none smtp.client-ip=66.220.155.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=linux.dev Received: by devbig309.ftw3.facebook.com (Postfix, from userid 128203) id 394F3D032467; Wed, 15 Jan 2025 21:51:34 -0800 (PST) From: Yonghong Song To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , kernel-team@fb.com, Martin KaFai Lau Subject: [PATCH bpf-next 2/3] bpf: Remove 'may_goto 0' instruction Date: Wed, 15 Jan 2025 21:51:34 -0800 Message-ID: <20250116055134.604867-1-yonghong.song@linux.dev> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250116055123.603790-1-yonghong.song@linux.dev> References: <20250116055123.603790-1-yonghong.song@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Since 'may_goto 0' insns are actually no-op, let us remove them. Otherwise, verifier will generate code like /* r10 - 8 stores the implicit loop count */ r11 = *(u64 *)(r10 -8) if r11 == 0x0 goto pc+2 r11 -= 1 *(u64 *)(r10 -8) = r11 which is the pure overhead. The following code patterns (from the previous commit) are also handled: may_goto 2 may_goto 1 may_goto 0 With this commit, the above three 'may_goto' insns are all eliminated. Signed-off-by: Yonghong Song --- kernel/bpf/verifier.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index edf3cc42a220..72b474bfba2d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -20133,6 +20133,40 @@ static int opt_remove_nops(struct bpf_verifier_env *env) return 0; } +static int opt_remove_useless_may_gotos(struct bpf_verifier_env *env) +{ + struct bpf_insn *insn = env->prog->insnsi; + int i, j, err, last_may_goto, removed_cnt; + int insn_cnt = env->prog->len; + + for (i = 0; i < insn_cnt; i++) { + if (!is_may_goto_insn(&insn[i])) + continue; + + for (j = i + 1; j < insn_cnt; j++) { + if (!is_may_goto_insn(&insn[j])) + break; + } + + last_may_goto = --j; + removed_cnt = 0; + while (j >= i) { + if (insn[j].off == 0) { + err = verifier_remove_insns(env, j, 1); + if (err) + return err; + removed_cnt++; + } + j--; + } + + insn_cnt -= removed_cnt; + i = last_may_goto - removed_cnt; + } + + return 0; +} + static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, const union bpf_attr *attr) { @@ -23089,6 +23123,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 ret = opt_remove_dead_code(env); if (ret == 0) ret = opt_remove_nops(env); + if (ret == 0) + ret = opt_remove_useless_may_gotos(env); } else { if (ret == 0) sanitize_dead_code(env);