Subject: [RFC PATCH v3 0/2] 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

--
2.42.1


Subject: [RFC PATCH v3 2/2] samples: rust: Add USB sample bindings

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.

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

diff --git a/drivers/usb/core/Kconfig b/drivers/usb/core/Kconfig
index 351ede4b5de2..4b5604282129 100644
--- a/drivers/usb/core/Kconfig
+++ b/drivers/usb/core/Kconfig
@@ -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.
diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
index 7d338e9c0657..00e116913591 100644
--- a/drivers/usb/core/Makefile
+++ b/drivers/usb/core/Makefile
@@ -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
diff --git a/drivers/usb/core/usb.rs b/drivers/usb/core/usb.rs
new file mode 100644
index 000000000000..3f7ad02153f5
--- /dev/null
+++ b/drivers/usb/core/usb.rs
@@ -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 }
+}
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index c41eaab4ddb2..845cdd856981 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -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>
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index b0f74a81c8f9..12116f6fb526 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -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
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index 03086dabbea4..f1ab58a9ecdd 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -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
diff --git a/samples/rust/rust_usb_simple.rs b/samples/rust/rust_usb_simple.rs
new file mode 100644
index 000000000000..3523f81d5eb8
--- /dev/null
+++ b/samples/rust/rust_usb_simple.rs
@@ -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: &'static ThisModule) -> Result<Self> {
+ pr_info!("usb enabled: {}", !usb::disabled());
+ Ok(UsbSimple)
+ }
+}
--
2.42.1

2023-11-05 07:27:02

by Greg KH

[permalink] [raw]
Subject: Re: [RFC PATCH v3 2/2] samples: rust: Add USB sample bindings

On Sat, Nov 04, 2023 at 06:11:59PM -0300, Martin Rodriguez Reboredo wrote:
> 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:

> +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".

> + depends on USB && RUST
> + default n

Again, "default n" is the default, never list it again.


> + help
> + Enables Rust bindings for USB.

USB Host, not all of USB.

> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> index 7d338e9c0657..00e116913591 100644
> --- a/drivers/usb/core/Makefile
> +++ b/drivers/usb/core/Makefile
> @@ -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

Why the "./", why not just ":= usb" ?

> +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.

> subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
> diff --git a/samples/rust/rust_usb_simple.rs b/samples/rust/rust_usb_simple.rs
> new file mode 100644
> index 000000000000..3523f81d5eb8
> --- /dev/null
> +++ b/samples/rust/rust_usb_simple.rs
> @@ -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

Subject: Re: [RFC PATCH v3 0/2] rust: crates in other kernel directories

On 11/13/23 11:13, Masahiro Yamada wrote:
> [...]
>
> 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.
>
> [...]

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.

2024-02-19 09:07:17

by Masahiro Yamada

[permalink] [raw]
Subject: Re: [RFC PATCH v3 0/2] rust: crates in other kernel directories

On Mon, Feb 19, 2024 at 1:11 AM Martin Rodriguez Reboredo
<[email protected]> wrote:
>
> On 11/13/23 11:13, Masahiro Yamada wrote:
> > [...]
> >
> > 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.
> >
> > [...]
>
> 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.


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.



--
Best Regards
Masahiro Yamada

2024-03-15 14:12:26

by Johannes Berg

[permalink] [raw]
Subject: Re: [RFC PATCH v3 0/2] rust: crates in other kernel directories

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:

> 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

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