fix(v4): a controller's drive slots are allocated once, never replaced (#178)

sk_v4_idle allocated a controller's slot array again whenever max_slots + 1
was more than it held -- the old array dropped, neither copied nor freed --
while the identity code holds pointers into it (g_offered, g_spare,
g_pending_real, g_real_drive, g_zuse_dev). Its two slot loops then ran to
max_slots whatever the array held.

sk_v4_slots_get() (kernel/src/v4/sk_v4_slots.c): allocated once, zeroed, at the
size first asked for; never replaced; the slots that may be used are those it
has. sk_v4_idle uses it, and its loops stop at the array's end.

kernel/tests/test_sk_v4_slots.c, kmalloc_aligned faked, seen to fail first
against the allocation as it was (4 failures: replaced when more were asked
for, 17 said usable where 9 existed, a held pointer left behind, an allocation
for 0); now 6 checks, 0 failures. Built for amd64, aarch64, riscv64; latent
today (max_slots does not grow), so the boots show no change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-12 18:05:57 +00:00
parent c778f6e0e7
commit 41f8648dd9
7 changed files with 103 additions and 12 deletions
+9 -9
View File
@@ -35,6 +35,7 @@
* ACL-ZUSE-BOOT.
*/
#include "starkernel/v4/sk_v4_identity.h"
#include "starkernel/v4/sk_v4_slots.h"
#include "starkernel/console.h"
#include "starkernel/arch.h"
#include "starkernel/apic.h"
@@ -510,15 +511,14 @@ static void sk_v4_idle(void)
for (xc = 0; xc < xhci_dev_count() && xc < XHCI_MAX_CONTROLLERS; xc++) {
xhci_dev_t *xdev = xhci_dev_at(xc);
if (!xdev) continue;
if (!g_usb[xc].slots || g_usb[xc].count < xdev->max_slots + 1) {
size_t bytes = (size_t)(xdev->max_slots + 1) * sizeof(sk_v4_drive_t);
sk_v4_drive_t *fresh = (sk_v4_drive_t *)kmalloc_aligned(bytes, 64);
if (!fresh) continue;
memset(fresh, 0, bytes);
g_usb[xc].slots = fresh;
g_usb[xc].count = xdev->max_slots + 1;
/* allocated once and never replaced: the identity code holds pointers into it (#178) */
{
void *slots = g_usb[xc].slots;
uint32_t usable = sk_v4_slots_get(&slots, &g_usb[xc].count, xdev->max_slots + 1, sizeof(sk_v4_drive_t));
g_usb[xc].slots = (sk_v4_drive_t *)slots;
if (usable == 0) continue;
}
for (slot = 1; slot <= xdev->max_slots; slot++) {
for (slot = 1; slot <= xdev->max_slots && slot < g_usb[xc].count; slot++) {
xhci_msc_slot_t *ms = xhci_msc_slot_for(xdev, slot);
if (!ms || !ms->bot_msc_attach_pending) continue;
ms->bot_msc_attach_pending = 0;
@@ -526,7 +526,7 @@ static void sk_v4_idle(void)
sk_v4_drive_arrived(&g_usb[xc].slots[slot]))
ms->bot_msc_attached = 1;
}
for (slot = 1; slot <= xdev->max_slots; slot++) {
for (slot = 1; slot <= xdev->max_slots && slot < g_usb[xc].count; slot++) {
xhci_msc_slot_t *ms = xhci_msc_slot_for(xdev, slot);
if (!ms || !ms->bot_msc_detach_pending) continue;
ms->bot_msc_detach_pending = 0;
+23
View File
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* sk_v4_slots.c -- see sk_v4_slots.h. */
#include "starkernel/v4/sk_v4_slots.h"
#include "starkernel/kmalloc.h"
#include <string.h>
/* sk_v4_idle replaced the array whenever a controller's max_slots + 1 was more than it held: the
* old one dropped, neither copied nor freed, while the identity code held pointers into it (#178).
* Now it is allocated once, at the size first asked for, and never replaced; the slots that may be
* used are the ones it has. */
uint32_t sk_v4_slots_get(void **slots, uint32_t *count, uint32_t want, size_t elem)
{
if (!*slots && want) {
void *fresh = kmalloc_aligned(want * elem, 64);
if (!fresh) return 0;
memset(fresh, 0, want * elem);
*slots = fresh;
*count = want;
}
return *slots ? *count : 0;
}