Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp3592155imj; Tue, 12 Feb 2019 01:06:11 -0800 (PST) X-Google-Smtp-Source: AHgI3Iav6p3m/RaJCfioySVIJvYpPFPpUmqbUV23icKqsv93HTfIFQRhVPsNzjIxpyT4woMIi+sY X-Received: by 2002:aa7:83c2:: with SMTP id j2mr2886752pfn.119.1549962371386; Tue, 12 Feb 2019 01:06:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549962371; cv=none; d=google.com; s=arc-20160816; b=IkWoekHcPYRQdbOGUrroE+LhNK6vW4WCuERprayjnYfazS77mMbpTW+SUGCN6YXtpE FP1b9NbUZkFGIUc5RDQtJf/7+wEdoyTjdNLI9SIj1FIL7wZcAGgytNjH6u8NIwN93Wjq NYPZhemE+5B5FrNDTiHQK0yqi9Dh0hiIQGbLKAuvVXVMRZydXoJ64PStEctVFjXg25ci 0plSYbEedKWj4OHCcXq3O4M2S6CRoQTup0RLKV34eCrVmtOoha6BhPf3hwISTI+G81lT qthsbTCjbLcodyLDWjKzTYc//MukxC7rxJJFCc/iMf+dfCfxOi9ekL0cLrUy9vK8MHIt q/4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :dlp-reaction:dlp-version:dlp-product:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:to:from; bh=ln4HF49gezTe1DZrw9rsbJQKIIcFct8WYfCBIaLd8Xg=; b=bICs7qVDISGC8A/AkycdHMLNqBw2mLCCmiamnwdwlrlSIkhslnYV6L3ZtJOjEwi/1K 7j54Md8nUgJ/wctpFA4TNQHaILdRS7Rqz8enLD1+p841PW5jEdTQAqQvX/0G+nDC9SNA nOojNHzNjo5kvlj5vfMfftb8UV8jsxVlYYcZtReHlnh2fc7joT/Bx3EsPMZYp9O+0uzB 6IsFHe6CK8bkfhHZZuD6VGi7/JLv2EjdPP3G3FmPsYNl4XBzHluvsb11+y8PmoxrMONh uAuPM7yYBh0jSazQcWWDnb/22Us9TnyWB9DfwofoJ6qkWjN1IynYeAglzic9sRdOBxWy Hwgw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j9si11231235pgp.410.2019.02.12.01.05.54; Tue, 12 Feb 2019 01:06:11 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728679AbfBLJEC convert rfc822-to-8bit (ORCPT + 99 others); Tue, 12 Feb 2019 04:04:02 -0500 Received: from mga11.intel.com ([192.55.52.93]:36381 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728495AbfBLJEC (ORCPT ); Tue, 12 Feb 2019 04:04:02 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Feb 2019 01:04:00 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,361,1544515200"; d="scan'208";a="299075799" Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by orsmga005.jf.intel.com with ESMTP; 12 Feb 2019 01:04:01 -0800 Received: from fmsmsx112.amr.corp.intel.com (10.18.116.6) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.408.0; Tue, 12 Feb 2019 01:04:00 -0800 Received: from shsmsx103.ccr.corp.intel.com (10.239.4.69) by FMSMSX112.amr.corp.intel.com (10.18.116.6) with Microsoft SMTP Server (TLS) id 14.3.408.0; Tue, 12 Feb 2019 01:04:00 -0800 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.207]) by SHSMSX103.ccr.corp.intel.com ([169.254.4.194]) with mapi id 14.03.0415.000; Tue, 12 Feb 2019 17:03:58 +0800 From: "Wang, Wei W" To: Nitesh Narayan Lal , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "pbonzini@redhat.com" , "lcapitulino@redhat.com" , "pagupta@redhat.com" , "yang.zhang.wz@gmail.com" , "riel@surriel.com" , "david@redhat.com" , "mst@redhat.com" , "dodgen@google.com" , "konrad.wilk@oracle.com" , "dhildenb@redhat.com" , "aarcange@redhat.com" Subject: RE: [RFC][Patch v8 0/7] KVM: Guest Free Page Hinting Thread-Topic: [RFC][Patch v8 0/7] KVM: Guest Free Page Hinting Thread-Index: AQHUvMb57IQCd72gCkOm4okDUKYrcaXb13cw Date: Tue, 12 Feb 2019 09:03:57 +0000 Message-ID: <286AC319A985734F985F78AFA26841F73DF68060@shsmsx102.ccr.corp.intel.com> References: <20190204201854.2328-1-nitesh@redhat.com> In-Reply-To: <20190204201854.2328-1-nitesh@redhat.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiOWMyOTkzODItZTAzYi00NjIwLWI5ZDgtNThhOTE1MmRmMGIxIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX05UIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE3LjEwLjE4MDQuNDkiLCJUcnVzdGVkTGFiZWxIYXNoIjoiUVwvSHhTS28xTERXNWIwWVpUSHlHd21Jc2VqVTdGNGZ0cVJybXVRYUI4NnlNSFl2V0NpRnZVSjM3Mm13NkZqdSsifQ== x-ctpclassification: CTP_NT dlp-product: dlpe-windows dlp-version: 11.0.400.15 dlp-reaction: no-action x-originating-ip: [10.239.127.40] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tuesday, February 5, 2019 4:19 AM, Nitesh Narayan Lal wrote: > The following patch-set proposes an efficient mechanism for handing freed > memory between the guest and the host. It enables the guests with no page > cache to rapidly free and reclaims memory to and from the host respectively. > > Benefit: > With this patch-series, in our test-case, executed on a single system and > single NUMA node with 15GB memory, we were able to successfully launch > atleast 5 guests when page hinting was enabled and 3 without it. (Detailed > explanation of the test procedure is provided at the bottom). > > Changelog in V8: > In this patch-series, the earlier approach [1] which was used to capture and > scan the pages freed by the guest has been changed. The new approach is > briefly described below: > > The patch-set still leverages the existing arch_free_page() to add this > functionality. It maintains a per CPU array which is used to store the pages > freed by the guest. The maximum number of entries which it can hold is > defined by MAX_FGPT_ENTRIES(1000). When the array is completely filled, it > is scanned and only the pages which are available in the buddy are stored. > This process continues until the array is filled with pages which are part of > the buddy free list. After which it wakes up a kernel per-cpu-thread. > This kernel per-cpu-thread rescans the per-cpu-array for any re-allocation > and if the page is not reallocated and present in the buddy, the kernel > thread attempts to isolate it from the buddy. If it is successfully isolated, the > page is added to another per-cpu array. Once the entire scanning process is > complete, all the isolated pages are reported to the host through an existing > virtio-balloon driver. Hi Nitesh, Have you guys thought about something like below, which would be simpler: - use bitmaps to record free pages, e.g. xbitmap: https://lkml.org/lkml/2018/1/9/304. The bitmap can be indexed by the guest pfn, and it's globally accessed by all the CPUs; - arch_free_page(): set the bits of the freed pages from the bitmap (no per-CPU array with hardcoded fixed length and no per-cpu scanning thread) - arch_alloc_page(): clear the related bits from the bitmap - expose 2 APIs for the callers: -- unsigned long get_free_page_hints(unsigned long pfn_start, unsigned int nr); This API searches for the next free page chunk (@nr of pages), starting from @pfn_start. Bits of those free pages will be cleared after this function returns. -- void put_free_page_hints(unsigned long pfn_start, unsigned int nr); This API sets the @nr continuous bits starting from pfn_start. Usage example with balloon: 1) host requests to start ballooning; 2) balloon driver get_free_page_hints and report the hints to host via report_vq; 3) host calls madvise(pfn_start, DONTNEED) for each reported chunk of free pages and put back pfn_start to ack_vq; 4) balloon driver receives pfn_start and calls put_free_page_hints(pfn_start) to have the related bits from the bitmap to be set, indicating that those free pages are ready to be allocated. In above 2), get_free_page_hints clears the bits which indicates that those pages are not ready to be used by the guest yet. Why? This is because 3) will unmap the underlying physical pages from EPT. Normally, when guest re-visits those pages, EPT violations and QEMU page faults will get a new host page to set up the related EPT entry. If guest uses that page before the page gets unmapped (i.e. right before step 3), no EPT violation happens and the guest will use the same physical page that will be unmapped and given to other host threads. So we need to make sure that the guest free page is usable only after step 3 finishes. Back to arch_alloc_page(), it needs to check if the allocated pages have "1" set in the bitmap, if that's true, just clear the bits. Otherwise, it means step 2) above has happened and step 4) hasn't been reached. In this case, we can either have arch_alloc_page() busywaiting a bit till 4) is done for that page Or better to have a balloon callback which prioritize 3) and 4) to make this page usable by the guest. Using bitmaps to record free page hints don't need to take the free pages off the buddy list and return them later, which needs to go through the long allocation/free code path. Best, Wei