Skip to content

sched: Fix and centralize sporadic parameter validation - #20084

Open
yushuailong wants to merge 4 commits into
apache:masterfrom
yushuailong:fix/sched-sporadic
Open

sched: Fix and centralize sporadic parameter validation#20084
yushuailong wants to merge 4 commits into
apache:masterfrom
yushuailong:fix/sched-sporadic

Conversation

@yushuailong

Copy link
Copy Markdown
Contributor

Summary

  • Fix the inverted replenishment period validation in sched_setparam().
  • Validate sched_priority before modifying sporadic scheduler state.
  • Centralize sporadic parameter validation and tick conversion.
  • Apply consistent validation to sched_setparam(), sched_setscheduler(), and pthread_create().
  • Propagate scheduler setup failures from the spawn path.

The sched_setparam() validation condition was inverted during an earlier refactoring, causing valid sporadic parameters to be rejected and invalid parameters to be applied.

The sporadic parameter checks were also duplicated across several paths and were inconsistent. In particular, pthread_create() did not validate sched_ss_max_repl and did not enforce the scheduler implementation's 50 percent duty-cycle restriction.

This change introduces nxsched_validate_sporadic() as the common validation path. It also performs validation before modifying sporadic state, preventing failed calls from leaving partially updated scheduling parameters or timers.

Impact

Bug fix only; no new features.

Testing

  • Host: macOS 26.6.2 ARM64, Apple Clang 21.0.0.
  • Target: sim:ostest with CONFIG_SCHED_SPORADIC=y, CONFIG_SCHED_SPORADIC_MAXREPL=3, and CONFIG_PRIORITY_INHERITANCE=y.
  • Build: make completes successfully with LD: nuttx.
  • Runtime: ran ./nuttx; all enabled OSTest subtests completed without assertions or unexpected errors.
  • Static checks: nxstyle, checkpatch, and commit-message checks pass.

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Sep 7, 2026
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

arduino-mega2560

  • flash: .text +16 B (+0.0%, 65,120 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.text +12 B (+0.0%, 124,932 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +12 B (+0.0%, 89,136 B / 3,342,304 B, total: 3% used)

mirtoo

  • kseg0_progmem: .text +20 B (+0.0%, 67,776 B / 131,072 B, total: 52% used)

qemu-armv8a

  • Code: .text.nxsched_set_param +16 B, .text.spawn_execattrs +4 B (+0.0%, 336,168 B)

qemu-intel64

  • Code: .text +16 B (+0.0%, 8,659,797 B)

rx65n-rsk2mb

  • ROM: .text +16 B (+0.0%, 86,896 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +64 B (+0.0%, 367,536 B)

stm32-nucleo-f103rb

  • flash: .text +8 B (+0.0%, 34,564 B / 131,072 B, total: 26% used)
    No memory changes detected for:
  • hifive1-revb

Commit 2ec7d90 refactored sched_setparam() into set_sporadic_param()
but moved the apply logic into the former reject branch without
inverting the condition.  As a result sched_setparam() rejects valid
sporadic parameters (repl >= 2 * budget) with EINVAL and accepts
invalid ones, tripping the DEBUGASSERT in sched_sporadic.c or wrapping
the replenishment calculation.

Invert the condition to match process_sporadic() in sched_setscheduler.c.

Fixes: 2ec7d90 ("sched_setparam.c: coverity HIS_metric_violation: RETURN")
Assisted-by: OpenAI Codex
Signed-off-by: yushuailong <yyyusl@qq.com>
nxsched_set_param() applied the sporadic parameters and restarted the
replenishment timer in set_sporadic_param() before nxsched_reprioritize()
rejected an out-of-range priority with EINVAL, leaving stale sporadic
state (e.g. a truncated hi_priority) behind on failure.

Validate sched_priority up front so the error path is atomic.

Assisted-by: OpenAI Codex
Signed-off-by: yushuailong <yyyusl@qq.com>
Comment thread sched/pthread/pthread_create.c Outdated
Sporadic scheduling parameters are processed independently by sched_setparam(), sched_setscheduler(), and pthread_create(). The three paths currently validate different subsets of the parameters.

In particular, pthread_create() does not validate sched_ss_max_repl and only requires the replenishment period to be greater than the budget, while the scheduler interfaces enforce the implementation's 50 percent duty-cycle limit.

Add nxsched_validate_sporadic() to validate the common parameters and convert the replenishment period and budget to ticks. Use it from all paths that directly initialize or update sporadic scheduler state.

Express the duty-cycle check using division to avoid overflow when doubling a clock_t value.

Assisted-by: OpenAI Codex
Signed-off-by: yushuailong <yyyusl@qq.com>
spawn_execattrs() discards the return value from nxsched_set_scheduler(). As a result, an invalid scheduling policy or parameter is silently ignored and the spawn operation continues with the original scheduling configuration.

Store and return the result from nxsched_set_scheduler() so that the caller can tear down the child task when applying the requested scheduler attributes fails. Update the function comments to describe the existing error return behavior.

Assisted-by: OpenAI Codex
Signed-off-by: yushuailong <yyyusl@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants