Skip to contentSkip to conversation

Implement the NVMe reservation feature

PATCH Superseded Guixin Liu 2 participants 10 messages Sep 23, 2024 – Sep 24, 2024

Subsystems (2)

v9 of 19 Superseded Latest is v19 →

lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

v9 cover Implement the NVMe reservation feature

Hi guys,
I've implemented the NVMe reservation feature. Please review it, all
comments are welcome as usual.

Changes from v8 to v9:
- Remove the maintianer request.

- Support "preemt and abort" by adding a per-controller percpu ref to ns, doing wait per-controller percpu ref to zero when a controller's reservation or registration is preempt and the racqa == "preempt and abort". Currently add per-controller percpu ref only when nvmet_pr_check_cmd_access success, others are not cared.

- Report ns support reservation when resv is enabled.

- Report ctrl support reservation.

- Change the log level to info when log lost.

- Fix the UAF issue in nvmet_pr_unreg_by_prkey, and change nvmet_pr_unreg_by_prkey to nvmet_pr_unreg_all_host_by_prkey.

- Dont unregister the host when ctrl is destroyed for keep the reservation info when reconnect.

- Remove the rcu lock and mutex lock when free ns's pr info.

- Fix the situation of log.count is zero.

- Move the rtype check to the start of preemtion, Dmitry suggests that:
1.4.1.6 reserved
Receipt of reserved coded values in defined fields in commands shall be reported as an error.
Look forward the suggestions.
And also, this avoid the non-atomic change of unregister and set new holder.

- Fix the compile error when close CONFIG_LOCKDEP.

- Change nvmet_pr_send_event_by_hostid to nvmet_pr_send_event_to_host.

- Change nvmet_pr_unreg_by_prkey_except_hostid to nvmet_pr_unreg_all_others_by_prkey.

- Change nvmet_pr_unreg_by_prkey to nvmet_pr_unreg_all_host_by_prkey.

Changes from v7 to v8:
- Add me as the new file pr.c's maintainer.

Changes from v6 to v7:
- Handle "reservation notification mask" feature command to mask reservation log.

- Add all the registrants that need to be freed to a temporary list fist, and then after calling synchronize_rcu(), release all the registrants on the temporary list.

- Fix the resv log page is random when there is no resv log page.

- Change nvmet_is_host_still_connected() to nvmet_is_host_connected().

- Remove nvmet_pr_set_rtype_and_holder() and change nvmet_pr_create_new_resv() to nvmet_pr_create_new_reservation().

- Change nvmet_pr_find_registrant_by_hostid() to nvmet_pr_find_registrant().

- Change nvmet_pr_send_resv_released() to nvmet_pr_resv_released().

- Change __nvmet_pr_unregister_one() to nvmet_pr_unregister_one().

- In nvmet_pr_unreg_by_prkey(), nvmet_pr_unreg_by_prkey_except_hostid() and nvmet_pr_unreg_except_hostid(), first do unregistering and then do event sending.

Changes from v5 to v6:
- Use synchronize_rcu() and kfree() to free registrant instead of kfree_rcu().

- Remove nvmet_pr_register_check_rkey(), put the check into pr_lock warp. And refactor the nvmet_pr_register().

- Add the print fmt to the head.

- Add lockdep_is_held(&pr->pr_lock) condition to list_for_each_entry_rcu.

- Fix the bug in nvmet_pr_update_reg_attr(), when the change_attr hook return fail, we should not replace the holder.

Changes from v4 to v5:

- Use rculist macros to handle registration_list instead of list macros regardless of in mutex lock or not.

- Use goto statement instead of return in nvmet_is_host_still_connected and __nvmet_pr_unregister_one.

- Add lockdep_assert_held and rcu_read_lock_held assert to many functions, if it's necessary.

- Add a comment to nvmet_execute_get_log_page_resv to explain how lost_count works.

- In nvmet_pr_clear, we should set holder to NULL first, I fixed this.

- Unify nvmet_pr_update_holder_rtype and __nvmet_pr_do_replace to nvmet_pr_update_reg_attr.

- Fix wrong nr_pages in nvmet_execute_get_log_page_resv.

- Fix the deadlock issue of nvmet_pr_exit_ns, put it out of the subsys lock.

Changes from v3 to v4:
- Use kfifo to handle resv log page instead of list, and also limit the resv log queue to 64.

- Change the function calling alignment style to:

    nvmet_pr_send_event_by_hostid(pr, hostid,
            NVME_PR_LOG_RESERVATOPM_PREEMPTED); 

- Put kmalloc out of rcu_read_lock in nvmet_execute_pr_report().

- Remove the goto in __nvmet_pr_unregister_one().

- Change generation to atomic_t, and remove nvmet_pr_inc_generation().

- In addtion, the number2 patch "nvmet: unify aer type enum" is not relate with this patch, so I will send it separately.

Changes from v2 to v3:
- Use rcu instead of rwlock to make IO path run faster, and put the rtype into the struct nvmet_pr_registrant.

- Limit the resv_log_list to 128.

- Change generation to atomic64.

- Put register rkey check to a warpper.

- Change nr_avl_pages to nr_pages.

- Use NVME_SC_SUCCESS instead of 0.

- Change kmalloc param to let it not sleep in mutex lock.

Changes from v1 to v2:
- Implement the reservation notification report, includes registration preempted, reservation released and reservation preempted. And also handle the reservation log page available event and send get
reservation log page command to clear log page at host.

- Put the reservation check access after validate opcode. And remove opcodes which nvmet not implement yet check.
Now there is no admin opcode nvmet implemented needs reservation check,
so I dont add reservation check to admin command path.
Next we need to do reservation check includes the situation of nsid is
0xffffffff at each admin command path, if it is needed.

- Add reservation commands support in nvmet_get_cmd_effects_nvm().

- From Chaitanya, change the local variable tree style to make it cleaner, and add some comments about NVMe spec.
And also change others advice from chaitanya.

- Put the nvmet_pr_check_cmd_access and nvmet_parse_pr_cmd into reservation enable check warp.

- Remove kmem_cache instead to use kmalloc and kfree.

- Change others advice from Sagi.

- Add a blktest test case, this patch will be sent before these series of patches.

Guixin Liu (1):
nvmet: support reservation feature

 drivers/nvme/target/Makefile    |    2 +-
 drivers/nvme/target/admin-cmd.c |   24 +-
 drivers/nvme/target/configfs.c  |   27 +
 drivers/nvme/target/core.c      |   58 +-
 drivers/nvme/target/nvmet.h     |   49 ++
 drivers/nvme/target/pr.c        | 1214 +++++++++++++++++++++++++++++++
 include/linux/nvme.h            |   54 ++
 7 files changed, 1419 insertions(+), 9 deletions(-)
 create mode 100644 drivers/nvme/target/pr.c
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v9 1/1 nvmet: support reservation feature

This patch implements the reservation feature, includes:
1. reservation register(register, unregister and replace).
2. reservation acquire(acquire, preempt, preempt and abort).
3. reservation release(release and clear).
4. reservation report.
5. set feature and get feature of reservation notify mask.
6. get log page of reservation event.

And also make reservation configurable, one can set ns to support reservation before enable ns. The default of resv_enable is false.

---
 drivers/nvme/target/Makefile    |    2 +-
 drivers/nvme/target/admin-cmd.c |   24 +-
 drivers/nvme/target/configfs.c  |   27 +
 drivers/nvme/target/core.c      |   58 +-
 drivers/nvme/target/nvmet.h     |   49 ++
 drivers/nvme/target/pr.c        | 1214 +++++++++++++++++++++++++++++++
 include/linux/nvme.h            |   54 ++
 7 files changed, 1419 insertions(+), 9 deletions(-)
 create mode 100644 drivers/nvme/target/pr.c
drivers/nvme/target/Makefile +1 -1
index c66820102493..f9bfc904a5b3 100644
@@ -10,7 +10,7 @@ obj-$(CONFIG_NVME_TARGET_FCLOOP)	+= nvme-fcloop.o
 obj-$(CONFIG_NVME_TARGET_TCP)		+= nvmet-tcp.o
 
 nvmet-y		+= core.o configfs.o admin-cmd.o fabrics-cmd.o \
-			discovery.o io-cmd-file.o io-cmd-bdev.o
+			discovery.o io-cmd-file.o io-cmd-bdev.o pr.o
 nvmet-$(CONFIG_NVME_TARGET_PASSTHRU)	+= passthru.o
 nvmet-$(CONFIG_BLK_DEV_ZONED)		+= zns.o
 nvmet-$(CONFIG_NVME_TARGET_AUTH)	+= fabrics-cmd-auth.o auth.o
drivers/nvme/target/admin-cmd.c +23 -1
index f5b7054a4a05..2cd3560f5c6f 100644
@@ -176,6 +176,10 @@ static void nvmet_get_cmd_effects_nvm(struct nvme_effects_log *log)
 	log->iocs[nvme_cmd_read] =
 	log->iocs[nvme_cmd_flush] =
 	log->iocs[nvme_cmd_dsm]	=
+	log->iocs[nvme_cmd_resv_acquire] =
+	log->iocs[nvme_cmd_resv_register] =
+	log->iocs[nvme_cmd_resv_release] =
+	log->iocs[nvme_cmd_resv_report] =
 		cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
 	log->iocs[nvme_cmd_write] =
 	log->iocs[nvme_cmd_write_zeroes] =
@@ -340,6 +344,8 @@ static void nvmet_execute_get_log_page(struct nvmet_req *req)
 		return nvmet_execute_get_log_cmd_effects_ns(req);
 	case NVME_LOG_ANA:
 		return nvmet_execute_get_log_page_ana(req);
+	case NVME_LOG_RESERVATION:
+		return nvmet_execute_get_log_page_resv(req);
 	}
 	pr_debug("unhandled lid %d on qid %d\n",
 	       req->cmd->get_log_page.lid, req->sq->qid);
@@ -433,7 +439,8 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
 	id->nn = cpu_to_le32(NVMET_MAX_NAMESPACES);
 	id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES);
 	id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
-			NVME_CTRL_ONCS_WRITE_ZEROES);
+			NVME_CTRL_ONCS_WRITE_ZEROES |
+			NVME_CTRL_ONCS_RESERVATIONS);
 
 	/* XXX: don't report vwc if the underlying device is write through */
 	id->vwc = NVME_CTRL_VWC_PRESENT;
@@ -551,6 +558,15 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
 	id->nmic = NVME_NS_NMIC_SHARED;
 	id->anagrpid = cpu_to_le32(req->ns->anagrpid);
 
+	if (req->ns->pr.enable)
+		id->rescap = NVME_PR_SUPPORT_WRITE_EXCLUSIVE |
+			NVME_PR_SUPPORT_EXCLUSIVE_ACCESS |
+			NVME_PR_SUPPORT_WRITE_EXCLUSIVE_REG_ONLY |
+			NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_REG_ONLY |
+			NVME_PR_SUPPORT_WRITE_EXCLUSIVE_ALL_REGS |
+			NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_ALL_REGS |
+			NVME_PR_SUPPORT_IEKEY_VER_1_3_DEF;
+
 	memcpy(&id->nguid, &req->ns->nguid, sizeof(id->nguid));
 
 	id->lbaf[0].ds = req->ns->blksize_shift;
@@ -851,6 +867,9 @@ void nvmet_execute_set_features(struct nvmet_req *req)
 	case NVME_FEAT_WRITE_PROTECT:
 		status = nvmet_set_feat_write_protect(req);
 		break;
+	case NVME_FEAT_RESV_MASK:
+		status = nvmet_set_feat_resv_notif_mask(req, cdw11);
+		break;
 	default:
 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
 		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
@@ -949,6 +968,9 @@ void nvmet_execute_get_features(struct nvmet_req *req)
 	case NVME_FEAT_WRITE_PROTECT:
 		status = nvmet_get_feat_write_protect(req);
 		break;
+	case NVME_FEAT_RESV_MASK:
+		status = nvmet_get_feat_resv_notif_mask(req);
+		break;
 	default:
 		req->error_loc =
 			offsetof(struct nvme_common_command, cdw10);
drivers/nvme/target/configfs.c +27 -0
index bd87dfd173a4..9a56269cefd0 100644
@@ -748,6 +748,32 @@ static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
 
 CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
 
+static ssize_t nvmet_ns_resv_enable_show(struct config_item *item, char *page)
+{
+	return sprintf(page, "%d\n", to_nvmet_ns(item)->pr.enable);
+}
+
+static ssize_t nvmet_ns_resv_enable_store(struct config_item *item,
+					const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	bool val;
+
+	if (kstrtobool(page, &val))
+		return -EINVAL;
+
+	mutex_lock(&ns->subsys->lock);
+	if (ns->enabled) {
+		pr_err("the ns:%d is already enabled.\n", ns->nsid);
+		mutex_unlock(&ns->subsys->lock);
+		return -EINVAL;
+	}
+	ns->pr.enable = val;
+	mutex_unlock(&ns->subsys->lock);
+	return count;
+}
+CONFIGFS_ATTR(nvmet_ns_, resv_enable);
+
 static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_device_path,
 	&nvmet_ns_attr_device_nguid,
@@ -756,6 +782,7 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_enable,
 	&nvmet_ns_attr_buffered_io,
 	&nvmet_ns_attr_revalidate_size,
+	&nvmet_ns_attr_resv_enable,
 #ifdef CONFIG_PCI_P2PDMA
 	&nvmet_ns_attr_p2pmem,
 #endif
drivers/nvme/target/core.c +51 -7

127 lines not on this page · raw message

drivers/nvme/target/nvmet.h +49 -0

95 lines not on this page · raw message

drivers/nvme/target/pr.c new file +1214 -0

1,214 lines not on this page · raw message

include/linux/nvme.h +54 -0

72 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v9 1/1] nvmet: support reservation feature

Quoted 22 lines

This patch implements the reservation feature, includes:
1. reservation register(register, unregister and replace).
2. reservation acquire(acquire, preempt, preempt and abort).
3. reservation release(release and clear).
4. reservation report.
5. set feature and get feature of reservation notify mask.
6. get log page of reservation event.

And also make reservation configurable, one can set ns to support reservation before enable ns. The default of resv_enable is false.

---
 drivers/nvme/target/Makefile    |    2 +-
 drivers/nvme/target/admin-cmd.c |   24 +-
 drivers/nvme/target/configfs.c  |   27 +
 drivers/nvme/target/core.c      |   58 +-
 drivers/nvme/target/nvmet.h     |   49 ++
 drivers/nvme/target/pr.c        | 1214 +++++++++++++++++++++++++++++++
 include/linux/nvme.h            |   54 ++
 7 files changed, 1419 insertions(+), 9 deletions(-)
 create mode 100644 drivers/nvme/target/pr.c

...

Quoted 19 lines
drivers/nvme/target/pr.c new file +19 -0
+
+static void nvmet_pr_unregister_one(struct nvmet_pr *pr,
+                                   struct nvmet_pr_registrant *reg)
+{
+       struct nvmet_pr_registrant *first_reg;
+       struct nvmet_pr_registrant *holder;
+       u8 original_rtype;
+
+       lockdep_assert_held(&pr->pr_lock);
+       list_del_rcu(&reg->entry);
+
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));
+       if (reg != holder)
+               goto out;
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {

drivers/nvme/target/pr.c line 297

Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+               first_reg = list_first_or_null_rcu(&pr->registrant_list,
+                               struct nvmet_pr_registrant, entry);
+               if (first_reg)
+                       first_reg->rtype = original_rtype;
+               rcu_assign_pointer(pr->holder, first_reg);
+       } else {
+               rcu_assign_pointer(pr->holder, NULL);
+
+               if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY ||
+                   original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY)

copy&past error?
The second check should be against NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY?

Quoted 275 lines
drivers/nvme/target/pr.c new file +275 -0
+                       nvmet_pr_resv_released(pr, &reg->hostid);
+       }
+out:
+       synchronize_rcu();
+       kfree(reg);
+}
+
+static u16 nvmet_pr_unregister(struct nvmet_req *req,
+                              struct nvmet_pr_register_data *d,
+                              bool ignore_key)
+{
+       u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_SC_DNR;
+       struct nvmet_ctrl *ctrl = req->sq->ctrl;
+       struct nvmet_pr *pr = &req->ns->pr;
+       struct nvmet_pr_registrant *reg;
+
+       mutex_lock(&pr->pr_lock);
+       list_for_each_entry_rcu(reg, &pr->registrant_list, entry,
+                               lockdep_is_held(&pr->pr_lock)) {
+               if (uuid_equal(&reg->hostid, &ctrl->hostid)) {
+                       if (ignore_key || reg->rkey == le64_to_cpu(d->crkey)) {
+                               status = NVME_SC_SUCCESS;
+                               nvmet_pr_unregister_one(pr, reg);
+                       }
+                       break;
+               }
+       }
+       mutex_unlock(&pr->pr_lock);
+
+       return status;
+}
+
+static void nvmet_pr_update_reg_rkey(struct nvmet_pr_registrant *reg,
+                                    void *attr)
+{
+       reg->rkey = *(u64 *)attr;
+}
+
+static u16 nvmet_pr_update_reg_attr(struct nvmet_pr *pr,
+                                   struct nvmet_pr_registrant *reg,
+                                   void (*change_attr)(struct nvmet_pr_registrant *reg,
+                                       void *attr),
+                                   void *attr)
+{
+       struct nvmet_pr_registrant *holder;
+       struct nvmet_pr_registrant *new;
+
+       lockdep_assert_held(&pr->pr_lock);
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));
+       if (reg != holder) {
+               change_attr(reg, attr);
+               return NVME_SC_SUCCESS;
+       }
+
+       new = kmalloc(sizeof(*new), GFP_ATOMIC);
+       if (!new)
+               return NVME_SC_INTERNAL;
+
+       new->rkey = holder->rkey;
+       new->rtype = holder->rtype;
+       uuid_copy(&new->hostid, &holder->hostid);
+       new->cntlid = holder->cntlid;
+       INIT_LIST_HEAD(&new->entry);
+
+       change_attr(new, attr);
+       list_replace_rcu(&holder->entry, &new->entry);
+       rcu_assign_pointer(pr->holder, new);
+       synchronize_rcu();
+       kfree(holder);
+
+       return NVME_SC_SUCCESS;
+}
+
+static u16 nvmet_pr_replace(struct nvmet_req *req,
+                           struct nvmet_pr_register_data *d,
+                           bool ignore_key)
+{
+       u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_SC_DNR;
+       struct nvmet_ctrl *ctrl = req->sq->ctrl;
+       struct nvmet_pr *pr = &req->ns->pr;
+       struct nvmet_pr_registrant *reg;
+       u64 nrkey = le64_to_cpu(d->nrkey);
+
+       mutex_lock(&pr->pr_lock);
+       list_for_each_entry_rcu(reg, &pr->registrant_list, entry,
+                               lockdep_is_held(&pr->pr_lock)) {
+               if (uuid_equal(&reg->hostid, &ctrl->hostid)) {
+                       if (ignore_key || reg->rkey == le64_to_cpu(d->crkey))
+                               status = nvmet_pr_update_reg_attr(pr, reg,
+                                               nvmet_pr_update_reg_rkey,
+                                               &nrkey);
+                       break;
+               }
+       }
+       mutex_unlock(&pr->pr_lock);
+       return status;
+}
+
+static void nvmet_execute_pr_register(struct nvmet_req *req)
+{
+       u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
+       bool ignore_key = (bool)((cdw10 >> 3) & 1); /* Ignore existing key, bit 03 */
+       struct nvmet_pr_register_data *d;
+       u8 reg_act = cdw10 & 0x07; /* Reservation Register Action, bit 02:00 */
+       u16 status;
+
+       d = kmalloc(sizeof(*d), GFP_KERNEL);
+       if (!d) {
+               status = NVME_SC_INTERNAL;
+               goto out;
+       }
+
+       status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
+       if (status)
+               goto free_data;
+
+       switch (reg_act) {
+       case NVME_PR_REGISTER_ACT_REG:
+               status = nvmet_pr_register(req, d);
+               break;
+       case NVME_PR_REGISTER_ACT_UNREG:
+               status = nvmet_pr_unregister(req, d, ignore_key);
+               break;
+       case NVME_PR_REGISTER_ACT_REPLACE:
+               status = nvmet_pr_replace(req, d, ignore_key);
+               break;
+       default:
+               req->error_loc = offsetof(struct nvme_common_command, cdw10);
+               status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
+               break;
+       }
+free_data:
+       kfree(d);
+out:
+       if (!status)
+               atomic_inc(&req->ns->pr.generation);
+       nvmet_req_complete(req, status);
+}
+
+static u16 nvmet_pr_acquire(struct nvmet_req *req,
+                           struct nvmet_pr_registrant *reg,
+                           u8 rtype)
+{
+       struct nvmet_pr *pr = &req->ns->pr;
+       struct nvmet_pr_registrant *holder;
+
+       lockdep_assert_held(&pr->pr_lock);
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));
+       if (holder && reg != holder)
+               return  NVME_SC_RESERVATION_CONFLICT | NVME_SC_DNR;
+       if (holder && reg == holder) {
+               if (holder->rtype == rtype)
+                       return NVME_SC_SUCCESS;
+               return NVME_SC_RESERVATION_CONFLICT | NVME_SC_DNR;
+       }
+
+       nvmet_pr_set_new_holder(pr, rtype, reg);
+       return NVME_SC_SUCCESS;
+}
+
+static void nvmet_pr_set_ctrl_to_abort(struct nvmet_req *req, u16 cntlid)
+{
+       struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
+       struct nvmet_ctrl *ctrl = NULL;
+
+       mutex_lock(&subsys->lock);
+       list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
+               if (ctrl->cntlid == cntlid) {
+                       if (!kref_get_unless_zero(&ctrl->ref))
+                               break;
+                       ctrl->pr_abort = true;
+                       break;
+               }
+       }
+       mutex_unlock(&subsys->lock);
+}
+
+static u16 nvmet_pr_unreg_all_host_by_prkey(struct nvmet_req *req, u64 prkey,
+                                           uuid_t *send_hostid,
+                                           bool abort)
+{
+       u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_SC_DNR;
+       struct nvmet_pr_registrant *reg, *tmp;
+       struct nvmet_pr *pr = &req->ns->pr;
+       uuid_t hostid;
+
+       lockdep_assert_held(&pr->pr_lock);
+
+       list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
+               if (reg->rkey == prkey) {
+                       status = NVME_SC_SUCCESS;
+                       uuid_copy(&hostid, &reg->hostid);
+                       if (abort)
+                               nvmet_pr_set_ctrl_to_abort(req, reg->cntlid);
+                       nvmet_pr_unregister_one(pr, reg);
+                       if (!uuid_equal(&hostid, send_hostid))
+                               nvmet_pr_registration_preempted(pr, &hostid);
+               }
+       }
+       return status;
+}
+
+static u16 nvmet_pr_unreg_all_others_by_prkey(struct nvmet_req *req,
+                                             u64 prkey,
+                                             uuid_t *send_hostid,
+                                             bool abort)
+{
+       u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_SC_DNR;
+       struct nvmet_pr_registrant *reg, *tmp;
+       struct nvmet_pr *pr = &req->ns->pr;
+       uuid_t hostid;
+
+       lockdep_assert_held(&pr->pr_lock);
+
+       list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
+               if (reg->rkey == prkey &&
+                   !uuid_equal(&reg->hostid, send_hostid)) {
+                       status = NVME_SC_SUCCESS;
+                       uuid_copy(&hostid, &reg->hostid);
+                       if (abort)
+                               nvmet_pr_set_ctrl_to_abort(req, reg->cntlid);
+                       nvmet_pr_unregister_one(pr, reg);
+                       nvmet_pr_registration_preempted(pr, &hostid);
+               }
+       }
+       return status;
+}
+
+static void nvmet_pr_unreg_all_others(struct nvmet_req *req,
+                                     uuid_t *send_hostid,
+                                     bool abort)
+{
+       struct nvmet_pr_registrant *reg, *tmp;
+       struct nvmet_pr *pr = &req->ns->pr;
+       uuid_t hostid;
+
+       lockdep_assert_held(&pr->pr_lock);
+
+       list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
+               if (!uuid_equal(&reg->hostid, send_hostid)) {
+                       uuid_copy(&hostid, &reg->hostid);
+                       if (abort)
+                               nvmet_pr_set_ctrl_to_abort(req, reg->cntlid);
+                       nvmet_pr_unregister_one(pr, reg);
+                       nvmet_pr_registration_preempted(pr, &hostid);
+               }
+       }
+}
+
+static void nvmet_pr_update_holder_rtype(struct nvmet_pr_registrant *reg,
+                                        void *attr)
+{
+       u8 new_rtype = *(u8 *)attr;
+
+       reg->rtype = new_rtype;
+}
+
+static u16 nvmet_pr_preempt(struct nvmet_req *req,
+                           struct nvmet_pr_registrant *reg,
+                           u8 rtype,
+                           struct nvmet_pr_acquire_data *d,
+                           bool abort)
+{
+       struct nvmet_ctrl *ctrl = req->sq->ctrl;
+       struct nvmet_pr *pr = &req->ns->pr;
+       struct nvmet_pr_registrant *holder;
+       enum nvme_pr_type original_rtype;
+       u64 prkey = le64_to_cpu(d->prkey);
+       u16 status;
+
+       lockdep_assert_held(&pr->pr_lock);
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));

drivers/nvme/target/pr.c line 582

Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+       if (!holder)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               if (!prkey) {
+                       nvmet_pr_unreg_all_others(req, &ctrl->hostid, abort);
+                       nvmet_pr_set_new_holder(pr, rtype, reg);

You didnot address the reservation atomicity here. You still remove an old holder and then after a some time you set the new holder. In this time gap an incomming WRITE command from a non holder will write to the media.

Quoted 7 lines
drivers/nvme/target/pr.c new file +7 -0
+                       return NVME_SC_SUCCESS;
+               }
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                               &ctrl->hostid, abort);
+       }
+
+       if (holder == reg) {

drivers/nvme/target/pr.c line 599

Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+               status = nvmet_pr_update_reg_attr(pr, holder,
+                               nvmet_pr_update_holder_rtype, &rtype);
+               if (!status && original_rtype != rtype)
+                       nvmet_pr_resv_released(pr, &reg->hostid);
+               return status;
+       }
+
+       if (prkey == holder->rkey) {
+               status = nvmet_pr_unreg_all_others_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);

And here too that timegap with released reservation.

drivers/nvme/target/pr.c line 605

Quoted 6 lines
drivers/nvme/target/pr.c new file +6 -0
+               if (status)
+                       return status;
+
+               nvmet_pr_set_new_holder(pr, rtype, reg);
+               if (original_rtype != rtype)
+                       nvmet_pr_resv_released(pr, &reg->hostid);

This function is(may be) already invoked in nvmet_pr_unregister_one.

Quoted 26 lines
drivers/nvme/target/pr.c new file +26 -0
+               return status;
+       }
+
+       if (prkey)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);
+       return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
+}
+
+static void nvmet_pr_confirm_ns_pc_ref(struct percpu_ref *ref)
+{
+       struct nvmet_pr_per_ctrl_ref *pc_ref =
+               container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
+
+       complete(&pc_ref->confirm_done);
+}
+
+static void nvmet_pr_do_abort(struct nvmet_req *req)
+{
+       struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
+       struct nvmet_pr_per_ctrl_ref *pc_ref;
+       struct nvmet_ctrl *ctrl = NULL;
+
+find_next:
+       mutex_lock(&subsys->lock);
+       list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {

drivers/nvme/target/pr.c line 641

Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+               if (ctrl->pr_abort)
+                       goto do_abort;
+       }
+       mutex_unlock(&subsys->lock);
+       return;
+
+do_abort:
+       mutex_unlock(&subsys->lock);
+       /*
+        * The target dose not support abort, just wait ref to 0.

typo "dose"->"does"

drivers/nvme/target/pr.c line 651

Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+        */
+       pc_ref = xa_load(&req->ns->pr_per_ctrl_refs, ctrl->cntlid);
+       percpu_ref_kill_and_confirm(&pc_ref->ref, nvmet_pr_confirm_ns_pc_ref);
+       wait_for_completion(&pc_ref->confirm_done);
+       wait_for_completion(&pc_ref->free_done);
+       reinit_completion(&pc_ref->confirm_done);
+       reinit_completion(&pc_ref->free_done);
+       percpu_ref_resurrect(&pc_ref->ref);
+
+       ctrl->pr_abort = false;

pr_abort flag is per controller, but this function is per namespace. The use case of preempt_and_abort is to preempt reservation of a "zomby" host for the all namespaces. It means that preempt_and_abort command will be sent for the each namespace and there will be several parallel abort processes for the same controllers. And you should not mix them.

May be doing ref_kill instead of setting ctlr->pr_abort=true will more accuratelly set the barier for incoming IO. You may use Mark feature in XArrays to mark pc_ref to wait the completion for and use xa_for_each_marked for going through such pc_refs.

Quoted 36 lines
drivers/nvme/target/pr.c new file +36 -0
+       nvmet_ctrl_put(ctrl);
+       goto find_next;
+}
+
+static u16 __nvmet_execute_pr_acquire(struct nvmet_req *req,
+                                     struct nvmet_pr_registrant *reg,
+                                     u8 acquire_act,
+                                     u8 rtype,
+                                     struct nvmet_pr_acquire_data *d)
+{
+       u16 status;
+
+       switch (acquire_act) {
+       case NVME_PR_ACQUIRE_ACT_ACQUIRE:
+               status = nvmet_pr_acquire(req, reg, rtype);
+               goto out;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, false);
+               goto inc_gen;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, true);
+               if (!status)
+                       nvmet_pr_do_abort(req);
+               goto inc_gen;
+       default:
+               req->error_loc = offsetof(struct nvme_common_command, cdw10);
+               status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
+               goto out;
+       }
+inc_gen:
+       if (!status)
+               atomic_inc(&req->ns->pr.generation);
+out:
+       return status;
+}
+
author
#3 lore raw

Re: [PATCH v9 1/1] nvmet: support reservation feature

Quoted 46 lines · Dmitry Bogdanov
Quoted 22 lines

This patch implements the reservation feature, includes:
1. reservation register(register, unregister and replace).
2. reservation acquire(acquire, preempt, preempt and abort).
3. reservation release(release and clear).
4. reservation report.
5. set feature and get feature of reservation notify mask.
6. get log page of reservation event.

And also make reservation configurable, one can set ns to support reservation before enable ns. The default of resv_enable is false.

---
  drivers/nvme/target/Makefile    |    2 +-
  drivers/nvme/target/admin-cmd.c |   24 +-
  drivers/nvme/target/configfs.c  |   27 +
  drivers/nvme/target/core.c      |   58 +-
  drivers/nvme/target/nvmet.h     |   49 ++
  drivers/nvme/target/pr.c        | 1214 +++++++++++++++++++++++++++++++
  include/linux/nvme.h            |   54 ++
  7 files changed, 1419 insertions(+), 9 deletions(-)

create mode 100644 drivers/nvme/target/pr.c

...

Quoted 23 lines
drivers/nvme/target/pr.c new file +23 -0
+
+static void nvmet_pr_unregister_one(struct nvmet_pr *pr,
+                                   struct nvmet_pr_registrant *reg)
+{
+       struct nvmet_pr_registrant *first_reg;
+       struct nvmet_pr_registrant *holder;
+       u8 original_rtype;
+
+       lockdep_assert_held(&pr->pr_lock);
+       list_del_rcu(&reg->entry);
+
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));
+       if (reg != holder)
+               goto out;
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               first_reg = list_first_or_null_rcu(&pr->registrant_list,
+                               struct nvmet_pr_registrant, entry);
+               if (first_reg)
+                       first_reg->rtype = original_rtype;
Quoted 8 lines

drivers/nvme/target/pr.c line 297

Quoted 6 lines
drivers/nvme/target/pr.c new file +6 -0
+               rcu_assign_pointer(pr->holder, first_reg);
+       } else {
+               rcu_assign_pointer(pr->holder, NULL);
+
+               if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY ||
+                   original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY)

copy&past error?
The second check should be against NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY?

Indeed, my mistake, it will be fixed in v10.

Quoted 31 lines
Quoted 31 lines
drivers/nvme/target/pr.c new file +30 -0
+                       nvmet_pr_resv_released(pr, &reg->hostid);
+       }
+out:
+       synchronize_rcu();
+       kfree(reg);
+}
+
......
+
+static u16 nvmet_pr_preempt(struct nvmet_req *req,
+                           struct nvmet_pr_registrant *reg,
+                           u8 rtype,
+                           struct nvmet_pr_acquire_data *d,
+                           bool abort)
+{
+       struct nvmet_ctrl *ctrl = req->sq->ctrl;
+       struct nvmet_pr *pr = &req->ns->pr;
+       struct nvmet_pr_registrant *holder;
+       enum nvme_pr_type original_rtype;
+       u64 prkey = le64_to_cpu(d->prkey);
+       u16 status;
+
+       lockdep_assert_held(&pr->pr_lock);
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));
+       if (!holder)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
Quoted 8 lines

drivers/nvme/target/pr.c line 582

Quoted 4 lines
drivers/nvme/target/pr.c new file +4 -0
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               if (!prkey) {
+                       nvmet_pr_unreg_all_others(req, &ctrl->hostid, abort);
+                       nvmet_pr_set_new_holder(pr, rtype, reg);

You didnot address the reservation atomicity here. You still remove an old holder and then after a some time you set the new holder. In this time gap an incomming WRITE command from a non holder will write to the media.

Right, I will change it in v10.

Quoted 10 lines
Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+                       return NVME_SC_SUCCESS;
+               }
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                               &ctrl->hostid, abort);
+       }
+
+       if (holder == reg) {
+               status = nvmet_pr_update_reg_attr(pr, holder,
+                               nvmet_pr_update_holder_rtype, &rtype);
+               if (!status && original_rtype != rtype)
Quoted 8 lines

drivers/nvme/target/pr.c line 599

Quoted 7 lines
drivers/nvme/target/pr.c new file +7 -0
+                       nvmet_pr_resv_released(pr, &reg->hostid);
+               return status;
+       }
+
+       if (prkey == holder->rkey) {
+               status = nvmet_pr_unreg_all_others_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);

And here too that timegap with released reservation.

I will fix this too, and also the

nvmet_pr_unreg_all_others_by_prkey() does not need a return value,

I remove it.

Quoted 8 lines

drivers/nvme/target/pr.c line 605

Quoted 6 lines
drivers/nvme/target/pr.c new file +6 -0
+               if (status)
+                       return status;
+
+               nvmet_pr_set_new_holder(pr, rtype, reg);
+               if (original_rtype != rtype)
+                       nvmet_pr_resv_released(pr, &reg->hostid);

This function is(may be) already invoked in nvmet_pr_unregister_one.

Not, here the original_rtype is not NVME_PR_WRITE_EXCLUSIVE_REG_ONLY or

NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS, so the nvmet_pr_unregister_one will

not call nvmet_pr_resv_released.

Quoted 29 lines
Quoted 29 lines
drivers/nvme/target/pr.c new file +29 -0
+               return status;
+       }
+
+       if (prkey)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);
+       return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
+}
+
+static void nvmet_pr_confirm_ns_pc_ref(struct percpu_ref *ref)
+{
+       struct nvmet_pr_per_ctrl_ref *pc_ref =
+               container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
+
+       complete(&pc_ref->confirm_done);
+}
+
+static void nvmet_pr_do_abort(struct nvmet_req *req)
+{
+       struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
+       struct nvmet_pr_per_ctrl_ref *pc_ref;
+       struct nvmet_ctrl *ctrl = NULL;
+
+find_next:
+       mutex_lock(&subsys->lock);
+       list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
+               if (ctrl->pr_abort)
+                       goto do_abort;
+       }
Quoted 9 lines

drivers/nvme/target/pr.c line 641

Quoted 7 lines
drivers/nvme/target/pr.c new file +7 -0
+       mutex_unlock(&subsys->lock);
+       return;
+
+do_abort:
+       mutex_unlock(&subsys->lock);
+       /*
+        * The target dose not support abort, just wait ref to 0.

typo "dose"->"does"

Sure.

Quoted 16 lines

drivers/nvme/target/pr.c line 651

Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+        */
+       pc_ref = xa_load(&req->ns->pr_per_ctrl_refs, ctrl->cntlid);
+       percpu_ref_kill_and_confirm(&pc_ref->ref, nvmet_pr_confirm_ns_pc_ref);
+       wait_for_completion(&pc_ref->confirm_done);
+       wait_for_completion(&pc_ref->free_done);
+       reinit_completion(&pc_ref->confirm_done);
+       reinit_completion(&pc_ref->free_done);
+       percpu_ref_resurrect(&pc_ref->ref);
+
+       ctrl->pr_abort = false;

pr_abort flag is per controller, but this function is per namespace. The use case of preempt_and_abort is to preempt reservation of a "zomby" host for the all namespaces. It means that preempt_and_abort command will be sent for the each namespace and there will be several parallel abort processes for the same controllers. And you should not mix them.

Quoted 4 lines

May be doing ref_kill instead of setting ctlr->pr_abort=true will more accuratelly set the barier for incoming IO. You may use Mark feature in XArrays to mark pc_ref to wait the completion for and use xa_for_each_marked for going through such pc_refs.

Good idea, thanks very much, will changed in v10.

Best Regards,

Guixin Liu

Quoted 36 lines
Quoted 36 lines
drivers/nvme/target/pr.c new file +36 -0
+       nvmet_ctrl_put(ctrl);
+       goto find_next;
+}
+
+static u16 __nvmet_execute_pr_acquire(struct nvmet_req *req,
+                                     struct nvmet_pr_registrant *reg,
+                                     u8 acquire_act,
+                                     u8 rtype,
+                                     struct nvmet_pr_acquire_data *d)
+{
+       u16 status;
+
+       switch (acquire_act) {
+       case NVME_PR_ACQUIRE_ACT_ACQUIRE:
+               status = nvmet_pr_acquire(req, reg, rtype);
+               goto out;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, false);
+               goto inc_gen;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, true);
+               if (!status)
+                       nvmet_pr_do_abort(req);
+               goto inc_gen;
+       default:
+               req->error_loc = offsetof(struct nvme_common_command, cdw10);
+               status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
+               goto out;
+       }
+inc_gen:
+       if (!status)
+               atomic_inc(&req->ns->pr.generation);
+out:
+       return status;
+}
+
#4 lore raw

Re: [PATCH v9 1/1] nvmet: support reservation feature

Quoted 127 lines · Guixin Liu
Quoted 46 lines · Dmitry Bogdanov
Quoted 22 lines

This patch implements the reservation feature, includes:
1. reservation register(register, unregister and replace).
2. reservation acquire(acquire, preempt, preempt and abort).
3. reservation release(release and clear).
4. reservation report.
5. set feature and get feature of reservation notify mask.
6. get log page of reservation event.

And also make reservation configurable, one can set ns to support reservation before enable ns. The default of resv_enable is false.

---
  drivers/nvme/target/Makefile    |    2 +-
  drivers/nvme/target/admin-cmd.c |   24 +-
  drivers/nvme/target/configfs.c  |   27 +
  drivers/nvme/target/core.c      |   58 +-
  drivers/nvme/target/nvmet.h     |   49 ++
  drivers/nvme/target/pr.c        | 1214 +++++++++++++++++++++++++++++++
  include/linux/nvme.h            |   54 ++
  7 files changed, 1419 insertions(+), 9 deletions(-)

create mode 100644 drivers/nvme/target/pr.c

...

Quoted 23 lines
drivers/nvme/target/pr.c new file +23 -0
+
+static void nvmet_pr_unregister_one(struct nvmet_pr *pr,
+                                   struct nvmet_pr_registrant *reg)
+{
+       struct nvmet_pr_registrant *first_reg;
+       struct nvmet_pr_registrant *holder;
+       u8 original_rtype;
+
+       lockdep_assert_held(&pr->pr_lock);
+       list_del_rcu(&reg->entry);
+
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));
+       if (reg != holder)
+               goto out;
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               first_reg = list_first_or_null_rcu(&pr->registrant_list,
+                               struct nvmet_pr_registrant, entry);
+               if (first_reg)
+                       first_reg->rtype = original_rtype;
Quoted 8 lines

drivers/nvme/target/pr.c line 297

Quoted 6 lines
drivers/nvme/target/pr.c new file +6 -0
+               rcu_assign_pointer(pr->holder, first_reg);
+       } else {
+               rcu_assign_pointer(pr->holder, NULL);
+
+               if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY ||
+                   original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY)

copy&past error?
The second check should be against NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY?

Indeed, my mistake, it will be fixed in v10.

Quoted 31 lines
Quoted 31 lines
drivers/nvme/target/pr.c new file +30 -0
+                       nvmet_pr_resv_released(pr, &reg->hostid);
+       }
+out:
+       synchronize_rcu();
+       kfree(reg);
+}
+
......
+
+static u16 nvmet_pr_preempt(struct nvmet_req *req,
+                           struct nvmet_pr_registrant *reg,
+                           u8 rtype,
+                           struct nvmet_pr_acquire_data *d,
+                           bool abort)
+{
+       struct nvmet_ctrl *ctrl = req->sq->ctrl;
+       struct nvmet_pr *pr = &req->ns->pr;
+       struct nvmet_pr_registrant *holder;
+       enum nvme_pr_type original_rtype;
+       u64 prkey = le64_to_cpu(d->prkey);
+       u16 status;
+
+       lockdep_assert_held(&pr->pr_lock);
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));
+       if (!holder)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
Quoted 8 lines

drivers/nvme/target/pr.c line 582

Quoted 4 lines
drivers/nvme/target/pr.c new file +4 -0
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               if (!prkey) {
+                       nvmet_pr_unreg_all_others(req, &ctrl->hostid, abort);
+                       nvmet_pr_set_new_holder(pr, rtype, reg);

You didnot address the reservation atomicity here. You still remove an old holder and then after a some time you set the new holder. In this time gap an incomming WRITE command from a non holder will write to the media.

Right, I will change it in v10.

Quoted 10 lines
Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+                       return NVME_SC_SUCCESS;
+               }
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                               &ctrl->hostid, abort);
+       }
+
+       if (holder == reg) {
+               status = nvmet_pr_update_reg_attr(pr, holder,
+                               nvmet_pr_update_holder_rtype, &rtype);
+               if (!status && original_rtype != rtype)
Quoted 8 lines

drivers/nvme/target/pr.c line 599

Quoted 7 lines
drivers/nvme/target/pr.c new file +7 -0
+                       nvmet_pr_resv_released(pr, &reg->hostid);
+               return status;
+       }
+
+       if (prkey == holder->rkey) {
+               status = nvmet_pr_unreg_all_others_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);

And here too that timegap with released reservation.

I will fix this too, and also the

nvmet_pr_unreg_all_others_by_prkey() does not need a return value,

I remove it.

Quoted 6 lines
Quoted 6 lines
drivers/nvme/target/pr.c new file +6 -0
+               if (status)
+                       return status;
+
+               nvmet_pr_set_new_holder(pr, rtype, reg);
+               if (original_rtype != rtype)
+                       nvmet_pr_resv_released(pr, &reg->hostid);
Quoted 5 lines
Quoted 2 lines

This function is(may be) already invoked in nvmet_pr_unregister_one.

Not, here the original_rtype is not NVME_PR_WRITE_EXCLUSIVE_REG_ONLY or NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS, so the nvmet_pr_unregister_one will not call nvmet_pr_resv_released.

nvmet_pr_unregister_one sends an event for original_rtype == *_REG_ONLY Here you have only original_rtype != ALL_REGS, so _REG_ONLY is possible.

Quoted 102 lines
Quoted 29 lines
Quoted 29 lines
drivers/nvme/target/pr.c new file +29 -0
+               return status;
+       }
+
+       if (prkey)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);
+       return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
+}
+
+static void nvmet_pr_confirm_ns_pc_ref(struct percpu_ref *ref)
+{
+       struct nvmet_pr_per_ctrl_ref *pc_ref =
+               container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
+
+       complete(&pc_ref->confirm_done);
+}
+
+static void nvmet_pr_do_abort(struct nvmet_req *req)
+{
+       struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
+       struct nvmet_pr_per_ctrl_ref *pc_ref;
+       struct nvmet_ctrl *ctrl = NULL;
+
+find_next:
+       mutex_lock(&subsys->lock);
+       list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
+               if (ctrl->pr_abort)
+                       goto do_abort;
+       }
Quoted 9 lines

drivers/nvme/target/pr.c line 641

Quoted 7 lines
drivers/nvme/target/pr.c new file +7 -0
+       mutex_unlock(&subsys->lock);
+       return;
+
+do_abort:
+       mutex_unlock(&subsys->lock);
+       /*
+        * The target dose not support abort, just wait ref to 0.

typo "dose"->"does"

Sure.

Quoted 16 lines

drivers/nvme/target/pr.c line 651

Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+        */
+       pc_ref = xa_load(&req->ns->pr_per_ctrl_refs, ctrl->cntlid);
+       percpu_ref_kill_and_confirm(&pc_ref->ref, nvmet_pr_confirm_ns_pc_ref);
+       wait_for_completion(&pc_ref->confirm_done);
+       wait_for_completion(&pc_ref->free_done);
+       reinit_completion(&pc_ref->confirm_done);
+       reinit_completion(&pc_ref->free_done);
+       percpu_ref_resurrect(&pc_ref->ref);
+
+       ctrl->pr_abort = false;

pr_abort flag is per controller, but this function is per namespace. The use case of preempt_and_abort is to preempt reservation of a "zomby" host for the all namespaces. It means that preempt_and_abort command will be sent for the each namespace and there will be several parallel abort processes for the same controllers. And you should not mix them.

Quoted 4 lines

May be doing ref_kill instead of setting ctlr->pr_abort=true will more accuratelly set the barier for incoming IO. You may use Mark feature in XArrays to mark pc_ref to wait the completion for and use xa_for_each_marked for going through such pc_refs.

Good idea, thanks very much, will changed in v10.

Best Regards,

Guixin Liu

Quoted 36 lines
Quoted 36 lines
drivers/nvme/target/pr.c new file +36 -0
+       nvmet_ctrl_put(ctrl);
+       goto find_next;
+}
+
+static u16 __nvmet_execute_pr_acquire(struct nvmet_req *req,
+                                     struct nvmet_pr_registrant *reg,
+                                     u8 acquire_act,
+                                     u8 rtype,
+                                     struct nvmet_pr_acquire_data *d)
+{
+       u16 status;
+
+       switch (acquire_act) {
+       case NVME_PR_ACQUIRE_ACT_ACQUIRE:
+               status = nvmet_pr_acquire(req, reg, rtype);
+               goto out;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, false);
+               goto inc_gen;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, true);
+               if (!status)
+                       nvmet_pr_do_abort(req);
+               goto inc_gen;
+       default:
+               req->error_loc = offsetof(struct nvme_common_command, cdw10);
+               status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
+               goto out;
+       }
+inc_gen:
+       if (!status)
+               atomic_inc(&req->ns->pr.generation);
+out:
+       return status;
+}
+
author
#5 lore raw

Re: [PATCH v9 1/1] nvmet: support reservation feature

Quoted 127 lines · Dmitry Bogdanov
Quoted 126 lines · Guixin Liu
Quoted 46 lines · Dmitry Bogdanov
Quoted 22 lines

This patch implements the reservation feature, includes:
1. reservation register(register, unregister and replace).
2. reservation acquire(acquire, preempt, preempt and abort).
3. reservation release(release and clear).
4. reservation report.
5. set feature and get feature of reservation notify mask.
6. get log page of reservation event.

And also make reservation configurable, one can set ns to support reservation before enable ns. The default of resv_enable is false.

---
   drivers/nvme/target/Makefile    |    2 +-
   drivers/nvme/target/admin-cmd.c |   24 +-
   drivers/nvme/target/configfs.c  |   27 +
   drivers/nvme/target/core.c      |   58 +-
   drivers/nvme/target/nvmet.h     |   49 ++
   drivers/nvme/target/pr.c        | 1214 +++++++++++++++++++++++++++++++
   include/linux/nvme.h            |   54 ++
   7 files changed, 1419 insertions(+), 9 deletions(-)

create mode 100644 drivers/nvme/target/pr.c

...

Quoted 23 lines
drivers/nvme/target/pr.c new file +23 -0
+
+static void nvmet_pr_unregister_one(struct nvmet_pr *pr,
+                                   struct nvmet_pr_registrant *reg)
+{
+       struct nvmet_pr_registrant *first_reg;
+       struct nvmet_pr_registrant *holder;
+       u8 original_rtype;
+
+       lockdep_assert_held(&pr->pr_lock);
+       list_del_rcu(&reg->entry);
+
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));
+       if (reg != holder)
+               goto out;
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               first_reg = list_first_or_null_rcu(&pr->registrant_list,
+                               struct nvmet_pr_registrant, entry);
+               if (first_reg)
+                       first_reg->rtype = original_rtype;
Quoted 8 lines

drivers/nvme/target/pr.c line 297

Quoted 6 lines
drivers/nvme/target/pr.c new file +6 -0
+               rcu_assign_pointer(pr->holder, first_reg);
+       } else {
+               rcu_assign_pointer(pr->holder, NULL);
+
+               if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY ||
+                   original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY)

copy&past error?
The second check should be against NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY?

Indeed, my mistake, it will be fixed in v10.

Quoted 31 lines
Quoted 31 lines
drivers/nvme/target/pr.c new file +30 -0
+                       nvmet_pr_resv_released(pr, &reg->hostid);
+       }
+out:
+       synchronize_rcu();
+       kfree(reg);
+}
+
......
+
+static u16 nvmet_pr_preempt(struct nvmet_req *req,
+                           struct nvmet_pr_registrant *reg,
+                           u8 rtype,
+                           struct nvmet_pr_acquire_data *d,
+                           bool abort)
+{
+       struct nvmet_ctrl *ctrl = req->sq->ctrl;
+       struct nvmet_pr *pr = &req->ns->pr;
+       struct nvmet_pr_registrant *holder;
+       enum nvme_pr_type original_rtype;
+       u64 prkey = le64_to_cpu(d->prkey);
+       u16 status;
+
+       lockdep_assert_held(&pr->pr_lock);
+       holder = rcu_dereference_protected(pr->holder,
+                       lockdep_is_held(&pr->pr_lock));
+       if (!holder)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
Quoted 8 lines

drivers/nvme/target/pr.c line 582

Quoted 4 lines
drivers/nvme/target/pr.c new file +4 -0
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               if (!prkey) {
+                       nvmet_pr_unreg_all_others(req, &ctrl->hostid, abort);
+                       nvmet_pr_set_new_holder(pr, rtype, reg);

You didnot address the reservation atomicity here. You still remove an old holder and then after a some time you set the new holder. In this time gap an incomming WRITE command from a non holder will write to the media.

Right, I will change it in v10.

Quoted 10 lines
Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+                       return NVME_SC_SUCCESS;
+               }
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                               &ctrl->hostid, abort);
+       }
+
+       if (holder == reg) {
+               status = nvmet_pr_update_reg_attr(pr, holder,
+                               nvmet_pr_update_holder_rtype, &rtype);
+               if (!status && original_rtype != rtype)
Quoted 8 lines

drivers/nvme/target/pr.c line 599

Quoted 7 lines
drivers/nvme/target/pr.c new file +7 -0
+                       nvmet_pr_resv_released(pr, &reg->hostid);
+               return status;
+       }
+
+       if (prkey == holder->rkey) {
+               status = nvmet_pr_unreg_all_others_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);

And here too that timegap with released reservation.

I will fix this too, and also the

nvmet_pr_unreg_all_others_by_prkey() does not need a return value,

I remove it.

Quoted 6 lines
Quoted 6 lines
drivers/nvme/target/pr.c new file +6 -0
+               if (status)
+                       return status;
+
+               nvmet_pr_set_new_holder(pr, rtype, reg);
+               if (original_rtype != rtype)
+                       nvmet_pr_resv_released(pr, &reg->hostid);
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This function is(may be) already invoked in nvmet_pr_unregister_one.

Not, here the original_rtype is not NVME_PR_WRITE_EXCLUSIVE_REG_ONLY or NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS, so the nvmet_pr_unregister_one will not call nvmet_pr_resv_released.

nvmet_pr_unregister_one sends an event for original_rtype == *_REG_ONLY Here you have only original_rtype != ALL_REGS, so _REG_ONLY is possible.

My mistake, it will be removed in v10, thanks.

Quoted 101 lines
Quoted 101 lines
Quoted 29 lines
Quoted 29 lines
drivers/nvme/target/pr.c new file +29 -0
+               return status;
+       }
+
+       if (prkey)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                                       &ctrl->hostid, abort);
+       return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
+}
+
+static void nvmet_pr_confirm_ns_pc_ref(struct percpu_ref *ref)
+{
+       struct nvmet_pr_per_ctrl_ref *pc_ref =
+               container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
+
+       complete(&pc_ref->confirm_done);
+}
+
+static void nvmet_pr_do_abort(struct nvmet_req *req)
+{
+       struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
+       struct nvmet_pr_per_ctrl_ref *pc_ref;
+       struct nvmet_ctrl *ctrl = NULL;
+
+find_next:
+       mutex_lock(&subsys->lock);
+       list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
+               if (ctrl->pr_abort)
+                       goto do_abort;
+       }
Quoted 9 lines

drivers/nvme/target/pr.c line 641

Quoted 7 lines
drivers/nvme/target/pr.c new file +7 -0
+       mutex_unlock(&subsys->lock);
+       return;
+
+do_abort:
+       mutex_unlock(&subsys->lock);
+       /*
+        * The target dose not support abort, just wait ref to 0.

typo "dose"->"does"

Sure.

Quoted 16 lines

drivers/nvme/target/pr.c line 651

Quoted 10 lines
drivers/nvme/target/pr.c new file +10 -0
+        */
+       pc_ref = xa_load(&req->ns->pr_per_ctrl_refs, ctrl->cntlid);
+       percpu_ref_kill_and_confirm(&pc_ref->ref, nvmet_pr_confirm_ns_pc_ref);
+       wait_for_completion(&pc_ref->confirm_done);
+       wait_for_completion(&pc_ref->free_done);
+       reinit_completion(&pc_ref->confirm_done);
+       reinit_completion(&pc_ref->free_done);
+       percpu_ref_resurrect(&pc_ref->ref);
+
+       ctrl->pr_abort = false;

pr_abort flag is per controller, but this function is per namespace. The use case of preempt_and_abort is to preempt reservation of a "zomby" host for the all namespaces. It means that preempt_and_abort command will be sent for the each namespace and there will be several parallel abort processes for the same controllers. And you should not mix them.

Quoted 4 lines

May be doing ref_kill instead of setting ctlr->pr_abort=true will more accuratelly set the barier for incoming IO. You may use Mark feature in XArrays to mark pc_ref to wait the completion for and use xa_for_each_marked for going through such pc_refs.

Good idea, thanks very much, will changed in v10.

Best Regards,

Guixin Liu

Quoted 36 lines
Quoted 36 lines
drivers/nvme/target/pr.c new file +36 -0
+       nvmet_ctrl_put(ctrl);
+       goto find_next;
+}
+
+static u16 __nvmet_execute_pr_acquire(struct nvmet_req *req,
+                                     struct nvmet_pr_registrant *reg,
+                                     u8 acquire_act,
+                                     u8 rtype,
+                                     struct nvmet_pr_acquire_data *d)
+{
+       u16 status;
+
+       switch (acquire_act) {
+       case NVME_PR_ACQUIRE_ACT_ACQUIRE:
+               status = nvmet_pr_acquire(req, reg, rtype);
+               goto out;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, false);
+               goto inc_gen;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, true);
+               if (!status)
+                       nvmet_pr_do_abort(req);
+               goto inc_gen;
+       default:
+               req->error_loc = offsetof(struct nvme_common_command, cdw10);
+               status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
+               goto out;
+       }
+inc_gen:
+       if (!status)
+               atomic_inc(&req->ns->pr.generation);
+out:
+       return status;
+}
+
author
#6 lore raw

Re: [PATCH v9 1/1] nvmet: support reservation feature

Quoted 135 lines · Guixin Liu
Quoted 133 lines · Dmitry Bogdanov
Quoted 132 lines · Guixin Liu
Quoted 50 lines · Dmitry Bogdanov
Quoted 23 lines

This patch implements the reservation feature, includes:
1. reservation register(register, unregister and replace).
2. reservation acquire(acquire, preempt, preempt and abort).
3. reservation release(release and clear).
4. reservation report.
5. set feature and get feature of reservation notify mask.
6. get log page of reservation event.

And also make reservation configurable, one can set ns to support reservation before enable ns. The default of resv_enable is false.

---
   drivers/nvme/target/Makefile    |    2 +-
   drivers/nvme/target/admin-cmd.c |   24 +-
   drivers/nvme/target/configfs.c  |   27 +
   drivers/nvme/target/core.c      |   58 +-
   drivers/nvme/target/nvmet.h     |   49 ++
   drivers/nvme/target/pr.c        | 1214
+++++++++++++++++++++++++++++++
   include/linux/nvme.h            |   54 ++

   7 files changed, 1419 insertions(+), 9 deletions(-)

   create mode 100644 drivers/nvme/target/pr.c

...

Quoted 26 lines
drivers/nvme/target/pr.c new file +20 -0
+
+static void nvmet_pr_unregister_one(struct nvmet_pr *pr,
+                                   struct nvmet_pr_registrant *reg)
+{
+       struct nvmet_pr_registrant *first_reg;
+       struct nvmet_pr_registrant *holder;
+       u8 original_rtype;
+
+       lockdep_assert_held(&pr->pr_lock);
+       list_del_rcu(&reg->entry);
+
+       holder = rcu_dereference_protected(pr->holder,
+ lockdep_is_held(&pr->pr_lock));
+       if (reg != holder)
+               goto out;
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               first_reg = 

list_first_or_null_rcu(&pr->registrant_list,
+                               struct nvmet_pr_registrant, entry); +               if (first_reg)
+                       first_reg->rtype = original_rtype; +               rcu_assign_pointer(pr->holder, first_reg); +       } else {

Quoted 8 lines
Quoted 6 lines

+               rcu_assign_pointer(pr->holder, NULL); +
+               if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY ||
+                   original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY)

copy&past error?
The second check should be against NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY?

Indeed, my mistake, it will be fixed in v10.

Quoted 32 lines
Quoted 32 lines

+ nvmet_pr_resv_released(pr, &reg->hostid);
+       }

drivers/nvme/target/pr.c new file +29 -0
+out:
+       synchronize_rcu();
+       kfree(reg);
+}
+
......
+
+static u16 nvmet_pr_preempt(struct nvmet_req *req,
+                           struct nvmet_pr_registrant *reg,
+                           u8 rtype,
+                           struct nvmet_pr_acquire_data *d,
+                           bool abort)
+{
+       struct nvmet_ctrl *ctrl = req->sq->ctrl;
+       struct nvmet_pr *pr = &req->ns->pr;
+       struct nvmet_pr_registrant *holder;
+       enum nvme_pr_type original_rtype;
+       u64 prkey = le64_to_cpu(d->prkey);
+       u16 status;
+
+       lockdep_assert_held(&pr->pr_lock);
+       holder = rcu_dereference_protected(pr->holder,
+ lockdep_is_held(&pr->pr_lock));
+       if (!holder)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+ &ctrl->hostid, abort);
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
Quoted 8 lines
Quoted 4 lines

+               if (!prkey) {
+                       nvmet_pr_unreg_all_others(req, &ctrl->hostid, abort);
+                       nvmet_pr_set_new_holder(pr, rtype, reg);

You didnot address the reservation atomicity here. You still remove an old holder and then after a some time you set the new holder. In this time gap an incomming WRITE command from a non holder will write to the media.

Right, I will change it in v10.

Quoted 11 lines
Quoted 11 lines

+                       return NVME_SC_SUCCESS;
+               }
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey, +                               &ctrl->hostid, abort);
+       }
+
+       if (holder == reg) {
+               status = nvmet_pr_update_reg_attr(pr, holder,
+ nvmet_pr_update_holder_rtype, &rtype);
+               if (!status && original_rtype != rtype) +                       nvmet_pr_resv_released(pr, &reg->hostid);

Quoted 8 lines
Quoted 7 lines

+               return status;
+       }
+
+       if (prkey == holder->rkey) {
+               status = nvmet_pr_unreg_all_others_by_prkey(req, prkey,
+ &ctrl->hostid, abort);

And here too that timegap with released reservation.

I will fix this too, and also the

nvmet_pr_unreg_all_others_by_prkey() does not need a return value,

I remove it.

Quoted 6 lines
Quoted 6 lines

+               if (status)
+                       return status;
+
+               nvmet_pr_set_new_holder(pr, rtype, reg); +               if (original_rtype != rtype)
+                       nvmet_pr_resv_released(pr, &reg->hostid);

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This function is(may be) already invoked in nvmet_pr_unregister_one.

Not, here the original_rtype is not NVME_PR_WRITE_EXCLUSIVE_REG_ONLY or NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS, so the nvmet_pr_unregister_one will not call nvmet_pr_resv_released.

nvmet_pr_unregister_one sends an event for original_rtype == *_REG_ONLY Here you have only original_rtype != ALL_REGS, so _REG_ONLY is possible.

My mistake, it will be removed in v10, thanks.

I take a look again, if we set self new holder before call nvmet_pr_unreg_all_others_by_prkey(), the nvmet_pr_unreg_all_others_by_prkey() will

not unregister self, so this will not goto nvmet_pr_unregister_one()'s calling nvmet_pr_resv_released().

Quoted 105 lines
Quoted 105 lines
Quoted 105 lines
Quoted 29 lines
Quoted 29 lines

+               return status;
+       }

drivers/nvme/target/pr.c new file +27 -0
+
+       if (prkey)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+ &ctrl->hostid, abort);
+       return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
+}
+
+static void nvmet_pr_confirm_ns_pc_ref(struct percpu_ref *ref)
+{
+       struct nvmet_pr_per_ctrl_ref *pc_ref =
+               container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
+
+       complete(&pc_ref->confirm_done);
+}
+
+static void nvmet_pr_do_abort(struct nvmet_req *req)
+{
+       struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
+       struct nvmet_pr_per_ctrl_ref *pc_ref;
+       struct nvmet_ctrl *ctrl = NULL;
+
+find_next:
+       mutex_lock(&subsys->lock);
+       list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
+               if (ctrl->pr_abort)
+                       goto do_abort;
+       }
Quoted 9 lines

drivers/nvme/target/pr.c line 641

Quoted 7 lines

+       mutex_unlock(&subsys->lock);
+       return;

drivers/nvme/target/pr.c new file +5 -0
+
+do_abort:
+       mutex_unlock(&subsys->lock);
+       /*
+        * The target dose not support abort, just wait ref to 0.

typo "dose"->"does"

Sure.

Quoted 17 lines
Quoted 11 lines

+        */
+       pc_ref = xa_load(&req->ns->pr_per_ctrl_refs, ctrl->cntlid); +       percpu_ref_kill_and_confirm(&pc_ref->ref, nvmet_pr_confirm_ns_pc_ref);
+       wait_for_completion(&pc_ref->confirm_done);
+       wait_for_completion(&pc_ref->free_done);
+       reinit_completion(&pc_ref->confirm_done);
+       reinit_completion(&pc_ref->free_done);
+       percpu_ref_resurrect(&pc_ref->ref);
+
+       ctrl->pr_abort = false;

pr_abort flag is per controller, but this function is per namespace. The use case of preempt_and_abort is to preempt reservation of a "zomby" host for the all namespaces. It means that preempt_and_abort command will be sent for the each namespace and there will be several parallel abort processes for the same controllers. And you should not mix them.

Quoted 4 lines

May be doing ref_kill instead of setting ctlr->pr_abort=true will more accuratelly set the barier for incoming IO. You may use Mark feature in XArrays to mark pc_ref to wait the completion for and use xa_for_each_marked for going through such pc_refs.

Good idea, thanks very much, will changed in v10.

Best Regards,

Guixin Liu

Quoted 39 lines
Quoted 39 lines

+       nvmet_ctrl_put(ctrl);
+       goto find_next;

drivers/nvme/target/pr.c new file +4 -0
+}
+
+static u16 __nvmet_execute_pr_acquire(struct nvmet_req *req,
+                                     struct nvmet_pr_registrant 

*reg,
+                                     u8 acquire_act,
+                                     u8 rtype,
+                                     struct nvmet_pr_acquire_data *d)

drivers/nvme/target/pr.c new file +17 -0
+{
+       u16 status;
+
+       switch (acquire_act) {
+       case NVME_PR_ACQUIRE_ACT_ACQUIRE:
+               status = nvmet_pr_acquire(req, reg, rtype);
+               goto out;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, false);
+               goto inc_gen;
+       case NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT:
+               status = nvmet_pr_preempt(req, reg, rtype, d, true);
+               if (!status)
+                       nvmet_pr_do_abort(req);
+               goto inc_gen;
+       default:
+               req->error_loc = offsetof(struct 

nvme_common_command, cdw10);
+               status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR; +               goto out;
+       }

drivers/nvme/target/pr.c new file +7 -0
+inc_gen:
+       if (!status)
+ atomic_inc(&req->ns->pr.generation);
+out:
+       return status;
+}
+
#7 lore raw

Re: [PATCH v9 1/1] nvmet: support reservation feature

Quoted 145 lines · Guixin Liu
Quoted 143 lines · Guixin Liu
Quoted 141 lines · Dmitry Bogdanov
Quoted 133 lines · Guixin Liu
Quoted 50 lines · Dmitry Bogdanov
Quoted 23 lines

This patch implements the reservation feature, includes:
1. reservation register(register, unregister and replace).
2. reservation acquire(acquire, preempt, preempt and abort).
3. reservation release(release and clear).
4. reservation report.
5. set feature and get feature of reservation notify mask.
6. get log page of reservation event.

And also make reservation configurable, one can set ns to support reservation before enable ns. The default of resv_enable is false.

---
   drivers/nvme/target/Makefile    |    2 +-
   drivers/nvme/target/admin-cmd.c |   24 +-
   drivers/nvme/target/configfs.c  |   27 +
   drivers/nvme/target/core.c      |   58 +-
   drivers/nvme/target/nvmet.h     |   49 ++
   drivers/nvme/target/pr.c        | 1214
+++++++++++++++++++++++++++++++
   include/linux/nvme.h            |   54 ++
   7 files changed, 1419 insertions(+), 9 deletions(-)

create mode 100644 drivers/nvme/target/pr.c

...

Quoted 26 lines
drivers/nvme/target/pr.c new file +20 -0
+
+static void nvmet_pr_unregister_one(struct nvmet_pr *pr,
+                                   struct nvmet_pr_registrant *reg)
+{
+       struct nvmet_pr_registrant *first_reg;
+       struct nvmet_pr_registrant *holder;
+       u8 original_rtype;
+
+       lockdep_assert_held(&pr->pr_lock);
+       list_del_rcu(&reg->entry);
+
+       holder = rcu_dereference_protected(pr->holder,
+ lockdep_is_held(&pr->pr_lock));
+       if (reg != holder)
+               goto out;
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               first_reg =

list_first_or_null_rcu(&pr->registrant_list,

drivers/nvme/target/pr.c new file +5 -0
+                               struct nvmet_pr_registrant, entry);
+               if (first_reg)
+                       first_reg->rtype = original_rtype;
+               rcu_assign_pointer(pr->holder, first_reg);
+       } else {
Quoted 8 lines
Quoted 6 lines
drivers/nvme/target/pr.c new file +3 -0
+               rcu_assign_pointer(pr->holder, NULL);
+
+               if (original_rtype ==

NVME_PR_WRITE_EXCLUSIVE_REG_ONLY ||
+ original_rtype ==
NVME_PR_WRITE_EXCLUSIVE_REG_ONLY)

copy&past error?
The second check should be against NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY?

Indeed, my mistake, it will be fixed in v10.

Quoted 33 lines
Quoted 33 lines

+ nvmet_pr_resv_released(pr, &reg->hostid);

drivers/nvme/target/pr.c new file +31 -0
+       }
+out:
+       synchronize_rcu();
+       kfree(reg);
+}
+
......
+
+static u16 nvmet_pr_preempt(struct nvmet_req *req,
+                           struct nvmet_pr_registrant *reg,
+                           u8 rtype,
+                           struct nvmet_pr_acquire_data *d,
+                           bool abort)
+{
+       struct nvmet_ctrl *ctrl = req->sq->ctrl;
+       struct nvmet_pr *pr = &req->ns->pr;
+       struct nvmet_pr_registrant *holder;
+       enum nvme_pr_type original_rtype;
+       u64 prkey = le64_to_cpu(d->prkey);
+       u16 status;
+
+       lockdep_assert_held(&pr->pr_lock);
+       holder = rcu_dereference_protected(pr->holder,
+ lockdep_is_held(&pr->pr_lock));
+       if (!holder)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+ &ctrl->hostid, abort);
+
+       original_rtype = holder->rtype;
+       if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
+           original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
+               if (!prkey) {
Quoted 8 lines
Quoted 3 lines

+ nvmet_pr_unreg_all_others(req,
&ctrl->hostid, abort);
+ nvmet_pr_set_new_holder(pr, rtype, reg);

You didnot address the reservation atomicity here. You still remove an old holder and then after a some time you set the new holder. In this time gap an incomming WRITE command from a non holder will write
to the media.

Right, I will change it in v10.

Quoted 11 lines
Quoted 11 lines
drivers/nvme/target/pr.c new file +11 -0
+                       return NVME_SC_SUCCESS;
+               }
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+                               &ctrl->hostid, abort);
+       }
+
+       if (holder == reg) {
+               status = nvmet_pr_update_reg_attr(pr, holder,
+ nvmet_pr_update_holder_rtype, &rtype);
+               if (!status && original_rtype != rtype)
+                       nvmet_pr_resv_released(pr, &reg->hostid);
Quoted 8 lines
Quoted 7 lines
drivers/nvme/target/pr.c new file +5 -0
+               return status;
+       }
+
+       if (prkey == holder->rkey) {
+               status = nvmet_pr_unreg_all_others_by_prkey(req,

prkey,
+ &ctrl->hostid, abort);

And here too that timegap with released reservation.

I will fix this too, and also the

nvmet_pr_unreg_all_others_by_prkey() does not need a return value,

I remove it.

Quoted 6 lines
Quoted 6 lines
drivers/nvme/target/pr.c new file +6 -0
+               if (status)
+                       return status;
+
+               nvmet_pr_set_new_holder(pr, rtype, reg);
+               if (original_rtype != rtype)
+                       nvmet_pr_resv_released(pr, &reg->hostid);
Quoted 5 lines
Quoted 2 lines

This function is(may be) already invoked in nvmet_pr_unregister_one.

Not, here the original_rtype is not NVME_PR_WRITE_EXCLUSIVE_REG_ONLY or NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS, so the nvmet_pr_unregister_one will not call nvmet_pr_resv_released.

nvmet_pr_unregister_one sends an event for original_rtype == *_REG_ONLY Here you have only original_rtype != ALL_REGS, so _REG_ONLY is possible.

Quoted 8 lines
Quoted 1 line

My mistake, it will be removed in v10, thanks.

I take a look again, if we set self new holder before call nvmet_pr_unreg_all_others_by_prkey(), the nvmet_pr_unreg_all_others_by_prkey() will

not unregister self, so this will not goto nvmet_pr_unregister_one()'s calling nvmet_pr_resv_released().

Yes, and this is a reason not to try to fix non-atomicity (anothter my comment) by setting new holder before unregistering.

Regarding this place, here nvmet_pr_resv_released should be called for original_rtype !=*_REG_ONLY with a note that _REG_ONLY handled in nvmet_pr_unregister_one.

Please, do not take my suggestions "how to fix" as a direct order, it's just suggestion.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
drivers/nvme/target/pr.c new file +17 -0
+               return status;
+       }
+
+       if (prkey)
+               return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
+ &ctrl->hostid, abort);
+       return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
+}
+
+static void nvmet_pr_confirm_ns_pc_ref(struct percpu_ref *ref)
+{
+       struct nvmet_pr_per_ctrl_ref *pc_ref =
+               container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
+
+       complete(&pc_ref->confirm_done);
+}
+
author
#8 lore raw

Re: [PATCH v9 1/1] nvmet: support reservation feature

Quoted 6 lines · Dmitry Bogdanov
Quoted 6 lines

I take a look again, if we set self new holder before call nvmet_pr_unreg_all_others_by_prkey(), the nvmet_pr_unreg_all_others_by_prkey() will

not unregister self, so this will not goto nvmet_pr_unregister_one()'s calling nvmet_pr_resv_released().

Quoted 9 lines

Yes, and this is a reason not to try to fix non-atomicity (anothter my comment) by setting new holder before unregistering.

Regarding this place, here nvmet_pr_resv_released should be called for original_rtype !=*_REG_ONLY with a note that _REG_ONLY handled in nvmet_pr_unregister_one.

Please, do not take my suggestions "how to fix" as a direct order, it's just suggestion.

I'm a little confused, if we dont set new holder before unregistering,

how do we fix the non-atomicity problem?

My opinion is that setting current host to holder first can not only

make sure that during unregistering other host can not access, but also

ensure that nvmet_pr_unregister_one will not unregiter the new holder(In nvmet_pr_unreg_all_others_by_prkey, I exclude current host),

so that we dont need to worry about doule call nvmet_pr_resv_released.

#9 lore raw

Re: [PATCH v9 1/1] nvmet: support reservation feature

Quoted 16 lines · Guixin Liu
Quoted 6 lines · Dmitry Bogdanov
Quoted 6 lines

I take a look again, if we set self new holder before call nvmet_pr_unreg_all_others_by_prkey(), the nvmet_pr_unreg_all_others_by_prkey() will

not unregister self, so this will not goto nvmet_pr_unregister_one()'s calling nvmet_pr_resv_released().

Quoted 9 lines

Yes, and this is a reason not to try to fix non-atomicity (anothter my comment) by setting new holder before unregistering.

Regarding this place, here nvmet_pr_resv_released should be called for original_rtype !=*_REG_ONLY with a note that _REG_ONLY handled in nvmet_pr_unregister_one.

Please, do not take my suggestions "how to fix" as a direct order, it's just suggestion.

Quoted 8 lines

I'm a little confused, if we dont set new holder before unregistering, how do we fix the non-atomicity problem?

My opinion is that setting current host to holder first can not only make sure that during unregistering other host can not access, but also ensure that nvmet_pr_unregister_one will not unregiter the new holder(In nvmet_pr_unreg_all_others_by_prkey, I exclude current host), so that we dont need to worry about doule call nvmet_pr_resv_released.

I didnot mean that that will not fix the non-atomicity.
I was worrying that in that case you will miss the existing logic for the changing the reservation - that reservation released notification in nvmet_pr_unregister_one.

But, looking into that now I see that keeping nvmet_pr_resv_released() here and setting the holder before unregistring others will actualy solve both my comments.

BR,
Dmitry