From nobody Sun May 19 10:38:53 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1586145196; cv=none; d=zohomail.com; s=zohoarc; b=MSoosYCgsVD8H35rty51xZfl7y4mXF5Moj80Hc++QIHVOUTiXrlKPE4agX0RlhDFq7x0onytQgDGKSKfhKKR752LtoKALAuRF5FACaeJ3WX9Jj5D+6Xlqs2JR176MKt/q67XXfUcA3M7QEZ1H5C4FBZuwNmiiCadAzHt+lKs6mo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586145196; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=ZXN38PTE4v3I2KKh/ImjIcWyYQLX2fmeNVsqUxmNd2U=; b=T9yvqoiAoJ3PQjFWhsMsqw/g0xJqC0Ekx6WcDmVJTECn2xD+FTaAsxbfqvuypUnhd3n00YflLzlDVcLb+gJ6Ds2sAsVAyccs85MeIXWOsOeOZkCr5jscTLISKyX8pubJxdQVmssqLjTstC5qHYSlkb1TASN3i+8X+cN0bE1XI8Y= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586145196500426.7283471220112; Sun, 5 Apr 2020 20:53:16 -0700 (PDT) Received: from localhost ([::1]:54276 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jLIoz-00029v-Rc for importer@patchew.org; Sun, 05 Apr 2020 23:53:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37033) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jLIoN-0001ga-6s for qemu-devel@nongnu.org; Sun, 05 Apr 2020 23:52:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jLIoL-0008E5-8s for qemu-devel@nongnu.org; Sun, 05 Apr 2020 23:52:34 -0400 Received: from mga07.intel.com ([134.134.136.100]:5815) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jLIoL-00089S-0M for qemu-devel@nongnu.org; Sun, 05 Apr 2020 23:52:33 -0400 Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Apr 2020 20:52:24 -0700 Received: from desktop-do5fptk.sh.intel.com (HELO localhost.localdomain) ([10.239.47.198]) by fmsmga005.fm.intel.com with ESMTP; 05 Apr 2020 20:52:23 -0700 IronPort-SDR: gJTgQNWNMHkIf4rYxUYpuw66d0E+2XjyxoFWWFRecucpK9cLgCLOY2iDcRDWb5f71D3OQ2GevT ft7ZlEgNlkhQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: HldVKowbZjJJIwSAR6pKNM1QGRJyt40x6NVRXPuXjeylPBHo0Mrp+NyFU29M95Ct0QBxgPiiyY RN+etqjQketQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,349,1580803200"; d="scan'208";a="450690792" From: WangBowen To: qemu-devel@nongnu.org Subject: [PATCH] hax: Dynamic allocate vcpu state structure Date: Mon, 6 Apr 2020 11:50:16 +0800 Message-Id: <20200406035016.609-1-bowen.wang@intel.com> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bowen.wang@intel.com, colin.xu@intel.com, wenchao.wang@intel.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Dynamic allocating vcpu state structure according to smp value to be more precise and safe. Previously it will allocate array of fixed size HAX_MAX_VCPU. This is achieved by using g_new0 to dynamic allocate the array. The allocated size is obtained from smp.max_cpus in MachineState. Also, the size is compared with HAX_MAX_VCPU when creating the vm. The reason for chosing dynamic array over linked list is because the status is visited by index all the time. This will lead to QEMU checking whether the smp value is larger than the HAX_MAX_VCPU when creating vm, if larger, the process will terminate, otherwise it will allocate array of size smp to store the status. Signed-off-by: WangBowen --- target/i386/hax-all.c | 25 +++++++++++++++++++------ target/i386/hax-i386.h | 5 +++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/target/i386/hax-all.c b/target/i386/hax-all.c index a8b6e5aeb8..7ccd53a901 100644 --- a/target/i386/hax-all.c +++ b/target/i386/hax-all.c @@ -232,10 +232,10 @@ int hax_init_vcpu(CPUState *cpu) return ret; } =20 -struct hax_vm *hax_vm_create(struct hax_state *hax) +struct hax_vm *hax_vm_create(struct hax_state *hax, int max_cpus) { struct hax_vm *vm; - int vm_id =3D 0, ret; + int vm_id =3D 0, ret, i; =20 if (hax_invalid_fd(hax->fd)) { return NULL; @@ -259,6 +259,17 @@ struct hax_vm *hax_vm_create(struct hax_state *hax) goto error; } =20 + if (max_cpus > HAX_MAX_VCPU){ + fprintf(stderr, "Failed to create vm, maximum possible VCPU number= supported by QEMU is %d\n", HAX_MAX_VCPU); + goto error; + } + + vm->numvcpus =3D max_cpus; + vm->vcpus =3D g_new0(struct hax_vcpu_state *, vm->numvcpus); + for(i =3D 0; i < vm->numvcpus; i++){ + vm->vcpus[i]=3DNULL; + } + hax->vm =3D vm; return vm; =20 @@ -272,12 +283,14 @@ int hax_vm_destroy(struct hax_vm *vm) { int i; =20 - for (i =3D 0; i < HAX_MAX_VCPU; i++) + for (i =3D 0; i < vm->numvcpus; i++) if (vm->vcpus[i]) { fprintf(stderr, "VCPU should be cleaned before vm clean\n"); return -1; } hax_close_fd(vm->fd); + vm->numvcpus =3D 0; + g_free(vm->vcpus); g_free(vm); hax_global.vm =3D NULL; return 0; @@ -292,7 +305,7 @@ static void hax_handle_interrupt(CPUState *cpu, int mas= k) } } =20 -static int hax_init(ram_addr_t ram_size) +static int hax_init(ram_addr_t ram_size, int max_cpus) { struct hax_state *hax =3D NULL; struct hax_qemu_version qversion; @@ -324,7 +337,7 @@ static int hax_init(ram_addr_t ram_size) goto error; } =20 - hax->vm =3D hax_vm_create(hax); + hax->vm =3D hax_vm_create(hax, max_cpus); if (!hax->vm) { fprintf(stderr, "Failed to create HAX VM\n"); ret =3D -EINVAL; @@ -352,7 +365,7 @@ static int hax_init(ram_addr_t ram_size) =20 static int hax_accel_init(MachineState *ms) { - int ret =3D hax_init(ms->ram_size); + int ret =3D hax_init(ms->ram_size, (int)ms->smp.max_cpus); =20 if (ret && (ret !=3D -ENOSPC)) { fprintf(stderr, "No accelerator found.\n"); diff --git a/target/i386/hax-i386.h b/target/i386/hax-i386.h index 54e9d8b057..7d988f81da 100644 --- a/target/i386/hax-i386.h +++ b/target/i386/hax-i386.h @@ -47,7 +47,8 @@ struct hax_state { struct hax_vm { hax_fd fd; int id; - struct hax_vcpu_state *vcpus[HAX_MAX_VCPU]; + int numvcpus; + struct hax_vcpu_state **vcpus; }; =20 #ifdef NEED_CPU_H @@ -58,7 +59,7 @@ int valid_hax_tunnel_size(uint16_t size); /* Host specific functions */ int hax_mod_version(struct hax_state *hax, struct hax_module_version *vers= ion); int hax_inject_interrupt(CPUArchState *env, int vector); -struct hax_vm *hax_vm_create(struct hax_state *hax); +struct hax_vm *hax_vm_create(struct hax_state *hax, int max_cpus); int hax_vcpu_run(struct hax_vcpu_state *vcpu); int hax_vcpu_create(int id); int hax_sync_vcpu_state(CPUArchState *env, struct vcpu_state_t *state, --=20 2.24.1