From nobody Fri Oct 31 09:40:07 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=1749560763; cv=none; d=zohomail.com; s=zohoarc; b=nED5rnlVqjEp5DRC5Hdc8bZSHYype5SdXxvMZzKrBH36pScrall9OK4PTP8KN4lTBzVIcUq2nXD7yZChFxlJ7EadLg2b9WLOz79A+6v4q/m65NRKCiaNq7TLhc2Fl36N8RXeRQJbSa6NOUHq6qMaRSedX3lagaY0OCCYnwLsQMs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560763; 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=O5cGIp6lzcilrw8ShbCVkPARsAjo5L5Ne3xaR6XcYW4=; b=QxHzNdPFPpFqptswnjEXipUysJAp9uDZt4u6tlKoNkzVyRs0Q+G+CF4v9akPkl0fboOKEEgYn6HI2q27ZaUHZ5Iba/JGYHn/lNksDEGULRddI1jZreXbcq2e6Po9KKl3AA+rgykydffT2msxQl89aOWLXJpBgmtqFg2CuVJ+g8I= 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 1749560763848269.86394744757854; Tue, 10 Jun 2025 06:06:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010786.1389013 (Exim 4.92) (envelope-from ) id 1uOyfj-0005XC-4y; Tue, 10 Jun 2025 13:05:47 +0000 Received: by outflank-mailman (output) from mailman id 1010786.1389013; Tue, 10 Jun 2025 13:05:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfi-0005WI-TX; Tue, 10 Jun 2025 13:05:46 +0000 Received: by outflank-mailman (input) for mailman id 1010786; Tue, 10 Jun 2025 13:05:45 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfh-0004iH-BP for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:45 +0000 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [2a00:1450:4864:20::631]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9e90be84-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:44 +0200 (CEST) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-acb5ec407b1so978046866b.1 for ; Tue, 10 Jun 2025 06:05:44 -0700 (PDT) Received: from fedora.. (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05:43 -0700 (PDT) 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: 9e90be84-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560744; x=1750165544; 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=O5cGIp6lzcilrw8ShbCVkPARsAjo5L5Ne3xaR6XcYW4=; b=OFSzIbgKWbtgOUiRHZyf64fFHOHxgcwdnXvLJDaxPuTXYnXkALBtg3zB2pXbuDoQx5 rB5ImVzsiIDRnvOpWDKvTsz1pLHEvl+2/cyvsCouopsFceEj+721fiYC5bS68D7Ybq8a yIPRKUqDMu9LeQJPSSqWMVAzKKnRuTcQwSH/lVKqyEiXloJtK1OsBq35fKx8WZvyrhFs IcqOHV0GlsTSjUcgFNuoyE543dUMV33rdCyD32kV+qDILJovc+EyCyYyDuEDBvi4Tnmw fiNG+zApYCWoUj2sQVYz6FFA5Hg2OJ7vdsSNKaYl443WpcS/p80Px2iZdolnHS54K6Rs 62HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560744; x=1750165544; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O5cGIp6lzcilrw8ShbCVkPARsAjo5L5Ne3xaR6XcYW4=; b=DIp35M450VG8W/4wo7osPNSBsvmcUhoAWOw9Hpn07/LsTxKUycgk92I6IqRc3gOuGQ pHzthnAijKh1Y43e1Hj2/xUfGkZCpFR9rgGIc27F4NhLIYoJFuJQ0KZx3tGuiqPFdkiz VlT4+3D92/Mw5Hl7Fzd9cW131pbr7Bm5lxcPuHgVNT75qmFRSxPs1bbWQQUokK0t6Yn2 dK11n7/qOUB2ErD/IUZZLD+KqmDEqwKq9W5kcxg1uxonwAzyl38XbGz9Gaq6EEGCA8Se 3x5nEW4IURQQrO4hR45Q8hkhrwVyGxrRAXtQj4SIIbQho3L2DLA+R+Z/okc8yMUV3PGj hICg== X-Gm-Message-State: AOJu0YymSJtSei0x/6HSXvUS63wv0fqHj+JtKYWa2UgMSeo6rqnNeLgG 9RQWKX3WltPgRgfFbRiBwQRNSFdGlnlcfmgyzNkNc0fuB9W/rml8KbqXFLmlWw== X-Gm-Gg: ASbGncvA6S5nKOGDGZEEOlodGT5qF45yqlcLCdPllb1Lfoqx1d989lvMNuAOMhqQG6N aYmQzPY2CpUXOVE3Nvt4vvD5NYTO3WLaSNr+dRFJMGox0qSIiYVnTx0St+4gmyKeqbxzgXqwlQp HYGYEu8Hsw8OgRcyiMIZ75LzxREWDEHEOywDY/Ko3dVdrB+B/Pc2Ef9q/7Usl2dnLT2nc0oakx3 T+Cz0vxOn8Gn6N7uUGfgv2F3u96wq+p2fAO1miZ37Zu5+mW8ImARsq9Cp9m8QER2GbHG4UVUSoz l3R7oqvIRUF8MQ5FshipndgLRMyAp5sZTHU24mHTRkq9dPagMUIGKIHA69jnAF8cMP/nYawb4pC kSqWHM9WCBhth+Uog7JtSPfhR1j97 X-Google-Smtp-Source: AGHT+IG+GuksJhWm+r89SaPyXR4MgjeFB20puLjU92lWbLXtuoPxVX1Hd0Ryeej0kxstXLeuped14g== X-Received: by 2002:a17:907:c22:b0:ad8:87a0:62aa with SMTP id a640c23a62f3a-ade1aab90f1mr1740126066b.27.1749560743862; Tue, 10 Jun 2025 06:05:43 -0700 (PDT) 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 v2 04/17] xen/riscv: construct the P2M pages pool for guests Date: Tue, 10 Jun 2025 15:05:19 +0200 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560788624124100 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. Signed-off-by: Oleksii Kurochko --- 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/include/asm/domain.h | 12 ++++++ xen/arch/riscv/p2m.c | 59 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/xen/arch/riscv/include/asm/domain.h b/xen/arch/riscv/include/a= sm/domain.h index b9a03e91c5..b818127f9f 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 @@ -18,10 +20,20 @@ struct arch_vcpu_io { struct arch_vcpu { }; =20 +struct paging_domain { + spinlock_t lock; + /* Free P2M pages from the pre-allocated P2M pool */ + struct page_list_head p2m_freelist; + /* Number of pages from the pre-allocated P2M pool */ + unsigned long p2m_total_pages; +}; + struct arch_domain { struct hvm_domain hvm; =20 struct p2m_domain p2m; + + struct paging_domain paging; }; =20 #include diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 9f7fd8290a..f33c7147ff 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -105,6 +106,9 @@ int p2m_init(struct domain *d) struct p2m_domain *p2m =3D p2m_get_hostp2m(d); int rc; =20 + spin_lock_init(&d->arch.paging.lock); + INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist); + p2m->vmid =3D INVALID_VMID; =20 rc =3D p2m_alloc_vmid(d); @@ -113,3 +117,58 @@ int p2m_init(struct domain *d) =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) +{ + struct page_info *pg; + + ASSERT(spin_is_locked(&d->arch.paging.lock)); + + for ( ; ; ) + { + if ( d->arch.paging.p2m_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 P2M pages.\n"); + return -ENOMEM; + } + ACCESS_ONCE(d->arch.paging.p2m_total_pages)++; + page_list_add_tail(pg, &d->arch.paging.p2m_freelist); + } + else if ( d->arch.paging.p2m_total_pages > pages ) + { + /* Need to return memory to domheap */ + pg =3D page_list_remove_head(&d->arch.paging.p2m_freelist); + if( pg ) + { + ACCESS_ONCE(d->arch.paging.p2m_total_pages)--; + free_domheap_page(pg); + } + else + { + printk(XENLOG_ERR + "Failed to free P2M pages, P2M 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; +} --=20 2.49.0