Skip to content

Commit 995909a

Browse files
joergroedeltorvalds
authored andcommitted
x86/mm/64: Do not dereference non-present PGD entries
The code for preallocate_vmalloc_pages() was written under the assumption that the p4d_offset() and pud_offset() functions will perform present checks before dereferencing the parent entries. This assumption is wrong an leads to a bug in the code which causes the physical address found in the PGD be used as a page-table page, even if the PGD is not present. So the code flow currently is: pgd = pgd_offset_k(addr); p4d = p4d_offset(pgd, addr); if (p4d_none(*p4d)) p4d = p4d_alloc(&init_mm, pgd, addr); This lacks a check for pgd_none() at least, the correct flow would be: pgd = pgd_offset_k(addr); if (pgd_none(*pgd)) p4d = p4d_alloc(&init_mm, pgd, addr); else p4d = p4d_offset(pgd, addr); But this is the same flow that the p4d_alloc() and the pud_alloc() functions use internally, so there is no need to duplicate them. Remove the p?d_none() checks from the function and just call into p4d_alloc() and pud_alloc() to correctly pre-allocate the PGD entries. Reported-and-tested-by: Jason A. Donenfeld <Jason@zx2c4.com> Reviewed-by: Mike Rapoport <rppt@linux.ibm.com> Fixes: 6eb82f9 ("x86/mm: Pre-allocate P4D/PUD pages for vmalloc area") Signed-off-by: Joerg Roedel <jroedel@suse.de> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9bceb80 commit 995909a

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

arch/x86/mm/init_64.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,28 +1253,23 @@ static void __init preallocate_vmalloc_pages(void)
12531253
p4d_t *p4d;
12541254
pud_t *pud;
12551255

1256-
p4d = p4d_offset(pgd, addr);
1257-
if (p4d_none(*p4d)) {
1258-
/* Can only happen with 5-level paging */
1259-
p4d = p4d_alloc(&init_mm, pgd, addr);
1260-
if (!p4d) {
1261-
lvl = "p4d";
1262-
goto failed;
1263-
}
1264-
}
1256+
lvl = "p4d";
1257+
p4d = p4d_alloc(&init_mm, pgd, addr);
1258+
if (!p4d)
1259+
goto failed;
12651260

1261+
/*
1262+
* With 5-level paging the P4D level is not folded. So the PGDs
1263+
* are now populated and there is no need to walk down to the
1264+
* PUD level.
1265+
*/
12661266
if (pgtable_l5_enabled())
12671267
continue;
12681268

1269-
pud = pud_offset(p4d, addr);
1270-
if (pud_none(*pud)) {
1271-
/* Ends up here only with 4-level paging */
1272-
pud = pud_alloc(&init_mm, p4d, addr);
1273-
if (!pud) {
1274-
lvl = "pud";
1275-
goto failed;
1276-
}
1277-
}
1269+
lvl = "pud";
1270+
pud = pud_alloc(&init_mm, p4d, addr);
1271+
if (!pud)
1272+
goto failed;
12781273
}
12791274

12801275
return;

0 commit comments

Comments
 (0)