2005-04-17 05:20:36 +07:00
/*
* Copyright ( C ) 1994 Linus Torvalds
*
* 29 dec 2001 - Fixed oopses caused by unchecked access to the vm86
2006-01-15 08:43:54 +07:00
* stack - Manfred Spraul < manfred @ colorfullife . com >
2005-04-17 05:20:36 +07:00
*
* 22 mar 2002 - Manfred detected the stackfaults , but didn ' t handle
* them correctly . Now the emulation will be in a
* consistent state after stackfaults - Kasper Dupont
* < kasperd @ daimi . au . dk >
*
* 22 mar 2002 - Added missing clear_IF in set_vflags_ * Kasper Dupont
* < kasperd @ daimi . au . dk >
*
* ? ? ? ? ? 2002 - Fixed premature returns from handle_vm86_fault
* caused by Kasper Dupont ' s changes - Stas Sergeev
*
* 4 apr 2002 - Fixed CHECK_IF_IN_TRAP broken by Stas ' changes .
* Kasper Dupont < kasperd @ daimi . au . dk >
*
* 9 apr 2002 - Changed syntax of macros in handle_vm86_fault .
* Kasper Dupont < kasperd @ daimi . au . dk >
*
* 9 apr 2002 - Changed stack access macros to jump to a label
* instead of returning to userspace . This simplifies
* do_int , and is needed by handle_vm6_fault . Kasper
* Dupont < kasperd @ daimi . au . dk >
*
*/
2012-05-22 09:50:07 +07:00
# define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2006-01-12 03:17:48 +07:00
# include <linux/capability.h>
2005-04-17 05:20:36 +07:00
# include <linux/errno.h>
# include <linux/interrupt.h>
2013-03-28 04:18:05 +07:00
# include <linux/syscalls.h>
2005-04-17 05:20:36 +07:00
# include <linux/sched.h>
# include <linux/kernel.h>
# include <linux/signal.h>
# include <linux/string.h>
# include <linux/mm.h>
# include <linux/smp.h>
# include <linux/highmem.h>
# include <linux/ptrace.h>
2006-02-01 04:56:28 +07:00
# include <linux/audit.h>
2006-12-07 08:14:03 +07:00
# include <linux/stddef.h>
2015-07-29 12:41:16 +07:00
# include <linux/slab.h>
2015-09-05 07:00:43 +07:00
# include <linux/security.h>
2005-04-17 05:20:36 +07:00
# include <asm/uaccess.h>
# include <asm/io.h>
# include <asm/tlbflush.h>
# include <asm/irq.h>
2015-07-29 12:41:19 +07:00
# include <asm/traps.h>
2015-07-29 12:41:21 +07:00
# include <asm/vm86.h>
2005-04-17 05:20:36 +07:00
/*
* Known problems :
*
* Interrupt handling is not guaranteed :
* - a real x86 will disable all interrupts for one instruction
* after a " mov ss,xx " to make stack handling atomic even without
* the ' lss ' instruction . We can ' t guarantee this in v86 mode ,
* as the next instruction might result in a page fault or similar .
* - a real x86 will have interrupts disabled for one instruction
* past the ' sti ' that enables them . We don ' t bother with all the
* details yet .
*
* Let ' s hope these problems do not actually matter for anything .
*/
/*
* 8 - and 16 - bit register defines . .
*/
2008-01-30 19:30:56 +07:00
# define AL(regs) (((unsigned char *)&((regs)->pt.ax))[0])
# define AH(regs) (((unsigned char *)&((regs)->pt.ax))[1])
# define IP(regs) (*(unsigned short *)&((regs)->pt.ip))
# define SP(regs) (*(unsigned short *)&((regs)->pt.sp))
2005-04-17 05:20:36 +07:00
/*
* virtual flags ( 16 and 32 - bit versions )
*/
2015-07-29 12:41:23 +07:00
# define VFLAGS (*(unsigned short *)&(current->thread.vm86->veflags))
# define VEFLAGS (current->thread.vm86->veflags)
2005-04-17 05:20:36 +07:00
2008-02-23 05:10:40 +07:00
# define set_flags(X, new, mask) \
2005-04-17 05:20:36 +07:00
( ( X ) = ( ( X ) & ~ ( mask ) ) | ( ( new ) & ( mask ) ) )
# define SAFE_MASK (0xDD5)
# define RETURN_MASK (0xDFF)
2015-07-29 12:41:19 +07:00
void save_v86_state ( struct kernel_vm86_regs * regs , int retval )
2005-04-17 05:20:36 +07:00
{
struct tss_struct * tss ;
2015-07-20 08:09:06 +07:00
struct task_struct * tsk = current ;
struct vm86plus_struct __user * user ;
2015-07-29 12:41:16 +07:00
struct vm86 * vm86 = current - > thread . vm86 ;
2015-07-20 08:09:06 +07:00
long err = 0 ;
2005-04-17 05:20:36 +07:00
/*
* This gets called from entry . S with interrupts disabled , but
* from process context . Enable interrupts here , before trying
* to access user space .
*/
local_irq_enable ( ) ;
2015-07-29 12:41:22 +07:00
if ( ! vm86 | | ! vm86 - > user_vm86 ) {
pr_alert ( " no user_vm86: BAD \n " ) ;
2005-04-17 05:20:36 +07:00
do_exit ( SIGSEGV ) ;
}
2015-07-29 12:41:23 +07:00
set_flags ( regs - > pt . flags , VEFLAGS , X86_EFLAGS_VIF | vm86 - > veflags_mask ) ;
2015-07-29 12:41:22 +07:00
user = vm86 - > user_vm86 ;
2015-07-20 08:09:06 +07:00
2015-07-29 12:41:17 +07:00
if ( ! access_ok ( VERIFY_WRITE , user , vm86 - > vm86plus . is_vm86pus ?
2015-07-20 08:09:06 +07:00
sizeof ( struct vm86plus_struct ) :
sizeof ( struct vm86_struct ) ) ) {
2015-07-29 12:41:22 +07:00
pr_alert ( " could not access userspace vm86 info \n " ) ;
2015-07-20 08:09:06 +07:00
do_exit ( SIGSEGV ) ;
}
put_user_try {
put_user_ex ( regs - > pt . bx , & user - > regs . ebx ) ;
put_user_ex ( regs - > pt . cx , & user - > regs . ecx ) ;
put_user_ex ( regs - > pt . dx , & user - > regs . edx ) ;
put_user_ex ( regs - > pt . si , & user - > regs . esi ) ;
put_user_ex ( regs - > pt . di , & user - > regs . edi ) ;
put_user_ex ( regs - > pt . bp , & user - > regs . ebp ) ;
put_user_ex ( regs - > pt . ax , & user - > regs . eax ) ;
put_user_ex ( regs - > pt . ip , & user - > regs . eip ) ;
put_user_ex ( regs - > pt . cs , & user - > regs . cs ) ;
put_user_ex ( regs - > pt . flags , & user - > regs . eflags ) ;
put_user_ex ( regs - > pt . sp , & user - > regs . esp ) ;
put_user_ex ( regs - > pt . ss , & user - > regs . ss ) ;
put_user_ex ( regs - > es , & user - > regs . es ) ;
put_user_ex ( regs - > ds , & user - > regs . ds ) ;
put_user_ex ( regs - > fs , & user - > regs . fs ) ;
put_user_ex ( regs - > gs , & user - > regs . gs ) ;
2015-07-29 12:41:16 +07:00
put_user_ex ( vm86 - > screen_bitmap , & user - > screen_bitmap ) ;
2015-07-20 08:09:06 +07:00
} put_user_catch ( err ) ;
if ( err ) {
2015-07-29 12:41:22 +07:00
pr_alert ( " could not access userspace vm86 info \n " ) ;
2005-04-17 05:20:36 +07:00
do_exit ( SIGSEGV ) ;
}
2015-03-06 10:19:05 +07:00
tss = & per_cpu ( cpu_tss , get_cpu ( ) ) ;
2015-07-29 12:41:16 +07:00
tsk - > thread . sp0 = vm86 - > saved_sp0 ;
2015-07-20 08:09:06 +07:00
tsk - > thread . sysenter_cs = __KERNEL_CS ;
load_sp0 ( tss , & tsk - > thread ) ;
2015-07-29 12:41:16 +07:00
vm86 - > saved_sp0 = 0 ;
2005-04-17 05:20:36 +07:00
put_cpu ( ) ;
2015-07-29 12:41:19 +07:00
memcpy ( & regs - > pt , & vm86 - > regs32 , sizeof ( struct pt_regs ) ) ;
2006-12-07 08:14:03 +07:00
2015-07-29 12:41:19 +07:00
lazy_load_gs ( vm86 - > regs32 . gs ) ;
2006-12-07 08:14:03 +07:00
2015-07-29 12:41:19 +07:00
regs - > pt . ax = retval ;
2005-04-17 05:20:36 +07:00
}
2005-10-30 08:16:34 +07:00
static void mark_screen_rdonly ( struct mm_struct * mm )
2005-04-17 05:20:36 +07:00
{
pgd_t * pgd ;
pud_t * pud ;
pmd_t * pmd ;
2005-10-30 08:16:34 +07:00
pte_t * pte ;
spinlock_t * ptl ;
2005-04-17 05:20:36 +07:00
int i ;
mm: thp: fix pmd_bad() triggering in code paths holding mmap_sem read mode
In some cases it may happen that pmd_none_or_clear_bad() is called with
the mmap_sem hold in read mode. In those cases the huge page faults can
allocate hugepmds under pmd_none_or_clear_bad() and that can trigger a
false positive from pmd_bad() that will not like to see a pmd
materializing as trans huge.
It's not khugepaged causing the problem, khugepaged holds the mmap_sem
in write mode (and all those sites must hold the mmap_sem in read mode
to prevent pagetables to go away from under them, during code review it
seems vm86 mode on 32bit kernels requires that too unless it's
restricted to 1 thread per process or UP builds). The race is only with
the huge pagefaults that can convert a pmd_none() into a
pmd_trans_huge().
Effectively all these pmd_none_or_clear_bad() sites running with
mmap_sem in read mode are somewhat speculative with the page faults, and
the result is always undefined when they run simultaneously. This is
probably why it wasn't common to run into this. For example if the
madvise(MADV_DONTNEED) runs zap_page_range() shortly before the page
fault, the hugepage will not be zapped, if the page fault runs first it
will be zapped.
Altering pmd_bad() not to error out if it finds hugepmds won't be enough
to fix this, because zap_pmd_range would then proceed to call
zap_pte_range (which would be incorrect if the pmd become a
pmd_trans_huge()).
The simplest way to fix this is to read the pmd in the local stack
(regardless of what we read, no need of actual CPU barriers, only
compiler barrier needed), and be sure it is not changing under the code
that computes its value. Even if the real pmd is changing under the
value we hold on the stack, we don't care. If we actually end up in
zap_pte_range it means the pmd was not none already and it was not huge,
and it can't become huge from under us (khugepaged locking explained
above).
All we need is to enforce that there is no way anymore that in a code
path like below, pmd_trans_huge can be false, but pmd_none_or_clear_bad
can run into a hugepmd. The overhead of a barrier() is just a compiler
tweak and should not be measurable (I only added it for THP builds). I
don't exclude different compiler versions may have prevented the race
too by caching the value of *pmd on the stack (that hasn't been
verified, but it wouldn't be impossible considering
pmd_none_or_clear_bad, pmd_bad, pmd_trans_huge, pmd_none are all inlines
and there's no external function called in between pmd_trans_huge and
pmd_none_or_clear_bad).
if (pmd_trans_huge(*pmd)) {
if (next-addr != HPAGE_PMD_SIZE) {
VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem));
split_huge_page_pmd(vma->vm_mm, pmd);
} else if (zap_huge_pmd(tlb, vma, pmd, addr))
continue;
/* fall through */
}
if (pmd_none_or_clear_bad(pmd))
Because this race condition could be exercised without special
privileges this was reported in CVE-2012-1179.
The race was identified and fully explained by Ulrich who debugged it.
I'm quoting his accurate explanation below, for reference.
====== start quote =======
mapcount 0 page_mapcount 1
kernel BUG at mm/huge_memory.c:1384!
At some point prior to the panic, a "bad pmd ..." message similar to the
following is logged on the console:
mm/memory.c:145: bad pmd ffff8800376e1f98(80000000314000e7).
The "bad pmd ..." message is logged by pmd_clear_bad() before it clears
the page's PMD table entry.
143 void pmd_clear_bad(pmd_t *pmd)
144 {
-> 145 pmd_ERROR(*pmd);
146 pmd_clear(pmd);
147 }
After the PMD table entry has been cleared, there is an inconsistency
between the actual number of PMD table entries that are mapping the page
and the page's map count (_mapcount field in struct page). When the page
is subsequently reclaimed, __split_huge_page() detects this inconsistency.
1381 if (mapcount != page_mapcount(page))
1382 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1383 mapcount, page_mapcount(page));
-> 1384 BUG_ON(mapcount != page_mapcount(page));
The root cause of the problem is a race of two threads in a multithreaded
process. Thread B incurs a page fault on a virtual address that has never
been accessed (PMD entry is zero) while Thread A is executing an madvise()
system call on a virtual address within the same 2 MB (huge page) range.
virtual address space
.---------------------.
| |
| |
.-|---------------------|
| | |
| | |<-- B(fault)
| | |
2 MB | |/////////////////////|-.
huge < |/////////////////////| > A(range)
page | |/////////////////////|-'
| | |
| | |
'-|---------------------|
| |
| |
'---------------------'
- Thread A is executing an madvise(..., MADV_DONTNEED) system call
on the virtual address range "A(range)" shown in the picture.
sys_madvise
// Acquire the semaphore in shared mode.
down_read(¤t->mm->mmap_sem)
...
madvise_vma
switch (behavior)
case MADV_DONTNEED:
madvise_dontneed
zap_page_range
unmap_vmas
unmap_page_range
zap_pud_range
zap_pmd_range
//
// Assume that this huge page has never been accessed.
// I.e. content of the PMD entry is zero (not mapped).
//
if (pmd_trans_huge(*pmd)) {
// We don't get here due to the above assumption.
}
//
// Assume that Thread B incurred a page fault and
.---------> // sneaks in here as shown below.
| //
| if (pmd_none_or_clear_bad(pmd))
| {
| if (unlikely(pmd_bad(*pmd)))
| pmd_clear_bad
| {
| pmd_ERROR
| // Log "bad pmd ..." message here.
| pmd_clear
| // Clear the page's PMD entry.
| // Thread B incremented the map count
| // in page_add_new_anon_rmap(), but
| // now the page is no longer mapped
| // by a PMD entry (-> inconsistency).
| }
| }
|
v
- Thread B is handling a page fault on virtual address "B(fault)" shown
in the picture.
...
do_page_fault
__do_page_fault
// Acquire the semaphore in shared mode.
down_read_trylock(&mm->mmap_sem)
...
handle_mm_fault
if (pmd_none(*pmd) && transparent_hugepage_enabled(vma))
// We get here due to the above assumption (PMD entry is zero).
do_huge_pmd_anonymous_page
alloc_hugepage_vma
// Allocate a new transparent huge page here.
...
__do_huge_pmd_anonymous_page
...
spin_lock(&mm->page_table_lock)
...
page_add_new_anon_rmap
// Here we increment the page's map count (starts at -1).
atomic_set(&page->_mapcount, 0)
set_pmd_at
// Here we set the page's PMD entry which will be cleared
// when Thread A calls pmd_clear_bad().
...
spin_unlock(&mm->page_table_lock)
The mmap_sem does not prevent the race because both threads are acquiring
it in shared mode (down_read). Thread B holds the page_table_lock while
the page's map count and PMD table entry are updated. However, Thread A
does not synchronize on that lock.
====== end quote =======
[akpm@linux-foundation.org: checkpatch fixes]
Reported-by: Ulrich Obergfell <uobergfe@redhat.com>
Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Hugh Dickins <hughd@google.com>
Cc: Dave Jones <davej@redhat.com>
Acked-by: Larry Woodman <lwoodman@redhat.com>
Acked-by: Rik van Riel <riel@redhat.com>
Cc: <stable@vger.kernel.org> [2.6.38+]
Cc: Mark Salter <msalter@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-03-22 06:33:42 +07:00
down_write ( & mm - > mmap_sem ) ;
2005-10-30 08:16:34 +07:00
pgd = pgd_offset ( mm , 0xA0000 ) ;
2005-04-17 05:20:36 +07:00
if ( pgd_none_or_clear_bad ( pgd ) )
goto out ;
pud = pud_offset ( pgd , 0xA0000 ) ;
if ( pud_none_or_clear_bad ( pud ) )
goto out ;
pmd = pmd_offset ( pud , 0xA0000 ) ;
2016-01-16 07:52:42 +07:00
if ( pmd_trans_huge ( * pmd ) ) {
struct vm_area_struct * vma = find_vma ( mm , 0xA0000 ) ;
split_huge_pmd ( vma , pmd , 0xA0000 ) ;
}
2005-04-17 05:20:36 +07:00
if ( pmd_none_or_clear_bad ( pmd ) )
goto out ;
2005-10-30 08:16:34 +07:00
pte = pte_offset_map_lock ( mm , pmd , 0xA0000 , & ptl ) ;
2005-04-17 05:20:36 +07:00
for ( i = 0 ; i < 32 ; i + + ) {
if ( pte_present ( * pte ) )
set_pte ( pte , pte_wrprotect ( * pte ) ) ;
pte + + ;
}
2005-10-30 08:16:34 +07:00
pte_unmap_unlock ( pte , ptl ) ;
2005-04-17 05:20:36 +07:00
out :
mm: thp: fix pmd_bad() triggering in code paths holding mmap_sem read mode
In some cases it may happen that pmd_none_or_clear_bad() is called with
the mmap_sem hold in read mode. In those cases the huge page faults can
allocate hugepmds under pmd_none_or_clear_bad() and that can trigger a
false positive from pmd_bad() that will not like to see a pmd
materializing as trans huge.
It's not khugepaged causing the problem, khugepaged holds the mmap_sem
in write mode (and all those sites must hold the mmap_sem in read mode
to prevent pagetables to go away from under them, during code review it
seems vm86 mode on 32bit kernels requires that too unless it's
restricted to 1 thread per process or UP builds). The race is only with
the huge pagefaults that can convert a pmd_none() into a
pmd_trans_huge().
Effectively all these pmd_none_or_clear_bad() sites running with
mmap_sem in read mode are somewhat speculative with the page faults, and
the result is always undefined when they run simultaneously. This is
probably why it wasn't common to run into this. For example if the
madvise(MADV_DONTNEED) runs zap_page_range() shortly before the page
fault, the hugepage will not be zapped, if the page fault runs first it
will be zapped.
Altering pmd_bad() not to error out if it finds hugepmds won't be enough
to fix this, because zap_pmd_range would then proceed to call
zap_pte_range (which would be incorrect if the pmd become a
pmd_trans_huge()).
The simplest way to fix this is to read the pmd in the local stack
(regardless of what we read, no need of actual CPU barriers, only
compiler barrier needed), and be sure it is not changing under the code
that computes its value. Even if the real pmd is changing under the
value we hold on the stack, we don't care. If we actually end up in
zap_pte_range it means the pmd was not none already and it was not huge,
and it can't become huge from under us (khugepaged locking explained
above).
All we need is to enforce that there is no way anymore that in a code
path like below, pmd_trans_huge can be false, but pmd_none_or_clear_bad
can run into a hugepmd. The overhead of a barrier() is just a compiler
tweak and should not be measurable (I only added it for THP builds). I
don't exclude different compiler versions may have prevented the race
too by caching the value of *pmd on the stack (that hasn't been
verified, but it wouldn't be impossible considering
pmd_none_or_clear_bad, pmd_bad, pmd_trans_huge, pmd_none are all inlines
and there's no external function called in between pmd_trans_huge and
pmd_none_or_clear_bad).
if (pmd_trans_huge(*pmd)) {
if (next-addr != HPAGE_PMD_SIZE) {
VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem));
split_huge_page_pmd(vma->vm_mm, pmd);
} else if (zap_huge_pmd(tlb, vma, pmd, addr))
continue;
/* fall through */
}
if (pmd_none_or_clear_bad(pmd))
Because this race condition could be exercised without special
privileges this was reported in CVE-2012-1179.
The race was identified and fully explained by Ulrich who debugged it.
I'm quoting his accurate explanation below, for reference.
====== start quote =======
mapcount 0 page_mapcount 1
kernel BUG at mm/huge_memory.c:1384!
At some point prior to the panic, a "bad pmd ..." message similar to the
following is logged on the console:
mm/memory.c:145: bad pmd ffff8800376e1f98(80000000314000e7).
The "bad pmd ..." message is logged by pmd_clear_bad() before it clears
the page's PMD table entry.
143 void pmd_clear_bad(pmd_t *pmd)
144 {
-> 145 pmd_ERROR(*pmd);
146 pmd_clear(pmd);
147 }
After the PMD table entry has been cleared, there is an inconsistency
between the actual number of PMD table entries that are mapping the page
and the page's map count (_mapcount field in struct page). When the page
is subsequently reclaimed, __split_huge_page() detects this inconsistency.
1381 if (mapcount != page_mapcount(page))
1382 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1383 mapcount, page_mapcount(page));
-> 1384 BUG_ON(mapcount != page_mapcount(page));
The root cause of the problem is a race of two threads in a multithreaded
process. Thread B incurs a page fault on a virtual address that has never
been accessed (PMD entry is zero) while Thread A is executing an madvise()
system call on a virtual address within the same 2 MB (huge page) range.
virtual address space
.---------------------.
| |
| |
.-|---------------------|
| | |
| | |<-- B(fault)
| | |
2 MB | |/////////////////////|-.
huge < |/////////////////////| > A(range)
page | |/////////////////////|-'
| | |
| | |
'-|---------------------|
| |
| |
'---------------------'
- Thread A is executing an madvise(..., MADV_DONTNEED) system call
on the virtual address range "A(range)" shown in the picture.
sys_madvise
// Acquire the semaphore in shared mode.
down_read(¤t->mm->mmap_sem)
...
madvise_vma
switch (behavior)
case MADV_DONTNEED:
madvise_dontneed
zap_page_range
unmap_vmas
unmap_page_range
zap_pud_range
zap_pmd_range
//
// Assume that this huge page has never been accessed.
// I.e. content of the PMD entry is zero (not mapped).
//
if (pmd_trans_huge(*pmd)) {
// We don't get here due to the above assumption.
}
//
// Assume that Thread B incurred a page fault and
.---------> // sneaks in here as shown below.
| //
| if (pmd_none_or_clear_bad(pmd))
| {
| if (unlikely(pmd_bad(*pmd)))
| pmd_clear_bad
| {
| pmd_ERROR
| // Log "bad pmd ..." message here.
| pmd_clear
| // Clear the page's PMD entry.
| // Thread B incremented the map count
| // in page_add_new_anon_rmap(), but
| // now the page is no longer mapped
| // by a PMD entry (-> inconsistency).
| }
| }
|
v
- Thread B is handling a page fault on virtual address "B(fault)" shown
in the picture.
...
do_page_fault
__do_page_fault
// Acquire the semaphore in shared mode.
down_read_trylock(&mm->mmap_sem)
...
handle_mm_fault
if (pmd_none(*pmd) && transparent_hugepage_enabled(vma))
// We get here due to the above assumption (PMD entry is zero).
do_huge_pmd_anonymous_page
alloc_hugepage_vma
// Allocate a new transparent huge page here.
...
__do_huge_pmd_anonymous_page
...
spin_lock(&mm->page_table_lock)
...
page_add_new_anon_rmap
// Here we increment the page's map count (starts at -1).
atomic_set(&page->_mapcount, 0)
set_pmd_at
// Here we set the page's PMD entry which will be cleared
// when Thread A calls pmd_clear_bad().
...
spin_unlock(&mm->page_table_lock)
The mmap_sem does not prevent the race because both threads are acquiring
it in shared mode (down_read). Thread B holds the page_table_lock while
the page's map count and PMD table entry are updated. However, Thread A
does not synchronize on that lock.
====== end quote =======
[akpm@linux-foundation.org: checkpatch fixes]
Reported-by: Ulrich Obergfell <uobergfe@redhat.com>
Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Hugh Dickins <hughd@google.com>
Cc: Dave Jones <davej@redhat.com>
Acked-by: Larry Woodman <lwoodman@redhat.com>
Acked-by: Rik van Riel <riel@redhat.com>
Cc: <stable@vger.kernel.org> [2.6.38+]
Cc: Mark Salter <msalter@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-03-22 06:33:42 +07:00
up_write ( & mm - > mmap_sem ) ;
2005-04-17 05:20:36 +07:00
flush_tlb ( ) ;
}
static int do_vm86_irq_handling ( int subfunction , int irqnumber ) ;
2015-07-29 12:41:22 +07:00
static long do_sys_vm86 ( struct vm86plus_struct __user * user_vm86 , bool plus ) ;
2005-04-17 05:20:36 +07:00
2015-07-29 12:41:22 +07:00
SYSCALL_DEFINE1 ( vm86old , struct vm86_struct __user * , user_vm86 )
2005-04-17 05:20:36 +07:00
{
2015-07-29 12:41:22 +07:00
return do_sys_vm86 ( ( struct vm86plus_struct __user * ) user_vm86 , false ) ;
2005-04-17 05:20:36 +07:00
}
2013-03-28 04:18:05 +07:00
SYSCALL_DEFINE2 ( vm86 , unsigned long , cmd , unsigned long , arg )
2005-04-17 05:20:36 +07:00
{
2009-12-10 07:01:55 +07:00
switch ( cmd ) {
2008-02-23 05:10:40 +07:00
case VM86_REQUEST_IRQ :
case VM86_FREE_IRQ :
case VM86_GET_IRQ_BITS :
case VM86_GET_AND_RESET_IRQ :
2013-03-28 04:18:05 +07:00
return do_vm86_irq_handling ( cmd , ( int ) arg ) ;
2008-02-23 05:10:40 +07:00
case VM86_PLUS_INSTALL_CHECK :
/*
* NOTE : on old vm86 stuff this will return the error
* from access_ok ( ) , because the subfunction is
* interpreted as ( invalid ) address to vm86_struct .
* So the installation check works .
*/
2013-03-28 04:18:05 +07:00
return 0 ;
2005-04-17 05:20:36 +07:00
}
/* we come here only for functions VM86_ENTER, VM86_ENTER_NO_BYPASS */
2015-07-29 12:41:18 +07:00
return do_sys_vm86 ( ( struct vm86plus_struct __user * ) arg , true ) ;
2005-04-17 05:20:36 +07:00
}
2015-07-29 12:41:22 +07:00
static long do_sys_vm86 ( struct vm86plus_struct __user * user_vm86 , bool plus )
2005-04-17 05:20:36 +07:00
{
struct tss_struct * tss ;
2015-07-20 08:09:06 +07:00
struct task_struct * tsk = current ;
2015-07-29 12:41:16 +07:00
struct vm86 * vm86 = tsk - > thread . vm86 ;
2015-07-29 12:41:18 +07:00
struct kernel_vm86_regs vm86regs ;
2015-07-29 12:41:19 +07:00
struct pt_regs * regs = current_pt_regs ( ) ;
2015-07-20 08:09:06 +07:00
unsigned long err = 0 ;
2015-09-05 07:00:43 +07:00
err = security_mmap_addr ( 0 ) ;
if ( err ) {
/*
* vm86 cannot virtualize the address space , so vm86 users
* need to manage the low 1 MB themselves using mmap . Given
* that BIOS places important data in the first page , vm86
* is essentially useless if mmap_min_addr ! = 0. DOSEMU ,
* for example , won ' t even bother trying to use vm86 if it
* can ' t map a page at virtual address 0.
*
* To reduce the available kernel attack surface , simply
* disallow vm86 ( old ) for users who cannot mmap at va 0.
*
* The implementation of security_mmap_addr will allow
* suitably privileged users to map va 0 even if
* vm . mmap_min_addr is set above 0 , and we want this
* behavior for vm86 as well , as it ensures that legacy
* tools like vbetool will not fail just because of
* vm . mmap_min_addr .
*/
pr_info_once ( " Denied a call to vm86(old) from %s[%d] (uid: %d). Set the vm.mmap_min_addr sysctl to 0 and/or adjust LSM mmap_min_addr policy to enable vm86 if you are using a vm86-based DOS emulator. \n " ,
current - > comm , task_pid_nr ( current ) ,
from_kuid_munged ( & init_user_ns , current_uid ( ) ) ) ;
return - EPERM ;
}
2015-07-29 12:41:16 +07:00
if ( ! vm86 ) {
if ( ! ( vm86 = kzalloc ( sizeof ( * vm86 ) , GFP_KERNEL ) ) )
return - ENOMEM ;
tsk - > thread . vm86 = vm86 ;
}
if ( vm86 - > saved_sp0 )
2015-07-20 08:09:06 +07:00
return - EPERM ;
2015-07-29 12:41:22 +07:00
if ( ! access_ok ( VERIFY_READ , user_vm86 , plus ?
2015-07-20 08:09:06 +07:00
sizeof ( struct vm86_struct ) :
sizeof ( struct vm86plus_struct ) ) )
return - EFAULT ;
2015-07-29 12:41:18 +07:00
memset ( & vm86regs , 0 , sizeof ( vm86regs ) ) ;
2015-07-20 08:09:06 +07:00
get_user_try {
unsigned short seg ;
2015-07-29 12:41:22 +07:00
get_user_ex ( vm86regs . pt . bx , & user_vm86 - > regs . ebx ) ;
get_user_ex ( vm86regs . pt . cx , & user_vm86 - > regs . ecx ) ;
get_user_ex ( vm86regs . pt . dx , & user_vm86 - > regs . edx ) ;
get_user_ex ( vm86regs . pt . si , & user_vm86 - > regs . esi ) ;
get_user_ex ( vm86regs . pt . di , & user_vm86 - > regs . edi ) ;
get_user_ex ( vm86regs . pt . bp , & user_vm86 - > regs . ebp ) ;
get_user_ex ( vm86regs . pt . ax , & user_vm86 - > regs . eax ) ;
get_user_ex ( vm86regs . pt . ip , & user_vm86 - > regs . eip ) ;
get_user_ex ( seg , & user_vm86 - > regs . cs ) ;
2015-07-29 12:41:18 +07:00
vm86regs . pt . cs = seg ;
2015-07-29 12:41:22 +07:00
get_user_ex ( vm86regs . pt . flags , & user_vm86 - > regs . eflags ) ;
get_user_ex ( vm86regs . pt . sp , & user_vm86 - > regs . esp ) ;
get_user_ex ( seg , & user_vm86 - > regs . ss ) ;
2015-07-29 12:41:18 +07:00
vm86regs . pt . ss = seg ;
2015-07-29 12:41:22 +07:00
get_user_ex ( vm86regs . es , & user_vm86 - > regs . es ) ;
get_user_ex ( vm86regs . ds , & user_vm86 - > regs . ds ) ;
get_user_ex ( vm86regs . fs , & user_vm86 - > regs . fs ) ;
get_user_ex ( vm86regs . gs , & user_vm86 - > regs . gs ) ;
get_user_ex ( vm86 - > flags , & user_vm86 - > flags ) ;
get_user_ex ( vm86 - > screen_bitmap , & user_vm86 - > screen_bitmap ) ;
get_user_ex ( vm86 - > cpu_type , & user_vm86 - > cpu_type ) ;
2015-07-20 08:09:06 +07:00
} get_user_catch ( err ) ;
if ( err )
return err ;
2015-07-29 12:41:22 +07:00
if ( copy_from_user ( & vm86 - > int_revectored ,
& user_vm86 - > int_revectored ,
2015-07-20 08:09:06 +07:00
sizeof ( struct revectored_struct ) ) )
return - EFAULT ;
2015-07-29 12:41:22 +07:00
if ( copy_from_user ( & vm86 - > int21_revectored ,
& user_vm86 - > int21_revectored ,
2015-07-20 08:09:06 +07:00
sizeof ( struct revectored_struct ) ) )
return - EFAULT ;
if ( plus ) {
2015-07-29 12:41:22 +07:00
if ( copy_from_user ( & vm86 - > vm86plus , & user_vm86 - > vm86plus ,
2015-07-20 08:09:06 +07:00
sizeof ( struct vm86plus_info_struct ) ) )
return - EFAULT ;
2015-07-29 12:41:17 +07:00
vm86 - > vm86plus . is_vm86pus = 1 ;
} else
memset ( & vm86 - > vm86plus , 0 ,
sizeof ( struct vm86plus_info_struct ) ) ;
2015-07-29 12:41:19 +07:00
memcpy ( & vm86 - > regs32 , regs , sizeof ( struct pt_regs ) ) ;
2015-07-29 12:41:22 +07:00
vm86 - > user_vm86 = user_vm86 ;
2005-04-17 05:20:36 +07:00
/*
2008-01-30 19:30:56 +07:00
* The flags register is also special : we cannot trust that the user
2005-04-17 05:20:36 +07:00
* has set it up safely , so this makes sure interrupt etc flags are
* inherited from protected mode .
*/
2015-07-29 12:41:18 +07:00
VEFLAGS = vm86regs . pt . flags ;
vm86regs . pt . flags & = SAFE_MASK ;
2015-07-29 12:41:19 +07:00
vm86regs . pt . flags | = regs - > flags & ~ SAFE_MASK ;
2015-07-29 12:41:18 +07:00
vm86regs . pt . flags | = X86_VM_MASK ;
2005-04-17 05:20:36 +07:00
2015-07-29 12:41:19 +07:00
vm86regs . pt . orig_ax = regs - > orig_ax ;
2015-07-20 08:09:05 +07:00
2015-07-29 12:41:17 +07:00
switch ( vm86 - > cpu_type ) {
2008-02-23 05:10:40 +07:00
case CPU_286 :
2015-07-29 12:41:23 +07:00
vm86 - > veflags_mask = 0 ;
2008-02-23 05:10:40 +07:00
break ;
case CPU_386 :
2015-07-29 12:41:23 +07:00
vm86 - > veflags_mask = X86_EFLAGS_NT | X86_EFLAGS_IOPL ;
2008-02-23 05:10:40 +07:00
break ;
case CPU_486 :
2015-07-29 12:41:23 +07:00
vm86 - > veflags_mask = X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL ;
2008-02-23 05:10:40 +07:00
break ;
default :
2015-07-29 12:41:23 +07:00
vm86 - > veflags_mask = X86_EFLAGS_ID | X86_EFLAGS_AC | X86_EFLAGS_NT | X86_EFLAGS_IOPL ;
2008-02-23 05:10:40 +07:00
break ;
2005-04-17 05:20:36 +07:00
}
/*
2015-07-29 12:41:19 +07:00
* Save old state
2005-04-17 05:20:36 +07:00
*/
2015-07-29 12:41:16 +07:00
vm86 - > saved_sp0 = tsk - > thread . sp0 ;
2015-07-29 12:41:19 +07:00
lazy_save_gs ( vm86 - > regs32 . gs ) ;
2005-04-17 05:20:36 +07:00
2015-03-06 10:19:05 +07:00
tss = & per_cpu ( cpu_tss , get_cpu ( ) ) ;
2015-07-29 12:41:19 +07:00
/* make room for real-mode segments */
tsk - > thread . sp0 + = 16 ;
2015-12-07 16:39:41 +07:00
2016-01-27 04:12:05 +07:00
if ( static_cpu_has ( X86_FEATURE_SEP ) )
2005-04-17 05:20:36 +07:00
tsk - > thread . sysenter_cs = 0 ;
2015-12-07 16:39:41 +07:00
2008-01-30 19:31:02 +07:00
load_sp0 ( tss , & tsk - > thread ) ;
2005-04-17 05:20:36 +07:00
put_cpu ( ) ;
2015-07-29 12:41:17 +07:00
if ( vm86 - > flags & VM86_SCREEN_BITMAP )
2005-10-30 08:16:34 +07:00
mark_screen_rdonly ( tsk - > mm ) ;
2006-02-01 04:56:28 +07:00
2015-07-29 12:41:19 +07:00
memcpy ( ( struct kernel_vm86_regs * ) regs , & vm86regs , sizeof ( vm86regs ) ) ;
force_iret ( ) ;
return regs - > ax ;
2005-04-17 05:20:36 +07:00
}
2008-02-23 05:10:40 +07:00
static inline void set_IF ( struct kernel_vm86_regs * regs )
2005-04-17 05:20:36 +07:00
{
2008-03-28 21:56:56 +07:00
VEFLAGS | = X86_EFLAGS_VIF ;
2005-04-17 05:20:36 +07:00
}
2008-02-23 05:10:40 +07:00
static inline void clear_IF ( struct kernel_vm86_regs * regs )
2005-04-17 05:20:36 +07:00
{
2008-03-28 21:56:56 +07:00
VEFLAGS & = ~ X86_EFLAGS_VIF ;
2005-04-17 05:20:36 +07:00
}
2008-02-23 05:10:40 +07:00
static inline void clear_TF ( struct kernel_vm86_regs * regs )
2005-04-17 05:20:36 +07:00
{
2008-03-28 21:56:56 +07:00
regs - > pt . flags & = ~ X86_EFLAGS_TF ;
2005-04-17 05:20:36 +07:00
}
2008-02-23 05:10:40 +07:00
static inline void clear_AC ( struct kernel_vm86_regs * regs )
2005-04-17 05:20:36 +07:00
{
2008-03-28 21:56:56 +07:00
regs - > pt . flags & = ~ X86_EFLAGS_AC ;
2005-04-17 05:20:36 +07:00
}
2008-02-23 05:10:40 +07:00
/*
* It is correct to call set_IF ( regs ) from the set_vflags_ *
2005-04-17 05:20:36 +07:00
* functions . However someone forgot to call clear_IF ( regs )
* in the opposite case .
* After the command sequence CLI PUSHF STI POPF you should
2008-01-30 19:31:42 +07:00
* end up with interrupts disabled , but you ended up with
2005-04-17 05:20:36 +07:00
* interrupts enabled .
* ( I was testing my own changes , but the only bug I
* could find was in a function I had not changed . )
* [ KD ]
*/
2008-02-23 05:10:40 +07:00
static inline void set_vflags_long ( unsigned long flags , struct kernel_vm86_regs * regs )
2005-04-17 05:20:36 +07:00
{
2015-07-29 12:41:23 +07:00
set_flags ( VEFLAGS , flags , current - > thread . vm86 - > veflags_mask ) ;
2008-01-30 19:30:56 +07:00
set_flags ( regs - > pt . flags , flags , SAFE_MASK ) ;
2008-03-28 21:56:56 +07:00
if ( flags & X86_EFLAGS_IF )
2005-04-17 05:20:36 +07:00
set_IF ( regs ) ;
else
clear_IF ( regs ) ;
}
2008-02-23 05:10:40 +07:00
static inline void set_vflags_short ( unsigned short flags , struct kernel_vm86_regs * regs )
2005-04-17 05:20:36 +07:00
{
2015-07-29 12:41:23 +07:00
set_flags ( VFLAGS , flags , current - > thread . vm86 - > veflags_mask ) ;
2008-01-30 19:30:56 +07:00
set_flags ( regs - > pt . flags , flags , SAFE_MASK ) ;
2008-03-28 21:56:56 +07:00
if ( flags & X86_EFLAGS_IF )
2005-04-17 05:20:36 +07:00
set_IF ( regs ) ;
else
clear_IF ( regs ) ;
}
2008-02-23 05:10:40 +07:00
static inline unsigned long get_vflags ( struct kernel_vm86_regs * regs )
2005-04-17 05:20:36 +07:00
{
2008-01-30 19:30:56 +07:00
unsigned long flags = regs - > pt . flags & RETURN_MASK ;
2005-04-17 05:20:36 +07:00
2008-03-28 21:56:56 +07:00
if ( VEFLAGS & X86_EFLAGS_VIF )
flags | = X86_EFLAGS_IF ;
flags | = X86_EFLAGS_IOPL ;
2015-07-29 12:41:23 +07:00
return flags | ( VEFLAGS & current - > thread . vm86 - > veflags_mask ) ;
2005-04-17 05:20:36 +07:00
}
2008-02-23 05:10:40 +07:00
static inline int is_revectored ( int nr , struct revectored_struct * bitmap )
2005-04-17 05:20:36 +07:00
{
__asm__ __volatile__ ( " btl %2,%1 \n \t sbbl %0,%0 "
: " =r " ( nr )
2008-02-23 05:10:40 +07:00
: " m " ( * bitmap ) , " r " ( nr ) ) ;
2005-04-17 05:20:36 +07:00
return nr ;
}
# define val_byte(val, n) (((__u8 *)&val)[n])
# define pushb(base, ptr, val, err_label) \
do { \
__u8 __val = val ; \
ptr - - ; \
if ( put_user ( __val , base + ptr ) < 0 ) \
goto err_label ; \
2008-02-23 05:10:40 +07:00
} while ( 0 )
2005-04-17 05:20:36 +07:00
# define pushw(base, ptr, val, err_label) \
do { \
__u16 __val = val ; \
ptr - - ; \
if ( put_user ( val_byte ( __val , 1 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr - - ; \
if ( put_user ( val_byte ( __val , 0 ) , base + ptr ) < 0 ) \
goto err_label ; \
2008-02-23 05:10:40 +07:00
} while ( 0 )
2005-04-17 05:20:36 +07:00
# define pushl(base, ptr, val, err_label) \
do { \
__u32 __val = val ; \
ptr - - ; \
if ( put_user ( val_byte ( __val , 3 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr - - ; \
if ( put_user ( val_byte ( __val , 2 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr - - ; \
if ( put_user ( val_byte ( __val , 1 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr - - ; \
if ( put_user ( val_byte ( __val , 0 ) , base + ptr ) < 0 ) \
goto err_label ; \
2008-02-23 05:10:40 +07:00
} while ( 0 )
2005-04-17 05:20:36 +07:00
# define popb(base, ptr, err_label) \
( { \
__u8 __res ; \
if ( get_user ( __res , base + ptr ) < 0 ) \
goto err_label ; \
ptr + + ; \
__res ; \
} )
# define popw(base, ptr, err_label) \
( { \
__u16 __res ; \
if ( get_user ( val_byte ( __res , 0 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr + + ; \
if ( get_user ( val_byte ( __res , 1 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr + + ; \
__res ; \
} )
# define popl(base, ptr, err_label) \
( { \
__u32 __res ; \
if ( get_user ( val_byte ( __res , 0 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr + + ; \
if ( get_user ( val_byte ( __res , 1 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr + + ; \
if ( get_user ( val_byte ( __res , 2 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr + + ; \
if ( get_user ( val_byte ( __res , 3 ) , base + ptr ) < 0 ) \
goto err_label ; \
ptr + + ; \
__res ; \
} )
/* There are so many possible reasons for this function to return
* VM86_INTx , so adding another doesn ' t bother me . We can expect
* userspace programs to be able to handle it . ( Getting a problem
* in userspace is always better than an Oops anyway . ) [ KD ]
*/
static void do_int ( struct kernel_vm86_regs * regs , int i ,
2008-02-23 05:10:40 +07:00
unsigned char __user * ssp , unsigned short sp )
2005-04-17 05:20:36 +07:00
{
unsigned long __user * intr_ptr ;
unsigned long segoffs ;
2015-07-29 12:41:18 +07:00
struct vm86 * vm86 = current - > thread . vm86 ;
2005-04-17 05:20:36 +07:00
2008-01-30 19:30:56 +07:00
if ( regs - > pt . cs = = BIOSSEG )
2005-04-17 05:20:36 +07:00
goto cannot_handle ;
2015-07-29 12:41:17 +07:00
if ( is_revectored ( i , & vm86 - > int_revectored ) )
2005-04-17 05:20:36 +07:00
goto cannot_handle ;
2015-07-29 12:41:17 +07:00
if ( i = = 0x21 & & is_revectored ( AH ( regs ) , & vm86 - > int21_revectored ) )
2005-04-17 05:20:36 +07:00
goto cannot_handle ;
intr_ptr = ( unsigned long __user * ) ( i < < 2 ) ;
if ( get_user ( segoffs , intr_ptr ) )
goto cannot_handle ;
if ( ( segoffs > > 16 ) = = BIOSSEG )
goto cannot_handle ;
pushw ( ssp , sp , get_vflags ( regs ) , cannot_handle ) ;
2008-01-30 19:30:56 +07:00
pushw ( ssp , sp , regs - > pt . cs , cannot_handle ) ;
2005-04-17 05:20:36 +07:00
pushw ( ssp , sp , IP ( regs ) , cannot_handle ) ;
2008-01-30 19:30:56 +07:00
regs - > pt . cs = segoffs > > 16 ;
2005-04-17 05:20:36 +07:00
SP ( regs ) - = 6 ;
IP ( regs ) = segoffs & 0xffff ;
clear_TF ( regs ) ;
clear_IF ( regs ) ;
clear_AC ( regs ) ;
return ;
cannot_handle :
2015-07-29 12:41:19 +07:00
save_v86_state ( regs , VM86_INTx + ( i < < 8 ) ) ;
2005-04-17 05:20:36 +07:00
}
2008-02-23 05:10:40 +07:00
int handle_vm86_trap ( struct kernel_vm86_regs * regs , long error_code , int trapno )
2005-04-17 05:20:36 +07:00
{
2015-07-29 12:41:18 +07:00
struct vm86 * vm86 = current - > thread . vm86 ;
if ( vm86 - > vm86plus . is_vm86pus ) {
x86, vm86: Fix preemption bug for int1 debug and int3 breakpoint handlers.
Impact: fix kernel bug such as:
BUG: scheduling while atomic: dosemu.bin/19680/0x00000004
See also Ubuntu bug 455067 at
https://bugs.launchpad.net/ubuntu/+source/linux/+bug/455067
Commits 4915a35e35a037254550a2ba9f367a812bc37d40
("Use preempt_conditional_sti/cli in do_int3, like on x86_64.")
and 3d2a71a596bd9c761c8487a2178e95f8a61da083
("x86, traps: converge do_debug handlers")
started disabling preemption in int1 and int3 handlers on i386.
The problem with vm86 is that the call to handle_vm86_trap() may jump
straight to entry_32.S and never returns so preempt is never enabled
again, and there is an imbalance in the preempt count.
Commit be716615fe596ee117292dc615e95f707fb67fd1 ("x86, vm86:
fix preemption bug"), which was later (accidentally?) reverted by commit
08d68323d1f0c34452e614263b212ca556dae47f ("hw-breakpoints: modifying
generic debug exception to use thread-specific debug registers")
fixed the problem for debug exceptions but not for breakpoints.
There are three solutions to this problem.
1. Reenable preemption before calling handle_vm86_trap(). This
was the approach that was later reverted.
2. Do not disable preemption for i386 in breakpoint and debug handlers.
This was the situation before October 2008. As far as I understand
preemption only needs to be disabled on x86_64 because a seperate stack is
used, but it's nice to have things work the same way on
i386 and x86_64.
3. Let handle_vm86_trap() return instead of jumping to assembly code.
By setting a flag in _TIF_WORK_MASK, either TIF_IRET or TIF_NOTIFY_RESUME,
the code in entry_32.S is instructed to return to 32 bit mode from
V86 mode. The logic in entry_32.S was already present to handle signals.
(I chose TIF_IRET because it's slightly more efficient in
do_notify_resume() in signal.c, but in fact TIF_IRET can probably be
replaced by TIF_NOTIFY_RESUME everywhere.)
I'm submitting approach 3, because I believe it is the most elegant
and prevents future confusion. Still, an obvious
preempt_conditional_cli(regs); is necessary in traps.c to correct the
bug.
[ hpa: This is technically a regression, but because:
1. the regression is so old,
2. the patch seems relatively high risk, justifying more testing, and
3. we're late in the 2.6.36-rc cycle,
I'm queuing it up for the 2.6.37 merge window. It might, however,
justify as a -stable backport at a latter time, hence Cc: stable. ]
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
LKML-Reference: <alpine.DEB.2.00.1009231312330.4732@localhost.localdomain>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: K.Prasad <prasad@linux.vnet.ibm.com>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Alexander van Heukelum <heukelum@fastmail.fm>
Cc: <stable@kernel.org>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2010-09-24 00:16:58 +07:00
if ( ( trapno = = 3 ) | | ( trapno = = 1 ) ) {
2015-07-29 12:41:19 +07:00
save_v86_state ( regs , VM86_TRAP + ( trapno < < 8 ) ) ;
x86, vm86: Fix preemption bug for int1 debug and int3 breakpoint handlers.
Impact: fix kernel bug such as:
BUG: scheduling while atomic: dosemu.bin/19680/0x00000004
See also Ubuntu bug 455067 at
https://bugs.launchpad.net/ubuntu/+source/linux/+bug/455067
Commits 4915a35e35a037254550a2ba9f367a812bc37d40
("Use preempt_conditional_sti/cli in do_int3, like on x86_64.")
and 3d2a71a596bd9c761c8487a2178e95f8a61da083
("x86, traps: converge do_debug handlers")
started disabling preemption in int1 and int3 handlers on i386.
The problem with vm86 is that the call to handle_vm86_trap() may jump
straight to entry_32.S and never returns so preempt is never enabled
again, and there is an imbalance in the preempt count.
Commit be716615fe596ee117292dc615e95f707fb67fd1 ("x86, vm86:
fix preemption bug"), which was later (accidentally?) reverted by commit
08d68323d1f0c34452e614263b212ca556dae47f ("hw-breakpoints: modifying
generic debug exception to use thread-specific debug registers")
fixed the problem for debug exceptions but not for breakpoints.
There are three solutions to this problem.
1. Reenable preemption before calling handle_vm86_trap(). This
was the approach that was later reverted.
2. Do not disable preemption for i386 in breakpoint and debug handlers.
This was the situation before October 2008. As far as I understand
preemption only needs to be disabled on x86_64 because a seperate stack is
used, but it's nice to have things work the same way on
i386 and x86_64.
3. Let handle_vm86_trap() return instead of jumping to assembly code.
By setting a flag in _TIF_WORK_MASK, either TIF_IRET or TIF_NOTIFY_RESUME,
the code in entry_32.S is instructed to return to 32 bit mode from
V86 mode. The logic in entry_32.S was already present to handle signals.
(I chose TIF_IRET because it's slightly more efficient in
do_notify_resume() in signal.c, but in fact TIF_IRET can probably be
replaced by TIF_NOTIFY_RESUME everywhere.)
I'm submitting approach 3, because I believe it is the most elegant
and prevents future confusion. Still, an obvious
preempt_conditional_cli(regs); is necessary in traps.c to correct the
bug.
[ hpa: This is technically a regression, but because:
1. the regression is so old,
2. the patch seems relatively high risk, justifying more testing, and
3. we're late in the 2.6.36-rc cycle,
I'm queuing it up for the 2.6.37 merge window. It might, however,
justify as a -stable backport at a latter time, hence Cc: stable. ]
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
LKML-Reference: <alpine.DEB.2.00.1009231312330.4732@localhost.localdomain>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: K.Prasad <prasad@linux.vnet.ibm.com>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Alexander van Heukelum <heukelum@fastmail.fm>
Cc: <stable@kernel.org>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2010-09-24 00:16:58 +07:00
return 0 ;
}
2008-01-30 19:30:56 +07:00
do_int ( regs , trapno , ( unsigned char __user * ) ( regs - > pt . ss < < 4 ) , SP ( regs ) ) ;
2005-04-17 05:20:36 +07:00
return 0 ;
}
2008-02-23 05:10:40 +07:00
if ( trapno ! = 1 )
2005-04-17 05:20:36 +07:00
return 1 ; /* we let this handle by the calling routine */
2012-03-12 16:25:55 +07:00
current - > thread . trap_nr = trapno ;
2005-04-17 05:20:36 +07:00
current - > thread . error_code = error_code ;
2008-03-17 16:21:08 +07:00
force_sig ( SIGTRAP , current ) ;
2005-04-17 05:20:36 +07:00
return 0 ;
}
2008-02-23 05:10:40 +07:00
void handle_vm86_fault ( struct kernel_vm86_regs * regs , long error_code )
2005-04-17 05:20:36 +07:00
{
unsigned char opcode ;
unsigned char __user * csp ;
unsigned char __user * ssp ;
2005-09-04 05:56:28 +07:00
unsigned short ip , sp , orig_flags ;
2005-04-17 05:20:36 +07:00
int data32 , pref_done ;
2015-07-29 12:41:17 +07:00
struct vm86plus_info_struct * vmpi = & current - > thread . vm86 - > vm86plus ;
2005-04-17 05:20:36 +07:00
# define CHECK_IF_IN_TRAP \
2015-07-29 12:41:17 +07:00
if ( vmpi - > vm86dbg_active & & vmpi - > vm86dbg_TFpendig ) \
2008-03-28 21:56:56 +07:00
newflags | = X86_EFLAGS_TF
2005-04-17 05:20:36 +07:00
2008-01-30 19:30:56 +07:00
orig_flags = * ( unsigned short * ) & regs - > pt . flags ;
2005-09-04 05:56:28 +07:00
2008-01-30 19:30:56 +07:00
csp = ( unsigned char __user * ) ( regs - > pt . cs < < 4 ) ;
ssp = ( unsigned char __user * ) ( regs - > pt . ss < < 4 ) ;
2005-04-17 05:20:36 +07:00
sp = SP ( regs ) ;
ip = IP ( regs ) ;
data32 = 0 ;
pref_done = 0 ;
do {
switch ( opcode = popb ( csp , ip , simulate_sigsegv ) ) {
2008-02-23 05:10:40 +07:00
case 0x66 : /* 32-bit data */ data32 = 1 ; break ;
case 0x67 : /* 32-bit address */ break ;
case 0x2e : /* CS */ break ;
case 0x3e : /* DS */ break ;
case 0x26 : /* ES */ break ;
case 0x36 : /* SS */ break ;
case 0x65 : /* GS */ break ;
case 0x64 : /* FS */ break ;
case 0xf2 : /* repnz */ break ;
case 0xf3 : /* rep */ break ;
default : pref_done = 1 ;
2005-04-17 05:20:36 +07:00
}
} while ( ! pref_done ) ;
switch ( opcode ) {
/* pushf */
case 0x9c :
if ( data32 ) {
pushl ( ssp , sp , get_vflags ( regs ) , simulate_sigsegv ) ;
SP ( regs ) - = 4 ;
} else {
pushw ( ssp , sp , get_vflags ( regs ) , simulate_sigsegv ) ;
SP ( regs ) - = 2 ;
}
IP ( regs ) = ip ;
2015-07-29 12:41:19 +07:00
goto vm86_fault_return ;
2005-04-17 05:20:36 +07:00
/* popf */
case 0x9d :
{
unsigned long newflags ;
if ( data32 ) {
2008-02-23 05:10:40 +07:00
newflags = popl ( ssp , sp , simulate_sigsegv ) ;
2005-04-17 05:20:36 +07:00
SP ( regs ) + = 4 ;
} else {
newflags = popw ( ssp , sp , simulate_sigsegv ) ;
SP ( regs ) + = 2 ;
}
IP ( regs ) = ip ;
CHECK_IF_IN_TRAP ;
2008-02-23 05:10:40 +07:00
if ( data32 )
2005-04-17 05:20:36 +07:00
set_vflags_long ( newflags , regs ) ;
2008-02-23 05:10:40 +07:00
else
2005-04-17 05:20:36 +07:00
set_vflags_short ( newflags , regs ) ;
2008-02-23 05:10:40 +07:00
2015-07-29 12:41:19 +07:00
goto check_vip ;
2005-04-17 05:20:36 +07:00
}
/* int xx */
case 0xcd : {
2008-02-23 05:10:40 +07:00
int intno = popb ( csp , ip , simulate_sigsegv ) ;
2005-04-17 05:20:36 +07:00
IP ( regs ) = ip ;
2015-07-29 12:41:17 +07:00
if ( vmpi - > vm86dbg_active ) {
2015-07-29 12:41:19 +07:00
if ( ( 1 < < ( intno & 7 ) ) & vmpi - > vm86dbg_intxxtab [ intno > > 3 ] ) {
save_v86_state ( regs , VM86_INTx + ( intno < < 8 ) ) ;
return ;
}
2005-04-17 05:20:36 +07:00
}
do_int ( regs , intno , ssp , sp ) ;
return ;
}
/* iret */
case 0xcf :
{
unsigned long newip ;
unsigned long newcs ;
unsigned long newflags ;
if ( data32 ) {
2008-02-23 05:10:40 +07:00
newip = popl ( ssp , sp , simulate_sigsegv ) ;
newcs = popl ( ssp , sp , simulate_sigsegv ) ;
newflags = popl ( ssp , sp , simulate_sigsegv ) ;
2005-04-17 05:20:36 +07:00
SP ( regs ) + = 12 ;
} else {
newip = popw ( ssp , sp , simulate_sigsegv ) ;
newcs = popw ( ssp , sp , simulate_sigsegv ) ;
newflags = popw ( ssp , sp , simulate_sigsegv ) ;
SP ( regs ) + = 6 ;
}
IP ( regs ) = newip ;
2008-01-30 19:30:56 +07:00
regs - > pt . cs = newcs ;
2005-04-17 05:20:36 +07:00
CHECK_IF_IN_TRAP ;
if ( data32 ) {
set_vflags_long ( newflags , regs ) ;
} else {
set_vflags_short ( newflags , regs ) ;
}
2015-07-29 12:41:19 +07:00
goto check_vip ;
2005-04-17 05:20:36 +07:00
}
/* cli */
case 0xfa :
IP ( regs ) = ip ;
clear_IF ( regs ) ;
2015-07-29 12:41:19 +07:00
goto vm86_fault_return ;
2005-04-17 05:20:36 +07:00
/* sti */
/*
* Damn . This is incorrect : the ' sti ' instruction should actually
* enable interrupts after the / next / instruction . Not good .
*
* Probably needs some horsing around with the TF flag . Aiee . .
*/
case 0xfb :
IP ( regs ) = ip ;
set_IF ( regs ) ;
2015-07-29 12:41:19 +07:00
goto check_vip ;
2005-04-17 05:20:36 +07:00
default :
2015-07-29 12:41:19 +07:00
save_v86_state ( regs , VM86_UNKNOWN ) ;
2005-04-17 05:20:36 +07:00
}
return ;
2015-07-29 12:41:19 +07:00
check_vip :
if ( VEFLAGS & X86_EFLAGS_VIP ) {
save_v86_state ( regs , VM86_STI ) ;
return ;
}
vm86_fault_return :
if ( vmpi - > force_return_for_pic & & ( VEFLAGS & ( X86_EFLAGS_IF | X86_EFLAGS_VIF ) ) ) {
save_v86_state ( regs , VM86_PICRETURN ) ;
return ;
}
if ( orig_flags & X86_EFLAGS_TF )
handle_vm86_trap ( regs , 0 , X86_TRAP_DB ) ;
return ;
2005-04-17 05:20:36 +07:00
simulate_sigsegv :
/* FIXME: After a long discussion with Stas we finally
* agreed , that this is wrong . Here we should
* really send a SIGSEGV to the user program .
* But how do we create the correct context ? We
* are inside a general protection fault handler
* and has just returned from a page fault handler .
* The correct context for the signal handler
* should be a mixture of the two , but how do we
* get the information ? [ KD ]
*/
2015-07-29 12:41:19 +07:00
save_v86_state ( regs , VM86_UNKNOWN ) ;
2005-04-17 05:20:36 +07:00
}
/* ---------------- vm86 special IRQ passing stuff ----------------- */
# define VM86_IRQNAME "vm86irq"
static struct vm86_irqs {
struct task_struct * tsk ;
int sig ;
} vm86_irqs [ 16 ] ;
static DEFINE_SPINLOCK ( irqbits_lock ) ;
static int irqbits ;
2008-02-23 05:10:40 +07:00
# define ALLOWED_SIGS (1 /* 0 = don't send a signal */ \
2005-04-17 05:20:36 +07:00
| ( 1 < < SIGUSR1 ) | ( 1 < < SIGUSR2 ) | ( 1 < < SIGIO ) | ( 1 < < SIGURG ) \
2008-02-23 05:10:40 +07:00
| ( 1 < < SIGUNUSED ) )
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 20:55:46 +07:00
static irqreturn_t irq_handler ( int intno , void * dev_id )
2005-04-17 05:20:36 +07:00
{
int irq_bit ;
unsigned long flags ;
2008-02-23 05:10:40 +07:00
spin_lock_irqsave ( & irqbits_lock , flags ) ;
2005-04-17 05:20:36 +07:00
irq_bit = 1 < < intno ;
2008-02-23 05:10:40 +07:00
if ( ( irqbits & irq_bit ) | | ! vm86_irqs [ intno ] . tsk )
2005-04-17 05:20:36 +07:00
goto out ;
irqbits | = irq_bit ;
if ( vm86_irqs [ intno ] . sig )
send_sig ( vm86_irqs [ intno ] . sig , vm86_irqs [ intno ] . tsk , 1 ) ;
/*
* IRQ will be re - enabled when user asks for the irq ( whether
* polling or as a result of the signal )
*/
2005-05-01 22:58:52 +07:00
disable_irq_nosync ( intno ) ;
spin_unlock_irqrestore ( & irqbits_lock , flags ) ;
2005-04-17 05:20:36 +07:00
return IRQ_HANDLED ;
out :
2008-02-23 05:10:40 +07:00
spin_unlock_irqrestore ( & irqbits_lock , flags ) ;
2005-04-17 05:20:36 +07:00
return IRQ_NONE ;
}
static inline void free_vm86_irq ( int irqnumber )
{
unsigned long flags ;
free_irq ( irqnumber , NULL ) ;
vm86_irqs [ irqnumber ] . tsk = NULL ;
2008-02-23 05:10:40 +07:00
spin_lock_irqsave ( & irqbits_lock , flags ) ;
2005-04-17 05:20:36 +07:00
irqbits & = ~ ( 1 < < irqnumber ) ;
2008-02-23 05:10:40 +07:00
spin_unlock_irqrestore ( & irqbits_lock , flags ) ;
2005-04-17 05:20:36 +07:00
}
void release_vm86_irqs ( struct task_struct * task )
{
int i ;
for ( i = FIRST_VM86_IRQ ; i < = LAST_VM86_IRQ ; i + + )
if ( vm86_irqs [ i ] . tsk = = task )
free_vm86_irq ( i ) ;
}
static inline int get_and_reset_irq ( int irqnumber )
{
int bit ;
unsigned long flags ;
2005-05-01 22:58:52 +07:00
int ret = 0 ;
2008-02-23 05:10:40 +07:00
2005-04-17 05:20:36 +07:00
if ( invalid_vm86_irq ( irqnumber ) ) return 0 ;
if ( vm86_irqs [ irqnumber ] . tsk ! = current ) return 0 ;
2008-02-23 05:10:40 +07:00
spin_lock_irqsave ( & irqbits_lock , flags ) ;
2005-04-17 05:20:36 +07:00
bit = irqbits & ( 1 < < irqnumber ) ;
irqbits & = ~ bit ;
2005-05-01 22:58:52 +07:00
if ( bit ) {
enable_irq ( irqnumber ) ;
ret = 1 ;
}
2008-02-23 05:10:40 +07:00
spin_unlock_irqrestore ( & irqbits_lock , flags ) ;
2005-05-01 22:58:52 +07:00
return ret ;
2005-04-17 05:20:36 +07:00
}
static int do_vm86_irq_handling ( int subfunction , int irqnumber )
{
int ret ;
switch ( subfunction ) {
case VM86_GET_AND_RESET_IRQ : {
return get_and_reset_irq ( irqnumber ) ;
}
case VM86_GET_IRQ_BITS : {
return irqbits ;
}
case VM86_REQUEST_IRQ : {
int sig = irqnumber > > 8 ;
int irq = irqnumber & 255 ;
if ( ! capable ( CAP_SYS_ADMIN ) ) return - EPERM ;
if ( ! ( ( 1 < < sig ) & ALLOWED_SIGS ) ) return - EPERM ;
if ( invalid_vm86_irq ( irq ) ) return - EPERM ;
if ( vm86_irqs [ irq ] . tsk ) return - EPERM ;
ret = request_irq ( irq , & irq_handler , 0 , VM86_IRQNAME , NULL ) ;
if ( ret ) return ret ;
vm86_irqs [ irq ] . sig = sig ;
vm86_irqs [ irq ] . tsk = current ;
return irq ;
}
case VM86_FREE_IRQ : {
if ( invalid_vm86_irq ( irqnumber ) ) return - EPERM ;
if ( ! vm86_irqs [ irqnumber ] . tsk ) return 0 ;
if ( vm86_irqs [ irqnumber ] . tsk ! = current ) return - EPERM ;
free_vm86_irq ( irqnumber ) ;
return 0 ;
}
}
return - EINVAL ;
}