From patchwork Fri Aug 4 00:42:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13341036 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 BA64CC001DF for ; Fri, 4 Aug 2023 00:43:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Reply-To:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id: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=5xTB7F04U1EfkXzCnU0xUotCPsjk/cnQDBk4gpvQh90=; b=dm9kJL8eevAGDc wWGkJBi6DfX+lFHNGXsoYO6g4PqHPCMbUHBKrmQyzEb4Mklaivkp2cWcOMIdxQkO54GTzWdiNbUVB EyLQMk9KTJ675CGNj+lVsHE0TVNTXdBt0D8TiZEy8Eh9lWQe2SztrQi0YNAb0+vn/KZVHpFolejRC 27doAevDisRUAAoX3J1yWHFkDpMzZyTZ8ATDSy4hBmn/5hkug1lIUrUBSpQ2+RoVuaTV323LxQvI2 0fi4HiwN9Wu2p+wJ7pUYE/WTH5Q8z7/zeSSCpGmda2sofzgnXNTynX0pfvUjlFs+Mi9SmRL4lxaSx tXouWoSQDxj3F0ta2v6A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qRitt-00BBSh-1x; Fri, 04 Aug 2023 00:42:41 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qRito-00BBQM-0i for linux-arm-kernel@lists.infradead.org; Fri, 04 Aug 2023 00:42:37 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-569e7aec37bso16741227b3.2 for ; Thu, 03 Aug 2023 17:42:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691109752; x=1691714552; 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=zoisc0YB1XdzgL3Mform49pg3ZfKIVNUNR7ICgYhzqE=; b=a4GYzzIGruxXUnQRulR7Q7xPVPhglwcMI8dSt+2D0nS1HKiyVkOdEdTA22kHKgAdy0 HTMrJvInhxVscUAT2jGAxD6GjetlDBEEjRbs5tgpCurFNtDO9srlbkGATO8A+j84QHPn LpTXCF+c1pA439z77TTyZdsi633JbNMsRox+apr+ntnTHhKnkysLOKyzwZ5Q5pXp59tr gtO1qSFLzwPBSFZOp5ceRlT+y/Fr2xvWXpQcJIcBI/Eu/Nnf77Qul2yHoNvPK84le3qc yZvzpyWSa2x/ElmpM+L8ZLi/IuMFNTuqvQUk6U2py9hi+gOnewe10HzpDZCYVmiCJipg Lt6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691109752; x=1691714552; 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=zoisc0YB1XdzgL3Mform49pg3ZfKIVNUNR7ICgYhzqE=; b=MnPBIaGCcGHE9yDzITSrSfZ1B6QRBB4zoFSeeNegs58+yDil4opEddtATksyoYehZL macAXy5B1zWt/7rlCDrUXhTacUOyDEC2O5n2PaaRFxIxBZs06TuB5vXuXEnHGgxVqtPp QUo7aUXYNUS3j2B9WwjqRk/6HdXupju7wHpptvW0co3hz7ofMNiJxsXAR1xqVpRfcBPf B5kzYqhjzyqJKJKzoQad+HZjo6LcF6BF8PHZobGwQAsPfn205R9TXitYZz4gtObymLpn jphyzOtqjigOv51Uy6iKSZMe2Pn6S4E5K0D8wMW/Q3/8VmGXle8sc2HroyxhDUZcvR/8 3Hrg== X-Gm-Message-State: AOJu0Ywu5NBNuAf3n5s3+pYJ6wuV19XzVKpwsNTol7GTeA+BFa9z+c9T 6Ud/yU74lFEhG7IsmACifZODmUutOpA= X-Google-Smtp-Source: AGHT+IEfIKaAO5uExbgPzBO0tfQZFXdYU8siIO4abEpJQBk+sr472KYhvYCdgm115nWDgEKOGaiceaxOXk0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:ad04:0:b0:583:6f04:4267 with SMTP id l4-20020a81ad04000000b005836f044267mr1388ywh.8.1691109752403; Thu, 03 Aug 2023 17:42:32 -0700 (PDT) Date: Thu, 3 Aug 2023 17:42:24 -0700 In-Reply-To: <20230804004226.1984505-1-seanjc@google.com> Mime-Version: 1.0 References: <20230804004226.1984505-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230804004226.1984505-3-seanjc@google.com> Subject: [PATCH 2/4] KVM: selftests: Add helper macros for ioctl()s that return file descriptors From: Sean Christopherson To: Marc Zyngier , Oliver Upton Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Sean Christopherson , Michal Luczaj X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230803_174236_254708_9B130D95 X-CRM114-Status: GOOD ( 14.04 ) 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 KVM, VM, and vCPU scoped helpers for ioctl()s that return file descriptors, i.e. deduplicate code for asserting success on ioctls() for which a positive return value, not just zero, is considered success. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 39 +++++++++++++------ tools/testing/selftests/kvm/lib/kvm_util.c | 17 ++++---- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 90b7739ca204..b35b0bd23683 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -253,6 +253,14 @@ static inline bool kvm_has_cap(long cap) TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ }) +#define kvm_fd_ioctl(kvm_fd, cmd, arg) \ +({ \ + int fd = __kvm_ioctl(kvm_fd, cmd, arg); \ + \ + TEST_ASSERT(fd >= 0, __KVM_IOCTL_ERROR(#cmd, fd)); \ + fd; \ +}) + static __always_inline void static_assert_is_vm(struct kvm_vm *vm) { } #define __vm_ioctl(vm, cmd, arg) \ @@ -268,6 +276,14 @@ static __always_inline void static_assert_is_vm(struct kvm_vm *vm) { } TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ }) +#define vm_fd_ioctl(vm, cmd, arg) \ +({ \ + int fd = __vm_ioctl(vm, cmd, arg); \ + \ + TEST_ASSERT(fd >= 0, __KVM_IOCTL_ERROR(#cmd, fd)); \ + fd; \ +}) + static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { } #define __vcpu_ioctl(vcpu, cmd, arg) \ @@ -283,16 +299,21 @@ static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { } TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ }) +#define vcpu_fd_ioctl(vcpu, cmd, arg) \ +({ \ + int fd = __vcpu_ioctl(vcpu, cmd, arg); \ + \ + TEST_ASSERT(fd >= 0, __KVM_IOCTL_ERROR(#cmd, fd)); \ + fd; \ +}) + /* * Looks up and returns the value corresponding to the capability * (KVM_CAP_*) given by cap. */ static inline int vm_check_cap(struct kvm_vm *vm, long cap) { - int ret = __vm_ioctl(vm, KVM_CHECK_EXTENSION, (void *)cap); - - TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_CHECK_EXTENSION, ret)); - return ret; + return vm_fd_ioctl(vm, KVM_CHECK_EXTENSION, (void *)cap); } static inline int __vm_enable_cap(struct kvm_vm *vm, uint32_t cap, uint64_t arg0) @@ -348,10 +369,7 @@ static inline uint32_t kvm_vm_reset_dirty_ring(struct kvm_vm *vm) static inline int vm_get_stats_fd(struct kvm_vm *vm) { - int fd = __vm_ioctl(vm, KVM_GET_STATS_FD, NULL); - - TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_GET_STATS_FD, fd)); - return fd; + return vm_fd_ioctl(vm, KVM_GET_STATS_FD, NULL); } static inline void read_stats_header(int stats_fd, struct kvm_stats_header *header) @@ -558,10 +576,7 @@ static inline void vcpu_nested_state_set(struct kvm_vcpu *vcpu, #endif static inline int vcpu_get_stats_fd(struct kvm_vcpu *vcpu) { - int fd = __vcpu_ioctl(vcpu, KVM_GET_STATS_FD, NULL); - - TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_GET_STATS_FD, fd)); - return fd; + return vcpu_fd_ioctl(vcpu, KVM_GET_STATS_FD, NULL); } int __kvm_has_device_attr(int dev_fd, uint32_t group, uint64_t attr); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 7a8af1821f5d..557de1d26f10 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -117,8 +117,12 @@ unsigned int kvm_check_cap(long cap) int kvm_fd; kvm_fd = open_kvm_dev_path_or_exit(); - ret = __kvm_ioctl(kvm_fd, KVM_CHECK_EXTENSION, (void *)cap); - TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_CHECK_EXTENSION, ret)); + + /* + * KVM_CHECK_EXTENSION doesn't return a file descriptor, but the + * semantics are the same: a negative value is considered a failure. + */ + ret = kvm_fd_ioctl(kvm_fd, KVM_CHECK_EXTENSION, (void *)cap); close(kvm_fd); @@ -136,12 +140,10 @@ void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size) static void vm_open(struct kvm_vm *vm) { - vm->kvm_fd = _open_kvm_dev_path_or_exit(O_RDWR); - TEST_REQUIRE(kvm_has_cap(KVM_CAP_IMMEDIATE_EXIT)); - 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)); + vm->kvm_fd = _open_kvm_dev_path_or_exit(O_RDWR); + vm->fd = kvm_fd_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type); } const char *vm_guest_mode_string(uint32_t i) @@ -1226,8 +1228,7 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) vcpu->vm = vm; vcpu->id = vcpu_id; - vcpu->fd = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)(unsigned long)vcpu_id); - TEST_ASSERT(vcpu->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VCPU, vcpu->fd)); + vcpu->fd = vm_fd_ioctl(vm, KVM_CREATE_VCPU, (void *)(unsigned long)vcpu_id); TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->run), "vcpu mmap size " "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",