Skip to content

Commit eee2acb

Browse files
Peter Zijlstratorvalds
authored andcommitted
mm: avoid repeated anon_vma lock/unlock sequences in unlink_anon_vmas()
This matches the anon_vma_clone() case, and uses the same lock helper functions. Because of the need to potentially release the anon_vma's, it's a bit more complex, though. We traverse the 'vma->anon_vma_chain' in two phases: the first loop gets the anon_vma lock (with the helper function that only takes the lock once for the whole loop), and removes any entries that don't need any more processing. The second phase just traverses the remaining list entries (without holding the anon_vma lock), and does any actual freeing of the anon_vma's that is required. Signed-off-by: Peter Zijlstra <peterz@infradead.org> Tested-by: Hugh Dickins <hughd@google.com> Tested-by: Tim Chen <tim.c.chen@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bb4aa39 commit eee2acb

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

mm/rmap.c

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -324,36 +324,43 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
324324
return -ENOMEM;
325325
}
326326

327-
static void anon_vma_unlink(struct anon_vma_chain *anon_vma_chain)
328-
{
329-
struct anon_vma *anon_vma = anon_vma_chain->anon_vma;
330-
int empty;
331-
332-
/* If anon_vma_fork fails, we can get an empty anon_vma_chain. */
333-
if (!anon_vma)
334-
return;
335-
336-
anon_vma_lock(anon_vma);
337-
list_del(&anon_vma_chain->same_anon_vma);
338-
339-
/* We must garbage collect the anon_vma if it's empty */
340-
empty = list_empty(&anon_vma->head);
341-
anon_vma_unlock(anon_vma);
342-
343-
if (empty)
344-
put_anon_vma(anon_vma);
345-
}
346-
347327
void unlink_anon_vmas(struct vm_area_struct *vma)
348328
{
349329
struct anon_vma_chain *avc, *next;
330+
struct anon_vma *root = NULL;
350331

351332
/*
352333
* Unlink each anon_vma chained to the VMA. This list is ordered
353334
* from newest to oldest, ensuring the root anon_vma gets freed last.
354335
*/
355336
list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
356-
anon_vma_unlink(avc);
337+
struct anon_vma *anon_vma = avc->anon_vma;
338+
339+
root = lock_anon_vma_root(root, anon_vma);
340+
list_del(&avc->same_anon_vma);
341+
342+
/*
343+
* Leave empty anon_vmas on the list - we'll need
344+
* to free them outside the lock.
345+
*/
346+
if (list_empty(&anon_vma->head))
347+
continue;
348+
349+
list_del(&avc->same_vma);
350+
anon_vma_chain_free(avc);
351+
}
352+
unlock_anon_vma_root(root);
353+
354+
/*
355+
* Iterate the list once more, it now only contains empty and unlinked
356+
* anon_vmas, destroy them. Could not do before due to __put_anon_vma()
357+
* needing to acquire the anon_vma->root->mutex.
358+
*/
359+
list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
360+
struct anon_vma *anon_vma = avc->anon_vma;
361+
362+
put_anon_vma(anon_vma);
363+
357364
list_del(&avc->same_vma);
358365
anon_vma_chain_free(avc);
359366
}

0 commit comments

Comments
 (0)