libmpathpersist: fix self-preemption when key has no active reservation
Benjamin Marzinski
maintainerPATCH 1/1 libmpathpersist: fix self-preemption when key has no active reservation
From: Cykang <[email protected]>
According to the SCSI-3 Persistent Reservations specification, when a self-preemption is performed with a key that does not currently hold an active reservation, the operation should be treated as an unregister and should not fail.
The current code in do_mpath_persistent_reserve_out() does not check whether the preempting key has a reservation before invoking the self-preemption path. It unconditionally calls preempt_self() and re-registers the key on the local paths, even when the key has no reservation. This violates the SCSI-3 PR spec and can lead to incorrect reservation states.
This patch moves the self-preemption check inside the conditional block so that it only executes when the key matches, but does not bypass the general preemption path for keys without a reservation. With this change, the code will fall through to the common PR_OUT handling (e.g., MPATH_PROUT_RES_SA or CLEAR_SA) when the key is not reserved, allowing the storage target to properly process the operation as an unregister.
--- libmpathpersist/mpath_persist_int.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
libmpathpersist/mpath_persist_int.c +16 -6
@@ -841,6 +841,7 @@ int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd,bool unregistering, preempting_reservation = false;bool updated_prkey = false;bool failed_paths = false; +bool self_preempt_unreg = false; ret =mpath_get_map (curmp, fd, &mpp);if (ret !=MPATH_PR_SUCCESS )
@@ -961,13 +962,20 @@ int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd, rq_type, noisy);break ; } +/* if we are preempting ourself */ +if (memcmp (paramp->sa_key, paramp->key,8 ) ==0 ) { + ret =preempt_self (mpp, rq_servact, rq_scope, + rq_type, noisy,PREE_WORK_NONE ); +break ; + } + }else if (memcmp (paramp->sa_key, paramp->key,8 ) ==0 ) { +/* +* We are self-preempting, but we don't hold the +* reservation. This will unregister the device +*/ + self_preempt_unreg = true; } -/* if we are preempting ourself */ -if (memcmp (paramp->sa_key, paramp->key,8 ) ==0 ) { - ret =preempt_self (mpp, rq_servact, rq_scope, rq_type, - noisy,PREE_WORK_NONE ); -break ; - } +/* fallthrough */ case MPATH_PROUT_RES_SA :case MPATH_PROUT_CLEAR_SA : {
@@ -1018,6 +1026,8 @@ int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd,case MPATH_PROUT_PREE_AB_SA :if (preempting_reservation)update_prhold (mpp->alias, true); +else if (self_preempt_unreg) +update_prflag (mpp,0 ); }return ret; }
Signature · 1 line
2.46.1
Quoted 63 lines · Benjamin Marzinski
From: Cykang <[email protected]>
According to the SCSI-3 Persistent Reservations specification, when a self-preemption is performed with a key that does not currently hold an active reservation, the operation should be treated as an unregister
and should not fail.The current code in
do_mpath_persistent_reserve_out()does not check whether the preempting key has a reservation before invoking the self-preemption path. It unconditionally callspreempt_self()and re-registers the key on the local paths, even when the key has no reservation. This violates the SCSI-3 PR spec and can lead to incorrect reservation states.This patch moves the self-preemption check inside the conditional block so that it only executes when the key matches, but does not bypass the general preemption path for keys without a reservation. With this change, the code will fall through to the common
PR_OUThandling (e.g.,MPATH_PROUT_RES_SAorCLEAR_SA) when the key is not reserved, allowing the storage target to properly process the operation as an unregister.Signed-off-by Cykang <[email protected]>Signed-off-by Benjamin Marzinski <[email protected]>---
libmpathpersist/mpath_persist_int.c | 22 ++++++++++++++++------1 file changed, 16 insertions(+), 6 deletions(-)libmpathpersist/mpath_persist_int.c +1
-0@@ -841,6 +841,7 @@ int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd, bool unregistering, preempting_reservation = false; bool updated_prkey = false; bool failed_paths = false; +bool self_preempt_unreg = false;
ret =mpath_get_map(curmp, fd, &mpp);
if (ret !=MPATH_PR_SUCCESS)libmpathpersist/mpath_persist_int.c +9
-0@@ -961,13 +962,20 @@ int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd, rq_type, noisy); break; } +/* if we are preempting ourself */ +if (memcmp (paramp->sa_key, paramp->key,8 ) ==0 ) { + ret =preempt_self (mpp, rq_servact, rq_scope, + rq_type, noisy,PREE_WORK_NONE ); +break ; + } + }else if (memcmp (paramp->sa_key, paramp->key,8 ) ==0 ) { +/* +* We are self-preempting, but we don't hold the
libmpathpersist/mpath_persist_int.c +1
-0+* reservation. This will unregister the
Quoted 1 line
device
key and sa_key are just parameters entered by the caller. How do you know, without calling check_holding_reservation(), whether or not we currently hold a reservation?
Regards,
Martin
Signature · 4 lines
Dr. Martin Wilck <[email protected]> SUSE Software Solutions Germany GmbH, Frankenstr. 146, 90461 Nürnberg, Germany Geschäftsführer: Jochen Jaser, Andrew McDonald (HRB 36809,AG Nürnberg)
Benjamin Marzinski
maintainerQuoted 61 lines · Martin Wilck
Quoted 60 lines · Benjamin Marzinski
From: Cykang <[email protected]>
According to the SCSI-3 Persistent Reservations specification, when a self-preemption is performed with a key that does not currently hold an active reservation, the operation should be treated as an unregister
and should not fail.The current code in
do_mpath_persistent_reserve_out()does not check whether the preempting key has a reservation before invoking the self-preemption path. It unconditionally callspreempt_self()and re-registers the key on the local paths, even when the key has no reservation. This violates the SCSI-3 PR spec and can lead to incorrect reservation states.This patch moves the self-preemption check inside the conditional block so that it only executes when the key matches, but does not bypass the general preemption path for keys without a reservation. With this change, the code will fall through to the common
PR_OUThandling (e.g.,MPATH_PROUT_RES_SAorCLEAR_SA) when the key is not reserved, allowing the storage target to properly process the operation as an unregister.Signed-off-by Cykang <[email protected]>Signed-off-by Benjamin Marzinski <[email protected]>---
libmpathpersist/mpath_persist_int.c | 22 ++++++++++++++++------1 file changed, 16 insertions(+), 6 deletions(-)libmpathpersist/mpath_persist_int.c +1
-0@@ -841,6 +841,7 @@ int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd, bool unregistering, preempting_reservation = false; bool updated_prkey = false; bool failed_paths = false; +bool self_preempt_unreg = false;
ret =mpath_get_map(curmp, fd, &mpp);
if (ret !=MPATH_PR_SUCCESS)libmpathpersist/mpath_persist_int.c +8
-0@@ -961,13 +962,20 @@ int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd, rq_type, noisy); break; } +/* if we are preempting ourself */ +if (memcmp (paramp->sa_key, paramp->key,8 ) ==0 ) { + ret =preempt_self (mpp, rq_servact, rq_scope, + rq_type, noisy,PREE_WORK_NONE ); +break ; + } + }else if (memcmp (paramp->sa_key, paramp->key,8 ) ==0 ) { +/*
Quoted 8 lines
Quoted 4 lines
libmpathpersist/mpath_persist_int.c +1
-0+* We are self-preempting, but we don't hold the
libmpathpersist/mpath_persist_int.c +1
-0+* reservation. This will unregister the device
key and
sa_keyare just parameters entered by the caller. How do you know, without callingcheck_holding_reservation(), whether or not we currently hold a reservation?
In the "if" statement for this "else", we call reservation_key_matches(), so we know that nobody is holding a reservation with "sa_key". Well... no, actually we don't.
reservation_key_matches() could return YNU_UNDEF. In that case who knows what's going on with the reservation. Perhaps in that case, we check if we need to do any self preemption (sa_key equals 0 or key). If so, we just fail, since a failure seems better than silently doing the wrong thing (either re-registering when we're not supposed to, or not re-registering when we are supposed to). The most likely reason why reservation_key_matches() returns YNU_UNDEF is that there are no usable paths, so we would fail sending the PREEMPT command anyways.
If we don't need to do any self-preemption work, we might as well attempt to send the command, since it can't hurt. But I'm open to being convinced that the correct answer is just to always fail if reservation_key_matches() returns YNU_UNDEF, on the grounds that this code is complex enough without adding yet another special case workaround, that is itself likely to fail, since the paths are likely all down.
-Ben
Quoted 8 lines
Regards,
MartinSignature · 4 lines
Dr. Martin Wilck <[email protected]> SUSE Software Solutions Germany GmbH, Frankenstr. 146, 90461 Nürnberg, Germany Geschäftsführer: Jochen Jaser, Andrew McDonald (HRB 36809,AG Nürnberg)
Quoted 11 lines · Benjamin Marzinski
Quoted 6 lines · Martin Wilck
key and
sa_keyare just parameters entered by the caller. How do you
know, without callingcheck_holding_reservation(), whether or not we
currently hold a reservation?In the "if" statement for this "else", we call
reservation_key_matches(), so we know that nobody is holding a reservation with "sa_key". Well... no, actually we don't.
My bad, I should have re-read the function reservation_key_matches().
Quoted 12 lines
reservation_key_matches()could returnYNU_UNDEF. In that case who knows
what's going on with the reservation. Perhaps in that case, we check if
we need to do any self preemption (sa_keyequals 0 or key). If so, we just fail, since a failure seems better than silently doing the wrong thing (either re-registering when we're not supposed to, or not re-registering when we are supposed to). The most likely reason whyreservation_key_matches()returnsYNU_UNDEFis that there are no usable
paths, so we would fail sending the PREEMPT command anyways.
Quoted 2 lines
If we don't need to do any self-preemption work, we might as well attempt to send the command, since it can't hurt.
It's quite likely to fail if the PRIN command failed beforehand.
Quoted 6 lines
But I'm open to being
convinced that the correct answer is just to always fail ifreservation_key_matches()returnsYNU_UNDEF, on the grounds that this code is complex enough without adding yet another special case workaround, that is itself likely to fail, since the paths are likely all down.
I agree.
If this was just a single path and the path was down, the command would fail, and nothing would happen on the storage side. So this is what we should do here. There's a minimal chance that the PROUT would succeed even after the PRIN had failed, but in that case we wouldn't know what to do with our registered keys, as you described correctly.
So we should just fail.
Regards
Martin