From patchwork Tue Sep 10 23:43:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13799496 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90B8BEE01F2 for ; Tue, 10 Sep 2024 23:45:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16C348D00DB; Tue, 10 Sep 2024 19:45:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E9638D00E5; Tue, 10 Sep 2024 19:45:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B1B98D00E2; Tue, 10 Sep 2024 19:45:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0BBF38D00DB for ; Tue, 10 Sep 2024 19:45:12 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AFACCC0E2E for ; Tue, 10 Sep 2024 23:45:11 +0000 (UTC) X-FDA: 82550462022.18.2A9A953 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf23.hostedemail.com (Postfix) with ESMTP id CE77614001C for ; Tue, 10 Sep 2024 23:45:09 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q7o42NY6; spf=pass (imf23.hostedemail.com: domain of 3A9rgZgsKCHgWYganhaupjcckkcha.Ykihejqt-iigrWYg.knc@flex--ackerleytng.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3A9rgZgsKCHgWYganhaupjcckkcha.Ykihejqt-iigrWYg.knc@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726011772; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/qKZmcmtlvLxUiaB9mxVA7dCbtHF2c/ntC+IAYmWTbE=; b=n2LYwoKRPZnr2YV8el/lYktiSEgxpadrjcvQdJsX3fSGGDfzURwDJrW8Axjye0M2uBdxHc oEaeymyEent5wbomSRHob5NIHeIZQjYcood9Ph1y/qghjjuiD3DiL9hwQTM8IGzQLmsHVy 7h0XokHlLpZZ8GMnk5D+Xz5UdeeUvdk= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q7o42NY6; spf=pass (imf23.hostedemail.com: domain of 3A9rgZgsKCHgWYganhaupjcckkcha.Ykihejqt-iigrWYg.knc@flex--ackerleytng.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3A9rgZgsKCHgWYganhaupjcckkcha.Ykihejqt-iigrWYg.knc@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726011772; a=rsa-sha256; cv=none; b=KcxXXDoOYHfj2bCVOhrIpbwRPemim3nyaJ2SM9ato/uy/XoSXDuYrcHMevLtb/wnVqoT+y lVPdaFG6WH1/MsiUAgLUVVY493ap35xXrjbkp9SPkuQtMYiljRRqz5HP7NmcXHG8gCgUzF zqyCvoKwvLQps3JDFPVk73kMRzrXaUo= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-206e07915c2so58148675ad.3 for ; Tue, 10 Sep 2024 16:45:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726011908; x=1726616708; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/qKZmcmtlvLxUiaB9mxVA7dCbtHF2c/ntC+IAYmWTbE=; b=q7o42NY6apEXBRC4cGJOlj2foNBSX9sKsTVXg5s9VqPjbl4H7ixL/8BAbIgjg6XK7A zJgRyklZU67F3IUWeJWyb6usN4aHxu99HI1swOObXmkMtNWFNZaO6FdaNwMN9BfrcigJ /uzJLbPxO28Q6jhGQc160Qdb5HsNiRNJU8YZhQ9BhIUW/fiVAX4ZoXcCyNHIGfUCj2WY 1OE7tAfS4KC52688Y2sYIoPgIebtG1tVwfdQNjxNbOA/k/ao/1GOdi1mh/v2kYbVL/xA 4dlFBJRyzoDaNwNLc4rPSlSkPeJKpB0PEzDlSLCEcdaOs+CYlWwRijTYFUPyv0+XazE1 eulA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726011908; x=1726616708; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/qKZmcmtlvLxUiaB9mxVA7dCbtHF2c/ntC+IAYmWTbE=; b=KQfUyohUOYe4jqSToX/N/yCtBvj1DriuRwJRD/FZ8m0iErJje4Q8Kx1jjmcDqKTb7r 08dc1FKnT5Jp1l1xEUnrygsLTOG5C1w+vYSU3ZIFsFNx3cgs/xd6ZnhLEcX7jdzR5hQ/ +pEhvkpZuDacD6glXP+UoV19M8igpExQ/2SnicB/fvqZJNpC8y7rd6mq29JlLiK4JbLR NnhsBdERzuikvHo9kSCSH/0W2WqzPgCjjFpm2MDPQMAoSyQHAgw/ReBZjMFyriraAosQ RrimTZ2CRPXB5GOymEAUeiZesWymHILn9yDya/dO6Lh8ykBV8vs4CoygexdiQ4dJa1mF q1IA== X-Forwarded-Encrypted: i=1; AJvYcCXU9gXPaR7OjbnUaW4Rp5ifHuklWTAURwUn9EMb6mm7USkaJESFDEFpH67+BSLm6oVD5tZQLessuQ==@kvack.org X-Gm-Message-State: AOJu0Yz/wr2WjoK74+hK00fNN9rQgaPLhQmqO7zd4lpBkY+o5B1rvSzo hzbMeZd/5+5oD4JW0u/kmlj5V2vpcZPudjNbs93qkXt05gBa9KlPySAArTKhUh3ZqEyt5KvElJR gAhYwDtCHGDmCPC96IBeZFw== X-Google-Smtp-Source: AGHT+IFg8OuONbuLb/Y7bD7B++eP0JBBTd74fUtN+JQ8GVPvsqKbJU+z/AbwfH4duxwkO4C0+G16tdQi6/1xEgA62w== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a17:902:c946:b0:206:a858:40d0 with SMTP id d9443c01a7336-2074c7cb495mr2763305ad.9.1726011907972; Tue, 10 Sep 2024 16:45:07 -0700 (PDT) Date: Tue, 10 Sep 2024 23:43:52 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog Message-ID: <405825c1c3924ca534da3016dda812df17d6c233.1726009989.git.ackerleytng@google.com> Subject: [RFC PATCH 21/39] KVM: selftests: Test that guest_memfd usage is reported via hugetlb From: Ackerley Tng To: tabba@google.com, quic_eberman@quicinc.com, roypat@amazon.co.uk, jgg@nvidia.com, peterx@redhat.com, david@redhat.com, rientjes@google.com, fvdl@google.com, jthoughton@google.com, seanjc@google.com, pbonzini@redhat.com, zhiquan1.li@intel.com, fan.du@intel.com, jun.miao@intel.com, isaku.yamahata@intel.com, muchun.song@linux.dev, mike.kravetz@oracle.com Cc: erdemaktas@google.com, vannapurve@google.com, ackerleytng@google.com, qperret@google.com, jhubbard@nvidia.com, willy@infradead.org, shuah@kernel.org, brauner@kernel.org, bfoster@redhat.com, kent.overstreet@linux.dev, pvorel@suse.cz, rppt@kernel.org, richard.weiyang@gmail.com, anup@brainfault.org, haibo1.xu@intel.com, ajones@ventanamicro.com, vkuznets@redhat.com, maciej.wieczor-retman@intel.com, pgonda@google.com, oliver.upton@linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-fsdevel@kvack.org X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: CE77614001C X-Stat-Signature: cg79re686ertekrwd5g9ek4yn11xhbxd X-Rspam-User: X-HE-Tag: 1726011909-140508 X-HE-Meta: U2FsdGVkX1+ugmSzDSsj/3zPJqDvI8IK1pAj32feBPbAEZ3cQN8Zg/uWVObKzIazzkqpOJAWQr76lcCdiT+h7u6av71avd4bvMxXPW/S8C1qwBZ9J4gl4/BWPm7G4dBhe29HHdAVTuBTycmuhM5KdhLzm5xDVS0fYR2FQ5K+zo1wsYZ/0jx4TMCQGLcSwmQYHuFU9sbgECXrFieLLb1TlxtwnQLAgQ2+qhUiVpd9ZbnhLidOGf09QHrl0wpZev0mvkqN8HcBZggFcBDGlvoYImpGgbmJ/QvGriP331OI7qzevFj6nROFsgkILPCsYjoj1Ovw62xwzKsNX6wRnwF9xb49Tdb1V2XLkYIG9iORbdcaWuObZ28T6kQcgkfAoDrYQKvQ1T0XHpvVLQpm70blqS/pYGOmnwC0O9mOMWxPHLf9z6rC20f55QVI8iQ3T9mmdTXcqMtpysjTWh9rAQbZMxXgFq0tUfChLfZIGpg52g7jzrAhHn7UDGhdyMYVKAgqTA+uU8lig3dizU0eec/BQOAFeREkkGkVXw90bNkQ5arg6ACqWx/uZ4oB2Lm7c8lSVsTWYGKkVOTDG69mnDQkf0nWzn73r7UyOrFrwQ7fSElcTnG47uR4CCXYIRQVO7u0ixkvhNNQCFc+fV3soaCdbiVPnA22o4DEzPjurJRGYxFG+ixgG+DTEUE9g9gcPqT0KUEY0R09+T/n7eXL0B2ZUKe6XzeBUsS9GNCxi4dR8M82nfpmC8N/OfS7zuQaP8pXbo1FsX4jJRDs7qseWTZilHh508As1j0gdBZZeuYmXBz3HIKCKQlICQYoOG2iHpVtXCjTVoltYDlTQl8m4GIj35IeSkH8N8mP/fS+kiSnYCWuwPLa0lvjiJNfhl8zoz5E3TanLRqCFF5T+Kuv9LYov3IsaDhKQSlGaQgpRxxvuhAMkuSr55kA+5WWIgdAdbUeu15XFPRzJejEk2WrRoW /C+7l1Jq ky40FoBd0mSmpONbTEwjyW6ifb1DD0aFCHOs+FpB1JJp143/+VHsOaBdfsXdR2nwYH08lqaHoTcCQ3QYdASrv+2mIhfnmfJ7ezX0oXMbDVI4rtT4X/67v7jDoyQjV/kesd8ecG/JU1E7BYMbcNzS1eBSKewQfh6rLc1InPJePPMg2ul1l3hxn+F8qX4G6ytg4Nrk3SbJTtKUsWrvgRXX33vax21sycTnVt64113x0WTgo/o/JBmZvmuOyaMe+xO8OJN3/y0fQzwO4Y+J8lMiYsngLFSvlu8we61Cw+34N33v+R0xFr2ulmET8NHnpgK0m6JdKcdkUZLQJQa0cjEKQATueJgv5EXLr3+sLd59AvvFvUP8G8IXOj7cpf4LBorPU/AA9IRFMcTaAQ+bzADg7gflPl1cxrVxQptR2UP7glNvGcwczkolQLKni8KjxVuivOswpysYrTtFT2w+fGOl59+Yix2jp4Q4XW+Bxnq8L+o5WFfPnA/WBWs7BNpAMw19zHKcWasZMDKf+BFicpje1F8vs0o0e/9P3qa2W91SqzmW/lgDae2IF+Kb/eg8GSHxB/6sUUp7AY3Le+0FBNhBvPYn0CxP1ydrxSklxWdZzNXO1MVNpyccz09NB15TGE2TXw/AfxR8bU3eiwxw0aTz7IQMuPsM0Rz1bJHN5nlcegKPB0rI9Q+IB8wRlfnhH2OWn5tk3y1FLowHjQ94= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Using HugeTLB as the huge page allocator for guest_memfd allows reuse of HugeTLB's reporting mechanism. Signed-off-by: Ackerley Tng --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/guest_memfd_hugetlb_reporting_test.c | 222 ++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 tools/testing/selftests/kvm/guest_memfd_hugetlb_reporting_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 48d32c5aa3eb..b3b7e83f39fc 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -134,6 +134,7 @@ TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test TEST_GEN_PROGS_x86_64 += dirty_log_perf_test TEST_GEN_PROGS_x86_64 += guest_memfd_test +TEST_GEN_PROGS_x86_64 += guest_memfd_hugetlb_reporting_test TEST_GEN_PROGS_x86_64 += guest_print_test TEST_GEN_PROGS_x86_64 += hardware_disable_test TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus diff --git a/tools/testing/selftests/kvm/guest_memfd_hugetlb_reporting_test.c b/tools/testing/selftests/kvm/guest_memfd_hugetlb_reporting_test.c new file mode 100644 index 000000000000..cb9fdf0d4ec8 --- /dev/null +++ b/tools/testing/selftests/kvm/guest_memfd_hugetlb_reporting_test.c @@ -0,0 +1,222 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "kvm_util.h" +#include "test_util.h" +#include "processor.h" + +static int read_int(const char *file_name) +{ + FILE *fp; + int num; + + fp = fopen(file_name, "r"); + TEST_ASSERT(fp != NULL, "Error opening file %s!\n", file_name); + + TEST_ASSERT_EQ(fscanf(fp, "%d", &num), 1); + + fclose(fp); + + return num; +} + +enum hugetlb_statistic { + FREE_HUGEPAGES, + NR_HUGEPAGES, + NR_OVERCOMMIT_HUGEPAGES, + RESV_HUGEPAGES, + SURPLUS_HUGEPAGES, + NR_TESTED_HUGETLB_STATISTICS, +}; + +static const char *hugetlb_statistics[NR_TESTED_HUGETLB_STATISTICS] = { + [FREE_HUGEPAGES] = "free_hugepages", + [NR_HUGEPAGES] = "nr_hugepages", + [NR_OVERCOMMIT_HUGEPAGES] = "nr_overcommit_hugepages", + [RESV_HUGEPAGES] = "resv_hugepages", + [SURPLUS_HUGEPAGES] = "surplus_hugepages", +}; + +enum test_page_size { + TEST_SZ_2M, + TEST_SZ_1G, + NR_TEST_SIZES, +}; + +struct test_param { + size_t page_size; + int memfd_create_flags; + int guest_memfd_flags; + char *path_suffix; +}; + +const struct test_param *test_params(enum test_page_size size) +{ + static const struct test_param params[] = { + [TEST_SZ_2M] = { + .page_size = PG_SIZE_2M, + .memfd_create_flags = MFD_HUGETLB | MFD_HUGE_2MB, + .guest_memfd_flags = KVM_GUEST_MEMFD_HUGETLB | KVM_GUEST_MEMFD_HUGE_2MB, + .path_suffix = "2048kB", + }, + [TEST_SZ_1G] = { + .page_size = PG_SIZE_1G, + .memfd_create_flags = MFD_HUGETLB | MFD_HUGE_1GB, + .guest_memfd_flags = KVM_GUEST_MEMFD_HUGETLB | KVM_GUEST_MEMFD_HUGE_1GB, + .path_suffix = "1048576kB", + }, + }; + + return ¶ms[size]; +} + +static int read_statistic(enum test_page_size size, enum hugetlb_statistic statistic) +{ + char path[PATH_MAX] = "/sys/kernel/mm/hugepages/hugepages-"; + + strcat(path, test_params(size)->path_suffix); + strcat(path, "/"); + strcat(path, hugetlb_statistics[statistic]); + + return read_int(path); +} + +static int baseline[NR_TEST_SIZES][NR_TESTED_HUGETLB_STATISTICS]; + +static void establish_baseline(void) +{ + int i, j; + + for (i = 0; i < NR_TEST_SIZES; ++i) + for (j = 0; j < NR_TESTED_HUGETLB_STATISTICS; ++j) + baseline[i][j] = read_statistic(i, j); +} + +static void assert_stats_at_baseline(void) +{ + TEST_ASSERT_EQ(read_statistic(TEST_SZ_2M, FREE_HUGEPAGES), + baseline[TEST_SZ_2M][FREE_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(TEST_SZ_2M, NR_HUGEPAGES), + baseline[TEST_SZ_2M][NR_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(TEST_SZ_2M, NR_OVERCOMMIT_HUGEPAGES), + baseline[TEST_SZ_2M][NR_OVERCOMMIT_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(TEST_SZ_2M, RESV_HUGEPAGES), + baseline[TEST_SZ_2M][RESV_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(TEST_SZ_2M, SURPLUS_HUGEPAGES), + baseline[TEST_SZ_2M][SURPLUS_HUGEPAGES]); + + TEST_ASSERT_EQ(read_statistic(TEST_SZ_1G, FREE_HUGEPAGES), + baseline[TEST_SZ_1G][FREE_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(TEST_SZ_1G, NR_HUGEPAGES), + baseline[TEST_SZ_1G][NR_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(TEST_SZ_1G, NR_OVERCOMMIT_HUGEPAGES), + baseline[TEST_SZ_1G][NR_OVERCOMMIT_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(TEST_SZ_1G, RESV_HUGEPAGES), + baseline[TEST_SZ_1G][RESV_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(TEST_SZ_1G, SURPLUS_HUGEPAGES), + baseline[TEST_SZ_1G][SURPLUS_HUGEPAGES]); +} + +static void assert_stats(enum test_page_size size, int num_reserved, int num_faulted) +{ + TEST_ASSERT_EQ(read_statistic(size, FREE_HUGEPAGES), + baseline[size][FREE_HUGEPAGES] - num_faulted); + TEST_ASSERT_EQ(read_statistic(size, NR_HUGEPAGES), + baseline[size][NR_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(size, NR_OVERCOMMIT_HUGEPAGES), + baseline[size][NR_OVERCOMMIT_HUGEPAGES]); + TEST_ASSERT_EQ(read_statistic(size, RESV_HUGEPAGES), + baseline[size][RESV_HUGEPAGES] + num_reserved - num_faulted); + TEST_ASSERT_EQ(read_statistic(size, SURPLUS_HUGEPAGES), + baseline[size][SURPLUS_HUGEPAGES]); +} + +/* Use hugetlb behavior as a baseline. guest_memfd should have comparable behavior. */ +static void test_hugetlb_behavior(enum test_page_size test_size) +{ + const struct test_param *param; + char *mem; + int memfd; + + param = test_params(test_size); + + assert_stats_at_baseline(); + + memfd = memfd_create("guest_memfd_hugetlb_reporting_test", + param->memfd_create_flags); + + mem = mmap(NULL, param->page_size, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_HUGETLB, memfd, 0); + TEST_ASSERT(mem != MAP_FAILED, "Couldn't mmap()"); + + assert_stats(test_size, 1, 0); + + *mem = 'A'; + + assert_stats(test_size, 1, 1); + + munmap(mem, param->page_size); + + assert_stats(test_size, 1, 1); + + madvise(mem, param->page_size, MADV_DONTNEED); + + assert_stats(test_size, 1, 1); + + madvise(mem, param->page_size, MADV_REMOVE); + + assert_stats(test_size, 1, 1); + + close(memfd); + + assert_stats_at_baseline(); +} + +static void test_guest_memfd_behavior(enum test_page_size test_size) +{ + const struct test_param *param; + struct kvm_vm *vm; + int guest_memfd; + + param = test_params(test_size); + + assert_stats_at_baseline(); + + vm = vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM); + + guest_memfd = vm_create_guest_memfd(vm, param->page_size, + param->guest_memfd_flags); + + assert_stats(test_size, 1, 0); + + fallocate(guest_memfd, FALLOC_FL_KEEP_SIZE, 0, param->page_size); + + assert_stats(test_size, 1, 1); + + fallocate(guest_memfd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, + param->page_size); + + assert_stats(test_size, 1, 0); + + close(guest_memfd); + + assert_stats_at_baseline(); + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + establish_baseline(); + + test_hugetlb_behavior(TEST_SZ_2M); + test_hugetlb_behavior(TEST_SZ_1G); + + test_guest_memfd_behavior(TEST_SZ_2M); + test_guest_memfd_behavior(TEST_SZ_1G); +}