From patchwork Mon Aug 19 07:08:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 820318 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 DE973155A59; Mon, 19 Aug 2024 07:08:37 +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=1724051319; cv=none; b=ek0Ke7Xf6MHjtAro5aVsIKB5I3ZFk568C8jd58+tVhEeWR499BP06oVTYaHry+p8benpIbv6OPopVjCP2u3gktl30X2k/lJ6IvDJiIcw3BqXCLz0Jtbnaz4YgpU1Y1QuZRtHXwCOC/dNtbd7XjEHAXcDOjxJWzAccil2Xqdwito= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724051319; c=relaxed/simple; bh=o8GaH3tn8yQHjvX1TbIRinqknzeRTVHuSafiEciVSrc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nWafnRWYJVCBdRS6HVzpR6np0CK3qqzPSisOb8tZOIznvjRJZM46565Yqhzt90N39O2f0qp7HNUYwgJNQ48VLMO2RIVjB0BwXQfL3rY+kk5mFhBH7JCyipA+4cz7Ic3UtVMk53SsS5zDkc98adbivx3Tppc+929uoFKFa6Knc0w= 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=RAPhoRaD; 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="RAPhoRaD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724051318; x=1755587318; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=o8GaH3tn8yQHjvX1TbIRinqknzeRTVHuSafiEciVSrc=; b=RAPhoRaDk/dM+YNGmjbRKg/oM8SblBVvqRilUZE9bpACwmgh90UImsxv 3n2Kv1KygUp9ITUhF8RKfz0PcgZ0t7i1JdwLj4czxC+6tKfZ3I99Gyfwx OaUbNZ+PfFzOYRThWVQv6u3dNg0zLC70lublC3wzDlgUMkMW+UYK8Pd2g QmD0XfJBh6QHJx9lPxst4IGSiZnpNIwnyKOJu0tAQtxNW1rklyOJCHmFi SqVie8M0PyRsDTOOK8gqojk7V26XK+UdwxWFo0agR/Amimq7QGUyVTUow s2T0r8tMKyIE+yu7zWzt7VhZew2Udqusov7JRhpyBfabeXiUrq1xPcd8p Q==; X-CSE-ConnectionGUID: 6oeQ3to2SWuYidpyFjwHXQ== X-CSE-MsgGUID: XD2Nv/pFS+CgjzQsC7Sfhg== X-IronPort-AV: E=McAfee;i="6700,10204,11168"; a="44804870" X-IronPort-AV: E=Sophos;i="6.10,158,1719903600"; d="scan'208";a="44804870" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2024 00:08:36 -0700 X-CSE-ConnectionGUID: 6z43RZPDSAmG0nk/NB0Uag== X-CSE-MsgGUID: TErgdoC5SSGjb2fepMwr/Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,158,1719903600"; d="scan'208";a="65113280" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa003.jf.intel.com with ESMTP; 19 Aug 2024 00:08:32 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 5300C252; Mon, 19 Aug 2024 10:08:30 +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: [PATCHv3 1/4] x86/mm/ident_map: Fix virtual address wrap to zero Date: Mon, 19 Aug 2024 10:08:24 +0300 Message-ID: <20240819070827.3620020-2-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240819070827.3620020-1-kirill.shutemov@linux.intel.com> References: <20240819070827.3620020-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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; - next = (addr & PUD_MASK) + PUD_SIZE; - if (next > end) - next = end; + next = pud_addr_end(addr, end); /* 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 *info, p4d_t *p4d_page, p4d_t *p4d = p4d_page + p4d_index(addr); pud_t *pud; - next = (addr & P4D_MASK) + P4D_SIZE; - if (next > end) - next = end; - + next = p4d_addr_end(addr, end); if (p4d_present(*p4d)) { pud = pud_offset(p4d, 0); result = 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 = pgd_page + pgd_index(addr); p4d_t *p4d; - next = (addr & PGDIR_MASK) + PGDIR_SIZE; - if (next > end) - next = end; - + next = pgd_addr_end(addr, end); if (pgd_present(*pgd)) { p4d = p4d_offset(pgd, 0); result = ident_p4d_init(info, p4d, addr, next); From patchwork Mon Aug 19 07:08:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 821022 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 062A6157476; Mon, 19 Aug 2024 07:08:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724051320; cv=none; b=fDweZbn+5L3lShy93VnwhwuDiX5sK8Y1men60jO4/8pHrCwNYQSvcuwy1P8UMgZaKLISOJXPU5v5fihkS68WgHGRx8eGDHnh6M2//QTXhyWER4GpUNwBPl93qESxjK8jU+sOTMqIpJkDlAp2wJspX8c2tzg85XammvJHfgTvvq0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724051320; c=relaxed/simple; bh=6ucCkpB0mFmRt1asl9hiphBlkV+T4SErqOKVfRYggY0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J82H8Jx4Tza2TGwjVciHl++0wzKFXyR09kMMe2rIpKVkrlSWuCRglYIWo+lrPs1OBi/W6jrIHDGhuZGaMd6vXcSRUOG+tOd+TjBiLyWyQ1PSpwl3t57Ez7MIKr8w+NhA4Q++eQZnQ+b3zPkpQfBLbmPYFCCBd98EyDztEGEjSYI= 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=I+QJErcZ; arc=none smtp.client-ip=192.198.163.10 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="I+QJErcZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724051319; x=1755587319; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6ucCkpB0mFmRt1asl9hiphBlkV+T4SErqOKVfRYggY0=; b=I+QJErcZG3rBesTK/PhhT++PmiMTNgfZMvzPZPhvQHLHgTsp6IL3aUkh AUwweiPEVsLJozCVBpuYPufdvYqZfSnaT2h7RlskeoSXCpWgSGyXq4ZAn 8yh5UaVKA3+rXNnqhvuXAlsORAoK5NNKDq9OFpgNF8QGgafs2wWPOWnnL EYFwHs79FSe2yBjuLHSzbWNFyQ2iLjy7zYNY/wkwDfjvFZK0ENMz1pogt sEZqrVSGpHB0Ez+jJtzUsCsVLKQ38TBaM8jlgthrWRxpPb3GPaG1OUdm2 p5JS/p7T4ARsJEHd2NOmfa6/+A2g4L3FUaKDEjO0z6wS6NzbliVOYSr5w g==; X-CSE-ConnectionGUID: 211MfnDiSZmuhUWWUwJY6Q== X-CSE-MsgGUID: 5jS4wi/1TmiLViopEr0ioA== X-IronPort-AV: E=McAfee;i="6700,10204,11168"; a="33697321" X-IronPort-AV: E=Sophos;i="6.10,158,1719903600"; d="scan'208";a="33697321" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2024 00:08:36 -0700 X-CSE-ConnectionGUID: /yl4IsXARw+bgf++1seU4Q== X-CSE-MsgGUID: /CIEJUubQg2w9xM+DvrIAg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,158,1719903600"; d="scan'208";a="60273955" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa009.jf.intel.com with ESMTP; 19 Aug 2024 00:08:32 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 641FC419; Mon, 19 Aug 2024 10:08:30 +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: [PATCHv3 2/4] x86/acpi: Replace manual page table initialization with kernel_ident_mapping_init() Date: Mon, 19 Aug 2024 10:08:25 +0300 Message-ID: <20240819070827.3620020-3-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240819070827.3620020-1-kirill.shutemov@linux.intel.com> References: <20240819070827.3620020-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- 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..78960b338be9 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); } -/* - * 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 has be present at the same spot in - * the virtual address space before and after switching page tables. - */ -static int __init init_transition_pgtable(pgd_t *pgd) -{ - pgprot_t prot = PAGE_KERNEL_EXEC_NOENC; - unsigned long vaddr, paddr; - p4d_t *p4d; - pud_t *pud; - pmd_t *pmd; - pte_t *pte; - - vaddr = (unsigned long)asm_acpi_mp_play_dead; - pgd += pgd_index(vaddr); - if (!pgd_present(*pgd)) { - p4d = (p4d_t *)alloc_pgt_page(NULL); - if (!p4d) - return -ENOMEM; - set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE)); - } - p4d = p4d_offset(pgd, vaddr); - if (!p4d_present(*p4d)) { - pud = (pud_t *)alloc_pgt_page(NULL); - if (!pud) - return -ENOMEM; - set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE)); - } - pud = pud_offset(p4d, vaddr); - if (!pud_present(*pud)) { - pmd = (pmd_t *)alloc_pgt_page(NULL); - if (!pmd) - return -ENOMEM; - set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); - } - pmd = pmd_offset(pud, vaddr); - if (!pmd_present(*pmd)) { - pte = (pte_t *)alloc_pgt_page(NULL); - if (!pte) - return -ENOMEM; - set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE)); - } - pte = pte_offset_kernel(pmd, vaddr); - - paddr = __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 = { @@ -130,6 +78,7 @@ static int __init acpi_mp_setup_reset(u64 reset_vector) .page_flag = __PAGE_KERNEL_LARGE_EXEC, .kernpg_flag = _KERNPG_TABLE_NOENC, }; + unsigned long mstart, mend; pgd_t *pgd; pgd = alloc_pgt_page(NULL); @@ -137,8 +86,6 @@ static int __init acpi_mp_setup_reset(u64 reset_vector) return -ENOMEM; for (int i = 0; i < nr_pfn_mapped; i++) { - unsigned long mstart, mend; - mstart = pfn_mapped[i].start << PAGE_SHIFT; mend = 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) } } - if (kernel_ident_mapping_init(&info, pgd, - PAGE_ALIGN_DOWN(reset_vector), - PAGE_ALIGN(reset_vector + 1))) { + mstart = PAGE_ALIGN_DOWN(reset_vector); + mend = mstart + PAGE_SIZE; + if (kernel_ident_mapping_init(&info, pgd, mstart, mend)) { kernel_ident_mapping_free(&info, pgd); return -ENOMEM; } - 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 has be present at the same spot in the virtual address space + * before and after switching page tables. + */ + info.offset = __START_KERNEL_map - phys_base; + mstart = PAGE_ALIGN_DOWN(__pa(asm_acpi_mp_play_dead)); + mend = mstart + PAGE_SIZE; + if (kernel_ident_mapping_init(&info, pgd, mstart, mend)) { kernel_ident_mapping_free(&info, pgd); return -ENOMEM; } From patchwork Mon Aug 19 07:08:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 820319 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 264541B960; Mon, 19 Aug 2024 07:08:35 +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=1724051317; cv=none; b=b85pO3aQOjlMe5iZAszqN7L2QxSE2NM1xLCg8cSmjQRBz9Xo4jWmfquxLn140EtRTnHSq+hV98fWUVGdyvDF3fW1yt9A/Dnu7JRKK0IggcdgqFsOkMIG0fGFzpCs5PRnIfZk2BRU2HiVI9XjM45JBgYwZ0bR61WDcQEBG3EKrmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724051317; c=relaxed/simple; bh=UWxjYETrwKYpnkeQWK7cdIq4YlS3gl5oLnqpqmIeIrc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SX51M94XyDyGw0ShACHMXQkSeeiWC8oFqpo2RxhhqGKk7rNLC5jkflRIhdeYc1DA2Z31oWoYk9mINOk4fYi6d9vBn2wKgQ4cVj9w4iCi5p3FPNtLGxNJk1tMdCIyQCkdi1dRgQmgf1qMP8JC++o5+sh3FVfrz3pvDsngkUM/Ck8= 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=j7uMBitx; 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="j7uMBitx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724051316; x=1755587316; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UWxjYETrwKYpnkeQWK7cdIq4YlS3gl5oLnqpqmIeIrc=; b=j7uMBitxb43s2OjlFqyjHkXMLxcs2SIuWS/0ci5A/z2Kd0PEvPcpKA5b ZTWnV1/3HOaJP2A8vVEIS1J+VKB1eLc3OoMujatxQeWjVeLJ1wGFwXdxi IRQEOj0XT6wyh9igTFnKKL6F5NnCzgs++Y2DxKNHqllmOkTH3AudWzvyP H1CUzI3RUx6TdXBl8QhF40yQC9i7X8uSWh1+M/UHiRqrEJ6Fda1SwfA4p /vZR4NoQp557+C0XrpW9hO9D+7FH4ccz+6WLtW1G7bKg5Q553h4KP8Vuj NBaTZe0qRoKnE9OAIV5aiT8oJ0vPtcIZKZVOxx1jhijbTLRQdykCGn34X Q==; X-CSE-ConnectionGUID: XRULnYE9QgKhBbwfxCcqXw== X-CSE-MsgGUID: SjyWQuPFT/mZLoOSq7A9hg== X-IronPort-AV: E=McAfee;i="6700,10204,11168"; a="44804862" X-IronPort-AV: E=Sophos;i="6.10,158,1719903600"; d="scan'208";a="44804862" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2024 00:08:36 -0700 X-CSE-ConnectionGUID: HsZlm58LQPykTvOHDHNebA== X-CSE-MsgGUID: dRm1BgzDRVK0PBZpSBNM6Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,158,1719903600"; d="scan'208";a="65113230" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa003.jf.intel.com with ESMTP; 19 Aug 2024 00:08:32 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 72DC6452; Mon, 19 Aug 2024 10:08:30 +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" Subject: [PATCHv3 3/4] x86/64/kexec: Map original relocate_kernel() in init_transition_pgtable() Date: Mon, 19 Aug 2024 10:08:26 +0300 Message-ID: <20240819070827.3620020-4-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240819070827.3620020-1-kirill.shutemov@linux.intel.com> References: <20240819070827.3620020-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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_kexec_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; vaddr = (unsigned long)relocate_kernel; - paddr = __pa(page_address(image->control_code_page)+PAGE_SIZE); + paddr = __pa(relocate_kernel); pgd += pgd_index(vaddr); if (!pgd_present(*pgd)) { p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL); From patchwork Mon Aug 19 07:08:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 820317 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 683E215747A; Mon, 19 Aug 2024 07:08:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724051321; cv=none; b=R5yns9BAzgca6Rcye9QFfIt6ne3k/FM23x69wcC7RzGzH2JexONZFJaU3LztKYPrnZ4eHEBuo45e+kKyG3RVv5tzE7EO/MoIl0exZFH4jHuqeNg1hl19A1ff+fiz3JAapXsSYIwz5aYOwnL8P6gWi+aXC5IAsMcV6x+i/xOfGnc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724051321; c=relaxed/simple; bh=4q/Sx14zLuag0d9ma9bwFcTZmoudhz7krAlHHOu9pgo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=m/U6kl2jzDHdbiLWQjxhEcv8auIwMaUxW9RtLnUPy5OIwE3WdCUT4hxL8qpxDDY7NyFy2IGvPUcedRISUtjfG6dN2U+5xMjOby7EoUPHTiAr2G8biJtblUOVaIk/TC/NtNWmkLRqhjJJ+uVyTDYNOn+F88qwjOl7qY4f+Z9FKls= 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=GrhsZMh0; arc=none smtp.client-ip=192.198.163.10 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="GrhsZMh0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724051319; x=1755587319; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4q/Sx14zLuag0d9ma9bwFcTZmoudhz7krAlHHOu9pgo=; b=GrhsZMh08TJC2dhE6sygoDO6z00HuAJIszVK0QD5DIU6FUj+AdWbJNKY 2i7lPJLcOtEdhcjbhA1aoVTJOJl8K7QbQ3OtFwvSSny66ifKTCQbrNucD Sijdxyb6pD8PrSDSArisrR9kaWShNonMm1OTUkU77p7uR3qciMiitYdpH k/Nd4WInt8H1nDYTwOTImOiNY4BBEMvGHSA1wQxSYGdIX+9PhZMKFx7la DNmv6dwZ854In1ZzTEzX/00idSk/vhC1Q8jNEO5XDLM6cnKGUcdhZfE9X NeHDvO+SZoP/egzsIXAxKt4wNDU9H18zBl0+U/lIrHFQ4b528M4/Ook5M g==; X-CSE-ConnectionGUID: x6zdrdpVTd2+o+Ih68VbgQ== X-CSE-MsgGUID: awuMRbNbQbWRiUPk/7PPnQ== X-IronPort-AV: E=McAfee;i="6700,10204,11168"; a="33697345" X-IronPort-AV: E=Sophos;i="6.10,158,1719903600"; d="scan'208";a="33697345" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2024 00:08:37 -0700 X-CSE-ConnectionGUID: zv32zb8GSYCwcWl0juadJQ== X-CSE-MsgGUID: XTf84VYnQ9yDbqmDcyW6lQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,158,1719903600"; d="scan'208";a="60273934" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa009.jf.intel.com with ESMTP; 19 Aug 2024 00:08:32 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 7E01C5F0; Mon, 19 Aug 2024 10:08:30 +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" Subject: [PATCHv3 4/4] x86/64/kexec: Rewrite init_transition_pgtable() with kernel_ident_mapping_init() Date: Mon, 19 Aug 2024 10:08:27 +0300 Message-ID: <20240819070827.3620020-5-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240819070827.3620020-1-kirill.shutemov@linux.intel.com> References: <20240819070827.3620020-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 */ diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_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; } +static void *alloc_transition_pgt_page(void *data) +{ + struct kimage *image = (struct kimage *)data; + unsigned long virt; + + virt = 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 = NULL; - free_page((unsigned long)image->arch.pud); - image->arch.pud = NULL; - free_page((unsigned long)image->arch.pmd); - image->arch.pmd = NULL; - free_page((unsigned long)image->arch.pte); - image->arch.pte = 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)); + } } static int init_transition_pgtable(struct kimage *image, pgd_t *pgd) { - pgprot_t prot = PAGE_KERNEL_EXEC_NOENC; - unsigned long vaddr, paddr; - int result = -ENOMEM; - p4d_t *p4d; - pud_t *pud; - pmd_t *pmd; - pte_t *pte; + struct x86_mapping_info info = { + .alloc_pgt_page = alloc_transition_pgt_page, + .context = image, + .page_flag = __PAGE_KERNEL_LARGE_EXEC, + .kernpg_flag = _KERNPG_TABLE_NOENC, + .offset = __START_KERNEL_map - phys_base, + }; + unsigned long mstart = PAGE_ALIGN_DOWN(__pa(relocate_kernel)); + unsigned long mend = mstart + PAGE_SIZE; - vaddr = (unsigned long)relocate_kernel; - paddr = __pa(relocate_kernel); - pgd += pgd_index(vaddr); - if (!pgd_present(*pgd)) { - p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL); - if (!p4d) - goto err; - image->arch.p4d = p4d; - set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE)); - } - p4d = p4d_offset(pgd, vaddr); - if (!p4d_present(*p4d)) { - pud = (pud_t *)get_zeroed_page(GFP_KERNEL); - if (!pud) - goto err; - image->arch.pud = pud; - set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE)); - } - pud = pud_offset(p4d, vaddr); - if (!pud_present(*pud)) { - pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL); - if (!pmd) - goto err; - image->arch.pmd = pmd; - set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); - } - pmd = pmd_offset(pud, vaddr); - if (!pmd_present(*pmd)) { - pte = (pte_t *)get_zeroed_page(GFP_KERNEL); - if (!pte) - goto err; - image->arch.pte = pte; - set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE)); - } - pte = pte_offset_kernel(pmd, vaddr); - - if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) - prot = 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); } static void *alloc_pgt_page(void *data) @@ -299,6 +270,8 @@ int machine_kexec_prepare(struct kimage *image) unsigned long start_pgtable; int result; + INIT_LIST_HEAD(&image->arch.pages); + /* Calculate the offsets */ start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;