From nobody Thu Dec 18 05:07:17 2025 Received: from mail-qv1-f74.google.com (mail-qv1-f74.google.com [209.85.219.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F1F378F59 for ; Thu, 27 Mar 2025 01:24:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038694; cv=none; b=OVUjOE4jg8cVUZQ2ZC2XBbH3FDHobAKujBA6BG8uqph+Yz703xxDJsc7pGw70ReQBdnbB8kEWYAfWDXQj8rmg83NBfuSOLaHwSX6yPmXLr60wo3YUWgkewn48beevBcv3EJFiutYSnVq2VJ9cVcro6I6d5e2ORoMa2yIWE7XMjE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038694; c=relaxed/simple; bh=sVPmJtk+M0QFe2DpgpAtXxhYDfGr4TqJUl6HVgYb1yE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=h8fpvw1HHgukeTS5ufXMB5XUZKzlmJ4K8/Alge70o4XYYouEtQL213y4DMF1TOd3QEzls4SBuFDX6XbYHAO+0jtqvo9b9juHX9VIPcl3PBa7iPDXFiRHuDnQoGhfxQjanTgCk76JOC7ZwElZCujlLbmARpl15mP9/rs2zG80xu0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NDgnPe2H; arc=none smtp.client-ip=209.85.219.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NDgnPe2H" Received: by mail-qv1-f74.google.com with SMTP id 6a1803df08f44-6e91a184588so10995346d6.2 for ; Wed, 26 Mar 2025 18:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743038691; x=1743643491; darn=vger.kernel.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=liGs0zsmCzXyApDpxJa5hQb5aVWUAwjiM/fW15pHclY=; b=NDgnPe2HKi3P7wPlUM9MeskIM3MiwgVMz+sUgld+TwKnpcFg4vxMhlQlm6JNALCqxh iDl29Crucip5+l6BRze2apweoyfR9Y8/+zmeqBSH/JkTWM6jSAO1k1PScLGINCsaouTu AGuVD31gTbGOixL4Pk25AqwEFc4ebX2u219f43uHEoKmCziX0QJNSzIWEaYQODc+kC3j LpVpkp6NiuN7Vmhhoc5zXaTU9kDWK8tE5VTub6ugCCJKuUPX0ac2XGYYLiZy2vXYP/VY A1pKSrDIO87df6y5UIOYhHO22Ci98A/H7/jkw7UGw5Z+xeNX4qWJ74+UdgqcLeryPlcX wk7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743038691; x=1743643491; 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=liGs0zsmCzXyApDpxJa5hQb5aVWUAwjiM/fW15pHclY=; b=oIQkJYDPqBJNp4ey0mx6weiiq9c52jpidsT6+f6ICSYEuBMm/7PBNIIEFv3Gn/kFBI kC8o84zvj5tym8+VCRIoQEuXC6XojaYiS/VFfnv5fWuX0SI9FkG8yBYX7KR6v7uMR935 4+VqtOJi8xmm18Jpj+YHfCplB3BfT3jgwjflFPGr7jHBIwlNGEfdcVnSzESrF9ZySZNj 71249OCm38KN1tDG+FYlQqqSdETWOBewsadcVF5Z5951Eo6u/BqHoJ5j2JJYTp+kCrLc yWwL8WCA5QhcbZXIhw+BJ8s9/vDNwNlhPye0GzuGLZHxsL9q9QiLO5S2Ic8RymQoJMLB lc7A== X-Forwarded-Encrypted: i=1; AJvYcCUbMeKnnHQGKHIVVdxIM6/BlyXo+iNSsSCCs3O82oJATaY90RcjQ0iHYJovi3DsReRVwIJ6zseAR3kJQh0=@vger.kernel.org X-Gm-Message-State: AOJu0YzYJ17mLu2B2MT9WfGnsvx1Sa94HX/1v+R2rAgM0KvdE9NisPut MYA2PatUxFNkotxm5oo1fWPt9qF0rSJcZBYAQSnDM1w0XfXfqanNAvLeS8+jlpBeKk+nFLhCjOA 5MsL8rJAgMuaXP+6hMg== X-Google-Smtp-Source: AGHT+IH7BAFDxR8LyIf/cCmDc14HH7jyC//3aG9i1kJW7Fvp8ZskK6kVxOKCBos5v4Y1+Vq5kmrmwnBeThHJhsNb X-Received: from qvbrf3.prod.google.com ([2002:a05:6214:5f83:b0:6ec:dd52:e52c]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6214:2585:b0:6e8:fbe2:2db0 with SMTP id 6a1803df08f44-6ed23912adfmr23153566d6.30.1743038691331; Wed, 26 Mar 2025 18:24:51 -0700 (PDT) Date: Thu, 27 Mar 2025 01:23:46 +0000 In-Reply-To: <20250327012350.1135621-1-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250327012350.1135621-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog Message-ID: <20250327012350.1135621-2-jthoughton@google.com> Subject: [PATCH 1/5] KVM: selftests: Extract guts of THP accessor to standalone sysfs helpers From: James Houghton To: Sean Christopherson , kvm@vger.kernel.org Cc: Maxim Levitsky , Axel Rasmussen , Tejun Heo , Johannes Weiner , mkoutny@suse.com, Yu Zhao , James Houghton , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sean Christopherson Extract the guts of thp_configured() and get_trans_hugepagesz() to standalone helpers so that the core logic can be reused for other sysfs files, e.g. to query numa_balancing. Opportunistically assert that the initial fscanf() read at least one byte, and add a comment explaining the second call to fscanf(). Signed-off-by: Sean Christopherson Signed-off-by: James Houghton --- tools/testing/selftests/kvm/lib/test_util.c | 35 ++++++++++++++------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/se= lftests/kvm/lib/test_util.c index 8ed0b74ae8373..3dc8538f5d696 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -132,37 +132,50 @@ void print_skip(const char *fmt, ...) puts(", skipping test"); } =20 -bool thp_configured(void) +static bool test_sysfs_path(const char *path) { - int ret; struct stat statbuf; + int ret; =20 - ret =3D stat("/sys/kernel/mm/transparent_hugepage", &statbuf); + ret =3D stat(path, &statbuf); TEST_ASSERT(ret =3D=3D 0 || (ret =3D=3D -1 && errno =3D=3D ENOENT), - "Error in stating /sys/kernel/mm/transparent_hugepage"); + "Error in stat()ing '%s'", path); =20 return ret =3D=3D 0; } =20 -size_t get_trans_hugepagesz(void) +bool thp_configured(void) +{ + return test_sysfs_path("/sys/kernel/mm/transparent_hugepage"); +} + +static size_t get_sysfs_val(const char *path) { size_t size; FILE *f; int ret; =20 - TEST_ASSERT(thp_configured(), "THP is not configured in host kernel"); - - f =3D fopen("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "r"); - TEST_ASSERT(f !=3D NULL, "Error in opening transparent_hugepage/hpage_pmd= _size"); + f =3D fopen(path, "r"); + TEST_ASSERT(f, "Error opening '%s'", path); =20 ret =3D fscanf(f, "%ld", &size); + TEST_ASSERT(ret > 0, "Error reading '%s'", path); + + /* Re-scan the input stream to verify the entire file was read. */ ret =3D fscanf(f, "%ld", &size); - TEST_ASSERT(ret < 1, "Error reading transparent_hugepage/hpage_pmd_size"); - fclose(f); + TEST_ASSERT(ret < 1, "Error reading '%s'", path); =20 + fclose(f); return size; } =20 +size_t get_trans_hugepagesz(void) +{ + TEST_ASSERT(thp_configured(), "THP is not configured in host kernel"); + + return get_sysfs_val("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size"= ); +} + size_t get_def_hugetlb_pagesz(void) { char buf[64]; --=20 2.49.0.395.g12beb8f557-goog From nobody Thu Dec 18 05:07:17 2025 Received: from mail-ua1-f74.google.com (mail-ua1-f74.google.com [209.85.222.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2C6C81741 for ; Thu, 27 Mar 2025 01:24:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038696; cv=none; b=sCC7JzJmfaNk6MEe+enTRiBz+Utf0oo9pPzVegb/y+r8c+JaQVKSKzW3reo7SdEF2LtzM2h/CQw7doaxPKcfyUFDBuH0jVTDwhYTkQj7tugVKJxk0V0QOwus01EO8h9Vt8ZqVkuFopJvTr7sueEvegF06CBsNOx3+TNODjISvw0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038696; c=relaxed/simple; bh=t3Wu8WcpTKr4Hin+dqDU2xino9yOT2vt8H5/bautCRE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GTrE0MZ04Q6xlF5cASccV/HghArruzYyIyYx0yLjyTLAHpB2S5yVQ3q1rZroOejj0/ME8sYuvWdiozsAjj32t8kpiAZKLvTcZKrOs2iz5kn2aLlktTXjHTR3/7hFJamdgDTaTFbU5vVvrzUfRBGf6jjibGWdTb5JBDmnt8tnKcM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vDqshJIt; arc=none smtp.client-ip=209.85.222.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vDqshJIt" Received: by mail-ua1-f74.google.com with SMTP id a1e0cc1a2514c-86d383c48ccso141468241.3 for ; Wed, 26 Mar 2025 18:24:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743038694; x=1743643494; darn=vger.kernel.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=v0fJldEh3ljJ4akdD7WwAxn/Ekty9pCM03IX50ZdZwg=; b=vDqshJIt4TrIrHLwYkq2s+Ews+BnUk+gqOBJzu7lltl4HgZpUlnj8+0JntsuC5Wx3C zbWwuWKMohwzV3jg7+UmKhmZ8mlrUYYePEfzo0lAiatpG0UyKs5nEEi8i/2yQa6MQrpR w8s6VxRF2ZYF5ukqSO4pC4DWI4IZCiqF+TTT/Rjyvoan9OdwR15WR/NMxym4n9l/sULa DCQm/UQ3w8HjBtCu4/fDkLdd2f9W+Z5J8hiS8IdkHmAmBg+Wqrjslu4n3kVuvqcotiby lzUZYXYLAS441qTEIGCFTr1EvmL8XKzdhqTSB9rOrmBxsh4LIt9y7XV8gwlev+RYwh/O yLYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743038694; x=1743643494; 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=v0fJldEh3ljJ4akdD7WwAxn/Ekty9pCM03IX50ZdZwg=; b=vJFKuWO9x7UP7V4MwAPnXm9uyww8odVDwSo1OJirp7otif8cHODx9b/lS6/damutmB wufoL7mhEBy1EnFLCFXwFHfxDsGsVA04SZJxjMdWUnAW5ijXrobZAQBEz0syQNZb0PiE 83HeO2juN9YBjX97iaOeWjiwkuVPnTcR0Fu8vmennPw9UE7VDSz3YSi2ZJoxraFJ/jYW LhmpDJnjt4bHj9yCKBz0rY9/8kRmoauMPwzJwXRwyYVT6sNok3UUpKwsKae88w5FjGsD 4N8tnFAVfTKbEx8yon8HngoyBAuWl57ZGRFBV8ak2610c8i0FNZl50L7MXS0ISPMAwdu YsJw== X-Forwarded-Encrypted: i=1; AJvYcCX3MwIXz92RwziT+DCPiAxxDG6Ax5qkpTCAjO/1yeySS6moeCbWbLc98NZFPokloWFJ6FIuRVNnFoyhIt0=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4qNi6w0baewzg9AGHui1fq9Cl+Fre3eO70NIu/ZoP9p/oSiB5 Da16M1Sn/kcOxEAC7H//trIkuP3RVZOafbKvcDfJHGeq876hV1zOQdP+e8Hz7Np7Gj/v9uXy3L5 MgO7XvX5Ub1yo/ZXqpw== X-Google-Smtp-Source: AGHT+IFSht9Rl/okSnd4o3+xfp04VCJSDQHoeYgTEaziVfScJ5lh7/JF2NmdZD1IhD0J/mpc5enurRpuXJWld2NG X-Received: from uabil9.prod.google.com ([2002:a05:6130:6009:b0:86b:9d15:75ab]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:3585:b0:4c5:1bff:4589 with SMTP id ada2fe7eead31-4c5870b7afcmr2199208137.22.1743038693829; Wed, 26 Mar 2025 18:24:53 -0700 (PDT) Date: Thu, 27 Mar 2025 01:23:47 +0000 In-Reply-To: <20250327012350.1135621-1-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250327012350.1135621-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog Message-ID: <20250327012350.1135621-3-jthoughton@google.com> Subject: [PATCH 2/5] KVM: selftests: access_tracking_perf_test: Add option to skip the sanity check From: James Houghton To: Sean Christopherson , kvm@vger.kernel.org Cc: Maxim Levitsky , Axel Rasmussen , Tejun Heo , Johannes Weiner , mkoutny@suse.com, Yu Zhao , James Houghton , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky Add an option to skip sanity check of number of still idle pages, and set it by default to skip, in case hypervisor or NUMA balancing is detected. Signed-off-by: Maxim Levitsky Co-developed-by: James Houghton Signed-off-by: James Houghton Reviewed-by: Maxim Levitsky --- .../selftests/kvm/access_tracking_perf_test.c | 61 ++++++++++++++++--- .../testing/selftests/kvm/include/test_util.h | 1 + tools/testing/selftests/kvm/lib/test_util.c | 7 +++ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/access_tracking_perf_test.c b/tool= s/testing/selftests/kvm/access_tracking_perf_test.c index 447e619cf856e..0e594883ec13e 100644 --- a/tools/testing/selftests/kvm/access_tracking_perf_test.c +++ b/tools/testing/selftests/kvm/access_tracking_perf_test.c @@ -65,6 +65,16 @@ static int vcpu_last_completed_iteration[KVM_MAX_VCPUS]; /* Whether to overlap the regions of memory vCPUs access. */ static bool overlap_memory_access; =20 +/* + * If the test should only warn if there are too many idle pages (i.e., it= is + * expected). + * -1: Not yet set. + * 0: We do not expect too many idle pages, so FAIL if too many idle page= s. + * 1: Having too many idle pages is expected, so merely print a warning if + * too many idle pages are found. + */ +static int idle_pages_warn_only =3D -1; + struct test_params { /* The backing source for the region of memory. */ enum vm_mem_backing_src_type backing_src; @@ -177,18 +187,12 @@ static void mark_vcpu_memory_idle(struct kvm_vm *vm, * arbitrary; high enough that we ensure most memory access went through * access tracking but low enough as to not make the test too brittle * over time and across architectures. - * - * When running the guest as a nested VM, "warn" instead of asserting - * as the TLB size is effectively unlimited and the KVM doesn't - * explicitly flush the TLB when aging SPTEs. As a result, more pages - * are cached and the guest won't see the "idle" bit cleared. */ if (still_idle >=3D pages / 10) { -#ifdef __x86_64__ - TEST_ASSERT(this_cpu_has(X86_FEATURE_HYPERVISOR), + TEST_ASSERT(idle_pages_warn_only, "vCPU%d: Too many pages still idle (%lu out of %lu)", vcpu_idx, still_idle, pages); -#endif + printf("WARNING: vCPU%d: Too many pages still idle (%lu out of %lu), " "this will affect performance results.\n", vcpu_idx, still_idle, pages); @@ -328,6 +332,31 @@ static void run_test(enum vm_guest_mode mode, void *ar= g) memstress_destroy_vm(vm); } =20 +static int access_tracking_unreliable(void) +{ +#ifdef __x86_64__ + /* + * When running nested, the TLB size is effectively unlimited and the + * KVM doesn't explicitly flush the TLB when aging SPTEs. As a result, + * more pages are cached and the guest won't see the "idle" bit cleared. + */ + if (this_cpu_has(X86_FEATURE_HYPERVISOR)) { + puts("Skipping idle page count sanity check, because the test is run nes= ted"); + return 1; + } +#endif + /* + * When NUMA balancing is enabled, guest memory can be mapped + * PROT_NONE, and the Accessed bits won't be queriable. + */ + if (is_numa_balancing_enabled()) { + puts("Skipping idle page count sanity check, because NUMA balancing is e= nabled"); + return 1; + } + + return 0; +} + static void help(char *name) { puts(""); @@ -342,6 +371,12 @@ static void help(char *name) printf(" -v: specify the number of vCPUs to run.\n"); printf(" -o: Overlap guest memory accesses instead of partitioning\n" " them into a separate region of memory for each vCPU.\n"); + printf(" -w: Control whether the test warns or fails if more than 10%\n" + " of pages are still seen as idle/old after accessing guest\n" + " memory. >0 =3D=3D warn only, 0 =3D=3D fail, <0 =3D=3D auto.= For auto\n" + " mode, the test fails by default, but switches to warn only\n" + " if NUMA balancing is enabled or the test detects it's runnin= g\n" + " in a VM.\n"); backing_src_help("-s"); puts(""); exit(0); @@ -359,7 +394,7 @@ int main(int argc, char *argv[]) =20 guest_modes_append_default(); =20 - while ((opt =3D getopt(argc, argv, "hm:b:v:os:")) !=3D -1) { + while ((opt =3D getopt(argc, argv, "hm:b:v:os:w:")) !=3D -1) { switch (opt) { case 'm': guest_modes_cmdline(optarg); @@ -376,6 +411,11 @@ int main(int argc, char *argv[]) case 's': params.backing_src =3D parse_backing_src_type(optarg); break; + case 'w': + idle_pages_warn_only =3D + atoi_non_negative("Idle pages warning", + optarg); + break; case 'h': default: help(argv[0]); @@ -388,6 +428,9 @@ int main(int argc, char *argv[]) "CONFIG_IDLE_PAGE_TRACKING is not enabled"); close(page_idle_fd); =20 + if (idle_pages_warn_only =3D=3D -1) + idle_pages_warn_only =3D access_tracking_unreliable(); + for_each_guest_mode(run_test, ¶ms); =20 return 0; diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testin= g/selftests/kvm/include/test_util.h index 77d13d7920cb8..c6ef895fbd9ab 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -153,6 +153,7 @@ bool is_backing_src_hugetlb(uint32_t i); void backing_src_help(const char *flag); enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name); long get_run_delay(void); +bool is_numa_balancing_enabled(void); =20 /* * Whether or not the given source type is shared memory (as opposed to diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/se= lftests/kvm/lib/test_util.c index 3dc8538f5d696..03eb99af9b8de 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -176,6 +176,13 @@ size_t get_trans_hugepagesz(void) return get_sysfs_val("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size"= ); } =20 +bool is_numa_balancing_enabled(void) +{ + if (!test_sysfs_path("/proc/sys/kernel/numa_balancing")) + return false; + return get_sysfs_val("/proc/sys/kernel/numa_balancing") =3D=3D 1; +} + size_t get_def_hugetlb_pagesz(void) { char buf[64]; --=20 2.49.0.395.g12beb8f557-goog From nobody Thu Dec 18 05:07:17 2025 Received: from mail-ua1-f73.google.com (mail-ua1-f73.google.com [209.85.222.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D46E4594A for ; Thu, 27 Mar 2025 01:24:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038699; cv=none; b=FdrxFTjZTJRuhK0YvAmKueJXzLeVmTStKCrmqncrFxcef/pbn/SoPbBaVuwRP/EnIX+AikgLQxXeWHQ/0hpl2OuBHYbypJvDiTZ+ZJ/5aw04bfzrvX69HygXvOwxe7yIE26eU2r62GWgtBxD63+lj/8jUrGZHnPYxA2vaZbz8aU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038699; c=relaxed/simple; bh=BfLa2QteAJuzJlCbZuLE5l/LTfJiKpsDE6KPN4nYm+Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dkldoebVFKrrX45Vxg6q0OPTRlwQGO9Iau8U2cIlmB8so67q1fIkZmuSad+OX0Sufmqrd/Og6M82Kb61vVAl9Ww61UgWr5RBbynhStm4TLSSsRN2qZdWcXasGDpIWubgxac1z5am2exV11oseWAl/S5EFkxvkTts44xaY/uLH+U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=KmrUwvL1; arc=none smtp.client-ip=209.85.222.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="KmrUwvL1" Received: by mail-ua1-f73.google.com with SMTP id a1e0cc1a2514c-86d8b6218easo124567241.0 for ; Wed, 26 Mar 2025 18:24:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743038696; x=1743643496; darn=vger.kernel.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=gGqxd23a/0EjTo66Tj0AOPzp5tI+DdYta3+Brtl9zUU=; b=KmrUwvL1hVwacRGIfD9buNw1suFiCTEnS1DkD0ZmATQfvusHIiyhG+yhWiMVlcndr/ DaVG43fkG5yGf5JQ4qpZV3SI5LSIgxD2DYreMBEw/mzq7/OHUs8+QFaZUeMQb+P+E15L 1qXsAM4ARcCBO6QskIHiAeFc/TOG1XHPbzgpx0Zqa5xfWuW2i7pYY1+p1A76ETyHozW4 UQkzVSfp662sY4Np3gZpegTZ//bCtCQKfI3KGbyftjc/x9sAShOyrrAjcYuw+uV2HFwl KKK2iXwwK2DRWnvfVFVn7NDLr2gi2VRUEca6VKkBJcCrJXdPHqUwOkCc0mAUB9THa+rj 7eSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743038696; x=1743643496; 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=gGqxd23a/0EjTo66Tj0AOPzp5tI+DdYta3+Brtl9zUU=; b=lhcolGqFVQk2lZpNCpQV9sbklQIDXwOsSKZBR9oZSd9juz+lza56Jky3MZ7ZOghdfz 3tPg52iicSGd8zdttXMBgEiIMcJdPH4akc2FjNJwpnMsQAPLDnshHG40qK7+LMvZ+H1f IVCTvcfCvjhJ3u2HM3YuAQCtRitL1TA3Qz8mMVgZ+8yOVEgnHkergdoD5+M9kSyFhzs/ 8+ok/F8g7gUZxXAXe5nwaEwD9ak9YtwMYAlFD/YueQvry6nG6KB+F76uMkWfbo+Md97B 7QwAwXvGihvBsJnQnPjDC/3BbMHllB/0Szlrgbr4N0CbnEl1or4DTiLPWagJOZP+OVMV F6tA== X-Forwarded-Encrypted: i=1; AJvYcCUU/Q6Pk7xTtFAe5rcMEgibQhXRZcCELdbj0Fg0HvQusFn0hXajYDTmaJETit/dtZgRxAf7V0awPtskJxk=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5E7q2OOC+36EkOEleYPlZKS7PSmWxezwqJjel/ihaZvzuSJ2d NcXZA0/j1OlzVkFnm9i9zTxHxSFww3CsoNG23tHcH66/ZaBmYKYVXu1Eu0rAmIUBehYH1gZXGg8 zzpuYRLWMLkeP7eZKFw== X-Google-Smtp-Source: AGHT+IHGl12nIoVpESkgRXoTlyIOOjXfd42fJqbcs3jdHsAlXFB74Da6kCtuJ1aDXxzdgLsg1P+I7e2U7pMaDK9c X-Received: from vsbby4.prod.google.com ([2002:a05:6102:5044:b0:4c5:5592:22ae]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a67:e716:0:b0:4c1:9cb2:8389 with SMTP id ada2fe7eead31-4c586efe52dmr2559753137.2.1743038696346; Wed, 26 Mar 2025 18:24:56 -0700 (PDT) Date: Thu, 27 Mar 2025 01:23:48 +0000 In-Reply-To: <20250327012350.1135621-1-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250327012350.1135621-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog Message-ID: <20250327012350.1135621-4-jthoughton@google.com> Subject: [PATCH 3/5] cgroup: selftests: Move cgroup_util into its own library From: James Houghton To: Sean Christopherson , kvm@vger.kernel.org Cc: Maxim Levitsky , Axel Rasmussen , Tejun Heo , Johannes Weiner , mkoutny@suse.com, Yu Zhao , James Houghton , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" KVM selftests will soon need to use some of the cgroup creation and deletion functionality from cgroup_util. Suggested-by: David Matlack Signed-off-by: James Houghton --- tools/testing/selftests/cgroup/Makefile | 21 ++++++++++--------- .../selftests/cgroup/{ =3D> lib}/cgroup_util.c | 3 +-- .../cgroup/{ =3D> lib/include}/cgroup_util.h | 4 ++-- .../testing/selftests/cgroup/lib/libcgroup.mk | 12 +++++++++++ 4 files changed, 26 insertions(+), 14 deletions(-) rename tools/testing/selftests/cgroup/{ =3D> lib}/cgroup_util.c (99%) rename tools/testing/selftests/cgroup/{ =3D> lib/include}/cgroup_util.h (9= 9%) create mode 100644 tools/testing/selftests/cgroup/lib/libcgroup.mk diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selfte= sts/cgroup/Makefile index 1b897152bab6e..e01584c2189ac 100644 --- a/tools/testing/selftests/cgroup/Makefile +++ b/tools/testing/selftests/cgroup/Makefile @@ -21,14 +21,15 @@ TEST_GEN_PROGS +=3D test_zswap LOCAL_HDRS +=3D $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidf= d.h =20 include ../lib.mk +include lib/libcgroup.mk =20 -$(OUTPUT)/test_core: cgroup_util.c -$(OUTPUT)/test_cpu: cgroup_util.c -$(OUTPUT)/test_cpuset: cgroup_util.c -$(OUTPUT)/test_freezer: cgroup_util.c -$(OUTPUT)/test_hugetlb_memcg: cgroup_util.c -$(OUTPUT)/test_kill: cgroup_util.c -$(OUTPUT)/test_kmem: cgroup_util.c -$(OUTPUT)/test_memcontrol: cgroup_util.c -$(OUTPUT)/test_pids: cgroup_util.c -$(OUTPUT)/test_zswap: cgroup_util.c +$(OUTPUT)/test_core: $(LIBCGROUP_O) +$(OUTPUT)/test_cpu: $(LIBCGROUP_O) +$(OUTPUT)/test_cpuset: $(LIBCGROUP_O) +$(OUTPUT)/test_freezer: $(LIBCGROUP_O) +$(OUTPUT)/test_hugetlb_memcg: $(LIBCGROUP_O) +$(OUTPUT)/test_kill: $(LIBCGROUP_O) +$(OUTPUT)/test_kmem: $(LIBCGROUP_O) +$(OUTPUT)/test_memcontrol: $(LIBCGROUP_O) +$(OUTPUT)/test_pids: $(LIBCGROUP_O) +$(OUTPUT)/test_zswap: $(LIBCGROUP_O) diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/s= elftests/cgroup/lib/cgroup_util.c similarity index 99% rename from tools/testing/selftests/cgroup/cgroup_util.c rename to tools/testing/selftests/cgroup/lib/cgroup_util.c index 1e2d46636a0ca..d5649486a11df 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/lib/cgroup_util.c @@ -16,8 +16,7 @@ #include #include =20 -#include "cgroup_util.h" -#include "../clone3/clone3_selftests.h" +#include =20 /* Returns read len on success, or -errno on failure. */ static ssize_t read_text(const char *path, char *buf, size_t max_len) diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/s= elftests/cgroup/lib/include/cgroup_util.h similarity index 99% rename from tools/testing/selftests/cgroup/cgroup_util.h rename to tools/testing/selftests/cgroup/lib/include/cgroup_util.h index 19b131ee77072..7a0441e5eb296 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.h +++ b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h @@ -2,9 +2,9 @@ #include #include =20 -#include "../kselftest.h" - +#ifndef PAGE_SIZE #define PAGE_SIZE 4096 +#endif =20 #define MB(x) (x << 20) =20 diff --git a/tools/testing/selftests/cgroup/lib/libcgroup.mk b/tools/testin= g/selftests/cgroup/lib/libcgroup.mk new file mode 100644 index 0000000000000..2cbf07337c23f --- /dev/null +++ b/tools/testing/selftests/cgroup/lib/libcgroup.mk @@ -0,0 +1,12 @@ +CGROUP_DIR :=3D $(selfdir)/cgroup + +LIBCGROUP_C :=3D lib/cgroup_util.c + +LIBCGROUP_O :=3D $(patsubst %.c, $(OUTPUT)/%.o, $(LIBCGROUP_C)) + +CFLAGS +=3D -I$(CGROUP_DIR)/lib/include + +$(LIBCGROUP_O): $(OUTPUT)/%.o : $(CGROUP_DIR)/%.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ + +EXTRA_CLEAN +=3D $(LIBCGROUP_O) --=20 2.49.0.395.g12beb8f557-goog From nobody Thu Dec 18 05:07:17 2025 Received: from mail-ua1-f73.google.com (mail-ua1-f73.google.com [209.85.222.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1C3870825 for ; Thu, 27 Mar 2025 01:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038705; cv=none; b=FOntDfRBEop0x/76bz46LPxrMYL7r0rURFR4AtjpyUzLuI3CXxeyh/p0kxs4U1yOrOoLb6hQfv1tuXiAoOnEFduotPHLXq8WkdsmbIImqIlPyezHGnuymysdDs9DyJudJwNezfy/Z5NGd0duxJUsN+o2TgaEX3euhNPMQN3r5P8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038705; c=relaxed/simple; bh=zLlqYxdhGw0cqRRjDi96z+MiFOFcknIK743A4mTOODc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nIt5Jm8Ox4zsGtU1UbtunApuNSm6PzDtnjQhwwoHDsIF4+RAm9u/xzxMEwe3iG7Y3x370bhHg9Ir/mTJV6rElNJo9RX5vRQA52cfLRmzTFjg0+gGqHgo5J46As7ifbQfeTeyRsVaEnf4WS6/WhePJI5KkH1bpdAnkgE/W6mG+ms= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OFDpO5vg; arc=none smtp.client-ip=209.85.222.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OFDpO5vg" Received: by mail-ua1-f73.google.com with SMTP id a1e0cc1a2514c-86de09a23c8so176793241.1 for ; Wed, 26 Mar 2025 18:25:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743038702; x=1743643502; darn=vger.kernel.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=h0gV2Hykp1nZANVivobuz4rpzhYTiIcgA3xTmvOSP0Q=; b=OFDpO5vg6rmLLlky+r5POeXYQXf3GYRT8jp/LN2TBpZvY2bY+Eq9l3CfvWbpUs6BzO Lx05cX2//I4yKjrtlXEFMoSqFeNyBWG8taLEr2MxkBNfzDvNHNVxfVGgswwZiPCbtInb geYrSQSUrwE8NMGvgxn6E/do/oYu9B5dc9zvNBGskhcOwnD2jT/4dFAVn/gS9YsOPE8f 9cowvT/WlIIPp5zmaa0RqBsdJUI7BK4im2wZJupoIf+Sxr3xTjUtFP+7KoLCD5gOHUrI BmcFK2mvu59pr05l2884oBRKphVJd2cvn9Tzj4AT2A7Zv3OxCS1dG6FeunyDdAPUuA2s I/6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743038702; x=1743643502; 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=h0gV2Hykp1nZANVivobuz4rpzhYTiIcgA3xTmvOSP0Q=; b=JOQ1XKAfLd7yl5JjQpmKISVzEyFNL26oiBRi5Z5srESRKL2OgLxsNUj0mcTrzXsIfn WKlJcNV/UNb5pncMEWLy8teOJXT5Fic7bY0/GbzDJUE+hCkHJybgcP8KcehnwWtB4gvp jx2HcsR6pKFgJ0Qz4YOJg9bpJ/CeLAm/R1hH+WyoDkXBb+4uUWj7FsGr5iWX9tzP/nxD 4QMMVLq4PAAv5Wi6Ruxq5TAeL888LgAWd0sM5upVpbVb+76UbETrRB72+MTbbp5d/viG JyBMtdw33QFtJO/Efwf0aRayiSt6l4zXGj9VWSEU4n5Dpw+J35Ndn4ae/ma4hmwKnawU JM7w== X-Forwarded-Encrypted: i=1; AJvYcCUOgRabeY7n2YY8KbdJSTVGFrPRKRl0hOIzYKopBxXmiF1dgIH4Mtq2ulCshmx0KyF54hv4AalqCxyzIY4=@vger.kernel.org X-Gm-Message-State: AOJu0YwdhrTgaPZ2ylsaBLp0gBnb/DiExloggGg3XGEI/Z6JVvS13wyU 7VFx/2e5M9ZCxHABmdYQy+hY1XD71n11HUgbIu1rQfkpCNphjPXoHuTUzjkzfRZGFnbnZOz89oV RHNadLC4iLKlfdZ9D5w== X-Google-Smtp-Source: AGHT+IGJmEiF1OitEok2mFLmVWOSM49l4tKCl8gfyYUS8Qjj9MZO3Hq+PCEaZnJlxmlEaGvIhaJ2wvGNJri11VQA X-Received: from uabfv8.prod.google.com ([2002:a05:6130:1a88:b0:868:f371:5d73]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:8015:b0:4c3:9b0:9e6b with SMTP id ada2fe7eead31-4c586f5f8d9mr2292397137.10.1743038702744; Wed, 26 Mar 2025 18:25:02 -0700 (PDT) Date: Thu, 27 Mar 2025 01:23:49 +0000 In-Reply-To: <20250327012350.1135621-1-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250327012350.1135621-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog Message-ID: <20250327012350.1135621-5-jthoughton@google.com> Subject: [PATCH 4/5] KVM: selftests: Build and link selftests/cgroup/lib into KVM selftests From: James Houghton To: Sean Christopherson , kvm@vger.kernel.org Cc: Maxim Levitsky , Axel Rasmussen , Tejun Heo , Johannes Weiner , mkoutny@suse.com, Yu Zhao , James Houghton , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" libcgroup.o is built separately from KVM selftests and cgroup selftests, so different compiler flags used by the different selftests will not conflict with each other. Signed-off-by: James Houghton --- tools/testing/selftests/kvm/Makefile.kvm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index f773f8f992494..c86a680f52b28 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -219,6 +219,7 @@ OVERRIDE_TARGETS =3D 1 # importantly defines, i.e. overwrites, $(CC) (unless `make -e` or `make C= C=3D`, # which causes the environment variable to override the makefile). include ../lib.mk +include ../cgroup/lib/libcgroup.mk =20 INSTALL_HDR_PATH =3D $(top_srcdir)/usr LINUX_HDR_PATH =3D $(INSTALL_HDR_PATH)/include/ @@ -272,7 +273,7 @@ LIBKVM_S :=3D $(filter %.S,$(LIBKVM)) LIBKVM_C_OBJ :=3D $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C)) LIBKVM_S_OBJ :=3D $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S)) LIBKVM_STRING_OBJ :=3D $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING)) -LIBKVM_OBJS =3D $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) +LIBKVM_OBJS =3D $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) $(LIB= CGROUP_O) SPLIT_TEST_GEN_PROGS :=3D $(patsubst %, $(OUTPUT)/%, $(SPLIT_TESTS)) SPLIT_TEST_GEN_OBJ :=3D $(patsubst %, $(OUTPUT)/$(ARCH)/%.o, $(SPLIT_TESTS= )) =20 --=20 2.49.0.395.g12beb8f557-goog From nobody Thu Dec 18 05:07:17 2025 Received: from mail-vk1-f201.google.com (mail-vk1-f201.google.com [209.85.221.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCC5D160783 for ; Thu, 27 Mar 2025 01:25:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038709; cv=none; b=n2VL1d4RjJ0sB0pxu7NQgSSR9Lm4AYxRahHoMwyxk6P2hinZ0/b9L01dTbGIAy2iFNEME5hAmQdY6O9TL+froTSx0KzMvFr3ZFhGeQ42tK3hYt+N1dWGMEIzx6ljoOHrNNNPwhvUeofzK4QDTooBydbyLeEXTfKSCIruHrxxJFc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743038709; c=relaxed/simple; bh=xHb0mMaSwF5wR+TP8oh+gOuI6NLha+LuKOnW7GOeuuk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GbyQxYpHLjLGSez82Q5ALrnRSudHxYqnTDfxx4tGRPz152LuQA8q+qLeE5jxa+SC4Iv2yIjAEVPCYu7IDD5RfmKRmKKyOvmy6qNgElNPH96O+mGc9LiHxcHMC5CWSaOrEETQQxdCh/LnEWJT6FG0WSZNmwEsViv9wLlozXYPIFg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=IvdH67h9; arc=none smtp.client-ip=209.85.221.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IvdH67h9" Received: by mail-vk1-f201.google.com with SMTP id 71dfb90a1353d-5240ae97377so127513e0c.2 for ; Wed, 26 Mar 2025 18:25:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743038705; x=1743643505; darn=vger.kernel.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=vrt5cNuKaC+KH0dLUPsP1/tctalnEZ+87XCE4qIF7WA=; b=IvdH67h9ZhHCbDndVv6HewpqRA3VF7WomJ6/9ZAbLjSOPY2DFz+R+AjPwF452LqeFf Cfo0fviI6h+7Z0u2l/9UGtdyYAfTA5Qegqrnv/UKU3Iz46rA4PwSQWOvZaKuH0ziBfvU eIaX07tn/V0wlM4bgDQZ/89I/+sFYgyxDSCCOKL3KBLbL0hYPr8RepQsTK2N+lGxGcj5 Xp5XuVdLVZvqiEjOmH630fHqb72XeK2U2GEy3aGi75lhrrZSiraHL5HYd/OkXYC40CUg nZ1bifNvxwaAVfn7SfpiUGSDp3Z8ZC29J7py7zNYznTEFwQNC4z37KrFD7uXdOKJFae6 2FoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743038705; x=1743643505; 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=vrt5cNuKaC+KH0dLUPsP1/tctalnEZ+87XCE4qIF7WA=; b=uJYdAtGvqUOlyrkph+fUx+lxlf9mAQzjDHVVIZbS6rqhIC2uPvz21l77xD7hGwjId1 /pwVc1W3o3c+tAIQyTudnHUC5MW8HGb5+sRZfV0Us9TBbVGp4wBdubmZRn78hQKFcHfS 4wGjGOSrqG0z69EYEaVv23ZVdFeICCxH4bSto6/vB1xwxiPV2aS9hyNx+u5Jtgo0Cy2T KLbfFjUFfJaAvMl3dUlHtiyeNqicZWoSayhJBmMPa6TInX1dLme90RHvPLvYQLZp2PuZ h5OiPWllgYApBSKblTKwdPsyDfLqux6uWBFj9h1XOX+rQVvY7P+osIbI+Agbc5vC4doH SaLw== X-Forwarded-Encrypted: i=1; AJvYcCX0We9CA5tUZ+FzCCJAY7VaNbpvsv1Zn6698GR/GmnS/RIJ017xvK2RBjN/E2oZJZLP/bG+KzExxRWcVT4=@vger.kernel.org X-Gm-Message-State: AOJu0YzxKgE/SScBZRsQKhk1XPnZDYQqgetVXpgAwBAEJUtGU8TfBOgE QOSAecjacgh81A2x6ZO9fbq61/pjQuSCj99/R0EoLx0kCGTbzG1yJvrMqc0MUYn+IQ55DEe/GNW TXvNF0ttR5gNDnBYZsg== X-Google-Smtp-Source: AGHT+IHAS6Q17X2gE5FR1UN5GUBGrxtXfrqd/wDiLtx9cW2QhRJIVGDDZx8QTfl54TSzXXslcANpuegZY24915dt X-Received: from vka15.prod.google.com ([2002:a05:6122:800f:b0:525:afa4:ae05]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6122:1804:b0:520:42d3:91d2 with SMTP id 71dfb90a1353d-526008fb06emr1411612e0c.1.1743038704810; Wed, 26 Mar 2025 18:25:04 -0700 (PDT) Date: Thu, 27 Mar 2025 01:23:50 +0000 In-Reply-To: <20250327012350.1135621-1-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250327012350.1135621-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog Message-ID: <20250327012350.1135621-6-jthoughton@google.com> Subject: [PATCH 5/5] KVM: selftests: access_tracking_perf_test: Use MGLRU for access tracking From: James Houghton To: Sean Christopherson , kvm@vger.kernel.org Cc: Maxim Levitsky , Axel Rasmussen , Tejun Heo , Johannes Weiner , mkoutny@suse.com, Yu Zhao , James Houghton , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" By using MGLRU's debugfs for invoking test_young() and clear_young(), we avoid page_idle's incompatibility with MGLRU, and we can mark pages as idle (clear_young()) much faster. The ability to use page_idle is left in, as it is useful for kernels that do not have MGLRU built in. If MGLRU is enabled but is not usable (e.g. we can't access the debugfs mount), the test will fail, as page_idle is not compatible with MGLRU. cgroup utility functions have been borrowed so that, when running with MGLRU, we can create a memcg in which to run our test. Other MGLRU-debugfs-specific parsing code has been added to lru_gen_util.{c,h}. Co-developed-by: Axel Rasmussen Signed-off-by: Axel Rasmussen Signed-off-by: James Houghton --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/access_tracking_perf_test.c | 208 ++++++++-- .../selftests/kvm/include/lru_gen_util.h | 51 +++ .../testing/selftests/kvm/lib/lru_gen_util.c | 383 ++++++++++++++++++ 4 files changed, 617 insertions(+), 26 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/lru_gen_util.h create mode 100644 tools/testing/selftests/kvm/lib/lru_gen_util.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index c86a680f52b28..6ab0441238a7f 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -8,6 +8,7 @@ LIBKVM +=3D lib/elf.c LIBKVM +=3D lib/guest_modes.c LIBKVM +=3D lib/io.c LIBKVM +=3D lib/kvm_util.c +LIBKVM +=3D lib/lru_gen_util.c LIBKVM +=3D lib/memstress.c LIBKVM +=3D lib/guest_sprintf.c LIBKVM +=3D lib/rbtree.c diff --git a/tools/testing/selftests/kvm/access_tracking_perf_test.c b/tool= s/testing/selftests/kvm/access_tracking_perf_test.c index 0e594883ec13e..1c8e43e18e4c6 100644 --- a/tools/testing/selftests/kvm/access_tracking_perf_test.c +++ b/tools/testing/selftests/kvm/access_tracking_perf_test.c @@ -7,9 +7,11 @@ * This test measures the performance effects of KVM's access tracking. * Access tracking is driven by the MMU notifiers test_young, clear_young,= and * clear_flush_young. These notifiers do not have a direct userspace API, - * however the clear_young notifier can be triggered by marking a pages as= idle - * in /sys/kernel/mm/page_idle/bitmap. This test leverages that mechanism = to - * enable access tracking on guest memory. + * however the clear_young notifier can be triggered either by + * 1. marking a pages as idle in /sys/kernel/mm/page_idle/bitmap OR + * 2. adding a new MGLRU generation using the lru_gen debugfs file. + * This test leverages page_idle to enable access tracking on guest memory + * unless MGLRU is enabled, in which case MGLRU is used. * * To measure performance this test runs a VM with a configurable number of * vCPUs that each touch every page in disjoint regions of memory. Perform= ance @@ -17,10 +19,11 @@ * predefined region. * * Note that a deterministic correctness test of access tracking is not po= ssible - * by using page_idle as it exists today. This is for a few reasons: + * by using page_idle or MGLRU aging as it exists today. This is for a few + * reasons: * - * 1. page_idle only issues clear_young notifiers, which lack a TLB flush.= This - * means subsequent guest accesses are not guaranteed to see page table + * 1. page_idle and MGLRU only issue clear_young notifiers, which lack a T= LB flush. + * This means subsequent guest accesses are not guaranteed to see page = table * updates made by KVM until some time in the future. * * 2. page_idle only operates on LRU pages. Newly allocated pages are not @@ -48,9 +51,17 @@ #include "guest_modes.h" #include "processor.h" =20 +#include "cgroup_util.h" +#include "lru_gen_util.h" + +static const char *TEST_MEMCG_NAME =3D "access_tracking_perf_test"; + /* Global variable used to synchronize all of the vCPU threads. */ static int iteration; =20 +/* The cgroup v2 root. Needed for lru_gen-based aging. */ +char cgroup_root[PATH_MAX]; + /* Defines what vCPU threads should do during a given iteration. */ static enum { /* Run the vCPU to access all its memory. */ @@ -75,6 +86,12 @@ static bool overlap_memory_access; */ static int idle_pages_warn_only =3D -1; =20 +/* Whether or not to use MGLRU instead of page_idle for access tracking */ +static bool use_lru_gen; + +/* Total number of pages to expect in the memcg after touching everything = */ +static long total_pages; + struct test_params { /* The backing source for the region of memory. */ enum vm_mem_backing_src_type backing_src; @@ -133,8 +150,24 @@ static void mark_page_idle(int page_idle_fd, uint64_t = pfn) "Set page_idle bits for PFN 0x%" PRIx64, pfn); } =20 -static void mark_vcpu_memory_idle(struct kvm_vm *vm, - struct memstress_vcpu_args *vcpu_args) +static void too_many_idle_pages(long idle_pages, long total_pages, int vcp= u_idx) +{ + char prefix[18] =3D {}; + + if (vcpu_idx >=3D 0) + snprintf(prefix, 18, "vCPU%d: ", vcpu_idx); + + TEST_ASSERT(idle_pages_warn_only, + "%sToo many pages still idle (%lu out of %lu)", + prefix, idle_pages, total_pages); + + printf("WARNING: %sToo many pages still idle (%lu out of %lu), " + "this will affect performance results.\n", + prefix, idle_pages, total_pages); +} + +static void pageidle_mark_vcpu_memory_idle(struct kvm_vm *vm, + struct memstress_vcpu_args *vcpu_args) { int vcpu_idx =3D vcpu_args->vcpu_idx; uint64_t base_gva =3D vcpu_args->gva; @@ -188,20 +221,81 @@ static void mark_vcpu_memory_idle(struct kvm_vm *vm, * access tracking but low enough as to not make the test too brittle * over time and across architectures. */ - if (still_idle >=3D pages / 10) { - TEST_ASSERT(idle_pages_warn_only, - "vCPU%d: Too many pages still idle (%lu out of %lu)", - vcpu_idx, still_idle, pages); - - printf("WARNING: vCPU%d: Too many pages still idle (%lu out of %lu), " - "this will affect performance results.\n", - vcpu_idx, still_idle, pages); - } + if (still_idle >=3D pages / 10) + too_many_idle_pages(still_idle, pages, + overlap_memory_access ? -1 : vcpu_idx); =20 close(page_idle_fd); close(pagemap_fd); } =20 +int find_generation(struct memcg_stats *stats, long total_pages) +{ + /* + * For finding the generation that contains our pages, use the same + * 90% threshold that page_idle uses. + */ + int gen =3D lru_gen_find_generation(stats, total_pages * 9 / 10); + + if (gen >=3D 0) + return gen; + + if (!idle_pages_warn_only) { + TEST_FAIL("Could not find a generation with 90%% of guest memory (%ld pa= ges).", + total_pages * 9 / 10); + return gen; + } + + /* + * We couldn't find a generation with 90% of guest memory, which can + * happen if access tracking is unreliable. Simply look for a majority + * of pages. + */ + puts("WARNING: Couldn't find a generation with 90% of guest memory. " + "Performance results may not be accurate."); + gen =3D lru_gen_find_generation(stats, total_pages / 2); + TEST_ASSERT(gen >=3D 0, + "Could not find a generation with 50%% of guest memory (%ld pages).", + total_pages / 2); + return gen; +} + +static void lru_gen_mark_memory_idle(struct kvm_vm *vm) +{ + struct timespec ts_start; + struct timespec ts_elapsed; + struct memcg_stats stats; + int found_gens[2]; + + /* Find current generation the pages lie in. */ + lru_gen_read_memcg_stats(&stats, TEST_MEMCG_NAME); + found_gens[0] =3D find_generation(&stats, total_pages); + + /* Make a new generation */ + clock_gettime(CLOCK_MONOTONIC, &ts_start); + lru_gen_do_aging(&stats, TEST_MEMCG_NAME); + ts_elapsed =3D timespec_elapsed(ts_start); + + /* Check the generation again */ + found_gens[1] =3D find_generation(&stats, total_pages); + + /* + * This function should only be invoked with newly-accessed pages, + * so pages should always move to a newer generation. + */ + if (found_gens[0] >=3D found_gens[1]) { + /* We did not move to a newer generation. */ + long idle_pages =3D lru_gen_sum_memcg_stats_for_gen(found_gens[1], + &stats); + + too_many_idle_pages(min_t(long, idle_pages, total_pages), + total_pages, -1); + } + pr_info("%-30s: %ld.%09lds\n", + "Mark memory idle (lru_gen)", ts_elapsed.tv_sec, + ts_elapsed.tv_nsec); +} + static void assert_ucall(struct kvm_vcpu *vcpu, uint64_t expected_ucall) { struct ucall uc; @@ -241,7 +335,7 @@ static void vcpu_thread_main(struct memstress_vcpu_args= *vcpu_args) assert_ucall(vcpu, UCALL_SYNC); break; case ITERATION_MARK_IDLE: - mark_vcpu_memory_idle(vm, vcpu_args); + pageidle_mark_vcpu_memory_idle(vm, vcpu_args); break; } =20 @@ -293,15 +387,18 @@ static void access_memory(struct kvm_vm *vm, int nr_v= cpus, =20 static void mark_memory_idle(struct kvm_vm *vm, int nr_vcpus) { + if (use_lru_gen) + return lru_gen_mark_memory_idle(vm); + /* * Even though this parallelizes the work across vCPUs, this is still a * very slow operation because page_idle forces the test to mark one pfn - * at a time and the clear_young notifier serializes on the KVM MMU + * at a time and the clear_young notifier may serialize on the KVM MMU * lock. */ pr_debug("Marking VM memory idle (slow)...\n"); iteration_work =3D ITERATION_MARK_IDLE; - run_iteration(vm, nr_vcpus, "Mark memory idle"); + run_iteration(vm, nr_vcpus, "Mark memory idle (page_idle)"); } =20 static void run_test(enum vm_guest_mode mode, void *arg) @@ -318,6 +415,15 @@ static void run_test(enum vm_guest_mode mode, void *ar= g) pr_info("\n"); access_memory(vm, nr_vcpus, ACCESS_WRITE, "Populating memory"); =20 + if (use_lru_gen) { + struct memcg_stats stats; + + /* Do an initial page table scan */ + lru_gen_read_memcg_stats(&stats, TEST_MEMCG_NAME); + TEST_ASSERT(lru_gen_sum_memcg_stats(&stats) >=3D total_pages, + "Not all pages accounted for. Was the memcg set up correctly?"); + } + /* As a control, read and write to the populated memory first. */ access_memory(vm, nr_vcpus, ACCESS_WRITE, "Writing to populated memory"); access_memory(vm, nr_vcpus, ACCESS_READ, "Reading from populated memory"); @@ -353,7 +459,12 @@ static int access_tracking_unreliable(void) puts("Skipping idle page count sanity check, because NUMA balancing is e= nabled"); return 1; } + return 0; +} =20 +int run_test_in_cg(const char *cgroup, void *arg) +{ + for_each_guest_mode(run_test, arg); return 0; } =20 @@ -371,7 +482,7 @@ static void help(char *name) printf(" -v: specify the number of vCPUs to run.\n"); printf(" -o: Overlap guest memory accesses instead of partitioning\n" " them into a separate region of memory for each vCPU.\n"); - printf(" -w: Control whether the test warns or fails if more than 10%\n" + printf(" -w: Control whether the test warns or fails if more than 10%%\n" " of pages are still seen as idle/old after accessing guest\n" " memory. >0 =3D=3D warn only, 0 =3D=3D fail, <0 =3D=3D auto.= For auto\n" " mode, the test fails by default, but switches to warn only\n" @@ -382,6 +493,12 @@ static void help(char *name) exit(0); } =20 +void destroy_cgroup(char *cg) +{ + printf("Destroying cgroup: %s\n", cg); + cg_destroy(cg); +} + int main(int argc, char *argv[]) { struct test_params params =3D { @@ -389,6 +506,7 @@ int main(int argc, char *argv[]) .vcpu_memory_bytes =3D DEFAULT_PER_VCPU_MEM_SIZE, .nr_vcpus =3D 1, }; + char *new_cg =3D NULL; int page_idle_fd; int opt; =20 @@ -423,15 +541,53 @@ int main(int argc, char *argv[]) } } =20 - page_idle_fd =3D open("/sys/kernel/mm/page_idle/bitmap", O_RDWR); - __TEST_REQUIRE(page_idle_fd >=3D 0, - "CONFIG_IDLE_PAGE_TRACKING is not enabled"); - close(page_idle_fd); + if (lru_gen_usable()) { + if (cg_find_unified_root(cgroup_root, sizeof(cgroup_root), NULL)) + ksft_exit_skip("cgroup v2 isn't mounted\n"); + + new_cg =3D cg_name(cgroup_root, TEST_MEMCG_NAME); + printf("Creating cgroup: %s\n", new_cg); + if (cg_create(new_cg) && errno !=3D EEXIST) + ksft_exit_skip("could not create new cgroup: %s\n", new_cg); + + use_lru_gen =3D true; + } else { + page_idle_fd =3D open("/sys/kernel/mm/page_idle/bitmap", O_RDWR); + __TEST_REQUIRE(page_idle_fd >=3D 0, + "Couldn't open /sys/kernel/mm/page_idle/bitmap. " + "Is CONFIG_IDLE_PAGE_TRACKING enabled?"); + + close(page_idle_fd); + } =20 if (idle_pages_warn_only =3D=3D -1) idle_pages_warn_only =3D access_tracking_unreliable(); =20 - for_each_guest_mode(run_test, ¶ms); + /* + * If guest_page_size is larger than the host's page size, the + * guest (memstress) will only fault in a subset of the host's pages. + */ + total_pages =3D params.nr_vcpus * params.vcpu_memory_bytes / + max(memstress_args.guest_page_size, + (uint64_t)getpagesize()); + + if (use_lru_gen) { + int ret; + + puts("Using lru_gen for aging"); + /* + * This will fork off a new process to run the test within + * a new memcg, so we need to properly propagate the return + * value up. + */ + ret =3D cg_run(new_cg, &run_test_in_cg, ¶ms); + destroy_cgroup(new_cg); + if (ret) + return ret; + } else { + puts("Using page_idle for aging"); + for_each_guest_mode(run_test, ¶ms); + } =20 return 0; } diff --git a/tools/testing/selftests/kvm/include/lru_gen_util.h b/tools/tes= ting/selftests/kvm/include/lru_gen_util.h new file mode 100644 index 0000000000000..d32ff5d8ffd05 --- /dev/null +++ b/tools/testing/selftests/kvm/include/lru_gen_util.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Tools for integrating with lru_gen, like parsing the lru_gen debugfs ou= tput. + * + * Copyright (C) 2025, Google LLC. + */ +#ifndef SELFTEST_KVM_LRU_GEN_UTIL_H +#define SELFTEST_KVM_LRU_GEN_UTIL_H + +#include +#include +#include + +#include "test_util.h" + +#define MAX_NR_GENS 16 /* MAX_NR_GENS in include/linux/mmzone.h */ +#define MAX_NR_NODES 4 /* Maximum number of nodes supported by the test */ + +#define LRU_GEN_DEBUGFS "/sys/kernel/debug/lru_gen" +#define LRU_GEN_ENABLED_PATH "/sys/kernel/mm/lru_gen/enabled" +#define LRU_GEN_ENABLED 1 +#define LRU_GEN_MM_WALK 2 + +struct generation_stats { + int gen; + long age_ms; + long nr_anon; + long nr_file; +}; + +struct node_stats { + int node; + int nr_gens; /* Number of populated gens entries. */ + struct generation_stats gens[MAX_NR_GENS]; +}; + +struct memcg_stats { + unsigned long memcg_id; + int nr_nodes; /* Number of populated nodes entries. */ + struct node_stats nodes[MAX_NR_NODES]; +}; + +void lru_gen_read_memcg_stats(struct memcg_stats *stats, const char *memcg= ); +long lru_gen_sum_memcg_stats(const struct memcg_stats *stats); +long lru_gen_sum_memcg_stats_for_gen(int gen, const struct memcg_stats *st= ats); +void lru_gen_do_aging(struct memcg_stats *stats, const char *memcg); +int lru_gen_find_generation(const struct memcg_stats *stats, + unsigned long total_pages); +bool lru_gen_usable(void); + +#endif /* SELFTEST_KVM_LRU_GEN_UTIL_H */ diff --git a/tools/testing/selftests/kvm/lib/lru_gen_util.c b/tools/testing= /selftests/kvm/lib/lru_gen_util.c new file mode 100644 index 0000000000000..783a1f1028a26 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/lru_gen_util.c @@ -0,0 +1,383 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025, Google LLC. + */ + +#include + +#include "lru_gen_util.h" + +/* + * Tracks state while we parse memcg lru_gen stats. The file we're parsing= is + * structured like this (some extra whitespace elided): + * + * memcg (id) (path) + * node (id) + * (gen_nr) (age_in_ms) (nr_anon_pages) (nr_file_pages) + */ +struct memcg_stats_parse_context { + bool consumed; /* Whether or not this line was consumed */ + /* Next parse handler to invoke */ + void (*next_handler)(struct memcg_stats *, + struct memcg_stats_parse_context *, char *); + int current_node_idx; /* Current index in nodes array */ + const char *name; /* The name of the memcg we're looking for */ +}; + +static void memcg_stats_handle_searching(struct memcg_stats *stats, + struct memcg_stats_parse_context *ctx, + char *line); +static void memcg_stats_handle_in_memcg(struct memcg_stats *stats, + struct memcg_stats_parse_context *ctx, + char *line); +static void memcg_stats_handle_in_node(struct memcg_stats *stats, + struct memcg_stats_parse_context *ctx, + char *line); + +struct split_iterator { + char *str; + char *save; +}; + +static char *split_next(struct split_iterator *it) +{ + char *ret =3D strtok_r(it->str, " \t\n\r", &it->save); + + it->str =3D NULL; + return ret; +} + +static void memcg_stats_handle_searching(struct memcg_stats *stats, + struct memcg_stats_parse_context *ctx, + char *line) +{ + struct split_iterator it =3D { .str =3D line }; + char *prefix =3D split_next(&it); + char *memcg_id =3D split_next(&it); + char *memcg_name =3D split_next(&it); + char *end; + + ctx->consumed =3D true; + + if (!prefix || strcmp("memcg", prefix)) + return; /* Not a memcg line (maybe empty), skip */ + + TEST_ASSERT(memcg_id && memcg_name, + "malformed memcg line; no memcg id or memcg_name"); + + if (strcmp(memcg_name + 1, ctx->name)) + return; /* Wrong memcg, skip */ + + /* Found it! */ + + stats->memcg_id =3D strtoul(memcg_id, &end, 10); + TEST_ASSERT(*end =3D=3D '\0', "malformed memcg id '%s'", memcg_id); + if (!stats->memcg_id) + return; /* Removed memcg? */ + + ctx->next_handler =3D memcg_stats_handle_in_memcg; +} + +static void memcg_stats_handle_in_memcg(struct memcg_stats *stats, + struct memcg_stats_parse_context *ctx, + char *line) +{ + struct split_iterator it =3D { .str =3D line }; + char *prefix =3D split_next(&it); + char *id =3D split_next(&it); + long found_node_id; + char *end; + + ctx->consumed =3D true; + ctx->current_node_idx =3D -1; + + if (!prefix) + return; /* Skip empty lines */ + + if (!strcmp("memcg", prefix)) { + /* Memcg done, found next one; stop. */ + ctx->next_handler =3D NULL; + return; + } else if (strcmp("node", prefix)) + TEST_ASSERT(false, "found malformed line after 'memcg ...'," + "token: '%s'", prefix); + + /* At this point we know we have a node line. Parse the ID. */ + + TEST_ASSERT(id, "malformed node line; no node id"); + + found_node_id =3D strtol(id, &end, 10); + TEST_ASSERT(*end =3D=3D '\0', "malformed node id '%s'", id); + + ctx->current_node_idx =3D stats->nr_nodes++; + TEST_ASSERT(ctx->current_node_idx < MAX_NR_NODES, + "memcg has stats for too many nodes, max is %d", + MAX_NR_NODES); + stats->nodes[ctx->current_node_idx].node =3D found_node_id; + + ctx->next_handler =3D memcg_stats_handle_in_node; +} + +static void memcg_stats_handle_in_node(struct memcg_stats *stats, + struct memcg_stats_parse_context *ctx, + char *line) +{ + char *my_line =3D strdup(line); + struct split_iterator it =3D { .str =3D my_line }; + char *gen, *age, *nr_anon, *nr_file; + struct node_stats *node_stats; + struct generation_stats *gen_stats; + char *end; + + TEST_ASSERT(it.str, "failed to copy input line"); + + gen =3D split_next(&it); + + if (!gen) + goto out_consume; /* Skip empty lines */ + + if (!strcmp("memcg", gen) || !strcmp("node", gen)) { + /* + * Reached next memcg or node section. Don't consume, let the + * other handler deal with this. + */ + ctx->next_handler =3D memcg_stats_handle_in_memcg; + goto out; + } + + node_stats =3D &stats->nodes[ctx->current_node_idx]; + TEST_ASSERT(node_stats->nr_gens < MAX_NR_GENS, + "found too many generation lines; max is %d", + MAX_NR_GENS); + gen_stats =3D &node_stats->gens[node_stats->nr_gens++]; + + age =3D split_next(&it); + nr_anon =3D split_next(&it); + nr_file =3D split_next(&it); + + TEST_ASSERT(age && nr_anon && nr_file, + "malformed generation line; not enough tokens"); + + gen_stats->gen =3D (int)strtol(gen, &end, 10); + TEST_ASSERT(*end =3D=3D '\0', "malformed generation number '%s'", gen); + + gen_stats->age_ms =3D strtol(age, &end, 10); + TEST_ASSERT(*end =3D=3D '\0', "malformed generation age '%s'", age); + + gen_stats->nr_anon =3D strtol(nr_anon, &end, 10); + TEST_ASSERT(*end =3D=3D '\0', "malformed anonymous page count '%s'", + nr_anon); + + gen_stats->nr_file =3D strtol(nr_file, &end, 10); + TEST_ASSERT(*end =3D=3D '\0', "malformed file page count '%s'", nr_file); + +out_consume: + ctx->consumed =3D true; +out: + free(my_line); +} + +static void print_memcg_stats(const struct memcg_stats *stats, const char = *name) +{ + int node, gen; + + pr_debug("stats for memcg %s (id %lu):\n", name, stats->memcg_id); + for (node =3D 0; node < stats->nr_nodes; ++node) { + pr_debug("\tnode %d\n", stats->nodes[node].node); + for (gen =3D 0; gen < stats->nodes[node].nr_gens; ++gen) { + const struct generation_stats *gstats =3D + &stats->nodes[node].gens[gen]; + + pr_debug("\t\tgen %d\tage_ms %ld" + "\tnr_anon %ld\tnr_file %ld\n", + gstats->gen, gstats->age_ms, gstats->nr_anon, + gstats->nr_file); + } + } +} + +/* Re-read lru_gen debugfs information for @memcg into @stats. */ +void lru_gen_read_memcg_stats(struct memcg_stats *stats, const char *memcg) +{ + FILE *f; + ssize_t read =3D 0; + char *line =3D NULL; + size_t bufsz; + struct memcg_stats_parse_context ctx =3D { + .next_handler =3D memcg_stats_handle_searching, + .name =3D memcg, + }; + + memset(stats, 0, sizeof(struct memcg_stats)); + + f =3D fopen(LRU_GEN_DEBUGFS, "r"); + TEST_ASSERT(f, "fopen(%s) failed", LRU_GEN_DEBUGFS); + + while (ctx.next_handler && (read =3D getline(&line, &bufsz, f)) > 0) { + ctx.consumed =3D false; + + do { + ctx.next_handler(stats, &ctx, line); + if (!ctx.next_handler) + break; + } while (!ctx.consumed); + } + + if (read < 0 && !feof(f)) + TEST_ASSERT(false, "getline(%s) failed", LRU_GEN_DEBUGFS); + + TEST_ASSERT(stats->memcg_id > 0, "Couldn't find memcg: %s\n" + "Did the memcg get created in the proper mount?", + memcg); + if (line) + free(line); + TEST_ASSERT(!fclose(f), "fclose(%s) failed", LRU_GEN_DEBUGFS); + + print_memcg_stats(stats, memcg); +} + +/* + * Find all pages tracked by lru_gen for this memcg in generation @target_= gen. + * + * If @target_gen is negative, look for all generations. + */ +long lru_gen_sum_memcg_stats_for_gen(int target_gen, + const struct memcg_stats *stats) +{ + int node, gen; + long total_nr =3D 0; + + for (node =3D 0; node < stats->nr_nodes; ++node) { + const struct node_stats *node_stats =3D &stats->nodes[node]; + + for (gen =3D 0; gen < node_stats->nr_gens; ++gen) { + const struct generation_stats *gen_stats =3D + &node_stats->gens[gen]; + + if (target_gen >=3D 0 && gen_stats->gen !=3D target_gen) + continue; + + total_nr +=3D gen_stats->nr_anon + gen_stats->nr_file; + } + } + + return total_nr; +} + +/* Find all pages tracked by lru_gen for this memcg. */ +long lru_gen_sum_memcg_stats(const struct memcg_stats *stats) +{ + return lru_gen_sum_memcg_stats_for_gen(-1, stats); +} + +/* + * If lru_gen aging should force page table scanning. + * + * If you want to set this to false, you will need to do eviction + * before doing extra aging passes. + */ +static const bool force_scan =3D true; + +static void run_aging_impl(unsigned long memcg_id, int node_id, int max_ge= n) +{ + FILE *f =3D fopen(LRU_GEN_DEBUGFS, "w"); + char *command; + size_t sz; + + TEST_ASSERT(f, "fopen(%s) failed", LRU_GEN_DEBUGFS); + sz =3D asprintf(&command, "+ %lu %d %d 1 %d\n", + memcg_id, node_id, max_gen, force_scan); + TEST_ASSERT(sz > 0, "creating aging command failed"); + + pr_debug("Running aging command: %s", command); + if (fwrite(command, sizeof(char), sz, f) < sz) { + TEST_ASSERT(false, "writing aging command %s to %s failed", + command, LRU_GEN_DEBUGFS); + } + + TEST_ASSERT(!fclose(f), "fclose(%s) failed", LRU_GEN_DEBUGFS); +} + +void lru_gen_do_aging(struct memcg_stats *stats, const char *memcg) +{ + int node, gen; + + pr_debug("lru_gen: invoking aging...\n"); + + /* Must read memcg stats to construct the proper aging command. */ + lru_gen_read_memcg_stats(stats, memcg); + + for (node =3D 0; node < stats->nr_nodes; ++node) { + int max_gen =3D 0; + + for (gen =3D 0; gen < stats->nodes[node].nr_gens; ++gen) { + int this_gen =3D stats->nodes[node].gens[gen].gen; + + max_gen =3D max_gen > this_gen ? max_gen : this_gen; + } + + run_aging_impl(stats->memcg_id, stats->nodes[node].node, + max_gen); + } + + /* Re-read so callers get updated information */ + lru_gen_read_memcg_stats(stats, memcg); +} + +/* + * Find which generation contains at least @pages pages, assuming that + * such a generation exists. + */ +int lru_gen_find_generation(const struct memcg_stats *stats, + unsigned long pages) +{ + int node, gen, gen_idx, min_gen =3D INT_MAX, max_gen =3D -1; + + for (node =3D 0; node < stats->nr_nodes; ++node) + for (gen_idx =3D 0; gen_idx < stats->nodes[node].nr_gens; + ++gen_idx) { + gen =3D stats->nodes[node].gens[gen_idx].gen; + max_gen =3D gen > max_gen ? gen : max_gen; + min_gen =3D gen < min_gen ? gen : min_gen; + } + + for (gen =3D min_gen; gen < max_gen; ++gen) + /* See if this generation has enough pages. */ + if (lru_gen_sum_memcg_stats_for_gen(gen, stats) > pages) + return gen; + + return -1; +} + +bool lru_gen_usable(void) +{ + long required_features =3D LRU_GEN_ENABLED | LRU_GEN_MM_WALK; + int lru_gen_fd, lru_gen_debug_fd; + char mglru_feature_str[8] =3D {}; + long mglru_features; + + lru_gen_fd =3D open(LRU_GEN_ENABLED_PATH, O_RDONLY); + if (lru_gen_fd < 0) { + puts("lru_gen: Could not open " LRU_GEN_ENABLED_PATH); + return false; + } + if (read(lru_gen_fd, &mglru_feature_str, 7) < 7) { + puts("lru_gen: Could not read from " LRU_GEN_ENABLED_PATH); + close(lru_gen_fd); + return false; + } + close(lru_gen_fd); + + mglru_features =3D strtol(mglru_feature_str, NULL, 16); + if ((mglru_features & required_features) !=3D required_features) { + printf("lru_gen: missing features, got: %s", mglru_feature_str); + return false; + } + + lru_gen_debug_fd =3D open(LRU_GEN_DEBUGFS, O_RDWR); + __TEST_REQUIRE(lru_gen_debug_fd >=3D 0, + "lru_gen: Could not open " LRU_GEN_DEBUGFS ", " + "but lru_gen is enabled, so cannot use page_idle."); + close(lru_gen_debug_fd); + return true; +} --=20 2.49.0.395.g12beb8f557-goog