Skip to content

Commit 15d36fe

Browse files
davejiangtorvalds
authored andcommitted
mm: disallow mappings that conflict for devm_memremap_pages()
When pmem namespaces created are smaller than section size, this can cause an issue during removal and gpf was observed: general protection fault: 0000 1 SMP PTI CPU: 36 PID: 3941 Comm: ndctl Tainted: G W 4.14.28-1.el7uek.x86_64 #2 task: ffff88acda150000 task.stack: ffffc900233a4000 RIP: 0010:__put_page+0x56/0x79 Call Trace: devm_memremap_pages_release+0x155/0x23a release_nodes+0x21e/0x260 devres_release_all+0x3c/0x48 device_release_driver_internal+0x15c/0x207 device_release_driver+0x12/0x14 unbind_store+0xba/0xd8 drv_attr_store+0x27/0x31 sysfs_kf_write+0x3f/0x46 kernfs_fop_write+0x10f/0x18b __vfs_write+0x3a/0x16d vfs_write+0xb2/0x1a1 SyS_write+0x55/0xb9 do_syscall_64+0x79/0x1ae entry_SYSCALL_64_after_hwframe+0x3d/0x0 Add code to check whether we have a mapping already in the same section and prevent additional mappings from being created if that is the case. Link: http://lkml.kernel.org/r/152909478401.50143.312364396244072931.stgit@djiang5-desk3.ch.intel.com Signed-off-by: Dave Jiang <dave.jiang@intel.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Robert Elliott <elliott@hpe.com> Cc: Jeff Moyer <jmoyer@redhat.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 03758db commit 15d36fe

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

kernel/memremap.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,27 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
176176
unsigned long pfn, pgoff, order;
177177
pgprot_t pgprot = PAGE_KERNEL;
178178
int error, nid, is_ram;
179+
struct dev_pagemap *conflict_pgmap;
179180

180181
align_start = res->start & ~(SECTION_SIZE - 1);
181182
align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
182183
- align_start;
184+
align_end = align_start + align_size - 1;
185+
186+
conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_start), NULL);
187+
if (conflict_pgmap) {
188+
dev_WARN(dev, "Conflicting mapping in same section\n");
189+
put_dev_pagemap(conflict_pgmap);
190+
return ERR_PTR(-ENOMEM);
191+
}
192+
193+
conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_end), NULL);
194+
if (conflict_pgmap) {
195+
dev_WARN(dev, "Conflicting mapping in same section\n");
196+
put_dev_pagemap(conflict_pgmap);
197+
return ERR_PTR(-ENOMEM);
198+
}
199+
183200
is_ram = region_intersects(align_start, align_size,
184201
IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
185202

@@ -199,7 +216,6 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
199216

200217
mutex_lock(&pgmap_lock);
201218
error = 0;
202-
align_end = align_start + align_size - 1;
203219

204220
foreach_order_pgoff(res, order, pgoff) {
205221
error = __radix_tree_insert(&pgmap_radix,

0 commit comments

Comments
 (0)