Re: [PATCH 1/2] ARM: Pass IFSR register to do_PrefetchAbort()

From: Kirill A. Shutemov
Date: Sun Sep 20 2009 - 10:34:40 EST


On Sun, Sep 20, 2009 at 3:24 PM, Russell King - ARM Linux
<linux@xxxxxxxxxxxxxxxx> wrote:
> On Sun, Sep 20, 2009 at 12:35:19PM +0300, Kirill A. Shutemov wrote:
>> Ok, so __do_page_fault() should know where we are: in data abort or in
>> prefetch abort. What is right way to do it? Should we create one more
>> argument or use one of reserved bits IFSR?
>
> Well, this is my solution to the problem - could you test it please?

I'll test it on Monday.

Do you want to ignore IFSR? I don't think that it's a good idea. We will
get infinite loop of faults on an unexpected for kernel type of fault, like
we have now for permission faults. Better to call do_bad() in this case.

> This patch is actually the result of several patches merged together,
> so I won't supply a proper description etc.
>
> diff --git a/arch/arm/mm/fault.c b/arch/arm/mm/fault.c
> index cc8829d..8fbb22d 100644
> --- a/arch/arm/mm/fault.c
> +++ b/arch/arm/mm/fault.c
> @@ -25,6 +25,19 @@
>
> Â#include "fault.h"
>
> +/*
> + * Fault status register encodings. ÂWe steal bit 31 for our own purposes.
> + */
> +#define FSR_LNX_PF Â Â Â Â Â Â (1 << 31)
> +#define FSR_WRITE Â Â Â Â Â Â Â(1 << 11)
> +#define FSR_FS4 Â Â Â Â Â Â Â Â Â Â Â Â(1 << 10)
> +#define FSR_FS3_0 Â Â Â Â Â Â Â(15)
> +
> +static inline int fsr_fs(unsigned int fsr)
> +{
> + Â Â Â return (fsr & FSR_FS3_0) | (fsr & FSR_FS4) >> 6;
> +}
> +
> Â#ifdef CONFIG_MMU
>
> Â#ifdef CONFIG_KPROBES
> @@ -182,18 +195,35 @@ void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
> Â#define VM_FAULT_BADMAP Â Â Â Â Â Â Â Â0x010000
> Â#define VM_FAULT_BADACCESS Â Â 0x020000
>
> -static int
> +/*
> + * Check that the permissions on the VMA allow for the fault which occurred.
> + * If we encountered a write fault, we must have write permission, otherwise
> + * we allow any permission.
> + */
> +static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
> +{
> + Â Â Â unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
> +
> + Â Â Â if (fsr & FSR_WRITE)
> + Â Â Â Â Â Â Â mask = VM_WRITE;
> + Â Â Â if (fsr & FSR_LNX_PF)
> + Â Â Â Â Â Â Â mask = VM_EXEC;
> +
> + Â Â Â return vma->vm_flags & mask ? false : true;
> +}
> +
> +static noinline int __kprobes
> Â__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
> Â Â Â Â Â Â Â Âstruct task_struct *tsk)
> Â{
> Â Â Â Âstruct vm_area_struct *vma;
> - Â Â Â int fault, mask;
> + Â Â Â int fault;
>
> Â Â Â Âvma = find_vma(mm, addr);
> Â Â Â Âfault = VM_FAULT_BADMAP;
> - Â Â Â if (!vma)
> + Â Â Â if (unlikely(!vma))
> Â Â Â Â Â Â Â Âgoto out;
> - Â Â Â if (vma->vm_start > addr)
> + Â Â Â if (unlikely(vma->vm_start > addr))
> Â Â Â Â Â Â Â Âgoto check_stack;
>
> Â Â Â Â/*
> @@ -201,47 +231,24 @@ __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
> Â Â Â Â * memory access, so we can handle it.
> Â Â Â Â */
> Âgood_area:
> - Â Â Â if (fsr & (1 << 11)) /* write? */
> - Â Â Â Â Â Â Â mask = VM_WRITE;
> - Â Â Â else
> - Â Â Â Â Â Â Â mask = VM_READ|VM_EXEC|VM_WRITE;
> -
> - Â Â Â fault = VM_FAULT_BADACCESS;
> - Â Â Â if (!(vma->vm_flags & mask))
> + Â Â Â if (access_error(fsr, vma)) {
> + Â Â Â Â Â Â Â fault = VM_FAULT_BADACCESS;
> Â Â Â Â Â Â Â Âgoto out;
> + Â Â Â }
>
> Â Â Â Â/*
> - Â Â Â Â* If for any reason at all we couldn't handle
> - Â Â Â Â* the fault, make sure we exit gracefully rather
> - Â Â Â Â* than endlessly redo the fault.
> + Â Â Â Â* If for any reason at all we couldn't handle the fault, make
> + Â Â Â Â* sure we exit gracefully rather than endlessly redo the fault.
> Â Â Â Â */
> -survive:
> - Â Â Â fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & (1 << 11)) ? FAULT_FLAG_WRITE : 0);
> - Â Â Â if (unlikely(fault & VM_FAULT_ERROR)) {
> - Â Â Â Â Â Â Â if (fault & VM_FAULT_OOM)
> - Â Â Â Â Â Â Â Â Â Â Â goto out_of_memory;
> - Â Â Â Â Â Â Â else if (fault & VM_FAULT_SIGBUS)
> - Â Â Â Â Â Â Â Â Â Â Â return fault;
> - Â Â Â Â Â Â Â BUG();
> - Â Â Â }
> + Â Â Â fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0);
> + Â Â Â if (unlikely(fault & VM_FAULT_ERROR))
> + Â Â Â Â Â Â Â return fault;
> Â Â Â Âif (fault & VM_FAULT_MAJOR)
> Â Â Â Â Â Â Â Âtsk->maj_flt++;
> Â Â Â Âelse
> Â Â Â Â Â Â Â Âtsk->min_flt++;
> Â Â Â Âreturn fault;
>
> -out_of_memory:
> - Â Â Â if (!is_global_init(tsk))
> - Â Â Â Â Â Â Â goto out;
> -
> - Â Â Â /*
> - Â Â Â Â* If we are out of memory for pid1, sleep for a while and retry
> - Â Â Â Â*/
> - Â Â Â up_read(&mm->mmap_sem);
> - Â Â Â yield();
> - Â Â Â down_read(&mm->mmap_sem);
> - Â Â Â goto survive;
> -
> Âcheck_stack:
> Â Â Â Âif (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
> Â Â Â Â Â Â Â Âgoto good_area;
> @@ -278,6 +285,13 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
> Â Â Â Â Â Â Â Âif (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
> Â Â Â Â Â Â Â Â Â Â Â Âgoto no_context;
> Â Â Â Â Â Â Â Âdown_read(&mm->mmap_sem);
> + Â Â Â } else {
> + Â Â Â Â Â Â Â /*
> + Â Â Â Â Â Â Â Â* The above down_read_trylock() might have succeeded in
> + Â Â Â Â Â Â Â Â* which case, we'll have missed the might_sleep() from
> + Â Â Â Â Â Â Â Â* down_read()
> + Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â might_sleep();
> Â Â Â Â}
>
> Â Â Â Âfault = __do_page_fault(mm, addr, fsr, tsk);
> @@ -289,6 +303,16 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
> Â Â Â Âif (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
> Â Â Â Â Â Â Â Âreturn 0;
>
> + Â Â Â if (fault & VM_FAULT_OOM) {
> + Â Â Â Â Â Â Â /*
> + Â Â Â Â Â Â Â Â* We ran out of memory, call the OOM killer, and return to
> + Â Â Â Â Â Â Â Â* userspace (which will retry the fault, or kill us if we
> + Â Â Â Â Â Â Â Â* got oom-killed)
> + Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â pagefault_out_of_memory();
> + Â Â Â Â Â Â Â return 0;
> + Â Â Â }
> +
> Â Â Â Â/*
> Â Â Â Â * If we are in kernel mode at this point, we
> Â Â Â Â * have no context to handle this fault with.
> @@ -296,16 +320,6 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
> Â Â Â Âif (!user_mode(regs))
> Â Â Â Â Â Â Â Âgoto no_context;
>
> - Â Â Â if (fault & VM_FAULT_OOM) {
> - Â Â Â Â Â Â Â /*
> - Â Â Â Â Â Â Â Â* We ran out of memory, or some other thing
> - Â Â Â Â Â Â Â Â* happened to us that made us unable to handle
> - Â Â Â Â Â Â Â Â* the page fault gracefully.
> - Â Â Â Â Â Â Â Â*/
> - Â Â Â Â Â Â Â printk("VM: killing process %s\n", tsk->comm);
> - Â Â Â Â Â Â Â do_group_exit(SIGKILL);
> - Â Â Â Â Â Â Â return 0;
> - Â Â Â }
> Â Â Â Âif (fault & VM_FAULT_SIGBUS) {
> Â Â Â Â Â Â Â Â/*
> Â Â Â Â Â Â Â Â * We had some memory, but were unable to
> @@ -489,10 +503,10 @@ hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *)
> Âasmlinkage void __exception
> Âdo_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
> Â{
> - Â Â Â const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
> + Â Â Â const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
> Â Â Â Âstruct siginfo info;
>
> - Â Â Â if (!inf->fn(addr, fsr, regs))
> + Â Â Â if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
> Â Â Â Â Â Â Â Âreturn;
>
> Â Â Â Âprintk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
> @@ -508,6 +522,6 @@ do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
> Âasmlinkage void __exception
> Âdo_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
> Â{
> - Â Â Â do_translation_fault(addr, 0, regs);
> + Â Â Â do_translation_fault(addr, FSR_LNX_PF, regs);
> Â}
>
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/