Page 1 of 1

Page management question

Posted: Mon Jun 23, 2008 8:29 pm
by midir
Hi all. I've been doing some studying on how to allocate pages in the physical memory manager. Seems to me there are two basic ways to do it: a bitmap of used/unused pages or a stack of unused pages (or some combination of the two). That much I understand okay.

At the moment I haven't done anything fancy. I've just got a stack of every unused 4 kB page in the 4 GB address space. The stack takes up 4 MB. I'm just wondering, how does this fit into the paging mechanism?? I've identity-mapped the first 8 MB where the kernel and page stack are to get around this for now. My assumption is that they will need to stay mapped into every process's address space. Is this correct?

I've also read/realised that every time the number of free pages drops by 1024 I've got another whole 4 kB page not being used by the stack, and that potentially I could send this out to the VMM as an available page, to avoid having to waste the whole 4 megabytes. But how?

Problem is if the virtual memory manager starts remapping that page, don't I lose my identity mapping on the first 8 MB? How do I / can I access my page stack if it's not identity-mapped? What happens when some other pages become unused and need to be added to the stack, so the stack needs to grow bigger, but the VMM hasn't given back the page I need? It wouldn't be a problem if *any* page was suitable, if the page stack was within the linear virtual memory system, but since it's not, I have to get back the exact physical pages that made up the stack in the first place.

I'm fairly confused about it now. There must be a magical answer to this because I don't see how either naïve bitmaps or stacks could handle the amount of pages once you move up to 64-bit.

Ideas?

Re: Page management question

Posted: Tue Jun 24, 2008 1:38 am
by JamesM
Hi,
At the moment I haven't done anything fancy. I've just got a stack of every unused 4 kB page in the 4 GB address space. The stack takes up 4 MB. I'm just wondering, how does this fit into the paging mechanism?? I've identity-mapped the first 8 MB where the kernel and page stack are to get around this for now. My assumption is that they will need to stay mapped into every process's address space. Is this correct?
Yes, they will need to be mapped into every process' address space.
I've also read/realised that every time the number of free pages drops by 1024 I've got another whole 4 kB page not being used by the stack, and that potentially I could send this out to the VMM as an available page, to avoid having to waste the whole 4 megabytes. But how?

Problem is if the virtual memory manager starts remapping that page, don't I lose my identity mapping on the first 8 MB? How do I / can I access my page stack if it's not identity-mapped? What happens when some other pages become unused and need to be added to the stack, so the stack needs to grow bigger, but the VMM hasn't given back the page I need? It wouldn't be a problem if *any* page was suitable, if the page stack was within the linear virtual memory system, but since it's not, I have to get back the exact physical pages that made up the stack in the first place.
I think you're becoming a little confused here. When you get that page free, you can send it out to be reallocated but you send it out to the physical memory manager, not the virtual memory manager. That region of virtual memory should stay reserved for your stack should it have to grow again, but the physical page underneath can be safely released (and added to your free page stack).

Note the recursiveness here. If your stack shrinks too much, you can release a page (which makes it grow by one), so if you implement this naively you will end up with a stack that never shrinks (it's about to shrink, but then a page gets released so it grows by one and so you need that page back to hold the larger stack...). That's something to think about in your implementation.

Cheers,

James

Re: Page management question

Posted: Tue Jun 24, 2008 4:09 am
by Brendan
Hi,
midir wrote:Problem is if the virtual memory manager starts remapping that page, don't I lose my identity mapping on the first 8 MB? How do I / can I access my page stack if it's not identity-mapped? What happens when some other pages become unused and need to be added to the stack, so the stack needs to grow bigger, but the VMM hasn't given back the page I need? It wouldn't be a problem if *any* page was suitable, if the page stack was within the linear virtual memory system, but since it's not, I have to get back the exact physical pages that made up the stack in the first place.
For a stack, there's only 2 operations you need to do: "pop" and "push". For both of these operations you only really need to access the top of the stack, and you never really need to access anything else. Therefore you never really need to have anything (except the top of the stack) mapped into the linear address space.

For an example (ignoring things like "totalFreePages" counters and re-entrancy locks), I normally only use a single variable for a free page stack: the physical address of the first page on the stack.

When a page is freed I copy the current "physical address of the first page on the stack" into the first dword of the page being freed, and then the physical address of the page being freed becomes the new "physical address of the first page on the stack".

When a page is allocated I just use the "physical address of the first page on the stack"; and the first dword from the allocated page becomes the new "physical address of the first page on the stack".

In this way one free page stack costs 4 bytes (ignoring things like "totalFreePages" counters and re-entrancy locks), which is typically in the kernel's data section.

The only tricky part is when you're using paging - in this case you need to map a physical page into the address space before you can access it. This isn't hard to figure out (if you understand what I said above)...

When a page is freed I copy the current "physical address of the first page on the stack" into the first dword of the page being freed (at it's linear address), and then the physical address of the page being freed (e.g. from the page table entry) becomes the new "physical address of the first page on the stack", and then I remove the page from the linear address space (and then invalidate TLBs).

When a page is allocated I just use the "physical address of the first page on the stack" (e.g. use it to make a new page table entry); and then invalidate TLBs; and then get the first dword from the allocated page (at it's new linear address) which becomes the new "physical address of the first page on the stack".

Now, if you've understood all of this the next step is to realise that a free page stack needs a re-entrancy lock (especially if you support "multi-CPU"). However, only one CPU can have access to a free page stack at one time, and if there's lots and lots of CPUs trying to acquire the re-entrancy lock then only one CPU will succeed and the rest will have to wait for it to be free. This causes poor performance when there's lots of CPUs (or, poor scalability). For an example, if there's 128 CPUs then (worst case) 127 CPUs could be getting nothing done.

To avoid this performance/scalability problem you could use lots of re-entrancy locks, but that means lots of separate free page stacks. Isn't it lucky that they only cost 4 bytes (or 8 bytes for 64-bit systems) each (ignoring things like "totalFreePages" counters and re-entrancy locks)? :)

So now we've got lots of free page stacks. I typically use up to 1024 of them, where each free page stack costs 16 bytes (e.g. 8 bytes for the "physical address of the first page on the stack", 4 bytes for the re-entrancy lock, 4 bytes for number of pages on the stack). That adds up to 16 KB of data in kernel space. The interesting thing is that you can use different stacks for different purposes. Page colouring (or cache colouring) is the first thing I'd consider, as improving cache efficiency is always a good idea. For this you just use a different free page stack for each different page/cache colour - it's fairly simple to implement when you think about it...


Cheers,

Brendan

Re: Page management question

Posted: Tue Jun 24, 2008 7:28 am
by egos
midir wrote:At the moment I haven't done anything fancy. I've just got a stack of every unused 4 kB page in the 4 GB address space. The stack takes up 4 MB. I'm just wondering, how does this fit into the paging mechanism?? I've identity-mapped the first 8 MB where the kernel and page stack are to get around this for now. My assumption is that they will need to stay mapped into every process's address space. Is this correct?
Yes.
midir wrote:I've also read/realised that every time the number of free pages drops by 1024 I've got another whole 4 kB page not being used by the stack, and that potentially I could send this out to the VMM as an available page, to avoid having to waste the whole 4 megabytes. But how?
Early I used the dynamic stack mapping. It had the following structure:
txxx...txxx...txxx...
where t is the page table address for current 4 Kb stack frame, x is the free page address.

But now I'm using stack with the following structure:
tt...xxxxxx...
midir wrote:Problem is if the virtual memory manager starts remapping that page, don't I lose my identity mapping on the first 8 MB? How do I / can I access my page stack if it's not identity-mapped? What happens when some other pages become unused and need to be added to the stack, so the stack needs to grow bigger, but the VMM hasn't given back the page I need? It wouldn't be a problem if *any* page was suitable, if the page stack was within the linear virtual memory system, but since it's not, I have to get back the exact physical pages that made up the stack in the first place.
"Identity-mapped" kernel and free page stack is not very good idea! Map both dynamically.

Re: Page management question

Posted: Tue Jun 24, 2008 8:13 am
by midir
Thanks for the detailed replies here everyone. Sorry for the long post but there's a lot of theory here.
When you get that page free, you can send it out to be reallocated but you send it out to the physical memory manager, not the virtual memory manager.
Hmmm.... I thought this was the same thing, no? If I get a page making up the stack that isn't needed, either I add its physical address to the stack for later, or wait until the VMM actually requests a page and send out the unneeded page of the stack directly without ever storing its address on the stack (avoiding the problem of having it grow by 1 immediately after trying to shrink it).

But the page stack in my kernel is still a contiguous 4 MB range at the low end of *physical* memory (identity-mapped virtual memory), so I still have the problem of needing the same physical pages back. I can only see two workarounds to this:

1) When the PMM needs its physical stack pages back, it calls a VMM method, VMM_Swap_2_Pages() or something, to ask the VMM to get back the needed page from whatever app is using it, giving the app a different page instead. This sounds very messy though.

2) Or, as egos suggests, map the page stack dynamically. I guess this means having the PMM and VMM live in symbiosis where the entire physical page stack is in a *linear* address space, with the pages for this managed by the VMM, which gets its pages from the PMM...... it sounds good but I can't seem to get my head around this method. :(
When a page is freed I copy the current "physical address of the first page on the stack" into the first dword of the page being freed, and then the physical address of the page being freed becomes the new "physical address of the first page on the stack".

When a page is allocated I just use the "physical address of the first page on the stack"; and the first dword from the allocated page becomes the new "physical address of the first page on the stack".
Brendan, it's taken me ages and ages to understand your post but I think I do. It's a sort of linked-list, where the low 4 bytes of each 4 kB page are a link to the previous free page. It's genius from a memory-efficiency perspective and avoids the problem of the stack needing to be contiguous, but begs the new problem of how to store a value at a physical address which may or may not be mapped anywhere in virtual memory...

It would be nice if the x86 had a "use physical addressing" prefix opcode.
When a page is freed I copy the current "physical address of the first page on the stack" into the first dword of the page being freed (at it's linear address), and then the physical address of the page being freed (e.g. from the page table entry) becomes the new "physical address of the first page on the stack", and then I remove the page from the linear address space (and then invalidate TLBs).

When a page is allocated I just use the "physical address of the first page on the stack" (e.g. use it to make a new page table entry); and then invalidate TLBs; and then get the first dword from the allocated page (at it's new linear address) which becomes the new "physical address of the first page on the stack".
I only sort of understand this. But I don't know whose virtual address space the pages would be mapped into. Can the kernel code be called from an app's address space? I don't even know. :?


Page coloring is a whole new concept on me... It sorta looks promising for performance but it's beyond what I have the skills to code right now.

I might have to stick with something simple for now, so that I can at least get something basic working.

Re: Page management question

Posted: Tue Jun 24, 2008 8:49 am
by JamesM
But the page stack in my kernel is still a contiguous 4 MB range at the low end of *physical* memory (identity-mapped virtual memory), so I still have the problem of needing the same physical pages back. I can only see two workarounds to this:
Why do you need the same page back? Like I said in my previous post, even if you start out with the lower 4MB identity mapped, why does it have to continue to be identity mapped? What advantage does it give you? Why do you need the exact same physical page back again?

Re: Page management question

Posted: Thu Jun 26, 2008 8:30 pm
by midir
I sort of got the page stack sorted out now. It commits and uncommits the pages of the stack with the VMM as it goes along.
JamesM wrote:Why do you need the same page back? Like I said in my previous post, even if you start out with the lower 4MB identity mapped, why does it have to continue to be identity mapped? What advantage does it give you? Why do you need the exact same physical page back again?
I don't know, I just thought that I did. I didn't realise I could change the kernel directory as it ran I think (I'm not very good at this, learning fast though :) ) Thanks for the help everyone!