Skip to contentSkip to conversation

libmpathpersist: fix self-preemption when key has no active reservation

PATCH In review Benjamin Marzinski 2 participants 3 messages Jul 21 – Jul 23 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

PATCH v2 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 | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
libmpathpersist/mpath_persist_int.c +20 -7
index 3091c5c2..97fc71a7 100644
@@ -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)
@@ -953,7 +954,10 @@ int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd,
 		break;
 	case MPATH_PROUT_PREE_SA:
 	case MPATH_PROUT_PREE_AB_SA:
-		if (reservation_key_matches(mpp, paramp->sa_key, NULL) == YNU_YES) {
+		ret = reservation_key_matches(mpp, paramp->sa_key, NULL);
+		if (ret == YNU_UNDEF)
+			return MPATH_PR_OTHER;
+		if (ret == YNU_YES) {
 			preempting_reservation = true;
 			if (memcmp(paramp->sa_key, &zerokey, 8) == 0) {
 				/* all registrants case */
@@ -961,13 +965,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 +1029,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.53.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 23 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 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.

Quoted 2 lines

One minor remark below.

Quoted 55 lines

---
 libmpathpersist/mpath_persist_int.c | 27 ++++++++++++++++++++-------

 1 file changed, 20 insertions(+), 7 deletions(-)
libmpathpersist/mpath_persist_int.c +1 -0
index 3091c5c2..97fc71a7 100644
@@ -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 +0 -0
@@ -953,7 +954,10 @@ int do_mpath_persistent_reserve_out(vector
 curmp, vector pathvec, int fd,
  		break;

  case MPATH_PROUT_PREE_SA:
  case MPATH_PROUT_PREE_AB_SA:

libmpathpersist/mpath_persist_int.c +0 -1
-		if (reservation_key_matches(mpp, paramp->sa_key,

NULL) == YNU_YES) {

libmpathpersist/mpath_persist_int.c +1 -0
+		ret = reservation_key_matches(mpp, paramp->sa_key,

NULL);

libmpathpersist/mpath_persist_int.c +3 -0
+		if (ret == YNU_UNDEF)
+			return MPATH_PR_OTHER;
+		if (ret == YNU_YES) {

  preempting_reservation = true; if (memcmp(paramp->sa_key, &zerokey, 8) ==
0) {
  /* all registrants case */

libmpathpersist/mpath_persist_int.c +9 -0
@@ -961,13 +965,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

device

libmpathpersist/mpath_persist_int.c int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd, · line 979

Quoted 2 lines
libmpathpersist/mpath_persist_int.c +2 -0
+			 */
+			self_preempt_unreg = true;

If you don't mind, I'll add a comment here that this will fall though further down. It's not immediately obvious IMO.

Regards
Martin

Quoted 22 lines

  }

libmpathpersist/mpath_persist_int.c +0 -3
-		/* if we are preempting ourself */
-		if (memcmp(paramp->sa_key, paramp->key, 8) == 0) {
-			ret = preempt_self(mpp, rq_servact,

rq_scope, rq_type,

libmpathpersist/mpath_persist_int.c +1 -3
-					   noisy, PREE_WORK_NONE);
-			break;
-		}
+

  /* fallthrough */
  case MPATH_PROUT_RES_SA:
  case MPATH_PROUT_CLEAR_SA: {

libmpathpersist/mpath_persist_int.c +0 -0
@@ -1018,6 +1029,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);

libmpathpersist/mpath_persist_int.c +2 -0
+		else if (self_preempt_unreg)
+			update_prflag(mpp, 0);

  }
  return ret;
 }

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)
#2 lore raw
Quoted 92 lines · Martin Wilck
Quoted 26 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 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.

Quoted 2 lines

One minor remark below.

Quoted 57 lines

---
 libmpathpersist/mpath_persist_int.c | 27 ++++++++++++++++++++----- --

 1 file changed, 20 insertions(+), 7 deletions(-)
libmpathpersist/mpath_persist_int.c +1 -0
index 3091c5c2..97fc71a7 100644
@@ -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 +0 -0
@@ -953,7 +954,10 @@ int do_mpath_persistent_reserve_out(vector
 curmp, vector pathvec, int fd,
  		break;

  case MPATH_PROUT_PREE_SA:
  case MPATH_PROUT_PREE_AB_SA:

libmpathpersist/mpath_persist_int.c +0 -1
-		if (reservation_key_matches(mpp, paramp->sa_key,

NULL) == YNU_YES) {

libmpathpersist/mpath_persist_int.c +1 -0
+		ret = reservation_key_matches(mpp, paramp->sa_key,

NULL);

libmpathpersist/mpath_persist_int.c +3 -0
+		if (ret == YNU_UNDEF)
+			return MPATH_PR_OTHER;
+		if (ret == YNU_YES) {

  preempting_reservation = true; if (memcmp(paramp->sa_key, &zerokey, 8) ==
0) {
  /* all registrants case */

libmpathpersist/mpath_persist_int.c +7 -0
@@ -961,13 +965,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) {

libmpathpersist/mpath_persist_int.c +2 -0
+			/*
+			 * We are self-preempting, but we don't

hold

Quoted 8 lines

libmpathpersist/mpath_persist_int.c int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd, · line 979

Quoted 5 lines

the

libmpathpersist/mpath_persist_int.c +1 -0
+			 * reservation. This will unregister the

device

libmpathpersist/mpath_persist_int.c +2 -0
+			 */
+			self_preempt_unreg = true;

If you don't mind, I'll add a comment here that this will fall though further down. It's not immediately obvious IMO.

Forget this remark, it's fine as-is.

Martin