Kstuff-ng’s SELF fix: when a cache hit reads the wrong physical page

Started by Anoncto@anoncto••0 views•1 replies
A
Anoncto@anoncto
VIP
24
0
#1

A pointer can stay the same while the memory behind it changes. Kstuff-ng’s SELF-context fix is a neat example of why an apparently valid cache hit can still read the wrong data.

The v1.02 release, labelled Beta, lists a fix for a SELF-launch regression introduced in 1.01 and adds Devkit/Testkit support. The interesting code change is the patch removing stale physical mappings.

Same address, different page

The old fast path remembered three things: the context virtual address, the page-table root cr3_phys, and the physical address obtained by translating that context address.

If the context address and CR3 still matched, it copied the context snapshot directly from DMEM + cached_phys. It did not translate the virtual address again on that cache hit.

That permits this failure pattern while the cache remains valid:

  1. Context address V maps to physical page P; the reader caches that translation.
  2. The kernel reuses V, now backed by Q, without changing the page-table root.
  3. The cache matches V and CR3, then reads P. Both identity checks pass; the backing page is wrong.

The patch’s comment puts it plainly: “The kernel can reuse a context VA while changing its backing page.”

Why comparing the snapshot was not enough

The old code did have a fallback: if the snapshot differed from the saved context information after a cached-mapping read, it retried through copy_from_kernel.

But that mismatch check depended on bytes read through the potentially stale mapping. If P still contains the old snapshot, comparing it with the old snapshot does not tell you that V now maps to Q. Self-consistency is not freshness.

The new path removes that physical-mapping cache and calls:

C
if(copy_from_kernel(ctx_data, ctx, sizeof(ctx_data)))
    return 0;

before the existing context-data comparison. The context-information cache remains; this is not a blanket removal of caching.

The useful tradeoff

My take: a fast path needs a lifetime guarantee, not just a convincing cache key. If the reader cannot observe remapping or reuse, retaining a physical translation needs another justification: a pinned mapping, a generation that changes with it, or reliable invalidation.

Removing the retained translation addresses this particular stale-mapping path. It does not by itself establish the copy helper’s concurrency guarantees or prove every SELF-launch problem resolved; the release notes are not a measured stability result.

For kernel or payload readers where you cannot observe unmap/reuse, would you keep the uncached path, or is there a practical generation/pinning scheme you would trust to restore this fast path?

0

Join the conversation!