Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp840647pxb; Tue, 14 Sep 2021 09:49:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZd/GAeH93udaWW5E7aHFhekfAwdsypXr2EPLnojPFONL+X8aKtSyMx/BTQxbtiH+c+zjz X-Received: by 2002:a05:6e02:dd3:: with SMTP id l19mr5873547ilj.144.1631638159374; Tue, 14 Sep 2021 09:49:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631638159; cv=none; d=google.com; s=arc-20160816; b=cmjBHSMH9Ywtvy1d3qRN6Yv/07//1eNh8BklSvmCoazZA3MYMjJGIbTvzuJbmWtHC9 DsaUWgPl8Hw3oVNMg1krYfo7r5qC4zkT3eOx3F/d2+7hBsHbQAYSp3VhPpFX/pBee7i9 gCTJdolMeWDL7yWKosusrRJphmWzi/Mvin9BIZ/C9fCjVRr/RtszWzc7h0m/6NmX8Taa 1ppeQK65Jq/xn2CaA7GYWOU1nETI+oEV/2rks4/JxOqptP0nWly1Ax6HAj2rZG40bz0v 2v7Gi9zq0tJtCi+AeyOQMNVhV48ncdIPdkazNH3VhU/KkkIooxRfAsUrkrotTGfJo2pT lI5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=RBZaKuZALLe3rJ2+w08+UFnfhUVY8qw7FYW7+il7fnw=; b=PGndpHBPOhnnRbUnNy5R33T8C7gfZqCyydnY/KGt0bIp1NA9u0N9fU+toVoeJeNhyH kMzLYx+m0HKZt6uL6YxDc+Rup+6gvIpzLsG2JV0Z7iEP8IN/ifpTz7M5/LRvadgRk+bz RvuNfEL62k8y6afambHTEr5SU/TBbHo+0fcGWYiTaqqYsg2U0V1RmmWBjWOsqx3Uc11T GmZ0TwWzq0AmXhlPvOhp7ofnt0OY98lb8eRFD55NmdZLPH6VmaaPJQpXpypZ8bZhwTJ7 P5wwTVcTCPu9E5Nuf1w0sJCsa7LFCH+nXHAxu3vKkBNnq4cKt6b61FTwfT/Fr4PXWmko yy/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oWWwD7gH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h199si10104260iof.50.2021.09.14.09.49.06; Tue, 14 Sep 2021 09:49:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oWWwD7gH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230238AbhINQtB (ORCPT + 99 others); Tue, 14 Sep 2021 12:49:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230150AbhINQsy (ORCPT ); Tue, 14 Sep 2021 12:48:54 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1F4BC061574 for ; Tue, 14 Sep 2021 09:47:36 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id s8-20020a170902ea0800b0013b6f74f3a2so4104048plg.14 for ; Tue, 14 Sep 2021 09:47:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=RBZaKuZALLe3rJ2+w08+UFnfhUVY8qw7FYW7+il7fnw=; b=oWWwD7gHC/h2WvG5OvgLQJwZ0NDmC2d0/Yyu2V7ag8ZO8zNb2bhVmG41eid62kEIyC 77aSl5+H08sBkT3bjDqKQuYTEFb9b+I8oqYokRGsc/+Y26V08J0F1QaHNfiD92U9m7JS 0268Q3FxwOMErA3LFMMDhre9aNkuhIy5f+aqpAMBpvfQgHBoVitVH5lT/jkX+JHKWx0M NBZcugs+USZl0WkD6UBNb/na3yc3rG34oNDaf+1cMZMlimHpcnZuRsgDBYgbOxN1o4j1 Rg2fAyNtdGtSsckA4brNd9FcCl4CsaiXx/K8inY/6yDZwLDl5g7m4UTDTqS9UaZSBg+h rGlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=RBZaKuZALLe3rJ2+w08+UFnfhUVY8qw7FYW7+il7fnw=; b=tRPGbnwXATes7QOdACsK7F3mnD4GOMQRE+HxuK5Be0x+zU8ZTbFsb3diKkp6nXjmI4 Uc++Qf576PaJOlI58P3kjzyrgmXEC4Dzi/8lSOe4hNgqihVQPhDDCc/jagwDr+1zHp80 14FbOx98QKUMbgbksz+ZoWZqV60ghJtiAjiau5DlBUAYOispc75lbaKlkat0VNCNpq6A FmvaZMCjLq3raYnvvVtl4u6w2GrlvObeRTURAvwsYYP1t/2uha9SxnfxhUPjorPSUdaZ ARxLZTbJow+4damSnILV+rxsXzKWMxJHvVeekL3wBkcox8Dvst6qncRN5doX6fPeYX9q Bfvg== X-Gm-Message-State: AOAM531RDir4Q/QgGSsrpAvTF8nZNMPeM87DanHyQAlrMib0BzCiXUyD H/s+apwbgoUsbj6bjAgou6DhyBgWpaA= X-Received: from pgonda1.kir.corp.google.com ([2620:15c:29:204:b358:1f40:79d5:ab23]) (user=pgonda job=sendgmr) by 2002:a17:90a:194a:: with SMTP id 10mr3143001pjh.176.1631638056374; Tue, 14 Sep 2021 09:47:36 -0700 (PDT) Date: Tue, 14 Sep 2021 09:47:26 -0700 In-Reply-To: <20210914164727.3007031-1-pgonda@google.com> Message-Id: <20210914164727.3007031-4-pgonda@google.com> Mime-Version: 1.0 References: <20210914164727.3007031-1-pgonda@google.com> X-Mailer: git-send-email 2.33.0.309.g3052b89438-goog Subject: [PATCH 3/4 V8] selftest: KVM: Add open sev dev helper From: Peter Gonda To: kvm@vger.kernel.org Cc: Peter Gonda , Sean Christopherson , Marc Orr , Paolo Bonzini , David Rientjes , "Dr . David Alan Gilbert" , Brijesh Singh , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Refactors out open path support from open_kvm_dev_path_or_exit() and adds new helper for SEV device path. Signed-off-by: Peter Gonda Suggested-by: Sean Christopherson Cc: Marc Orr Cc: Paolo Bonzini Cc: Sean Christopherson Cc: David Rientjes Cc: Dr. David Alan Gilbert Cc: Brijesh Singh Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: Joerg Roedel Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- .../testing/selftests/kvm/include/kvm_util.h | 1 + .../selftests/kvm/include/x86_64/svm_util.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 24 +++++++++++-------- tools/testing/selftests/kvm/lib/x86_64/svm.c | 13 ++++++++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 010b59b13917..368e88305046 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -80,6 +80,7 @@ struct vm_guest_mode_params { }; extern const struct vm_guest_mode_params vm_guest_mode_params[]; +int open_path_or_exit(const char *path, int flags); int open_kvm_dev_path_or_exit(void); int kvm_check_cap(long cap); int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap); diff --git a/tools/testing/selftests/kvm/include/x86_64/svm_util.h b/tools/testing/selftests/kvm/include/x86_64/svm_util.h index b7531c83b8ae..587fbe408b99 100644 --- a/tools/testing/selftests/kvm/include/x86_64/svm_util.h +++ b/tools/testing/selftests/kvm/include/x86_64/svm_util.h @@ -46,4 +46,6 @@ static inline bool cpu_has_svm(void) return ecx & CPUID_SVM; } +int open_sev_dev_path_or_exit(void); + #endif /* SELFTEST_KVM_SVM_UTILS_H */ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 10a8ed691c66..06a6c04010fb 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -31,6 +31,19 @@ static void *align(void *x, size_t size) return (void *) (((size_t) x + mask) & ~mask); } +int open_path_or_exit(const char *path, int flags) +{ + int fd; + + fd = open(path, flags); + if (fd < 0) { + print_skip("%s not available (errno: %d)", path, errno); + exit(KSFT_SKIP); + } + + return fd; +} + /* * Open KVM_DEV_PATH if available, otherwise exit the entire program. * @@ -42,16 +55,7 @@ static void *align(void *x, size_t size) */ static int _open_kvm_dev_path_or_exit(int flags) { - int fd; - - fd = open(KVM_DEV_PATH, flags); - if (fd < 0) { - print_skip("%s not available, is KVM loaded? (errno: %d)", - KVM_DEV_PATH, errno); - exit(KSFT_SKIP); - } - - return fd; + return open_path_or_exit(KVM_DEV_PATH, flags); } int open_kvm_dev_path_or_exit(void) diff --git a/tools/testing/selftests/kvm/lib/x86_64/svm.c b/tools/testing/selftests/kvm/lib/x86_64/svm.c index 2ac98d70d02b..14a8618efa9c 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/svm.c +++ b/tools/testing/selftests/kvm/lib/x86_64/svm.c @@ -13,6 +13,8 @@ #include "processor.h" #include "svm_util.h" +#define SEV_DEV_PATH "/dev/sev" + struct gpr64_regs guest_regs; u64 rflags; @@ -160,3 +162,14 @@ void nested_svm_check_supported(void) exit(KSFT_SKIP); } } + +/* + * Open SEV_DEV_PATH if available, otherwise exit the entire program. + * + * Return: + * The opened file descriptor of /dev/sev. + */ +int open_sev_dev_path_or_exit(void) +{ + return open_path_or_exit(SEV_DEV_PATH, 0); +} -- 2.33.0.309.g3052b89438-goog