Re: [RFC MM 4/4] speculative page fault

Minchan Kim
2009-11-13T15:59:58Z

On Fri, Nov 13, 2009 at 4:41 PM, KAMEZAWA Hiroyuki
<kamezawa.hiroyu@jp.fujitsu.com> wrote:
> Speculative page fault.
>
>  This patch tries to implement speculative page fault.
>  Do page fault without taking mm->semaphore and check tag mm->generation
>  after taking page table lock. If generation is modified, someone took
>  write lock on mm->semaphore and we need to take read lock.
>
>  Now, hugepage is not handled. And stack page is not handled because
>  it can change [vm_start, vm_end).
>
> Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
> ---
>  arch/x86/mm/fault.c |   54 ++++++++++++++++++++++++++--------------
>  include/linux/mm.h  |    2 -
>  mm/memory.c         |   70 ++++++++++++++++++++++++++++++++--------------------
>  3 files changed, 81 insertions(+), 45 deletions(-)
>
> Index: mmotm-2.6.32-Nov2/arch/x86/mm/fault.c
> ===================================================================
> --- mmotm-2.6.32-Nov2.orig/arch/x86/mm/fault.c
> +++ mmotm-2.6.32-Nov2/arch/x86/mm/fault.c
> @@ -11,6 +11,7 @@
>  #include <linux/kprobes.h>             /* __kprobes, ...               */
>  #include <linux/mmiotrace.h>           /* kmmio_handler, ...           */
>  #include <linux/perf_event.h>          /* perf_sw_event                */
> +#include <linux/hugetlb.h>             /* is_vm_hugetlbe_page()...     */
>
>  #include <asm/traps.h>                 /* dotraplinkage, ...           */
>  #include <asm/pgalloc.h>               /* pgd_*(), ...                 */
> @@ -952,7 +953,8 @@ do_page_fault(struct pt_regs *regs, unsi
>        struct mm_struct *mm;
>        int write;
>        int fault;
> -       int cachehit = 0;
> +       int cachehit;
> +       unsigned int key;
>
>        tsk = current;
>        mm = tsk->mm;
> @@ -1057,6 +1059,18 @@ do_page_fault(struct pt_regs *regs, unsi
>         * validate the source. If this is invalid we can skip the address
>         * space check, thus avoiding the deadlock:
>         */
> +        if ((error_code & PF_USER) &&
> +            (mm->generation == current->mm_generation) && current->vma_cache) {
> +               vma = current->vma_cache;
> +               if ((vma->vm_start <= address) && (address < vma->vm_end)) {
> +                       key = mm->generation;
> +                       cachehit = 1;
> +                       goto got_vma;
> +               }
> +       }
> +speculative_fault_retry:
> +       cachehit = 0;
> +       vma = NULL;
>        if (unlikely(!mm_reader_trylock(mm))) {
>                if ((error_code & PF_USER) == 0 &&
>                    !search_exception_tables(regs->ip)) {
> @@ -1072,13 +1086,9 @@ do_page_fault(struct pt_regs *regs, unsi
>                 */
>                might_sleep();
>        }
> -       if ((mm->generation == current->mm_generation) && current->vma_cache) {
> -               vma = current->vma_cache;
> -               if ((vma->vm_start <= address) && (address < vma->vm_end))
> -                       cachehit = 1;
> -       }
> -       if (!cachehit)
> -               vma = find_vma(mm, address);
> +       key = mm->generation;
> +       vma = find_vma(mm, address);
> +got_vma:
>        if (unlikely(!vma)) {
>                bad_area(regs, error_code, address);
>                return;
> @@ -1123,13 +1133,17 @@ good_area:
>         * make sure we exit gracefully rather than endlessly redo
>         * the fault:
>         */
> -       fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
> +       fault = handle_mm_fault(mm, vma, address,
> +               write ? FAULT_FLAG_WRITE : 0, key);
>
>        if (unlikely(fault & VM_FAULT_ERROR)) {
>                mm_fault_error(regs, error_code, address, fault);
>                return;
>        }
>
> +       if (mm->generation != key)
> +               goto speculative_fault_retry;
> +

You can use match_key in here again. :)

--
Kind regards,
Minchan Kim