net: Reduce rcu_barrier() contentions from 'unshare(CLONE_NEWNET)'
Subsystems (4)
cover net: Reduce rcu_barrier() contentions from 'unshare(CLONE_NEWNET)'
From: SeongJae Park <[email protected]>
On a few of our systems, I found frequent 'unshare(CLONE_NEWNET)' calls make the number of active slab objects including 'sock_inode_cache' type rapidly and continuously increase. As a result, memory pressure occurs.
'cleanup_net()' and 'fqdir_work_fn()' are functions that deallocate the relevant memory objects. They are asynchronously invoked by the work queues and internally use 'rcu_barrier()' to ensure safe destructions. 'cleanup_net()' works in a batched maneer in a single thread worker, while 'fqdir_work_fn()' works for each 'fqdir_exit()' call in the 'system_wq'.
Therefore, 'fqdir_work_fn()' called frequently under the workload and made the contention for 'rcu_barrier()' high. In more detail, the global mutex, 'rcu_state.barrier_mutex' became the bottleneck.
I tried making 'fqdir_work_fn()' batched and confirmed it works. The following patch is for the change. I think this is the right solution for point fix of this issue, but someone might blame different parts.
1. User: Frequent 'unshare()' calls
From some point of view, such frequent 'unshare()' calls might seem only insane.
2. Global mutex in 'rcu_barrier()'
Because of the global mutex, 'rcu_barrier()' callers could wait long even after the callbacks started before the call finished. Therefore, similar issues could happen in another 'rcu_barrier()' usages. Maybe we can use some wait queue like mechanism to notify the waiters when the desired time came.
I personally believe applying the point fix for now and making 'rcu_barrier()' improvement in longterm make sense. If I'm missing something or you have different opinions, please feel free to let me know.
SeongJae Park (1):
net/ipv4/inet_fragment: Batch fqdir destroy works
include/net/inet_frag.h | 2 +- net/ipv4/inet_fragment.c | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/1 net/ipv4/inet_fragment: Batch fqdir destroy works
From: SeongJae Park <[email protected]>
In 'fqdir_exit()', a work for destruction of the 'fqdir' is enqueued. The work function, 'fqdir_work_fn()', calls 'rcu_barrier()'. In case of intensive 'fqdir_exit()' (e.g., frequent 'unshare(CLONE_NEWNET)' systemcalls), this increased contention could result in unacceptably high latency of 'rcu_barrier()'. This commit avoids such contention by doing the destruction in batched manner, as similar to that of 'cleanup_net()'.
--- include/net/inet_frag.h | 2 +- net/ipv4/inet_fragment.c | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-)
include/net/inet_frag.h +1 -1
@@ -20,7 +20,7 @@ struct fqdir {/* Keep atomic mem on separate cachelines in structs that include it */ atomic_long_t mem____cacheline_aligned_in_smp ; -struct work_struct destroy_work; +struct llist_node destroy_list; };/**
net/ipv4/inet_fragment.c +20 -8
@@ -145,12 +145,19 @@ static void inet_frags_free_cb(void *ptr, void *arg)inet_frag_destroy (fq); } +static LLIST_HEAD (destroy_list); +static void fqdir_work_fn (struct work_struct *work) { - struct fqdir *fqdir = container_of(work, struct fqdir, destroy_work); -struct inet_frags *f = fqdir->f; +struct llist_node *kill_list; +struct fqdir *fqdir; +struct inet_frags *f; + +/* Atomically snapshot the list of fqdirs to destroy */ + kill_list =llist_del_all (&destroy_list); -rhashtable_free_and_destroy (&fqdir->rhashtable, inet_frags_free_cb,NULL ); +llist_for_each_entry (fqdir, kill_list, destroy_list) +rhashtable_free_and_destroy (&fqdir->rhashtable, inet_frags_free_cb,NULL );/* We need to make sure all ongoing call_rcu(..., inet_frag_destroy_rcu) * have completed, since they need to dereference fqdir.
@@ -158,10 +165,13 @@ static void fqdir_work_fn(struct work_struct *work)*/ rcu_barrier (); -if (refcount_dec_and_test (&f->refcnt)) -complete (&f->completion); +llist_for_each_entry (fqdir, kill_list, destroy_list) { + f = fqdir->f; +if (refcount_dec_and_test (&f->refcnt)) +complete (&f->completion); -kfree (fqdir); +kfree (fqdir); + } }int fqdir_init (struct fqdir **fqdirp,struct inet_frags *f,struct net *net)
@@ -184,10 +194,12 @@ int fqdir_init(struct fqdir **fqdirp, struct inet_frags *f, struct net *net) }EXPORT_SYMBOL (fqdir_init); +static DECLARE_WORK(fqdir_destroy_work, fqdir_work_fn); +void fqdir_exit (struct fqdir *fqdir) { -INIT_WORK (&fqdir->destroy_work, fqdir_work_fn); -queue_work (system_wq, &fqdir->destroy_work); +if (llist_add (&fqdir->destroy_list, &destroy_list)) +queue_work (system_wq, &fqdir_destroy_work); }EXPORT_SYMBOL (fqdir_exit);
Signature · 1 line
2.17.1
Jakub Kicinski
maintainerRe: [PATCH 1/1] net/ipv4/inet_fragment: Batch fqdir destroy works
Quoted 11 lines · On Tue, 8 Dec 2020 10:45:29 +0100 SeongJae Park wrote:
From: SeongJae Park <[email protected]>
In '
fqdir_exit()', a work for destruction of the 'fqdir' is enqueued. The work function, 'fqdir_work_fn()', calls 'rcu_barrier()'. In case of intensive 'fqdir_exit()' (e.g., frequent 'unshare(CLONE_NEWNET)' systemcalls), this increased contention could result in unacceptably high latency of 'rcu_barrier()'. This commit avoids such contention by doing the destruction in batched manner, as similar to that of 'cleanup_net()'.Signed-off-by SeongJae Park <[email protected]>
Looks fine to me, but you haven't CCed Florian or Eric who where the last two people to touch this function. Please repost CCing them and fixing the nit below, thanks!
net/ipv4/inet_fragment.c static void inet_frags_free_cb(void *ptr, void *arg) · line 154
Quoted 7 lines
static void
fqdir_work_fn(struct work_struct *work){net/ipv4/inet_fragment.c +3
-2- struct fqdir *fqdir = container_of(work, struct fqdir, destroy_work); -struct inet_frags *f = fqdir->f; +struct llist_node *kill_list; +struct fqdir *fqdir; +struct inet_frags *f;
nit: reorder fqdir and f to keep reverse xmas tree variable ordering.
Re: [PATCH 1/1] net/ipv4/inet_fragment: Batch fqdir destroy works
In reply to SeongJae ParkQuoted 9 lines · SeongJae Park
From: SeongJae Park <[email protected]>
In '
fqdir_exit()', a work for destruction of the 'fqdir' is enqueued. The work function, 'fqdir_work_fn()', calls 'rcu_barrier()'. In case of intensive 'fqdir_exit()' (e.g., frequent 'unshare(CLONE_NEWNET)' systemcalls), this increased contention could result in unacceptably high latency of 'rcu_barrier()'. This commit avoids such contention by doing the destruction in batched manner, as similar to that of 'cleanup_net()'.
Any numbers to share ? I have never seen an issue.
Quoted 35 lines
Signed-off-by SeongJae Park <[email protected]>--- include/net/inet_frag.h | 2 +- net/ipv4/inet_fragment.c | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-)include/net/inet_frag.h +1
-1@@ -20,7 +20,7 @@ struct fqdir {/* Keep atomic mem on separate cachelines in structs that include it */ atomic_long_t mem____cacheline_aligned_in_smp ; -struct work_struct destroy_work; +struct llist_node destroy_list; };/** net/ipv4/inet_fragment.c +3
-2@@ -145,12 +145,19 @@ static void inet_frags_free_cb(void *ptr, void *arg)inet_frag_destroy (fq); } +static LLIST_HEAD (destroy_list); +static void fqdir_work_fn (struct work_struct *work) { - struct fqdir *fqdir = container_of(work, struct fqdir, destroy_work); -struct inet_frags *f = fqdir->f; +struct llist_node *kill_list;
net/ipv4/inet_fragment.c static void inet_frags_free_cb(void *ptr, void *arg) · line 161
Quoted 10 lines
net/ipv4/inet_fragment.c +7
-1+struct fqdir *fqdir; +struct inet_frags *f; + +/* Atomically snapshot the list of fqdirs to destroy */ + kill_list =llist_del_all (&destroy_list); -rhashtable_free_and_destroy (&fqdir->rhashtable, inet_frags_free_cb,NULL ); +llist_for_each_entry (fqdir, kill_list, destroy_list) +rhashtable_free_and_destroy (&fqdir->rhashtable, inet_frags_free_cb,NULL );
OK, it seems rhashtable_free_and_destroy() has cond_resched() so we are not going to hold this cpu for long periods.
net/ipv4/inet_fragment.c static void fqdir_work_fn(struct work_struct *work) · line 168
Quoted 9 lines
net/ipv4/inet_fragment.c +1
-2/* We need to make sure all ongoing call_rcu(..., inet_frag_destroy_rcu) * have completed, since they need to dereference fqdir. @@ -158,10 +165,13 @@ static void fqdir_work_fn(struct work_struct *work)*/ rcu_barrier (); -if (refcount_dec_and_test (&f->refcnt)) -complete (&f->completion); +llist_for_each_entry (fqdir, kill_list, destroy_list) {
Don't we need the llist_for_each_entry_safe() variant here ???
Quoted 26 lines
net/ipv4/inet_fragment.c +9
-3+ f = fqdir->f; +if (refcount_dec_and_test (&f->refcnt)) +complete (&f->completion); -kfree (fqdir); +kfree (fqdir); + } }int fqdir_init (struct fqdir **fqdirp,struct inet_frags *f,struct net *net)@@ -184,10 +194,12 @@ int fqdir_init(struct fqdir **fqdirp, struct inet_frags *f, struct net *net) }EXPORT_SYMBOL (fqdir_init); +static DECLARE_WORK(fqdir_destroy_work, fqdir_work_fn); +void fqdir_exit (struct fqdir *fqdir) { -INIT_WORK (&fqdir->destroy_work, fqdir_work_fn); -queue_work (system_wq, &fqdir->destroy_work); +if (llist_add (&fqdir->destroy_list, &destroy_list)) +queue_work (system_wq, &fqdir_destroy_work); }EXPORT_SYMBOL (fqdir_exit);
Re: [PATCH 1/1] net/ipv4/inet_fragment: Batch fqdir destroy works
In reply to Jakub KicinskiQuoted 11 lines · On Wed, 9 Dec 2020 15:16:59 -0800 Jakub Kicinski <[email protected]> wrote:
Quoted 10 lines · On Tue, 8 Dec 2020 10:45:29 +0100 SeongJae Park wrote:
From: SeongJae Park <[email protected]>
In '
fqdir_exit()', a work for destruction of the 'fqdir' is enqueued. The work function, 'fqdir_work_fn()', calls 'rcu_barrier()'. In case of intensive 'fqdir_exit()' (e.g., frequent 'unshare(CLONE_NEWNET)' systemcalls), this increased contention could result in unacceptably high latency of 'rcu_barrier()'. This commit avoids such contention by doing the destruction in batched manner, as similar to that of 'cleanup_net()'.
Quoted 5 lines
Quoted 1 line
Signed-off-by SeongJae Park <[email protected]>Looks fine to me, but you haven't CCed Florian or Eric who where the last two people to touch this function. Please repost CCing them and fixing the nit below, thanks!
Thank you for let me know that. I will send the next version so.
Quoted 10 lines
net/ipv4/inet_fragment.c static void inet_frags_free_cb(void *ptr, void *arg) · line 154
Quoted 8 lines
static void
fqdir_work_fn(struct work_struct *work){net/ipv4/inet_fragment.c +3
-2- struct fqdir *fqdir = container_of(work, struct fqdir, destroy_work); -struct inet_frags *f = fqdir->f; +struct llist_node *kill_list; +struct fqdir *fqdir; +struct inet_frags *f;nit: reorder fqdir and f to keep reverse xmas tree variable ordering.
Hehe, ok, I will. :)
Thanks,
SeongJae Park
Re: [PATCH 1/1] net/ipv4/inet_fragment: Batch fqdir destroy works
In reply to Eric DumazetQuoted 13 lines · On Thu, 10 Dec 2020 01:17:58 +0100 Eric Dumazet <[email protected]> wrote:
Quoted 9 lines · SeongJae Park
From: SeongJae Park <[email protected]>
In '
fqdir_exit()', a work for destruction of the 'fqdir' is enqueued. The work function, 'fqdir_work_fn()', calls 'rcu_barrier()'. In case of intensive 'fqdir_exit()' (e.g., frequent 'unshare(CLONE_NEWNET)' systemcalls), this increased contention could result in unacceptably high latency of 'rcu_barrier()'. This commit avoids such contention by doing the destruction in batched manner, as similar to that of 'cleanup_net()'.
Quoted 1 line
Any numbers to share ? I have never seen an issue.
On our 40 CPU cores / 70GB DRAM machine, 15GB of available memory was reduced within 2 minutes while my artificial reproducer runs. The reproducer merely repeats 'unshare(CLONE_NEWNET)' in a loop for 50,000 times. The reproducer is not only artificial but resembles the behavior of our real workloads. While the reproducer runs, 'cleanup_net()' was called only 4 times. First two calls quickly finished, but third call took about 30 seconds, and the fourth call didn't finished until the reproducer finishes. We also confirmed the third and fourth calls just waiting for 'rcu_barrier()'.
I think you've not seen this issue before because we are doing very intensive 'unshare()' calls. Also, this is not reproducible on every hardware. On my 6 CPU machine, the problem didn't reproduce.
Quoted 57 lines
Quoted 36 lines
Signed-off-by SeongJae Park <[email protected]>--- include/net/inet_frag.h | 2 +- net/ipv4/inet_fragment.c | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-)include/net/inet_frag.h +1
-1@@ -20,7 +20,7 @@ struct fqdir {/* Keep atomic mem on separate cachelines in structs that include it */ atomic_long_t mem____cacheline_aligned_in_smp ; -struct work_struct destroy_work; +struct llist_node destroy_list; };/** net/ipv4/inet_fragment.c +3
-2@@ -145,12 +145,19 @@ static void inet_frags_free_cb(void *ptr, void *arg)inet_frag_destroy (fq); } +static LLIST_HEAD (destroy_list); +static void fqdir_work_fn (struct work_struct *work) { - struct fqdir *fqdir = container_of(work, struct fqdir, destroy_work); -struct inet_frags *f = fqdir->f; +struct llist_node *kill_list;net/ipv4/inet_fragment.c static void inet_frags_free_cb(void *ptr, void *arg) · line 161
Quoted 10 lines
net/ipv4/inet_fragment.c +7
-1+struct fqdir *fqdir; +struct inet_frags *f; + +/* Atomically snapshot the list of fqdirs to destroy */ + kill_list =llist_del_all (&destroy_list); -rhashtable_free_and_destroy (&fqdir->rhashtable, inet_frags_free_cb,NULL ); +llist_for_each_entry (fqdir, kill_list, destroy_list) +rhashtable_free_and_destroy (&fqdir->rhashtable, inet_frags_free_cb,NULL );OK, it seems
rhashtable_free_and_destroy()hascond_resched()so we are not going to hold this cpu for long periods.Quoted 6 lines
net/ipv4/inet_fragment.c +0
-0/* We need to make sure all ongoing call_rcu(..., inet_frag_destroy_rcu) * have completed, since they need to dereference fqdir. @@ -158,10 +165,13 @@ static void fqdir_work_fn(struct work_struct *work)*/ rcu_barrier ();
Quoted 5 lines
net/ipv4/inet_fragment.c static void fqdir_work_fn(struct work_struct *work) · line 168
Quoted 3 lines
net/ipv4/inet_fragment.c +1
-2-if (refcount_dec_and_test (&f->refcnt)) -complete (&f->completion); +llist_for_each_entry (fqdir, kill_list, destroy_list) {Don't we need the
llist_for_each_entry_safe()variant here ???
Oh, indeed. I will do so in the next version.
Quoted 8 lines
net/ipv4/inet_fragment.c static void fqdir_work_fn(struct work_struct *work) · line 174
Quoted 8 lines
net/ipv4/inet_fragment.c +5
-1+ f = fqdir->f; +if (refcount_dec_and_test (&f->refcnt)) +complete (&f->completion); -kfree (fqdir); +kfree (fqdir); + }
Thanks,
SeongJae Park