rcu: Reduce synchronize_rcu() delays when all wait heads are in use
PATCH 1/1 rcu: Reduce synchronize_rcu() delays when all wait heads are in use
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new synchronize_rcu() user's rcu_synchronize node's processing is deferred to future GP periods. This can result in long list of synchronize_rcu() invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.
--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)
kernel/rcu/tree.c +13 -14
@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes
@@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry. +if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail) +return start_new_poll; + wait_head = first; } -/* Inject a wait-dummy-node. */ -llist_add (wait_head, &rcu_state.srs_next); -/* * A waiting list of rcu_synchronize nodes should be empty on * this step, since a GP-kthread, rcu_gp_init() -> gp_cleanup(),
Signature · 1 line
2.34.1
Hi Neeraj,
Quoted 46 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-8@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head ();
kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1731
Quoted 10 lines
kernel/rcu/tree.c +6
-3-if (!wait_head) { -// Kick another GP to retry. +if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail)
small nit:
Does done_tail access here need smp_load_acquire() or READ_ONCE() to match the other users?
Also if you don't mind could you please rebase your patch on top of mine [1] ? I think it will otherwise trigger this warning in my patch:
WARN_ON_ONCE(!rcu);
Because I always assume there to be at least 2 wait heads at clean up time.
[1] https://lore.kernel.org/all/[email protected]/
Thanks!
- Joel
Quoted 10 lines
kernel/rcu/tree.c +2
-3+return start_new_poll; + wait_head = first; } -/* Inject a wait-dummy-node. */ -llist_add (wait_head, &rcu_state.srs_next); -/* * A waiting list of rcu_synchronize nodes should be empty on * this step, since a GP-kthread, rcu_gp_init() -> gp_cleanup(),
Hi Joel,
Quoted 48 lines · Joel Fernandes
Hi Neeraj,
Quoted 45 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-8@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll;
Quoted 16 lines
kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1731
Quoted 11 lines
kernel/rcu/tree.c +6
-3wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry. +if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail)small nit:
Doesdone_tailaccess here needsmp_load_acquire()orREAD_ONCE()to match the other users?
As srs_done_tail is only updated in RCU GP thread context, I think it is not required. Please correct me if I am wrong here.
Quoted 2 lines
Also if you don't mind could you please rebase your patch on top of mine [1] ? I think it will otherwise trigger this warning in my patch:
Sure!
Thanks
Neeraj
Quoted 22 lines
WARN_ON_ONCE(!rcu);Because I always assume there to be at least 2 wait heads at clean up time.
[1] https://lore.kernel.org/all/[email protected]/
Thanks!
- Joel
Quoted 10 lines
kernel/rcu/tree.c +2
-3+return start_new_poll; + wait_head = first; } -/* Inject a wait-dummy-node. */ -llist_add (wait_head, &rcu_state.srs_next); -/* * A waiting list of rcu_synchronize nodes should be empty on * this step, since a GP-kthread, rcu_gp_init() -> gp_cleanup(),
Quoted 68 lines · Neeraj Upadhyay
Hi Joel,
Quoted 48 lines · Joel Fernandes
Hi Neeraj,
Quoted 45 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-8@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll;Quoted 16 lines
kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1731
Quoted 11 lines
kernel/rcu/tree.c +6
-3wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry. +if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail)small nit:
Doesdone_tailaccess here needsmp_load_acquire()orREAD_ONCE()to match the other users?
Quoted 2 lines
As
srs_done_tailis only updated in RCU GP thread context, I think it is not required. Please correct me if I am wrong here.
But will KCSAN not scream that its a data race?
thanks,
- Joel
Quoted 71 lines · Joel Fernandes
Quoted 68 lines · Neeraj Upadhyay
Hi Joel,
Quoted 64 lines · Joel Fernandes
Hi Neeraj,
Quoted 46 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-8@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head ();kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1731
Quoted 10 lines
kernel/rcu/tree.c +6
-3-if (!wait_head) { -// Kick another GP to retry. +if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail)small nit:
Doesdone_tailaccess here needsmp_load_acquire()orREAD_ONCE()to match the other users?
Quoted 5 lines
Quoted 2 lines
As
srs_done_tailis only updated in RCU GP thread context, I think it is not required. Please correct me if I am wrong here.But will KCSAN not scream that its a data race?
For reads from the exclusive writer context? Interesting, let me check that...
Thanks
Neeraj
Quoted 4 lines
thanks,
- Joel
Frederic Weisbecker
maintainerQuoted 48 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-10@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry.
kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1733
Quoted 10 lines
kernel/rcu/tree.c +8
-1+if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail) +return start_new_poll; + wait_head = first;
This means you're setting a non-wait-head as srs_wait_tail, right? Doesn't it trigger the following warning in rcu_sr_normal_gp_cleanup():
WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail));
Also there is a risk that this non-wait-head gets later assigned as rcu_state.srs_done_tail. And then this pending sr may not be completed until the next grace period calling rcu_sr_normal_gp_cleanup()? (Because the work doesn't take care of rcu_state.srs_done_tail itself). And then the delay can be arbitrary.
And the next grace period completing this sr (that non-wait-head set as rcu_state.srs_done_tail) and thus allowing its caller to wipe it out of its stack may race with the cleanup work dereferencing it?
Thanks.
Quoted 12 lines
kernel/rcu/tree.c +0
-3} -/* Inject a wait-dummy-node. */ -llist_add (wait_head, &rcu_state.srs_next); -/* * A waiting list of rcu_synchronize nodes should be empty on * this step, since a GP-kthread, rcu_gp_init() -> gp_cleanup(), Signature · 1 line
2.34.1
Hi Frederic,
Quoted 63 lines · Frederic Weisbecker
Quoted 48 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-10@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry. kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1733
Quoted 10 lines
kernel/rcu/tree.c +8
-1+if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail) +return start_new_poll; + wait_head = first;This means you're setting a non-wait-head as
srs_wait_tail, right? Doesn't it trigger the following warning inrcu_sr_normal_gp_cleanup():
Quoted 2 lines
WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail));
Oh I missed it. Will fix it, thanks!
Quoted 6 lines
Also there is a risk that this non-wait-head gets later assigned as rcu_state.srs_done_tail. And then this pending sr may not be completed until the next grace period calling
rcu_sr_normal_gp_cleanup()? (Because the work doesn't take care of rcu_state.srs_done_tail itself). And then the delay can be arbitrary.
That is correct. Only the first node suffers from deferred GP. If there are large number of callbacks which got added after last available wait head was queued, all those callbacks (except one) can still have a GP assigned to them.
Quoted 4 lines
And the next grace period completing this sr (that non-wait-head set as rcu_state.srs_done_tail) and thus allowing its caller to wipe it out of its stack may race with the cleanup work dereferencing it?
This sr can only be completed when done tail moves to new node. Till then, it gets deferred continuously. So, we won't be entering into the situation where the sr processing is complete while done tail is pointing to it. Please correct me if I am missing something here.
Thanks
Neeraj
Quoted 16 lines
Thanks.
Quoted 12 lines
kernel/rcu/tree.c +0
-3} -/* Inject a wait-dummy-node. */ -llist_add (wait_head, &rcu_state.srs_next); -/* * A waiting list of rcu_synchronize nodes should be empty on * this step, since a GP-kthread, rcu_gp_init() -> gp_cleanup(), Signature · 1 line
2.34.1
Frederic Weisbecker
maintainerQuoted 88 lines · Neeraj Upadhyay
Hi Frederic,
Quoted 63 lines · Frederic Weisbecker
Quoted 48 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-10@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry. kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1733
Quoted 10 lines
kernel/rcu/tree.c +8
-1+if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail) +return start_new_poll; + wait_head = first;This means you're setting a non-wait-head as
srs_wait_tail, right? Doesn't it trigger the following warning inrcu_sr_normal_gp_cleanup():Quoted 2 lines
WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail));Oh I missed it. Will fix it, thanks!
Quoted 6 lines
Also there is a risk that this non-wait-head gets later assigned as rcu_state.srs_done_tail. And then this pending sr may not be completed until the next grace period calling
rcu_sr_normal_gp_cleanup()? (Because the work doesn't take care of rcu_state.srs_done_tail itself). And then the delay can be arbitrary.That is correct. Only the first node suffers from deferred GP. If there are large number of callbacks which got added after last available wait head was queued, all those callbacks (except one) can still have a GP assigned to them.
Quoted 4 lines
And the next grace period completing this sr (that non-wait-head set as rcu_state.srs_done_tail) and thus allowing its caller to wipe it out of its stack may race with the cleanup work dereferencing it?
Quoted 4 lines
This sr can only be completed when done tail moves to new node. Till then, it gets deferred continuously. So, we won't be entering into the situation where the sr processing is complete while done tail is pointing to it. Please correct me if I am missing something here.
Ok I'm confused as usual. Let's take a practical case. Is the following sequence possible?
1) wait_tail = NULL
done_tail = WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...
Initial layout
2) wait_tail = SR5 -> WH4...
done_tail = WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...
New GP
3) wait_tail = NULL
done_tail = SR5->WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...
GP completes, normal cleanup
3) wait_tail = SR6->SR5...
done_tail = SR5->WH4->SR4->WH3->SR2->WH2->SR1->WH1->SR1...
New GP
4) GP completes and SR5 is completed by rcu_sr_normal_gp_cleanup(). So the caller releases it from the stack. But before rcu_sr_normal_gp_cleanup() overwrites done_tail to SR6->WH4->SR4.... , the workqueue manages to run and concurrently dereferences SR5.
But I bet I'm missing something obvious in the middle, preventing that...
Hi Frederic,
Quoted 115 lines · Frederic Weisbecker
Quoted 88 lines · Neeraj Upadhyay
Hi Frederic,
Quoted 63 lines · Frederic Weisbecker
Quoted 48 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-10@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry. kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1733
Quoted 10 lines
kernel/rcu/tree.c +8
-1+if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail) +return start_new_poll; + wait_head = first;This means you're setting a non-wait-head as
srs_wait_tail, right? Doesn't it trigger the following warning inrcu_sr_normal_gp_cleanup():Quoted 2 lines
WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail));Oh I missed it. Will fix it, thanks!
Quoted 6 lines
Also there is a risk that this non-wait-head gets later assigned as rcu_state.srs_done_tail. And then this pending sr may not be completed until the next grace period calling
rcu_sr_normal_gp_cleanup()? (Because the work doesn't take care of rcu_state.srs_done_tail itself). And then the delay can be arbitrary.That is correct. Only the first node suffers from deferred GP. If there are large number of callbacks which got added after last available wait head was queued, all those callbacks (except one) can still have a GP assigned to them.
Quoted 4 lines
And the next grace period completing this sr (that non-wait-head set as rcu_state.srs_done_tail) and thus allowing its caller to wipe it out of its stack may race with the cleanup work dereferencing it?
Quoted 4 lines
This sr can only be completed when done tail moves to new node. Till then, it gets deferred continuously. So, we won't be entering into the situation where the sr processing is complete while done tail is pointing to it. Please correct me if I am missing something here.
Ok I'm confused as usual. Let's take a practical case. Is the following sequence possible?
1)
wait_tail= NULL
done_tail=WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...Initial layout
2)
wait_tail= SR5 -> WH4...
done_tail=WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...New GP
3)
wait_tail= NULL
done_tail=SR5->WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...GP completes, normal cleanup
3)
wait_tail=SR6->SR5...
done_tail=SR5->WH4->SR4->WH3->SR2->WH2->SR1->WH1->SR1...
Quoted 8 lines
New GP
4) GP completes and SR5 is completed by
rcu_sr_normal_gp_cleanup(). So the caller releases it from the stack. But beforercu_sr_normal_gp_cleanup()overwritesdone_tailtoSR6->WH4->SR4.... , the workqueue manages to run and concurrently dereferences SR5.But I bet I'm missing something obvious in the middle, preventing that...
Your analysis looks correct to me. Maybe, one way to fix this can be that rcu_sr_normal_gp_cleanup() stops processing nodes in its context, when it reaches done tail and done tail is not a wait head. I will think more on this, thanks!
Thanks
Neeraj
Frederic Weisbecker
maintainerQuoted 125 lines · Neeraj Upadhyay
Hi Frederic,
Quoted 122 lines · Frederic Weisbecker
Quoted 88 lines · Neeraj Upadhyay
Hi Frederic,
Quoted 63 lines · Frederic Weisbecker
Quoted 48 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-10@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry. kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1733
Quoted 10 lines
kernel/rcu/tree.c +8
-1+if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail) +return start_new_poll; + wait_head = first;This means you're setting a non-wait-head as
srs_wait_tail, right? Doesn't it trigger the following warning inrcu_sr_normal_gp_cleanup():Quoted 2 lines
WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail));Oh I missed it. Will fix it, thanks!
Quoted 6 lines
Also there is a risk that this non-wait-head gets later assigned as rcu_state.srs_done_tail. And then this pending sr may not be completed until the next grace period calling
rcu_sr_normal_gp_cleanup()? (Because the work doesn't take care of rcu_state.srs_done_tail itself). And then the delay can be arbitrary.That is correct. Only the first node suffers from deferred GP. If there are large number of callbacks which got added after last available wait head was queued, all those callbacks (except one) can still have a GP assigned to them.
Quoted 4 lines
And the next grace period completing this sr (that non-wait-head set as rcu_state.srs_done_tail) and thus allowing its caller to wipe it out of its stack may race with the cleanup work dereferencing it?
Quoted 4 lines
This sr can only be completed when done tail moves to new node. Till then, it gets deferred continuously. So, we won't be entering into the situation where the sr processing is complete while done tail is pointing to it. Please correct me if I am missing something here.
Ok I'm confused as usual. Let's take a practical case. Is the following sequence possible?
1)
wait_tail= NULL
done_tail=WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...Initial layout
2)
wait_tail= SR5 -> WH4...
done_tail=WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...New GP
3)
wait_tail= NULL
done_tail=SR5->WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...GP completes, normal cleanup
3)
wait_tail=SR6->SR5...
done_tail=SR5->WH4->SR4->WH3->SR2->WH2->SR1->WH1->SR1...New GP
4) GP completes and SR5 is completed by
rcu_sr_normal_gp_cleanup(). So the caller releases it from the stack. But beforercu_sr_normal_gp_cleanup()overwritesdone_tailtoSR6->WH4->SR4.... , the workqueue manages to run and concurrently dereferences SR5.
Quoted 6 lines
Quoted 1 line
But I bet I'm missing something obvious in the middle, preventing that...
Your analysis looks correct to me. Maybe, one way to fix this can be that
rcu_sr_normal_gp_cleanup()stops processing nodes in its context, when it reaches done tail and done tail is not a wait head. I will think more on this, thanks!
That alone is probably not enough. In the end you may end up with a real pending sr stuck as done tail without completion, until one day a new real queue comes up, preferably with a real wait head in order not to get stuck with a new sr as done tail.
Quoted 4 lines
Thanks
Neeraj
Quoted 133 lines · Frederic Weisbecker
Quoted 125 lines · Neeraj Upadhyay
Hi Frederic,
Quoted 122 lines · Frederic Weisbecker
Quoted 88 lines · Neeraj Upadhyay
Hi Frederic,
Quoted 63 lines · Frederic Weisbecker
Quoted 48 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-10@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry. kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1733
Quoted 10 lines
kernel/rcu/tree.c +8
-1+if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail) +return start_new_poll; + wait_head = first;This means you're setting a non-wait-head as
srs_wait_tail, right? Doesn't it trigger the following warning inrcu_sr_normal_gp_cleanup():Quoted 2 lines
WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail));Oh I missed it. Will fix it, thanks!
Quoted 6 lines
Also there is a risk that this non-wait-head gets later assigned as rcu_state.srs_done_tail. And then this pending sr may not be completed until the next grace period calling
rcu_sr_normal_gp_cleanup()? (Because the work doesn't take care of rcu_state.srs_done_tail itself). And then the delay can be arbitrary.That is correct. Only the first node suffers from deferred GP. If there are large number of callbacks which got added after last available wait head was queued, all those callbacks (except one) can still have a GP assigned to them.
Quoted 4 lines
And the next grace period completing this sr (that non-wait-head set as rcu_state.srs_done_tail) and thus allowing its caller to wipe it out of its stack may race with the cleanup work dereferencing it?
Quoted 4 lines
This sr can only be completed when done tail moves to new node. Till then, it gets deferred continuously. So, we won't be entering into the situation where the sr processing is complete while done tail is pointing to it. Please correct me if I am missing something here.
Ok I'm confused as usual. Let's take a practical case. Is the following sequence possible?
1)
wait_tail= NULL
done_tail=WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...Initial layout
2)
wait_tail= SR5 -> WH4...
done_tail=WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...New GP
3)
wait_tail= NULL
done_tail=SR5->WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...GP completes, normal cleanup
3)
wait_tail=SR6->SR5...
done_tail=SR5->WH4->SR4->WH3->SR2->WH2->SR1->WH1->SR1...New GP
4) GP completes and SR5 is completed by
rcu_sr_normal_gp_cleanup(). So the caller releases it from the stack. But beforercu_sr_normal_gp_cleanup()overwritesdone_tailtoSR6->WH4->SR4.... , the workqueue manages to run and concurrently dereferences SR5.Quoted 6 lines
Quoted 1 line
But I bet I'm missing something obvious in the middle, preventing that...
Your analysis looks correct to me. Maybe, one way to fix this can be that
rcu_sr_normal_gp_cleanup()stops processing nodes in its context, when it reaches done tail and done tail is not a wait head. I will think more on this, thanks!
Quoted 5 lines
That alone is probably not enough. In the end you may end up with a real pending sr stuck as done tail without completion, until one day a new real queue comes up, preferably with a real wait head in order not to get stuck with a new sr as done tail.
But after point 4 in your sequence, rcu_sr_normal_gp_cleanup() would move the done tail to SR6 and queue a new work, which will process SR5, so, we will be able to progress real pending srs?
Thanks
Neeraj
Quoted 4 lines
Quoted 4 lines
Thanks
Neeraj
Quoted 142 lines · Neeraj Upadhyay
Quoted 133 lines · Frederic Weisbecker
Quoted 125 lines · Neeraj Upadhyay
Hi Frederic,
Quoted 122 lines · Frederic Weisbecker
Quoted 88 lines · Neeraj Upadhyay
Hi Frederic,
Quoted 63 lines · Frederic Weisbecker
Quoted 48 lines · Neeraj Upadhyay
When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new
synchronize_rcu()user'srcu_synchronizenode's processing is deferred to future GP periods. This can result in long list ofsynchronize_rcu()invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes.Signed-off-by Neeraj Upadhyay <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)kernel/rcu/tree.c +5
-10@@ -1470,14 +1470,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)* for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing -* is delayed) and additional grace period is requested. -* This means, a system is slow in processing callbacks. -* -* TODO: If a slow processing is detected, a first node -* in the llist should be used as a wait-tail for this -* grace period, therefore users which should wait due -* to a slow process are handled by _this_ grace period -* and not next. +* is delayed), first node in the llist is used as wait +* tail for this grace period. This means, the first node +* has to go through additional grace periods before it is +* part of the wait callbacks. This should be ok, as +* the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1725,15 +1722,17 @@ static bool rcu_sr_normal_gp_init(void)return start_new_poll; wait_head =rcu_sr_get_wait_head (); -if (!wait_head) { -// Kick another GP to retry. kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1733
Quoted 10 lines
kernel/rcu/tree.c +8
-1+if (wait_head) { +/* Inject a wait-dummy-node. */ +llist_add (wait_head, &rcu_state.srs_next); + }else { +// Kick another GP for first node. start_new_poll = true; -return start_new_poll; +if (first == rcu_state.srs_done_tail) +return start_new_poll; + wait_head = first;This means you're setting a non-wait-head as
srs_wait_tail, right? Doesn't it trigger the following warning inrcu_sr_normal_gp_cleanup():Quoted 2 lines
WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail));Oh I missed it. Will fix it, thanks!
Quoted 6 lines
Also there is a risk that this non-wait-head gets later assigned as rcu_state.srs_done_tail. And then this pending sr may not be completed until the next grace period calling
rcu_sr_normal_gp_cleanup()? (Because the work doesn't take care of rcu_state.srs_done_tail itself). And then the delay can be arbitrary.That is correct. Only the first node suffers from deferred GP. If there are large number of callbacks which got added after last available wait head was queued, all those callbacks (except one) can still have a GP assigned to them.
Quoted 4 lines
And the next grace period completing this sr (that non-wait-head set as rcu_state.srs_done_tail) and thus allowing its caller to wipe it out of its stack may race with the cleanup work dereferencing it?
Quoted 4 lines
This sr can only be completed when done tail moves to new node. Till then, it gets deferred continuously. So, we won't be entering into the situation where the sr processing is complete while done tail is pointing to it. Please correct me if I am missing something here.
Ok I'm confused as usual. Let's take a practical case. Is the following sequence possible?
1)
wait_tail= NULL
done_tail=WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...Initial layout
2)
wait_tail= SR5 -> WH4...
done_tail=WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...New GP
3)
wait_tail= NULL
done_tail=SR5->WH4->SR4->WH3->SR3->WH2->SR2->WH1->SR1...GP completes, normal cleanup
3)
wait_tail=SR6->SR5...
done_tail=SR5->WH4->SR4->WH3->SR2->WH2->SR1->WH1->SR1...New GP
4) GP completes and SR5 is completed by
rcu_sr_normal_gp_cleanup(). So the caller releases it from the stack. But beforercu_sr_normal_gp_cleanup()overwritesdone_tailtoSR6->WH4->SR4.... , the workqueue manages to run and concurrently dereferences SR5.Quoted 6 lines
Quoted 1 line
But I bet I'm missing something obvious in the middle, preventing that...
Your analysis looks correct to me. Maybe, one way to fix this can be that
rcu_sr_normal_gp_cleanup()stops processing nodes in its context, when it reaches done tail and done tail is not a wait head. I will think more on this, thanks!Quoted 5 lines
That alone is probably not enough. In the end you may end up with a real pending sr stuck as done tail without completion, until one day a new real queue comes up, preferably with a real wait head in order not to get stuck with a new sr as done tail.
Quoted 5 lines
But after point 4 in your sequence,
rcu_sr_normal_gp_cleanup()would move the done tail to SR6 and queue a new work, which will process SR5, so, we will be able to progress real pending srs?
Missed one point. We will continue initiating new GP until first node cb is processed, which can happen when a wait head becomes available.
Thanks
Neeraj
Quoted 7 lines
Thanks
NeerajQuoted 4 lines
Quoted 4 lines
Thanks
Neeraj
Frederic Weisbecker
maintainerQuoted 2 lines · Neeraj Upadhyay
Missed one point. We will continue initiating new GP until first node cb is processed, which can happen when a wait head becomes available.
Ah yes, I missed that indeed. Ok that seem to make sense.
Frederic Weisbecker
maintainerQuoted 11 lines · Neeraj Upadhyay
Quoted 6 lines
Also there is a risk that this non-wait-head gets later assigned as rcu_state.srs_done_tail. And then this pending sr may not be completed until the next grace period calling
rcu_sr_normal_gp_cleanup()? (Because the work doesn't take care of rcu_state.srs_done_tail itself). And then the delay can be arbitrary.That is correct. Only the first node suffers from deferred GP. If there are large number of callbacks which got added after last available wait head was queued, all those callbacks (except one) can still have a GP assigned to them.
Oh and yes I missed the fact that you still reissue a GP while queueing a non wait-head as ->srs_wait_tail. That point looks good.
Thanks.