Skip to content

Commit 2d363e9

Browse files
aagitgregkh
authored andcommitted
mm: pmd_read_atomic: fix 32bit PAE pmd walk vs pmd_populate SMP race condition
commit 26c1917 upstream. When holding the mmap_sem for reading, pmd_offset_map_lock should only run on a pmd_t that has been read atomically from the pmdp pointer, otherwise we may read only half of it leading to this crash. PID: 11679 TASK: f06e8000 CPU: 3 COMMAND: "do_race_2_panic" #0 [f06a9dd8] crash_kexec at c049b5ec raspberrypi#1 [f06a9e2c] oops_end at c083d1c2 raspberrypi#2 [f06a9e40] no_context at c0433ded raspberrypi#3 [f06a9e64] bad_area_nosemaphore at c043401a raspberrypi#4 [f06a9e6c] __do_page_fault at c0434493 raspberrypi#5 [f06a9eec] do_page_fault at c083eb45 raspberrypi#6 [f06a9f04] error_code (via page_fault) at c083c5d5 EAX: 01fb470c EBX: fff35000 ECX: 00000003 EDX: 00000100 EBP: 00000000 DS: 007b ESI: 9e201000 ES: 007b EDI: 01fb4700 GS: 00e0 CS: 0060 EIP: c083bc14 ERR: ffffffff EFLAGS: 00010246 raspberrypi#7 [f06a9f38] _spin_lock at c083bc14 raspberrypi#8 [f06a9f44] sys_mincore at c0507b7d raspberrypi#9 [f06a9fb0] system_call at c083becd start len EAX: ffffffda EBX: 9e200000 ECX: 00001000 EDX: 6228537f DS: 007b ESI: 00000000 ES: 007b EDI: 003d0f00 SS: 007b ESP: 62285354 EBP: 62285388 GS: 0033 CS: 0073 EIP: 00291416 ERR: 000000da EFLAGS: 00000286 This should be a longstanding bug affecting x86 32bit PAE without THP. Only archs with 64bit large pmd_t and 32bit unsigned long should be affected. With THP enabled the barrier() in pmd_none_or_trans_huge_or_clear_bad() would partly hide the bug when the pmd transition from none to stable, by forcing a re-read of the *pmd in pmd_offset_map_lock, but when THP is enabled a new set of problem arises by the fact could then transition freely in any of the none, pmd_trans_huge or pmd_trans_stable states. So making the barrier in pmd_none_or_trans_huge_or_clear_bad() unconditional isn't good idea and it would be a flakey solution. This should be fully fixed by introducing a pmd_read_atomic that reads the pmd in order with THP disabled, or by reading the pmd atomically with cmpxchg8b with THP enabled. Luckily this new race condition only triggers in the places that must already be covered by pmd_none_or_trans_huge_or_clear_bad() so the fix is localized there but this bug is not related to THP. NOTE: this can trigger on x86 32bit systems with PAE enabled with more than 4G of ram, otherwise the high part of the pmd will never risk to be truncated because it would be zero at all times, in turn so hiding the SMP race. This bug was discovered and fully debugged by Ulrich, quote: ---- [..] pmd_none_or_trans_huge_or_clear_bad() loads the content of edx and eax. 496 static inline int pmd_none_or_trans_huge_or_clear_bad(pmd_t *pmd) 497 { 498 /* depend on compiler for an atomic pmd read */ 499 pmd_t pmdval = *pmd; // edi = pmd pointer 0xc0507a74 <sys_mincore+548>: mov 0x8(%esp),%edi ... // edx = PTE page table high address 0xc0507a84 <sys_mincore+564>: mov 0x4(%edi),%edx ... // eax = PTE page table low address 0xc0507a8e <sys_mincore+574>: mov (%edi),%eax [..] Please note that the PMD is not read atomically. These are two "mov" instructions where the high order bits of the PMD entry are fetched first. Hence, the above machine code is prone to the following race. - The PMD entry {high|low} is 0x0000000000000000. The "mov" at 0xc0507a84 loads 0x00000000 into edx. - A page fault (on another CPU) sneaks in between the two "mov" instructions and instantiates the PMD. - The PMD entry {high|low} is now 0x00000003fda38067. The "mov" at 0xc0507a8e loads 0xfda38067 into eax. ---- Reported-by: Ulrich Obergfell <[email protected]> Signed-off-by: Andrea Arcangeli <[email protected]> Cc: Mel Gorman <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Larry Woodman <[email protected]> Cc: Petr Matousek <[email protected]> Cc: Rik van Riel <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent dce59c2 commit 2d363e9

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

arch/x86/include/asm/pgtable-3level.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,56 @@ static inline void native_set_pte(pte_t *ptep, pte_t pte)
3131
ptep->pte_low = pte.pte_low;
3232
}
3333

34+
#define pmd_read_atomic pmd_read_atomic
35+
/*
36+
* pte_offset_map_lock on 32bit PAE kernels was reading the pmd_t with
37+
* a "*pmdp" dereference done by gcc. Problem is, in certain places
38+
* where pte_offset_map_lock is called, concurrent page faults are
39+
* allowed, if the mmap_sem is hold for reading. An example is mincore
40+
* vs page faults vs MADV_DONTNEED. On the page fault side
41+
* pmd_populate rightfully does a set_64bit, but if we're reading the
42+
* pmd_t with a "*pmdp" on the mincore side, a SMP race can happen
43+
* because gcc will not read the 64bit of the pmd atomically. To fix
44+
* this all places running pmd_offset_map_lock() while holding the
45+
* mmap_sem in read mode, shall read the pmdp pointer using this
46+
* function to know if the pmd is null nor not, and in turn to know if
47+
* they can run pmd_offset_map_lock or pmd_trans_huge or other pmd
48+
* operations.
49+
*
50+
* Without THP if the mmap_sem is hold for reading, the
51+
* pmd can only transition from null to not null while pmd_read_atomic runs.
52+
* So there's no need of literally reading it atomically.
53+
*
54+
* With THP if the mmap_sem is hold for reading, the pmd can become
55+
* THP or null or point to a pte (and in turn become "stable") at any
56+
* time under pmd_read_atomic, so it's mandatory to read it atomically
57+
* with cmpxchg8b.
58+
*/
59+
#ifndef CONFIG_TRANSPARENT_HUGEPAGE
60+
static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
61+
{
62+
pmdval_t ret;
63+
u32 *tmp = (u32 *)pmdp;
64+
65+
ret = (pmdval_t) (*tmp);
66+
if (ret) {
67+
/*
68+
* If the low part is null, we must not read the high part
69+
* or we can end up with a partial pmd.
70+
*/
71+
smp_rmb();
72+
ret |= ((pmdval_t)*(tmp + 1)) << 32;
73+
}
74+
75+
return (pmd_t) { ret };
76+
}
77+
#else /* CONFIG_TRANSPARENT_HUGEPAGE */
78+
static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
79+
{
80+
return (pmd_t) { atomic64_read((atomic64_t *)pmdp) };
81+
}
82+
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
83+
3484
static inline void native_set_pte_atomic(pte_t *ptep, pte_t pte)
3585
{
3686
set_64bit((unsigned long long *)(ptep), native_pte_val(pte));

include/asm-generic/pgtable.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,18 @@ static inline int pmd_write(pmd_t pmd)
445445
#endif /* __HAVE_ARCH_PMD_WRITE */
446446
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
447447

448+
#ifndef pmd_read_atomic
449+
static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
450+
{
451+
/*
452+
* Depend on compiler for an atomic pmd read. NOTE: this is
453+
* only going to work, if the pmdval_t isn't larger than
454+
* an unsigned long.
455+
*/
456+
return *pmdp;
457+
}
458+
#endif
459+
448460
/*
449461
* This function is meant to be used by sites walking pagetables with
450462
* the mmap_sem hold in read mode to protect against MADV_DONTNEED and
@@ -458,11 +470,17 @@ static inline int pmd_write(pmd_t pmd)
458470
* undefined so behaving like if the pmd was none is safe (because it
459471
* can return none anyway). The compiler level barrier() is critically
460472
* important to compute the two checks atomically on the same pmdval.
473+
*
474+
* For 32bit kernels with a 64bit large pmd_t this automatically takes
475+
* care of reading the pmd atomically to avoid SMP race conditions
476+
* against pmd_populate() when the mmap_sem is hold for reading by the
477+
* caller (a special atomic read not done by "gcc" as in the generic
478+
* version above, is also needed when THP is disabled because the page
479+
* fault can populate the pmd from under us).
461480
*/
462481
static inline int pmd_none_or_trans_huge_or_clear_bad(pmd_t *pmd)
463482
{
464-
/* depend on compiler for an atomic pmd read */
465-
pmd_t pmdval = *pmd;
483+
pmd_t pmdval = pmd_read_atomic(pmd);
466484
/*
467485
* The barrier will stabilize the pmdval in a register or on
468486
* the stack so that it will stop changing under the code.

0 commit comments

Comments
 (0)