Received: by 2002:a17:906:5a52:b0:9d0:bf65:29fa with SMTP id my18csp1595447ejc; Mon, 13 Nov 2023 12:14:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IETmIeQk/yZJ/CR182vcsaQrCqRtRPdo8WsW3qo32GzxhTA84sKGL/gfpgpg2qmfj6DN4XT X-Received: by 2002:a05:6000:1888:b0:329:6d09:61f7 with SMTP id a8-20020a056000188800b003296d0961f7mr4560487wri.48.1699906490419; Mon, 13 Nov 2023 12:14:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699906490; cv=none; d=google.com; s=arc-20160816; b=oHPy5ZJafFWGqrCldNMk9Dj9FMb8sf4MrsJyXO958Y0tnLeTfBcLDuX+2Lb3AEQTDW D1jtDg8cQmxMT2tyAWw+aXiu+oi22JgcoiX20yqGhKUHYAWkbJZhp6R3psK5l4n1lYyV 6dYCF7+WCKWe+eBlabYfGPHPbOxj38z2552R2dlnz6WGCl1G244BzP8b32O7m5kbxVHF PB49vtMB7DNdqg7ROHKAsuknMgdAjb8MBwIynOb1w+JMYX8M4tyVx2VnkppUF7jYk0F2 76e0AbXdIXJvrxz4Ycyg9QCbwSSty7UWzqqetI4Xck9PbVBv0rb0DKzdw1j31Rj0hjxr tWxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=nLrT/0Cjdjf4UV3h1qTNgZMTbcETNFPuCrfI9fv6EDE=; fh=I+6CS2uJqPEgP3gz8gmk6tLbm1jsmO9tblK+QTjWHxs=; b=WbhIA7oLikS18aSoKS0FgH4v/XIOkQbb75gLdLYL1IQu5frtE8LEzieHpmLFJvIwiJ +ZrBSC0swvpfgMoAE16awClfdEV26klodfaXFgwj/miVmZ2Ie6hyRvqi9ptdWCaqRHEL Ev9zgalUKGl3/G5IcfxL6nGaO8XjiC5WMhnwv1WvuIQjaiE92na5FKL4JQWUzYv0JVqM VZZcIw0oQ1FhEk5MI0cvfTEU1e7xVfUU32CQO101IOXruv1TwIHOixnheKkcz+EP1Z88 Om4jkfdQ+0z48RlSDHReja7A0Z4psQY2DB66QgUCLTQOLOJPM97ZuuhjR3Iw3iCggWHj kGIA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of salil.mehta@huawei.com designates 185.176.79.56 as permitted sender) smtp.mailfrom=salil.mehta@huawei.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from frasgout.his.huawei.com (frasgout.his.huawei.com. [185.176.79.56]) by mx.google.com with ESMTPS id e22-20020a5d5956000000b0032f7cb1984dsi3407548wri.117.2023.11.13.12.14.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Nov 2023 12:14:50 -0800 (PST) Received-SPF: pass (google.com: domain of salil.mehta@huawei.com designates 185.176.79.56 as permitted sender) client-ip=185.176.79.56; Authentication-Results: mx.google.com; spf=pass (google.com: domain of salil.mehta@huawei.com designates 185.176.79.56 as permitted sender) smtp.mailfrom=salil.mehta@huawei.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from lhrpeml500001.china.huawei.com (unknown [172.18.147.226]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4STgX62fksz67f4J; Tue, 14 Nov 2023 04:10:14 +0800 (CST) Received: from A190218597.china.huawei.com (10.48.147.128) by lhrpeml500001.china.huawei.com (7.191.163.213) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Mon, 13 Nov 2023 20:14:32 +0000 From: Salil Mehta To: , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH V7 6/8] physmem: Add helper function to destroy CPU AddressSpace Date: Mon, 13 Nov 2023 20:12:34 +0000 Message-ID: <20231113201236.30668-7-salil.mehta@huawei.com> X-Mailer: git-send-email 2.8.3 In-Reply-To: <20231113201236.30668-1-salil.mehta@huawei.com> References: <20231113201236.30668-1-salil.mehta@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.48.147.128] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To lhrpeml500001.china.huawei.com (7.191.163.213) X-CFilter-Loop: Reflected X-TUID: XRpn5gHVV2Dv Virtual CPU Hot-unplug leads to unrealization of a CPU object. This also involves destruction of the CPU AddressSpace. Add common function to help destroy the CPU AddressSpace. Signed-off-by: Salil Mehta Tested-by: Vishnu Pajjuri Reviewed-by: Gavin Shan Tested-by: Xianglai Li Tested-by: Miguel Luis Reviewed-by: Shaoqin Huang --- include/exec/cpu-common.h | 8 ++++++++ include/hw/core/cpu.h | 1 + system/physmem.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index 605b160a7e..a930e49e02 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -127,6 +127,14 @@ size_t qemu_ram_pagesize_largest(void); */ void cpu_address_space_init(CPUState *cpu, int asidx, const char *prefix, MemoryRegion *mr); +/** + * cpu_address_space_destroy: + * @cpu: CPU for which address space needs to be destroyed + * @asidx: integer index of this address space + * + * Note that with KVM only one address space is supported. + */ +void cpu_address_space_destroy(CPUState *cpu, int asidx); void cpu_physical_memory_rw(hwaddr addr, void *buf, hwaddr len, bool is_write); diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 3968369554..708b6b48de 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -496,6 +496,7 @@ struct CPUState { QSIMPLEQ_HEAD(, qemu_work_item) work_list; CPUAddressSpace *cpu_ases; + int cpu_ases_count; int num_ases; AddressSpace *as; MemoryRegion *memory; diff --git a/system/physmem.c b/system/physmem.c index edc3ed8ab9..a16f1d4056 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -761,6 +761,7 @@ void cpu_address_space_init(CPUState *cpu, int asidx, if (!cpu->cpu_ases) { cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases); + cpu->cpu_ases_count = cpu->num_ases; } newas = &cpu->cpu_ases[asidx]; @@ -774,6 +775,34 @@ void cpu_address_space_init(CPUState *cpu, int asidx, } } +void cpu_address_space_destroy(CPUState *cpu, int asidx) +{ + CPUAddressSpace *cpuas; + + assert(cpu->cpu_ases); + assert(asidx >= 0 && asidx < cpu->num_ases); + /* KVM cannot currently support multiple address spaces. */ + assert(asidx == 0 || !kvm_enabled()); + + cpuas = &cpu->cpu_ases[asidx]; + if (tcg_enabled()) { + memory_listener_unregister(&cpuas->tcg_as_listener); + } + + address_space_destroy(cpuas->as); + g_free_rcu(cpuas->as, rcu); + + if (asidx == 0) { + /* reset the convenience alias for address space 0 */ + cpu->as = NULL; + } + + if (--cpu->cpu_ases_count == 0) { + g_free(cpu->cpu_ases); + cpu->cpu_ases = NULL; + } +} + AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx) { /* Return the AddressSpace corresponding to the specified index */ -- 2.34.1