From patchwork Sat May 7 19:09:05 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pekka Enberg X-Patchwork-Id: 764622 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p47J9EjK007824 for ; Sat, 7 May 2011 19:09:15 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756016Ab1EGTJK (ORCPT ); Sat, 7 May 2011 15:09:10 -0400 Received: from filtteri5.pp.htv.fi ([213.243.153.188]:60562 "EHLO filtteri5.pp.htv.fi" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756003Ab1EGTJJ (ORCPT ); Sat, 7 May 2011 15:09:09 -0400 Received: from localhost (localhost [127.0.0.1]) by filtteri5.pp.htv.fi (Postfix) with ESMTP id 3616F5A625E; Sat, 7 May 2011 22:09:08 +0300 (EEST) X-Virus-Scanned: Debian amavisd-new at pp.htv.fi Received: from smtp4.welho.com ([213.243.153.38]) by localhost (filtteri5.pp.htv.fi [213.243.153.188]) (amavisd-new, port 10024) with ESMTP id yIbzfolqsvTD; Sat, 7 May 2011 22:09:07 +0300 (EEST) Received: from localhost.localdomain (cs181148025.pp.htv.fi [82.181.148.25]) by smtp4.welho.com (Postfix) with ESMTP id AC2AE5BC011; Sat, 7 May 2011 22:09:07 +0300 (EEST) From: Pekka Enberg To: kvm@vger.kernel.org Cc: Pekka Enberg , Asias He , Cyrill Gorcunov , Ingo Molnar , Prasad Joshi , Sasha Levin Subject: [PATCH 2/2] kvm tools: Warn if guest RAM size exceeds host RAM size Date: Sat, 7 May 2011 22:09:05 +0300 Message-Id: <1304795345-7092-2-git-send-email-penberg@kernel.org> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1304795345-7092-1-git-send-email-penberg@kernel.org> References: <1304795345-7092-1-git-send-email-penberg@kernel.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Sat, 07 May 2011 19:09:15 +0000 (UTC) Guest memory size that's larger than host physical RAM can cause swap deaths on the host so warn the user about it. Cc: Asias He Cc: Cyrill Gorcunov Cc: Ingo Molnar Cc: Prasad Joshi Cc: Sasha Levin Suggested-by: Ingo Molnar Signed-off-by: Pekka Enberg --- tools/kvm/kvm-run.c | 23 ++++++++++++++++------- 1 files changed, 16 insertions(+), 7 deletions(-) diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c index b9cdec2..764a242 100644 --- a/tools/kvm/kvm-run.c +++ b/tools/kvm/kvm-run.c @@ -213,6 +213,18 @@ static void kernel_usage_with_options(void) fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n"); } +static u64 host_ram_size(void) +{ + long page_size; + long nr_pages; + + nr_pages = sysconf(_SC_PHYS_PAGES); + + page_size = sysconf(_SC_PAGE_SIZE); + + return (nr_pages * page_size) >> MB_SHIFT; +} + /* * If user didn't specify how much memory it wants to allocate for the guest, * avoid filling the whole host RAM. @@ -222,17 +234,11 @@ static void kernel_usage_with_options(void) static u64 get_ram_size(int nr_cpus) { long available; - long page_size; - long nr_pages; long ram_size; ram_size = 64 * (nr_cpus + 3); - nr_pages = sysconf(_SC_PHYS_PAGES); - - page_size = sysconf(_SC_PAGE_SIZE); - - available = ((nr_pages * page_size) >> MB_SHIFT) * RAM_SIZE_RATIO; + available = host_ram_size() * RAM_SIZE_RATIO; if (ram_size > available) ram_size = available; @@ -364,6 +370,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) if (ram_size < MIN_RAM_SIZE_MB) die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB); + if (ram_size > host_ram_size()) + warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size, host_ram_size()); + ram_size <<= MB_SHIFT; if (!kvm_dev)