History

Merge patch series "Rust support for `struct iov_iter`"

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.

Greg Kroah-Hartman committed 1 year ago

Commit 1da0ca42 parents 2d2664f e5b0d7d6 files · +482 −3Browse files

Showing the changes to samples (1 of 6 files). Show all

Unified · Split

samples/rust/rust_misc_device.rs+34−2
@@ -100,8 +100,9 @@ use core::pin::Pin; · 99 unmodified lines
100100 use kernel::{
101101 c_str,
102102 device::Device,
103− fs::File,
103+ fs::{File, Kiocb},
104104 ioctl::{_IO, _IOC_SIZE, _IOR, _IOW},
105+ iov::{IovIterDest, IovIterSource},
105106 miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration},
106107 new_mutex,
107108 prelude::*,
@@ -143,6 +144,7 @@ impl kernel::InPlaceModule for RustMiscDeviceModule { · 35 unmodified lines
143144
144145 struct Inner {
145146 value: i32,
147+ buffer: KVVec<u8>,
146148 }
147149
148150 #[pin_data(PinnedDrop)]
@@ -164,7 +166,10 @@ impl MiscDevice for RustMiscDevice { · 15 unmodified lines
164166 KBox::try_pin_init(
165167 try_pin_init! {
166168 RustMiscDevice {
167− inner <- new_mutex!( Inner{ value: 0_i32 } ),
169+ inner <- new_mutex!(Inner {
170+ value: 0_i32,
171+ buffer: KVVec::new(),
172+ }),
168173 dev: dev,
169174 }
170175 },
@@ -172,6 +177,33 @@ impl MiscDevice for RustMiscDevice { · 1 unmodified line
172177 )
173178 }
174179
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+
175207 fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> Result<isize> {
176208 dev_info!(me.dev, "IOCTLing Rust Misc Device Sample\n");
177209

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t