From nobody Thu Oct 30 23:04:55 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=1758146833; cv=none; d=zohomail.com; s=zohoarc; b=BXKPmD6u0AotTeLUYEvypSWlLRc0zu4QldHy8ea/Bjz9EGhTxougBWBGjQbM9QYRD5hw1fepktADY6CAGsqkAZR3hXBkiFVS3O+vhOOD5aspwp++L29/lwg9lroda3ZrcOlnFiLRyky6Te+rPlXKzJYU/pumH4GuSrPqPRi1F5c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1758146833; 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=ZWW/RPoHDDMmiNcnSl6hhO3V0ICFKFmoC7Uis76f4UI=; b=VJNhO0wwdmYZ5Tc5cUEVvGzFEBfd1040xNLnubZckrmh4YWbFhz1uwFMl4qSUb2FJBkWwCJrGGSj0LOkiz1/gj5GLrvdr4WrWyX9yUW9oo5j4Jgy8e7sHjgaaxkG6g4nPY8ujVFWEZp5Djmbtr0wR1NRp/KC3jEkLRnhqrMJWn4= 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 1758146833738674.3337201030268; Wed, 17 Sep 2025 15:07:13 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1125769.1467649 (Exim 4.92) (envelope-from ) id 1uz0Ik-0000xi-Tm; Wed, 17 Sep 2025 22:06:58 +0000 Received: by outflank-mailman (output) from mailman id 1125769.1467649; Wed, 17 Sep 2025 22:06:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uz0Ik-0000wx-Mj; Wed, 17 Sep 2025 22:06:58 +0000 Received: by outflank-mailman (input) for mailman id 1125769; Wed, 17 Sep 2025 22:06:57 +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 1uz08M-0007Lu-IJ for xen-devel@lists.xenproject.org; Wed, 17 Sep 2025 21:56:14 +0000 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [2607:f8b0:4864:20::62e]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 202fd343-9411-11f0-9d13-b5c5bf9af7f9; Wed, 17 Sep 2025 23:56:13 +0200 (CEST) Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2570bf6058aso3814545ad.0 for ; Wed, 17 Sep 2025 14:56:13 -0700 (PDT) Received: from fedora ([149.199.65.200]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-26980053da4sm5538095ad.20.2025.09.17.14.56.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Sep 2025 14:56:11 -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: 202fd343-9411-11f0-9d13-b5c5bf9af7f9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758146171; x=1758750971; 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=ZWW/RPoHDDMmiNcnSl6hhO3V0ICFKFmoC7Uis76f4UI=; b=ABuzYF087xnXF8aZvIWexr/DQX5IOScDoJupi1bN1CQQsMRVlORkhZ+8j07nGgw7St t9O2aU1iv0pE/TWIxCUfKZ2r1NEioOjgbpRqHvWSZ7If/oemnnK9wRMsCwltsTKHyFA+ 0O8dN48vOQmf/d/DOwMhjXytuzkna3us94eCnGSkn21CYKYFquRzNyHDzI0s0d/7PBQD c5UoRS2hyIzrBQxUth32WXhpgL7pHUtH/xynr+byzqfm2H/leD7rhCoAX3x+oLbmy1+O tAtoXFntkbB61LKxgV3tgeHbK17pzMn3T1UFsVIogGBCgB8v76zUjvVTVAPPXNJuMTO/ F5nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758146171; x=1758750971; 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=ZWW/RPoHDDMmiNcnSl6hhO3V0ICFKFmoC7Uis76f4UI=; b=kTX8kD1lU9Ef0/C1xyrd+nclDq1NKnIwd/3SWC3iF0zFHzqVjqwX0bMvEIDjftqCz5 nTU6pFxRY1wLfM9M8XivHWJif3EEAcZuOPKmJmf+7fUtmwQyT5mOk+ZkM+143EPjWOSM d+SrmxFs+QsprBL4Vzu2OmCxRrlsI3SHYeX+NkffttOp7h3ORXiUgyJtly065jodsjYf 9FfvTK8S1sgevX2+Td029T2JBYZ9nqZhhWJKc7fXls2hyQfEWCJkhcJXXtUeti+A3KL6 ugFC+dHMwW/P/djLFUQusbHiy0na4e72QZiDOOTxDAVWQZCGKeyJLp6i5D+O78tT7hhS Rf+g== X-Gm-Message-State: AOJu0Yy62NTW19Js9w6T2/oYvofIO2pPU47DH5tSNrSnAZvU90iXDRKZ 2FAexhKD68V+d5i42nv+J6GQsM4ROFoR68jNZONLAtuaFvEjbCKd6EjZYuEk6zLYIvA= X-Gm-Gg: ASbGnctz8AeWI10+GvUwpstiP5U9PIDlHpKBV9fjzPzh6u/64+hTZMltQb6ycaa+rEw NmkavaTSrUd+sd2Ug8xKMExc2NJr6R62wfOY0tHIOfxXb4qNfE3W+r/9BAnRLOKAd18spcUfmPI iD9Et81C8nHBTT6zAhvEhrVJ/Ikp0TEWzkctRGX7tOUbxknlKCkLZyCZ86zgBKPbLvtieBi0SX5 +JAMgu8EJ4fvXatO6wcSzY1GahjVdA3dRAEW/2V2nUrO+Fam/huvTkxsLSRTo3WMZ88xXLBFtRV /WJaMM72WrGHgZ7Q1g6TIBOQzCbYjb+jLKRSHkbi1mD96BvXMMcKo3GkLPdCvLAakw2r/LAEWZD aaNvI9OkOgPK5kvLGiEIF0dCdytOPsZAsx89u11sb4qqv X-Google-Smtp-Source: AGHT+IFNulg9ua/1KcKiq1d9eq2CTw7cQ9UG4yEDrbmVuEwCKwmA/vPRU34oqpsarcTO/vxdiRpRjw== X-Received: by 2002:a17:903:11c8:b0:25d:5b09:a201 with SMTP id d9443c01a7336-2681238073amr49800165ad.27.1758146171396; Wed, 17 Sep 2025 14:56:11 -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 v4 16/18] xen/riscv: implement mfn_valid() and page reference, ownership handling helpers Date: Wed, 17 Sep 2025 23:55:36 +0200 Message-ID: <09317ebbd1f6fb7dda9454aa7e0b1ba3cbd0726c.1758145428.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1758146836346116600 Content-Type: text/plain; charset="utf-8" Implement the mfn_valid() macro to verify whether a given MFN is valid by checking that it falls within the range [start_page, max_page). These bounds are initialized based on the start and end addresses of RAM. As part of this patch, start_page is introduced and initialized with the PFN of the first RAM page. Also, initialize pdx_group_valid() by calling set_pdx_range() when memory banks are being mapped. Also, after providing a non-stub implementation of the mfn_valid() macro, the following compilation errors started to occur: riscv64-linux-gnu-ld: prelink.o: in function `alloc_heap_pages': /build/xen/common/page_alloc.c:1054: undefined reference to `page_is_offl= inable' riscv64-linux-gnu-ld: /build/xen/common/page_alloc.c:1035: undefined refe= rence to `page_is_offlinable' riscv64-linux-gnu-ld: prelink.o: in function `reserve_offlined_page': /build/xen/common/page_alloc.c:1151: undefined reference to `page_is_offl= inable' riscv64-linux-gnu-ld: ./.xen-syms.0: hidden symbol `page_is_offlinable' i= sn't defined riscv64-linux-gnu-ld: final link failed: bad value make[2]: *** [arch/riscv/Makefile:28: xen-syms] Error 1 To resolve these errors, the following functions have also been introduced, based on their Arm counterparts: - page_get_owner_and_reference() and its variant to safely acquire a reference to a page and retrieve its owner. - Implement page_is_offlinable() to return false for RISC-V. Signed-off-by: Oleksii Kurochko Acked-by: Jan Beulich --- Changes in V4: - Rebase the patch on top of patch series "[PATCH v2 0/2] constrain page_i= s_ram_type() to x86". - Add implementation of page_is_offlinable() instead of page_is_ram(). - Update the commit message. --- Changes in V3: - Update defintion of mfn_valid(). - Use __ro_after_init for variable start_page. - Drop ASSERT_UNREACHABLE() in page_get_owner_and_nr_reference(). - Update the comment inside do/while in page_get_owner_and_nr_reference(). - Define _PGC_static and drop "#ifdef CONFIG_STATIC_MEMORY" in put_page_nr= (). - Initialize pdx_group_valid() by calling set_pdx_range() when memory bank= s are mapped. - Drop page_get_owner_and_nr_reference() and implement page_get_owner_and_= reference() without reusing of a page_get_owner_and_nr_reference() to avoid potentia= l dead code. - Move defintion of get_page() to "xen/riscv: add support of page lookup b= y GFN", where it is really used. --- Changes in V2: - New patch. --- xen/arch/riscv/include/asm/mm.h | 9 +++++++-- xen/arch/riscv/mm.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index 0503c92e6c..1b16809749 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -5,6 +5,7 @@ =20 #include #include +#include #include #include #include @@ -300,8 +301,12 @@ static inline bool arch_mfns_in_directmap(unsigned lon= g mfn, unsigned long nr) #define page_get_owner(p) (p)->v.inuse.domain #define page_set_owner(p, d) ((p)->v.inuse.domain =3D (d)) =20 -/* TODO: implement */ -#define mfn_valid(mfn) ({ (void)(mfn); 0; }) +extern unsigned long start_page; + +#define mfn_valid(mfn) ({ \ + unsigned long tmp_mfn =3D mfn_x(mfn); \ + likely((tmp_mfn >=3D start_page)) && likely(__mfn_valid(tmp_mfn)); \ +}) =20 #define domain_set_alloc_bitsize(d) ((void)(d)) #define domain_clamp_alloc_bitsize(d, b) ((void)(d), (b)) diff --git a/xen/arch/riscv/mm.c b/xen/arch/riscv/mm.c index 3cac16f1b7..8c6e8075f3 100644 --- a/xen/arch/riscv/mm.c +++ b/xen/arch/riscv/mm.c @@ -521,6 +521,8 @@ static void __init setup_directmap_mappings(unsigned lo= ng base_mfn, #error setup_{directmap,frametable}_mapping() should be implemented for RV= _32 #endif =20 +unsigned long __ro_after_init start_page; + /* * Setup memory management * @@ -570,9 +572,13 @@ void __init setup_mm(void) ram_end =3D max(ram_end, bank_end); =20 setup_directmap_mappings(PFN_DOWN(bank_start), PFN_DOWN(bank_size)= ); + + set_pdx_range(paddr_to_pfn(bank_start), paddr_to_pfn(bank_end)); } =20 setup_frametable_mappings(ram_start, ram_end); + + start_page =3D PFN_DOWN(ram_start); max_page =3D PFN_DOWN(ram_end); } =20 @@ -642,3 +648,30 @@ void put_page(struct page_info *page) free_domheap_page(page); } } + +bool page_is_offlinable(mfn_t mfn) +{ + return false; +} + +struct domain *page_get_owner_and_reference(struct page_info *page) +{ + unsigned long x, y =3D page->count_info; + struct domain *owner; + + do { + x =3D y; + /* + * Count =3D=3D 0: Page is not allocated, so we cannot take a ref= erence. + * Count =3D=3D -1: Reference count would wrap, which is invalid. + */ + if ( unlikely(((x + 1) & PGC_count_mask) <=3D 1) ) + return NULL; + } + while ( (y =3D cmpxchg(&page->count_info, x, x + 1)) !=3D x ); + + owner =3D page_get_owner(page); + ASSERT(owner); + + return owner; +} --=20 2.51.0