You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
add an API to the standard library to learn the system page size #11308
contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.enhancementSolving this issue will likely involve adding new logic or components to the codebase.standard libraryThis issue involves writing Zig code for the standard library.
I am running Linux on a MacBook Pro which uses 16 KiB pages; however, Zig's std lib always assumes 4 KiB pages on Linux running on aarch64.
Expected Behavior
Zig should infer/detect the correct page size.
Actual Behavior
Zig assumes 4 KiB pages, which results in errors in calls to msync or mmap (e.g. in std.debug.captureStackTrace).
#4082 is mentioned in the source code, so I'm guessing that is the long term solution. That issue also mentions a "hacky" solution of using madvise to check for a valid page size. A short term solution could be to check madvise at compile time when running Linux on aarch64 to check between all possible page sizes (ARM64 can also support 64 KiB pages). If this is acceptable I can open a PR.EDIT: It looks like syscalls can't run at comptime so I don't think this would work.
Reacted by Patrick Leiser, Aron Derenyi, Techcable, jowadmax, Gary Gozlan, cassepipe, rxhfcy, wvr, Jan Ph. H., Swyter and 14 more
As a reference point, this particular issue was addressed in Go (golang) at golang/go#10180 by determining system page size at runtime. The side effect is to improve compatibility on a variety of platforms with non-4K page sizes.
@dkwo According to The Asahi Linux docs, macOS has its own way of letting programs "choose" their own page size, so I'm guessing Zig just uses 4K pages and lets macOS deal with it. Linux doesn't have its own way of working with varied page sizes, and "likely never will" (quote from the Asahi docs)
So to answer your question, Zig doesn't need to worry about page sizes on macOS, so that's not really a solution that can be transferred to Linux.
P.S. If I got something horribly wrong, feel free to correct me ;)! I'm not an expert, this is just my understanding.
Reacted by dkwo, cassepipe, Patrick Leiser and wvr
@andrewrk Why was the bug label removed? if I cannot use zig to build e.g. river on aarch64 linux on my 16k page size kernel, this looks like a bug to me, not enhancement.
@andrewrk Why was the bug label removed? if I cannot use zig to build e.g. river on aarch64 linux on my 16k page size kernel, this looks like a bug to me, not enhancement.
If you want the bug label you need to fill out the bug template. This issue does not provide steps to reproduce, observed behavior, or actual behavior.
Perhaps you are interested in #16331 which is labeled as a bug.
Rather, that bug looks like a duplicate of this one :) since that was opened 3 weeks ago (vs 1+ year), and this bug is also tracked by asahi-linux, which is kind of useful.
I have applied the same patch to Zig 0.11.0 with a binary download at https://marsh.digitya.com/cache/zig-linux-aarch64-0.11.0.tar.xz if it helps anyone using Asahi Linux on an Apple M1 chip (use only if you are sure that this is not malicious). I compiled it using ReleaseFast, so the binary is quite large (around 130 MB). Again, it's a temporary fix; the software compiled using this would use 16k page sizes on Linux (unless you edit mem.zig?). Good luck!
I've managed to add a function for checking the page size at runtime in #17382. It seems like this issue somewhat is related to my PR. It supports setting the page size for a target so cross compiling would work. The only issue atm is it works best when your compiling on the device your using it for. This means prebuilt Zig versions won't work on Apple Silicon without a specific build. But I hope to fix that somewhat soon by replacing some of the std.mem.page_size usage with the runtime function.
@vielmetti Yeah, I won't be able to get a Pi 5 to test out #17382 on it but since I have an Apple Silicon device which the PR works for, this should work. Though Windows on ARM may not support the Pi 5, the code should work as soon as I add the Windows code today. But Linux does work so that should mean it will work.
I'm working on adding CPU-specific cross-compile to change the page size so if you specify the Pi CPU and Zig detects it then it should be able to use 16k pages. Though, the functionality might break programs if a non-16k page sized kernel is used. Work to detect this better could be done.
contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.enhancementSolving this issue will likely involve adding new logic or components to the codebase.standard libraryThis issue involves writing Zig code for the standard library.
Activity
vielmetti commentedon Mar 27, 2022
I've added this to the @AsahiLinux list of code that is known to have problems with a 16K page size, at
https://github.com/AsahiLinux/docs/wiki/Software-known-to-have-issues-with-16k-page-size
As a reference point, this particular issue was addressed in Go (golang) at golang/go#10180 by determining system page size at runtime. The side effect is to improve compatibility on a variety of platforms with non-4K page sizes.
Change AArch64-linux page size to 16k
dkwo commentedon Sep 9, 2022
@Vexu Why does this issue have an os-macos label, while it is specific of linux? also, any chance it could be assigned to an earlier milestone?
Vexu commentedon Sep 9, 2022
I can put it on 0.11.0 but it doesn't mean much since it could be resolved earlier or later anyways.
dkwo commentedon Nov 21, 2022
Would it be feasible to adopt for linux whatever solution zig already adopts for macos?
miameows commentedon Dec 23, 2022
@dkwo According to The Asahi Linux docs, macOS has its own way of letting programs "choose" their own page size, so I'm guessing Zig just uses 4K pages and lets macOS deal with it. Linux doesn't have its own way of working with varied page sizes, and "likely never will" (quote from the Asahi docs)
So to answer your question, Zig doesn't need to worry about page sizes on macOS, so that's not really a solution that can be transferred to Linux.
P.S. If I got something horribly wrong, feel free to correct me ;)! I'm not an expert, this is just my understanding.
18 remaining items
dkwo commentedon Jul 25, 2023
@andrewrk Why was the bug label removed? if I cannot use zig to build e.g. river on aarch64 linux on my 16k page size kernel, this looks like a bug to me, not enhancement.
andrewrk commentedon Jul 25, 2023
If you want the bug label you need to fill out the bug template. This issue does not provide steps to reproduce, observed behavior, or actual behavior.
Perhaps you are interested in #16331 which is labeled as a bug.
dkwo commentedon Jul 25, 2023
I see. Would the following be acceptable?
I'm using Void Linux on an Apple m1 machine (aarch64-glibc), which has a 16k page-size kernel, but any Linux with these requirements https://github.com/void-linux/void-packages#requirements and a non-standard (but perfectly fine) page-size kernel should be able to reproduce.
Clone the repo
Then build
zighttps://github.com/void-linux/void-packages/blob/master/srcpkgs/zig/template (version 0.10.1)Then build (e.g.)
riverhttps://github.com/void-linux/void-packages/blob/master/srcpkgs/river/template (version 0.2.4)The build process fails with
More details:
andrewrk commentedon Jul 25, 2023
Yes that looks like a proper bug report. It also looks like a duplicate of #16331. I suggest subscribing to #16331.
dkwo commentedon Jul 25, 2023
Rather, that bug looks like a duplicate of this one :) since that was opened 3 weeks ago (vs 1+ year), and this bug is also tracked by asahi-linux, which is kind of useful.
andrewrk commentedon Jul 25, 2023
If you have time to argue with me on the issue tracker, you have time to submit a patch :P
monomycelium commentedon Oct 1, 2023
I have applied the same patch to Zig
0.11.0with a binary download at https://marsh.digitya.com/cache/zig-linux-aarch64-0.11.0.tar.xz if it helps anyone using Asahi Linux on an Apple M1 chip (use only if you are sure that this is not malicious). I compiled it usingReleaseFast, so the binary is quite large (around 130 MB). Again, it's a temporary fix; the software compiled using this would use 16k page sizes on Linux (unless you editmem.zig?). Good luck!Update
I have compiled Zig
0.12.0with the temporary patch (with-mcpu=baseline) with a download at https://fs.ilmul.com/zig-linux-aarch64-0.12.0.tar.xz.RossComputerGuy commentedon Oct 3, 2023
I've managed to add a function for checking the page size at runtime in #17382. It seems like this issue somewhat is related to my PR. It supports setting the page size for a target so cross compiling would work. The only issue atm is it works best when your compiling on the device your using it for. This means prebuilt Zig versions won't work on Apple Silicon without a specific build. But I hope to fix that somewhat soon by replacing some of the
std.mem.page_sizeusage with the runtime function.vielmetti commentedon Nov 23, 2023
It is worthwhile to note that the new Pi 5 also has 16K pages by default, so it's likely that code running on it will be affected by this issue.
https://www.raspberrypi.com/documentation/computers/config_txt.html#kernel is the Pi 5 kernel config doc that's relevant.
RossComputerGuy commentedon Nov 23, 2023
@vielmetti Yeah, I won't be able to get a Pi 5 to test out #17382 on it but since I have an Apple Silicon device which the PR works for, this should work. Though Windows on ARM may not support the Pi 5, the code should work as soon as I add the Windows code today. But Linux does work so that should mean it will work.
I'm working on adding CPU-specific cross-compile to change the page size so if you specify the Pi CPU and Zig detects it then it should be able to use 16k pages. Though, the functionality might break programs if a non-16k page sized kernel is used. Work to detect this better could be done.
RoundDuckKira commentedon Jan 22, 2024
wait, sorry that it isn't the most related to this discussion, but how are you running void, natively or in a VM?
dkwo commentedon Jan 22, 2024
@RoundDuckKira native, as you figured out :)
par5er commentedon Feb 7, 2025
Building Zig from source on Asahi Linux finally works! Thanks wonderful Zig team!