Skip to content

Commit

Permalink
mm/alloc: acquire ROOT_MEM lock for the whole function in layout_from…
Browse files Browse the repository at this point in the history
…_ptr()

In layout_from_ptr() we are acquiring the root memory lock twice back
to back. Simply acquire it once and hold it for the whole function -
that is the only way we can guarantee that, if the page we are
looking at is a slab page, the contained pointer will be valid when
we dereference it.

Signed-off-by: Carlos López <[email protected]>
  • Loading branch information
00xc committed Apr 19, 2024
1 parent ce22b2e commit 55e03f9
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions kernel/src/mm/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1631,10 +1631,9 @@ pub fn layout_from_size(size: usize) -> Layout {
pub fn layout_from_ptr(ptr: *mut u8) -> Option<Layout> {
let va = VirtAddr::from(ptr);

let Ok(pfn) = ROOT_MEM.lock().get_pfn(va) else {
return None;
};
let info = ROOT_MEM.lock().read_page_info(pfn);
let root = ROOT_MEM.lock();
let pfn = root.get_pfn(va).ok()?;
let info = root.read_page_info(pfn);

match info {
PageInfo::Allocated(ai) => {
Expand Down

0 comments on commit 55e03f9

Please sign in to comment.