From nobody Tue Nov 26 17:35:55 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 587012076CC; Wed, 16 Oct 2024 11:15:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729077309; cv=none; b=rE+Ldi6yfacmy7FRaHmh46yu3OzmQDsx5D/Ysmo3YIJFMXmGM8UG82kTJlbLF+x+q1xYHFp96ZAOSC6SB9OEGUfwdBs4QsIGnTu/zZehQy+VJlvICYtHlpJnvaozPIGPWl04KzeeWqcwZnVHwooS4XYs837mjTB2q5118kQsa7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729077309; c=relaxed/simple; bh=2G12tmQBMRi94wiYlnzPggyN9sb1W4iSnDlgG9gAd9I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=injQlmof7nGXhfaWe4xKUtFiKatohfQI5IYeFKVxtNvw0wkZP4wtqF1QJxeOwiUBXpRjzg5I0C7TJeD/o6xQj7CMlT0Hk3eU0c1kjZVSh5SlRVqAlMJFz8dznCfnbp3Ntlqr45c0kujkpgwwzVvUBmCA/u9gUYmiVBQFstmkITM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Dg2i3tSq; arc=none smtp.client-ip=198.175.65.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Dg2i3tSq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729077309; x=1760613309; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2G12tmQBMRi94wiYlnzPggyN9sb1W4iSnDlgG9gAd9I=; b=Dg2i3tSqi9qGiAudUVI+DZpGAmUJUkl4Xy0+wvQoEe6+xQhONkTTxiLV oMFWpKuCVdUSHSHSilMvwhUFqrqHoRA6Jy+lm5J6bg56ZJXUxyhDqBMo5 YNQUemV0hcsSjbjpWsSfXJ8YekhdAx2Jo5GEC36qgAd+diZzF9AKf/jgl 7URdiKDVUodnA+5f+VHEo+4hYQhsQtE4l0WBvgwh04pbLrrE24fRSGNQD XViIrLKP5OOMZOJtsQjqOmnQ/N3gYh/JJtTHUvUGp9xey8m0qUG/+FvIA rcJruJWRAYEPl/zOFfCPIjDJuQ3d4LFaC3hbm1+X74GOx6L+hpDQCsE1M A==; X-CSE-ConnectionGUID: R/zfUpSDRvKQDrMswyya+A== X-CSE-MsgGUID: EFSqUkQ1RMWQImD5z/Cwvw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="39066168" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="39066168" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2024 04:15:06 -0700 X-CSE-ConnectionGUID: OSZnxQsVTTqIzlqFj+V4Sg== X-CSE-MsgGUID: q9Bkae3YSLiVD3IOv6T/Yg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="83005216" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa003.jf.intel.com with ESMTP; 16 Oct 2024 04:15:02 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id E3F9FCB; Wed, 16 Oct 2024 14:14:59 +0300 (EEST) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , "Rafael J. Wysocki" , Andy Lutomirski , Peter Zijlstra , Baoquan He Cc: Ard Biesheuvel , Tom Lendacky , Andrew Morton , Thomas Zimmermann , Sean Christopherson , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, "Kirill A. Shutemov" , Kai Huang Subject: [PATCHv4, REBASED 1/4] x86/mm/ident_map: Fix virtual address wrap to zero Date: Wed, 16 Oct 2024 14:14:55 +0300 Message-ID: <20241016111458.846228-2-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241016111458.846228-1-kirill.shutemov@linux.intel.com> References: <20241016111458.846228-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Calculation of 'next' virtual address doesn't protect against wrapping to zero. It can result in page table corruption and hang. The problematic case is possible if user sets high x86_mapping_info::offset. The wrapping to zero only occurs if the top PGD entry is accessed. There are no such users in the upstream. Only hibernate_64.c uses x86_mapping_info::offset, and it operates on the direct mapping range, which is not the top PGD entry. Replace manual 'next' calculation with p?d_addr_end() which handles wrapping correctly. Signed-off-by: Kirill A. Shutemov Reviewed-by: Kai Huang Reviewed-by: Tom Lendacky --- arch/x86/mm/ident_map.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/arch/x86/mm/ident_map.c b/arch/x86/mm/ident_map.c index 437e96fb4977..5872f3ee863c 100644 --- a/arch/x86/mm/ident_map.c +++ b/arch/x86/mm/ident_map.c @@ -101,9 +101,7 @@ static int ident_pud_init(struct x86_mapping_info *info= , pud_t *pud_page, pmd_t *pmd; bool use_gbpage; =20 - next =3D (addr & PUD_MASK) + PUD_SIZE; - if (next > end) - next =3D end; + next =3D pud_addr_end(addr, end); =20 /* if this is already a gbpage, this portion is already mapped */ if (pud_leaf(*pud)) @@ -154,10 +152,7 @@ static int ident_p4d_init(struct x86_mapping_info *inf= o, p4d_t *p4d_page, p4d_t *p4d =3D p4d_page + p4d_index(addr); pud_t *pud; =20 - next =3D (addr & P4D_MASK) + P4D_SIZE; - if (next > end) - next =3D end; - + next =3D p4d_addr_end(addr, end); if (p4d_present(*p4d)) { pud =3D pud_offset(p4d, 0); result =3D ident_pud_init(info, pud, addr, next); @@ -199,10 +194,7 @@ int kernel_ident_mapping_init(struct x86_mapping_info = *info, pgd_t *pgd_page, pgd_t *pgd =3D pgd_page + pgd_index(addr); p4d_t *p4d; =20 - next =3D (addr & PGDIR_MASK) + PGDIR_SIZE; - if (next > end) - next =3D end; - + next =3D pgd_addr_end(addr, end); if (pgd_present(*pgd)) { p4d =3D p4d_offset(pgd, 0); result =3D ident_p4d_init(info, p4d, addr, next); --=20 2.45.2 From nobody Tue Nov 26 17:35:55 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D25D2076DE; Wed, 16 Oct 2024 11:15:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729077310; cv=none; b=cYWb1pX7qfqch+TDEaDmfSUW3qAiULJR4fGlR2JKn6ft5MVlczuY1EDng/dJwUM5c26JxVDZkZgAi5SUQINQy7r1CaN+a6d1/drjiv9Vw+bJg3gVeluJ+DIqaA1wmQ2W8wWHRkTDEUFUGiqiZwRVQ6dlrZXMFRWbVBXQD39SqZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729077310; c=relaxed/simple; bh=Vsji7fvGKoZdggpdAR7OdUv+hIBMd4JgmdmhDh86f/Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GiI5tsm7v9pKQZDpowLF3we/Ycqf7Jug5S+0VhE6Ol6zSDzdrQw7kpw8nvbMXkignLFFMyLenB1Yb+Mf2+i1zLC9kQ1dpSMhVyscaUJsrDDR/54tnu80I77XBpSp6TaKyeflmcscd3FIUFg7+zxmwX71fs2vn8I6a/TW52gDwU0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=joRgA+9Y; arc=none smtp.client-ip=198.175.65.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="joRgA+9Y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729077309; x=1760613309; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Vsji7fvGKoZdggpdAR7OdUv+hIBMd4JgmdmhDh86f/Y=; b=joRgA+9YVk2ua48mzggg30dePbba7bxBFvHXrzbQsAXYnzBJwPuDUYB5 cMr+iTJfCJieDWFPNBOEx+PccbT3RRIuU88IgiNtQf0oSQoKW+CzMmyMd uA4V4NMR2Y4llT6hq6C2iQw22giUSAq40ZMKk4kfF98x7sMwCqcRGswgJ YhNYkNX1+ET9PELHcHQZ0a8JVtZcccDEltLqDlLRydE0y/qv90lxXVQpJ j3/ELpX+qqX1+zX5LKxYcIcqgfh2wjjSR0hYe/Uq+ypZ2xARp3eLwDzdX pHdWY8XkFXxjKWF5Ky6OaqyW76GTrdj33tyMM2g7klbQZ/ir25hlck5hC Q==; X-CSE-ConnectionGUID: 2Qws6h3uT0uLntklBTrqiQ== X-CSE-MsgGUID: nUFc0cADT/GmBiAVXpkYVQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="39066179" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="39066179" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2024 04:15:06 -0700 X-CSE-ConnectionGUID: fqRJ8b2eRsif7EIGcZVWRg== X-CSE-MsgGUID: 6kn2GzlQS16R7ueit9HN5A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="83005218" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa003.jf.intel.com with ESMTP; 16 Oct 2024 04:15:02 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id EE0D31AC; Wed, 16 Oct 2024 14:14:59 +0300 (EEST) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , "Rafael J. Wysocki" , Andy Lutomirski , Peter Zijlstra , Baoquan He Cc: Ard Biesheuvel , Tom Lendacky , Andrew Morton , Thomas Zimmermann , Sean Christopherson , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, "Kirill A. Shutemov" , Kai Huang , "Rafael J . Wysocki" Subject: [PATCHv4, REBASED 2/4] x86/acpi: Replace manual page table initialization with kernel_ident_mapping_init() Date: Wed, 16 Oct 2024 14:14:56 +0300 Message-ID: <20241016111458.846228-3-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241016111458.846228-1-kirill.shutemov@linux.intel.com> References: <20241016111458.846228-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The function init_transition_pgtable() maps the page with asm_acpi_mp_play_dead() into an identity mapping. Replace manual page table initialization with kernel_ident_mapping_init() to avoid code duplication. Use x86_mapping_info::offset to get the page mapped at the correct location. Signed-off-by: Kirill A. Shutemov Reviewed-by: Kai Huang Reviewed-by: Tom Lendacky Acked-by: Rafael J. Wysocki --- arch/x86/kernel/acpi/madt_wakeup.c | 73 ++++++------------------------ 1 file changed, 15 insertions(+), 58 deletions(-) diff --git a/arch/x86/kernel/acpi/madt_wakeup.c b/arch/x86/kernel/acpi/madt= _wakeup.c index d5ef6215583b..f36f28405dcc 100644 --- a/arch/x86/kernel/acpi/madt_wakeup.c +++ b/arch/x86/kernel/acpi/madt_wakeup.c @@ -70,58 +70,6 @@ static void __init free_pgt_page(void *pgt, void *dummy) return memblock_free(pgt, PAGE_SIZE); } =20 -/* - * Make sure asm_acpi_mp_play_dead() is present in the identity mapping at - * the same place as in the kernel page tables. asm_acpi_mp_play_dead() sw= itches - * to the identity mapping and the function has be present at the same spo= t in - * the virtual address space before and after switching page tables. - */ -static int __init init_transition_pgtable(pgd_t *pgd) -{ - pgprot_t prot =3D PAGE_KERNEL_EXEC_NOENC; - unsigned long vaddr, paddr; - p4d_t *p4d; - pud_t *pud; - pmd_t *pmd; - pte_t *pte; - - vaddr =3D (unsigned long)asm_acpi_mp_play_dead; - pgd +=3D pgd_index(vaddr); - if (!pgd_present(*pgd)) { - p4d =3D (p4d_t *)alloc_pgt_page(NULL); - if (!p4d) - return -ENOMEM; - set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE)); - } - p4d =3D p4d_offset(pgd, vaddr); - if (!p4d_present(*p4d)) { - pud =3D (pud_t *)alloc_pgt_page(NULL); - if (!pud) - return -ENOMEM; - set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE)); - } - pud =3D pud_offset(p4d, vaddr); - if (!pud_present(*pud)) { - pmd =3D (pmd_t *)alloc_pgt_page(NULL); - if (!pmd) - return -ENOMEM; - set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); - } - pmd =3D pmd_offset(pud, vaddr); - if (!pmd_present(*pmd)) { - pte =3D (pte_t *)alloc_pgt_page(NULL); - if (!pte) - return -ENOMEM; - set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE)); - } - pte =3D pte_offset_kernel(pmd, vaddr); - - paddr =3D __pa(vaddr); - set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot)); - - return 0; -} - static int __init acpi_mp_setup_reset(u64 reset_vector) { struct x86_mapping_info info =3D { @@ -130,6 +78,7 @@ static int __init acpi_mp_setup_reset(u64 reset_vector) .page_flag =3D __PAGE_KERNEL_LARGE_EXEC, .kernpg_flag =3D _KERNPG_TABLE_NOENC, }; + unsigned long mstart, mend; pgd_t *pgd; =20 pgd =3D alloc_pgt_page(NULL); @@ -137,8 +86,6 @@ static int __init acpi_mp_setup_reset(u64 reset_vector) return -ENOMEM; =20 for (int i =3D 0; i < nr_pfn_mapped; i++) { - unsigned long mstart, mend; - mstart =3D pfn_mapped[i].start << PAGE_SHIFT; mend =3D pfn_mapped[i].end << PAGE_SHIFT; if (kernel_ident_mapping_init(&info, pgd, mstart, mend)) { @@ -147,14 +94,24 @@ static int __init acpi_mp_setup_reset(u64 reset_vector) } } =20 - if (kernel_ident_mapping_init(&info, pgd, - PAGE_ALIGN_DOWN(reset_vector), - PAGE_ALIGN(reset_vector + 1))) { + mstart =3D PAGE_ALIGN_DOWN(reset_vector); + mend =3D mstart + PAGE_SIZE; + if (kernel_ident_mapping_init(&info, pgd, mstart, mend)) { kernel_ident_mapping_free(&info, pgd); return -ENOMEM; } =20 - if (init_transition_pgtable(pgd)) { + /* + * Make sure asm_acpi_mp_play_dead() is present in the identity mapping + * at the same place as in the kernel page tables. + * asm_acpi_mp_play_dead() switches to the identity mapping and the + * function must be present at the same spot in the virtual address space + * before and after switching page tables. + */ + info.offset =3D __START_KERNEL_map - phys_base; + mstart =3D PAGE_ALIGN_DOWN(__pa(asm_acpi_mp_play_dead)); + mend =3D mstart + PAGE_SIZE; + if (kernel_ident_mapping_init(&info, pgd, mstart, mend)) { kernel_ident_mapping_free(&info, pgd); return -ENOMEM; } --=20 2.45.2 From nobody Tue Nov 26 17:35:55 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90B472076C0; Wed, 16 Oct 2024 11:15:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729077309; cv=none; b=dUdmTAUgXzTqIYrsdRwtV2LeqzkkU5pgMKqP0sFzKU+zwtdOsPk/IzNXSYwdJFIC4QaAGBwSaEjuqwCBQnHjN462zaRzdzuVEKOwLAVdsi/ZLFokowOHDd9QRv+rbqeJeIisZB/TyIIjJdtMMkcuoAK4E2bvDnqxKi/DIJYk2xs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729077309; c=relaxed/simple; bh=OGHaZPLBCwVGgyYZNgzO+oCLSrdJlWpxHavpfRhGxYY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=m2FYnOiNMgdhafQLabX7QhM9QRbEOC45JEsj+PIl863JEdzzMin/CXaFWJ00eH8b1dXMjEfJt4C4oD0YNC/Pp3nVuYt2hj6A2XmEASpnw+OcwF4xfglqTKBmaH1KsjvhkrxBPZsyYb4ELED8tehXctsJR1H5bjLfBtr52FjNmKg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Bpq2Stdk; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Bpq2Stdk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729077308; x=1760613308; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OGHaZPLBCwVGgyYZNgzO+oCLSrdJlWpxHavpfRhGxYY=; b=Bpq2StdkNNTlFFrQJhmA/H1hVQm/VJiU1n2X6Sdx/MYX0EbYdj676M1O yNaBQxuJDyiB5AsFu0b6zfjS9Id4Nok5hhE5yKPT7ooNasMtUwJBSx2O4 wdYBMvFNMXW0DP9ptNviUnzHwrLPFfty3zV+8YqahIJBZ0cKN/OAv7AhQ A49MiWoYjt19l9vS4WOC/QGcpTQyyeniBjVIHGJdCchr2bS6tDdVIu6QA yDJ0CYMdf1cvSsO9sVOm6Bx8BOL9A+/NY4KC5KWU4TupuDkq/XBb5AKuh szkTQMefQ5iY1DZmflyIGQxrY8iQkilyTZp3CBYZj7QClz/dpGh7a4xsA A==; X-CSE-ConnectionGUID: GP3IlmV7QwKSvfu/AbRuuQ== X-CSE-MsgGUID: twdGYvDZQ5iLl7uGfdgdIQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="51054363" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="51054363" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2024 04:15:06 -0700 X-CSE-ConnectionGUID: fRGWWL1eRASml4rdXnlnCQ== X-CSE-MsgGUID: nDhgLP/jQJ+f8s/32X3ciw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,207,1725346800"; d="scan'208";a="108937914" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa001.fm.intel.com with ESMTP; 16 Oct 2024 04:15:01 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 0D4572AC; Wed, 16 Oct 2024 14:15:00 +0300 (EEST) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , "Rafael J. Wysocki" , Andy Lutomirski , Peter Zijlstra , Baoquan He Cc: Ard Biesheuvel , Tom Lendacky , Andrew Morton , Thomas Zimmermann , Sean Christopherson , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, "Kirill A. Shutemov" , Kai Huang Subject: [PATCHv4, REBASED 3/4] x86/64/kexec: Map original relocate_kernel() in init_transition_pgtable() Date: Wed, 16 Oct 2024 14:14:57 +0300 Message-ID: <20241016111458.846228-4-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241016111458.846228-1-kirill.shutemov@linux.intel.com> References: <20241016111458.846228-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The init_transition_pgtable() function sets up transitional page tables. It ensures that the relocate_kernel() function is present in the identity mapping at the same location as in the kernel page tables. relocate_kernel() switches to the identity mapping, and the function must be present at the same location in the virtual address space before and after switching page tables. init_transition_pgtable() maps a copy of relocate_kernel() in image->control_code_page at the relocate_kernel() virtual address, but the original physical address of relocate_kernel() would also work. It is safe to use original relocate_kernel() physical address cannot be overwritten until swap_pages() is called, and the relocate_kernel() virtual address will not be used by then. Map the original relocate_kernel() at the relocate_kernel() virtual address in the identity mapping. It is preparation to replace the init_transition_pgtable() implementation with a call to kernel_ident_mapping_init(). Note that while relocate_kernel() switches to the identity mapping, it does not flush global TLB entries (CR4.PGE is not cleared). This means that in most cases, the kernel still runs relocate_kernel() from the original physical address before the change. Signed-off-by: Kirill A. Shutemov Reviewed-by: Kai Huang --- arch/x86/kernel/machine_kexec_64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 9c9ac606893e..645690e81c2d 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -157,7 +157,7 @@ static int init_transition_pgtable(struct kimage *image= , pgd_t *pgd) pte_t *pte; =20 vaddr =3D (unsigned long)relocate_kernel; - paddr =3D __pa(page_address(image->control_code_page)+PAGE_SIZE); + paddr =3D __pa(relocate_kernel); pgd +=3D pgd_index(vaddr); if (!pgd_present(*pgd)) { p4d =3D (p4d_t *)get_zeroed_page(GFP_KERNEL); --=20 2.45.2 From nobody Tue Nov 26 17:35:55 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C05BD1B0F0D; Wed, 16 Oct 2024 11:15:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729077308; cv=none; b=eCRsZQcMiCzxG1zfr7cxFLjuBGm1kkplg7qDgiBKbEdDeOPrgWrOCijuh5sSPvrtTAVaNP1pHrYaATTBWc7M9Cu7pCUgWw2FhL1UZ0WpLK8Jv3waupkRB0X/TnuhJ1T2ej2feOCCnawR9lqFJcvom5Mg09PUAXqC3TEb585qPm0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729077308; c=relaxed/simple; bh=S1yqy8Rerz6Navb8umTvjQcz44gY1prjsxSQoXtS/R4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Pvuqt/+OY/ASmmk0ClgmI03homV8fsBBD161rKw+MaJL5LjbKo4DUmnUjWxW1W3RN6NPNZU7Ov/zNZgwsd7MFu3pjkTXOSlKT6hZf64lXG6PgbRikE2lrLmgrqLNVXdHgL0yKBGyY2rZDZbg75oEldOPdXkQk+TdWohJIT+p+LY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HmR8ihbi; arc=none smtp.client-ip=198.175.65.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HmR8ihbi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729077307; x=1760613307; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=S1yqy8Rerz6Navb8umTvjQcz44gY1prjsxSQoXtS/R4=; b=HmR8ihbiRstVUoBeZHErN+7vGJUqFGaMemkXMiOtMlz/hJLtR23RWkAQ z5DjutdqXdmeAbn5HBLkUyoGNDzj9JyPfZkoF7M2dyPtrOpA6FluXcq6v l0YP8uDAh7XJwXlKgwL+1YMVS/ro9UQBYzf4uRyrNBwWmHBUDP+t3Hu8z 1cW6PFvUQj/61VvgoL3bw78WIK5G/pw7EgZ8RtXMqEIHS+OPgy8j4KK11 Mg4WQwcP9I6KewW1Al7WZF37DrotFtMClu0u93v11zqt4osbiWS0xn7wY vO2R2AK4InE4pfOrXOrbEt0og2zGpeqJFBndCbD9SLafK9gIZLlbxFkpT w==; X-CSE-ConnectionGUID: R2Pvzz4cSp+zr9mUK3CPPg== X-CSE-MsgGUID: PlvM/eJ+TM+ih8ZPYv+C1w== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="39066149" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="39066149" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2024 04:15:06 -0700 X-CSE-ConnectionGUID: wPSTL9YUR+G/WFX0N1JpsA== X-CSE-MsgGUID: U4Aom/kyRymfNYaI9qzXvg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="83005215" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa003.jf.intel.com with ESMTP; 16 Oct 2024 04:15:02 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 16FE51C4; Wed, 16 Oct 2024 14:15:00 +0300 (EEST) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , "Rafael J. Wysocki" , Andy Lutomirski , Peter Zijlstra , Baoquan He Cc: Ard Biesheuvel , Tom Lendacky , Andrew Morton , Thomas Zimmermann , Sean Christopherson , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, "Kirill A. Shutemov" , Kai Huang Subject: [PATCHv4, REBASED 4/4] x86/64/kexec: Rewrite init_transition_pgtable() with kernel_ident_mapping_init() Date: Wed, 16 Oct 2024 14:14:58 +0300 Message-ID: <20241016111458.846228-5-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241016111458.846228-1-kirill.shutemov@linux.intel.com> References: <20241016111458.846228-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" init_transition_pgtable() sets up transitional page tables. Rewrite it using kernel_ident_mapping_init() to avoid code duplication. Change struct kimage_arch to track allocated page tables as a list, not linking them to specific page table levels. Signed-off-by: Kirill A. Shutemov Reviewed-by: Tom Lendacky Reviewed-by: Kai Huang --- arch/x86/include/asm/kexec.h | 5 +- arch/x86/kernel/machine_kexec_64.c | 89 +++++++++++------------------- 2 files changed, 32 insertions(+), 62 deletions(-) diff --git a/arch/x86/include/asm/kexec.h b/arch/x86/include/asm/kexec.h index ae5482a2f0ca..7f9287f371e6 100644 --- a/arch/x86/include/asm/kexec.h +++ b/arch/x86/include/asm/kexec.h @@ -145,10 +145,7 @@ struct kimage_arch { }; #else struct kimage_arch { - p4d_t *p4d; - pud_t *pud; - pmd_t *pmd; - pte_t *pte; + struct list_head pages; }; #endif /* CONFIG_X86_32 */ =20 diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 645690e81c2d..fb350372835c 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -134,71 +134,42 @@ map_efi_systab(struct x86_mapping_info *info, pgd_t *= level4p) return 0; } =20 +static void *alloc_transition_pgt_page(void *data) +{ + struct kimage *image =3D (struct kimage *)data; + unsigned long virt; + + virt =3D get_zeroed_page(GFP_KERNEL); + if (!virt) + return NULL; + + list_add(&virt_to_page(virt)->lru, &image->arch.pages); + return (void *)virt; +} + static void free_transition_pgtable(struct kimage *image) { - free_page((unsigned long)image->arch.p4d); - image->arch.p4d =3D NULL; - free_page((unsigned long)image->arch.pud); - image->arch.pud =3D NULL; - free_page((unsigned long)image->arch.pmd); - image->arch.pmd =3D NULL; - free_page((unsigned long)image->arch.pte); - image->arch.pte =3D NULL; + struct page *page, *tmp; + + list_for_each_entry_safe(page, tmp, &image->arch.pages, lru) { + list_del(&page->lru); + free_page((unsigned long)page_address(page)); + } } =20 static int init_transition_pgtable(struct kimage *image, pgd_t *pgd) { - pgprot_t prot =3D PAGE_KERNEL_EXEC_NOENC; - unsigned long vaddr, paddr; - int result =3D -ENOMEM; - p4d_t *p4d; - pud_t *pud; - pmd_t *pmd; - pte_t *pte; + struct x86_mapping_info info =3D { + .alloc_pgt_page =3D alloc_transition_pgt_page, + .context =3D image, + .page_flag =3D __PAGE_KERNEL_LARGE_EXEC, + .kernpg_flag =3D _KERNPG_TABLE_NOENC, + .offset =3D __START_KERNEL_map - phys_base, + }; + unsigned long mstart =3D PAGE_ALIGN_DOWN(__pa(relocate_kernel)); + unsigned long mend =3D mstart + PAGE_SIZE; =20 - vaddr =3D (unsigned long)relocate_kernel; - paddr =3D __pa(relocate_kernel); - pgd +=3D pgd_index(vaddr); - if (!pgd_present(*pgd)) { - p4d =3D (p4d_t *)get_zeroed_page(GFP_KERNEL); - if (!p4d) - goto err; - image->arch.p4d =3D p4d; - set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE)); - } - p4d =3D p4d_offset(pgd, vaddr); - if (!p4d_present(*p4d)) { - pud =3D (pud_t *)get_zeroed_page(GFP_KERNEL); - if (!pud) - goto err; - image->arch.pud =3D pud; - set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE)); - } - pud =3D pud_offset(p4d, vaddr); - if (!pud_present(*pud)) { - pmd =3D (pmd_t *)get_zeroed_page(GFP_KERNEL); - if (!pmd) - goto err; - image->arch.pmd =3D pmd; - set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); - } - pmd =3D pmd_offset(pud, vaddr); - if (!pmd_present(*pmd)) { - pte =3D (pte_t *)get_zeroed_page(GFP_KERNEL); - if (!pte) - goto err; - image->arch.pte =3D pte; - set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE)); - } - pte =3D pte_offset_kernel(pmd, vaddr); - - if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) - prot =3D PAGE_KERNEL_EXEC; - - set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot)); - return 0; -err: - return result; + return kernel_ident_mapping_init(&info, pgd, mstart, mend); } =20 static void *alloc_pgt_page(void *data) @@ -299,6 +270,8 @@ int machine_kexec_prepare(struct kimage *image) unsigned long start_pgtable; int result; =20 + INIT_LIST_HEAD(&image->arch.pages); + /* Calculate the offsets */ start_pgtable =3D page_to_pfn(image->control_code_page) << PAGE_SHIFT; =20 --=20 2.45.2