1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2011-2014, Intel Corporation.
4 * Copyright (c) 2017-2021 Christoph Hellwig.
6 #include <linux/ptrace.h> /* for force_successful_syscall_return */
7 #include <linux/nvme_ioctl.h>
8 #include <linux/io_uring.h>
12 * Convert integer values from ioctl structures to user pointers, silently
13 * ignoring the upper bits in the compat case to match behaviour of 32-bit
16 static void __user *nvme_to_user_ptr(uintptr_t ptrval)
18 if (in_compat_syscall())
19 ptrval = (compat_uptr_t)ptrval;
20 return (void __user *)ptrval;
23 static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
24 unsigned len, u32 seed, bool write)
26 struct bio_integrity_payload *bip;
30 buf = kmalloc(len, GFP_KERNEL);
35 if (write && copy_from_user(buf, ubuf, len))
38 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
44 bip->bip_iter.bi_size = len;
45 bip->bip_iter.bi_sector = seed;
46 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
57 static int nvme_finish_user_metadata(struct request *req, void __user *ubuf,
58 void *meta, unsigned len, int ret)
60 if (!ret && req_op(req) == REQ_OP_DRV_IN &&
61 copy_to_user(ubuf, meta, len))
67 static struct request *nvme_alloc_user_request(struct request_queue *q,
68 struct nvme_command *cmd, void __user *ubuffer,
69 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
70 u32 meta_seed, void **metap, unsigned timeout, bool vec,
71 unsigned int rq_flags, blk_mq_req_flags_t blk_flags)
73 bool write = nvme_is_write(cmd);
74 struct nvme_ns *ns = q->queuedata;
75 struct block_device *bdev = ns ? ns->disk->part0 : NULL;
77 struct bio *bio = NULL;
81 req = blk_mq_alloc_request(q, nvme_req_op(cmd) | rq_flags, blk_flags);
84 nvme_init_request(req, cmd);
87 req->timeout = timeout;
88 nvme_req(req)->flags |= NVME_REQ_USERCMD;
90 if (ubuffer && bufflen) {
92 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
95 struct iovec fast_iov[UIO_FASTIOV];
96 struct iovec *iov = fast_iov;
99 ret = import_iovec(rq_data_dir(req), ubuffer, bufflen,
100 UIO_FASTIOV, &iov, &iter);
103 ret = blk_rq_map_user_iov(q, req, NULL, &iter,
111 bio_set_dev(bio, bdev);
112 if (bdev && meta_buffer && meta_len) {
113 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
119 req->cmd_flags |= REQ_INTEGRITY;
128 blk_rq_unmap_user(bio);
130 blk_mq_free_request(req);
134 static int nvme_submit_user_cmd(struct request_queue *q,
135 struct nvme_command *cmd, void __user *ubuffer,
136 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
137 u32 meta_seed, u64 *result, unsigned timeout, bool vec)
144 req = nvme_alloc_user_request(q, cmd, ubuffer, bufflen, meta_buffer,
145 meta_len, meta_seed, &meta, timeout, vec, 0, 0);
151 ret = nvme_execute_passthru_rq(req);
154 *result = le64_to_cpu(nvme_req(req)->result.u64);
156 ret = nvme_finish_user_metadata(req, meta_buffer, meta,
159 blk_rq_unmap_user(bio);
160 blk_mq_free_request(req);
164 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
166 struct nvme_user_io io;
167 struct nvme_command c;
168 unsigned length, meta_len;
169 void __user *metadata;
171 if (copy_from_user(&io, uio, sizeof(io)))
179 case nvme_cmd_compare:
185 length = (io.nblocks + 1) << ns->lba_shift;
187 if ((io.control & NVME_RW_PRINFO_PRACT) &&
188 ns->ms == sizeof(struct t10_pi_tuple)) {
190 * Protection information is stripped/inserted by the
193 if (nvme_to_user_ptr(io.metadata))
198 meta_len = (io.nblocks + 1) * ns->ms;
199 metadata = nvme_to_user_ptr(io.metadata);
202 if (ns->features & NVME_NS_EXT_LBAS) {
205 } else if (meta_len) {
206 if ((io.metadata & 3) || !io.metadata)
210 memset(&c, 0, sizeof(c));
211 c.rw.opcode = io.opcode;
212 c.rw.flags = io.flags;
213 c.rw.nsid = cpu_to_le32(ns->head->ns_id);
214 c.rw.slba = cpu_to_le64(io.slba);
215 c.rw.length = cpu_to_le16(io.nblocks);
216 c.rw.control = cpu_to_le16(io.control);
217 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
218 c.rw.reftag = cpu_to_le32(io.reftag);
219 c.rw.apptag = cpu_to_le16(io.apptag);
220 c.rw.appmask = cpu_to_le16(io.appmask);
222 return nvme_submit_user_cmd(ns->queue, &c,
223 nvme_to_user_ptr(io.addr), length,
224 metadata, meta_len, lower_32_bits(io.slba), NULL, 0,
228 static bool nvme_validate_passthru_nsid(struct nvme_ctrl *ctrl,
229 struct nvme_ns *ns, __u32 nsid)
231 if (ns && nsid != ns->head->ns_id) {
232 dev_err(ctrl->device,
233 "%s: nsid (%u) in cmd does not match nsid (%u)"
235 current->comm, nsid, ns->head->ns_id);
242 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
243 struct nvme_passthru_cmd __user *ucmd)
245 struct nvme_passthru_cmd cmd;
246 struct nvme_command c;
247 unsigned timeout = 0;
251 if (!capable(CAP_SYS_ADMIN))
253 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
257 if (!nvme_validate_passthru_nsid(ctrl, ns, cmd.nsid))
260 memset(&c, 0, sizeof(c));
261 c.common.opcode = cmd.opcode;
262 c.common.flags = cmd.flags;
263 c.common.nsid = cpu_to_le32(cmd.nsid);
264 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
265 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
266 c.common.cdw10 = cpu_to_le32(cmd.cdw10);
267 c.common.cdw11 = cpu_to_le32(cmd.cdw11);
268 c.common.cdw12 = cpu_to_le32(cmd.cdw12);
269 c.common.cdw13 = cpu_to_le32(cmd.cdw13);
270 c.common.cdw14 = cpu_to_le32(cmd.cdw14);
271 c.common.cdw15 = cpu_to_le32(cmd.cdw15);
274 timeout = msecs_to_jiffies(cmd.timeout_ms);
276 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
277 nvme_to_user_ptr(cmd.addr), cmd.data_len,
278 nvme_to_user_ptr(cmd.metadata), cmd.metadata_len,
279 0, &result, timeout, false);
282 if (put_user(result, &ucmd->result))
289 static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
290 struct nvme_passthru_cmd64 __user *ucmd, bool vec)
292 struct nvme_passthru_cmd64 cmd;
293 struct nvme_command c;
294 unsigned timeout = 0;
297 if (!capable(CAP_SYS_ADMIN))
299 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
303 if (!nvme_validate_passthru_nsid(ctrl, ns, cmd.nsid))
306 memset(&c, 0, sizeof(c));
307 c.common.opcode = cmd.opcode;
308 c.common.flags = cmd.flags;
309 c.common.nsid = cpu_to_le32(cmd.nsid);
310 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
311 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
312 c.common.cdw10 = cpu_to_le32(cmd.cdw10);
313 c.common.cdw11 = cpu_to_le32(cmd.cdw11);
314 c.common.cdw12 = cpu_to_le32(cmd.cdw12);
315 c.common.cdw13 = cpu_to_le32(cmd.cdw13);
316 c.common.cdw14 = cpu_to_le32(cmd.cdw14);
317 c.common.cdw15 = cpu_to_le32(cmd.cdw15);
320 timeout = msecs_to_jiffies(cmd.timeout_ms);
322 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
323 nvme_to_user_ptr(cmd.addr), cmd.data_len,
324 nvme_to_user_ptr(cmd.metadata), cmd.metadata_len,
325 0, &cmd.result, timeout, vec);
328 if (put_user(cmd.result, &ucmd->result))
335 struct nvme_uring_data {
344 * This overlays struct io_uring_cmd pdu.
345 * Expect build errors if this grows larger than that.
347 struct nvme_uring_cmd_pdu {
352 void *meta; /* kernel-resident buffer */
353 void __user *meta_buffer;
357 static inline struct nvme_uring_cmd_pdu *nvme_uring_cmd_pdu(
358 struct io_uring_cmd *ioucmd)
360 return (struct nvme_uring_cmd_pdu *)&ioucmd->pdu;
363 static void nvme_uring_task_cb(struct io_uring_cmd *ioucmd)
365 struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
366 struct request *req = pdu->req;
367 struct bio *bio = req->bio;
371 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
374 status = nvme_req(req)->status;
376 result = le64_to_cpu(nvme_req(req)->result.u64);
379 status = nvme_finish_user_metadata(req, pdu->meta_buffer,
380 pdu->meta, pdu->meta_len, status);
382 blk_rq_unmap_user(bio);
383 blk_mq_free_request(req);
385 io_uring_cmd_done(ioucmd, status, result);
388 static void nvme_uring_cmd_end_io(struct request *req, blk_status_t err)
390 struct io_uring_cmd *ioucmd = req->end_io_data;
391 struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
392 /* extract bio before reusing the same field for request */
393 struct bio *bio = pdu->bio;
397 /* this takes care of moving rest of completion-work to task context */
398 io_uring_cmd_complete_in_task(ioucmd, nvme_uring_task_cb);
401 static int nvme_uring_cmd_io(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
402 struct io_uring_cmd *ioucmd, unsigned int issue_flags, bool vec)
404 struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
405 const struct nvme_uring_cmd *cmd = ioucmd->cmd;
406 struct request_queue *q = ns ? ns->queue : ctrl->admin_q;
407 struct nvme_uring_data d;
408 struct nvme_command c;
410 unsigned int rq_flags = 0;
411 blk_mq_req_flags_t blk_flags = 0;
414 if (!capable(CAP_SYS_ADMIN))
417 c.common.opcode = READ_ONCE(cmd->opcode);
418 c.common.flags = READ_ONCE(cmd->flags);
422 c.common.command_id = 0;
423 c.common.nsid = cpu_to_le32(cmd->nsid);
424 if (!nvme_validate_passthru_nsid(ctrl, ns, le32_to_cpu(c.common.nsid)))
427 c.common.cdw2[0] = cpu_to_le32(READ_ONCE(cmd->cdw2));
428 c.common.cdw2[1] = cpu_to_le32(READ_ONCE(cmd->cdw3));
429 c.common.metadata = 0;
430 c.common.dptr.prp1 = c.common.dptr.prp2 = 0;
431 c.common.cdw10 = cpu_to_le32(READ_ONCE(cmd->cdw10));
432 c.common.cdw11 = cpu_to_le32(READ_ONCE(cmd->cdw11));
433 c.common.cdw12 = cpu_to_le32(READ_ONCE(cmd->cdw12));
434 c.common.cdw13 = cpu_to_le32(READ_ONCE(cmd->cdw13));
435 c.common.cdw14 = cpu_to_le32(READ_ONCE(cmd->cdw14));
436 c.common.cdw15 = cpu_to_le32(READ_ONCE(cmd->cdw15));
438 d.metadata = READ_ONCE(cmd->metadata);
439 d.addr = READ_ONCE(cmd->addr);
440 d.data_len = READ_ONCE(cmd->data_len);
441 d.metadata_len = READ_ONCE(cmd->metadata_len);
442 d.timeout_ms = READ_ONCE(cmd->timeout_ms);
444 if (issue_flags & IO_URING_F_NONBLOCK) {
445 rq_flags = REQ_NOWAIT;
446 blk_flags = BLK_MQ_REQ_NOWAIT;
449 req = nvme_alloc_user_request(q, &c, nvme_to_user_ptr(d.addr),
450 d.data_len, nvme_to_user_ptr(d.metadata),
451 d.metadata_len, 0, &meta, d.timeout_ms ?
452 msecs_to_jiffies(d.timeout_ms) : 0, vec, rq_flags,
456 req->end_io_data = ioucmd;
458 /* to free bio on completion, as req->bio will be null at that time */
461 pdu->meta_buffer = nvme_to_user_ptr(d.metadata);
462 pdu->meta_len = d.metadata_len;
464 blk_execute_rq_nowait(req, 0, nvme_uring_cmd_end_io);
468 static bool is_ctrl_ioctl(unsigned int cmd)
470 if (cmd == NVME_IOCTL_ADMIN_CMD || cmd == NVME_IOCTL_ADMIN64_CMD)
472 if (is_sed_ioctl(cmd))
477 static int nvme_ctrl_ioctl(struct nvme_ctrl *ctrl, unsigned int cmd,
481 case NVME_IOCTL_ADMIN_CMD:
482 return nvme_user_cmd(ctrl, NULL, argp);
483 case NVME_IOCTL_ADMIN64_CMD:
484 return nvme_user_cmd64(ctrl, NULL, argp, false);
486 return sed_ioctl(ctrl->opal_dev, cmd, argp);
490 #ifdef COMPAT_FOR_U64_ALIGNMENT
491 struct nvme_user_io32 {
504 } __attribute__((__packed__));
505 #define NVME_IOCTL_SUBMIT_IO32 _IOW('N', 0x42, struct nvme_user_io32)
506 #endif /* COMPAT_FOR_U64_ALIGNMENT */
508 static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned int cmd,
513 force_successful_syscall_return();
514 return ns->head->ns_id;
515 case NVME_IOCTL_IO_CMD:
516 return nvme_user_cmd(ns->ctrl, ns, argp);
518 * struct nvme_user_io can have different padding on some 32-bit ABIs.
519 * Just accept the compat version as all fields that are used are the
520 * same size and at the same offset.
522 #ifdef COMPAT_FOR_U64_ALIGNMENT
523 case NVME_IOCTL_SUBMIT_IO32:
525 case NVME_IOCTL_SUBMIT_IO:
526 return nvme_submit_io(ns, argp);
527 case NVME_IOCTL_IO64_CMD:
528 return nvme_user_cmd64(ns->ctrl, ns, argp, false);
529 case NVME_IOCTL_IO64_CMD_VEC:
530 return nvme_user_cmd64(ns->ctrl, ns, argp, true);
536 static int __nvme_ioctl(struct nvme_ns *ns, unsigned int cmd, void __user *arg)
538 if (is_ctrl_ioctl(cmd))
539 return nvme_ctrl_ioctl(ns->ctrl, cmd, arg);
540 return nvme_ns_ioctl(ns, cmd, arg);
543 int nvme_ioctl(struct block_device *bdev, fmode_t mode,
544 unsigned int cmd, unsigned long arg)
546 struct nvme_ns *ns = bdev->bd_disk->private_data;
548 return __nvme_ioctl(ns, cmd, (void __user *)arg);
551 long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
554 container_of(file_inode(file)->i_cdev, struct nvme_ns, cdev);
556 return __nvme_ioctl(ns, cmd, (void __user *)arg);
559 static int nvme_uring_cmd_checks(unsigned int issue_flags)
561 /* IOPOLL not supported yet */
562 if (issue_flags & IO_URING_F_IOPOLL)
565 /* NVMe passthrough requires big SQE/CQE support */
566 if ((issue_flags & (IO_URING_F_SQE128|IO_URING_F_CQE32)) !=
567 (IO_URING_F_SQE128|IO_URING_F_CQE32))
572 static int nvme_ns_uring_cmd(struct nvme_ns *ns, struct io_uring_cmd *ioucmd,
573 unsigned int issue_flags)
575 struct nvme_ctrl *ctrl = ns->ctrl;
578 BUILD_BUG_ON(sizeof(struct nvme_uring_cmd_pdu) > sizeof(ioucmd->pdu));
580 ret = nvme_uring_cmd_checks(issue_flags);
584 switch (ioucmd->cmd_op) {
585 case NVME_URING_CMD_IO:
586 ret = nvme_uring_cmd_io(ctrl, ns, ioucmd, issue_flags, false);
588 case NVME_URING_CMD_IO_VEC:
589 ret = nvme_uring_cmd_io(ctrl, ns, ioucmd, issue_flags, true);
598 int nvme_ns_chr_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
600 struct nvme_ns *ns = container_of(file_inode(ioucmd->file)->i_cdev,
601 struct nvme_ns, cdev);
603 return nvme_ns_uring_cmd(ns, ioucmd, issue_flags);
606 #ifdef CONFIG_NVME_MULTIPATH
607 static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
608 void __user *argp, struct nvme_ns_head *head, int srcu_idx)
609 __releases(&head->srcu)
611 struct nvme_ctrl *ctrl = ns->ctrl;
614 nvme_get_ctrl(ns->ctrl);
615 srcu_read_unlock(&head->srcu, srcu_idx);
616 ret = nvme_ctrl_ioctl(ns->ctrl, cmd, argp);
622 int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
623 unsigned int cmd, unsigned long arg)
625 struct nvme_ns_head *head = bdev->bd_disk->private_data;
626 void __user *argp = (void __user *)arg;
628 int srcu_idx, ret = -EWOULDBLOCK;
630 srcu_idx = srcu_read_lock(&head->srcu);
631 ns = nvme_find_path(head);
636 * Handle ioctls that apply to the controller instead of the namespace
637 * seperately and drop the ns SRCU reference early. This avoids a
638 * deadlock when deleting namespaces using the passthrough interface.
640 if (is_ctrl_ioctl(cmd))
641 return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx);
643 ret = nvme_ns_ioctl(ns, cmd, argp);
645 srcu_read_unlock(&head->srcu, srcu_idx);
649 long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
652 struct cdev *cdev = file_inode(file)->i_cdev;
653 struct nvme_ns_head *head =
654 container_of(cdev, struct nvme_ns_head, cdev);
655 void __user *argp = (void __user *)arg;
657 int srcu_idx, ret = -EWOULDBLOCK;
659 srcu_idx = srcu_read_lock(&head->srcu);
660 ns = nvme_find_path(head);
664 if (is_ctrl_ioctl(cmd))
665 return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx);
667 ret = nvme_ns_ioctl(ns, cmd, argp);
669 srcu_read_unlock(&head->srcu, srcu_idx);
673 int nvme_ns_head_chr_uring_cmd(struct io_uring_cmd *ioucmd,
674 unsigned int issue_flags)
676 struct cdev *cdev = file_inode(ioucmd->file)->i_cdev;
677 struct nvme_ns_head *head = container_of(cdev, struct nvme_ns_head, cdev);
678 int srcu_idx = srcu_read_lock(&head->srcu);
679 struct nvme_ns *ns = nvme_find_path(head);
683 ret = nvme_ns_uring_cmd(ns, ioucmd, issue_flags);
684 srcu_read_unlock(&head->srcu, srcu_idx);
687 #endif /* CONFIG_NVME_MULTIPATH */
689 int nvme_dev_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
691 struct nvme_ctrl *ctrl = ioucmd->file->private_data;
694 ret = nvme_uring_cmd_checks(issue_flags);
698 switch (ioucmd->cmd_op) {
699 case NVME_URING_CMD_ADMIN:
700 ret = nvme_uring_cmd_io(ctrl, NULL, ioucmd, issue_flags, false);
702 case NVME_URING_CMD_ADMIN_VEC:
703 ret = nvme_uring_cmd_io(ctrl, NULL, ioucmd, issue_flags, true);
712 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
717 down_read(&ctrl->namespaces_rwsem);
718 if (list_empty(&ctrl->namespaces)) {
723 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
724 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
725 dev_warn(ctrl->device,
726 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
731 dev_warn(ctrl->device,
732 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
734 up_read(&ctrl->namespaces_rwsem);
736 ret = nvme_user_cmd(ctrl, ns, argp);
741 up_read(&ctrl->namespaces_rwsem);
745 long nvme_dev_ioctl(struct file *file, unsigned int cmd,
748 struct nvme_ctrl *ctrl = file->private_data;
749 void __user *argp = (void __user *)arg;
752 case NVME_IOCTL_ADMIN_CMD:
753 return nvme_user_cmd(ctrl, NULL, argp);
754 case NVME_IOCTL_ADMIN64_CMD:
755 return nvme_user_cmd64(ctrl, NULL, argp, false);
756 case NVME_IOCTL_IO_CMD:
757 return nvme_dev_user_cmd(ctrl, argp);
758 case NVME_IOCTL_RESET:
759 dev_warn(ctrl->device, "resetting controller\n");
760 return nvme_reset_ctrl_sync(ctrl);
761 case NVME_IOCTL_SUBSYS_RESET:
762 return nvme_reset_subsystem(ctrl);
763 case NVME_IOCTL_RESCAN:
764 nvme_queue_scan(ctrl);