Skip to content
This repository was archived by the owner on Jul 17, 2025. It is now read-only.

Commit 0091044

Browse files
committed
prevent mixing shmem affinities in realloc and dealloc
1 parent f101f0a commit 0091044

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

kernel/src/memory/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ unsafe impl GlobalAlloc for KernelAllocator {
631631
|pcm| {
632632
#[cfg(feature = "rackscale")]
633633
{
634-
use crate::transport::shmem::{is_shmem_addr, SHMEM_INITIALIZED};
634+
use crate::transport::shmem::{is_shmem_addr, is_shmem_addr_with_affinity, SHMEM_INITIALIZED};
635635
use core::sync::atomic::Ordering;
636636

637637
// If shmem is not initialized, do not force it.
@@ -640,10 +640,10 @@ unsafe impl GlobalAlloc for KernelAllocator {
640640
let affinity = { pcm.physical_memory.borrow().affinity };
641641
if is_shmem_addr(ptr as u64, false, false) {
642642
panic!("Should not be trying to dealloc non-kernel mapped shmem in kernel dealloc");
643-
} else if is_shmem_affinity(affinity) && !is_shmem_addr(ptr as u64, false, true) {
644-
log::error!("Trying to deallocate non-shmem into shmem allocator - losing this memory. Oh well.");
643+
} else if is_shmem_affinity(affinity) && !is_shmem_addr_with_affinity(ptr as u64, affinity, true) {
644+
log::error!("Trying to deallocate memory not in shmem affinity into shmem allocator - losing this memory. Oh well.");
645645
return;
646-
} else if !is_shmem_affinity(affinity) && (is_shmem_addr(ptr as u64, false, true)){
646+
} else if !is_shmem_affinity(affinity) && is_shmem_addr(ptr as u64, false, true) {
647647
log::error!("Trying to deallocate shmem into non-shmem allocator - losing this memory. Oh well.");
648648
return;
649649
}
@@ -743,18 +743,18 @@ unsafe impl GlobalAlloc for KernelAllocator {
743743
} else {
744744
#[cfg(feature = "rackscale")]
745745
{
746-
use crate::transport::shmem::{is_shmem_addr, SHMEM_INITIALIZED};
746+
use crate::transport::shmem::{is_shmem_addr, is_shmem_addr_with_affinity, SHMEM_INITIALIZED};
747747
use core::sync::atomic::Ordering;
748748

749749
// If shmem is not initialized, do not force it.
750750
if 0 != SHMEM_INITIALIZED.load(Ordering::SeqCst) {
751751
let affinity = { pcm.physical_memory.borrow().affinity };
752752
if is_shmem_addr(ptr as u64, false, false) {
753753
panic!("Should not be trying to realloc non-kernel mapped shmem in kernel dealloc");
754-
} else if is_shmem_affinity(affinity) && !is_shmem_addr(ptr as u64, false, true) {
754+
} else if is_shmem_affinity(affinity) && !is_shmem_addr_with_affinity(ptr as u64, affinity, true) {
755755
// TODO(rackscale): should switch to non-shmem affinity for alloc below.
756756
// TODO(rackscale): check if shmem is a match for id?
757-
panic!("Trying to realloc non-shmem using shmem allocator");
757+
panic!("Trying to realloc shmem to wrong or non- shmem allocator");
758758
} else if !is_shmem_affinity(affinity) && is_shmem_addr(ptr as u64, false, true) {
759759
// TODO(rackscale): should switch to use shmem affinity for alloc below.
760760
// TODO(rackscale): check if shmem is a match for id?

kernel/src/transport/shmem.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::pci::claim_devices;
2222
use {
2323
crate::cmdline::{Mode, Transport},
2424
crate::error::{KError, KResult},
25+
crate::memory::shmem_affinity::shmem_affinity_to_mid,
2526
alloc::boxed::Box,
2627
kpi::system::MachineId,
2728
rpc::rpc::MAX_BUFF_LEN,
@@ -444,6 +445,15 @@ pub(crate) fn is_shmem_addr(addr: u64, is_affinity: bool, is_kaddr: bool) -> boo
444445
}
445446
}
446447

448+
#[cfg(feature = "rackscale")]
449+
#[inline(always)]
450+
pub(crate) fn is_shmem_addr_with_affinity(addr: u64, affinity: NodeId, is_kaddr: bool) -> bool {
451+
let offset = if is_kaddr { KERNEL_BASE } else { 0 };
452+
let mid = shmem_affinity_to_mid(affinity);
453+
let frame = get_affinity_shmem_by_mid(mid);
454+
addr >= frame.base.as_u64() + offset && addr < frame.base.as_u64() + offset + frame.size as u64
455+
}
456+
447457
#[cfg(feature = "rackscale")]
448458
pub(crate) fn get_shmem_index_by_addr(addr: u64, is_kaddr: bool) -> Option<usize> {
449459
let offset = if is_kaddr { KERNEL_BASE } else { 0 };

0 commit comments

Comments
 (0)