bcachefs: Do not allow no fail lock request to fail
PATCH 1/1 bcachefs: Do not allow no fail lock request to fail
If the transaction chose itself as a victim before and restarted, it might request a no fail lock request this time. But it might be added to others' lock graph and be chose as the victim again, it's no longer safe without additional check. We can also convert the cycle detector to be fully RCU-based to solve that unsoundness, but the latency added to trans_put and additional memory required may not worth it.
--- fs/bcachefs/btree_locking.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/bcachefs/btree_locking.c +1 -1
@@ -293,7 +293,7 @@ int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle) g.nr =0 ; -if (trans->lock_must_abort) { +if (trans->lock_must_abort && !trans->lock_may_not_fail) {if (cycle)return -1 ;
Signature · 1 line
2.45.2
Quoted 6 lines · Alan Huang
If the transaction chose itself as a victim before and restarted, it might request a no fail lock request this time. But it might be added to others' lock graph and be chose as the victim again, it's no longer safe without additional check. We can also convert the cycle detector to be fully RCU-based to solve that unsoundness, but the latency added to
trans_putand additional memory required may not worth it.
This is going to need ascii art diagrams...
I would expect the lock waitlist locks and sequence numbers to protect against this kind of race, do they not?
Quoted 22 lines
Signed-off-by Alan Huang <[email protected]>--- fs/bcachefs/btree_locking.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/bcachefs/btree_locking.c +1
-1@@ -293,7 +293,7 @@ int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle) g.nr =0 ; -if (trans->lock_must_abort) { +if (trans->lock_must_abort && !trans->lock_may_not_fail) {if (cycle)return -1 ;Signature · 1 line
2.45.2
Quoted 10 lines · Kent Overstreet
Quoted 6 lines · Alan Huang
If the transaction chose itself as a victim before and restarted, it might request a no fail lock request this time. But it might be added to others' lock graph and be chose as the victim again, it's no longer safe without additional check. We can also convert the cycle detector to be fully RCU-based to solve that unsoundness, but the latency added to
trans_putand additional memory required may not worth it.This is going to need ascii art diagrams...
Assume (txn represents transaction):
txn1 owns lock1 and requests lock2
txn2 owns lock2 and requests lock1
txn2 runs cycle detector first, so it has added itself to lock1’s wait list,
txn1 now requests lock2, since txn2 haven’t lock lock2’s waitlist lock, so txn1 added itself to lock2’s waitlist. txn1 now runs cycle detector and detects it will have a cycle and choose txn2 as the victim. txn2 now also detects a cycle, and choose itself as the victim, so it restarts.
txn2 now requests a no fail lock and runs cycle detector again and sleep
txn1 sets txn2’s lock_must_abort to true and wake up txn2.
So txn2 is going to abort.
Quoted 26 lines
I would expect the lock waitlist locks and sequence numbers to protect against this kind of race, do they not?
Quoted 22 lines
Signed-off-by Alan Huang <[email protected]>---
fs/bcachefs/btree_locking.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)fs/bcachefs/btree_locking.c +1
-1@@ -293,7 +293,7 @@ int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle) g.nr =0 ; -if (trans->lock_must_abort) { +if (trans->lock_must_abort && !trans->lock_may_not_fail) {if (cycle)return -1 ;Signature · 1 line
2.45.2