Skip to content

Commit 8b2e7c9

Browse files
committed
lock allocator in axalloc
1 parent 578cd37 commit 8b2e7c9

2 files changed

Lines changed: 20 additions & 31 deletions

File tree

modules/axalloc/src/lib.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use core::{
1414

1515
use buddy_slab_allocator::{AllocResult, PageAllocator};
1616
use kspin::SpinNoIrq;
17-
use kernel_guard::NoPreemptIrqSave;
1817
use strum::{IntoStaticStr, VariantArray};
1918

2019
pub use buddy_slab_allocator::AddrTranslator;
@@ -81,7 +80,7 @@ impl fmt::Debug for Usages {
8180
/// This is an adapter around the allocator::GlobalAllocator that provides
8281
/// compatibility with the original axalloc API.
8382
pub struct GlobalAllocator {
84-
inner: buddy_slab_allocator::GlobalAllocator<PAGE_SIZE>,
83+
inner: SpinNoIrq<buddy_slab_allocator::GlobalAllocator<PAGE_SIZE>>,
8584
usages: SpinNoIrq<Usages>,
8685
}
8786

@@ -95,7 +94,7 @@ impl GlobalAllocator {
9594
/// Creates an empty [`GlobalAllocator`].
9695
pub const fn new() -> Self {
9796
Self {
98-
inner: buddy_slab_allocator::GlobalAllocator::<PAGE_SIZE>::new(),
97+
inner: SpinNoIrq::new(buddy_slab_allocator::GlobalAllocator::<PAGE_SIZE>::new()),
9998
usages: SpinNoIrq::new(Usages::new()),
10099
}
101100
}
@@ -107,8 +106,7 @@ impl GlobalAllocator {
107106
&self,
108107
translator: &'static dyn buddy_slab_allocator::AddrTranslator,
109108
) {
110-
let _guard = NoPreemptIrqSave::new();
111-
self.inner.set_addr_translator(translator);
109+
self.inner.lock().set_addr_translator(translator);
112110
}
113111

114112
/// Returns the name of the allocator.
@@ -122,8 +120,7 @@ impl GlobalAllocator {
122120
"Initialize global memory allocator, start_vaddr: {}, size: {}",
123121
start_vaddr, size
124122
);
125-
let _guard = NoPreemptIrqSave::new();
126-
if let Err(e) = self.inner.init(start_vaddr, size) {
123+
if let Err(e) = self.inner.lock().init(start_vaddr, size) {
127124
panic!("Failed to initialize allocator: {:?}", e);
128125
}
129126
}
@@ -134,15 +131,13 @@ impl GlobalAllocator {
134131
"Add memory region, start_vaddr: {}, size: {}",
135132
start_vaddr, size
136133
);
137-
let _guard = NoPreemptIrqSave::new();
138-
self.inner.add_memory(start_vaddr, size)
134+
self.inner.lock().add_memory(start_vaddr, size)
139135
}
140136

141137
/// Allocate arbitrary number of bytes. Returns the left bound of the
142138
/// allocated region.
143139
pub fn alloc(&self, layout: Layout) -> AllocResult<NonNull<u8>> {
144-
let _guard = NoPreemptIrqSave::new();
145-
let result = self.inner.alloc(layout);
140+
let result = self.inner.lock().alloc(layout);
146141
if let Ok(_ptr) = result {
147142
self.usages.lock().alloc(UsageKind::RustHeap, layout.size());
148143
}
@@ -151,11 +146,10 @@ impl GlobalAllocator {
151146

152147
/// Gives back the allocated region to the byte allocator.
153148
pub fn dealloc(&self, pos: NonNull<u8>, layout: Layout) {
154-
let _guard = NoPreemptIrqSave::new();
155149
self.usages
156150
.lock()
157151
.dealloc(UsageKind::RustHeap, layout.size());
158-
self.inner.dealloc(pos, layout);
152+
self.inner.lock().dealloc(pos, layout);
159153
}
160154

161155
/// Allocates contiguous pages.
@@ -165,8 +159,7 @@ impl GlobalAllocator {
165159
alignment: usize,
166160
kind: UsageKind,
167161
) -> AllocResult<usize> {
168-
let _guard = NoPreemptIrqSave::new();
169-
let result = self.inner.alloc_pages(num_pages, alignment);
162+
let result = self.inner.lock().alloc_pages(num_pages, alignment);
170163
if let Ok(_addr) = result {
171164
let size = num_pages * PAGE_SIZE;
172165
self.usages.lock().alloc(kind, size);
@@ -181,8 +174,7 @@ impl GlobalAllocator {
181174
alignment: usize,
182175
kind: UsageKind,
183176
) -> AllocResult<usize> {
184-
let _guard = NoPreemptIrqSave::new();
185-
let result = self.inner.alloc_dma32_pages(num_pages, alignment);
177+
let result = self.inner.lock().alloc_dma32_pages(num_pages, alignment);
186178
if let Ok(_addr) = result {
187179
let size = num_pages * PAGE_SIZE;
188180
self.usages.lock().alloc(kind, size);
@@ -192,14 +184,16 @@ impl GlobalAllocator {
192184

193185
/// Allocates contiguous pages starting from the given address.
194186
pub fn alloc_pages_at(
195-
&mut self,
187+
&self,
196188
start: usize,
197189
num_pages: usize,
198190
alignment: usize,
199191
kind: UsageKind,
200192
) -> AllocResult<usize> {
201-
let _guard = NoPreemptIrqSave::new();
202-
let result = self.inner.alloc_pages_at(start, num_pages, alignment);
193+
let result = self
194+
.inner
195+
.lock()
196+
.alloc_pages_at(start, num_pages, alignment);
203197
if let Ok(_addr) = result {
204198
let size = num_pages * PAGE_SIZE;
205199
self.usages.lock().alloc(kind, size);
@@ -209,42 +203,37 @@ impl GlobalAllocator {
209203

210204
/// Gives back the allocated pages starts from `pos` to the page allocator.
211205
pub fn dealloc_pages(&self, pos: usize, num_pages: usize, kind: UsageKind) {
212-
let _guard = NoPreemptIrqSave::new();
213206
let size = num_pages * PAGE_SIZE;
214207
self.usages.lock().dealloc(kind, size);
215-
self.inner.dealloc_pages(pos, num_pages);
208+
self.inner.lock().dealloc_pages(pos, num_pages);
216209
}
217210

218211
/// Returns the number of allocated bytes in the byte allocator.
219212
#[cfg(feature = "tracking")]
220213
pub fn used_bytes(&self) -> usize {
221-
let _guard = NoPreemptIrqSave::new();
222-
let stats = self.inner.get_stats();
214+
let stats = self.inner.lock().get_stats();
223215
stats.heap_bytes + stats.slab_bytes
224216
}
225217

226218
/// Returns the number of available bytes in the byte allocator.
227219
#[cfg(feature = "tracking")]
228220
pub fn available_bytes(&self) -> usize {
229-
let _guard = NoPreemptIrqSave::new();
230221
// The new allocator doesn't have this exact method, so we approximate
231-
let stats = self.inner.get_stats();
222+
let stats = self.inner.lock().get_stats();
232223
stats.free_pages * PAGE_SIZE
233224
}
234225

235226
/// Returns the number of allocated pages in the page allocator.
236227
#[cfg(feature = "tracking")]
237228
pub fn used_pages(&self) -> usize {
238-
let _guard = NoPreemptIrqSave::new();
239-
let stats = self.inner.get_stats();
229+
let stats = self.inner.lock().get_stats();
240230
stats.used_pages
241231
}
242232

243233
/// Returns the number of available pages in the page allocator.
244234
#[cfg(feature = "tracking")]
245235
pub fn available_pages(&self) -> usize {
246-
let _guard = NoPreemptIrqSave::new();
247-
let stats = self.inner.get_stats();
236+
let stats = self.inner.lock().get_stats();
248237
stats.free_pages
249238
}
250239

modules/axruntime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub fn rust_main(cpu_id: usize, arg: usize) -> ! {
226226

227227
#[cfg(feature = "alloc")]
228228
fn init_allocator() {
229-
use axhal::mem::{MemRegionFlags, memory_regions, phys_to_virt, virt_to_phys, VirtAddr};
229+
use axhal::mem::{MemRegionFlags, VirtAddr, memory_regions, phys_to_virt, virt_to_phys};
230230
use memory_addr::MemoryAddr;
231231

232232
info!("Initialize global memory allocator...");

0 commit comments

Comments
 (0)