Skip to content

add an API to the standard library to learn the system page size #11308

Closed

Description

@gpanders

Zig Version

0.10.0-dev.1434+676652865

Steps to Reproduce

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.

Activity

  1. added
    bug
    on Mar 26, 2022
  2. vielmetti commented on Mar 27, 2022

    @vielmetti

    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.

  3. added
    arch-aarch64
    standard library
    on Jul 29, 2022
  4. added this to the 0.12.0 milestone on Jul 29, 2022
  5. added a commit that references this issue on Aug 15, 2022
    4540fd0
  6. dkwo commented on Sep 9, 2022

    @dkwo

    @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?

  7. added and removed on Sep 9, 2022
  8. modified the milestones: 0.12.0, 0.11.0 on Sep 9, 2022
  9. Vexu commented on Sep 9, 2022

    @Vexu
    Member

    I can put it on 0.11.0 but it doesn't mean much since it could be resolved earlier or later anyways.

  10. dkwo commented on Nov 21, 2022

    @dkwo

    Would it be feasible to adopt for linux whatever solution zig already adopts for macos?

  11. miameows commented on Dec 23, 2022

    @miameows

    @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.

  12. 18 remaining items

  13. added this to the 0.12.0 milestone on Jul 24, 2023
  14. dkwo commented on Jul 25, 2023

    @dkwo

    @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.

  15. andrewrk commented on Jul 25, 2023

    @andrewrk
    Member

    @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.

  16. dkwo commented on Jul 25, 2023

    @dkwo

    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

    git clone https://github.com/void-linux/void-packages.git
    cd void-packages
    ./xbps-src binary-bootstrap
    

    Then build zig https://github.com/void-linux/void-packages/blob/master/srcpkgs/zig/template (version 0.10.1)

    ./xbps-src pkg zig
    

    Then build (e.g.) river https://github.com/void-linux/void-packages/blob/master/srcpkgs/river/template (version 0.2.4)

    ./xbps-src pkg river
    

    The build process fails with

    thread 2820 panic: reached unreachable code 
    Panicked during a panic. Aborting. 
    

    More details:

    => xbps-src: updating repositories for host (aarch64)... 
    [*] Updating repository `https://repo-default.voidlinux.org/current/aarch64/bootstrap/aarch64-repodata' ...
    [*] Updating repository `https://repo-default.voidlinux.org/current/aarch64/aarch64-repodata' ...
    [*] Updating repository `https://repo-default.voidlinux.org/current/aarch64/nonfree/aarch64-repodata' ...
    [*] Updating repository `https://repo-default.voidlinux.org/current/aarch64/debug/aarch64-repodata' ...
    => xbps-src: updating software in / masterdir... 
    => xbps-src: cleaning up / masterdir... 
    => river-0.2.4_2: removing autodeps, please wait... 
    => river-0.2.4_2: the following build options are set: 
       xwayland: Enable Xwayland support (ON) 
    => river-0.2.4_2: building with [zig-build] for aarch64... 
       [host] pkg-config-0.29.2_3: found (https://repo-default.voidlinux.org/current/aarch64)
       [host] wayland-devel-1.22.0_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [host] scdoc-1.11.2_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [host] zig-0.10.1_2: found (/host/binpkgs) 
       [target] wlroots-devel-0.16.2_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] libevdev-devel-1.13.0_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] pixman-devel-0.42.2_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] wayland-protocols-1.32_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] libxkbcommon-devel-1.5.0_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] wayland-devel-1.22.0_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [runtime] xorg-server-xwayland-23.1.2_1: found (https://repo-default.voidlinux.org/current/aarch64)
    => river-0.2.4_2: installing host dependencies: pkg-config-0.29.2_3 wayland-devel-1.22.0_1 scdoc-1.11.2_1 zig-0.10.1_2 ...
    => river-0.2.4_2: installing target dependencies: wlroots-devel-0.16.2_1 libevdev-devel-1.13.0_1 pixman-devel-0.42.2_1 wayland-protocols-1.32_1 libxkbcommon-devel-1.5.0_1 wayland-devel-1.22.0_1 ...
    => river-0.2.4_2: running do-fetch hook: 00-distfiles ... 
    => river-0.2.4_2: running do-extract hook: 00-distfiles ... 
    => river-0.2.4_2: extracting distfile(s), please wait... 
    => river-0.2.4_2: running do-patch hook: 00-patches ... 
    => river-0.2.4_2: running pre-configure hook: 00-gnu-configure-asneeded ...
    => river-0.2.4_2: running pre-configure hook: 01-override-config ...
    => river-0.2.4_2: running pre-configure hook: 02-script-wrapper ...
    => river-0.2.4_2: running pre-build hook: 02-script-wrapper ... 
    => river-0.2.4_2: running do_build ... 
    thread 2820 panic: reached unreachable code 
    Panicked during a panic. Aborting. 
    error: the following build command crashed: 
    /builddir/river-0.2.4/zig-cache/o/fd98d92d81de57486a870485e6d8aa9e/build /usr/bin/zig /builddir/river-0.2.4 /builddir/river-0.2.4/zig-cache /tmp/.cache/zig --sysroot  --libc xbps_zig_libc.txt -Dtarget=aarch64-linux-gnu -Dcpu=baseline -Drelease-safe --prefix /usr install -Dxwayland -Dpie
    => ERROR: river-0.2.4_2: do_build: 'DESTDIR="zig-out" zig build --sysroot "${XBPS_CROSS_BASE}" --libc xbps_zig_libc.txt -Dtarget="${zig_target}" -Dcpu="${zig_cpu}" -Drelease-safe --prefix /usr install ${configure_args}' exited with 1
    => ERROR:   in do_build() at common/build-style/zig-build.sh:31  
    
  17. andrewrk commented on Jul 25, 2023

    @andrewrk
    Member

    Yes that looks like a proper bug report. It also looks like a duplicate of #16331. I suggest subscribing to #16331.

  18. dkwo commented on Jul 25, 2023

    @dkwo

    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.

  19. andrewrk commented on Jul 25, 2023

    @andrewrk
    Member

    If you have time to argue with me on the issue tracker, you have time to submit a patch :P

  20. monomycelium commented on Oct 1, 2023

    @monomycelium

    In case it helps anyone, I have made a temporary fix with binary download here: https://github.com/par5er/zig/releases

    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!

    Update

    I have compiled Zig 0.12.0 with the temporary patch (with -mcpu=baseline) with a download at https://fs.ilmul.com/zig-linux-aarch64-0.12.0.tar.xz.

  21. RossComputerGuy commented on Oct 3, 2023

    @RossComputerGuy
    Contributor

    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.

  22. vielmetti commented on Nov 23, 2023

    @vielmetti

    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.

  23. RossComputerGuy commented on Nov 23, 2023

    @RossComputerGuy
    Contributor

    @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.

  24. RoundDuckKira commented on Jan 22, 2024

    @RoundDuckKira

    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

    git clone https://github.com/void-linux/void-packages.git
    cd void-packages
    ./xbps-src binary-bootstrap
    

    Then build zig https://github.com/void-linux/void-packages/blob/master/srcpkgs/zig/template (version 0.10.1)

    ./xbps-src pkg zig
    

    Then build (e.g.) river https://github.com/void-linux/void-packages/blob/master/srcpkgs/river/template (version 0.2.4)

    ./xbps-src pkg river
    

    The build process fails with

    thread 2820 panic: reached unreachable code 
    Panicked during a panic. Aborting. 
    

    More details:

    => xbps-src: updating repositories for host (aarch64)... 
    [*] Updating repository `https://repo-default.voidlinux.org/current/aarch64/bootstrap/aarch64-repodata' ...
    [*] Updating repository `https://repo-default.voidlinux.org/current/aarch64/aarch64-repodata' ...
    [*] Updating repository `https://repo-default.voidlinux.org/current/aarch64/nonfree/aarch64-repodata' ...
    [*] Updating repository `https://repo-default.voidlinux.org/current/aarch64/debug/aarch64-repodata' ...
    => xbps-src: updating software in / masterdir... 
    => xbps-src: cleaning up / masterdir... 
    => river-0.2.4_2: removing autodeps, please wait... 
    => river-0.2.4_2: the following build options are set: 
       xwayland: Enable Xwayland support (ON) 
    => river-0.2.4_2: building with [zig-build] for aarch64... 
       [host] pkg-config-0.29.2_3: found (https://repo-default.voidlinux.org/current/aarch64)
       [host] wayland-devel-1.22.0_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [host] scdoc-1.11.2_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [host] zig-0.10.1_2: found (/host/binpkgs) 
       [target] wlroots-devel-0.16.2_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] libevdev-devel-1.13.0_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] pixman-devel-0.42.2_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] wayland-protocols-1.32_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] libxkbcommon-devel-1.5.0_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [target] wayland-devel-1.22.0_1: found (https://repo-default.voidlinux.org/current/aarch64)
       [runtime] xorg-server-xwayland-23.1.2_1: found (https://repo-default.voidlinux.org/current/aarch64)
    => river-0.2.4_2: installing host dependencies: pkg-config-0.29.2_3 wayland-devel-1.22.0_1 scdoc-1.11.2_1 zig-0.10.1_2 ...
    => river-0.2.4_2: installing target dependencies: wlroots-devel-0.16.2_1 libevdev-devel-1.13.0_1 pixman-devel-0.42.2_1 wayland-protocols-1.32_1 libxkbcommon-devel-1.5.0_1 wayland-devel-1.22.0_1 ...
    => river-0.2.4_2: running do-fetch hook: 00-distfiles ... 
    => river-0.2.4_2: running do-extract hook: 00-distfiles ... 
    => river-0.2.4_2: extracting distfile(s), please wait... 
    => river-0.2.4_2: running do-patch hook: 00-patches ... 
    => river-0.2.4_2: running pre-configure hook: 00-gnu-configure-asneeded ...
    => river-0.2.4_2: running pre-configure hook: 01-override-config ...
    => river-0.2.4_2: running pre-configure hook: 02-script-wrapper ...
    => river-0.2.4_2: running pre-build hook: 02-script-wrapper ... 
    => river-0.2.4_2: running do_build ... 
    thread 2820 panic: reached unreachable code 
    Panicked during a panic. Aborting. 
    error: the following build command crashed: 
    /builddir/river-0.2.4/zig-cache/o/fd98d92d81de57486a870485e6d8aa9e/build /usr/bin/zig /builddir/river-0.2.4 /builddir/river-0.2.4/zig-cache /tmp/.cache/zig --sysroot  --libc xbps_zig_libc.txt -Dtarget=aarch64-linux-gnu -Dcpu=baseline -Drelease-safe --prefix /usr install -Dxwayland -Dpie
    => ERROR: river-0.2.4_2: do_build: 'DESTDIR="zig-out" zig build --sysroot "${XBPS_CROSS_BASE}" --libc xbps_zig_libc.txt -Dtarget="${zig_target}" -Dcpu="${zig_cpu}" -Drelease-safe --prefix /usr install ${configure_args}' exited with 1
    => ERROR:   in do_build() at common/build-style/zig-build.sh:31  
    

    wait, sorry that it isn't the most related to this discussion, but how are you running void, natively or in a VM?

  25. dkwo commented on Jan 22, 2024

    @dkwo

    @RoundDuckKira native, as you figured out :)

  26. par5er commented on Feb 7, 2025

    @par5er

    Building Zig from source on Asahi Linux finally works! Thanks wonderful Zig team!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    contributor friendlyenhancementstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions