Asyncronous IO and memory

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Asyncronous IO and memory

Post by Jeko »

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?
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Asyncronous IO and memory

Post by Korona »

I don't really understand your problem. Why don't you use a spinlock or a semaphore?
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].
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Asyncronous IO and memory

Post by Jeko »

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
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Asyncronous IO and memory

Post by Korona »

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)
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].
User avatar
thepowersgang
Member
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

Post by thepowersgang »

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
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Asyncronous IO and memory

Post by Korona »

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].
User avatar
Combuster
Member
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

Post by Combuster »

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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Asyncronous IO and memory

Post by jal »

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


JAL
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Asyncronous IO and memory

Post by Jeko »

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?
Yes. I need to lock a page because it mustn't be swapped or freed before the request is finished. I make an example:
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
Post Reply