nvme: protect against possible request reference after completion
Subsystems (6)
Sagi Grimberg
maintainerv3 cover nvme: protect against possible request reference after completion
Nothing in nvme protects against referencing a request after it was completed. For example, in case a buggy controller sends a completion twice for the same request, the host can access and modify a request that was already completed.
At best, this will cause a panic, but on the worst case, this can cause a silent data corruption if the request was already reused and executed by the time we reference it.
The nvme command_id is an opaque that we simply placed the request tag thus far. To protect against a access after completion, we introduce a generation counter to the upper 4-bits of the command_id that will increment every invocation and be validated upon the reception of a completion. This will limit the maximum queue depth to be effectively 4095, but we hardly ever use such long queues (in fabrics the maximum is already 1024).
Changes from v2:
- cc linux-nfs,linux-kernel for patch 1/4
- fix expected genctr print in patch 4/4
- match param_set_uint_minmax indentation
- collected review tags
Changes from v1:
- lift param_set_uint_minmax and reuse it
- simplify initialization in patch 3/4
Sagi Grimberg (4):
params: lift param_set_uint_minmax to common code
nvme-pci: limit maximum queue depth to 4095
nvme-tcp: don't check blk_mq_tag_to_rq when receiving pdu data
nvme: code command_id with a genctr for use-after-free validation
drivers/nvme/host/core.c | 3 ++- drivers/nvme/host/nvme.h | 47 ++++++++++++++++++++++++++++++++++++- drivers/nvme/host/pci.c | 17 ++++++-------- drivers/nvme/host/rdma.c | 4 ++-- drivers/nvme/host/tcp.c | 38 ++++++++++++------------------ drivers/nvme/target/loop.c | 4 ++-- include/linux/moduleparam.h | 3 +++ kernel/params.c | 19 +++++++++++++++ net/sunrpc/xprtsock.c | 18 -------------- 9 files changed, 96 insertions(+), 57 deletions(-)
Signature · 7 lines
2.27.0 _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Sagi Grimberg
maintainerPATCH v3 1/4 params: lift param_set_uint_minmax to common code
It is a useful helper hence move it to common code so others can enjoy it.
--- include/linux/moduleparam.h | 3 +++ kernel/params.c | 19 +++++++++++++++++++ net/sunrpc/xprtsock.c | 18 ------------------ 3 files changed, 22 insertions(+), 18 deletions(-)
include/linux/moduleparam.h +3 -0
@@ -432,6 +432,9 @@ extern const struct kernel_param_ops param_ops_uint;extern int param_set_uint (const char *val,const struct kernel_param *kp);extern int param_get_uint (char *buffer,const struct kernel_param *kp);#define param_check_uint (name, p) __param_check(name, p, unsigned int) +int param_set_uint_minmax (const char *val, +const struct kernel_param *kp, +unsigned int min,unsigned int max);extern const struct kernel_param_ops param_ops_long;extern int param_set_long (const char *val,const struct kernel_param *kp);
kernel/params.c +19 -0
@@ -243,6 +243,25 @@ STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", kstrtoul); STANDARD_PARAM_DEF(ullong, unsigned long long, "%llu", kstrtoull); STANDARD_PARAM_DEF(hexint, unsigned int, "%#08x", kstrtouint); +int param_set_uint_minmax (const char *val, +const struct kernel_param *kp, +unsigned int min,unsigned int max) +{ +unsigned int num; +int ret; + +if (!val) +return -EINVAL ; + ret =kstrtouint (val,0 , &num); +if (ret) +return ret; +if (num < min || num > max) +return -EINVAL ; + *((unsigned int *)kp->arg) = num; +return 0 ; +} +EXPORT_SYMBOL (param_set_uint_minmax); +int param_set_charp (const char *val,const struct kernel_param *kp) {if (strlen (val) >1024 ) {
net/sunrpc/xprtsock.c +0 -18
@@ -3130,24 +3130,6 @@ void cleanup_socket_xprt(void)xprt_unregister_transport (&xs_bc_tcp_transport); } -static int param_set_uint_minmax (const char *val, -const struct kernel_param *kp, -unsigned int min,unsigned int max) -{ -unsigned int num; -int ret; - -if (!val) -return -EINVAL ; - ret =kstrtouint (val,0 , &num); -if (ret) -return ret; -if (num < min || num > max) -return -EINVAL ; - *((unsigned int *)kp->arg) = num; -return 0 ; -} -static int param_set_portnr (const char *val,const struct kernel_param *kp) { return param_set_uint_minmax(val, kp,
Signature · 7 lines
2.27.0 _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH v3 1/4] params: lift param_set_uint_minmax to common code
Quoted 8 lines · Sagi Grimberg
It is a useful helper hence move it to common code so others can enjoy it.
Suggested-by Christoph Hellwig <[email protected]>Reviewed-by Chaitanya Kulkarni <[email protected]>Signed-off-by Sagi Grimberg <[email protected]>
Quoted 6 lines
--- include/linux/moduleparam.h | 3 +++ kernel/params.c | 19 +++++++++++++++++++ net/sunrpc/xprtsock.c | 18 ------------------ 3 files changed, 22 insertions(+), 18 deletions(-)
Cheers,
Hannes
Signature · 9 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH v3 1/4] params: lift param_set_uint_minmax to common code
In reply to Sagi GrimbergQuoted 8 lines · Sagi Grimberg
It is a useful helper hence move it to common code so others can enjoy it.
Suggested-by Christoph Hellwig <[email protected]>Reviewed-by Chaitanya Kulkarni <[email protected]>Signed-off-by Sagi Grimberg <[email protected]>
[...]
Quoted 8 lines
kernel/params.c +8
-0+int param_set_uint_minmax (const char *val, +const struct kernel_param *kp, +unsigned int min,unsigned int max) +{ +unsigned int num; +int ret; + +if (!val)
kernel/params.c STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", kstrtoul); · line 263
Quoted 10 lines
kernel/params.c +10
-0+return -EINVAL ; + ret =kstrtouint (val,0 , &num); +if (ret) +return ret; +if (num < min || num > max) +return -EINVAL ; + *((unsigned int *)kp->arg) = num; +return 0 ; +} +EXPORT_SYMBOL (param_set_uint_minmax);
Couldn't this be EXPORT_SYMBOL_GPL?
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Christoph Hellwig
maintainerRe: [PATCH v3 1/4] params: lift param_set_uint_minmax to common code
In reply to Sagi GrimbergThis needs a Cc to linux-kernel..
include/linux/moduleparam.h extern const struct kernel_param_ops param_ops_uint; · line 437
Quoted 7 lines
include/linux/moduleparam.h +3
-0@@ -432,6 +432,9 @@ extern const struct kernel_param_ops param_ops_uint;extern int param_set_uint (const char *val,const struct kernel_param *kp);extern int param_get_uint (char *buffer,const struct kernel_param *kp);#define param_check_uint (name, p) __param_check(name, p, unsigned int) +int param_set_uint_minmax (const char *val, +const struct kernel_param *kp, +unsigned int min,unsigned int max);
Super minor nitpick, but for consistency I'd move it above the param_check_uint definition/
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Sagi Grimberg
maintainerPATCH v3 2/4 nvme-pci: limit maximum queue depth to 4095
In reply to Sagi GrimbergWe are going to use the upper 4-bits of the command_id for a generation counter, so enforce the new queue depth upper limit. As we enforce both min and max queue depth, use param_set_uint_minmax istead of open coding it.
--- drivers/nvme/host/pci.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/nvme/host/pci.c +6 -9
@@ -60,6 +60,8 @@ MODULE_PARM_DESC(sgl_threshold,"Use SGLs when average request segment size is larger or equal to " "this size. Use 0 to disable SGLs."); +#define NVME_PCI_MIN_QUEUE_SIZE 2 +#define NVME_PCI_MAX_QUEUE_SIZE 4095static int io_queue_depth_set (const char *val,const struct kernel_param *kp);static const struct kernel_param_ops io_queue_depth_ops = { .set = io_queue_depth_set,
@@ -68,7 +70,7 @@ static const struct kernel_param_ops io_queue_depth_ops = {static unsigned int io_queue_depth =1024 ;module_param_cb (io_queue_depth, &io_queue_depth_ops, &io_queue_depth,0644 ); -MODULE_PARM_DESC (io_queue_depth,"set io queue depth, should >= 2" ); +MODULE_PARM_DESC (io_queue_depth,"set io queue depth, should >= 2 and < 4096" );static int io_queue_count_set (const char *val,const struct kernel_param *kp) {
@@ -157,14 +159,9 @@ struct nvme_dev {static int io_queue_depth_set (const char *val,const struct kernel_param *kp) { -int ret; -u32 n; - - ret =kstrtou32 (val,10 , &n); -if (ret !=0 || n <2 ) -return -EINVAL ; - -return param_set_uint (val, kp); +return param_set_uint_minmax (val, kp, +NVME_PCI_MIN_QUEUE_SIZE , +NVME_PCI_MAX_QUEUE_SIZE ); }static inline unsigned int sq_idx (unsigned int qid,u32 stride)
Signature · 7 lines
2.27.0 _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH v3 2/4] nvme-pci: limit maximum queue depth to 4095
Quoted 11 lines · Sagi Grimberg
We are going to use the upper 4-bits of the
command_idfor a generation counter, so enforce the new queue depth upper limit. As we enforce both min and max queue depth, useparam_set_uint_minmaxistead of open coding it.Reviewed-by Chaitanya Kulkarni <[email protected]>Signed-off-by Sagi Grimberg <[email protected]>--- drivers/nvme/host/pci.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
Cheers,
Hannes
Signature · 9 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH v3 2/4] nvme-pci: limit maximum queue depth to 4095
In reply to Sagi GrimbergQuoted 8 lines · Sagi Grimberg
We are going to use the upper 4-bits of the
command_idfor a generation counter, so enforce the new queue depth upper limit. As we enforce both min and max queue depth, useparam_set_uint_minmaxistead of open coding it.Reviewed-by Chaitanya Kulkarni <[email protected]>Signed-off-by Sagi Grimberg <[email protected]>---
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Sagi Grimberg
maintainerPATCH v3 3/4 nvme-tcp: don't check blk_mq_tag_to_rq when receiving pdu data
In reply to Sagi GrimbergWe already validate it when receiving the c2hdata pdu header and this is not changing so this is a redundant check.
--- drivers/nvme/host/tcp.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/nvme/host/tcp.c +3 -11
@@ -703,17 +703,9 @@ static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb, unsigned int *offset, size_t *len) { struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu; -struct nvme_tcp_request *req; -struct request *rq; - - rq =blk_mq_tag_to_rq (nvme_tcp_tagset (queue), pdu->command_id); -if (!rq) { -dev_err (queue->ctrl->ctrl.device, -"queue %d tag %#x not found\n" , -nvme_tcp_queue_id (queue), pdu->command_id); -return -ENOENT ; - } - req =blk_mq_rq_to_pdu (rq); +struct request *rq = +blk_mq_tag_to_rq (nvme_tcp_tagset (queue), pdu->command_id); +struct nvme_tcp_request *req =blk_mq_rq_to_pdu (rq);while (true) {int recv_len, ret;
Signature · 7 lines
2.27.0 _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH v3 3/4] nvme-tcp: don't check blk_mq_tag_to_rq when receiving pdu data
Quoted 5 lines · Sagi Grimberg
We already validate it when receiving the c2hdata pdu header and this is not changing so this is a redundant check.
Reviewed-by Hannes Reinecke <[email protected]>Signed-off-by Sagi Grimberg <[email protected]>
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Sagi Grimberg
maintainerPATCH v3 4/4 nvme: code command_id with a genctr for use-after-free validation
In reply to Sagi GrimbergWe cannot detect a (perhaps buggy) controller that is sending us a completion for a request that was already completed (for example sending a completion twice), this phenomenon was seen in the wild a few times.
So to protect against this, we use the upper 4 msbits of the nvme sqe command_id to use as a 4-bit generation counter and verify it matches the existing request generation that is incrementing on every execution.
The 16-bit command_id structure now is constructed by:
| xxxx | xxxxxxxxxxxx | gen request tag
This means that we are giving up some possible queue depth as 12 bits allow for a maximum queue depth of 4095 instead of 65536, however we never create such long queues anyways so no real harm done.
--- drivers/nvme/host/core.c | 3 ++- drivers/nvme/host/nvme.h | 47 +++++++++++++++++++++++++++++++++++++- drivers/nvme/host/pci.c | 2 +- drivers/nvme/host/rdma.c | 4 ++-- drivers/nvme/host/tcp.c | 26 ++++++++++----------- drivers/nvme/target/loop.c | 4 ++-- 6 files changed, 66 insertions(+), 20 deletions(-)
drivers/nvme/host/core.c +2 -1
@@ -1026,7 +1026,8 @@ blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req)return BLK_STS_IOERR ; } - cmd->common.command_id = req->tag; +nvme_req (req)->genctr++; + cmd->common.command_id =nvme_cid (req);trace_nvme_setup_cmd (req, cmd);return ret; }
drivers/nvme/host/nvme.h +46 -1
65 lines not on this page · raw message
drivers/nvme/host/pci.c +1 -1
@@ -1012,7 +1012,7 @@ static inline void nvme_handle_cqe(struct nvme_queue *nvmeq, u16 idx)return ; } - req =blk_mq_tag_to_rq (nvme_queue_tagset (nvmeq), command_id); + req =nvme_find_rq (nvme_queue_tagset (nvmeq), command_id);if (unlikely (!req)) { dev_warn(nvmeq->dev->ctrl.device, "invalid id %d completed on queue %d\n",
drivers/nvme/host/rdma.c +2 -2
@@ -1730,10 +1730,10 @@ static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,struct request *rq;struct nvme_rdma_request *req; - rq =blk_mq_tag_to_rq (nvme_rdma_tagset (queue), cqe->command_id); + rq =nvme_find_rq (nvme_rdma_tagset (queue), cqe->command_id);if (!rq) {dev_err (queue->ctrl->ctrl.device, -"tag 0x%x on QP %#x not found\n" , +"got bad command_id %#x on QP %#x\n" , cqe->command_id, queue->qp->qp_num);nvme_rdma_error_recovery (queue->ctrl);return ;
drivers/nvme/host/tcp.c +13 -13
@@ -488,11 +488,11 @@ static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue, {struct request *rq; - rq =blk_mq_tag_to_rq (nvme_tcp_tagset (queue), cqe->command_id); + rq =nvme_find_rq (nvme_tcp_tagset (queue), cqe->command_id);if (!rq) {dev_err (queue->ctrl->ctrl.device, -"queue %d tag 0x%x not found\n" , -nvme_tcp_queue_id (queue), cqe->command_id); +"got bad cqe.command_id %#x on queue %d\n" , + cqe->command_id,nvme_tcp_queue_id (queue));nvme_tcp_error_recovery (&queue->ctrl->ctrl);return -EINVAL ; }
@@ -509,11 +509,11 @@ static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue, {struct request *rq; - rq =blk_mq_tag_to_rq (nvme_tcp_tagset (queue), pdu->command_id); + rq =nvme_find_rq (nvme_tcp_tagset (queue), pdu->command_id);if (!rq) {dev_err (queue->ctrl->ctrl.device, -"queue %d tag %#x not found\n" , -nvme_tcp_queue_id (queue), pdu->command_id); +"got bad c2hdata.command_id %#x on queue %d\n" , + pdu->command_id,nvme_tcp_queue_id (queue));return -ENOENT ; }
@@ -607,7 +607,7 @@ static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req, data->hdr.plen =cpu_to_le32 (data->hdr.hlen + hdgst + req->pdu_len + ddgst); data->ttag = pdu->ttag; - data->command_id = rq->tag; + data->command_id =nvme_cid (rq); data->data_offset =cpu_to_le32 (req->data_sent); data->data_length =cpu_to_le32 (req->pdu_len);return 0 ;
@@ -620,11 +620,11 @@ static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,struct request *rq;int ret; - rq =blk_mq_tag_to_rq (nvme_tcp_tagset (queue), pdu->command_id); + rq =nvme_find_rq (nvme_tcp_tagset (queue), pdu->command_id);if (!rq) {dev_err (queue->ctrl->ctrl.device, -"queue %d tag %#x not found\n" , -nvme_tcp_queue_id (queue), pdu->command_id); +"got bad r2t.command_id %#x on queue %d\n" , + pdu->command_id,nvme_tcp_queue_id (queue));return -ENOENT ; } req =blk_mq_rq_to_pdu (rq);
@@ -704,7 +704,7 @@ static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb, {struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;struct request *rq = -blk_mq_tag_to_rq (nvme_tcp_tagset (queue), pdu->command_id); +nvme_cid_to_rq (nvme_tcp_tagset (queue), pdu->command_id);struct nvme_tcp_request *req =blk_mq_rq_to_pdu (rq);while (true) {
@@ -797,8 +797,8 @@ static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue, }if (pdu->hdr.flags &NVME_TCP_F_DATA_SUCCESS ) { -struct request *rq =blk_mq_tag_to_rq (nvme_tcp_tagset (queue), - pdu->command_id); +struct request *rq =nvme_cid_to_rq (nvme_tcp_tagset (queue), + pdu->command_id);nvme_tcp_end_request (rq,NVME_SC_SUCCESS ); queue->nr_cqe++;
drivers/nvme/target/loop.c +2 -2
@@ -107,10 +107,10 @@ static void nvme_loop_queue_response(struct nvmet_req *req) }else {struct request *rq; - rq =blk_mq_tag_to_rq (nvme_loop_tagset (queue), cqe->command_id); + rq =nvme_find_rq (nvme_loop_tagset (queue), cqe->command_id);if (!rq) {dev_err (queue->ctrl->ctrl.device, -"tag 0x%x on queue %d not found\n" , +"got bad command_id %#x on queue %d\n" , cqe->command_id,nvme_loop_queue_idx (queue));return ; }
Signature · 7 lines
2.27.0 _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH v3 4/4] nvme: code command_id with a genctr for use-after-free validation
Quoted 13 lines · Sagi Grimberg
We cannot detect a (perhaps buggy) controller that is sending us a completion for a request that was already completed (for example sending a completion twice), this phenomenon was seen in the wild a few times.
So to protect against this, we use the upper 4 msbits of the nvme sqe
command_idto use as a 4-bit generation counter and verify it matches the existing request generation that is incrementing on every execution.The 16-bit
command_idstructure now is constructed by:| xxxx | xxxxxxxxxxxx | gen request tag
Quoted 8 lines
This means that we are giving up some possible queue depth as 12 bits allow for a maximum queue depth of 4095 instead of 65536, however we never create such long queues anyways so no real harm done.
Suggested-by Keith Busch <[email protected]>Reviewed-by Hannes Reinecke <[email protected]>Acked-by Keith Busch <[email protected]>Signed-off-by Sagi Grimberg <[email protected]>
I've tested (only functional) this on FC (NetApp target). All looks good.
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Christoph Hellwig
maintainerThanks,
applies to nvme-5.15.
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme