mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2024-12-29 09:13:38 +00:00
rust: add bindings
crate
This crate contains the bindings to the C side of the kernel. Calling C (in general, FFI) is assumed to be unsafe in Rust and, in many cases, this is accurate. For instance, virtually all C functions that take a pointer are unsafe since, typically, it will be dereferenced at some point (and in most cases there is no way for the callee to check its validity beforehand). Since one of the goals of using Rust in the kernel is precisely to avoid unsafe code in "leaf" kernel modules (e.g. drivers), these bindings should not be used directly by them. Instead, these bindings need to be wrapped into safe abstractions. These abstractions provide a safe API that kernel modules can use. In this way, unsafe code in kernel modules is minimized. Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com> Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com> Co-developed-by: Finn Behrens <me@kloenk.de> Signed-off-by: Finn Behrens <me@kloenk.de> Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com> Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com> Co-developed-by: Sven Van Asbroeck <thesven73@gmail.com> Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com> Co-developed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Gary Guo <gary@garyguo.net> Co-developed-by: Maciej Falkowski <m.falkowski@samsung.com> Signed-off-by: Maciej Falkowski <m.falkowski@samsung.com> Co-developed-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com> Signed-off-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com> Co-developed-by: Björn Roy Baron <bjorn3_gh@protonmail.com> Signed-off-by: Björn Roy Baron <bjorn3_gh@protonmail.com> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
parent
1fbde52bde
commit
8326ac05ee
13
rust/bindings/bindings_helper.h
Normal file
13
rust/bindings/bindings_helper.h
Normal file
@ -0,0 +1,13 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
/*
|
||||
* Header that contains the code (mostly headers) for which Rust bindings
|
||||
* will be automatically generated by `bindgen`.
|
||||
*
|
||||
* Sorted alphabetically.
|
||||
*/
|
||||
|
||||
#include <linux/slab.h>
|
||||
|
||||
/* `bindgen` gets confused at certain things. */
|
||||
const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL;
|
||||
const gfp_t BINDINGS___GFP_ZERO = __GFP_ZERO;
|
53
rust/bindings/lib.rs
Normal file
53
rust/bindings/lib.rs
Normal file
@ -0,0 +1,53 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
//! Bindings.
|
||||
//!
|
||||
//! Imports the generated bindings by `bindgen`.
|
||||
//!
|
||||
//! This crate may not be directly used. If you need a kernel C API that is
|
||||
//! not ported or wrapped in the `kernel` crate, then do so first instead of
|
||||
//! using this crate.
|
||||
|
||||
#![no_std]
|
||||
#![feature(core_ffi_c)]
|
||||
// See <https://github.com/rust-lang/rust-bindgen/issues/1651>.
|
||||
#![cfg_attr(test, allow(deref_nullptr))]
|
||||
#![cfg_attr(test, allow(unaligned_references))]
|
||||
#![cfg_attr(test, allow(unsafe_op_in_unsafe_fn))]
|
||||
#![allow(
|
||||
clippy::all,
|
||||
missing_docs,
|
||||
non_camel_case_types,
|
||||
non_upper_case_globals,
|
||||
non_snake_case,
|
||||
improper_ctypes,
|
||||
unreachable_pub,
|
||||
unsafe_op_in_unsafe_fn
|
||||
)]
|
||||
|
||||
mod bindings_raw {
|
||||
// Use glob import here to expose all helpers.
|
||||
// Symbols defined within the module will take precedence to the glob import.
|
||||
pub use super::bindings_helper::*;
|
||||
include!(concat!(
|
||||
env!("OBJTREE"),
|
||||
"/rust/bindings/bindings_generated.rs"
|
||||
));
|
||||
}
|
||||
|
||||
// When both a directly exposed symbol and a helper exists for the same function,
|
||||
// the directly exposed symbol is preferred and the helper becomes dead code, so
|
||||
// ignore the warning here.
|
||||
#[allow(dead_code)]
|
||||
mod bindings_helper {
|
||||
// Import the generated bindings for types.
|
||||
include!(concat!(
|
||||
env!("OBJTREE"),
|
||||
"/rust/bindings/bindings_helpers_generated.rs"
|
||||
));
|
||||
}
|
||||
|
||||
pub use bindings_raw::*;
|
||||
|
||||
pub const GFP_KERNEL: gfp_t = BINDINGS_GFP_KERNEL;
|
||||
pub const __GFP_ZERO: gfp_t = BINDINGS___GFP_ZERO;
|
Loading…
Reference in New Issue
Block a user