From patchwork Wed Jun 22 02:34:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: KarimAllah Ahmed X-Patchwork-Id: 9191755 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A329B60756 for ; Wed, 22 Jun 2016 02:36:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 953F328389 for ; Wed, 22 Jun 2016 02:36:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 89C0F28395; Wed, 22 Jun 2016 02:36:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.0 required=2.0 tests=BAYES_00,DKIM_ADSP_ALL, DKIM_SIGNED,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 00D0728389 for ; Wed, 22 Jun 2016 02:36:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752085AbcFVCf4 (ORCPT ); Tue, 21 Jun 2016 22:35:56 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.189.228]:17933 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751746AbcFVCfz (ORCPT ); Tue, 21 Jun 2016 22:35:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1466562954; x=1498098954; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=6qn8na6mB8ZlHYBfI/LxLmTsJRBqnZd+FI8L5JWwymo=; b=TaBT9TU7owWwTZk6/13JBwy5DIvaHQWBlW9rzyKBvjjKWb1+JkXxMH5g UScKfHX74ILmqRrraJYSe+ioMJYUVqj0XdtXUnU/5kzceHFzrzLmxfdwY 8E3gsttgWsOZGLSqeEa11F9vi276MnrC7FBVUFgOykOFZMDh2elkRpMyq k=; X-IronPort-AV: E=Sophos;i="5.26,508,1459814400"; d="scan'208";a="518547120" Received: from sea3-co-svc-lb6-vlan2.sea.amazon.com (HELO email-inbound-relay-7009.iad7.amazon.com) ([10.47.22.34]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 22 Jun 2016 02:35:38 +0000 Received: from uf8b156b38dc8546b270f.ant.amazon.com (iad1-ws-svc-lb91-vlan3.amazon.com [10.0.103.150]) by email-inbound-relay-7009.iad7.amazon.com (8.14.7/8.14.7) with ESMTP id u5M2ZTOi012393 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 22 Jun 2016 02:35:31 GMT Received: from uf8b156b38dc8546b270f.ant.amazon.com (localhost [127.0.0.1]) by uf8b156b38dc8546b270f.ant.amazon.com (8.14.4/8.14.4/Debian-2ubuntu2.1) with ESMTP id u5M2Yrjr031578; Wed, 22 Jun 2016 04:34:53 +0200 Received: (from karahmed@localhost) by uf8b156b38dc8546b270f.ant.amazon.com (8.14.4/8.14.4/Submit) id u5M2Yoik031575; Wed, 22 Jun 2016 04:34:50 +0200 From: KarimAllah Ahmed To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, x86@kernel.org Cc: KarimAllah Ahmed , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Borislav Petkov , Denys Vlasenko , Andrew Morton , Toshi Kani , Tony Luck Subject: [PATCH] kvm, x86: Properly check whether a pfn is an MMIO or not Date: Wed, 22 Jun 2016 04:34:26 +0200 Message-Id: <1466562866-31524-1-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.8.2 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP pfn_valid check is not sufficient because it only checks if a page has a struct page or not, if for example "mem=" was passed to the kernel some valid pages won't have a struct page. This means that if guests were assigned valid memory that lies after the mem= boundary it will be passed uncached to the guest no matter what the guest caching attributes are for this memory. Use the original e820 map to check whether a certain pfn belongs to RAM or not. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: H. Peter Anvin Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Borislav Petkov Cc: Denys Vlasenko Cc: Andrew Morton Cc: Toshi Kani Cc: Tony Luck Cc: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org Cc: x86@kernel.org Signed-off-by: KarimAllah Ahmed --- arch/x86/include/asm/e820.h | 1 + arch/x86/kernel/e820.c | 18 ++++++++++++++++++ arch/x86/kvm/mmu.c | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/e820.h b/arch/x86/include/asm/e820.h index 3ab0537..2d4f7d8 100644 --- a/arch/x86/include/asm/e820.h +++ b/arch/x86/include/asm/e820.h @@ -16,6 +16,7 @@ extern struct e820map e820_saved; extern unsigned long pci_mem_start; extern int e820_any_mapped(u64 start, u64 end, unsigned type); extern int e820_all_mapped(u64 start, u64 end, unsigned type); +extern bool e820_is_ram(u64 addr); extern void e820_add_region(u64 start, u64 size, int type); extern void e820_print_map(char *who); extern int diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index 621b501..387cdba 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -105,6 +105,24 @@ int __init e820_all_mapped(u64 start, u64 end, unsigned type) return 0; } +bool +e820_is_ram(u64 addr) +{ + int i; + + for (i = 0; i < e820_saved.nr_map; i++) { + struct e820entry *ei = &e820_saved.map[i]; + + if (ei->type != E820_RAM) + continue; + if ((addr >= ei->addr) && (addr < (ei->addr + ei->size))) + return true; + } + + return false; +} +EXPORT_SYMBOL_GPL(e820_is_ram); + /* * Add a memory region to the kernel e820 map. */ diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index 24e8001..5e07bf5 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -2507,7 +2507,7 @@ static bool kvm_is_mmio_pfn(kvm_pfn_t pfn) if (pfn_valid(pfn)) return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn)); - return true; + return !e820_is_ram(pfn << PAGE_SHIFT); } static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,