Skip to contentSkip to conversation

libmpathpersist: two final fixes

PATCH In review Benjamin Marzinski 2 participants 7 messages Sep 10, 2025 – Sep 22, 2025 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

cover libmpathpersist: two final fixes

These patches take care of a corner case where changing a registered key can race with a path getting restored and a make peempting work correctly with All Registrants type reservations.

Benjamin Marzinski (2):
libmpathpersist: handle updating key race condition
libmpathpersist: handle preempting all registrants reservations

 libmpathpersist/mpath_persist_int.c | 31 +++++++++++++++++++++++++----
 libmultipath/prkey.c                | 16 +++++++++++++--
 libmultipath/structs.h              |  1 +
 multipathd/main.c                   | 12 ++++++++++-
 4 files changed, 53 insertions(+), 7 deletions(-)
Signature · 1 line
2.50.1
author maintainer
#1 lore raw

PATCH 1/2 libmpathpersist: handle updating key race condition

If a multipath device's registered key is changed, mpathpersist needs to update the key in multipathd before it registers the new key on the paths. This means that there is a time when multipathd thinks the paths should be using the new key, but none of them are. If a path is restored after mpathpersist checks which paths are usable to set the key on, but before it sets the key on any path, multipathd will see that the new key is not registered on any paths, and think that the key has been preempted or cleared. This will leave the path without a key, and possibly make multipathd think the device does not hold the reservation, even if it does.

To avoid this, multipathd will now remember the old key when registering a new one. Once the registration is finished, and (un)setprstatus is called, multipathd will forget the old key. Until then, multipathd will check for either key when it looks to see if there is an existing key. If the registration fails and the key get reverted, multipathd will also forget the old key.

---
 libmultipath/prkey.c   | 16 ++++++++++++++--
 libmultipath/structs.h |  1 +
 multipathd/main.c      | 12 +++++++++++-
 3 files changed, 26 insertions(+), 3 deletions(-)
libmultipath/prkey.c +14 -2
index c66d293b..4fbde5ad 100644
@@ -222,10 +222,22 @@ int set_prkey(struct config *conf, struct multipath *mpp, uint64_t prkey,
 	}
 	else
 		ret = do_prkey(fd, mpp->wwid, NULL, PRKEY_WRITE);
-	if (ret == 0)
+	if (ret == 0) {
+		/*
+		 * If you are reverting back to the old key, because you
+		 * did not successfully set a new key, don't remember the
+		 * key you never successfully set.
+		 */
+		if (get_be64(mpp->old_pr_key) == prkey)
+			memset(&mpp->old_pr_key, 0, 8);
+		else
+			memcpy(&mpp->old_pr_key, &mpp->reservation_key, 8);
 		select_reservation_key(conf, mpp);
-	if (get_be64(mpp->reservation_key) != prkey)
+	}
+	if (get_be64(mpp->reservation_key) != prkey) {
+		memset(&mpp->old_pr_key, 0, 8);
 		ret = 1;
+	}
 out_file:
 	close(fd);
 out:
libmultipath/structs.h +1 -0
index a206a7d1..9247e74f 100644
@@ -520,6 +520,7 @@ struct multipath {
 	/* persistent management data*/
 	int prkey_source;
 	struct be64 reservation_key;
+	struct be64 old_pr_key;
 	uint8_t sa_flags;
 	int prflag;
 	int prhold;
multipathd/main.c +11 -1
index 86d9a098..39d4f8cd 100644
@@ -4249,6 +4249,7 @@ void set_pr(struct multipath *mpp)
 {
 	mpp->ever_registered_pr = true;
 	mpp->prflag = PR_SET;
+	memset(&mpp->old_pr_key, 0, 8);
 }
 
 void unset_pr(struct multipath *mpp)
@@ -4256,6 +4257,7 @@ void unset_pr(struct multipath *mpp)
 	mpp->prflag = PR_UNSET;
 	mpp->prhold = PR_UNSET;
 	mpp->sa_flags = 0;
+	memset(&mpp->old_pr_key, 0, 8);
 }
 
 /*
@@ -4308,7 +4310,15 @@ static int update_map_pr(struct multipath *mpp, struct path *pp)
 			dumpHex((char *)keyp, 8, 1);
 		}
 
-		if (!memcmp(&mpp->reservation_key, keyp, 8))
+		/*
+		 * If you are in the middle of updating a key (old_pr_key
+		 * is set) check for either the new key or the old key,
+		 * since you might be checking before any paths have
+		 * updated their keys.
+		 */
+		if (!memcmp(&mpp->reservation_key, keyp, 8) ||
+		    (get_be64(mpp->old_pr_key) &&
+		     !memcmp(&mpp->old_pr_key, keyp, 8)))
 			isFound = 1;
 	}
 
Signature · 1 line
2.50.1
author maintainer
#2 lore raw

PATCH 2/2 libmpathpersist: handle preempting all registrants reservations

In reply to Benjamin Marzinski

All Registrants reservations (types 7 and 8) are held by key 0x0. When preempting one, all registrations are cleared, except for the one on the path that issued the PREEMPT. libmpathpersist needs to handle this just like the other cases of self-preemption, so that all the paths of the preempting multipath device have their registrations restored while the device is suspended.

---
 libmpathpersist/mpath_persist_int.c | 31 +++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
libmpathpersist/mpath_persist_int.c +27 -4
index 9f301859..ab3d70cd 100644
@@ -569,8 +569,9 @@ static int mpath_prout_reg(struct multipath *mpp,int rq_servact, int rq_scope,
  * is suspended before issuing the preemption, and the keys are reregistered
  * before resuming it.
  */
-static int preempt_self(struct multipath *mpp, int rq_servact, int rq_scope,
-			unsigned int rq_type, int noisy, bool do_release)
+static int do_preempt_self(struct multipath *mpp, struct be64 sa_key,
+			   int rq_servact, int rq_scope, unsigned int rq_type,
+			   int noisy, bool do_release)
 {
 	int status, rel_status = MPATH_PR_SUCCESS;
 	struct path *pp = NULL;
@@ -583,7 +584,7 @@ static int preempt_self(struct multipath *mpp, int rq_servact, int rq_scope,
 	}
 
 	memcpy(paramp.key, &mpp->reservation_key, 8);
-	memcpy(paramp.sa_key, &mpp->reservation_key, 8);
+	memcpy(paramp.sa_key, &sa_key, 8);
 	status = mpath_prout_common(mpp, rq_servact, rq_scope, rq_type,
 				    &paramp, noisy, &pp);
 	if (status != MPATH_PR_SUCCESS) {
@@ -619,6 +620,21 @@ fail_resume:
 	return (rel_status != MPATH_PR_SUCCESS) ? rel_status : status;
 }
 
+static int preempt_self(struct multipath *mpp, int rq_servact, int rq_scope,
+			unsigned int rq_type, int noisy, bool do_release)
+{
+	return do_preempt_self(mpp, mpp->reservation_key, rq_servact, rq_scope,
+			       rq_type, noisy, do_release);
+}
+
+static int preempt_all(struct multipath *mpp, int rq_servact, int rq_scope,
+		       unsigned int rq_type, int noisy)
+{
+	struct be64 zerokey = {0};
+	return do_preempt_self(mpp, zerokey, rq_servact, rq_scope, rq_type,
+			       noisy, false);
+}
+
 static int mpath_prout_rel(struct multipath *mpp,int rq_servact, int rq_scope,
 			   unsigned int rq_type,
 			   struct prout_param_descriptor * paramp, int noisy)
@@ -889,8 +905,15 @@ 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, noisy) == YNU_YES)
+		if (reservation_key_matches(mpp, paramp->sa_key, noisy) == YNU_YES) {
 			preempting_reservation = true;
+			if (memcmp(paramp->sa_key, &zerokey, 8) == 0) {
+				/* all registrants case */
+				ret = preempt_all(mpp, rq_servact, rq_scope,
+						  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,
Signature · 1 line
2.50.1
#3 lore raw

Re: [PATCH 2/2] libmpathpersist: handle preempting all registrants reservations

Quoted 92 lines · Benjamin Marzinski

All Registrants reservations (types 7 and 8) are held by key 0x0. When
preempting one, all registrations are cleared, except for the one on the
path that issued the PREEMPT. libmpathpersist needs to handle this just
like the other cases of self-preemption, so that all the paths of the preempting multipath device have their registrations restored while the
device is suspended.

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

 1 file changed, 27 insertions(+), 4 deletions(-)
libmpathpersist/mpath_persist_int.c +0 -0
index 9f301859..ab3d70cd 100644
@@ -569,8 +569,9 @@ static int mpath_prout_reg(struct multipath
 *mpp,int rq_servact, int rq_scope,
   * is suspended before issuing the preemption, and the keys are

reregistered
  * before resuming it.
  */
-static int preempt_self(struct multipath *mpp, int rq_servact, int rq_scope,

libmpathpersist/mpath_persist_int.c +0 -1
-			unsigned int rq_type, int noisy, bool

do_release)
+static int do_preempt_self(struct multipath *mpp, struct be64 sa_key,

libmpathpersist/mpath_persist_int.c +1 -0
+			   int rq_servact, int rq_scope, unsigned

int rq_type,

libmpathpersist/mpath_persist_int.c +1 -0
+			   int noisy, bool do_release)

 {
  int status, rel_status = MPATH_PR_SUCCESS; struct path *pp = NULL;

libmpathpersist/mpath_persist_int.c +0 -0
@@ -583,7 +584,7 @@ static int preempt_self(struct multipath *mpp,
 int rq_servact, int rq_scope,
  	}


  memcpy(paramp.key, &mpp->reservation_key, 8);

libmpathpersist/mpath_persist_int.c +1 -1
-	memcpy(paramp.sa_key, &mpp->reservation_key, 8);
+	memcpy(paramp.sa_key, &sa_key, 8);

  status = mpath_prout_common(mpp, rq_servact, rq_scope, rq_type,
      &paramp, noisy, &pp); if (status != MPATH_PR_SUCCESS) {

libmpathpersist/mpath_persist_int.c +0 -0
@@ -619,6 +620,21 @@ fail_resume:

  return (rel_status != MPATH_PR_SUCCESS) ? rel_status :
status;
 }

+static int preempt_self(struct multipath *mpp, int rq_servact, int rq_scope,

libmpathpersist/mpath_persist_int.c +1 -0
+			unsigned int rq_type, int noisy, bool

do_release)

libmpathpersist/mpath_persist_int.c +2 -0
+{
+	return do_preempt_self(mpp, mpp->reservation_key,

rq_servact, rq_scope,

libmpathpersist/mpath_persist_int.c +4 -0
+			       rq_type, noisy, do_release);
+}
+
+static int preempt_all(struct multipath *mpp, int rq_servact, int

rq_scope,

libmpathpersist/mpath_persist_int.c +4 -0
+		       unsigned int rq_type, int noisy)
+{
+	struct be64 zerokey = {0};
+	return do_preempt_self(mpp, zerokey, rq_servact, rq_scope,

rq_type,

libmpathpersist/mpath_persist_int.c +3 -0
+			       noisy, false);
+}
+

 static int mpath_prout_rel(struct multipath *mpp,int rq_servact, int rq_scope,
     unsigned int rq_type,
     struct prout_param_descriptor * paramp, int noisy)

libmpathpersist/mpath_persist_int.c +0 -0
@@ -889,8 +905,15 @@ 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,

noisy) == YNU_YES)

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

noisy) == YNU_YES) {
  preempting_reservation = true;

libmpathpersist/mpath_persist_int.c +1 -0
+			if (memcmp(paramp->sa_key, &zerokey, 8) ==
Quoted 1 line

0) {

Don't you need to check if pr_type is 7 or 8 here?

Quoted 11 lines
libmpathpersist/mpath_persist_int.c +2 -0
+				/* all registrants case */
+				ret = preempt_all(mpp, rq_servact,

rq_scope,

libmpathpersist/mpath_persist_int.c +4 -0
+						  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,

author maintainer
#4 lore raw

Re: [PATCH 2/2] libmpathpersist: handle preempting all registrants reservations

Quoted 89 lines · Martin Wilck
Quoted 88 lines · Benjamin Marzinski

All Registrants reservations (types 7 and 8) are held by key 0x0. When
preempting one, all registrations are cleared, except for the one on the
path that issued the PREEMPT. libmpathpersist needs to handle this just
like the other cases of self-preemption, so that all the paths of the preempting multipath device have their registrations restored while the
device is suspended.

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

 1 file changed, 27 insertions(+), 4 deletions(-)
libmpathpersist/mpath_persist_int.c +0 -0
index 9f301859..ab3d70cd 100644
@@ -569,8 +569,9 @@ static int mpath_prout_reg(struct multipath
 *mpp,int rq_servact, int rq_scope,
   * is suspended before issuing the preemption, and the keys are

reregistered
  * before resuming it.
  */
-static int preempt_self(struct multipath *mpp, int rq_servact, int rq_scope,

libmpathpersist/mpath_persist_int.c +0 -1
-			unsigned int rq_type, int noisy, bool

do_release)
+static int do_preempt_self(struct multipath *mpp, struct be64 sa_key,

libmpathpersist/mpath_persist_int.c +1 -0
+			   int rq_servact, int rq_scope, unsigned

int rq_type,

libmpathpersist/mpath_persist_int.c +1 -0
+			   int noisy, bool do_release)

 {
  int status, rel_status = MPATH_PR_SUCCESS; struct path *pp = NULL;

libmpathpersist/mpath_persist_int.c +0 -0
@@ -583,7 +584,7 @@ static int preempt_self(struct multipath *mpp,
 int rq_servact, int rq_scope,
  	}


  memcpy(paramp.key, &mpp->reservation_key, 8);

libmpathpersist/mpath_persist_int.c +1 -1
-	memcpy(paramp.sa_key, &mpp->reservation_key, 8);
+	memcpy(paramp.sa_key, &sa_key, 8);

  status = mpath_prout_common(mpp, rq_servact, rq_scope, rq_type,
      &paramp, noisy, &pp); if (status != MPATH_PR_SUCCESS) {

libmpathpersist/mpath_persist_int.c +0 -0
@@ -619,6 +620,21 @@ fail_resume:

  return (rel_status != MPATH_PR_SUCCESS) ? rel_status :
status;
 }

+static int preempt_self(struct multipath *mpp, int rq_servact, int rq_scope,

libmpathpersist/mpath_persist_int.c +1 -0
+			unsigned int rq_type, int noisy, bool

do_release)

libmpathpersist/mpath_persist_int.c +2 -0
+{
+	return do_preempt_self(mpp, mpp->reservation_key,

rq_servact, rq_scope,

libmpathpersist/mpath_persist_int.c +4 -0
+			       rq_type, noisy, do_release);
+}
+
+static int preempt_all(struct multipath *mpp, int rq_servact, int

rq_scope,

libmpathpersist/mpath_persist_int.c +4 -0
+		       unsigned int rq_type, int noisy)
+{
+	struct be64 zerokey = {0};
+	return do_preempt_self(mpp, zerokey, rq_servact, rq_scope,

rq_type,

libmpathpersist/mpath_persist_int.c +3 -0
+			       noisy, false);
+}
+

 static int mpath_prout_rel(struct multipath *mpp,int rq_servact, int rq_scope,
     unsigned int rq_type,
     struct prout_param_descriptor * paramp, int noisy)

libmpathpersist/mpath_persist_int.c +0 -0
@@ -889,8 +905,15 @@ 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,

noisy) == YNU_YES)

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

noisy) == YNU_YES) {
  preempting_reservation = true;

libmpathpersist/mpath_persist_int.c +1 -0
+			if (memcmp(paramp->sa_key, &zerokey, 8) ==

0) {

Don't you need to check if pr_type is 7 or 8 here?

I don't think so. When we get here, we know that the reservation is reported to be held by the key 0x0. That's obviously not a valid registered key. I'm pretty sure that a SCSI device will only report this reservation key in the "all registrants" case, and that "all registrants" reservations will always report 0x0 as the key.

That being said, I needed to make reservation_key_matches() return the reservation type to handle a different issue, so it's pretty trivial to make this code double-check it for this case.

-Ben

Quoted 12 lines
Quoted 12 lines
libmpathpersist/mpath_persist_int.c +2 -0
+				/* all registrants case */
+				ret = preempt_all(mpp, rq_servact,

rq_scope,

libmpathpersist/mpath_persist_int.c +4 -0
+						  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,

#5 lore raw

Re: [PATCH 2/2] libmpathpersist: handle preempting all registrants reservations

Quoted 113 lines · Benjamin Marzinski
Quoted 103 lines · Martin Wilck
Quoted 102 lines · Benjamin Marzinski

All Registrants reservations (types 7 and 8) are held by key 0x0. When
preempting one, all registrations are cleared, except for the one on
the
path that issued the PREEMPT. libmpathpersist needs to handle this
just
like the other cases of self-preemption, so that all the paths of the
preempting multipath device have their registrations restored while
the
device is suspended.

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

 1 file changed, 27 insertions(+), 4 deletions(-)
libmpathpersist/mpath_persist_int.c +0 -0
index 9f301859..ab3d70cd 100644
@@ -569,8 +569,9 @@ static int mpath_prout_reg(struct multipath
 *mpp,int rq_servact, int rq_scope,
   * is suspended before issuing the preemption, and the keys are

reregistered
  * before resuming it.
  */
-static int preempt_self(struct multipath *mpp, int rq_servact, int
rq_scope,

libmpathpersist/mpath_persist_int.c +0 -1
-			unsigned int rq_type, int noisy, bool

do_release)
+static int do_preempt_self(struct multipath *mpp, struct be64 sa_key,

libmpathpersist/mpath_persist_int.c +1 -0
+			   int rq_servact, int rq_scope,

unsigned
int rq_type,

libmpathpersist/mpath_persist_int.c +1 -0
+			   int noisy, bool do_release)

 {
  int status, rel_status = MPATH_PR_SUCCESS; struct path *pp = NULL;

libmpathpersist/mpath_persist_int.c +0 -0
@@ -583,7 +584,7 @@ static int preempt_self(struct multipath
 *mpp,
 int rq_servact, int rq_scope,
  	}


  memcpy(paramp.key, &mpp->reservation_key, 8);

libmpathpersist/mpath_persist_int.c +1 -1
-	memcpy(paramp.sa_key, &mpp->reservation_key, 8);
+	memcpy(paramp.sa_key, &sa_key, 8);

  status = mpath_prout_common(mpp, rq_servact, rq_scope, rq_type,
      &paramp, noisy, &pp); if (status != MPATH_PR_SUCCESS) {

libmpathpersist/mpath_persist_int.c +0 -0
@@ -619,6 +620,21 @@ fail_resume:

  return (rel_status != MPATH_PR_SUCCESS) ? rel_status :
status;
 }

+static int preempt_self(struct multipath *mpp, int rq_servact, int
rq_scope,

libmpathpersist/mpath_persist_int.c +1 -0
+			unsigned int rq_type, int noisy, bool

do_release)

libmpathpersist/mpath_persist_int.c +2 -0
+{
+	return do_preempt_self(mpp, mpp->reservation_key,

rq_servact, rq_scope,

libmpathpersist/mpath_persist_int.c +4 -0
+			       rq_type, noisy, do_release);
+}
+
+static int preempt_all(struct multipath *mpp, int rq_servact,

int
rq_scope,

libmpathpersist/mpath_persist_int.c +4 -0
+		       unsigned int rq_type, int noisy)
+{
+	struct be64 zerokey = {0};
+	return do_preempt_self(mpp, zerokey, rq_servact,

rq_scope,
rq_type,

libmpathpersist/mpath_persist_int.c +3 -0
+			       noisy, false);
+}
+

 static int mpath_prout_rel(struct multipath *mpp,int rq_servact, int
rq_scope,
     unsigned int rq_type,
     struct prout_param_descriptor * paramp,
int noisy)

libmpathpersist/mpath_persist_int.c +0 -0
@@ -889,8 +905,15 @@ 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,

noisy) == YNU_YES)

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

noisy) == YNU_YES) {
  preempting_reservation = true;

libmpathpersist/mpath_persist_int.c +1 -0
+			if (memcmp(paramp->sa_key, &zerokey, 8)

==
0) {

Don't you need to check if pr_type is 7 or 8 here?

Quoted 6 lines

I don't think so. When we get here, we know that the reservation is reported to be held by the key 0x0. That's obviously not a valid registered key. I'm pretty sure that a SCSI device will only report this
reservation key in the "all registrants" case, and that "all registrants" reservations will always report 0x0 as the key.

Ok, fine. Thanks for confirming.

Quoted 6 lines

That being said, I needed to make reservation_key_matches() return the
reservation type to handle a different issue, so it's pretty trivial to
make this code double-check it for this case.

Not necessary, thanks.

Martin

Reviewed-by on the v1 series
#6 lore raw
In reply to Benjamin Marzinski
Quoted 9 lines · Benjamin Marzinski

These patches take care of a corner case where changing a registered key
can race with a path getting restored and a make peempting work correctly with All Registrants type reservations.

Benjamin Marzinski (2):
  libmpathpersist: handle updating key race condition
  libmpathpersist: handle preempting all registrants reservations

Quoted 6 lines

 libmpathpersist/mpath_persist_int.c | 31 +++++++++++++++++++++++++-- --
 libmultipath/prkey.c                | 16 +++++++++++++-- libmultipath/structs.h              |  1 + multipathd/main.c                   | 12 ++++++++++-

 4 files changed, 53 insertions(+), 7 deletions(-)

For the series: