Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp4105781ima; Mon, 4 Feb 2019 10:17:10 -0800 (PST) X-Google-Smtp-Source: AHgI3Ibt7UCNuhY+7+y7zetesysDoaXH7i6t81gEX8siuL4duH7QlbAzdGDbGqGzJaZzIPZ73q62 X-Received: by 2002:a63:1a4b:: with SMTP id a11mr634234pgm.254.1549304230793; Mon, 04 Feb 2019 10:17:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549304230; cv=none; d=google.com; s=arc-20160816; b=LX/PeMKCk+h2HVllmFE1DwMuXP9BNXAIvsa2+arWV6nbzJR1zDDIeRMCwaL5HzZ0an GqA5wnJ0IKZSwP+WqGrvncSV+uTidIsfHbCoOSvG7upmIRZiKku1Eg6uSQClwKdjKSjG BosXPwdF0jEi1/sRL+ufMRDgrASHxxHs0FUb9RIw5iOQG6mX/xPMTEq5F4yp+0Aa3meU nnP3XFH7p2Yv8WryKKraeUwYXzmAmvhzADYG/E45EJg0XX511JUg8Xnk6M2WwlbneOOo quyzfRBw3HWuZ+od1CnwK2OvFHgl84GFjycl0LevQSE5IrlInSJbTyW+QV3rxrPbPaBs h2Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:dkim-signature; bh=YZe004olxwKUVDNhwEwrZ8AUV4iVt97unB7E5kh6nQ4=; b=R7XMjULwy5dpdG5/MmbD+OIKEmsLPgNTEwlcFKfC+3zvSpfsZww/0tBERX4nDEyqEe ctDk82G1Oo77nCSVa80X1kLUaZOUK4VJW1aXuz5944oj0Nc+0kfzp2/hO7RYervcKrfS U+KEOzDX/xNrL0YqyBvzeQJrT/NIxZHzK4q9ZrItFTYrYBx4fsAzr/4C1q42kFg0TNiS XOLZhDjoRR7FvYpnbGEobM7qYQ80MzKE28K5oY8iXb4+AyeklFa1aSyy15rNtEmd+ZKS /d9f25zuSIQWDw6YJ+62UnDh/FZ/XUsvd4GCUnIR7Bg1GJJCPlpH/+/Knk3Y6xL6eFUo y0cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=IQrmXhYJ; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p19si617235pgj.375.2019.02.04.10.16.53; Mon, 04 Feb 2019 10:17:10 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=IQrmXhYJ; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729577AbfBDSP6 (ORCPT + 99 others); Mon, 4 Feb 2019 13:15:58 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:41183 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726585AbfBDSPz (ORCPT ); Mon, 4 Feb 2019 13:15:55 -0500 Received: by mail-pf1-f195.google.com with SMTP id b7so282460pfi.8; Mon, 04 Feb 2019 10:15:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=YZe004olxwKUVDNhwEwrZ8AUV4iVt97unB7E5kh6nQ4=; b=IQrmXhYJn+hqxmnp2CKgxQtlZ/Plcxr3XKvJAlVd5LVl4ehyQEQyTW3i6g9aDkFdWB BlSQvYrHO04cqFRXHDFceoyzCyJjkEUYcdv8SMX3X8kEcAFyGnZZnQy3YTjYAAoKzZzL 37CyqRl4Xz3IG9WZGdn/ub1I0CoSEBtCocL0KPfAtM/Pq8+/TphetVo8ZCIOKRkSMe/e 6zdTFFr4eBZmo16fnkCw3kEFRC29XwHCOtqP8uxcfKFLPIb9OtClIu6+Ed1cZ6kCaIqz YrpTH1YSNpQoSXSdmT38pszxTEWgFqbZ+qx5I7iUCxMXnWy77QKrrIdOErtX/PYZ/ZRv s1xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=YZe004olxwKUVDNhwEwrZ8AUV4iVt97unB7E5kh6nQ4=; b=RZBzVY5Xtgk3uvGrp08r4Mv9ddknGM3TUPCwNMN11AdGNFa79LTOBs0o9xHTmIXbwr FpBQJWBerPzWEFFb0wVK1KxWRKY0V/rYbhdk/RRjF2e8TV6DO2BLlefcPu4z2st3BjZ4 ClYbUN/ex6oH95KUhs69ZZrgBy5dCE7kFms0vKPTKvSZzrn4ArjXl1fGlScP0mWamanl dMvOyIRKkpL1xFHhRdadyZ+8atzoNwfh91KGC36Q05qITTI7ywAaFTGFgXPEGljLj9kk W5hv09okoDRXrm1iJYrL9xJiCR/IqXJrf9uZKHTlaRhGoDLxIP5tqHA7miFV1Dl+1fxh DQEg== X-Gm-Message-State: AHQUAuZns8SO5zgv41qfomIxZ0zh/aIIkrXO8HOAsoxkeuHLO4BC4WR5 HTRtWqOtPeN58UVVwPW+/Cs= X-Received: by 2002:a63:1f64:: with SMTP id q36mr580276pgm.230.1549304153874; Mon, 04 Feb 2019 10:15:53 -0800 (PST) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id q5sm2033090pfi.165.2019.02.04.10.15.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Feb 2019 10:15:53 -0800 (PST) Subject: [RFC PATCH 3/4] kvm: Add guest side support for free memory hints From: Alexander Duyck To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: rkrcmar@redhat.com, alexander.h.duyck@linux.intel.com, x86@kernel.org, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, pbonzini@redhat.com, tglx@linutronix.de, akpm@linux-foundation.org Date: Mon, 04 Feb 2019 10:15:52 -0800 Message-ID: <20190204181552.12095.46287.stgit@localhost.localdomain> In-Reply-To: <20190204181118.12095.38300.stgit@localhost.localdomain> References: <20190204181118.12095.38300.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexander Duyck Add guest support for providing free memory hints to the KVM hypervisor for freed pages huge TLB size or larger. I am restricting the size to huge TLB order and larger because the hypercalls are too expensive to be performing one per 4K page. Using the huge TLB order became the obvious choice for the order to use as it allows us to avoid fragmentation of higher order memory on the host. I have limited the functionality so that it doesn't work when page poisoning is enabled. I did this because a write to the page after doing an MADV_DONTNEED would effectively negate the hint, so it would be wasting cycles to do so. Signed-off-by: Alexander Duyck --- arch/x86/include/asm/page.h | 13 +++++++++++++ arch/x86/kernel/kvm.c | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/arch/x86/include/asm/page.h b/arch/x86/include/asm/page.h index 7555b48803a8..4487ad7a3385 100644 --- a/arch/x86/include/asm/page.h +++ b/arch/x86/include/asm/page.h @@ -18,6 +18,19 @@ struct page; +#ifdef CONFIG_KVM_GUEST +#include +extern struct static_key_false pv_free_page_hint_enabled; + +#define HAVE_ARCH_FREE_PAGE +void __arch_free_page(struct page *page, unsigned int order); +static inline void arch_free_page(struct page *page, unsigned int order) +{ + if (static_branch_unlikely(&pv_free_page_hint_enabled)) + __arch_free_page(page, order); +} +#endif + #include extern struct range pfn_mapped[]; extern int nr_pfn_mapped; diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 5c93a65ee1e5..09c91641c36c 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -48,6 +48,7 @@ #include static int kvmapf = 1; +DEFINE_STATIC_KEY_FALSE(pv_free_page_hint_enabled); static int __init parse_no_kvmapf(char *arg) { @@ -648,6 +649,15 @@ static void __init kvm_guest_init(void) if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) apic_set_eoi_write(kvm_guest_apic_eoi_write); + /* + * The free page hinting doesn't add much value if page poisoning + * is enabled. So we only enable the feature if page poisoning is + * no present. + */ + if (!page_poisoning_enabled() && + kvm_para_has_feature(KVM_FEATURE_PV_UNUSED_PAGE_HINT)) + static_branch_enable(&pv_free_page_hint_enabled); + #ifdef CONFIG_SMP smp_ops.smp_prepare_cpus = kvm_smp_prepare_cpus; smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu; @@ -762,6 +772,19 @@ static __init int kvm_setup_pv_tlb_flush(void) } arch_initcall(kvm_setup_pv_tlb_flush); +void __arch_free_page(struct page *page, unsigned int order) +{ + /* + * Limit hints to blocks no smaller than pageblock in + * size to limit the cost for the hypercalls. + */ + if (order < KVM_PV_UNUSED_PAGE_HINT_MIN_ORDER) + return; + + kvm_hypercall2(KVM_HC_UNUSED_PAGE_HINT, page_to_phys(page), + PAGE_SIZE << order); +} + #ifdef CONFIG_PARAVIRT_SPINLOCKS /* Kick a cpu by its apicid. Used to wake up a halted vcpu */