Received: by 2002:a05:7412:85a1:b0:e2:908c:2ebd with SMTP id n33csp146607rdh; Mon, 30 Oct 2023 17:21:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEhTBvrbbLIZkpXefZ/XSqwcWLo0XG+3m34NNofHfHJoEAXMLkBBdMrEuRYQtLrd4LI9xBG X-Received: by 2002:a05:6358:91a3:b0:169:845b:344d with SMTP id j35-20020a05635891a300b00169845b344dmr1395461rwa.22.1698711666629; Mon, 30 Oct 2023 17:21:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698711666; cv=none; d=google.com; s=arc-20160816; b=JFbhag3FP1+Gy1c3rdrI4o+JSH05JZS6qAbx3R56csQxkoJuYgp4rkP2EmBFOxw7X9 vspgV6XTjc+ji5vCTXWgFdh/u61FsmG3UgjFvVK0EhH6XSPCYE/swfpXL3RiOhaj98z0 cB/ocP8pjHCbpsjB6zQNwVISLCo/KvaDWzVNt25g9AdNg+QHnaT5bxhmMaovhGKMoj9K sUhtm52QVzrTWHRZqvNpveEe2zvSJNB8H9LJo8aE3jNDHxOUGlMqibqsk2OllRVP+9An dkRKopNH2wMbuYzh7fKvcmFv9HI2ShPWqe+R+2edjkOuwS+7yVB4PWkHIw4pmORzNFqn d2fw== 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:message-id:mime-version:date :reply-to:dkim-signature; bh=QBv8J2j2tFrn/qyS6YAMefYGNMpKBA+EnJ/tz9XeZcQ=; fh=dogxdQWc7Q2U5Ekp9F18wEHwMgE3kRwQ/60KgTj3654=; b=aYU16tZdiaQfHXITyabSF2lfN86sRHH93KArOgzyLXiSD1NgDYHWMqews4mMEqkIOs 6uKhkXz7ZzjSS6WFHV1Wh+tPjtSm+g5NAnKpD8OeuZnh3uEZ5dWYEURos1bfoAiUEPa6 LD1PNdZg2C3lLF7YshIJLdFeLMvlJ3EEV25QZiuklpTeDYp1P7J5EICMBF5uaix+elS3 gn5a4yh/07stKVYhEw1HGRFqIPuHaAGlgACx+ZAR8S804nNY4xDPlDb+aJSyYtpESuxI cSjxLR7y+hmIC232/6LuDtriCv6oQVEvZcImsXcp/b/UxWMmJgosOGeZecUp3Q8pOlVD QNLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DutbvxFd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id 138-20020a630190000000b005644a9be955si203178pgb.179.2023.10.30.17.21.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 17:21:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DutbvxFd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 0D7A08042A80; Mon, 30 Oct 2023 17:21:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236565AbjJaAU6 (ORCPT + 99 others); Mon, 30 Oct 2023 20:20:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233824AbjJaAU5 (ORCPT ); Mon, 30 Oct 2023 20:20:57 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6B79E1 for ; Mon, 30 Oct 2023 17:20:53 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da03ef6fc30so4796275276.0 for ; Mon, 30 Oct 2023 17:20:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698711653; x=1699316453; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=QBv8J2j2tFrn/qyS6YAMefYGNMpKBA+EnJ/tz9XeZcQ=; b=DutbvxFdNj+zxFsXIApxuqgL/svtHFE4X/w9rnzDlctDS5aEKYBHsMFuGsD2ppEan5 YFMqvnCKG53Rlybn98y5q77a5MpuTtAucEPhIo7eNk2V2+s7EPAj4EjoZBsxjiDA1Wgp wZyw9LMd2L4yUILKkMIJFH/1f1+Ais7lSpK/pUNwe4B9tfDfsTc1jUYuMaYPzxVg7ODU Iwvs+PMW6VidbEzn0tHKXJ+viUEAs8xkSYidMx8TdObaEwuzsWe2YmDYf0JxpwIfdben DccNkbIfGdDwrnq3qLloMhx6T6yKHqGXtqh+AZmjyqVu2zal4Z5Kh2E43uC+4/vEsslQ BfSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698711653; x=1699316453; h=cc:to:from:subject:message-id:mime-version:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QBv8J2j2tFrn/qyS6YAMefYGNMpKBA+EnJ/tz9XeZcQ=; b=nLuhsRI71Fwn5AW2bQ19XKfg/QpZCo8wntd9hCGJ5FmleXsp9CeXqc9p/Vysw97ach TV1q0igbepDTJYTgm22M56lO3XAEnmRvgoCqGwPBlmiMsJx46XfX7VysZVhK+4YdWSRx /Gm+BasJ/cgopIHPgb9IteMPcpivHcmhvAoPUVSHIUWvXWRncLmcszAflM1ZWOfheXax NKobiENmsm/rdmSv0bZAibTERwQzm91sjWyMFC6jmBiI1zccZK/zXXd6xe9zi0b2Ly9p ionPqc/bGJQQ1WaICDNbIPBeTGvX3ar444WCALjs+XejYAaHR8HWeGUmCyg88EZVHWmE WQwA== X-Gm-Message-State: AOJu0Yw+zn6moSM6kXiph54oSSzPEE3l42lamLOwd98ivXfBqaWpoeAw 9eFZ0gY4+VcoWZcHllDBW6QESiR3ka0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:168c:b0:da0:3e46:8ba5 with SMTP id bx12-20020a056902168c00b00da03e468ba5mr205460ybb.8.1698711652929; Mon, 30 Oct 2023 17:20:52 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 30 Oct 2023 17:20:49 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231031002049.3915752-1-seanjc@google.com> Subject: [PATCH guest_memfd] KVM: selftests: Add a memory region subtest to validate invalid flags From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Christopherson Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 30 Oct 2023 17:21:04 -0700 (PDT) Add a subtest to set_memory_region_test to verify that KVM rejects invalid flags and combinations with -EINVAL. Signed-off-by: Sean Christopherson --- Selftest that *tries* to detect cases where KVM allows v2 flags for KVM_SET_USER_MEMORY_REGION. It's kinda worthless because KVM will likely fail with EINVAL anyways, but maybe it'll provide meaningful coverage in concert with a sanitizer? .../selftests/kvm/set_memory_region_test.c | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index ca83e3307a98..268baf853bd6 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -326,6 +326,53 @@ static void test_zero_memory_regions(void) } #endif /* __x86_64__ */ +static void test_invalid_memory_region_flags(void) +{ + uint32_t supported_flags = KVM_MEM_LOG_DIRTY_PAGES; + const uint32_t v2_only_flags = KVM_MEM_PRIVATE; + struct kvm_vm *vm; + int r, i; + +#ifdef __x86_64__ + supported_flags |= KVM_MEM_READONLY; + + if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)) + vm = vm_create_barebones_protected_vm(); + else +#endif + vm = vm_create_barebones(); + + if (kvm_check_cap(KVM_CAP_MEMORY_ATTRIBUTES) & KVM_MEMORY_ATTRIBUTE_PRIVATE) + supported_flags |= KVM_MEM_PRIVATE; + + for (i = 0; i < 32; i++) { + if ((supported_flags & BIT(i)) && !(v2_only_flags & BIT(i))) + continue; + + r = __vm_set_user_memory_region(vm, MEM_REGION_SLOT, BIT(i), + MEM_REGION_GPA, MEM_REGION_SIZE, NULL); + + TEST_ASSERT(r && errno == EINVAL, + "KVM_SET_USER_MEMORY_REGION should have failed on v2 only flag 0x%lx", BIT(i)); + + if (supported_flags & BIT(i)) + continue; + + r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, BIT(i), + MEM_REGION_GPA, MEM_REGION_SIZE, NULL, 0, 0); + TEST_ASSERT(r && errno == EINVAL, + "KVM_SET_USER_MEMORY_REGION2 should have failed on unsupported flag 0x%lx", BIT(i)); + } + + if (supported_flags & KVM_MEM_PRIVATE) { + r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, + KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_PRIVATE, + MEM_REGION_GPA, MEM_REGION_SIZE, NULL, 0, 0); + TEST_ASSERT(r && errno == EINVAL, + "KVM_SET_USER_MEMORY_REGION2 should have failed, dirty logging private memory is unsupported"); + } +} + /* * Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any * tentative to add further slots should fail. @@ -491,6 +538,8 @@ int main(int argc, char *argv[]) test_zero_memory_regions(); #endif + test_invalid_memory_region_flags(); + test_add_max_memory_regions(); if (kvm_has_cap(KVM_CAP_GUEST_MEMFD) && base-commit: 881375a408c0f4ea451ff14545b59216d2923881 -- 2.42.0.820.g83a721a137-goog