devm_memremap_pages() is currently used by the PCI P2PDMA code to create struct page mappings for IO memory. At present, these mappings are created with PAGE_KERNEL which implies setting the PAT bits to be WB. However, on x86, an mtrr register will typically override this and force the cache type to be UC-. In the case firmware doesn't set this register it is effectively WB and will typically result in a machine check exception when it's accessed. Other arches are not currently likely to function correctly seeing they don't have any MTRR registers to fall back on. To solve this, provide a way to specify the pgprot value explicitly to arch_add_memory(). Of the arches that support MEMORY_HOTPLUG: x86_64, and arm64 need a simple change to pass the pgprot_t down to their respective functions which set up the page tables. For x86_32, set the page tables explicitly using _set_memory_prot() (seeing they are already mapped). For ia64, s390 and sh, reject anything but PAGE_KERNEL settings -- this should be fine, for now, seeing these architectures don't support ZONE_DEVICE. A check in __add_pages() is also added to ensure the pgprot parameter was set for all arches.
Unified · Split
| @@ -1382,7 +1382,8 @@ int arch_add_memory(int nid, u64 start, u64 size, · 1381 unmodified lines | |||
| 1382 | flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; | 1382 | flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; |
| 1383 | 1383 | ||
| 1384 | __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start), | 1384 | __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start), |
| 1385 | size, PAGE_KERNEL, __pgd_pgtable_alloc, flags); | 1385 | size, params->pgprot, __pgd_pgtable_alloc, |
| 1386 | flags); | ||
| 1386 | 1387 | ||
| 1387 | memblock_clear_nomap(start, size); | 1388 | memblock_clear_nomap(start, size); |
| 1388 | 1389 | ||
| @@ -676,6 +676,9 @@ int arch_add_memory(int nid, u64 start, u64 size, · 675 unmodified lines | |||
| 676 | unsigned long nr_pages = size >> PAGE_SHIFT; | 676 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 677 | int ret; | 677 | int ret; |
| 678 | 678 | ||
| 679 | if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot)) | ||
| 680 | return -EINVAL; | ||
| 681 | |||
| 679 | ret = __add_pages(nid, start_pfn, nr_pages, params); | 682 | ret = __add_pages(nid, start_pfn, nr_pages, params); |
| 680 | if (ret) | 683 | if (ret) |
| 681 | printk("%s: Problem encountered in __add_pages() as ret=%d\n", | 684 | printk("%s: Problem encountered in __add_pages() as ret=%d\n", |
| @@ -132,7 +132,8 @@ int __ref arch_add_memory(int nid, u64 start, u64 size, · 131 unmodified lines | |||
| 132 | resize_hpt_for_hotplug(memblock_phys_mem_size()); | 132 | resize_hpt_for_hotplug(memblock_phys_mem_size()); |
| 133 | 133 | ||
| 134 | start = (unsigned long)__va(start); | 134 | start = (unsigned long)__va(start); |
| 135 | rc = create_section_mapping(start, start + size, nid, PAGE_KERNEL); | 135 | rc = create_section_mapping(start, start + size, nid, |
| 136 | params->pgprot); | ||
| 136 | if (rc) { | 137 | if (rc) { |
| 137 | pr_warn("Unable to create mapping for hot added memory 0x%llx..0x%llx: %d\n", | 138 | pr_warn("Unable to create mapping for hot added memory 0x%llx..0x%llx: %d\n", |
| 138 | start, start + size, rc); | 139 | start, start + size, rc); |
| @@ -277,6 +277,9 @@ int arch_add_memory(int nid, u64 start, u64 size, · 276 unmodified lines | |||
| 277 | if (WARN_ON_ONCE(params->altmap)) | 277 | if (WARN_ON_ONCE(params->altmap)) |
| 278 | return -EINVAL; | 278 | return -EINVAL; |
| 279 | 279 | ||
| 280 | if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot)) | ||
| 281 | return -EINVAL; | ||
| 282 | |||
| 280 | rc = vmem_add_mapping(start, size); | 283 | rc = vmem_add_mapping(start, size); |
| 281 | if (rc) | 284 | if (rc) |
| 282 | return rc; | 285 | return rc; |
| @@ -412,6 +412,9 @@ int arch_add_memory(int nid, u64 start, u64 size, · 411 unmodified lines | |||
| 412 | unsigned long nr_pages = size >> PAGE_SHIFT; | 412 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 413 | int ret; | 413 | int ret; |
| 414 | 414 | ||
| 415 | if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot) | ||
| 416 | return -EINVAL; | ||
| 417 | |||
| 415 | /* We only have ZONE_NORMAL, so this is easy.. */ | 418 | /* We only have ZONE_NORMAL, so this is easy.. */ |
| 416 | ret = __add_pages(nid, start_pfn, nr_pages, params); | 419 | ret = __add_pages(nid, start_pfn, nr_pages, params); |
| 417 | if (unlikely(ret)) | 420 | if (unlikely(ret)) |
| @@ -824,6 +824,18 @@ int arch_add_memory(int nid, u64 start, u64 size, · 823 unmodified lines | |||
| 824 | { | 824 | { |
| 825 | unsigned long start_pfn = start >> PAGE_SHIFT; | 825 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 826 | unsigned long nr_pages = size >> PAGE_SHIFT; | 826 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 827 | int ret; | ||
| 828 | |||
| 829 | /* | ||
| 830 | * The page tables were already mapped at boot so if the caller | ||
| 831 | * requests a different mapping type then we must change all the | ||
| 832 | * pages with __set_memory_prot(). | ||
| 833 | */ | ||
| 834 | if (params->pgprot.pgprot != PAGE_KERNEL.pgprot) { | ||
| 835 | ret = __set_memory_prot(start, nr_pages, params->pgprot); | ||
| 836 | if (ret) | ||
| 837 | return ret; | ||
| 838 | } | ||
| 827 | 839 | ||
| 828 | return __add_pages(nid, start_pfn, nr_pages, params); | 840 | return __add_pages(nid, start_pfn, nr_pages, params); |
| 829 | } | 841 | } |
| @@ -867,7 +867,7 @@ int arch_add_memory(int nid, u64 start, u64 size, · 866 unmodified lines | |||
| 867 | unsigned long start_pfn = start >> PAGE_SHIFT; | 867 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 868 | unsigned long nr_pages = size >> PAGE_SHIFT; | 868 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 869 | 869 | ||
| 870 | init_memory_mapping(start, start + size, PAGE_KERNEL); | 870 | init_memory_mapping(start, start + size, params->pgprot); |
| 871 | 871 | ||
| 872 | return add_pages(nid, start_pfn, nr_pages, params); | 872 | return add_pages(nid, start_pfn, nr_pages, params); |
| 873 | } | 873 | } |
| @@ -60,9 +60,12 @@ enum { · 59 unmodified lines | |||
| 60 | /* | 60 | /* |
| 61 | * Extended parameters for memory hotplug: | 61 | * Extended parameters for memory hotplug: |
| 62 | * altmap: alternative allocator for memmap array (optional) | 62 | * altmap: alternative allocator for memmap array (optional) |
| 63 | * pgprot: page protection flags to apply to newly created page tables | ||
| 64 | * (required) | ||
| 63 | */ | 65 | */ |
| 64 | struct mhp_params { | 66 | struct mhp_params { |
| 65 | struct vmem_altmap *altmap; | 67 | struct vmem_altmap *altmap; |
| 68 | pgprot_t pgprot; | ||
| 66 | }; | 69 | }; |
| 67 | 70 | ||
| 68 | /* | 71 | /* |
| @@ -311,6 +311,9 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, · 310 unmodified lines | |||
| 311 | int err; | 311 | int err; |
| 312 | struct vmem_altmap *altmap = params->altmap; | 312 | struct vmem_altmap *altmap = params->altmap; |
| 313 | 313 | ||
| 314 | if (WARN_ON_ONCE(!params->pgprot.pgprot)) | ||
| 315 | return -EINVAL; | ||
| 316 | |||
| 314 | err = check_hotplug_memory_addressable(pfn, nr_pages); | 317 | err = check_hotplug_memory_addressable(pfn, nr_pages); |
| 315 | if (err) | 318 | if (err) |
| 316 | return err; | 319 | return err; |
| @@ -1002,7 +1005,7 @@ static int online_memory_block(struct memory_block *mem, void *arg) · 685 unmodified lines | |||
| 1002 | */ | 1005 | */ |
| 1003 | int __ref add_memory_resource(int nid, struct resource *res) | 1006 | int __ref add_memory_resource(int nid, struct resource *res) |
| 1004 | { | 1007 | { |
| 1005 | struct mhp_params params = {}; | 1008 | struct mhp_params params = { .pgprot = PAGE_KERNEL }; |
| 1006 | u64 start, size; | 1009 | u64 start, size; |
| 1007 | bool new_node = false; | 1010 | bool new_node = false; |
| 1008 | int ret; | 1011 | int ret; |
| @@ -189,8 +189,8 @@ void *memremap_pages(struct dev_pagemap *pgmap, int nid) · 188 unmodified lines | |||
| 189 | * We do not want any optional features only our own memmap | 189 | * We do not want any optional features only our own memmap |
| 190 | */ | 190 | */ |
| 191 | .altmap = pgmap_altmap(pgmap), | 191 | .altmap = pgmap_altmap(pgmap), |
| 192 | .pgprot = PAGE_KERNEL, | ||
| 192 | }; | 193 | }; |
| 193 | pgprot_t pgprot = PAGE_KERNEL; | ||
| 194 | int error, is_ram; | 194 | int error, is_ram; |
| 195 | bool need_devmap_managed = true; | 195 | bool need_devmap_managed = true; |
| 196 | 196 | ||
| @@ -282,8 +282,8 @@ void *memremap_pages(struct dev_pagemap *pgmap, int nid) · 85 unmodified lines | |||
| 282 | if (nid < 0) | 282 | if (nid < 0) |
| 283 | nid = numa_mem_id(); | 283 | nid = numa_mem_id(); |
| 284 | 284 | ||
| 285 | error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(res->start), 0, | 285 | error = track_pfn_remap(NULL, ¶ms.pgprot, PHYS_PFN(res->start), |
| 286 | resource_size(res)); | 286 | 0, resource_size(res)); |
| 287 | if (error) | 287 | if (error) |
| 288 | goto err_pfn_remap; | 288 | goto err_pfn_remap; |
| 289 | 289 | ||