Alice Ryhl <aliceryhl@google.com> says: This series adds support for the `struct iov_iter` type. This type represents an IO buffer for reading or writing, and can be configured for either direction of communication. In Rust, we define separate types for reading and writing. This will ensure that you cannot mix them up and e.g. call copy_from_iter in a read_iter syscall. To use the new abstractions, miscdevices are given new methods read_iter and write_iter that can be used to implement the read/write syscalls on a miscdevice. The miscdevice sample is updated to provide read/write operations. Intended for Greg's miscdevice tree.
Showing the changes to samples (1 of 6 files). Show all
Unified · Split
| @@ -100,8 +100,9 @@ use core::pin::Pin; · 99 unmodified lines | ||
| 100 | 100 | use kernel::{ |
| 101 | 101 | c_str, |
| 102 | 102 | device::Device, |
| 103 | − fs::File, | |
| 103 | + fs::{File, Kiocb}, | |
| 104 | 104 | ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, |
| 105 | + iov::{IovIterDest, IovIterSource}, | |
| 105 | 106 | miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration}, |
| 106 | 107 | new_mutex, |
| 107 | 108 | prelude::*, |
| @@ -143,6 +144,7 @@ impl kernel::InPlaceModule for RustMiscDeviceModule { · 35 unmodified lines | ||
| 143 | 144 | |
| 144 | 145 | struct Inner { |
| 145 | 146 | value: i32, |
| 147 | + buffer: KVVec<u8>, | |
| 146 | 148 | } |
| 147 | 149 | |
| 148 | 150 | #[pin_data(PinnedDrop)] |
| @@ -164,7 +166,10 @@ impl MiscDevice for RustMiscDevice { · 15 unmodified lines | ||
| 164 | 166 | KBox::try_pin_init( |
| 165 | 167 | try_pin_init! { |
| 166 | 168 | RustMiscDevice { |
| 167 | − inner <- new_mutex!( Inner{ value: 0_i32 } ), | |
| 169 | + inner <- new_mutex!(Inner { | |
| 170 | + value: 0_i32, | |
| 171 | + buffer: KVVec::new(), | |
| 172 | + }), | |
| 168 | 173 | dev: dev, |
| 169 | 174 | } |
| 170 | 175 | }, |
| @@ -172,6 +177,33 @@ impl MiscDevice for RustMiscDevice { · 1 unmodified line | ||
| 172 | 177 | ) |
| 173 | 178 | } |
| 174 | 179 | |
| 180 | + fn read_iter(mut kiocb: Kiocb<'_, Self::Ptr>, iov: &mut IovIterDest<'_>) -> Result<usize> { | |
| 181 | + let me = kiocb.file(); | |
| 182 | + dev_info!(me.dev, "Reading from Rust Misc Device Sample\n"); | |
| 183 | + | |
| 184 | + let inner = me.inner.lock(); | |
| 185 | + // Read the buffer contents, taking the file position into account. | |
| 186 | + let read = iov.simple_read_from_buffer(kiocb.ki_pos_mut(), &inner.buffer)?; | |
| 187 | + | |
| 188 | + Ok(read) | |
| 189 | + } | |
| 190 | + | |
| 191 | + fn write_iter(mut kiocb: Kiocb<'_, Self::Ptr>, iov: &mut IovIterSource<'_>) -> Result<usize> { | |
| 192 | + let me = kiocb.file(); | |
| 193 | + dev_info!(me.dev, "Writing to Rust Misc Device Sample\n"); | |
| 194 | + | |
| 195 | + let mut inner = me.inner.lock(); | |
| 196 | + | |
| 197 | + // Replace buffer contents. | |
| 198 | + inner.buffer.clear(); | |
| 199 | + let len = iov.copy_from_iter_vec(&mut inner.buffer, GFP_KERNEL)?; | |
| 200 | + | |
| 201 | + // Set position to zero so that future `read` calls will see the new contents. | |
| 202 | + *kiocb.ki_pos_mut() = 0; | |
| 203 | + | |
| 204 | + Ok(len) | |
| 205 | + } | |
| 206 | + | |
| 175 | 207 | fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> Result<isize> { |
| 176 | 208 | dev_info!(me.dev, "IOCTLing Rust Misc Device Sample\n"); |
| 177 | 209 | |