Skip to content

Commit 968a9aa

Browse files
authored
Merge pull request coconut-svsm#825 from msft-jlange/ptalloc
mm/pagetable: reduce stack consumption
2 parents 231401e + 7599e7d commit 968a9aa

2 files changed

Lines changed: 14 additions & 19 deletions

File tree

kernel/src/address.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use core::slice;
1414

1515
use verus_stub::*;
1616

17+
use zerocopy::FromZeros;
18+
1719
#[cfg(verus_keep_ghost)]
1820
include!("address.verus.rs");
1921

@@ -188,7 +190,7 @@ pub trait Address: Copy + From<InnerAddr> + Into<InnerAddr> + Ord {
188190
}
189191

190192
#[verus_verify]
191-
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
193+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, FromZeros)]
192194
#[repr(transparent)]
193195
pub struct PhysAddr(InnerAddr);
194196

kernel/src/mm/pagetable.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ use bitflags::bitflags;
2424
use core::cmp;
2525
use core::ops::{Index, IndexMut};
2626
use core::ptr::NonNull;
27-
28-
extern crate alloc;
29-
use alloc::boxed::Box;
27+
use zerocopy::FromZeros;
3028

3129
/// Number of entries in a page table (4KB/8B).
3230
const ENTRY_COUNT: usize = 512;
@@ -211,7 +209,7 @@ impl PagingMode {
211209

212210
/// Represents a page table entry.
213211
#[repr(C)]
214-
#[derive(Copy, Clone, Debug, Default)]
212+
#[derive(Copy, Clone, Debug, FromZeros)]
215213
pub struct PTEntry(PhysAddr);
216214

217215
impl PTEntry {
@@ -391,7 +389,7 @@ impl PTEntry {
391389

392390
/// A pagetable page with multiple entries.
393391
#[repr(C)]
394-
#[derive(Debug)]
392+
#[derive(Debug, FromZeros)]
395393
pub struct PTPage {
396394
entries: [PTEntry; ENTRY_COUNT],
397395
}
@@ -404,7 +402,7 @@ impl PTPage {
404402
///
405403
/// Returns [`SvsmError`] if the page cannot be allocated.
406404
fn alloc() -> Result<(&'static mut Self, PhysAddr), SvsmError> {
407-
let page = PageBox::try_new(PTPage::default())?;
405+
let page: PageBox<Self> = PageBox::try_new_zeroed()?;
408406
let paddr = virt_to_phys(page.vaddr());
409407
Ok((PageBox::leak(page), paddr))
410408
}
@@ -438,13 +436,6 @@ impl PTPage {
438436
}
439437
}
440438

441-
impl Default for PTPage {
442-
fn default() -> Self {
443-
let entries = [PTEntry::default(); ENTRY_COUNT];
444-
PTPage { entries }
445-
}
446-
}
447-
448439
/// Can be used to access page table entries by index.
449440
impl Index<usize> for PTPage {
450441
type Output = PTEntry;
@@ -514,7 +505,7 @@ impl PageFrame {
514505

515506
/// Page table structure containing a root page with multiple entries.
516507
#[repr(C)]
517-
#[derive(Default, Debug)]
508+
#[derive(Debug, FromZeros)]
518509
pub struct PageTable {
519510
root: PTPage,
520511
}
@@ -544,7 +535,7 @@ impl PageTable {
544535
/// # Errors
545536
/// Returns [`SvsmError`] if the page cannot be allocated.
546537
pub fn allocate_new() -> Result<PageBox<Self>, SvsmError> {
547-
let mut pgtable = PageBox::try_new(PageTable::default())?;
538+
let mut pgtable: PageBox<Self> = PageBox::try_new_zeroed()?;
548539
let paddr = virt_to_phys(pgtable.vaddr());
549540

550541
// Set the self-map entry.
@@ -1273,7 +1264,7 @@ impl PageTable {
12731264
}
12741265

12751266
/// Represents a sub-tree of a page-table which can be mapped at a top-level index
1276-
#[derive(Default, Debug)]
1267+
#[derive(Debug, FromZeros)]
12771268
struct RawPageTablePart {
12781269
page: PTPage,
12791270
}
@@ -1514,7 +1505,7 @@ impl Drop for RawPageTablePart {
15141505
#[derive(Debug)]
15151506
pub struct PageTablePart {
15161507
/// The root of the page-table sub-tree
1517-
raw: Option<Box<RawPageTablePart>>,
1508+
raw: Option<PageBox<RawPageTablePart>>,
15181509
/// The top-level index this PageTablePart is populated at
15191510
idx: usize,
15201511
}
@@ -1541,7 +1532,9 @@ impl PageTablePart {
15411532
}
15421533

15431534
fn get_or_init_mut(&mut self) -> &mut RawPageTablePart {
1544-
self.raw.get_or_insert_with(Box::default)
1535+
self.raw.get_or_insert_with(|| {
1536+
PageBox::try_new_zeroed().expect("Failed to allocate page table page")
1537+
})
15451538
}
15461539

15471540
fn get_mut(&mut self) -> Option<&mut RawPageTablePart> {

0 commit comments

Comments
 (0)