Skip to content

rust: add regulator consumer abstraction #1040

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

Draft
wants to merge 51 commits into
base: rust-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
1a5a9f4
rust: alloc: implement `ReallocFunc`
Oct 4, 2024
6797ab5
rust: alloc: make `allocator` module public
Oct 4, 2024
933d9e9
rust: alloc: implement `Allocator` for `Kmalloc`
Oct 4, 2024
2c2ea82
rust: alloc: add module `allocator_test`
Oct 4, 2024
d8416d9
rust: alloc: implement `Vmalloc` allocator
Oct 4, 2024
2e3b351
rust: alloc: implement `KVmalloc` allocator
Oct 4, 2024
51c3a4d
rust: alloc: add __GFP_NOWARN to `Flags`
Oct 4, 2024
89ec226
rust: alloc: implement kernel `Box`
Oct 4, 2024
5b36d1f
rust: treewide: switch to our kernel `Box` type
Oct 4, 2024
90acde2
rust: alloc: remove extension of std's `Box`
Oct 4, 2024
48134b2
rust: alloc: add `Box` to prelude
Oct 4, 2024
b457a49
rust: alloc: introduce `ArrayLayout`
BennoLossin Oct 4, 2024
77b4b24
rust: alloc: implement kernel `Vec` type
Oct 4, 2024
4678518
rust: alloc: implement `IntoIterator` for `Vec`
Oct 4, 2024
e4fcfb3
rust: alloc: implement `collect` for `IntoIter`
Oct 4, 2024
f1af10d
rust: treewide: switch to the kernel `Vec` type
Oct 4, 2024
0a001a1
rust: alloc: remove `VecExt` extension
Oct 4, 2024
13639ed
rust: alloc: add `Vec` to prelude
Oct 4, 2024
b9fce36
rust: error: use `core::alloc::LayoutError`
Oct 4, 2024
ee94f80
rust: error: check for config `test` in `Error::name`
Oct 4, 2024
061ef9e
rust: alloc: implement `contains` for `Flags`
Oct 4, 2024
ea53ed0
rust: alloc: implement `Cmalloc` in module allocator_test
Oct 4, 2024
6923f83
rust: str: test: replace `alloc::format`
Oct 4, 2024
f9fa508
rust: alloc: update module comment of alloc.rs
Oct 4, 2024
eed0405
kbuild: rust: remove the `alloc` crate and `GlobalAlloc`
Oct 4, 2024
8d8f785
MAINTAINERS: add entry for the Rust `alloc` module
Oct 4, 2024
da4a526
rust: init: introduce `Opaque::try_ffi_init`
wedsonaf Sep 29, 2023
19f84d2
rust: introduce `InPlaceModule`
wedsonaf Sep 29, 2023
d88dd90
rust: pass module name to `Module::init`
Oct 15, 2024
28f06ce
rust: implement generic driver registration
Oct 15, 2024
10ccf32
rust: implement `IdArray`, `IdTable` and `RawDeviceId`
Oct 15, 2024
ec2e215
rust: add rcu abstraction
wedsonaf Sep 4, 2023
9730145
rust: add `Revocable` type
wedsonaf Sep 4, 2023
f444613
rust: add `dev_*` print macros.
wedsonaf Feb 16, 2023
b1c72ac
rust: add `io::Io` base type
Oct 15, 2024
5abe57e
rust: add devres abstraction
Oct 15, 2024
265db15
fixup! rust: add `dev_*` print macros.
Fabo Oct 20, 2024
632bcff
fixup! rust: add devres abstraction
Fabo Oct 20, 2024
eae8e61
rust: str: allow dereferencing BStr in a const ctx
Fabo Mar 6, 2024
2282cba
rust: device_id: add macro for alias generation
Fabo Oct 20, 2024
cf0351f
rust: add basic OF abstraction
Nov 10, 2023
faf3c48
rust: enable associated_type_defaults unstable feature
Fabo Sep 20, 2024
b087b60
rust: add I2C client abstraction
kloenk Dec 17, 2022
1cf952d
rust: add function to create masks
Fabo Dec 16, 2023
84a8d2a
rust: macros: paste: add support for using var as part of group
Fabo Dec 19, 2023
563d61a
rust: macros: add foreach! macro to repeat code
Fabo Dec 20, 2023
f5a2ea2
rust: Add new abstraction for regmap
Fabo Nov 29, 2023
c197ae4
rust: error: Add declaration for ENOTRECOVERABLE error
Fabo Feb 8, 2024
dfc9c6a
rust: regulator: add regulator consumer abstractions
Fabo Nov 13, 2023
31a0a6b
rust: regulator: add Regulator Driver abstraction
Fabo Dec 6, 2023
0b42512
rust: regulator: Add support for regmap
Fabo Mar 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -20230,6 +20230,13 @@ F: scripts/*rust*
F: tools/testing/selftests/rust/
K: \b(?i:rust)\b

RUST [ALLOC]
M: Danilo Krummrich <[email protected]>
L: [email protected]
S: Maintained
F: rust/kernel/alloc.rs
F: rust/kernel/alloc/

RXRPC SOCKETS (AF_RXRPC)
M: David Howells <[email protected]>
M: Marc Dionne <[email protected]>
Expand Down
6 changes: 3 additions & 3 deletions drivers/block/rnull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ module! {
}

struct NullBlkModule {
_disk: Pin<Box<Mutex<GenDisk<NullBlkDevice>>>>,
_disk: Pin<KBox<Mutex<GenDisk<NullBlkDevice>>>>,
}

impl kernel::Module for NullBlkModule {
fn init(_module: &'static ThisModule) -> Result<Self> {
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
pr_info!("Rust null_blk loaded\n");
let tagset = Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?;

Expand All @@ -47,7 +47,7 @@ impl kernel::Module for NullBlkModule {
.rotational(false)
.build(format_args!("rnullb{}", 0), tagset)?;

let disk = Box::pin_init(new_mutex!(disk, "nullb:disk"), flags::GFP_KERNEL)?;
let disk = KBox::pin_init(new_mutex!(disk, "nullb:disk"), flags::GFP_KERNEL)?;

Ok(Self { _disk: disk })
}
Expand Down
2 changes: 1 addition & 1 deletion mm/kasan/kasan_test_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use kernel::prelude::*;
/// drop the vector, and touch it.
#[no_mangle]
pub extern "C" fn kasan_test_rust_uaf() -> u8 {
let mut v: Vec<u8> = Vec::new();
let mut v: KVec<u8> = KVec::new();
for _ in 0..4096 {
v.push(0x42, GFP_KERNEL).unwrap();
}
Expand Down
43 changes: 10 additions & 33 deletions rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ always-$(CONFIG_RUST) += libmacros.so
no-clean-files += libmacros.so

always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_helpers_generated.h \
obj-$(CONFIG_RUST) += bindings.o kernel.o
always-$(CONFIG_RUST) += exports_helpers_generated.h \
exports_bindings_generated.h exports_kernel_generated.h

always-$(CONFIG_RUST) += uapi/uapi_generated.rs
Expand Down Expand Up @@ -53,11 +53,6 @@ endif
core-cfgs = \
--cfg no_fp_fmt_parse

alloc-cfgs = \
--cfg no_global_oom_handling \
--cfg no_rc \
--cfg no_sync

quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
cmd_rustdoc = \
OBJTREE=$(abspath $(objtree)) \
Expand All @@ -81,7 +76,7 @@ quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
# command-like flags to solve the issue. Meanwhile, we use the non-custom case
# and then retouch the generated files.
rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
rustdoc-alloc rustdoc-kernel
rustdoc-kernel
$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
Expand All @@ -108,20 +103,11 @@ rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
+$(call if_changed,rustdoc)

# We need to allow `rustdoc::broken_intra_doc_links` because some
# `no_global_oom_handling` functions refer to non-`no_global_oom_handling`
# functions. Ideally `rustdoc` would have a way to distinguish broken links
# due to things that are "configured out" vs. entirely non-existing ones.
rustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \
-Arustdoc::broken_intra_doc_links
rustdoc-alloc: $(RUST_LIB_SRC)/alloc/src/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
+$(call if_changed,rustdoc)

rustdoc-kernel: private rustc_target_flags = --extern alloc \
rustdoc-kernel: private rustc_target_flags = \
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
--extern bindings --extern uapi
rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
rustdoc-compiler_builtins $(obj)/libmacros.so \
$(obj)/bindings.o FORCE
+$(call if_changed,rustdoc)

Expand Down Expand Up @@ -165,7 +151,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
OBJTREE=$(abspath $(objtree)) \
$(RUSTDOC) --test $(rust_flags) \
-L$(objtree)/$(obj) --extern alloc --extern kernel \
-L$(objtree)/$(obj) --extern kernel \
--extern build_error --extern macros \
--extern bindings --extern uapi \
--no-run --crate-name kernel -Zunstable-options \
Expand Down Expand Up @@ -201,7 +187,7 @@ rusttest-macros: $(src)/macros/lib.rs FORCE
+$(call if_changed,rustc_test)
+$(call if_changed,rustdoc_test)

rusttest-kernel: private rustc_target_flags = --extern alloc \
rusttest-kernel: private rustc_target_flags = \
--extern build_error --extern macros --extern bindings --extern uapi
rusttest-kernel: $(src)/kernel/lib.rs \
rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
Expand Down Expand Up @@ -314,9 +300,6 @@ quiet_cmd_exports = EXPORTS $@
$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
$(call if_changed,exports)

$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
$(call if_changed,exports)

# Even though Rust kernel modules should never use the bindings directly,
# symbols from the `bindings` crate and the C helpers need to be exported
# because Rust generics and inlined functions may not get their code generated
Expand Down Expand Up @@ -363,7 +346,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L

rust-analyzer:
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
--cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
--cfgs='core=$(core-cfgs)' \
$(realpath $(srctree)) $(realpath $(objtree)) \
$(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
Expand Down Expand Up @@ -401,12 +384,6 @@ $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
+$(call if_changed_rule,rustc_library)

$(obj)/alloc.o: private skip_clippy = 1
$(obj)/alloc.o: private skip_flags = -Wunreachable_pub
$(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
$(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE
+$(call if_changed_rule,rustc_library)

$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
+$(call if_changed_rule,rustc_library)

Expand All @@ -421,9 +398,9 @@ $(obj)/uapi.o: $(src)/uapi/lib.rs \
$(obj)/uapi/uapi_generated.rs FORCE
+$(call if_changed_rule,rustc_library)

$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
$(obj)/kernel.o: private rustc_target_flags = \
--extern build_error --extern macros --extern bindings --extern uapi
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o \
$(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
+$(call if_changed_rule,rustc_library)

Expand Down
5 changes: 5 additions & 0 deletions rust/bindings/bindings_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
#include <linux/errname.h>
#include <linux/ethtool.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <linux/jiffies.h>
#include <linux/mdio.h>
#include <linux/phy.h>
#include <linux/refcount.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/regulator/driver.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/wait.h>
Expand All @@ -31,4 +35,5 @@ const gfp_t RUST_CONST_HELPER_GFP_KERNEL_ACCOUNT = GFP_KERNEL_ACCOUNT;
const gfp_t RUST_CONST_HELPER_GFP_NOWAIT = GFP_NOWAIT;
const gfp_t RUST_CONST_HELPER___GFP_ZERO = __GFP_ZERO;
const gfp_t RUST_CONST_HELPER___GFP_HIGHMEM = ___GFP_HIGHMEM;
const gfp_t RUST_CONST_HELPER___GFP_NOWARN = ___GFP_NOWARN;
const blk_features_t RUST_CONST_HELPER_BLK_FEAT_ROTATIONAL = BLK_FEAT_ROTATIONAL;
1 change: 0 additions & 1 deletion rust/exports.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#define EXPORT_SYMBOL_RUST_GPL(sym) extern int sym; EXPORT_SYMBOL_GPL(sym)

#include "exports_core_generated.h"
#include "exports_alloc_generated.h"
#include "exports_helpers_generated.h"
#include "exports_bindings_generated.h"
#include "exports_kernel_generated.h"
Expand Down
10 changes: 10 additions & 0 deletions rust/helpers/device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: GPL-2.0

#include <linux/device.h>

int rust_helper_devm_add_action(struct device *dev,
void (*action)(void *),
void *data)
{
return devm_add_action(dev, action, data);
}
6 changes: 6 additions & 0 deletions rust/helpers/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
#include "bug.c"
#include "build_assert.c"
#include "build_bug.c"
#include "device.c"
#include "err.c"
#include "i2c.c"
#include "io.c"
#include "kunit.c"
#include "mutex.c"
#include "page.c"
#include "rbtree.c"
#include "rcu.c"
#include "refcount.c"
#include "regmap.c"
#include "signal.c"
#include "slab.c"
#include "spinlock.c"
#include "task.c"
#include "uaccess.c"
#include "vmalloc.c"
#include "wait.c"
#include "workqueue.c"
13 changes: 13 additions & 0 deletions rust/helpers/i2c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: GPL-2.0

#include <linux/i2c.h>

void *rust_helper_i2c_get_clientdata(const struct i2c_client *client)
{
return i2c_get_clientdata(client);
}

void rust_helper_i2c_set_clientdata(struct i2c_client *client, void *data)
{
i2c_set_clientdata(client, data);
}
91 changes: 91 additions & 0 deletions rust/helpers/io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: GPL-2.0

#include <linux/io.h>

u8 rust_helper_readb(const volatile void __iomem *addr)
{
return readb(addr);
}

u16 rust_helper_readw(const volatile void __iomem *addr)
{
return readw(addr);
}

u32 rust_helper_readl(const volatile void __iomem *addr)
{
return readl(addr);
}

#ifdef CONFIG_64BIT
u64 rust_helper_readq(const volatile void __iomem *addr)
{
return readq(addr);
}
#endif

void rust_helper_writeb(u8 value, volatile void __iomem *addr)
{
writeb(value, addr);
}

void rust_helper_writew(u16 value, volatile void __iomem *addr)
{
writew(value, addr);
}

void rust_helper_writel(u32 value, volatile void __iomem *addr)
{
writel(value, addr);
}

#ifdef CONFIG_64BIT
void rust_helper_writeq(u64 value, volatile void __iomem *addr)
{
writeq(value, addr);
}
#endif

u8 rust_helper_readb_relaxed(const volatile void __iomem *addr)
{
return readb_relaxed(addr);
}

u16 rust_helper_readw_relaxed(const volatile void __iomem *addr)
{
return readw_relaxed(addr);
}

u32 rust_helper_readl_relaxed(const volatile void __iomem *addr)
{
return readl_relaxed(addr);
}

#ifdef CONFIG_64BIT
u64 rust_helper_readq_relaxed(const volatile void __iomem *addr)
{
return readq_relaxed(addr);
}
#endif

void rust_helper_writeb_relaxed(u8 value, volatile void __iomem *addr)
{
writeb_relaxed(value, addr);
}

void rust_helper_writew_relaxed(u16 value, volatile void __iomem *addr)
{
writew_relaxed(value, addr);
}

void rust_helper_writel_relaxed(u32 value, volatile void __iomem *addr)
{
writel_relaxed(value, addr);
}

#ifdef CONFIG_64BIT
void rust_helper_writeq_relaxed(u64 value, volatile void __iomem *addr)
{
writeq_relaxed(value, addr);
}
#endif
13 changes: 13 additions & 0 deletions rust/helpers/rcu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: GPL-2.0

#include <linux/rcupdate.h>

void rust_helper_rcu_read_lock(void)
{
rcu_read_lock();
}

void rust_helper_rcu_read_unlock(void)
{
rcu_read_unlock();
}
48 changes: 48 additions & 0 deletions rust/helpers/regmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-2.0

#include <linux/i2c.h>
#include <linux/regmap.h>

#if IS_BUILTIN(CONFIG_REGMAP_I2C)
struct regmap *rust_helper_regmap_init_i2c(struct i2c_client *i2c,
const struct regmap_config *config)
{
return regmap_init_i2c(i2c, config);
}
#endif

int rust_helper_regmap_field_write(struct regmap_field *field, unsigned int val)
{
return regmap_field_write(field, val);
}

int rust_helper_regmap_field_force_write(struct regmap_field *field,
unsigned int val)
{
return regmap_field_force_write(field, val);
}

int rust_helper_regmap_field_update_bits(struct regmap_field *field,
unsigned int mask, unsigned int val)
{
return regmap_field_update_bits(field, mask, val);
}

int rust_helper_regmap_field_set_bits(struct regmap_field *field,
unsigned int bits)
{
return regmap_field_set_bits(field, bits);
}

int rust_helper_regmap_field_clear_bits(struct regmap_field *field,
unsigned int bits)
{
return regmap_field_clear_bits(field, bits);
}

int rust_helper_regmap_field_force_update_bits(struct regmap_field *field,
unsigned int mask,
unsigned int val)
{
return regmap_field_force_update_bits(field, mask, val);
}
Loading