From patchwork Sat Jan 11 00:50:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13935671 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4DE31487D5 for ; Sat, 11 Jan 2025 00:51:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736556663; cv=none; b=gdyT/hbaSYbeyxedqpFEu30Hi7YkxHoPEWomovgopjv4AX2X0QMjWwznAxVWVgGZHjoRbgOZi5CTexI5fDxCHnELBfnuG+OwYRoI7vnXAwWYBcOkiIpHE+wdM4uvvFdXVgl0krBlXl1cqnGeUIAdlkJAsoIa/9hZkNhR2+2CPZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736556663; c=relaxed/simple; bh=eeF/zKkticJHl5FU2ydo7jdEXM5cm9h9vTJoDSn8JMo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NLWc6geBTgl/YsCsVaihPr+qNSULJG6aBYuemRAmRZEOJGqCjfYre5QErcdcFrt5b8zGgAwTtz7hkhYjoSiftFLqIaTCfaNhCePkFvAmuDMzGDVwhxWNdRMr+6m39IIqh2qwqV/B8r5H02a7X/01rG4rGnhIPh5TKEcI0eJkx6U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DCHytbGt; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DCHytbGt" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f2a9f056a8so4815128a91.2 for ; Fri, 10 Jan 2025 16:51:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736556660; x=1737161460; darn=vger.kernel.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=DCHytbGtIm8nYIwCQSd144BIs6PoBRV3V4F2VQnJC+2Sgi44aJsWEZSjitAcAdAQE6 WuT0+RmZDS1ZmtoW4kORn5DPbGA9MCmr8XdrMD9j+IV8mHCS+DHCiWG+88vvYkV0ysEi pozXJ2sR+PSGWDT7VKODnyxois0PQpvjEEe2oHnGFhdxgxx2Cmdwg6dJLZKFDMn2jmE1 8lNrqyBxacq2ezW/hE0C0Rl0lLOuU4DyxLVvDNIb+ihle78ym7vLHXH5AUqmQ5YeyTy7 I/kkqkWS0+E+YHMnbSxAptZNx63ND5FsrvBiDM7376DY6NL0FED95X9tRhauwYKI4hXW x8WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736556660; x=1737161460; 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=J2rRGz/rJq/VonZDU18TqnxcqvAv/8WhYCTNlnMjjYJinOJ9oEM7eJthd5oVpzUS1z y1nPUaeWtYQASrFinqB/zPX9hgy64P40puCdyL6F3dMDgl3OTiMP/8apVkiCjHjStsNz w1cvqBA+o/aCgI+cIzOlxSEQTMXBUoo8Dznk3u3DmKlmwA+/xXRjZdRBzQAEYvqHHFUE 5tFWCTM8McoHliOzcCxUwdRx0RNZcRkh+vNQlEmueULskupDYVYquwSgAEygFMkhz3+b slLlEA92LyhsuUa9mSVpODdXhvY1sOgNAgwiG6J8qzIYzI3+1h4kYtYVZsT9DIaLrDtg aryw== X-Forwarded-Encrypted: i=1; AJvYcCULvicNxiIFNWS1nabtVhe60q6j2GNyvDC/Fw3rOAqyLme+s4zJ9kGfGQVGATZcK3AEZwk=@vger.kernel.org X-Gm-Message-State: AOJu0Yy3bvob6U2uqdyhMeqK6DwgZgLWOyTI/UuSkccXIu7bVPWC2ntz vxP1vgnF3mE8IK7hPd395+YBvzoB85A4UJ/KHZCoRcDOIWhc3n66x3TegQXCIiwXxy3q5qyK+3+ xoA== X-Google-Smtp-Source: AGHT+IFkTOLXQinvGI8s6GviRk7XKu9XJa8etcXjW5yAd+8/VTBQJ+NHE0WBibUOfLnKYqGfPAkdUeiGwEY= X-Received: from pjbqd11.prod.google.com ([2002:a17:90b:3ccb:b0:2e5:8726:a956]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:dfcc:b0:2ee:ad18:b30d with SMTP id 98e67ed59e1d1-2f548e98f31mr17356212a91.6.1736556660239; Fri, 10 Jan 2025 16:51:00 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:50:45 -0800 In-Reply-To: <20250111005049.1247555-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111005049.1247555-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111005049.1247555-6-seanjc@google.com> Subject: [PATCH v2 5/9] 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 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)