-
Notifications
You must be signed in to change notification settings - Fork 126
sched: fix ABI issues and add full sched-family test suite #970
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,10 @@ use ax_task::{ | |
| AxCpuMask, current, | ||
| future::{block_on, interruptible, sleep}, | ||
| }; | ||
| use bytemuck::{Pod, Zeroable}; | ||
| use linux_raw_sys::general::{ | ||
| __kernel_clockid_t, CLOCK_MONOTONIC, CLOCK_REALTIME, PRIO_PGRP, PRIO_PROCESS, PRIO_USER, | ||
| SCHED_RR, TIMER_ABSTIME, timespec, | ||
| SCHED_BATCH, SCHED_FIFO, SCHED_IDLE, SCHED_NORMAL, SCHED_RR, TIMER_ABSTIME, timespec, | ||
| }; | ||
| use starry_vm::{VmMutPtr, VmPtr, vm_load, vm_write_slice}; | ||
|
|
||
|
|
@@ -20,6 +21,12 @@ use crate::{ | |
| time::TimeValueLike, | ||
| }; | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Copy, Clone, Pod, Zeroable)] | ||
| struct SchedParam { | ||
| sched_priority: i32, | ||
| } | ||
|
|
||
| pub fn sys_sched_yield() -> AxResult<isize> { | ||
| ax_task::yield_now(); | ||
| Ok(0) | ||
|
|
@@ -107,7 +114,27 @@ pub fn sys_sched_getaffinity(pid: i32, cpusetsize: usize, user_mask: *mut u8) -> | |
| Ok(mask_bytes.len() as _) | ||
| } | ||
|
|
||
| pub fn check_sched_permission(pid: i32) -> AxResult<()> { | ||
| let caller = current().as_thread().cred(); | ||
| let task = get_task_by_sched_pid(pid)?; | ||
| if task.id() == current().id() { | ||
| return Ok(()); | ||
| } | ||
| let target_proc = get_process_data(pid as u32)?; | ||
| let target_cred = process_cred(&target_proc)?; | ||
| if caller.has_cap_sys_nice() | ||
| || caller.euid == target_cred.uid | ||
| || caller.euid == target_cred.euid | ||
| { | ||
| Ok(()) | ||
| } else { | ||
| Err(AxError::OperationNotPermitted) | ||
| } | ||
| } | ||
|
|
||
| pub fn sys_sched_setaffinity(pid: i32, cpusetsize: usize, user_mask: *const u8) -> AxResult<isize> { | ||
| check_sched_permission(pid)?; | ||
| let task = get_task_by_sched_pid(pid)?; | ||
| let size = cpusetsize.min(ax_hal::cpu_num().div_ceil(8)); | ||
| let user_mask = vm_load(user_mask, size)?; | ||
| let mut cpu_mask = AxCpuMask::new(); | ||
|
|
@@ -121,8 +148,6 @@ pub fn sys_sched_setaffinity(pid: i32, cpusetsize: usize, user_mask: *const u8) | |
| if cpu_mask.is_empty() { | ||
| return Err(AxError::InvalidInput); | ||
| } | ||
|
|
||
| let task = get_task_by_sched_pid(pid)?; | ||
| if task.id() == current().id() { | ||
| ax_task::set_current_affinity(cpu_mask); | ||
| } else { | ||
|
|
@@ -141,14 +166,61 @@ fn get_task_by_sched_pid(pid: i32) -> AxResult<ax_task::AxTaskRef> { | |
| } | ||
|
|
||
| pub fn sys_sched_getscheduler(_pid: i32) -> AxResult<isize> { | ||
| Ok(SCHED_RR as _) | ||
| let task = get_task_by_sched_pid(_pid)?; | ||
| Ok(task.sched_policy() as isize) | ||
| } | ||
|
|
||
| pub fn sys_sched_setscheduler(_pid: i32, _policy: i32, _param: *const ()) -> AxResult<isize> { | ||
| check_sched_permission(_pid)?; | ||
| let task = get_task_by_sched_pid(_pid)?; | ||
| if _param.is_null() { | ||
| return Err(AxError::InvalidInput); | ||
| } | ||
| let user_param = vm_load::<SchedParam>(_param.cast(), 1)?; | ||
| let user_param = user_param[0]; | ||
| let mut policy = _policy as u32; | ||
| const SCHED_RESET_ON_FORK: u32 = 0x40000000; | ||
| let _reset_on_fork = (policy & SCHED_RESET_ON_FORK) != 0; | ||
| policy &= !SCHED_RESET_ON_FORK; | ||
| let prio = user_param.sched_priority; | ||
| match policy { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议补充注释说明 // TODO: SCHED_RESET_ON_FORK — 已解析,待后续 fork 时实现继承逻辑这样后续开发者能明确知道这是待完成项而非遗漏。 |
||
| SCHED_NORMAL | SCHED_FIFO | SCHED_RR | SCHED_BATCH | SCHED_IDLE => {} | ||
| _ => return Err(AxError::InvalidInput), | ||
| } | ||
| match policy { | ||
| SCHED_NORMAL | SCHED_BATCH | SCHED_IDLE => { | ||
| if prio != 0 { | ||
| return Err(AxError::InvalidInput); | ||
| } | ||
| } | ||
| SCHED_FIFO | SCHED_RR => { | ||
| if !(1..=99).contains(&prio) { | ||
| return Err(AxError::InvalidInput); | ||
| } | ||
| } | ||
| _ => unreachable!(), | ||
| } | ||
| task.set_sched_policy(policy as i32); | ||
| task.set_sched_priority(prio); | ||
| Ok(0) | ||
| } | ||
|
|
||
| pub fn sys_sched_getparam(_pid: i32, _param: *mut ()) -> AxResult<isize> { | ||
| let task = get_task_by_sched_pid(_pid)?; | ||
| if _param.is_null() { | ||
| return Err(AxError::InvalidInput); | ||
| } | ||
| let param = SchedParam { | ||
| sched_priority: task.sched_priority(), | ||
| }; | ||
| let ptr = _param as *mut SchedParam; | ||
| unsafe { | ||
| let bytes = core::slice::from_raw_parts( | ||
| ¶m as *const SchedParam as *const u8, | ||
| core::mem::size_of::<SchedParam>(), | ||
| ); | ||
| vm_write_slice(ptr as *mut u8, bytes)?; | ||
| } | ||
| Ok(0) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
test-suit/starryos/normal/qemu-smp4/test-sched-family/c/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| cmake_minimum_required(VERSION 3.20) | ||
| project(test-sched-family C) | ||
| set(CMAKE_C_STANDARD 11) | ||
| set(CMAKE_C_STANDARD_REQUIRED ON) | ||
| set(CMAKE_C_EXTENSIONS OFF) | ||
| add_executable(test-sched-family src/main.c) | ||
| target_include_directories(test-sched-family PRIVATE src) | ||
| target_compile_options(test-sched-family PRIVATE -Wall -Wextra -Werror) | ||
| install(TARGETS test-sched-family RUNTIME DESTINATION usr/bin) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok(()),跳过所有权限检查。在 Linux 中,
sched_setscheduler对SCHED_FIFO/SCHED_RR实时调度策略要求CAP_SYS_NICE权限,即使目标是自身进程也不例外(除非进程拥有非零RLIMIT_RTPRIO,而 StarryOS 尚未实现该限制)。当前实现允许任何非特权进程通过
sched_setscheduler(0, SCHED_FIFO, &{.sched_priority=99})将自身提升为最高优先级实时进程,构成权限提升风险。建议修复方向:
不在
check_sched_permission中修改自检查逻辑(因为setaffinity对自身确实不需要权限),而是在sys_sched_setscheduler的 policy 校验阶段,对SCHED_FIFO/SCHED_RR额外检查caller.has_cap_sys_nice()。