From nobody Thu Oct 30 23:21:34 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1752145410; cv=none; d=zohomail.com; s=zohoarc; b=FJZlTTRcJwwyZ02hO8T8cIfIPtGgeTNUv/xTjDuOdVYvN269zKOZ5ur3OU5D8fiYzlqgHB8oSnoxDLHOACGkBFs7MikVP6IetZLIOCvniTf6GUWzWZd86eHRyliifVSsQ3B7aKfjR1LT5LB/MPO6bZQN8d9vh2T6+O7PjPv5jQY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1752145410; 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=W6r0Bex0UStGaZB6/IW4HglFMAoiQOhLuQjg8gMplp0=; b=HpZUPuGOD9XSY4DwTwoRFkMOo3o14E/6CLPbZAseDSJeC8h1jvWwXX9KYa6ya+/+xlHRAPMMlaP+zX96L8hDMqRy3hco18Akucys781Kp1ey5WR0IEW6mKAMpIjs+dZIvKgvgO4GsHpvrZQbRyV7OOa0K0hR7AE4kr0vQfvNO4k= 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1752145410006136.696005149705; Thu, 10 Jul 2025 04:03:30 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1039345.1411195 (Exim 4.92) (envelope-from ) id 1uZp3W-0002DV-Us; Thu, 10 Jul 2025 11:03:10 +0000 Received: by outflank-mailman (output) from mailman id 1039345.1411195; Thu, 10 Jul 2025 11:03:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uZp3W-0002DO-S1; Thu, 10 Jul 2025 11:03:10 +0000 Received: by outflank-mailman (input) for mailman id 1039345; Thu, 10 Jul 2025 11:03:09 +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 1uZp3V-0002DI-FS for xen-devel@lists.xenproject.org; Thu, 10 Jul 2025 11:03:09 +0000 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [2a00:1450:4864:20::336]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 70a5e84e-5d7d-11f0-b894-0df219b8e170; Thu, 10 Jul 2025 13:02:59 +0200 (CEST) Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-453647147c6so7593575e9.2 for ; Thu, 10 Jul 2025 04:02:59 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-23de42849d7sm17419475ad.21.2025.07.10.04.02.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Jul 2025 04:02:57 -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: 70a5e84e-5d7d-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1752145378; x=1752750178; darn=lists.xenproject.org; h=content-transfer-encoding:autocrypt:content-language:cc:to:subject :from:user-agent:mime-version:date:message-id:from:to:cc:subject :date:message-id:reply-to; bh=W6r0Bex0UStGaZB6/IW4HglFMAoiQOhLuQjg8gMplp0=; b=NiBdcjBnHvrv6qztvNDQAdVWCjWwKvk2KxVrgooicU0ZGgfp+qsOvJdBrqOuylxLjl A0BvkVh6ge5WfHCSVRyupzwlmg8BdD+rENXwp6nTIxRezmWaQebqDHwv6T8R7JwV2rDX 18ESCvObTzztOpdRyfCXWYeXVtyZtuMfhCD42fKtHx7q8VRExheOUIRkZsRHD1jEXy7B P3C++knQYdmSY2uPGbAtfOjaXh9ek9i6vngfSPnbbrYv/DnXtkE6cOrio6ffs4eAH5qL hsZDFRrg30GwObpN8rZQwmY1YVi3LgwYDd4inNv5fqY6F15bwFyUNXq6PtZkAmJaJ878 4MpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752145378; x=1752750178; h=content-transfer-encoding:autocrypt:content-language:cc:to:subject :from:user-agent:mime-version:date:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=W6r0Bex0UStGaZB6/IW4HglFMAoiQOhLuQjg8gMplp0=; b=OOAbuANz101uszi/zw58BfvBLGU+2qaNapYAFsdvEJsJkxS6UaqiIfCZ91ddRmLJvH H4N9cXPMWebWnU/eDpRBod+6dcO7rdZRf3Ol6guz52q4ERWV5kG+z1ZU8jeYRPQ06e4t bzlMP8LM1Dtx0bbRlYlPvkVXzfGZMOC1B4nIELcUfm0Pre4TiNtZS7cbVm6Ji8skEzhn hgFVYfyPeqXU7Z8ne3VBFSrHovr3eoqMgAyJx6M/UbLC7FdisvJgBx+5HuJRlGtNcEXr BkXqksgLYSolIFE36GHYQztBCklyQfcUdgkjqqJSnFxoaVphqbL9QoMtOgY4AuY+egmx vHZw== X-Gm-Message-State: AOJu0YwQ1nAOzH2EwboV0C6nEARp/bgrlwfJx3nBPsy+QhnJSIINvV7A tmLCTpf3dQT5AbRcs6DWT3n5Toxwuju0dhY5G03M3MNga5VBIUsGS4CQlyNbTPJ5gAiRBR7en+D hGwY= X-Gm-Gg: ASbGncsNclPROIc5aqwrfpmMpxBr4Q7/WTvU7kBwHk4d69kelsuJliCZVpBYYWCW/yk 43Ufp4h2GRlOiF1LkpRi6sEcEvOXrxO9gIPbXDnvXvXVWlJh3NreD12+4R1ZQ35xMfLOWvVlbNK yyuLz6f2w8v9U3EhjNrmlNG7YlHoV2yI5ySNgcDTUEp2iZPRGuHpIjG+9Eds0uYEgvYAljC8DOJ kvznJ1Zga/Didxo74fkU4HtMYo2Fmz/jY9gltxcM1/6DdLY1NXpjc9rvsEwvZmK/9ZaT1QTDpV1 4GyYfuhBdIsxhryWz79ZNxLkfNSWJ/AWvL3+q2Xdg5v2eozd0sI4pHmOCp/j6B9JitEYu3/sZhn JjsQAjbCxe8dT9KZS4tGlkYWrb5x/9y2gSa5sZrPTK6S+kItzuvAZsG3DpQ== X-Google-Smtp-Source: AGHT+IGJ/fglKlpIAil8MM7eHZGPbQlR1Z7JHbQcT/bsBtov3NcEpy28fVDvySdjap6B9+VB+cHJZA== X-Received: by 2002:a05:6000:25c4:b0:3a5:52d4:5b39 with SMTP id ffacd0b85a97d-3b5e44da884mr5552176f8f.8.1752145378395; Thu, 10 Jul 2025 04:02:58 -0700 (PDT) Message-ID: Date: Thu, 10 Jul 2025 13:02:49 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Jan Beulich Subject: [PATCH v2] xvmalloc: extend to cover 2-dimensional arrays To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Julien Grall , Stefano Stabellini , Anthony PERARD , Michal Orzel , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= Content-Language: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wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1752145413016116600 Content-Type: text/plain; charset="utf-8" Take care of the multiplication(s) involved in determining overall size in the macros themselves, saturating to ULONG_MAX. This way on 64-bit systems the subsequent check against UINT_MAX will fail, while on 32- bit systems allocations of this size simply cannot be fulfilled anyway (such an allocation would consume the entire address space). The only place where we truly consume guest input (but constrained to hwdom) is cpufreq_statistic_init(). Play safe however and convert the other three instances where a multiplication is involved as well. While touching those sites also switch to xv*alloc_array(), following what was settled upon when those were introduced. Don't bother extending x*alloc_array() the same way. Reported-by: Andrew Cooper Signed-off-by: Jan Beulich Reviewed-by: Julien Grall --- To my surprise code gen even improves a tiny bit for do_memory_op(), in surrounding code (with gcc14). --- v2: Drop (unused) logic for 3- and 4-dim arrays. Re-base. --- a/xen/arch/x86/cpu/mcheck/mctelem.c +++ b/xen/arch/x86/cpu/mcheck/mctelem.c @@ -20,6 +20,7 @@ #include #include #include +#include =20 #include #include @@ -340,7 +341,7 @@ void __init mctelem_init(unsigned int da =20 if ((mctctl.mctc_elems =3D xmalloc_array(struct mctelem_ent, MC_NENT)) =3D=3D NULL || - (datarr =3D xmalloc_bytes(MC_NENT * datasz)) =3D=3D NULL) { + (datarr =3D xvmalloc_array(char, MC_NENT, datasz)) =3D=3D NULL) { xfree(mctctl.mctc_elems); printk("Allocations for MCA telemetry failed\n"); return; --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -26,6 +26,8 @@ #include #include #include +#include + #include #include #include @@ -160,7 +162,7 @@ void vnuma_destroy(struct vnuma_info *vn { xfree(vnuma->vmemrange); xfree(vnuma->vcpu_to_vnode); - xfree(vnuma->vdistance); + xvfree(vnuma->vdistance); xfree(vnuma->vnode_to_pnode); xfree(vnuma); } @@ -197,7 +199,7 @@ static struct vnuma_info *vnuma_alloc(un if ( !vnuma ) return ERR_PTR(-ENOMEM); =20 - vnuma->vdistance =3D xmalloc_array(unsigned int, nr_vnodes * nr_vnodes= ); + vnuma->vdistance =3D xvmalloc_array(unsigned int, nr_vnodes, nr_vnodes= ); vnuma->vcpu_to_vnode =3D xmalloc_array(unsigned int, nr_vcpus); vnuma->vnode_to_pnode =3D xmalloc_array(nodeid_t, nr_vnodes); vnuma->vmemrange =3D xmalloc_array(xen_vmemrange_t, nr_ranges); --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -26,6 +26,8 @@ #include #include #include +#include + #include #include #include @@ -1750,7 +1752,7 @@ long do_memory_op(unsigned long cmd, XEN =20 read_unlock(&d->vnuma_rwlock); =20 - tmp.vdistance =3D xmalloc_array(unsigned int, dom_vnodes * dom_vno= des); + tmp.vdistance =3D xvmalloc_array(unsigned int, dom_vnodes, dom_vno= des); tmp.vmemrange =3D xmalloc_array(xen_vmemrange_t, dom_vranges); tmp.vcpu_to_vnode =3D xmalloc_array(unsigned int, dom_vcpus); =20 @@ -1813,7 +1815,7 @@ long do_memory_op(unsigned long cmd, XEN vnumainfo_out: rcu_unlock_domain(d); =20 - xfree(tmp.vdistance); + xvfree(tmp.vdistance); xfree(tmp.vmemrange); xfree(tmp.vcpu_to_vnode); break; --- a/xen/drivers/acpi/pmstat.c +++ b/xen/drivers/acpi/pmstat.c @@ -36,6 +36,7 @@ #include #include #include +#include =20 #include #include @@ -125,7 +126,7 @@ int cpufreq_statistic_init(unsigned int return -ENOMEM; } =20 - pxpt->u.trans_pt =3D xzalloc_array(uint64_t, count * count); + pxpt->u.trans_pt =3D xvzalloc_array(uint64_t, count, count); if ( !pxpt->u.trans_pt ) { xfree(pxpt); @@ -136,7 +137,7 @@ int cpufreq_statistic_init(unsigned int pxpt->u.pt =3D xzalloc_array(struct pm_px_val, count); if ( !pxpt->u.pt ) { - xfree(pxpt->u.trans_pt); + xvfree(pxpt->u.trans_pt); xfree(pxpt); spin_unlock(cpufreq_statistic_lock); return -ENOMEM; @@ -172,7 +173,7 @@ void cpufreq_statistic_exit(unsigned int return; } =20 - xfree(pxpt->u.trans_pt); + xvfree(pxpt->u.trans_pt); xfree(pxpt->u.pt); xfree(pxpt); per_cpu(cpufreq_statistic_data, cpu) =3D NULL; --- a/xen/include/xen/xvmalloc.h +++ b/xen/include/xen/xvmalloc.h @@ -22,11 +22,21 @@ (typeof(*(ptr)) *)p_; \ }) =20 +#define DIM_MUL1(n) (n) +#define DIM_MUL2(n1, n2) ({ \ + unsigned long res_; \ + __builtin_umull_overflow(n1, n2, &res_) ? ULONG_MAX : res_; \ +}) +#define DIM_MUL_(n, nums...) DIM_MUL##n(nums) +#define DIM_MUL(n, nums...) DIM_MUL_(n, ## nums) + /* Allocate space for array of typed objects. */ -#define xvmalloc_array(_type, _num) \ - ((_type *)_xvmalloc_array(sizeof(_type), __alignof__(_type), _num)) -#define xvzalloc_array(_type, _num) \ - ((_type *)_xvzalloc_array(sizeof(_type), __alignof__(_type), _num)) +#define xvmalloc_array(type, num, nums...) \ + ((type *)_xvmalloc_array(sizeof(type), __alignof__(type), \ + DIM_MUL(count_args(num, ## nums), num, ## num= s))) +#define xvzalloc_array(type, num, nums...) \ + ((type *)_xvzalloc_array(sizeof(type), __alignof__(type), \ + DIM_MUL(count_args(num, ## nums), num, ## num= s))) =20 /* Allocate space for a structure with a flexible array of typed objects. = */ #define xvzalloc_flex_struct(type, field, nr) \