Skip to content

Commit e8c716b

Browse files
Hugh Dickinstorvalds
authored andcommitted
mm/khugepaged: fix regression in collapse_file()
There is no xas_pause(&xas) in collapse_file()'s main loop, at the points where it does xas_unlock_irq(&xas) and then continues. That would explain why, once two weeks ago and twice yesterday, I have hit the VM_BUG_ON_PAGE(page != xas_load(&xas), page) since "mm/khugepaged: fix iteration in collapse_file" removed the xas_set(&xas, index) just before it: xas.xa_node could be left pointing to a stale node, if there was concurrent activity on the file which transformed its xarray. I tried inserting xas_pause()s, but then even bootup crashed on that VM_BUG_ON_PAGE(): there appears to be a subtle "nextness" implicit in xas_pause(). xas_next() and xas_pause() are good for use in simple loops, but not in this one: xas_set() worked well until now, so use xas_set(&xas, index) explicitly at the head of the loop; and change that VM_BUG_ON_PAGE() not to need its own xas_set(), and not to interfere with the xa_state (which would probably stop the crashes from xas_pause(), but I trust that less). The user-visible effects of this bug (if VM_BUG_ONs are configured out) would be data loss and data leak - potentially - though in practice I expect it is more likely that a subsequent check (e.g. on mapping or on nr_none) would notice an inconsistency, and just abandon the collapse. Link: https://lore.kernel.org/linux-mm/[email protected]/ Fixes: c8a8f3b ("mm/khugepaged: fix iteration in collapse_file") Signed-off-by: Hugh Dickins <[email protected]> Cc: [email protected] Cc: Andrew Morton <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: David Stevens <[email protected]> Cc: Peter Xu <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 9471f1f commit e8c716b

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

mm/khugepaged.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,9 +1937,9 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
19371937
}
19381938
} while (1);
19391939

1940-
xas_set(&xas, start);
19411940
for (index = start; index < end; index++) {
1942-
page = xas_next(&xas);
1941+
xas_set(&xas, index);
1942+
page = xas_load(&xas);
19431943

19441944
VM_BUG_ON(index != xas.xa_index);
19451945
if (is_shmem) {
@@ -1954,7 +1954,6 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
19541954
result = SCAN_TRUNCATED;
19551955
goto xa_locked;
19561956
}
1957-
xas_set(&xas, index + 1);
19581957
}
19591958
if (!shmem_charge(mapping->host, 1)) {
19601959
result = SCAN_FAIL;
@@ -2090,7 +2089,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
20902089

20912090
xas_lock_irq(&xas);
20922091

2093-
VM_BUG_ON_PAGE(page != xas_load(&xas), page);
2092+
VM_BUG_ON_PAGE(page != xa_load(xas.xa, index), page);
20942093

20952094
/*
20962095
* We control three references to the page:

0 commit comments

Comments
 (0)