Skip to contentSkip to conversation

xen/riscv: don't decrement total_pages on failed paging_alloc_page()

PATCH In review Oleksii Kurochko 3 participants 3 messages Nov 26 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/riscv: don't decrement total_pages on failed paging_alloc_page()

paging_alloc_page() unconditionally decrements d->arch.paging.total_pages, even when the freelist is empty and page_list_remove_head() returns NULL. In that case the counter underflows, so arch_get_paging_mempool_size() reports a bogus pool size and subsequent paging_freelist_adjust() calls operate on a wrong page count.

Only decrement the counter when a page was actually taken from the freelist.

Fixes af87a609c79d ("xen/riscv: implement p2m_next_level()")
---
 xen/arch/riscv/paging.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/riscv/paging.c +2 -1
index 35f572689a7c..6464c7ce68a6 100644
@@ -120,7 +120,8 @@ struct page_info *paging_alloc_page(struct domain *d)
 
     spin_lock(&d->arch.paging.lock);
     pg = page_list_remove_head(&d->arch.paging.freelist);
-    d->arch.paging.total_pages--;
+    if ( pg )
+        d->arch.paging.total_pages--;
     spin_unlock(&d->arch.paging.lock);
 
     return pg;
Signature · 1 line
2.56.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Oleksii Kurochko

paging_alloc_page() unconditionally decrements d->arch.paging.total_pages, even when the freelist is empty and page_list_remove_head() returns NULL. In that case the counter underflows, so arch_get_paging_mempool_size() reports a bogus pool size and subsequent paging_freelist_adjust() calls operate on a wrong page count.

Only decrement the counter when a page was actually taken from the freelist.

Fixes af87a609c79d ("xen/riscv: implement p2m_next_level()")
Reviewed-by on v1 1/1
#2 lore raw
In reply to Oleksii Kurochko
Quoted 29 lines · Oleksii Kurochko

paging_alloc_page() unconditionally decrements d->arch.paging.total_pages, even when the freelist is empty and page_list_remove_head() returns NULL. In that case the counter underflows, so arch_get_paging_mempool_size() reports a bogus pool size and subsequent paging_freelist_adjust() calls operate on a wrong page count.

Only decrement the counter when a page was actually taken from the freelist.

Fixes af87a609c79d ("xen/riscv: implement p2m_next_level()")
---
  xen/arch/riscv/paging.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/riscv/paging.c +2 -1
index 35f572689a7c..6464c7ce68a6 100644
@@ -120,7 +120,8 @@ struct page_info *paging_alloc_page(struct domain *d)
 
      spin_lock(&d->arch.paging.lock);
      pg = page_list_remove_head(&d->arch.paging.freelist);
-    d->arch.paging.total_pages--;
+    if ( pg )
+        d->arch.paging.total_pages--;
      spin_unlock(&d->arch.paging.lock);
 
      return pg;