From nobody Thu Dec 18 09:44:38 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1765904189; cv=none; d=zohomail.com; s=zohoarc; b=A771ZgZY1YkoQbyXy2IipMaS2yIDcokcgpxsouMy8jW3Ga3VBTYYcS1i4gQjMexph+VEYHIBgvYXA1GiK/epU8E/0YbkwNIBjZogBxujCuDXOPtaPpkFcIQISkzamJ22xHMWcZrK4moZPGSVhynQ/h9IPHOx6LYsyvuOztT04yo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1765904189; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cc3HXZPlPB/gccG/CezolOSjWVEDuZPosWEfUeOasnw=; b=ApCa4jxN1hsro3yqEMXVYIShCCEDyoOucBu7aO6rA7GBzOgyxyo4VgmflGYu6UPLs8+N0VC/OShQEmeMBfIkdpv4eWaPFqN8m9IjSZqHeHs8QkiCDJbEnGkSaq1ZcIeosMyRwMySb/VEGGRkD9+ZL7PIlk8S4bfoqhEcN6S1nao= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1765904189524397.4950817107474; Tue, 16 Dec 2025 08:56:29 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1188146.1509418 (Exim 4.92) (envelope-from ) id 1vVYL0-0007a3-Ju; Tue, 16 Dec 2025 16:55:50 +0000 Received: by outflank-mailman (output) from mailman id 1188146.1509418; Tue, 16 Dec 2025 16:55:50 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vVYL0-0007Zs-H6; Tue, 16 Dec 2025 16:55:50 +0000 Received: by outflank-mailman (input) for mailman id 1188146; Tue, 16 Dec 2025 16:55:48 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vVYKy-0006gk-HY for xen-devel@lists.xenproject.org; Tue, 16 Dec 2025 16:55:48 +0000 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [2a00:1450:4864:20::535]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 111b0e2f-daa0-11f0-9cce-f158ae23cfc8; Tue, 16 Dec 2025 17:55:46 +0100 (CET) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-6418738efa0so9619625a12.1 for ; Tue, 16 Dec 2025 08:55:46 -0800 (PST) Received: from fedora (user-109-243-71-38.play-internet.pl. [109.243.71.38]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b8003cb349csm63169766b.68.2025.12.16.08.55.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Dec 2025 08:55:45 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 111b0e2f-daa0-11f0-9cce-f158ae23cfc8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765904146; x=1766508946; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cc3HXZPlPB/gccG/CezolOSjWVEDuZPosWEfUeOasnw=; b=Sc9+AlFxvLfjOakec93bga3+XQh2/o0b/LqctjT9RrOcGxJuZ/0WoxQrezS05PZav6 XsZVhTa/K5rBHObZDqd47Pithn2tBBuLD56O3P5wG7b/lwBvJnbfefvrcfhgaDKZpnQp lCfDg6P8FI1551SkXevKsYf2qk877i9iQtOKL6qZGzgfsL8VaxuLXpmyMmyNcm9+mGYV 0vgR3FVdcjYKrVM93ruTyXKk5/wljB1LvUqXCrkzD10uyaUU4n/3Qarue2e7rDoiANBq r3Y8GcHMlturHJKQNWtWtDQzIhH0SfzrrzcsPmAy5BrrbSYSXk8nqxNDVS/TITtnT1Ue dJDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765904146; x=1766508946; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=cc3HXZPlPB/gccG/CezolOSjWVEDuZPosWEfUeOasnw=; b=t1hjl8EGcBepUr5LwrYzOi9UkCflwcrKgslVWXE3ybJvHFXt+oRGo4FqIBQ+i6Ckjr orFgTggURBbucG1B1JgGJGseGcO88YHifFw8l6Yrrz61zrufZLP+xmMpMAAgzBTNDhDJ vw7/UmCAJU7iGO7D3/PkbC8eq6LgIWB2226KZpeVc7u/TfM/VHzqo6lDQg1cz6q4Pcoa UJYh651uwM48Uv/pSnUPA1EZhGW1K5xHli2MRw3iw0nKgXXA0F+n6fHqRdnxoiJPs37J N86757MD/7Rwfvx9t/S7myziyKK3V/fULSIjKrqsC6iJ9B6wUg+UmEsRYglIw1MSJDeu Am4w== X-Gm-Message-State: AOJu0YyuYUjbiqB7Gc2IGC48DfhDRbhdsP/5+wghanFR2oBsX1RR2Tdm 0n1jEYDUUizRNLAKmjIMYn0s+IeAuwibEyRLRj8sarEo9w8FUAP8YoSaLAZgkQ== X-Gm-Gg: AY/fxX6cxfnF9N1Gx0PvA23hmKWPX4B8GPleMl3jiZ9PJrCGbDSp33oaLju6YVnuV91 9D4nw+9jZGgyiGwsN51EZfTbV6cJUtJ2LiByxvUTdBAvE/tpWVZNIi3gp73F/HY8ZUqS/+0D7j+ IrhJmVU9Jvq0Ldr3P1cU6uf7IukPh2+5cbMPpVonJFz6qC6viid/RUASVsJsaibIvGqxWCVA+8H brwKPqQt5hWv0hU8tayUjKqcYTtYMZw4e/J3dSoy/PZq0oF6XeJqC12LaThIBESojk94XDi9Giu 5CWuSKdl8XV85wIK7a3TijNGW4K+9qI+vh2y+QzlZE/3falZqOPPn1ATPwu/u6QRQQAtGAylGI4 ze6LMBQUhCn6ZQPHpe2iShoVWNE60ok9S+amJ4wbKVaBzndh0Ym/J+sUvES5pekodaf3K+fSjf6 DHKRB4FHtUJvayIs6jLOCObZPahL95k97Z2lgaABxWQ4nvEcl9EW1H0bk= X-Google-Smtp-Source: AGHT+IFD5kt79BKZYSLc7CDp2O5EY9oTWFy7gFhz9nNEv5BhIsuQ+5wibcyus2cZYZopIQKBHcdYqA== X-Received: by 2002:a17:907:3f88:b0:b73:1baa:6424 with SMTP id a640c23a62f3a-b7d23d002eamr1538155266b.55.1765904145704; Tue, 16 Dec 2025 08:55:45 -0800 (PST) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Oleksii Kurochko , Alistair Francis , Bob Eshleman , Connor Davis , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v7 04/19] xen/riscv: construct the P2M pages pool for guests Date: Tue, 16 Dec 2025 17:55:14 +0100 Message-ID: <684f846bb9b05a37b9650b2e1ccc3ba1b2cc9b10.1765879052.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1765904192053154100 Content-Type: text/plain; charset="utf-8" Implement p2m_set_allocation() to construct p2m pages pool for guests based on required number of pages. This is implemented by: - Adding a `struct paging_domain` which contains a freelist, a counter variable and a spinlock to `struct arch_domain` to indicate the free p2m pages and the number of p2m total pages in the p2m pages pool. - Adding a helper `p2m_set_allocation` to set the p2m pages pool size. This helper should be called before allocating memory for a guest and is called from domain_p2m_set_allocation(), the latter is a part of common dom0less code. - Adding implementation of paging_freelist_adjust() and paging_domain_init(). Signed-off-by: Oleksii Kurochko Acked-by: Jan Beulich --- Changes in V5-7: - Nothing changed. Only rebase. --- Changes in V4: - s/paging_freelist_init/paging_freelist_adjust. - Add empty line between definiton of paging_freelist_adjust() and paging_domain_init(). - Update commit message. - Add Acked-by: Jan Beulich . --- Changes in v3: - Drop usage of p2m_ prefix inside struct paging_domain(). - Introduce paging_domain_init() to init paging struct. --- Changes in v2: - Drop the comment above inclusion of in riscv/p2m.c. - Use ACCESS_ONCE() for lhs and rhs for the expressions in p2m_set_allocation(). --- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/Makefile | 1 - xen/arch/riscv/include/asm/domain.h | 12 ++++++ xen/arch/riscv/include/asm/paging.h | 13 ++++++ xen/arch/riscv/p2m.c | 18 ++++++++ xen/arch/riscv/paging.c | 65 +++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 xen/arch/riscv/include/asm/paging.h create mode 100644 xen/arch/riscv/paging.c diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile index c93c837aac..9dde693db4 100644 --- a/xen/arch/riscv/Makefile +++ b/xen/arch/riscv/Makefile @@ -7,6 +7,7 @@ obj-y +=3D intc.o obj-y +=3D irq.o obj-y +=3D mm.o obj-y +=3D p2m.o +obj-y +=3D paging.o obj-y +=3D pt.o obj-$(CONFIG_RISCV_64) +=3D riscv64/ obj-y +=3D sbi.o diff --git a/xen/arch/riscv/include/asm/Makefile b/xen/arch/riscv/include/a= sm/Makefile index bfdf186c68..3824f31c39 100644 --- a/xen/arch/riscv/include/asm/Makefile +++ b/xen/arch/riscv/include/asm/Makefile @@ -6,7 +6,6 @@ generic-y +=3D hardirq.h generic-y +=3D hypercall.h generic-y +=3D iocap.h generic-y +=3D irq-dt.h -generic-y +=3D paging.h generic-y +=3D percpu.h generic-y +=3D perfc_defn.h generic-y +=3D random.h diff --git a/xen/arch/riscv/include/asm/domain.h b/xen/arch/riscv/include/a= sm/domain.h index e688980efa..316e7c6c84 100644 --- a/xen/arch/riscv/include/asm/domain.h +++ b/xen/arch/riscv/include/asm/domain.h @@ -2,6 +2,8 @@ #ifndef ASM__RISCV__DOMAIN_H #define ASM__RISCV__DOMAIN_H =20 +#include +#include #include #include =20 @@ -24,11 +26,21 @@ struct arch_vcpu { struct vcpu_vmid vmid; }; =20 +struct paging_domain { + spinlock_t lock; + /* Free pages from the pre-allocated pool */ + struct page_list_head freelist; + /* Number of pages from the pre-allocated pool */ + unsigned long total_pages; +}; + struct arch_domain { struct hvm_domain hvm; =20 /* Virtual MMU */ struct p2m_domain p2m; + + struct paging_domain paging; }; =20 #include diff --git a/xen/arch/riscv/include/asm/paging.h b/xen/arch/riscv/include/a= sm/paging.h new file mode 100644 index 0000000000..98d8b06d45 --- /dev/null +++ b/xen/arch/riscv/include/asm/paging.h @@ -0,0 +1,13 @@ +#ifndef ASM_RISCV_PAGING_H +#define ASM_RISCV_PAGING_H + +#include + +struct domain; + +int paging_domain_init(struct domain *d); + +int paging_freelist_adjust(struct domain *d, unsigned long pages, + bool *preempted); + +#endif /* ASM_RISCV_PAGING_H */ diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index a6e2456f54..39c84f1a3b 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include =20 @@ -118,8 +119,25 @@ int p2m_init(struct domain *d) */ p2m->domain =3D d; =20 + paging_domain_init(d); + rwlock_init(&p2m->lock); INIT_PAGE_LIST_HEAD(&p2m->pages); =20 return 0; } + +/* + * Set the pool of pages to the required number of pages. + * Returns 0 for success, non-zero for failure. + * Call with d->arch.paging.lock held. + */ +int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preemp= ted) +{ + int rc; + + if ( (rc =3D paging_freelist_adjust(d, pages, preempted)) ) + return rc; + + return 0; +} diff --git a/xen/arch/riscv/paging.c b/xen/arch/riscv/paging.c new file mode 100644 index 0000000000..2df8de033b --- /dev/null +++ b/xen/arch/riscv/paging.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include + +int paging_freelist_adjust(struct domain *d, unsigned long pages, + bool *preempted) +{ + struct page_info *pg; + + ASSERT(spin_is_locked(&d->arch.paging.lock)); + + for ( ; ; ) + { + if ( d->arch.paging.total_pages < pages ) + { + /* Need to allocate more memory from domheap */ + pg =3D alloc_domheap_page(d, MEMF_no_owner); + if ( pg =3D=3D NULL ) + { + printk(XENLOG_ERR "Failed to allocate pages.\n"); + return -ENOMEM; + } + ACCESS_ONCE(d->arch.paging.total_pages)++; + page_list_add_tail(pg, &d->arch.paging.freelist); + } + else if ( d->arch.paging.total_pages > pages ) + { + /* Need to return memory to domheap */ + pg =3D page_list_remove_head(&d->arch.paging.freelist); + if ( pg ) + { + ACCESS_ONCE(d->arch.paging.total_pages)--; + free_domheap_page(pg); + } + else + { + printk(XENLOG_ERR + "Failed to free pages, freelist is empty.\n"); + return -ENOMEM; + } + } + else + break; + + /* Check to see if we need to yield and try again */ + if ( preempted && general_preempt_check() ) + { + *preempted =3D true; + return -ERESTART; + } + } + + return 0; +} + +/* Domain paging struct initialization. */ +int paging_domain_init(struct domain *d) +{ + spin_lock_init(&d->arch.paging.lock); + INIT_PAGE_LIST_HEAD(&d->arch.paging.freelist); + + return 0; +} --=20 2.52.0