From nobody Fri Oct 31 03:42:49 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=1750241903; cv=none; d=zohomail.com; s=zohoarc; b=jUmsDAhAXHAVF8j9luFIUs/9BLjSQo8KLOD+51C4S98ZeHO/7m0IotGvC3eAtn1VUMWRZUc6ueMlfflDuV02slJ4Josc28gbKjfpLAyUYLhxMoHWaI/cFyRRHtuRuXNSWtd9IholWxKxON3Z5orwpQDCw2QJ0Cbu0ZqNKeKqKr0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750241903; 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=lTcj4F18ielqy3N30HxOMFo1QHYtCfKFunvb6xsSaz8=; b=POtxAKdRv5c+JEbf6oLHGaoQ0AcOutFrpFfHir9dmud32G0T6/kzce0PGewWqfbrLc8kn3lkJLb2pHcY8xi2FaPbhwbfdnSG65/VDz34N20E59Rg1IyOBjlSR5bMJsrLphg1YviJrnL1OpIV+yCBf8PD1cGjP7a636jDPxbCVaU= 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 1750241903401745.5960373431436; Wed, 18 Jun 2025 03:18:23 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1019028.1395886 (Exim 4.92) (envelope-from ) id 1uRprm-0003Wa-Dd; Wed, 18 Jun 2025 10:18:02 +0000 Received: by outflank-mailman (output) from mailman id 1019028.1395886; Wed, 18 Jun 2025 10:18:02 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uRprm-0003WT-90; Wed, 18 Jun 2025 10:18:02 +0000 Received: by outflank-mailman (input) for mailman id 1019028; Wed, 18 Jun 2025 10:18:00 +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 1uRprk-0003WN-Qf for xen-devel@lists.xenproject.org; Wed, 18 Jun 2025 10:18:00 +0000 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [2a00:1450:4864:20::42a]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 81e95848-4c2d-11f0-a30a-13f23c93f187; Wed, 18 Jun 2025 12:17:58 +0200 (CEST) Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-3a57c8e247cso3213940f8f.1 for ; Wed, 18 Jun 2025 03:17:58 -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 98e67ed59e1d1-313c1c6a80bsm12349266a91.44.2025.06.18.03.17.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Jun 2025 03:17: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: 81e95848-4c2d-11f0-a30a-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1750241878; x=1750846678; darn=lists.xenproject.org; h=content-transfer-encoding:autocrypt:subject:from:cc:to :content-language:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to; bh=lTcj4F18ielqy3N30HxOMFo1QHYtCfKFunvb6xsSaz8=; b=f5/zzFb/klmpT8RUZJuHiPJKV/N39T5ph6gLBh4H4gBxRm5WglGvuy25hDTYS6d8pX 1XBgChNd8MdYzDpo8jN8tZBWs64A/s7s+FFqJ6gprdyzh0QytSBKB2O4Qi+ngoxr6TsJ PhQMEKk++NRYF8VZHEYv7hpeOSL0t3QcY3U5I1ywn3KMBSCNQzhAG5Wk44uyuchx4oN2 X/5e4eKvMWg3+Q0kzh3Wl2jWMTbP6PINZ/YzrPQwqgskTL+sqSvhJzgTz11RrHuTrlhd DgKwyGf0SYHLj1ZOmSr5aHkNM0c57Ic979YgVn8jL6byHlI14JdY8hc0KviR9rfrDR2k VidA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750241878; x=1750846678; h=content-transfer-encoding:autocrypt:subject:from:cc:to :content-language:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lTcj4F18ielqy3N30HxOMFo1QHYtCfKFunvb6xsSaz8=; b=MZG+O0F08sNR3t+UFlrFDwWKEtlAdd/MHVqsLuPB3sLuRTOM8gE8Do98Tn9sHUphN/ e7pSDCUkiX9LAhvwg3XCmPPjKjcTUt89xwMJjgSzCURL4zN3gkn5ygY6/eUz+lzqKL+O i6aXQahPhyH3wyogCgVDoNKhivYG7uhvt6QanRWiHEPP+WCml5VY8UCbFLP6lmgMSdOn 3CXomusSHX4g+gMwtyasSSjHWmCOiSblYJlXWMd6T0Qjcri9StBr3cckrxxnWzEfGMq/ yJ8cMpYWsS6DcwKNyPX1P+JEPsjIzdoGzRs1Idj5o2IozJP4okpVgXIrXVfPWNZN+T6k cbKQ== X-Gm-Message-State: AOJu0Yzv0EpVoyjtlSHirOT5f3B9ksQOzZyiPA+Kqv/EWYnE1mUPzqa3 5/eIFLDTVFdrNgM9t9TXCHIch1oTrfK8Uf67mYxNk5PS5INzkZJDVXYhNCVcbyPsAv3YH49B6SY VYZE= X-Gm-Gg: ASbGncv+eJx02PsKWP8eVPnB2lhvunJvRVQlqLnr+qZk+OR1R8NKmN6V0lW7Ox6FGhW BGvqP/pkjAAeUUYmO45aqLBwIwpnGCM/v56/s9IKn5G3UGOJS+8lVEcR/ejzVfFVzYqbv8gdLfZ yFCGyLlqasVjDTg35M50F/UYFezpcywhzH31UtqUO9qGEpcwmt/A1mP4yjmlCHwUxhXZqz8w8Fy OqkyQx4sjmEHB6wpRnJ7aYYm+lnvDg5//j/dHbdMMJlgd+SQFIOn5vQ7cJwvKcBQsXRk2V166in +z0mWwCY71tx3lSmfLPi95OUIoaCVI7HniZUpEIbrfHZgTXgcaL3gIDyoBDehYu8KUBbd8UdIuv 186b3Wov6A97PSmHNz/rZ6sqKrlFG9ZMkwZdm12LLwYFYYk4= X-Google-Smtp-Source: AGHT+IFhpdbRQbB2G+DgsX8ubk6qlIxzd0fwMkTgMBrOdi1huey823h/9o1k8OaXDfEpVJixAdQz/A== X-Received: by 2002:a05:6000:3112:b0:3a4:e480:b5df with SMTP id ffacd0b85a97d-3a572e79749mr14751450f8f.44.1750241878068; Wed, 18 Jun 2025 03:17:58 -0700 (PDT) Message-ID: <57107b15-affc-43a6-b6f3-6196b40bbc8b@suse.com> Date: Wed, 18 Jun 2025 12:17:48 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US 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?= From: Jan Beulich Subject: [PATCH] xvmalloc: extend to cover multi-dimensional arrays Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/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: 1750241904955116600 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 --- To my surprise code gen even improves a tiny bit for do_memory_op(), in surrounding code (with gcc14). --- 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 @@ -126,7 +127,7 @@ int cpufreq_statistic_init(unsigned int } per_cpu(cpufreq_statistic_data, cpu) =3D pxpt; =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); @@ -137,7 +138,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; @@ -171,7 +172,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,24 @@ (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_MUL3(n1, n2, n3) DIM_MUL2(DIM_MUL2(n1, n2), n3) +#define DIM_MUL4(n1, n2, n3, n4) DIM_MUL2(DIM_MUL2(n1, n2), \ + DIM_MUL2(n3, n4)) +#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) \