From patchwork Mon Jul 17 12:12:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13315550 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24CDBC001B0 for ; Mon, 17 Jul 2023 12:12:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230268AbjGQMMm (ORCPT ); Mon, 17 Jul 2023 08:12:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229779AbjGQMMk (ORCPT ); Mon, 17 Jul 2023 08:12:40 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9934D118 for ; Mon, 17 Jul 2023 05:12:37 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c361777c7f7so8671579276.0 for ; Mon, 17 Jul 2023 05:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689595957; x=1692187957; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4azJb86a4WmgALvbmR8yn/1ad4s0HlGtDbq1VbhmENs=; b=Kho+0sGzwcpR++Nyb0731VhgCw9eC1gMeJSDCUZZn+kRwGfUZdPMPY1psSGFZ5FhDA pLy9d1gew+FB1lTr9DBeXx2LgpJX9bq9NDlcnEA+epOR2I24IzeLY4bNeZjGWhDI6wHU F8KFmGIRtfwg5bUwCspGeEsbN5nBqctNB6stT2cJyPd/IJ6f9zhctYEuY7RAA+zF29of ohTS8Z0u8nq2P0IcQOgs6AkGOIrRnkowTTo4Ux3cZ5RACPKuIrywYJ3I6jUlo4yM3tBl g1X+vEdnCK45mel97vvyBjT7NRYGfBqYomWKtf/At9JbD8A/qNdGRsBzowmDRDL4gSFe dwqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689595957; x=1692187957; 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=4azJb86a4WmgALvbmR8yn/1ad4s0HlGtDbq1VbhmENs=; b=BpNneNpIa8g06x8zbPETYsBy6Di2uGrN5dnzHttzQ6fIg4nShtFMw8ghXqaBui7UFt iRqEQD62FOqFvyRAw3PKaCfRjzxSGUa3E3stGfPY7vGOh5Go2wYeSrpc6hmPDxQDpiqZ 6uHhe0p1oHdZaFydjq5qfOGkAT22PBFhEnSur3syPLs1NaUNmi+fkzoekXyEUjhTzcD4 efj+BZmgcoNY8QucrJ5/twnnYjqOJOE7P54aUFAPP8k0PVw31AcCWZnTMtlab1PUmfax s2KAmqs8Q9u8/omY4x+KRxIWGVg7AdbGi0pQOdF9a+9sOuNNdxbYJUJD5ihJHm9Elix2 U5EA== X-Gm-Message-State: ABy/qLbeIokL/0D9D4FH2v4LeLaB0X+Pgc4aT1HaNdyYOAHFjs93ZZIt OVSBmE2TDsc8b1WzW4n23JDhYyZulnptmFgEaBBekLWiotwwitKFPnuBUJyDt211WBbzIesxgSO pbH/2FynTHOBEmZW1GJ/JHkJJB7O8cFigs2EqXmGG7B1WPCXYzvWkwLs= X-Google-Smtp-Source: APBJJlFndxcL0vcK05hAIkrh+dLL0psAj8yDKxmv6Ipl7looOvfOW65abdEM3mbQD0ZhqHFCErpxwqKJZw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:6902:11c9:b0:c2c:1b68:99b0 with SMTP id n9-20020a05690211c900b00c2c1b6899b0mr187313ybu.5.1689595956747; Mon, 17 Jul 2023 05:12:36 -0700 (PDT) Date: Mon, 17 Jul 2023 13:12:30 +0100 In-Reply-To: <20230717121232.3559948-1-tabba@google.com> Mime-Version: 1.0 References: <20230717121232.3559948-1-tabba@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230717121232.3559948-2-tabba@google.com> Subject: [PATCH kvmtool v2 1/3] Factor out getting the host page size From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, will@kernel.org, penberg@kernel.org, alexandru.elisei@arm.com, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Factor out getting the page size of the host into a separate function. This will be used in a subsequent patch. No functional change intended. Signed-off-by: Fuad Tabba --- builtin-run.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index b1a27fd..2801735 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -360,9 +360,21 @@ static void kernel_usage_with_options(void) KVM_BINARY_NAME); } +static long host_page_size(void) +{ + long page_size = sysconf(_SC_PAGE_SIZE); + + if (page_size < 0) { + pr_warning("sysconf(_SC_PAGE_SIZE) failed"); + return 0; + } + + return page_size; +} + static u64 host_ram_size(void) { - long page_size; + long page_size = host_page_size(); long nr_pages; nr_pages = sysconf(_SC_PHYS_PAGES); @@ -371,12 +383,6 @@ static u64 host_ram_size(void) return 0; } - page_size = sysconf(_SC_PAGE_SIZE); - if (page_size < 0) { - pr_warning("sysconf(_SC_PAGE_SIZE) failed"); - return 0; - } - return (u64)nr_pages * page_size; } From patchwork Mon Jul 17 12:12:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13315552 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 287C0EB64DC for ; Mon, 17 Jul 2023 12:12:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231218AbjGQMMq (ORCPT ); Mon, 17 Jul 2023 08:12:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231197AbjGQMMn (ORCPT ); Mon, 17 Jul 2023 08:12:43 -0400 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F6AC118 for ; Mon, 17 Jul 2023 05:12:40 -0700 (PDT) Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-3144bf68a03so2462470f8f.2 for ; Mon, 17 Jul 2023 05:12:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689595959; x=1692187959; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9rW4nK/BV1RMi69g81MFe255uXHx50GdISNGnF/xOhk=; b=S0hQv11THvUz7i6nWCQ3ScH1iGlPllP3xj9+uvpiPJ6b/Z9xCe0Df2ba+O/H7/3jQ1 YcGoPxP/Mpj2EFtbaVc3F4djkkG1s3sz0umDKrfwREBJ6/3iiaqEjqxUHvLVR8yV42S4 2SL7xW5cwoqPkBdmJNKia9k8o6QrXf0/giSuucshA1BUAJ8QBaAActN0Q659CviB2UtE PcYcpr8AB2CEu3OvbKPWIZWJZmL/+rv3atg9OwWgD3K6sRUTn6Y6DHizbFgVtxmjxWAP 0Wdysc7bAYM+YRGi14AJxAXLnkmmbucrcGGYQVswilj2BM/lblPoVwDKt+E0IWTtsEjj U3Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689595959; x=1692187959; 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=9rW4nK/BV1RMi69g81MFe255uXHx50GdISNGnF/xOhk=; b=K/MNSfjPTEgHPl5gIrgVgqStOgHvoWQlDw4Fkvz7Pe5Vgd2578UyZelEW4hmlLKKnh EIO4OklnNySFtMJYoUBM5/oydZDD8DDgGBe19rU3V/db2Gsj75Q+Wjwnvv2KEFYmSB5i /1UyAwo2nsdlbtVHASitpKrysh3M4N75pHciqHdc4oTQ3Awnj8Ao2gGP1JM6WkdMxWMy 9IPDdbc005PteOlehbXWH/0EJzQVAwwxvZuR8vnskjxsX8Y4XMAZmLgcQEHXJv862NqH BuAoj0ZkZADbrJBP4hBD/90YUZGQfAj0NiW74Y3cD/ETVz0POiKGpSZW2gHc+X6DIhwr X2OQ== X-Gm-Message-State: ABy/qLYZkY29Pd+2E3Kk9S9xeAhJl+AirXBvYX0bWIx3BqYWEnZuyVIK drnJ2O8AkG2n7FNAqrCs6DCEPJFTQZn3KZ3kbHczj2MBD7UfwIjwpkdB4tAh6A7akJ/A3VjBnvd uF5yDCfQK8FfDQOYsRyCELeN7827QvLsXCbBhZEf0ui1eKeo+NQXwyC4= X-Google-Smtp-Source: APBJJlHyDkVoY1Ob4ztLHt0+dMzJdr/O6DlQOmekssoXdZknKA8Zjo8tf3jXOPBSSQQtcxlzsvVn7Q7Xlw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a5d:524b:0:b0:314:35c:a91 with SMTP id k11-20020a5d524b000000b00314035c0a91mr86957wrc.2.1689595958904; Mon, 17 Jul 2023 05:12:38 -0700 (PDT) Date: Mon, 17 Jul 2023 13:12:31 +0100 In-Reply-To: <20230717121232.3559948-1-tabba@google.com> Mime-Version: 1.0 References: <20230717121232.3559948-1-tabba@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230717121232.3559948-3-tabba@google.com> Subject: [PATCH kvmtool v2 2/3] Factor out getting the number of physical memory host pages From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, will@kernel.org, penberg@kernel.org, alexandru.elisei@arm.com, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Factor out getting the number of physical pages available for the host into a separate function. This will be used in a subsequent patch. No functional change intended. Signed-off-by: Fuad Tabba --- builtin-run.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 2801735..44ea690 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -372,17 +372,23 @@ static long host_page_size(void) return page_size; } -static u64 host_ram_size(void) +static long host_ram_nrpages(void) { - long page_size = host_page_size(); - long nr_pages; + long nr_pages = sysconf(_SC_PHYS_PAGES); - nr_pages = sysconf(_SC_PHYS_PAGES); if (nr_pages < 0) { pr_warning("sysconf(_SC_PHYS_PAGES) failed"); return 0; } + return nr_pages; +} + +static u64 host_ram_size(void) +{ + long page_size = host_page_size(); + long nr_pages = host_ram_nrpages(); + return (u64)nr_pages * page_size; } From patchwork Mon Jul 17 12:12:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13315553 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07BE6EB64DC for ; Mon, 17 Jul 2023 12:13:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231237AbjGQMMs (ORCPT ); Mon, 17 Jul 2023 08:12:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229379AbjGQMMn (ORCPT ); Mon, 17 Jul 2023 08:12:43 -0400 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7986AD8 for ; Mon, 17 Jul 2023 05:12:42 -0700 (PDT) Received: by mail-wr1-x44a.google.com with SMTP id ffacd0b85a97d-31444df0fafso2729621f8f.2 for ; Mon, 17 Jul 2023 05:12:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689595961; x=1692187961; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NLms2yNMjCb4l7NoGPMRYQDTCpmy6IHTKw/97o5lGBU=; b=QmdHU+U4emA+GF2AYjkPJ8VNkHxwvF6P/IkZuWjfuMREq1MqlRp9XyUJl96wFibCok N8Xps3Gf4PBDnVCEz6YfzdXe+Y9ZMkZ1/OfCbet02uikl017K/8RK+gWani522JPDmsu U9jTpyqE3hB6/5cvdyNSVWK1Y0d1EJOXCOgnhoYvjAYjFM+SrMwP1FYljnJQHT5rJDw2 93PgtjTkJN+fvW3hPDopAbY29Gm2/l2NAnLVvvhl8vehPa2iQmCkKIWMLgfTwz7n0gWW Ugoy++xUdGDWRS9x7IJh7qIK7gkcdK1nk1VL0D7x71Zi6jMGJZAvnO0q9h17pHW+UAVu FY7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689595961; x=1692187961; 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=NLms2yNMjCb4l7NoGPMRYQDTCpmy6IHTKw/97o5lGBU=; b=gqbQmUBlsb8CpvbZhAYw5BvE9OD1YgNFgEoqICfvvzOr4TxrFD+oM7NHrX52zpt8hC u4WcnuK+3zUyGUVrm94RnKZUX9F+4uEIQ1mPWqvQlncxtUqJYl8bHJGIAcciyijSeuGU WzemlwIOsWCtBVqklq0Uhwk0otFBW4b5g+gyHRi/WzXRMcKwMj9ng8oCjro41SFG0+X/ /HF7pl9NqhCnVvkMtOyrp/4AaMBUi7ftBj7FFTTea9S9peprbhkz/xRJz2ytzyLzMQFp Li9R8UvD+WnjgstRNwA9W+qtivVNwrj8JNd5Es3PGma4JMkGco9RxG9XQob7pEOZfNYG FEVw== X-Gm-Message-State: ABy/qLYU/DZB5vzJr65gqWPUt86WV7S651Mm2eYpl3VmwZ6SijsiDk0G EwoVJR2yZT3esh2CUH69Sc4/k0tzb0NDGHFjow6mANjNEgoWOk2OhGJW/bZNwBcmfyit1kfSkko aQE0mAMpusty5iHZYVGraWLoyrBxTnDyYPa/SgpxyaFuj7p7vQFUOAsk= X-Google-Smtp-Source: APBJJlHla9QvfvGoaIP/iegIGu34wNmyMyC6f5qHUv/oj86f6Vckg8aIeeIkC5Qtf2mpkdQo1kYF+6MPYQ== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a5d:5603:0:b0:30f:c589:8779 with SMTP id l3-20020a5d5603000000b0030fc5898779mr79353wrv.5.1689595961142; Mon, 17 Jul 2023 05:12:41 -0700 (PDT) Date: Mon, 17 Jul 2023 13:12:32 +0100 In-Reply-To: <20230717121232.3559948-1-tabba@google.com> Mime-Version: 1.0 References: <20230717121232.3559948-1-tabba@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230717121232.3559948-4-tabba@google.com> Subject: [PATCH kvmtool v2 3/3] Apply scaling down the calculated guest ram size to the number of pages From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, will@kernel.org, penberg@kernel.org, alexandru.elisei@arm.com, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Calculate the guest ram size based a ratio proportional to the number of pages available, rather than the amount of memory available in bytes, in the host. This is to ensure that the result is always page-aligned. If the result of get_ram_size() isn't aligned to the host page size, it triggers an error in __kvm_set_memory_region(), called via the KVM_SET_USER_MEMORY_REGION ioctl, which requires the size to be page-aligned. Fixes: 18bd8c3bd2a7 ("kvm tools: Don't use all of host RAM for guests by default") Signed-off-by: Fuad Tabba --- builtin-run.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 44ea690..21373d4 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -400,17 +400,15 @@ static u64 host_ram_size(void) static u64 get_ram_size(int nr_cpus) { - u64 available; - u64 ram_size; + long nr_pages_available = host_ram_nrpages() * RAM_SIZE_RATIO; + u64 ram_size = (u64)SZ_64M * (nr_cpus + 3); + u64 available = MIN_RAM_SIZE; - ram_size = (u64)SZ_64M * (nr_cpus + 3); - - available = host_ram_size() * RAM_SIZE_RATIO; - if (!available) - available = MIN_RAM_SIZE; + if (nr_pages_available) + available = nr_pages_available * host_page_size(); if (ram_size > available) - ram_size = available; + ram_size = available; return ram_size; }