rust: crates in other kernel directories
v2 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 +++++++++ samples/rust/Kconfig | 10 +++++++ samples/rust/Makefile | 3 ++ samples/rust/rust_usb_simple.rs | 22 +++++++++++++++ scripts/Makefile.build | 49 ++++++++++++++++++++++++++++++--- scripts/Makefile.lib | 18 ++++++++++-- scripts/Makefile.modfinal | 7 ++++- 11 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 drivers/usb/core/usb.rs create mode 100644 samples/rust/rust_usb_simple.rs
Signature · 1 line
2.42.0
RFC PATCH v2 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.
---
v1 -> v2:
- Fixed kernel compilation.
- Added support for building crates as modules.
.gitignore | 2 ++ Makefile | 4 ++-- scripts/Makefile.build | 49 +++++++++++++++++++++++++++++++++++---- scripts/Makefile.lib | 18 +++++++++++--- scripts/Makefile.modfinal | 7 +++++- 5 files changed, 70 insertions(+), 10 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 +45 -4
87 lines not on this page · raw message
scripts/Makefile.lib +15 -3
@@ -45,12 +45,17 @@ else obj-y := $(filter-out %/, $(obj-y)) endif +rlib-y := $(foreach m, $(filter lib%.rlib, $(obj-y)), $(m:lib%.rlib=%.o)) +rlib-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,7 +64,7 @@ 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-y := $(call rlib-exports, $(call real-search, $(obj-y), .o, -objs -y)) real-obj-m := $(call real-search, $(obj-m), .o, -objs -y -m) always-y += $(always-m)
@@ -95,6 +100,8 @@ extra-y := $(addprefix $(obj)/,$(extra-y)) always-y := $(addprefix $(obj)/,$(always-y)) targets := $(addprefix $(obj)/,$(targets)) obj-m := $(addprefix $(obj)/,$(obj-m)) +rlib-y := $(addprefix $(obj)/,$(rlib-y)) +rlib-m := $(addprefix $(obj)/,$(rlib-m)) lib-y := $(addprefix $(obj)/,$(lib-y)) real-obj-y := $(addprefix $(obj)/,$(real-obj-y)) real-obj-m := $(addprefix $(obj)/,$(real-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 +6 -1
@@ -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 = $(foreach o, $(filter %.o, $^),$(if \ + $(filter-out %$o,$(realpath $o)),$(dir $o)exports_$(notdir $*)_generated.o) $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.0
kernel test robot
build botRe: [RFC PATCH v2 1/2] kbuild: Build Rust crates as libraries
kernel test robot noticed the following build errors:
Hi Martin,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on masahiroy-kbuild/fixes]
[also build test ERROR on usb/usb-testing usb/usb-next usb/usb-linus rust/rust-next linus/master v6.6-rc7 next-20231027] [cannot apply to masahiroy-kbuild/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Martin-Rodriguez-Reboredo/kbuild-Build-Rust-crates-as-libraries/20231027-083627 base: https://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild.git fixes patch link: https://lore.kernel.org/r/20231027003504.146703-2-yakoyoku%40gmail.com patch subject: [RFC PATCH v2 1/2] kbuild: Build Rust crates as libraries config: um-randconfig-001-20231029 (https://download.01.org/0day-ci/archive/20231029/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231029/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> ld: cannot find drivers/nvdimm/../../tools/testing/nvdimm/test/exports_iomap_generated.o: No such file or directory >> ld: cannot find drivers/nvdimm/../../tools/testing/nvdimm/test/exports_iomap_generated.o: No such file or directory
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH v2 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.
--- drivers/usb/core/Kconfig | 7 +++++++ drivers/usb/core/Makefile | 3 +++ drivers/usb/core/usb.rs | 13 +++++++++++++ samples/rust/Kconfig | 10 ++++++++++ samples/rust/Makefile | 3 +++ samples/rust/rust_usb_simple.rs | 22 ++++++++++++++++++++++ 6 files changed, 58 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 } +}
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.0
Re: [RFC PATCH v2 2/2] samples: rust: Add USB sample bindings
This was missing in the patch, but you get the point...
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>
Greg Kroah-Hartman
maintainerRe: [RFC PATCH v2 2/2] samples: rust: Add USB sample bindings
In reply to Martin Rodriguez ReboredoQuoted 20 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]>--- drivers/usb/core/Kconfig | 7 +++++++ drivers/usb/core/Makefile | 3 +++ drivers/usb/core/usb.rs | 13 +++++++++++++ samples/rust/Kconfig | 10 ++++++++++ samples/rust/Makefile | 3 +++ samples/rust/rust_usb_simple.rs | 22 ++++++++++++++++++++++ 6 files changed, 58 insertions(+) create mode 100644 drivers/usb/core/usb.rs create mode 100644 samples/rust/rust_usb_simple.rsdrivers/usb/core/Kconfig +0
-0
drivers/usb/core/Kconfig config USB_AUTOSUSPEND_DELAY · line 123
Quoted 8 lines
drivers/usb/core/Kconfig +5
-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
Nit, "n" is the default, this line is not needed.
Also, if you want to get really picky, _which_ USB is this for, the "host" apis (you plug a USB device into a Linux maching), or the "gadget" apis (i.e. Linux is running in the device that you plug into a USB host)? Linux supports both :)
Quoted 85 lines
drivers/usb/core/Kconfig +2
-0+ 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.odrivers/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 } +}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" helpsamples/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) += hostprogssamples/rust/rust_usb_simple.rs new file +5
-0@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust USB sample. + +use kernel::prelude::*;
samples/rust/rust_usb_simple.rs line 15
Quoted 10 lines
samples/rust/rust_usb_simple.rs new file +10
-0+ +module! { +type :UsbSimple , + name:"rust_usb_simple" , + author:"Martin Rodriguez Reboredo" , + description:"Rust USB sample" , + license:"GPL v2" , +} + +struct UsbSimple ;
"USBSimple" please.
samples/rust/rust_usb_simple.rs line 22
Quoted 7 lines
samples/rust/rust_usb_simple.rs new file +7
-0+ +impl kernel::Module for UsbSimple { +fn init (_module: &'staticThisModule ) ->Result <Self > { +pr_info! ("usb enabled: {}" , !usb::disabled ()); +Ok (UsbSimple ) + } +}
I know this is just a fake patch to test the bindings logic, so sorry for the noise, just wanted to get terminology right :)
thanks,
greg k-h
Re: [RFC PATCH v2 2/2] samples: rust: Add USB sample bindings
Quoted 10 lines · Greg Kroah-Hartman
drivers/usb/core/Kconfig config USB_AUTOSUSPEND_DELAY · line 123
Quoted 6 lines · Martin Rodriguez Reboredo
[...]
drivers/usb/core/Kconfig +5
-0+ +config USB_RUST + bool "Rust USB bindings" + depends on USB && RUST + default nNit, "n" is the default, this line is not needed.
Quoted 4 lines
Also, if you want to get really picky, _which_ USB is this for, the "host" apis (you plug a USB device into a Linux maching), or the "gadget" apis (i.e. Linux is running in the device that you plug into a USB host)? Linux supports both :)
Nice one! I'll probably target host APIs as of now due to what I have in my hands.
Quoted 30 lines
Quoted 6 lines
samples/rust/rust_usb_simple.rs new file +5
-0+ help + Enables Rust bindings for USB. +//! Rust USB sample. + +use kernel::prelude::*;samples/rust/rust_usb_simple.rs line 15
Quoted 10 lines
samples/rust/rust_usb_simple.rs new file +10
-0+ +module! { +type :UsbSimple , + name:"rust_usb_simple" , + author:"Martin Rodriguez Reboredo" , + description:"Rust USB sample" , + license:"GPL v2" , +} + +struct UsbSimple ;"USBSimple" please.
samples/rust/rust_usb_simple.rs line 22
Quoted 7 lines
samples/rust/rust_usb_simple.rs new file +7
-0+ +impl kernel::Module for UsbSimple { +fn init (_module: &'staticThisModule ) ->Result <Self > { +pr_info! ("usb enabled: {}" , !usb::disabled ()); +Ok (UsbSimple ) + } +}I know this is just a fake patch to test the bindings logic, so sorry for the noise, just wanted to get terminology right :)
Quoted 3 lines
thanks,
greg k-h
No, you are right. Because Rust uses upper camel case for struct names there's the tendency of lowering initialisms, so you can either leave it as USBSimple or as UsbSimple. When I publish my USB host bindings I'll take your comment into account for sure.
Greg Kroah-Hartman
maintainerRe: [RFC PATCH v2 2/2] samples: rust: Add USB sample bindings
In reply to Martin Rodriguez ReboredoQuoted 4 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]>
Quoted 11 lines
--- drivers/usb/core/Kconfig | 7 +++++++ drivers/usb/core/Makefile | 3 +++ drivers/usb/core/usb.rs | 13 +++++++++++++ samples/rust/Kconfig | 10 ++++++++++ samples/rust/Makefile | 3 +++ samples/rust/rust_usb_simple.rs | 22 ++++++++++++++++++++++ 6 files changed, 58 insertions(+) create mode 100644 drivers/usb/core/usb.rs create mode 100644 samples/rust/rust_usb_simple.rs
Hi,
This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.
You are receiving this message because of the following common error(s) as indicated below:
- This looks like a new version of a previously submitted patch, but you did not list below the --- line any changes from the previous version. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/process/submitting-patches.rst for what needs to be done here to properly describe this.
If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.
thanks,
greg k-h's patch email bot