Skip to content

Commit dd33b58

Browse files
fix(ipc): make the SysV SHM manager lock a mutex (#2720)
* fix(ipc): make the SysV SHM manager lock a mutex * test(ipc): give SysV SHM test resources per-case lifetimes Signed-off-by: longjin <longjin@dragonos.org> --------- Signed-off-by: mizuk1 <mizuk1@mzk1.dev> Signed-off-by: longjin <longjin@dragonos.org> Co-authored-by: longjin <longjin@dragonos.org>
1 parent 7635de4 commit dd33b58

3 files changed

Lines changed: 103 additions & 4 deletions

File tree

‎kernel/src/process/namespace/ipc_namespace.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use alloc::sync::{Arc, Weak};
22

33
use crate::ipc::sem::SemManager;
44
use crate::ipc::shm::ShmManager;
5-
use crate::libs::spinlock::SpinLock;
5+
use crate::libs::{mutex::Mutex, spinlock::SpinLock};
66
use crate::process::namespace::{
77
nsproxy::NsCommon, user_namespace::UserNamespace, NamespaceOps, NamespaceType,
88
};
@@ -21,7 +21,7 @@ pub struct IpcNamespace {
2121
pub user_ns: Arc<UserNamespace>,
2222

2323
/// SysV SHM manager (phase one: per-namespace SHM only)
24-
pub shm: SpinLock<ShmManager>,
24+
pub shm: Mutex<ShmManager>,
2525
/// SysV semaphore manager
2626
pub sem: SpinLock<SemManager>,
2727
}
@@ -38,7 +38,7 @@ impl IpcNamespace {
3838
ns_common: NsCommon::new(0, NamespaceType::Ipc),
3939
self_ref: weak_self.clone(),
4040
user_ns: crate::process::namespace::user_namespace::INIT_USER_NAMESPACE.clone(),
41-
shm: SpinLock::new(ShmManager::new()),
41+
shm: Mutex::new(ShmManager::new()),
4242
sem: SpinLock::new(SemManager::new()),
4343
})
4444
}
@@ -58,7 +58,7 @@ impl IpcNamespace {
5858
ns_common: NsCommon::new(self.ns_common.level + 1, NamespaceType::Ipc),
5959
self_ref: weak_self.clone(),
6060
user_ns,
61-
shm: SpinLock::new(ShmManager::new()),
61+
shm: Mutex::new(ShmManager::new()),
6262
sem: SpinLock::new(SemManager::new()),
6363
})
6464
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <atomic>
4+
#include <cerrno>
5+
#include <cstdlib>
6+
#include <pthread.h>
7+
#include <sys/ipc.h>
8+
#include <sys/mman.h>
9+
#include <sys/shm.h>
10+
11+
namespace {
12+
13+
constexpr size_t kPageSize = 4096;
14+
constexpr size_t kSegmentSize = 16 * kPageSize;
15+
constexpr int kRounds = 50000;
16+
17+
class SysvShmLockFault : public ::testing::Test {
18+
protected:
19+
void SetUp() override {
20+
id_ = shmget(IPC_PRIVATE, kSegmentSize, IPC_CREAT | 0600);
21+
ASSERT_GE(id_, 0);
22+
addr_ = static_cast<char*>(shmat(id_, nullptr, 0));
23+
ASSERT_NE(addr_, reinterpret_cast<char*>(-1));
24+
ASSERT_EQ(0, pthread_create(&worker_, nullptr, RunWorker, this));
25+
started_ = true;
26+
}
27+
28+
void TearDown() override {
29+
if (started_) {
30+
stop_.store(true);
31+
int error = pthread_join(worker_, nullptr);
32+
if (error != 0) {
33+
// The worker may still reference this fixture. Process exit drops the mapping.
34+
ADD_FAILURE() << "pthread_join failed with error " << error;
35+
EXPECT_EQ(0, shmctl(id_, IPC_RMID, nullptr));
36+
std::abort();
37+
}
38+
EXPECT_EQ(0, worker_error_);
39+
EXPECT_TRUE(worked_);
40+
}
41+
if (addr_ != reinterpret_cast<char*>(-1)) {
42+
EXPECT_EQ(0, shmdt(addr_));
43+
}
44+
if (id_ >= 0) {
45+
EXPECT_EQ(0, shmctl(id_, IPC_RMID, nullptr));
46+
}
47+
}
48+
49+
int id_ = -1;
50+
51+
private:
52+
static void* RunWorker(void* arg) {
53+
static_cast<SysvShmLockFault*>(arg)->AccessMapping();
54+
return nullptr;
55+
}
56+
57+
void AccessMapping() {
58+
while (!stop_.load()) {
59+
for (size_t offset = 0; offset < kSegmentSize; offset += kPageSize) {
60+
addr_[offset] = 1;
61+
}
62+
if (madvise(addr_, kSegmentSize, MADV_DONTNEED) != 0) {
63+
worker_error_ = errno;
64+
return;
65+
}
66+
worked_ = true;
67+
}
68+
}
69+
70+
char* addr_ = reinterpret_cast<char*>(-1);
71+
pthread_t worker_{};
72+
bool started_ = false;
73+
std::atomic<bool> stop_{false};
74+
// Only read after pthread_join synchronizes with the worker.
75+
int worker_error_ = 0;
76+
bool worked_ = false;
77+
};
78+
79+
TEST_F(SysvShmLockFault, LockUnlockWhileSegmentFaults) {
80+
for (int round = 0; round < kRounds; ++round) {
81+
ASSERT_EQ(0, shmctl(id_, SHM_LOCK, nullptr));
82+
ASSERT_EQ(0, shmctl(id_, SHM_UNLOCK, nullptr));
83+
}
84+
}
85+
86+
TEST_F(SysvShmLockFault, ShmInfoWhileSegmentFaults) {
87+
for (int round = 0; round < kRounds; ++round) {
88+
struct shm_info info = {};
89+
ASSERT_GE(shmctl(0, SHM_INFO, reinterpret_cast<struct shmid_ds*>(&info)), 0);
90+
}
91+
}
92+
93+
} // namespace
94+
95+
int main(int argc, char** argv) {
96+
::testing::InitGoogleTest(&argc, argv);
97+
return RUN_ALL_TESTS();
98+
}

‎user/apps/tests/dunitest/whitelist.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,4 @@ normal/tty_fionread
329329
normal/bpf_map_lookup_fault
330330
normal/cpu_topology_sysfs
331331
normal/arch_prctl_fault
332+
normal/sysv_shm_lock_fault

0 commit comments

Comments
 (0)