Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2410916rdb; Thu, 21 Sep 2023 19:15:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEoN9vQmf1t2fV2ymXLgiqePIiTy7LbZu3UzOG0sbtE0itOPVKTgavCtAtl/s562Ja6N9hC X-Received: by 2002:a17:90a:1347:b0:273:ec9c:a7e3 with SMTP id y7-20020a17090a134700b00273ec9ca7e3mr7297205pjf.17.1695348946834; Thu, 21 Sep 2023 19:15:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695348946; cv=none; d=google.com; s=arc-20160816; b=kKd5Fgo9UD68wITO3ZKQQmJZmGwbaodzb1eH+qI6AA0Rz8mykPm4JYUbBuuevCPU7Y lD2wYkLgUxU5ca9Mb2CGokYo2gFP3d289HrzDSh0GELVeH/qCXJ480kcqgG1/FYOYojx M3x87DEB8dwZw0IRqVnhT79todLXNzYCIoGsWcGFV0Cy0jOpQFh/CaYwMSHrUtWDv0oO xvWAs8CuhOSe7Q2uA/Eslq3oIgGk6gkYSXRbcukqPdPtz3gW7OJFE35mG3MgfPhxB0PB nqbc6DWH4PbneemuWH7Ot3alA5z3/Cz+cX4rLfV/1o8gNLZNGsHtIjpj18UTQbQihcsB wIpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2mT141LEsbBjf/83/IvNkCwraLGH5ZBYkLyAKv7iv9Q=; fh=ANS6iRpdj3ntKSmwSzT4LEHUH9ZAudCj9zvP8/EeThQ=; b=qY5IcACtASWTlmWXFZ+S9qEyhk3LUi1f1blJ/v388LN4PP01pPZ202tXVfDKeP0r0D szrLXEoTgu99GnLzOavZTW9ufkU4Nir7uhKDDYD7Q1u5JRQN8RFXiDqrxnLOt/ZnaaJd Xxp1gGzDe0XBOYzDfQ947/idpgZZY/lGm9vPLQvXH7Q2DFKHo3m8VVqERwk2Eyp619Y/ uGP/VyzXWh9TEDEhsSPp5c1VSZ2XgK72CrRhrzUm7G3cGjUDV2YERYbHdS2ezxAGT3RQ ukpjETOOxHEY7ARe3BkvsJdz8seLGGhTJEd6n2mrUuVyyD+wEZzgrw6p66R8zR6I/Xdt Qvwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="EHO0LIM/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id gf3-20020a17090ac7c300b002748b96d88csi4865656pjb.179.2023.09.21.19.15.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 19:15:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="EHO0LIM/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 55BF583762FB; Thu, 21 Sep 2023 13:42:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229591AbjIUUlz (ORCPT + 99 others); Thu, 21 Sep 2023 16:41:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230197AbjIUUlf (ORCPT ); Thu, 21 Sep 2023 16:41:35 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F1511731; Thu, 21 Sep 2023 13:15:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695327319; x=1726863319; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xgBlKNZhDNJa7QFdNnudGsohvQxeiXqUZBEeWY2YObQ=; b=EHO0LIM/x1tFIuqEOXOraBxGoiC3wL3XeEvUQi76PSAv2AYAJCbQk7Qi D24pIX6CkkEAwsv4Pk0/OZQ679ETeH2lAh/Zatn1cJq6AAtZm/x/uTEqR Df9a5aGmrCb4/7S/4qOnWBA9TZ1cjmbQ+YaC/GvACvYTDhfC3VsiraNjX Gelt3u36fY6T5Id7Nhl3WgFRXUKVgqueiwQKfRmmBmu3QA4EXJXi2xMQ2 S4tKbVNuhArxpvYuVmsVbrJ03Vc+Ww0OgPHRsgbOM08lcRi74eWeE7nJc nA5j6q5HAbehud+d3DE7Rj9J9wMa6aEiAXAPOZO01pWGfgyPuj01xufDm g==; X-IronPort-AV: E=McAfee;i="6600,9927,10840"; a="383401651" X-IronPort-AV: E=Sophos;i="6.03,166,1694761200"; d="scan'208";a="383401651" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Sep 2023 13:14:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10840"; a="696897810" X-IronPort-AV: E=Sophos;i="6.03,166,1694761200"; d="scan'208";a="696897810" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Sep 2023 13:14:49 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Michael Roth , Paolo Bonzini , Sean Christopherson , erdemaktas@google.com, Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, linux-coco@lists.linux.dev, Chao Peng , Ackerley Tng , Vishal Annapurve , Yuan Yao , Jarkko Sakkinen , Xu Yilun , Quentin Perret , wei.w.wang@intel.com, Fuad Tabba Subject: [RFC PATCH v2 5/6] KVM: selftests: Add test cases for KVM_GUEST_MEMORY_FAILURE Date: Thu, 21 Sep 2023 13:14:38 -0700 Message-Id: <71be0cea7cd1ed2d7d20403a803213a9e2b28c11.1695327124.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.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 (fry.vger.email [0.0.0.0]); Thu, 21 Sep 2023 13:42:11 -0700 (PDT) From: Isaku Yamahata Add text cases for guest_memfd KVM_GUEST_MEMORY_FAILURE ioctl. + ioctl(KVM_GUEST_MEMORY_FAILURE) success with backing pages + ioctl(KVM_GUEST_MEMORY_FAILURE) fails with ENOENT without backing page + interaction with fallocate(PUNCH_HOLE) Signed-off-by: Isaku Yamahata --- .../testing/selftests/kvm/guest_memfd_test.c | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index d5b4bfcdc3fe..f8b242c9319d 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -122,6 +122,53 @@ static void test_fallocate_fail(int fd, size_t page_size, size_t total_size) } } +static void test_memory_failure(int fd, size_t page_size, size_t total_size) +{ + struct kvm_guest_memory_failure mf; + int ret; + int i; + + /* Make whole file unallocated as known initial state */ + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + 0, total_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) at while file should succeed"); + + /* Because there is no backing page, fail to inject memory failure. */ + for (i = 0; i < total_size / page_size; i++) { + mf = (struct kvm_guest_memory_failure) { + .offset = i * page_size, + }; + + ret = ioctl(fd, KVM_GUEST_MEMORY_FAILURE, &mf); + if (ret == -1 && errno == EPERM) { + pr_info("KVM_GUEST_MEMORY_FAILURE requires CAP_SYS_ADMIN. Skipping.\n") + return; + } + TEST_ASSERT(ret == -1 && errno == ENOENT, + "ioctl(KVM_GUEST_MEMORY_FAILURE) should fail i %d", i); + } + + /* Allocate pages with index one and two. */ + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, page_size, page_size * 2); + TEST_ASSERT(!ret, "fallocate beginning at page_size should succeed"); + + for (i = 0; i < total_size / page_size; i++) { + mf = (struct kvm_guest_memory_failure) { + .offset = i * page_size, + }; + + ret = ioctl(fd, KVM_GUEST_MEMORY_FAILURE, &mf); + + if (i == 1 || i == 2) { + TEST_ASSERT(!ret || (ret == -1 && errno == EBUSY), + "ioctl(KVM_GUEST_MEMORY_FAILURE) should succeed i %d", i); + } else { + TEST_ASSERT(ret == -1 && errno == ENOENT, + "ioctl(KVM_GUEST_MEMORY_FAILURE) should fail i %d", i); + } + } +} + static void test_create_guest_memfd_invalid(struct kvm_vm *vm) { uint64_t valid_flags = 0; @@ -192,6 +239,7 @@ int main(int argc, char *argv[]) test_file_size(fd, page_size, total_size); test_fallocate(fd, page_size, total_size); test_fallocate_fail(fd, page_size, total_size); + test_memory_failure(fd, page_size, total_size); close(fd); } -- 2.25.1