From patchwork Tue Jun 9 10:38:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 187643 Delivered-To: patch@linaro.org Received: by 2002:a92:cf06:0:0:0:0:0 with SMTP id c6csp401467ilo; Tue, 9 Jun 2020 03:41:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJ9RD7GQHukiPSIDFlgsIbvjxHmmIytr0VgVTUn7K7ZkL1rtv8CfO/hI2buyNfWLz+3vgB X-Received: by 2002:a25:2415:: with SMTP id k21mr5510126ybk.349.1591699273389; Tue, 09 Jun 2020 03:41:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591699273; cv=none; d=google.com; s=arc-20160816; b=gVNYQaEFH+tHp/3Gy1vYffG53sop981VnxWltDBWkq84cJMhYqqwQZ96ZCx4BMJlmc 38RstUIOne/r4wawGEqkfUad8HjEX4sFn5UQENjag66tmHfvEq4Sr+UUojaeFXLQqHmY /JYSS9KS8/SafuAxLbDAFgNPM+7py9/aeenfN4n6te8sDIlML92qA5JMEN1vrFwlyrev CPgiy91de2FLo6TndvkDFqCIJtb8Au4qZZfpPyNm/Y5h91CMkc8axFanSghrrHivBmMn avFd6GAWhDH6H/wv4KSLKy73juwlrZQGLEz1GOrGOxYwqS68uNcORtUdfx00+c8mouJh YRfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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:to:from :dkim-signature; bh=2ELcfDT80Dh4TpW73j2IcyKGQvBld3yZPEqT0DBBzd0=; b=Eayka9r2eZOGiFX677pmu/X/Egm3HZ7QfnMUV47y2zFUkE/GMGSkf6IzPoFFOaO/Ug YZ1FW1I+ZLi4u9eZvmeH6EAoHZr7Cni/xag2CNc7uK73r54WTqtWHhYGTfWMNNFe4bys Vne1V27hfrFWa56Ad0wynuY5T+8nPVE/9EOrkvUi3xDPzQG3+caa8dSRJ5+rPQWaWK5T CoTeMuHt9O1vVF3eWVLlZuAJ0zX3aj9FBcMSmHpJAv9K+dFQQCEVGDdZBOpv6S7VWhsg lRXnqWNGWSlJePv9IRLBigy8UXoWUoMkqyTlb4zKRfChtlVW+i3H+hfFu7yf2zAtUHNH 7I5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=AJwZJyU2; 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=fail (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 k22si2319477ybf.91.2020.06.09.03.41.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 09 Jun 2020 03:41:13 -0700 (PDT) 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=fail header.i=@linaro.org header.s=google header.b=AJwZJyU2; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:39568 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jibgu-0002cF-NJ for patch@linaro.org; Tue, 09 Jun 2020 06:41:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59308) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jibeE-0007lI-K6 for qemu-devel@nongnu.org; Tue, 09 Jun 2020 06:38:26 -0400 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]:53351) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jibeD-0004PY-Ga for qemu-devel@nongnu.org; Tue, 09 Jun 2020 06:38:26 -0400 Received: by mail-wm1-x32a.google.com with SMTP id l26so2322127wme.3 for ; Tue, 09 Jun 2020 03:38:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2ELcfDT80Dh4TpW73j2IcyKGQvBld3yZPEqT0DBBzd0=; b=AJwZJyU2gsqSDPrMFkk71y1yCCjistWBuBbIaYcPSrr1SLy61hOWwHn+oL0MScE1Md K8p48RL4mLQWhcK5Js+Hpv1wZ5O8Frbarb4z4RtNM6igfEY2REKvVapHijOHYPMtoxcs kE1xrehbj0ikPbrrBWi0CiVsxRN7aapDZ3HZAGZDaDmZqwW/bTPISDHaCyLBD3KONfsm 4GcMpVWM14xHN1p52C3li5elbbq+Zr+eXlxvtAAY9kvV6DH9MeF4fcbYTzf74A/BN5cN +x7evb/YosQpw8p3RHuEzddO5Eamwau/Uc+Tm7OD68dNKMCJRnUeXzvm+dNGrgImNK8J aczQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2ELcfDT80Dh4TpW73j2IcyKGQvBld3yZPEqT0DBBzd0=; b=SY1Mp4cGv6fyQ8s7PfN+EbmyB4ezwUEE6uujh+ySrQPXPzN29qdUTCVWI8YNJXgMxA 6wbGjRN/5PvjUgLTjrEVoKhEuykBS/aWLA/Ya8G6SiM10ZRfAS6zJL3qKxLqLwTwBsLp koAnWXSwSdXLYrFxuf2aAM5ZnIiAzDS3R2V5isesd1uue+Z2Pu3xCtjayeRa7pr8cl1y 4uzaLOaP9PC5GtgUIdoq+HwBWknWMc9lsuTZuUkkswN1CUdHK1A5XzW5bXCLRPOGXXGn oNdbRUwRleeZWqxzIAVRL4UmfQB/J4TOMFTSPGlJeI4yrE621jW9L53hoMQJawuXwHc4 Ausg== X-Gm-Message-State: AOAM533ar6zYcpCk7+YqVl+00+zHwAsRymag8h7nwMRNgTUZ/WP2tGbQ zxJBfVnlv8jl5ccbp5fD17mpdA== X-Received: by 2002:a1c:64d5:: with SMTP id y204mr3455213wmb.131.1591699103519; Tue, 09 Jun 2020 03:38:23 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id g82sm2513720wmf.1.2020.06.09.03.38.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jun 2020 03:38:19 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 5E8B91FF98; Tue, 9 Jun 2020 11:38:11 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Subject: [PULL 09/17] hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE Date: Tue, 9 Jun 2020 11:38:01 +0100 Message-Id: <20200609103809.23443-10-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200609103809.23443-1-alex.bennee@linaro.org> References: <20200609103809.23443-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32a; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x32a.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action 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: "Michael S . Tsirkin" , Fabiano Rosas , qemu-devel@nongnu.org, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?utf-8?q?Alex_Benn=C3=A9e?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" The purpose of vhost_section is to identify RAM regions that need to be made available to a vhost client. However when running under TCG all RAM sections have DIRTY_MEMORY_CODE set which leads to problems down the line. Re-factor the code so: - steps are clearer to follow - reason for rejection is recorded in the trace point - we allow DIRTY_MEMORY_CODE We expand the comment to explain that kernel based vhost has specific support for migration tracking. Signed-off-by: Alex Bennée Tested-by: Fabiano Rosas Cc: Michael S. Tsirkin Cc: Dr. David Alan Gilbert Cc: Stefan Hajnoczi Message-Id: <20200605154929.26910-11-alex.bennee@linaro.org> -- 2.20.1 diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index aff98a0ede5..e3e21812905 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -27,6 +27,7 @@ #include "migration/blocker.h" #include "migration/qemu-file-types.h" #include "sysemu/dma.h" +#include "sysemu/tcg.h" #include "trace.h" /* enabled until disconnected backend stabilizes */ @@ -403,26 +404,50 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev, return r; } +/* + * vhost_section: identify sections needed for vhost access + * + * We only care about RAM sections here (where virtqueue and guest + * internals accessed by virtio might live). If we find one we still + * allow the backend to potentially filter it out of our list. + */ static bool vhost_section(struct vhost_dev *dev, MemoryRegionSection *section) { - bool result; - bool log_dirty = memory_region_get_dirty_log_mask(section->mr) & - ~(1 << DIRTY_MEMORY_MIGRATION); - result = memory_region_is_ram(section->mr) && - !memory_region_is_rom(section->mr); - - /* Vhost doesn't handle any block which is doing dirty-tracking other - * than migration; this typically fires on VGA areas. - */ - result &= !log_dirty; + MemoryRegion *mr = section->mr; + + if (memory_region_is_ram(mr) && !memory_region_is_rom(mr)) { + uint8_t dirty_mask = memory_region_get_dirty_log_mask(mr); + uint8_t handled_dirty; + + /* + * Kernel based vhost doesn't handle any block which is doing + * dirty-tracking other than migration for which it has + * specific logging support. However for TCG the kernel never + * gets involved anyway so we can also ignore it's + * self-modiying code detection flags. However a vhost-user + * client could still confuse a TCG guest if it re-writes + * executable memory that has already been translated. + */ + handled_dirty = (1 << DIRTY_MEMORY_MIGRATION) | + (1 << DIRTY_MEMORY_CODE); - if (result && dev->vhost_ops->vhost_backend_mem_section_filter) { - result &= - dev->vhost_ops->vhost_backend_mem_section_filter(dev, section); - } + if (dirty_mask & ~handled_dirty) { + trace_vhost_reject_section(mr->name, 1); + return false; + } + + if (dev->vhost_ops->vhost_backend_mem_section_filter && + !dev->vhost_ops->vhost_backend_mem_section_filter(dev, section)) { + trace_vhost_reject_section(mr->name, 2); + return false; + } - trace_vhost_section(section->mr->name, result); - return result; + trace_vhost_section(mr->name); + return true; + } else { + trace_vhost_reject_section(mr->name, 3); + return false; + } } static void vhost_begin(MemoryListener *listener) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index e83500bee92..6427a0047df 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -5,7 +5,8 @@ vhost_commit(bool started, bool changed) "Started: %d Changed: %d" vhost_region_add_section(const char *name, uint64_t gpa, uint64_t size, uint64_t host) "%s: 0x%"PRIx64"+0x%"PRIx64" @ 0x%"PRIx64 vhost_region_add_section_merge(const char *name, uint64_t new_size, uint64_t gpa, uint64_t owr) "%s: size: 0x%"PRIx64 " gpa: 0x%"PRIx64 " owr: 0x%"PRIx64 vhost_region_add_section_aligned(const char *name, uint64_t gpa, uint64_t size, uint64_t host) "%s: 0x%"PRIx64"+0x%"PRIx64" @ 0x%"PRIx64 -vhost_section(const char *name, int r) "%s:%d" +vhost_section(const char *name) "%s" +vhost_reject_section(const char *name, int d) "%s:%d" vhost_iotlb_miss(void *dev, int step) "%p step %d" # vhost-user.c