Possibly dumb question, but if I have some code that tries to blindly jump to, say, address 0x4000, would userfaultfd let me redirect accesses to 0x4000 to an address I've allocated elsewhere?
Sort of, but not exactly. You can't redirect the program to a different address, but you can respond to the fault by mapping memory at address 0x4000 and placing whatever you want in it. Perhaps, you can map the memory as an alias of some other page, so that they refer to the same underlying physical memory. But the program will still see the memory as living at 0x4000, it won't jump to a different address. (Though I…
Userfaultfd – Create a file descriptor for handling page faults in user space
11–18 of 18 posts
Re: Userfaultfd – Create a file descriptor for handling page faults in user space
#12Earlier quoted context omitted.
Sort of, but not exactly. You can't redirect the program to a different address, but you can respond to the fault by mapping memory at address 0x4000 and placing whatever you want in it. Perhaps, you can map the memory as an alias of some other page, so that they refer to the same underlying physical memory. But the program will still see the memory as living at 0x4000, it won't jump to a different address. (Though I…
I think specifically for 0x4000 you can't map anything there with a default Linux kernel configuration. The minimum address where you can map things now is 0x10000. This broke my StoneKnifeForth and Max Bernstein fixed it: https://github.com/tekknolagi/stoneknifecpp/commit/905105b44...
Re: Userfaultfd – Create a file descriptor for handling page faults in user space
#13Earlier quoted context omitted.
Sort of, but not exactly. You can't redirect the program to a different address, but you can respond to the fault by mapping memory at address 0x4000 and placing whatever you want in it. Perhaps, you can map the memory as an alias of some other page, so that they refer to the same underlying physical memory. But the program will still see the memory as living at 0x4000, it won't jump to a different address. (Though I…
I think specifically for 0x4000 you can't map anything there with a default Linux kernel configuration. The minimum address where you can map things now is 0x10000. This broke my StoneKnifeForth and Max Bernstein fixed it: https://github.com/tekknolagi/stoneknifecpp/commit/905105b44...
Re: Userfaultfd – Create a file descriptor for handling page faults in user space
#14One of my favorite examples of userfaultfd: transparently mapping databases on cloud storage into RAM. https://tech.nextroll.com/blog/data/2016/11/29/traildb-mmap-...
Re: Userfaultfd – Create a file descriptor for handling page faults in user space
#15Earlier quoted context omitted.
I think specifically for 0x4000 you can't map anything there with a default Linux kernel configuration. The minimum address where you can map things now is 0x10000. This broke my StoneKnifeForth and Max Bernstein fixed it: https://github.com/tekknolagi/stoneknifecpp/commit/905105b44...
Technically Tom fixed it :) He's the Linux whisperer. I just submitted the patch
Re: Userfaultfd – Create a file descriptor for handling page faults in user space
#16Possibly dumb question, but if I have some code that tries to blindly jump to, say, address 0x4000, would userfaultfd let me redirect accesses to 0x4000 to an address I've allocated elsewhere?
You don't need userfaultfd for that. Just mmap the same file twice, at different addresses: int mem_fd = memfd_create("", 0); ftruncate(fd, 4096); void* mem = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0); void* alias = mmap((void*) 0x4000, 4096, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, mem_fd, 0); close(mem_fd); // alias now points to the same physical memory as mem This is commonly used to…
Re: Userfaultfd – Create a file descriptor for handling page faults in user space
#17Earlier quoted context omitted.
You don't need userfaultfd for that. Just mmap the same file twice, at different addresses: int mem_fd = memfd_create("", 0); ftruncate(fd, 4096); void* mem = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0); void* alias = mmap((void*) 0x4000, 4096, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, mem_fd, 0); close(mem_fd); // alias now points to the same physical memory as mem This is commonly used to…
Do you have a link or can you suggest search terms for an example? I can't imagine that being useful, as you have to wrap around at some point anyway.
(If your buffer is bigger than a page, this will still work, just use bigger addresses, etc. if your buffer is smaller than a page, you're stuck)
Re: Userfaultfd – Create a file descriptor for handling page faults in user space
#18Possibly dumb question, but if I have some code that tries to blindly jump to, say, address 0x4000, would userfaultfd let me redirect accesses to 0x4000 to an address I've allocated elsewhere?
Sort of, but not exactly. You can't redirect the program to a different address, but you can respond to the fault by mapping memory at address 0x4000 and placing whatever you want in it. Perhaps, you can map the memory as an alias of some other page, so that they refer to the same underlying physical memory. But the program will still see the memory as living at 0x4000, it won't jump to a different address. (Though I…