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

Unified · Split

6 files
  1. rust/kernel/fs.rs +3 −0
  2. rust/kernel/fs/kiocb.rs +68 −0
  3. rust/kernel/iov.rs +314 −0
  4. rust/kernel/lib.rs +1 −0
  5. rust/kernel/miscdevice.rs +62 −1
  6. samples/rust/rust_misc_device.rs +34 −2
rust/kernel/fs.rs+3−0
@@ -6,3 +6,6 @@ · 5 unmodified lines
6 6
7pub mod file;7pub mod file;
8pub use self::file::{File, LocalFile};8pub use self::file::{File, LocalFile};
9
10mod kiocb;
11pub use self::kiocb::Kiocb;
rust/kernel/fs/kiocb.rs+68−0
@@ -0,0 +1,68 @@
1// SPDX-License-Identifier: GPL-2.0
2
3// Copyright (C) 2024 Google LLC.
4
5//! Kernel IO callbacks.
6//!
7//! C headers: [`include/linux/fs.h`](srctree/include/linux/fs.h)
8
9use core::marker::PhantomData;
10use core::ptr::NonNull;
11use kernel::types::ForeignOwnable;
12
13/// Wrapper for the kernel's `struct kiocb`.
14///
15/// Currently this abstractions is incomplete and is essentially just a tuple containing a
16/// reference to a file and a file position.
17///
18/// The type `T` represents the filesystem or driver specific data associated with the file.
19///
20/// # Invariants
21///
22/// `inner` points at a valid `struct kiocb` whose file has the type `T` as its private data.
23pub struct Kiocb<'a, T> {
24 inner: NonNull<bindings::kiocb>,
25 _phantom: PhantomData<&'a T>,
26}
27
28impl<'a, T: ForeignOwnable> Kiocb<'a, T> {
29 /// Create a `Kiocb` from a raw pointer.
30 ///
31 /// # Safety
32 ///
33 /// The pointer must reference a valid `struct kiocb` for the duration of `'a`. The private
34 /// data of the file must be `T`.
35 pub unsafe fn from_raw(kiocb: *mut bindings::kiocb) -> Self {
36 Self {
37 // SAFETY: If a pointer is valid it is not null.
38 inner: unsafe { NonNull::new_unchecked(kiocb) },
39 _phantom: PhantomData,
40 }
41 }
42
43 /// Access the underlying `struct kiocb` directly.
44 pub fn as_raw(&self) -> *mut bindings::kiocb {
45 self.inner.as_ptr()
46 }
47
48 /// Get the filesystem or driver specific data associated with the file.
49 pub fn file(&self) -> <T as ForeignOwnable>::Borrowed<'a> {
50 // SAFETY: We have shared access to this kiocb and hence the underlying file, so we can
51 // read the file's private data.
52 let private = unsafe { (*(*self.as_raw()).ki_filp).private_data };
53 // SAFETY: The kiocb has shared access to the private data.
54 unsafe { <T as ForeignOwnable>::borrow(private) }
55 }
56
57 /// Gets the current value of `ki_pos`.
58 pub fn ki_pos(&self) -> i64 {
59 // SAFETY: We have shared access to the kiocb, so we can read its `ki_pos` field.
60 unsafe { (*self.as_raw()).ki_pos }
61 }
62
63 /// Gets a mutable reference to the `ki_pos` field.
64 pub fn ki_pos_mut(&mut self) -> &mut i64 {
65 // SAFETY: We have exclusive access to the kiocb, so we can write to `ki_pos`.
66 unsafe { &mut (*self.as_raw()).ki_pos }
67 }
68}
rust/kernel/iov.rs+314−0
@@ -0,0 +1,314 @@
1// SPDX-License-Identifier: GPL-2.0
2
3// Copyright (C) 2025 Google LLC.
4
5//! IO vectors.
6//!
7//! C headers: [`include/linux/iov_iter.h`](srctree/include/linux/iov_iter.h),
8//! [`include/linux/uio.h`](srctree/include/linux/uio.h)
9
10use crate::{
11 alloc::{Allocator, Flags},
12 bindings,
13 prelude::*,
14 types::Opaque,
15};
16use core::{marker::PhantomData, mem::MaybeUninit, ptr, slice};
17
18const ITER_SOURCE: bool = bindings::ITER_SOURCE != 0;
19const ITER_DEST: bool = bindings::ITER_DEST != 0;
20
21// Compile-time assertion for the above constants.
22const _: () = {
23 build_assert!(
24 ITER_SOURCE != ITER_DEST,
25 "ITER_DEST and ITER_SOURCE should be different."
26 );
27};
28
29/// An IO vector that acts as a source of data.
30///
31/// The data may come from many different sources. This includes both things in kernel-space and
32/// reading from userspace. It's not necessarily the case that the data source is immutable, so
33/// rewinding the IO vector to read the same data twice is not guaranteed to result in the same
34/// bytes. It's also possible that the data source is mapped in a thread-local manner using e.g.
35/// `kmap_local_page()`, so this type is not `Send` to ensure that the mapping is read from the
36/// right context in that scenario.
37///
38/// # Invariants
39///
40/// Must hold a valid `struct iov_iter` with `data_source` set to `ITER_SOURCE`. For the duration
41/// of `'data`, it must be safe to read from this IO vector using the standard C methods for this
42/// purpose.
43#[repr(transparent)]
44pub struct IovIterSource<'data> {
45 iov: Opaque<bindings::iov_iter>,
46 /// Represent to the type system that this value contains a pointer to readable data it does
47 /// not own.
48 _source: PhantomData<&'data [u8]>,
49}
50
51impl<'data> IovIterSource<'data> {
52 /// Obtain an `IovIterSource` from a raw pointer.
53 ///
54 /// # Safety
55 ///
56 /// * The referenced `struct iov_iter` must be valid and must only be accessed through the
57 /// returned reference for the duration of `'iov`.
58 /// * The referenced `struct iov_iter` must have `data_source` set to `ITER_SOURCE`.
59 /// * For the duration of `'data`, it must be safe to read from this IO vector using the
60 /// standard C methods for this purpose.
61 #[track_caller]
62 #[inline]
63 pub unsafe fn from_raw<'iov>(ptr: *mut bindings::iov_iter) -> &'iov mut IovIterSource<'data> {
64 // SAFETY: The caller ensures that `ptr` is valid.
65 let data_source = unsafe { (*ptr).data_source };
66 assert_eq!(data_source, ITER_SOURCE);
67
68 // SAFETY: The caller ensures the type invariants for the right durations, and
69 // `IovIterSource` is layout compatible with `struct iov_iter`.
70 unsafe { &mut *ptr.cast::<IovIterSource<'data>>() }
71 }
72
73 /// Access this as a raw `struct iov_iter`.
74 #[inline]
75 pub fn as_raw(&mut self) -> *mut bindings::iov_iter {
76 self.iov.get()
77 }
78
79 /// Returns the number of bytes available in this IO vector.
80 ///
81 /// Note that this may overestimate the number of bytes. For example, reading from userspace
82 /// memory could fail with `EFAULT`, which will be treated as the end of the IO vector.
83 #[inline]
84 pub fn len(&self) -> usize {
85 // SAFETY: We have shared access to this IO vector, so we can read its `count` field.
86 unsafe {
87 (*self.iov.get())
88 .__bindgen_anon_1
89 .__bindgen_anon_1
90 .as_ref()
91 .count
92 }
93 }
94
95 /// Returns whether there are any bytes left in this IO vector.
96 ///
97 /// This may return `true` even if there are no more bytes available. For example, reading from
98 /// userspace memory could fail with `EFAULT`, which will be treated as the end of the IO vector.
99 #[inline]
100 pub fn is_empty(&self) -> bool {
101 self.len() == 0
102 }
103
104 /// Advance this IO vector by `bytes` bytes.
105 ///
106 /// If `bytes` is larger than the size of this IO vector, it is advanced to the end.
107 #[inline]
108 pub fn advance(&mut self, bytes: usize) {
109 // SAFETY: By the type invariants, `self.iov` is a valid IO vector.
110 unsafe { bindings::iov_iter_advance(self.as_raw(), bytes) };
111 }
112
113 /// Advance this IO vector backwards by `bytes` bytes.
114 ///
115 /// # Safety
116 ///
117 /// The IO vector must not be reverted to before its beginning.
118 #[inline]
119 pub unsafe fn revert(&mut self, bytes: usize) {
120 // SAFETY: By the type invariants, `self.iov` is a valid IO vector, and the caller
121 // ensures that `bytes` is in bounds.
122 unsafe { bindings::iov_iter_revert(self.as_raw(), bytes) };
123 }
124
125 /// Read data from this IO vector.
126 ///
127 /// Returns the number of bytes that have been copied.
128 #[inline]
129 pub fn copy_from_iter(&mut self, out: &mut [u8]) -> usize {
130 // SAFETY: `Self::copy_from_iter_raw` guarantees that it will not write any uninitialized
131 // bytes in the provided buffer, so `out` is still a valid `u8` slice after this call.
132 let out = unsafe { &mut *(ptr::from_mut(out) as *mut [MaybeUninit<u8>]) };
133
134 self.copy_from_iter_raw(out).len()
135 }
136
137 /// Read data from this IO vector and append it to a vector.
138 ///
139 /// Returns the number of bytes that have been copied.
140 #[inline]
141 pub fn copy_from_iter_vec<A: Allocator>(
142 &mut self,
143 out: &mut Vec<u8, A>,
144 flags: Flags,
145 ) -> Result<usize> {
146 out.reserve(self.len(), flags)?;
147 let len = self.copy_from_iter_raw(out.spare_capacity_mut()).len();
148 // SAFETY:
149 // - `len` is the length of a subslice of the spare capacity, so `len` is at most the
150 // length of the spare capacity.
151 // - `Self::copy_from_iter_raw` guarantees that the first `len` bytes of the spare capacity
152 // have been initialized.
153 unsafe { out.inc_len(len) };
154 Ok(len)
155 }
156
157 /// Read data from this IO vector into potentially uninitialized memory.
158 ///
159 /// Returns the sub-slice of the output that has been initialized. If the returned slice is
160 /// shorter than the input buffer, then the entire IO vector has been read.
161 ///
162 /// This will never write uninitialized bytes to the provided buffer.
163 #[inline]
164 pub fn copy_from_iter_raw(&mut self, out: &mut [MaybeUninit<u8>]) -> &mut [u8] {
165 let capacity = out.len();
166 let out = out.as_mut_ptr().cast::<u8>();
167
168 // GUARANTEES: The C API guarantees that it does not write uninitialized bytes to the
169 // provided buffer.
170 // SAFETY:
171 // * By the type invariants, it is still valid to read from this IO vector.
172 // * `out` is valid for writing for `capacity` bytes because it comes from a slice of
173 // that length.
174 let len = unsafe { bindings::_copy_from_iter(out.cast(), capacity, self.as_raw()) };
175
176 // SAFETY: The underlying C api guarantees that initialized bytes have been written to the
177 // first `len` bytes of the spare capacity.
178 unsafe { slice::from_raw_parts_mut(out, len) }
179 }
180}
181
182/// An IO vector that acts as a destination for data.
183///
184/// IO vectors support many different types of destinations. This includes both buffers in
185/// kernel-space and writing to userspace. It's possible that the destination buffer is mapped in a
186/// thread-local manner using e.g. `kmap_local_page()`, so this type is not `Send` to ensure that
187/// the mapping is written to the right context in that scenario.
188///
189/// # Invariants
190///
191/// Must hold a valid `struct iov_iter` with `data_source` set to `ITER_DEST`. For the duration of
192/// `'data`, it must be safe to write to this IO vector using the standard C methods for this
193/// purpose.
194#[repr(transparent)]
195pub struct IovIterDest<'data> {
196 iov: Opaque<bindings::iov_iter>,
197 /// Represent to the type system that this value contains a pointer to writable data it does
198 /// not own.
199 _source: PhantomData<&'data mut [u8]>,
200}
201
202impl<'data> IovIterDest<'data> {
203 /// Obtain an `IovIterDest` from a raw pointer.
204 ///
205 /// # Safety
206 ///
207 /// * The referenced `struct iov_iter` must be valid and must only be accessed through the
208 /// returned reference for the duration of `'iov`.
209 /// * The referenced `struct iov_iter` must have `data_source` set to `ITER_DEST`.
210 /// * For the duration of `'data`, it must be safe to write to this IO vector using the
211 /// standard C methods for this purpose.
212 #[track_caller]
213 #[inline]
214 pub unsafe fn from_raw<'iov>(ptr: *mut bindings::iov_iter) -> &'iov mut IovIterDest<'data> {
215 // SAFETY: The caller ensures that `ptr` is valid.
216 let data_source = unsafe { (*ptr).data_source };
217 assert_eq!(data_source, ITER_DEST);
218
219 // SAFETY: The caller ensures the type invariants for the right durations, and
220 // `IovIterSource` is layout compatible with `struct iov_iter`.
221 unsafe { &mut *ptr.cast::<IovIterDest<'data>>() }
222 }
223
224 /// Access this as a raw `struct iov_iter`.
225 #[inline]
226 pub fn as_raw(&mut self) -> *mut bindings::iov_iter {
227 self.iov.get()
228 }
229
230 /// Returns the number of bytes available in this IO vector.
231 ///
232 /// Note that this may overestimate the number of bytes. For example, reading from userspace
233 /// memory could fail with EFAULT, which will be treated as the end of the IO vector.
234 #[inline]
235 pub fn len(&self) -> usize {
236 // SAFETY: We have shared access to this IO vector, so we can read its `count` field.
237 unsafe {
238 (*self.iov.get())
239 .__bindgen_anon_1
240 .__bindgen_anon_1
241 .as_ref()
242 .count
243 }
244 }
245
246 /// Returns whether there are any bytes left in this IO vector.
247 ///
248 /// This may return `true` even if there are no more bytes available. For example, reading from
249 /// userspace memory could fail with EFAULT, which will be treated as the end of the IO vector.
250 #[inline]
251 pub fn is_empty(&self) -> bool {
252 self.len() == 0
253 }
254
255 /// Advance this IO vector by `bytes` bytes.
256 ///
257 /// If `bytes` is larger than the size of this IO vector, it is advanced to the end.
258 #[inline]
259 pub fn advance(&mut self, bytes: usize) {
260 // SAFETY: By the type invariants, `self.iov` is a valid IO vector.
261 unsafe { bindings::iov_iter_advance(self.as_raw(), bytes) };
262 }
263
264 /// Advance this IO vector backwards by `bytes` bytes.
265 ///
266 /// # Safety
267 ///
268 /// The IO vector must not be reverted to before its beginning.
269 #[inline]
270 pub unsafe fn revert(&mut self, bytes: usize) {
271 // SAFETY: By the type invariants, `self.iov` is a valid IO vector, and the caller
272 // ensures that `bytes` is in bounds.
273 unsafe { bindings::iov_iter_revert(self.as_raw(), bytes) };
274 }
275
276 /// Write data to this IO vector.
277 ///
278 /// Returns the number of bytes that were written. If this is shorter than the provided slice,
279 /// then no more bytes can be written.
280 #[inline]
281 pub fn copy_to_iter(&mut self, input: &[u8]) -> usize {
282 // SAFETY:
283 // * By the type invariants, it is still valid to write to this IO vector.
284 // * `input` is valid for `input.len()` bytes.
285 unsafe { bindings::_copy_to_iter(input.as_ptr().cast(), input.len(), self.as_raw()) }
286 }
287
288 /// Utility for implementing `read_iter` given the full contents of the file.
289 ///
290 /// The full contents of the file being read from is represented by `contents`. This call will
291 /// write the appropriate sub-slice of `contents` and update the file position in `ppos` so
292 /// that the file will appear to contain `contents` even if takes multiple reads to read the
293 /// entire file.
294 #[inline]
295 pub fn simple_read_from_buffer(&mut self, ppos: &mut i64, contents: &[u8]) -> Result<usize> {
296 if *ppos < 0 {
297 return Err(EINVAL);
298 }
299 let Ok(pos) = usize::try_from(*ppos) else {
300 return Ok(0);
301 };
302 if pos >= contents.len() {
303 return Ok(0);
304 }
305
306 // BOUNDS: We just checked that `pos < contents.len()` above.
307 let num_written = self.copy_to_iter(&contents[pos..]);
308
309 // OVERFLOW: `pos+num_written <= contents.len() <= isize::MAX <= i64::MAX`.
310 *ppos = (pos + num_written) as i64;
311
312 Ok(num_written)
313 }
314}
rust/kernel/lib.rs+1−0
@@ -92,6 +92,7 @@ pub mod fs; · 91 unmodified lines
92pub mod init;92pub mod init;
93pub mod io;93pub mod io;
94pub mod ioctl;94pub mod ioctl;
95pub mod iov;
95pub mod jump_label;96pub mod jump_label;
96#[cfg(CONFIG_KUNIT)]97#[cfg(CONFIG_KUNIT)]
97pub mod kunit;98pub mod kunit;
rust/kernel/miscdevice.rs+62−1
@@ -13,7 +13,8 @@ use crate::{ · 12 unmodified lines
13 device::Device,13 device::Device,
14 error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR},14 error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR},
15 ffi::{c_int, c_long, c_uint, c_ulong},15 ffi::{c_int, c_long, c_uint, c_ulong},
16 fs::File,16 fs::{File, Kiocb},
17 iov::{IovIterDest, IovIterSource},
17 mm::virt::VmaNew,18 mm::virt::VmaNew,
18 prelude::*,19 prelude::*,
19 seq_file::SeqFile,20 seq_file::SeqFile,
@@ -141,6 +142,16 @@ pub trait MiscDevice: Sized { · 121 unmodified lines
141 build_error!(VTABLE_DEFAULT_ERROR)142 build_error!(VTABLE_DEFAULT_ERROR)
142 }143 }
143 144
145 /// Read from this miscdevice.
146 fn read_iter(_kiocb: Kiocb<'_, Self::Ptr>, _iov: &mut IovIterDest<'_>) -> Result<usize> {
147 build_error!(VTABLE_DEFAULT_ERROR)
148 }
149
150 /// Write to this miscdevice.
151 fn write_iter(_kiocb: Kiocb<'_, Self::Ptr>, _iov: &mut IovIterSource<'_>) -> Result<usize> {
152 build_error!(VTABLE_DEFAULT_ERROR)
153 }
154
144 /// Handler for ioctls.155 /// Handler for ioctls.
145 ///156 ///
146 /// The `cmd` argument is usually manipulated using the utilities in [`kernel::ioctl`].157 /// The `cmd` argument is usually manipulated using the utilities in [`kernel::ioctl`].
@@ -245,6 +256,46 @@ impl<T: MiscDevice> MiscdeviceVTable<T> { · 98 unmodified lines
245 0256 0
246 }257 }
247 258
259 /// # Safety
260 ///
261 /// `kiocb` must be correspond to a valid file that is associated with a
262 /// `MiscDeviceRegistration<T>`. `iter` must be a valid `struct iov_iter` for writing.
263 unsafe extern "C" fn read_iter(
264 kiocb: *mut bindings::kiocb,
265 iter: *mut bindings::iov_iter,
266 ) -> isize {
267 // SAFETY: The caller provides a valid `struct kiocb` associated with a
268 // `MiscDeviceRegistration<T>` file.
269 let kiocb = unsafe { Kiocb::from_raw(kiocb) };
270 // SAFETY: This is a valid `struct iov_iter` for writing.
271 let iov = unsafe { IovIterDest::from_raw(iter) };
272
273 match T::read_iter(kiocb, iov) {
274 Ok(res) => res as isize,
275 Err(err) => err.to_errno() as isize,
276 }
277 }
278
279 /// # Safety
280 ///
281 /// `kiocb` must be correspond to a valid file that is associated with a
282 /// `MiscDeviceRegistration<T>`. `iter` must be a valid `struct iov_iter` for writing.
283 unsafe extern "C" fn write_iter(
284 kiocb: *mut bindings::kiocb,
285 iter: *mut bindings::iov_iter,
286 ) -> isize {
287 // SAFETY: The caller provides a valid `struct kiocb` associated with a
288 // `MiscDeviceRegistration<T>` file.
289 let kiocb = unsafe { Kiocb::from_raw(kiocb) };
290 // SAFETY: This is a valid `struct iov_iter` for reading.
291 let iov = unsafe { IovIterSource::from_raw(iter) };
292
293 match T::write_iter(kiocb, iov) {
294 Ok(res) => res as isize,
295 Err(err) => err.to_errno() as isize,
296 }
297 }
298
248 /// # Safety299 /// # Safety
249 ///300 ///
250 /// `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`.301 /// `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`.
@@ -341,6 +392,16 @@ impl<T: MiscDevice> MiscdeviceVTable<T> { · 90 unmodified lines
341 open: Some(Self::open),392 open: Some(Self::open),
342 release: Some(Self::release),393 release: Some(Self::release),
343 mmap: if T::HAS_MMAP { Some(Self::mmap) } else { None },394 mmap: if T::HAS_MMAP { Some(Self::mmap) } else { None },
395 read_iter: if T::HAS_READ_ITER {
396 Some(Self::read_iter)
397 } else {
398 None
399 },
400 write_iter: if T::HAS_WRITE_ITER {
401 Some(Self::write_iter)
402 } else {
403 None
404 },
344 unlocked_ioctl: if T::HAS_IOCTL {405 unlocked_ioctl: if T::HAS_IOCTL {
345 Some(Self::ioctl)406 Some(Self::ioctl)
346 } else {407 } else {
samples/rust/rust_misc_device.rs+34−2
@@ -100,8 +100,9 @@ use core::pin::Pin; · 99 unmodified lines
100use kernel::{100use kernel::{
101 c_str,101 c_str,
102 device::Device,102 device::Device,
103 fs::File,103 fs::{File, Kiocb},
104 ioctl::{_IO, _IOC_SIZE, _IOR, _IOW},104 ioctl::{_IO, _IOC_SIZE, _IOR, _IOW},
105 iov::{IovIterDest, IovIterSource},
105 miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration},106 miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration},
106 new_mutex,107 new_mutex,
107 prelude::*,108 prelude::*,
@@ -143,6 +144,7 @@ impl kernel::InPlaceModule for RustMiscDeviceModule { · 35 unmodified lines
143 144
144struct Inner {145struct Inner {
145 value: i32,146 value: i32,
147 buffer: KVVec<u8>,
146}148}
147 149
148#[pin_data(PinnedDrop)]150#[pin_data(PinnedDrop)]
@@ -164,7 +166,10 @@ impl MiscDevice for RustMiscDevice { · 15 unmodified lines
164 KBox::try_pin_init(166 KBox::try_pin_init(
165 try_pin_init! {167 try_pin_init! {
166 RustMiscDevice {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 dev: dev,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 fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> Result<isize> {207 fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> Result<isize> {
176 dev_info!(me.dev, "IOCTLing Rust Misc Device Sample\n");208 dev_info!(me.dev, "IOCTLing Rust Misc Device Sample\n");
177 209

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