From patchwork Fri Aug 4 00:42:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13341034 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 AE3B9C00528 for ; Fri, 4 Aug 2023 00:43:41 +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=SZyOfFJA3de2Sl54Fh9cwPvI2oSB1X53PJziIWZgYMY=; b=Px50gZU06hWuKH xd4PpD2nGwCtprI/5e0IzpztSPhkBB80JTJQvZQlFDuhnyCSxFnKAr9pn8vKWFNJKS4KAMHuSP6nZ TQ/+OJwjzgLPLgF3O98Mj/o9veDslen9lOEPprII4CzH2NyCzotsQkf9NQ8DfWDO2iG6q1Mm0M8zo eWZeK6iQl+QfpnCLwRtAPI1lvpcKw1cbOLH6+ELMu95FxM81zqDVwb42wVKQtP4Aa+G00jckLCURM owfulnow2WRMUmDjnkL7VHGKM+BD5LGvSxKMbY2+Vfe5cyDqZCYPN5hFhH07yjoc4kn1j9weDnzJs UjaXkpOSPF6BdwZBaekQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qRitn-00BBQj-2v; Fri, 04 Aug 2023 00:42:35 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qRitk-00BBQ5-1M for linux-arm-kernel@lists.infradead.org; Fri, 04 Aug 2023 00:42:33 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1bba7a32a40so12217395ad.0 for ; Thu, 03 Aug 2023 17:42:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691109750; x=1691714550; 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=TPdXiND7N94yuFEwSbECpKkQfmAanLh9RZ+A6BrvR7M=; b=2tZIajDGiYM8LOP6oRS7sIm2pcBnaXfZ7Wymplbdn69KUU8q53NcZ7y0HFK8pN3rJv XsgnlEdOHLu6ZqLedVwaja+dve26SOtilLHroxOKcn1gMKyUuqCKj30/Z97mg+qQ0A5R 3Ikj3Cg1e6W0kaLu9miTZWiHb9zsHokxrA3qv+mc/lCMvO6k2YOsSQj0EFs1erqLxsGU 3tJpi1uPKeG7pLmg5VlnYlIpw8x0qWFmi2sg4OydjUAuuqTERt756yyvc0gyoig//M6H 5fTKOOVxD2iGkXDjRyyWPE5G1u+8j6kSj/3rqzdeuj9Xdr5V2hQG6q1B7q16VMnrsqdk zf2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691109750; x=1691714550; 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=TPdXiND7N94yuFEwSbECpKkQfmAanLh9RZ+A6BrvR7M=; b=F7xfNgSxKbsFui9Hkt7tVhdoYZKrIkLTeZb8W5MlVHfLMAhwJklnMU9t8gULIPbt2o hfd8/aLWx37p21r4xd/aepiD7sYrx2mP4RWBmiFiaIGa74g+Y1jWp6sY/nXbrNXb9x4Q p3ifHEWxAZcTMz/u0ALup9Fv8AhrYeEEfLlOl+GwTXnocjL0QkQHk/DNqNga7xhG7pOt NojNXIRKBG9fxtH1GIwKfiAPMvpHf8xCaRg4jDy8WQSQeSB9MWRkK+eRfkMrSum8syH8 sn5iq3bZNppQhKFAUhbPpM59e3zenj/E38+U5tpMoVR/C+8liCQzgYCzOw3zL+8WUl0Z HQIw== X-Gm-Message-State: AOJu0Ywb9o48yP6D/5100WjlNEtqD95vXGbDU/aekXXdG8RANDECMpgb OrkPd7c1P8q7jsjmkgm+qT5vrmMA7CY= X-Google-Smtp-Source: AGHT+IGxPWjxkWB9Y/JG84vlZ7jPt3zPsnr0k+1ejtlvOppoogL05sX+e7/sQkS50kBaur8MYBj5ozDKMXE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:32d2:b0:1b8:5541:9d4d with SMTP id i18-20020a17090332d200b001b855419d4dmr1307plr.6.1691109750368; Thu, 03 Aug 2023 17:42:30 -0700 (PDT) Date: Thu, 3 Aug 2023 17:42:23 -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-2-seanjc@google.com> Subject: [PATCH 1/4] KVM: selftests: Drop the single-underscore ioctl() helpers 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_174232_480385_20CC6968 X-CRM114-Status: GOOD ( 13.38 ) 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 Drop _kvm_ioctl(), _vm_ioctl(), and _vcpu_ioctl(), as they are no longer used by anything other than the no-underscores variants (and may have never been used directly). The single-underscore variants were never intended to be a "feature", they were a stopgap of sorts to ease the conversion to pretty printing ioctl() names when reporting errors. Opportunistically add a comment explaining when to use __KVM_IOCTL_ERROR() versus KVM_IOCTL_ERROR(). The single-underscore macros were subtly ensuring that the name of the ioctl() was printed on error, i.e. it's all too easy to overlook the fact that using __KVM_IOCTL_ERROR() is intentional. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 07732a157ccd..90b7739ca204 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -227,6 +227,13 @@ static inline bool kvm_has_cap(long cap) #define __KVM_SYSCALL_ERROR(_name, _ret) \ "%s failed, rc: %i errno: %i (%s)", (_name), (_ret), errno, strerror(errno) +/* + * Use the "inner", double-underscore macro when reporting errors from within + * other macros so that the name of ioctl() and not its literal numeric value + * is printed on error. The "outer" macro is strongly preferred when reporting + * errors "directly", i.e. without an additional layer of macros, as it reduces + * the probability of passing in the wrong string. + */ #define __KVM_IOCTL_ERROR(_name, _ret) __KVM_SYSCALL_ERROR(_name, _ret) #define KVM_IOCTL_ERROR(_ioctl, _ret) __KVM_IOCTL_ERROR(#_ioctl, _ret) @@ -239,17 +246,13 @@ static inline bool kvm_has_cap(long cap) #define __kvm_ioctl(kvm_fd, cmd, arg) \ kvm_do_ioctl(kvm_fd, cmd, arg) - -#define _kvm_ioctl(kvm_fd, cmd, name, arg) \ +#define kvm_ioctl(kvm_fd, cmd, arg) \ ({ \ int ret = __kvm_ioctl(kvm_fd, cmd, arg); \ \ - TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(name, ret)); \ + TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ }) -#define kvm_ioctl(kvm_fd, cmd, arg) \ - _kvm_ioctl(kvm_fd, cmd, #cmd, arg) - static __always_inline void static_assert_is_vm(struct kvm_vm *vm) { } #define __vm_ioctl(vm, cmd, arg) \ @@ -258,16 +261,12 @@ static __always_inline void static_assert_is_vm(struct kvm_vm *vm) { } kvm_do_ioctl((vm)->fd, cmd, arg); \ }) -#define _vm_ioctl(vm, cmd, name, arg) \ -({ \ - int ret = __vm_ioctl(vm, cmd, arg); \ - \ - TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(name, ret)); \ -}) - #define vm_ioctl(vm, cmd, arg) \ - _vm_ioctl(vm, cmd, #cmd, arg) - +({ \ + int ret = __vm_ioctl(vm, cmd, arg); \ + \ + TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ +}) static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { } @@ -277,15 +276,12 @@ static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { } kvm_do_ioctl((vcpu)->fd, cmd, arg); \ }) -#define _vcpu_ioctl(vcpu, cmd, name, arg) \ -({ \ - int ret = __vcpu_ioctl(vcpu, cmd, arg); \ - \ - TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(name, ret)); \ -}) - #define vcpu_ioctl(vcpu, cmd, arg) \ - _vcpu_ioctl(vcpu, cmd, #cmd, arg) +({ \ + int ret = __vcpu_ioctl(vcpu, cmd, arg); \ + \ + TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ +}) /* * Looks up and returns the value corresponding to the capability 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", From patchwork Fri Aug 4 00:42:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13341032 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 76B4DC001DB for ; Fri, 4 Aug 2023 00:43:37 +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=ZehOJb3vPAUHHHMGTyGrt/0b/jbgZIAHnt98QUkJp6I=; b=0TbG6/seTYtM+B 4bMtqvRKBvCdyOu3m4OfYOg0YRmy+W2Bm7KO1Te6f8sFUmtgTaXai31hhU+uDWSnd5QgduRkzRb7o aFlPz35IrY28QK0SeNbo+/jpQkLDV6sS+2ZrS3yoS66pSDd062gofYvWhAEzg8IM5A4a9ODIWsZ3E L+BUY1AGSnCzHMbuDzMHihMNn2xPZmjwQuNk404GtSgEHnDxATWpfnITeHI2z2fWQ7sR273Gg4J+c Qt8PW5qDWkLoNXOxtXUg7EaLxrw5vPYHrhFH4ezRCxrr6D/NDNWBaQdC7rJh3sUmQT/jbdTUYMuq6 QEFSvVB1GK6wuFFzjDSQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qRitq-00BBRy-06; Fri, 04 Aug 2023 00:42:38 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qRitn-00BBQg-0u for linux-arm-kernel@lists.infradead.org; Fri, 04 Aug 2023 00:42:36 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1bb982d2572so13376045ad.0 for ; Thu, 03 Aug 2023 17:42:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691109754; x=1691714554; 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=J+mxzLyo3KvL1X1/io+X1OzBSufdvdiiGzpaJwRMhFQ=; b=SOX0LmZ7M/i5Xu6IShPkO2I+BxiwZ7/tKfFw/OszRJyMzjeA8srrxfMqCj4Z29VgNn T0538ljVhrlqi2oWqsf8SDO/oV45EgP4x4oPafLtiFcA3cCpGCSnbWPlD7O1UBhgOJNe iWOEAZPpkZuEI+dl8Vup28taKKrYkoe9EEkFDZdVyWlSSumkRefUTDG/veJy2F6RniEr D4CN9ItmtE3GLQpzTfzusmxS7gLu+J3zXqOmaxg3BFTHOgLSHC5+aOpOePxVzXkX7y+j 40aPvclyhw23NLYArpVti1fhnKJ/y5UqJGdMWPnW2w3RzepQtSrazDZXeGyOlRrMC0eG uSAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691109754; x=1691714554; 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=J+mxzLyo3KvL1X1/io+X1OzBSufdvdiiGzpaJwRMhFQ=; b=isNx3g0J44wxraPHvJhI/YDLHPWFI1jfbWOMn0LPDfiYwyVYDbnLHWGo82UdPkQjm0 QzpVv5sb6MB1ovo2jQ5eVecwFM65yIINhaLptqoy1PBFfFzma5o/FbNn8DW3ots9oDF1 3xiTR420r4R+gOP4ZQPxk70tWoXEYX8p04syCqWplgdOYVKzTMF7eu9rxjTmiFv3CO48 qWtjq5vqjMinH/U3vLusAfgVGo4Vuyy2HQkCW4zJLmmWPLAryLKWQekvgM8zESOYn43S vbFUjqoWHlDaos3ReDhO6bEqIhkZH+a5kRa3RU/ZkmQUImeoUNgMqlm3EmC5arK0YYw+ p81w== X-Gm-Message-State: AOJu0YxC//deB9ay64YczxqTLF3ffNV9F3zMwE6M11afxxzPAl9eUbF6 SWXYygktgLhQG7zclRRcZeS5XpQe+Fw= X-Google-Smtp-Source: AGHT+IHgxmGXjdzVwOxMuN7YUlHLd0HAzkRoLNDux7bwtM6itZt1UHKYVf8WxlurAEE0Phc3A5aSSAIz63w= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:dad1:b0:1b8:a555:385d with SMTP id q17-20020a170902dad100b001b8a555385dmr1165plx.9.1691109754176; Thu, 03 Aug 2023 17:42:34 -0700 (PDT) Date: Thu, 3 Aug 2023 17:42:25 -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-4-seanjc@google.com> Subject: [PATCH 3/4] KVM: selftests: Use asserting kvm_ioctl() macros when getting ARM page sizes 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_174235_324100_55E364C8 X-CRM114-Status: GOOD ( 14.25 ) 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 Use kvm_ioctl() instead of open coding equivalent ioctl()+TEST_ASSERT() calls when getting the support page sizes on ARM. The macro usage is a little funky since the "kvm_fd" parameter implies an actual /dev/kvm fd, but on the other hand the code is invoking KVM ioctl()s. Alternatively, the core utilities could expose a vm_open()+vm_close() pair so that the ARM code could create a dummy, on-stack VM+vCPU pair and use {vm,vcpu}_ioctl() as appropriate. But the odds of something breaking due to oddball, partial usage of kvm_vm and kvm_vcpu structures is much higher than realizing meaningful benefit from using {vm,vcpu}_ioctl(). Signed-off-by: Sean Christopherson --- .../selftests/kvm/lib/aarch64/processor.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/aarch64/processor.c b/tools/testing/selftests/kvm/lib/aarch64/processor.c index 3a0259e25335..afec1a30916f 100644 --- a/tools/testing/selftests/kvm/lib/aarch64/processor.c +++ b/tools/testing/selftests/kvm/lib/aarch64/processor.c @@ -496,7 +496,7 @@ void aarch64_get_supported_page_sizes(uint32_t ipa, bool *ps4k, bool *ps16k, bool *ps64k) { struct kvm_vcpu_init preferred_init; - int kvm_fd, vm_fd, vcpu_fd, err; + int kvm_fd, vm_fd, vcpu_fd; uint64_t val; struct kvm_one_reg reg = { .id = KVM_ARM64_SYS_REG(SYS_ID_AA64MMFR0_EL1), @@ -504,19 +504,13 @@ void aarch64_get_supported_page_sizes(uint32_t ipa, }; kvm_fd = open_kvm_dev_path_or_exit(); - vm_fd = __kvm_ioctl(kvm_fd, KVM_CREATE_VM, (void *)(unsigned long)ipa); - TEST_ASSERT(vm_fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm_fd)); + vm_fd = kvm_fd_ioctl(kvm_fd, KVM_CREATE_VM, (void *)(unsigned long)ipa); + vcpu_fd = kvm_fd_ioctl(vm_fd, KVM_CREATE_VCPU, (void *)0ul); - vcpu_fd = ioctl(vm_fd, KVM_CREATE_VCPU, 0); - TEST_ASSERT(vcpu_fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VCPU, vcpu_fd)); + kvm_ioctl(vm_fd, KVM_ARM_PREFERRED_TARGET, &preferred_init); + kvm_ioctl(vcpu_fd, KVM_ARM_VCPU_INIT, &preferred_init); - err = ioctl(vm_fd, KVM_ARM_PREFERRED_TARGET, &preferred_init); - TEST_ASSERT(err == 0, KVM_IOCTL_ERROR(KVM_ARM_PREFERRED_TARGET, err)); - err = ioctl(vcpu_fd, KVM_ARM_VCPU_INIT, &preferred_init); - TEST_ASSERT(err == 0, KVM_IOCTL_ERROR(KVM_ARM_VCPU_INIT, err)); - - err = ioctl(vcpu_fd, KVM_GET_ONE_REG, ®); - TEST_ASSERT(err == 0, KVM_IOCTL_ERROR(KVM_GET_ONE_REG, vcpu_fd)); + kvm_ioctl(vcpu_fd, KVM_GET_ONE_REG, ®); *ps4k = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64MMFR0_TGRAN4), val) != 0xf; *ps64k = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64MMFR0_TGRAN64), val) == 0; From patchwork Fri Aug 4 00:42:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13341033 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 2CB90C001DB for ; Fri, 4 Aug 2023 00:43:41 +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=LEJUd/UTjjmPq4gPwf551WIERpb9FWFoyc12yI3NkGU=; b=Vvi3NbIhjQstwv KpPZwUXSK5g1STvqhfQdl7Fb0crIvOr4Zq1tpTa/YBVtejHlXMBv9NrVbXcOqJrQIXpTUQw2ynkIK IcilBvbXF6UypsZmf95aQhgAeAM2Htme0fchsTlnIw+QwAuQMJmMnnFTlHw+yPjmNCIfuu8n3lbPY GV1z1wOOba+NrPbMBDujiRgpxYaKGjp/4HiSOTvwf6Wk4zLxkHl8LTAdXSg/8WygT9Q6U55QRFlXQ h81zdev+a0j4TeMXkUzqGJPPryZXWmqtTaG/tMhQkY5UXb7XlTCRmJeWSErnN2Lc3VC81x7wnZbTt isztgtzKwxgsv9mimNkw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qRitu-00BBSx-0E; Fri, 04 Aug 2023 00:42:42 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qRitq-00BBRW-2T for linux-arm-kernel@lists.infradead.org; Fri, 04 Aug 2023 00:42:40 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-684c8a66e86so993928b3a.0 for ; Thu, 03 Aug 2023 17:42:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691109756; x=1691714556; 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=2CsV9BMCbijce3IEX7syYt6oaSf9HBzVzUSeSJTTl/c=; b=DNaTTcvyymaqWv5JS/b+Ul58o6UASX5Gzy58D8OgNUL3QP3segv+8o1Nz+GizNekfv rFDM+9jgVrY2UJtz2kl/i/L6udW4AVtdgPGyAjht4ak4W3jUQVvvpj9GFbt2AV7cn4l+ 38Kkl5Ze1Wn9JzG07hlvDqExeNIGqBvSHZmT6nAMyQFB+uihzhtqKN/XrVFhOHZxwJoJ bJ97KvFNHowNRmq00dAMx7hg868GXdY6QdJJAIPSVa5n1tK2uB8ro5zAKxUnv0XJe787 vRwGsDf3w10BvuLmfXZQHbQLUlzVsbY9ElYEmKScB7sxzF9olsQfyFnqpZRmKvxrB54P 7c9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691109756; x=1691714556; 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=2CsV9BMCbijce3IEX7syYt6oaSf9HBzVzUSeSJTTl/c=; b=HT35J7J6CRi8N7AcIINqQ01AwH9/xWo9AOGFVppYtnXpafRR5XIMG+nGBA58GXUHdC 6RBlw4LcMxt2aozjz3IhvXsv8eAQw0mrYxcRZbexCQqI3K+aaV/WptM6CKP6Sa5R2Hc1 W1UKyQhCjru0Aj2S1yLj9viiKVe4319ZGPOM6sfJx9xEvQ8KysgizZ7J6x0Nln1k6lNW 57wicaLDmVL4eYpRcPrDm//feKAN4F47w3mXktEnJpriNOV0buyYJvqYmt5m0IzzOURL NmckZ1KOENlP/iQz8dJqNShe64gJPibHNtfJHMvGytuDyfJ9C4LdVezrRojXYx55Gt1L qxGg== X-Gm-Message-State: AOJu0YxSM40t7Neu0EmmijdDI3x/C3RNcSnhk5ZgeNGL/QDXTAQCcW+U jHxdfiR9qgef8KXnc0HdDOK3sK5tt5U= X-Google-Smtp-Source: AGHT+IGuCrgdNKO1/9Fk3u0Zo/Reh2Y3zb5ohylW7eg4Swp9EAXsJiPsGMrXKVJst+F5prtcttfq9x81VV0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:124c:b0:687:926f:e62f with SMTP id u12-20020a056a00124c00b00687926fe62fmr1573pfi.2.1691109756391; Thu, 03 Aug 2023 17:42:36 -0700 (PDT) Date: Thu, 3 Aug 2023 17:42:26 -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-5-seanjc@google.com> Subject: [PATCH 4/4] KVM: selftests: Add logic to detect if ioctl() failed because VM was killed 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_174238_801750_B5E966E5 X-CRM114-Status: GOOD ( 17.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 Add yet another macro to the VM/vCPU ioctl() framework to detect when an ioctl() failed because KVM killed/bugged the VM, i.e. when there was nothing wrong with the ioctl() itself. If KVM kills a VM, e.g. by way of a failed KVM_BUG_ON(), all subsequent VM and vCPU ioctl()s will fail with -EIO, which can be quite misleading and ultimately waste user/developer time. Use KVM_CHECK_EXTENSION on KVM_CAP_USER_MEMORY to detect if the VM is dead and/or bug, as KVM doesn't provide a dedicated ioctl(). Using a heuristic is obviously less than ideal, but practically speaking the logic is bulletproof barring a KVM change, and any such change would arguably break userspace, e.g. if KVM returns something other than -EIO. Without the detection, tearing down a bugged VM yields a cryptic failure when deleting memslots: ==== Test Assertion Failure ==== lib/kvm_util.c:689: !ret pid=45131 tid=45131 errno=5 - Input/output error 1 0x00000000004036c3: __vm_mem_region_delete at kvm_util.c:689 2 0x00000000004042f0: kvm_vm_free at kvm_util.c:724 (discriminator 12) 3 0x0000000000402929: race_sync_regs at sync_regs_test.c:193 4 0x0000000000401cab: main at sync_regs_test.c:334 (discriminator 6) 5 0x0000000000416f13: __libc_start_call_main at libc-start.o:? 6 0x000000000041855f: __libc_start_main_impl at ??:? 7 0x0000000000401d40: _start at ??:? KVM_SET_USER_MEMORY_REGION failed, rc: -1 errno: 5 (Input/output error) Which morphs into a more pointed error message with the detection: ==== Test Assertion Failure ==== lib/kvm_util.c:689: false pid=80347 tid=80347 errno=5 - Input/output error 1 0x00000000004039ab: __vm_mem_region_delete at kvm_util.c:689 (discriminator 5) 2 0x0000000000404660: kvm_vm_free at kvm_util.c:724 (discriminator 12) 3 0x0000000000402ac9: race_sync_regs at sync_regs_test.c:193 4 0x0000000000401cb7: main at sync_regs_test.c:334 (discriminator 6) 5 0x0000000000418263: __libc_start_call_main at libc-start.o:? 6 0x00000000004198af: __libc_start_main_impl at ??:? 7 0x0000000000401d90: _start at ??:? KVM killed/bugged the VM, check the kernel log for clues Suggested-by: Michal Luczaj Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index b35b0bd23683..49ad681d2161 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -269,18 +269,44 @@ static __always_inline void static_assert_is_vm(struct kvm_vm *vm) { } kvm_do_ioctl((vm)->fd, cmd, arg); \ }) +/* + * Assert that a VM or vCPU ioctl() succeeded, with extra magic to detect if + * the ioctl() failed because KVM killed/bugged the VM. To detect a dead VM, + * probe KVM_CAP_USER_MEMORY, which (a) has been supported by KVM since before + * selftests existed and (b) should never outright fail, i.e. is supposed to + * return 0 or 1. If KVM kills a VM, KVM returns -EIO for all ioctl()s for the + * VM and its vCPUs, including KVM_CHECK_EXTENSION. + */ +#define TEST_ASSERT_VM_VCPU_IOCTL(cond, name, ret, vm) \ +do { \ + int __errno = errno; \ + \ + static_assert_is_vm(vm); \ + \ + if (cond) \ + break; \ + \ + if (errno == EIO && \ + __vm_ioctl(vm, KVM_CHECK_EXTENSION, (void *)KVM_CAP_USER_MEMORY) < 0) { \ + TEST_ASSERT(errno == EIO, "KVM killed the VM, should return -EIO"); \ + TEST_FAIL("KVM killed/bugged the VM, check the kernel log for clues"); \ + } \ + errno = __errno; \ + TEST_ASSERT(cond, __KVM_IOCTL_ERROR(name, ret)); \ +} while (0) + #define vm_ioctl(vm, cmd, arg) \ ({ \ int ret = __vm_ioctl(vm, cmd, arg); \ \ - TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ + TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \ }) #define vm_fd_ioctl(vm, cmd, arg) \ ({ \ int fd = __vm_ioctl(vm, cmd, arg); \ \ - TEST_ASSERT(fd >= 0, __KVM_IOCTL_ERROR(#cmd, fd)); \ + TEST_ASSERT_VM_VCPU_IOCTL(fd >= 0, #cmd, fd, vm); \ fd; \ }) @@ -296,14 +322,14 @@ static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { } ({ \ int ret = __vcpu_ioctl(vcpu, cmd, arg); \ \ - TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ + TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, (vcpu)->vm); \ }) #define vcpu_fd_ioctl(vcpu, cmd, arg) \ ({ \ int fd = __vcpu_ioctl(vcpu, cmd, arg); \ \ - TEST_ASSERT(fd >= 0, __KVM_IOCTL_ERROR(#cmd, fd)); \ + TEST_ASSERT_VM_VCPU_IOCTL(fd >= 0, #cmd, fd, (vcpu)->vm);\ fd; \ })