Skip to content

kernel (pc): act on a pending task switch on return from LAPIC exceptions - #2377

Open
terminills wants to merge 1 commit into
aros-development-team:masterfrom
terminills:kernel-pc-lapic-exit-v1
Open

terminills wants to merge 1 commit into
aros-development-team:masterfrom
terminills:kernel-pc-lapic-exit-v1

Conversation

@terminills

Copy link
Copy Markdown
Contributor

On pc the LAPIC timer and the IPIs reach core_IRQHandle() as exceptions. It ran their handlers and returned; only the device IRQ path went on to core_ExitInterrupt(), where a quantum that has run out or a pending switch is acted on. An AP takes no other interrupts, so it switched tasks only when the running task made a system call or left Disable(), or when another CPU sent it IPI_RESCHEDULE. A busy task kept its AP, and a second task pinned to that CPU never ran.

With this change the LAPIC exceptions below the system call vector go through core_ExitInterrupt() on the way back to user mode, as device IRQs do. Interrupts are masked again before the frame is restored, because core_ExitInterrupt() can return with them on (SoftIntDispatch() ends in KrnSti()).

Tested

pc-x86_64 (SMP build) and pc-i386, GCC 15.2.0, QEMU/KVM. The guests are full builds of master 5792c0f without and with the commit. The pc-x86_64 SMP build of later master stops in rom/m68kemu, whose layout table names ExecBase fields that the SMP execbase.h replaces; arch/all-pc/kernel, arch/all-pc/exec, rom/kernel and rom/exec are the same at that commit and at this branch's base.

The test/smp programs were run one per boot, 28 boots of each build on x86_64 at 4 and 16 CPUs.

What the change is for:

master with the change
SMP-Preempt, 4 CPUs 1 PASS, 3 FAIL: on each AP the second worker is never scheduled 4 PASS
SMP-Preempt, 16 CPUs CPU 0 PASS, CPUs 1 to 7 FAIL the same way, then the guest stops 16 PASS
SMP-Affinity, 4 CPUs 22 PASS, 2 FAIL (a busy task told to move stays on its CPU) 24 PASS
SMP-Timer, 4 CPUs, 4 boots none finishes: the guest freezes within 30 s 2 PASS; 1 fills the log with core_DoCallIPI: Failed to allocate IPIHook entry after 10 s; 1 froze during startup

What it does not change. On master the SMP tests freeze the guest in a good part of the boots, and they still do:

master with the change
SMP-DoIO, 4 CPUs, 6 boots 2 pass, 3 freeze, 1 did not boot 2 pass, 4 freeze
SMP-Proc, 4 CPUs, 6 boots 4 pass, 2 freeze 4 pass, 1 freeze, 1 never started the test
SMP-Sched, 16 CPUs, 5 boots 4 pass, 1 [Kernel:TLSF] free-list corruption the same
SMP-Sem, 4 CPUs does not finish does not finish
SMP-Timer, 16 CPUs does not finish does not finish
SMP-SigRace, 4 CPUs 3 PASS, 1 FAIL the same
SMP-Lists, 4 CPUs 4 PASS the same

The counts are too small to say whether the freezes are more or less frequent with the change.

In the repeated runs, every CPU's registers and stack were saved from each guest that did not finish, before it was stopped: 8 guests of master, 6 with the change. Both builds show the same pictures:

  • CPUs spinning with interrupts off on the task list locks in core_Schedule() / core_Switch(), or walking a task list in APICHeartbeatServer() that does not end.
  • Two CPUs spinning on the lock core_DoCallIPI() takes, called from Signal(), one of them from signal_hook inside the IPI handler. On master a trap frame shows that IPI arriving in core_DoIPI(). The boot with the change that froze during startup is this one.
  • A page fault or alert in supervisor state, then a halt.

core_ExitInterrupt() is on none of the saved stacks. One SMP-DoIO boot with the change had all four CPUs idle and the test's workers not running; that did not occur in master's six.

pc-i386 (one CPU): the image boots, a task that spins without a system call is switched out by the scheduler, and a second task goes on counting, as on master.

32, 64 and 128 CPUs

8 GB guests on a 128-thread host that other VMs were using, so a 128-CPU guest never had the host to itself. One boot per cell.

master with the change
SMP-Preempt, 32 CPUs 1 PASS, 18 FAIL, then [Kernel:TLSF] free-list corruption and the test stops 32 PASS
SMP-Preempt, 64 CPUs 1 PASS, 63 FAIL CPUs 0 to 46 PASS, then the guest freezes
SMP-Preempt, 128 CPUs 5 PASS and 28 FAIL among CPUs 0 to 32, then TLSF free-list corruption and a freeze CPUs 0 to 12 PASS, then the guest freezes
SMP-Affinity, 32 CPUs did not reach the test freezes in the first phase
SMP-Affinity, 64 CPUs freezes after the first move 108 PASS
SMP-Affinity, 128 CPUs freezes after the first move did not reach the test (3 boots)
SMP-SigRace, 32 CPUs 31 PASS, 1 FAIL freezes when the test starts
SMP-SigRace, 64 CPUs 63 PASS, 1 FAIL the same
SMP-SigRace, 128 CPUs 127 PASS, 1 FAIL freezes when the test starts
SMP-Sched, 32 and 64 CPUs 3 PASS the same
SMP-Timer, 32 and 64 CPUs fills the log with core_DoCallIPI: Failed to allocate IPIHook entry the same

So at these counts the change removes the SMP-Preempt failures as far as a run gets, and it is not enough for a clean run at 64 or 128 CPUs. SMP-SigRace at 32 and 128 CPUs and SMP-Affinity at 32 are worse with it in these single boots.

"Did not reach the test": at 128 CPUs both builds often stop during startup, master in 8 of 18 boots and the build with the change in 10 of 17. Booted alternately six times each with SMP-Sched as the test, master stopped during startup 4 times and the build with the change once; the boots that got through passed (3 PASS), except one with the change that froze in the test after TLSF free-list corruption.

State was saved from 17 guests of master and 18 with the change. The IPI hook lock picture above is the most frequent one in both (8 and 7 guests), and the task list lock pictures occur in both. Two things were seen at these counts only with the change:

  • Every CPU waiting with interrupts off for TaskRunningSpinLock in core_Switch() while a few CPUs are in APICHeartbeatServer(): 5 guests (SMP-SigRace at 32 and 128 CPUs, SMP-Preempt at 64, two during startup at 128). Master shows this at 4 CPUs in SMP-Timer.
  • During startup, page faults on several CPUs at the same instruction of Enable() with the same stack pointer, which is on the stack of the Exec housekeeper task, while each CPU's current task is its idle task: 2 boots at 128 CPUs (18 CPUs in one, which stopped; one CPU in the other, which went on and passed its test). None of master's 36 boots at these counts logged a page fault. It was also seen once at 64 CPUs in a build that does not have this change.

The counts do not show whether the change causes these or makes faults that are already there show more often. core_ExitInterrupt() is on none of the saved stacks here either.

Not tested: real hardware and a non-SMP pc-x86_64 build.

…ions

The LAPIC timer and the IPIs are the only interrupts an AP takes, and
they arrive as exceptions: core_IRQHandle() passed them to their
handlers and returned. The heartbeat sets the CPU's quantum and switch
flags, but only the device IRQ path called core_ExitInterrupt() to act
on them, so an AP did not act on its own quantum running out on the way
back from the interrupt: it switched tasks only when the running task
made a system call or left Disable(), or when another CPU sent it
IPI_RESCHEDULE. In test/smp's SMP-Preempt, of two busy tasks pinned to
one CPU the second was never scheduled, on every CPU but the first.
The LAPIC exceptions up to the system call vector now go through
core_ExitInterrupt() on the way back to user mode, as device IRQs do.

core_ExitInterrupt() may return with interrupts on, since
SoftIntDispatch() ends in KrnSti(), so interrupts are masked again
before the frame is restored, on both paths.
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.

1 participant