forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 435
/
rust_miscdev.rs
144 lines (117 loc) · 3.94 KB
/
rust_miscdev.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: GPL-2.0
//! Rust miscellaneous device sample
#![no_std]
#![feature(allocator_api, global_asm)]
use alloc::{boxed::Box, sync::Arc};
use core::pin::Pin;
use kernel::prelude::*;
use kernel::{
c_str,
file::File,
file_operations::{FileOpener, FileOperations},
io_buffer::{IoBufferReader, IoBufferWriter},
miscdev,
sync::{CondVar, Mutex},
Error,
};
module! {
type: RustMiscdev,
name: b"rust_miscdev",
author: b"Rust for Linux Contributors",
description: b"Rust miscellaneous device sample",
license: b"GPL v2",
}
const MAX_TOKENS: usize = 3;
struct SharedStateInner {
token_count: usize,
}
struct SharedState {
state_changed: CondVar,
inner: Mutex<SharedStateInner>,
}
impl SharedState {
fn try_new() -> Result<Pin<Arc<Self>>> {
let state = Arc::try_pin(Self {
// SAFETY: `condvar_init!` is called below.
state_changed: unsafe { CondVar::new() },
// SAFETY: `mutex_init!` is called below.
inner: unsafe { Mutex::new(SharedStateInner { token_count: 0 }) },
})?;
// SAFETY: `state_changed` is pinned behind `Pin<Arc>`.
let state_changed = unsafe { Pin::new_unchecked(&state.state_changed) };
kernel::condvar_init!(state_changed, "SharedState::state_changed");
// SAFETY: `inner` is pinned behind `Pin<Arc>`.
let inner = unsafe { Pin::new_unchecked(&state.inner) };
kernel::mutex_init!(inner, "SharedState::inner");
Ok(state)
}
}
struct Token {
shared: Pin<Arc<SharedState>>,
}
impl FileOpener<Pin<Arc<SharedState>>> for Token {
fn open(shared: &Pin<Arc<SharedState>>) -> Result<Self::Wrapper> {
Ok(Box::try_new(Self {
shared: shared.clone(),
})?)
}
}
impl FileOperations for Token {
type Wrapper = Box<Self>;
kernel::declare_file_operations!(read, write);
fn read<T: IoBufferWriter>(&self, _: &File, data: &mut T, offset: u64) -> Result<usize> {
// Succeed if the caller doesn't provide a buffer or if not at the start.
if data.is_empty() || offset != 0 {
return Ok(0);
}
{
let mut inner = self.shared.inner.lock();
// Wait until we are allowed to decrement the token count or a signal arrives.
while inner.token_count == 0 {
if self.shared.state_changed.wait(&mut inner) {
return Err(Error::EINTR);
}
}
// Consume a token.
inner.token_count -= 1;
}
// Notify a possible writer waiting.
self.shared.state_changed.notify_all();
// Write a one-byte 1 to the reader.
data.write_slice(&[1u8; 1])?;
Ok(1)
}
fn write<T: IoBufferReader>(&self, _: &File, data: &mut T, _offset: u64) -> Result<usize> {
{
let mut inner = self.shared.inner.lock();
// Wait until we are allowed to increment the token count or a signal arrives.
while inner.token_count == MAX_TOKENS {
if self.shared.state_changed.wait(&mut inner) {
return Err(Error::EINTR);
}
}
// Increment the number of token so that a reader can be released.
inner.token_count += 1;
}
// Notify a possible reader waiting.
self.shared.state_changed.notify_all();
Ok(data.len())
}
}
struct RustMiscdev {
_dev: Pin<Box<miscdev::Registration<Pin<Arc<SharedState>>>>>,
}
impl KernelModule for RustMiscdev {
fn init() -> Result<Self> {
pr_info!("Rust miscellaneous device sample (init)\n");
let state = SharedState::try_new()?;
Ok(RustMiscdev {
_dev: miscdev::Registration::new_pinned::<Token>(c_str!("rust_miscdev"), None, state)?,
})
}
}
impl Drop for RustMiscdev {
fn drop(&mut self) {
pr_info!("Rust miscellaneous device sample (exit)\n");
}
}