x86/sgx: Synchronize encl->srcu in sgx_encl_release().
Jarkko Sakkinen
maintainerPATCH v3 1/1 x86/sgx: Synchronize encl->srcu in sgx_encl_release().
Add synchronize_srcu_expedited() to sgx_encl_release() to catch a grace period initiated by sgx_mmu_notifier_release().
A trivial example of a failing sequence with tasks A and B:
1. A: -> sgx_release()
2. B: -> sgx_mmu_notifier_release()
3. B: -> list_del_rcu()
3. A: -> sgx_encl_release()
4. A: -> cleanup_srcu_struct()
The loop in sgx_release() observes an empty list because B has removed its entry in the middle, and calls cleanup_srcu_struct() before B has a chance to calls synchronize_srcu().
---
v3: Fine-tuned tags, and added missing change log for v2.
v2: Switch to synchronize_srcu_expedited().
arch/x86/kernel/cpu/sgx/encl.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/kernel/cpu/sgx/encl.c +6 -0
@@ -438,6 +438,12 @@ void sgx_encl_release(struct kref *ref)if (encl->backing)fput (encl->backing); +/* +* Each sgx_mmu_notifier_release() starts a grace period. Therefore, an +* additional sync is required here. +*/ +synchronize_srcu_expedited (&encl->srcu); +cleanup_srcu_struct (&encl->srcu);WARN_ON_ONCE (!list_empty (&encl->mm_list));
Signature · 1 line
2.27.0
Quoted 30 lines · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch a grace period initiated bysgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()The loop in
sgx_release()observes an empty list because B has removed its entry in the middle, and callscleanup_srcu_struct()before B has a chance to callssynchronize_srcu().Fixes 1728ab54b4be ("x86/sgx: Add a page reclaimer")Suggested-by Sean Christopherson <[email protected]>Suggested-by Haitao Huang <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v3: Fine-tuned tags, and added missing change log for v2.
v2: Switch tosynchronize_srcu_expedited().arch/x86/kernel/cpu/sgx/encl.c | 6 ++++++ 1 file changed, 6 insertions(+)arch/x86/kernel/cpu/sgx/encl.c +0
-0
arch/x86/kernel/cpu/sgx/encl.c void sgx_encl_release(struct kref *ref) · line 447
Quoted 10 lines
arch/x86/kernel/cpu/sgx/encl.c +6
-0@@ -438,6 +438,12 @@ void sgx_encl_release(struct kref *ref)if (encl->backing)fput (encl->backing); +/* +* Each sgx_mmu_notifier_release() starts a grace period. Therefore, an +* additional sync is required here. +*/ +synchronize_srcu_expedited (&encl->srcu); +cleanup_srcu_struct (&encl->srcu);WARN_ON_ONCE (!list_empty (&encl->mm_list));
Thanks
Haitao
Jarkko Sakkinen
maintainerQuoted 40 lines · Haitao Huang
Quoted 37 lines · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch a grace period initiated bysgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()The loop in
sgx_release()observes an empty list because B has removed its
entry in the middle, and callscleanup_srcu_struct()before B has a chance
to callssynchronize_srcu().Fixes 1728ab54b4be ("x86/sgx: Add a page reclaimer")Suggested-by Sean Christopherson <[email protected]>Suggested-by Haitao Huang <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v3: Fine-tuned tags, and added missing change log for v2.
v2: Switch tosynchronize_srcu_expedited().arch/x86/kernel/cpu/sgx/encl.c | 6 ++++++ 1 file changed, 6 insertions(+)arch/x86/kernel/cpu/sgx/encl.c +2
-0@@ -438,6 +438,12 @@ void sgx_encl_release(struct kref *ref)if (encl->backing)fput (encl->backing); +/* +* Each sgx_mmu_notifier_release() starts a grace period. Therefore, an
Quoted 8 lines
arch/x86/kernel/cpu/sgx/encl.c void sgx_encl_release(struct kref *ref) · line 449
Quoted 6 lines
arch/x86/kernel/cpu/sgx/encl.c +4
-0+* additional sync is required here. +*/ +synchronize_srcu_expedited (&encl->srcu); +cleanup_srcu_struct (&encl->srcu);WARN_ON_ONCE (!list_empty (&encl->mm_list));Tested-by Haitao Huang <[email protected]>
Thanks.
/Jarkko
Borislav Petkov
maintainerQuoted 11 lines · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch a grace period initiated bysgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()
Quoted 3 lines
The loop in
sgx_release()observes an empty list because B has removed its entry in the middle, and callscleanup_srcu_struct()before B has a chance to callssynchronize_srcu().
Leading to what? NULL ptr?
https://lkml.kernel.org/r/[email protected]
already suggested that you should explain the bug better and add the splat but I'm still missing that explanation.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteJarkko Sakkinen
maintainerQuoted 16 lines · Borislav Petkov
Quoted 11 lines · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch a grace period initiated bysgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()Quoted 3 lines
The loop in
sgx_release()observes an empty list because B has removed its entry in the middle, and callscleanup_srcu_struct()before B has a chance to callssynchronize_srcu().
Quoted 6 lines
Leading to what? NULL ptr?
https://lkml.kernel.org/r/[email protected]
already suggested that you should explain the bug better and add the splat but I'm still missing that explanation.
OK, I'll try to explain it how I understand the issue.
Consider this loop in the VFS release hook (sgx_release):
/*
* Drain the remaining mm_list entries. At this point the list contains
* entries for processes, which have closed the enclave file but have
* not exited yet. The processes, which have exited, are gone from the
* list by sgx_mmu_notifier_release().
*/
for ( ; ; ) {
spin_lock(&encl->mm_lock);
if (list_empty(&encl->mm_list)) {
encl_mm = NULL;
} else {
encl_mm = list_first_entry(&encl->mm_list,
struct sgx_encl_mm, list);
list_del_rcu(&encl_mm->list);
}
spin_unlock(&encl->mm_lock);
/* The enclave is no longer mapped by any mm. */
if (!encl_mm)
break;
synchronize_srcu(&encl->srcu);
mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm);
kfree(encl_mm);
}At this point all processes have closed the enclave file, but that doesn't mean that they all have exited yet.
Now, let's imagine that there is exactly one entry in the encl->mm_list. and sgx_release() execution gets scheduled right after returning from synchronize_srcu().
With some bad luck, some process comes and removes that last entry befoe sgx_release() acquires mm_lock. The loop in sgx_release() just leaves
/* The enclave is no longer mapped by any mm. */ if (!encl_mm) break;
No synchronize_srcu().
After writing this, I think that the placement for synchronize_srcu() in this patch is not best possible. It should be rather that the above loop would also call synchronize_srcu() when leaving.
I.e. the code change would result:
for ( ; ; ) {
spin_lock(&encl->mm_lock);
if (list_empty(&encl->mm_list)) {
encl_mm = NULL;
} else {
encl_mm = list_first_entry(&encl->mm_list,
struct sgx_encl_mm, list);
list_del_rcu(&encl_mm->list);
}
spin_unlock(&encl->mm_lock);
/*
* synchronize_srcu() is mandatory *even* when the list was
* empty, in order make sure that grace periods stays in
* sync even when another task took away the last entry
* (i.e. exiting process when it deletes its mm_list).
*/
synchronize_srcu(&encl->srcu);
/* The enclave is no longer mapped by any mm. */
if (!encl_mm)
break;
mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm);
kfree(encl_mm);
}What do you think? Does this start to make more sense now? I don't have logs for this but the bug can be also reasoned.
/Jarkko
Borislav Petkov
maintainer+ paulmck.
Quoted 108 lines · Jarkko Sakkinen
Quoted 16 lines · Borislav Petkov
Quoted 11 lines · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch a grace period initiated bysgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()Quoted 3 lines
The loop in
sgx_release()observes an empty list because B has removed its entry in the middle, and callscleanup_srcu_struct()before B has a chance to callssynchronize_srcu().Quoted 6 lines
Leading to what? NULL ptr?
https://lkml.kernel.org/r/[email protected]
already suggested that you should explain the bug better and add the splat but I'm still missing that explanation.
OK, I'll try to explain it how I understand the issue.
Consider this loop in the VFS release hook (
sgx_release):/* * Drain the remaining mm_list entries. At this point the list contains * entries for processes, which have closed the enclave file but have * not exited yet. The processes, which have exited, are gone from the * list by sgx_mmu_notifier_release(). */ for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; synchronize_srcu(&encl->srcu); mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }At this point all processes have closed the enclave file, but that doesn't mean that they all have exited yet.
Now, let's imagine that there is exactly one entry in the
encl->mm_list. andsgx_release()execution gets scheduled right after returning fromsynchronize_srcu().With some bad luck, some process comes and removes that last entry befoe
sgx_release()acquiresmm_lock. The loop insgx_release()just leaves/* The enclave is no longer mapped by any mm. */ if (!encl_mm) break;No
synchronize_srcu().After writing this, I think that the placement for
synchronize_srcu()in this patch is not best possible. It should be rather that the above loop would also callsynchronize_srcu()when leaving.I.e. the code change would result:
for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* * synchronize_srcu() is mandatory *even* when the list was * empty, in order make sure that grace periods stays in * sync even when another task took away the last entry * (i.e. exiting process when it deletes its mm_list). */ synchronize_srcu(&encl->srcu); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }
Quoted 2 lines
What do you think? Does this start to make more sense now? I don't have logs for this but the bug can be also reasoned.
It does. Now you need to write it up in a detailed form so that it is clear to readers months/years from now what exactly can happen. You can use a two-column format like
CPU A CPU B
Bla
Blu
This happens now here
But this needs to happen there
and so on.
Also, from reading up a bit on this, Documentation/RCU/checklist.rst says
"Use of the expedited primitives should be restricted to rare configuration-change operations that would not normally be undertaken while a real-time workload is running."
so why are you using synchronize_srcu_expedited()? Grepping the tree reveals only a couple of call sites only... but I've almost no clue of RCU so lemme CC Paul.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 130 lines · Borislav Petkov
+ paulmck.
Quoted 108 lines · Jarkko Sakkinen
Quoted 16 lines · Borislav Petkov
Quoted 11 lines · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch a grace period initiated bysgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()Quoted 3 lines
The loop in
sgx_release()observes an empty list because B has removed its entry in the middle, and callscleanup_srcu_struct()before B has a chance to callssynchronize_srcu().Quoted 6 lines
Leading to what? NULL ptr?
https://lkml.kernel.org/r/[email protected]
already suggested that you should explain the bug better and add the splat but I'm still missing that explanation.
OK, I'll try to explain it how I understand the issue.
Consider this loop in the VFS release hook (
sgx_release):/* * Drain the remaining mm_list entries. At this point the list contains * entries for processes, which have closed the enclave file but have * not exited yet. The processes, which have exited, are gone from the * list by sgx_mmu_notifier_release(). */ for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; synchronize_srcu(&encl->srcu); mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }At this point all processes have closed the enclave file, but that doesn't mean that they all have exited yet.
Now, let's imagine that there is exactly one entry in the
encl->mm_list. andsgx_release()execution gets scheduled right after returning fromsynchronize_srcu().With some bad luck, some process comes and removes that last entry befoe
sgx_release()acquiresmm_lock. The loop insgx_release()just leaves/* The enclave is no longer mapped by any mm. */ if (!encl_mm) break;No
synchronize_srcu().After writing this, I think that the placement for
synchronize_srcu()in this patch is not best possible. It should be rather that the above loop would also callsynchronize_srcu()when leaving.I.e. the code change would result:
for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* * synchronize_srcu() is mandatory *even* when the list was * empty, in order make sure that grace periods stays in * sync even when another task took away the last entry * (i.e. exiting process when it deletes its mm_list). */ synchronize_srcu(&encl->srcu); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }Quoted 2 lines
What do you think? Does this start to make more sense now? I don't have logs for this but the bug can be also reasoned.
It does. Now you need to write it up in a detailed form so that it is clear to readers months/years from now what exactly can happen. You can use a two-column format like
CPU A CPU BBla
BluThis happens now here
But this needs to happen thereand so on.
Also, from reading up a bit on this,
Documentation/RCU/checklist.rstsays
Quoted 7 lines
"Use of the expedited primitives should be restricted to rare configuration-change operations that would not normally be undertaken while a real-time workload is running."
so why are you using
synchronize_srcu_expedited()? Grepping the tree reveals only a couple of call sites only... but I've almost no clue of RCU so lemme CC Paul.
The SRCU expedited grace periods are easier on real-time workloads than synchronize_rcu_expedited(), but the SRCU variant still burns more CPU time on a given grace period. But either way, as the document says further down "However, real-time workloads can use rcupdate.rcu_normal kernel boot parameter to completely disable expedited grace periods, though this might have performance implications."
So what are the performance implications in this case?
Thanx, Paul
Jarkko Sakkinen
maintainerQuoted 130 lines · Borislav Petkov
+ paulmck.
Quoted 108 lines · Jarkko Sakkinen
Quoted 16 lines · Borislav Petkov
Quoted 11 lines · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch a grace period initiated bysgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()Quoted 3 lines
The loop in
sgx_release()observes an empty list because B has removed its entry in the middle, and callscleanup_srcu_struct()before B has a chance to callssynchronize_srcu().Quoted 6 lines
Leading to what? NULL ptr?
https://lkml.kernel.org/r/[email protected]
already suggested that you should explain the bug better and add the splat but I'm still missing that explanation.
OK, I'll try to explain it how I understand the issue.
Consider this loop in the VFS release hook (
sgx_release):/* * Drain the remaining mm_list entries. At this point the list contains * entries for processes, which have closed the enclave file but have * not exited yet. The processes, which have exited, are gone from the * list by sgx_mmu_notifier_release(). */ for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; synchronize_srcu(&encl->srcu); mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }At this point all processes have closed the enclave file, but that doesn't mean that they all have exited yet.
Now, let's imagine that there is exactly one entry in the
encl->mm_list. andsgx_release()execution gets scheduled right after returning fromsynchronize_srcu().With some bad luck, some process comes and removes that last entry befoe
sgx_release()acquiresmm_lock. The loop insgx_release()just leaves/* The enclave is no longer mapped by any mm. */ if (!encl_mm) break;No
synchronize_srcu().After writing this, I think that the placement for
synchronize_srcu()in this patch is not best possible. It should be rather that the above loop would also callsynchronize_srcu()when leaving.I.e. the code change would result:
for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* * synchronize_srcu() is mandatory *even* when the list was * empty, in order make sure that grace periods stays in * sync even when another task took away the last entry * (i.e. exiting process when it deletes its mm_list). */ synchronize_srcu(&encl->srcu); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }Quoted 2 lines
What do you think? Does this start to make more sense now? I don't have logs for this but the bug can be also reasoned.
It does. Now you need to write it up in a detailed form so that it is clear to readers months/years from now what exactly can happen. You can use a two-column format like
CPU A CPU BBla
BluThis happens now here
But this needs to happen thereand so on.
Also, from reading up a bit on this,
Documentation/RCU/checklist.rstsays
Quoted 7 lines
"Use of the expedited primitives should be restricted to rare configuration-change operations that would not normally be undertaken while a real-time workload is running."
so why are you using
synchronize_srcu_expedited()? Grepping the tree reveals only a couple of call sites only... but I've almost no clue of RCU so lemme CC Paul.
It spun out of this discussion:
https://lore.kernel.org/linux-sgx/[email protected]/raw
My reasoning was that this is not a common case. The main loop that uses synchronize_srcu().
Quoted 4 lines
Signature · 3 lines
-- Regards/Gruss, Boris.
/Jarkko
Quoted 139 lines · Jarkko Sakkinen
Quoted 130 lines · Borislav Petkov
+ paulmck.
Quoted 108 lines · Jarkko Sakkinen
Quoted 16 lines · Borislav Petkov
Quoted 11 lines · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch a grace period initiated bysgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()Quoted 3 lines
The loop in
sgx_release()observes an empty list because B has removed its entry in the middle, and callscleanup_srcu_struct()before B has a chance to callssynchronize_srcu().Quoted 6 lines
Leading to what? NULL ptr?
https://lkml.kernel.org/r/[email protected]
already suggested that you should explain the bug better and add the splat but I'm still missing that explanation.
OK, I'll try to explain it how I understand the issue.
Consider this loop in the VFS release hook (
sgx_release):/* * Drain the remaining mm_list entries. At this point the list contains * entries for processes, which have closed the enclave file but have * not exited yet. The processes, which have exited, are gone from the * list by sgx_mmu_notifier_release(). */ for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; synchronize_srcu(&encl->srcu); mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }At this point all processes have closed the enclave file, but that doesn't mean that they all have exited yet.
Now, let's imagine that there is exactly one entry in the
encl->mm_list. andsgx_release()execution gets scheduled right after returning fromsynchronize_srcu().With some bad luck, some process comes and removes that last entry befoe
sgx_release()acquiresmm_lock. The loop insgx_release()just leaves/* The enclave is no longer mapped by any mm. */ if (!encl_mm) break;No
synchronize_srcu().After writing this, I think that the placement for
synchronize_srcu()in this patch is not best possible. It should be rather that the above loop would also callsynchronize_srcu()when leaving.I.e. the code change would result:
for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* * synchronize_srcu() is mandatory *even* when the list was * empty, in order make sure that grace periods stays in * sync even when another task took away the last entry * (i.e. exiting process when it deletes its mm_list). */ synchronize_srcu(&encl->srcu); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }Quoted 2 lines
What do you think? Does this start to make more sense now? I don't have logs for this but the bug can be also reasoned.
It does. Now you need to write it up in a detailed form so that it is clear to readers months/years from now what exactly can happen. You can use a two-column format like
CPU A CPU BBla
BluThis happens now here
But this needs to happen thereand so on.
Also, from reading up a bit on this,
Documentation/RCU/checklist.rstsaysQuoted 7 lines
"Use of the expedited primitives should be restricted to rare configuration-change operations that would not normally be undertaken while a real-time workload is running."
so why are you using
synchronize_srcu_expedited()? Grepping the tree reveals only a couple of call sites only... but I've almost no clue of RCU so lemme CC Paul.
Quoted 6 lines
It spun out of this discussion:
https://lore.kernel.org/linux-sgx/[email protected]/raw
My reasoning was that this is not a common case. The main loop that uses
synchronize_srcu().
It seems to me that loading and unloading SGX enclaves qualifies as a configuration operation, so use of synchronize_srcu_expedited() should be just fine in that case. This of course implies that SGX enclaves should not be loaded or unloaded while an aggressive real-time application is running. Which might well be the case for other reasons.
So I believe synchronize_srcu_expedited() should be fine in this case.
Thanx, Paul
Borislav Petkov
maintainerOn Wed, Jan 13, 2021 at 09:46:02AM -0800, Paul E. McKenney wrote:
< Lemme trim that mail fat >
Quoted 5 lines
It seems to me that loading and unloading SGX enclaves qualifies as a configuration operation, so use of
synchronize_srcu_expedited()should be just fine in that case. This of course implies that SGX enclaves should not be loaded or unloaded while an aggressive real-time application is running. Which might well be the case for other reasons.
I believe RT and SGX should be orthogonal to each-other unless someone rolls out of the woodwork, wanting to run realtime enclaves... Ewww.
Quoted 1 line
So I believe
synchronize_srcu_expedited()should be fine in this case.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 4 lines · Borislav Petkov
On Wed, Jan 13, 2021 at 09:46:02AM -0800, Paul E. McKenney wrote:
< Lemme trim that mail fat >
Quoted 8 lines
Quoted 5 lines
It seems to me that loading and unloading SGX enclaves qualifies as a configuration operation, so use of
synchronize_srcu_expedited()should be just fine in that case. This of course implies that SGX enclaves should not be loaded or unloaded while an aggressive real-time application is running. Which might well be the case for other reasons.I believe RT and SGX should be orthogonal to each-other unless someone rolls out of the woodwork, wanting to run realtime enclaves... Ewww.
I could speculate about an RT workload running on a system that also ran non-realtime SGX workloads, but who knows? Stranger things have happened. ;-)
Thanx, Paul
Jarkko Sakkinen
maintainerQuoted 153 lines · Paul E. McKenney
Quoted 139 lines · Jarkko Sakkinen
Quoted 130 lines · Borislav Petkov
+ paulmck.
Quoted 108 lines · Jarkko Sakkinen
Quoted 16 lines · Borislav Petkov
Quoted 11 lines · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch a grace period initiated bysgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()Quoted 3 lines
The loop in
sgx_release()observes an empty list because B has removed its entry in the middle, and callscleanup_srcu_struct()before B has a chance to callssynchronize_srcu().Quoted 6 lines
Leading to what? NULL ptr?
https://lkml.kernel.org/r/[email protected]
already suggested that you should explain the bug better and add the splat but I'm still missing that explanation.
OK, I'll try to explain it how I understand the issue.
Consider this loop in the VFS release hook (
sgx_release):/* * Drain the remaining mm_list entries. At this point the list contains * entries for processes, which have closed the enclave file but have * not exited yet. The processes, which have exited, are gone from the * list by sgx_mmu_notifier_release(). */ for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; synchronize_srcu(&encl->srcu); mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }At this point all processes have closed the enclave file, but that doesn't mean that they all have exited yet.
Now, let's imagine that there is exactly one entry in the
encl->mm_list. andsgx_release()execution gets scheduled right after returning fromsynchronize_srcu().With some bad luck, some process comes and removes that last entry befoe
sgx_release()acquiresmm_lock. The loop insgx_release()just leaves/* The enclave is no longer mapped by any mm. */ if (!encl_mm) break;No
synchronize_srcu().After writing this, I think that the placement for
synchronize_srcu()in this patch is not best possible. It should be rather that the above loop would also callsynchronize_srcu()when leaving.I.e. the code change would result:
for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* * synchronize_srcu() is mandatory *even* when the list was * empty, in order make sure that grace periods stays in * sync even when another task took away the last entry * (i.e. exiting process when it deletes its mm_list). */ synchronize_srcu(&encl->srcu); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }Quoted 2 lines
What do you think? Does this start to make more sense now? I don't have logs for this but the bug can be also reasoned.
It does. Now you need to write it up in a detailed form so that it is clear to readers months/years from now what exactly can happen. You can use a two-column format like
CPU A CPU BBla
BluThis happens now here
But this needs to happen thereand so on.
Also, from reading up a bit on this,
Documentation/RCU/checklist.rstsaysQuoted 7 lines
"Use of the expedited primitives should be restricted to rare configuration-change operations that would not normally be undertaken while a real-time workload is running."
so why are you using
synchronize_srcu_expedited()? Grepping the tree reveals only a couple of call sites only... but I've almost no clue of RCU so lemme CC Paul.Quoted 6 lines
It spun out of this discussion:
https://lore.kernel.org/linux-sgx/[email protected]/raw
My reasoning was that this is not a common case. The main loop that uses
synchronize_srcu().It seems to me that loading and unloading SGX enclaves qualifies as a configuration operation, so use of
synchronize_srcu_expedited()should be just fine in that case. This of course implies that SGX enclaves should not be loaded or unloaded while an aggressive real-time application is running. Which might well be the case for other reasons.
Quoted 3 lines
So I believe
synchronize_srcu_expedited()should be fine in this case.Thanx, Paul
Thank you for explaining this in detail.
I'll leave it out of the bug fix, and reconsider as a separate patch. I think it should be fine to use it here but is really out-of-scope for the change.
/Jarkko
Quoted 112 lines · Jarkko Sakkinen
Quoted 17 lines · Borislav Petkov
Quoted 1 line · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch agrace
Quoted 11 lines
period initiated by
sgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()The loop in
sgx_release()observes an empty list because B hasremoved its
Quoted 1 line
entry in the middle, and calls
cleanup_srcu_struct()before B has achance
Quoted 8 lines
Quoted 1 line
to calls
synchronize_srcu().Leading to what? NULL ptr?
https://lkml.kernel.org/r/[email protected]
already suggested that you should explain the bug better and add the splat but I'm still missing that explanation.
OK, I'll try to explain it how I understand the issue.
Consider this loop in the VFS release hook (
sgx_release):/* * Drain the remaining mm_list entries. At this point the list contains * entries for processes, which have closed the enclave file but have * not exited yet. The processes, which have exited, are gone from the * list by sgx_mmu_notifier_release(). */ for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; synchronize_srcu(&encl->srcu); mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }At this point all processes have closed the enclave file, but that doesn't mean that they all have exited yet.
Now, let's imagine that there is exactly one entry in the
encl->mm_list. andsgx_release()execution gets scheduled right after returning fromsynchronize_srcu().With some bad luck, some process comes and removes that last entry befoe
sgx_release()acquiresmm_lock. The loop insgx_release()just leaves/* The enclave is no longer mapped by any mm. */ if (!encl_mm) break;No
synchronize_srcu().After writing this, I think that the placement for
synchronize_srcu()in this patch is not best possible. It should be rather that the above loop would also callsynchronize_srcu()when leaving.I.e. the code change would result:
for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* * synchronize_srcu() is mandatory *even* when the list was * empty, in order make sure that grace periods stays in * sync even when another task took away the last entry * (i.e. exiting process when it deletes its mm_list). */ synchronize_srcu(&encl->srcu); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }
Quoted 4 lines
What do you think? Does this start to make more sense now? I don't have logs for this but the bug can be also reasoned.
/Jarkko
I did this experiment just now and find it runs much much slower than both original code and code with synchronize_srcu_expedited fix in this patch. Haitao
Jarkko Sakkinen
maintainerQuoted 116 lines · Haitao Huang
Quoted 113 lines · Jarkko Sakkinen
Quoted 17 lines · Borislav Petkov
Quoted 1 line · Jarkko Sakkinen
Add
synchronize_srcu_expedited()tosgx_encl_release()to catch agrace
Quoted 11 lines
period initiated by
sgx_mmu_notifier_release().A trivial example of a failing sequence with tasks A and B:
1. A: ->
sgx_release()
2. B: ->sgx_mmu_notifier_release()
3. B: ->list_del_rcu()
3. A: ->sgx_encl_release()
4. A: ->cleanup_srcu_struct()The loop in
sgx_release()observes an empty list because B hasremoved its
Quoted 1 line
entry in the middle, and calls
cleanup_srcu_struct()before B hasa chance
Quoted 8 lines
Quoted 1 line
to calls
synchronize_srcu().Leading to what? NULL ptr?
https://lkml.kernel.org/r/[email protected]
already suggested that you should explain the bug better and add the splat but I'm still missing that explanation.
OK, I'll try to explain it how I understand the issue.
Consider this loop in the VFS release hook (
sgx_release):/* * Drain the remaining mm_list entries. At this point the list contains * entries for processes, which have closed the enclave file but have * not exited yet. The processes, which have exited, are gone from the * list by sgx_mmu_notifier_release(). */ for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; synchronize_srcu(&encl->srcu); mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }At this point all processes have closed the enclave file, but that doesn't
mean that they all have exited yet.Now, let's imagine that there is exactly one entry in the
encl->mm_list. andsgx_release()execution gets scheduled right after returning fromsynchronize_srcu().With some bad luck, some process comes and removes that last entry befoe
sgx_release()acquiresmm_lock. The loop insgx_release()just leaves/* The enclave is no longer mapped by any mm. */ if (!encl_mm) break;No
synchronize_srcu().After writing this, I think that the placement for
synchronize_srcu()in this patch is not best possible. It should be rather that the above loop would also callsynchronize_srcu()when leaving.I.e. the code change would result:
for ( ; ; ) { spin_lock(&encl->mm_lock); if (list_empty(&encl->mm_list)) { encl_mm = NULL; } else { encl_mm = list_first_entry(&encl->mm_list, struct sgx_encl_mm, list); list_del_rcu(&encl_mm->list); } spin_unlock(&encl->mm_lock); /* * synchronize_srcu() is mandatory *even* when the list was * empty, in order make sure that grace periods stays in * sync even when another task took away the last entry * (i.e. exiting process when it deletes its mm_list). */ synchronize_srcu(&encl->srcu); /* The enclave is no longer mapped by any mm. */ if (!encl_mm) break; mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); }
Quoted 8 lines
Quoted 4 lines
What do you think? Does this start to make more sense now? I don't have logs for this but the bug can be also reasoned.
/Jarkko
I did this experiment just now and find it runs much much slower than both original code and code with
synchronize_srcu_expeditedfix in this patch. Haitao
Yeah, but using expedited is not really in the scope of a bug fix.
It's a change that needs to be considered separately.
And it's more complicated than that. It also needs data to back it up. How we can test and compare?
/Jarkko