Skip to content

Commit 0207df4

Browse files
aryabinintorvalds
authored andcommitted
kernel/memremap, kasan: make ZONE_DEVICE with work with KASAN
KASAN learns about hotadded memory via the memory hotplug notifier. devm_memremap_pages() intentionally skips calling memory hotplug notifiers. So KASAN doesn't know anything about new memory added by devm_memremap_pages(). This causes a crash when KASAN tries to access non-existent shadow memory: BUG: unable to handle kernel paging request at ffffed0078000000 RIP: 0010:check_memory_region+0x82/0x1e0 Call Trace: memcpy+0x1f/0x50 pmem_do_bvec+0x163/0x720 pmem_make_request+0x305/0xac0 generic_make_request+0x54f/0xcf0 submit_bio+0x9c/0x370 submit_bh_wbc+0x4c7/0x700 block_read_full_page+0x5ef/0x870 do_read_cache_page+0x2b8/0xb30 read_dev_sector+0xbd/0x3f0 read_lba.isra.0+0x277/0x670 efi_partition+0x41a/0x18f0 check_partition+0x30d/0x5e9 rescan_partitions+0x18c/0x840 __blkdev_get+0x859/0x1060 blkdev_get+0x23f/0x810 __device_add_disk+0x9c8/0xde0 pmem_attach_disk+0x9a8/0xf50 nvdimm_bus_probe+0xf3/0x3c0 driver_probe_device+0x493/0xbd0 bus_for_each_drv+0x118/0x1b0 __device_attach+0x1cd/0x2b0 bus_probe_device+0x1ac/0x260 device_add+0x90d/0x1380 nd_async_device_register+0xe/0x50 async_run_entry_fn+0xc3/0x5d0 process_one_work+0xa0a/0x1810 worker_thread+0x87/0xe80 kthread+0x2d7/0x390 ret_from_fork+0x3a/0x50 Add kasan_add_zero_shadow()/kasan_remove_zero_shadow() - post mm_init() interface to map/unmap kasan_zero_page at requested virtual addresses. And use it to add/remove the shadow memory for hotplugged/unplugged device memory. Link: http://lkml.kernel.org/r/20180629164932.740-1-aryabinin@virtuozzo.com Fixes: 41e94a8 ("add devm_memremap_pages") Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com> Reported-by: Dave Chinner <david@fromorbit.com> Reviewed-by: Dan Williams <dan.j.williams@intel.com> Tested-by: Dan Williams <dan.j.williams@intel.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Alexander Potapenko <glider@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 50f8b92 commit 0207df4

3 files changed

Lines changed: 325 additions & 14 deletions

File tree

‎include/linux/kasan.h‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extern pmd_t kasan_zero_pmd[PTRS_PER_PMD];
2020
extern pud_t kasan_zero_pud[PTRS_PER_PUD];
2121
extern p4d_t kasan_zero_p4d[MAX_PTRS_PER_P4D];
2222

23-
void kasan_populate_zero_shadow(const void *shadow_start,
23+
int kasan_populate_zero_shadow(const void *shadow_start,
2424
const void *shadow_end);
2525

2626
static inline void *kasan_mem_to_shadow(const void *addr)
@@ -71,6 +71,9 @@ struct kasan_cache {
7171
int kasan_module_alloc(void *addr, size_t size);
7272
void kasan_free_shadow(const struct vm_struct *vm);
7373

74+
int kasan_add_zero_shadow(void *start, unsigned long size);
75+
void kasan_remove_zero_shadow(void *start, unsigned long size);
76+
7477
size_t ksize(const void *);
7578
static inline void kasan_unpoison_slab(const void *ptr) { ksize(ptr); }
7679
size_t kasan_metadata_size(struct kmem_cache *cache);
@@ -124,6 +127,14 @@ static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
124127
static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
125128
static inline void kasan_free_shadow(const struct vm_struct *vm) {}
126129

130+
static inline int kasan_add_zero_shadow(void *start, unsigned long size)
131+
{
132+
return 0;
133+
}
134+
static inline void kasan_remove_zero_shadow(void *start,
135+
unsigned long size)
136+
{}
137+
127138
static inline void kasan_unpoison_slab(const void *ptr) { }
128139
static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
129140

‎kernel/memremap.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/types.h>
66
#include <linux/pfn_t.h>
77
#include <linux/io.h>
8+
#include <linux/kasan.h>
89
#include <linux/mm.h>
910
#include <linux/memory_hotplug.h>
1011
#include <linux/swap.h>
@@ -137,6 +138,7 @@ static void devm_memremap_pages_release(void *data)
137138
mem_hotplug_begin();
138139
arch_remove_memory(align_start, align_size, pgmap->altmap_valid ?
139140
&pgmap->altmap : NULL);
141+
kasan_remove_zero_shadow(__va(align_start), align_size);
140142
mem_hotplug_done();
141143

142144
untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
@@ -239,6 +241,12 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
239241
goto err_pfn_remap;
240242

241243
mem_hotplug_begin();
244+
error = kasan_add_zero_shadow(__va(align_start), align_size);
245+
if (error) {
246+
mem_hotplug_done();
247+
goto err_kasan;
248+
}
249+
242250
error = arch_add_memory(nid, align_start, align_size, altmap, false);
243251
if (!error)
244252
move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
@@ -267,6 +275,8 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
267275
return __va(res->start);
268276

269277
err_add_memory:
278+
kasan_remove_zero_shadow(__va(align_start), align_size);
279+
err_kasan:
270280
untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
271281
err_pfn_remap:
272282
err_radix:

0 commit comments

Comments
 (0)