@@ -102,7 +102,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
102
102
/// Get a bitmask of supported virtio/vhost features.
103
103
fn get_features ( & self ) -> Result < u64 > {
104
104
let mut avail_features: u64 = 0 ;
105
- // This ioctl is called on a valid vhost fd and has its return value checked.
105
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
106
106
let ret = unsafe { ioctl_with_mut_ref ( self , VHOST_GET_FEATURES ( ) , & mut avail_features) } ;
107
107
ioctl_result ( ret, avail_features)
108
108
}
@@ -113,21 +113,21 @@ impl<T: VhostKernBackend> VhostBackend for T {
113
113
/// # Arguments
114
114
/// * `features` - Bitmask of features to set.
115
115
fn set_features ( & self , features : u64 ) -> Result < ( ) > {
116
- // This ioctl is called on a valid vhost fd and has its return value checked.
116
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
117
117
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_FEATURES ( ) , & features) } ;
118
118
ioctl_result ( ret, ( ) )
119
119
}
120
120
121
121
/// Set the current process as the owner of this file descriptor.
122
122
/// This must be run before any other vhost ioctls.
123
123
fn set_owner ( & self ) -> Result < ( ) > {
124
- // This ioctl is called on a valid vhost fd and has its return value checked.
124
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
125
125
let ret = unsafe { ioctl ( self , VHOST_SET_OWNER ( ) ) } ;
126
126
ioctl_result ( ret, ( ) )
127
127
}
128
128
129
129
fn reset_owner ( & self ) -> Result < ( ) > {
130
- // This ioctl is called on a valid vhost fd and has its return value checked.
130
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
131
131
let ret = unsafe { ioctl ( self , VHOST_RESET_OWNER ( ) ) } ;
132
132
ioctl_result ( ret, ( ) )
133
133
}
@@ -151,7 +151,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
151
151
) ?;
152
152
}
153
153
154
- // This ioctl is called with a pointer that is valid for the lifetime
154
+ // SAFETY: This ioctl is called with a pointer that is valid for the lifetime
155
155
// of this function. The kernel will make its own copy of the memory
156
156
// tables. As always, check the return value.
157
157
let ret = unsafe { ioctl_with_ptr ( self , VHOST_SET_MEM_TABLE ( ) , vhost_memory. as_ptr ( ) ) } ;
@@ -167,15 +167,15 @@ impl<T: VhostKernBackend> VhostBackend for T {
167
167
return Err ( Error :: LogAddress ) ;
168
168
}
169
169
170
- // This ioctl is called on a valid vhost fd and has its return value checked.
170
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
171
171
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_LOG_BASE ( ) , & base) } ;
172
172
ioctl_result ( ret, ( ) )
173
173
}
174
174
175
175
/// Specify an eventfd file descriptor to signal on log write.
176
176
fn set_log_fd ( & self , fd : RawFd ) -> Result < ( ) > {
177
- // This ioctl is called on a valid vhost fd and has its return value checked.
178
177
let val: i32 = fd;
178
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
179
179
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_LOG_FD ( ) , & val) } ;
180
180
ioctl_result ( ret, ( ) )
181
181
}
@@ -191,7 +191,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
191
191
num : u32:: from ( num) ,
192
192
} ;
193
193
194
- // This ioctl is called on a valid vhost fd and has its return value checked.
194
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
195
195
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_VRING_NUM ( ) , & vring_state) } ;
196
196
ioctl_result ( ret, ( ) )
197
197
}
@@ -210,7 +210,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
210
210
// The addresses are converted into the host address space.
211
211
let vring_addr = config_data. to_vhost_vring_addr ( queue_index, self . mem ( ) ) ?;
212
212
213
- // This ioctl is called on a valid vhost fd and has its
213
+ // SAFETY: This ioctl is called on a valid vhost fd and has its
214
214
// return value checked.
215
215
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_VRING_ADDR ( ) , & vring_addr) } ;
216
216
ioctl_result ( ret, ( ) )
@@ -227,7 +227,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
227
227
num : u32:: from ( base) ,
228
228
} ;
229
229
230
- // This ioctl is called on a valid vhost fd and has its return value checked.
230
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
231
231
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_VRING_BASE ( ) , & vring_state) } ;
232
232
ioctl_result ( ret, ( ) )
233
233
}
@@ -238,7 +238,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
238
238
index : queue_index as u32 ,
239
239
num : 0 ,
240
240
} ;
241
- // This ioctl is called on a valid vhost fd and has its return value checked.
241
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
242
242
let ret = unsafe { ioctl_with_ref ( self , VHOST_GET_VRING_BASE ( ) , & vring_state) } ;
243
243
ioctl_result ( ret, vring_state. num )
244
244
}
@@ -254,7 +254,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
254
254
fd : fd. as_raw_fd ( ) ,
255
255
} ;
256
256
257
- // This ioctl is called on a valid vhost fd and has its return value checked.
257
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
258
258
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_VRING_CALL ( ) , & vring_file) } ;
259
259
ioctl_result ( ret, ( ) )
260
260
}
@@ -271,7 +271,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
271
271
fd : fd. as_raw_fd ( ) ,
272
272
} ;
273
273
274
- // This ioctl is called on a valid vhost fd and has its return value checked.
274
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
275
275
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_VRING_KICK ( ) , & vring_file) } ;
276
276
ioctl_result ( ret, ( ) )
277
277
}
@@ -287,7 +287,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
287
287
fd : fd. as_raw_fd ( ) ,
288
288
} ;
289
289
290
- // This ioctl is called on a valid vhost fd and has its return value checked.
290
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
291
291
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_VRING_ERR ( ) , & vring_file) } ;
292
292
ioctl_result ( ret, ( ) )
293
293
}
@@ -304,8 +304,9 @@ pub trait VhostKernFeatures: Sized + AsRawFd {
304
304
/// Get a bitmask of supported vhost backend features.
305
305
fn get_backend_features ( & self ) -> Result < u64 > {
306
306
let mut avail_features: u64 = 0 ;
307
- // This ioctl is called on a valid vhost fd and has its return value checked.
307
+
308
308
let ret =
309
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
309
310
unsafe { ioctl_with_mut_ref ( self , VHOST_GET_BACKEND_FEATURES ( ) , & mut avail_features) } ;
310
311
ioctl_result ( ret, avail_features)
311
312
}
@@ -316,7 +317,7 @@ pub trait VhostKernFeatures: Sized + AsRawFd {
316
317
/// # Arguments
317
318
/// * `features` - Bitmask of features to set.
318
319
fn set_backend_features ( & mut self , features : u64 ) -> Result < ( ) > {
319
- // This ioctl is called on a valid vhost fd and has its return value checked.
320
+ // SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
320
321
let ret = unsafe { ioctl_with_ref ( self , VHOST_SET_BACKEND_FEATURES ( ) , & features) } ;
321
322
322
323
if ret >= 0 {
@@ -348,6 +349,8 @@ impl<I: VhostKernBackend + VhostKernFeatures> VhostIotlbBackend for I {
348
349
msg_v2. __bindgen_anon_1 . iotlb . perm = msg. perm as u8 ;
349
350
msg_v2. __bindgen_anon_1 . iotlb . type_ = msg. msg_type as u8 ;
350
351
352
+ // SAFETY: This is safe because we are using a valid vhost fd, and
353
+ // a valid pointer and size to the vhost_msg_v2 structure.
351
354
ret = unsafe {
352
355
write (
353
356
self . as_raw_fd ( ) ,
@@ -367,6 +370,8 @@ impl<I: VhostKernBackend + VhostKernFeatures> VhostIotlbBackend for I {
367
370
msg_v1. __bindgen_anon_1 . iotlb . perm = msg. perm as u8 ;
368
371
msg_v1. __bindgen_anon_1 . iotlb . type_ = msg. msg_type as u8 ;
369
372
373
+ // SAFETY: This is safe because we are using a valid vhost fd, and
374
+ // a valid pointer and size to the vhost_msg structure.
370
375
ret = unsafe {
371
376
write (
372
377
self . as_raw_fd ( ) ,
@@ -386,6 +391,9 @@ impl VhostIotlbMsgParser for vhost_msg {
386
391
return Err ( Error :: InvalidIotlbMsg ) ;
387
392
}
388
393
394
+ // SAFETY: We trust the kernel to return a structure with the union
395
+ // fields properly initialized. We are sure it is a vhost_msg, because
396
+ // we checked that `self.type_` is VHOST_IOTLB_MSG.
389
397
unsafe {
390
398
if self . __bindgen_anon_1 . iotlb . type_ == 0 {
391
399
return Err ( Error :: InvalidIotlbMsg ) ;
@@ -408,6 +416,9 @@ impl VhostIotlbMsgParser for vhost_msg_v2 {
408
416
return Err ( Error :: InvalidIotlbMsg ) ;
409
417
}
410
418
419
+ // SAFETY: We trust the kernel to return a structure with the union
420
+ // fields properly initialized. We are sure it is a vhost_msg_v2, because
421
+ // we checked that `self.type_` is VHOST_IOTLB_MSG_V2.
411
422
unsafe {
412
423
if self . __bindgen_anon_1 . iotlb . type_ == 0 {
413
424
return Err ( Error :: InvalidIotlbMsg ) ;
0 commit comments