All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 0/2] bpf: Warn with bpf_unreachable() kfunc maybe due to uninitialized variable
@ 2025-05-19 20:33 Yonghong Song
  2025-05-19 20:33 ` [PATCH bpf-next v3 1/2] " Yonghong Song
  2025-05-19 20:33 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add unit tests with bpf_unreachable() kfunc Yonghong Song
  0 siblings, 2 replies; 13+ messages in thread
From: Yonghong Song @ 2025-05-19 20:33 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team,
	Martin KaFai Lau

Marc Suñé (Isovalent, part of Cisco) reported an issue where an
uninitialized variable caused generating bpf prog binary code not
working as expected. The reproducer is in [1] where the flags
“-Wall -Werror” are enabled, but there is no warning as the compiler
takes advantage of uninitialized variable to do aggressive optimization.
Such optimization results in a verification log:
  last insn is not an exit or jmp
User still needs to take quite some time to figure out what is
the root cause.

To give a better hint to user, bpf_unreachable() kfunc is introduced
in kernel and the compiler ([2]) will encode bpf_unreachable()
as needed. For example, compiler may generate 'unreachable' IR
after do optimizaiton by taking advantage of uninitialized variable,
and later bpf backend will translate such 'unreachable' IR to
bpf_unreachable() func in final binary. When kernel detects
bpf_unreachable(), it is able to issue much better verifier log, e.g.
  unexpected bpf_unreachable() due to uninitialized variable?

  [1] https://github.com/msune/clang_bpf/blob/main/Makefile#L3
  [2] https://github.com/llvm/llvm-project/pull/131731

Changelogs:
  v2 -> v3:
    - v2: https://lore.kernel.org/bpf/CAADnVQL9A8vB-yRjnZn8bgMrfDSO17FFBtS_xOs5w-LSq+p74g@mail.gmail.com/
    - The newer llvm patch (above [2]) added 'exit' insn if the last insn
      in the function is bpf_unreachable(). This way, check_subprogs()
      handling is unnecessary and removed.
    - Remove the big C test (above [1]) and add a simple C test and three
      inline asm tests.

  v1 -> v2:
    - v1: https://lore.kernel.org/bpf/20250511182744.1806792-1-yonghong.song@linux.dev/
    - If bpf_unreachable() is hit during check_kfunc_call(), report the
      verification failure.
    - Add three inline asm test cases.

Yonghong Song (2):
  bpf: Warn with bpf_unreachable() kfunc maybe due to uninitialized
    variable
  selftests/bpf: Add unit tests with bpf_unreachable() kfunc

 kernel/bpf/helpers.c                          |  5 ++
 kernel/bpf/verifier.c                         |  5 ++
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 .../bpf/progs/verifier_bpf_unreachable.c      | 61 +++++++++++++++++++
 4 files changed, 73 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_bpf_unreachable.c

-- 
2.47.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2025-05-20 21:14 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-19 20:33 [PATCH bpf-next v3 0/2] bpf: Warn with bpf_unreachable() kfunc maybe due to uninitialized variable Yonghong Song
2025-05-19 20:33 ` [PATCH bpf-next v3 1/2] " Yonghong Song
2025-05-19 22:48   ` Alexei Starovoitov
2025-05-20 15:25     ` Yonghong Song
2025-05-20 16:29       ` Alexei Starovoitov
2025-05-20 18:01         ` Yonghong Song
2025-05-20 18:39           ` Alexei Starovoitov
2025-05-20 18:46             ` Alexei Starovoitov
2025-05-20 19:50               ` Yonghong Song
2025-05-20 19:40             ` Yonghong Song
2025-05-20 20:59             ` Yonghong Song
2025-05-20 21:14               ` Alexei Starovoitov
2025-05-19 20:33 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add unit tests with bpf_unreachable() kfunc Yonghong Song

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.