devlink: Introduce cpu_affinity command
Subsystems (6)
cover devlink: Introduce cpu_affinity command
Currently a user can only configure the IRQ CPU affinity of a device via the global /proc/irq/../smp_afinity interface, however this interface changes the affinity globally across all subsystems connected to the device.
Historically, this API is useful for single function devices since, generally speaking, the queue structure created on top of the IRQ vectors is predictable enough that this control is usable.
However, with complex multi-subsystem devices, like mlx5, the assignment of queues at every layer throughout the software stack is complex and there are multiple queues, each for different usage, over the same IRQ. Hence, a simple fiddling of the base IRQ is no longer effective.
As an example mlx5 SF's can share MSI-X IRQ between themselves, which means that currently user doesn't have control over which SF to use which CPU set. Hence, an application and IRQ can run on different CPUs, which leads to lower performance, as shown in the bellow table.
application=netperf, SF-IRQ channel affinity latecy(usec)
(lower is better)
cpu=0 (numa=0) cpu={0} cpu={0} 14.417
cpu=8 (numa=0) cpu={0} cpu={0} 15.114 (+5%)
cpu=1 (numa=1) cpu={0} cpu={0} 17.784 (+30%)This series is a start at resolving this problem by inverting the control of the affinities. Instead of having the user go around behind the driver and adjusting the IRQs the driver already created we want to have the user tell the software layer what CPUs to use and the software layer will manage this. The suggested command will then trickle down to the PCI driver which will create/share MSI-X IRQs and resources to achieve it. In the mlx5 SF example the involved software components would be devlink, rdma, vdpa and netdev.
This series introduces a devlink control that assigns a CPU set to the cross-subsystem mlx5_core PCI function device. This can be used either on PF, VF or SF and restricts all the software layers above it to the given CPU set.
For specified CPU, SF either uses an existing IRQ affiliated to the CPU or a new IRQ available from the device. For example if user gives affinity 3 (11 in binary), SF will create driver internal required completion EQ, attached to these specific CPU's IRQ.
If SF is already fully probed, devlink reload is required for cpu_affinity to take effect.
The following command sets the affinity of mlx5 PF/VF/SF. devlink command structure:
$ devlink dev param set auxiliary/mlx5_core.sf.4 name cpu_affinity value \
[cpu_bitmask] cmode driverinitApplications that want to restrict a SF or VF HW to a CPU set, for instance container workloads, can make use of this API to easily achieve it.
Shay Drory (4):
net netlink: Introduce NLA_BITFIELD type
devlink: Add support for NLA_BITFIELD for devlink param
devlink: Add new cpu_affinity generic device param net/mlx5: Support cpu_affinity devlink dev param
.../networking/devlink/devlink-params.rst | 5 + Documentation/networking/devlink/mlx5.rst | 3 + .../net/ethernet/mellanox/mlx5/core/devlink.c | 123 +++++++++++++++ .../net/ethernet/mellanox/mlx5/core/devlink.h | 2 + drivers/net/ethernet/mellanox/mlx5/core/eq.c | 39 +++++ .../ethernet/mellanox/mlx5/core/mlx5_core.h | 2 + .../ethernet/mellanox/mlx5/core/mlx5_irq.h | 5 +- .../net/ethernet/mellanox/mlx5/core/pci_irq.c | 85 +++++++++- include/net/devlink.h | 22 +++ include/net/netlink.h | 30 ++++ include/uapi/linux/netlink.h | 10 ++ lib/nlattr.c | 145 +++++++++++++++++- net/core/devlink.c | 143 +++++++++++++++-- net/netlink/policy.c | 4 + 14 files changed, 594 insertions(+), 24 deletions(-)
Signature · 1 line
2.21.3
PATCH 1/4 net netlink: Introduce NLA_BITFIELD type
Generic bitfield attribute content sent to the kernel by user. With this netlink attr type the user can either set or unset a bitmap in the kernel.
This attribute is an extension (dynamic array) of NLA_BITFIELD32, and have similar checks and policies.
--- include/net/netlink.h | 30 ++++++++ include/uapi/linux/netlink.h | 10 +++ lib/nlattr.c | 145 ++++++++++++++++++++++++++++++++++- net/netlink/policy.c | 4 + 4 files changed, 185 insertions(+), 4 deletions(-)
include/net/netlink.h +30 -0
@@ -180,6 +180,7 @@ enum {NLA_S32 ,NLA_S64 ,NLA_BITFIELD32 , +NLA_BITFIELD ,NLA_REJECT , __NLA_TYPE_MAX, };
@@ -235,12 +236,16 @@ enum nla_policy_validation {* given type fits, using it verifies minimum length * just like "All other" * NLA_BITFIELD32 Unused +* NLA_BITFIELD Maximum length of attribute payload * NLA_REJECT Unused * All other Minimum length of attribute payload * * Meaning of validation union: * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and * `bitfield32_valid' is the u32 value of valid flags +* NLA_BITFIELD This is a dynamic array of 32-bit bitmap/bitselector +* attribute and `arr_bitfield32_valid' is the u32 +* values array of valid flags. * NLA_REJECT This attribute is always rejected and `reject_message' * may point to a string to report as the error instead * of the generic one in extended ACK.
@@ -318,6 +323,7 @@ struct nla_policy {u16 len;union {const u32 bitfield32_valid; +const u32 *arr_bitfield32_valid;const u32 mask;const char *reject_message;const struct nla_policy *nested_policy;
@@ -363,6 +369,8 @@ struct nla_policy { _NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)#define NLA_POLICY_BITFIELD32 (valid) \ { .type = NLA_BITFIELD32, .bitfield32_valid = valid } +#define NLA_POLICY_BITFIELD (valid, size) \ + { .type = NLA_BITFIELD, .arr_bitfield32_valid = valid, .len = size }#define __NLA_IS_UINT_TYPE (tp) \ (tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 || tp == NLA_U64)
@@ -1545,6 +1553,19 @@ static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype,return nla_put (skb, attrtype,sizeof (tmp), &tmp); } +/** +* nla_put_bitfield - Add a bitfield netlink attribute to a socket buffer +* @skb: socket buffer to add attribute to +* @attrtype: attribute type +* @bitfield: bitfield +*/ +static inline int nla_put_bitfield (struct sk_buff *skb,int attrtype, +const struct nla_bitfield *bitfield) +{ +return nla_put (skb, attrtype, bitfield->size *sizeof (struct nla_bitfield32 ) + +sizeof (*bitfield), bitfield); +} +/** * nla_get_u32 - return payload of u32 attribute * @nla: u32 netlink attribute
@@ -1738,6 +1759,15 @@ static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)return tmp; } +struct nla_bitfield *nla_bitfield_alloc (__u64 nbits); +void nla_bitfield_free (struct nla_bitfield *bitfield); +void nla_bitfield_to_bitmap (unsigned long *bitmap, +struct nla_bitfield *bitfield); +void nla_bitfield_from_bitmap (struct nla_bitfield *bitfield, +unsigned long *bitmap,__u64 bitmap_nbits); +bool nla_bitfield_len_is_valid (struct nla_bitfield *bitfield,size_t user_len); +bool nla_bitfield_nbits_valid (struct nla_bitfield *bitfield,size_t nbits); +/** * nla_memdup - duplicate attribute memory (kmemdup) * @src: netlink attribute to duplicate from
include/uapi/linux/netlink.h +10 -0
@@ -252,6 +252,14 @@ struct nla_bitfield32 {__u32 selector; }; +/* Generic bitmap attribute content sent to the kernel. +* The size is the number of elements in the array. +*/ +struct nla_bitfield { +__u64 size; +struct nla_bitfield32 data[0 ]; +}; +/* * policy descriptions - it's specific to each family how this is used * Normally, it should be retrieved via a dump inside another attribute
@@ -283,6 +291,7 @@ struct nla_bitfield32 {* entry has attributes again, the policy for those inner ones * and the corresponding maxtype may be specified. * @NL_ATTR_TYPE_BITFIELD32: &struct nla_bitfield32 attribute +* @NL_ATTR_TYPE_BITFIELD: &struct nla_bitfield attribute */ enum netlink_attribute_type {NL_ATTR_TYPE_INVALID ,
@@ -307,6 +316,7 @@ enum netlink_attribute_type {NL_ATTR_TYPE_NESTED_ARRAY ,NL_ATTR_TYPE_BITFIELD32 , + NL_ATTR_TYPE_BITFIELD, };/**
lib/nlattr.c +141 -4
170 lines not on this page · raw message
net/netlink/policy.c +4 -0
@@ -227,6 +227,7 @@ int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt)case NLA_STRING :case NLA_NUL_STRING :case NLA_BINARY : +case NLA_BITFIELD :/* maximum is common, u32 min-length/max-length */ return common +2 *nla_attr_size (sizeof (u32));case NLA_FLAG :
@@ -338,11 +339,14 @@ __netlink_policy_dump_write_attr(struct netlink_policy_dump_state *state,break ;case NLA_STRING :case NLA_NUL_STRING : +case NLA_BITFIELD :case NLA_BINARY :if (pt->type ==NLA_STRING ) type =NL_ATTR_TYPE_STRING ;else if (pt->type ==NLA_NUL_STRING ) type =NL_ATTR_TYPE_NUL_STRING ; +else if (pt->type ==NLA_BITFIELD ) + type =NL_ATTR_TYPE_BITFIELD ;else type =NL_ATTR_TYPE_BINARY ;
Signature · 1 line
2.21.3
PATCH 2/4 devlink: Add support for NLA_BITFIELD for devlink param
In reply to Shay DroryAdd devlink support for param of type NLA_BITFIELD. kernel user need to provide a bitmap to devlink.
--- include/net/devlink.h | 18 ++++++ net/core/devlink.c | 138 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 139 insertions(+), 17 deletions(-)
include/net/devlink.h +18 -0
@@ -372,6 +372,7 @@ enum devlink_param_type {DEVLINK_PARAM_TYPE_U32 ,DEVLINK_PARAM_TYPE_STRING ,DEVLINK_PARAM_TYPE_BOOL , + DEVLINK_PARAM_TYPE_BITFIELD, }; union devlink_param_value {
@@ -380,6 +381,7 @@ union devlink_param_value {u32 vu32;char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE];bool vbool; +unsigned long *vbitmap; };struct devlink_param_gset_ctx {
@@ -412,6 +414,8 @@ struct devlink_flash_notify {* @generic: indicates if the parameter is generic or driver specific * @type: parameter type * @supported_cmodes: bitmap of supported configuration modes +* @nbits: number of bits this param need to use, if this param is +* of dynamic len. * @get: get parameter value, used for runtime and permanent * configuration modes * @set: set parameter value, used for runtime and permanent
@@ -427,6 +431,7 @@ struct devlink_param {bool generic;enum devlink_param_type type;unsigned long supported_cmodes; +u64 nbits;int (*get)(struct devlink *devlink,u32 id,struct devlink_param_gset_ctx *ctx); int (*set)(struct devlink *devlink, u32 id,
@@ -542,6 +547,19 @@ enum devlink_param_generic_id { .validate = _validate, \ } +#define DEVLINK_PARAM_DYNAMIC_GENERIC (_id, _cmodes, _get, _set, _validate, _nbits)\ +{ \ + .id = DEVLINK_PARAM_GENERIC_ID_##_id, \ + .name = DEVLINK_PARAM_GENERIC_##_id##_NAME, \ + .type = DEVLINK_PARAM_GENERIC_##_id##_TYPE, \ + .generic = true, \ + .supported_cmodes = _cmodes, \ + .nbits = _nbits, \ + .get = _get, \ + .set = _set, \ + .validate = _validate, \ +} +/* Part number, identifier of board design */ #define DEVLINK_INFO_VERSION_GENERIC_BOARD_ID "board.id"/* Revision of board design */
net/core/devlink.c +121 -17
276 lines not on this page · raw message
Signature · 1 line
2.21.3
PATCH 3/4 devlink: Add new cpu_affinity generic device param
In reply to Shay DroryAdd new device generic parameter to configure device affinity.
A user who wishes to customize the affinity of the device can do it using below example.
$ devlink dev param set auxiliary/mlx5_core.sf.4 name cpu_affinity \
value [cpu_bitmask] cmode driverinit
$ devlink dev reload pci/0000:06:00.0At this point devlink instance will use the customize affinity.
--- Documentation/networking/devlink/devlink-params.rst | 5 +++++ include/net/devlink.h | 4 ++++ net/core/devlink.c | 5 +++++ 3 files changed, 14 insertions(+)
Documentation/networking/devlink/devlink-params.rst +5 -0
@@ -137,3 +137,8 @@ own name. * - ``event_eq_size`` - u32 - Control the size of asynchronous control events EQ. + * - ``cpu_affinity`` + - Bitfield + - control the cpu affinity of the device. user is able to change cpu + affinity also via procfs interface (/proc/irq/\*/smp_affinity). This will + overwrite the devlink setting.
include/net/devlink.h +4 -0
@@ -466,6 +466,7 @@ enum devlink_param_generic_id {DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP ,DEVLINK_PARAM_GENERIC_ID_IO_EQ_SIZE ,DEVLINK_PARAM_GENERIC_ID_EVENT_EQ_SIZE , +DEVLINK_PARAM_GENERIC_ID_CPU_AFFINITY ,/* add new param generic ids above here*/ __DEVLINK_PARAM_GENERIC_ID_MAX,
@@ -524,6 +525,9 @@ enum devlink_param_generic_id {#define DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME "event_eq_size"#define DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE DEVLINK_PARAM_TYPE_U32 +#define DEVLINK_PARAM_GENERIC_CPU_AFFINITY_NAME "cpu_affinity" +#define DEVLINK_PARAM_GENERIC_CPU_AFFINITY_TYPE DEVLINK_PARAM_TYPE_BITFIELD +#define DEVLINK_PARAM_GENERIC (_id, _cmodes, _get, _set, _validate) \ { \ .id = DEVLINK_PARAM_GENERIC_ID_##_id, \
net/core/devlink.c +5 -0
@@ -4477,6 +4477,11 @@ static const struct devlink_param devlink_param_generic[] = { .name = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME, .type = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE, }, + { + .id = DEVLINK_PARAM_GENERIC_ID_CPU_AFFINITY, + .name = DEVLINK_PARAM_GENERIC_CPU_AFFINITY_NAME, + .type = DEVLINK_PARAM_GENERIC_CPU_AFFINITY_TYPE, + }, };static int devlink_param_generic_verify (const struct devlink_param *param)
Signature · 1 line
2.21.3
PATCH 4/4 net/mlx5: Support cpu_affinity devlink dev param
In reply to Shay DroryEnable users to control the affinity of PCI Function. The default value is the affinity assigned by kernel for the PCI Function.
The runtime value shows the current affinity, the driverinit value is used in order to set a new affinity on the next driver reload. Setting empty affinity means kernel default policy.
Example:
- Show the current affinity.
$ devlink dev param show auxiliary/mlx5_core.sf.4 name cpu_affinity
name cpu_affinity type driver-specific
values:
cmode runtime value ff
cmode driverinit value 0- Set affinity to 3 (cpu 0 and cpu 1).
$ devlink dev param set auxiliary/mlx5_core.sf.4 name cpu_affinity \
value 3 cmode driverinitThen run devlink reload command to apply the new value.
$ devlink dev reload auxiliary/mlx5_core.sf.4
--- Documentation/networking/devlink/mlx5.rst | 3 + .../net/ethernet/mellanox/mlx5/core/devlink.c | 123 ++++++++++++++++++ .../net/ethernet/mellanox/mlx5/core/devlink.h | 2 + drivers/net/ethernet/mellanox/mlx5/core/eq.c | 39 ++++++ .../ethernet/mellanox/mlx5/core/mlx5_core.h | 2 + .../ethernet/mellanox/mlx5/core/mlx5_irq.h | 5 +- .../net/ethernet/mellanox/mlx5/core/pci_irq.c | 85 +++++++++++- 7 files changed, 256 insertions(+), 3 deletions(-)
Documentation/networking/devlink/mlx5.rst +3 -0
@@ -27,6 +27,9 @@ Parameters * - ``max_macs`` - driverinit - The range is between 1 and 2^31. Only power of 2 values are supported. + * - ``cpu_affinity`` + - driverinit | runtime + - empty affinity (0) means kernel assign the affinity The ``mlx5`` driver also implements the following driver-specific parameters.
drivers/net/ethernet/mellanox/mlx5/core/devlink.c +123 -0
153 lines not on this page · raw message
drivers/net/ethernet/mellanox/mlx5/core/devlink.h +2 -0
@@ -6,6 +6,8 @@#include <net/devlink.h> +#define MLX5_CPU_AFFINITY_MAX_LEN (NR_CPUS) +enum mlx5_devlink_param_id {MLX5_DEVLINK_PARAM_ID_BASE =DEVLINK_PARAM_GENERIC_ID_MAX ,MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE ,
drivers/net/ethernet/mellanox/mlx5/core/eq.c +39 -0
@@ -794,6 +794,30 @@ void mlx5_eq_update_ci(struct mlx5_eq *eq, u32 cc, bool arm) }EXPORT_SYMBOL (mlx5_eq_update_ci); +static int comp_irqs_request_by_cpu_affinity (struct mlx5_core_dev *dev) +{ +struct mlx5_eq_table *table = dev->priv.eq_table; +struct devlink *devlink =priv_to_devlink (dev); +union devlink_param_value val; +cpumask_var_t user_mask; +int ret; + +if (!zalloc_cpumask_var (&user_mask,GFP_KERNEL )) +return -ENOMEM ; + + val.vbitmap =cpumask_bits (user_mask); + ret =devlink_param_driverinit_value_get (devlink, +DEVLINK_PARAM_GENERIC_ID_CPU_AFFINITY , + &val); +if (ret) + goto out; + + ret =mlx5_irqs_request_mask (dev, table->comp_irqs, user_mask); +out: +free_cpumask_var (user_mask); +return ret; +} +static void comp_irqs_release (struct mlx5_core_dev *dev) {struct mlx5_eq_table *table = dev->priv.eq_table;
@@ -817,6 +841,11 @@ static int comp_irqs_request(struct mlx5_core_dev *dev) table->comp_irqs =kcalloc (ncomp_eqs,sizeof (*table->comp_irqs),GFP_KERNEL );if (!table->comp_irqs)return -ENOMEM ; + + ret =comp_irqs_request_by_cpu_affinity (dev); +if (ret >0 ) +return ret; +mlx5_core_dbg (dev,"failed to get param cpu_affinity. use default policy\n" );if (mlx5_core_is_sf (dev)) { ret =mlx5_irq_affinity_irqs_request_auto (dev, ncomp_eqs, table->comp_irqs);if (ret <0 )
@@ -987,6 +1016,16 @@ mlx5_comp_irq_get_affinity_mask(struct mlx5_core_dev *dev, int vector) }EXPORT_SYMBOL (mlx5_comp_irq_get_affinity_mask); +void mlx5_core_affinity_get (struct mlx5_core_dev *dev,struct cpumask *dev_mask) +{ +struct mlx5_eq_table *table = dev->priv.eq_table; +struct mlx5_eq_comp *eq, *n; + +list_for_each_entry_safe (eq, n, &table->comp_eqs_list, list) +cpumask_or (dev_mask, dev_mask, +mlx5_irq_get_affinity_mask (eq->core.irq)); +} +#ifdef CONFIG_RFS_ACCEL struct cpu_rmap *mlx5_eq_table_get_rmap (struct mlx5_core_dev *dev) {
drivers/net/ethernet/mellanox/mlx5/core/mlx5_core.h +2 -0
@@ -307,4 +307,6 @@ bool mlx5_rdma_supported(struct mlx5_core_dev *dev);bool mlx5_vnet_supported (struct mlx5_core_dev *dev);bool mlx5_same_hw_devs (struct mlx5_core_dev *dev,struct mlx5_core_dev *peer_dev); +void mlx5_core_affinity_get (struct mlx5_core_dev *dev,struct cpumask *dev_mask); +#endif /* __MLX5_CORE_H__ */
drivers/net/ethernet/mellanox/mlx5/core/mlx5_irq.h +4 -1
@@ -16,6 +16,7 @@ int mlx5_irq_table_create(struct mlx5_core_dev *dev);void mlx5_irq_table_destroy (struct mlx5_core_dev *dev);int mlx5_irq_table_get_num_comp (struct mlx5_irq_table *table);int mlx5_irq_table_get_sfs_vec (struct mlx5_irq_table *table); +bool mlx5_irq_table_have_dedicated_sfs_irqs (struct mlx5_irq_table *table);struct mlx5_irq_table *mlx5_irq_table_get (struct mlx5_core_dev *dev); int mlx5_set_msix_vec_count(struct mlx5_core_dev *dev, int devfn,
@@ -25,10 +26,12 @@ int mlx5_get_default_msix_vec_count(struct mlx5_core_dev *dev, int num_vfs);struct mlx5_irq *mlx5_ctrl_irq_request (struct mlx5_core_dev *dev);void mlx5_ctrl_irq_release (struct mlx5_irq *ctrl_irq);struct mlx5_irq *mlx5_irq_request (struct mlx5_core_dev *dev,u16 vecidx, -struct cpumask *affinity); +const struct cpumask *affinity);int mlx5_irqs_request_vectors (struct mlx5_core_dev *dev,u16 *cpus,int nirqs,struct mlx5_irq **irqs);void mlx5_irqs_release_vectors (struct mlx5_irq **irqs,int nirqs); +int mlx5_irqs_request_mask (struct mlx5_core_dev *dev,struct mlx5_irq **irqs, +struct cpumask *irqs_req_mask);int mlx5_irq_attach_nb (struct mlx5_irq *irq,struct notifier_block *nb);int mlx5_irq_detach_nb (struct mlx5_irq *irq,struct notifier_block *nb);struct cpumask *mlx5_irq_get_affinity_mask (struct mlx5_irq *irq);
drivers/net/ethernet/mellanox/mlx5/core/pci_irq.c +83 -2
109 lines not on this page · raw message
Signature · 1 line
2.21.3