Skip to content

Commit bfeb022

Browse files
lsgunthtorvalds
authored andcommitted
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. Signed-off-by: Logan Gunthorpe <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Acked-by: David Hildenbrand <[email protected]> Acked-by: Michal Hocko <[email protected]> Acked-by: Dan Williams <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Eric Badger <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jason Gunthorpe <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Will Deacon <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4e00c5a commit bfeb022

File tree

10 files changed

+36
-7
lines changed

10 files changed

+36
-7
lines changed

arch/arm64/mm/mmu.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,8 @@ int arch_add_memory(int nid, u64 start, u64 size,
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ int arch_add_memory(int nid, u64 start, u64 size,
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ int __ref arch_add_memory(int nid, u64 start, u64 size,
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ int arch_add_memory(int nid, u64 start, u64 size,
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ int arch_add_memory(int nid, u64 start, u64 size,
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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,18 @@ int arch_add_memory(int nid, u64 start, u64 size,
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ enum {
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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
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)
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ void *memremap_pages(struct dev_pagemap *pgmap, int nid)
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)
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

0 commit comments

Comments
 (0)