Skip to content

WIP: Seq file #232

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

Closed
wants to merge 7 commits into from
Closed
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
10 changes: 5 additions & 5 deletions .github/workflows/busybox.config
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ CONFIG_GZIP_FAST=0
# Coreutils
#
# CONFIG_BASENAME is not set
# CONFIG_CAT is not set
CONFIG_CAT=y
# CONFIG_FEATURE_CATN is not set
# CONFIG_FEATURE_CATV is not set
# CONFIG_CHGRP is not set
Expand Down Expand Up @@ -263,9 +263,9 @@ CONFIG_GZIP_FAST=0
# CONFIG_SHA512SUM is not set
# CONFIG_SHA3SUM is not set
# CONFIG_FEATURE_MD5_SHA1_SUM_CHECK is not set
# CONFIG_MKDIR is not set
CONFIG_MKDIR=y
# CONFIG_MKFIFO is not set
# CONFIG_MKNOD is not set
CONFIG_MKNOD=y
# CONFIG_MKTEMP is not set
# CONFIG_MV is not set
# CONFIG_NICE is not set
Expand All @@ -280,7 +280,7 @@ CONFIG_GZIP_FAST=0
# CONFIG_READLINK is not set
# CONFIG_FEATURE_READLINK_FOLLOW is not set
# CONFIG_REALPATH is not set
# CONFIG_RM is not set
CONFIG_RM=y
# CONFIG_RMDIR is not set
# CONFIG_SEQ is not set
# CONFIG_SHRED is not set
Expand Down Expand Up @@ -635,7 +635,7 @@ CONFIG_DEFAULT_DEPMOD_FILE="modules.dep"
# CONFIG_MKSWAP is not set
# CONFIG_FEATURE_MKSWAP_UUID is not set
# CONFIG_MORE is not set
# CONFIG_MOUNT is not set
CONFIG_MOUNT=y
# CONFIG_FEATURE_MOUNT_FAKE is not set
# CONFIG_FEATURE_MOUNT_VERBOSE is not set
# CONFIG_FEATURE_MOUNT_HELPERS is not set
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
timeout-minutes: 20

strategy:
fail-fast: false
matrix:
arch: [arm, arm64, ppc64le, riscv64, x86_64]
toolchain: [gcc, clang, llvm]
Expand Down Expand Up @@ -360,6 +361,12 @@ jobs:
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

- run: |
grep '] rust_seq_file: Rust seq_file sample (init)$' qemu-stdout.log
grep '] rust_seq_file: Rust seq_file sample (exit)$' qemu-stdout.log
grep 'rust_seq_file read log: 1' qemu-stdout.log
grep 'rust_seq_file read log: 2' qemu-stdout.log

# Report
- run: |
cat ${{ env.BUILD_DIR }}.config
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/kernel-arm-debug.config
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
# CONFIG_STRICT_DEVMEM is not set

#
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/kernel-arm-release.config
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
# CONFIG_STRICT_DEVMEM is not set

#
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/kernel-arm64-debug-thinlto.config
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m

#
# arm64 Debugging
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/kernel-arm64-debug.config
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m

#
# arm64 Debugging
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/kernel-arm64-release-thinlto.config
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,10 @@ CONFIG_FRAME_POINTER=y
# Generic Kernel Debugging Instruments
#
# CONFIG_MAGIC_SYSRQ is not set
# CONFIG_DEBUG_FS is not set
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_FS_ALLOW_ALL=y
# CONFIG_DEBUG_FS_DISALLOW_MOUNT is not set
# CONFIG_DEBUG_FS_ALLOW_NONE is not set
CONFIG_HAVE_ARCH_KGDB=y
# CONFIG_KGDB is not set
CONFIG_ARCH_HAS_UBSAN_SANITIZE_ALL=y
Expand Down Expand Up @@ -1355,6 +1358,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m

#
# arm64 Debugging
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/kernel-arm64-release.config
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m

#
# arm64 Debugging
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/kernel-ppc64le-debug.config
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/kernel-ppc64le-release.config
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/kernel-riscv64-debug.config
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,10 @@ CONFIG_FRAME_POINTER=y
# Generic Kernel Debugging Instruments
#
# CONFIG_MAGIC_SYSRQ is not set
# CONFIG_DEBUG_FS is not set
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_FS_ALLOW_ALL=y
# CONFIG_DEBUG_FS_DISALLOW_MOUNT is not set
# CONFIG_DEBUG_FS_ALLOW_NONE is not set
CONFIG_HAVE_ARCH_KGDB=y
CONFIG_HAVE_ARCH_KGDB_QXFER_PKT=y
# CONFIG_KGDB is not set
Expand Down Expand Up @@ -1286,6 +1289,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
# CONFIG_STRICT_DEVMEM is not set

#
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/kernel-riscv64-release.config
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,10 @@ CONFIG_FRAME_POINTER=y
# Generic Kernel Debugging Instruments
#
# CONFIG_MAGIC_SYSRQ is not set
# CONFIG_DEBUG_FS is not set
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_FS_ALLOW_ALL=y
# CONFIG_DEBUG_FS_DISALLOW_MOUNT is not set
# CONFIG_DEBUG_FS_ALLOW_NONE is not set
CONFIG_HAVE_ARCH_KGDB=y
CONFIG_HAVE_ARCH_KGDB_QXFER_PKT=y
# CONFIG_UBSAN is not set
Expand Down Expand Up @@ -1202,6 +1205,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
# CONFIG_STRICT_DEVMEM is not set

#
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/kernel-x86_64-debug-thinlto.config
Original file line number Diff line number Diff line change
Expand Up @@ -1222,9 +1222,9 @@ CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0x1
CONFIG_MAGIC_SYSRQ_SERIAL=y
CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE=""
CONFIG_DEBUG_FS=y
# CONFIG_DEBUG_FS_ALLOW_ALL is not set
CONFIG_DEBUG_FS_ALLOW_ALL=y
# CONFIG_DEBUG_FS_DISALLOW_MOUNT is not set
CONFIG_DEBUG_FS_ALLOW_NONE=y
# CONFIG_DEBUG_FS_ALLOW_NONE is not set
CONFIG_HAVE_ARCH_KGDB=y
CONFIG_KGDB=y
CONFIG_KGDB_HONOUR_BLOCKLIST=y
Expand Down Expand Up @@ -1441,6 +1441,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/kernel-x86_64-debug.config
Original file line number Diff line number Diff line change
Expand Up @@ -1217,9 +1217,9 @@ CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0x1
CONFIG_MAGIC_SYSRQ_SERIAL=y
CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE=""
CONFIG_DEBUG_FS=y
# CONFIG_DEBUG_FS_ALLOW_ALL is not set
CONFIG_DEBUG_FS_ALLOW_ALL=y
# CONFIG_DEBUG_FS_DISALLOW_MOUNT is not set
CONFIG_DEBUG_FS_ALLOW_NONE=y
# CONFIG_DEBUG_FS_ALLOW_NONE is not set
CONFIG_HAVE_ARCH_KGDB=y
CONFIG_KGDB=y
CONFIG_KGDB_HONOUR_BLOCKLIST=y
Expand Down Expand Up @@ -1444,6 +1444,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/kernel-x86_64-release-thinlto.config
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,10 @@ CONFIG_STACK_VALIDATION=y
# Generic Kernel Debugging Instruments
#
# CONFIG_MAGIC_SYSRQ is not set
# CONFIG_DEBUG_FS is not set
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_FS_ALLOW_ALL=y
# CONFIG_DEBUG_FS_DISALLOW_MOUNT is not set
# CONFIG_DEBUG_FS_ALLOW_NONE is not set
CONFIG_HAVE_ARCH_KGDB=y
CONFIG_ARCH_HAS_UBSAN_SANITIZE_ALL=y
# CONFIG_UBSAN is not set
Expand Down Expand Up @@ -1397,6 +1400,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/kernel-x86_64-release.config
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
CONFIG_SAMPLE_RUST_SEMAPHORE=m
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
CONFIG_SAMPLE_RUST_RANDOM=m
CONFIG_SAMPLE_RUST_SEQ_FILE=m
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
# CONFIG_STRICT_DEVMEM is not set

Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/qemu-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,17 @@ busybox insmod rust_module_parameters_loadable_custom.ko \
busybox rmmod rust_module_parameters_loadable_default.ko
busybox rmmod rust_module_parameters_loadable_custom.ko

busybox insmod rust_seq_file.ko
busybox mkdir proc
busybox mount -t proc proc /proc
busybox mkdir debugfs
busybox mount -t debugfs debugfs /debugfs
export RUST_SEQ_MINOR=$(busybox cat /proc/misc | busybox grep rust_seq_file | busybox cut -d ' ' -f 1)
busybox mknod /dev/rust_seq_file0 c 10 $RUST_SEQ_MINOR
busybox cat /dev/rust_seq_file0
busybox cat /dev/rust_seq_file0
busybox cat /debugfs/rust_seq_file
busybox rm /dev/rust_seq_file0
busybox rmmod rust_seq_file.ko

busybox reboot -f
3 changes: 2 additions & 1 deletion .github/workflows/qemu-initramfs.desc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ file /bin/busybox busybox 0755 0 0
slink /bin/sh /bin/busybox 0755 0 0
file /init .github/workflows/qemu-init.sh 0755 0 0

file /rust_minimal.ko samples/rust/rust_minimal.ko 0755 0 0
file /rust_print.ko samples/rust/rust_print.ko 0755 0 0
file /rust_module_parameters.ko samples/rust/rust_module_parameters.ko 0755 0 0
file /rust_sync.ko samples/rust/rust_sync.ko 0755 0 0
Expand All @@ -17,3 +16,5 @@ file /rust_semaphore_c.ko samples/rust/rust_semaphore_c.ko 0755

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
file /rust_minimal.ko samples/rust/rust_minimal.ko 0755 0 0
file /rust_seq_file.ko samples/rust/rust_seq_file.ko 0755 0 0
2 changes: 2 additions & 0 deletions rust/kernel/bindings_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include <linux/cdev.h>
#include <linux/clk.h>
#include <linux/debugfs.h>
#include <linux/errname.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/random.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/sysctl.h>
#include <linux/uaccess.h>
Expand Down
71 changes: 71 additions & 0 deletions rust/kernel/debugfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: GPL-2.0

//! TODOABK: finish doc

use core::{
marker::{PhantomData, Sync},
ptr,
};

use crate::{bindings, c_types, error, str::CStr, types::PointerWrapper, Result};

/// An `inode` in debugfs with a `T` stored in `i_private`.
pub(crate) struct DebugFsDirEntry<T: PointerWrapper> {
dentry: *mut bindings::dentry,
data: *mut c_types::c_void,
_wrapper: PhantomData<T>,
}

// SAFETY: There are methods available on [`DebugFsDirEntry`] so a thread can't
// actually do anything with a `&DebugFsDirEntry`. This makes it is safe to
// share across threads.
unsafe impl<T: PointerWrapper> Sync for DebugFsDirEntry<T> {}

impl<T: PointerWrapper> DebugFsDirEntry<T> {
/// Create a file in `debugfs`.
///
/// # Safety
///
/// `fops` must be valid when opening an `inode` with `data::into_pointer`
/// stored in `i_private`.
pub(crate) unsafe fn create_file(
name: &CStr,
data: T,
fops: &'static bindings::file_operations,
) -> Result<Self> {
let name = name.as_char_ptr();
let data = data.into_pointer() as *mut _;
// SAFETY: Calling a C function. `name` will be a valid null-terminated
// string because it came from a `CStr`. The caller guarantees that
// `fops` is valid for an inode with `data` in `i_private`.
let dentry_ptr = error::from_kernel_err_ptr(unsafe {
bindings::debugfs_create_file(name, 0, ptr::null_mut(), data, fops)
});
match dentry_ptr {
Err(err) => {
// SAFETY: `data` was created by `T::into_pointer` just above.
drop(unsafe { T::from_pointer(data) });
Err(err)
}
Ok(dentry) => Ok(DebugFsDirEntry {
dentry,
data,
_wrapper: PhantomData,
}),
}
}
}

impl<T: PointerWrapper> Drop for DebugFsDirEntry<T> {
fn drop(&mut self) {
// SAFETY: Calling a C function. `dentry` must have been created by a
// call to [`DebugFsDirEntry::create_file`] which always returns a valid
// `dentry`.
unsafe {
bindings::debugfs_remove(self.dentry);
}
// SAFETY: `self.data` was created by a call to `T::into_pointer` in
// [`DebugFsDirEntry::create_file`].
unsafe { T::from_pointer(self.data) };
}
}
4 changes: 4 additions & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub mod chrdev;
#[cfg(CONFIG_COMMON_CLK)]
pub mod clk;
pub mod cred;
#[cfg(CONFIG_DEBUG_FS)]
pub mod debugfs;
pub mod device;
pub mod driver;
mod error;
Expand Down Expand Up @@ -75,6 +77,8 @@ mod build_assert;
pub mod prelude;
pub mod print;
pub mod random;
#[cfg(CONFIG_DEBUG_FS)]
pub mod seq_file;
mod static_assert;
#[doc(hidden)]
pub mod std_vendor;
Expand Down
Loading