2

When a page is created for a process (which will be mapped into process address space), will that page be mapped into kernel address space ?

If not, then it won't have kernel virtual address. Then how the swapper will find the page and swap that out, if a need arises ?

viji
  • 2,706
  • 5
  • 28
  • 34

1 Answers1

3

If we're talking about the x86 or similar (in terms of page translation) architectures, at any given time there's one virtual address space and normally one part of it is reserved for the kernel and the other for user-mode processes.

On a context switch between two processes only the user-mode part of the virtual address space changes.

With such an organization, the kernel always has full access to the current user-mode process, because, again, there's only one current virtual address space at any moment for both the kernel and a user-mode process, it's not two, it's one. So, the kernel doesn't really have to have another, extra mapping for user-mode pages. But that's not the main point.

The main point is that the kernel keeps some sort of statistics for every page that if needed can be saved to the disk and reused elsewhere. The CPU marks each page's page table entry (PTE) as accessed when the page is first read from or written to and as dirty when it's first written to.

The kernel scans the PTEs periodically, reads the accessed and dirty markers to update said statistics and clears accessed and dirty so it can detect a change in them later (of course, if any). Based on this statistics it determines which pages are rarely used or long unused and can be repurposed.

If the "swapper" runs in the context of the current process and if it runs in the kernel, then in theory it has enough information from the kernel (the list of rarely used or long unused pages to save and unmap if dirty or just unmap if not dirty) and sufficient access to the pages of interest.

If the "swapper" itself runs as a user-mode process, things become more complicated because it doesn't have access to another process' pages by default and has to either create a mapping or ask the kernel do some extra work for it in the context of the process of interest.

So, finding rarely used and long unused pages and their addresses occurs in the kernel. The CPU helps by automatically marking PTEs as accessed and dirty. There may need to be an extra mapping to dirty pages if they get saved to the disk not in the context of the process that owns them.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • Are you saying that kernel (letz say linux) will run always in process context (in x86 architecture) ? If that, do you have any idea about how linux kernel accesss the page(for process that is not in context) that needs to be swapped out ? And one more, what if no process is running (in theory) ?\ – viji Apr 06 '12 at 14:13
  • It can either switch to that process' context or create a mapping without switching. Which of the two it does I don't know. – Alexey Frunze Apr 06 '12 at 14:18
  • Then why there are 3 types of context ? (kernel context, interrupt context, process context) [http://www.learninglinuxkernel.com/Introduction_03.html](http://www.learninglinuxkernel.com/Introduction_03.html) – viji Apr 06 '12 at 14:25
  • You need to learn more about CPUs and operating systems and what is meant by the word context in every context (sorry for the tautology). Get some Tanenbaum's book (Minix or Modern Operating Systems) to fill in the gaps. – Alexey Frunze Apr 07 '12 at 03:42
  • But kernel threads won't be running in process context. So it will have access only to kernel virtual memory at that time. – viji Apr 20 '12 at 04:31