Skip to content

#24471: add mlock syscalls in std.os.linux #24473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeffective
Copy link
Contributor

@jeffective jeffective commented Jul 16, 2025

closes #24471

MCL Flags

Please double-check the implementation of MCL. I interpreted results of searching the linux source tree:
https://github.com/search?q=repo%3Atorvalds%2Flinux+MCL_ONFAULT&type=code

I found the flags by referencing man mlock: https://man7.org/linux/man-pages/man2/mlock.2.html

Verification evidence

  • on WSL2 on windows

Create file mlock-test.zig:

const std = @import("std");

test "mlock syscalls" {
    const allocator = std.testing.allocator;
    const my_memory_area: []u8 = try allocator.alloc(u8, 2048);
    defer allocator.free(my_memory_area);

    const mlock_result = std.os.linux.mlock(my_memory_area.ptr, my_memory_area.len);
    std.log.warn("mlock errno: {}", .{std.posix.errno(mlock_result)});
    try std.testing.expect(mlock_result == 0);

    const munlock_result = std.os.linux.munlock(my_memory_area.ptr, my_memory_area.len);
    std.log.warn("munlock errno: {}", .{std.posix.errno(munlock_result)});
    try std.testing.expect(munlock_result == 0);

    const mlock2_result = std.os.linux.mlock2(my_memory_area.ptr, my_memory_area.len, std.os.linux.MLOCK{ .ONFAULT = false });
    std.log.warn("mlock2 errno: {}", .{std.posix.errno(mlock2_result)});
    try std.testing.expect(mlock2_result == 0);

    const munlock_result2 = std.os.linux.munlock(my_memory_area.ptr, my_memory_area.len);
    std.log.warn("munlock errno: {}", .{std.posix.errno(munlock_result2)});
    try std.testing.expect(munlock_result2 == 0);

    const mlockall_result = std.os.linux.mlockall(std.os.linux.MCL{ .CURRENT = true });
    std.log.warn("mlockall errno: {}", .{std.posix.errno(mlockall_result)});
    try std.testing.expect(mlockall_result == 0);

    const munlockall_result = std.os.linux.munlockall();
    std.log.warn("munlockall errno: {}", .{std.posix.errno(munlockall_result)});
    try std.testing.expect(munlockall_result == 0);
}

Run using zig built from source at commit 7caf40b (this PR)

janderson@DESKTOP-RN10O9U:~/repos/untracked$ sudo ../zig/build/stage3/bin/zig test --zig-lib-dir /home/janderson/repos/zig/lib/ mlock-test.zig
[default] (warn): mlock errno: .SUCCESS
[default] (warn): munlock errno: .SUCCESS
[default] (warn): mlock2 errno: .SUCCESS
[default] (warn): munlock errno: .SUCCESS
[default] (warn): mlockall errno: .SUCCESS
[default] (warn): munlockall errno: .SUCCESS
All 1 tests passed.

Prior Art

https://github.com/tigerbeetle/tigerbeetle/blob/41c9a90dd930d360914f96bb2bd8a46d0ea2104d/src/stdx/mlock.zig#L29

Copy link
Contributor

@rootbeer rootbeer left a comment

Choose a reason for hiding this comment

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

This looks like a reasonable addition to the ZIg linux syscall wrappers. I've got a couple notes, but feel free to ignore me as I'm just a drive-by reviewer.

In addition to the notes below, I also suggest adding some very simple tests. Its okay if they just return EPERM or EINVAL for the calls, the important thing is to make sure the wrapper code is at least compiled, as unreached code is ignored.

@jeffective jeffective marked this pull request as draft July 16, 2025 22:14
@jeffective jeffective force-pushed the 24471-mlock-syscalls branch from 86cb7da to 7caf40b Compare July 18, 2025 06:32
@rootbeer
Copy link
Contributor

Fixes LGTM.

@jeffective jeffective marked this pull request as ready for review July 19, 2025 06:40
@jeffective
Copy link
Contributor Author

In addition to the notes below, I also suggest adding some very simple tests. Its okay if they just return EPERM or EINVAL for the calls, the important thing is to make sure the wrapper code is at least compiled, as unreached code is ignored.

Will wait for additional guidance on this from core team

@alexrp alexrp self-assigned this Jul 19, 2025
@blblack
Copy link
Contributor

blblack commented Jul 21, 2025

I'd love to see these implemented as well (and std.c equivalents, and std.posix wrappers, but those could come separately/after this).

@jeffective - feel free to compare/steal anything from my abandoned old draft PR here (this was from before the std.os/posix re-org, no longer directly-applicabe) - https://github.com/ziglang/zig/pull/19203/commits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mlockall() and friends missing from std.os.linux
4 participants