Skip to content

[RFC] A fallible from_kernel_errno with Result<Error> return - #347

Closed
foxhlchen wants to merge 1 commit into
Rust-for-Linux:rustfrom
foxhlchen:fallible-from_kernel_errno
Closed

[RFC] A fallible from_kernel_errno with Result<Error> return#347
foxhlchen wants to merge 1 commit into
Rust-for-Linux:rustfrom
foxhlchen:fallible-from_kernel_errno

Conversation

@foxhlchen

@foxhlchenfoxhlchen commented Jun 4, 2021

Copy link
Copy Markdown

Currently, from_kernel_errno() is an infallible function acting as a constructor for Error. In order to achieve its type invariant, We add a check in it which will prompt a warning and return Error::EINVL when errno given is invalid.

While this approach ensures type invariant, it brings great ambiguities. When Error::EINVL is returned, the caller has no way to recognize whether it is a valid errno coming from the kernel or an error issued by the check. This tricky behavior may confuse developers and introduce subtle bugs. Since Error will be used in all respects of the kernel, It's definitely not
a sound solution.

This RFC proposes that we make from_kernel_errno() return a Result<Error>. Thus, we have an explicit, clear, and fallible version of from_kernel_errno() by which callers are able to know what really happened behind the scene. And it also provides certain flexibility. We pass the power to callers, they can decide how to deal with invalid errno case by case.

Note:

  • The commit is a proposal that can't be compiled at this moment. Because a few other codes rely on it, they need to be changed as well.
  • Another approach @ojeda mentioned here is to propose upstream to have a dedicated errno (e.g. EBUG or EIKE). This is also viable, however: 1) it needs interaction with upstream 2) from_kernel_errno is still an infallible function that can fail.

Reference:
#324#283

May influence:
#335

@ksquirrel

This comment has been minimized.

Currently, from_kernel_errno is an infallible function acting as a
constructor for Error. In order to achieve its type invariant, We add
a check in it which will prompt a warning and return Error::EINVL when
errno given is invalid.
While this approach ensures type invariant, it brings great
ambiguities. When Error::EINVL is returned, the caller has no way to
recognize whether it is a valid errno coming from the kernel or an
error issued by the check. This tricky behavior may confuse developers
and introduce subtle bugs. Since Error will be used in all respects of
the kernel, It's definitely not a sound solution.
This RFC proposes that we make from_kernel_errno return a
Result<Error>. Thus, we have an explicit, clear, and fallible version
of from_kernel_errno by which callers are able to know what really
happened behind the scene. And it also provides certain flexibility.
We pass the power to callers, they can decide how to deal with invalid
`errno` case by case.
Signed-off-by: Fox Chen <foxhlchen@gmail.com>
@foxhlchen
foxhlchenforce-pushed the fallible-from_kernel_errno branch from d0ac814 to b11524bCompareJune 5, 2021 03:39
@ksquirrel

Copy link
Copy Markdown
Member

Review of b11524bfec5f:

Comment threadrust/kernel/error.rs
/// Creates an [`Error`] from a kernel error code.
///
/// It is a bug to pass an out-of-range `errno`. `EINVAL` would
/// It is a bug to pass an out-of-range `errno`. Err(`EINVAL`) would

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// It is a bug to pass an out-of-range `errno`. Err(`EINVAL`) would
/// It is a bug to pass an out-of-range `errno`. `Err(EINVAL)` would

@foxhlchen
foxhlchen marked this pull request as draft July 29, 2021 01:19
ojeda pushed a commit that referenced this pull request Jun 11, 2024
Add a test case to assert that the skb->pkt_type which was set from the BPF
program is retained from the netkit xmit side to the peer's device at tcx
ingress location.
# ./vmtest.sh -- ./test_progs -t netkit
[...]
./test_progs -t netkit
[ 1.140780] bpf_testmod: loading out-of-tree module taints kernel.
[ 1.141127] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel
[ 1.284601] tsc: Refined TSC clocksource calibration: 3408.006 MHz
[ 1.286672] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fd9b189d, max_idle_ns: 440795225691 ns
[ 1.290384] clocksource: Switched to clocksource tsc
#345 tc_netkit_basic:OK
#346 tc_netkit_device:OK
#347 tc_netkit_multi_links:OK
#348 tc_netkit_multi_opts:OK
#349 tc_netkit_neigh_links:OK
#350 tc_netkit_pkt_type:OK
Summary: 6/0 PASSED, 0 SKIPPED, 0 FAILED
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20240524163619.26001-4-daniel@iogearbox.net
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@foxhlchen@ksquirrel@nbdd0121