On the SMP kernel a workqueue worker in the middle of an item could wait
for kernel threads that had already parked, never reach its own park
point, and end the cycle with -EBUSY naming it. A netns teardown's
cleanup_net() does that: it waits for grace periods, the expedited
grace-period worker and other work items. A cycle started right after an
`unshare -n` exit failed 13 times in 15 on 2 CPUs and 12 in 15 on 4; beside
a loop of netns teardowns 22 of 30 and 20 of 30 failed. Docker's
containers (#59) make such teardowns routine in Debian guests.
- The TREE_RCU grace-period kthread parks only while no grace period is
in progress or requested, so that a kernel thread in synchronize_rcu()
or rcu_barrier() can reach its park point (a fixup of the park points
patch).
- Between the user and the kernel-thread phases kwparkd waits, at most
HZ/2, until every workqueue worker is idle (in TASK_IDLE), while every
kernel thread still runs (kw_park_settle_work(), a fixup of the arch
patch).
With both, every cycle of those runs passed (120 on 2 and 4 CPUs). An idle
cycle's median went from 17.9 ms to 0.3 ms on 2 CPUs; a cycle that waits
for a teardown takes 60-230 ms. kwtest's park check is unchanged (2-5 ms
mean). parking.md describes the settle step and the grace-period rule.
Based on Martin Brook's patches on the android branch of
martinbrook/unkernel (cc78ca9, 4849ed9). His settle step also ran
rcu_barrier() and waited only for workers in an uninterruptible sleep:
here the barrier cost 40-50 ms in every cycle, and a worker between two
waits (runnable) ended the wait early, so 11 of 15 cycles still failed.
Co-authored-by: Martin Brook <martin.brook100@googlemail.com>
get_user() and put_user() of an aligned 1, 2, 4 or 8 bytes are now one
load or store through kw_user_addr() instead of a memcpy (memory.copy,
which the Wasm spec does not make single-copy atomic on a shared
memory), so a futex word read beside another CPU's stores cannot tear;
an unaligned access still goes through raw_copy_*_user().
kwfb's set_par checks the mappings, allocates and switches the buffer
under info->mm_lock, which fbdev's mmap holds (check_var reads the count
under it too): an mmap racing FBIOPUT_VSCREENINFO from another thread
could map the old buffer.
kwtest checks both (uaccess: FUTEX_WAIT and getcpu beside another CPU's
stores, fault-in, EFAULT and a put_user across a page; kwfb: 200 mmaps
beside a thread changing the mode), and `kwtest smp` runs the kwfb
checks. Ported from VOD-Studio/unkernel@25e90b3f.
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: xfy <i@rua.plus>
WebGL2 on ANGLE's D3D11 backend (Chrome on Windows) has exactly 12
uniform blocks per stage. vrend keeps one for its VirglBlock, so Mesa's
virgl driver saw 11 uniform buffers, turned uniform buffer objects off
and with them GLES 3.0: the guest got OpenGL ES 2.0.
virglrenderer patch 21 declares the system values as plain uniforms
instead when 24 blocks and bindings fit and the ordinary uniform limits
have room for them, so the guest gets all 12 blocks. test/gl/kwgl-ubo12.c
(vtest-check.sh ubo12) draws with twelve distinct buffers in each stage;
bridge-check.sh --ubo N has the page cap the limits the wasm renderer
sees, which reproduces the D3D11 host on SwiftShader (14 per stage).
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: Niklas Olsson <niklas.olsson@hotmail.co.uk>
A failed run (a script that exits non-zero, no prompt for --trace, a
--get that does not match) now prints the end of the console, and on a
guest booted with systemd (/run/systemd/system) also systemctl --failed,
its jobs and the boot's journal; the other images print nothing more.
The traced boot's prompt detector also strips OSC sequences ended by ST
(ESC \), as systemd sends them, and CSI with a ! intermediate.
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: Vineel Sai <mail@vineelsai.com>
The project's Wisp server accepts only the page's own origins, so a page
built and served elsewhere had no network unless every load named a
server with ?wisp=. KW_WISP=<url>[,<url>...] sets the default list
instead (vite.config.ts defines __KW_WISP__, net.ts makes it
DEFAULT_WISP_SERVERS); a relative URL such as /wisp/ is on the page's own
origin, over ws: or wss:. ?wisp= still overrides it, the Network option's
note says whose server the default is, and without KW_WISP the page is
unchanged.
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: Swaroop Patil <swarooppatil850@gmail.com>
Node on Windows reports no execute bits (0o666, or 0o444 for a read-only
file, directories included), so the guest could not enter a directory of
a nodeFs mount (EACCES) nor run a script or binary in it, and its chmod
cannot add the bit. On win32, nodeFs now adds x where r is, as WSL's
drvfs shows them (hostMode); other hosts' bits are unchanged.
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: ARNO <arnochen101@gmail.com>
The CLI boots any count from 1 to 32, given by --cpus or taken from a
snapshot. Above os.availableParallelism() the CPU threads take turns,
and a CPU that waits for another (an IPI, a TLB shootdown) waits until
the host schedules that one. It still boots, now with a warning on
stderr. The --help text and js.md say so.
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: xfy <i@rua.plus>
* js: keep host file systems in worker-mode snapshots taken with commit: the owner saves its 9p servers' state while the worker holds the kernel at its consistent point, and fails the commit (the kernel goes on) while a request is unanswered or packets it sent have not reached the kernel (the worker counts those it delivered, snapshot-held's vsockIn); a restore in worker mode serves the mounts on the owner before the boot and passes the connections to keep to the worker's device (vsock.raw.keep)
Co-authored-by: Michael Lohr <michael@lohr.dev>
Co-authored-by: Claude <noreply@anthropic.com>
* js: describe host file systems in worker-mode snapshots where the docs still said inline only: VsockSnapshot.mounts, createKernel's restore, MountTable.snapshotBlocker() and the WASI binfmt test's unmount; rewrap the docs
---------
Co-authored-by: vibepodml[bot] <319756436+vibepodml[bot]@users.noreply.github.com>
Co-authored-by: Michael Lohr <michael@lohr.dev>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Pooya Parsa <pooya@pi0.io>
readTexture destroyed its MAP_READ staging buffer only after a
successful map, so a rejected mapAsync (a lost device, a validation
error) leaked it. Destroy it in a finally block, and check both the
failing and the succeeding path in test/vk.test.ts.
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: Niklas Olsson <niklas.olsson@hotmail.co.uk>
ConsoleRouter marks user space on a shell prompt that ends in #, $, %
or >, but the Alpine and Debian images' prompt (kw-prompt.sh) ends in
"⇒ ", so a prompt that is user space's first output went unnoticed and
the page fell back to its 300 ms timer. Match ⇒ as well, as
guest/tools/vm-run.ts already does, and test the router on both
prompts without a boot.
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: João Lucas de Oliveira Lopes <jlucaso@hotmail.com>
The initrd size check reads the export _end, which Linux's linker script
defines. A kernel linked without a script exports only wasm-ld's
__heap_base, and booting it with an initrd failed with "missing export
_end". Upstream's kernels export _end and keep using it.
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: Tsiry Sandratraina <tsiry.sndr@rocksky.app>
std's HashMap seeds each map differently (on wasm32 from a count of the
maps made before), and several passes emit ids and declarations in map
order, so the same module translated twice in one instance could give
different WGSL (small.comp: declaration order and names). The passes'
maps and sets now have a fixed seed (crate::HashMap and HashSet), the
8/16-bit storage pass also emits its groups and typed constants sorted,
and vk/naga/test/run.ts translates every test shader a second time and
requires the same output.
wgpu apps on guest Vulkan failed at pipeline creation on two spv-in gaps,
both in SPIR-V that naga's writer produces:
- GLSL.std.450 integer min, max, clamp, abs, sign and MSB take their
signedness from the opcode, but spv-in takes it from the operands. The
writer's Restrict bounds checks are UMin with a u32 result on i32 indices
and UMin on signed image coordinates, so mixed operands failed validation
(min(i32, u32), FilmCraft's effects) and signed ones became a signed min
that no longer clamps negative values; glslang's findMSB(uint), FindUMsb
with an int result, failed too. Operands and results of the other
signedness are now bitcast to the opcode's.
- OpLogicalAnd/OpLogicalOr on bool vectors, which the writer uses to guard
integer vector divisions (egui-wgpu 0.37's fragment shaders: every egui
0.37 app), are typed by spv-in as scalars only; they become OpSelect with
an all-false or all-true vector.
Checks in vk/naga/test (intsign.comp, boolvec.comp, and each with the
writer's shapes patched in).
* Enable Docker IPv4 bridge networking in Debian guests
* kernel: leave out the netfilter and bridge defaults Docker does not use
NETFILTER_INGRESS and NETFILTER_EGRESS (hooks for nft's netdev family,
which is not built), NF_CT_PROTO_SCTP and BRIDGE_IGMP_SNOOPING come on
by default with the options the Docker bridge needs; turning them off
saves 78,289 bytes of vmlinux32.wasm (25,917 bytes gzip). Re-export the
series: 0049, 0052 and 0055 carried index lines for the old
kw_defconfig.
* kernel: fix the conntrack hash size at 1024 buckets
Built-in conntrack allocates its hash table and the NAT bysource table,
which takes the same size, at boot also without rules, sized from RAM:
512 KiB each above 1 GiB, as on the page's 2 GiB default. A new generic
patch adds NF_CONNTRACK_HASHSIZE; kw_defconfig sets it to 1024, 8 KiB
per table, with nf_conntrack_max 8192. A 2 GiB guest keeps about 940 KiB
more free after boot.
---------
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Fold the 56 kernel patches into 32: one arch patch (which now includes
the riscv ISA patch, parking, atomics, SMP, blocking waits, unfair
spinlocks, the arm64 user ABI, pc sampling, ptrace, single-step and core
dumps), each driver with its SMP part, the generic changes one per piece
(the cond_resched() poll split out of the arch patch), and kw_defconfig
last. The messages describe the code as it is; every patch that was
kernel-wasm@localhost's is Pooya Parsa's. The tree is unchanged.
kernel/build.sh patches (kernel/tools/export-patches.sh) exports the
series without what changed in every later patch on each edit: no
numbers in the file names (the order is the new series file, which
build.sh applies), a zero commit hash, no git version signature and no
index lines, with the diff options fixed.
* README: name the Wisp proxy the page uses by default
The paragraph only said "a Wisp proxy", which leaves the reader to assume it
is their own. Name the default endpoint, say what it sees of a guest's
traffic, and keep the ?wisp= escape hatch in the same paragraph.
Refs #3
* Apply suggestion from @pi0
* Options: say whose Wisp server the default choice is
The Network section offered "Wisp" without saying that it is this project's
server and that the guest's traffic goes through it. One line under the choice
says both, next to the custom-servers field that replaces it.
---------
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
* js: hard links on host file systems: Fs gets an optional link(target, path), which the 9p server calls for Tlink of a regular file (it answered EPERM always); nodeFs makes them with fs.link, refusing a source that is not a regular file and undoing a link whose new name is not the file it lstat'ed (Darwin's link(2) follows a symlink, which would name a file outside the directory); MemoryFs still answers EPERM, as do a directory, a symlink and a read-only mount, and a fid restored without its file answers ESTALE; package managers that put a file in place with a link (uv's lock and cache, cargo, pnpm's store) work on a nodeFs mount and on tabc's volumes; tests in 9p.test.ts and api.test.ts (ln in the guest, one inode on the host)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* js: nodeFs runs a hard link alone, so the guest cannot rename, remove or symlink a source or a name into place during it (on Darwin, link(2) follows a symlink); link uses the bigint lstat; a restore leaves a file stale that was open under a removed name even when a hard link still names it (documented); the api hard-link test uses its own directory (below scratch it broke the vm.mount() test's listing) and no longer checks a directory link, which the guest's VFS refuses before 9p; the 9p test removes its directories and checks that a link and a rename do not overlap
* js: nodeFs's hard link checks that every directory on both its paths is a directory, not a symlink (ESTALE), and its lock is per host directory with waits and holds bounded to 10 s (EAGAIN), so a hung rename no longer stalls links and renames of every mount; tests for both, the Tlink test's title says EROFS, the api test unmounts in t.after; Fs.link says the target's type may have changed since the walk
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Pooya Parsa <pooya@pi0.io>
The SDK named the project's Wisp server (DEFAULT_WISP_SERVERS), and the
Node entry sent Origin: http://localhost to pass that server's allow
list (NODE_WISP_HEADERS). The server accepts only the page's origins,
so both are gone: wispBackend() takes the caller's server, and Node
sends handshake headers only when `headers` asks for them. The page
keeps wss://wisp.unkernel.dev:6680/ as its own default (web/src/net.ts).
The live Wisp test takes its server from UNKERNEL_TEST_WISP.
The README and the SDK docs now say what a guest sees over Wisp: no
half-close (a client that waits for the server to see end of file
hangs), placeholder addresses for every name, no connect
acknowledgement. wispBackend() takes a `lookup` resolver for real
addresses, and its fake address table now evicts the least recently
used name instead of wrapping. The default server is
wss://wisp.unkernel.dev:6680/.
The live Wisp test waited for its marker anywhere in the screen, so the
shell's echo of the command satisfied it; it now waits for the marker at
the start of a line.
Refs #44
A release build (and a dev server without a local build) downloads only
the release's page.json; the copy in web/kernel/release/ with its checks,
the GL and Vulkan downloads and KW_MIRROR=0 are gone. A release without
page.json is refused with what to do.
A page build with a release that carries page.json downloads only that
and points the page at mirror.unkernel.dev's copy of the release assets
(CORS, Range, immutable); nothing is copied into the output. The release
workflow now also publishes a gzipped copy of every .wasm, which the SDK
gunzips when a body starts with gzip's magic (the mirror sends files as
they are).
Sockets: sock_accept, sock_recv, sock_send and sock_shutdown on sockets the program inherited, with socket file types, poll and non-blocking; KWWASI_TCPLISTEN=ADDR:PORT[,...] opens listening TCP sockets at descriptors 4, 5, ... (exit 126 when one is already open); a write to a socket whose peer went away is EPIPE, not SIGPIPE.
Threads: modules that import wasi.thread-spawn and a shared env.memory run on the shared path, each wasm thread with a worker, a channel and a kwwasi connection of its own (a kwwasi pthread sharing the process's descriptors), so a thread blocked on stdin holds up no other; proc_exit or a trap in any thread ends the program; the message path refuses such a module with exit 126.
Clocks: realtime and monotonic follow the guest's (kwwasi sends both readings at START, no request per call); the CPU clocks are the worker's CPU time on Node, the process clock covering every thread through worker.cpuUsage(); unknown clock ids are EINVAL and the resolution is the timer's measured step.
Files: fd_allocate; fd_fdstat_set_flags refuses a sync flag change with ENOTSUP; more Linux errnos mapped; KWWASI_CWD=cwd resolves every path without a leading "/" from the cwd, with no guessing.
A wasm64 module is refused with exit 126, and a sleep on the message path no longer ends a fraction of a millisecond early. The kwwasi protocol is version 3. test/wasi-binfmt.ts runs its guest commands as root and covers the above.