From nobody Mon Feb 9 10:33:15 2026 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; 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; arc=pass (i=1 dmarc=pass fromdomain=citrix.com); dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=2; a=rsa-sha256; t=1767808947; cv=pass; d=zohomail.com; s=zohoarc; b=FLZvJn2wkg8bm/bjfXXP4vt0PNZ33+JJ7FH2zrityPCfUZc5XFq2iUvxdQZ/ZVCRurgyOWpog1wQbY8arwG7bO9f1/ZRxzPlKAexaszag258hQ8ovloz2svrSW8jBJE50QfDO5wqtl0RrKRFNhwbMhDQF4b1hl2SPn+2P0bU42c= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1767808947; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=YHsCGsnpSWYt16lVqmzOOp06AU35khhTb5siHQs4e94=; b=lMadwArfkzisgjlsQYOs4ocyhzqQ815pScMb0B0gm9zJLGaa2dfwHf6IM75HJhkoQScPwXZu/90D7W05KtR3z2lyorV/SRNHc1WesK6fLLII9rweEMoKatLrGJpNYVE8IxAFl0qtbbxSVnucwbFJvhyzQtU/F3YfpnNsmdRGXbA= ARC-Authentication-Results: i=2; 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; arc=pass (i=1 dmarc=pass fromdomain=citrix.com); dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1767808947733980.5708667349065; Wed, 7 Jan 2026 10:02:27 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1197104.1514757 (Exim 4.92) (envelope-from ) id 1vdXrE-00041y-Te; Wed, 07 Jan 2026 18:02:08 +0000 Received: by outflank-mailman (output) from mailman id 1197104.1514757; Wed, 07 Jan 2026 18:02:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vdXrE-00041r-R2; Wed, 07 Jan 2026 18:02:08 +0000 Received: by outflank-mailman (input) for mailman id 1197104; Wed, 07 Jan 2026 18:02:07 +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 1vdXrD-00041l-Pe for xen-devel@lists.xenproject.org; Wed, 07 Jan 2026 18:02:07 +0000 Received: from CH5PR02CU005.outbound.protection.outlook.com (mail-northcentralusazlp170120005.outbound.protection.outlook.com [2a01:111:f403:c105::5]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id f74701aa-ebf2-11f0-9ccf-f158ae23cfc8; Wed, 07 Jan 2026 19:02:02 +0100 (CET) Received: from CH7PR03MB7860.namprd03.prod.outlook.com (2603:10b6:610:24e::14) by LV5PR03MB8411.namprd03.prod.outlook.com (2603:10b6:408:360::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9499.2; Wed, 7 Jan 2026 18:01:59 +0000 Received: from CH7PR03MB7860.namprd03.prod.outlook.com ([fe80::f5ba:35df:1c9f:b343]) by CH7PR03MB7860.namprd03.prod.outlook.com ([fe80::f5ba:35df:1c9f:b343%4]) with mapi id 15.20.9499.002; Wed, 7 Jan 2026 18:01:59 +0000 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: f74701aa-ebf2-11f0-9ccf-f158ae23cfc8 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vSdaQrPPas3r4DmDVsYDPbpWld91majtERyOWjFJuLHvCdDSPQ1mtSj7rNJsiEUnBqdnXbOl5HmFdBi1jUBGUZL4LQOftsTwfT8ahFfnp5pQdxopVIFObXwRKyaN+SEj1uLkJgH+9T/xq7msPhfK5YJ+bMuBKUBEpDCspHXGPBZmJV6mRSAVi9kZGs8V7/b4hy+SsSNkECt3qcFxa0h8f81XjltRultuV7flN02r+wsMhFSvsFKORpEnuWsuK089zL0/IQ3H33bluBpgFXZTtwlcpnbhT/kWo+YJlZtynISm0OJD6joBdz4j3m5rnCLIuE02EB+qf9mjn8Cbvda7yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=YHsCGsnpSWYt16lVqmzOOp06AU35khhTb5siHQs4e94=; b=pdJ/N5UGyG2zu4XebiJouXqJC4iWzyOusF0R80rXAwOEAH9ylqomDP3gULks+W4wC7p4dbMK4zqKk1pp3evs+saFK7oQX3HeaWYi4fgEDrtGRAZ/gqHaTizCasRJulZ2jSlfU7xm82vBL42QsQztSNclcAX8uVO2DymAuo98ho0BbX5OWV5cE5vjQ28eKsNjYocyAfImcVZ+RAycVvS1VYrB9Ff8SuXvCVFGA2MREJyGV8SF2uPSU5liCWsmYoEEpTj85tz3UEQFH+M3AabF+Pzu2fVUNwaojrLQxCXayd9ZyWh+mBrZeWOQWA8Ygk/Zw9BkW9cRbRAY2FLbjaSfiA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=YHsCGsnpSWYt16lVqmzOOp06AU35khhTb5siHQs4e94=; b=Xe03Lo6IV5WjF6E3pvQfBB/pAGmX/0hxfUaSQa+i6TKFDHFjYqDirdvq32aaVIbytryGsLz5uJkHVHPfdu74kAuEB1dlFlRmR24sX+6UPTdt0bcOJGeluCPl0y1fJ22sN3orYvlbj/JyOeo3pvLsDwTnJgVrGnDFDAUE7dWV6hA= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com; From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , Stefano Stabellini Subject: [PATCH v3] xen/mm: move adjustment of claimed pages counters on allocation Date: Wed, 7 Jan 2026 18:56:05 +0100 Message-ID: <20260107175605.56617-1-roger.pau@citrix.com> X-Mailer: git-send-email 2.51.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: PR1P264CA0046.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2cb::17) To CH7PR03MB7860.namprd03.prod.outlook.com (2603:10b6:610:24e::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH7PR03MB7860:EE_|LV5PR03MB8411:EE_ X-MS-Office365-Filtering-Correlation-Id: ef52875f-0bbe-4490-f82a-08de4e16da15 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?utf-8?B?WWNOZWtTaEhEL3BxTWNwRG5LbHNuOHRvTVJmTXZ4SW13N3U1cHl3eTNXTkIx?= =?utf-8?B?L3JUMzR0S21WNVJwajRodjlZamxuQ2tnaG1yaWloLzBmR2Q0OE9TM25uNGJ1?= =?utf-8?B?aWVWa3QrMUpVWHBlcWJHSDY1Ulc5d3NwUU82eTh5MSszQlUzZ0U2N3BDMjMr?= =?utf-8?B?aVIrUkErTks0SHRGR1RuMTBrMDMxd3AyVFBCRXBReVp6UCsybTNEWGxKaFd3?= =?utf-8?B?TkE1c2NpU1ZiK0dsMFpBZDEyNURUclBGT2hhZk9FcjdzbUYzcXJwY1V6ejFr?= =?utf-8?B?dHpBWW5hRnA5MHJ4TlYwcTN6SWJsSlhOT1JpSkU5WlR3cUhqdGV6UG43SDIw?= =?utf-8?B?TUQ2U1BZMDFjZ2dsUjBWeVNPemFidE5rSTN2N0FjNHY5M1F1ZUVwb3lzUFZS?= =?utf-8?B?WWtXT1pFYmdTTkFmWURQR1pOTGxXWml0MDRPaWlpWVBPb3gxdUJzcCtCdlpP?= =?utf-8?B?NVdnSFplNThBbHRyaDNPNmg0RXdES0NBZW5Sb2liVjcrUUJSNlRFdisvelVy?= =?utf-8?B?QWkxUldydDVWN2d1MVZvcHovc3NmN3NkVW1iY2NGTzJ1Q2E5cldMOHVaN0Rp?= =?utf-8?B?dkJtZ01EZVBLR3gxbi80RGo1SXl4L003ZmVZdExyYzUzdXIzckdGdmFOSWhH?= =?utf-8?B?R2lBQjhIK1FXd1JRbjVuTitMZGc5WHl6N3g2TEdSVmVMckkrYnpTdXVnUXVi?= =?utf-8?B?cTdiMVNEaTNZcS9nV3F2TTBwVHVOZjhsNU00SHRSd0cxSE8rZ3lNSlRkSHNE?=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ndBdU05U2V4aU42Z2pJ?= =?utf-8?B?N3ZTQUtOWnVLd1k5ek5mOVNxczdxLzN2ZmNMSXZ5a1l2Nm5YTTdsdVVsYTNw?= =?utf-8?B?enRCeDZNYm9VVzd0UnRLYmc2WkQ4UmdqUThqTXNhUmZqL2pJeHZLM0xzZEZ5?= =?utf-8?B?cDlSMEdLb3lDejVkcjluN0Y5SWJjVWk0VzF2NjRPaDY4Z3kzc1pVVG1Qc0hm?= =?utf-8?B?UDM0b3p5dlpoNVBxZ0xhL3dGbTJ6Y2NXMlExWXRxQXZGNGNOcW5yRkRocU1q?= =?utf-8?Q?TEOADmeDieeFi1KGG5YTH83FX5QuzNc0?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH7PR03MB7860.namprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?OTBQK1Q1M00wWW02VWlFaE9vYmR2dStQbEc3RGpZQ05IVlZCR1dTUE5oT29u?= =?utf-8?B?S09uSWRlVDJHNXVTRlppR0dRTm9XNFRJbXQ0Z21QTU56ZGtWV08rRmpLTUI4?= =?utf-8?B?UzhrMWx4VEp0bXRlRFJYMW1MdDNrWllpc2FtclprRW5QVWFoK1M2dEpUWDda?= =?utf-8?B?SFhvOHNxZGtqUGpHbHdjZzV2TGV6d1NxMjdlbGwrL3RIU0t5QWhoT0hQRjZU?= =?utf-8?B?THRqWkFUaGY0YlAvVWVkZDZ6VVY0UlBNSlBMdmhVdUY1TldUOENzaVZCMktW?= =?utf-8?B?dzhIeWlZL0tzVTdsVERjd0Y2YnpPVEhyTWtPbTJtbW01WUFCcTNZaW1YdU5P?= =?utf-8?B?Y01sUUQ1U1c5clhmMXVPYWVTZ3BtcjdLa2tqcWtrSHBEcHpNSG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2ZtZG52MGptRHJxSXhMU01VWmJGUTZZZzZ1ZTVoc1IyQzlBSlNQcWIw?= =?utf-8?B?NkwrT0FtQVpkZWI2NnV3NlBHZGcxTS9CaFVVb0FaYTI4cGUvYTB4c1ovc0ow?= =?utf-8?B?T1JnUWw5V1lDcWxPK0dhdmJqQ2FHTnZraDVpOHltRFFVaTgxN0ZtUT09?= X-OriginatorOrg: citrix.com X-MS-Exchange-CrossTenant-Network-Message-Id: ef52875f-0bbe-4490-f82a-08de4e16da15 X-MS-Exchange-CrossTenant-AuthSource: CH7PR03MB7860.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jan 2026 18:01:59.0222 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 335836de-42ef-43a2-b145-348c2ee9ca5b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: aizouMNBN+qSS0YOimmBlCd0PorY1dDp/+Fxr1bRp/RVbWefa3Hm9nPP/LVEKhfj+DtxXQH9zs1bGU/H6GGS3Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV5PR03MB8411 X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1767808949349158500 The current logic splits the update of the amount of available memory in the system (total_avail_pages) and pending claims into two separately locked regions. This leads to a window between counters adjustments where the result of total_avail_pages - outstanding_claims doesn't reflect the real amount of free memory available, and can return a negative value due to total_avail_pages having been updated ahead of outstanding_claims. Fix by adjusting outstanding_claims and d->outstanding_pages in the same place where total_avail_pages is updated. Note that accesses to d->outstanding_pages is protected by the global heap_lock, just like total_avail_pages or outstanding_claims. Add a comment to the field declaration, and also adjust the comment at the top of domain_set_outstanding_pages() to be clearer in that regard. Note that failures in assign_pages() causes the claimed amount that has been allocated to be lost, as the amount is not added back to the domain quota once pages are freed. Given the intended usage of claims is limited to initial physmap populate, and the current failure paths in assign_pages() would lead to the domain being destroyed anyway, don't add extra logic to recover the claimed amount on failure - it's just adding complexity for no real benefit. Fixes: 65c9792df600 ("mmu: Introduce XENMEM_claim_pages (subop of memory op= s)") Signed-off-by: Roger Pau Monn=C3=A9 Reviewed-by: Jan Beulich --- Changes since v2: - Revert back to the approach in v1. - Add extra comments and justification in commit message. Changes since v1: - Regain the claim if allocated page cannot be assigned to the domain. - Adjust comments regarding d->outstanding_pages being protected by the heap_lock (instead of the d->page_alloc_lock). --- xen/common/page_alloc.c | 59 ++++++++++++++++++++++------------------- xen/include/xen/sched.h | 3 ++- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 1f67b88a8933..49ca70334458 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -515,30 +515,6 @@ unsigned long domain_adjust_tot_pages(struct domain *d= , long pages) ASSERT(rspin_is_locked(&d->page_alloc_lock)); d->tot_pages +=3D pages; =20 - /* - * can test d->outstanding_pages race-free because it can only change - * if d->page_alloc_lock and heap_lock are both held, see also - * domain_set_outstanding_pages below - */ - if ( !d->outstanding_pages || pages <=3D 0 ) - goto out; - - spin_lock(&heap_lock); - BUG_ON(outstanding_claims < d->outstanding_pages); - if ( d->outstanding_pages < pages ) - { - /* `pages` exceeds the domain's outstanding count. Zero it out. */ - outstanding_claims -=3D d->outstanding_pages; - d->outstanding_pages =3D 0; - } - else - { - outstanding_claims -=3D pages; - d->outstanding_pages -=3D pages; - } - spin_unlock(&heap_lock); - -out: return d->tot_pages; } =20 @@ -548,9 +524,10 @@ int domain_set_outstanding_pages(struct domain *d, uns= igned long pages) unsigned long claim, avail_pages; =20 /* - * take the domain's page_alloc_lock, else all d->tot_page adjustments - * must always take the global heap_lock rather than only in the much - * rarer case that d->outstanding_pages is non-zero + * Two locks are needed here: + * - d->page_alloc_lock: protects accesses to d->{tot,max,extra}_page= s. + * - heap_lock: protects accesses to d->outstanding_pages, total_avai= l_pages + * and outstanding_claims. */ nrspin_lock(&d->page_alloc_lock); spin_lock(&heap_lock); @@ -999,7 +976,7 @@ static struct page_info *alloc_heap_pages( { nodeid_t node; unsigned int i, buddy_order, zone, first_dirty; - unsigned long request =3D 1UL << order; + unsigned int request =3D 1UL << order; struct page_info *pg; bool need_tlbflush =3D false; uint32_t tlbflush_timestamp =3D 0; @@ -1008,6 +985,8 @@ static struct page_info *alloc_heap_pages( =20 /* Make sure there are enough bits in memflags for nodeID. */ BUILD_BUG_ON((_MEMF_bits - _MEMF_node) < (8 * sizeof(nodeid_t))); + /* Make sure max order doesn't overflow the local storage type. */ + BUILD_BUG_ON(MAX_ORDER >=3D sizeof(request) * 8); =20 ASSERT(zone_lo <=3D zone_hi); ASSERT(zone_hi < NR_ZONES); @@ -1071,6 +1050,30 @@ static struct page_info *alloc_heap_pages( total_avail_pages -=3D request; ASSERT(total_avail_pages >=3D 0); =20 + if ( d && d->outstanding_pages && !(memflags & MEMF_no_refcount) ) + { + /* + * Adjust claims in the same locked region where total_avail_pages= is + * adjusted, not doing so would lead to a window where the amount = of + * free memory (avail - claimed) would be incorrect. + * + * Note that by adjusting the claimed amount here it's possible for + * pages to fail to be assigned to the claiming domain while alrea= dy + * having been subtracted from d->outstanding_pages. Such claimed + * amount is then lost, as the pages that fail to be assigned to t= he + * domain are freed without replenishing the claim. This is fine = given + * claims are only to be used during physmap population as part of + * domain build, and any failure in assign_pages() there will resu= lt in + * the domain being destroyed before creation is finished. Losing= part + * of the claim makes no difference. + */ + unsigned int outstanding =3D min(d->outstanding_pages, request); + + outstanding_claims -=3D outstanding; + BUG_ON(outstanding > d->outstanding_pages); + d->outstanding_pages -=3D outstanding; + } + check_low_mem_virq(); =20 if ( d !=3D NULL ) diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 1f77e0869b5d..d922c908c29f 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -413,7 +413,8 @@ struct domain unsigned int tot_pages; =20 unsigned int xenheap_pages; /* pages allocated from Xen heap */ - unsigned int outstanding_pages; /* pages claimed but not possessed= */ + /* Pages claimed but not possessed, protected by global heap_lock. */ + unsigned int outstanding_pages; unsigned int max_pages; /* maximum value for domain_tot_pa= ges() */ unsigned int extra_pages; /* pages not included in domain_to= t_pages() */ =20 --=20 2.51.0