Skip to content

Commit 2555283

Browse files
thejhtorvalds
authored andcommitted
mm/rmap: Fix anon_vma->degree ambiguity leading to double-reuse
anon_vma->degree tracks the combined number of child anon_vmas and VMAs that use the anon_vma as their ->anon_vma. anon_vma_clone() then assumes that for any anon_vma attached to src->anon_vma_chain other than src->anon_vma, it is impossible for it to be a leaf node of the VMA tree, meaning that for such VMAs ->degree is elevated by 1 because of a child anon_vma, meaning that if ->degree equals 1 there are no VMAs that use the anon_vma as their ->anon_vma. This assumption is wrong because the ->degree optimization leads to leaf nodes being abandoned on anon_vma_clone() - an existing anon_vma is reused and no new parent-child relationship is created. So it is possible to reuse an anon_vma for one VMA while it is still tied to another VMA. This is an issue because is_mergeable_anon_vma() and its callers assume that if two VMAs have the same ->anon_vma, the list of anon_vmas attached to the VMAs is guaranteed to be the same. When this assumption is violated, vma_merge() can merge pages into a VMA that is not attached to the corresponding anon_vma, leading to dangling page->mapping pointers that will be dereferenced during rmap walks. Fix it by separately tracking the number of child anon_vmas and the number of VMAs using the anon_vma as their ->anon_vma. Fixes: 7a3ef20 ("mm: prevent endless growth of anon_vma hierarchy") Cc: [email protected] Acked-by: Michal Hocko <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Signed-off-by: Jann Horn <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c5e4d5e commit 2555283

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

include/linux/rmap.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ struct anon_vma {
4141
atomic_t refcount;
4242

4343
/*
44-
* Count of child anon_vmas and VMAs which points to this anon_vma.
44+
* Count of child anon_vmas. Equals to the count of all anon_vmas that
45+
* have ->parent pointing to this one, including itself.
4546
*
4647
* This counter is used for making decision about reusing anon_vma
4748
* instead of forking new one. See comments in function anon_vma_clone.
4849
*/
49-
unsigned degree;
50+
unsigned long num_children;
51+
/* Count of VMAs whose ->anon_vma pointer points to this object. */
52+
unsigned long num_active_vmas;
5053

5154
struct anon_vma *parent; /* Parent of this anon_vma */
5255

mm/rmap.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ static inline struct anon_vma *anon_vma_alloc(void)
9393
anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
9494
if (anon_vma) {
9595
atomic_set(&anon_vma->refcount, 1);
96-
anon_vma->degree = 1; /* Reference for first vma */
96+
anon_vma->num_children = 0;
97+
anon_vma->num_active_vmas = 0;
9798
anon_vma->parent = anon_vma;
9899
/*
99100
* Initialise the anon_vma root to point to itself. If called
@@ -201,6 +202,7 @@ int __anon_vma_prepare(struct vm_area_struct *vma)
201202
anon_vma = anon_vma_alloc();
202203
if (unlikely(!anon_vma))
203204
goto out_enomem_free_avc;
205+
anon_vma->num_children++; /* self-parent link for new root */
204206
allocated = anon_vma;
205207
}
206208

@@ -210,8 +212,7 @@ int __anon_vma_prepare(struct vm_area_struct *vma)
210212
if (likely(!vma->anon_vma)) {
211213
vma->anon_vma = anon_vma;
212214
anon_vma_chain_link(vma, avc, anon_vma);
213-
/* vma reference or self-parent link for new root */
214-
anon_vma->degree++;
215+
anon_vma->num_active_vmas++;
215216
allocated = NULL;
216217
avc = NULL;
217218
}
@@ -296,19 +297,19 @@ int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
296297
anon_vma_chain_link(dst, avc, anon_vma);
297298

298299
/*
299-
* Reuse existing anon_vma if its degree lower than two,
300-
* that means it has no vma and only one anon_vma child.
300+
* Reuse existing anon_vma if it has no vma and only one
301+
* anon_vma child.
301302
*
302-
* Do not choose parent anon_vma, otherwise first child
303-
* will always reuse it. Root anon_vma is never reused:
303+
* Root anon_vma is never reused:
304304
* it has self-parent reference and at least one child.
305305
*/
306306
if (!dst->anon_vma && src->anon_vma &&
307-
anon_vma != src->anon_vma && anon_vma->degree < 2)
307+
anon_vma->num_children < 2 &&
308+
anon_vma->num_active_vmas == 0)
308309
dst->anon_vma = anon_vma;
309310
}
310311
if (dst->anon_vma)
311-
dst->anon_vma->degree++;
312+
dst->anon_vma->num_active_vmas++;
312313
unlock_anon_vma_root(root);
313314
return 0;
314315

@@ -358,6 +359,7 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
358359
anon_vma = anon_vma_alloc();
359360
if (!anon_vma)
360361
goto out_error;
362+
anon_vma->num_active_vmas++;
361363
avc = anon_vma_chain_alloc(GFP_KERNEL);
362364
if (!avc)
363365
goto out_error_free_anon_vma;
@@ -378,7 +380,7 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
378380
vma->anon_vma = anon_vma;
379381
anon_vma_lock_write(anon_vma);
380382
anon_vma_chain_link(vma, avc, anon_vma);
381-
anon_vma->parent->degree++;
383+
anon_vma->parent->num_children++;
382384
anon_vma_unlock_write(anon_vma);
383385

384386
return 0;
@@ -410,15 +412,15 @@ void unlink_anon_vmas(struct vm_area_struct *vma)
410412
* to free them outside the lock.
411413
*/
412414
if (RB_EMPTY_ROOT(&anon_vma->rb_root.rb_root)) {
413-
anon_vma->parent->degree--;
415+
anon_vma->parent->num_children--;
414416
continue;
415417
}
416418

417419
list_del(&avc->same_vma);
418420
anon_vma_chain_free(avc);
419421
}
420422
if (vma->anon_vma) {
421-
vma->anon_vma->degree--;
423+
vma->anon_vma->num_active_vmas--;
422424

423425
/*
424426
* vma would still be needed after unlink, and anon_vma will be prepared
@@ -436,7 +438,8 @@ void unlink_anon_vmas(struct vm_area_struct *vma)
436438
list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
437439
struct anon_vma *anon_vma = avc->anon_vma;
438440

439-
VM_WARN_ON(anon_vma->degree);
441+
VM_WARN_ON(anon_vma->num_children);
442+
VM_WARN_ON(anon_vma->num_active_vmas);
440443
put_anon_vma(anon_vma);
441444

442445
list_del(&avc->same_vma);

0 commit comments

Comments
 (0)