How to artificially cause a page fault in linux kernel?

Why is Linux kernel unable to handle page faults generated with in the kernel?

  • Kernel cannot handle its own page fault.  That is the reason why we have to use copyt_to_user and copy_from_user. Why can't it do as following: If a page fault happens an interrupt will be raised and that invokes the kernel , now the kernel can load the required pages and return so the kernel functions which caused page fault can continue. NOTE : The question is not about the kernel memory being pageable.

  • Answer:

    It is not true, copy_to_user, copy_from_user can in fact refer to adresses which can trigger page fault in kernel mode. If the page fault could not be resolved because it is invalid, it is because the access is regulated via the copy_to_user and copy_from_user, the kernel is able to propagate EFAULT to the user. So copy_to_user and copy_from_user provide protection, first it provides protection ensuring the address is a valid user space address and handling the page fault in case of invalid pointer sending EFAULT to user rather than crashing the kernel. For exact details, please refer the documentation in the linux kernel how this is handled, please refer https://www.kernel.org/doc/Documentation/x86/exception-tables.txt In fact i answered the earlier assuming you meant why the kernel memory not being pageable. Anyway i am retaining it (though you edited adding some more clarity). NOTE: It guess it makes the kernel design a lot simpler like if kernel data structures become page able, there would be complexity around locking and careful consideration need to be taken into account to avoid deadlocks and performance issues.(say for ex,  the storage driver code to bring back the page in could be paged out which can cause a page fault which again the page fault handler  has to depend on the storage driver code to bring back the page in (if paged out memory is on this storage). Also linux kernel before 2.6 was non preempt able which makes it easy not to have pageable kernel memory. The only downside kernel memory usage needs to be under control given linux also allows dynamic modules. Also certain unix kernel like AIX make the kernel memory pageable.

Thayumanavar Sachithanantham at Quora Visit the source

Was this solution helpful to you?

Other answers

A very detailed explanation can be found at:https://www.kernel.org/doc/Documentation/x86/exception-tables.txtThis explains the handling of a page fault in kernel mode. It focuses on the case where the address passed has no mapping. In case a the page needed to be paged in, the Linux kernel can ( and does) do so only if the fault occurred in a schedulable context. Paging in necessitates waiting for I/O, i.e. sleeping. Hence, paging cannot happen in case the fault occurs in an atomic context (interrupt, spin lock held, etc).There is a significant amount of material available online to explain more on attempting to sleep while in an atomic context. So, unless necessary, I shall avoid that part here.

Udipta Das

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.