From nobody Mon Feb 9 01:06:29 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1632232337; cv=none; d=zohomail.com; s=zohoarc; b=IYOaghaAqe3fEGvmEecyKTPE4zi17g3TdZ6OUws6yBk63ID3bxK/i6sIlVM9GSfK/KOHw439SoSkuNSoYmLwAvmUVSvYJWC7Itu40fVAAWTX1KQn/l6/+j3WWID1I7uZwMu1nwDpVpDmZS9Zue8SxtTzygWrDbIcBYX5S/j6lcQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1632232337; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5gKuUV3oYO/eYXzJW11ZJUJW5Nes3R4CWFsFDXPGKfE=; b=OI10ymL4U4svpg8j88xkm+jkMZzcdmHF+iym5k2xg4nrYUaV8CVC7PIhXoDW7IFhRYPZNGQYOwYrqyLq9Ru3Fjn1bb/r5OJMrvFAx4DsgE//DgkWMsgc5cApgyu2SLqNeAmQkZMa9/kCfGI0cbrG+0Itn4BwfyCWLBIIBJupNaU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1632232337150180.10434242860117; Tue, 21 Sep 2021 06:52:17 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-474-4UixDuSSMkiF6RWd7leSUw-1; Tue, 21 Sep 2021 09:52:13 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BB762802921; Tue, 21 Sep 2021 13:52:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9689A18761; Tue, 21 Sep 2021 13:52:08 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4E9FB1803B30; Tue, 21 Sep 2021 13:52:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 18LDq7v9004228 for ; Tue, 21 Sep 2021 09:52:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3959760877; Tue, 21 Sep 2021 13:52:07 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.93]) by smtp.corp.redhat.com (Postfix) with ESMTP id 90D105BAE0; Tue, 21 Sep 2021 13:51:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1632232336; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=5gKuUV3oYO/eYXzJW11ZJUJW5Nes3R4CWFsFDXPGKfE=; b=d4DNT8q33Nen1HV2yq+AIAzy8Z5OxbxkHD84izI1bf48LnK+yjnFnFxvvEYWw5cnUxMV1x Krjm1dfV7B6hza4+gjDVFeMQsPvv20nkGFQqHnnpxuQbvUSm3H3A4eSG+NNRuinPcOnPc9 /aMtY9i7RoleAWgVseSnOYoI8s0VYtY= X-MC-Unique: 4UixDuSSMkiF6RWd7leSUw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v6 01/16] virhostmem: Introduce virHostMemGetTHPSize() Date: Tue, 21 Sep 2021 15:51:17 +0200 Message-Id: <610eb41f9354411f6bbc01f6c2af8e3aa0219238.1632231694.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: david@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1632232339165100001 Content-Type: text/plain; charset="utf-8" New virHostMemGetTHPSize() is introduced which allows caller to obtain THP PMD (Page Middle Directory) size, which is equal to the minimal size that THP can use, taken from kernel doc (Documentation/admin-guide/mm/transhuge.rst): Some userspace (such as a test program, or an optimized memory allocation library) may want to know the size (in bytes) of a transparent hugepage:: cat /sys/kernel/mm/transparent_hugepage/hpage_pmd_size Since this size depends on the host architecture and the kernel it won't change whilst libvirtd is running. Therefore, we can use virOnce() and cache the value. Of course, we can be running under kernel that has THP disabled or has no notion of THP at all. In that case a negative value is returned to signal error. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virhostmem.c | 54 ++++++++++++++++++++++++++++++++++++++++ src/util/virhostmem.h | 3 +++ tests/domaincapsmock.c | 9 +++++++ 4 files changed, 67 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 25ee21463c..efb6648913 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2410,6 +2410,7 @@ virHostMemGetFreePages; virHostMemGetInfo; virHostMemGetParameters; virHostMemGetStats; +virHostMemGetTHPSize; virHostMemSetParameters; =20 =20 diff --git a/src/util/virhostmem.c b/src/util/virhostmem.c index b13c3fe38e..5984dfab3f 100644 --- a/src/util/virhostmem.c +++ b/src/util/virhostmem.c @@ -45,11 +45,14 @@ #include "virstring.h" #include "virnuma.h" #include "virlog.h" +#include "virthread.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE =20 VIR_LOG_INIT("util.hostmem"); =20 +static unsigned long long virHostTHPPMDSize; /* in kibibytes */ +static virOnceControl virHostMemGetTHPSizeOnce =3D VIR_ONCE_CONTROL_INITIA= LIZER; =20 #ifdef __FreeBSD__ # define BSD_MEMORY_STATS_ALL 4 @@ -930,3 +933,54 @@ virHostMemAllocPages(unsigned int npages, =20 return ncounts; } + +#if defined(__linux__) +# define HPAGE_PMD_SIZE_PATH "/sys/kernel/mm/transparent_hugepage/hpage_pm= d_size" +static void +virHostMemGetTHPSizeSysfs(unsigned long long *size) +{ + if (virFileReadValueUllong(size, "%s", HPAGE_PMD_SIZE_PATH) < 0) { + VIR_WARN("unable to get THP PMD size: %s", g_strerror(errno)); + return; + } + + /* Size is now in bytes. Convert to KiB. */ + *size >>=3D 10; +} +#endif /* defined(__linux__) */ + + +static void +virHostMemGetTHPSizeOnceInit(void) +{ +#if defined(__linux__) + virHostMemGetTHPSizeSysfs(&virHostTHPPMDSize); +#else /* !defined(__linux__) */ + VIR_WARN("Getting THP size not ported yet"); +#endif /* !defined(__linux__) */ +} + + +/** + * virHostMemGetTHPSize: + * @size: returned size of THP in kibibytes + * + * Obtain Transparent Huge Page size in kibibytes. The size + * depends on host architecture and kernel. Because of virOnce(), + * do not rely on errno in case of failure. + * + * Returns: 0 on success, + * -1 on failure. + */ +int +virHostMemGetTHPSize(unsigned long long *size) +{ + if (virOnce(&virHostMemGetTHPSizeOnce, virHostMemGetTHPSizeOnceInit) <= 0) + return -1; + + if (virHostTHPPMDSize =3D=3D 0) + return -1; + + *size =3D virHostTHPPMDSize; + return 0; +} diff --git a/src/util/virhostmem.h b/src/util/virhostmem.h index 3265215d84..c36de94f0f 100644 --- a/src/util/virhostmem.h +++ b/src/util/virhostmem.h @@ -55,3 +55,6 @@ int virHostMemAllocPages(unsigned int npages, unsigned int cellCount, int lastCell, bool add); + +int virHostMemGetTHPSize(unsigned long long *size) + G_GNUC_NO_INLINE; diff --git a/tests/domaincapsmock.c b/tests/domaincapsmock.c index 0a6c541f77..d382d06e27 100644 --- a/tests/domaincapsmock.c +++ b/tests/domaincapsmock.c @@ -17,6 +17,7 @@ #include =20 #include "virhostcpu.h" +#include "virhostmem.h" =20 #if WITH_QEMU # include "virmock.h" @@ -51,3 +52,11 @@ virQEMUCapsGetKVMSupportsSecureGuest(virQEMUCaps *qemuCa= ps) return real_virQEMUCapsGetKVMSupportsSecureGuest(qemuCaps); } #endif + +int +virHostMemGetTHPSize(unsigned long long *size) +{ + /* Pretend Transparent Huge Page size is 2MiB. */ + *size =3D 2048; + return 0; +} --=20 2.32.0