Skip to content

Commit 4f9669f

Browse files
kirylsfrothwell
authored andcommitted
mm/filemap.c: fix a data race in filemap_fault()
struct file_ra_state ra.mmap_miss could be accessed concurrently during page faults as noticed by KCSAN, BUG: KCSAN: data-race in filemap_fault / filemap_map_pages write to 0xffff9b1700a2c1b4 of 4 bytes by task 3292 on cpu 30: filemap_fault+0x920/0xfc0 do_sync_mmap_readahead at mm/filemap.c:2384 (inlined by) filemap_fault at mm/filemap.c:2486 __xfs_filemap_fault+0x112/0x3e0 [xfs] xfs_filemap_fault+0x74/0x90 [xfs] __do_fault+0x9e/0x220 do_fault+0x4a0/0x920 __handle_mm_fault+0xc69/0xd00 handle_mm_fault+0xfc/0x2f0 do_page_fault+0x263/0x6f9 page_fault+0x34/0x40 read to 0xffff9b1700a2c1b4 of 4 bytes by task 3313 on cpu 32: filemap_map_pages+0xc2e/0xd80 filemap_map_pages at mm/filemap.c:2625 do_fault+0x3da/0x920 __handle_mm_fault+0xc69/0xd00 handle_mm_fault+0xfc/0x2f0 do_page_fault+0x263/0x6f9 page_fault+0x34/0x40 Reported by Kernel Concurrency Sanitizer on: CPU: 32 PID: 3313 Comm: systemd-udevd Tainted: G W L 5.5.0-next-20200210+ #1 Hardware name: HPE ProLiant DL385 Gen10/ProLiant DL385 Gen10, BIOS A40 07/10/2019 ra.mmap_miss is used to contribute the readahead decisions, a data race could be undesirable. Both the read and write is only under non-exclusive mmap_sem, two concurrent writers could even underflow the counter. Fix the underflow by writing to a local variable before committing a final store to ra.mmap_miss given a small inaccuracy of the counter should be acceptable. Link: http://lkml.kernel.org/r/20200211030134.1847-1-cai@lca.pw Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name> Signed-off-by: Qian Cai <cai@lca.pw> Tested-by: Qian Cai <cai@lca.pw> Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Marco Elver <elver@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
1 parent db63258 commit 4f9669f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

mm/filemap.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,6 +2409,7 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
24092409
struct address_space *mapping = file->f_mapping;
24102410
struct file *fpin = NULL;
24112411
pgoff_t offset = vmf->pgoff;
2412+
unsigned int mmap_miss;
24122413

24132414
/* If we don't want any read-ahead, don't bother */
24142415
if (vmf->vma->vm_flags & VM_RAND_READ)
@@ -2424,14 +2425,15 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
24242425
}
24252426

24262427
/* Avoid banging the cache line if not needed */
2427-
if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
2428-
ra->mmap_miss++;
2428+
mmap_miss = READ_ONCE(ra->mmap_miss);
2429+
if (mmap_miss < MMAP_LOTSAMISS * 10)
2430+
WRITE_ONCE(ra->mmap_miss, ++mmap_miss);
24292431

24302432
/*
24312433
* Do we miss much more than hit in this file? If so,
24322434
* stop bothering with read-ahead. It will only hurt.
24332435
*/
2434-
if (ra->mmap_miss > MMAP_LOTSAMISS)
2436+
if (mmap_miss > MMAP_LOTSAMISS)
24352437
return fpin;
24362438

24372439
/*
@@ -2457,13 +2459,15 @@ static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
24572459
struct file_ra_state *ra = &file->f_ra;
24582460
struct address_space *mapping = file->f_mapping;
24592461
struct file *fpin = NULL;
2462+
unsigned int mmap_miss;
24602463
pgoff_t offset = vmf->pgoff;
24612464

24622465
/* If we don't want any read-ahead, don't bother */
24632466
if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages)
24642467
return fpin;
2465-
if (ra->mmap_miss > 0)
2466-
ra->mmap_miss--;
2468+
mmap_miss = READ_ONCE(ra->mmap_miss);
2469+
if (mmap_miss)
2470+
WRITE_ONCE(ra->mmap_miss, --mmap_miss);
24672471
if (PageReadahead(page)) {
24682472
fpin = maybe_unlock_mmap_for_io(vmf, fpin);
24692473
page_cache_async_readahead(mapping, ra, file,
@@ -2629,6 +2633,7 @@ void filemap_map_pages(struct vm_fault *vmf,
26292633
unsigned long max_idx;
26302634
XA_STATE(xas, &mapping->i_pages, start_pgoff);
26312635
struct page *page;
2636+
unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
26322637

26332638
rcu_read_lock();
26342639
xas_for_each(&xas, page, end_pgoff) {
@@ -2665,8 +2670,8 @@ void filemap_map_pages(struct vm_fault *vmf,
26652670
if (page->index >= max_idx)
26662671
goto unlock;
26672672

2668-
if (file->f_ra.mmap_miss > 0)
2669-
file->f_ra.mmap_miss--;
2673+
if (mmap_miss > 0)
2674+
mmap_miss--;
26702675

26712676
vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
26722677
if (vmf->pte)
@@ -2686,6 +2691,7 @@ void filemap_map_pages(struct vm_fault *vmf,
26862691
break;
26872692
}
26882693
rcu_read_unlock();
2694+
WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss);
26892695
}
26902696
EXPORT_SYMBOL(filemap_map_pages);
26912697

0 commit comments

Comments
 (0)