From patchwork Tue Mar 25 01:57:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 14028003 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0404E16EB4C for ; Tue, 25 Mar 2025 01:57:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742867876; cv=none; b=hfmWZQWPVUY8Zo3gByNtzk2eqNcENk8/1E0yLWfWTg/wvSUqf/zS4buRHuijiVysSI2x+XsFxLUjS2Cm/zqK2cl24iftuYAv0UG4tePf/BOMfR3DN2QRNiI0G0bPiSyOFY9nHnQrsJeh6jKkathQZbhzyRhrlIB+3/IegRS7OLQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742867876; c=relaxed/simple; bh=lif6d8bWyTb3kY1w8tbIfNSxz0oEm+nK9IO5vgIDGZw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e+UR7Ns6WmvcdczT2zJu+vXaFMjzTai4eKAwaKHaTByi54TKhpu4P6R38O3pfchpsLh5lpBg1HgOv+Cm0m/NmVwoZN/86fLBXn6oQW3W0DcCFJUosIbiqQbVeRgFs/CkShl6E8OOcJLhMkbTXXR+vPrJQ2a9D8eei3Dncgkt0qs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Qp+l8ecT; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Qp+l8ecT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1742867873; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kTv/L90XVpBM4ekA2qEPhG7vXF0lbFqegJyvg+paXag=; b=Qp+l8ecTjr6BcdROy+7gZuIpGT6h1l5n01v8JsUkMZJ7lESV8NQlEE28zG5IwAMjGDLEPh mlepFajV2Og1wZoptNxKOf+grtbkF25THFgHJql/nlxXVoQ3azfX1YTg9U0P/f0eD+lGZz Kgyd9k0joRtfpCUbSfpMLJRTGxChBGE= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-136--KUwRpU6NHupmsJ2c19hFA-1; Mon, 24 Mar 2025 21:57:49 -0400 X-MC-Unique: -KUwRpU6NHupmsJ2c19hFA-1 X-Mimecast-MFC-AGG-ID: -KUwRpU6NHupmsJ2c19hFA_1742867867 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7BB3E18004A9; Tue, 25 Mar 2025 01:57:47 +0000 (UTC) Received: from starship.lan (unknown [10.22.65.191]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 630FF180175F; Tue, 25 Mar 2025 01:57:45 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: Muhammad Usama Anjum , linux-kernel@vger.kernel.org, Sean Christopherson , Maxim Levitsky , James Houghton , Shuah Khan , Claudio Imbrenda , Oliver Upton , Paolo Bonzini , linux-kselftest@vger.kernel.org, Anup Patel Subject: [PATCH v2 1/2] KVM: selftests: Extract guts of THP accessor to standalone sysfs helpers Date: Mon, 24 Mar 2025 21:57:40 -0400 Message-Id: <20250325015741.2478906-2-mlevitsk@redhat.com> In-Reply-To: <20250325015741.2478906-1-mlevitsk@redhat.com> References: <20250325015741.2478906-1-mlevitsk@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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 --- 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/selftests/kvm/lib/test_util.c index 8ed0b74ae837..3dc8538f5d69 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"); } -bool thp_configured(void) +static bool test_sysfs_path(const char *path) { - int ret; struct stat statbuf; + int ret; - ret = stat("/sys/kernel/mm/transparent_hugepage", &statbuf); + ret = stat(path, &statbuf); TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT), - "Error in stating /sys/kernel/mm/transparent_hugepage"); + "Error in stat()ing '%s'", path); return ret == 0; } -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; - TEST_ASSERT(thp_configured(), "THP is not configured in host kernel"); - - f = fopen("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "r"); - TEST_ASSERT(f != NULL, "Error in opening transparent_hugepage/hpage_pmd_size"); + f = fopen(path, "r"); + TEST_ASSERT(f, "Error opening '%s'", path); ret = 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 = 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); + fclose(f); return size; } +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];