Thread #108582499
HomeIndexCatalogAll ThreadsNew ThreadReply
H
previous: >>108572928

#define __NR_mlock                    149
#define __NR_munlock 150
#define __NR_mlockall 151
#define __NR_munlockall 152
#define __NR_mlock2 325

https://man7.org/linux/man-pages/man2/mlock.2.html

tl;dr:
lock/unlock memory

by lock, we mean keep the memory pinned into RAM and prevent it from being swapped out
for once, i'm actually really happy with these syscalls. super nice change of pace. having to have a v2 of something isn't ideal, of course, but i'm still of the opinion that these syscalls in particular are really well designed. i think the flags are clear, sane, and useful. so i'm actually quite impressed. kudos to the developers on this one
interestingly, this syscall is one of the very few realistic ways for a developer to cause calls to malloc to fail. if you've never played around with it, it's actually surprisingly difficult to make that happen lol. so that's a neat piece of trivia
the notes section is also pretty good. very nice writeup on the "why" for these syscalls, as well as potential footguns, etc.
god this really is a breath of fresh air. more syscalls need to be like this...

relevant resources:
man man

man syscalls

https://man7.org/linux/man-pages/
https://linux.die.net/man/
https://elixir.bootlin.com/linux/
https://elixir.bootlin.com/musl/
https://elixir.bootlin.com/glibc/
+Showing all 24 replies.
>>
>>108582499
>interestingly, this syscall is one of the very few realistic ways for a developer to cause calls to malloc to fail
There's no equivalent of VirtualAlloc with MEM_COMMIT on Linux?
>>
>>108582536
oh, no, mmap has MAP_POPULATE, MAP_FIXED, etc.
https://man7.org/linux/man-pages/man2/mmap.2.html
i meant literally malloc(3) itself, not the syscalls it uses
>>
>>108582561
Oh, okay. How do you do it?
>>
>>108582644
cause malloc to fail with this?
       If MCL_FUTURE has been specified, then a later system call (e.g.,
mmap(2), sbrk(2), malloc(3)), may fail if it would cause the
number of locked bytes to exceed the permitted maximum (see
below). In the same circumstances, stack growth may likewise
fail: the kernel will deny stack expansion and deliver a SIGSEGV
signal to the process.
>>
>>108582499
>v2 of something isn't ideal
BSDs wouldn't have messed up the first time btw
>>
>>108582702
>then a later system call
>e.g. malloc(3)
That's a strange thing to write in a man page. Is this glibc-specific?
>>
>>108582742
i also noticed that lol
it's technically slightly incorrect, but malloc calls one of the first two under the hood, so i can forgive it
>>
No usecase.
>>
>>108583191
cryptography
>>
>>108583264
Encrypt your swap.
>>
>>108582536
You could enable overcommit and then use PROT_NONE to reserve VAS without your commit charge going up.
>>
>>108583369
>enable
Errr, disable.
>>
>>108582499
me on the right
>>
>>108583387
hi...
>>
>>108583191
Ring buffers in real time critical applications, i.e. sound servers.
>>
>>108584398
That shit belongs in the kernel though.
>>
>>108584412
You want to be doing audio mixing in kernelspace?
>>
>>108584442
Like ALSA, yeah.
>>
>>108584442
>>108584454
>>108584412
eBPF kernel mixer when
>>
>>108584691
The moment they finally admit that a separation between mechanism and policy doesn't make sense in a monolithic kernel.
So basically never.
>>
>>108583290
not good enough
>>
>>108585784
Why not?
>>
How is kernel and system programming in rust like compared to c #include <sys/header>?
Does it end up invoking the same syscalls in the kernel. And if that’s the case, rust essentially being a wrapper around syscalls, why should I use rust?
>>
>>108586266
>Does it end up invoking the same syscalls in the kernel.
I think the most straightforward way is to just use bindings to libc
low level rust is surprisingly similar to C
>why should I use rust?
I might be weird, but I really like sumtypes+tuples+match statement
the stdlib is also miles better than libc

Reply to Thread #108582499


Supported: JPG, PNG, GIF, WebP, WebM, MP4, MP3 (max 4MB)