From nobody Fri Dec 19 16:05:31 2025 Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) (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 24BB01DB346 for ; Mon, 14 Apr 2025 20:09:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744661377; cv=none; b=YmVVfJdi9sFKdTVl1H5cZX0HUguMhkfaQhlEXZPd8wupTjuNzyRKTry9AcLUda41EPORNZHEz62pkx1S+pW0vEGoQfxQgPpUQcfX1Nra+oA2XnHNCi9hfqB/WbQzqY8wQQCFnmQdki4simhZwM/3faUXOaUxMqU3B6FHhaaAlQY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744661377; c=relaxed/simple; bh=BF/n3ojsyDlB1gEPdRnL42H4qr9IhToSWAUvHs9sZWU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Bu1Qd3XJpvM/fo5ztOXp7De1ptaVUzzbysG7V+LORLZGfSvw1XaYo5lqpBEaZoCN05M6ViuEyoPEEyRpnmPTxiKYVw1xD4nPXRC6A+6QQgPHCn1kbEgMlDDG/Q/DEimoARZwf+wvtsxIEe11VhmO04oUnkOByFJ1oWB3ugm3y1w= 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=Oc7bcXHc; arc=none smtp.client-ip=209.85.160.202 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="Oc7bcXHc" Received: by mail-qt1-f202.google.com with SMTP id d75a77b69052e-4767e6b4596so87888721cf.2 for ; Mon, 14 Apr 2025 13:09:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744661375; x=1745266175; 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=+OmKBQBv9lPDr6Lz0gJXTMJ6j2uWNIUBa5L40UsL5lc=; b=Oc7bcXHcG9z7rt1Rjos2AJ+ExWT5XEUkOuFEHoIlAq44c4ng5LCT90c5Jc/XTGrHpm kRlwpLDgG9igWApzW0w+8F35iiRSp9PL1FJkIDxH5200DyIR9oYB4sw7VlE2zOarH9EI XhPBjqD5dnjNg1IBctn2DaAXkASwxmHmBcQAnyfzRBtuU8Qnv2+fTcsnDs6sBgUtDRNk WTvA+CgaltkK6uCmAk1FPNx7awmpGZBigL9X/0714Yl+HnFAaPhNbHYNYTb8j55t0Fin fBg/540N3pQYzcuyTFrDx3fG4F8zkEkMD0w54GuUBaotzqlwa0c/P7wPCcheQAyRumac ckVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744661375; x=1745266175; 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=+OmKBQBv9lPDr6Lz0gJXTMJ6j2uWNIUBa5L40UsL5lc=; b=YuzJ2GnQDZH8hTSJRttkcCHSHXRi6fkRbklf3xh7fjNli1hgpeOUoNFed/Yt7H3smc /HH2Qv/+jSf6rPsXViEi21EVzjGKZdvRbsHywOmenM4kduUdLNayTkwYxg7zPfaLtLG7 9igW9ta3dlk2o27HaemKdxljJQhXqSMAG+0h91CvfdL9pv/nZMpHu4DXCyFeWsK7BJrX sZJb1KrHbzMF12kNhG0kiljnFS2A8bFkCdbNkMdCAljhKOSZk+/YO7xxUvfsqAGsTQNk sBFeO5ZOyBlQqpj88OKqiML13KwUZDEbcFvWQ9UEjujLBm3OzFVv+DoPs72+1xMH1BjL rxpA== X-Forwarded-Encrypted: i=1; AJvYcCUXSwDC4UAvZ2aq7oNS1GXm3AnfzmBF1Hal1mFyZiikxFFbXD0QHOwZMJ6HZ+74g5/v2amwNkwN+BVujtk=@vger.kernel.org X-Gm-Message-State: AOJu0YyNLnYqVoxbo6FUGR5U8YgA0aRwa5Zjn55uCWCEgYwxER6ah6jk jyRGJf7L9Szg5cHv01FbxwrMbJevZLBb8vWJXO0bal75rDwyAFlrNfsTMTVUjyx7b5Qi/zfu+zW qtzxITnf/p7I8Af4cOA== X-Google-Smtp-Source: AGHT+IGpM6NeNiCFi/bkPo1rMtQTc/NqEW2YkK8IB9Rf0x2rTDTCkmJztPeQBYp1ihD6g6OodTF5TA9C2YCT7W+w X-Received: from qtbhg22.prod.google.com ([2002:a05:622a:6116:b0:477:677d:45b8]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:ac8:5a8f:0:b0:476:9ec4:5006 with SMTP id d75a77b69052e-479775d5e5fmr206159171cf.37.1744661374947; Mon, 14 Apr 2025 13:09:34 -0700 (PDT) Date: Mon, 14 Apr 2025 20:09:25 +0000 In-Reply-To: <20250414200929.3098202-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: <20250414200929.3098202-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250414200929.3098202-2-jthoughton@google.com> Subject: [PATCH v3 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, Yosry Ahmed , Yu Zhao , David Matlack , 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: Maxim Levitsky 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.604.gff1f9ca942-goog From nobody Fri Dec 19 16:05:31 2025 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.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 169741DB951 for ; Mon, 14 Apr 2025 20:09:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744661379; cv=none; b=H9sl8Qf93Glj63gcIZ9FANHAosT6frYMrAexE52RFpP1KvCBdEjmgk2SCgZsVDk8St5j9DNxc6830rZeqoP+tEzR2+WFmt0A98FqUu5wEbBSpavnTb2fRttWMPy1D0VkZay9pdrqoqtV8wufqrY3tg1DD6iPFhHmo9FKMfRW068= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744661379; c=relaxed/simple; bh=A9agp1wVVTN9gLJ7vLgLfDxvVbChZWKeOB6jEwY00oE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T8AtWkSEooP1Lc0PdqUwPaoPl0eTO+I7vUlCq1tJOL1amsfphpm5p+/bKYOI0jXnAqfCNGaHJoRNSMYRId86uTtuk+71RUA+yD5CroY20pA9Y2abXmQspzQzPzor9Hr83YTgCq8HFzldxg8NUpHcCf8pQlRUis6rHF32CK7WFTE= 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=BbbKVVDX; arc=none smtp.client-ip=209.85.222.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="BbbKVVDX" Received: by mail-qk1-f201.google.com with SMTP id af79cd13be357-7c5f876bfe0so776101285a.3 for ; Mon, 14 Apr 2025 13:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744661376; x=1745266176; 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=zEPCPsuu4mOv4RpZG9bxPkCm1mldRsLNLtiJyqFAhfo=; b=BbbKVVDXil6GwtHDXj46KTFzIJFOSyyQbkggiwUDt4LGIeMgVyBm1ytwgawhRMxlv2 k8enYGNxUbBrr0XA8wN7IK21pCsW16tfWHECHH9k3KXR5FzPv3ndXane9kXsAv2Jp+qe sYMRYBRFluYQAbKiYWdePQc5+4wLSwgUkph5cUuorgShkKU4IncJKwEWvbMBVuu5B2WM 5BF54mqqI6wMpVxv/3+DMB2xIlq42aRfoHZrGemIH6UN8N67yfh4rUjGIOWv9DWljUUX WjQihe8I8gUu06f2fg0ZCh/ITMRAAq4nlPmqWqIVfsMChVQF3ASnsT0XcdUIwZ9fjtzz d2yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744661376; x=1745266176; 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=zEPCPsuu4mOv4RpZG9bxPkCm1mldRsLNLtiJyqFAhfo=; b=PJZrTbnn9j53kX+T3O4fCRcjb6m8ndRetvsYQu4lWTImfZhdAN82wfcJsiVQgMM181 iEgNFwkykjvSiM8Ygn0ePJ4eNdvHZj5MOSspkayFDsiQHkadiR4L4sBsn+Ldo1M3I/wz +/FWXgbXDO1PedJwg5YPnK8e/ckByUPkCWxWMgOiix5IB2HAhNc2dWEQMCTbfTrLO/YO e2vOUCEGa12tnQUejvfaTSh95TI4S3cHse9nk8zZ4WU69wU9KTERDeyepjejEd0F7kan HqhUB6NN2iUGDOS/xkkWag+XkMhkD89E8WMsqXTgPmtYnCJZp1aHN3IfsVX+ft4kqgeg RqKg== X-Forwarded-Encrypted: i=1; AJvYcCXS/UxESbxqcAKhA62TpoLdXPqp8A3WPv4KP1FqBUVUEZeikj6SGuCXQSOSxbEqpOggPXgrgKs/DkQ1vE4=@vger.kernel.org X-Gm-Message-State: AOJu0YxmkzcrE7ibVJ+0mBAwg4txGQGACGQe52ljUW6lJGpLnh20PB5T h4k+c+2BiE7fiQZXP3Kk8zUNvfpWVUoTvmEbKsEpysPa8LRV4p54C5lwrLPNxAlUB4NYgKlxuH9 qGCurDRG3aaTc8szXXw== X-Google-Smtp-Source: AGHT+IFo27YAEXtdB9NO9z8x8Qu4EDaExdktQ60IBBNvdKMRDz2sKxo9rLMgeMU1RMTXNBzeHGQ39ioAKjECzVSv X-Received: from qkae13.prod.google.com ([2002:a05:620a:208d:b0:7c5:b5ba:4136]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:460b:b0:7c0:bb3f:e285 with SMTP id af79cd13be357-7c7af13370dmr1874025885a.24.1744661376057; Mon, 14 Apr 2025 13:09:36 -0700 (PDT) Date: Mon, 14 Apr 2025 20:09:26 +0000 In-Reply-To: <20250414200929.3098202-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: <20250414200929.3098202-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250414200929.3098202-3-jthoughton@google.com> Subject: [PATCH v3 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, Yosry Ahmed , Yu Zhao , David Matlack , 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 | 62 ++++++++++++++++--- .../testing/selftests/kvm/include/test_util.h | 1 + tools/testing/selftests/kvm/lib/test_util.c | 7 +++ 3 files changed, 61 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..a2ac6fa2ba141 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,32 @@ 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 may be effectively unlimited (for + * example, this is the case when running on KVM L0), and 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 will be unmapped to get + * NUMA faults, dropping the Accessed bits. + */ + 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 +372,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 +395,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 +412,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 +429,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.604.gff1f9ca942-goog From nobody Fri Dec 19 16:05:31 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 2E52A1DE8AE for ; Mon, 14 Apr 2025 20:09:37 +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=1744661380; cv=none; b=TnpPhBoGDptbBc5D8RwXz2htPz80SjonRQyz1ElQCNjShv+8D14KNDI8SQWR+F/snoWGnNk/uao1NpH+Qfksbzrvl9ot7Du9gjJyfzQJZB4+ZkE6Rgg5CKuvmJtwcH17UIYnwi/l615F5JnScuIgSgGFuLbfYqLWPml6aJuX0d4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744661380; c=relaxed/simple; bh=Q5s04arBO6wP0YtImTtLVVwE4rPFfsjS9e7jXcuiB/U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T5iF5uUN0eaKK9ZG/yfkfbxcsG9Lkh/K7hbsp04KeYuuxzjWT2cZRwqyVox4kwbegqyB5vFpzkdu+rq12IxVUs9xT+p93EmRwEG3hJIjO6PsPVmC0WYcEI7FWArjFAHBHEKywqa/fZUYkTsULVcuZdkxqEMyLZEflDRcmUvtjsU= 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=wJmvWFiY; 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="wJmvWFiY" Received: by mail-qv1-f74.google.com with SMTP id 6a1803df08f44-6ecfbdaaee3so78454636d6.3 for ; Mon, 14 Apr 2025 13:09:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744661377; x=1745266177; 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=3ZNC09kfFvwbMY+TuFPooH5ZpXiI2zM5Sfzza51R45o=; b=wJmvWFiYg9QaVghWy5s5AXVGVAxY7585xwC2gINmLTtz5CGwUbvbq+ta4FOHqIg7TW 4XAnExKQOIRcjok6lelBg8AJg7gYswe/WHJdNXIAjBZLdlpZhH4RqY/Z4cDgjO35XkfZ v1LkpG9NdPFeZ2Mp7UgKY3nUzsCDi6JM6Dq17KpkvBdEuzP2Jjrva0GrWKPQ5TMv+Ypv YAIN/eyFFpJLPTrNjgvcFl3JAEGJJ9eTJVXXQCOfBf18T6ZGYE8VRuBKruYnOWlJO/tk MVgiln6lrs10eMzmaqStcvmu0icEmtQUHfNPHZKcqYFCJBLFOW0avywDGLuU0mItTDw5 9MTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744661377; x=1745266177; 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=3ZNC09kfFvwbMY+TuFPooH5ZpXiI2zM5Sfzza51R45o=; b=gmTJ9k6nIzPGR96SF/sd0Yw7TqbHinniz/AMxGOpDOdtofMo+CWLGdXfJY6ZMlgci2 XTHwDh6IllfaSp7hoQD3jeqBFHQ7bNHjJM17/b0kKSvreoE4MTkt0NjL2BRH49aaecUY qzoa3YxS2372NwnGxAAxUHgIYzPz8wH4L11twjkS8MKq7QzcqJMHy9ghl0QfAdw8biH1 4AeQzU/fGBvB8DSRCfznByCPCiKSHz3n/clqwJS+RjgdMcIwqYEvYETYMZJOCJOxSoZM Lrxx/grm29eKIu1nwnAJJ6RiQZDGiYdM8rEQ8w5B913KLI2vgsEOt1J9QK9hx/CBMebZ RAdw== X-Forwarded-Encrypted: i=1; AJvYcCULus35AGW/A9Sxo3z3S3MfQqmR+PAbUNaC8SO6GOcHXOQP/4s8ZmzGesLzDqapWUCRAWtCaGo/Swc/kLQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwhPDY2oFZ3xTTxc4vMIyYLy3NUwS7Yfrkbw0JhRBQvxcbIogPC Eq1M5aBvTuWK4boTOdNdZuUYLr5PRVMDB30S3178TAwQ/+mrt2dZ5FV4PqhOwgmSY34TZI731or hoNEy78QJEcU+ExdPsw== X-Google-Smtp-Source: AGHT+IEmhHPOtSLRv5SjgoAz4o7I3BOkLb3Ktb52XwE92Hh9Nyak79yxZ3UyDRTI9exZl8vbFvxhssgNe4SMJrXM X-Received: from qvb1.prod.google.com ([2002:a05:6214:6001:b0:6eb:2b16:6854]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:ad4:5ca4:0:b0:6f0:e2d4:51fe with SMTP id 6a1803df08f44-6f230d950dfmr184580506d6.26.1744661376927; Mon, 14 Apr 2025 13:09:36 -0700 (PDT) Date: Mon, 14 Apr 2025 20:09:27 +0000 In-Reply-To: <20250414200929.3098202-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: <20250414200929.3098202-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250414200929.3098202-4-jthoughton@google.com> Subject: [PATCH v3 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, Yosry Ahmed , Yu Zhao , David Matlack , James Houghton , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org 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 | 2 +- .../cgroup/{ =3D> lib/include}/cgroup_util.h | 4 ++-- .../testing/selftests/cgroup/lib/libcgroup.mk | 19 +++++++++++++++++ 4 files changed, 33 insertions(+), 13 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..f047d8adaec65 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/lib/cgroup_util.c @@ -17,7 +17,7 @@ #include =20 #include "cgroup_util.h" -#include "../clone3/clone3_selftests.h" +#include "../../clone3/clone3_selftests.h" =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..7a73007204c39 --- /dev/null +++ b/tools/testing/selftests/cgroup/lib/libcgroup.mk @@ -0,0 +1,19 @@ +CGROUP_DIR :=3D $(selfdir)/cgroup + +LIBCGROUP_C :=3D lib/cgroup_util.c + +LIBCGROUP_O :=3D $(patsubst %.c, $(OUTPUT)/%.o, $(LIBCGROUP_C)) + +LIBCGROUP_O_DIRS :=3D $(shell dirname $(LIBCGROUP_O) | uniq) + +CFLAGS +=3D -I$(CGROUP_DIR)/lib/include + +EXTRA_HDRS :=3D $(selfdir)/clone3/clone3_selftests.h + +$(LIBCGROUP_O_DIRS): + mkdir -p $@ + +$(LIBCGROUP_O): $(OUTPUT)/%.o : $(CGROUP_DIR)/%.c $(EXTRA_HDRS) $(LIBCGROU= P_O_DIRS) + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ + +EXTRA_CLEAN +=3D $(LIBCGROUP_O) --=20 2.49.0.604.gff1f9ca942-goog From nobody Fri Dec 19 16:05:31 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 066131CEAB2 for ; Mon, 14 Apr 2025 20:09:38 +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=1744661382; cv=none; b=nm/Ek1mDIaSJFX7KjoAGwP3a/NhspT+vaR8iMEUCzfhApbK57J2mx95T789OQBNM/LhcS/MCbvVxA9dmzzDkJ8HW1o4k9XzKxcKok/rXvX7uxbqCyEgjmVm1e439KbZhHBeVApx7s0OsenePCSefDLDr1+oul+jGFtUt6TS8lW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744661382; c=relaxed/simple; bh=x6q1xZ3yBUeYcvd83AJGvAMjSYwBLFzBm1ig+KWei+E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sN7HEMOfS3u7gdVVllhZHOn7LHi3SE/TzzihwU1Lp99M7vKV7FUUKlE1tuSw645bWBSMR5RJnZ+uLsgy19HvSO9QdfX2sxEky8QrEVortNI4PiNJtjat9GNvsUZmqpV0XzvWh2oEjyX/M6f2o9Gr/gusOMykwwc1SWB+b4JHt6M= 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=CN+Adv6X; 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="CN+Adv6X" Received: by mail-ua1-f74.google.com with SMTP id a1e0cc1a2514c-87003ab0511so885909241.2 for ; Mon, 14 Apr 2025 13:09:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744661378; x=1745266178; 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=yShiSNlbRrsjewo8erMpkmzkSvjH2QZkd/vX1jbbzhI=; b=CN+Adv6X0n7ch09DqGrGklfsFTNcD6Rw9lFX6mr5zfSmoYKiQz85MDhD9s4uW+MEFh pLHhcDqh5ZIyBxyN1aB6bR5Rgnb3bL3vQBwkaguURaEFwYPdFunoLeUOo4maAx/Z8Txk YlFMsnM9NdwB/zyna+77mMA+Z/obH1f0f1JxgiWVldvrBNWJWwvoiwhuIQ1TrHIBPhiD YuY2P7voLpKW/7WF3WyhNN8QQHFYi1zyBNpXDa8LNpOA1UigS8TIquvbyyjB1jpegzqg yh1Q0B4DX5ZQovivLouDWNpusZz1Th/h2IhhYVv2l9Pny+V5/cIjXEjqF07Zgunueshu hcWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744661378; x=1745266178; 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=yShiSNlbRrsjewo8erMpkmzkSvjH2QZkd/vX1jbbzhI=; b=k09S2iUpVBwt03TW47BeBkRPAYjwfSvxv8AQs8ApV4DqWEXdH5dpfvLfkgT9c3vpg1 jhKcML7gG+QEsmKkKIj98g+Ixi+iLRLZ2R6Tqr6jcj26nNjsPeGB6Q7LKQZUJ0fx9Eek Z53H/c/3bsFYaVDUqZiBRf/kUs+0cagb/khHJ6U6x0olOWs04lY2ADBo6wOX6K/vSaAH NrZyS2uzJ4JNdEPmKPuPRMtEmXsKSg8nETRXD4Ibe19fFSmb2AeDoA5OHOGhD7YFY1jQ +78Si6mWL4MQISD4o8xaVVvqg7DiPYVGF6ze6M7/8MpMJAnJGTnBEVvRDyFZRZ+uw80E fYFQ== X-Forwarded-Encrypted: i=1; AJvYcCUnLNe8kkDLH5ikRfjMITBPS7fMPGbsUE5hwD/tp54j+GLdel8cY10Xh3s1g+PlQ467WgsH6mhIMYVsnRA=@vger.kernel.org X-Gm-Message-State: AOJu0YzVYED6OIFGuk9iOkAir2dsOF6eedO4L0cUgkj1DkOtnfnXt+fM Nm8epWiWZ1MZr2octAjrXTq+yGsitcEDFsbnyXKdrsXwHYeCekkyIhiVpe7qoAd0W+T5IjKmpZe ROlBWf0tus3UmSzLjvQ== X-Google-Smtp-Source: AGHT+IFWx4KpMIbxFpAwRHbcSPjOf45XcTuJLvHUktQ+xHXKdwuwHIuf1RL64vv5UDAQCwn8mu36HAZXiw5xxVhZ X-Received: from vsbia12.prod.google.com ([2002:a05:6102:4b0c:b0:4c1:8cb2:18cb]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:5247:b0:4bb:eb4a:f9f2 with SMTP id ada2fe7eead31-4c9e4eec0damr9244631137.9.1744661377900; Mon, 14 Apr 2025 13:09:37 -0700 (PDT) Date: Mon, 14 Apr 2025 20:09:28 +0000 In-Reply-To: <20250414200929.3098202-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: <20250414200929.3098202-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250414200929.3098202-5-jthoughton@google.com> Subject: [PATCH v3 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, Yosry Ahmed , Yu Zhao , David Matlack , 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 Acked-by: Michal Koutn=C3=BD --- 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 f62b0a5aba35a..bea746878bcaa 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -204,6 +204,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/ @@ -257,7 +258,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.604.gff1f9ca942-goog From nobody Fri Dec 19 16:05:31 2025 Received: from mail-qv1-f73.google.com (mail-qv1-f73.google.com [209.85.219.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 E90511E1DE3 for ; Mon, 14 Apr 2025 20:09:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744661383; cv=none; b=gDdkoggjgV0idEdPR3Xsp9Vy5aagM1geK/O0vk9bIJOENJCJG30LymfNEzGz3MoUqqAuMPA37qEKyUCQvDPxidjpwdgpIPOhYit0bhbP6YRfFz61vh4959zWx3HXU2P3ROSwL52px3BAovfcCODoOR+4d+97aG0045VsrodcP18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744661383; c=relaxed/simple; bh=p201043Di99t1nLw6rut+rD8avuWB+hFhzTcDkeJaCA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KGOXayvRxcPzRcZVT+gLouiCFXKSWb804V//YQ0hET2PkNFlkouiZmO9WNJfN6s24V009bloiwXXdjK9Q7A5heklTj8u5REcIINvFSlU+GlK6tTHhuu1LIVpTuMhL+/7wyrh1HVKgoKsKlyLmKcvvzAjCYpAdaJKyewtruS+u0w= 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=pYUJwSO2; arc=none smtp.client-ip=209.85.219.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="pYUJwSO2" Received: by mail-qv1-f73.google.com with SMTP id 6a1803df08f44-6ed0526b507so82750156d6.0 for ; Mon, 14 Apr 2025 13:09:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744661379; x=1745266179; 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=xSg187F3qngplpXjZRdaeSKwXOSBbhcolWz5+6darDQ=; b=pYUJwSO2nTPxx2/zJKy9+MreOQpohE7PFeDIQNMHA/W4K0pNugZzg+dkN9T+eEdbXT NR0Yawh4H/xewsRELxc2pBYQmY0zu35r/4zl29dVhlCyx+evDT1NLxQ2oUAfT+QCaPKe VuDf9xZg372b0Kb52XHqSrhwMFT0WKnYpkRtVRaYL4RoRoogDeZpA8sS7pU3bEeT+eHL s2ycbeuNnLRhe23nCK6okIQNAuN7pZc8hQh2baqnKxFWDExdpXh+NCGhvHa0dXTQawK6 4TRjj2stdaiTpIzQVAvHojLNukJuiJ5Q4niYb7ThOjl8SGWyXk8f/xm+6SiNsVgUdcin Yo9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744661379; x=1745266179; 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=xSg187F3qngplpXjZRdaeSKwXOSBbhcolWz5+6darDQ=; b=GCZDDRGbudHNR5s52vMisF349+ZiVlveBKCOqDVrCG2R5T9lLTEwwBZ3nunvk8cI7Z v77UjWC06g3q6cgoAEfOdeugQl873zI0IM5TRG4a4n/GD5QBm50KgeVpbO0grwKaPqDP urFPM1+KGRB6MMgqxEFp4rRwTvETMg2pR8qYFrRJzB9XgtPQO/3/YZM3WvB9Ca68qAg5 1cnbkIw8Yk4Of+AxeizajPiqI0Lj5XPPuEfmK3ubsn76lYscKiN+ro1yxD2lxyEoPIiC vxaSrycCRSbbVTMUSOFcbAybVidPGakE6IbMG1d/zWgsh1bSb0u2bLsrS8VtQY6rn3LJ oSmw== X-Forwarded-Encrypted: i=1; AJvYcCWKmbodrttB01dKcDEClm4xr+qW0h9j4DpOEMbQaIJ6N8fdLUP8vcoSFZbmtQxHMVtScm6IHJAmqLV+9XU=@vger.kernel.org X-Gm-Message-State: AOJu0YwVNCNzU2VymaB9X7rN+c+oZWPuPrHJgFUosUjiXqEZzpdTqUVr OAlMi9j3sKXce2rOrayNKdL7GyMZX+Ee0db5fMHJFlXfPasy4NPs5y6JsfJHSbu/2ddwmjE18B1 8sQzzDqVVUprhwXlvHQ== X-Google-Smtp-Source: AGHT+IEIEXAL7ANWvr5l46/g8IoUN7NPskqTZUf8hREtMYgDzcvVx+vGaTn2JtFWXOGrjzYMPasd7uGQbxohMxuS X-Received: from qvkj10.prod.google.com ([2002:a0c:e00a:0:b0:6ee:f6c5:c00b]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6214:5003:b0:6e8:9a55:8259 with SMTP id 6a1803df08f44-6f230cb35admr250915706d6.9.1744661378801; Mon, 14 Apr 2025 13:09:38 -0700 (PDT) Date: Mon, 14 Apr 2025 20:09:29 +0000 In-Reply-To: <20250414200929.3098202-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: <20250414200929.3098202-1-jthoughton@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250414200929.3098202-6-jthoughton@google.com> Subject: [PATCH v3 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, Yosry Ahmed , Yu Zhao , David Matlack , 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 | 207 ++++++++-- .../selftests/kvm/include/lru_gen_util.h | 51 +++ .../testing/selftests/kvm/lib/lru_gen_util.c | 383 ++++++++++++++++++ 4 files changed, 616 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 bea746878bcaa..24d2a5d387d34 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 a2ac6fa2ba141..d4ef201b67055 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,14 @@ 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; + + 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"); @@ -354,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 @@ -372,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" @@ -383,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 { @@ -390,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 @@ -424,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.604.gff1f9ca942-goog