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

Oleksii Kurochko posted 1 patch 2 hours ago
xen/arch/riscv/paging.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] xen/riscv: don't decrement total_pages on failed paging_alloc_page()
Posted by Oleksii Kurochko 2 hours ago
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()")
Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
---
 xen/arch/riscv/paging.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/xen/arch/riscv/paging.c b/xen/arch/riscv/paging.c
index 35f572689a7c..6464c7ce68a6 100644
--- a/xen/arch/riscv/paging.c
+++ b/xen/arch/riscv/paging.c
@@ -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;
-- 
2.56.1
Re: [PATCH] xen/riscv: don't decrement total_pages on failed paging_alloc_page()
Posted by Baptiste Le Duc an hour ago
Reviewed-by: Baptiste Le Duc <baptiste.le-duc@vates.tech>

On 11/26/26 9:33 AM, Oleksii Kurochko wrote:
> 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()")
> Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
> ---
>   xen/arch/riscv/paging.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/xen/arch/riscv/paging.c b/xen/arch/riscv/paging.c
> index 35f572689a7c..6464c7ce68a6 100644
> --- a/xen/arch/riscv/paging.c
> +++ b/xen/arch/riscv/paging.c
> @@ -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;
Re: [PATCH] xen/riscv: don't decrement total_pages on failed paging_alloc_page()
Posted by Jan Beulich 2 hours ago
On 26.11.2026 09:31, Oleksii Kurochko wrote:
> 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()")
> Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>

Reviewed-by: Jan Beulich <jbeulich@suse.com>