Received: by 10.223.176.5 with SMTP id f5csp779460wra; Sat, 3 Feb 2018 10:05:31 -0800 (PST) X-Google-Smtp-Source: AH8x224K7/73OZhA++PFbfbxkjqIM9mim8a/ZZBTrKE5jUllh3mp8r0ur6O1VSpDwPZffPiOM5ne X-Received: by 10.98.17.80 with SMTP id z77mr21084601pfi.163.1517681131519; Sat, 03 Feb 2018 10:05:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517681131; cv=none; d=google.com; s=arc-20160816; b=ko7SLkvJ0CTs+E0PTAIIJI1avRN9aCGii44TUw7y9lVbxNmwLeMbhwLsu2CuDS5GUE 6D9cUyGzjMjUUlw4oOugK6k+dvQwkucLEIXetxIr4fVv0Iw9dKF0qjjOnhVaOVUrbDD1 EI/2jUSEI45amdMofLG00GvHpbEALuNVEUMPrVcJp1rlCcb4D3sWyiT/0z46TmDmlzxS RUMa46UVPPMbKzfgfYi0TeG6dsmedIEWLCXKgUaQNC7QesnDU0qYqlK2TR2iCMpmcJKx 8OHGiwf91dTYfYEzwDzhGK5cE4n0LRNm68VgNRmnGOlk3HRCx8RpNQYxUZUkvLRZlZ0e lakg== 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 :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=kBpq0XB67AqyWhARhe85KWKjhTieJFSmTweHBNApwMs=; b=W/CQZm11qCIm52KWhUATMoc5X716CtNUiDHRzcR1kf07yHVVSoTU028ZyiM3Jw1Lxm JDHN96FA4k/Wua7GObuj3Grws12xYwyB7DQYEE817iwkI1fx/otBByBeT+H7IYmz1Hwo 3coEPgCzTBkDKlSfas05pXOviy3/XQ3vC0uyS7DuoYHvdVI/J6nspvFXpgAsaABtA03P E9nOVrYYCxQ2c+JPG0bjcRzG9ulgD3MNkQYEt9GOZ94HV+1ndKNsedfncRmqudDOfLCz 8pt/fqR1GH6+MC3l8vZ4KIPGsxxt4R4GUI0n1NdOZemo/T6arGII5ZY5wxuyPtXPaH9n wVqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=cup1ecL7; 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=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 86si1237369pfp.347.2018.02.03.10.05.16; Sat, 03 Feb 2018 10:05:31 -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=@microsoft.com header.s=selector1 header.b=cup1ecL7; 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=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753302AbeBCSEP (ORCPT + 99 others); Sat, 3 Feb 2018 13:04:15 -0500 Received: from mail-dm3nam03on0113.outbound.protection.outlook.com ([104.47.41.113]:19232 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753047AbeBCSCN (ORCPT ); Sat, 3 Feb 2018 13:02:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=kBpq0XB67AqyWhARhe85KWKjhTieJFSmTweHBNApwMs=; b=cup1ecL7g3bDJiobEsg5gLWJ0c++CPyE1q+UkEWEk/YxIW8ds6zBqDmUFTN0RSUzW/1//fUE18CfdT3gcYq2YEHi5Q9vAhDCXmD18yrIPR7Z9muAUpS8awLn1mhCgduLcdwUw9sRO+52OIr8ZP541E5IJCyst3PbPcksVY9xWWE= Received: from BL0PR2101MB1027.namprd21.prod.outlook.com (52.132.20.161) by BL0PR2101MB1092.namprd21.prod.outlook.com (52.132.24.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.485.4; Sat, 3 Feb 2018 18:01:24 +0000 Received: from BL0PR2101MB1027.namprd21.prod.outlook.com ([fe80::a8da:b5d9:d710:9bf9]) by BL0PR2101MB1027.namprd21.prod.outlook.com ([fe80::a8da:b5d9:d710:9bf9%3]) with mapi id 15.20.0485.006; Sat, 3 Feb 2018 18:01:24 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Boris Ostrovsky , Sasha Levin Subject: [PATCH AUTOSEL for 4.14 045/110] xen/balloon: Mark unallocated host memory as UNUSABLE Thread-Topic: [PATCH AUTOSEL for 4.14 045/110] xen/balloon: Mark unallocated host memory as UNUSABLE Thread-Index: AQHTnRjsqZPmsZPEk0i+tod3BmZEag== Date: Sat, 3 Feb 2018 18:00:50 +0000 Message-ID: <20180203180015.29073-45-alexander.levin@microsoft.com> References: <20180203180015.29073-1-alexander.levin@microsoft.com> In-Reply-To: <20180203180015.29073-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;BL0PR2101MB1092;7:75r8DoOmfaG/P/oMqd2j53mF0UcDV6uAomWNOmmCtXDVsDt8ilr/Qu2xHcj8P8xxs4QzXVJ+98Q68mim05ajctG2iQZwzhcjHPv9+ZNsKbzUIi1cC3qojqgVB4vtE1l2lQzBgIX6kUzOuijlBiagfMTZ676eQrJMkOPlmOsG7Do5XcRwT6QuNNSt8ClO49n5jKbQKnIeldCC4xATDLHN2yYVdyJt9sAlMg2wXheslq9ehyTuq1lG3SdY/Ykx3fH3 x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: a7538541-9ae3-496d-ec4c-08d56b3023a5 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(4534165)(4627221)(201703031133081)(201702281549075)(5600026)(4604075)(3008032)(2017052603307)(7193020);SRVR:BL0PR2101MB1092; x-ms-traffictypediagnostic: BL0PR2101MB1092: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(146099531331640); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(61425038)(6040501)(2401047)(5005006)(8121501046)(3002001)(10201501046)(93006095)(93001095)(3231101)(2400082)(944501161)(6055026)(61426038)(61427038)(6041288)(20161123558120)(20161123562045)(20161123564045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:BL0PR2101MB1092;BCL:0;PCL:0;RULEID:;SRVR:BL0PR2101MB1092; x-forefront-prvs: 05724A8921 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39860400002)(39380400002)(376002)(346002)(396003)(366004)(189003)(199004)(3280700002)(14454004)(3660700001)(105586002)(3846002)(6666003)(22452003)(6116002)(99286004)(305945005)(7736002)(2950100002)(66066001)(478600001)(54906003)(110136005)(68736007)(316002)(86362001)(106356001)(186003)(26005)(59450400001)(6506007)(76176011)(6346003)(10090500001)(575784001)(102836004)(6436002)(4326008)(8936002)(2501003)(53936002)(86612001)(2900100001)(6486002)(2906002)(6512007)(25786009)(97736004)(72206003)(1076002)(107886003)(36756003)(5660300001)(5250100002)(8676002)(81156014)(81166006)(10290500003)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:BL0PR2101MB1092;H:BL0PR2101MB1027.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-message-info: 0u3w0OAEMedtqvmzFVuhQsK5Eb+IjNuJyklnk64bTWgjbugaaZ2aBppJMqpJi/+eNJCysOcesb2AI/XAvSwJLQ== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: a7538541-9ae3-496d-ec4c-08d56b3023a5 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Feb 2018 18:00:50.1440 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR2101MB1092 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Boris Ostrovsky [ Upstream commit b3cf8528bb21febb650a7ecbf080d0647be40b9f ] Commit f5775e0b6116 ("x86/xen: discard RAM regions above the maximum reservation") left host memory not assigned to dom0 as available for memory hotplug. Unfortunately this also meant that those regions could be used by others. Specifically, commit fa564ad96366 ("x86/PCI: Enable a 64bit BAR on AMD Family 15h (Models 00-1f, 30-3f, 60-7f)") may try to map those addresses as MMIO. To prevent this mark unallocated host memory as E820_TYPE_UNUSABLE (thus effectively reverting f5775e0b6116) and keep track of that region as a hostmem resource that can be used for the hotplug. Signed-off-by: Boris Ostrovsky Reviewed-by: Juergen Gross Signed-off-by: Sasha Levin --- arch/x86/xen/enlighten.c | 81 ++++++++++++++++++++++++++++++++++++++++++++= ++++ arch/x86/xen/setup.c | 6 ++-- drivers/xen/balloon.c | 65 ++++++++++++++++++++++++++++++++------ include/xen/balloon.h | 5 +++ 4 files changed, 144 insertions(+), 13 deletions(-) diff --git a/arch/x86/xen/enlighten.c b/arch/x86/xen/enlighten.c index d669e9d89001..c9081c6671f0 100644 --- a/arch/x86/xen/enlighten.c +++ b/arch/x86/xen/enlighten.c @@ -1,8 +1,12 @@ +#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG +#include +#endif #include #include =20 #include #include +#include =20 #include #include @@ -331,3 +335,80 @@ void xen_arch_unregister_cpu(int num) } EXPORT_SYMBOL(xen_arch_unregister_cpu); #endif + +#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG +void __init arch_xen_balloon_init(struct resource *hostmem_resource) +{ + struct xen_memory_map memmap; + int rc; + unsigned int i, last_guest_ram; + phys_addr_t max_addr =3D PFN_PHYS(max_pfn); + struct e820_table *xen_e820_table; + const struct e820_entry *entry; + struct resource *res; + + if (!xen_initial_domain()) + return; + + xen_e820_table =3D kmalloc(sizeof(*xen_e820_table), GFP_KERNEL); + if (!xen_e820_table) + return; + + memmap.nr_entries =3D ARRAY_SIZE(xen_e820_table->entries); + set_xen_guest_handle(memmap.buffer, xen_e820_table->entries); + rc =3D HYPERVISOR_memory_op(XENMEM_machine_memory_map, &memmap); + if (rc) { + pr_warn("%s: Can't read host e820 (%d)\n", __func__, rc); + goto out; + } + + last_guest_ram =3D 0; + for (i =3D 0; i < memmap.nr_entries; i++) { + if (xen_e820_table->entries[i].addr >=3D max_addr) + break; + if (xen_e820_table->entries[i].type =3D=3D E820_TYPE_RAM) + last_guest_ram =3D i; + } + + entry =3D &xen_e820_table->entries[last_guest_ram]; + if (max_addr >=3D entry->addr + entry->size) + goto out; /* No unallocated host RAM. */ + + hostmem_resource->start =3D max_addr; + hostmem_resource->end =3D entry->addr + entry->size; + + /* + * Mark non-RAM regions between the end of dom0 RAM and end of host RAM + * as unavailable. The rest of that region can be used for hotplug-based + * ballooning. + */ + for (; i < memmap.nr_entries; i++) { + entry =3D &xen_e820_table->entries[i]; + + if (entry->type =3D=3D E820_TYPE_RAM) + continue; + + if (entry->addr >=3D hostmem_resource->end) + break; + + res =3D kzalloc(sizeof(*res), GFP_KERNEL); + if (!res) + goto out; + + res->name =3D "Unavailable host RAM"; + res->start =3D entry->addr; + res->end =3D (entry->addr + entry->size < hostmem_resource->end) ? + entry->addr + entry->size : hostmem_resource->end; + rc =3D insert_resource(hostmem_resource, res); + if (rc) { + pr_warn("%s: Can't insert [%llx - %llx) (%d)\n", + __func__, res->start, res->end, rc); + kfree(res); + goto out; + } + } + + out: + kfree(xen_e820_table); +} +#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */ diff --git a/arch/x86/xen/setup.c b/arch/x86/xen/setup.c index c114ca767b3b..6e0d2086eacb 100644 --- a/arch/x86/xen/setup.c +++ b/arch/x86/xen/setup.c @@ -808,7 +808,6 @@ char * __init xen_memory_setup(void) addr =3D xen_e820_table.entries[0].addr; size =3D xen_e820_table.entries[0].size; while (i < xen_e820_table.nr_entries) { - bool discard =3D false; =20 chunk_size =3D size; type =3D xen_e820_table.entries[i].type; @@ -824,11 +823,10 @@ char * __init xen_memory_setup(void) xen_add_extra_mem(pfn_s, n_pfns); xen_max_p2m_pfn =3D pfn_s + n_pfns; } else - discard =3D true; + type =3D E820_TYPE_UNUSABLE; } =20 - if (!discard) - xen_align_and_add_e820_region(addr, chunk_size, type); + xen_align_and_add_e820_region(addr, chunk_size, type); =20 addr +=3D chunk_size; size -=3D chunk_size; diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c index f77e499afddd..065f0b607373 100644 --- a/drivers/xen/balloon.c +++ b/drivers/xen/balloon.c @@ -257,10 +257,25 @@ static void release_memory_resource(struct resource *= resource) kfree(resource); } =20 +/* + * Host memory not allocated to dom0. We can use this range for hotplug-ba= sed + * ballooning. + * + * It's a type-less resource. Setting IORESOURCE_MEM will make resource + * management algorithms (arch_remove_reservations()) look into guest e820= , + * which we don't want. + */ +static struct resource hostmem_resource =3D { + .name =3D "Host RAM", +}; + +void __attribute__((weak)) __init arch_xen_balloon_init(struct resource *r= es) +{} + static struct resource *additional_memory_resource(phys_addr_t size) { - struct resource *res; - int ret; + struct resource *res, *res_hostmem; + int ret =3D -ENOMEM; =20 res =3D kzalloc(sizeof(*res), GFP_KERNEL); if (!res) @@ -269,13 +284,42 @@ static struct resource *additional_memory_resource(ph= ys_addr_t size) res->name =3D "System RAM"; res->flags =3D IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; =20 - ret =3D allocate_resource(&iomem_resource, res, - size, 0, -1, - PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL); - if (ret < 0) { - pr_err("Cannot allocate new System RAM resource\n"); - kfree(res); - return NULL; + res_hostmem =3D kzalloc(sizeof(*res), GFP_KERNEL); + if (res_hostmem) { + /* Try to grab a range from hostmem */ + res_hostmem->name =3D "Host memory"; + ret =3D allocate_resource(&hostmem_resource, res_hostmem, + size, 0, -1, + PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL); + } + + if (!ret) { + /* + * Insert this resource into iomem. Because hostmem_resource + * tracks portion of guest e820 marked as UNUSABLE noone else + * should try to use it. + */ + res->start =3D res_hostmem->start; + res->end =3D res_hostmem->end; + ret =3D insert_resource(&iomem_resource, res); + if (ret < 0) { + pr_err("Can't insert iomem_resource [%llx - %llx]\n", + res->start, res->end); + release_memory_resource(res_hostmem); + res_hostmem =3D NULL; + res->start =3D res->end =3D 0; + } + } + + if (ret) { + ret =3D allocate_resource(&iomem_resource, res, + size, 0, -1, + PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL); + if (ret < 0) { + pr_err("Cannot allocate new System RAM resource\n"); + kfree(res); + return NULL; + } } =20 #ifdef CONFIG_SPARSEMEM @@ -287,6 +331,7 @@ static struct resource *additional_memory_resource(phys= _addr_t size) pr_err("New System RAM resource outside addressable RAM (%lu > %lu)\n", pfn, limit); release_memory_resource(res); + release_memory_resource(res_hostmem); return NULL; } } @@ -765,6 +810,8 @@ static int __init balloon_init(void) set_online_page_callback(&xen_online_page); register_memory_notifier(&xen_memory_nb); register_sysctl_table(xen_root); + + arch_xen_balloon_init(&hostmem_resource); #endif =20 #ifdef CONFIG_XEN_PV diff --git a/include/xen/balloon.h b/include/xen/balloon.h index 4914b93a23f2..61f410fd74e4 100644 --- a/include/xen/balloon.h +++ b/include/xen/balloon.h @@ -44,3 +44,8 @@ static inline void xen_balloon_init(void) { } #endif + +#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG +struct resource; +void arch_xen_balloon_init(struct resource *hostmem_resource); +#endif --=20 2.11.0