From nobody Fri Dec 19 17:53:02 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