Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Yann Droneaud and committed by
Roland Dreier
f21519b2 2490f20b

+165 -78
+18 -2
drivers/infiniband/core/uverbs.h
··· 47 47 #include <rdma/ib_umem.h> 48 48 #include <rdma/ib_user_verbs.h> 49 49 50 + #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \ 51 + do { \ 52 + (udata)->inbuf = (void __user *) (ibuf); \ 53 + (udata)->outbuf = (void __user *) (obuf); \ 54 + (udata)->inlen = (ilen); \ 55 + (udata)->outlen = (olen); \ 56 + } while (0) 57 + 50 58 /* 51 59 * Our lifetime rules for these structs are the following: 52 60 * ··· 241 233 IB_UVERBS_DECLARE_CMD(create_xsrq); 242 234 IB_UVERBS_DECLARE_CMD(open_xrcd); 243 235 IB_UVERBS_DECLARE_CMD(close_xrcd); 236 + 244 237 #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 245 - IB_UVERBS_DECLARE_CMD(create_flow); 246 - IB_UVERBS_DECLARE_CMD(destroy_flow); 238 + 239 + #define IB_UVERBS_DECLARE_EX_CMD(name) \ 240 + int ib_uverbs_ex_##name(struct ib_uverbs_file *file, \ 241 + struct ib_udata *ucore, \ 242 + struct ib_udata *uhw) 243 + 244 + IB_UVERBS_DECLARE_EX_CMD(create_flow); 245 + IB_UVERBS_DECLARE_EX_CMD(destroy_flow); 246 + 247 247 #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 248 248 249 249 #endif /* UVERBS_H */
+26 -30
drivers/infiniband/core/uverbs_cmd.c
··· 58 58 static struct uverbs_lock_class rule_lock_class = { .name = "RULE-uobj" }; 59 59 #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 60 60 61 - #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \ 62 - do { \ 63 - (udata)->inbuf = (void __user *) (ibuf); \ 64 - (udata)->outbuf = (void __user *) (obuf); \ 65 - (udata)->inlen = (ilen); \ 66 - (udata)->outlen = (olen); \ 67 - } while (0) 68 - 69 61 /* 70 62 * The ib_uobject locking scheme is as follows: 71 63 * ··· 2634 2642 return 0; 2635 2643 } 2636 2644 2637 - ssize_t ib_uverbs_create_flow(struct ib_uverbs_file *file, 2638 - const char __user *buf, int in_len, 2639 - int out_len) 2645 + int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file, 2646 + struct ib_udata *ucore, 2647 + struct ib_udata *uhw) 2640 2648 { 2641 2649 struct ib_uverbs_create_flow cmd; 2642 2650 struct ib_uverbs_create_flow_resp resp; ··· 2650 2658 void *ib_spec; 2651 2659 int i; 2652 2660 2653 - if (out_len < sizeof(resp)) 2661 + if (ucore->outlen < sizeof(resp)) 2654 2662 return -ENOSPC; 2655 2663 2656 - if (copy_from_user(&cmd, buf, sizeof(cmd))) 2657 - return -EFAULT; 2664 + err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd)); 2665 + if (err) 2666 + return err; 2667 + 2668 + ucore->inbuf += sizeof(cmd); 2669 + ucore->inlen -= sizeof(cmd); 2658 2670 2659 2671 if (cmd.comp_mask) 2660 2672 return -EINVAL; ··· 2670 2674 if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS) 2671 2675 return -EINVAL; 2672 2676 2673 - if (cmd.flow_attr.size > (in_len - sizeof(cmd)) || 2677 + if (cmd.flow_attr.size > ucore->inlen || 2674 2678 cmd.flow_attr.size > 2675 2679 (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec))) 2676 2680 return -EINVAL; ··· 2682 2686 return -ENOMEM; 2683 2687 2684 2688 memcpy(kern_flow_attr, &cmd.flow_attr, sizeof(*kern_flow_attr)); 2685 - if (copy_from_user(kern_flow_attr + 1, buf + sizeof(cmd), 2686 - cmd.flow_attr.size)) { 2687 - err = -EFAULT; 2689 + err = ib_copy_from_udata(kern_flow_attr + 1, ucore, 2690 + cmd.flow_attr.size); 2691 + if (err) 2688 2692 goto err_free_attr; 2689 - } 2690 2693 } else { 2691 2694 kern_flow_attr = &cmd.flow_attr; 2692 2695 } ··· 2753 2758 memset(&resp, 0, sizeof(resp)); 2754 2759 resp.flow_handle = uobj->id; 2755 2760 2756 - if (copy_to_user((void __user *)(unsigned long) cmd.response, 2757 - &resp, sizeof(resp))) { 2758 - err = -EFAULT; 2761 + err = ib_copy_to_udata(ucore, 2762 + &resp, sizeof(resp)); 2763 + if (err) 2759 2764 goto err_copy; 2760 - } 2761 2765 2762 2766 put_qp_read(qp); 2763 2767 mutex_lock(&file->mutex); ··· 2769 2775 kfree(flow_attr); 2770 2776 if (cmd.flow_attr.num_of_specs) 2771 2777 kfree(kern_flow_attr); 2772 - return in_len; 2778 + return 0; 2773 2779 err_copy: 2774 2780 idr_remove_uobj(&ib_uverbs_rule_idr, uobj); 2775 2781 destroy_flow: ··· 2786 2792 return err; 2787 2793 } 2788 2794 2789 - ssize_t ib_uverbs_destroy_flow(struct ib_uverbs_file *file, 2790 - const char __user *buf, int in_len, 2791 - int out_len) { 2795 + int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file, 2796 + struct ib_udata *ucore, 2797 + struct ib_udata *uhw) 2798 + { 2792 2799 struct ib_uverbs_destroy_flow cmd; 2793 2800 struct ib_flow *flow_id; 2794 2801 struct ib_uobject *uobj; 2795 2802 int ret; 2796 2803 2797 - if (copy_from_user(&cmd, buf, sizeof(cmd))) 2798 - return -EFAULT; 2804 + ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd)); 2805 + if (ret) 2806 + return ret; 2799 2807 2800 2808 uobj = idr_write_uobj(&ib_uverbs_rule_idr, cmd.flow_handle, 2801 2809 file->ucontext); ··· 2819 2823 2820 2824 put_uobj(uobj); 2821 2825 2822 - return ret ? ret : in_len; 2826 + return ret; 2823 2827 } 2824 2828 #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 2825 2829
+102 -33
drivers/infiniband/core/uverbs_main.c
··· 115 115 [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd, 116 116 [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq, 117 117 [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp, 118 - #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 119 - [IB_USER_VERBS_CMD_CREATE_FLOW] = ib_uverbs_create_flow, 120 - [IB_USER_VERBS_CMD_DESTROY_FLOW] = ib_uverbs_destroy_flow 121 - #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 122 118 }; 119 + 120 + #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 121 + static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file, 122 + struct ib_udata *ucore, 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 126 + }; 127 + #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 123 128 124 129 static void ib_uverbs_add_one(struct ib_device *device); 125 130 static void ib_uverbs_remove_one(struct ib_device *device); ··· 594 589 { 595 590 struct ib_uverbs_file *file = filp->private_data; 596 591 struct ib_uverbs_cmd_hdr hdr; 592 + __u32 flags; 597 593 598 594 if (count < sizeof hdr) 599 595 return -EINVAL; ··· 602 596 if (copy_from_user(&hdr, buf, sizeof hdr)) 603 597 return -EFAULT; 604 598 605 - if (hdr.command >= ARRAY_SIZE(uverbs_cmd_table) || 606 - !uverbs_cmd_table[hdr.command]) 607 - return -EINVAL; 599 + flags = (hdr.command & 600 + IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT; 608 601 609 - if (!file->ucontext && 610 - hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT) 611 - return -EINVAL; 602 + if (!flags) { 603 + __u32 command; 612 604 613 - if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << hdr.command))) 614 - return -ENOSYS; 615 - 616 - #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 617 - if (hdr.command >= IB_USER_VERBS_CMD_THRESHOLD) { 618 - struct ib_uverbs_cmd_hdr_ex hdr_ex; 619 - 620 - if (copy_from_user(&hdr_ex, buf, sizeof(hdr_ex))) 621 - return -EFAULT; 622 - 623 - if (((hdr_ex.in_words + hdr_ex.provider_in_words) * 4) != count) 605 + if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK | 606 + IB_USER_VERBS_CMD_COMMAND_MASK)) 624 607 return -EINVAL; 625 608 626 - return uverbs_cmd_table[hdr.command](file, 627 - buf + sizeof(hdr_ex), 628 - (hdr_ex.in_words + 629 - hdr_ex.provider_in_words) * 4, 630 - (hdr_ex.out_words + 631 - hdr_ex.provider_out_words) * 4); 632 - } else { 633 - #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 609 + command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK; 610 + 611 + if (command >= ARRAY_SIZE(uverbs_cmd_table) || 612 + !uverbs_cmd_table[command]) 613 + return -EINVAL; 614 + 615 + if (!file->ucontext && 616 + command != IB_USER_VERBS_CMD_GET_CONTEXT) 617 + return -EINVAL; 618 + 619 + if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << command))) 620 + return -ENOSYS; 621 + 634 622 if (hdr.in_words * 4 != count) 635 623 return -EINVAL; 636 624 637 - return uverbs_cmd_table[hdr.command](file, 638 - buf + sizeof(hdr), 639 - hdr.in_words * 4, 640 - hdr.out_words * 4); 625 + return uverbs_cmd_table[command](file, 626 + buf + sizeof(hdr), 627 + hdr.in_words * 4, 628 + hdr.out_words * 4); 629 + 641 630 #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 631 + 632 + } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) { 633 + __u32 command; 634 + 635 + struct ib_uverbs_ex_cmd_hdr ex_hdr; 636 + struct ib_udata ucore; 637 + struct ib_udata uhw; 638 + int err; 639 + size_t written_count = count; 640 + 641 + if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK | 642 + IB_USER_VERBS_CMD_COMMAND_MASK)) 643 + return -EINVAL; 644 + 645 + command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK; 646 + 647 + if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) || 648 + !uverbs_ex_cmd_table[command]) 649 + return -ENOSYS; 650 + 651 + if (!file->ucontext) 652 + return -EINVAL; 653 + 654 + if (!(file->device->ib_dev->uverbs_ex_cmd_mask & (1ull << command))) 655 + return -ENOSYS; 656 + 657 + if (count < (sizeof(hdr) + sizeof(ex_hdr))) 658 + return -EINVAL; 659 + 660 + if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) 661 + return -EFAULT; 662 + 663 + count -= sizeof(hdr) + sizeof(ex_hdr); 664 + buf += sizeof(hdr) + sizeof(ex_hdr); 665 + 666 + if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count) 667 + return -EINVAL; 668 + 669 + if (ex_hdr.response) { 670 + if (!hdr.out_words && !ex_hdr.provider_out_words) 671 + return -EINVAL; 672 + } else { 673 + if (hdr.out_words || ex_hdr.provider_out_words) 674 + return -EINVAL; 675 + } 676 + 677 + INIT_UDATA(&ucore, 678 + (hdr.in_words) ? buf : 0, 679 + (unsigned long)ex_hdr.response, 680 + hdr.in_words * 8, 681 + hdr.out_words * 8); 682 + 683 + INIT_UDATA(&uhw, 684 + (ex_hdr.provider_in_words) ? buf + ucore.inlen : 0, 685 + (ex_hdr.provider_out_words) ? (unsigned long)ex_hdr.response + ucore.outlen : 0, 686 + ex_hdr.provider_in_words * 8, 687 + ex_hdr.provider_out_words * 8); 688 + 689 + err = uverbs_ex_cmd_table[command](file, 690 + &ucore, 691 + &uhw); 692 + 693 + if (err) 694 + return err; 695 + 696 + return written_count; 642 697 } 643 698 #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 699 + 700 + return -ENOSYS; 644 701 } 645 702 646 703 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
+3 -3
drivers/infiniband/hw/mlx4/main.c
··· 1692 1692 ibdev->ib_dev.destroy_flow = mlx4_ib_destroy_flow; 1693 1693 1694 1694 #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 1695 - ibdev->ib_dev.uverbs_cmd_mask |= 1696 - (1ull << IB_USER_VERBS_CMD_CREATE_FLOW) | 1697 - (1ull << IB_USER_VERBS_CMD_DESTROY_FLOW); 1695 + ibdev->ib_dev.uverbs_ex_cmd_mask |= 1696 + (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 1697 + (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW); 1698 1698 #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 1699 1699 } 1700 1700
+1
include/rdma/ib_verbs.h
··· 1436 1436 1437 1437 int uverbs_abi_ver; 1438 1438 u64 uverbs_cmd_mask; 1439 + u64 uverbs_ex_cmd_mask; 1439 1440 1440 1441 char node_desc[64]; 1441 1442 __be64 node_guid;
+15 -10
include/uapi/rdma/ib_user_verbs.h
··· 87 87 IB_USER_VERBS_CMD_CLOSE_XRCD, 88 88 IB_USER_VERBS_CMD_CREATE_XSRQ, 89 89 IB_USER_VERBS_CMD_OPEN_QP, 90 - #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 91 - IB_USER_VERBS_CMD_CREATE_FLOW = IB_USER_VERBS_CMD_THRESHOLD, 92 - IB_USER_VERBS_CMD_DESTROY_FLOW 93 - #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 94 90 }; 91 + 92 + #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 93 + enum { 94 + IB_USER_VERBS_EX_CMD_CREATE_FLOW = IB_USER_VERBS_CMD_THRESHOLD, 95 + IB_USER_VERBS_EX_CMD_DESTROY_FLOW 96 + }; 97 + #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */ 95 98 96 99 /* 97 100 * Make sure that all structs defined in this file remain laid out so ··· 125 122 * the rest of the command struct based on these value. 126 123 */ 127 124 125 + #define IB_USER_VERBS_CMD_COMMAND_MASK 0xff 126 + #define IB_USER_VERBS_CMD_FLAGS_MASK 0xff000000u 127 + #define IB_USER_VERBS_CMD_FLAGS_SHIFT 24 128 + 129 + #define IB_USER_VERBS_CMD_FLAG_EXTENDED 0x80 130 + 128 131 struct ib_uverbs_cmd_hdr { 129 132 __u32 command; 130 133 __u16 in_words; ··· 138 129 }; 139 130 140 131 #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING 141 - struct ib_uverbs_cmd_hdr_ex { 142 - __u32 command; 143 - __u16 in_words; 144 - __u16 out_words; 132 + struct ib_uverbs_ex_cmd_hdr { 133 + __u64 response; 145 134 __u16 provider_in_words; 146 135 __u16 provider_out_words; 147 136 __u32 cmd_hdr_reserved; ··· 789 782 790 783 struct ib_uverbs_create_flow { 791 784 __u32 comp_mask; 792 - __u32 reserved; 793 - __u64 response; 794 785 __u32 qp_handle; 795 786 struct ib_uverbs_flow_attr flow_attr; 796 787 };