aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/infiniband/core/uverbs_main.c
diff options
context:
space:
mode:
authorYann Droneaud <ydroneaud@opteya.com>2013-11-06 17:21:49 -0500
committerRoland Dreier <roland@purestorage.com>2013-11-17 11:22:09 -0500
commitf21519b23c1b6fa25366be4114ccf7fcf1c190f9 (patch)
tree66a93d5621a3d992f7f4b5410846bfc89e0b6157 /drivers/infiniband/core/uverbs_main.c
parent2490f20be496c2da14ae4632a8c60e0633e97fd0 (diff)
IB/core: extended command: an improved infrastructure for uverbs commands
Commit 400dbc96583f ("IB/core: Infrastructure for extensible uverbs commands") added an infrastructure for extensible uverbs commands while later commit 436f2ad05a0b ("IB/core: Export ib_create/destroy_flow through uverbs") exported ib_create_flow()/ib_destroy_flow() functions using this new infrastructure. According to the commit 400dbc96583f, the purpose of this infrastructure is to support passing around provider (eg. hardware) specific buffers when userspace issue commands to the kernel, so that it would be possible to extend uverbs (eg. core) buffers independently from the provider buffers. But the new kernel command function prototypes were not modified to take advantage of this extension. This issue was exposed by Roland Dreier in a previous review[1]. So the following patch is an attempt to a revised extensible command infrastructure. This improved extensible command infrastructure distinguish between core (eg. legacy)'s command/response buffers from provider (eg. hardware)'s command/response buffers: each extended command implementing function is given a struct ib_udata to hold core (eg. uverbs) input and output buffers, and another struct ib_udata to hold the hw (eg. provider) input and output buffers. Having those buffers identified separately make it easier to increase one buffer to support extension without having to add some code to guess the exact size of each command/response parts: This should make the extended functions more reliable. Additionally, instead of relying on command identifier being greater than IB_USER_VERBS_CMD_THRESHOLD, the proposed infrastructure rely on unused bits in command field: on the 32 bits provided by command field, only 6 bits are really needed to encode the identifier of commands currently supported by the kernel. (Even using only 6 bits leaves room for about 23 new commands). So this patch makes use of some high order bits in command field to store flags, leaving enough room for more command identifiers than one will ever need (eg. 256). The new flags are used to specify if the command should be processed as an extended one or a legacy one. While designing the new command format, care was taken to make usage of flags itself extensible. Using high order bits of the commands field ensure that newer libibverbs on older kernel will properly fail when trying to call extended commands. On the other hand, older libibverbs on newer kernel will never be able to issue calls to extended commands. The extended command header includes the optional response pointer so that output buffer length and output buffer pointer are located together in the command, allowing proper parameters checking. This should make implementing functions easier and safer. Additionally the extended header ensure 64bits alignment, while making all sizes multiple of 8 bytes, extending the maximum buffer size: legacy extended Maximum command buffer: 256KBytes 1024KBytes (512KBytes + 512KBytes) Maximum response buffer: 256KBytes 1024KBytes (512KBytes + 512KBytes) For the purpose of doing proper buffer size accounting, the headers size are no more taken in account in "in_words". One of the odds of the current extensible infrastructure, reading twice the "legacy" command header, is fixed by removing the "legacy" command header from the extended command header: they are processed as two different parts of the command: memory is read once and information are not duplicated: it's making clear that's an extended command scheme and not a different command scheme. The proposed scheme will format input (command) and output (response) buffers this way: - command: legacy header + extended header + command data (core + hw): +----------------------------------------+ | flags | 00 00 | command | | in_words | out_words | +----------------------------------------+ | response | | response | | provider_in_words | provider_out_words | | padding | +----------------------------------------+ | | . <uverbs input> . . (in_words * 8) . | | +----------------------------------------+ | | . <provider input> . . (provider_in_words * 8) . | | +----------------------------------------+ - response, if present: +----------------------------------------+ | | . <uverbs output space> . . (out_words * 8) . | | +----------------------------------------+ | | . <provider output space> . . (provider_out_words * 8) . | | +----------------------------------------+ The overall design is to ensure that the extensible infrastructure is itself extensible while begin more reliable with more input and bound checking. Note: The unused field in the extended header would be perfect candidate to hold the command "comp_mask" (eg. bit field used to handle compatibility). This was suggested by Roland Dreier in a previous review[2]. But "comp_mask" field is likely to be present in the uverb input and/or provider input, likewise for the response, as noted by Matan Barak[3], so it doesn't make sense to put "comp_mask" in the header. [1]: http://marc.info/?i=CAL1RGDWxmM17W2o_era24A-TTDeKyoL6u3NRu_=t_dhV_ZA9MA@mail.gmail.com [2]: http://marc.info/?i=CAL1RGDXJtrc849M6_XNZT5xO1+ybKtLWGq6yg6LhoSsKpsmkYA@mail.gmail.com [3]: http://marc.info/?i=525C1149.6000701@mellanox.com Signed-off-by: Yann Droneaud <ydroneaud@opteya.com> Link: http://marc.info/?i=cover.1383773832.git.ydroneaud@opteya.com [ Convert "ret ? ret : 0" to the equivalent "ret". - Roland ] Signed-off-by: Roland Dreier <roland@purestorage.com>
Diffstat (limited to 'drivers/infiniband/core/uverbs_main.c')
-rw-r--r--drivers/infiniband/core/uverbs_main.c127
1 files changed, 98 insertions, 29 deletions
diff --git a/drivers/infiniband/core/uverbs_main.c b/drivers/infiniband/core/uverbs_main.c
index 2df31f68ea09..189d99e76d9f 100644
--- a/drivers/infiniband/core/uverbs_main.c
+++ b/drivers/infiniband/core/uverbs_main.c
@@ -115,11 +115,16 @@ static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
115 [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd, 115 [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd,
116 [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq, 116 [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq,
117 [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp, 117 [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp,
118};
119
118#ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 120#ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING
119 [IB_USER_VERBS_CMD_CREATE_FLOW] = ib_uverbs_create_flow, 121static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
120 [IB_USER_VERBS_CMD_DESTROY_FLOW] = ib_uverbs_destroy_flow 122 struct ib_udata *ucore,
121#endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 123 struct ib_udata *uhw) = {
124 [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow,
125 [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow
122}; 126};
127#endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */
123 128
124static void ib_uverbs_add_one(struct ib_device *device); 129static void ib_uverbs_add_one(struct ib_device *device);
125static void ib_uverbs_remove_one(struct ib_device *device); 130static void ib_uverbs_remove_one(struct ib_device *device);
@@ -589,6 +594,7 @@ static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
589{ 594{
590 struct ib_uverbs_file *file = filp->private_data; 595 struct ib_uverbs_file *file = filp->private_data;
591 struct ib_uverbs_cmd_hdr hdr; 596 struct ib_uverbs_cmd_hdr hdr;
597 __u32 flags;
592 598
593 if (count < sizeof hdr) 599 if (count < sizeof hdr)
594 return -EINVAL; 600 return -EINVAL;
@@ -596,45 +602,108 @@ static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
596 if (copy_from_user(&hdr, buf, sizeof hdr)) 602 if (copy_from_user(&hdr, buf, sizeof hdr))
597 return -EFAULT; 603 return -EFAULT;
598 604
599 if (hdr.command >= ARRAY_SIZE(uverbs_cmd_table) || 605 flags = (hdr.command &
600 !uverbs_cmd_table[hdr.command]) 606 IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT;
601 return -EINVAL;
602 607
603 if (!file->ucontext && 608 if (!flags) {
604 hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT) 609 __u32 command;
605 return -EINVAL;
606 610
607 if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << hdr.command))) 611 if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
608 return -ENOSYS; 612 IB_USER_VERBS_CMD_COMMAND_MASK))
613 return -EINVAL;
609 614
610#ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 615 command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
611 if (hdr.command >= IB_USER_VERBS_CMD_THRESHOLD) {
612 struct ib_uverbs_cmd_hdr_ex hdr_ex;
613 616
614 if (copy_from_user(&hdr_ex, buf, sizeof(hdr_ex))) 617 if (command >= ARRAY_SIZE(uverbs_cmd_table) ||
615 return -EFAULT; 618 !uverbs_cmd_table[command])
619 return -EINVAL;
616 620
617 if (((hdr_ex.in_words + hdr_ex.provider_in_words) * 4) != count) 621 if (!file->ucontext &&
622 command != IB_USER_VERBS_CMD_GET_CONTEXT)
618 return -EINVAL; 623 return -EINVAL;
619 624
620 return uverbs_cmd_table[hdr.command](file, 625 if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << command)))
621 buf + sizeof(hdr_ex), 626 return -ENOSYS;
622 (hdr_ex.in_words + 627
623 hdr_ex.provider_in_words) * 4,
624 (hdr_ex.out_words +
625 hdr_ex.provider_out_words) * 4);
626 } else {
627#endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */
628 if (hdr.in_words * 4 != count) 628 if (hdr.in_words * 4 != count)
629 return -EINVAL; 629 return -EINVAL;
630 630
631 return uverbs_cmd_table[hdr.command](file, 631 return uverbs_cmd_table[command](file,
632 buf + sizeof(hdr), 632 buf + sizeof(hdr),
633 hdr.in_words * 4, 633 hdr.in_words * 4,
634 hdr.out_words * 4); 634 hdr.out_words * 4);
635
635#ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 636#ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING
637
638 } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) {
639 __u32 command;
640
641 struct ib_uverbs_ex_cmd_hdr ex_hdr;
642 struct ib_udata ucore;
643 struct ib_udata uhw;
644 int err;
645 size_t written_count = count;
646
647 if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
648 IB_USER_VERBS_CMD_COMMAND_MASK))
649 return -EINVAL;
650
651 command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
652
653 if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) ||
654 !uverbs_ex_cmd_table[command])
655 return -ENOSYS;
656
657 if (!file->ucontext)
658 return -EINVAL;
659
660 if (!(file->device->ib_dev->uverbs_ex_cmd_mask & (1ull << command)))
661 return -ENOSYS;
662
663 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
664 return -EINVAL;
665
666 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
667 return -EFAULT;
668
669 count -= sizeof(hdr) + sizeof(ex_hdr);
670 buf += sizeof(hdr) + sizeof(ex_hdr);
671
672 if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count)
673 return -EINVAL;
674
675 if (ex_hdr.response) {
676 if (!hdr.out_words && !ex_hdr.provider_out_words)
677 return -EINVAL;
678 } else {
679 if (hdr.out_words || ex_hdr.provider_out_words)
680 return -EINVAL;
681 }
682
683 INIT_UDATA(&ucore,
684 (hdr.in_words) ? buf : 0,
685 (unsigned long)ex_hdr.response,
686 hdr.in_words * 8,
687 hdr.out_words * 8);
688
689 INIT_UDATA(&uhw,
690 (ex_hdr.provider_in_words) ? buf + ucore.inlen : 0,
691 (ex_hdr.provider_out_words) ? (unsigned long)ex_hdr.response + ucore.outlen : 0,
692 ex_hdr.provider_in_words * 8,
693 ex_hdr.provider_out_words * 8);
694
695 err = uverbs_ex_cmd_table[command](file,
696 &ucore,
697 &uhw);
698
699 if (err)
700 return err;
701
702 return written_count;
636 } 703 }
637#endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 704#endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */
705
706 return -ENOSYS;
638} 707}
639 708
640static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma) 709static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)