xref: /linux/drivers/nvme/target/pr.c (revision 976311c5594a7a26047891f087ad59a804e02baa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVMe over Fabrics Persist Reservation.
4  * Copyright (c) 2024 Guixin Liu, Alibaba Group.
5  * All rights reserved.
6  */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/unaligned.h>
9 #include "nvmet.h"
10 
11 #define NVMET_PR_NOTIFY_MASK_ALL \
12 	(1 << NVME_PR_NOTIFY_BIT_REG_PREEMPTED | \
13 	 1 << NVME_PR_NOTIFY_BIT_RESV_RELEASED | \
14 	 1 << NVME_PR_NOTIFY_BIT_RESV_PREEMPTED)
15 
16 static inline bool nvmet_pr_parse_ignore_key(u32 cdw10)
17 {
18 	/* Ignore existing key, bit 03. */
19 	return (cdw10 >> 3) & 1;
20 }
21 
22 static inline struct nvmet_ns *nvmet_pr_to_ns(struct nvmet_pr *pr)
23 {
24 	return container_of(pr, struct nvmet_ns, pr);
25 }
26 
27 static struct nvmet_pr_registrant *
28 nvmet_pr_find_registrant(struct nvmet_pr *pr, uuid_t *hostid)
29 {
30 	struct nvmet_pr_registrant *reg;
31 
32 	list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
33 		if (uuid_equal(&reg->hostid, hostid))
34 			return reg;
35 	}
36 	return NULL;
37 }
38 
39 u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask)
40 {
41 	u32 nsid = le32_to_cpu(req->cmd->common.nsid);
42 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
43 	struct nvmet_ns *ns;
44 	unsigned long idx;
45 	u16 status;
46 
47 	if (mask & ~(NVMET_PR_NOTIFY_MASK_ALL)) {
48 		req->error_loc = offsetof(struct nvme_common_command, cdw11);
49 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
50 	}
51 
52 	if (nsid != U32_MAX) {
53 		status = nvmet_req_find_ns(req);
54 		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
55 			return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
56 		if (status)
57 			return status;
58 		if (!req->ns->pr.enable)
59 			return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
60 
61 		WRITE_ONCE(req->ns->pr.notify_mask, mask);
62 		goto success;
63 	}
64 
65 	nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
66 		if (ns->pr.enable)
67 			WRITE_ONCE(ns->pr.notify_mask, mask);
68 	}
69 
70 success:
71 	nvmet_set_result(req, mask);
72 	return NVME_SC_SUCCESS;
73 }
74 
75 u16 nvmet_get_feat_resv_notif_mask(struct nvmet_req *req)
76 {
77 	u16 status;
78 
79 	status = nvmet_req_find_ns(req);
80 	if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
81 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
82 	if (status)
83 		return status;
84 
85 	if (!req->ns->pr.enable)
86 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
87 
88 	nvmet_set_result(req, READ_ONCE(req->ns->pr.notify_mask));
89 	return status;
90 }
91 
92 void nvmet_execute_get_log_page_resv(struct nvmet_req *req)
93 {
94 	struct nvmet_pr_log_mgr *log_mgr = &req->sq->ctrl->pr_log_mgr;
95 	struct nvme_pr_log next_log = {0};
96 	struct nvme_pr_log log = {0};
97 	u16 status = NVME_SC_SUCCESS;
98 	u64 lost_count;
99 	u64 cur_count;
100 	u64 next_count;
101 
102 	status = nvmet_zero_sgl(req, 0, req->transfer_len);
103 	if (status) {
104 		nvmet_req_complete(req, status);
105 		return;
106 	}
107 
108 	mutex_lock(&log_mgr->lock);
109 	if (!kfifo_get(&log_mgr->log_queue, &log))
110 		goto out;
111 
112 	/*
113 	 * We can't get the last in kfifo.
114 	 * Utilize the current count and the count from the next log to
115 	 * calculate the number of lost logs, while also addressing cases
116 	 * of overflow. If there is no subsequent log, the number of lost
117 	 * logs is equal to the lost_count within the nvmet_pr_log_mgr.
118 	 */
119 	cur_count = le64_to_cpu(log.count);
120 	if (kfifo_peek(&log_mgr->log_queue, &next_log)) {
121 		next_count = le64_to_cpu(next_log.count);
122 		if (next_count > cur_count)
123 			lost_count = next_count - cur_count - 1;
124 		else
125 			lost_count = U64_MAX - cur_count + next_count - 1;
126 	} else {
127 		lost_count = log_mgr->lost_count;
128 	}
129 
130 	log.count = cpu_to_le64((cur_count + lost_count) == 0 ?
131 				1 : (cur_count + lost_count));
132 	log_mgr->lost_count -= lost_count;
133 
134 	log.nr_pages = kfifo_len(&log_mgr->log_queue);
135 
136 out:
137 	status = nvmet_copy_to_sgl(req, 0, &log, sizeof(log));
138 	mutex_unlock(&log_mgr->lock);
139 	nvmet_req_complete(req, status);
140 }
141 
142 static void nvmet_pr_add_resv_log(struct nvmet_ctrl *ctrl, u8 log_type,
143 				  u32 nsid)
144 {
145 	struct nvmet_pr_log_mgr *log_mgr = &ctrl->pr_log_mgr;
146 	struct nvme_pr_log log = {0};
147 
148 	mutex_lock(&log_mgr->lock);
149 	log_mgr->counter++;
150 	if (log_mgr->counter == 0)
151 		log_mgr->counter = 1;
152 
153 	log.count = cpu_to_le64(log_mgr->counter);
154 	log.type = log_type;
155 	log.nsid = cpu_to_le32(nsid);
156 
157 	if (!kfifo_put(&log_mgr->log_queue, log)) {
158 		pr_info("a reservation log lost, cntlid:%d, log_type:%d, nsid:%u\n",
159 			ctrl->cntlid, log_type, nsid);
160 		log_mgr->lost_count++;
161 	}
162 
163 	mutex_unlock(&log_mgr->lock);
164 }
165 
166 static void nvmet_pr_resv_released(struct nvmet_pr *pr, uuid_t *hostid)
167 {
168 	struct nvmet_ns *ns = nvmet_pr_to_ns(pr);
169 	struct nvmet_subsys *subsys = ns->subsys;
170 	struct nvmet_ctrl *ctrl;
171 
172 	if (test_bit(NVME_PR_NOTIFY_BIT_RESV_RELEASED, &pr->notify_mask))
173 		return;
174 
175 	mutex_lock(&subsys->lock);
176 	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
177 		if (!uuid_equal(&ctrl->hostid, hostid) &&
178 		    nvmet_pr_find_registrant(pr, &ctrl->hostid)) {
179 			nvmet_pr_add_resv_log(ctrl,
180 				NVME_PR_LOG_RESERVATION_RELEASED, ns->nsid);
181 			nvmet_add_async_event(ctrl, NVME_AER_CSS,
182 				NVME_AEN_RESV_LOG_PAGE_AVAILABLE,
183 				NVME_LOG_RESERVATION);
184 		}
185 	}
186 	mutex_unlock(&subsys->lock);
187 }
188 
189 static void nvmet_pr_send_event_to_host(struct nvmet_pr *pr, uuid_t *hostid,
190 					  u8 log_type)
191 {
192 	struct nvmet_ns *ns = nvmet_pr_to_ns(pr);
193 	struct nvmet_subsys *subsys = ns->subsys;
194 	struct nvmet_ctrl *ctrl;
195 
196 	mutex_lock(&subsys->lock);
197 	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
198 		if (uuid_equal(hostid, &ctrl->hostid)) {
199 			nvmet_pr_add_resv_log(ctrl, log_type, ns->nsid);
200 			nvmet_add_async_event(ctrl, NVME_AER_CSS,
201 				NVME_AEN_RESV_LOG_PAGE_AVAILABLE,
202 				NVME_LOG_RESERVATION);
203 		}
204 	}
205 	mutex_unlock(&subsys->lock);
206 }
207 
208 static void nvmet_pr_resv_preempted(struct nvmet_pr *pr, uuid_t *hostid)
209 {
210 	if (test_bit(NVME_PR_NOTIFY_BIT_RESV_PREEMPTED, &pr->notify_mask))
211 		return;
212 
213 	nvmet_pr_send_event_to_host(pr, hostid,
214 		NVME_PR_LOG_RESERVATION_PREEMPTED);
215 }
216 
217 static void nvmet_pr_registration_preempted(struct nvmet_pr *pr,
218 					    uuid_t *hostid)
219 {
220 	if (test_bit(NVME_PR_NOTIFY_BIT_REG_PREEMPTED, &pr->notify_mask))
221 		return;
222 
223 	nvmet_pr_send_event_to_host(pr, hostid,
224 		NVME_PR_LOG_REGISTRATION_PREEMPTED);
225 }
226 
227 static inline void nvmet_pr_set_new_holder(struct nvmet_pr *pr, u8 new_rtype,
228 					   struct nvmet_pr_registrant *reg)
229 {
230 	reg->rtype = new_rtype;
231 	rcu_assign_pointer(pr->holder, reg);
232 }
233 
234 static u16 nvmet_pr_register(struct nvmet_req *req,
235 			     struct nvmet_pr_register_data *d)
236 {
237 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
238 	struct nvmet_pr_registrant *new, *reg;
239 	struct nvmet_pr *pr = &req->ns->pr;
240 	u16 status = NVME_SC_SUCCESS;
241 	u64 nrkey = le64_to_cpu(d->nrkey);
242 
243 	new = kmalloc_obj(*new);
244 	if (!new)
245 		return NVME_SC_INTERNAL;
246 
247 	down(&pr->pr_sem);
248 	reg = nvmet_pr_find_registrant(pr, &ctrl->hostid);
249 	if (reg) {
250 		if (reg->rkey != nrkey)
251 			status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
252 		kfree(new);
253 		goto out;
254 	}
255 
256 	memset(new, 0, sizeof(*new));
257 	INIT_LIST_HEAD(&new->entry);
258 	new->rkey = nrkey;
259 	uuid_copy(&new->hostid, &ctrl->hostid);
260 	list_add_tail_rcu(&new->entry, &pr->registrant_list);
261 
262 out:
263 	up(&pr->pr_sem);
264 	return status;
265 }
266 
267 static void nvmet_pr_unregister_one(struct nvmet_pr *pr,
268 				    struct nvmet_pr_registrant *reg)
269 {
270 	struct nvmet_pr_registrant *first_reg;
271 	struct nvmet_pr_registrant *holder;
272 	u8 original_rtype;
273 
274 	list_del_rcu(&reg->entry);
275 
276 	holder = rcu_dereference_protected(pr->holder, 1);
277 	if (reg != holder)
278 		goto out;
279 
280 	original_rtype = holder->rtype;
281 	if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
282 	    original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
283 		first_reg = list_first_or_null_rcu(&pr->registrant_list,
284 				struct nvmet_pr_registrant, entry);
285 		if (first_reg)
286 			first_reg->rtype = original_rtype;
287 		rcu_assign_pointer(pr->holder, first_reg);
288 	} else {
289 		rcu_assign_pointer(pr->holder, NULL);
290 
291 		if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY ||
292 		    original_rtype == NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY)
293 			nvmet_pr_resv_released(pr, &reg->hostid);
294 	}
295 out:
296 	kfree_rcu(reg, rcu);
297 }
298 
299 static u16 nvmet_pr_unregister(struct nvmet_req *req,
300 			       struct nvmet_pr_register_data *d,
301 			       bool ignore_key)
302 {
303 	u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
304 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
305 	struct nvmet_pr *pr = &req->ns->pr;
306 	struct nvmet_pr_registrant *reg;
307 
308 	down(&pr->pr_sem);
309 	list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
310 		if (uuid_equal(&reg->hostid, &ctrl->hostid)) {
311 			if (ignore_key || reg->rkey == le64_to_cpu(d->crkey)) {
312 				status = NVME_SC_SUCCESS;
313 				nvmet_pr_unregister_one(pr, reg);
314 			}
315 			break;
316 		}
317 	}
318 	up(&pr->pr_sem);
319 
320 	return status;
321 }
322 
323 static void nvmet_pr_update_reg_rkey(struct nvmet_pr_registrant *reg,
324 				     void *attr)
325 {
326 	reg->rkey = *(u64 *)attr;
327 }
328 
329 static u16 nvmet_pr_update_reg_attr(struct nvmet_pr *pr,
330 			struct nvmet_pr_registrant *reg,
331 			void (*change_attr)(struct nvmet_pr_registrant *reg,
332 			void *attr),
333 			void *attr)
334 {
335 	struct nvmet_pr_registrant *holder;
336 	struct nvmet_pr_registrant *new;
337 
338 	holder = rcu_dereference_protected(pr->holder, 1);
339 	if (reg != holder) {
340 		change_attr(reg, attr);
341 		return NVME_SC_SUCCESS;
342 	}
343 
344 	new = kmalloc_obj(*new, GFP_KERNEL);
345 	if (!new)
346 		return NVME_SC_INTERNAL;
347 
348 	new->rkey = holder->rkey;
349 	new->rtype = holder->rtype;
350 	uuid_copy(&new->hostid, &holder->hostid);
351 	INIT_LIST_HEAD(&new->entry);
352 
353 	change_attr(new, attr);
354 	list_replace_rcu(&holder->entry, &new->entry);
355 	rcu_assign_pointer(pr->holder, new);
356 	kfree_rcu(holder, rcu);
357 
358 	return NVME_SC_SUCCESS;
359 }
360 
361 static u16 nvmet_pr_replace(struct nvmet_req *req,
362 			    struct nvmet_pr_register_data *d,
363 			    bool ignore_key)
364 {
365 	u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
366 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
367 	struct nvmet_pr *pr = &req->ns->pr;
368 	struct nvmet_pr_registrant *reg, *new = NULL;
369 	u64 nrkey = le64_to_cpu(d->nrkey);
370 
371 	if (ignore_key && nrkey) {
372 		new = kzalloc_obj(*new);
373 		if (!new)
374 			return NVME_SC_INTERNAL;
375 	}
376 
377 	down(&pr->pr_sem);
378 	list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
379 		if (uuid_equal(&reg->hostid, &ctrl->hostid)) {
380 			if (ignore_key || reg->rkey == le64_to_cpu(d->crkey))
381 				status = nvmet_pr_update_reg_attr(pr, reg,
382 						nvmet_pr_update_reg_rkey,
383 						&nrkey);
384 			goto free_data;
385 		}
386 	}
387 
388 	if (ignore_key) {
389 		if (!nrkey) {
390 			status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
391 			goto free_data;
392 		}
393 		INIT_LIST_HEAD(&new->entry);
394 		new->rkey = nrkey;
395 		uuid_copy(&new->hostid, &ctrl->hostid);
396 		list_add_tail_rcu(&new->entry, &pr->registrant_list);
397 		status = NVME_SC_SUCCESS;
398 		goto out;
399 	}
400 
401 free_data:
402 	kfree(new);
403 out:
404 	up(&pr->pr_sem);
405 	return status;
406 }
407 
408 static void nvmet_execute_pr_register(struct nvmet_req *req)
409 {
410 	u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
411 	bool ignore_key = nvmet_pr_parse_ignore_key(cdw10);
412 	struct nvmet_pr_register_data *d;
413 	u8 reg_act = cdw10 & 0x07; /* Reservation Register Action, bit 02:00 */
414 	u16 status;
415 
416 	d = kmalloc_obj(*d);
417 	if (!d) {
418 		status = NVME_SC_INTERNAL;
419 		goto out;
420 	}
421 
422 	status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
423 	if (status)
424 		goto free_data;
425 
426 	switch (reg_act) {
427 	case NVME_PR_REGISTER_ACT_REG:
428 		status = nvmet_pr_register(req, d);
429 		break;
430 	case NVME_PR_REGISTER_ACT_UNREG:
431 		status = nvmet_pr_unregister(req, d, ignore_key);
432 		break;
433 	case NVME_PR_REGISTER_ACT_REPLACE:
434 		status = nvmet_pr_replace(req, d, ignore_key);
435 		break;
436 	default:
437 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
438 		status = NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
439 		break;
440 	}
441 free_data:
442 	kfree(d);
443 out:
444 	if (!status)
445 		atomic_inc(&req->ns->pr.generation);
446 	nvmet_req_complete(req, status);
447 }
448 
449 static u16 nvmet_pr_acquire(struct nvmet_req *req,
450 			    struct nvmet_pr_registrant *reg,
451 			    u8 rtype)
452 {
453 	struct nvmet_pr *pr = &req->ns->pr;
454 	struct nvmet_pr_registrant *holder;
455 
456 	holder = rcu_dereference_protected(pr->holder, 1);
457 	if (holder && reg != holder)
458 		return  NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
459 	if (holder && reg == holder) {
460 		if (holder->rtype == rtype)
461 			return NVME_SC_SUCCESS;
462 		return NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
463 	}
464 
465 	nvmet_pr_set_new_holder(pr, rtype, reg);
466 	return NVME_SC_SUCCESS;
467 }
468 
469 static void nvmet_pr_confirm_ns_pc_ref(struct percpu_ref *ref)
470 {
471 	struct nvmet_pr_per_ctrl_ref *pc_ref =
472 		container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
473 
474 	complete(&pc_ref->confirm_done);
475 }
476 
477 static void nvmet_pr_set_ctrl_to_abort(struct nvmet_req *req, uuid_t *hostid)
478 {
479 	struct nvmet_pr_per_ctrl_ref *pc_ref;
480 	struct nvmet_ns *ns = req->ns;
481 	unsigned long idx;
482 
483 	xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
484 		if (uuid_equal(&pc_ref->hostid, hostid)) {
485 			percpu_ref_kill_and_confirm(&pc_ref->ref,
486 						nvmet_pr_confirm_ns_pc_ref);
487 			wait_for_completion(&pc_ref->confirm_done);
488 		}
489 	}
490 }
491 
492 static u16 nvmet_pr_unreg_all_host_by_prkey(struct nvmet_req *req, u64 prkey,
493 					    uuid_t *send_hostid,
494 					    bool abort)
495 {
496 	u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
497 	struct nvmet_pr_registrant *reg, *tmp;
498 	struct nvmet_pr *pr = &req->ns->pr;
499 	uuid_t hostid;
500 
501 	list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
502 		if (reg->rkey == prkey) {
503 			status = NVME_SC_SUCCESS;
504 			uuid_copy(&hostid, &reg->hostid);
505 			if (abort)
506 				nvmet_pr_set_ctrl_to_abort(req, &hostid);
507 			nvmet_pr_unregister_one(pr, reg);
508 			if (!uuid_equal(&hostid, send_hostid))
509 				nvmet_pr_registration_preempted(pr, &hostid);
510 		}
511 	}
512 	return status;
513 }
514 
515 static void nvmet_pr_unreg_all_others_by_prkey(struct nvmet_req *req,
516 					       u64 prkey,
517 					       uuid_t *send_hostid,
518 					       bool abort)
519 {
520 	struct nvmet_pr_registrant *reg, *tmp;
521 	struct nvmet_pr *pr = &req->ns->pr;
522 	uuid_t hostid;
523 
524 	list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
525 		if (reg->rkey == prkey &&
526 		    !uuid_equal(&reg->hostid, send_hostid)) {
527 			uuid_copy(&hostid, &reg->hostid);
528 			if (abort)
529 				nvmet_pr_set_ctrl_to_abort(req, &hostid);
530 			nvmet_pr_unregister_one(pr, reg);
531 			nvmet_pr_registration_preempted(pr, &hostid);
532 		}
533 	}
534 }
535 
536 static void nvmet_pr_unreg_all_others(struct nvmet_req *req,
537 				      uuid_t *send_hostid,
538 				      bool abort)
539 {
540 	struct nvmet_pr_registrant *reg, *tmp;
541 	struct nvmet_pr *pr = &req->ns->pr;
542 	uuid_t hostid;
543 
544 	list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
545 		if (!uuid_equal(&reg->hostid, send_hostid)) {
546 			uuid_copy(&hostid, &reg->hostid);
547 			if (abort)
548 				nvmet_pr_set_ctrl_to_abort(req, &hostid);
549 			nvmet_pr_unregister_one(pr, reg);
550 			nvmet_pr_registration_preempted(pr, &hostid);
551 		}
552 	}
553 }
554 
555 static void nvmet_pr_update_holder_rtype(struct nvmet_pr_registrant *reg,
556 					 void *attr)
557 {
558 	u8 new_rtype = *(u8 *)attr;
559 
560 	reg->rtype = new_rtype;
561 }
562 
563 static u16 nvmet_pr_preempt(struct nvmet_req *req,
564 			    struct nvmet_pr_registrant *reg,
565 			    u8 rtype,
566 			    struct nvmet_pr_acquire_data *d,
567 			    bool abort)
568 {
569 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
570 	struct nvmet_pr *pr = &req->ns->pr;
571 	struct nvmet_pr_registrant *holder;
572 	enum nvme_pr_type original_rtype;
573 	u64 prkey = le64_to_cpu(d->prkey);
574 	u16 status;
575 
576 	holder = rcu_dereference_protected(pr->holder, 1);
577 	if (!holder)
578 		return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
579 					&ctrl->hostid, abort);
580 
581 	original_rtype = holder->rtype;
582 	if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
583 	    original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
584 		if (!prkey) {
585 			/*
586 			 * To prevent possible access from other hosts, and
587 			 * avoid terminate the holder, set the new holder
588 			 * first before unregistering.
589 			 */
590 			nvmet_pr_set_new_holder(pr, rtype, reg);
591 			nvmet_pr_unreg_all_others(req, &ctrl->hostid, abort);
592 			return NVME_SC_SUCCESS;
593 		}
594 		return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
595 				&ctrl->hostid, abort);
596 	}
597 
598 	if (holder == reg && prkey == holder->rkey) {
599 		status = nvmet_pr_update_reg_attr(pr, holder,
600 				nvmet_pr_update_holder_rtype, &rtype);
601 		if (!status && original_rtype != rtype)
602 			nvmet_pr_resv_released(pr, &ctrl->hostid);
603 		return status;
604 	}
605 
606 	if (prkey == holder->rkey) {
607 		/*
608 		 * Same as before, set the new holder first.
609 		 */
610 		nvmet_pr_set_new_holder(pr, rtype, reg);
611 		nvmet_pr_unreg_all_others_by_prkey(req, prkey, &ctrl->hostid,
612 						abort);
613 		if (original_rtype != rtype)
614 			nvmet_pr_resv_released(pr, &reg->hostid);
615 		return NVME_SC_SUCCESS;
616 	}
617 
618 	if (prkey)
619 		return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
620 					&ctrl->hostid, abort);
621 	return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
622 }
623 
624 static void nvmet_pr_do_abort(struct work_struct *w)
625 {
626 	struct nvmet_req *req = container_of(w, struct nvmet_req, r.abort_work);
627 	struct nvmet_pr_per_ctrl_ref *pc_ref;
628 	struct nvmet_ns *ns = req->ns;
629 	unsigned long idx;
630 
631 	/*
632 	 * The target does not support abort, just wait per-controller ref to 0.
633 	 */
634 	xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
635 		if (percpu_ref_is_dying(&pc_ref->ref)) {
636 			wait_for_completion(&pc_ref->free_done);
637 			reinit_completion(&pc_ref->confirm_done);
638 			reinit_completion(&pc_ref->free_done);
639 			percpu_ref_resurrect(&pc_ref->ref);
640 		}
641 	}
642 
643 	up(&ns->pr.pr_sem);
644 	nvmet_req_complete(req, NVME_SC_SUCCESS);
645 }
646 
647 static u16 __nvmet_execute_pr_acquire(struct nvmet_req *req,
648 				      struct nvmet_pr_registrant *reg,
649 				      u8 acquire_act,
650 				      u8 rtype,
651 				      struct nvmet_pr_acquire_data *d)
652 {
653 	u16 status;
654 
655 	switch (acquire_act) {
656 	case NVME_PR_ACQUIRE_ACT_ACQUIRE:
657 		status = nvmet_pr_acquire(req, reg, rtype);
658 		goto out;
659 	case NVME_PR_ACQUIRE_ACT_PREEMPT:
660 		status = nvmet_pr_preempt(req, reg, rtype, d, false);
661 		goto inc_gen;
662 	case NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT:
663 		status = nvmet_pr_preempt(req, reg, rtype, d, true);
664 		goto inc_gen;
665 	default:
666 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
667 		status = NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
668 		goto out;
669 	}
670 inc_gen:
671 	if (!status)
672 		atomic_inc(&req->ns->pr.generation);
673 out:
674 	return status;
675 }
676 
677 static void nvmet_execute_pr_acquire(struct nvmet_req *req)
678 {
679 	u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
680 	bool ignore_key = nvmet_pr_parse_ignore_key(cdw10);
681 	/* Reservation type, bit 15:08 */
682 	u8 rtype = (u8)((cdw10 >> 8) & 0xff);
683 	/* Reservation acquire action, bit 02:00 */
684 	u8 acquire_act = cdw10 & 0x07;
685 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
686 	struct nvmet_pr_acquire_data *d = NULL;
687 	struct nvmet_pr *pr = &req->ns->pr;
688 	struct nvmet_pr_registrant *reg;
689 	u16 status = NVME_SC_SUCCESS;
690 
691 	if (ignore_key ||
692 	    rtype < NVME_PR_WRITE_EXCLUSIVE ||
693 	    rtype > NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
694 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
695 		goto out;
696 	}
697 
698 	d = kmalloc_obj(*d);
699 	if (!d) {
700 		status = NVME_SC_INTERNAL;
701 		goto out;
702 	}
703 
704 	status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
705 	if (status)
706 		goto free_data;
707 
708 	status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
709 	down(&pr->pr_sem);
710 	list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
711 		if (uuid_equal(&reg->hostid, &ctrl->hostid) &&
712 		    reg->rkey == le64_to_cpu(d->crkey)) {
713 			status = __nvmet_execute_pr_acquire(req, reg,
714 					acquire_act, rtype, d);
715 			break;
716 		}
717 	}
718 
719 	if (!status && acquire_act == NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT) {
720 		kfree(d);
721 		INIT_WORK(&req->r.abort_work, nvmet_pr_do_abort);
722 		queue_work(nvmet_wq, &req->r.abort_work);
723 		return;
724 	}
725 
726 	up(&pr->pr_sem);
727 
728 free_data:
729 	kfree(d);
730 out:
731 	nvmet_req_complete(req, status);
732 }
733 
734 static u16 nvmet_pr_release(struct nvmet_req *req,
735 			    struct nvmet_pr_registrant *reg,
736 			    u8 rtype)
737 {
738 	struct nvmet_pr *pr = &req->ns->pr;
739 	struct nvmet_pr_registrant *holder;
740 	u8 original_rtype;
741 
742 	holder = rcu_dereference_protected(pr->holder, 1);
743 	if (!holder || reg != holder)
744 		return NVME_SC_SUCCESS;
745 
746 	original_rtype = holder->rtype;
747 	if (original_rtype != rtype)
748 		return NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
749 
750 	rcu_assign_pointer(pr->holder, NULL);
751 
752 	if (original_rtype != NVME_PR_WRITE_EXCLUSIVE &&
753 	    original_rtype != NVME_PR_EXCLUSIVE_ACCESS)
754 		nvmet_pr_resv_released(pr, &reg->hostid);
755 
756 	return NVME_SC_SUCCESS;
757 }
758 
759 static void nvmet_pr_clear(struct nvmet_req *req)
760 {
761 	struct nvmet_pr_registrant *reg, *tmp;
762 	struct nvmet_pr *pr = &req->ns->pr;
763 
764 	rcu_assign_pointer(pr->holder, NULL);
765 
766 	list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
767 		list_del_rcu(&reg->entry);
768 		if (!uuid_equal(&req->sq->ctrl->hostid, &reg->hostid))
769 			nvmet_pr_resv_preempted(pr, &reg->hostid);
770 		kfree_rcu(reg, rcu);
771 	}
772 
773 	atomic_inc(&pr->generation);
774 }
775 
776 static u16 __nvmet_execute_pr_release(struct nvmet_req *req,
777 				      struct nvmet_pr_registrant *reg,
778 				      u8 release_act, u8 rtype)
779 {
780 	switch (release_act) {
781 	case NVME_PR_RELEASE_ACT_RELEASE:
782 		return nvmet_pr_release(req, reg, rtype);
783 	case NVME_PR_RELEASE_ACT_CLEAR:
784 		nvmet_pr_clear(req);
785 		return NVME_SC_SUCCESS;
786 	default:
787 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
788 		return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
789 	}
790 }
791 
792 static void nvmet_execute_pr_release(struct nvmet_req *req)
793 {
794 	u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
795 	bool ignore_key = nvmet_pr_parse_ignore_key(cdw10);
796 	u8 rtype = (u8)((cdw10 >> 8) & 0xff); /* Reservation type, bit 15:08 */
797 	u8 release_act = cdw10 & 0x07; /* Reservation release action, bit 02:00 */
798 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
799 	struct nvmet_pr *pr = &req->ns->pr;
800 	struct nvmet_pr_release_data *d;
801 	struct nvmet_pr_registrant *reg;
802 	u16 status;
803 
804 	if (ignore_key) {
805 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
806 		goto out;
807 	}
808 
809 	d = kmalloc_obj(*d);
810 	if (!d) {
811 		status = NVME_SC_INTERNAL;
812 		goto out;
813 	}
814 
815 	status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
816 	if (status)
817 		goto free_data;
818 
819 	status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
820 	down(&pr->pr_sem);
821 	list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
822 		if (uuid_equal(&reg->hostid, &ctrl->hostid) &&
823 		    reg->rkey == le64_to_cpu(d->crkey)) {
824 			status = __nvmet_execute_pr_release(req, reg,
825 					release_act, rtype);
826 			break;
827 		}
828 	}
829 	up(&pr->pr_sem);
830 free_data:
831 	kfree(d);
832 out:
833 	nvmet_req_complete(req, status);
834 }
835 
836 static void nvmet_execute_pr_report(struct nvmet_req *req)
837 {
838 	u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
839 	u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
840 	u32 num_bytes = 4 * (cdw10 + 1); /* cdw10 is number of dwords */
841 	u8 eds = cdw11 & 1; /* Extended data structure, bit 00 */
842 	struct nvme_registered_ctrl_ext *ctrl_eds;
843 	struct nvme_reservation_status_ext *data;
844 	struct nvmet_pr *pr = &req->ns->pr;
845 	struct nvmet_pr_registrant *holder;
846 	struct nvmet_pr_registrant *reg;
847 	u16 num_ctrls = 0;
848 	u16 status;
849 	u8 rtype;
850 
851 	/* nvmet hostid(uuid_t) is 128 bit. */
852 	if (!eds) {
853 		req->error_loc = offsetof(struct nvme_common_command, cdw11);
854 		status = NVME_SC_HOST_ID_INCONSIST | NVME_STATUS_DNR;
855 		goto out;
856 	}
857 
858 	if (num_bytes < sizeof(struct nvme_reservation_status_ext)) {
859 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
860 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
861 		goto out;
862 	}
863 
864 	data = kzalloc(num_bytes, GFP_KERNEL);
865 	if (!data) {
866 		status = NVME_SC_INTERNAL;
867 		goto out;
868 	}
869 	data->gen = cpu_to_le32(atomic_read(&pr->generation));
870 	data->ptpls = 0;
871 	ctrl_eds = data->regctl_eds;
872 
873 	rcu_read_lock();
874 	holder = rcu_dereference(pr->holder);
875 	rtype = holder ? holder->rtype : 0;
876 	data->rtype = rtype;
877 
878 	list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
879 		num_ctrls++;
880 		/*
881 		 * continue to get the number of all registrans.
882 		 */
883 		if (((void *)ctrl_eds + sizeof(*ctrl_eds)) >
884 		    ((void *)data + num_bytes))
885 			continue;
886 		/*
887 		 * Dynamic controller, set cntlid to 0xffff.
888 		 */
889 		ctrl_eds->cntlid = cpu_to_le16(NVME_CNTLID_DYNAMIC);
890 		if (rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
891 		    rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS)
892 			ctrl_eds->rcsts = 1;
893 		if (reg == holder)
894 			ctrl_eds->rcsts = 1;
895 		uuid_copy((uuid_t *)&ctrl_eds->hostid, &reg->hostid);
896 		ctrl_eds->rkey = cpu_to_le64(reg->rkey);
897 		ctrl_eds++;
898 	}
899 	rcu_read_unlock();
900 
901 	put_unaligned_le16(num_ctrls, data->regctl);
902 	status = nvmet_copy_to_sgl(req, 0, data, num_bytes);
903 	kfree(data);
904 out:
905 	nvmet_req_complete(req, status);
906 }
907 
908 u16 nvmet_parse_pr_cmd(struct nvmet_req *req)
909 {
910 	struct nvme_command *cmd = req->cmd;
911 
912 	switch (cmd->common.opcode) {
913 	case nvme_cmd_resv_register:
914 		req->execute = nvmet_execute_pr_register;
915 		break;
916 	case nvme_cmd_resv_acquire:
917 		req->execute = nvmet_execute_pr_acquire;
918 		break;
919 	case nvme_cmd_resv_release:
920 		req->execute = nvmet_execute_pr_release;
921 		break;
922 	case nvme_cmd_resv_report:
923 		req->execute = nvmet_execute_pr_report;
924 		break;
925 	default:
926 		return 1;
927 	}
928 	return NVME_SC_SUCCESS;
929 }
930 
931 static bool nvmet_is_req_write_cmd_group(struct nvmet_req *req)
932 {
933 	u8 opcode = req->cmd->common.opcode;
934 
935 	if (req->sq->qid) {
936 		switch (opcode) {
937 		case nvme_cmd_flush:
938 		case nvme_cmd_write:
939 		case nvme_cmd_write_zeroes:
940 		case nvme_cmd_dsm:
941 		case nvme_cmd_zone_append:
942 		case nvme_cmd_zone_mgmt_send:
943 			return true;
944 		default:
945 			return false;
946 		}
947 	}
948 	return false;
949 }
950 
951 static bool nvmet_is_req_read_cmd_group(struct nvmet_req *req)
952 {
953 	u8 opcode = req->cmd->common.opcode;
954 
955 	if (req->sq->qid) {
956 		switch (opcode) {
957 		case nvme_cmd_read:
958 		case nvme_cmd_zone_mgmt_recv:
959 			return true;
960 		default:
961 			return false;
962 		}
963 	}
964 	return false;
965 }
966 
967 u16 nvmet_pr_check_cmd_access(struct nvmet_req *req)
968 {
969 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
970 	struct nvmet_pr_registrant *holder;
971 	struct nvmet_ns *ns = req->ns;
972 	struct nvmet_pr *pr = &ns->pr;
973 	u16 status = NVME_SC_SUCCESS;
974 
975 	rcu_read_lock();
976 	holder = rcu_dereference(pr->holder);
977 	if (!holder)
978 		goto unlock;
979 	if (uuid_equal(&ctrl->hostid, &holder->hostid))
980 		goto unlock;
981 
982 	/*
983 	 * The Reservation command group is checked in executing,
984 	 * allow it here.
985 	 */
986 	switch (holder->rtype) {
987 	case NVME_PR_WRITE_EXCLUSIVE:
988 		if (nvmet_is_req_write_cmd_group(req))
989 			status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
990 		break;
991 	case NVME_PR_EXCLUSIVE_ACCESS:
992 		if (nvmet_is_req_read_cmd_group(req) ||
993 		    nvmet_is_req_write_cmd_group(req))
994 			status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
995 		break;
996 	case NVME_PR_WRITE_EXCLUSIVE_REG_ONLY:
997 	case NVME_PR_WRITE_EXCLUSIVE_ALL_REGS:
998 		if ((nvmet_is_req_write_cmd_group(req)) &&
999 		    !nvmet_pr_find_registrant(pr, &ctrl->hostid))
1000 			status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
1001 		break;
1002 	case NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY:
1003 	case NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS:
1004 		if ((nvmet_is_req_read_cmd_group(req) ||
1005 		    nvmet_is_req_write_cmd_group(req)) &&
1006 		    !nvmet_pr_find_registrant(pr, &ctrl->hostid))
1007 			status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
1008 		break;
1009 	default:
1010 		pr_warn("the reservation type is set wrong, type:%d\n",
1011 			holder->rtype);
1012 		break;
1013 	}
1014 
1015 unlock:
1016 	rcu_read_unlock();
1017 	if (status)
1018 		req->error_loc = offsetof(struct nvme_common_command, opcode);
1019 	return status;
1020 }
1021 
1022 u16 nvmet_pr_get_ns_pc_ref(struct nvmet_req *req)
1023 {
1024 	struct nvmet_pr_per_ctrl_ref *pc_ref;
1025 
1026 	pc_ref = xa_load(&req->ns->pr_per_ctrl_refs,
1027 			req->sq->ctrl->cntlid);
1028 	if (unlikely(!percpu_ref_tryget_live(&pc_ref->ref)))
1029 		return NVME_SC_INTERNAL;
1030 	req->pc_ref = pc_ref;
1031 	return NVME_SC_SUCCESS;
1032 }
1033 
1034 static void nvmet_pr_ctrl_ns_all_cmds_done(struct percpu_ref *ref)
1035 {
1036 	struct nvmet_pr_per_ctrl_ref *pc_ref =
1037 		container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
1038 
1039 	complete(&pc_ref->free_done);
1040 }
1041 
1042 static int nvmet_pr_alloc_and_insert_pc_ref(struct nvmet_ns *ns,
1043 					    unsigned long idx,
1044 					    uuid_t *hostid)
1045 {
1046 	struct nvmet_pr_per_ctrl_ref *pc_ref;
1047 	int ret;
1048 
1049 	pc_ref = kmalloc_obj(*pc_ref, GFP_KERNEL);
1050 	if (!pc_ref)
1051 		return  -ENOMEM;
1052 
1053 	ret = percpu_ref_init(&pc_ref->ref, nvmet_pr_ctrl_ns_all_cmds_done,
1054 			PERCPU_REF_ALLOW_REINIT, GFP_KERNEL);
1055 	if (ret)
1056 		goto free;
1057 
1058 	init_completion(&pc_ref->free_done);
1059 	init_completion(&pc_ref->confirm_done);
1060 	uuid_copy(&pc_ref->hostid, hostid);
1061 
1062 	ret = xa_insert(&ns->pr_per_ctrl_refs, idx, pc_ref, GFP_KERNEL);
1063 	if (ret)
1064 		goto exit;
1065 	return ret;
1066 exit:
1067 	percpu_ref_exit(&pc_ref->ref);
1068 free:
1069 	kfree(pc_ref);
1070 	return ret;
1071 }
1072 
1073 int nvmet_ctrl_init_pr(struct nvmet_ctrl *ctrl)
1074 {
1075 	struct nvmet_subsys *subsys = ctrl->subsys;
1076 	struct nvmet_pr_per_ctrl_ref *pc_ref;
1077 	struct nvmet_ns *ns = NULL;
1078 	unsigned long idx;
1079 	int ret;
1080 
1081 	ctrl->pr_log_mgr.counter = 0;
1082 	ctrl->pr_log_mgr.lost_count = 0;
1083 	mutex_init(&ctrl->pr_log_mgr.lock);
1084 	INIT_KFIFO(ctrl->pr_log_mgr.log_queue);
1085 
1086 	/*
1087 	 * Here we are under subsys lock, if an ns not in subsys->namespaces,
1088 	 * we can make sure that ns is not enabled, and not call
1089 	 * nvmet_pr_init_ns(), see more details in nvmet_ns_enable().
1090 	 * So just check ns->pr.enable.
1091 	 */
1092 	nvmet_for_each_enabled_ns(&subsys->namespaces, idx, ns) {
1093 		if (ns->pr.enable) {
1094 			ret = nvmet_pr_alloc_and_insert_pc_ref(ns, ctrl->cntlid,
1095 							&ctrl->hostid);
1096 			if (ret)
1097 				goto free_per_ctrl_refs;
1098 		}
1099 	}
1100 	return 0;
1101 
1102 free_per_ctrl_refs:
1103 	nvmet_for_each_enabled_ns(&subsys->namespaces, idx, ns) {
1104 		if (ns->pr.enable) {
1105 			pc_ref = xa_erase(&ns->pr_per_ctrl_refs, ctrl->cntlid);
1106 			if (pc_ref)
1107 				percpu_ref_exit(&pc_ref->ref);
1108 			kfree(pc_ref);
1109 		}
1110 	}
1111 	return ret;
1112 }
1113 
1114 void nvmet_ctrl_destroy_pr(struct nvmet_ctrl *ctrl)
1115 {
1116 	struct nvmet_pr_per_ctrl_ref *pc_ref;
1117 	struct nvmet_ns *ns;
1118 	unsigned long idx;
1119 
1120 	kfifo_free(&ctrl->pr_log_mgr.log_queue);
1121 	mutex_destroy(&ctrl->pr_log_mgr.lock);
1122 
1123 	nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
1124 		if (ns->pr.enable) {
1125 			pc_ref = xa_erase(&ns->pr_per_ctrl_refs, ctrl->cntlid);
1126 			if (pc_ref)
1127 				percpu_ref_exit(&pc_ref->ref);
1128 			kfree(pc_ref);
1129 		}
1130 	}
1131 }
1132 
1133 int nvmet_pr_init_ns(struct nvmet_ns *ns)
1134 {
1135 	struct nvmet_subsys *subsys = ns->subsys;
1136 	struct nvmet_pr_per_ctrl_ref *pc_ref;
1137 	struct nvmet_ctrl *ctrl = NULL;
1138 	unsigned long idx;
1139 	int ret;
1140 
1141 	ns->pr.holder = NULL;
1142 	atomic_set(&ns->pr.generation, 0);
1143 	sema_init(&ns->pr.pr_sem, 1);
1144 	INIT_LIST_HEAD(&ns->pr.registrant_list);
1145 	ns->pr.notify_mask = 0;
1146 
1147 	xa_init(&ns->pr_per_ctrl_refs);
1148 
1149 	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
1150 		ret = nvmet_pr_alloc_and_insert_pc_ref(ns, ctrl->cntlid,
1151 						&ctrl->hostid);
1152 		if (ret)
1153 			goto free_per_ctrl_refs;
1154 	}
1155 	return 0;
1156 
1157 free_per_ctrl_refs:
1158 	xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
1159 		xa_erase(&ns->pr_per_ctrl_refs, idx);
1160 		percpu_ref_exit(&pc_ref->ref);
1161 		kfree(pc_ref);
1162 	}
1163 	return ret;
1164 }
1165 
1166 void nvmet_pr_exit_ns(struct nvmet_ns *ns)
1167 {
1168 	struct nvmet_pr_registrant *reg, *tmp;
1169 	struct nvmet_pr_per_ctrl_ref *pc_ref;
1170 	struct nvmet_pr *pr = &ns->pr;
1171 	unsigned long idx;
1172 
1173 	list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
1174 		list_del(&reg->entry);
1175 		kfree(reg);
1176 	}
1177 
1178 	xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
1179 		/*
1180 		 * No command on ns here, we can safely free pc_ref.
1181 		 */
1182 		pc_ref = xa_erase(&ns->pr_per_ctrl_refs, idx);
1183 		percpu_ref_exit(&pc_ref->ref);
1184 		kfree(pc_ref);
1185 	}
1186 
1187 	xa_destroy(&ns->pr_per_ctrl_refs);
1188 }
1189