From patchwork Fri Dec 20 01:38:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13915990 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ECD7AE77184 for ; Fri, 20 Dec 2024 01:41:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=OYKvlAfMuP2ktBfEDhDytfcvSdR/S4IqDZKQdTsYjuw=; b=G19iPwbQcyCdyUA4P4b7sv51SZ b29TJvls3sHb4SweHFfXY/0ygOJr8AvMY7RS8ZofPSUyi1iqjhbXdAdCWQd3vaKckzk+HR119Dzsc REHImfZqtWPaMeqDTLgO5RH8Blr3oGUvM4ZRJ05DtzJyuKA9KFPx8yHiVDtsHqHsiYL1tt8niSx5c p4Vc2yzJRLpfs/vXB9kkarJ8cKaz/iXpm7rxudHf1RdakTXhzji0WFAzcQDetjZPLETs/bSHaPItE XK3Iit1Up41zsGf8dp3Pxqs6z7nCtgGyigaxEauFvduCHOS0tsu6tEejLxwqM6FrVx/uqAcs4/857 Y6wy38yg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tOS1E-00000003dph-2iDT; Fri, 20 Dec 2024 01:41:32 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tORyx-00000003cv0-1hKn for linux-arm-kernel@lists.infradead.org; Fri, 20 Dec 2024 01:39:13 +0000 Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-7fd4dd6c506so778143a12.1 for ; Thu, 19 Dec 2024 17:39:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734658750; x=1735263550; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=OYKvlAfMuP2ktBfEDhDytfcvSdR/S4IqDZKQdTsYjuw=; b=iro4SV/QiKX63TCSjbkUp7eYtUPBAWAH2NZD0ThqJh4fgV7tofeBho6eCLSWjpwlt9 ugvnuMey3Sg22+aVRJEdnOFhgqZA8xqyRk7bGGgCjHxesl1FDOMrAHBzmUU6Mx2tC2xD 3KUlmO1PGsgNht404tdGiotnRGJIG/8gGhwYs4kYAmkuRjNGrNdK8U8dSsiidgsdgxdG 9tScuJOfAIeBJxu8ZnctTOOrOoTUSpeeXFQqors98xQNwBKnxwKgk6JkEwavH3UNvJ0J 5X0o0DNe0bL6ERu1ORjMPbNrAk7rH+tfSqAvaBBp9V9gl2fZsUp2Qh3kyErqx1DdoqS6 /GGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734658750; x=1735263550; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OYKvlAfMuP2ktBfEDhDytfcvSdR/S4IqDZKQdTsYjuw=; b=rm1S6EbilUqxvb0hBEVwsT1Abmo0b0jVS4nDuNw79s4DGFNIbSLhrQXsdF0MkTCJdQ t8XzXgQH/Lg1UZqUfIp6pfhq130G0XjUSqPSxC1Lweytc7KNoM9New7/J6djx/MgQ9ip GXrAyNfgumwJ7pzJbZTSnHuBAnmlKDgD8gl1ElR1pwphzrK5hayh2iJcKXJepvSc5SL/ YKf8VLAgftwamzFe/IXWovHkFFVklz2kNOKxRNKOvggoP7sODQIxyGcSvHrpzLd362w8 2X/a3beiW3B+U/9Mwv3nX4QQsumuA0B8jveCKLoRZmImmAOSxTMaZoamG0OehO7qco32 CDiw== X-Gm-Message-State: AOJu0YxByFYPLFOgjvDUnwMWm98FZ5sJi053fFqqO+Fr0eB1aWOWw9A6 gGNVnPTvg59M8qaYzyqWtBglEA1O7d1l630Ws+qS4Zq59gn1WKLqKVO/XMQwaOlGni+QhAuZ11o 6gg== X-Google-Smtp-Source: AGHT+IGntiLPaNNzeAfuu6/TEmKwPAvoTiJFX2SO6583j9CN5As5xvpXTmf/fSQgTUKQrOus8R97PMQPQe4= X-Received: from pfbch14.prod.google.com ([2002:a05:6a00:288e:b0:727:3a40:52cd]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3a85:b0:1e1:c8f5:19ee with SMTP id adf61e73a8af0-1e5e04945e8mr2117339637.25.1734658750347; Thu, 19 Dec 2024 17:39:10 -0800 (PST) Date: Thu, 19 Dec 2024 17:38:59 -0800 In-Reply-To: <20241220013906.3518334-1-seanjc@google.com> Mime-Version: 1.0 References: <20241220013906.3518334-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241220013906.3518334-2-seanjc@google.com> Subject: [PATCH 1/8] KVM: selftests: Fix mostly theoretical leak of VM's binary stats FD From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241219_173911_439567_B860547A X-CRM114-Status: GOOD ( 11.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org When allocating and freeing a VM's cached binary stats info, check for a NULL descriptor, not a '0' file descriptor, as '0' is a legal FD. E.g. in the unlikely scenario the kernel installs the stats FD at entry '0', selftests would reallocate on the next __vm_get_stat() and/or fail to free the stats in kvm_vm_free(). Fixes: 83f6e109f562 ("KVM: selftests: Cache binary stats metadata for duration of test") Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/lib/kvm_util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 33fefeb3ca44..91d295ef5d02 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -749,7 +749,7 @@ void kvm_vm_free(struct kvm_vm *vmp) return; /* Free cached stats metadata and close FD */ - if (vmp->stats_fd) { + if (vmp->stats_desc) { free(vmp->stats_desc); close(vmp->stats_fd); } @@ -2218,7 +2218,7 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, size_t size_desc; int i; - if (!vm->stats_fd) { + if (!vm->stats_desc) { vm->stats_fd = vm_get_stats_fd(vm); read_stats_header(vm->stats_fd, &vm->stats_header); vm->stats_desc = read_stats_descriptors(vm->stats_fd, From patchwork Fri Dec 20 01:39:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13915991 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AC1DDE7718A for ; Fri, 20 Dec 2024 01:42:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=VJst1sRt7o765YHKTsyCLHu+qBA9pewJkWtnndgMLN0=; b=SsFiM3SyHWLaStV5kNX6whk4jw me8OXTV7uvkZ+fXwVxmvfjLj3r2pcHPZc9Jax1c4cDsh4NU4c8p7KSJkir9KzG8SYjQTPNo0AkNqm wOGQhEzcGc+P7IIEVKw5mKYT0Jh4TjuWSrITzwlkJoWyyp4tTLdpg135K52aggCPueYLWupxNjD7g F47E/pnc+MDINE7BGOJWBTcFacve1tcn0sTM7acFPtrQdWo+WT0/TTz/Ht51TYJOg2Bu/kWq0F5us eBjfec/0PkD6vrV8xf1Y83pfhv9jIOkj6XjhAg4KJos0hgVIuUC8gy7DvpaVxo+e9G9fAW8LQ1Rl8 ZlB/mBtw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tOS2M-00000003e8v-2ZW8; Fri, 20 Dec 2024 01:42:42 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tORyz-00000003cwM-2Qnz for linux-arm-kernel@lists.infradead.org; Fri, 20 Dec 2024 01:39:14 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-21655569152so12654765ad.2 for ; Thu, 19 Dec 2024 17:39:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734658752; x=1735263552; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=VJst1sRt7o765YHKTsyCLHu+qBA9pewJkWtnndgMLN0=; b=EQUSsGcrzBcoPbyjmmYM5Di9G5KIK5aIvTZD88/cuwPC3+PAQ2x+tyZOE80kKxPsdE /jWfp8106rWK8pYOARsMmFLWZ9LigG0gjsqFkNC8/eHn60xd8/zpFXPlC8IhTTxc0XA2 MN1VZevsu+TMWwn3fUDEVfPE2MNeCtCSaFrZ2L4YOo8wMLbPGvBN6+Pgu0MKVAPCGdvo tYNKyh3TAMlLlmR7ymoGrgUoLqL+3OrmlODfYa+E+U68MK98CyjZQGVY/tm1QKkbOKiH TNjciH7pxOUQzfxyd7Hw9OJlF/hBf3waD2tOGTrniNYw2pie+8fg51tg166eUr4bRyd3 Pnrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734658752; x=1735263552; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VJst1sRt7o765YHKTsyCLHu+qBA9pewJkWtnndgMLN0=; b=c1kew8Zjp/jM3jgQph67lLohjDX5rGkiwADp1Qhyj8R06roNUflhu9aHHZMAcvpuxP FZF+RSHe0xmBPG/8jldJF01ECIIK+CaQLr8AQWy30oV1RlnNNLvFQRTe2tptN6NgiCz0 PVw7nFWRCs5A/yrFfoiJWILofiBkT7nrMIH8KGoparAlM8S1JSD2lGn44BZ47UfpnuCC uPOa1FPS0I2r/zfzX2sS6RTMSvhcTT2nv2DZLCm9PX+SsfiKviEcDYd1VlIRI38UPV5i 4ibIc1vbsvhYfoKT5kXPVbvjQ6lfAswQflgwjI3gT9aSsblkzVxsHse0NBvLH7VoNo5M Un7w== X-Gm-Message-State: AOJu0YzQD3rEFJG2aO3nPtCXZzASxqGhKzDs/tH7EY62fL7mEH7CLuj0 hMavDhONZhItMG/TCIJRQzFblC/Ju5OwRaFMDCYgEGW1TB6l3dIc8PGeK7q7Kkn9JW79DkfKsnm QOQ== X-Google-Smtp-Source: AGHT+IGsWQbkvzY4Nq8NW+TxgBWjNbfzrSia5xRKbrBdsJJ8VjsmNkZOW2nxKqNkxRXVufnLriMmB7/RPeQ= X-Received: from plbkw12.prod.google.com ([2002:a17:902:f90c:b0:216:3e9d:6bc4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c406:b0:215:854c:a71a with SMTP id d9443c01a7336-219e6ebb68dmr11030635ad.34.1734658751894; Thu, 19 Dec 2024 17:39:11 -0800 (PST) Date: Thu, 19 Dec 2024 17:39:00 -0800 In-Reply-To: <20241220013906.3518334-1-seanjc@google.com> Mime-Version: 1.0 References: <20241220013906.3518334-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241220013906.3518334-3-seanjc@google.com> Subject: [PATCH 2/8] KVM: selftests: Close VM's binary stats FD when releasing VM From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241219_173913_613431_2E2D65BB X-CRM114-Status: GOOD ( 13.40 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Close/free a VM's binary stats cache when the VM is released, not when the VM is fully freed. When a VM is re-created, e.g. for state save/restore tests, the stats FD and descriptor points at the old, defunct VM. The FD is still valid, in that the underlying stats file won't be freed until the FD is closed, but reading stats will always pull information from the old VM. Note, this is a benign bug in the current code base as none of the tests that recreate VMs use binary stats. Fixes: 83f6e109f562 ("KVM: selftests: Cache binary stats metadata for duration of test") Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/lib/kvm_util.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 91d295ef5d02..9138801ecb60 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -709,6 +709,15 @@ void kvm_vm_release(struct kvm_vm *vmp) ret = close(vmp->kvm_fd); TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret)); + + /* Free cached stats metadata and close FD */ + if (vmp->stats_desc) { + free(vmp->stats_desc); + vmp->stats_desc = NULL; + + ret = close(vmp->stats_fd); + TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret)); + } } static void __vm_mem_region_delete(struct kvm_vm *vm, @@ -748,12 +757,6 @@ void kvm_vm_free(struct kvm_vm *vmp) if (vmp == NULL) return; - /* Free cached stats metadata and close FD */ - if (vmp->stats_desc) { - free(vmp->stats_desc); - close(vmp->stats_fd); - } - /* Free userspace_mem_regions. */ hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node) __vm_mem_region_delete(vmp, region); From patchwork Fri Dec 20 01:39:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13915992 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 82D34E77184 for ; Fri, 20 Dec 2024 01:44:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=C0bgCVLlKE2bsIas/97mE7OWeLqD3b90q0/zRCnz1gQ=; b=olVvBf7D+t5usVqjNE1XUPCmzF VY1rulBuNqXEeKHAJIznvc1anL9Z7bS7TKpWD3OtaRyEFHZWTO4wAYMvzNe0PceeMw/d6e3Um0fYZ uku0y8X+znv5CP69EKroCocgjuDNkiw5+BredzQh9HQzjed5vpgOVvS3ge6YGiyo0trL46+NIiFt0 WDBq1+CVOpQZy7+B/ZW6yiN1QF+zjDcAjsNM5giygC/DW190fxvjqMYEgf2zoy0bVxRaUSP6icHcv CtTEWuX+0eMis54JY/alL2VmHf7Xw9E2EzHv8oesFqal6MWvTImWONl9qiatfc9N57TzlFNomztja VWPu3RbA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tOS3T-00000003eVG-3bPJ; Fri, 20 Dec 2024 01:43:51 +0000 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tORz1-00000003cxi-0VOs for linux-arm-kernel@lists.infradead.org; Fri, 20 Dec 2024 01:39:16 +0000 Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-725cf25d47bso1339403b3a.2 for ; Thu, 19 Dec 2024 17:39:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734658754; x=1735263554; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=C0bgCVLlKE2bsIas/97mE7OWeLqD3b90q0/zRCnz1gQ=; b=yQHwZOwiLTSu24wt9+PED1v3hweTkB3Keuh6taUrSb98kszSmFh7vQMYgDA9c88lVc 8SomardDSt+3xtIBN0OxHOQwypTqZPQV1LHBinFZligJDJj3L90nFIjXA0EJZ6/sG9Bg cKqigGqod9pvk99AlWYjXa/XXKh4S9Kd03/BrZa3IEIeZx48JQEZSh4RWZPyDiMfJvt6 XvLFEdtwL6zbp7qTTtJgbaHhAXTPsxvSPieYok02I8Xr5UgMQESqsNtV2fxHf6/tevJ8 0FzKU5hHnU09JJE7lq7ZV4oA0vv3bdpOv9vnt1asOtZUO68c0cahX300ImLm8H5lUao1 65ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734658754; x=1735263554; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=C0bgCVLlKE2bsIas/97mE7OWeLqD3b90q0/zRCnz1gQ=; b=BXY6TEtQFvZybF4mBGk1eZBzWvq/MH6Mcqmfmi9xTMuTfKAZX4XHfS0HFSDPe1pQFi UufjWRdj/cwWzhyKd89PHhJXNADoRzcJXJpxjSBRqsJakAlzh8t5YpGETax6ST2xhhUg Ek5FJTMfd1sCTACvaQDR5+GiqW6H84dVoArYOno0kiVoYSrO9Ni931YRRjKG8tO6Lc8Y 2PHeuDEF9zSwf+L93D8SNQrfOW2FmUsmNTyWrUK5gx8wpclznhRTVsz9WoasUe8JpQsL yTSYh4jpBg3VuSYbAqifh69UIQxzgRR6cfDh20RuLYGDfpSD9ToefhAIrV+Dp1mp5SPC zPvQ== X-Gm-Message-State: AOJu0YxMa9Y3RgmBYknUz+tw3JVnytfTNtVoZKA2fK6/skgVaVfDlZ7X SKtmACdhJN69KApUDzpFxNGxLJu8uHkJtpK/wlUd4fvqu1gi+4/ybMT84fRuONwtRa/YqfGDJGK wiw== X-Google-Smtp-Source: AGHT+IEnCf3mHfBvLfwshZ15Q9EpX1HZcjdF08kZ2lrJwSlTx89Hx0AQSr2Fy6V/QWuDYbh+JhZtgEK5cgY= X-Received: from pfbbd38.prod.google.com ([2002:a05:6a00:27a6:b0:725:ccfc:fd85]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1942:b0:725:cfa3:bc6b with SMTP id d2e1a72fcca58-72abde404b0mr1283201b3a.3.1734658753719; Thu, 19 Dec 2024 17:39:13 -0800 (PST) Date: Thu, 19 Dec 2024 17:39:01 -0800 In-Reply-To: <20241220013906.3518334-1-seanjc@google.com> Mime-Version: 1.0 References: <20241220013906.3518334-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241220013906.3518334-4-seanjc@google.com> Subject: [PATCH 3/8] KVM: selftests: Assert that __vm_get_stat() actually finds a stat From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241219_173915_153623_A676916D X-CRM114-Status: GOOD ( 11.07 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Fail the test if it attempts to read a stat that doesn't exist, e.g. due to a typo, or because the test tried to get a stat for the wrong scope. As is, there's no indiciation of failure and @data is left untouched, e.g. holds '0' or random stack data in most cases. Fixes: 8448ec5993be ("KVM: selftests: Add NX huge pages test") Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 9138801ecb60..21b5a6261106 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -2238,9 +2238,10 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, read_stat_data(vm->stats_fd, &vm->stats_header, desc, data, max_elements); - - break; + return; } + + TEST_FAIL("Unabled to find stat '%s'", stat_name); } __weak void kvm_arch_vm_post_create(struct kvm_vm *vm) From patchwork Fri Dec 20 01:39:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13916007 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DD7D2E77184 for ; Fri, 20 Dec 2024 01:45:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=FOB7JN0OSt2j8FiVppNC5qzgljC2YrzarOO3DmltLC8=; b=dezIpBwLtZ39DsWyNoMD8ETqNi 0fmS0FQ6TUHKZizwvapauyzjFhn4TjYSEyID/1QsFi1XGaItg5U63adbgilcz0ryhZTJQOB3uPcuO 0/7uu7rGCV2xgAgqWFViR3BJ+Xa98PFjuYhEQdVF3XVAF0vdv5OvjyqlNkdBIU2pMQ99oy0BPAhGz UAMA6HoE5kbsfo+cIEgTwMcwIdVFqkA67PriYQzu6xjKbxef7W8YpabHcqtpD3rAi9OzlGxbPB53k aiSTmYhtbTX3XUvdfzub47x/DuADX1qqWhSJNZUYjfpJicxwc/y4CnPOjY3G/0dVqOKE3M2LabLVk Iu4cAsYA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tOS4b-00000003epC-2C4z; Fri, 20 Dec 2024 01:45:01 +0000 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tORz2-00000003cz5-2ST1 for linux-arm-kernel@lists.infradead.org; Fri, 20 Dec 2024 01:39:17 +0000 Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-72aa68f306fso1227577b3a.0 for ; Thu, 19 Dec 2024 17:39:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734658755; x=1735263555; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=FOB7JN0OSt2j8FiVppNC5qzgljC2YrzarOO3DmltLC8=; b=dqmpaYvsouZlmnoDUd5y+4P9GMHVvzfaPxrPv5bOvpSo/xf6LGELMSD+lWwEsFARSX vM1gHqbSbdY5BMCYshNI5u4Qbzpjc4QjNAGK6k1UXd696Wslo5Fm2RLPSFpC/DOR7Emb TQNUvB8d2nVepACaM+QxjOXfd9Sciyl4HgKBPv6kN14J2CLf2lHetJ+T6NnY3mbm0s9l DW6qQ7rdjHvW+WlVKxNeMawJy9VemWuR/6/mQ92DNERN6SnwSG5lxOZBgd3oxo2zF2jo i5vz2p/MC2LA0UksinKF+04wnWjufbQzvztkq4vOFePkMG4a1J0tdrW9Sgpvg03AlhgC YJRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734658755; x=1735263555; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FOB7JN0OSt2j8FiVppNC5qzgljC2YrzarOO3DmltLC8=; b=cj4/WNlM70+Lp/9CKlI5qgoBcovZ+u7s8BaIk+ygkrLwSYGw42dlFSw3G1P5U3B4Po ztKgBjCoH3MgAINM1Vp75ClZnJH/tcCoO3RoG872DG/U8xr0EzSiDlzf7NEHsqvT1A3Y Ln/9pQAvPFkvgM47xkG/tsoVESMpmWa9eUCF/wwrGz+xcfhrTxRE3KdwJE+92Hem/IRR pUlQtDfZcm/pnhb5JKOU7UT4FiWXqaRor8LYJ7oz1ySUHYx8XdhOzE+h/skIVAh7mUp6 S9DxexPacjdUUNzuIM3t3AcI43ORTRF+ZRE4QnUxOi1Idry++4RebzQfUxMaouXYQA1q qx0A== X-Gm-Message-State: AOJu0YwtY9wPHlWaXizAphIb6YQKbRtScSltUQbBtvbU7ye6Gg25PSc1 VOrnG9gHlwc0MnNrVHaY+SXcBaptknQpFSA5Wq7oNiX70s4TdvB5ykvXYHrgQIyfFBOwaa/QQ04 XBg== X-Google-Smtp-Source: AGHT+IGgztxRUxTUpaMUvL86uRz7hYZyu/zAmCoVzo4yvg4Q5SlFHbLBFYN3ktm59BGI0bTCiW14R+y2ITQ= X-Received: from pfbkt9.prod.google.com ([2002:a05:6a00:4ba9:b0:72a:bc54:8507]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:33a6:b0:1e5:c43f:f36d with SMTP id adf61e73a8af0-1e5e046189cmr1878060637.18.1734658755535; Thu, 19 Dec 2024 17:39:15 -0800 (PST) Date: Thu, 19 Dec 2024 17:39:02 -0800 In-Reply-To: <20241220013906.3518334-1-seanjc@google.com> Mime-Version: 1.0 References: <20241220013906.3518334-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241220013906.3518334-5-seanjc@google.com> Subject: [PATCH 4/8] KVM: selftests: Macrofy vm_get_stat() to auto-generate stat name string From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241219_173916_647935_94865BD2 X-CRM114-Status: GOOD ( 11.23 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Turn vm_get_stat() into a macro that generates a string for the stat name, as opposed to taking a string. This will allow hardening stat usage in the future to generate errors on unknown stats at compile time. No functional change intended. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/include/kvm_util.h | 14 +++++++------- .../kvm/x86/dirty_log_page_splitting_test.c | 6 +++--- .../testing/selftests/kvm/x86/nx_huge_pages_test.c | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 4c4e5a847f67..044c2231431e 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -534,13 +534,13 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, size_t max_elements); -static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) -{ - uint64_t data; - - __vm_get_stat(vm, stat_name, &data, 1); - return data; -} +#define vm_get_stat(vm, stat) \ +({ \ + uint64_t data; \ + \ + __vm_get_stat(vm, #stat, &data, 1); \ + data; \ +}) void vm_create_irqchip(struct kvm_vm *vm); diff --git a/tools/testing/selftests/kvm/x86/dirty_log_page_splitting_test.c b/tools/testing/selftests/kvm/x86/dirty_log_page_splitting_test.c index 2929c067c207..b0d2b04a7ff2 100644 --- a/tools/testing/selftests/kvm/x86/dirty_log_page_splitting_test.c +++ b/tools/testing/selftests/kvm/x86/dirty_log_page_splitting_test.c @@ -41,9 +41,9 @@ struct kvm_page_stats { static void get_page_stats(struct kvm_vm *vm, struct kvm_page_stats *stats, const char *stage) { - stats->pages_4k = vm_get_stat(vm, "pages_4k"); - stats->pages_2m = vm_get_stat(vm, "pages_2m"); - stats->pages_1g = vm_get_stat(vm, "pages_1g"); + stats->pages_4k = vm_get_stat(vm, pages_4k); + stats->pages_2m = vm_get_stat(vm, pages_2m); + stats->pages_1g = vm_get_stat(vm, pages_1g); stats->hugepages = stats->pages_2m + stats->pages_1g; pr_debug("\nPage stats after %s: 4K: %ld 2M: %ld 1G: %ld huge: %ld\n", diff --git a/tools/testing/selftests/kvm/x86/nx_huge_pages_test.c b/tools/testing/selftests/kvm/x86/nx_huge_pages_test.c index e7efb2b35f8b..c0d84827f736 100644 --- a/tools/testing/selftests/kvm/x86/nx_huge_pages_test.c +++ b/tools/testing/selftests/kvm/x86/nx_huge_pages_test.c @@ -73,7 +73,7 @@ static void check_2m_page_count(struct kvm_vm *vm, int expected_pages_2m) { int actual_pages_2m; - actual_pages_2m = vm_get_stat(vm, "pages_2m"); + actual_pages_2m = vm_get_stat(vm, pages_2m); TEST_ASSERT(actual_pages_2m == expected_pages_2m, "Unexpected 2m page count. Expected %d, got %d", @@ -84,7 +84,7 @@ static void check_split_count(struct kvm_vm *vm, int expected_splits) { int actual_splits; - actual_splits = vm_get_stat(vm, "nx_lpage_splits"); + actual_splits = vm_get_stat(vm, nx_lpage_splits); TEST_ASSERT(actual_splits == expected_splits, "Unexpected NX huge page split count. Expected %d, got %d", From patchwork Fri Dec 20 01:39:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13916008 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1284CE77184 for ; Fri, 20 Dec 2024 01:46:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=lQGXs4fRzhUjC9h6471rkcBzWgIFKg5z0MpBCPWHmMU=; b=0W7FhD+L6uF0sk0VIbGO/pX6YJ wtnYKPNlzWpbSuTiM0EHrzkPorx7PbSP8Kn/jYNR3MZ5QGQpWD/pdMqatMqc6HAp3i6mlwzXH9PzH qGWXkvMN1MgSQPrfbBxa9lle4E4pdCkYaZZTgoqo97FC93oMSp2H0cfRrGlmWk4dQ74Jwc7Vmmvhb 6N0DCmxlVgVyLGqDsVKdKMMdPe4RJQJDMpEL2b7if7iTZx/OfiohpZBLBlADjz340fYSzWwXjRHC9 u8qqQQ2JQCK3d1xNvYJeCF1pbXe6w8F9sb8kGkjD/S92pHs0YVDhS8EX5wHiqEzBsls+YEkbxSFx2 fOWXe4iQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tOS5h-00000003f5d-1XKV; Fri, 20 Dec 2024 01:46:09 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tORz4-00000003d0a-3Hqu for linux-arm-kernel@lists.infradead.org; Fri, 20 Dec 2024 01:39:20 +0000 Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-7fcb7afb4e1so1348137a12.0 for ; Thu, 19 Dec 2024 17:39:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734658757; x=1735263557; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=lQGXs4fRzhUjC9h6471rkcBzWgIFKg5z0MpBCPWHmMU=; b=Fdb/CW31FLRzWTePnE83ZWd4y7HyplmIcyZLT+WcN7t7aT1DC57/6DD/3CHetZR9Wj PjmU5tK93hVn1PyRXO9ZAyTnKO4OjfpWT/B5TWA8RRIPvLJsr/shQ7sTgE2JpJ8xcpPa deXaZDYT969Vnqo7uyRmk5byofJR1+LoyMYLuEfvm99pYL7XPLmD5YjmuW3utLan4Pw8 v0j9KmmvdDXarpa/aqtRn0YR0W9hfNLnQ7LuWG8sgGHoC7jlj98h34Ou9jxtoRSla2TX XXl/DRVT0rurfB4MjXNLYNnFjjWGX7V5xNzyMWnNZQiPmy72Exz9v1g604/wfMYhBGle Iqfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734658757; x=1735263557; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lQGXs4fRzhUjC9h6471rkcBzWgIFKg5z0MpBCPWHmMU=; b=UKO7V9iZpT7wGFEDHfv7OOsq91HABb2a+HigmZkuAdaQzrI8Ufm0pE825oIgSpiasn a5XmXc8FqE15vzaoyAJcqklOSW/audEAbrfkZybusIeA0g7uAdzGOI+DBe1wYO7twr9w shmVsnfzqLG5fqeLdyDHA4Vvi0oRTQAOj7H9vCsuDz9sjQXj3YXqryNlz3bGlzPPLjMi rRSy6QMgj9qVjlWTAcVAZyoJswz8Ep7sFkZShgvIK4OAluCfGQT+DIxJ5m2qdFt+cBiH WtLdaexayQ+oEIAHQ2lqgUHjecnaELSRxn9WzQPY7k53bbxKdfL7JEruODM9FT97TDSw /eLA== X-Gm-Message-State: AOJu0YyhPm5Z8jaZNyNFr0xfkRGFJxMd88U5k+x8ATKOsqLJE6QegUQb JAuwciRUTx6aHbR5dysFRN//5pynH0PGEfyBBGzy+znToDyhjTzJmrcDyZUKh5x/WfGb0Vi+4kM AsA== X-Google-Smtp-Source: AGHT+IGUByijU78nvHemXzOEaAQQDSmObXI7NYnb4v2toowk5S9287aNHUtx84WY5rqumwN+j1k67ZUHOWo= X-Received: from pfd7.prod.google.com ([2002:a05:6a00:a807:b0:727:2d74:d385]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:2d1f:b0:1e1:b1e4:e750 with SMTP id adf61e73a8af0-1e5e04717a2mr2014875637.18.1734658757426; Thu, 19 Dec 2024 17:39:17 -0800 (PST) Date: Thu, 19 Dec 2024 17:39:03 -0800 In-Reply-To: <20241220013906.3518334-1-seanjc@google.com> Mime-Version: 1.0 References: <20241220013906.3518334-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241220013906.3518334-6-seanjc@google.com> Subject: [PATCH 5/8] KVM: selftests: Add struct and helpers to wrap binary stats cache From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241219_173918_815960_57D3CF41 X-CRM114-Status: GOOD ( 16.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a struct and helpers to manage the binary stats cache, which is currently used only for VM-scoped stats. This will allow expanding the selftests infrastructure to provide support for vCPU-scoped binary stats, which, except for the ioctl to get the stats FD are identical to VM-scoped stats. Defer converting __vm_get_stat() to a scope-agnostic helper to a future patch, as getting the stats FD from KVM needs to be moved elsewhere before it can be made completely scope-agnostic. Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/include/kvm_util.h | 11 +++-- tools/testing/selftests/kvm/lib/kvm_util.c | 47 +++++++++++-------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 044c2231431e..9a64bab42f89 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -46,6 +46,12 @@ struct userspace_mem_region { struct hlist_node slot_node; }; +struct kvm_binary_stats { + int fd; + struct kvm_stats_header header; + struct kvm_stats_desc *desc; +}; + struct kvm_vcpu { struct list_head list; uint32_t id; @@ -99,10 +105,7 @@ struct kvm_vm { struct kvm_vm_arch arch; - /* Cache of information for binary stats interface */ - int stats_fd; - struct kvm_stats_header stats_header; - struct kvm_stats_desc *stats_desc; + struct kvm_binary_stats stats; /* * KVM region slots. These are the default memslots used by page diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 21b5a6261106..c88f5e7871f7 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -657,6 +657,20 @@ userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end) return NULL; } +static void kvm_stats_release(struct kvm_binary_stats *stats) +{ + int ret; + + if (!stats->desc) + return; + + free(stats->desc); + stats->desc = NULL; + + ret = close(stats->fd); + TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret)); +} + __weak void vcpu_arch_free(struct kvm_vcpu *vcpu) { @@ -711,13 +725,7 @@ void kvm_vm_release(struct kvm_vm *vmp) TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret)); /* Free cached stats metadata and close FD */ - if (vmp->stats_desc) { - free(vmp->stats_desc); - vmp->stats_desc = NULL; - - ret = close(vmp->stats_fd); - TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret)); - } + kvm_stats_release(&vmp->stats); } static void __vm_mem_region_delete(struct kvm_vm *vm, @@ -2214,34 +2222,33 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, * * Read the data values of a specified stat from the binary stats interface. */ -void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, +void __vm_get_stat(struct kvm_vm *vm, const char *name, uint64_t *data, size_t max_elements) { + struct kvm_binary_stats *stats = &vm->stats; struct kvm_stats_desc *desc; size_t size_desc; int i; - if (!vm->stats_desc) { - vm->stats_fd = vm_get_stats_fd(vm); - read_stats_header(vm->stats_fd, &vm->stats_header); - vm->stats_desc = read_stats_descriptors(vm->stats_fd, - &vm->stats_header); + if (!stats->desc) { + stats->fd = vm_get_stats_fd(vm); + read_stats_header(stats->fd, &stats->header); + stats->desc = read_stats_descriptors(stats->fd, &stats->header); } - size_desc = get_stats_descriptor_size(&vm->stats_header); + size_desc = get_stats_descriptor_size(&stats->header); - for (i = 0; i < vm->stats_header.num_desc; ++i) { - desc = (void *)vm->stats_desc + (i * size_desc); + for (i = 0; i < stats->header.num_desc; ++i) { + desc = (void *)stats->desc + (i * size_desc); - if (strcmp(desc->name, stat_name)) + if (strcmp(desc->name, name)) continue; - read_stat_data(vm->stats_fd, &vm->stats_header, desc, - data, max_elements); + read_stat_data(stats->fd, &stats->header, desc, data, max_elements); return; } - TEST_FAIL("Unabled to find stat '%s'", stat_name); + TEST_FAIL("Unabled to find stat '%s'", name); } __weak void kvm_arch_vm_post_create(struct kvm_vm *vm) From patchwork Fri Dec 20 01:39:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13916009 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F17E2E77184 for ; Fri, 20 Dec 2024 01:47:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=9d0sb2aXj7DAHSi3NyOXr17347erebt+0rHoY2hViJM=; b=o3jXQVm18mhQfmqv7+C/KPj3QN 11EGaa9HzLhToS5WmqIroGIcIMV7jEtOqPcjI+U13c2p5hC3Lu9jB+Fr8GfP9xRJgA01eL+M1Y/wE FDgzBYjbdEqYZBGpFAh5yQPNhk/lf68V1GnBqS2nGu7sfZy3gq2YJovnoiirI10dPTqrMY+IJL6Gz /J0pEGI5L2X+3gtEKK0xM6ogbFHBeY2ja6nDfnWsYxY9eSjIR+ilZDJl2ej9+AayTSoLz2z2fvG+j kqhYV+skjBYhhYvchA09sLWo5Hj0Un4f8QZQAQq/H/PpqvPOczI2MuUkY3kD2ynE5VpiiX5iN93t6 fm9a+dHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tOS6o-00000003fKA-3UlH; Fri, 20 Dec 2024 01:47:18 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tORz6-00000003d1m-0dNY for linux-arm-kernel@lists.infradead.org; Fri, 20 Dec 2024 01:39:21 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-728eda1754eso1968895b3a.2 for ; Thu, 19 Dec 2024 17:39:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734658759; x=1735263559; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=9d0sb2aXj7DAHSi3NyOXr17347erebt+0rHoY2hViJM=; b=Q0NYNkhVl8uJafBlK5AwmQ/zoSH3tzP5RV7UEAqrkWuTOyk9vpEIsBRT6ydbUhRlgq 7F0Rsu66y/CVxt0ICQSosYlEbi9FdcYNPZJzsz8Zkjcb7QbrOvJvonA4k8hC4pLUDgK8 tXtyctYQVRYuqnchCVY+V+pv5z+mLVVeGSsnsJwRKjc5okI/wyc9GL9wcAyoFGmPaN1t DyVJ0yBfRplSt8jEjJKXuViQnrtTvUC+9jp6hL3VeUADcRF21+F4GjcP/Lc+gZ64FCP8 WDuOuBEgkaId2uAAx1w2GJdI8600vExiC3qgx6DJTLQz4KgxVFiTBObxJQC6qPtU2cOO kIuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734658759; x=1735263559; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9d0sb2aXj7DAHSi3NyOXr17347erebt+0rHoY2hViJM=; b=HgXzZCG3QLt/1nGaUDsWL8IvFV5NPAbbneMCrXx9lNkdxJ6F0QOrsDThJ3cgf2BTal Z48+yu346Fh2OhxWRuO+Cm7x+SG1HK2ETfgN7PvF5vwxtD/CA47igpitIS8X2EYvfWii yJ5KKp+CQf+EhX9kLJKMcwDtwdiFteQMxOZL7HGwlBk5muwgdE6AxOhimqzleuiAXsbc nTwdUglMoNY4HxFMIZG3Jyo1awKxuAr2ZHP/Xan8PJcJzqZqzf8ZPXKocuCCTsj9R//5 nvEXRNU5Y2UGQcy/BxYIJbLwvVWpJgb1SPgfAhOQC1s3eCZU0IhQLJLv36Fb6fFbZbKT 2LhA== X-Gm-Message-State: AOJu0YzZ5zDx78Ey+cH7I7p3QcXW8ukeK39aIUsMAZaReAMcSzwMSipk diYh+M6BYxa6+woPDT9QO1nv9+zL8/Y8mZeNAsR2SfJ51QRDH8s+M0J5LtOEbH0Giie7J/KRqYK NTg== X-Google-Smtp-Source: AGHT+IFCVSOEBNw3Lv/7iuAoV97N3pDi0VWzw4l+AQLpoLja8FzKhTyc8mSa5fPmEkzN6L6pcIM/EKjTQ3o= X-Received: from pfbby5.prod.google.com ([2002:a05:6a00:4005:b0:725:e39e:1055]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3cc2:b0:71e:4cff:2654 with SMTP id d2e1a72fcca58-72abdd6eb8cmr1219933b3a.6.1734658759224; Thu, 19 Dec 2024 17:39:19 -0800 (PST) Date: Thu, 19 Dec 2024 17:39:04 -0800 In-Reply-To: <20241220013906.3518334-1-seanjc@google.com> Mime-Version: 1.0 References: <20241220013906.3518334-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241220013906.3518334-7-seanjc@google.com> Subject: [PATCH 6/8] KVM: selftests: Get VM's binary stats FD when opening VM From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241219_173920_189688_8D6FE307 X-CRM114-Status: GOOD ( 12.94 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Get and cache a VM's binary stats FD when the VM is opened, as opposed to waiting until the stats are first used. Opening the stats FD outside of __vm_get_stat() will allow converting it to a scope-agnostic helper. Note, this doesn't interfere with kvm_binary_stats_test's testcase that verifies a stats FD can be used after its own VM's FD is closed, as the cached FD is also closed during kvm_vm_free(). Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/lib/kvm_util.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index c88f5e7871f7..16ee03e76d66 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -196,6 +196,11 @@ static void vm_open(struct kvm_vm *vm) vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type); TEST_ASSERT(vm->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm->fd)); + + if (kvm_has_cap(KVM_CAP_BINARY_STATS_FD)) + vm->stats.fd = vm_get_stats_fd(vm); + else + vm->stats.fd = -1; } const char *vm_guest_mode_string(uint32_t i) @@ -661,14 +666,17 @@ static void kvm_stats_release(struct kvm_binary_stats *stats) { int ret; - if (!stats->desc) + if (stats->fd < 0) return; - free(stats->desc); - stats->desc = NULL; + if (stats->desc) { + free(stats->desc); + stats->desc = NULL; + } ret = close(stats->fd); TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret)); + stats->fd = -1; } __weak void vcpu_arch_free(struct kvm_vcpu *vcpu) @@ -2231,7 +2239,6 @@ void __vm_get_stat(struct kvm_vm *vm, const char *name, uint64_t *data, int i; if (!stats->desc) { - stats->fd = vm_get_stats_fd(vm); read_stats_header(stats->fd, &stats->header); stats->desc = read_stats_descriptors(stats->fd, &stats->header); } From patchwork Fri Dec 20 01:39:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13916010 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6B8FFE77184 for ; Fri, 20 Dec 2024 01:48:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=dH5y8/UE39Fu2msLM/JJYa9i5LZG9N1ZOJAUvzMfFnk=; b=IMlJZCu6V+7QVuo5fianAm//Aj oCAQprIplTgclREh83JK/0cmoPkXgQh2SaNBFjY4R2DAffxOlym9TjlKpPorXuVe8VLtaSapTUh9h vfI8wN5lafppC3IcR4DSkCW+Xhpr1xU+H8MNqlOIIx2Yqmn5SJXP2AIDcmx1cdjQUFEE4uCtfpNrt XrtxfwQvlVhX4YxWOb2J/MgRJaq94Zr6RKQeLpdueZkc09yskxVqKflzzz416AHzfgcOOkW2GDjy3 VPNrJC1DbNK7+RHA/umgPBLcQOpaxzqwPfPxy4f08ylZQUILLYufJUi9O+1SD3Iu8b0LnwXO69/zj URcDPzLg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tOS7v-00000003fT3-1q5B; Fri, 20 Dec 2024 01:48:27 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tORz8-00000003d30-1H9q for linux-arm-kernel@lists.infradead.org; Fri, 20 Dec 2024 01:39:23 +0000 Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2ee46799961so1741944a91.2 for ; Thu, 19 Dec 2024 17:39:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734658761; x=1735263561; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=dH5y8/UE39Fu2msLM/JJYa9i5LZG9N1ZOJAUvzMfFnk=; b=tMBPTXlk9L2I7L4k+z3dAYYBAU52qU2ghqkLMvrE9KwJGyk/uJ3Af9wHyRqVqgR9iv sY+7Im+G3/tCT4xBozWIlYoRAhIFrGHlVbHGCgoQ9e3yPwhw0/+XgfvArcNVbw9EGSk6 H6VDWX8q5Qep2UTJoPgPYWm+y5GepG+kofABsvUD1fhpuGiTAf7eHWTi3W8LH6fLlZjZ 4Ym3eLgg83Dk6URoDVZzEqrKlYzRwocv1qB+9fOQCX0rxfyPn5VKnX7woZGj7Te63t4o OdS94Z3igog2QFsoSmjem29OKsfATStZ69omqAg1Dqv6azDaifoNPXnKDPEq0dOhDSY/ Z19A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734658761; x=1735263561; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dH5y8/UE39Fu2msLM/JJYa9i5LZG9N1ZOJAUvzMfFnk=; b=rdR7/zER/Tjox6dOlfuPfBMwfDpyJsrblyQYzi53z6wivWykSkB3r5O8MXlpEoA0LS PUSU/io7WQAVXlaNilxxeCtZs6tqieDCGUgzrtT+K/i64N10fY+8UyZkzeLzefD4SGdB fOJ/o0J5UP/uxbLUDUaYuxaQSgK2Yigfkq4T23aaS4D/S2e3+m5fJt6pcnLaCWbOkeWb a/V6sv+oTCfg6J3hbZ0115/0h26ANLvOexjTE95+73UseuGs5Ifevb87ZCnnmJTvB4tJ 2APPk39Uyf8fDBXW/gBi+fwhvIxqyoHfnvLGKmt67xtk5yRABkeF+/E7KQEFMf5cYZc+ wZEg== X-Gm-Message-State: AOJu0YygKZ2W6A//5WaEinpxKAxwPPSz3owQL+lO0K70W10g6Ft1RJQn D5Fo+W4wFdkyUXR0y2DspMRP0IhISuVJ16AUxN+X4zMgE1HEJKTdCOxEs16PUk9WemRy2Srbjim Xjw== X-Google-Smtp-Source: AGHT+IEWDX/fMBQN37LNDm8pwZAbb4nPz2A12lsLKXmgW0NfsvdhBH23GcxtGNe29UPGQ7kGMBybcOA6PXg= X-Received: from pjbqn15.prod.google.com ([2002:a17:90b:3d4f:b0:2ef:8612:dd6b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:e18b:b0:2ee:9902:18b4 with SMTP id 98e67ed59e1d1-2f452ee838bmr1687135a91.27.1734658760980; Thu, 19 Dec 2024 17:39:20 -0800 (PST) Date: Thu, 19 Dec 2024 17:39:05 -0800 In-Reply-To: <20241220013906.3518334-1-seanjc@google.com> Mime-Version: 1.0 References: <20241220013906.3518334-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241220013906.3518334-8-seanjc@google.com> Subject: [PATCH 7/8] KVM: selftests: Add infrastructure for getting vCPU binary stats From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241219_173922_345691_EBF5620D X-CRM114-Status: GOOD ( 16.67 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Now that the binary stats cache infrastructure is largely scope agnostic, add support for vCPU-scoped stats. Like VM stats, open and cache the stats FD when the vCPU is created so that it's guaranteed to be valid when vcpu_get_stats() is invoked. To sanity check that the infrastructure actually works, and to get a bit of bonus coverage, add an assert in x86's xapic_ipi_test to verify that the number of HLTs executed by the test matches the number of HLT exits observed by KVM. Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/include/kvm_util.h | 20 +++++++++------ tools/testing/selftests/kvm/lib/kvm_util.c | 25 +++++++------------ .../selftests/kvm/x86/xapic_ipi_test.c | 2 ++ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 9a64bab42f89..e0d23873158e 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -61,6 +61,7 @@ struct kvm_vcpu { #ifdef __x86_64__ struct kvm_cpuid2 *cpuid; #endif + struct kvm_binary_stats stats; struct kvm_dirty_gfn *dirty_gfns; uint32_t fetch_index; uint32_t dirty_gfns_count; @@ -534,17 +535,20 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, struct kvm_stats_desc *desc, uint64_t *data, size_t max_elements); -void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, - size_t max_elements); +void kvm_get_stat(struct kvm_binary_stats *stats, const char *name, + uint64_t *data, size_t max_elements); -#define vm_get_stat(vm, stat) \ -({ \ - uint64_t data; \ - \ - __vm_get_stat(vm, #stat, &data, 1); \ - data; \ +#define __get_stat(stats, stat) \ +({ \ + uint64_t data; \ + \ + kvm_get_stat(stats, #stat, &data, 1); \ + data; \ }) +#define vm_get_stat(vm, stat) __get_stat(&(vm)->stats, stat) +#define vcpu_get_stat(vcpu, stat) __get_stat(&(vcpu)->stats, stat) + void vm_create_irqchip(struct kvm_vm *vm); static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 16ee03e76d66..99fe8bcd2346 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -712,6 +712,8 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu) ret = close(vcpu->fd); TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret)); + kvm_stats_release(&vcpu->stats); + list_del(&vcpu->list); vcpu_arch_free(vcpu); @@ -1305,6 +1307,11 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) TEST_ASSERT(vcpu->run != MAP_FAILED, __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED)); + if (kvm_has_cap(KVM_CAP_BINARY_STATS_FD)) + vcpu->stats.fd = vcpu_get_stats_fd(vcpu); + else + vcpu->stats.fd = -1; + /* Add to linked-list of VCPUs. */ list_add(&vcpu->list, &vm->vcpus); @@ -2217,23 +2224,9 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, desc->name, size, ret); } -/* - * Read the data of the named stat - * - * Input Args: - * vm - the VM for which the stat should be read - * stat_name - the name of the stat to read - * max_elements - the maximum number of 8-byte values to read into data - * - * Output Args: - * data - the buffer into which stat data should be read - * - * Read the data values of a specified stat from the binary stats interface. - */ -void __vm_get_stat(struct kvm_vm *vm, const char *name, uint64_t *data, - size_t max_elements) +void kvm_get_stat(struct kvm_binary_stats *stats, const char *name, + uint64_t *data, size_t max_elements) { - struct kvm_binary_stats *stats = &vm->stats; struct kvm_stats_desc *desc; size_t size_desc; int i; diff --git a/tools/testing/selftests/kvm/x86/xapic_ipi_test.c b/tools/testing/selftests/kvm/x86/xapic_ipi_test.c index a76078a08ff8..574a944763b7 100644 --- a/tools/testing/selftests/kvm/x86/xapic_ipi_test.c +++ b/tools/testing/selftests/kvm/x86/xapic_ipi_test.c @@ -465,6 +465,8 @@ int main(int argc, char *argv[]) cancel_join_vcpu_thread(threads[0], params[0].vcpu); cancel_join_vcpu_thread(threads[1], params[1].vcpu); + TEST_ASSERT_EQ(data->hlt_count, vcpu_get_stat(params[0].vcpu, halt_exits)); + fprintf(stderr, "Test successful after running for %d seconds.\n" "Sending vCPU sent %lu IPIs to halting vCPU\n" From patchwork Fri Dec 20 01:39:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13916079 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C8813E77184 for ; Fri, 20 Dec 2024 02:55:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Content-Transfer-Encoding:Content-Type:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Dg8fmjMM2o4ZdUzcCM3IsD7JuuynFC9AcsVyNRsKeBU=; b=OeWSbI4goKexL+ vmjsWOcHKe43jCyWyYFIghrxt3ic7KYySYdIn9oXUBDvonKvqb6iHhQ3eCFoSyE8WUSpQYgsKcwju aRvLlhyTgVBA9ALaA3IErfDV/NymqB3fDbR1gMpO6c59oHGHXGDtAEKkEoQlRazFQu5VFDdPUFxTC ra2W7DxzJqg0EQXgyRowd8hZFlSgzjtr9f79mSpMtGDnqGf1cwQlLhXoQWdx1aWdmpUkk4G+Uy1NA QHGdsK6QW0orR6+t4wNVywj7Nvt6AFWE6+zkoPD0sYIaJA/R6h2DAHQL/QPNpZSUbx8s3zE7Qu32I ETGynHsXJaic5smTq+xA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tOTAi-00000003kxV-2SSP; Fri, 20 Dec 2024 02:55:24 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tORz9-00000003d4i-3ycS for linux-arm-kernel@lists.infradead.org; Fri, 20 Dec 2024 01:39:25 +0000 Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-7eaac1e95ffso910932a12.2 for ; Thu, 19 Dec 2024 17:39:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734658763; x=1735263563; darn=lists.infradead.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:from:to:cc:subject:date :message-id:reply-to; bh=Dg8fmjMM2o4ZdUzcCM3IsD7JuuynFC9AcsVyNRsKeBU=; b=VQW/HJbM8Mz3bqURjdEqVzHJuOW1beKe8KMUVYYgBUpoEbekOp/Ttp7z2fryLQc+LG 4in6vEvTHVW2VYA4cfIbEhCY3fV4/GZUb3Gj68yUr0dWYkJKo2IMitVgjAnVie9Hkb/K Y0m/87mwKYYfISMAzBHEaBa9eneVxh7oVFXOhPOft93KBKvk/amOgS95AMV8sa4jPRmU W0JLxmP0mCnsLsAR1ju51a68sh2No9hzfFw/NlfACNiFK3FDQk5LSWIiTuB00iayF+Uy 0dcGj3kZen7vV57o7Ls0brJ2o3QEcfgTUC8mO86uvgxOzre2kNGseopFJFfBSEOTddpj sf/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734658763; x=1735263563; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Dg8fmjMM2o4ZdUzcCM3IsD7JuuynFC9AcsVyNRsKeBU=; b=YWh0irqHlMrU8ZWt4QrfY9FRtQdC5tnD/4Ww9oCX12+i2d+dSY/wh23PEg64wHOfVd ko3yzct26uokrTT58qa7JWipuGHe8S7HXHQzJUdmm3YrE53pERqQ0FV1B3Dr/Op/HYKF 0Okq77UvpleQeRx2EysSiddnJ/IHF/V2UKa8XCf5DpU3n6j12cobbnlw4Nvq03bO4/no 0HILYiU4YjlL/9+JG/LmKUUz/54M/B7UcICcIYfXvHYIkHlSO6Eqmj1GX/Mww24qijnf ll048RtESZI4p7Xo0YDgxdO8GOVZTtK6CKG78KMaaMj/HXGSgK2/O+EWGO4hS1cvMV+k AIJA== X-Gm-Message-State: AOJu0YwNB6aA/QRdfx/dCmyPMKdj98rx2kxAkx8LeOiW6Cm3MMv9+E22 yTtPM5tmapWT1bwcfsoxTXoO6z4tK9tALDeZ73Oxaa4GOv2IiI3uNbeh/qGx8p7IfNQ7kK9zbDv U9A== X-Google-Smtp-Source: AGHT+IE6EA1Soh2HbsBhs8mA4Cxp5/kcHpg0QYYkszskXT1G6lUfvmxNE+q8q9njljny5qS/JMm3vKiIjQ4= X-Received: from pgbbw29.prod.google.com ([2002:a05:6a02:49d:b0:801:d768:c174]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6300:6681:b0:1e0:c6c0:1e1f with SMTP id adf61e73a8af0-1e5e07f8a98mr2122068637.36.1734658762701; Thu, 19 Dec 2024 17:39:22 -0800 (PST) Date: Thu, 19 Dec 2024 17:39:06 -0800 In-Reply-To: <20241220013906.3518334-1-seanjc@google.com> Mime-Version: 1.0 References: <20241220013906.3518334-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241220013906.3518334-9-seanjc@google.com> Subject: [PATCH 8/8] KVM: selftests: Add compile-time assertions to guard against stats typos From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241219_173924_028198_97AA23E4 X-CRM114-Status: GOOD ( 14.61 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add compile-time assertions to the main binary stats accessor to verify the stat is a known KVM stat of the appropriate scope, and "add" all known stats for all architectures. To build the set of known stats, define enums for each stat, with a completely arbitrary magic value to specify the scope of the stat. With the assert in place, misuse of stat (or usage of a new stat) generates error like so: In file included from include/x86/kvm_util_arch.h:8, from include/kvm_util.h:23, from x86/dirty_log_page_splitting_test.c:16: x86/dirty_log_page_splitting_test.c: In function ‘get_page_stats’: include/kvm_util.h:563:42: error: ‘VM_STAT_pages_4m’ undeclared (first use in this function); did you mean ‘VM_STAT_pages_2m’? 563 | #define vm_get_stat(vm, stat) __get_stat(VM, &(vm)->stats, stat) | ^~ ... x86/dirty_log_page_splitting_test.c:45:27: note: in expansion of macro ‘vm_get_stat’ 45 | stats->pages_2m = vm_get_stat(vm, pages_4m); | ^~~~~~~~~~~ Using pre-defined lists of stats doesn't completely eliminate human error, e.g. it's obviously possible to make a typo when adding a state. And while there is also a non-zero cost to maintaining the set of stats, adding stats in KVM is relatively uncommon, and removing stats is extremely rare. On the flip side, providing a list of known stats should make it easier to use stats in test, at which point detecting goofs at compile-time will also be more valuable. Signed-off-by: Sean Christopherson --- .../kvm/include/arm64/kvm_util_arch.h | 12 ++ .../testing/selftests/kvm/include/kvm_util.h | 24 +++- .../selftests/kvm/include/kvm_util_types.h | 6 + .../kvm/include/riscv/kvm_util_arch.h | 14 +++ .../kvm/include/s390/kvm_util_arch.h | 113 ++++++++++++++++++ .../selftests/kvm/include/x86/kvm_util_arch.h | 52 ++++++++ 6 files changed, 218 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/include/arm64/kvm_util_arch.h b/tools/testing/selftests/kvm/include/arm64/kvm_util_arch.h index e43a57d99b56..12097262f585 100644 --- a/tools/testing/selftests/kvm/include/arm64/kvm_util_arch.h +++ b/tools/testing/selftests/kvm/include/arm64/kvm_util_arch.h @@ -2,6 +2,18 @@ #ifndef SELFTEST_KVM_UTIL_ARCH_H #define SELFTEST_KVM_UTIL_ARCH_H +#include "kvm_util_types.h" + struct kvm_vm_arch {}; +enum kvm_arm64_stats { + VCPU_STAT(hvc_exit_stat), + VCPU_STAT(wfe_exit_stat), + VCPU_STAT(wfi_exit_stat), + VCPU_STAT(mmio_exit_user), + VCPU_STAT(mmio_exit_kernel), + VCPU_STAT(signal_exits), + VCPU_STAT(exits), +}; + #endif // SELFTEST_KVM_UTIL_ARCH_H diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index e0d23873158e..f4f0e27cea27 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -535,19 +535,37 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header, struct kvm_stats_desc *desc, uint64_t *data, size_t max_elements); +enum kvm_common_stats { + VM_STAT(remote_tlb_flush), + VM_STAT(remote_tlb_flush_requests), + + VCPU_STAT(halt_successfull_poll), + VCPU_STAT(halt_attempted_poll), + VCPU_STAT(halt_poll_invalid), + VCPU_STAT(halt_wakeup), + VCPU_STAT(halt_poll_success_ns), + VCPU_STAT(halt_poll_fail_ns), + VCPU_STAT(halt_wait_ns), + VCPU_STAT(halt_poll_success_hist), + VCPU_STAT(halt_poll_fail_hist), + VCPU_STAT(halt_wait_hist), + VCPU_STAT(blocking), +}; + void kvm_get_stat(struct kvm_binary_stats *stats, const char *name, uint64_t *data, size_t max_elements); -#define __get_stat(stats, stat) \ +#define __get_stat(type, stats, stat) \ ({ \ uint64_t data; \ \ + kvm_static_assert(type##_STAT_##stat == type##_STAT_MAGIC_NUMBER); \ kvm_get_stat(stats, #stat, &data, 1); \ data; \ }) -#define vm_get_stat(vm, stat) __get_stat(&(vm)->stats, stat) -#define vcpu_get_stat(vcpu, stat) __get_stat(&(vcpu)->stats, stat) +#define vm_get_stat(vm, stat) __get_stat(VM, &(vm)->stats, stat) +#define vcpu_get_stat(vcpu, stat) __get_stat(VCPU, &(vcpu)->stats, stat) void vm_create_irqchip(struct kvm_vm *vm); diff --git a/tools/testing/selftests/kvm/include/kvm_util_types.h b/tools/testing/selftests/kvm/include/kvm_util_types.h index ec787b97cf18..20e6717a0d24 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_types.h +++ b/tools/testing/selftests/kvm/include/kvm_util_types.h @@ -17,4 +17,10 @@ typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */ +#define VM_STAT_MAGIC_NUMBER 1 +#define VM_STAT(stat) VM_STAT_##stat = VM_STAT_MAGIC_NUMBER + +#define VCPU_STAT_MAGIC_NUMBER 2 +#define VCPU_STAT(stat) VCPU_STAT_##stat = VCPU_STAT_MAGIC_NUMBER + #endif /* SELFTEST_KVM_UTIL_TYPES_H */ diff --git a/tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h b/tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h index e43a57d99b56..ea53d6aeb693 100644 --- a/tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h +++ b/tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h @@ -2,6 +2,20 @@ #ifndef SELFTEST_KVM_UTIL_ARCH_H #define SELFTEST_KVM_UTIL_ARCH_H +#include "kvm_util_types.h" + struct kvm_vm_arch {}; +enum kvm_riscv_stats { + VCPU_STAT(ecall_exit_stat), + VCPU_STAT(wfi_exit_stat), + VCPU_STAT(wrs_exit_stat), + VCPU_STAT(mmio_exit_user), + VCPU_STAT(mmio_exit_kernel), + VCPU_STAT(csr_exit_user), + VCPU_STAT(csr_exit_kernel), + VCPU_STAT(signal_exits), + VCPU_STAT(exits), +}; + #endif // SELFTEST_KVM_UTIL_ARCH_H diff --git a/tools/testing/selftests/kvm/include/s390/kvm_util_arch.h b/tools/testing/selftests/kvm/include/s390/kvm_util_arch.h index e43a57d99b56..64d4de333e09 100644 --- a/tools/testing/selftests/kvm/include/s390/kvm_util_arch.h +++ b/tools/testing/selftests/kvm/include/s390/kvm_util_arch.h @@ -2,6 +2,119 @@ #ifndef SELFTEST_KVM_UTIL_ARCH_H #define SELFTEST_KVM_UTIL_ARCH_H +#include "kvm_util_types.h" + struct kvm_vm_arch {}; +enum kvm_s390_stats { + VM_STAT(inject_io), + VM_STAT(inject_float_mchk), + VM_STAT(inject_pfault_done), + VM_STAT(inject_service_signal), + VM_STAT(inject_virtio), + VM_STAT(aen_forward), + VM_STAT(gmap_shadow_reuse), + VM_STAT(gmap_shadow_create), + VM_STAT(gmap_shadow_r1_entry), + VM_STAT(gmap_shadow_r2_entry), + VM_STAT(gmap_shadow_r3_entry), + VM_STAT(gmap_shadow_sg_entry), + VM_STAT(gmap_shadow_pg_entry), + + VCPU_STAT(exit_userspace), + VCPU_STAT(exit_null), + VCPU_STAT(exit_external_request), + VCPU_STAT(exit_io_request), + VCPU_STAT(exit_external_interrupt), + VCPU_STAT(exit_stop_request), + VCPU_STAT(exit_validity), + VCPU_STAT(exit_instruction), + VCPU_STAT(exit_pei), + VCPU_STAT(halt_no_poll_steal), + VCPU_STAT(instruction_lctl), + VCPU_STAT(instruction_lctlg), + VCPU_STAT(instruction_stctl), + VCPU_STAT(instruction_stctg), + VCPU_STAT(exit_program_interruption), + VCPU_STAT(exit_instr_and_program), + VCPU_STAT(exit_operation_exception), + VCPU_STAT(deliver_ckc), + VCPU_STAT(deliver_cputm), + VCPU_STAT(deliver_external_call), + VCPU_STAT(deliver_emergency_signal), + VCPU_STAT(deliver_service_signal), + VCPU_STAT(deliver_virtio), + VCPU_STAT(deliver_stop_signal), + VCPU_STAT(deliver_prefix_signal), + VCPU_STAT(deliver_restart_signal), + VCPU_STAT(deliver_program), + VCPU_STAT(deliver_io), + VCPU_STAT(deliver_machine_check), + VCPU_STAT(exit_wait_state), + VCPU_STAT(inject_ckc), + VCPU_STAT(inject_cputm), + VCPU_STAT(inject_external_call), + VCPU_STAT(inject_emergency_signal), + VCPU_STAT(inject_mchk), + VCPU_STAT(inject_pfault_init), + VCPU_STAT(inject_program), + VCPU_STAT(inject_restart), + VCPU_STAT(inject_set_prefix), + VCPU_STAT(inject_stop_signal), + VCPU_STAT(instruction_epsw), + VCPU_STAT(instruction_gs), + VCPU_STAT(instruction_io_other), + VCPU_STAT(instruction_lpsw), + VCPU_STAT(instruction_lpswe), + VCPU_STAT(instruction_lpswey), + VCPU_STAT(instruction_pfmf), + VCPU_STAT(instruction_ptff), + VCPU_STAT(instruction_sck), + VCPU_STAT(instruction_sckpf), + VCPU_STAT(instruction_stidp), + VCPU_STAT(instruction_spx), + VCPU_STAT(instruction_stpx), + VCPU_STAT(instruction_stap), + VCPU_STAT(instruction_iske), + VCPU_STAT(instruction_ri), + VCPU_STAT(instruction_rrbe), + VCPU_STAT(instruction_sske), + VCPU_STAT(instruction_ipte_interlock), + VCPU_STAT(instruction_stsi), + VCPU_STAT(instruction_stfl), + VCPU_STAT(instruction_tb), + VCPU_STAT(instruction_tpi), + VCPU_STAT(instruction_tprot), + VCPU_STAT(instruction_tsch), + VCPU_STAT(instruction_sie), + VCPU_STAT(instruction_essa), + VCPU_STAT(instruction_sthyi), + VCPU_STAT(instruction_sigp_sense), + VCPU_STAT(instruction_sigp_sense_running), + VCPU_STAT(instruction_sigp_external_call), + VCPU_STAT(instruction_sigp_emergency), + VCPU_STAT(instruction_sigp_cond_emergency), + VCPU_STAT(instruction_sigp_start), + VCPU_STAT(instruction_sigp_stop), + VCPU_STAT(instruction_sigp_stop_store_status), + VCPU_STAT(instruction_sigp_store_status), + VCPU_STAT(instruction_sigp_store_adtl_status), + VCPU_STAT(instruction_sigp_arch), + VCPU_STAT(instruction_sigp_prefix), + VCPU_STAT(instruction_sigp_restart), + VCPU_STAT(instruction_sigp_init_cpu_reset), + VCPU_STAT(instruction_sigp_cpu_reset), + VCPU_STAT(instruction_sigp_unknown), + VCPU_STAT(instruction_diagnose_10), + VCPU_STAT(instruction_diagnose_44), + VCPU_STAT(instruction_diagnose_9c), + VCPU_STAT(diag_9c_ignored), + VCPU_STAT(diag_9c_forward), + VCPU_STAT(instruction_diagnose_258), + VCPU_STAT(instruction_diagnose_308), + VCPU_STAT(instruction_diagnose_500), + VCPU_STAT(instruction_diagnose_other), + VCPU_STAT(pfault_sync), +}; + #endif // SELFTEST_KVM_UTIL_ARCH_H diff --git a/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h b/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h index 972bb1c4ab4c..f9c4aedddbd0 100644 --- a/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h +++ b/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h @@ -48,4 +48,56 @@ do { \ } \ } while (0) +enum kvm_x86_stats { + VM_STAT(mmu_shadow_zapped), + VM_STAT(mmu_pte_write), + VM_STAT(mmu_pde_zapped), + VM_STAT(mmu_flooded), + VM_STAT(mmu_recycled), + VM_STAT(mmu_cache_miss), + VM_STAT(mmu_unsync), + VM_STAT(pages_4k), + VM_STAT(pages_2m), + VM_STAT(pages_1g), + VM_STAT(pages), + VM_STAT(nx_lpage_splits), + VM_STAT(max_mmu_page_hash_collisions), + VM_STAT(max_mmu_rmap_size), + + VCPU_STAT(pf_taken), + VCPU_STAT(pf_fixed), + VCPU_STAT(pf_emulate), + VCPU_STAT(pf_spurious), + VCPU_STAT(pf_fast), + VCPU_STAT(pf_mmio_spte_created), + VCPU_STAT(pf_guest), + VCPU_STAT(tlb_flush), + VCPU_STAT(invlpg), + VCPU_STAT(exits), + VCPU_STAT(io_exits), + VCPU_STAT(mmio_exits), + VCPU_STAT(signal_exits), + VCPU_STAT(irq_window_exits), + VCPU_STAT(nmi_window_exits), + VCPU_STAT(l1d_flush), + VCPU_STAT(halt_exits), + VCPU_STAT(request_irq_exits), + VCPU_STAT(irq_exits), + VCPU_STAT(host_state_reload), + VCPU_STAT(fpu_reload), + VCPU_STAT(insn_emulation), + VCPU_STAT(insn_emulation_fail), + VCPU_STAT(hypercalls), + VCPU_STAT(irq_injections), + VCPU_STAT(nmi_injections), + VCPU_STAT(req_event), + VCPU_STAT(nested_run), + VCPU_STAT(directed_yield_attempted), + VCPU_STAT(directed_yield_successful), + VCPU_STAT(preemption_reported), + VCPU_STAT(preemption_other), + VCPU_STAT(guest_mode), + VCPU_STAT(notify_window_exits), +}; + #endif // SELFTEST_KVM_UTIL_ARCH_H