Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3608896imw; Mon, 11 Jul 2022 11:59:10 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s0rGC5a1uBNK40lTHgsbIotT0qd6mPY78y18oB87WkLakBr2TKeZr9WYkM2t+6E6nTNWQ5 X-Received: by 2002:a17:906:5d0d:b0:726:be93:1118 with SMTP id g13-20020a1709065d0d00b00726be931118mr19327634ejt.503.1657565950515; Mon, 11 Jul 2022 11:59:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657565950; cv=none; d=google.com; s=arc-20160816; b=pDzV6Xy2TqtDJTS9fHLmsbaomNZBU5E0PMl5ooUMxsdDz7fjdnZN5xqBjc6G9P/Jn9 ufbCpY0W6npEDIP3mBbRuer5IcbL5LeAEyVrXq2MSdCDExmGy36rGrE91u8Oa91L6owq /np+ES2WirnUtIRwv58sdEKEuoMR5WVQOOZNp+jifwV57rSNK28RlT06C7dSShaUZ1S2 vZSZZZLhJMbZAPbJ6GU6mXBTB/RrL7Zkq0iok3jKwUfgihYnsuMg1AAZBdyyZWilQnU2 70C929kRokLGrycONJdDMavKjQtse4a0JCiqklErTb4Fw0C6WYGvo1SiTrVfoVPLL0RJ oadg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=6TNzOmmaE7owK+Xs0Wz9tumaPftJVKStdbpBQwl8RRY=; b=Ag026hgVjEN2k8oM0L20bXIAUK/bJXMXotxIZej6BQYOsSiv58/6k+Q9EmbkvvTY8o qEelyRR3Av5t4QZRpHmvMjPsNHezzvJ0RnjicP7L46Zy0L5oSq3h72+KAGDOsviqlsY7 4/1SotSGf58mjR8MRyVVY3DmnFQ/vVbGGlX7yy3z5w49aELW8CwZW05MiLZiB9/SbajR oklc662m2xuzR917Tx59ZYlmv+PmAy+NNrNROC1WyEnTWohXXj6v+CC38OdUrQwQsFT/ ONkBsX/BtohSMvfhRMH7KvAaRt1S7Hbub23Gwp9YK65+VkGtAK+LwXBDqOIq1M+cYBVX TKAg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hp38-20020a1709073e2600b0072b3bc0d215si9789701ejc.149.2022.07.11.11.58.43; Mon, 11 Jul 2022 11:59:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229490AbiGKSTP (ORCPT + 99 others); Mon, 11 Jul 2022 14:19:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229903AbiGKSTL (ORCPT ); Mon, 11 Jul 2022 14:19:11 -0400 Received: from relay.virtuozzo.com (relay.virtuozzo.com [130.117.225.111]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF9E5DFD5; Mon, 11 Jul 2022 11:19:09 -0700 (PDT) Received: from [192.168.16.236] (helo=vzdev.sw.ru) by relay.virtuozzo.com with esmtp (Exim 4.95) (envelope-from ) id 1oAxzO-009oVU-VU; Mon, 11 Jul 2022 20:18:46 +0200 From: Alexander Atanasov To: "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui Cc: kernel@openvz.org, Alexander Atanasov , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/1] Create debugfs file with hyper-v balloon usage information Date: Mon, 11 Jul 2022 18:18:22 +0000 Message-Id: <20220711181825.52318-1-alexander.atanasov@virtuozzo.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allow the guest to know how much it is ballooned by the host. It is useful when debugging out of memory conditions. When host gets back memory from the guest it is accounted as used memory in the guest but the guest have no way to know how much it is actually ballooned. Expose current state, flags and max possible memory to the guest. While at it - fix a 10+ years old typo. Signed-off-by: Alexander Atanasov --- drivers/hv/hv_balloon.c | 135 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 6 deletions(-) V1->V2: - Fix C&P errors V2->V3: - Move computation to a separate funcion - Remove ifdefs to reduce code clutter This patch addresses your suggestions. Once again, sorry i have missed your email reply last time. diff --git a/drivers/hv/hv_balloon.c b/drivers/hv/hv_balloon.c index 91e8a72eee14..ba52d3a3e3e3 100644 --- a/drivers/hv/hv_balloon.c +++ b/drivers/hv/hv_balloon.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -248,7 +249,7 @@ struct dm_capabilities_resp_msg { * num_committed: Committed memory in pages. * page_file_size: The accumulated size of all page files * in the system in pages. - * zero_free: The nunber of zero and free pages. + * zero_free: The number of zero and free pages. * page_file_writes: The writes to the page file in pages. * io_diff: An indicator of file cache efficiency or page file activity, * calculated as File Cache Page Fault Count - Page Read Count. @@ -567,6 +568,11 @@ struct hv_dynmem_device { __u32 version; struct page_reporting_dev_info pr_dev_info; + + /* + * Maximum number of pages that can be hot_add-ed + */ + __u64 max_dynamic_page_count; }; static struct hv_dynmem_device dm_device; @@ -1078,6 +1084,7 @@ static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg) pr_info("Max. dynamic memory size: %llu MB\n", (*max_page_count) >> (20 - HV_HYP_PAGE_SHIFT)); + dm->max_dynamic_page_count = *max_page_count; } break; @@ -1116,6 +1123,19 @@ static unsigned long compute_balloon_floor(void) return min_pages; } +/* + * Compute total committed memory pages + */ + +static unsigned long get_pages_committed(struct hv_dynmem_device *dm) +{ + return vm_memory_committed() + + dm->num_pages_ballooned + + (dm->num_pages_added > dm->num_pages_onlined ? + dm->num_pages_added - dm->num_pages_onlined : 0) + + compute_balloon_floor(); +} + /* * Post our status as it relates memory pressure to the * host. Host expects the guests to post this status @@ -1157,11 +1177,7 @@ static void post_status(struct hv_dynmem_device *dm) * asking us to balloon them out. */ num_pages_avail = si_mem_available(); - num_pages_committed = vm_memory_committed() + - dm->num_pages_ballooned + - (dm->num_pages_added > dm->num_pages_onlined ? - dm->num_pages_added - dm->num_pages_onlined : 0) + - compute_balloon_floor(); + num_pages_committed = get_pages_committed(dm); trace_balloon_status(num_pages_avail, num_pages_committed, vm_memory_committed(), dm->num_pages_ballooned, @@ -1807,6 +1823,109 @@ static int balloon_connect_vsp(struct hv_device *dev) return ret; } +/* + * DEBUGFS Interface + */ +#ifdef CONFIG_DEBUG_FS + +/** + * hv_balloon_debug_show - shows statistics of balloon operations. + * @f: pointer to the &struct seq_file. + * @offset: ignored. + * + * Provides the statistics that can be accessed in hv-balloon in the debugfs. + * + * Return: zero on success or an error code. + */ +static int hv_balloon_debug_show(struct seq_file *f, void *offset) +{ + struct hv_dynmem_device *dm = f->private; + char *sname; + + seq_printf(f, "%-22s: %u.%u\n", "host_version", + DYNMEM_MAJOR_VERSION(dm->version), + DYNMEM_MINOR_VERSION(dm->version)); + + seq_printf(f, "%-22s:", "capabilities"); + if (ballooning_enabled()) + seq_puts(f, " enabled"); + + if (hot_add_enabled()) + seq_puts(f, " hot_add"); + + seq_puts(f, "\n"); + + seq_printf(f, "%-22s: %u", "state", dm->state); + switch (dm->state) { + case DM_INITIALIZING: + sname = "Initializing"; + break; + case DM_INITIALIZED: + sname = "Initialized"; + break; + case DM_BALLOON_UP: + sname = "Balloon Up"; + break; + case DM_BALLOON_DOWN: + sname = "Balloon Down"; + break; + case DM_HOT_ADD: + sname = "Hot Add"; + break; + case DM_INIT_ERROR: + sname = "Error"; + break; + default: + sname = "Unknown"; + } + seq_printf(f, " (%s)\n", sname); + + /* HV Page Size */ + seq_printf(f, "%-22s: %ld\n", "page_size", HV_HYP_PAGE_SIZE); + + /* Pages added with hot_add */ + seq_printf(f, "%-22s: %u\n", "pages_added", dm->num_pages_added); + + /* pages that are "onlined"/used from pages_added */ + seq_printf(f, "%-22s: %u\n", "pages_onlined", dm->num_pages_onlined); + + /* pages we have given back to host */ + seq_printf(f, "%-22s: %u\n", "pages_ballooned", dm->num_pages_ballooned); + + seq_printf(f, "%-22s: %lu\n", "total_pages_commited", + get_pages_committed(dm)); + + seq_printf(f, "%-22s: %llu\n", "max_dynamic_page_count", + dm->max_dynamic_page_count); + + return 0; +} + +DEFINE_SHOW_ATTRIBUTE(hv_balloon_debug); + +static void hv_balloon_debugfs_init(struct hv_dynmem_device *b) +{ + debugfs_create_file("hv-balloon", 0444, NULL, b, + &hv_balloon_debug_fops); +} + +static void hv_balloon_debugfs_exit(struct hv_dynmem_device *b) +{ + debugfs_remove(debugfs_lookup("hv-balloon", NULL)); +} + +#else + +static inline void hv_balloon_debugfs_init(struct hv_dynmem_device *b) +{ +} + +static inline void hv_balloon_debugfs_exit(struct hv_dynmem_device *b) +{ +} + +#endif /* CONFIG_DEBUG_FS */ + static int balloon_probe(struct hv_device *dev, const struct hv_vmbus_device_id *dev_id) { @@ -1854,6 +1973,8 @@ static int balloon_probe(struct hv_device *dev, goto probe_error; } + hv_balloon_debugfs_init(&dm_device); + return 0; probe_error: @@ -1879,6 +2000,8 @@ static int balloon_remove(struct hv_device *dev) if (dm->num_pages_ballooned != 0) pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned); + hv_balloon_debugfs_exit(dm); + cancel_work_sync(&dm->balloon_wrk.wrk); cancel_work_sync(&dm->ha_wrk.wrk); -- 2.25.1