Received: by 2002:ac2:5544:0:0:0:0:0 with SMTP id l4csp1077567lfk; Thu, 2 Dec 2021 19:37:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJwlR85W/e2BLG5EoqtCOxjo4db949pqwgpNrctHLKU9XsVPZXISxZn4lOKOVFyUMTthE3AX X-Received: by 2002:a25:2d17:: with SMTP id t23mr19929661ybt.409.1638502658213; Thu, 02 Dec 2021 19:37:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1638502658; cv=none; d=google.com; s=arc-20160816; b=o5cK2AqRU80lIbU1774hoUngtTfpXejkEKlea4rzyUctQLjXUPDuNg2DsDPiGMKBIp fM1FGt7/VbusGqN3s+mPWKqWRcgb677fy4SzK4rm4V2BBGA0+FtxLfSYJarxb+GM0JTa P0yfaglALBN72S9Eg/19wuvrnFGqgH6TMedoZIrpjWwLHuOX0esm6k0qHmQAV1Q5fKa8 5x+Bfawa62j00mmkZ3zgH2xmj1PeBkWMfAfHf85sB+qh/5Sku4hN/ST8EzeZS2Ntv/wv oDy5nv9tw80xHBFETHPKy86p9lp4JF0LfW3qsmVX+JcxGl9faknPsg2mrkpHuXFjtjEm rQ6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=MvFj68JEa2qXhIsVUtFSb5qPeyg0dZQyfJNLJhegMoA=; b=FYEVaP52wmY0LMlKuA7a9OEbuKLeLQprC5HZSX4tFoIItcembHxsUe+HpfVJVVTmiB Afk2FTGFdc/Bxjhj3BdiC1KbSrhhn8fbczZK5y8S096isZN1FYY6+BZg8soFJ6JbCes+ Dm5E2743+KSaFKQMffk04HLEkXVASOotDITrGXFU1CuYKkK6e7u4JLwTDfmnnuzhljue yWdGtDecFhFCQY7g3p8q2gPkQLDz+OvznP7zKKlAQeHn1l3d9CVZe52OvL3Hyo+nvcL0 7Bs8NvYps6E5NdD1yEDzeY1h9hpyN3mB79ODpIMvFbVSYAALtdntuHIjWMT+LY8imYBn VV4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@redhat.com header.s=mimecast20190719 header.b=OkeSPIU7; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id h67si2038299ybb.748.2021.12.02.19.37.38 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 02 Dec 2021 19:37:38 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@redhat.com header.s=mimecast20190719 header.b=OkeSPIU7; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from localhost ([::1]:57612 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mszOD-0003PS-K4 for alex.bennee@linaro.org; Thu, 02 Dec 2021 22:37:37 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46382) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mszNV-0003Lc-BD for qemu-arm@nongnu.org; Thu, 02 Dec 2021 22:36:56 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:33512) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mszNT-0002Wd-DB for qemu-arm@nongnu.org; Thu, 02 Dec 2021 22:36:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1638502610; h=from:from: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; bh=MvFj68JEa2qXhIsVUtFSb5qPeyg0dZQyfJNLJhegMoA=; b=OkeSPIU7kokgeD/HhpawHi5U6iWpDuH+qDJ3Sy/eoRy0US0/K+cG5NsTPNUET7276sSHEx 8yxsIdjPUYNTFBx9VDSmceV0ExnaVTlPdqF369ST7lKBYw6XNVx7mT5HTRU2JY/pRQlMiZ ZZKt/TRChFiRCxZgur9kSAH81uPlWqM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-102-KUSUOyGNMJakJZMlrYmlow-1; Thu, 02 Dec 2021 22:36:45 -0500 X-MC-Unique: KUSUOyGNMJakJZMlrYmlow-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 746211006AA1; Fri, 3 Dec 2021 03:36:44 +0000 (UTC) Received: from gshan.redhat.com (vpn2-54-43.bne.redhat.com [10.64.54.43]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7D5B588F6; Fri, 3 Dec 2021 03:36:25 +0000 (UTC) From: Gavin Shan To: qemu-arm@nongnu.org Subject: [PATCH v2 2/2] virtio-mem: Correct default THP size for ARM64 Date: Fri, 3 Dec 2021 11:35:22 +0800 Message-Id: <20211203033522.27580-3-gshan@redhat.com> In-Reply-To: <20211203033522.27580-1-gshan@redhat.com> References: <20211203033522.27580-1-gshan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=gshan@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=170.10.133.124; envelope-from=gshan@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.719, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-arm@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, drjones@redhat.com, david@redhat.com, richard.henderson@linaro.org, qemu-devel@nongnu.org, eric.auger@redhat.com, shan.gavin@gmail.com, Jonathan.Cameron@huawei.com, imammedo@redhat.com Errors-To: qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org Sender: "Qemu-arm" X-TUID: gb/3iHCZ4seg The default block size is same as to the THP size, which is either retrieved from "/sys/kernel/mm/transparent_hugepage/hpage_pmd_size" or hardcoded to 2MB. There are flaws in both mechanisms and this intends to fix them up. * When "/sys/kernel/mm/transparent_hugepage/hpage_pmd_size" is used to getting the THP size, 32MB and 512MB are valid values when we have 16KB and 64KB page size on ARM64. * When the hardcoded THP size is used, 2MB, 32MB and 512MB are valid values when we have 4KB, 16KB and 64KB page sizes on ARM64. Co-developed-by: David Hildenbrand Signed-off-by: Gavin Shan --- hw/virtio/virtio-mem.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index ac7a40f514..8f3c95300f 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -38,14 +38,25 @@ */ #define VIRTIO_MEM_MIN_BLOCK_SIZE ((uint32_t)(1 * MiB)) -#if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || \ - defined(__powerpc64__) -#define VIRTIO_MEM_DEFAULT_THP_SIZE ((uint32_t)(2 * MiB)) -#else - /* fallback to 1 MiB (e.g., the THP size on s390x) */ -#define VIRTIO_MEM_DEFAULT_THP_SIZE VIRTIO_MEM_MIN_BLOCK_SIZE +static uint32_t virtio_mem_default_thp_size(void) +{ + uint32_t default_thp_size = VIRTIO_MEM_MIN_BLOCK_SIZE; + +#if defined(__x86_64__) || defined(__arm__) || defined(__powerpc64__) + default_thp_size = (uint32_t)(2 * MiB); +#elif defined(__aarch64__) + if (qemu_real_host_page_size == (4 * KiB)) { + default_thp_size = (uint32_t)(2 * MiB); + } else if (qemu_real_host_page_size == (16 * KiB)) { + default_thp_size = (uint32_t)(32 * MiB); + } else if (qemu_real_host_page_size == (64 * KiB)) { + default_thp_size = (uint32_t)(512 * MiB); + } #endif + return default_thp_size; +} + /* * We want to have a reasonable default block size such that * 1. We avoid splitting THPs when unplugging memory, which degrades @@ -78,11 +89,8 @@ static uint32_t virtio_mem_thp_size(void) if (g_file_get_contents(HPAGE_PMD_SIZE_PATH, &content, NULL, NULL) && !qemu_strtou64(content, &endptr, 0, &tmp) && (!endptr || *endptr == '\n')) { - /* - * Sanity-check the value, if it's too big (e.g., aarch64 with 64k base - * pages) or weird, fallback to something smaller. - */ - if (!tmp || !is_power_of_2(tmp) || tmp > 16 * MiB) { + /* Sanity-check the value and fallback to something reasonable. */ + if (!tmp || !is_power_of_2(tmp)) { warn_report("Read unsupported THP size: %" PRIx64, tmp); } else { thp_size = tmp; @@ -90,7 +98,7 @@ static uint32_t virtio_mem_thp_size(void) } if (!thp_size) { - thp_size = VIRTIO_MEM_DEFAULT_THP_SIZE; + thp_size = virtio_mem_default_thp_size(); warn_report("Could not detect THP size, falling back to %" PRIx64 " MiB.", thp_size / MiB); } -- 2.23.0