Avoid using current in semop

This commit is contained in:
Yuke Peng
2024-08-25 15:33:33 +08:00
committed by Tate, Hongliang Tian
parent 090b5a1c1d
commit 4b1f2f720e
3 changed files with 21 additions and 16 deletions

View File

@ -99,14 +99,14 @@ pub struct Semaphore {
} }
impl Semaphore { impl Semaphore {
pub fn set_val(&self, val: i32) -> Result<()> { pub fn set_val(&self, val: i32, current_pid: Pid) -> Result<()> {
if !(0..SEMVMX).contains(&val) { if !(0..SEMVMX).contains(&val) {
return_errno!(Errno::ERANGE); return_errno!(Errno::ERANGE);
} }
let mut current_val = self.val.lock(); let mut current_val = self.val.lock();
*current_val = val; *current_val = val;
*self.latest_modified_pid.write() = current!().pid(); *self.latest_modified_pid.write() = current_pid;
self.update_pending_ops(current_val); self.update_pending_ops(current_val);
Ok(()) Ok(())
@ -166,9 +166,10 @@ impl Semaphore {
); );
} }
fn sem_op(&self, sem_buf: SemBuf, timeout: Option<Duration>) -> Result<()> { fn sem_op(&self, sem_buf: SemBuf, timeout: Option<Duration>, ctx: &Context) -> Result<()> {
let mut val = self.val.lock(); let mut val = self.val.lock();
let sem_op = sem_buf.sem_op; let sem_op = sem_buf.sem_op;
let current_pid = ctx.process.pid();
let flags = IpcFlags::from_bits(sem_buf.sem_flags as u32).unwrap(); let flags = IpcFlags::from_bits(sem_buf.sem_flags as u32).unwrap();
if flags.contains(IpcFlags::SEM_UNDO) { if flags.contains(IpcFlags::SEM_UNDO) {
@ -189,7 +190,7 @@ impl Semaphore {
} }
*val = new_val; *val = new_val;
*self.latest_modified_pid.write() = current!().pid(); *self.latest_modified_pid.write() = current_pid;
self.update_otime(); self.update_otime();
self.update_pending_ops(val); self.update_pending_ops(val);
@ -206,14 +207,12 @@ impl Semaphore {
// Add current to pending list // Add current to pending list
let (waiter, waker) = Waiter::new_pair(); let (waiter, waker) = Waiter::new_pair();
let status = Arc::new(AtomicStatus::new(Status::Pending)); let status = Arc::new(AtomicStatus::new(Status::Pending));
let current = current!();
let pid = current.pid();
let pending_op = Box::new(PendingOp { let pending_op = Box::new(PendingOp {
sem_buf, sem_buf,
status: status.clone(), status: status.clone(),
waker: waker.clone(), waker: waker.clone(),
process: Arc::downgrade(&current), process: ctx.posix_thread.weak_process(),
pid, pid: current_pid,
}); });
if sem_op == 0 { if sem_op == 0 {
self.pending_const.lock().push_back(pending_op); self.pending_const.lock().push_back(pending_op);
@ -241,7 +240,7 @@ impl Semaphore {
} else { } else {
self.pending_alters.lock() self.pending_alters.lock()
}; };
pending_ops.retain(|op| op.pid != pid); pending_ops.retain(|op| op.pid != current_pid);
Err(Error::new(Errno::EAGAIN)) Err(Error::new(Errno::EAGAIN))
} }
} }
@ -302,7 +301,12 @@ impl Semaphore {
} }
} }
pub fn sem_op(sem_id: key_t, sem_buf: SemBuf, timeout: Option<Duration>) -> Result<()> { pub fn sem_op(
sem_id: key_t,
sem_buf: SemBuf,
timeout: Option<Duration>,
ctx: &Context,
) -> Result<()> {
debug_assert!(sem_id > 0); debug_assert!(sem_id > 0);
debug!("[semop] sembuf: {:?}", sem_buf); debug!("[semop] sembuf: {:?}", sem_buf);
@ -318,5 +322,5 @@ pub fn sem_op(sem_id: key_t, sem_buf: SemBuf, timeout: Option<Duration>) -> Resu
.clone() .clone()
}; };
sem.sem_op(sem_buf, timeout) sem.sem_op(sem_buf, timeout, ctx)
} }

View File

@ -58,7 +58,7 @@ pub fn sys_semctl(
.get(semnum as usize) .get(semnum as usize)
.ok_or(Error::new(Errno::EINVAL))?; .ok_or(Error::new(Errno::EINVAL))?;
sem.set_val(val)?; sem.set_val(val, ctx.process.pid())?;
sem_set.update_ctime(); sem_set.update_ctime();
Ok(()) Ok(())

View File

@ -13,12 +13,12 @@ use crate::{
time::timespec_t, time::timespec_t,
}; };
pub fn sys_semop(sem_id: i32, tsops: Vaddr, nsops: usize, _ctx: &Context) -> Result<SyscallReturn> { pub fn sys_semop(sem_id: i32, tsops: Vaddr, nsops: usize, ctx: &Context) -> Result<SyscallReturn> {
debug!( debug!(
"[sys_semop] sem_id = {:?}, tsops_vaddr = {:x?}, nsops = {:?}", "[sys_semop] sem_id = {:?}, tsops_vaddr = {:x?}, nsops = {:?}",
sem_id, tsops, nsops sem_id, tsops, nsops
); );
do_sys_semtimedop(sem_id, tsops, nsops, None) do_sys_semtimedop(sem_id, tsops, nsops, None, ctx)
} }
pub fn sys_semtimedop( pub fn sys_semtimedop(
@ -41,7 +41,7 @@ pub fn sys_semtimedop(
)?) )?)
}; };
do_sys_semtimedop(sem_id, tsops, nsops, timeout) do_sys_semtimedop(sem_id, tsops, nsops, timeout, ctx)
} }
fn do_sys_semtimedop( fn do_sys_semtimedop(
@ -49,6 +49,7 @@ fn do_sys_semtimedop(
tsops: Vaddr, tsops: Vaddr,
nsops: usize, nsops: usize,
timeout: Option<Duration>, timeout: Option<Duration>,
ctx: &Context,
) -> Result<SyscallReturn> { ) -> Result<SyscallReturn> {
if sem_id <= 0 || nsops == 0 { if sem_id <= 0 || nsops == 0 {
return_errno!(Errno::EINVAL); return_errno!(Errno::EINVAL);
@ -64,7 +65,7 @@ fn do_sys_semtimedop(
check_sem(sem_id, None, PermissionMode::ALTER)?; check_sem(sem_id, None, PermissionMode::ALTER)?;
} }
sem_op(sem_id, sem_buf, timeout)?; sem_op(sem_id, sem_buf, timeout, ctx)?;
} }
Ok(SyscallReturn::Return(0)) Ok(SyscallReturn::Return(0))