Commit c8d99672 authored by Christoffer Dall's avatar Christoffer Dall Committed by Luis Henriques

arm/arm64: KVM: Fix and refactor unmap_range

commit 4f853a71 upstream.

unmap_range() was utterly broken, to quote Marc, and broke in all sorts
of situations.  It was also quite complicated to follow and didn't
follow the usual scheme of having a separate iterating function for each
level of page tables.

Address this by refactoring the code and introduce a pgd_clear()
function.
Reviewed-by: default avatarJungseok Lee <jays.lee@samsung.com>
Reviewed-by: default avatarMario Smarduch <m.smarduch@samsung.com>
Acked-by: default avatarMarc Zyngier <marc.zyngier@arm.com>
Signed-off-by: default avatarChristoffer Dall <christoffer.dall@linaro.org>
Signed-off-by: default avatarShannon Zhao <shannon.zhao@linaro.org>
Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
parent 45edcd45
...@@ -127,6 +127,18 @@ static inline void kvm_set_s2pmd_writable(pmd_t *pmd) ...@@ -127,6 +127,18 @@ static inline void kvm_set_s2pmd_writable(pmd_t *pmd)
(__boundary - 1 < (end) - 1)? __boundary: (end); \ (__boundary - 1 < (end) - 1)? __boundary: (end); \
}) })
static inline bool kvm_page_empty(void *ptr)
{
struct page *ptr_page = virt_to_page(ptr);
return page_count(ptr_page) == 1;
}
#define kvm_pte_table_empty(ptep) kvm_page_empty(ptep)
#define kvm_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
#define kvm_pud_table_empty(pudp) (0)
struct kvm; struct kvm;
#define kvm_flush_dcache_to_poc(a,l) __cpuc_flush_dcache_area((a), (l)) #define kvm_flush_dcache_to_poc(a,l) __cpuc_flush_dcache_area((a), (l))
......
...@@ -90,104 +90,115 @@ static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc) ...@@ -90,104 +90,115 @@ static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
return p; return p;
} }
static bool page_empty(void *ptr) static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
{ {
struct page *ptr_page = virt_to_page(ptr); pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
return page_count(ptr_page) == 1; pgd_clear(pgd);
kvm_tlb_flush_vmid_ipa(kvm, addr);
pud_free(NULL, pud_table);
put_page(virt_to_page(pgd));
} }
static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr) static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
{ {
if (pud_huge(*pud)) { pmd_t *pmd_table = pmd_offset(pud, 0);
pud_clear(pud); VM_BUG_ON(pud_huge(*pud));
kvm_tlb_flush_vmid_ipa(kvm, addr); pud_clear(pud);
} else { kvm_tlb_flush_vmid_ipa(kvm, addr);
pmd_t *pmd_table = pmd_offset(pud, 0); pmd_free(NULL, pmd_table);
pud_clear(pud);
kvm_tlb_flush_vmid_ipa(kvm, addr);
pmd_free(NULL, pmd_table);
}
put_page(virt_to_page(pud)); put_page(virt_to_page(pud));
} }
static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr) static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
{ {
if (kvm_pmd_huge(*pmd)) { pte_t *pte_table = pte_offset_kernel(pmd, 0);
pmd_clear(pmd); VM_BUG_ON(kvm_pmd_huge(*pmd));
kvm_tlb_flush_vmid_ipa(kvm, addr); pmd_clear(pmd);
} else { kvm_tlb_flush_vmid_ipa(kvm, addr);
pte_t *pte_table = pte_offset_kernel(pmd, 0); pte_free_kernel(NULL, pte_table);
pmd_clear(pmd);
kvm_tlb_flush_vmid_ipa(kvm, addr);
pte_free_kernel(NULL, pte_table);
}
put_page(virt_to_page(pmd)); put_page(virt_to_page(pmd));
} }
static void clear_pte_entry(struct kvm *kvm, pte_t *pte, phys_addr_t addr) static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
phys_addr_t addr, phys_addr_t end)
{ {
if (pte_present(*pte)) { phys_addr_t start_addr = addr;
kvm_set_pte(pte, __pte(0)); pte_t *pte, *start_pte;
put_page(virt_to_page(pte));
kvm_tlb_flush_vmid_ipa(kvm, addr); start_pte = pte = pte_offset_kernel(pmd, addr);
} do {
if (!pte_none(*pte)) {
kvm_set_pte(pte, __pte(0));
put_page(virt_to_page(pte));
kvm_tlb_flush_vmid_ipa(kvm, addr);
}
} while (pte++, addr += PAGE_SIZE, addr != end);
if (kvm_pte_table_empty(start_pte))
clear_pmd_entry(kvm, pmd, start_addr);
} }
static void unmap_range(struct kvm *kvm, pgd_t *pgdp, static void unmap_pmds(struct kvm *kvm, pud_t *pud,
unsigned long long start, u64 size) phys_addr_t addr, phys_addr_t end)
{ {
pgd_t *pgd; phys_addr_t next, start_addr = addr;
pud_t *pud; pmd_t *pmd, *start_pmd;
pmd_t *pmd;
pte_t *pte;
unsigned long long addr = start, end = start + size;
u64 next;
while (addr < end) { start_pmd = pmd = pmd_offset(pud, addr);
pgd = pgdp + pgd_index(addr); do {
pud = pud_offset(pgd, addr); next = kvm_pmd_addr_end(addr, end);
pte = NULL; if (!pmd_none(*pmd)) {
if (pud_none(*pud)) { if (kvm_pmd_huge(*pmd)) {
addr = kvm_pud_addr_end(addr, end); pmd_clear(pmd);
continue; kvm_tlb_flush_vmid_ipa(kvm, addr);
} put_page(virt_to_page(pmd));
} else {
if (pud_huge(*pud)) { unmap_ptes(kvm, pmd, addr, next);
/* }
* If we are dealing with a huge pud, just clear it and
* move on.
*/
clear_pud_entry(kvm, pud, addr);
addr = kvm_pud_addr_end(addr, end);
continue;
} }
} while (pmd++, addr = next, addr != end);
pmd = pmd_offset(pud, addr); if (kvm_pmd_table_empty(start_pmd))
if (pmd_none(*pmd)) { clear_pud_entry(kvm, pud, start_addr);
addr = kvm_pmd_addr_end(addr, end); }
continue;
}
if (!kvm_pmd_huge(*pmd)) { static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
pte = pte_offset_kernel(pmd, addr); phys_addr_t addr, phys_addr_t end)
clear_pte_entry(kvm, pte, addr); {
next = addr + PAGE_SIZE; phys_addr_t next, start_addr = addr;
} pud_t *pud, *start_pud;
/* start_pud = pud = pud_offset(pgd, addr);
* If the pmd entry is to be cleared, walk back up the ladder do {
*/ next = kvm_pud_addr_end(addr, end);
if (kvm_pmd_huge(*pmd) || (pte && page_empty(pte))) { if (!pud_none(*pud)) {
clear_pmd_entry(kvm, pmd, addr); if (pud_huge(*pud)) {
next = kvm_pmd_addr_end(addr, end); pud_clear(pud);
if (page_empty(pmd) && !page_empty(pud)) { kvm_tlb_flush_vmid_ipa(kvm, addr);
clear_pud_entry(kvm, pud, addr); put_page(virt_to_page(pud));
next = kvm_pud_addr_end(addr, end); } else {
unmap_pmds(kvm, pud, addr, next);
} }
} }
} while (pud++, addr = next, addr != end);
addr = next; if (kvm_pud_table_empty(start_pud))
} clear_pgd_entry(kvm, pgd, start_addr);
}
static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
phys_addr_t start, u64 size)
{
pgd_t *pgd;
phys_addr_t addr = start, end = start + size;
phys_addr_t next;
pgd = pgdp + pgd_index(addr);
do {
next = kvm_pgd_addr_end(addr, end);
unmap_puds(kvm, pgd, addr, next);
} while (pgd++, addr = next, addr != end);
} }
static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd, static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
......
...@@ -125,6 +125,21 @@ static inline void kvm_set_s2pmd_writable(pmd_t *pmd) ...@@ -125,6 +125,21 @@ static inline void kvm_set_s2pmd_writable(pmd_t *pmd)
#define kvm_pud_addr_end(addr, end) pud_addr_end(addr, end) #define kvm_pud_addr_end(addr, end) pud_addr_end(addr, end)
#define kvm_pmd_addr_end(addr, end) pmd_addr_end(addr, end) #define kvm_pmd_addr_end(addr, end) pmd_addr_end(addr, end)
static inline bool kvm_page_empty(void *ptr)
{
struct page *ptr_page = virt_to_page(ptr);
return page_count(ptr_page) == 1;
}
#define kvm_pte_table_empty(ptep) kvm_page_empty(ptep)
#ifndef CONFIG_ARM64_64K_PAGES
#define kvm_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
#else
#define kvm_pmd_table_empty(pmdp) (0)
#endif
#define kvm_pud_table_empty(pudp) (0)
struct kvm; struct kvm;
#define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l)) #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment