Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rust int param types #87

Merged
merged 10 commits into from
Mar 3, 2021
Merged

Conversation

adamrk
Copy link

@adamrk adamrk commented Feb 13, 2021

Adds module param types for i8, i64, isize, and usize. This is done using a trait ModuleParam which will be expanded to work for custom param types as well. Addresses part of #11.

rust/kernel/module_param.rs Outdated Show resolved Hide resolved
rust/Makefile Outdated Show resolved Hide resolved
rust/kernel/module_param.rs Outdated Show resolved Hide resolved
rust/kernel/module_param.rs Outdated Show resolved Hide resolved
rust/kernel/module_param.rs Outdated Show resolved Hide resolved
rust/kernel/module_param.rs Outdated Show resolved Hide resolved
rust/kernel/module_param.rs Outdated Show resolved Hide resolved
@adamrk
Copy link
Author

adamrk commented Feb 26, 2021

Rebased onto the latest changes. Does this need a second review?

rust/Makefile Outdated Show resolved Hide resolved
rust/kernel/buffer.rs Show resolved Hide resolved
rust/kernel/module_param.rs Outdated Show resolved Hide resolved
rust/kernel/module_param.rs Show resolved Hide resolved
rust/kernel/module_param.rs Show resolved Hide resolved
rust/kernel/module_param.rs Show resolved Hide resolved
rust/kernel/module_param.rs Show resolved Hide resolved
rust/kernel/module_param.rs Outdated Show resolved Hide resolved
rust/kernel/module_param.rs Outdated Show resolved Hide resolved
rust/kernel/module_param.rs Outdated Show resolved Hide resolved
@ojeda
Copy link
Member

ojeda commented Feb 27, 2021

Rebased onto the latest changes.

Yeah sorry, we should have merged this before -- anyway this gives a chance to review the docs; the person who writes some code is the best one that can write the documentation anyway :)

@adamrk
Copy link
Author

adamrk commented Feb 27, 2021

Yeah sorry, we should have merged this before -- anyway this gives a chance to review the docs; the person who writes some code is the best one that can write the documentation anyway :)

No worries.

Comment on lines 3 to 5
//! Struct for writing to a pre-allocated buffer with the [`write!`] macro.
//!
//! [`write!`]: https://doc.rust-lang.org/core/macro.write.html
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
//! Struct for writing to a pre-allocated buffer with the [`write!`] macro.
//!
//! [`write!`]: https://doc.rust-lang.org/core/macro.write.html
//! Struct for writing to a pre-allocated buffer with the [`write!`] macro.

Intra-doc links (for core and alloc, not std since we don't use it) should work automatically (or did you try and it was not working?).

Copy link
Author

Choose a reason for hiding this comment

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

Ah, I didn't try it. Was just following the pattern for existing links.

Looks like items in core and alloc link to doc.rust-lang.org even without explicitly setting them. We're not generating our own docs for core and alloc - at least I don't see any rules for them in the Makefile and they don't seem to be generated by make rustdoc.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, they go to the official docs at the moment, but perhaps we should generate them too. I can look into that. Let me create an issue.

Copy link
Member

Choose a reason for hiding this comment

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

#97

rust/kernel/lib.rs Show resolved Hide resolved
Comment on lines 11 to 17
/// Note that displaying the type in `sysfs` will fail if
/// [`alloc::string::ToString::to_string`] (as implemented through the
/// [`core::fmt::Display`] trait) writes more than `kernel::PAGE_SIZE`
/// bytes (including an additional null terminator).
///
/// [`alloc::string::ToString::to_string`]: https://doc.rust-lang.org/alloc/string/trait.ToString.html#tymethod.to_string
/// [`core::fmt::Display`]: https://doc.rust-lang.org/core/fmt/trait.Display.html
Copy link
Member

@ojeda ojeda Feb 28, 2021

Choose a reason for hiding this comment

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

Suggested change
/// Note that displaying the type in `sysfs` will fail if
/// [`alloc::string::ToString::to_string`] (as implemented through the
/// [`core::fmt::Display`] trait) writes more than `kernel::PAGE_SIZE`
/// bytes (including an additional null terminator).
///
/// [`alloc::string::ToString::to_string`]: https://doc.rust-lang.org/alloc/string/trait.ToString.html#tymethod.to_string
/// [`core::fmt::Display`]: https://doc.rust-lang.org/core/fmt/trait.Display.html
/// Note that displaying the type in `sysfs` will fail if
/// [`alloc::string::ToString::to_string`] (as implemented through the
/// [`core::fmt::Display`] trait) writes more than [`PAGE_SIZE`]
/// bytes (including an additional null terminator).

Ditto: i.e. the Rust intra-doc links are found automatically by rustdoc.

I think PAGE_SIZE will also work without kernel::, but please double-check.

Copy link
Author

Choose a reason for hiding this comment

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

Removed the links rust-lang.org, but PAGE_SIZE needs to be referenced as crate::PAGE_SIZE.

@ojeda
Copy link
Member

ojeda commented Mar 3, 2021

Let's get this in!

Thanks a lot for all the work and enduring the reviews!

@ojeda ojeda merged commit 304ee69 into Rust-for-Linux:rust Mar 3, 2021
@ojeda ojeda mentioned this pull request Mar 8, 2021
13 tasks
ojeda pushed a commit that referenced this pull request Jul 29, 2021
During testing of f263a81 ("bpf: Track subprog poke descriptors correctly
and fix use-after-free") under various failure conditions, for example, when
jit_subprogs() fails and tries to clean up the program to be run under the
interpreter, we ran into the following freeze:

  [...]
  #127/8 tailcall_bpf2bpf_3:FAIL
  [...]
  [   92.041251] BUG: KASAN: slab-out-of-bounds in ___bpf_prog_run+0x1b9d/0x2e20
  [   92.042408] Read of size 8 at addr ffff88800da67f68 by task test_progs/682
  [   92.043707]
  [   92.044030] CPU: 1 PID: 682 Comm: test_progs Tainted: G   O   5.13.0-53301-ge6c08cb33a30-dirty #87
  [   92.045542] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1 04/01/2014
  [   92.046785] Call Trace:
  [   92.047171]  ? __bpf_prog_run_args64+0xc0/0xc0
  [   92.047773]  ? __bpf_prog_run_args32+0x8b/0xb0
  [   92.048389]  ? __bpf_prog_run_args64+0xc0/0xc0
  [   92.049019]  ? ktime_get+0x117/0x130
  [...] // few hundred [similar] lines more
  [   92.659025]  ? ktime_get+0x117/0x130
  [   92.659845]  ? __bpf_prog_run_args64+0xc0/0xc0
  [   92.660738]  ? __bpf_prog_run_args32+0x8b/0xb0
  [   92.661528]  ? __bpf_prog_run_args64+0xc0/0xc0
  [   92.662378]  ? print_usage_bug+0x50/0x50
  [   92.663221]  ? print_usage_bug+0x50/0x50
  [   92.664077]  ? bpf_ksym_find+0x9c/0xe0
  [   92.664887]  ? ktime_get+0x117/0x130
  [   92.665624]  ? kernel_text_address+0xf5/0x100
  [   92.666529]  ? __kernel_text_address+0xe/0x30
  [   92.667725]  ? unwind_get_return_address+0x2f/0x50
  [   92.668854]  ? ___bpf_prog_run+0x15d4/0x2e20
  [   92.670185]  ? ktime_get+0x117/0x130
  [   92.671130]  ? __bpf_prog_run_args64+0xc0/0xc0
  [   92.672020]  ? __bpf_prog_run_args32+0x8b/0xb0
  [   92.672860]  ? __bpf_prog_run_args64+0xc0/0xc0
  [   92.675159]  ? ktime_get+0x117/0x130
  [   92.677074]  ? lock_is_held_type+0xd5/0x130
  [   92.678662]  ? ___bpf_prog_run+0x15d4/0x2e20
  [   92.680046]  ? ktime_get+0x117/0x130
  [   92.681285]  ? __bpf_prog_run32+0x6b/0x90
  [   92.682601]  ? __bpf_prog_run64+0x90/0x90
  [   92.683636]  ? lock_downgrade+0x370/0x370
  [   92.684647]  ? mark_held_locks+0x44/0x90
  [   92.685652]  ? ktime_get+0x117/0x130
  [   92.686752]  ? lockdep_hardirqs_on+0x79/0x100
  [   92.688004]  ? ktime_get+0x117/0x130
  [   92.688573]  ? __cant_migrate+0x2b/0x80
  [   92.689192]  ? bpf_test_run+0x2f4/0x510
  [   92.689869]  ? bpf_test_timer_continue+0x1c0/0x1c0
  [   92.690856]  ? rcu_read_lock_bh_held+0x90/0x90
  [   92.691506]  ? __kasan_slab_alloc+0x61/0x80
  [   92.692128]  ? eth_type_trans+0x128/0x240
  [   92.692737]  ? __build_skb+0x46/0x50
  [   92.693252]  ? bpf_prog_test_run_skb+0x65e/0xc50
  [   92.693954]  ? bpf_prog_test_run_raw_tp+0x2d0/0x2d0
  [   92.694639]  ? __fget_light+0xa1/0x100
  [   92.695162]  ? bpf_prog_inc+0x23/0x30
  [   92.695685]  ? __sys_bpf+0xb40/0x2c80
  [   92.696324]  ? bpf_link_get_from_fd+0x90/0x90
  [   92.697150]  ? mark_held_locks+0x24/0x90
  [   92.698007]  ? lockdep_hardirqs_on_prepare+0x124/0x220
  [   92.699045]  ? finish_task_switch+0xe6/0x370
  [   92.700072]  ? lockdep_hardirqs_on+0x79/0x100
  [   92.701233]  ? finish_task_switch+0x11d/0x370
  [   92.702264]  ? __switch_to+0x2c0/0x740
  [   92.703148]  ? mark_held_locks+0x24/0x90
  [   92.704155]  ? __x64_sys_bpf+0x45/0x50
  [   92.705146]  ? do_syscall_64+0x35/0x80
  [   92.706953]  ? entry_SYSCALL_64_after_hwframe+0x44/0xae
  [...]

Turns out that the program rejection from e411901 ("bpf: allow for tailcalls
in BPF subprograms for x64 JIT") is buggy since env->prog->aux->tail_call_reachable
is never true. Commit ebf7d1f ("bpf, x64: rework pro/epilogue and tailcall
handling in JIT") added a tracker into check_max_stack_depth() which propagates
the tail_call_reachable condition throughout the subprograms. This info is then
assigned to the subprogram's func[i]->aux->tail_call_reachable. However, in the
case of the rejection check upon JIT failure, env->prog->aux->tail_call_reachable
is used. func[0]->aux->tail_call_reachable which represents the main program's
information did not propagate this to the outer env->prog->aux, though. Add this
propagation into check_max_stack_depth() where it needs to belong so that the
check can be done reliably.

Fixes: ebf7d1f ("bpf, x64: rework pro/epilogue and tailcall handling in JIT")
Fixes: e411901 ("bpf: allow for tailcalls in BPF subprograms for x64 JIT")
Co-developed-by: John Fastabend <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
Signed-off-by: John Fastabend <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Acked-by: Maciej Fijalkowski <[email protected]>
Link: https://lore.kernel.org/bpf/618c34e3163ad1a36b1e82377576a6081e182f25.1626123173.git.daniel@iogearbox.net
Sign up for free to 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.

2 participants