Skip to content

Spacemit legacy loadpatch#9496

Open
sven-ola wants to merge 2 commits intoarmbian:mainfrom
sven-ola:spacemit-legacy-loadpatch
Open

Spacemit legacy loadpatch#9496
sven-ola wants to merge 2 commits intoarmbian:mainfrom
sven-ola:spacemit-legacy-loadpatch

Conversation

@sven-ola
Copy link
Member

@sven-ola sven-ola commented Mar 6, 2026

Description

As stated by @pyavitz in the RV2 Armbian forum thread: the legacy spacemit kernel also needs the load < 2 fix. See https://forum.armbian.com/topic/56846-orange-pi-rv2/page/4/#findComment-233778

This PR also corrects the respective patches for current spacemit kernel: moved wait_for_completion_timeout() -> wait_for_completion_idle_timeout() change to the correct patch file

Tested with a local compile (legacy and current kernel)

Summary by CodeRabbit

  • New Features

    • Added idle-state completion mechanism to improve resource efficiency during kernel operations.
  • Bug Fixes

    • Fixed potential system crashes from uninitialized thread shutdown during device removal and power-down.
  • Performance

    • Optimized thread scheduling to reduce CPU overhead and improve power efficiency with extended loop intervals.

sven-ola added 2 commits March 6, 2026 22:12
…munication kthread

Signed-off-by: Sven-Ola Tuecke <sven-ola@gmx.de>
… communication kthread

Signed-off-by: Sven-Ola Tuecke <sven-ola@gmx.de>
@sven-ola sven-ola requested a review from pyavitz as a code owner March 6, 2026 22:16
@github-actions github-actions bot added size/medium PR with more then 50 and less then 250 lines 05 Milestone: Second quarter release labels Mar 6, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 6, 2026

📝 Walkthrough

Walkthrough

These patches introduce a new kernel API (wait_for_completion_idle_timeout) across two kernel versions (6.18 and 6.6), adjust thread scheduling priority to lower kthread loop execution speed, and add safety checks for thread shutdown in the remote processor driver to prevent stopping uninitialized threads.

Changes

Cohort / File(s) Summary
New wait_for_completion_idle_timeout API
include/linux/completion.h, kernel/sched/completion.c, drivers/remoteproc/k1x-rproc.c
Introduces wait_for_completion_idle_timeout() function declaration and implementation in both 6.18 and 6.6 kernel versions; updates driver call site to use the new API instead of wait_for_completion_timeout().
Thread Scheduling Priority
drivers/remoteproc/k1x-rproc.c
Adjusts thread scheduling priority in kthread functions by changing sched_param initialization to MAX_RT_PRIO - 1 in both 6.18 and 6.6 versions; includes removal of kthread loop block structure in 6.18 variant.
Thread Shutdown Safety
drivers/remoteproc/k1x-rproc.c
Adds null pointer checks for thread pointers before calling kthread_stop() in both spacemit_rproc_remove() and spacemit_rproc_shutdown() functions in 6.6 version.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

🐰 Through kernel threads we gently hop,
With priorities set high and loop speed slow,
A new completion waits, idle and still,
While safety guards prevent the stumble and spill—
Spacemit's speed tamed by scheduler's will! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Spacemit legacy loadpatch' is vague and lacks specificity about the actual changes made. It does not clearly convey the core fix being addressed (CPU load issue from rCPU remoteproc communication kthread). Consider a more descriptive title such as 'Fix high CPU load in spacemit legacy kthread loop' to clearly communicate the primary issue being resolved.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added Needs review Seeking for review Hardware Hardware related like kernel, U-Boot, ... Patches Patches related to kernel, U-Boot, ... labels Mar 6, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
patch/kernel/archive/spacemit-6.18/017-Spacemit-lower-kthread-loop-speed-to-1-sec.patch (1)

19-20: ⚠️ Potential issue | 🟠 Major

This current-kernel patch carries the same RT-priority risk.

Line 20 raises the worker to MAX_RT_PRIO - 1 (max FIFO priority) without any corresponding change to loop timing or wait cadence. The patch title claims "lower kthread loop speed" but only escalates scheduler priority—a large and orthogonal scheduling behavior change for a mailbox thread that could degrade system responsiveness.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@patch/kernel/archive/spacemit-6.18/017-Spacemit-lower-kthread-loop-speed-to-1-sec.patch`
around lines 19 - 20, The patch incorrectly escalates the mailbox kthread to
real-time by setting struct sched_param param = {.sched_priority = MAX_RT_PRIO -
1 }, which is unnecessary and risky; revert the priority change to a safe
default (e.g., .sched_priority = 0) and implement the intended "lower loop
speed" by adding an explicit sleep/wait in the kthread's main loop (use
msleep(), schedule_timeout_interruptible(), or similar) to enforce a ~1s
cadence; update the code paths that call set_scheduler/setparam so they use the
restored param and place the delay inside the mailbox worker loop to achieve the
lower frequency without changing scheduler priority.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@patch/kernel/archive/spacemit-6.6/015-Spacemit-lower-kthread-loop-speed-to-1-sec.patch`:
- Around line 19-20: The change sets struct sched_param param from non-real-time
(0) to MAX_RT_PRIO - 1, which promotes the kthread to the highest FIFO RT
priority; revert this by restoring a non-RT priority (e.g., .sched_priority = 0
or use SCHED_OTHER) for the mailbox kthread so it does not run at MAX_RT_PRIO -
1; update the initialization of param (the struct sched_param named param) and
any related sched_setscheduler call to keep the thread non-realtime rather than
elevating it to MAX_RT_PRIO - 1.

---

Outside diff comments:
In
`@patch/kernel/archive/spacemit-6.18/017-Spacemit-lower-kthread-loop-speed-to-1-sec.patch`:
- Around line 19-20: The patch incorrectly escalates the mailbox kthread to
real-time by setting struct sched_param param = {.sched_priority = MAX_RT_PRIO -
1 }, which is unnecessary and risky; revert the priority change to a safe
default (e.g., .sched_priority = 0) and implement the intended "lower loop
speed" by adding an explicit sleep/wait in the kthread's main loop (use
msleep(), schedule_timeout_interruptible(), or similar) to enforce a ~1s
cadence; update the code paths that call set_scheduler/setparam so they use the
restored param and place the delay inside the mailbox worker loop to achieve the
lower frequency without changing scheduler priority.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3dfd49c0-18ea-4239-95a3-750c4ce045bc

📥 Commits

Reviewing files that changed from the base of the PR and between 1b38e53 and 2046238.

📒 Files selected for processing (5)
  • patch/kernel/archive/spacemit-6.18/017-Spacemit-lower-kthread-loop-speed-to-1-sec.patch
  • patch/kernel/archive/spacemit-6.18/019-Add-a-wait_for_completion_idle_timeout-to-normalize-.patch
  • patch/kernel/archive/spacemit-6.6/015-Spacemit-lower-kthread-loop-speed-to-1-sec.patch
  • patch/kernel/archive/spacemit-6.6/016-Spacemit-prevent-uninitialized-thread-shutdown.patch
  • patch/kernel/archive/spacemit-6.6/017-Add-a-wait_for_completion_idle_timeout-to-normalize-.patch

Comment on lines +19 to +20
- struct sched_param param = {.sched_priority = 0 };
+ struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1 };
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

For Linux sched_setscheduler(..., SCHED_FIFO, ...), what is the valid sched_priorityrange, and what effective priority doesMAX_RT_PRIO - 1 represent?

💡 Result:

  • Valid sched_priority range for sched_setscheduler(..., SCHED_FIFO, ...) (user space): 1–99 inclusive on Linux (1 = lowest RT priority, 99 = highest). [1][2]
  • What MAX_RT_PRIO - 1 represents:
    • In the kernel headers, MAX_RT_PRIO is 100, so MAX_RT_PRIO - 1 is 99. [3]
    • If you mean the user-visible POSIX RT priority (param.sched_priority): MAX_RT_PRIO - 1 corresponds to sched_priority = 99 (highest RT priority). [1][2][3]
    • If you mean the kernel’s internal task_struct->prio numbering: RT tasks occupy prio 0..99, and lower prio means higher priority, so prio = 99 is the lowest RT internal priority (whereas the highest RT internal priority is prio = 0). [3][4]

Sources:
[1] sched_setscheduler(2) man page (man7.org) — Linux returns min/max RT priorities 1 and 99 for FIFO/RR. (man7.org)
[2] sched(7) man page (man7.org) — SCHED_FIFO/SCHED_RR priorities range 1 (low) to 99 (high). (man7.org)
[3] Linux kernel header include/linux/sched/prio.hMAX_USER_RT_PRIO 100, MAX_RT_PRIO and note that lower p->prio means higher priority. (android.googlesource.com)
[4] Oracle Linux blog on task priorities — shows mapping between rt_priority and effective kernel prio. (blogs.oracle.com)

Citations:


Avoid pushing this worker to the top FIFO priority.

Line 20 changes scheduling priority from 0 (non-real-time) to MAX_RT_PRIO - 1 (99, the highest FIFO priority). This is a dramatic scheduling behavior change that does not affect the timeout governing how often the loop wakes. For a mailbox kthread, pushing to the top RT priority is high-risk when landed as part of a load-normalization fix.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@patch/kernel/archive/spacemit-6.6/015-Spacemit-lower-kthread-loop-speed-to-1-sec.patch`
around lines 19 - 20, The change sets struct sched_param param from
non-real-time (0) to MAX_RT_PRIO - 1, which promotes the kthread to the highest
FIFO RT priority; revert this by restoring a non-RT priority (e.g.,
.sched_priority = 0 or use SCHED_OTHER) for the mailbox kthread so it does not
run at MAX_RT_PRIO - 1; update the initialization of param (the struct
sched_param named param) and any related sched_setscheduler call to keep the
thread non-realtime rather than elevating it to MAX_RT_PRIO - 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

05 Milestone: Second quarter release Hardware Hardware related like kernel, U-Boot, ... Needs review Seeking for review Patches Patches related to kernel, U-Boot, ... size/medium PR with more then 50 and less then 250 lines

Development

Successfully merging this pull request may close these issues.

1 participant