History

mm/memory_hotplug: add pgprot_t to mhp_params

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.

Logan Gunthorpe committed 6 years ago

Commit bfeb0221 parent 4e00c5a10 files · +36 −7Browse files

Unified · Split

10 files
  1. arch/arm64/mm/mmu.c +2 −1
  2. arch/ia64/mm/init.c +3 −0
  3. arch/powerpc/mm/mem.c +2 −1
  4. arch/s390/mm/init.c +3 −0
  5. arch/sh/mm/init.c +3 −0
  6. arch/x86/mm/init_32.c +12 −0
  7. arch/x86/mm/init_64.c +1 −1
  8. include/linux/memory_hotplug.h +3 −0
  9. mm/memory_hotplug.c +4 −1
  10. mm/memremap.c +3 −3
arch/arm64/mm/mmu.c+2−1
@@ -1382,7 +1382,8 @@ int arch_add_memory(int nid, u64 start, u64 size, · 1381 unmodified lines
13821382 flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
13831383
13841384 __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);
13861387
13871388 memblock_clear_nomap(start, size);
13881389
arch/ia64/mm/init.c+3−0
@@ -676,6 +676,9 @@ int arch_add_memory(int nid, u64 start, u64 size, · 675 unmodified lines
676676 unsigned long nr_pages = size >> PAGE_SHIFT;
677677 int ret;
678678
679+ if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
680+ return -EINVAL;
681+
679682 ret = __add_pages(nid, start_pfn, nr_pages, params);
680683 if (ret)
681684 printk("%s: Problem encountered in __add_pages() as ret=%d\n",
arch/powerpc/mm/mem.c+2−1
@@ -132,7 +132,8 @@ int __ref arch_add_memory(int nid, u64 start, u64 size, · 131 unmodified lines
132132 resize_hpt_for_hotplug(memblock_phys_mem_size());
133133
134134 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);
136137 if (rc) {
137138 pr_warn("Unable to create mapping for hot added memory 0x%llx..0x%llx: %d\n",
138139 start, start + size, rc);
arch/s390/mm/init.c+3−0
@@ -277,6 +277,9 @@ int arch_add_memory(int nid, u64 start, u64 size, · 276 unmodified lines
277277 if (WARN_ON_ONCE(params->altmap))
278278 return -EINVAL;
279279
280+ if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
281+ return -EINVAL;
282+
280283 rc = vmem_add_mapping(start, size);
281284 if (rc)
282285 return rc;
arch/sh/mm/init.c+3−0
@@ -412,6 +412,9 @@ int arch_add_memory(int nid, u64 start, u64 size, · 411 unmodified lines
412412 unsigned long nr_pages = size >> PAGE_SHIFT;
413413 int ret;
414414
415+ if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot)
416+ return -EINVAL;
417+
415418 /* We only have ZONE_NORMAL, so this is easy.. */
416419 ret = __add_pages(nid, start_pfn, nr_pages, params);
417420 if (unlikely(ret))
arch/x86/mm/init_32.c+12−0
@@ -824,6 +824,18 @@ int arch_add_memory(int nid, u64 start, u64 size, · 823 unmodified lines
824824 {
825825 unsigned long start_pfn = start >> PAGE_SHIFT;
826826 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+ }
827839
828840 return __add_pages(nid, start_pfn, nr_pages, params);
829841 }
arch/x86/mm/init_64.c+1−1
@@ -867,7 +867,7 @@ int arch_add_memory(int nid, u64 start, u64 size, · 866 unmodified lines
867867 unsigned long start_pfn = start >> PAGE_SHIFT;
868868 unsigned long nr_pages = size >> PAGE_SHIFT;
869869
870− init_memory_mapping(start, start + size, PAGE_KERNEL);
870+ init_memory_mapping(start, start + size, params->pgprot);
871871
872872 return add_pages(nid, start_pfn, nr_pages, params);
873873 }
include/linux/memory_hotplug.h+3−0
@@ -60,9 +60,12 @@ enum { · 59 unmodified lines
6060 /*
6161 * Extended parameters for memory hotplug:
6262 * altmap: alternative allocator for memmap array (optional)
63+ * pgprot: page protection flags to apply to newly created page tables
64+ * (required)
6365 */
6466 struct mhp_params {
6567 struct vmem_altmap *altmap;
68+ pgprot_t pgprot;
6669 };
6770
6871 /*
mm/memory_hotplug.c+4−1
@@ -311,6 +311,9 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, · 310 unmodified lines
311311 int err;
312312 struct vmem_altmap *altmap = params->altmap;
313313
314+ if (WARN_ON_ONCE(!params->pgprot.pgprot))
315+ return -EINVAL;
316+
314317 err = check_hotplug_memory_addressable(pfn, nr_pages);
315318 if (err)
316319 return err;
@@ -1002,7 +1005,7 @@ static int online_memory_block(struct memory_block *mem, void *arg) · 685 unmodified lines
10021005 */
10031006 int __ref add_memory_resource(int nid, struct resource *res)
10041007 {
1005− struct mhp_params params = {};
1008+ struct mhp_params params = { .pgprot = PAGE_KERNEL };
10061009 u64 start, size;
10071010 bool new_node = false;
10081011 int ret;
mm/memremap.c+3−3
@@ -189,8 +189,8 @@ void *memremap_pages(struct dev_pagemap *pgmap, int nid) · 188 unmodified lines
189189 * We do not want any optional features only our own memmap
190190 */
191191 .altmap = pgmap_altmap(pgmap),
192+ .pgprot = PAGE_KERNEL,
192193 };
193− pgprot_t pgprot = PAGE_KERNEL;
194194 int error, is_ram;
195195 bool need_devmap_managed = true;
196196
@@ -282,8 +282,8 @@ void *memremap_pages(struct dev_pagemap *pgmap, int nid) · 85 unmodified lines
282282 if (nid < 0)
283283 nid = numa_mem_id();
284284
285− error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(res->start), 0,
286− resource_size(res));
285+ error = track_pfn_remap(NULL, &params.pgprot, PHYS_PFN(res->start),
286+ 0, resource_size(res));
287287 if (error)
288288 goto err_pfn_remap;
289289