mm: prevent endless growth of anon_vma hierarchy
Subsystems (3)
PATCH v3 1/1 mm: prevent endless growth of anon_vma hierarchy
Constantly forking task causes unlimited grow of anon_vma chain. Each next child allocate new level of anon_vmas and links vmas to all previous levels because it inherits pages from them. None of anon_vmas cannot be freed because there might be pages which points to them.
This patch adds heuristic which decides to reuse existing anon_vma instead of forking new one. It counts vmas and direct descendants for each anon_vma. Anon_vma with degree lower than two will be reused at next fork.
As a result each anon_vma has either alive vma or at least two descendants, endless chains are no longer possible and count of anon_vmas is no more than two times more than count of vmas.
---
v2: update degree in anon_vma_prepare for merged anon_vma
v3: update comment and tags
--- include/linux/rmap.h | 16 ++++++++++++++++ mm/rmap.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-)
include/linux/rmap.h +16 -0
@@ -45,6 +45,22 @@ struct anon_vma {* mm_take_all_locks() (mm_all_locks_mutex). */ struct rb_root rb_root;/* Interval tree of private "related" vmas */ + +/* +* Count of child anon_vmas and VMAs which points to this anon_vma. +* +* This counter is used for making decision about reusing old anon_vma +* instead of forking new one. It allows to detect anon_vmas which have +* just one direct descendant and no vmas. Reusing such anon_vma not +* leads to significant preformance regression but prevents degradation +* of anon_vma hierarchy to endless linear chain. +* +* Root anon_vma is never reused because it is its own parent and it has +* at leat one vma or child, thus at fork it's degree is at least 2. +*/ +unsigned degree; + +struct anon_vma *parent;/* Parent of this anon_vma */ };/*
mm/rmap.c +29 -1
@@ -72,6 +72,8 @@ static inline struct anon_vma *anon_vma_alloc(void) anon_vma =kmem_cache_alloc (anon_vma_cachep,GFP_KERNEL );if (anon_vma) {atomic_set (&anon_vma->refcount,1 ); + anon_vma->degree =1 ;/* Reference for first vma */ + anon_vma->parent = anon_vma;/* * Initialise the anon_vma root to point to itself. If called * from fork, the root will be reset to the parents anon_vma.
@@ -188,6 +190,8 @@ int anon_vma_prepare(struct vm_area_struct *vma)if (likely (!vma->anon_vma)) { vma->anon_vma = anon_vma;anon_vma_chain_link (vma, avc, anon_vma); +/* vma link if merged or child link for new root */ + anon_vma->degree++; allocated =NULL ; avc =NULL ; }
@@ -256,7 +260,17 @@ int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) anon_vma = pavc->anon_vma; root =lock_anon_vma_root (root, anon_vma);anon_vma_chain_link (dst, avc, anon_vma); + +/* +* Reuse existing anon_vma if its degree lower than two, +* that means it has no vma and just one anon_vma child. +*/ +if (!dst->anon_vma && anon_vma != src->anon_vma && + anon_vma->degree <2 ) + dst->anon_vma = anon_vma; } +if (dst->anon_vma) + dst->anon_vma->degree++;unlock_anon_vma_root (root);return 0 ;
@@ -279,6 +293,9 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)if (!pvma->anon_vma)return 0 ; +/* Drop inherited anon_vma, we'll reuse old one or allocate new. */ + vma->anon_vma =NULL ; +/* * First, attach the new VMA to the parent VMA's anon_vmas, * so rmap can find non-COWed pages in child processes.
@@ -286,6 +303,10 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)if (anon_vma_clone (vma, pvma))return -ENOMEM ; +/* An old anon_vma has been reused. */ +if (vma->anon_vma) +return 0 ; +/* Then add our own anon_vma. */ anon_vma =anon_vma_alloc ();if (!anon_vma)
@@ -299,6 +320,7 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)* lock any of the anon_vmas in this anon_vma tree. */ anon_vma->root = pvma->anon_vma->root; + anon_vma->parent = pvma->anon_vma;/* * With refcounts, an anon_vma can stay around longer than the * process it belongs to. The root anon_vma needs to be pinned until
@@ -309,6 +331,7 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) vma->anon_vma = anon_vma;anon_vma_lock_write (anon_vma);anon_vma_chain_link (vma, avc, anon_vma); + anon_vma->parent->degree++;anon_vma_unlock_write (anon_vma);return 0 ;
@@ -339,12 +362,16 @@ void unlink_anon_vmas(struct vm_area_struct *vma)* Leave empty anon_vmas on the list - we'll need * to free them outside the lock. */ -if (RB_EMPTY_ROOT (&anon_vma->rb_root)) +if (RB_EMPTY_ROOT (&anon_vma->rb_root)) { + anon_vma->parent->degree--;continue ; + }list_del (&avc->same_vma);anon_vma_chain_free (avc); } +if (vma->anon_vma) + vma->anon_vma->degree--;unlock_anon_vma_root (root);/*
@@ -355,6 +382,7 @@ void unlink_anon_vmas(struct vm_area_struct *vma)list_for_each_entry_safe (avc, next, &vma->anon_vma_chain, same_vma) {struct anon_vma *anon_vma = avc->anon_vma; +BUG_ON (anon_vma->degree);put_anon_vma (anon_vma);list_del (&avc->same_vma);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 8 lines · Konstantin Khlebnikov
include/linux/rmap.h index renamed from include/linux/rmap.h +0
-0c0c2bce..b1d140c 100644 ---
a/include/linux/rmap.h+++b/include/linux/rmap.h@@ -45,6 +45,22 @@ structanon_vma{ *mm_take_all_locks()(mm_all_locks_mutex). */ structrb_rootrb_root; /* Interval tree of private "related" vmas */ + + /* + *
Count of childanon_vmasand VMAs which points to thisanon_vma. +
* + * This counter is used for making decision about reusing oldanon_vma+ * instead of forking new one. It allows to detect
Quoted 8 lines
anon_vmaswhich have + * just one direct descendant and no vmas.
Reusing suchanon_vmanot + * leads to significant preformance
regression but prevents degradation + * ofanon_vmahierarchy to
endless linear chain. + * + * Rootanon_vmais never reused
because it is its own parent and it has + * at leat one vma or
child, thus at fork it's degree is at least 2. + */ + unsigned
degree; + + structanon_vma*parent; /* Parent of thisanon_vma*/
};
Could this be put earlier in the struct, so the "unsigned degree" can be packed into the same long word as the spinlock, on 64 bit systems?
Otherwise there are two 4-byte entities in the struct, each of which get padded out to 8 bytes.
Quoted 12 lines
mm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +0
-1--- a/mm/rmap.c +++ b/mm/rmap.c @@ -72,6 +72,8 @@ static inlinestruct
anon_vma*anon_vma_alloc(void)anon_vma=kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL); if (anon_vma) {atomic_set(&anon_vma->refcount, 1); +anon_vma->degree= 1; /*
Reference for first vma */ +anon_vma->parent=anon_vma; /* *
Initialise theanon_vmaroot to point to itself. If called * from fork, the root will be reset to the parentsanon_vma. @@ -188,6 +190,8 @@ intanon_vma_prepare(struct vm_area_struct *vma)if (likely(!vma->anon_vma)) {vma->anon_vma=anon_vma;anon_vma_chain_link(vma, avc, anon_vma); + /* vma link if merged
or child link for new root */ + anon_vma->degree++; allocated =
Quoted 8 lines
NULL; avc = NULL; } @@ -256,7 +260,17 @@ int
anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)anon_vma=pavc->anon_vma; root =lock_anon_vma_root(root, anon_vma);anon_vma_chain_link(dst, avc, anon_vma); + + /* + * Reuse
existinganon_vmaif its degree lower than two, + * that means it
has no vma and just oneanon_vmachild. + */ + if
(!dst->anon_vma &&anon_vma!=src->anon_vma&& +anon_vma->degree< 2) +dst->anon_vma=anon_vma; }
Why can src->anon_vma not be reused if it still not shared with any other task?
Would it be more readable to use a "reuse_anon_vma" pointer here, and assign dst->anon_vma the value of reuse_anon_vma if we choose to reuse one?
Assigning different things to dst->anon_vma looks a little confusing.
Would it make sense to rename anon_vma->degree to anon_vma->sharing or anon_vma->shared, or even anon_vma->users, to indicate that it is a counter of how many VMAs are sharing this anon_vma?
mm/rmap.c index 19886fb..df5c44e 100644 int anon_vma_fork(struct vm_area_struct *vma, · line 296
Quoted 8 lines
mm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +1
-0+ if (dst->anon_vma) + dst->anon_vma->degree++;
unlock_anon_vma_root(root); return 0;mm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +1
-0@@ -279,6 +293,9 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) if (!pvma->anon_vma) return 0; + /* Drop inherited anon_vma, we'll reuse old one or allocate new. */ + vma->anon_vma = NULL;
Use of a temporary variable in anon_vma_clone() would avoid this.
mm/rmap.c index 19886fb..df5c44e 100644
Quoted 5 lines
mm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +0
-0@@ -286,6 +303,10 @@ int anon_vma_fork(struct vm_area_struct *vma,struct
vm_area_struct*pvma) if (anon_vma_clone(vma, pvma)) returnmm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +1
-1-ENOMEM; + /* An old anon_vma has been reused. */
s/old/existing/ ?
Quoted 2 lines
mm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +1
-0+ if (vma->anon_vma) + return 0; + /* Then add our own anon_vma.*/
anon_vma=anon_vma_alloc(); if (!anon_vma)
Overall the patch looks good.
- --
All rights reversed
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQEcBAEBAgAGBQJUdipzAAoJEM553pKExN6Db2kH/20CfKy2ntayKb03tqYnlohu OUxtCwqiow8XsfYc2cEBrCznCNPD5B0sdDdEgRWybBnRCikHNQS4vUBhNl/F13gS Hu8LM+RElhZwr69cCshUXefIx5xMKimUeAsHutpvy09onZy0DvYutdR958/Nhca/ 1OjXqtE+LbPd0aG87OQQlagk4DQls0uA2l609qBRKsfMgm2444MRPAN0RGQwlYIv SENvzVFN4ZvIdzsU8IoSw2EkhBankYDKbbTxAy+sHCHaxKzq0eKn+JgRaoZLjxU9 +43snI/fkWNN+S5KLgshUKIVO84kAmRAIfdKUjt/DYYkOj6YPp48aJnOKVFwjIY= =Bhp4
-----END PGP SIGNATURE-----
Quoted 25 lines · Rik van Riel
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Quoted 8 lines · Konstantin Khlebnikov
include/linux/rmap.h index renamed from include/linux/rmap.h +0
-0c0c2bce..b1d140c 100644 ---
a/include/linux/rmap.h+++b/include/linux/rmap.h@@ -45,6 +45,22 @@ structanon_vma{ *mm_take_all_locks()(mm_all_locks_mutex). */ structrb_rootrb_root; /* Interval tree of private "related" vmas */ + + /* + *
Count of childanon_vmasand VMAs which points to thisanon_vma. +
* + * This counter is used for making decision about reusing oldanon_vma+ * instead of forking new one. It allows to detectQuoted 8 lines
anon_vmaswhich have + * just one direct descendant and no vmas.
Reusing suchanon_vmanot + * leads to significant preformance
regression but prevents degradation + * ofanon_vmahierarchy to
endless linear chain. + * + * Rootanon_vmais never reused
because it is its own parent and it has + * at leat one vma or
child, thus at fork it's degree is at least 2. + */ + unsigned
degree; + + structanon_vma*parent; /* Parent of thisanon_vma*/
};Could this be put earlier in the struct, so the "unsigned degree" can be packed into the same long word as the spinlock, on 64 bit systems?
Quoted 2 lines
Otherwise there are two 4-byte entities in the struct, each of which get padded out to 8 bytes.
As I see, rw-semaphore is used here now.
But here is 4-byte padding after atomic counter. So we could shrink structure from 8 to 7 64-bit words.
Quoted 17 lines
Quoted 17 lines
mm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +0
-1--- a/mm/rmap.c +++ b/mm/rmap.c @@ -72,6 +72,8 @@ static inlinestruct
anon_vma*anon_vma_alloc(void)anon_vma=kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL); if (anon_vma) {atomic_set(&anon_vma->refcount, 1); +anon_vma->degree= 1; /*
Reference for first vma */ +anon_vma->parent=anon_vma; /* *
Initialise theanon_vmaroot to point to itself. If called * from fork, the root will be reset to the parentsanon_vma. @@ -188,6 +190,8 @@ intanon_vma_prepare(struct vm_area_struct *vma)if (likely(!vma->anon_vma)) {vma->anon_vma=anon_vma;anon_vma_chain_link(vma, avc, anon_vma); + /* vma link if merged
or child link for new root */ + anon_vma->degree++; allocated =
NULL; avc = NULL; } @@ -256,7 +260,17 @@ intanon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)anon_vma=pavc->anon_vma; root =lock_anon_vma_root(root, anon_vma);
Quoted 8 lines
Quoted 5 lines
anon_vma_chain_link(dst, avc, anon_vma); + + /* + * Reuse
existinganon_vmaif its degree lower than two, + * that means it
has no vma and just oneanon_vmachild. + */ + if
(!dst->anon_vma &&anon_vma!=src->anon_vma&& +anon_vma->degree< 2) +dst->anon_vma=anon_vma; }Why can
src->anon_vmanot be reused if it still not shared with any other task?
First child will see parent anon-vma with degree counter == 1. Child haven't incremented this counter yet.
It could be shared but this isn't necessary for fixing original problem. So I've decided to minimized influence.
Quoted 6 lines
Would it be more readable to use a "
reuse_anon_vma" pointer here, and assigndst->anon_vmathe value ofreuse_anon_vmaif we choose to reuse one?Assigning different things to
dst->anon_vmalooks a little confusing.
What different things? It always points to anon_vma, but sometimes it's NULL.
Quoted 4 lines
Would it make sense to rename
anon_vma->degreetoanon_vma->sharingoranon_vma->shared, or evenanon_vma->users, to indicate that it is a counter of how many VMAs are sharing thisanon_vma?
It counts two different things: vmas and directly descended anon_vmas. So I give it abstract name to prevent misunderstanding.
For example, if it's 1 that doesn't means that here is just one user:
that might be one child anon_vma and a lot of grandchilds.
Quoted 11 lines
mm/rmap.c index 19886fb..df5c44e 100644 int anon_vma_fork(struct vm_area_struct *vma, · line 296
Quoted 9 lines
+ if (
dst->anon_vma) + dst->anon_vma->degree++;unlock_anon_vma_root(root); return 0;mm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +1
-0@@ -279,6 +293,9 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) if (!pvma->anon_vma) return 0; + /* Drop inherited anon_vma, we'll reuse old one or allocate new. */ + vma->anon_vma = NULL;Use of a temporary variable in
anon_vma_clone()would avoid this.
anon_vma_clone() is used in some other places where we clone vma without creating new level: for example in vma_split. This trick allows to keep interface untouched.
Quoted 8 lines
mm/rmap.c index 19886fb..df5c44e 100644
Quoted 6 lines
mm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +0
-0@@ -286,6 +303,10 @@ int anon_vma_fork(struct vm_area_struct *vma,struct
vm_area_struct*pvma) if (anon_vma_clone(vma, pvma)) returnmm/rmap.c index 19886fb..df5c44e 100644 renamed from mm/rmap.c +1
-1-ENOMEM; + /* An old anon_vma has been reused. */s/old/existing/ ?
or maybe antecedent
Quoted 11 lines
Quoted 3 lines
+ if (
vma->anon_vma) + return 0; + /* Then add our ownanon_vma.
*/anon_vma=anon_vma_alloc(); if (!anon_vma)Overall the patch looks good.
- --
All rights reversed
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Quoted 8 lines
iQEcBAEBAgAGBQJUdipzAAoJEM553pKExN6Db2kH/20CfKy2ntayKb03tqYnlohu OUxtCwqiow8XsfYc2cEBrCznCNPD5B0sdDdEgRWybBnRCikHNQS4vUBhNl/F13gS Hu8LM+RElhZwr69cCshUXefIx5xMKimUeAsHutpvy09onZy0DvYutdR958/Nhca/ 1OjXqtE+LbPd0aG87OQQlagk4DQls0uA2l609qBRKsfMgm2444MRPAN0RGQwlYIv SENvzVFN4ZvIdzsU8IoSw2EkhBankYDKbbTxAy+sHCHaxKzq0eKn+JgRaoZLjxU9 +43snI/fkWNN+S5KLgshUKIVO84kAmRAIfdKUjt/DYYkOj6YPp48aJnOKVFwjIY= =Bhp4
-----END PGP SIGNATURE-----
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 5 lines · Konstantin Khlebnikov
Constantly forking task causes unlimited grow of
anon_vmachain. Each next child allocate new level ofanon_vmasand links vmas to all previous levels because it inherits pages from them. None ofanon_vmascannot be freed because there might be pages which points to them.
Quoted 7 lines
This patch adds heuristic which decides to reuse existing
anon_vmainstead of forking new one. It counts vmas and direct descendants for eachanon_vma.Anon_vmawith degree lower than two will be reused at next fork.As a result each
anon_vmahas either alive vma or at least two descendants, endless chains are no longer possible and count ofanon_vmasis no more than two times more than count of vmas.
While I was working on the previous fix for this bug, Andrew Morton noticed that the error return from anon_vma_clone() was being dropped and replaced with -ENOMEM (which is not itself a bug because the only error return value from anon_vma_clone() is -ENOMEM).
I did an audit of callers of anon_vma_clone() and discovered an actual bug where the error return was being lost. In __split_vma(), between Linux 3.11 and 3.12 the code was changed so the err variable is used before the call to anon_vma_clone() and the default initial value of -ENOMEM is overwritten. So a failure of anon_vma_clone() will return success since err at this point is now zero.
Below is a patch which fixes this bug and also propagates the error return value from anon_vma_clone() in all cases.
I can send this as a separate patch, but maybe it would be easier if you were to incorporate it into yours?
--- mmap.c | 10 +++++++--- rmap.c | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-)
mm/mmap.c +7 -3
@@ -776,8 +776,11 @@ again: remove_next = 1 + (end > next->* shrinking vma had, to cover any anon pages imported. */ if (exporter && exporter->anon_vma && !importer->anon_vma) { -if (anon_vma_clone (importer, exporter)) -return -ENOMEM ; +int error; + + error =anon_vma_clone (importer, exporter); +if (error) +return error; importer->anon_vma = exporter->anon_vma; } }
@@ -2469,7 +2472,8 @@ static int __split_vma(struct mm_structif (err) goto out_free_vma; -if (anon_vma_clone (new, vma)) + err =anon_vma_clone (new, vma); +if (err) goto out_free_mpol;if (new->vm_file)
mm/rmap.c +4 -2
@@ -274,6 +274,7 @@ int anon_vma_fork(struct vm_area_struct {struct anon_vma_chain *avc;struct anon_vma *anon_vma; +int error;/* Don't bother if the parent process has no anon_vma here. */ if (!pvma->anon_vma)
@@ -283,8 +284,9 @@ int anon_vma_fork(struct vm_area_struct* First, attach the new VMA to the parent VMA's anon_vmas, * so rmap can find non-COWed pages in child processes. */ -if (anon_vma_clone (vma, pvma)) -return -ENOMEM ; + error =anon_vma_clone (vma, pvma); +if (error) +return error;/* Then add our own anon_vma. */ anon_vma =anon_vma_alloc ();
Signature · 9 lines
Daniel K. Forrest Space Science and [email protected] Engineering Center (608) 890 - 0558 University of Wisconsin, Madison -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 27 lines · On Wed, 26 Nov 2014 15:05:59 -0600 Daniel Forrest <[email protected]>…
Quoted 5 lines · Konstantin Khlebnikov
Constantly forking task causes unlimited grow of
anon_vmachain. Each next child allocate new level ofanon_vmasand links vmas to all previous levels because it inherits pages from them. None ofanon_vmascannot be freed because there might be pages which points to them.Quoted 7 lines
This patch adds heuristic which decides to reuse existing
anon_vmainstead of forking new one. It counts vmas and direct descendants for eachanon_vma.Anon_vmawith degree lower than two will be reused at next fork.As a result each
anon_vmahas either alive vma or at least two descendants, endless chains are no longer possible and count ofanon_vmasis no more than two times more than count of vmas.While I was working on the previous fix for this bug, Andrew Morton noticed that the error return from
anon_vma_clone()was being dropped and replaced with-ENOMEM(which is not itself a bug because the only error return value fromanon_vma_clone()is-ENOMEM).I did an audit of callers of
anon_vma_clone()and discovered an actual bug where the error return was being lost. In__split_vma(), between Linux 3.11 and 3.12 the code was changed so the err variable is used before the call toanon_vma_clone()and the default initial value of-ENOMEMis overwritten. So a failure ofanon_vma_clone()will return success since err at this point is now zero.
Quoted 6 lines
Below is a patch which fixes this bug and also propagates the error return value from
anon_vma_clone()in all cases.I can send this as a separate patch, but maybe it would be easier if you were to incorporate it into yours?
I grabbed it. A bugfix is a bugfix.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Michal Hocko
Reviewed-byQuoted 27 lines · Daniel Forrest
Quoted 5 lines · Konstantin Khlebnikov
Constantly forking task causes unlimited grow of
anon_vmachain. Each next child allocate new level ofanon_vmasand links vmas to all previous levels because it inherits pages from them. None ofanon_vmascannot be freed because there might be pages which points to them.Quoted 7 lines
This patch adds heuristic which decides to reuse existing
anon_vmainstead of forking new one. It counts vmas and direct descendants for eachanon_vma.Anon_vmawith degree lower than two will be reused at next fork.As a result each
anon_vmahas either alive vma or at least two descendants, endless chains are no longer possible and count ofanon_vmasis no more than two times more than count of vmas.While I was working on the previous fix for this bug, Andrew Morton noticed that the error return from
anon_vma_clone()was being dropped and replaced with-ENOMEM(which is not itself a bug because the only error return value fromanon_vma_clone()is-ENOMEM).I did an audit of callers of
anon_vma_clone()and discovered an actual bug where the error return was being lost. In__split_vma(), between Linux 3.11 and 3.12 the code was changed so the err variable is used before the call toanon_vma_clone()and the default initial value of-ENOMEMis overwritten. So a failure ofanon_vma_clone()will return success since err at this point is now zero.
Quoted 5 lines
Below is a patch which fixes this bug and also propagates the error return value from
anon_vma_clone()in all cases.I can send this as a separate patch, but maybe it would be easier if you were to incorporate it into yours?
I would prefer two patches as they address two different things and also target different set of stable trees.
Quoted 1 line
Signed-off-by Daniel Forrest <[email protected]>
and mark for stable (3.12+) please.
Feel free to add
Thanks!
Quoted 67 lines
--- mmap.c | 10 +++++++--- rmap.c | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-)diff -rup
a/mm/mmap.cb/mm/mmap.cmm/mmap.c +7
-3@@ -776,8 +776,11 @@ again: remove_next = 1 + (end > next->* shrinking vma had, to cover any anon pages imported. */ if (exporter && exporter->anon_vma && !importer->anon_vma) { -if (anon_vma_clone (importer, exporter)) -return -ENOMEM ; +int error; + + error =anon_vma_clone (importer, exporter); +if (error) +return error; importer->anon_vma = exporter->anon_vma; } }@@ -2469,7 +2472,8 @@ static int __split_vma(struct mm_structif (err) goto out_free_vma; -if (anon_vma_clone (new, vma)) + err =anon_vma_clone (new, vma); +if (err) goto out_free_mpol;if (new->vm_file)diff -rup
a/mm/rmap.cb/mm/rmap.cmm/rmap.c +4
-2@@ -274,6 +274,7 @@ int anon_vma_fork(struct vm_area_struct {struct anon_vma_chain *avc;struct anon_vma *anon_vma; +int error;/* Don't bother if the parent process has no anon_vma here. */ if (!pvma->anon_vma)@@ -283,8 +284,9 @@ int anon_vma_fork(struct vm_area_struct* First, attach the new VMA to the parent VMA's anon_vmas, * so rmap can find non-COWed pages in child processes. */ -if (anon_vma_clone (vma, pvma)) -return -ENOMEM ; + error =anon_vma_clone (vma, pvma); +if (error) +return error;/* Then add our own anon_vma. */ anon_vma =anon_vma_alloc ();Signature · 10 lines
-- Daniel K. Forrest Space Science and [email protected] Engineering Center (608) 890 - 0558 University of Wisconsin, Madison -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Signature · 8 lines
Michal Hocko SUSE Labs -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
What happened to this patch? I do not see it merged for 3.19 and nor in the current mmotm tree (2014-12-15-17-05)
Quoted 166 lines · Konstantin Khlebnikov
Constantly forking task causes unlimited grow of
anon_vmachain. Each next child allocate new level ofanon_vmasand links vmas to all previous levels because it inherits pages from them. None ofanon_vmascannot be freed because there might be pages which points to them.This patch adds heuristic which decides to reuse existing
anon_vmainstead of forking new one. It counts vmas and direct descendants for eachanon_vma.Anon_vmawith degree lower than two will be reused at next fork.As a result each
anon_vmahas either alive vma or at least two descendants, endless chains are no longer possible and count ofanon_vmasis no more than two times more than count of vmas.Signed-off-by Konstantin Khlebnikov <[email protected]>Reported-by Daniel Forrest <[email protected]>Tested-by Michal Hocko <[email protected]>Reviewed-by Michal Hocko <[email protected]>Fixes 5beb49305251 ("mm: change anon_vma linking to fix multi-process server scalability issue")Cc Stable <[email protected]> (2.6.34+)---
v2: update degree in
anon_vma_preparefor mergedanon_vma
v3: update comment and tags--- include/linux/rmap.h | 16 ++++++++++++++++ mm/rmap.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-)include/linux/rmap.h +16
-0@@ -45,6 +45,22 @@ struct anon_vma {* mm_take_all_locks() (mm_all_locks_mutex). */ struct rb_root rb_root;/* Interval tree of private "related" vmas */ + +/* +* Count of child anon_vmas and VMAs which points to this anon_vma. +* +* This counter is used for making decision about reusing old anon_vma +* instead of forking new one. It allows to detect anon_vmas which have +* just one direct descendant and no vmas. Reusing such anon_vma not +* leads to significant preformance regression but prevents degradation +* of anon_vma hierarchy to endless linear chain. +* +* Root anon_vma is never reused because it is its own parent and it has +* at leat one vma or child, thus at fork it's degree is at least 2. +*/ +unsigned degree; + +struct anon_vma *parent;/* Parent of this anon_vma */ };/* mm/rmap.c +29
-2@@ -72,6 +72,8 @@ static inline struct anon_vma *anon_vma_alloc(void) anon_vma =kmem_cache_alloc (anon_vma_cachep,GFP_KERNEL );if (anon_vma) {atomic_set (&anon_vma->refcount,1 ); + anon_vma->degree =1 ;/* Reference for first vma */ + anon_vma->parent = anon_vma;/* * Initialise the anon_vma root to point to itself. If called * from fork, the root will be reset to the parents anon_vma. @@ -188,6 +190,8 @@ int anon_vma_prepare(struct vm_area_struct *vma)if (likely (!vma->anon_vma)) { vma->anon_vma = anon_vma;anon_vma_chain_link (vma, avc, anon_vma); +/* vma link if merged or child link for new root */ + anon_vma->degree++; allocated =NULL ; avc =NULL ; }@@ -256,7 +260,17 @@ int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) anon_vma = pavc->anon_vma; root =lock_anon_vma_root (root, anon_vma);anon_vma_chain_link (dst, avc, anon_vma); + +/* +* Reuse existing anon_vma if its degree lower than two, +* that means it has no vma and just one anon_vma child. +*/ +if (!dst->anon_vma && anon_vma != src->anon_vma && + anon_vma->degree <2 ) + dst->anon_vma = anon_vma; } +if (dst->anon_vma) + dst->anon_vma->degree++;unlock_anon_vma_root (root);return 0 ;@@ -279,6 +293,9 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)if (!pvma->anon_vma)return 0 ; +/* Drop inherited anon_vma, we'll reuse old one or allocate new. */ + vma->anon_vma =NULL ; +/* * First, attach the new VMA to the parent VMA's anon_vmas, * so rmap can find non-COWed pages in child processes. @@ -286,6 +303,10 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)if (anon_vma_clone (vma, pvma))return -ENOMEM ; +/* An old anon_vma has been reused. */ +if (vma->anon_vma) +return 0 ; +/* Then add our own anon_vma. */ anon_vma =anon_vma_alloc ();if (!anon_vma)@@ -299,6 +320,7 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)* lock any of the anon_vmas in this anon_vma tree. */ anon_vma->root = pvma->anon_vma->root; + anon_vma->parent = pvma->anon_vma;/* * With refcounts, an anon_vma can stay around longer than the * process it belongs to. The root anon_vma needs to be pinned until @@ -309,6 +331,7 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) vma->anon_vma = anon_vma;anon_vma_lock_write (anon_vma);anon_vma_chain_link (vma, avc, anon_vma); + anon_vma->parent->degree++;anon_vma_unlock_write (anon_vma);return 0 ;@@ -339,12 +362,16 @@ void unlink_anon_vmas(struct vm_area_struct *vma)* Leave empty anon_vmas on the list - we'll need * to free them outside the lock. */ -if (RB_EMPTY_ROOT (&anon_vma->rb_root)) +if (RB_EMPTY_ROOT (&anon_vma->rb_root)) { + anon_vma->parent->degree--;continue ; + }list_del (&avc->same_vma);anon_vma_chain_free (avc); } +if (vma->anon_vma) + vma->anon_vma->degree--;unlock_anon_vma_root (root);/* @@ -355,6 +382,7 @@ void unlink_anon_vmas(struct vm_area_struct *vma)list_for_each_entry_safe (avc, next, &vma->anon_vma_chain, same_vma) {struct anon_vma *anon_vma = avc->anon_vma; +BUG_ON (anon_vma->degree);put_anon_vma (anon_vma);list_del (&avc->same_vma); --To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Signature · 8 lines
Michal Hocko SUSE Labs -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 3 lines · On Tue, 16 Dec 2014 11:42:18 +0100 Michal Hocko <[email protected]> wrote:
What happened to this patch? I do not see it merged for 3.19 and nor in the current mmotm tree (2014-12-15-17-05)
Awaiting v4. See Konstantin's reply to Rik's review comments.
Konstantin, please ensure that the questions which Rik asked are answered (via code comments and changelogging) in the next version? Because other future readers will have the same questions.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 6 lines · Andrew Morton
Quoted 3 lines · On Tue, 16 Dec 2014 11:42:18 +0100 Michal Hocko <[email protected]> wrote:
What happened to this patch? I do not see it merged for 3.19 and nor in the current mmotm tree (2014-12-15-17-05)
Quoted 6 lines
Awaiting v4. See Konstantin's reply to Rik's review comments.
Konstantin, please ensure that the questions which Rik asked are answered (via code comments and changelogging) in the next version? Because other future readers will have the same questions.
Done.
Thanks for reminding.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>