From patchwork Mon Dec 12 23:05:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 633245 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp2770160pvb; Mon, 12 Dec 2022 15:07:18 -0800 (PST) X-Google-Smtp-Source: AA0mqf7MQpj1VS7bv8o5HaRMaRHCCcMOGjYzFo7Nd2Xwd5cH+DR9hmQ0MQOekzt6r8HYH2fK/OZn X-Received: by 2002:ad4:58ae:0:b0:4c7:b0f:675b with SMTP id ea14-20020ad458ae000000b004c70b0f675bmr5280309qvb.5.1670886438746; Mon, 12 Dec 2022 15:07:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670886438; cv=none; d=google.com; s=arc-20160816; b=OhG0WnVNPKv1XviYQI6oELB6tDuFkHB8ESN+lYqJ0w9I35ab/k3zvecZUeYCNJzflp Ap+lsAFm+0XfAqvx4kuzpJ8f5WjoykgsxpOvWaJgmj5KS7vvVnbWZ1Hkg5vIKOlBMBFY zaS6wEEIutyquak0DL4nlQWw3qa00mzEE5bjM5uW+eQ5YPIP5Ovi3oWm+cMkho1W+A/5 mW29WIlk/IuN2+fT0XNuXXVsLza2J7mgcRGxZ56mE+oZviHv1mw7rUwdOlcB30w8sYwG 9qi8kl3TFcei1e0W/cbOk09p04ogKyY/Mj7Q9p4GaK8m1pZl5IL6bY+KhnoeBAd8fYLb zotg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=a+yLOOTWW5RTMZqFbarXXQFRPj78oIVaFPvsMYq9Uqc=; b=FlOVa6a2ZLKc4bQQ4dAkO7dpkNZQcaJDOIDpjHhamIwBmyAGMhbOFxOX3vZiGJd0j4 e9YuD1Wb7JNc9vN6dcfpRi5jXrn7jQdRXqIQairIbAbevF+Rmaf8upcpEBE28cLVpWjV +8jPT2mhE9Hg+knwtnnVI1LyjzE998QHfLktKMhwWsDOeWWFMNmlANhLTXLw7vpq/Lei G3rIBwIZyCqK1sfsCZpjujJ/c3PZZVZ9cLpWv2RM2vfo23DJIrJrYL4Vp0lxyXyBw+dv h+76PPfb4RfAGrTGaWApTK5JnWNgNuLceEa6cFYIp+u35YtSMLdb3m311ebpwrbQJyyh FYgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fiFCDIm+; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id gg3-20020a056214252300b004c6eacc1131si5021450qvb.268.2022.12.12.15.07.18 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 12 Dec 2022 15:07:18 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fiFCDIm+; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p4rsG-0007I0-VR; Mon, 12 Dec 2022 18:06:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p4rsF-0007HU-JV for qemu-devel@nongnu.org; Mon, 12 Dec 2022 18:06:15 -0500 Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p4rsD-0004NT-Pe for qemu-devel@nongnu.org; Mon, 12 Dec 2022 18:06:15 -0500 Received: by mail-ej1-x635.google.com with SMTP id m18so32179967eji.5 for ; Mon, 12 Dec 2022 15:06:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=a+yLOOTWW5RTMZqFbarXXQFRPj78oIVaFPvsMYq9Uqc=; b=fiFCDIm+EKkVAK23HboGpSRrjMsjKykCCZckVXf3RsUXvuUJeT0DysQUybE1XemEZi 6m3hyez/T1BmZRTdPCV6PQc5h11TmIwDavybln43tuwJp8gYLTK4bsh8F9cH51x0C9/2 mvAnYS3jInbwsq8ObVP2lYYosVTOXoRZtRG51wvmef6AMFIcGl+/q3IQt+zsz6C020hQ BNm9PFqOsTu4esNp8tSctCVNkjpY44/h21GXlkSQ9HyR026uItjGJ8ugNj6oUd3fDEj5 wlE/4KTbK/a7aAxaelNR1eT11PSEViNTs2W1zRVd/Zv+8YM791tPDlqmyAi2X2wPCau1 1Bkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a+yLOOTWW5RTMZqFbarXXQFRPj78oIVaFPvsMYq9Uqc=; b=mMoJCQ1BHpuH7N75KGcrqELPmbLlCXVPuhYWxzNuYvnjK1sukpF3Dd8AJD/DsbSRWT dLlo/o/iDSiGtTZddfY/mRDLx8tvg/0fTiajXxDqwp8yrO2JLv/0sb6Nd5sIXy7ZGS7R o4rHftQwnTHAlW7REnsOoRmIyeTnY5tfn1887VHDi/u86S0O9yjFLFFzw0qlaKbQXh2G US+qH5CN6k4wXq6lpEC/C9k4fvnrSDDBZH+A9DOuAPXXcWMvmYTNuInVaG0pi5Vl+XdO SVe3jWNbFlOxTwnNsqd2psHWQC40v5CcRkjInCi0iwA+vGv47sII4Iz19qitKyVWyo4C dH+A== X-Gm-Message-State: ANoB5pkrp8IokE1KpGWhH2s/BPPqyq0otc3kBRtpnpcjo+jFgDpq7A7M 8hGsksiq5j/s1pcjvoEd9PJIfCAj4xz8pS09cOs= X-Received: by 2002:a17:906:1805:b0:7b2:7b66:9ed4 with SMTP id v5-20020a170906180500b007b27b669ed4mr20604503eje.47.1670886372185; Mon, 12 Dec 2022 15:06:12 -0800 (PST) Received: from localhost.localdomain ([185.126.107.38]) by smtp.gmail.com with ESMTPSA id h20-20020a170906111400b007c0deb2449fsm3792478eja.82.2022.12.12.15.06.10 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 12 Dec 2022 15:06:11 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, Thomas Huth Cc: Greg Kurz , Stefan Hajnoczi , Kevin Wolf , qemu-block@nongnu.org, Paolo Bonzini , "Michael S. Tsirkin" , =?utf-8?q?Alex_Benn=C3=A9e?= , Jason Wang , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Christian Schoenebeck , Hanna Reitz , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [RFC PATCH-for-8.0 09/10] hw/virtio: Extract vhost_user_ram_slots_max() to vhost-user-target.c Date: Tue, 13 Dec 2022 00:05:16 +0100 Message-Id: <20221212230517.28872-10-philmd@linaro.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221212230517.28872-1-philmd@linaro.org> References: <20221212230517.28872-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::635; envelope-from=philmd@linaro.org; helo=mail-ej1-x635.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org The current definition of VHOST_USER_MAX_RAM_SLOTS is target specific. By converting this definition to a runtime vhost_user_ram_slots_max() helper declared in a target specific unit, we can have the rest of vhost-user.c target independent. To avoid variable length array or using the heap to store arrays of vhost_user_ram_slots_max() elements, we simply declare an array of the biggest VHOST_USER_MAX_RAM_SLOTS, and each target uses up to vhost_user_ram_slots_max() elements of it. Ensure arrays are big enough by adding an assertion in vhost_user_init(). Signed-off-by: Philippe Mathieu-Daudé --- RFC: Should I add VHOST_USER_MAX_RAM_SLOTS to vhost-user.h or create an internal header for it? --- hw/virtio/meson.build | 1 + hw/virtio/vhost-user-target.c | 29 +++++++++++++++++++++++++++++ hw/virtio/vhost-user.c | 26 +++++--------------------- include/hw/virtio/vhost-user.h | 7 +++++++ 4 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 hw/virtio/vhost-user-target.c diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index eb7ee8ea92..bf7e35fa8a 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -11,6 +11,7 @@ if have_vhost specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c')) if have_vhost_user specific_virtio_ss.add(files('vhost-user.c')) + specific_virtio_ss.add(files('vhost-user-target.c')) endif if have_vhost_vdpa specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c')) diff --git a/hw/virtio/vhost-user-target.c b/hw/virtio/vhost-user-target.c new file mode 100644 index 0000000000..6a0d0f53d0 --- /dev/null +++ b/hw/virtio/vhost-user-target.c @@ -0,0 +1,29 @@ +/* + * vhost-user target-specific helpers + * + * Copyright (c) 2013 Virtual Open Systems Sarl. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "hw/virtio/vhost-user.h" + +#if defined(TARGET_X86) || defined(TARGET_X86_64) || \ + defined(TARGET_ARM) || defined(TARGET_ARM_64) +#include "hw/acpi/acpi.h" +#elif defined(TARGET_PPC) || defined(TARGET_PPC64) +#include "hw/ppc/spapr.h" +#endif + +unsigned int vhost_user_ram_slots_max(void) +{ +#if defined(TARGET_X86) || defined(TARGET_X86_64) || \ + defined(TARGET_ARM) || defined(TARGET_ARM_64) + return ACPI_MAX_RAM_SLOTS; +#elif defined(TARGET_PPC) || defined(TARGET_PPC64) + return SPAPR_MAX_RAM_SLOTS; +#else + return 512; +#endif +} diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 8f635844af..21fc176725 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -41,24 +41,7 @@ #define VHOST_MEMORY_BASELINE_NREGIONS 8 #define VHOST_USER_F_PROTOCOL_FEATURES 30 #define VHOST_USER_SLAVE_MAX_FDS 8 - -/* - * Set maximum number of RAM slots supported to - * the maximum number supported by the target - * hardware plaform. - */ -#if defined(TARGET_X86) || defined(TARGET_X86_64) || \ - defined(TARGET_ARM) || defined(TARGET_ARM_64) -#include "hw/acpi/acpi.h" -#define VHOST_USER_MAX_RAM_SLOTS ACPI_MAX_RAM_SLOTS - -#elif defined(TARGET_PPC) || defined(TARGET_PPC64) -#include "hw/ppc/spapr.h" -#define VHOST_USER_MAX_RAM_SLOTS SPAPR_MAX_RAM_SLOTS - -#else #define VHOST_USER_MAX_RAM_SLOTS 512 -#endif /* * Maximum size of virtio device config space @@ -935,7 +918,7 @@ static int vhost_user_add_remove_regions(struct vhost_dev *dev, if (track_ramblocks) { memcpy(u->postcopy_client_bases, shadow_pcb, - sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS); + sizeof(uint64_t) * vhost_user_ram_slots_max()); /* * Now we've registered this with the postcopy code, we ack to the * client, because now we're in the position to be able to deal with @@ -956,7 +939,7 @@ static int vhost_user_add_remove_regions(struct vhost_dev *dev, err: if (track_ramblocks) { memcpy(u->postcopy_client_bases, shadow_pcb, - sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS); + sizeof(uint64_t) * vhost_user_ram_slots_max()); } return ret; @@ -1030,7 +1013,7 @@ static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev, } memset(u->postcopy_client_bases, 0, - sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS); + sizeof(uint64_t) * vhost_user_ram_slots_max()); /* * They're in the same order as the regions that were sent @@ -2169,7 +2152,7 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque, return -EINVAL; } - u->user->memory_slots = MIN(ram_slots, VHOST_USER_MAX_RAM_SLOTS); + u->user->memory_slots = MIN(ram_slots, vhost_user_ram_slots_max()); } } @@ -2649,6 +2632,7 @@ static void vhost_user_state_destroy(gpointer data) bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp) { + assert(vhost_user_ram_slots_max() <= VHOST_USER_MAX_RAM_SLOTS); if (user->chr) { error_setg(errp, "Cannot initialize vhost-user state"); return false; diff --git a/include/hw/virtio/vhost-user.h b/include/hw/virtio/vhost-user.h index 191216a74f..e13584ade8 100644 --- a/include/hw/virtio/vhost-user.h +++ b/include/hw/virtio/vhost-user.h @@ -86,4 +86,11 @@ void vhost_user_async_close(DeviceState *d, CharBackend *chardev, struct vhost_dev *vhost, vu_async_close_fn cb); +/** + * vhost_user_ram_slots_max() + * + * Return: maximum number of RAM slots supported by the target hardware plaform. + */ +unsigned int vhost_user_ram_slots_max(void); + #endif