From e2ec09e00100c9c7a339246899283272af967dde Mon Sep 17 00:00:00 2001 From: Sam Leffler Date: Wed, 11 May 2022 23:18:26 +0000 Subject: [PATCH] MemoryManager: clippy findings Change-Id: I9a82a9cd7628c7a770752baccf382a7c4e7b625c GitOrigin-RevId: 7cf81bf93076305d733ea658fae08e58c3e6f7f1 --- .../kata-memory-component/src/run.rs | 187 ++++++++---------- .../kata-memory-interface/src/lib.rs | 18 +- .../src/memory_manager/mod.rs | 4 +- 3 files changed, 99 insertions(+), 110 deletions(-) diff --git a/apps/system/components/MemoryManager/kata-memory-component/src/run.rs b/apps/system/components/MemoryManager/kata-memory-component/src/run.rs index 27064fb..667f0a5 100644 --- a/apps/system/components/MemoryManager/kata-memory-component/src/run.rs +++ b/apps/system/components/MemoryManager/kata-memory-component/src/run.rs @@ -2,6 +2,7 @@ // Code here binds the camkes component to the rust code. #![no_std] +#![allow(clippy::missing_safety_doc)] use core::ops::Range; use core::slice; @@ -41,7 +42,7 @@ extern "C" { } #[no_mangle] -pub extern "C" fn pre_init() { +pub unsafe extern "C" fn pre_init() { static KATA_LOGGER: KataLogger = KataLogger; log::set_logger(&KATA_LOGGER).unwrap(); // NB: set to max; the LoggerInterface will filter @@ -49,66 +50,58 @@ pub extern "C" fn pre_init() { // TODO(sleffler): temp until we integrate with seL4 static mut HEAP_MEMORY: [u8; 8 * 1024] = [0; 8 * 1024]; - unsafe { - allocator::ALLOCATOR.init(HEAP_MEMORY.as_mut_ptr() as usize, HEAP_MEMORY.len()); - trace!( - "setup heap: start_addr {:p} size {}", - HEAP_MEMORY.as_ptr(), - HEAP_MEMORY.len() - ); - } + allocator::ALLOCATOR.init(HEAP_MEMORY.as_mut_ptr() as usize, HEAP_MEMORY.len()); + trace!( + "setup heap: start_addr {:p} size {}", + HEAP_MEMORY.as_ptr(), + HEAP_MEMORY.len() + ); extern "C" { fn sel4runtime_bootinfo() -> *const seL4_BootInfo; } - unsafe { - // The MemoryManager component is labeled to receive BootInfo); use - // it to complete initialization of the MemoryManager interface. - let bootinfo = &*sel4runtime_bootinfo(); - KATA_MEMORY.init( - /*slots=*/Range:: { - start: bootinfo.untyped.start, - end: bootinfo.untyped.end - }, - /*untypeds=*/ bootinfo.untyped_descs(), + // The MemoryManager component is labeled to receive BootInfo); use + // it to complete initialization of the MemoryManager interface. + let bootinfo = &*sel4runtime_bootinfo(); + KATA_MEMORY.init( + /*slots=*/Range:: { + start: bootinfo.untyped.start, + end: bootinfo.untyped.end + }, + /*untypeds=*/ bootinfo.untyped_descs(), + ); + if let Ok(stats) = KATA_MEMORY.stats() { + trace!("Global memory: {} allocated {} free", + stats.allocated_bytes, + stats.free_bytes, ); - if let Ok(stats) = KATA_MEMORY.stats() { - trace!("Global memory: {} allocated {} free", - stats.allocated_bytes, - stats.free_bytes, - ); - } - - KATA_CSPACE_SLOTS.init( - /*first_slot=*/ bootinfo.empty.start, - /*size=*/ bootinfo.empty.end - bootinfo.empty.start - ); - trace!("setup cspace slots: first slot {} free {}", - KATA_CSPACE_SLOTS.base_slot(), - KATA_CSPACE_SLOTS.free_slots()); } - unsafe { - // Delete the CAmkES-setup CNode; we're going to reuse the - // well-known slot once it is empty (see below). - seL4_CNode_Delete(SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits as u8) - .expect("recv_node"); - } + KATA_CSPACE_SLOTS.init( + /*first_slot=*/ bootinfo.empty.start, + /*size=*/ bootinfo.empty.end - bootinfo.empty.start + ); + trace!("setup cspace slots: first slot {} free {}", + KATA_CSPACE_SLOTS.base_slot(), + KATA_CSPACE_SLOTS.free_slots()); + + // Delete the CAmkES-setup CNode; we're going to reuse the + // well-known slot once it is empty (see memory__init below). + seL4_CNode_Delete(SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits as u8) + .expect("recv_node"); } #[no_mangle] -pub extern "C" fn memory__init() { - unsafe { - // Point the receive path to the well-known slot that was emptied. - // This will be used to receive CNode's from clients for alloc & - // free requests. - // - // NB: this must be done here (rather than someplace like pre_init) - // so it's in the context of the MemoryInterface thread (so we write - // the correct ipc buffer). - seL4_SetCapReceivePath(SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits); - trace!("Cap receive path {}:{}:{}", SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits); - } +pub unsafe extern "C" fn memory__init() { + // Point the receive path to the well-known slot that was emptied. + // This will be used to receive CNode's from clients for alloc & + // free requests. + // + // NB: this must be done here (rather than someplace like pre_init) + // so it's in the context of the MemoryInterface thread (so we write + // the correct ipc buffer). + seL4_SetCapReceivePath(SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits); + trace!("Cap receive path {}:{}:{}", SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits); } // MemoryInterface glue stubs. @@ -124,71 +117,65 @@ fn clear_path(&(root, index, depth): &(seL4_CPtr, seL4_CPtr, seL4_Word)) { } #[no_mangle] -pub extern "C" fn memory_alloc( +pub unsafe extern "C" fn memory_alloc( c_raw_data_len: u32, c_raw_data: *const u8, ) -> MemoryManagerError { - unsafe { - let recv_path = seL4_GetCapReceivePath(); - // NB: make sure noone clobbers the setup done in memory__init - assert_eq!(recv_path, (SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits)); + let recv_path = seL4_GetCapReceivePath(); + // NB: make sure noone clobbers the setup done in memory__init + assert_eq!(recv_path, (SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits)); - let raw_slice = slice::from_raw_parts(c_raw_data, c_raw_data_len as usize); - let ret_status = match postcard::from_bytes::(raw_slice) { - Ok(mut bundle) => { - // TODO(sleffler): verify we received a CNode in MEMORY_RECV_CNODE. - bundle.cnode = recv_path.1; - // NB: bundle.depth should reflect the received cnode - KATA_MEMORY.alloc(&bundle).into() - } - Err(_) => MemoryManagerError::MmeDeserializeFailed, - }; - // NB: must clear ReceivePath for next request - clear_path(&recv_path); - ret_status - } + let raw_slice = slice::from_raw_parts(c_raw_data, c_raw_data_len as usize); + let ret_status = match postcard::from_bytes::(raw_slice) { + Ok(mut bundle) => { + // TODO(sleffler): verify we received a CNode in MEMORY_RECV_CNODE. + bundle.cnode = recv_path.1; + // NB: bundle.depth should reflect the received cnode + KATA_MEMORY.alloc(&bundle).into() + } + Err(_) => MemoryManagerError::MmeDeserializeFailed, + }; + // NB: must clear ReceivePath for next request + clear_path(&recv_path); + ret_status } #[no_mangle] -pub extern "C" fn memory_free( +pub unsafe extern "C" fn memory_free( c_raw_data_len: u32, c_raw_data: *const u8, ) -> MemoryManagerError { - unsafe { - let recv_path = seL4_GetCapReceivePath(); - // NB: make sure noone clobbers the setup done in memory__init - assert_eq!(recv_path, (SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits)); + let recv_path = seL4_GetCapReceivePath(); + // NB: make sure noone clobbers the setup done in memory__init + assert_eq!(recv_path, (SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits)); - let raw_slice = slice::from_raw_parts(c_raw_data, c_raw_data_len as usize); - let ret_status = match postcard::from_bytes::(raw_slice) { - Ok(mut bundle) => { - // TODO(sleffler): verify we received a CNode in MEMORY_RECV_CNODE. - bundle.cnode = recv_path.1; - // NB: bundle.depth should reflect the received cnode - KATA_MEMORY.free(&bundle).into() - } - Err(_) => MemoryManagerError::MmeDeserializeFailed, - }; - // NB: must clear ReceivePath for next request - clear_path(&recv_path); - ret_status - } + let raw_slice = slice::from_raw_parts(c_raw_data, c_raw_data_len as usize); + let ret_status = match postcard::from_bytes::(raw_slice) { + Ok(mut bundle) => { + // TODO(sleffler): verify we received a CNode in MEMORY_RECV_CNODE. + bundle.cnode = recv_path.1; + // NB: bundle.depth should reflect the received cnode + KATA_MEMORY.free(&bundle).into() + } + Err(_) => MemoryManagerError::MmeDeserializeFailed, + }; + // NB: must clear ReceivePath for next request + clear_path(&recv_path); + ret_status } #[no_mangle] -pub extern "C" fn memory_stats( +pub unsafe extern "C" fn memory_stats( c_raw_resp_data: *mut RawMemoryStatsData, ) -> MemoryManagerError { - unsafe { - // TODO(sleffler): verify no cap was received - match KATA_MEMORY.stats() { - Ok(stats) => { - match postcard::to_slice(&stats, &mut (*c_raw_resp_data)[..]) { - Ok(_) => MemoryManagerError::MmeSuccess, - Err(_) => MemoryManagerError::MmeSerializeFailed, - } - } - Err(e) => e.into(), + // TODO(sleffler): verify no cap was received + match KATA_MEMORY.stats() { + Ok(stats) => { + match postcard::to_slice(&stats, &mut (*c_raw_resp_data)[..]) { + Ok(_) => MemoryManagerError::MmeSuccess, + Err(_) => MemoryManagerError::MmeSerializeFailed, + } } + Err(e) => e.into(), } } diff --git a/apps/system/components/MemoryManager/kata-memory-interface/src/lib.rs b/apps/system/components/MemoryManager/kata-memory-interface/src/lib.rs index ab27265..fdfc844 100644 --- a/apps/system/components/MemoryManager/kata-memory-interface/src/lib.rs +++ b/apps/system/components/MemoryManager/kata-memory-interface/src/lib.rs @@ -10,7 +10,6 @@ use core::fmt; use kata_os_common::slot_allocator; use kata_os_common::sel4_sys; use log::trace; -use postcard; use serde::{Deserialize, Serialize}; use sel4_sys::seL4_CNode_Move; @@ -150,6 +149,9 @@ impl ObjDescBundle { ObjDescBundle { cnode, depth, objs } } + // Returns whether there are any object descriptors. + pub fn is_empty(&self) -> bool { self.objs.len() == 0 } + // Returns the number of object descriptors. pub fn len(&self) -> usize { self.objs.len() } @@ -239,7 +241,7 @@ impl ObjDescBundle { } unsafe { KATA_CSPACE_SLOTS.free(od.cptr, count) }; od.cptr = dest_slot; - dest_slot = dest_slot + count; + dest_slot += count; } self.cnode = dest_cnode; self.depth = dest_depth; @@ -347,7 +349,7 @@ impl From for MemoryManagerError { impl From> for MemoryManagerError { fn from(result: Result<(), MemoryError>) -> MemoryManagerError { result.map_or_else( - |e| MemoryManagerError::from(e), + MemoryManagerError::from, |_v| MemoryManagerError::MmeSuccess, ) } @@ -400,7 +402,7 @@ pub fn kata_object_alloc_in_toplevel( let mut request = ObjDescBundle::new( unsafe { MEMORY_RECV_CNODE }, unsafe { MEMORY_RECV_CNODE_DEPTH }, - objs.clone(), + objs, ); kata_object_alloc(&request)?; match request.move_objects_to_toplevel() { @@ -424,7 +426,7 @@ pub fn kata_object_alloc_in_cnode( fn next_log2(value: usize) -> usize { let mut size_bits = 0; while value > (1 << size_bits) { - size_bits = size_bits + 1; + size_bits += 1; } size_bits } @@ -437,7 +439,7 @@ pub fn kata_object_alloc_in_cnode( // Now construct the request for |objs| with |cnode| as the container. let request = ObjDescBundle::new( - cnode.objs[0].cptr, cnode_depth as u8, objs.clone(), + cnode.objs[0].cptr, cnode_depth as u8, objs, ); match kata_object_alloc(&request) { Err(e) => { @@ -642,7 +644,7 @@ pub fn kata_object_free_in_cnode( /*cptr=*/ request.cnode )], ); - kata_object_free(&request)?; + kata_object_free(request)?; // No way to recover if this fails.. kata_object_free_toplevel(&cnode_obj) } @@ -666,7 +668,7 @@ pub fn kata_memory_stats() -> Result { fn memory_stats(c_data: *mut RawMemoryStatsData) -> MemoryManagerError; } let raw_data = &mut [0u8; RAW_MEMORY_STATS_DATA_SIZE]; - match unsafe { memory_stats(raw_data as *mut _) }.into() { + match unsafe { memory_stats(raw_data as *mut _) } { MemoryManagerError::MmeSuccess => { let stats = postcard::from_bytes::(raw_data) .map_err(|_| MemoryManagerError::MmeDeserializeFailed)?; diff --git a/apps/system/components/MemoryManager/kata-memory-manager/src/memory_manager/mod.rs b/apps/system/components/MemoryManager/kata-memory-manager/src/memory_manager/mod.rs index 4ab4a8c..65af0e1 100644 --- a/apps/system/components/MemoryManager/kata-memory-manager/src/memory_manager/mod.rs +++ b/apps/system/components/MemoryManager/kata-memory-manager/src/memory_manager/mod.rs @@ -47,7 +47,7 @@ impl UntypedSlab { size_bits: ut.size_bits(), _base_paddr: ut.paddr, _last_paddr: ut.paddr + (1 << ut.size_bits()), - cptr: cptr, + cptr, _next_paddr: ut.paddr, } } @@ -89,7 +89,7 @@ impl MemoryManager { // Creates a new MemoryManager instance. The allocator is seeded // from the untyped memory descriptors. pub fn new(slots: Range, untypeds: &[seL4_UntypedDesc]) -> Self { - assert!(untypeds.len() > 0); + assert!(!untypeds.is_empty()); assert_eq!(slots.end - slots.start, untypeds.len()); let mut m = MemoryManager { untypeds: SmallVec::new(),