Skip to content

Normalize semaphore samples and add them to the CI #176

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

Merged
merged 1 commit into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ jobs:
grep '] rust_stack_probing: Rust stack probing sample (init)$' qemu-stdout.log
grep '] rust_stack_probing: Rust stack probing sample (exit)$' qemu-stdout.log

- run: |
grep '] rust_semaphore: Rust semaphore sample (init)$' qemu-stdout.log
grep '] rust_semaphore: Rust semaphore sample (exit)$' qemu-stdout.log

- run: |
grep '] rust_semaphore_c: Rust semaphore sample (in C, for comparison) (init)$' qemu-stdout.log
grep '] rust_semaphore_c: Rust semaphore sample (in C, for comparison) (exit)$' qemu-stdout.log

# Report
- run: |
ls -l \
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/kernel-arm64-debug.config
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,8 @@ CONFIG_SAMPLE_RUST_SYNC=m
CONFIG_SAMPLE_RUST_CHRDEV=m
CONFIG_SAMPLE_RUST_MISCDEV=m
CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m

#
# arm64 Debugging
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/kernel-arm64-release.config
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,8 @@ CONFIG_SAMPLE_RUST_SYNC=m
CONFIG_SAMPLE_RUST_CHRDEV=m
CONFIG_SAMPLE_RUST_MISCDEV=m
CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m

#
# arm64 Debugging
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/kernel-ppc64le-debug.config
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,8 @@ CONFIG_SAMPLE_RUST_SYNC=m
CONFIG_SAMPLE_RUST_CHRDEV=m
CONFIG_SAMPLE_RUST_MISCDEV=m
CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/kernel-ppc64le-release.config
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,8 @@ CONFIG_SAMPLE_RUST_SYNC=m
CONFIG_SAMPLE_RUST_CHRDEV=m
CONFIG_SAMPLE_RUST_MISCDEV=m
CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/kernel-x86_64-debug.config
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,8 @@ CONFIG_SAMPLE_RUST_SYNC=m
CONFIG_SAMPLE_RUST_CHRDEV=m
CONFIG_SAMPLE_RUST_MISCDEV=m
CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/kernel-x86_64-release.config
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,8 @@ CONFIG_SAMPLE_RUST_SYNC=m
CONFIG_SAMPLE_RUST_CHRDEV=m
CONFIG_SAMPLE_RUST_MISCDEV=m
CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/qemu-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ busybox rmmod rust_miscdev.ko
busybox insmod rust_stack_probing.ko
busybox rmmod rust_stack_probing.ko

busybox insmod rust_semaphore.ko
busybox rmmod rust_semaphore.ko

busybox insmod rust_semaphore_c.ko
busybox rmmod rust_semaphore_c.ko

busybox insmod rust_module_parameters_loadable_default.ko
busybox insmod rust_module_parameters_loadable_custom.ko \
my_bool=n \
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/qemu-initramfs.desc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ file /rust_sync.ko samples/rust/rust_sync.ko 0755
file /rust_chrdev.ko samples/rust/rust_chrdev.ko 0755 0 0
file /rust_miscdev.ko samples/rust/rust_miscdev.ko 0755 0 0
file /rust_stack_probing.ko samples/rust/rust_stack_probing.ko 0755 0 0
file /rust_semaphore.ko samples/rust/rust_semaphore.ko 0755 0 0
file /rust_semaphore_c.ko samples/rust/rust_semaphore_c.ko 0755 0 0

file /rust_module_parameters_loadable_default.ko samples/rust/rust_module_parameters_loadable_default.ko 0755 0 0
file /rust_module_parameters_loadable_custom.ko samples/rust/rust_module_parameters_loadable_custom.ko 0755 0 0
20 changes: 12 additions & 8 deletions samples/rust/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,24 @@ config SAMPLE_RUST_STACK_PROBING

If unsure, say N.

config SAMPLE_SEMAPHORE
tristate "Semaphore in C"
config SAMPLE_RUST_SEMAPHORE
tristate "Semaphore"
help
This option builds the C semaphore sample.
This option builds the Rust semaphore sample.

To compile this as a module, choose M here:
the module will be called semaphore.
the module will be called rust_semaphore.

config SAMPLE_RUST_SEMAPHORE
tristate "Semaphore in Rust"
If unsure, say N.

config SAMPLE_RUST_SEMAPHORE_C
tristate "Semaphore (in C, for comparison)"
help
This option builds the Rust semaphore sample.
This option builds the Rust semaphore sample (in C, for comparison).

To compile this as a module, choose M here:
the module will be called rust_semaphore.
the module will be called rust_semaphore_c.

If unsure, say N.

endif # SAMPLES_RUST
2 changes: 1 addition & 1 deletion samples/rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ obj-$(CONFIG_SAMPLE_RUST_SYNC) += rust_sync.o
obj-$(CONFIG_SAMPLE_RUST_CHRDEV) += rust_chrdev.o
obj-$(CONFIG_SAMPLE_RUST_MISCDEV) += rust_miscdev.o
obj-$(CONFIG_SAMPLE_RUST_STACK_PROBING) += rust_stack_probing.o
obj-$(CONFIG_SAMPLE_SEMAPHORE) += semaphore.o
obj-$(CONFIG_SAMPLE_RUST_SEMAPHORE) += rust_semaphore.o
obj-$(CONFIG_SAMPLE_RUST_SEMAPHORE_C) += rust_semaphore_c.o
22 changes: 18 additions & 4 deletions samples/rust/rust_semaphore.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: GPL-2.0

//! Rust semaphore sample
//!
//! A counting semaphore that can be used by userspace.
//!
//! The count is incremented by writes to the device. A write of `n` bytes results in an increment
Expand Down Expand Up @@ -31,10 +33,10 @@ use kernel::{
};

module! {
type: RustSemaphoreModule,
type: RustSemaphore,
name: b"rust_semaphore",
author: b"Rust for Linux Contributors",
description: b"An example kernel module written in Rust",
description: b"Rust semaphore sample",
license: b"GPL v2",
params: {},
}
Expand Down Expand Up @@ -111,15 +113,18 @@ impl FileOperations for FileState {
fn release(_obj: Box<Self>, _file: &File) {}
}

struct RustSemaphoreModule {
struct RustSemaphore {
_dev: Pin<Box<Registration<Arc<Semaphore>>>>,
}

impl KernelModule for RustSemaphoreModule {
impl KernelModule for RustSemaphore {
fn init() -> KernelResult<Self> {
info!("Rust semaphore sample (init)");

let sema = Arc::try_new(Semaphore {
// SAFETY: `condvar_init!` is called below.
changed: unsafe { CondVar::new() },

// SAFETY: `mutex_init!` is called below.
inner: unsafe {
Mutex::new(SemaphoreInner {
Expand All @@ -128,16 +133,25 @@ impl KernelModule for RustSemaphoreModule {
})
},
})?;

// SAFETY: `changed` is pinned behind `Arc`.
condvar_init!(Pin::new_unchecked(&sema.changed), "Semaphore::changed");

// SAFETY: `inner` is pinned behind `Arc`.
mutex_init!(Pin::new_unchecked(&sema.inner), "Semaphore::inner");

Ok(Self {
_dev: Registration::new_pinned::<FileState>(cstr!("rust_semaphore"), None, sema)?,
})
}
}

impl Drop for RustSemaphore {
fn drop(&mut self) {
info!("Rust semaphore sample (exit)");
}
}

const IOCTL_GET_READ_COUNT: u32 = 0x80086301;
const IOCTL_SET_READ_COUNT: u32 = 0x40086301;

Expand Down
16 changes: 14 additions & 2 deletions samples/rust/semaphore.c → samples/rust/rust_semaphore_c.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Rust semaphore sample (in C, for comparison)
*
* This is a C implementation of `rust_semaphore.rs`. Refer to the description
* in that file for details on the device.
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

// This is a C implementation of `rust_semaphore.rs`. Refer to the description
// in that file for details on the device.
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/fs.h>
Expand Down Expand Up @@ -165,6 +171,8 @@ static int __init semaphore_init(void)
int ret;
struct semaphore_state *state;

pr_info("Rust semaphore sample (in C, for comparison) (init)\n");

state = kzalloc(sizeof(*state), GFP_KERNEL);
if (!state)
return -ENOMEM;
Expand All @@ -190,6 +198,8 @@ static int __init semaphore_init(void)

static void __exit semaphore_exit(void)
{
pr_info("Rust semaphore sample (in C, for comparison) (exit)\n");

misc_deregister(&device->miscdev);
kref_put(&device->ref, semaphore_free);
}
Expand All @@ -198,3 +208,5 @@ module_init(semaphore_init);
module_exit(semaphore_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Rust for Linux Contributors");
MODULE_DESCRIPTION("Rust semaphore sample (in C, for comparison)");