Page 1 of 1
Asyncronous IO and memory
Posted: Wed Jul 30, 2008 12:15 pm
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?
Re: Asyncronous IO and memory
Posted: Wed Jul 30, 2008 12:40 pm
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.
Re: Asyncronous IO and memory
Posted: Wed Jul 30, 2008 4:04 pm
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.
Re: Asyncronous IO and memory
Posted: Wed Jul 30, 2008 4:39 pm
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)
Re: Asyncronous IO and memory
Posted: Wed Jul 30, 2008 7:20 pm
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.
Re: Asyncronous IO and memory
Posted: Thu Jul 31, 2008 2:18 am
by Korona
That won't work if the page table is modified by two processors simultaneously.
Re: Asyncronous IO and memory
Posted: Thu Jul 31, 2008 2:57 am
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.
Re: Asyncronous IO and memory
Posted: Thu Jul 31, 2008 7:24 am
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
Re: Asyncronous IO and memory
Posted: Thu Jul 31, 2008 12:59 pm
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.