Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2053189pxa; Fri, 7 Aug 2020 01:42:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCLHDP8yaS1GI5Lj7yVM0ShLEipPOLxh+4onb7osTpD011X106zoCpxwR1zLOtIsgdx1sG X-Received: by 2002:a05:6402:1bc5:: with SMTP id ch5mr7533345edb.364.1596789755334; Fri, 07 Aug 2020 01:42:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596789755; cv=none; d=google.com; s=arc-20160816; b=YtmGN27Sm56mr3OzliWY1UAvU9lEfJ7mMBmRfx2Y1No5caBQ88IlYs6Kr6v+bca2yf zPry7mA1eDtNRVO8Vri+O1hy3zr2f95O052U+Gmp+gD721EHK7EN3xsxKosTDmDian7j qO3zsei2veA6vioBh3/ipNsUwEkVJXy449fM5mHMv/sV7KpcGXn0QduZqGZlMwmpyUie 4XCbOMqoH57ZkB9hTWy3tj9rw0uw1pXHtTY17/+6CJtFfXV2ofsYPUiZ/DhD+BimhP8V UEAFYPVA5Ll4TZcYqqQJlT1KywjOq/lG/n+g7hsyjsQKWqqpU1bRwGMoPUeTi/DKeR0/ KTyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=SY8QXrkxDlGbIYajZxkS3gc0CLg9dLxWKvNGTclsgiM=; b=qB4CBaOxKZNamAl9aYrP+7EDexiu2/M3HJtARe2PMI+KokjxTXpL8dgF0MCNsXHV7L S8FLEZGUX6IAX3I/u3EhM2vck5ulROm0kNP0wi9351rSTut6arnOT7H+7ssA28E76iV8 GjpN9mzbQ4X6GG2WB6ofNYjs1vr1Or+iEaOUBvhJfhWTS+6+THANdlG6THejcRoYpn3K wHN/LpQxHoP2rx6Ocig098LTxqily2fT0fmdWm3efeFkaUzQMLgDrr7d3Z8ky/q3aFgH OUzDsyaWaw1EvlRIzryrqeCa0irmFbxB3Fn+ToUcQS6NWw75s4CRSd46VzFaEnh3mVBw 15LA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j18si4465656edq.576.2020.08.07.01.42.12; Fri, 07 Aug 2020 01:42:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728257AbgHGIkh (ORCPT + 99 others); Fri, 7 Aug 2020 04:40:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728179AbgHGIkc (ORCPT ); Fri, 7 Aug 2020 04:40:32 -0400 Received: from theia.8bytes.org (8bytes.org [IPv6:2a01:238:4383:600:38bc:a715:4b6d:a889]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C638C061574 for ; Fri, 7 Aug 2020 01:40:30 -0700 (PDT) Received: by theia.8bytes.org (Postfix, from userid 1000) id 3D2AD321; Fri, 7 Aug 2020 10:40:27 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Dave Hansen , Andy Lutomirski , Peter Zijlstra , hpa@zytor.com, Mike Rapoport , Joerg Roedel , Linus Torvalds , Jason@zx2c4.com, Andrew Morton , linux-kernel@vger.kernel.org, joro@8bytes.org Subject: [PATCH] x86/mm/64: Do not dereference non-present PGD entries Date: Fri, 7 Aug 2020 10:40:13 +0200 Message-Id: <20200807084013.7090-1-joro@8bytes.org> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel 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-by: Jason A. Donenfeld Fixes: 6eb82f994026 ("x86/mm: Pre-allocate P4D/PUD pages for vmalloc area") Signed-off-by: Joerg Roedel --- arch/x86/mm/init_64.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index 3f4e29a78f2b..449e071240e1 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1253,28 +1253,23 @@ static void __init preallocate_vmalloc_pages(void) p4d_t *p4d; pud_t *pud; - p4d = p4d_offset(pgd, addr); - if (p4d_none(*p4d)) { - /* Can only happen with 5-level paging */ - p4d = p4d_alloc(&init_mm, pgd, addr); - if (!p4d) { - lvl = "p4d"; - goto failed; - } - } + lvl = "p4d"; + p4d = p4d_alloc(&init_mm, pgd, addr); + if (!p4d) + goto failed; + /* + * With 5-level paging the P4D level is not folded. So the PGDs + * are now populated and there is no need to walk down to the + * PUD level. + */ if (pgtable_l5_enabled()) continue; - pud = pud_offset(p4d, addr); - if (pud_none(*pud)) { - /* Ends up here only with 4-level paging */ - pud = pud_alloc(&init_mm, p4d, addr); - if (!pud) { - lvl = "pud"; - goto failed; - } - } + lvl = "pud"; + pud = pud_alloc(&init_mm, p4d, addr); + if (!pud) + goto failed; } return; -- 2.26.2