I'm implementing asyncronous IO in my kernel, but I need a method to lock pages in memory.
I think I will lock the virtual memory associated to the async IO request to being swapped out.
Is this a good method? Or there is a better one?
Asyncronous IO and memory
Asyncronous IO and memory
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling
http://sourceforge.net/projects/jeko - Jeko Operating System
http://sourceforge.net/projects/jeko - Jeko Operating System
Re: Asyncronous IO and memory
I don't really understand your problem. Why don't you use a spinlock or a semaphore?
You could also implement lock-free algorithms.
You could also implement lock-free algorithms.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
Re: Asyncronous IO and memory
I must lock a page, because if that page is freed before the request is finished, then the request will use a memory that isn't allocated.
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling
http://sourceforge.net/projects/jeko - Jeko Operating System
http://sourceforge.net/projects/jeko - Jeko Operating System
Re: Asyncronous IO and memory
On a single processor machine you can simply deactivate interrupts every time you manipulate the page tables.
On a multiprocessor machine you'll have to use spinlocks or a similar synchronization mechanism. I use one spinlock per process to make sure that the page tables are not modified concurrently but of course there are other ways of doing that. (e.g. one spinlock per page table)
On a multiprocessor machine you'll have to use spinlocks or a similar synchronization mechanism. I use one spinlock per process to make sure that the page tables are not modified concurrently but of course there are other ways of doing that. (e.g. one spinlock per page table)
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
- thepowersgang
- Member

- Posts: 734
- Joined: Tue Dec 25, 2007 6:03 am
- Libera.chat IRC: thePowersGang
- Location: Perth, Western Australia
- Contact:
Re: Asyncronous IO and memory
you could try using one of the free bits in the page table entries to mark a page as "locked" and alter you paging code to check for this bit before altering a page table.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Re: Asyncronous IO and memory
That won't work if the page table is modified by two processors simultaneously.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
- Combuster
- Member

- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Asyncronous IO and memory
the x86 architecture has *lots* of read-modify-write instructions that you can use for this thing. Read up on lock cmpxchg and try to figure why this is one possible solution. (486 onward, so its present once you have a chance of multi-cpu systems)
With a bit of luck, lock bts may be good enough, which works even on a 386.
With a bit of luck, lock bts may be good enough, which works even on a 386.
Re: Asyncronous IO and memory
I take it you are using the page for device I/O using DMA? In that case, you should indeed somehow indicate to the memory manager not to swap out that page. But I guess you must already have such a method to prevent the kernel from being swapped out?Jeko wrote:I'm implementing asyncronous IO in my kernel, but I need a method to lock pages in memory.
I think I will lock the virtual memory associated to the async IO request to being swapped out.
JAL
Re: Asyncronous IO and memory
Yes. I need to lock a page because it mustn't be swapped or freed before the request is finished. I make an example:jal wrote:I take it you are using the page for device I/O using DMA? In that case, you should indeed somehow indicate to the memory manager not to swap out that page. But I guess you must already have such a method to prevent the kernel from being swapped out?
TASK A request to read 512 bytes to the address 0xB0000000 from the driver HARDDISK0
HARDDISK0 put the request in his request's queue.
While the harddisk is being readed TASK A can do everything.
Harddisk finishes reading
HARDDISK0 put the data readed into the physical address pointed by 0xB0000000.
So the physical pages mustn't be freed or swapped out before the request is finished.
jal I haven't a method to prevent the kernel from being swapped out... I think I will set a flag to page locked that prevent them to be swapped out.
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling
http://sourceforge.net/projects/jeko - Jeko Operating System
http://sourceforge.net/projects/jeko - Jeko Operating System