Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[closes #354, #258] myproc()의 사용을 줄이고, usertrap()에서 syscall 함수로 proc전달 #385

Merged
60 commits merged into from
Feb 4, 2021

Conversation

anemoneflower
Copy link
Collaborator

@anemoneflower anemoneflower commented Jan 29, 2021

주요 변경사항입니다.

  • myproc()Kernel의 메소드로 이동
  • struct ExecutingProc 추가: *mut Proc을 감싸는 struct
    • kernel().myexproc() 추가: myproc()에서 Proc이 존재함을 확인하고 ExecutingProc을 리턴
    • usertrap()에서 sys_* 함수들에 ExecutingProc을 전달
  • proc이 null이 아니어야 하는 경우에는 myproc()의 사용을 모두 myexproc()으로 수정했습니다.

@anemoneflower anemoneflower marked this pull request as draft January 29, 2021 06:22
@anemoneflower anemoneflower changed the title WIP: Exeproc [closes #354] myproc()의 사용을 줄이고, usertrap()에서 syscall 함수로 proc전달 Feb 1, 2021
@anemoneflower anemoneflower self-assigned this Feb 1, 2021
@anemoneflower anemoneflower changed the title [closes #354] myproc()의 사용을 줄이고, usertrap()에서 syscall 함수로 proc전달 [closes #354, #258] myproc()의 사용을 줄이고, usertrap()에서 syscall 함수로 proc전달 Feb 1, 2021
@anemoneflower anemoneflower marked this pull request as ready for review February 1, 2021 15:56
kernel-rs/src/exec.rs Outdated Show resolved Hide resolved
kernel-rs/src/exec.rs Outdated Show resolved Hide resolved
kernel-rs/src/fs/path.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/syscall.rs Outdated Show resolved Hide resolved
kernel-rs/src/trap.rs Outdated Show resolved Hide resolved
kernel-rs/src/trap.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/fs/inode.rs Outdated Show resolved Hide resolved
}

impl DerefMut for CurrentProc {
fn deref_mut(&mut self) -> &mut Self::Target {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아래는 아까 오프라인 미팅에서 말씀드린 내용입니다.

rv6/kernel-rs/src/proc.rs

Lines 946 to 953 in f06956c

for p in &kernel().procs.process_pool {
let mut guard = p.lock();
if guard.deref_info().state == Procstate::RUNNABLE {
// Switch to chosen process. It is the process's job
// to release its lock and then reacquire it
// before jumping back to us.
guard.deref_mut_info().state = Procstate::RUNNING;
unsafe { (*c).proc = p as *const _ as *mut _ };

Cpuproc*mut Proc인데, schedule에서 &Proc*mut Proc으로 변환하여 proc에 저장하고 있습니다. &T*mut T로 변환한 다음, 값을 쓰거나 &mut T로 변환하면 UB이기 때문에 현재 구현에 다소 문제가 있어 보입니다. Cpuproc*const Proc으로 바꾸고 Proc은 항상 &mut Proc 대신 &Proc으로만 사용되게 해야 할 것 같습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*const Proc으로 사용하기 위해 다음과 같은 변경사항을 적용했습니다.

  • Cpu.proc, ProcessSystem.initial_proc, Proc.parent 에서 *mut Proc대신 *const Proc사용
  • CurrentProc에서 *const Proc사용 (NonNull은 *mut T를 받기 때문에 사용하지 않았습니다.)
  • CurrentProc, ProcDerefMut 대신 Procderef_mut_data() 구현
  • Proc.nameProcData의 field로 이동 (exec에서 디버깅을 위해 &mut p.name을 사용하는데, 이때 Proc이 mutable 하게 참조되기 때문입니다. )

    rv6/kernel-rs/src/exec.rs

    Lines 188 to 193 in 551a6b1

    let p_name = &mut p.name;
    let len = cmp::min(p_name.len(), name.len());
    p_name[..len].copy_from_slice(&name[..len]);
    if len < p_name.len() {
    p_name[len] = 0;
    }

Copy link
Collaborator Author

@anemoneflower anemoneflower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 반영했습니다 :)

  • CurrentProc, ProcDeref 구현 추가
  • CurentProc에 lifetime 추가
  • myproc() 대신 CurrentProcOption리턴
  • Cpu.proc, ProcessSystem.initial_proc, Proc.parent 에서 *mut Proc대신 *const Proc사용 (관련해서 구체적인 내용은 아래 코멘트에 있습니다.)

kernel-rs/src/fs/path.rs Outdated Show resolved Hide resolved
}

impl DerefMut for CurrentProc {
fn deref_mut(&mut self) -> &mut Self::Target {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*const Proc으로 사용하기 위해 다음과 같은 변경사항을 적용했습니다.

  • Cpu.proc, ProcessSystem.initial_proc, Proc.parent 에서 *mut Proc대신 *const Proc사용
  • CurrentProc에서 *const Proc사용 (NonNull은 *mut T를 받기 때문에 사용하지 않았습니다.)
  • CurrentProc, ProcDerefMut 대신 Procderef_mut_data() 구현
  • Proc.nameProcData의 field로 이동 (exec에서 디버깅을 위해 &mut p.name을 사용하는데, 이때 Proc이 mutable 하게 참조되기 때문입니다. )

    rv6/kernel-rs/src/exec.rs

    Lines 188 to 193 in 551a6b1

    let p_name = &mut p.name;
    let len = cmp::min(p_name.len(), name.len());
    p_name[..len].copy_from_slice(&name[..len]);
    if len < p_name.len() {
    p_name[len] = 0;
    }

kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/exec.rs Outdated Show resolved Hide resolved
kernel-rs/src/exec.rs Outdated Show resolved Hide resolved
kernel-rs/src/file.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/sleeplock.rs Show resolved Hide resolved
kernel-rs/src/trap.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
pub fn current_proc(&self) -> Option<CurrentProc<'_>> {
unsafe { push_off() };
let c = self.mycpu();
let p = unsafe { (*c).proc };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CurrentProc의 lifetime에 대한 invariant를 보장할 수 있도록 (*c).proc이 nonnull이면 &self의 lifetime 동안 살아있다는 설명이 추가되어야 할 것 같습니다. 이를 위해서는 Kernel의 invariant로 cpus안에 있는 proc들의 lifetime에 대한 조건이 기술되어야 할 것으로 보입니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8cc4aab
반영했습니다. 제가 맞게 이해한 것인지 궁금합니다. @Medowhill

Copy link
Collaborator Author

@anemoneflower anemoneflower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코멘트 반영을 완료했습니다. (commit이 많은 것 같아서 코멘트에 해당하는 commit을 기록했습니다.)

주요 변경점입니다.

  • syscall관련 함수들은 모두 CurrentProc을 input으로 받아서 사용합니다.
  • CurrentProc과 관련된 safety invarient를 추가했습니다.
  • &mut Proc대신 &Proc을 사용하도록 수정했습니다.
  • 추가적으로 ProcProcGuard에서 Deref를 사용할 수 있는 일부 코드를 수정했습니다.

kernel-rs/src/exec.rs Outdated Show resolved Hide resolved
kernel-rs/src/exec.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Show resolved Hide resolved
pub fn current_proc(&self) -> Option<CurrentProc<'_>> {
unsafe { push_off() };
let c = self.mycpu();
let p = unsafe { (*c).proc };
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8cc4aab
반영했습니다. 제가 맞게 이해한 것인지 궁금합니다. @Medowhill

kernel-rs/src/proc.rs Show resolved Hide resolved
kernel-rs/src/sleeplock.rs Show resolved Hide resolved
kernel-rs/src/proc.rs Show resolved Hide resolved
Copy link
Member

@jeehoonkang jeehoonkang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굉장히 잘 해주셨습니다. 감사합니다.
마이너한 코멘트 몇개 더 남깁니다.

kernel-rs/src/kernel.rs Outdated Show resolved Hide resolved
kernel-rs/src/proc.rs Show resolved Hide resolved
kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
@@ -453,12 +499,6 @@ impl Deref for ProcGuard {
}
}

impl DerefMut for ProcGuard {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 지워진 까닭은?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DerefMut&mut Proc을 리턴하기 때문에 지웠습니다.

관련된 재민님의 코멘트입니다.
#385 (comment)
#385 (comment)

kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
/// `inner` is current Cpu's proc, this means it's state is `RUNNING`.
/// `Proc` lives during lifetime `'p`.
pub struct CurrentProc<'p> {
inner: *const Proc,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위의 invariant가 의미하는 것이 결국 inner'p 동안 유효한 Proc을 가리킨다는 것이니, 지금처럼 inner: *const Proc으로 raw pointer를 사용하기보다는 그냥 inner: &'p Proc으로 해도 되지 않을까 싶습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kernel-rs/src/proc.rs Outdated Show resolved Hide resolved
kernel-rs/src/kernel.rs Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@anemoneflower anemoneflower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 반영했습니다 :)

주요 변경점입니다.

  • CurrentProc*const Proc대신 &'p Procinner로 가집니다.
  • 코멘트주신 내용을 반영하여 invariant 관련 주석을 수정했습니다.
    • CurrentProcProc의 포인터를 가지지 않기 때문에 non-null pointer와 관련된 주석은 삭제했습니다.

kernel-rs/src/proc.rs Show resolved Hide resolved
@@ -453,12 +499,6 @@ impl Deref for ProcGuard {
}
}

impl DerefMut for ProcGuard {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DerefMut&mut Proc을 리턴하기 때문에 지웠습니다.

관련된 재민님의 코멘트입니다.
#385 (comment)
#385 (comment)

/// `inner` is current Cpu's proc, this means it's state is `RUNNING`.
/// `Proc` lives during lifetime `'p`.
pub struct CurrentProc<'p> {
inner: *const Proc,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Medowhill
Copy link
Collaborator

bors retry

@Medowhill
Copy link
Collaborator

bors r+

@ghost
Copy link

ghost commented Feb 4, 2021

Build succeeded:

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

myproc() refactoring ProcExecutionGuard
5 participants