Skip to content

fix(kernel): stabilize apt update under sustained I/O#2157

Merged
fslongjin merged 7 commits into
DragonOS-Community:masterfrom
fslongjin:codex/fix-apt-update-data-path
Jul 24, 2026
Merged

fix(kernel): stabilize apt update under sustained I/O#2157
fslongjin merged 7 commits into
DragonOS-Community:masterfrom
fslongjin:codex/fix-apt-update-data-path

Conversation

@fslongjin

Copy link
Copy Markdown
Member

Summary

  • replace duplicate-prone NAPI scheduling with an exact-once SCHED/MISSED state machine and bounded, fair polling
  • move virtio-net to Q64 raw queues with preallocated DMA slots, asynchronous TX completion, and race-closing EVENT_IDX notification handling
  • prevent the same task from running or being enqueued on two CPUs during remote wakeups, stop/resume, affinity changes, and migration tails
  • batch ext4 sequential range allocation and orphan extent reclamation while preserving journal credits, checksums, and initialized-before-published block ordering
  • avoid read-before-write I/O for complete ext4 blocks and handle disappearing proc fd tables without panicking
  • pin the merged virtio-drivers queue fixes from the DragonOS mirror

Root cause

The observed apt update stalls were produced by several interacting bottlenecks and correctness gaps:

  • virtio-net used two-descriptor RX and TX rings, while each TX packet synchronously waited for completion
  • NAPI could enqueue the same instance more than once and did not close the callback-enable race
  • remote network wakeups could enqueue a task before its previous CPU completed switch-out
  • apt package-store writes caused ext4 to allocate and journal missing blocks one at a time, and interrupted-download cleanup committed each tail extent separately
  • proc fd traversal could race process exit and unwrap an already removed descriptor table

Together these issues amplified interrupt, scheduler, and synchronous metadata costs into low throughput, long cleanup delays, and occasional kernel instability.

Implementation

Network data path

  • add a host-testable atomic NAPI transition crate
  • use FIFO scheduling with packet, poll-call, and elapsed-time budgets
  • keep RX/TX callback state paired with NAPI ownership
  • preallocate page-backed RX/TX DMA slots and recycle completions asynchronously
  • fail-stop on queue-accounting or malformed completion invariants

Scheduler

  • track actual CPU execution ownership independently from runqueue placement
  • wait for switch-out completion before remote enqueue
  • serialize stop wakeups and affinity placement under the scheduler placement locks
  • revalidate pending migration state, affinity, and runqueue ownership in the switch tail

Filesystem and procfs

  • allocate contiguous append ranges in one ext4 transaction
  • support journaled appends to the right-most external extent leaf
  • reserve transaction credits before batched tail reclamation
  • batch same-group orphan extent cleanup
  • write complete blocks without a preceding read
  • treat missing fd/fdinfo tables as normal process-exit races

Validation

  • cargo test -p napi-state: 7 passed
  • cargo test -p another_ext4: 131 passed
  • virtio-drivers: 27 unit tests and 8 doctests passed before pinning the merged revision
  • make kernel: passed against the pinned mirror revision
  • QEMU fresh apt update: completed a 31.6 MB refresh without a hash mismatch
  • interrupted partial download: resumed from the previous offset
  • fragmented partial-file cleanup: reduced from minutes to a fraction of a second

Remaining scope

This change removes the confirmed correctness failures and synchronous-I/O amplification. Full Linux-style ext4 delayed allocation and writeback-time allocation remain separate future performance work.

Rework NAPI scheduling around an exact-once SCHED/MISSED state machine with bounded, fair polling. Move virtio-net to deeper raw queues with preallocated DMA buffers, asynchronous TX completion, and race-closing EVENT_IDX callback handling.

Track actual cross-CPU task ownership independently from runqueue placement and serialize stop wakeups, affinity changes, and migration tails so a task cannot execute or enqueue twice during network wakeups.

Batch ext4 sequential range allocation and orphan extent reclamation, preserve journal credit and checksum invariants, and avoid read-before-write for complete blocks. This prevents apt package-store writes and interrupted-download cleanup from synchronously amplifying metadata I/O.

Treat disappearing proc fd and fdinfo tables as normal exit races, refresh the pinned virtio-drivers revision, and add host-testable NAPI transition coverage.

Validated with cargo test -p napi-state, cargo test -p another_ext4, and make kernel.

Signed-off-by: longjin <longjin@dragonos.org>
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@github-actions github-actions Bot added the Bug fix A bug is fixed in this pull request label Jul 23, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 732d525c08

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kernel/src/filesystem/procfs/pid/fd.rs Outdated
Comment thread kernel/src/filesystem/procfs/pid/fdinfo.rs Outdated
Refresh whole-disk capacity from the backing block device so loop devices expose their configured size after LOOP_SET_FD. Keep the static start LBA on I/O hot paths, safely fall back after device teardown, and report only complete loop sectors.

Revalidate cached proc fd and fdinfo entries against the live descriptor table. Return ENOENT across zombie and reaped-task paths, including reads from an already-open fdinfo file, matching Linux 6.6 semantics.

Add a dunitest covering cached fd entries across process exit.

Signed-off-by: longjin <longjin@dragonos.org>
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c285a71b9c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kernel/src/driver/net/virtio_net.rs Outdated
Consume completed RX buffers independently of TX queue availability so NAPI can continue making receive progress under transmit pressure.

Use a lazy response token for receive-side replies and reserve DMA capacity only when smoltcp actually emits a frame. Keep explicitly reserved tokens for standalone transmit calls and split RX/TX token types to preserve their ownership invariants.

Signed-off-by: longjin <longjin@dragonos.org>
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d65ff73d07

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kernel/src/driver/net/virtio_net.rs Outdated
Comment thread kernel/src/net/net_core.rs
Keep the smoltcp Ethernet frame limit separate from the user-visible IP MTU so rtnetlink reports 1500 bytes instead of 1514.

Run the existing DHCP acquisition window in a kernel worker, allowing SystemState::Running and userspace startup to proceed when no DHCP server is available.

Add a strict rtnetlink MTU regression and teach dunitest to parse singular GoogleTest summaries so the one-case suite is enforced by CI.

Signed-off-by: longjin <longjin@dragonos.org>
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 3ba7c4babc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Signed-off-by: longjin <longjin@dragonos.org>
Signed-off-by: longjin <longjin@dragonos.org>
@fslongjin

Copy link
Copy Markdown
Member Author

Root cause confirmed: FAT cluster allocation and release were not serialized across different inodes. Concurrent page-cache writeback could select the same free FAT entry, corrupt a cluster chain, and eventually make FATFile::ensure_len() reach an impossible missing cluster.

The fix adds a filesystem-wide FAT metadata lock, aligned with Linux 6.6 msdos_sb_info::fat_lock, around allocation/free chain transitions. Data-cluster zeroing remains outside that lock after the cluster is reserved and linked. A new Dunitest concurrently grows and fsyncs eight independent FAT files, then verifies every block; it also checks the filesystem type, capacity, cleanup paths, and interrupted/short reads.

Validation: make kernel, cross-build of the new Dunitest, git diff --check, and an 8-role adversarial review all passed.

@github-actions github-actions Bot added the test Unitest/User space test label Jul 23, 2026
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 36c3be8b74

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kernel/crates/another_ext4/src/ext4/alloc.rs Outdated
Signed-off-by: longjin <longjin@dragonos.org>
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 5e3693d178

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@fslongjin
fslongjin merged commit e374b45 into DragonOS-Community:master Jul 24, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug fix A bug is fixed in this pull request test Unitest/User space test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant