rust: crates in other kernel directories
v3 cover rust: crates in other kernel directories
This RFC provides makes possible to have bindings for kernel subsystems that are compiled as modules.
Previously, if you wanted to have Rust bindings for a subsystem, like AMBA for example, you had to put it under rust/kernel/ so it came part of the kernel crate, but this came with many downsides. Namely if you compiled said subsystem as a module you've a dependency on it from kernel, which is linked directly on vmlinux.
So instead of overpopulating kernel with a gazillion modules that throws you into dire straits you should rather have the bindings in the same directory as the subsystem you want to bind with and link it to it.
With this patch Rust sources can be compiled into libraries for them to be consumed. These libraries are ar archives that follow the .rlib structure, namely a libfoo.rlib thin archive with a foo.foo.o object and a libfoo.rmeta rustc metadata as members. Such Rust crates get their symbols exposed and the bindings crate is made available for them.
Also included there's a sample usage of this in another patch, but it is not meant to be merged as it remains as an example.
If you want to use a crate with your Rust module just add a rust-libs variable in your Makefile with a value of the relative directory of said crate plus its name, e.g.
# Link with the foo crate
rust-libs += ../path/to/foo
Martin Rodriguez Reboredo (2):
kbuild: Build Rust crates as libraries
samples: rust: Add USB sample bindings
.gitignore | 2 ++ Makefile | 4 ++-- drivers/usb/core/Kconfig | 7 ++++++ drivers/usb/core/Makefile | 3 +++ drivers/usb/core/usb.rs | 13 ++++++++++ rust/bindings/bindings_helper.h | 1 + samples/rust/Kconfig | 10 ++++++++ samples/rust/Makefile | 3 +++ samples/rust/rust_usb_simple.rs | 22 +++++++++++++++++ scripts/Makefile.build | 42 ++++++++++++++++++++++++++++++--- scripts/Makefile.lib | 20 ++++++++++++---- scripts/Makefile.modfinal | 9 +++++-- 12 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 drivers/usb/core/usb.rs create mode 100644 samples/rust/rust_usb_simple.rs
Signature · 1 line
2.42.1
RFC PATCH v3 1/2 kbuild: Build Rust crates as libraries
Enables compiling Rust crates as dependencies of kernel modules. These are also meant to be used as libraries for other parts of the kernel.
For these crates bindings is also exposed to them and they get their symbols exported for them to be used by other code.
When a composite object depends on an .rlib file, which by the way is a current ar archive, Kbuild will compile it from its base Rust source and archive it.
This makes possible to have Rust bindings for a subsystem that is compiled either built-in or as a module. They can also be made into modules by themselves too.
---
v2 -> v3:
- Fixed erroneous dependency on exported symbols from KUnit tests. v1 -> v2:
- Fixed builtins compilation.
- Added support for building crates as modules.
.gitignore | 2 ++ Makefile | 4 ++-- scripts/Makefile.build | 42 ++++++++++++++++++++++++++++++++++++--- scripts/Makefile.lib | 20 +++++++++++++++---- scripts/Makefile.modfinal | 9 +++++++-- 5 files changed, 66 insertions(+), 11 deletions(-)
.gitignore +2 -0
@@ -38,6 +38,7 @@ *.o *.o.* *.patch +*.rlib *.rmeta *.rpm *.rsi
@@ -53,6 +54,7 @@ *.zst Module.symvers modules.order +exports_*_generated.c # # Top-level generic files
Makefile +2 -2
@@ -283,7 +283,7 @@ no-compiler-targets := $(no-dot-config-targets) install dtbs_install \ headers_install modules_install modules_sign kernelrelease image_name no-sync-config-targets := $(no-dot-config-targets) %install modules_sign kernelrelease \ image_name -single-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rsi %.s %.symtypes %/ +single-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rlib %.rsi %.s %.symtypes %/ config-build := mixed-build :=
@@ -1919,7 +1919,7 @@ $(clean-dirs): clean: $(clean-dirs) $(call cmd,rmfiles) @find $(or $(KBUILD_EXTMOD), .) $(RCS_FIND_IGNORE) \ - \( -name '*.[aios]' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \ + \( -name '*.[aios]' -o -name '*.rlib' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \ -o -name '*.ko.*' \ -o -name '*.dtb' -o -name '*.dtbo' \ -o -name '*.dtb.S' -o -name '*.dtbo.S' \
scripts/Makefile.build +39 -3
72 lines not on this page · raw message
scripts/Makefile.lib +16 -4
@@ -45,12 +45,17 @@ else obj-y := $(filter-out %/, $(obj-y)) endif +crate-obj-y := $(foreach m, $(filter lib%.rlib, $(obj-y)), $(m:lib%.rlib=%.o)) +crate-obj-m := $(foreach m, $(filter lib%.rlib, $(obj-m)), $(m:lib%.rlib=%.o)) + # Expand $(foo-objs) $(foo-y) etc. by replacing their individuals suffix-search = $(strip $(foreach s, $3, $($(1:%$(strip $2)=%$s)))) # List composite targets that are constructed by combining other targets multi-search = $(sort $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $m))) # List primitive targets that are compiled from source files real-search = $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $(call suffix-search, $m, $2, $3), $m)) +# List exported symbols from .rlib targets +rlib-exports = $(foreach m, $1, $(if $(findstring .rlib, $m),$(m:lib%.rlib=exports_%_generated.o)) $m) # If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object multi-obj-y := $(call multi-search, $(obj-y), .o, -objs -y)
@@ -59,8 +64,8 @@ multi-obj-ym := $(multi-obj-y) $(multi-obj-m) # Replace multi-part objects by their individual parts, # including built-in.a from subdirectories -real-obj-y := $(call real-search, $(obj-y), .o, -objs -y) -real-obj-m := $(call real-search, $(obj-m), .o, -objs -y -m) +real-obj-y := $(call rlib-exports, $(call real-search, $(obj-y), .o, -objs -y)) +real-obj-m := $(call rlib-exports, $(call real-search, $(obj-m), .o, -objs -y -m)) always-y += $(always-m)
@@ -96,6 +101,8 @@ always-y := $(addprefix $(obj)/,$(always-y)) targets := $(addprefix $(obj)/,$(targets)) obj-m := $(addprefix $(obj)/,$(obj-m)) lib-y := $(addprefix $(obj)/,$(lib-y)) +crate-obj-y := $(addprefix $(obj)/,$(crate-obj-y)) +crate-obj-m := $(addprefix $(obj)/,$(crate-obj-m)) real-obj-y := $(addprefix $(obj)/,$(real-obj-y)) real-obj-m := $(addprefix $(obj)/,$(real-obj-m)) multi-obj-m := $(addprefix $(obj)/, $(multi-obj-m))
@@ -210,7 +217,11 @@ _cpp_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj) endif endif -part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y) +_rust_libs = $(foreach l, $(rust-libs), -L $(obj)/$(dir $l) --extern $(notdir $(basename $l))) + +part-of-module = \ + $(if $(or $(filter $(basename $@).o, $(real-obj-m)), \ + $(filter $(basename $@).rlib, $(real-obj-m))),y) quiet_modtag = $(if $(part-of-module),[M], ) modkern_cflags = \
@@ -232,7 +243,8 @@ c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ $(_c_flags) $(modkern_cflags) \ $(basename_flags) $(modname_flags) -rust_flags = $(_rust_flags) $(modkern_rustflags) @$(objtree)/include/generated/rustc_cfg +rust_flags = $(_rust_flags) $(modkern_rustflags) $(_rust_libs) \ + @$(objtree)/include/generated/rustc_cfg a_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ $(_a_flags) $(modkern_aflags)
scripts/Makefile.modfinal +7 -2
@@ -19,7 +19,7 @@ __modfinal: $(modules:%.o=%.ko) @: # modname and part-of-module are set to make c_flags define proper module flags -modname = $(notdir $(@:.mod.o=)) +modname = $(notdir $*) part-of-module = y quiet_cmd_cc_o_c = CC [M] $@
@@ -30,11 +30,16 @@ quiet_cmd_cc_o_c = CC [M] $@ ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink) +# Symlinks of foo.o that point to foo.foo.o are members of a .rlib archives, +# so their exports should be added to the module +ko_objs = $(if $(filter %$(modname).$(modname).o,$(realpath $*.o)),\ + $(dir $*)exports_$(modname)_generated.o) $(filter %.o, $^) + quiet_cmd_ld_ko_o = LD [M] $@ cmd_ld_ko_o += \ $(LD) -r $(KBUILD_LDFLAGS) \ $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ - -T scripts/module.lds -o $@ $(filter %.o, $^); \ + -T scripts/module.lds -o $@ $(ko_objs); \ $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) quiet_cmd_btf_ko = BTF [M] $@
Signature · 1 line
2.42.1
RFC PATCH v3 2/2 samples: rust: Add USB sample bindings
In reply to Martin Rodriguez ReboredoThis is a demonstration of the capabilities of doing bindings with subsystems that may or may not be statically linked.
---
v2 -> v3:
- Generate bindings for USB.
v1 -> v2:
- Added this patch.
drivers/usb/core/Kconfig | 7 +++++++ drivers/usb/core/Makefile | 3 +++ drivers/usb/core/usb.rs | 13 +++++++++++++ rust/bindings/bindings_helper.h | 1 + samples/rust/Kconfig | 10 ++++++++++ samples/rust/Makefile | 3 +++ samples/rust/rust_usb_simple.rs | 22 ++++++++++++++++++++++ 7 files changed, 59 insertions(+) create mode 100644 drivers/usb/core/usb.rs create mode 100644 samples/rust/rust_usb_simple.rs
drivers/usb/core/Kconfig +7 -0
@@ -116,3 +116,10 @@ config USB_AUTOSUSPEND_DELAY The default value Linux has always had is 2 seconds. Change this value if you want a different delay and cannot modify the command line or module parameter. + +config USB_RUST + bool "Rust USB bindings" + depends on USB && RUST + default n + help + Enables Rust bindings for USB.
drivers/usb/core/Makefile +3 -0
@@ -11,6 +11,7 @@ usbcore-y += phy.o port.o usbcore-$(CONFIG_OF) += of.o usbcore-$(CONFIG_USB_PCI) += hcd-pci.o usbcore-$(CONFIG_ACPI) += usb-acpi.o +usbcore-$(CONFIG_USB_RUST) += libusb.rlib ifdef CONFIG_USB_ONBOARD_HUB usbcore-y += ../misc/onboard_usb_hub_pdevs.o
@@ -18,4 +19,6 @@ endif obj-$(CONFIG_USB) += usbcore.o +rust-libs := ./usb + obj-$(CONFIG_USB_LEDS_TRIGGER_USBPORT) += ledtrig-usbport.o
drivers/usb/core/usb.rs new file +13 -0
@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! USB devices and drivers. +//! +//! C header: [`include/linux/usb.h`](../../../../include/linux/usb.h) + +use kernel::bindings; + +/// Check if USB is disabled. +pub fn disabled () ->bool { +// SAFETY: FFI call. +unsafe { bindings::usb_disabled () !=0 } +}
rust/bindings/bindings_helper.h +1 -0
@@ -10,6 +10,7 @@#include <linux/errname.h> #include <linux/slab.h> #include <linux/refcount.h> +#include <linux/usb.h> #include <linux/wait.h> #include <linux/sched.h> #include <linux/workqueue.h>
samples/rust/Kconfig +10 -0
@@ -30,6 +30,16 @@ config SAMPLE_RUST_PRINT If unsure, say N. +config SAMPLE_RUST_USB_SIMPLE + tristate "USB simple device driver" + help + This option builds the Rust USB simple driver sample. + + To compile this as a module, choose M here: + the module will be called rust_usb_simple. + + If unsure, say N. + config SAMPLE_RUST_HOSTPROGS bool "Host programs" help
samples/rust/Makefile +3 -0
@@ -2,5 +2,8 @@ obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o +obj-$(CONFIG_SAMPLE_RUST_USB_SIMPLE) += rust_usb_simple.o + +rust-libs := ../../drivers/usb/core/usb subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
samples/rust/rust_usb_simple.rs new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust USB sample. + +use kernel::prelude::*; + +module! { +type :UsbSimple , + name:"rust_usb_simple" , + author:"Martin Rodriguez Reboredo" , + description:"Rust USB sample" , + license:"GPL v2" , +} + +struct UsbSimple ; + +impl kernel::Module for UsbSimple { +fn init (_module: &'staticThisModule ) ->Result <Self > { +pr_info! ("usb enabled: {}" , !usb::disabled ()); +Ok (UsbSimple ) + } +}
Signature · 1 line
2.42.1
Greg Kroah-Hartman
maintainerRe: [RFC PATCH v3 2/2] samples: rust: Add USB sample bindings
Quoted 9 lines · Martin Rodriguez Reboredo
This is a demonstration of the capabilities of doing bindings with subsystems that may or may not be statically linked.
Signed-off-by Martin Rodriguez Reboredo <[email protected]>---
v2 -> v3:
- Generate bindings for USB.
v1 -> v2:
- Added this patch.
I know you are just using this for an example, but here's some USB-specific things that you might want to clean up for when you submit this as a "real" binding sometime in the future:
drivers/usb/core/Kconfig config USB_AUTOSUSPEND_DELAY · line 121
Quoted 2 lines
drivers/usb/core/Kconfig +2
-0+config USB_RUST + bool "Rust USB bindings"
This is a "USB Host" binding. We have both USB host mode (when you plug a USB device into a system running Linux), and USB gadget mode (when Linux is running on the USB device you plug into any other USB system).
So please be specific here, this should be "USB_HOST_RUST" and then later, "USB_GADGET_RUST".
drivers/usb/core/Kconfig config USB_AUTOSUSPEND_DELAY · line 123
Quoted 2 lines
drivers/usb/core/Kconfig +2
-0+ depends on USB && RUST + default n
Again, "default n" is the default, never list it again.
drivers/usb/core/Kconfig config USB_AUTOSUSPEND_DELAY · line 125
Quoted 2 lines
drivers/usb/core/Kconfig +2
-0+ help + Enables Rust bindings for USB.
USB Host, not all of USB.
Quoted 13 lines
drivers/usb/core/Makefile +1
-0@@ -11,6 +11,7 @@ usbcore-y += phy.o port.o usbcore-$(CONFIG_OF) += of.o usbcore-$(CONFIG_USB_PCI) += hcd-pci.o usbcore-$(CONFIG_ACPI) += usb-acpi.o +usbcore-$(CONFIG_USB_RUST) += libusb.rlib ifdef CONFIG_USB_ONBOARD_HUB usbcore-y += ../misc/onboard_usb_hub_pdevs.o
drivers/usb/core/Makefile endif · line 22
Quoted 4 lines
drivers/usb/core/Makefile +1
-0@@ -18,4 +19,6 @@ endif obj-$(CONFIG_USB) += usbcore.o +rust-libs := ./usb
Why the "./", why not just ":= usb" ?
drivers/usb/core/Makefile endif
Quoted 4 lines
drivers/usb/core/Makefile +4
-0+config SAMPLE_RUST_USB_SIMPLE + tristate "USB simple device driver" + help + This option builds the Rust USB simple driver sample.
Rust USB simple host driver sample.
Quoted 7 lines
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
samples/rust/rust_usb_simple.rs new file +0
-0
samples/rust/rust_usb_simple.rs line 8
Quoted 8 lines
samples/rust/rust_usb_simple.rs new file +8
-0@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust USB sample. + +use kernel::prelude::*; + +module! { +type :UsbSimple ,
"USBSimple" please. I thought I said that before.
thanks,
greg k-h
Quoted 25 lines · Martin Rodriguez Reboredo
This RFC provides makes possible to have bindings for kernel subsystems that are compiled as modules.
Previously, if you wanted to have Rust bindings for a subsystem, like AMBA for example, you had to put it under
rust/kernel/so it came part of thekernelcrate, but this came with many downsides. Namely if you compiled said subsystem as a module you've a dependency on it fromkernel, which is linked directly onvmlinux.So instead of overpopulating
kernelwith a gazillion modules that throws you into dire straits you should rather have the bindings in the same directory as the subsystem you want to bind with and link it to it.With this patch Rust sources can be compiled into libraries for them to be consumed. These libraries are ar archives that follow the
.rlibstructure, namely a libfoo.rlib thin archive with a foo.foo.o object and a libfoo.rmeta rustc metadata as members. Such Rust crates get their symbols exposed and thebindingscrate is made available for them.Also included there's a sample usage of this in another patch, but it is not meant to be merged as it remains as an example.
Quoted 6 lines
If you want to use a crate with your Rust module just add a
rust-libsvariable in yourMakefilewith a value of the relative directory of said crate plus its name, e.g.# Link with the foo crate
rust-libs += ../path/to/foo
I will not provide a line-by-line review.
Just one thing I'd like to point out.
You assume the library (drivers/usb/core/*)
is built before its consumers (samples/rust/*).
If Kbuild ends up with building lib consumers first, it will be a build error.
Kbuild descends into multiple directories in parallel building.
You cannot predict which directory is built first.
Quoted 23 lines
Martin Rodriguez Reboredo (2):
kbuild: Build Rust crates as libraries
samples: rust: Add USB sample bindings.gitignore | 2 ++ Makefile | 4 ++-- drivers/usb/core/Kconfig | 7 ++++++ drivers/usb/core/Makefile | 3 +++ drivers/usb/core/usb.rs | 13 ++++++++++ rust/bindings/bindings_helper.h | 1 + samples/rust/Kconfig | 10 ++++++++ samples/rust/Makefile | 3 +++ samples/rust/rust_usb_simple.rs | 22 +++++++++++++++++ scripts/Makefile.build | 42 ++++++++++++++++++++++++++++++--- scripts/Makefile.lib | 20 ++++++++++++---- scripts/Makefile.modfinal | 9 +++++-- 12 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 drivers/usb/core/usb.rs create mode 100644 samples/rust/rust_usb_simple.rs--
2.42.1
Signature · 2 lines
Best Regards Masahiro Yamada
Quoted 15 lines · Masahiro Yamada
[...]
I will not provide a line-by-line review.
Just one thing I'd like to point out.
You assume the library (
drivers/usb/core/*)
is built before its consumers (samples/rust/*).If
Kbuildends up with building lib consumers first, it will be a build error.
Quoted 5 lines
Kbuilddescends into multiple directories in parallel building.You cannot predict which directory is built first.
[...]
Thinking with what you've said the same thing might apply to any Rust written code that depends on crates under the rust dir. Adding Make dependencies to object code made from .rs will be required if so, assuming that Kbuild is intelligent enough to build the crates first.
Quoted 19 lines · Martin Rodriguez Reboredo
Quoted 17 lines · Masahiro Yamada
[...]
I will not provide a line-by-line review.
Just one thing I'd like to point out.
You assume the library (
drivers/usb/core/*)
is built before its consumers (samples/rust/*).If
Kbuildends up with building lib consumers first, it will be a build error.
Kbuilddescends into multiple directories in parallel building.
Quoted 8 lines
Quoted 3 lines
You cannot predict which directory is built first.
[...]
Thinking with what you've said the same thing might apply to any Rust written code that depends on crates under the
rustdir. Adding Make dependencies to object code made from.rswill be required if so, assuming thatKbuildis intelligent enough to build the crates first.
rust/Makefile specifies dependencies between crates.
I do not know how to specify crate dependencies across multiple Makefiles, like you did in drivers/usb/core/Makefile and samples/rust/Makefile.
Signature · 2 lines
Best Regards Masahiro Yamada
Hi,
So I realize this is an old patch, but I didn't find another version, and discussions seemed ongoing at least a month ago. I also came across it pretty randomly.
Anyway, question:
Quoted 6 lines
If you want to use a crate with your Rust module just add a
rust-libsvariable in yourMakefilewith a value of the relative directory of said crate plus its name, e.g.# Link with the foo crate
rust-libs += ../path/to/foo
Should this perhaps be relative to the kernel's root dir instead?
If I'm reading this correctly, then a hypothetical rust wireless driver that lives in
drivers/net/wireless/<vendor>/<name>/
using some wireless rust infrastructure would probably end up with something like
rust-libs += ../../../../../net/mac80211/rust/
or whatever, which seems rather odd vs.
rust-libs += net/mac80211/rust/
Seems to me that chances are that subsystems/drivers that have rust infrastructure will not necessarily have them close to each other, like in this example?
You have this in the sample too:
+rust-libs := ../../drivers/usb/core/usb
but it's less pronounced since it's just samples/rust/ :)
johannes