Skip to content

Commit d88e7b3

Browse files
kirylbp3tk0v
authored andcommitted
x86/mm: Introduce kernel_ident_mapping_free()
The helper complements kernel_ident_mapping_init(): it frees the identity mapping that was previously allocated. It will be used in the error path to free a partially allocated mapping or if the mapping is no longer needed. The caller provides a struct x86_mapping_info with the free_pgd_page() callback hooked up and the pgd_t to free. Signed-off-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Acked-by: Kai Huang <[email protected]> Tested-by: Tao Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 26ba735 commit d88e7b3

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

arch/x86/include/asm/init.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
struct x86_mapping_info {
88
void *(*alloc_pgt_page)(void *); /* allocate buf for page table */
9+
void (*free_pgt_page)(void *, void *); /* free buf for page table */
910
void *context; /* context for alloc_pgt_page */
1011
unsigned long page_flag; /* page flag for PMD or PUD entry */
1112
unsigned long offset; /* ident mapping offset */
@@ -16,4 +17,6 @@ struct x86_mapping_info {
1617
int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page,
1718
unsigned long pstart, unsigned long pend);
1819

20+
void kernel_ident_mapping_free(struct x86_mapping_info *info, pgd_t *pgd);
21+
1922
#endif /* _ASM_X86_INIT_H */

arch/x86/mm/ident_map.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,79 @@
44
* included by both the compressed kernel and the regular kernel.
55
*/
66

7+
static void free_pte(struct x86_mapping_info *info, pmd_t *pmd)
8+
{
9+
pte_t *pte = pte_offset_kernel(pmd, 0);
10+
11+
info->free_pgt_page(pte, info->context);
12+
}
13+
14+
static void free_pmd(struct x86_mapping_info *info, pud_t *pud)
15+
{
16+
pmd_t *pmd = pmd_offset(pud, 0);
17+
int i;
18+
19+
for (i = 0; i < PTRS_PER_PMD; i++) {
20+
if (!pmd_present(pmd[i]))
21+
continue;
22+
23+
if (pmd_leaf(pmd[i]))
24+
continue;
25+
26+
free_pte(info, &pmd[i]);
27+
}
28+
29+
info->free_pgt_page(pmd, info->context);
30+
}
31+
32+
static void free_pud(struct x86_mapping_info *info, p4d_t *p4d)
33+
{
34+
pud_t *pud = pud_offset(p4d, 0);
35+
int i;
36+
37+
for (i = 0; i < PTRS_PER_PUD; i++) {
38+
if (!pud_present(pud[i]))
39+
continue;
40+
41+
if (pud_leaf(pud[i]))
42+
continue;
43+
44+
free_pmd(info, &pud[i]);
45+
}
46+
47+
info->free_pgt_page(pud, info->context);
48+
}
49+
50+
static void free_p4d(struct x86_mapping_info *info, pgd_t *pgd)
51+
{
52+
p4d_t *p4d = p4d_offset(pgd, 0);
53+
int i;
54+
55+
for (i = 0; i < PTRS_PER_P4D; i++) {
56+
if (!p4d_present(p4d[i]))
57+
continue;
58+
59+
free_pud(info, &p4d[i]);
60+
}
61+
62+
if (pgtable_l5_enabled())
63+
info->free_pgt_page(p4d, info->context);
64+
}
65+
66+
void kernel_ident_mapping_free(struct x86_mapping_info *info, pgd_t *pgd)
67+
{
68+
int i;
69+
70+
for (i = 0; i < PTRS_PER_PGD; i++) {
71+
if (!pgd_present(pgd[i]))
72+
continue;
73+
74+
free_p4d(info, &pgd[i]);
75+
}
76+
77+
info->free_pgt_page(pgd, info->context);
78+
}
79+
780
static void ident_pmd_init(struct x86_mapping_info *info, pmd_t *pmd_page,
881
unsigned long addr, unsigned long end)
982
{

0 commit comments

Comments
 (0)