Skip to content

Commit 569744c

Browse files
joergroedelpiso77
authored andcommitted
mm: track page table modifications in __apply_to_page_range()
BugLink: https://bugs.launchpad.net/bugs/1895097 commit e80d390 upstream. __apply_to_page_range() is also used to change and/or allocate page-table pages in the vmalloc area of the address space. Make sure these changes get synchronized to other page-tables in the system by calling arch_sync_kernel_mappings() when necessary. The impact appears limited to x86-32, where apply_to_page_range may miss updating the PMD. That leads to explosions in drivers like BUG: unable to handle page fault for address: fe036000 #PF: supervisor write access in kernel mode #PF: error_code(0x0002) - not-present page *pde = 00000000 Oops: 0002 [#1] SMP CPU: 3 PID: 1300 Comm: gem_concurrent_ Not tainted 5.9.0-rc1+ #16 Hardware name: /NUC6i3SYB, BIOS SYSKLi35.86A.0024.2015.1027.2142 10/27/2015 EIP: __execlists_context_alloc+0x132/0x2d0 [i915] Code: 31 d2 89 f0 e8 2f 55 02 00 89 45 e8 3d 00 f0 ff ff 0f 87 11 01 00 00 8b 4d e8 03 4b 30 b8 5a 5a 5a 5a ba 01 00 00 00 8d 79 04 <c7> 01 5a 5a 5a 5a c7 81 fc 0f 00 00 5a 5a 5a 5a 83 e7 fc 29 f9 81 EAX: 5a5a5a5a EBX: f60ca000 ECX: fe036000 EDX: 00000001 ESI: f43b7340 EDI: fe036004 EBP: f6389cb8 ESP: f6389c9c DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010286 CR0: 80050033 CR2: fe036000 CR3: 2d361000 CR4: 001506d0 DR0: 00000000 DR1: 00000000 DR2: 00000000 DR3: 00000000 DR6: fffe0ff0 DR7: 00000400 Call Trace: execlists_context_alloc+0x10/0x20 [i915] intel_context_alloc_state+0x3f/0x70 [i915] __intel_context_do_pin+0x117/0x170 [i915] i915_gem_do_execbuffer+0xcc7/0x2500 [i915] i915_gem_execbuffer2_ioctl+0xcd/0x1f0 [i915] drm_ioctl_kernel+0x8f/0xd0 drm_ioctl+0x223/0x3d0 __ia32_sys_ioctl+0x1ab/0x760 __do_fast_syscall_32+0x3f/0x70 do_fast_syscall_32+0x29/0x60 do_SYSENTER_32+0x15/0x20 entry_SYSENTER_32+0x9f/0xf2 EIP: 0xb7f28559 Code: 03 74 c0 01 10 05 03 74 b8 01 10 06 03 74 b4 01 10 07 03 74 b0 01 10 08 03 74 d8 01 00 00 00 00 00 51 52 55 89 e5 0f 34 cd 80 <5d> 5a 59 c3 90 90 90 90 8d 76 00 58 b8 77 00 00 00 cd 80 90 8d 76 EAX: ffffffda EBX: 00000005 ECX: c0406469 EDX: bf95556c ESI: b7e68000 EDI: c0406469 EBP: 00000005 ESP: bf9554d8 DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00000296 Modules linked in: i915 x86_pkg_temp_thermal intel_powerclamp crc32_pclmul crc32c_intel intel_cstate intel_uncore intel_gtt drm_kms_helper intel_pch_thermal video button autofs4 i2c_i801 i2c_smbus fan CR2: 00000000fe036000 It looks like kasan, xen and i915 are vulnerable. Actual impact is "on thinkpad X60 in 5.9-rc1, screen starts blinking after 30-or-so minutes, and machine is unusable" [sfr@canb.auug.org.au: ARCH_PAGE_TABLE_SYNC_MASK needs vmalloc.h] Link: https://lkml.kernel.org/r/20200825172508.16800a4f@canb.auug.org.au [chris@chris-wilson.co.uk: changelog addition] [pavel@ucw.cz: changelog addition] Fixes: 2ba3e69 ("mm/vmalloc: track which page-table levels were modified") Fixes: 86cf69f ("x86/mm/32: implement arch_sync_kernel_mappings()") Signed-off-by: Joerg Roedel <jroedel@suse.de> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Tested-by: Chris Wilson <chris@chris-wilson.co.uk> [x86-32] Tested-by: Pavel Machek <pavel@ucw.cz> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Cc: <stable@vger.kernel.org> [5.8+] Link: https://lkml.kernel.org/r/20200821123746.16904-1-joro@8bytes.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
1 parent af71f41 commit 569744c

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

mm/memory.c

+23-13
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include <linux/dax.h>
7272
#include <linux/oom.h>
7373
#include <linux/numa.h>
74+
#include <linux/vmalloc.h>
7475

7576
#include <trace/events/kmem.h>
7677

@@ -2202,15 +2203,16 @@ EXPORT_SYMBOL(vm_iomap_memory);
22022203

22032204
static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
22042205
unsigned long addr, unsigned long end,
2205-
pte_fn_t fn, void *data, bool create)
2206+
pte_fn_t fn, void *data, bool create,
2207+
pgtbl_mod_mask *mask)
22062208
{
22072209
pte_t *pte;
22082210
int err = 0;
22092211
spinlock_t *uninitialized_var(ptl);
22102212

22112213
if (create) {
22122214
pte = (mm == &init_mm) ?
2213-
pte_alloc_kernel(pmd, addr) :
2215+
pte_alloc_kernel_track(pmd, addr, mask) :
22142216
pte_alloc_map_lock(mm, pmd, addr, &ptl);
22152217
if (!pte)
22162218
return -ENOMEM;
@@ -2231,6 +2233,7 @@ static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
22312233
break;
22322234
}
22332235
} while (addr += PAGE_SIZE, addr != end);
2236+
*mask |= PGTBL_PTE_MODIFIED;
22342237

22352238
arch_leave_lazy_mmu_mode();
22362239

@@ -2241,7 +2244,8 @@ static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
22412244

22422245
static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
22432246
unsigned long addr, unsigned long end,
2244-
pte_fn_t fn, void *data, bool create)
2247+
pte_fn_t fn, void *data, bool create,
2248+
pgtbl_mod_mask *mask)
22452249
{
22462250
pmd_t *pmd;
22472251
unsigned long next;
@@ -2250,7 +2254,7 @@ static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
22502254
BUG_ON(pud_huge(*pud));
22512255

22522256
if (create) {
2253-
pmd = pmd_alloc(mm, pud, addr);
2257+
pmd = pmd_alloc_track(mm, pud, addr, mask);
22542258
if (!pmd)
22552259
return -ENOMEM;
22562260
} else {
@@ -2260,7 +2264,7 @@ static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
22602264
next = pmd_addr_end(addr, end);
22612265
if (create || !pmd_none_or_clear_bad(pmd)) {
22622266
err = apply_to_pte_range(mm, pmd, addr, next, fn, data,
2263-
create);
2267+
create, mask);
22642268
if (err)
22652269
break;
22662270
}
@@ -2270,14 +2274,15 @@ static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
22702274

22712275
static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
22722276
unsigned long addr, unsigned long end,
2273-
pte_fn_t fn, void *data, bool create)
2277+
pte_fn_t fn, void *data, bool create,
2278+
pgtbl_mod_mask *mask)
22742279
{
22752280
pud_t *pud;
22762281
unsigned long next;
22772282
int err = 0;
22782283

22792284
if (create) {
2280-
pud = pud_alloc(mm, p4d, addr);
2285+
pud = pud_alloc_track(mm, p4d, addr, mask);
22812286
if (!pud)
22822287
return -ENOMEM;
22832288
} else {
@@ -2287,7 +2292,7 @@ static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
22872292
next = pud_addr_end(addr, end);
22882293
if (create || !pud_none_or_clear_bad(pud)) {
22892294
err = apply_to_pmd_range(mm, pud, addr, next, fn, data,
2290-
create);
2295+
create, mask);
22912296
if (err)
22922297
break;
22932298
}
@@ -2297,14 +2302,15 @@ static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
22972302

22982303
static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
22992304
unsigned long addr, unsigned long end,
2300-
pte_fn_t fn, void *data, bool create)
2305+
pte_fn_t fn, void *data, bool create,
2306+
pgtbl_mod_mask *mask)
23012307
{
23022308
p4d_t *p4d;
23032309
unsigned long next;
23042310
int err = 0;
23052311

23062312
if (create) {
2307-
p4d = p4d_alloc(mm, pgd, addr);
2313+
p4d = p4d_alloc_track(mm, pgd, addr, mask);
23082314
if (!p4d)
23092315
return -ENOMEM;
23102316
} else {
@@ -2314,7 +2320,7 @@ static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
23142320
next = p4d_addr_end(addr, end);
23152321
if (create || !p4d_none_or_clear_bad(p4d)) {
23162322
err = apply_to_pud_range(mm, p4d, addr, next, fn, data,
2317-
create);
2323+
create, mask);
23182324
if (err)
23192325
break;
23202326
}
@@ -2327,8 +2333,9 @@ static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr,
23272333
void *data, bool create)
23282334
{
23292335
pgd_t *pgd;
2330-
unsigned long next;
2336+
unsigned long start = addr, next;
23312337
unsigned long end = addr + size;
2338+
pgtbl_mod_mask mask = 0;
23322339
int err = 0;
23332340

23342341
if (WARN_ON(addr >= end))
@@ -2339,11 +2346,14 @@ static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr,
23392346
next = pgd_addr_end(addr, end);
23402347
if (!create && pgd_none_or_clear_bad(pgd))
23412348
continue;
2342-
err = apply_to_p4d_range(mm, pgd, addr, next, fn, data, create);
2349+
err = apply_to_p4d_range(mm, pgd, addr, next, fn, data, create, &mask);
23432350
if (err)
23442351
break;
23452352
} while (pgd++, addr = next, addr != end);
23462353

2354+
if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
2355+
arch_sync_kernel_mappings(start, start + size);
2356+
23472357
return err;
23482358
}
23492359

0 commit comments

Comments
 (0)